I am trying to find the 1st and 2nd derivatives of the implicit function y = f(x)
which is defined by the equation: exp(sin(x)) - x * exp(sin(y)) = 0
SymPy calculates the 1st derivative and gives this answer:
But this expression can be written much simpler as:
(x * cos(x) - 1) / (x * cos(y))
using the fact that x = exp(sin(x)-sin(y))
The answer given for the 2nd derivative is also quite complicated.
Of course the second derivative can also be simplified
quite a lot using the same fact x = exp(sin(x)-sin(y))
.
How can I make/force SymPy apply these additional simplifications?
Is that possible even?
Here is my script.
#!/usr/bin/env python
# coding: utf-8
# ### Differentiating an implicit function using SymPy
# In[1]:
import sympy as sp
# In[2]:
sp.__version__
# In[3]:
sp.init_printing(use_latex='mathjax') # use pretty mathjax output
# In[4]:
sp.var('x y z')
F = sp.exp(sp.sin(x)) - x * sp.exp(sp.sin(y))
# In[5]:
f1 = sp.idiff( F, y, x ) # First derivative of y w.r.t. x
f1
# In[6]:
sp.simplify(f1)
# In[7]:
f2 = sp.idiff( F, y, x, 2) # Second derivative of y w.r.t. x
f2
sp.simplify(f2)
# In[ ]:
And also, here is an even simpler example which shows this undesired behavior.
#!/usr/bin/env python
# coding: utf-8
# ### Differentiating an implicit function using SymPy
# In[1]:
import sympy as sp
# In[2]:
sp.__version__
# In[3]:
sp.init_printing(use_latex='mathjax') # use pretty mathjax output
# In[4]:
sp.var('x y')
F = sp.ln(sp.sqrt(x**2 + y**2)) - sp.atan(y / x)
# In[5]:
f1 = sp.idiff( F, y, x ) # First derivative of y w.r.t. x
f1
# In[6]:
sp.simplify(f1)
# In[7]:
f2 = sp.idiff( F, y, x, 2) # Second derivative of y w.r.t. x
f2
sp.simplify(f2)
# In[ ]:
The second derivative here is given as:
This expression obviously can be simplified further even without using any special facts.
You can simplify the expressions yourself. In the first example you can just choose a term to eliminate and solve F
for that:
In [42]: F
Out[42]:
sin(y) sin(x)
- x⋅ℯ + ℯ
In [43]: solve(F, exp(sin(y)))
Out[43]:
⎡ sin(x)⎤
⎢ℯ ⎥
⎢───────⎥
⎣ x ⎦
In [44]: [esy] = solve(F, exp(sin(y)))
In [45]: f2.subs(exp(sin(y)), esy)
Out[45]:
2
x⋅sin(y)⋅cos (x) 2⋅sin(y)⋅cos(x) sin(y) 1
-x⋅sin(x) + ──────────────── - ─────────────── + ───────── + ─
2 2 2 x
cos (y) cos (y) x⋅cos (y)
──────────────────────────────────────────────────────────────
x⋅cos(y)
You can apply further simplification operations from there.
In the second example you can just call factor
:
In [47]: f2
Out[47]:
⎛ 2 2⎞
2⋅⎝x + y ⎠
─────────────────────────
3 2 2 3
x - 3⋅x ⋅y + 3⋅x⋅y - y
In [48]: factor(f2)
Out[48]:
⎛ 2 2⎞
2⋅⎝x + y ⎠
───────────
3
(x - y)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.