I was using the sympy to verify some hand calculation, the version was anaconda and
import sympy as sp
sp.__version__
'1.11.1'
on Windows 11 Jupyter notebook.
The code ran as follow
from sympy import Function
eta = Function('eta')
from sympy import *
s,c,h=symbols('s c h',real=True)
A,B =symbols("A B")
q=exp(-2*pi*exp(s))
chi_0=eta(s)**(-1) * q**(0-(c-1)/24)*(1-q)
The following code
print( chi_0.diff(s).subs((eta(s)**(-1)).diff(s),-B) .subs(eta(s)**(-1),A) .subs(s,0) )
produced the correct results
print( chi_0.diff(s).subs((eta(s)**(-1)).diff(s),-B) .subs(eta(s)**(-1),A) .subs(s,0) )
-2*pi*A*(1/24 - c/24)*(1 - exp(-2*pi))*exp(-2*pi*(1/24 - c/24)) + 2*pi*A*exp(-2*pi)*exp(-2*pi*(1/24 - c/24)) - B*(1 - exp(-2*pi))*exp(-2*pi*(1/24 - c/24))
Notice the sign in the term -2*pi*A*(1/24 - c/24)*(1 - exp(-2*pi))*exp(-2*pi*(1/24 - c/24))
. However, when I tried to cancel the *exp(-2*pi*(1/24 - c/24))
,
#bug
print( (( chi_0.diff(s).subs((eta(s)**(-1)).diff(s),-B) .subs(eta(s)**(-1),A) .subs(s,0) )/exp(pi*(c-1)/12) ).simplify() )
(-pi*A*(1 - exp(2*pi))*(c - 1)/12 + 2*pi*A + B*(1 - exp(2*pi)))*exp(-2*pi)
Notice that two of the terms (-pi*A*(1 - exp(2*pi))*(c - 1)/12
and B*(1 - exp(2*pi)))*exp(-2*pi)
obtained the wrong sign, while the sign of the 2*pi*A
remained correct.
Though an expansion statement could fix the result,
print( (( chi_0.diff(s).subs((eta(s)**(-1)).diff(s),-B) .subs(eta(s)**(-1),A) .subs(s,0) ).expand()/exp(pi*(c-1)/12) ).simplify() )
-pi*A*c*exp(-2*pi)/12 + pi*A*c/12 - pi*A/12 + 25*pi*A*exp(-2*pi)/12 - B + B*exp(-2*pi)
it was not useful.
How did this happen?
You can see the behavior with a simpler example:
>>> from sympy.abc import x, y, z
>>> from sympy import signsimp
>>> a = (1-x)*(y-1)*(1-z)/-1; a
-(1 - x)*(1 - z)*(y - 1)
>>> b = signsimp(_); b # make expression canonical wrt sign
-(x - 1)*(y - 1)*(z - 1)
You can see that two things happened: order of factors was changed (but that's ok with commutative factors) and the signs of two factors was changed (and that preserves the overall sign).
If you have doubts about whether two exprssions are equal or not you can try the following (which will return None if it can't tell):
>>> a.equals(b)
True
signsimp
is part of the simplification process and might be one of the reasons that signs within factors changed (in a mathematically valid way).