simplify loses derivatives

41 views
Skip to first unread message

thomas...@gmail.com

unread,
Dec 26, 2019, 3:05:15 PM12/26/19
to sy...@googlegroups.com

It looks to me like “simplify” has lost all derivatives. As you might guess from the code snippet, this was working earlier, but I don’t know what broke it, and I have made some modifications since then. I am using Python 3.7 and SymPy 1.5.

 

Here is a code snippet:

eq27 = Eq(eq24.lhs + I*eq25.lhs, 0)

print('eq27 after creation:')

print(eq27)

print(latex(eq27))

eq28 = Eq(eq24.lhs - I*eq25.lhs, 0)

eq27 = eq27.simplify()

print('eq27 after simplify 1:')

print(eq27)

print(latex(eq27))

eq28 = eq28.simplify()

eq27 = eq27.subs(Derivative(s/2 + u/2, tau, 2), Derivative(s, tau, 2)/2 + Derivative(u, tau, 2)/2)

eq27 = eq27.subs(Derivative(I*s/2 - I*u/2, tau, 2), I*Derivative(s, tau, 2)/2 - I*Derivative(u, tau, 2)/2)

eq28 = eq28.subs(Derivative(s/2 + u/2, tau, 2), Derivative(s, tau, 2)/2 + Derivative(u, tau, 2)/2)

eq28 = eq28.subs(Derivative(I*s/2 - I*u/2, tau, 2), I*Derivative(s, tau, 2)/2 - I*Derivative(u, tau, 2)/2)

eq27 = eq27.subs(Derivative(s/2 + u/2, tau), Derivative(s, tau)/2+Derivative(u, tau)/2)

eq27 = eq27.subs(Derivative(I*s/2 - I*u/2, tau), I*Derivative(s, tau)/2-I*Derivative(u, tau)/2)

eq28 = eq28.subs(Derivative(s/2 + u/2, tau), Derivative(s, tau)/2+Derivative(u, tau)/2)

eq28 = eq28.subs(Derivative(I*s/2 - I*u/2, tau), I*Derivative(s, tau)/2-I*Derivative(u, tau)/2)

eq27 = eq27.simplify()

print('eq27 after simplify 2:')

print(eq27)

print(latex(eq27))

eq28 = eq28.simplify()

eq29 = eq26

eq29 = eq29.subs(-I*(-s + u)/2, I*s/2 - I*u/2)

eq29 = eq29.subs(Derivative(s/2 + u/2, tau), Derivative(s, tau)/2+Derivative(u, tau)/2)

eq29 = eq29.subs(Derivative(I*s/2 - I*u/2, tau), I*Derivative(s, tau)/2-I*Derivative(u, tau)/2)

eq29 = eq29.subs((I*Derivative(s, tau)/2 - I*Derivative(u, tau)/2)**2, -Derivative(s, tau)**2/4 + Derivative(s, tau)*Derivative(u, tau)/2 - Derivative(u, tau)**2/4)

eq29 = eq29.subs((Derivative(s, tau)/2 + Derivative(u, tau)/2)**2, Derivative(s, tau)**2/4 + Derivative(s, tau)*Derivative(u, tau)/2 + Derivative(u, tau)**2/4)

eq29 = eq29.simplify()

eq29 = eq29.subs(3*s**2/8 + 3*s*u/4 + 3*u**2/8, 3*(s + u )**2/8)

print('eq27:')

print(eq27)

print(latex(eq27))

print('eq28:')

print(eq28)

print(latex(eq28))

print('eq29:')

print(eq29)

print(latex(eq29))

 

…and here is a piece of the output:

eq24:

Eq(\nu**2*Derivative(x, (\tau, 2)) - 2*\nu*Derivative(y, \tau) - 3*x + x/r**3, 0)

\nu^{2} \frac{d^{2}}{d \tau^{2}} x - 2 \nu \frac{d}{d \tau} y - 3 x + \frac{x}{r^{3}} = 0

eq25:

Eq(\nu**2*Derivative(y, (\tau, 2)) + 2*\nu*Derivative(x, \tau) + y/r**3, 0)

\nu^{2} \frac{d^{2}}{d \tau^{2}} y + 2 \nu \frac{d}{d \tau} x + \frac{y}{r^{3}} = 0

eq26:

Eq(-\nu**2*Derivative(x, \tau)**2/2 - \nu**2*Derivative(y, \tau)**2/2 + 3*x**2/2 + 1/r, C)

- \frac{\nu^{2} \left(\frac{d}{d \tau} x\right)^{2}}{2} - \frac{\nu^{2} \left(\frac{d}{d \tau} y\right)^{2}}{2} + \frac{3 x^{2}}{2} + \frac{1}{r} = C

eq24:

Eq(\nu**2*Derivative(s/2 + u/2, (\tau, 2)) - 2*\nu*Derivative(I*(s - u)/2, \tau) - 3*s/2 - 3*u/2 + (s/2 + u/2)/(s*u)**(3/2), 0)

