Is there a good reason to use Derivative
instead of diff
in the definition (and solution) of an ODE in Sympy? diff
seems to do the job just fine:
Derivative() or diff() in SymPy ODE?
2.1k Views Asked by Jörg J. Buchholz At
2
There are 2 best solutions below
0

The Derivative
object represents an unevaluated derivative. It will never evaluate, for example:
>>> Derivative(x**2, x)
Derivative(x**2, x)
diff
is a function which always tries to evaluate the derivative. If the derivative in question cannot be evaluated, it just returns an unevaluated Derivative
object.
>>> diff(x**2, x)
2*x
Since undefined functions are always things whose derivatives won't be evaluated, Derivative
and diff
are the same.
>>> diff(f(x), x)
Derivative(f(x), x)
>>> Derivative(f(x), x)
Derivative(f(x), x)
There's only a difference between the two in cases where the derivative can be evaluated. For ODEs, this means that it generally doesn't matter, except maybe if you have something like the following that you don't want expanded
>>> diff(x*f(x), x)
x*Derivative(f(x), x) + f(x)
>>> Derivative(x*f(x), x)
Derivative(x*f(x), x)
diff
is a "wrapper" method that it is going to instantiate theDerivative
class. So, doing this:is equivalent to do:
However, the
Derivative
class might be useful to delay the evaluation of a derivative. For example:But the same thing can also be achieved with:
So, to answer your question, in the example you provided there is absolutely no difference in using
diff
vsDerivative
.If
expr.diff(variable)
can be evaluated, it will return an instance ofExpr
(either a symbol, a number, multiplication, addition, power operation, depending onexpr
). Otherwise, it will return an object of typeDerivative
.