# Simplify derivatives expression

Hi,

I have a really simple problem. Assuming that I have this simple code

sage: rho = function("rho")(x)
sage: J = function("J")(x)
sage: expr = -J**2/rho**2*Derivative(rho, x) + 2*J/rho*Derivative(J, x)


Is there a way in sage to have this expression in a more compact expression. That is to say

Derivative(J**2/rho, x)

Thanks, Loic

edit retag close merge delete

In principle, one could recognize this by taking the antiderivative of expr and recognizing the form you mention. However, the maxima backend that is used by default does not recognize this. Note that diff(J**2/rho, x) returns the expression in the form expr, so expr is considered the "simplified" form in that sense (it's certainly closer to a general standard form than the other). "compact for a human" is not always "best for computation".

Sort by » oldest newest most voted

WorksForMe(TM) :

sage: rho=function("rho")(x)
sage: J=function("J")(x)
sage: rho.diff(x)
diff(rho(x), x)
sage: (J**2/rho**2).diff(x).factor()
2*(rho(x)*diff(J(x), x) - J(x)*diff(rho(x), x))*J(x)/rho(x)^3


I. e. $$\frac{2 {\left(\rho\left(x\right) \frac{\partial}{\partial x}J\left(x\right) - J\left(x\right) \frac{\partial}{\partial x}\rho\left(x\right)\right)} J\left(x\right)}{\rho\left(x\right)^{3}}$$.

If you are dead set to use a "function call" syntax, it works, too :

sage: factor(diff(J**2/rho**2,x))
2*(rho(x)*diff(J(x), x) - J(x)*diff(rho(x), x))*J(x)/rho(x)^3


or even :

sage: factor(derivative(J**2/rho**2))
2*(rho(x)*diff(J(x), x) - J(x)*diff(rho(x), x))*J(x)/rho(x)^3


So where's your beef ? Oh, I see :

sage: Derivative(J**2/rho**2,x)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-221717693903> in <module>()
----> 1 Derivative(J**Integer(2)/rho**Integer(2),x)

NameError: name 'Derivative' is not defined


So what ?

more

The asker is aware that, setting

sage: rho = function('rho')(x)
sage: J = function('J')(x)


both a and b below end up the same in Sage:

sage: a = -J**2/rho**2*diff(rho, x) + 2*J/rho*diff(J, x)
sage: b = (J**2/rho).diff(x)
sage: print('{}\n{}'.format(a, b))
2*J(x)*diff(J(x), x)/rho(x) - J(x)^2*diff(rho(x), x)/rho(x)^2
2*J(x)*diff(J(x), x)/rho(x) - J(x)^2*diff(rho(x), x)/rho(x)^2


The question is: is there a way, starting from a or b, to transform the expression so that it prints as

diff(J(x)^2/rho(x), x)


which is a more compact representation.

While one can "hold" multiplication as follows, to prevent any simplification of an expression:

sage: rho = function('rho')(x)
sage: J = function('J')(x)

sage: a, b = J(x)/rho(x), rho(x)
sage: a * b
J(x)

sage: a.mul(b, hold=True)
(J(x)/rho(x))*rho(x)


the same is not true for differentiation:

sage: diff(J(x)^2/rho(x), x, hold=True)
Traceback (most recent call last)
...
TypeError: derivative() got an unexpected keyword argument 'hold'


The only workaround I can think of is this trick:

sage: f = function('(J^2/rho)')(x)
sage: diff(f, x)
diff((J^2/rho)(x), x)

sage: f = function('J(x)^2/rho')(x)
sage: diff(f, x)
diff(J(x)^2/rho(x), x)


which one might then try to combine with expression substitutions...

more