Overview

The DiracMatrix package (Github link) constructs Dirac gamma matrices that satisfy the Clifford anticommutation relation for an arbitrary real symmetric metric in any dimension. It uses the Brauer–Weyl (Pauli–Kronecker) construction in flat space and extends to curved/non-flat metrics through a vielbein decomposition. It also exposes basis transformations into the Dirac and Weyl/chiral representations and two implementations of the canonical graded Clifford operator basis.
The package source lives at Kernel/DiracMatrix.wl. The exported public symbols are GammaMatrices, EuclideanGammaMatrices, FlatMetric, RandomCurvedMetric, MetricVielbein, ToDiracBasis, ToWeylBasis, NumericZeroQ, CliffordCanonicalBasis, and CliffordBasis.

Mathematical background

Clifford relation

The defining property of the Dirac gamma matrices is the Clifford anticommutation relation:
{
μ
γ
,
ν
γ
}=
μ
γ
ν
γ
+
ν
γ
μ
γ
=2
μν
η

N
where N = 2^Floor[n/2] is the spinor dimension for an n-dimensional spacetime.

Brauer-Weyl (Pauli-Kronecker) representation

For n = 2m or n = 2m + 1, a flat-space realisation in terms of Pauli matrices σ_j is:
2k-1
Γ
=
⊗(k-1)

⊗
σ
1
⊗
⊗(m-k)
σ
3
2k
Γ
=
⊗(k-1)

⊗
σ
2
⊗
⊗(m-k)
σ
3
and, for odd n, an additional
2m+1
Γ
=
⊗m
σ
3

Signed-diagonal metric

For a flat metric
η
μν
= diag(+1, ..., +1, -1, ..., -1) with p positive entries and q negative entries, the gamma matrices are:
μ
γ
=
μ
Γ
if
μμ
η
=+1
μ
γ
=
μ
Γ
if
μμ
η
=-1
The resulting algebra is Cl(p, q).

Vielbein construction for general metrics

For a general real symmetric metric g, a vielbein
a
e
μ
(x) is computed such that
g
μν
=
a
e
μ
b
e
ν
η
ab
(equivalently g =
T
e
· η · e), and:
μ
γ
(x)=
μ
e
a
a
Γ
where η is the signature matrix and Γ^a are the flat-space gamma matrices in the orthonormal frame.

Loading the package

PacletDirectoryLoad["path/DiracMatrixPackage"];​​Needs["DiracMatrix`"]

Function reference

List of public symbols:
In[]:=
?DiracMatrix`*
Out[]=
DiracMatrix`
CliffordBasis
EuclideanGammaMatrices
GammaMatrices
NumericZeroQ
ToDiracBasis
CliffordCanonicalBasis
FlatMetric
MetricVielbein
RandomCurvedMetric
ToWeylBasis
Selected usage messages:
In[]:=
?GammaMatrices
Out[]=
Symbol
GammaMatrices[n] returns n Euclidean gamma matrices of dimension 2^Floor[n/2] in the Brauer–Weyl (Pauli–Kronecker) representation.GammaMatrices[η] returns gamma matrices satisfying the Clifford relation {​
μ
γ
​,​
ν
γ
​} = 2
μν
η
 for any real symmetric metric η. For a flat diagonal metric of signature (p,q) the result is 
μ
Γ
for time-like indices and
μ
Γ
for space-like indices; for a general metric a vielbein decomposition is used.
In[]:=
?EuclideanGammaMatrices
Out[]=
Symbol
EuclideanGammaMatrices[n] returns the n Euclidean gamma matrices in the Brauer–Weyl representation:
2 k - 1
Γ
=
⊗ (k - 1)

⊗
σ
1
⊗
⊗ (m - k)
σ
3
​,
2 k
Γ
=
⊗ (k - 1)

⊗
σ
2
⊗
⊗ (m - k)
σ
3
​, and for odd n a final
2 m + 1
Γ
=
⊗ m
σ
3
.
In[]:=
?FlatMetric
Out[]=
Symbol
FlatMetric[p, q] returns the diagonal flat metric DiagonalMatrix[Join[ConstantArray[1,p], ConstantArray[-1,q]]] of signature (p,q).
In[]:=
?RandomCurvedMetric
Out[]=
Symbol
RandomCurvedMetric[p, q] (or RandomCurvedMetric[p, q, a, s]​) returns a random real symmetric non-diagonal metric of signature (p,q) constructed as
T
Q
. Λ . Q with Q ∈ O(n) a random orthogonal matrix and Λ a diagonal matrix whose first p eigenvalues are
2
s
r
10
and last q eigenvalues are -
2
s
r
10
for independent r drawn uniformly from [-a, a]. The default a = 2 gives a condition number κ ~
2 a
10
​; s = 1 sets the typical magnitude of the metric components.
In[]:=
?MetricVielbein
Out[]=
Symbol
MetricVielbein[η] returns {signature, vielbein} for the real symmetric metric η, where signature = DiagonalMatrix[Sign[eigenvalues]​] and the vielbein e satisfies
T
e
. signature . e = η.
In[]:=
?ToDiracBasis
Out[]=
Symbol
ToDiracBasis[γ] takes a list of gamma matrices in the Brauer–Weyl basis and returns them in the Dirac basis where
0
γ
is diagonal.
In[]:=
?ToWeylBasis
Out[]=
Symbol
ToWeylBasis[γ] takes a list of gamma matrices in the Brauer–Weyl basis and returns them in the Weyl/chiral basis. For odd dimension there is no chiral decomposition; the Dirac basis is returned with a message.
In[]:=
?CliffordBasis
Out[]=
Symbol
CliffordBasis[η] returns the canonical graded Clifford operator basis using the antisymmetrised recursion
Γ
ℐ ⋃ {μ}
=
Γ
ℐ
·
Γ
μ
-
|ℐ|
∑
j = 1
j - 1
(-1)
η
μ, i_j
Γ
ℐ ∖ {i_j}
. Much faster than CliffordCanonicalBasis and numerically stable; agrees with it on flat metrics at all grades.
In[]:=
?CliffordCanonicalBasis
Out[]=
Symbol
CliffordCanonicalBasis[η] returns the canonical graded operator basis of the Clifford algebra Cl(η) as a list grouped by grade {grade-0, grade-1, …, grade-n}. The grade-k element on indices
i
1
​, …,
i
k
is the explicitly antisymmetrised product
1
k!
∑
σ ∈ S_k
sgn(σ)
Γ
i_(σ(1))
· · ·
Γ
i_(σ(k))
. Pedagogically transparent but combinatorially expensive; for non-flat metrics with widely scaled eigenvalues precision can degrade at high grades. Prefer CliffordBasis for production use.
In[]:=
?NumericZeroQ
Out[]=
Symbol
NumericZeroQ[a] returns True if every entry of a after Chop with tolerance
-8
10
​ is zero.NumericZeroQ[a, tol] uses the supplied tolerance.

Examples

Euclidean space (n = 4)

In Euclidean space the metric is the identity and the gamma matrices coincide with the flat-space Γ matrices.
In[]:=
MatrixForm/@GammaMatrices[4]
Out[]=

0
0
1
0
0
0
0
-1
1
0
0
0
0
-1
0
0
,
0
0
-
0
0
0
0


0
0
0
0
-
0
0
,
0
1
0
0
1
0
0
0
0
0
0
1
0
0
1
0
,
0
-
0
0

0
0
0
0
0
0
-
0
0

0


Minkowski space (1, 3)

The mostly-minus Minkowski metric. By default the result is in the Brauer-Weyl basis.
In[]:=
MatrixForm@FlatMetric[1,3]
Out[]//MatrixForm=
1
0
0
0
0
-1
0
0
0
0
-1
0
0
0
0
-1
In[]:=
MatrixForm/@GammaMatrices[ηMink]
Out[]=
GammaMatrices[ηMink]

Split signature (2, 2)

Used in twistor theory and some lower-dimensional supersymmetric models.
In[]:=
MatrixForm/@GammaMatrices[FlatMetric[2,2]]
Out[]=

0
0
1
0
0
0
0
-1
1
0
0
0
0
-1
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

0
,
0
1
0
0
-1
0
0
0
0
0
0
1
0
0
-1
0


Six-dimensional Euclidean

The construction extends to any dimension; for n = 6 we obtain six 8x8 matrices.

Schwarzschild metric at r = 3M

Evaluate the Schwarzschild metric components on the equatorial plane at r = 3M, giving diag(-1/3, 3, 9, 9).

FLRW metric with scale factor a = 2

A spatially-flat FLRW slice with a = 2.

Random non-diagonal metric

A random real symmetric non-diagonal metric of signature (2, 2). The vielbein decomposition handles the general case.

Basis transformations

Brauer-Weyl basis (default output of GammaMatrices)

In[]:=
MatrixForm /@ GammaMatrices[FlatMetric[1, 3]]

Dirac basis

Weyl/chiral basis

The Weyl basis block-diagonalises the chirality operator.
For odd dimension there is no Weyl/chiral decomposition; the function emits a message and returns the Dirac basis instead.

Verification of standard identities

Each test below evaluates to True if the identity is satisfied for the listed range. For non-flat metrics, equality is checked numerically with NumericZeroQ.

Number of matrices and spinor dimension

There are n gamma matrices, each of size 2^Floor[n/2] x 2^Floor[n/2].

Tracelessness

Anticommutator (Clifford relation)

Square of a single gamma

Trace of two gammas

Trace of four gammas

Contraction identities

Commutator identity

Chirality (even dimension)

It is Hermitian, squares to the identity, and anticommutes with every gamma.

Non-flat metrics

The same identities hold for arbitrary real symmetric metrics; numerical equality is checked with NumericZeroQ.

Canonical Clifford operator basis

The grade-k element of the canonical Clifford operator basis is the antisymmetrised product

Direct evaluation

CliffordCanonicalBasis is a literal transcription of the formula above. It is pedagogically transparent but combinatorially expensive, and on non-flat metrics it can lose precision at high grades because of cancellations between terms of widely different magnitudes.

Antisymmetric recursion (production-ready)

CliffordBasis grows the basis grade-by-grade using the recursion
This avoids the explicit antisymmetrisation sum and is much more numerically stable. For flat metrics it agrees with CliffordCanonicalBasis at all grades.

Drift on non-flat metrics at high grade

For random non-flat metrics the two implementations agree at low grades but the explicit antisymmetrisation accumulates error at grades ≥ 4.
The mismatch at higher grades reflects numerical cancellation in the explicit-sum implementation, not an algebraic disagreement; prefer CliffordBasis in production.

CITE THIS NOTEBOOK

Dirac matrices in a curved spacetime​
by Mohammad Bahrami​
Wolfram Community, STAFF PICKS, May 2, 2026
​https://community.wolfram.com/groups/-/m/t/3710334