I am trying to simplify
a rather complicated expression in sympy.
from sympy import *
xi, eta = symbols('xi eta')
expr = (sin(eta)*sin(xi) - 1)**2*(sin(eta)*sin(xi) + 1)**2/((cos(eta)**2*tan(xi)**2 + 1)*(cos(xi)**2*tan(eta)**2 + 1)*cos(eta)**2*cos(xi)**2)
expr
should simplify to 1
, but sympy fails to do so automatically. In order to help it a little, I need a change of variables:
X, Y = symbols('X Y')
xi = atan(X)
eta = atan(Y)
So xi
, and eta
are not symbols anymore. Now I need to re-parse expr
in order to change the variables. What I currently do is:
simplify(repr(expr).replace('xi',repr(xi)).replace('eta', repr(eta)))
This works. But it seems like a crude way of doing it. Is there any other way for sympy ro recognize that xi
and eta
are expressions and not symbols, when parsing repr(expr)
.
sympify
(or S
for short) will convert a string to an expression, and it has an option to make user-defined replacements while doing so. This toy example should help you:
>>> from sympy import S, cos
>>> from sympy.abc import x
S('x',dict(x=cos(x)))
cos(x)