Trouble getting *collect* to work with derivatives.

53 views
Skip to first unread message

Christian Seberino

unread,
Jan 22, 2021, 1:46:35 PM1/22/21
to sage-support

I'm trying to collect all the terms in an expression with the same
second partial derivative but it doesn't seem to be working.
I can't figure out why.

Here is my code....

# ============================================================

function("xp yp zp tp f")
var("x y z t v c")

xp = (x - v * t) / sqrt(1 - v^2 / c^2)
yp = y
zp = z
tp = (t - v * x / c^2) / sqrt(1 - v^2 / c^2)

e = derivative(f(xp, yp, zp, tp), x, 2) + derivative(f(xp, yp, zp, tp), y, 2) + derivative(f(xp, yp, zp, tp), z, 2) - derivative(f(xp, yp, zp, tp), t, 2) / c^2

e.collect(derivative(f(xp, yp, zp, tp), x, 2))

# ============================================================ 

Here is the output.  I added spaces at a subtraction to make it easy to see
there are TWO of those D[0, 0](f) terms (each at the beginning of the sections).

(D[0, 0](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1))/sqrt(-v^2/c^2 + 1) - v*D[0, 3](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1))/(c^2*sqrt(-v^2/c^2 + 1)))/sqrt(-v^2/c^2 + 1) - v*(D[0, 3](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1))/sqrt(-v^2/c^2 + 1) - v*D[3, 3](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1))/(c^2*sqrt(-v^2/c^2 + 1)))/(c^2*sqrt(-v^2/c^2 + 1)) 


((v*D[0, 0](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1))/sqrt(-v^2/c^2 + 1) - D[0, 3](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1))/sqrt(-v^2/c^2 + 1))*v/sqrt(-v^2/c^2 + 1) - (v*D[0, 3](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1))/sqrt(-v^2/c^2 + 1) - D[3, 3](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1))/sqrt(-v^2/c^2 + 1))/sqrt(-v^2/c^2 + 1))/c^2 + D[1, 1](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1)) + D[2, 2](f)(-(t*v - x)/sqrt(-v^2/c^2 + 1), y, z, (t - v*x/c^2)/sqrt(-v^2/c^2 + 1)) 


Emmanuel Charpentier

unread,
Jan 23, 2021, 3:55:47 PM1/23/21
to sage-support

I think that your code doesn’t do what you think it does. Note for example that your declarations are self-contradicting :

Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:function("xp yp zp tp f")
:var("x y z t v c")
:--
(xp, yp, zp, tp, f)
(x, y, z, t, v, c)
sage: xp.parent()
<class 'sage.symbolic.function_factory.function_factory.<locals>.NewSymbolicFunction'>
sage: xp = (x - v * t) / sqrt(1 - v^2 / c^2)
sage: xp.parent()
Symbolic Ring

What you intend to do isn’t really clear… Could you try and clear your goals ?

HTH,

Christian Seberino

unread,
Jan 23, 2021, 5:17:26 PM1/23/21
to sage-support

What you intend to do isn’t really clear… Could you try and clear your goals ?

Emmanuel  

Thanks so much for your help.  I'm trying to show that the wave equation (https://en.wikipedia.org/wiki/Wave_equation)
is invariant under a certain coordinate transformation called the Lorentz transformation (special relativity).

I represent the function that obeys the wave equation in the primed coordinate system by f(xp, yp, zp, tp).

I also represent the primed coordinates by the coordinates in the unprimed coordinate system.
Therefore,   f(xp, yp, zp, tp) =  f(xp(x, y, z, t), yp(x, y, z, t), zp(x, y, z, t), tp(x, y, z, t)).

I then find a bunch of derivates of f(xp(x, y, z, t), yp(x, y, z, t), zp(x, y, z, t), tp(x, y, z, t)) and try to collect terms.

All the coordinates should be real numbers.

Does that explain everything?


 

Emmanuel Charpentier

unread,
Jan 24, 2021, 3:15:02 AM1/24/21
to sage-support

Sage has recently acquired a large set of tools relative to manifolds. A look at these tools and related tutorials/references may be in order…

HTH,

Christian Seberino

unread,
Jan 24, 2021, 11:36:44 AM1/24/21
to sage-s...@googlegroups.com
Emmanuel

But my question is more simple than that. I just want to know why the collect method was not able to collect all the terms with the given second derivative.

--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/TDseIA1M7vY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-support...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/068e5ac0-1d78-453f-a465-bc84e1d1fc90n%40googlegroups.com.

Emmanuel Charpentier

unread,
Jan 24, 2021, 12:33:46 PM1/24/21
to sage-support

Probably because “the given second derivative” has a “computed operator”…

BTW :

var("x,y,z,t,v,c")
f=function("f")
xp=(t-v*x)/sqrt(1-v^2/c^2)
yp=y
zp=z
tp=(t-v*x/c^2)/sqrt(1-v^2/c^2)
foo=(sum(map(lambda u:derivative(f(xp,yp,zp,tp),u,2), (x, y, z)))-derivative(f(xp,yp,zp,tp),t,2)/c^2).factor()
view(foo.simplify_full())

does partially what you mean…

Vincent Delecroix

unread,
Jan 24, 2021, 1:02:05 PM1/24/21
to sage-s...@googlegroups.com
Dear Christian,

Given the specifications, I suspect that the function collect
only works for variables. And indeed

sage: expr = (x+y)*(z+t) + (x+y)*(1+z^2) + 2


sage: expr


(z^2 + 1)*(x + y) + (t + z)*(x + y) + 2
sage: expr.collect(x+y)


