You probably just want the derivative of Abs
to be sign
. SymPy does do this, but only if it can deduce that the argument to the absolute value is real, which it can't in this case (even if x
is real).
You can make your own custom version of Abs
that always uses sign
pretty easily by subclassing and overriding the _eval_derivative
method:
class MyAbs(Abs):
def _eval_derivative(self, x):
return Derivative(self.args[0], x, evaluate=True)*sign(conjugate(self.args[0]))
.
In [110]: x = Symbol('x')
In [111]: y = MyAbs(0.5-0.5*sqrt(1-4*x))
In [112]: deriv = y.diff(x)
In [113]: print(deriv)
1.0*sign(-0.5*conjugate(sqrt(-4*x + 1)) + 0.5)/sqrt(-4*x + 1)
In [114]: lambdify(x, deriv, 'numpy')(0.1)
Out[114]: 1.29099444874