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.

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

-----Original Message-----
From: sympy@googlegroups.com <sympy@googlegroups.com> On Behalf Of Oscar 
Benjamin
Sent: Friday, December 27, 2019 2:00 PM
To: sympy <sympy@googlegroups.com>
Subject: Re: [sympy] simplify loses derivatives

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

On Fri, 27 Dec 2019 at 09:33, <thomassli...@gmail.com> wrote:
>
> 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)
> thomassli...@gmail.com
> Frohnloher Str. 6a
> 81475 Muenchen
> Germany
> Tel. +49(89)74575075
>
> -----Original Message-----
> From: sympy@googlegroups.com <sympy@googlegroups.com> On Behalf Of 
> Oscar Benjamin
> Sent: Thursday, December 26, 2019 10:12 PM
> To: sympy <sympy@googlegroups.com>
> Subject: Re: [sympy] simplify loses derivatives
>
> 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)
>
> On Thu, 26 Dec 2019 at 20:05, <thomassli...@gmail.com> wrote:
> >
> > 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)
> >
> > thomassli...@gmail.com
> >
> > Frohnloher Str. 6a
> > 81475 Muenchen
> > Germany
> > Tel. +49(89)74575075
> >
> >
> >
> > --
> > 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+unsubscr...@googlegroups.com.
> > To view this discussion on the web visit 
> > https://groups.google.com/d/msgid/sympy/001301d5bc27%24c66b39c0%245341ad40%24%40gmail.com.
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/CAHVvXxQ8Lv1kx3M06tCU-iiOJTQbSt17y4dXUu4_U1Ju6s7DUA%40mail.gmail.com.
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/000c01d5bc98%24ba958670%242fc09350%24%40gmail.com.

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxThsWOF0qd90FCU84jehh-ziPnoXPJDBqBARrsGei_eaA%40mail.gmail.com.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/001701d5bccb%24712857f0%24537907d0%24%40gmail.com.

Reply via email to