Tensors in Mathematica 9:
Built-In Capabilities

George E. Hrabovsky
MAST

This Talk

I intend to cover four main topics:

How to make tensors in the newest version of Mathematica.

The metric tensor and how to transform vectors into covectors.

Cartesian tensor operations.

GR Operations

How to Build a Tensor in Mathematica 9

Rank One

For rank one tensors, we can write them as tangent vectors,

Table[{(x^i)},{i,{"1","2","3"}}]//MatrixForm

or as covectors

Table[(Subscript[x, i]),{i,{"1","2","3"}}]

Rank Two and Higher

We can use similar methods to develop rank two tensors, though Mathematica is not able to cope with abstract indices without help from third-party software—I like xAct.

Table[(σ^(i j)),{i,{x,y,z}},{j,{x,y,z}}]//MatrixForm

Table[(σ^(Grid[{{i,j}},Spacings->.1])),{i,{x,y,z}},{j,{x,y,z}}]//MatrixForm

You can produce the individual tensor components,

σ[i_,j_,n_]:=Table[(η ((∂_(Subscript[x, j]))(Subscript[v, i])[(Subscript[x, j])]+(∂_(Subscript[x, i]))(Subscript[v, j])[(Subscript[x, i])]-If[i==j,(2/3)(∂_(Subscript[x, k]))(Subscript[v, k])[(Subscript[x, k])],0])+If[i==j,ξ (∂_(Subscript[x, k]))(Subscript[v, k])[(Subscript[x, k])],0]),{k,1,n,1}]//MatrixForm

{σ[1,1,3],σ[1,2,3],σ[1,3,3]}//TraditionalForm

You can also write a table to produce the entire tensor

st[n_]:=Table[σ[i,j,n],{i,1,n,1},{j,1,n,1}]//MatrixForm

st[3]//TraditionalForm

The Metric Tensor

A specific example of a calculation is one where we transform from a tangent vector to a covector using the metric tensor,

(Subscript[v, i])=(Subscript[g, ij])(v^j)

So, given the tangent vector (v^j)=(2,r cos θ,-r ϕ), and assuming we are in spherical coordinates, we can find the metric.

tv={2,r Cos[θ],-r ϕ};

met=CoordinateChartData["Spherical", "Metric",{r,θ,ϕ}]//TraditionalForm

We can even find the inverse metric,

im=CoordinateChartData["Spherical", "InverseMetric",{r,θ,ϕ}]//TraditionalForm

We take the product of the inverse metric with the tangent vector,

tv . CoordinateChartData["Spherical", "InverseMetric",{r,θ,ϕ}]//TraditionalForm

giving us the covector

The Cartesian Tensor

We create a new tensor,

newtensor=Table[(x^(Grid[{{i}}])) (y^(Grid[{{j}}])) (z^(Grid[{{k}}])),{i,1,3},{j,1,3},{k,1,3}];

newtensor//MatrixForm//TraditionalForm

We can see if its a tensor,

ArrayQ[newtensor]

We can find its rank

TensorRank[newtensor]

We can perform a contraction

contens=Table[(∑_(a=1)^3)newtensor[[i,a,a]],{i,1,3}]//MatrixForm//TraditionalForm

We have the inner product

in1=Inner[Times,newtensor,newtensor,Plus]//FullSimplify//MatrixForm

newtensor . newtensor//FullSimplify//MatrixForm

We can take the direct product,

in1=Outer[Times,newtensor,newtensor]//FullSimplify//MatrixForm

We can take the trace of the tensor,

Tr[newtensor]

We can transpose,

Transpose[newtensor]//MatrixForm//TraditionalForm

We can take a partial derivative

(∂_x)newtensor//MatrixForm//TraditionalForm

we can determine the differentials

Dt[newtensor]//MatrixForm//TraditionalForm

We can look for symmetries,

TensorSymmetry[newtensor]

symten=Symmetrize[newtensor,Antisymmetric[{1,2}]]

Normal[symten]//MatrixForm

TensorSymmetry[symten]

GR Operations

The Metric

Here we input the metric

Metric[gi_]:=Module[
{n=Length[gi],g,ing},
g=Table[If[μ≥ν,gi[[μ,ν]],gi[[ν,μ]]],{μ,n},{ν,n}];
ing=Simplify[Inverse[g]];
{n,g,ing}]

we assign labels to the coordinates

Evaluate[Table[x[μ],{μ,4}]]={t,r,θ,φ};

Here is the Schwarzschild metric:

Metric[{{1-1/r},{0,-1/(1-1/r)},{0,0,-(r^2)},{0,0,0,-(r^2)2((Sin[θ])^2)}}]//TraditionalForm

Here we have the Christoffel Symbols:

