Relativistic Spacetime in Python...Time Dilation and Length Contractions using Clifford Algebra

This is simple example of what can be done using clifford algebra in the context of Einstein's Special theory of relativity.

Relativistic Spacetime in Python...Time Dilation and Length Contractions using Clifford Algebra
Photo by Harrison Macourt:

In this article, we shall implement Einstein's Special Relativity  using Clifford package for Clifford Algebra in Python. Time Dilation and Length Contraction are core features of the theory that will be shown how to calculate using the clifford package.

Full code for this setup is available at my github page!

Clifford algebra is proving extremely useful in implementing the otherwise colloquially known hard concepts in physics almost a breeze. There have been a significant development in the field of Special Relativity , where it is known that a special class of the Clifford Algebra, which is known as Spacetime Algebra, can simulate the famous Lorentz Transformation using only rotations in this 4-D hyperbolic space.


  1. Geometric Algebra: If you are new to the topic of Geometric Algebra, I highly recommend watching all the videos from sudgylacmoe's channel on
  2. A little of Special Relativity: If new to this, then please watch this amazing animated from a youtuber ScienceClic, for an overview.
  3. Know how to Python.
  4. Basics of PyGame: A game engine used to render our space time diagram.


Geometric Algebra

Special Relativity

NOTE: All the motions are relative unless otherwise stated.

In the theory of Special Relativity, the speed of light will measured to be the same, which is about 300 million m/s, for all observer, no matter how fast the observers themselves are moving. As contradictory as it sounds, it is nonetheless experimentally verified and widely accepted as a fact among physicists.

If you move with a speed of half of the speed of light, and someone turned on a flashlight behind you (light moving in your direction), you would see the light rushing ahead of you with a full value of 300 million Km/s. You, because moving fast, are experiencing, Time Dilation and Length Contraction, which are two of three bizarre implications of special relativity.  

Because of these implications you will end up calculating the same speed of light as an observer at rest. The third implication is the relativity of simultaneity, which says that relatively moving observers will experience different events as happening simultaneous.

Special Relativity is a property of a space which is Hyperbolic in nature and includes time as its fourth dimension. Everyone, if not moving in space, is moving through time and whenever we move in space that motion through time is affected because of the following equation,

$$\tau^{2} = 1 = t^{2} - x^{2} -y^{2}-z^{2}$$

where $\tau$ stands for proper time, which is also the magnitude of the Spacetime vector and which is also the time that you experience in your daily life, and this value is constant, as you would always feel 1 second to be 1 second long, not more and not less. Proper time is Spacetime distance, and the formula indeed looks like a modified version of the Pythagoras Theorem for right triangles.

The hyperbolic nature comes because of the negative(-) signs in front of the space components. So, if you are at rest, we see that the only non-zero component is the component of time, $t$, and as you move, the X components increase, but in order to keep the $\tau$ constant, the time($t$) component is also increased, making you passing faster through time, and experiencing time dilation because the rest of the world is moving slow through time unlike you.

Because there are two kind of times, $\tau$ and $t$, there are two types of speeds, Proper speed($V$) and relative speed($v$). Proper speed is always constant, but the relative speed keeps on changing.  Relative speed is how fast you are moving through the space axis with respect to the time axis.


The time component, if other than 1, gives you directly the Time Dilation factor($\gamma$) for an observer at rest. And the length contraction is given by dividing the length with the Time dilation factor, $l=L/\gamma$. We are not using $c$ anywhere because it is a general practice to keep it equal to 1.

To visualize the hyperbolic nature of space, we can take the time axis and just one space axis, for the illustrative purposes, we use the Minkowski Diagrams.

The diagram shows how your axes are transformed as the subject changes its speed with respect to an observer at rest(only moving in time axis). Notice how it is respecting the lines at 45 degrees(yellow), which is the path for the light.

Spacetime Algebra

The spacetime algebra is built using 4 orthogonal bases vectors, $\gamma_{0-3}$, satisfying,



The generated geometry contains 16 bases elements, which are, 1 scalar, 4 basis vectors, 6 bivectors, 4 trivectors(pseudovectors) and one pseudoscalar($I$).

For a vector in Geometric Algebra, magnitude is equal to its square and for a real spacetime vector, magnitude is 1 (c=1) and it contains 1 time-like basis vector, $\gamma_{0}$ and 1 or more space-like vectors, $\gamma_{1-3}$. For example, a particle at rest will only contain a single time-like basis vector

$$\tau = 1\gamma_{0}$$

As we start to move in a spatial direction with a speed $v$, we construct a new vector with non-zero $\gamma_{1}$ resulting in,

$$\tau = 1\gamma_{0}+v\gamma_{1}$$

but now, our vector is not normalized, magnitude is not 1, so we normalize it,

$$\tau =\frac{1}{\sqrt{(1-v^{2})}} (1\gamma_{0}+v\gamma_{1})$$

What we have done is called a Lorentz Transformation. the first term with the square root is called $\gamma$, the Lorentz Factor. A fun fact about spacetime algebras, Rotations do not change magnitude of a vector, and are applied using bivectors, of which we have 6 in the spacetime algebra,

3 of which are time-like bivectors, which cause rotations in time




and 3 space-like, which cause rotations in space




