CITE THIS NOTEBOOK: SpTm: package for Differential Geometry and General Relativity calculations by Bowen Ping. Wolfram Community FEB 27 2023.
GITHUB: https://github.com/Jayce-Ping/SpTm.
SpTm is short for Space-Tim, which is the background of General Relativity. This package is for calculations of tensors expression with abstract index notation. You can use this package to do some calculations when you are learning Differential Geometry or General Relativity. Calculate all components of a tensor of a expression runs out of draft paper and makes it easy to make mistake. But this package will make it easier. In this package, you can use a readable form to input tensors and expression in a easy way.

Introduction

I programmed a package for calculations with Wolfram Language, which can do some calculations for differential geometry and general relativity. I like to input an expression in a more readable way. I want to input tensors with subscripts and superscripts, which are able to input to the notebook with ctrl+- and ctrl+6. The English document is as following, in which I introduce the installation steps and usage examples.

Install and Import

Method 1

Download SpTm.wl from my GitHub - Jayce-Ping/SpTm manually, place it in the same directory of the current Notebook and run one of the following commands:
In[]:=
Get@FileNameJoin[{NotebookDirectory[],"SpTm.wl"}]
<<SpTm`

Method 2 (recommend)

With the Internet connected, run the following command:
URLDownload["https://raw.githubusercontent.com/Jayce-Ping/SpTm/main/SpTm.wl",FileNameJoin[{$UserBaseDirectory,"Applications","SpTm.wl"}]];
The command above will download the SpTm.wl into the right directory. After this, any time you want to import SpTm, just run one of the following commands in any Notebook:
In[]:=
<<SpTm`
In[]:=
Needs["SpTm`"]

Get Help and Usage Information

Using the following command to get the usage of every function:
In[]:=
?SpTm`*
Out[]=
SpTm`
BoostMatrix
SCalcRicciScalar
SetMetric
STAntiSymmetrize
STSpecify
CoordinatesInfo
SCalcRicciTensor
SetMetricSymbol
STCalcTensor
STSymmetrize
InputExplain
SCalcRiemannTensor
SetTensor
STCalculate
SVolumeElement
MetricInfo
SCalcWeylTensor
ShowForm
STensor
​
SCalcChristoffel
SCoordinatesTransform
SLineElement
STensorInfo
​
SCalcEinsteinTensor
SetCoordinates
SpTmHelp
STSimSpecify
​

Basic Usage

Input a Tensor

Use Ctrl+_ to input the subscript,use Ctrl+^ to input superscript. For example,
cd
h
ab
or
cd
h
ab
.
You can use
ctrl+-
to input the subscripts and use ctrl+6 or ctrl+5 to input the superscripts.And you can use the following function to see what SpTm will do to the expression before:
In[]:=
InputExplain
cd
h
ab

Out[]=
STensor[h,{a,b},{c,d}]
InputExplain shows the SpTm Explanation of your input. STensor[h, {sub}, {sup}] represents a tensor with abstract indices, named h, with sub indices {sub} and super indices {sup}.

Simplify an Abstract Expression

Use STCalculate to transform and simplify an abstract expression:
In[]:=
expr=
d
v
∇
a
(
g
bc
e
w
);
In[]:=
STCalculate[expr]
Out[]=
∇
a
STensor[w,{},{e}]STensor[g,{b,c},{}]STensor[v,{},{d}]
In[]:=
%//FullForm
Out[]//FullForm=
Times[Grad[STensor[w,List[],List[e]],a],STensor[g,List[b,c],List[]],STensor[v,List[],List[d]]]
Because ∇ is the derivative operator adapted to the metric g, the result is as above.
The expression above is the form SpTm deal, which is not that readable for human. We can use ShowForm to make it readable.
In[]:=
%//ShowForm
Out[]//StandardForm=
∇
a
e
w
g
bc
d
v

Symmetrize and Antisymmetrize

You can use STSymmetrize/STAntisymmetrize to symmetrize/antisymmetrize a tensor or an expression as follows:
In[]:=
STSymmetrize[expr,{a,b,c}]
Out[]=
1
6
(
∇
c
(STensor[g,{a,b},{}]STensor[w,{},{e}])STensor[v,{},{d}]+
∇
b
(STensor[g,{a,c},{}]STensor[w,{},{e}])STensor[v,{},{d}]+
∇
c
(STensor[g,{b,a},{}]STensor[w,{},{e}])STensor[v,{},{d}]+
∇
a
(STensor[g,{b,c},{}]STensor[w,{},{e}])STensor[v,{},{d}]+
∇
b
(STensor[g,{c,a},{}]STensor[w,{},{e}])STensor[v,{},{d}]+
∇
a
(STensor[g,{c,b},{}]STensor[w,{},{e}])STensor[v,{},{d}])
In[]:=
%//ShowForm
Out[]//StandardForm=
1
6

∇
c
(
g
ab
e
w
)
d
v
+
∇
b
(
g
ac
e
w
)
d
v
+
∇
c
(
g
ba
e
w
)
d
v
+
∇
a
(
g
bc
e
w
)
d
v
+
∇
b
(
g
ca
e
w
)
d
v
+
∇
a
(
g
cb
e
w
)
d
v

In[]:=
STAntiSymmetrize[expr,{a,e,d}]
Out[]=
1
6
(
∇
e
(STensor[g,{b,c},{}]STensor[w,{},{d}])STensor[v,{},{a}]-
∇
d
(STensor[g,{b,c},{}]STensor[w,{},{e}])STensor[v,{},{a}]-
∇
e
(STensor[g,{b,c},{}]STensor[w,{},{a}])STensor[v,{},{d}]+
∇
a
(STensor[g,{b,c},{}]STensor[w,{},{e}])STensor[v,{},{d}]+
∇
d
(STensor[g,{b,c},{}]STensor[w,{},{a}])STensor[v,{},{e}]-
∇
a
(STensor[g,{b,c},{}]STensor[w,{},{d}])STensor[v,{},{e}])
In[]:=
%//ShowForm
Out[]//StandardForm=
1
6

∇
e

g
bc
d
w

a
v
-
∇
d
(
g
bc
e
w
)
a
v
-
∇
e
(
g
bc
a
w
)
d
v
+
∇
a
(
g
bc
e
w
)
d
v
+
∇
d
(
g
bc
a
w
)
e
v
-
∇
a

g
bc
d
w

e
v


Set Coordinate System

In fact, most of our work will be done with coordinate system. You can use SetCoordinates to choose a coordinate system:
In[]:=
SetCoordinates[{r,θ,ϕ}]
If there is no warning, the coordinate system has been set.

Coordinate Basis and Dual Coordinate Basis

In Differential Geometry, there is one set of important vectors - coordinate basis, which we usually denote as
a
∂
∂
i
x
, where a is the abstract index and
i
x
is the i-th coordinate basis. But in Mathematica, directly use ∂ will cause a warning and abort like below:
In[]:=
∂
Besides, it’s complicated for input a vector using
∂
∂
i
x
. You need to press Ctrl and switch cursor again and again. But there is a meaningless symbol in Mathematica  or DifferentialD[] which is able to denote the dual coordinate basis like x or DifferentialD[x] and not encounter a warning.​According to this, I find another symbol to denote the coordinate basis in a simple and easy-input way - using or CapitalDifferentialD[].​I’ll restate how SpTm denote coordinate basis and dual coordinate basis with examples.​Assuming that we already set the coordinate system {r, θ, ϕ}, the coordinate basis will be:​​
a
(r)
,
a
(θ)
and
a
(ϕ)
.
a
(r)
;
a
(θ)
;
a
(ϕ)
;
​The way to input  is to press DD. And input  is to press dd. ​The dual coordinate basis will be:​​
(r)
a
,
(θ)
a
,and
(ϕ)
a
(r)
a
;
(θ)
a
;
(ϕ)
a
;
Once you set coordinate system, the coordinate basis and dual coordinate basis will be set automatically.
In[]:=
SetCoordinates[{r, θ, ϕ}]
You can use STensorInfo to get information of each tensor.
In[]:=
{STensorInfo[
(r)
a
],STensorInfo[
(θ)
b
],STensorInfo[
a
(ϕ)
]}
Out[]=

r
α
=
1
0
0
,
θ
β
=
0
1
0
,
α
ϕ
=
0
0
1

The heads of two expressions are different. But we regard x as a vector and a is its index, so the second form is correct. Remember this. This is the same as x.

Set Metric

Then, we can use SetMetric to set the metric field on this manifold:
If you have already set the coordinate system, you can use the command above. Or, you can set metric and coordinate system together as following:

Examples

Next, I will use some examples with different metrics to introduce more functions:

2-D Euclidean Metric

Set Metric Components and Coordinate System

Set Tensor Components

Coordinates Transformation

The second argument is the rule list of current coordinates to the target coordinates.

Verify the Commutation of Coordinate Bases

{u,v} is a set of coordinate bases,{w,v} are a set of orthonormal bases
Calculate the norm of every vector, verify the {w,v} are normal vectors.

2-D Cylinder Metric

Transform the Abstract Expression into Specific Expression

Here we calculate the geodesics on the cylinder, which are circumference and bus bar.

Calculate the Christoffel Symbol in Current Coordinate System

So Riemann Tensor, Ricci Tensor, Ricci Scalar all vanish. The intrinsic curvature is null but the extrinsic curvature exists.

Calculate the Expression of the Line Element

Calculate the Expression of Volume Element with Coordinates Conditions

Calculate the Christoffel Symbol and Set as a Tensor

For the result shown above, we can use FullForm to see the full structure of the expression:
The result is a Row. We can use [[1]] to get every parts:
The list includes three parts - tensor expression, equal sign and components array of tensor.
In order to show the components in a beautiful way, It is headed by MatrixForm. So we need to use [[1]] one more time to get the components array in the form of list.
This is the components list.

Calculate the Riemann Tensor and Set as a Tensor

Verify the Definition of the Riemann Tensor

Here we notice that the order of indices are different, so we need to transpose one of them and then compare:
Now we can see they are the same, which verify the definition of Riemann Tensor.

Calculate the Ricci Tensor and Set as a Tensor

Calculate the Ricci Scalar

Calculate the Einstein Tensor and Set as a Tensor

Show the Components Information of All Tensors named “R”

Get Components Information with the Input of Abstract Expression Directly

Verify that the Einstein Tensor of all two-dimensional generalized Riemannian spaces is zero

The components of metric g are functions of coordinates.
Simplify the results with the symmetry of metric g.

Solve the Killing Vector Fields

The differential equations here are not easy to solve. Here is a roundabout strategy - guessing that the symmetry of the sphere is a rotational symmetry around three axes in the Cartesian coordinate system.
This is changed to a Cartesian coordinate system in three-dimensional Euclidean space where the three Killing vectors of rotational symmetry are written directly and a coordinate transformation is used to obtain the components in the spherical coordinate system, which are then substituted back here for verification.
The two-dimensional sphere has three symmetries - rotational symmetry around the x-y-z axis in a Cartesian coordinate system with three independent Killing vector fields.

3-D Euclidean Metric

Rotational Symmetries Corresponding to the Three Killing Vector Fields

Here three independent Killing vectors can be written based on the rotational symmetry in each coordinate plan:
Do the coordinates transformation:
At this point the specific component expressions for each Killing vector field in the spherical coordinate system are obtained, written back to the corresponding content in the previous spherical metric, and then verified.

Minkowski Spacetime

Set the Components, Coordinate System and Symbol of Metric.

Verify the Symmetries of Minkowski Spacetime

Verify the Invariance of Space-time Translations

Verify Spatial Rotation

Spatial rotation is naturally established. Because the three-dimensional Euclidean space is a submanifold of the Minkowski spacetime, its metric is the 3-D Euclidean metric, which naturally carries three spatial rotation invariants.

Verify Boost Invariance

The boost of Minkowski spacetime corresponds to the Lorentz transformation between coordinate system.
Here we verify the t-x boost:
Besides, you can use BoostMatrix to generate a boost matrix corresponding to a velocity vector like this:
Here I choose geometrized unit system, which means the speed of light is chosen as 1. So the norm of velocity should be less than 1.
We can build the coordinates transformation as following:
We can see the metric didn’t change after boost.

Schwarzschild Metric

Calculate the Line Element Expression

Obviously, when the mass of star M is equal to zero, the spacetime is Minkowski spacetime, which is flat.

Calculation the Volume Element

We can use STSpecify to calculate every components of the VolumeElement:
Use TensorSymmetry to check the symmetry of the volume element like:

Calculate Riemann Tensor, Ricci Tensor, Ricci Scalar and Einstein Tensor
Verify the Schwarzschild metric is a solution of vacuum Einstein Field Equation

Then, calculate the Weyl Tensor with Schwarzschild metric:
The specific indices here are Greek letters δβαγ corresponding to dbac. So we need to transpose the result to get the indices abcd, and compare it with the result above. The result shows they are equal.

Isotropic Observer - Robertson-Walker Metric

Here I find the Weyl Tensor of Robertson-Walker Metric vanishes, but I don’t know it’s physical meaning yet. If you know, please acknowledge me.
The package may cause bugs and the document is not that good. I will make them better when I’m not busy.
If you are interested in my package and want to know more, please move to https://github.com/Jayce-Ping/SpTm​
If you like my package, please star my repository.
If you find any bug, you can open an issue on my GitHub.