how to tell sympy the first derivative of a function is real

114 views
Skip to first unread message

chaowen guo

unread,
Aug 11, 2015, 7:08:22 PM8/11/15
to sympy
Hi:

source code:
import sympy
x=sympy.symbols('x',real=True)
f=sympy.symbols('f',cls=sympy.Function)
f.is_real=True
a=sympy.Abs(f(x).diff(x)).diff(f(x).diff(x))
a

the output is:
(re(Derivative(f(x), x))*Subs(Derivative(re(_xi_2), _xi_2), (_xi_2,), (Derivative(f(x), x),)) + im(Derivative(f(x), x))*Subs(Derivative(im(_xi_2), _xi_2), (_xi_2,), (Derivative(f(x), x),)))/Abs(Derivative(f(x), x))

the imaginary part still exist. Obviously, sympy does not think f(x).diff(x) is real. So I try:
sympy.refine(a,sympy.Q.real(f(x).diff(x)))
but the output remains the same. So how to tell sympy that f(x).diff(x) is real?

More weird stuff is:
f(x).diff(f(x))
the output is 1

but:
sympy.Abs(f(x)).diff(f(x))
the output is:
f(x)*Subs(Derivative(re(_xi_2), _xi_2), (_xi_2,), (f(x),))/Abs(f(x))
why sympy is not smart enough to simplify it to f(x)/Abs(f(x))?

I use python3.4 and sympy 0.7.6

Aaron Meurer

unread,
Aug 11, 2015, 9:24:45 PM8/11/15
to sy...@googlegroups.com
It looks like some parts of the assumptions could be improved here. 

Probably the simplest way to get what you want is to blanket replace all instances of re() with the identity function and im() with 0, like

In [21]: print(a.replace(re, Id).replace(im, lambda x: 0))
Derivative(f(x), x)*Subs(Derivative(_xi_2, _xi_2), (_xi_2,), (Derivative(f(x), x),))/Abs(Derivative(f(x), x))

In [22]: print(a.replace(re, Id).replace(im, lambda x: 0).doit())
Derivative(f(x), x)/Abs(Derivative(f(x), x))

Aaron Meurer

--
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 post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/2deb519b-9cdd-4b2e-93d7-f3f252ff8fc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

chaowen guo

unread,
Aug 12, 2015, 9:20:21 PM8/12/15
to sympy
Thank you very much

Is there a way, like f.is_real, to tell sympy that f(x).diff(x) is real, so that print((f(x).diff(x)).is_real) will be true? Right now the output of print((f(x).diff(x)).is_real) is none.

Aaron Meurer

unread,
Aug 25, 2015, 4:11:24 PM8/25/15
to sy...@googlegroups.com
I don't think there is, at least not without some work (basically, creating a custom Derivative subclass and a custom Function subclass and setting the derivative to it). 

Really, Derivative ought to know that diff(f(x), x) is real if f(x) is real.  Or rather I suppose diff(f(x), x).is_imaginary should be False (the derivative could still be infinite or undefined, which would technically not be real).

Aaron Meurer

On Wed, Aug 12, 2015 at 8:20 PM, chaowen guo <gcwenk...@ttu.edu> wrote:
Thank you very much

Is there a way, like f.is_real, to tell sympy that f(x).diff(x) is real, so that print((f(x).diff(x)).is_real) will be true? Right now the output of print((f(x).diff(x)).is_real) is none.

--
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 post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages