## SAGE tip: Convert a 2D list into a matrix

February 4, 2011 by doxdrum

In yesterday SAGE tip, I did a “small” worksheet for calculating Riemann and Ricci tensors given a metric.

The worked example was the Schwarzschild solution,

,

which is a vacuum solution, i.e.,

When I ran the worksheet, the result of the Ricci tensor was right, but didn’t simplify to zero… So, I decide to bypass this applying the trick of matrix simplification.

But first… How do I convert a list to a matrix? Well, my first try worked!! 🙂 (wonderful!)

For the five dimensional Schwarzschild metric the Ricci tensor (find the worksheet in my webpage obtained was,

`sage: Rdd = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 2*(2*M/r^2`

- 1)*sin(theta)^2 + sin(theta)^2 - cos(theta)^2 - 4*M*sin(theta)^2/r^2 +

1, 0], [0, 0, 0, 0, 2*(2*M/r^2 - 1)*sin(phi)^2*sin(theta)^2 +

sin(phi)^2*sin(theta)^2 - sin(phi)^2*cos(theta)^2 -

4*M*sin(phi)^2*sin(theta)^2/r^2 + sin(phi)^2]]

which would be zero.

The line,

`sage:matrix(SR, Rdd).apply_map(lambda x: x.simplify_full())`

` `

`[0 0 0 0 0]`

[0 0 0 0 0]

[0 0 0 0 0]

[0 0 0 0 0]

[0 0 0 0 0]

converts the list into a matrix, and the **apply_map()** with the defined *lambda* function simplifies the elements of the matrix.

Enjoy!

DOX

### Like this:

Like Loading...

*Related*

## Leave a Reply