Skip to content

Instantly share code, notes, and snippets.

@rehno-lindeque
Last active September 11, 2023 18:51
Show Gist options
  • Save rehno-lindeque/5b6f9500f390541e2a0aa3fc32b861ef to your computer and use it in GitHub Desktop.
Save rehno-lindeque/5b6f9500f390541e2a0aa3fc32b861ef to your computer and use it in GitHub Desktop.
Lorentz boost animation
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Define the SplitComplex class
class SplitComplex:
def __init__(self, real, j_component):
self.real = real
self.j_component = j_component
def __mul__(self, other):
real_part = self.real * other.real + self.j_component * other.j_component
j_part = self.real * other.j_component + self.j_component * other.real
return SplitComplex(real_part, j_part)
# Updated Lorentz boost function using the SplitComplex class and the boost
def lorentz_boost(t, x, boost):
event = SplitComplex(t, x)
transformed_event = boost * event
return transformed_event.real, transformed_event.j_component
# Create a grid of events in spacetime
t, x = np.meshgrid(np.linspace(-2, 2, 10), np.linspace(-2, 2, 10))
t = t.flatten()
x = x.flatten()
# Define highlighted points and their paths
highlighted_points = [
(1.5, 0.5),
(0.5, 1.5),
(-1.5, -0.5),
(-0.5, -1.5)
]
paths = {point: {'x': [], 'y': []} for point in highlighted_points}
# Update function for the animation
def update(phi):
ax.clear() # Clear the entire axes
# Reset basic settings for the ax after clearing
ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
ax.set_xlabel('x')
ax.set_ylabel('ct')
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.axline((0, 0), slope=1, color='red', linestyle='--', linewidth=0.5)
ax.axline((0, 0), slope=-1, color='red', linestyle='--', linewidth=0.5)
# Use the boost for the transformation
boost = SplitComplex(np.cosh(phi), np.sinh(phi))
t_prime, x_prime = lorentz_boost(t, x, boost)
ax.scatter(x_prime, t_prime, color='blue')
for point in highlighted_points:
t_point, x_point = point
t_prime_point, x_prime_point = lorentz_boost(t_point, x_point, boost)
ax.scatter(x_prime_point, t_prime_point, color='red', s=50, zorder=5)
paths[point]['x'].append(t_prime_point)
paths[point]['y'].append(x_prime_point)
for point, path in paths.items():
ax.plot(path['x'], path['y'], color='green', alpha=0.5)
ax.set_title(f"multiplication by {boost.real:.2f} + {boost.j_component:.2f}j")
# Create the figure for the animation
fig, ax = plt.subplots(figsize=(6, 6))
# Create and save the updated animation
ani = FuncAnimation(fig, update, frames=np.linspace(-1, 1, 20), blit=False, repeat=True)
gif_path = "./lorentz_boost_animation_cleaned.gif"
ani.save(gif_path, writer='pillow', fps=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment