Gwaihir
6/23/2025, 8:06:37 PM No.16705844
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Quaternion utility functions
def quat_mult(q1, q2):
# Hamilton product of two quaternions q = [w, x, y, z]
w1, x1, y1, z1 = q1
w2, x2, y2, z2 = q2
w = w1*w2 - x1*x2 - y1*y2 - z1*z2
x = w1*x2 + x1*w2 + y1*z2 - z1*y2
y = w1*y2 - x1*z2 + y1*w2 + z1*x2
z = w1*z2 + x1*y2 - y1*x2 + z1*w2
return np.array([w, x, y, z])
def quat_conj(q):
w, x, y, z = q
return np.array([w, -x, -y, -z])
def quat_rotate_vector(v, q):
# Rotate vector v by unit quaternion q: q * v_quat * q_conj
v_quat = np.array([0] + list(v))
return quat_mult(quat_mult(q, v_quat), quat_conj(q))[1:]
# Generate a circular orbit in XY plane with radius r
def generate_circular_orbit(radius, num_points=100):
angles = np.linspace(0, 2*np.pi, num_points)
positions = np.stack((radius * np.cos(angles),
radius * np.sin(angles),
np.zeros_like(angles)), axis=1)
return positions
# Create a unit quaternion representing rotation about axis by angle (radians)
def axis_angle_to_quat(axis, angle):
axis = axis / np.linalg.norm(axis)
w = np.cos(angle / 2)
xyz = axis * np.sin(angle / 2)
return np.array([w, *xyz])
# Main visualization function
def quaternionic_orbit_morphing():
radius = 1.0
orbit = generate_circular_orbit(radius)
num_points = len(orbit)
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-1.5*radius, 1.5*radius)
ax.set_ylim(-1.5*radius, 1.5*radius)
ax.set_zlim(-1.5*radius, 1.5*radius)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Quaternionic Orbit Morphing')
line, = ax.plot([], [], [], lw=2)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Quaternion utility functions
def quat_mult(q1, q2):
# Hamilton product of two quaternions q = [w, x, y, z]
w1, x1, y1, z1 = q1
w2, x2, y2, z2 = q2
w = w1*w2 - x1*x2 - y1*y2 - z1*z2
x = w1*x2 + x1*w2 + y1*z2 - z1*y2
y = w1*y2 - x1*z2 + y1*w2 + z1*x2
z = w1*z2 + x1*y2 - y1*x2 + z1*w2
return np.array([w, x, y, z])
def quat_conj(q):
w, x, y, z = q
return np.array([w, -x, -y, -z])
def quat_rotate_vector(v, q):
# Rotate vector v by unit quaternion q: q * v_quat * q_conj
v_quat = np.array([0] + list(v))
return quat_mult(quat_mult(q, v_quat), quat_conj(q))[1:]
# Generate a circular orbit in XY plane with radius r
def generate_circular_orbit(radius, num_points=100):
angles = np.linspace(0, 2*np.pi, num_points)
positions = np.stack((radius * np.cos(angles),
radius * np.sin(angles),
np.zeros_like(angles)), axis=1)
return positions
# Create a unit quaternion representing rotation about axis by angle (radians)
def axis_angle_to_quat(axis, angle):
axis = axis / np.linalg.norm(axis)
w = np.cos(angle / 2)
xyz = axis * np.sin(angle / 2)
return np.array([w, *xyz])
# Main visualization function
def quaternionic_orbit_morphing():
radius = 1.0
orbit = generate_circular_orbit(radius)
num_points = len(orbit)
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-1.5*radius, 1.5*radius)
ax.set_ylim(-1.5*radius, 1.5*radius)
ax.set_zlim(-1.5*radius, 1.5*radius)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Quaternionic Orbit Morphing')
line, = ax.plot([], [], [], lw=2)
Replies: