Quaternion Addition

325 days ago by sweetser

r""" Tools to add quaternions Description: This notebook shows how to generate one quaternion added to another many times. If the quaternion is viewed as an event in spacetime, then constantly adding the same quaternion generates constant motion, an inertial observer in the physics lexicon. It is remarkable that the simplest mathematical act with quaternions - addition - leads directly to an inertial observer, the key player in special relativity. AUTHORS: -- Doug Sweetser (2009): Initial version. EXAMPLES: Add quaternions 3 times sage: q1 = q(1,1,1,1); q2 = q(1,2,3,4); q3 = q(3, 5, 7, 9) sage: test = add_n(q2, 3) sage: q3 == test True """ #***************************************************************************** # Copyright (C) 2009 Doug Sweetser <sweetser@alum.mit.edu> # # Distributed under the terms of the GNU General Public License (GPL) # http://www.gnu.org/licenses/ #***************************************************************************** 
       
class q(list): def __init__(self,t,x,y,z): self.t = t self.x = x self.y = y self.z = z self.qn = [] list.__init__(self,[t,x,y,z]) def __repr__(self): return "Quaternion tools." def show(self): print self.t, self.x, self.y, self.z def add(self, q1): return q(self.t + q1.t, self.x + q1.x, self.y + q1.y, self.z + q1.z) def add_n(self, q1, n): for i in range (0, n): t = self.t + i * q1.t x = self.x + i * q1.x y = self.y + i * q1.y z = self.z + i * q1.z self.qn.append(q(t, x, y, z)) return q(t, x, y, z) #auto 
       
Test Cases 
       
# Examples q1 = q(1,1,1,1); q2 = q(1,2,3,4); q3 = q(3, 5, 7, 9) test = q1.add_n(q2, 3) q3 == test 
       
True
q1=q(1,1,1,1) q2=q(1,2,3,4) q1.show() q2.show() q1.add(q2) q1.add_n(q2, 3) for r in q1.qn: r.show() 
       
1 1 1 1
1 2 3 4
1 1 1 1
2 3 4 5
3 5 7 9
q1 = q(-1,-2,3,-4) q2 = q(.2,.3,-.4,.4) q11.add_n(q2, 10) for r in q11.qn: r.show() 
       
0.800000000000000 0.700000000000000 -0.600000000000000
-0.400000000000000
1.00000000000000 1.00000000000000 -1.00000000000000
1.11022302462516e-16
1.20000000000000 1.30000000000000 -1.40000000000000
0.400000000000000
1.40000000000000 1.60000000000000 -1.80000000000000
0.800000000000000
1.60000000000000 1.90000000000000 -2.20000000000000 1.20000000000000
1.80000000000000 2.20000000000000 -2.60000000000000 1.60000000000000
2.00000000000000 2.50000000000000 -3.00000000000000 2.00000000000000
2.20000000000000 2.80000000000000 -3.40000000000000 2.40000000000000
2.40000000000000 3.10000000000000 -3.80000000000000 2.80000000000000
2.60000000000000 3.40000000000000 -4.20000000000000 3.20000000000000
Conclusion: Repeated addition is used more often than any other function for quaternion animations since it represents an inertial observer.