Christoffel[{n_,g_,ing_},OptionsPattern[]]:=Module[
{Γ,inΓ},Γ=inΓ=Table[0,{λ,n},{μ,n},{ν,n}];
Do[Γ[[λ,μ,ν]]=Simplify[(D[g[[λ,ν]],x[μ]]+D[g[[λ,μ]],x[ν]]-D[g[[μ,ν]],x[λ]])/2];If[μ≠ν,Γ[[λ,ν,μ]]=Γ[[λ,μ,ν]]],{λ,n},{μ,n},{ν,μ}];Do[inΓ[[λ,μ,ν]]=Simplify[Sum[ing[[λ,ρ]]Γ[[ρ,μ,ν]],{ρ,n}]];If[μ≠ν,inΓ[[λ,ν,μ]]=inΓ[[λ,μ,ν]]],{λ,n},{μ,n},{ν,μ}];
If[OptionValue[PrintNonZero],
Do[If[Γ[[λ,μ,ν]]=!=0,Print[(Subscript["Γ", Grid[{{λ-1,μ-1,ν-1}}]])," ",Γ[[λ,μ,ν]]]],{λ,n},{μ,n},{ν,μ}];Do[If[inΓ[[λ,μ,ν]]=!=0,Print[(Subscript["Γ", Grid[{{"",μ-1,ν-1}}]]^(Grid[{{λ-1}}]))," ",inΓ[[λ,μ,ν]]]],{λ,n},{μ,n},{ν,μ}
]];
{Γ,inΓ}]

Christoffel[{n_,g_,ing_},OptionsPattern[]]:=Module[
{Γ,inΓ},Γ=inΓ=Table[0,{λ,n},{μ,n},{ν,n}];
Do[Γ[[λ,μ,ν]]=Simplify[(D[g[[λ,ν]],x[μ]]+D[g[[λ,μ]],x[ν]]-D[g[[μ,ν]],x[λ]])/2];If[μ≠ν,Γ[[λ,ν,μ]]=Γ[[λ,μ,ν]]],{λ,n},{μ,n},{ν,μ}];Do[inΓ[[λ,μ,ν]]=Simplify[Sum[ing[[λ,ρ]]*Γ[[ρ,μ,ν]],{ρ,n}]];If[μ≠ν,inΓ[[λ,ν,μ]]=inΓ[[λ,μ,ν]]],{λ,n},{μ,n},{ν,μ}];
Do[If[Γ[[λ,μ,ν]]=!=0,Print["Γ",λ-1,μ-1,ν-1," ",Γ[[λ,μ,ν]]]],{λ,n},{μ,n},{ν,μ}];Do[If[inΓ[[λ,μ,ν]]=!=0,Print["Γ",λ-1,μ-1,ν-1," ",inΓ[[λ,μ,ν]]]],{λ,n},{μ,n},{ν,μ}
];
{Γ,inΓ}]

Here we calculate the Christoffel symbols for the Schwarzschild Metric

Christoffel[Metric[{{1 - 1/r}, {0, -1/(1 - 1/r)}, {0, 0, -(r^2)}, {0, 0, 0, -(r^2)((Sin[θ])^2)}}]];

Here we calculate the components of the Riemann tensor:

Riemann[{n_,g_,ing_},OptionsPattern[]]:=Module[
{Γ,inΓ,R=Table[0,{α,n},{β,n},{μ,n},{ν,n}],R2=Table[0,{μ,n},{ν,n}],R0},{Γ,inΓ}=Christoffel[{n,g,ing}];Do[R[[α,β,μ,ν]]=R[[β,α,ν,μ]]=Simplify[Sum[g[[α,λ]](D[inΓ[[λ,β,ν]],x[μ]]-D[inΓ[[λ,β,μ]],x[ν]])+Γ[[α,λ,μ]]inΓ[[λ,β,ν]]-Γ[[α,λ,ν]]inΓ[[λ,β,μ]],{λ,n}]];R[[β,α,μ,ν]]=R[[α,β,ν,μ]]=-R[[α,β,μ,ν]];
If[μ≠α,R[[μ,ν,α,β]]=R[[ν,μ,β,α]]=R[[α,β,μ,ν]];
R[[ν,μ,α,β]]=R[[μ,ν,β,α]]=-R[[α,β,μ,ν]]],{α,2,n},{β,α-1},{μ,2,α},{ν,If[μ===α,β,μ-1]}];Do[R2[[μ,ν]]=Simplify[Sum[ing[[α,β]]*R[[α,μ,β,ν]],{α,n},{β,n}]];
If[μ≠ν,R2[[ν,μ]]=R2[[μ,ν]]],{μ,n},{ν,μ}];
R0=Simplify[Sum[ing[[μ,ν]]R2[[μ,ν]]If[μ≠ν,2,1],{μ,n},{ν,μ}]];
Do[If[R[[α,β,μ,ν]]=!=0,Print[(Subscript["R", Grid[{{α-1,β-1,μ-1,ν-1}}]])," ",R[[α,β,μ,ν]]]],{α,2,n},{β,α-1},{μ,2,α},{ν,If[μ===α,β,μ-1]}];
Do[If[R2[[μ,ν]]=!=0,Print[(Subscript["R", Grid[{{μ-1,ν-1}}]])," ",R2[[μ,ν]]]],{μ,n},{ν,μ}];If[R0=!=0,Print["R ",R0]];
{R,R2,R0}]

Here is the output for the Schwarzschild metric, assuming (Subscript[r, S])=1:

Riemann[Metric[{{1-1/r},{0,-1/(1-1/r)},{0,0,-r^2},{0,0,0,-r^2Sin[θ]^2}}]];

Thank You!

George E. Hrabovsky
MAST
www.madscitech.org
george@madscitech.org