简体   繁体   English

SymPy 在区分隐式 function 时没有简化“足够”

[英]SymPy not simplifying "enough" while differentiating an implicit function

I am trying to find the 1st and 2nd derivatives of the implicit function y = f(x)我试图找到隐式 function y = f(x)的一阶和二阶导数
which is defined by the equation: exp(sin(x)) - x * exp(sin(y)) = 0由以下等式定义: exp(sin(x)) - x * exp(sin(y)) = 0

SymPy calculates the 1st derivative and gives this answer: SymPy 计算一阶导数并给出以下答案:

图1

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))使用x = exp(sin(x)-sin(y))的事实

The answer given for the 2nd derivative is also quite complicated.对二阶导数给出的答案也相当复杂。

图2

Of course the second derivative can also be simplified当然二阶导数也可以化简
quite a lot using the same fact x = exp(sin(x)-sin(y)) .很多使用相同的事实x = exp(sin(x)-sin(y))

How can I make/force SymPy apply these additional simplifications?我怎样才能使/强制 SymPy 应用这些额外的简化?
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:这里的二阶导数为:

p30

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:在第一个示例中,您可以选择一个项来消除并解决F

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 :在第二个示例中,您可以调用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)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM