Reputation: 411
I am using Sympy to calculate the derivative of a complicated potential. I have defined x, y, D0, e, C, k, d, b as Sympy Symbols. Then I go on to do the following definitions:
import sympy as sm
x, y, D0, e, C, k, d, b = sm.symbols("x, y, D0, e, C, k, d, b")
phi = sm.atan2(y,x)
D = D0 + e*D0*sm.sin(k*phi)
rho = sm.sqrt(x**2 + y**2)
U = (2**b)/((2*D-d)**b)*(D - rho)**b
The symbol "U" stands for the 2D potential.
Now when I differentiate this vs. x using:
sm.simplify(U.diff(x))
I get an extremely long answer:
As you can see, in the answer there is explicitly the full expression for e.g. D
: D0 + e*D0*sin(k*phi)
. Also, instead of sin(phi) I get sin(atan2(x,y) and the same happens for all of my defined expressions.
Is there a way for the result to automatically show my definitions instead of the long versions, without having the need to use the subs
method for every single user-defined variable?
E.g. instead of D0 + e*D0*sin(k*phi)
Sympy automatically uses the symbol D
?
Upvotes: 1
Views: 373
Reputation: 91490
When you write phi = sm.atan2(y,x)
, this assigns a Python variable to the result of atan2(y, x)
. If you want phi
to be a symbol, you have to define it that way
phi = symbols('phi')
and then substitute phi
in as atan2(y, x)
later.
U.subs(phi, atan2(y, x))
This is worth reading regarding this.
Upvotes: 1