Quaternions in Python
Johann Hibschman
johann at physics.berkeley.edu
Sat Oct 6 16:05:19 EDT 2001
More information about the Python-list mailing list
Sat Oct 6 16:05:19 EDT 2001
- Previous message (by thread): Quaternions in Python
- Next message (by thread): Quaternions in Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
>>>>> "Erik" == Erik Max Francis <max at alcyone.com> writes: Erik> That's perfectly valid, but I don't think it's what the Erik> poster I responded to meant. He meant encoding the Erik> transform represented by the quarternion into a matrix. But Pauli spin matrices are matrices! But I get the point that you're thinking of real matrices. It doesn't mean that NumPy doesn't make this easy. You could do something like: import Numeric class Quat: def __init__(self, matrix): self.matrix = matrix # Extracting components (Is this right?) def c0(self): m = self.matrix return (m[0][0]+m[1][1])/2.0 def c1(self): m = self.matrix return (m[0][1]+m[1][0])/2.0 def c2(self): m = self.matrix return 1.0j*(m[0][1]-m[1][0])/2.0 def c3(self): m = self.matrix return (m[0][0]-m[1][1])/2.0 # Math def __mul__(self, other): if hasattr(other, 'matrix'): return Quat(Numeric.matrixmultiply(self.matrix, other.matrix)) else: return Quat(self.matrix * other) def __rmul__(self, other): return Quat(other * self.matrix) def __add__(self, other): if hasattr(other, 'matrix'): return Quat(self.matrix + other.matrix) else: return Quat(self.matrix + other) def __radd__(self, other): return Quat(other + self.matrix) def __sub__(self, other): if hasattr(other, 'matrix'): return Quat(self.matrix - other.matrix) else: return Quat(self.matrix - other) def __rsub__(self, other): return Quat(other - self.matrix) # Basis s0 = Quat(Numeric.array([[1.0, 0.0], [0.0, 1.0]])) s1 = Quat(Numeric.array([[0.0, 1.0], [1.0, 0.0]])) s2 = Quat(Numeric.array([[0.0, -1.0j], [1.0j, 0.0]])) s3 = Quat(Numeric.array([[1.0, 0.0], [0.0, -1.0]])) # Create from components. def q(i, j, k, l): "Create a quaternion from components." return s0*i+s1*j+s2*k+s3*l
- Previous message (by thread): Quaternions in Python
- Next message (by thread): Quaternions in Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list