(z^2 + 1)*(x + y) + (t + z)*(x + y) + 2

Instead of

(z^2 + t + z + 1)*(x + y) + 2

Secondly, what are you trying to collect? The D[0,0] terms or
f(xp, yp, zp, tp), x, 2) which are two different things? Given
what you are trying to do I guess it is the first one, in which
case you should have tried

sage: term = f(x,y,z,t).derivative(x,2).subs(x=xp,y=yp,z=zp,t=tp)

Finally, note also that there is some factorization happening in
your expression e, so before doing anything do

sage: e = e.expand()

Now, the only way I found to make it work was to implement my
own collecting routine (see attachment). With the function
implemented in the file that walks along the expression you
obtain

sage: L = my_collect(e, term)
sage: len(L)
2
sage: L[1] # the terms linear in D[0,0](f)(xp, yp, zp, tp)
v^2/(c^2*(v^2/c^2 - 1)) - 1/(v^2/c^2 - 1)

The above term is one as can be checked with

sage: L[1].simplify_full()


1

Best
Vincent

Le 24/01/2021 à 17:23, Christian Seberino a écrit :
> Emmanuel
>
> But my question is more simple than that. I just want to know why the
> collect method was not able to collect all the terms with the given second
> derivative.
>
> On Sun, Jan 24, 2021, 2:15 AM Emmanuel Charpentier <
> emanuel.c...@gmail.com> wrote:
>
>> Sage has recently acquired a large set of tools relative to manifolds
>> <https://sagemanifolds.obspm.fr/>. A look at these tools and related
>> <https://groups.google.com/d/msgid/sage-support/068e5ac0-1d78-453f-a465-bc84e1d1fc90n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
collect.py

Nils Bruin

unread,
Jan 24, 2021, 4:14:25 PM1/24/21
to sage-support
I suspect you want to collect wrt. the second partial derivatives of f(x,y,z,t) with respect to x,y,z,t ; evaluated at xp,yp,zp,tp. Those are not the same as the partial second derivatives of f(xp,yp,zp,tp) with respect to (x,y,z,t). If you inspect what derivative(f(xp, yp, zp, tp), x, 2) gives you, you'll see it's a complicated expression. "collecting" relative to it doesn't make sense from first principles and if you try to make the notion precise you'll find there are fundamental ambiguities that prevent it from being a well-defined operation in general.

The operation you probably intend is quite doable, though. You first have to construct the expressions that you want to collect relative to:

    V=[x,y,z,t]
    Ds=[derivative(f(x,y,z,t),V[i],V[j])({x:xp,y:yp,z:zp,t:tp}) for j in range(4) for i in range(j+1)]

You can inspect the entries in Ds: these are all just purely expressions of the form D[i,j](f), so from the perspective of expression manipulations such as "subs" and "collect" and "coeffient", they are "atomic": they behave as polynomial variables themselves. For instance, we could replace them by actual, named variables to exhibit this. We'll call these variables D00,...,D33:

    Dvars=[SR.symbol('D{}{}'.format(i,j))  for j in range(4) for i in range(j+1)]

We can substitute those:

    eD=e.subs(dict(zip(Ds,Dvars)));eD
    D11 + D22 + (D00/sqrt(-v^2/c^2 + 1) - D03*v/(c^2*sqrt(-v^2/c^2 + 1)))/sqrt(-v^2/c^2 + 1) - v*(D03/sqrt(-v^2/c^2 + 1) - D33*v/(c^2*sqrt(-v^2/c^2 + 1)))/(c^2*sqrt(-v^2/c^2 + 1)) - ((D00*v/sqrt(-v^2/c^2 + 1) - D03/sqrt(-v^2/c^2 + 1))*v/sqrt(-v^2/c^2 + 1) - (D03*v/sqrt(-v^2/c^2 + 1) - D33/sqrt(-v^2/c^2 + 1))/sqrt(-v^2/c^2 + 1))/c^2

And we can check this is correct by substituting back and comparing (many CAS packages have rough edges in this area, where they'll allow you to do something but not necessarily do what you thought you would do; so checking is good!)

     bool(eD.subs(dict(zip(Dvars,Ds))) == e)
    True

Note that multivariate calculus books describe the linear algebra that partial derivatives obey under these kinds of operations. That would allow you to get this expression eD right away.

Christian Seberino

unread,
Jan 24, 2021, 9:47:02 PM1/24/21
to sage-s...@googlegroups.com
What are computed operators?  Are those the D[0, 0] things? How avoid those? Is that same as second derivative of 1st variable?

Christian Seberino

unread,
Jan 25, 2021, 10:16:00 AM1/25/21
to sage-support

Secondly, what are you trying to collect? The D[0,0] terms or
f(xp, yp, zp, tp), x, 2) which are two different things?

What is the difference between those 2?
 
Given
what you are trying to do I guess it is the first one, in which
case you should have tried

sage: term = f(x,y,z,t).derivative(x,2).subs(x=xp,y=yp,z=zp,t=tp)


Is this the same as derivative(f(xp(x, y, z, t), yp(x, y, z, t), zp(x, y, z, t), tp(x, y, z, t)) , x, 2)?

Thanks,

Chris

Nils Bruin

unread,
Jan 25, 2021, 2:37:58 PM1/25/21
to sage-support
On Monday, January 25, 2021 at 7:16:00 AM UTC-8 ... wrote:
Is this the same as derivative(f(xp(x, y, z, t), yp(x, y, z, t), zp(x, y, z, t), tp(x, y, z, t)) , x, 2)?

No, it is not. Look up "Multivariable Chain rule".
Reply all
Reply to author
Forward
0 new messages