Ask Your Question
1

Simplify derivatives expression

asked 2018-06-25 14:44:46 +0100

loicg gravatar image

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 flag offensive close merge delete

Comments

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 19:26:28 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-06-27 08:20:17 +0100

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

Comments

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 09:20:13 +0100 )edit
0

answered 2018-06-27 09:30:37 +0100

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

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2018-06-25 14:44:46 +0100

Seen: 569 times

Last updated: Jun 27 '18