SimpleTensor: a package for tensor and differential-geometric calculations​
​by Denys Rybalka
ABSTRACT: we present a short overview of the new Wolfram Mathematica package intended for elementary ”in-basis” tensor and differential-geometric calculations. In contrast to alternatives our package is designed to be easy-to-use, short, all-purpose, and hackable. It supports tensor contractions using Einstein notation, transformations between different bases, tensor derivative operator, expansion in basis vectors and forms, exterior derivative, and interior product.
In[]:=
Needs["SimpleTensor`"]

Electromagnetic tensor

In this part we will examine applications of SimpleTensor on the example of relativistic electromagnetism. We start by defining a Minkowski space with mostly negative metric:
In[]:=
DefineSpace["Minkowski",4,index{μ,ν,λ,ρ},coord{t,x,y,z}]​​metric[-μ,-ν]≡DiagonalMatrix[{1,-1,-1,-1}]
We define a Levi-Civita tensor and a time direction vector. This can also be done using the full function name:
In[]:=
ϵ[μ,ν,λ,ρ]≡Normal@LeviCivitaTensor[4]​​SetTensor[T[μ],{1,0,0,0}]
We define a general electromagnetic potential:
In[]:=
A[μ]≡{ϕ[t,x,y,z],A1[t,x,y,z],A2[t,x,y,z],A3[t,x,y,z]}
Individual components of a tensor can be extracted by contracting it with a corresponding unit vector. For example, by contracting EM potential with a time direction vector we get the corresponding zeroth component - the scalar potential:
In[]:=
A[-μ]T[μ]//GetArray​​First@GetArray[A[-μ]]%
Out[]=
ϕ[t,x,y,z]
Out[]=
True
Using the partial derivative tensor operator we can calculate the electromagnetic tensor and assign it to a tensor:
In[]:=
ð[-μ][A[-ν]]-ð[-ν][A[-μ]]//GetArray//MatrixForm​​F[-μ,-ν]≡%
Out[]//MatrixForm=
0
-
(0,1,0,0)
ϕ
[t,x,y,z]-
(1,0,0,0)
A1
[t,x,y,z]
-
(0,0,1,0)
ϕ
[t,x,y,z]-
(1,0,0,0)
A2
[t,x,y,z]
-
(0,0,0,1)
ϕ
[t,x,y,z]-
(1,0,0,0)
A3
[t,x,y,z]
(0,1,0,0)
ϕ
[t,x,y,z]+
(1,0,0,0)
A1
[t,x,y,z]
0
(0,0,1,0)
A1
[t,x,y,z]-
(0,1,0,0)
A2
[t,x,y,z]
(0,0,0,1)
A1
[t,x,y,z]-
(0,1,0,0)
A3
[t,x,y,z]
(0,0,1,0)
ϕ
[t,x,y,z]+
(1,0,0,0)
A2
[t,x,y,z]
-
(0,0,1,0)
A1
[t,x,y,z]+
(0,1,0,0)
A2
[t,x,y,z]
0
(0,0,0,1)
A2
[t,x,y,z]-
(0,0,1,0)
A3
[t,x,y,z]
(0,0,0,1)
ϕ
[t,x,y,z]+
(1,0,0,0)
A3
[t,x,y,z]
-
(0,0,0,1)
A1
[t,x,y,z]+
(0,1,0,0)
A3
[t,x,y,z]
-
(0,0,0,1)
A2
[t,x,y,z]+
(0,0,1,0)
A3
[t,x,y,z]
0
The antisymmetry of EM tensor can be shown either elementwise or by summing the arrays of normal and transposed arrays:
In[]:=
GetArray[F[-μ,-ν]+F[-ν,-μ]]​​GetArray[F[-μ,-ν],{-μ,-ν}]+GetArray[F[-μ,-ν],{-ν,-μ}]
Out[]=
{{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}
Out[]=
{{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}
From the electromagnetic tensor we can extract the vectors of the electric and the magnetic fields (both purely spatial):
In[]:=
F[μ,ν]T[-ν]//GetArray//MatrixForm​​
1
2
ϵ[μ,ν,λ,ρ]F[-λ,-ρ]T[-ν]//GetArray//MatrixForm
Out[]//MatrixForm=
0
-
(0,1,0,0)
ϕ
[t,x,y,z]-
(1,0,0,0)
A1
[t,x,y,z]
-
(0,0,1,0)
ϕ
[t,x,y,z]-
(1,0,0,0)
A2
[t,x,y,z]
-
(0,0,0,1)
ϕ
[t,x,y,z]-
(1,0,0,0)
A3
[t,x,y,z]
Out[]//MatrixForm=
0
-
(0,0,0,1)
A2
[t,x,y,z]+
(0,0,1,0)
A3
[t,x,y,z]
(0,0,0,1)
A1
[t,x,y,z]-
(0,1,0,0)
A3
[t,x,y,z]
-
(0,0,1,0)
A1
[t,x,y,z]+
(0,1,0,0)
A2
[t,x,y,z]
Let us now derive the field part of Maxwell equations. First we define the electromagnetic tensor with the field components:
In[]:=
Ffield[-μ,-ν]≡
0
Ex[t,x,y,z]
Ey[t,x,y,z]
Ez[t,x,y,z]
-Ex[t,x,y,z]
0
-Bz[t,x,y,z]
By[t,x,y,z]
-Ey[t,x,y,z]
Bz[t,x,y,z]
0
-Bx[t,x,y,z]
-Ez[t,x,y,z]
-By[t,x,y,z]
Bx[t,x,y,z]
0
The field part of the homogeneous Maxwell equations then reads (for simplicity we filter out trivial components):
In[]:=
ð[-λ][Ffield[-μ,-ν]]+ð[-μ][Ffield[-ν,-λ]]+ð[-ν][Ffield[-λ,-μ]]//GetArray//Flatten//DeleteCases[0]//DeleteDuplicatesBy[Sort[{#,-#}]&]//Column
Out[]=
(0,0,1,0)
Ex
[t,x,y,z]-
(0,1,0,0)
Ey
[t,x,y,z]-
(1,0,0,0)
Bz
[t,x,y,z]
(0,0,0,1)
Ex
[t,x,y,z]-
(0,1,0,0)
Ez
[t,x,y,z]+
(1,0,0,0)
By
[t,x,y,z]
(0,0,0,1)
Ey
[t,x,y,z]-
(0,0,1,0)
Ez
[t,x,y,z]-
(1,0,0,0)
Bx
[t,x,y,z]
-
(0,0,0,1)
Bz
[t,x,y,z]-
(0,0,1,0)
By
[t,x,y,z]-
(0,1,0,0)
Bx
[t,x,y,z]
The field part of the inhomogeneous Maxwell equations:
In[]:=
ð[-μ][Ffield[μ,ν]]//GetArray//Column
Out[]=
(0,0,0,1)
Ez
[t,x,y,z]+
(0,0,1,0)
Ey
[t,x,y,z]+
(0,1,0,0)
Ex
[t,x,y,z]
-
(0,0,0,1)
By
[t,x,y,z]+
(0,0,1,0)
Bz
[t,x,y,z]-
(1,0,0,0)
Ex
[t,x,y,z]
(0,0,0,1)
Bx
[t,x,y,z]-
(0,1,0,0)
Bz
[t,x,y,z]-
(1,0,0,0)
Ey
[t,x,y,z]
-
(0,0,1,0)
Bx
[t,x,y,z]+
(0,1,0,0)
By
[t,x,y,z]-
(1,0,0,0)
Ez
[t,x,y,z]
The above calculation can also be done using the exterior calculus. Let us obtain the electromagnetic potential 1-form by contracting the potential with the basis of 1-forms:
In[]:=
Aform=A[-μ]basis[μ]//GetArray
Out[]=
-dxA1[t,x,y,z]-dyA2[t,x,y,z]-dzA3[t,x,y,z]+dtϕ[t,x,y,z]
The electromagnetic field 2-form is the exterior derivative of the potential 1-form. It is equal to the previously calculated electromagnetic tensor:
In[]:=
Fform=ExteriorDerivative[][Aform]//FullSimplify​​Fform-F[-μ,-ν]*
1
2
basis[μ]⋀basis[ν]//GetArray//FullSimplify
Out[]=
dy⋀dz
(0,0,0,1)
A2
[t,x,y,z]-
(0,0,1,0)
A3
[t,x,y,z]+dx⋀dy
(0,0,1,0)
A1
[t,x,y,z]-
(0,1,0,0)
A2
[t,x,y,z]+dx⋀dz
(0,0,0,1)
A1
[t,x,y,z]-
(0,1,0,0)
A3
[t,x,y,z]-dt⋀dx
(0,1,0,0)
ϕ
[t,x,y,z]+
(1,0,0,0)
A1
[t,x,y,z]-dt⋀dy
(0,0,1,0)
ϕ
[t,x,y,z]+
(1,0,0,0)
A2
[t,x,y,z]-dt⋀dz
(0,0,0,1)
ϕ
[t,x,y,z]+
(1,0,0,0)
A3
[t,x,y,z]
Out[]=
0
The homogeneous Maxwell equations are trivially satisfied:
In[]:=
ExteriorDerivative[][Fform]//FullSimplify
Out[]=
0
The field part of the inhomogeneous Maxwell equations (for potentials) can be derived using an improvised Hodge star operator:
In[]:=
HodgeStar[f_]:=Module{tmp},​​tmp=f/.{a_⋀b_a⊗b-b⊗a};​​tmp=tmp/.{dtðt,dxðx,dyðy,dzðz};​​
1
2
InteriorProduct[tmp,dt⋀dx⋀dy⋀dz]​​;​​ExteriorDerivative[][HodgeStar[Fform]]//FullSimplify
Out[]=
-(dx⋀dy⋀dz)
(0,0,0,2)
ϕ
[t,x,y,z]+
(0,0,2,0)
ϕ
[t,x,y,z]+
(0,2,0,0)
ϕ
[t,x,y,z]+
(1,0,0,1)
A3
[t,x,y,z]+
(1,0,1,0)
A2
[t,x,y,z]+
(1,1,0,0)
A1
[t,x,y,z]-dt⋀dy⋀dz
(0,0,0,2)
A1
[t,x,y,z]+
(0,0,2,0)
A1
[t,x,y,z]-
(0,1,0,1)
A3
[t,x,y,z]-
(0,1,1,0)
A2
[t,x,y,z]+
(1,1,0,0)
ϕ
[t,x,y,z]+
(2,0,0,0)
A1
[t,x,y,z]+dt⋀dx⋀dz
(0,0,0,2)
A2
[t,x,y,z]-
(0,0,1,1)
A3
[t,x,y,z]-
(0,1,1,0)
A1
[t,x,y,z]+
(0,2,0,0)
A2
[t,x,y,z]+
(1,0,1,0)
ϕ
[t,x,y,z]+
(2,0,0,0)
A2
[t,x,y,z]+dt⋀dx⋀dy
(0,0,1,1)
A2
[t,x,y,z]-
(0,0,2,0)
A3
[t,x,y,z]+
(0,1,0,1)
A1
[t,x,y,z]-
(0,2,0,0)
A3
[t,x,y,z]-
(1,0,0,1)
ϕ
[t,x,y,z]-
(2,0,0,0)
A3
[t,x,y,z]
Let us now derive the field transformations. First we define a general 4-dimensional space:
In[]:=
DefineSpace["Moving",4]
Next we define the transformation matrix from the original space to the moving space with the help of a vielbein (tetrad). As no indices were defined in the moving space we will be using the standard generated indices. Let the new system be moving in the z-direction with velocity v in natural units:
In[]:=
metric[-μ,iMoving1]≡
1
1-
2
v
0
0
-v
1-
2
v
0
1
0
0
0
0
1
0
-v
1-
2
v
0
0
1
1-
2
v
Let us check that the moving space is also Minkowski:
In[]:=
metric[-iMoving1,-iMoving2]//GetArray//Simplify//MatrixForm
Out[]//MatrixForm=
1
0
0
0
0
-1
0
0
0
0
-1
0
0
0
0
-1
We also define the time direction vector in the moving space:
In[]:=
Tmoving[iMoving0]≡{1,0,0,0}
Now we can easily derive the electric and magnetic fields in the moving frame:
In[]:=
Ffield[iMoving0,iMoving1]Tmoving[-iMoving1]//GetArray//FullSimplify//MatrixForm​​
1
2
ϵ[iMoving0,iMoving1,iMoving2,iMoving3]Ffield[-iMoving2,-iMoving3]Tmoving[-iMoving1]//GetArray//FullSimplify//MatrixForm
As a side note we mention, that ð is the partial derivative in the coordinates of its index, therefore the rhs of the first comparison below is in fact zero. One must use ðspace to specify the coordinates explicitly, independent of the index.
When the space is no longer needed the following command effectively deletes it and all definitions connected to it:

Spinors

In this part we will examine applications of SimpleTensor to Dirac and Weyl particles. We start again by defining a Minkowski space with mostly negative metric, Levi-Civita tensor and time-direction 4-vector:
Let us also introduce a convenient conjugate-transpose and adjoint operations:
Next we define a 4-vector of gamma (Dirac) matrices, the “fifth” gamma matrix and a set of sigma matrices:
It is easy to check, for example, that the trace of Gamma matrices product is proportional to the metric:
Next let us find a plane-wave solution to the Dirac equation for a particle with mass m and momentum p. First we define the 4-momentum and write the matrix part of the Dirac equation. Note that when adding a list-valued tensor to an explicit Mathematica list one should use the TensorPlus (⊕) operator. Otherwise Mathematica automatically threads the tensor inside the explicit list before any computation can take place yielding a list of lists in the result.
Let us now find the dispersion. A non-trivial solution exists only when the determinant is non-zero.
This is a doubly-degenerate dispersion for Dirac particles and anti-particles. Let us choose the positive branch and find the conveniently normalized bi-spinor solution:
Let us check that this spinor has the right current (the factor 2 is because of ψ being a bi-spinor):
As a more advanced result, the Berry connection of the bi-spinor is zero because the Berry connections of comprising left and right spinors cancel each other:
Let us now quickly calculate the Berry potential for a Weyl particle, as it will be needed in the next chapter. First we define a 4-vector of Pauli matrices and write the equation for a right-handed Weyl spinor:
Next we find the dispersion:
Now we find the right-handed Weyl spinor and normalize it to 1:
Finally we calculate the Berry potential and check that it indeed gives the monopole in momentum space at the coordinate origin:
Let us now check that the Dirac spinor transforms correctly under Lorentz transformations. For simplicity we consider a Lorentz boost in z-direction and parametrize it with an anti-symmetric tensor:
We can also check the transformation properties of the Dirac matrices directly:
Lastly we clear the Minkowski space to not interfere with the next chapter:

Chiral Kinetic Theory

In this part we will examine applications of SimpleTensor to elementary differential geometric calculations on the example of chiral kinetic theory. This chapter is based mostly on the excellent papers by Stephanov, Yin, and others [Phys. Rev. Lett. 109, 162001] and [Phys. Rev. Lett. 113, 182302]. Note, that because of the partially deliberate lack of optimization in favor of package code clarity, calculations in this chapter can take a few minutes.
In its simplest form chiral particles are living in the Euclidean phase space - a product of 3-dimensional spatial and momentum spaces, coupled to a 1-dimensional time line. Therefore we define:
Next we define in the spatial space a momentum vector and in the momentum space a Berry potential vector that was derived in previous chapter:
Let us also introduce a uniform electric and magnetic fields both pointing in the z-direction. We therefore define the following electromagnetic potential:
The dispersion of a relativistic chiral particle gets modified in the presence of the magnetic field due to its interaction with spin:
Now we have all ingredients to define the Lagrangian 1-form of a chiral particle:
Let us prove that this Lagrangian is indeed Lorentz invariant, at least up to the second order in ℏ. Let us for simplicity consider a boost in the z-direction. In this case the electric and magnetic fields do not change and therefore the Lagrangian is invariant with respect to the transformation if its Lie derivative with the vector field that generates this transformation is zero. The vector field of the Lorentz transformation is then:
The Lagrangian 1-form (without the dt term) works as a soldering form in our phase space and glues the particle velocity to its momentum. The exterior derivative of a soldering form gives the symplectic form of the corresponding phase space. However, our Lagrangian has an extra time coordinate and a term connected to it. The exterior derivative dL gives what is called a pre-symplectic form - an odd-dimensional 2-form whose null vector gives the system evolution and the quotient space with respect to this vector gives the phase-space. The projection of dL to this phase space is the symplectic form. Let us therefore calculate the exterior derivative of L.
ExteriorDerivative with empty brackets is just a shortcut to take the derivative in the product space of all defined spaces:
Let us now find the evolution equations. We define a general vector field v and then demand it to be the null vector of dL, i.e., its contraction with dL must be zero:
This system of equations indeed has the unique solution for the system evolution. The resulting vector reads:
Due to complexity of the evolution it is hard to actually find the canonical coordinates of the phase space and project dL onto them. It is still easy, however, to find the phase space volume, as it can be found from the highest non-trivial power of the symplectic form:
Let us finally check that the Liouville theorem in this theory still holds and the volume form stays invariant during the evolution. In other words, we show that its Lie derivative in the direction of evolution is zero:
Lastly, after all the calculations we tidy up after ourselves: