# Globally redefine symbolic function in a tensor field

As a simple example, I have the following manifold and chart:

M = Manifold(2, 'M', structure='Lorentzian')
X.<t,r> = M.chart(r"t r:(0,+oo)")


with these functions:

h = function('h')(r)
h0 = function('h0')(r)


Now, I define the following tensor:

A = M.tensor_field(0,2)
A[0,0] = h


If I print A, I get:

[h(r)    0]
[   0    0]


as I expected. However, from now on I want h to be:

h = 2*h0


After setting h=2*h0, if I print A[:] I get the same tensor I had before, instead of A[0,0] = 2*h0.

How can I redefine a symbolic function inside a tensor? I have tried with A.subs and A.apply_map, but none of them did the job.

edit retag close merge delete

Sort by » oldest newest most voted

You should first define a callable symbolic expression containing 2*h0:

f(r) = 2*h0


and then call

A.apply_map(lambda x: x.substitute_function(function('h'), f))


Then A[:] yields

[2*h0(r)       0]
[      0       0]

more