Feeds:
Posts

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

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,

$ds^2(g) = -\frac{dt\otimes dt}{1-\frac{2M}{r}}+\left(1-\frac{2M}{r}\right) dr\otimes dr+ r^2\left(d\theta\otimes d\theta +\sin^2(\theta) d\varphi\otimes d\varphi\right)$,

which is a vacuum solution, i.e., $R_{\mu\nu} = 0.$

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