\nu^{2} \frac{\partial^{2}}{\partial \tau^{2}} \left(\frac{s}{2} + \frac{u}{2}\right) - 2 \nu \frac{\partial}{\partial \tau} \frac{i \left(s - u\right)}{2} - \frac{3 s}{2} - \frac{3 u}{2} + \frac{\frac{s}{2} + \frac{u}{2}}{\left(s u\right)^{\frac{3}{2}}} = 0

eq25:

Eq(\nu**2*Derivative(I*(s - u)/2, (\tau, 2)) + 2*\nu*Derivative(s/2 + u/2, \tau) + I*(s - u)/(2*(s*u)**(3/2)), 0)

\nu^{2} \frac{\partial^{2}}{\partial \tau^{2}} \frac{i \left(s - u\right)}{2} + 2 \nu \frac{\partial}{\partial \tau} \left(\frac{s}{2} + \frac{u}{2}\right) + \frac{i \left(s - u\right)}{2 \left(s u\right)^{\frac{3}{2}}} = 0

eq26:

Eq(-\nu**2*Derivative(I*(s - u)/2, \tau)**2/2 - \nu**2*Derivative(s/2 + u/2, \tau)**2/2 + 3*(s/2 + u/2)**2/2 + 1/sqrt(s*u), C)

- \frac{\nu^{2} \left(\frac{\partial}{\partial \tau} \frac{i \left(s - u\right)}{2}\right)^{2}}{2} - \frac{\nu^{2} \left(\frac{\partial}{\partial \tau} \left(\frac{s}{2} + \frac{u}{2}\right)\right)^{2}}{2} + \frac{3 \left(\frac{s}{2} + \frac{u}{2}\right)^{2}}{2} + \frac{1}{\sqrt{s u}} = C

Loaded '__main__'

Loaded 'runpy'

eq27 after creation:

Eq(\nu**2*Derivative(s/2 + u/2, (\tau, 2)) - 2*\nu*Derivative(I*(s - u)/2, \tau) - 3*s/2 - 3*u/2 + I*(\nu**2*Derivative(I*(s - u)/2, (\tau, 2)) + 2*\nu*Derivative(s/2 + u/2, \tau) + I*(s - u)/(2*(s*u)**(3/2))) + (s/2 + u/2)/(s*u)**(3/2), 0)

\nu^{2} \frac{\partial^{2}}{\partial \tau^{2}} \left(\frac{s}{2} + \frac{u}{2}\right) - 2 \nu \frac{\partial}{\partial \tau} \frac{i \left(s - u\right)}{2} - \frac{3 s}{2} - \frac{3 u}{2} + i \left(\nu^{2} \frac{\partial^{2}}{\partial \tau^{2}} \frac{i \left(s - u\right)}{2} + 2 \nu \frac{\partial}{\partial \tau} \left(\frac{s}{2} + \frac{u}{2}\right) + \frac{i \left(s - u\right)}{2 \left(s u\right)^{\frac{3}{2}}}\right) + \frac{\frac{s}{2} + \frac{u}{2}}{\left(s u\right)^{\frac{3}{2}}} = 0

eq27 after simplify 1:

Eq(3*s/2 + 3*u/2 - u/(s*u)**(3/2), 0)

\frac{3 s}{2} + \frac{3 u}{2} - \frac{u}{\left(s u\right)^{\frac{3}{2}}} = 0

eq27 after simplify 2:

Eq(3*s/2 + 3*u/2 - u/(s*u)**(3/2), 0)

\frac{3 s}{2} + \frac{3 u}{2} - \frac{u}{\left(s u\right)^{\frac{3}{2}}} = 0

eq27:

Eq(3*s/2 + 3*u/2 - u/(s*u)**(3/2), 0)

\frac{3 s}{2} + \frac{3 u}{2} - \frac{u}{\left(s u\right)^{\frac{3}{2}}} = 0

eq28:

Eq(3*s/2 - s/(s*u)**(3/2) + 3*u/2, 0)

\frac{3 s}{2} - \frac{s}{\left(s u\right)^{\frac{3}{2}}} + \frac{3 u}{2} = 0

eq29:

Eq(C, (3*sqrt(s*u)*(s + u)**2 + 8)/(8*sqrt(s*u)))

C = \frac{3 \sqrt{s u} \left(s + u\right)^{2} + 8}{8 \sqrt{s u}}

 

Finally, I have pasted the LaTeX output into a Word document to make them more readable. (I know that Jupyter is a great tool for this, but Word is a quick and easy way for me to do it.) In case someone doesn’t have Word, I have included a pdf version.

 

Tom

(Dr. Thomas S. Ligon)

thomas...@gmail.com

Frohnloher Str. 6a
81475 Muenchen
Germany
Tel. +49(89)74575075

 

Temp.docx
Temp.pdf

Oscar Benjamin

unread,
Dec 26, 2019, 4:12:11 PM12/26/19
to sympy
Can you simplify this a bit? That's a lot of code but presumably there
is a smaller part that shows the problem. Is this it?

In [4]: simplify(Derivative(sin(x), x))
Out[4]: cos(x)
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/001301d5bc27%24c66b39c0%245341ad40%24%40gmail.com.

thomas...@gmail.com

