# Source

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56``` ```import math class Quat(object): def __init__(self,w,x,y,z): self.w = w self.x = x self.y = y self.z = z def __mul__(self,other): try: w = self.w*other.w - self.x*other.x - self.y*other.y - self.z*other.z x = self.w*other.x + self.x*other.w + self.y*other.z - self.z*other.y y = self.w*other.y - self.x*other.z + self.y*other.w + self.z*other.x z = self.w*other.z + self.x*other.y - self.y*other.x + self.z*other.w except AttributeError: w = self.w*other x = self.x*other y = self.y*other z = self.z*other return Quat(w,x,y,z) def __rmul__(self,other): w = self.w*other x = self.x*other y = self.y*other z = self.z*other return Quat(w,x,y,z) def __str__(self): return '({0}, {1}, {2}, {3})'.format(self.w, self.x, self.y, self.z) def conj(self): return Quat(self.w,-self.x,-self.y,-self.z) def mag(self): return math.sqrt(self.w**2 + self.x**2 + self.y**2 + self.z**2) def inv(self): return self.conj()*(1.0/self.mag()) def quatFromAxisAngle(ax,ang): w = math.cos(0.5*ang) x = ax[0]*math.sin(0.5*ang) y = ax[1]*math.sin(0.5*ang) z = ax[2]*math.sin(0.5*ang) return Quat(w,x,y,z) # ------------------------------------------------------------------------------------- if __name__ == '__main__': q = quatFromAxisAngle((1,0,0),math.radians(45.0)) print q ```