2019-04-17 12:16:03 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2019-04-19 11:34:04 +05:30
|
|
|
P = -1 # convention (see DOI:10.1088/0965-0393/23/8/083501)
|
2019-04-17 12:16:03 +05:30
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
class Quaternion:
|
|
|
|
u"""
|
|
|
|
Quaternion with basic operations
|
|
|
|
|
|
|
|
q is the real part, p = (x, y, z) are the imaginary parts.
|
2019-04-19 04:12:20 +05:30
|
|
|
Definition of multiplication depends on variable P, P ∈ {-1,1}.
|
2019-04-17 12:16:03 +05:30
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
q = 0.0,
|
|
|
|
p = np.zeros(3,dtype=float)):
|
|
|
|
"""Initializes to identity unless specified"""
|
2019-04-19 04:12:20 +05:30
|
|
|
self.q = float(q)
|
|
|
|
self.p = np.array(p,dtype=float)
|
2019-04-17 12:16:03 +05:30
|
|
|
|
|
|
|
|
|
|
|
def __copy__(self):
|
|
|
|
"""Copy"""
|
|
|
|
return self.__class__(q=self.q,
|
|
|
|
p=self.p.copy())
|
|
|
|
|
|
|
|
copy = __copy__
|
|
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""Components"""
|
|
|
|
return iter(self.asList())
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
"""Readable string"""
|
|
|
|
return 'Quaternion: (real={q:+.6f}, imag=<{p[0]:+.6f}, {p[1]:+.6f}, {p[2]:+.6f}>)'.format(q=self.q,p=self.p)
|
|
|
|
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
"""Addition"""
|
|
|
|
if isinstance(other, Quaternion):
|
|
|
|
return self.__class__(q=self.q + other.q,
|
|
|
|
p=self.p + other.p)
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __iadd__(self, other):
|
|
|
|
"""In-place addition"""
|
|
|
|
if isinstance(other, Quaternion):
|
|
|
|
self.q += other.q
|
|
|
|
self.p += other.p
|
|
|
|
return self
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __pos__(self):
|
|
|
|
"""Unary positive operator"""
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
def __sub__(self, other):
|
|
|
|
"""Subtraction"""
|
|
|
|
if isinstance(other, Quaternion):
|
|
|
|
return self.__class__(q=self.q - other.q,
|
|
|
|
p=self.p - other.p)
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __isub__(self, other):
|
|
|
|
"""In-place subtraction"""
|
|
|
|
if isinstance(other, Quaternion):
|
|
|
|
self.q -= other.q
|
|
|
|
self.p -= other.p
|
|
|
|
return self
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __neg__(self):
|
2019-05-06 02:05:12 +05:30
|
|
|
"""Unary negative operator"""
|
|
|
|
return self * -1.0
|
2019-04-17 12:16:03 +05:30
|
|
|
|
|
|
|
|
|
|
|
def __mul__(self, other):
|
|
|
|
"""Multiplication with quaternion or scalar"""
|
|
|
|
if isinstance(other, Quaternion):
|
|
|
|
return self.__class__(q=self.q*other.q - np.dot(self.p,other.p),
|
|
|
|
p=self.q*other.p + other.q*self.p + P * np.cross(self.p,other.p))
|
|
|
|
elif isinstance(other, (int, float)):
|
|
|
|
return self.__class__(q=self.q*other,
|
|
|
|
p=self.p*other)
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __imul__(self, other):
|
|
|
|
"""In-place multiplication with quaternion or scalar"""
|
|
|
|
if isinstance(other, Quaternion):
|
|
|
|
self.q = self.q*other.q - np.dot(self.p,other.p)
|
|
|
|
self.p = self.q*other.p + other.q*self.p + P * np.cross(self.p,other.p)
|
|
|
|
return self
|
|
|
|
elif isinstance(other, (int, float)):
|
2019-04-19 04:12:20 +05:30
|
|
|
self.q *= other
|
|
|
|
self.p *= other
|
2019-04-17 12:16:03 +05:30
|
|
|
return self
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
def __truediv__(self, other):
|
|
|
|
"""Divsion with quaternion or scalar"""
|
|
|
|
if isinstance(other, Quaternion):
|
|
|
|
s = other.conjugate()/abs(other)**2.
|
|
|
|
return self.__class__(q=self.q * s,
|
|
|
|
p=self.p * s)
|
|
|
|
elif isinstance(other, (int, float)):
|
2019-04-19 04:12:20 +05:30
|
|
|
return self.__class__(q=self.q / other,
|
|
|
|
p=self.p / other)
|
2019-04-17 12:16:03 +05:30
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __itruediv__(self, other):
|
|
|
|
"""In-place divsion with quaternion or scalar"""
|
|
|
|
if isinstance(other, Quaternion):
|
|
|
|
s = other.conjugate()/abs(other)**2.
|
|
|
|
self *= s
|
|
|
|
return self
|
|
|
|
elif isinstance(other, (int, float)):
|
|
|
|
self.q /= other
|
2019-04-19 04:12:20 +05:30
|
|
|
self.p /= other
|
2019-04-17 12:16:03 +05:30
|
|
|
return self
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
def __pow__(self, exponent):
|
|
|
|
"""Power"""
|
|
|
|
if isinstance(exponent, (int, float)):
|
|
|
|
omega = np.acos(self.q)
|
|
|
|
return self.__class__(q= np.cos(exponent*omega),
|
|
|
|
p=self.p * np.sin(exponent*omega)/np.sin(omega))
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __ipow__(self, exponent):
|
|
|
|
"""In-place power"""
|
|
|
|
if isinstance(exponent, (int, float)):
|
|
|
|
omega = np.acos(self.q)
|
|
|
|
self.q = np.cos(exponent*omega)
|
|
|
|
self.p *= np.sin(exponent*omega)/np.sin(omega)
|
2019-04-19 04:12:20 +05:30
|
|
|
return self
|
2019-04-17 12:16:03 +05:30
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
def __abs__(self):
|
|
|
|
"""Norm"""
|
|
|
|
return np.sqrt(self.q ** 2 + np.dot(self.p,self.p))
|
|
|
|
|
|
|
|
magnitude = __abs__
|
|
|
|
|
|
|
|
|
|
|
|
def __eq__(self,other):
|
|
|
|
"""Equal (sufficiently close) to each other"""
|
|
|
|
return np.isclose(( self-other).magnitude(),0.0) \
|
|
|
|
or np.isclose((-self-other).magnitude(),0.0)
|
|
|
|
|
|
|
|
def __ne__(self,other):
|
|
|
|
"""Not equal (sufficiently close) to each other"""
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
|
|
|
|
def asM(self):
|
|
|
|
"""Intermediate representation useful for quaternion averaging (see F. Landis Markley et al.)"""
|
|
|
|
return np.outer(self.asArray(),self.asArray())
|
|
|
|
|
|
|
|
def asArray(self):
|
|
|
|
"""As numpy array"""
|
|
|
|
return np.array((self.q,self.p[0],self.p[1],self.p[2]))
|
|
|
|
|
|
|
|
def asList(self):
|
2019-04-19 04:12:20 +05:30
|
|
|
"""As list"""
|
2019-04-17 12:16:03 +05:30
|
|
|
return [self.q]+list(self.p)
|
|
|
|
|
2019-04-19 04:12:20 +05:30
|
|
|
|
2019-04-17 12:16:03 +05:30
|
|
|
def normalize(self):
|
2019-04-19 11:34:04 +05:30
|
|
|
"""Normalizes in-place"""
|
2019-04-17 12:16:03 +05:30
|
|
|
d = self.magnitude()
|
2019-04-19 04:12:20 +05:30
|
|
|
if d > 0.0: self /= d
|
2019-04-19 11:34:04 +05:30
|
|
|
return self
|
2019-04-17 12:16:03 +05:30
|
|
|
|
|
|
|
def normalized(self):
|
2019-04-19 04:12:20 +05:30
|
|
|
"""Returns normalized copy"""
|
2019-04-19 11:47:02 +05:30
|
|
|
return self.copy().normalize()
|
2019-04-17 12:16:03 +05:30
|
|
|
|
2019-04-19 04:12:20 +05:30
|
|
|
|
2019-04-17 12:16:03 +05:30
|
|
|
def conjugate(self):
|
2019-04-19 11:34:04 +05:30
|
|
|
"""Conjugates in-place"""
|
2019-04-17 12:16:03 +05:30
|
|
|
self.p = -self.p
|
2019-04-19 11:34:04 +05:30
|
|
|
return self
|
2019-04-17 12:16:03 +05:30
|
|
|
|
|
|
|
def conjugated(self):
|
2019-04-19 04:12:20 +05:30
|
|
|
"""Returns conjugated copy"""
|
2019-04-19 11:47:02 +05:30
|
|
|
return self.copy().conjugate()
|
2019-04-17 12:16:03 +05:30
|
|
|
|
|
|
|
|
|
|
|
def homomorph(self):
|
2019-04-19 11:34:04 +05:30
|
|
|
"""Homomorphs in-place"""
|
2019-05-06 02:05:12 +05:30
|
|
|
self *= -1.0
|
2019-04-19 11:34:04 +05:30
|
|
|
return self
|
2019-04-17 12:16:03 +05:30
|
|
|
|
|
|
|
def homomorphed(self):
|
2019-04-19 04:12:20 +05:30
|
|
|
"""Returns homomorphed copy"""
|
2019-04-19 11:47:02 +05:30
|
|
|
return self.copy().homomorph()
|