Ask Your Question

Unable to use substitute_function in SageManifolds

asked 2020-01-17 22:33:24 +0200

thethinker gravatar image

updated 2020-01-17 22:34:45 +0200

I'm trying to do a function substitution into one of the Einstein Field equations, using tools from SageManifolds. I am doing nearly the same thing as the tutorial on the Lemaitre-Tolman equations ( However, using substitute_function does not seem to achive the result. The MWE is below. The setup is the following:

M = Manifold(4, 'R^4')
coord.<t,x,z1,z2> = M.chart(r't x z1 z2') #name of the chart is coord.
g = M.metric('g')
g[0,0] = -1
g[1,1] = a^2
g[2,2] = a^2*sin(x)^2
g[3,3] = a^2*cos(x)^2
nab = g.connection()
ric = nab.ricci()
SCAL = g.ricci_scalar()
phi = M.scalar_field(function('phi')(*coord), name='phi')

Obviously, on my sheet I run all these and they display what's expected - let me know if you think more info is needed here, and I'll attach the sheet as well (sorry, turns out I don't have the rep for that). So now I define the Einstein equations and try to make a trivial substitution:

Lam=var('Lam', latex_name=r'\Lambda')
E=ric - SCAL/2*g + Lam*g - (8*pi*G)*T3
E[0,0].expr().expand() == 0

The result is

-12*pi*G*diff(phi(t, x, z1, z2), t)^2 + 4*pi*G*diff(phi(t, x, z1, z2), x)^2/a(t)^2 - Lam + 3*diff(a(t), t)^2/a(t)^2 + 4*pi*G*diff(phi(t, x, z1, z2), z1)^2/(a(t)^2*sin(x)^2) + 4*pi*G*diff(phi(t, x, z1, z2), z2)^2/(a(t)^2*cos(x)^2) + 3/a(t)^2 == 0

Cool, so what I really want to do is make some assumptions about the scalar field, but I can't even figure out how to substitute a different, equivalent function. My first try:


Results in

(4*pi*G*cos(x)^2*diff(phi(t, x, z1, z2), z1)^2 + (4*pi*G*diff(phi(t, x, z1, z2), z2)^2 - (12*pi*G*a(t)^2*diff(phi(t, x, z1, z2), t)^2 - 4*pi*G*diff(phi(t, x, z1, z2), x)^2 + Lam*a(t)^2 - 3*diff(a(t), t)^2 - 3)*cos(x)^2)*sin(x)^2)/(a(t)^2*cos(x)^2*sin(x)^2)

e.g. exactly the same thing (specifically, phi is not replaced with F1). Here are bunch of other tries that also return the same thing:

E_expr.substitute_function(phi, F1)
E_expr.substitute_function(phi, F2)

Anyone have any insight into what is happening?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-01-18 00:05:22 +0200

rburing gravatar image

updated 2020-01-18 00:06:25 +0200

Consider the definition

phi = M.scalar_field(function('phi')(*coord), name='phi')

There are three layers here:

  • the scalar field phi itself,
  • inside it is the expression function('phi')(*coord),
  • and inside that is the function function('phi').

If you want to call substitute_function on an expression, you should pass two functions.

To access the function inside phi you need to peel off the layers: phi.expr().operator().

So, you can do what you want as follows:

E[0,0].expr().substitute_function(phi.expr().operator(), F1.expr().operator())

You can also assign the function inside phi to a variable (before defining phi), so you can use that instead.

edit flag offensive delete link more


Tried it both ways and they work. Thanks very much! So the .operator() returns the function, and the .expr() gives the expression of that function?

cduston gravatar imagecduston ( 2020-01-18 01:53:44 +0200 )edit

You're welcome! Yes, that's right. The expression is the symbolic function evaluated at symbolic arguments (namely the coordinates, which are coerced into the symbolic ring).

rburing gravatar imagerburing ( 2020-01-18 11:23:17 +0200 )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


Asked: 2020-01-17 22:33:24 +0200

Seen: 396 times

Last updated: Jan 18 '20