Ask Your Question
1

How to collect the derivatives in an expression for a scalar field

asked 2023-12-04 22:39:07 +0100

GPN gravatar image

Please consider the following example; it calculates the commutator of two vector fields acting on a scalar field, that is to say it calculates [u,v]f.

from sage.all import *

%display latex

M = Manifold(4, 'M', latex_name=r'\mathcal{M}', structure='Lorentzian')
X.<t,x,y,z> = M.chart()

u0 = function(r'u_0')(t,x,y,z)
u1 = function(r'u_1')(t,x,y,z)
u2 = function(r'u_2')(t,x,y,z)
u3 = function(r'u_3')(t,x,y,z)
u = M.vector_field(u0,u1,u2,u3, latex_name=r'\mathbf{u}')
v0 = function(r'v_0')(t,x,y,z)
v1 = function(r'v_1')(t,x,y,z)
v2 = function(r'v_2')(t,x,y,z)
v3 = function(r'v_3')(t,x,y,z)
v = M.vector_field(v0,v1,v2,v3, latex_name=r'\mathbf{v}')

f = M.scalar_field(function('f')(t,x,y,z), latex_name='f')
commutator_f = u(v(f)) - v(u(f))
commutator_f

This works. Now I can look at the expression for [u,v]f using

commutator_f.expr()

This also works but has many terms that I would like to collect - I would like all terms that are derivatives of f to "go to the right". I tried

commutator_f.expr().collect(f)

but that does not work: TypeError: no canonical coercion from Algebra of differentiable scalar fields on the 4-dimensional Lorentzian manifold M to Symbolic Ring.

How can I do this please?

Using SageMath version 9.5, Release Date: 2022-01-30, on Ubuntu 22.04.

Thank you

GPN

edit retag flag offensive close merge delete

Comments

1

Maybe commutator_f.expr().collect(f.expr()) ?

Max Alekseyev gravatar imageMax Alekseyev ( 2023-12-04 22:46:31 +0100 )edit

Thanks for the suggestion, I tried. It gets rid of the error but nothing is collected. I then also tried

commutator_f.expr().collect(diff(f).expr())

but no luck: now a different error AttributeError: 'DiffFormFreeModule_with_category.element_class' object has no attribute 'expr'.

GPN gravatar imageGPN ( 2023-12-04 23:32:27 +0100 )edit
1

It should be diff(f)[i].expr() for $\partial f /\partial x^i$, so I guess

commutator_f.expr().collect(diff(f)[0].expr())

should work (for $\partial f /\partial x^0$).

eric_g gravatar imageeric_g ( 2023-12-05 09:43:43 +0100 )edit

cool, I will try that

GPN gravatar imageGPN ( 2023-12-05 15:38:51 +0100 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2023-12-05 15:40:32 +0100

GPN gravatar image

Based on the advice from @max and @eric_g - the following works:

commutator_f.expr() \
    .collect(diff(f)[0].expr()) \
    .collect(diff(f)[1].expr()) \
    .collect(diff(f)[2].expr()) \
    .collect(diff(f)[3].expr())

Many thanks GPN

edit flag offensive delete link more

Comments

2

Tip: use parentheses and skip end-of-line backslash.

(
    commutator_f.expr()
        .collect(diff(f)[0].expr())
        .collect(diff(f)[1].expr())
        .collect(diff(f)[2].expr())
        .collect(diff(f)[3].expr())
)
slelievre gravatar imageslelievre ( 2023-12-05 15:51:31 +0100 )edit

@slelievre thanks.

GPN gravatar imageGPN ( 2023-12-05 16:13:47 +0100 )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

1 follower

Stats

Asked: 2023-12-04 22:39:07 +0100

Seen: 143 times

Last updated: Dec 05 '23