The space-like bivectors induce rotations in our normal 3-D space but the 3 time-like bivectos are used in creating rotations in time.

Lorentz transformations are just rotations in one of the planes(bivectors) containing the time axis.

The component of $\gamma_{0}$ is called the Lorentz Factor and is directly measured after a Lorentz Transformation. This gives us the time dilation factor! The Length Contraction factor is just the reciprocal of it.

Relative velocity is the ratio of component of space basis vector with time, for example $\frac{component( \gamma_{1})}{ component(\gamma_{0})}$.

Rotations in Geometric Algebra

The induction of rotation is caused by multiplying a bivector. A bivector can also be represented as exponentials, also called as Rotor: $e^{i\theta}$, where $i$ represents a bivector and theta is represented as half of the angle by which we want to rotate the system.

The way we actually rotate is by sandwitching our vector $v$, in between the Rotor,

Rotate $v$ by $\theta$ in the plane of $i$


Spacetime Split

We multiply our vector with $\gamma_{0}$ to produce a scalar(temporal) and a bivector(spatial) component. This process of separating out time and space components is called a Spacetime Split!

$$\tau\gamma_{0} = {Scalar} + {Bivector}$$

In reality, we see the world after the split. All relativistic effects come into effect when we split a vector with a $\gamma_{0}$ of the frame of reference of the observer. In order to account for different observer's perspective, we got multiple ways of splitting a vector, simply by using other observer's $\gamma_{0}$.

The Setup

We are going to create a simple system which encompasses Lorentz Transformation as Boost, rotating the vector in a time-like plane, and analyzing the new component of $\gamma_{0}$ post rotation which gives us the values of Time Dilation factor gamma, $\gamma$.

Adding the imports,

from clifford import Cl, pretty
from clifford import BladeMap

from math import e,pi,sqrt
import pygame

Clifford Algebra setup

Setup the algebra, also known as Dirac Algebra, which includes Pauli Algebra as its sub-algebra, initialize a mapping in between those. This part of code is taken directly from the official documentation example from the Clifford package


# Dirac Algebra  `D`
D, D_blades = Cl(1,3, firstIdx=0, names='d')

# Pauli Algebra  `P`
P, P_blades = Cl(3, names='p')

# put elements of each in namespace

bm = BladeMap([(d01,p1),
               (d0123, p123)])

Create a function which returns a Rotor for a particular angle $\theta$ in a particular Bivector plane.

def lorentzRotor(phi,theta=0,psi=0):
    return e**(-(phi/2.0)*d01)*(e**(-(theta/2.0)*d02))*(e**(-(psi/2.0)*d03))

Here, we use 3 angles which denote the angles from 3 time-like bivectors and then composing rotation in all three axes so we define a smallest boost,

#smallest boost

Initially we are only moving through time, so there is no space component to our vector.


In order to be able to draw on screen we are using PyGame library.

Let's initialize a render function,

def renderFunction(vec):
    window.fill((255, 255, 255))
    #Dot Products in clifford

    text_surface1 = my_font.render('Time Dilation(Gamma):'+str((tl.mag2())), False, (0, 0, 0))
    screen.blit(text_surface1, (0,0))
    text_surface2 = my_font.render('speed(m/s):'+str((sl.mag2()*300000000/tl.mag2())), False, (0, 0, 0))
    screen.blit(text_surface2, (0,25))
    text_surface2 = my_font.render('Length Contraction(1/Gamma):'+str(1/(tl.mag2())), False, (0, 0, 0))
    screen.blit(text_surface2, (0,50))
    #draw axes
    pygame.draw.line(window, (50,50,60), (400,0), (400,600),1)
    pygame.draw.line(window, (50,50,60), (0,300), (800,300),1)

    #draw light paths
    pygame.draw.line(window, (5,5,6), (100,0), (700,600),1)
    pygame.draw.line(window, (5,5,6), (100,600), (700,0),1)

    #draw our 4-D vector
    pygame.draw.line(window, (240,45,67), (400,300), (int(400+(sl)*100),int(300-(tl)*100)),3)
    # pygame.time.wait(1000)

It is in this render function that we calculate the relative velocity, time dilation factor and length contraction factor!

Finally, we add listeners for the keyboard events,

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
        # checking if keydown event happened or not
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                #boost left, by performing a rotation
                start = rotor*start*~rotor
            if event.key == pygame.K_RIGHT:
                #boost right, by performing an inverse rotation
                start = ~rotor*start*rotor

Executing and Testing

Run the code using

$ python3

You should see a window appear and be able to press left and right to perform lorentz transformations!

As you can see that the spacetime vector traces hyperbola.

This was just a bread and butter example of what can be done using Clifford Algebra. Since, the model follows Dirac Algebra, there is relativistic spin quantum mechanics inbuilt into the mathematical model and so is Electrodynamics. We shall cover those in a future article as well as general relativity as gauge theory if I could manage to understand that.


We saw that a special type of Clifford Algebra, spacetime algebra, mimics the real concepts of time dilation and length contractions in the universe with simple calculations, just using multiplication with Rotors. Changing ones perspective can be done using a different kind of a split of the spacetime vector. We rendered the final outcome on a Minkowski Diagram with the help of a simple game engine pygame.

Thank-you for reading.

Sign up for the blog-newsletter! I promise that we do not spam...