#!/usr/bin/env python
from sympy import *
X = Symbol('X', real=True)
f = Function('f', real=True)(X)
g = Function('g', real=True)(X)
print diff (abs(f-g), X)
((re(f(X)) - re(g(X)))*(re(Derivative(f(X), X)) - re(Derivative(g(X), X))) + (im(f(X)) - im(g(X)))*(im(Derivative(f(X), X)) - im(Derivative(g(X), X))))/Abs(f(X) - g(X))
print diff (abs(f-g), X).subs([(im(f),0),
(im(g),0),
(re(f),f),
(re(g),g),
(re(Derivative(f, X)), Derivative(f, X)),
(re(Derivative(g, X)), Derivative(g, X))])
(f(X) - g(X))*(Derivative(f(X), X) - Derivative(g(X), X))/Abs(f(X) - g(X))
and what you found seems like a bug. SymPy should be able to use the
fact that "f" is real.
The problem is that assumptions on Function don't do anything
(https://github.com/sympy/sympy/issues/6494). If you want to create a
Function with assumptions, you'll need to subclass Function
explicitly, like
class f(Function):
is_real = True
#!/usr/bin/env python
from sympy import *
class real_function(Function):
is_real=True
X = Symbol('X', real=True)
f = real_function('f')(X)
g = real_function('g')(X)
print diff (abs(f-g), X)
Traceback (most recent call last):
File "testcase.py", line 10, in <module>
f = real_function('f')(X)
TypeError: 'real_function' object is not callable
which I don't understand how to fix.
On Tue, Feb 24, 2015 at 3:51 PM, John Peterson <jwpet...@gmail.com> wrote:
>
>
Try this:
In [1]: class f(Function):
...: is_real=True
...:
In [2]: class g(Function):
...: is_real=True
...:
In [3]: X = Symbol('X', real=True)
In [4]: diff(abs(f(X)-g(X)), X)
Out[4]:
⎛d d ⎞
⎜──(f(X)) - ──(g(X))⎟⋅sign(f(X) - g(X))
⎝dX dX ⎠