unread,
Dec 27, 2019, 4:33:47 AM12/27/19
to sy...@googlegroups.com
Hi Oscar,
thanks for the quick reply. Yes, I can simplify, but with a warning "Simplification is not a well-defined term". I have written a smaller script to reproduce the issue.

By the way, this is not urgent; I have achieved my first goal already (reproduce and understand the original paper), all calculations up to here can be done by hand, and I have learned the basics of how to use SymPy. Now, I have started with the hard part (extend the original work), and don't need the derivatives for that.

import sympy
from sympy import symbols, latex, Eq, Derivative
from sympy import I, simplify
nu = symbols('\\nu')
tau = symbols('\\tau')
v, u, s = symbols('v u s')
eqX1 = 2*nu*Derivative(I*(s - u)/2, tau)
print('eqX1:')
print(eqX1)
print(latex(eqX1))
eqX2 = eqX1.simplify()
print('eqX2 = simplification of eqX1:')
print(eqX2)
print(latex(eqX2))
# The rest can be ignored
eqTest1 = Eq(nu**2*Derivative(s/2 + u/2, (tau, 2)) - 2*nu*Derivative(I*(s - u)/2, tau) - 3*s/2 - 3*u/2 + I*(nu**2*Derivative(I*(s - u)/2, (tau, 2)) + 2*nu*Derivative(s/2 + u/2, tau) + I*(s - u)/(2*(s*u)**(3/2))) + (s/2 + u/2)/(s*u)**(3/2), 0)
print('eqTest1:')
print(eqTest1)
print(latex(eqTest1))
eqTest2 = eqTest1.simplify()
print('eqTest2 = simplification of eqTest1:')
print(eqTest2)
print(latex(eqTest2))

Tom
(Dr. Thomas S. Ligon)
thomas...@gmail.com
Frohnloher Str. 6a
81475 Muenchen
Germany
Tel. +49(89)74575075

To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxQ8Lv1kx3M06tCU-iiOJTQbSt17y4dXUu4_U1Ju6s7DUA%40mail.gmail.com.

David Bailey

unread,
Dec 27, 2019, 5:27:28 AM12/27/19
to sy...@googlegroups.com
On 27/12/2019 09:33, thomas...@gmail.com wrote:


Strangely enough, I get the following results:

>>> from sympy.abc import *
>>> Derivative(x,y)
Derivative(x, y)
>>> Derivative(x**2,y)
Derivative(x**2, y)
>>> Derivative(x**2,x)
Derivative(x**2, x)
>>> Derivative(sin(x),x)
Derivative(sin(x), x)
>>> simplify(Derivative(sin(x),x))
Derivative(sin(x), x)
>>> simplify(Derivative(sin(x), x))
Derivative(sin(x), x)
>>>

I.e. simplify did not evaluate the derivative at all. That was using Python 3.7 and sympy 1.5.

I tried this because my guess is that in the OP's original problem, the variables 's' and 'u' had been accidentally overwritten somewhere earlier in his sympy session- e.g.

s=0

u=0

Then the derivatives would obviously evaluate to zero.

David


Oscar Benjamin

unread,
Dec 27, 2019, 7:59:38 AM12/27/19
to sympy
Hi Thomas,

You need to declare what is a function of what in your expressions.
Unrelated symbols are assumed to have no relationship so that
differentiating one wrt another is like differentiating a constant:

In [1]: x = Symbol('x')

In [2]: y = Symbol('y')

In [3]: Derivative(x**2, x)
Out[3]:
d ⎛ 2⎞
──⎝x ⎠
dx

In [4]: Derivative(x**2, x).doit()
Out[4]: 2⋅x

In [5]: simplify(Derivative(x**2, x))
Out[5]: 2⋅x

In [6]: Derivative(y**2, x)
Out[6]:
d ⎛ 2⎞
──⎝y ⎠
dx

In [7]: Derivative(y**2, x).doit()
Out[7]: 0

In [8]: simplify(Derivative(y**2, x))
Out[8]: 0

If you want `Derivative` to understand that something depends on x
then it needs to be a function of x. You clarify this by using
Function rather than Symbol:

In [9]: f = Function('f')

In [10]: Derivative(f(x)**2, x)
Out[10]:
d ⎛ 2 ⎞
──⎝f (x)⎠
dx

In [11]: Derivative(f(x)**2, x).doit()
Out[11]:
d
2⋅f(x)⋅──(f(x))
dx

In [12]: simplify(Derivative(f(x)**2, x))
Out[12]:
d
2⋅f(x)⋅──(f(x))
dx

--
Oscar
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/000c01d5bc98%24ba958670%242fc09350%24%40gmail.com.

thomas...@gmail.com

unread,
Dec 27, 2019, 10:36:49 AM12/27/19
to sy...@googlegroups.com
Hi Oscar,

thanks! This is the solution, I need to use Function instead of Symbol. With that, things are working well, both in the main script and the little repro. I read about functions when I went through the tutorial, but somehow overlooked them here.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxThsWOF0qd90FCU84jehh-ziPnoXPJDBqBARrsGei_eaA%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages