# Displaying a matrix with scalar fields

I have some scalar fields and a matrix whose elements are these scalar fields. A sample code is the following:

reset()
M=Manifold(4, 'M', r'\mathcal{M}')
BL.<t,r,th,ph> = M.chart(r't r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi')
f=M.scalar_field(function('f')(*BL))
g=M.scalar_field(function('g')(*BL))
h=M.scalar_field(function('h')(*BL))
h=0
mymatrix=matrix([[f+g,g,h],[f,h,g],[h,g,f]])


f, g, or h can be zero or nonzero (according to some calculations).

When I try to display my matrix with show(mymatrix) or print(mymatrix), I see Scalar field on the 4-dimensional differentiable manifold M instead of the non-zero elements.

What should I do to display my matrix as

[f+g g 0]
[f 0 g]
[0 g f]


preferably without using loops, etc?

edit retag close merge delete

1

If you accept arguments, then try

matrix(3,3,lambda i,j: mymatrix[i,j].expr())

( 2023-08-02 16:13:40 +0200 )edit

Thank you @achrzesz. This is a solution to my problem.

( 2023-08-02 16:27:53 +0200 )edit

Sort by ยป oldest newest most voted
sage: mymatrix.apply_map(lambda z: z.expr())
[f(t, r, th, ph) + g(t, r, th, ph)                   g(t, r, th, ph)                                 0]
[                  f(t, r, th, ph)                                 0                   g(t, r, th, ph)]
[                                0                   g(t, r, th, ph)                   f(t, r, th, ph)]

more

That solves my problem. Thank you.

( 2023-08-02 16:23:28 +0200 )edit