Ask Your Question

Simplify derivatives expression

asked 2018-06-25 07:44:46 -0500

loicg gravatar image


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 flag offensive 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".

nbruin gravatar imagenbruin ( 2018-06-26 12:26:28 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2018-06-27 02:30:37 -0500

slelievre gravatar image

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

sage: a.mul(b, hold=True)

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...

edit flag offensive delete link more

answered 2018-06-27 01:20:17 -0500

Emmanuel Charpentier gravatar image

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 ?

edit flag offensive delete link 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.

slelievre gravatar imageslelievre ( 2018-06-27 02:20:13 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-06-25 07:44:46 -0500

Seen: 47 times

Last updated: Jun 27