quaternion.py
Go to the documentation of this file.
1 """ run with: python3 -m sophus.quaternion """
2 
3 import sophus
4 import sympy
5 import sys
6 import unittest
7 
8 
9 class Quaternion:
10  """ Quaternion class """
11 
12  def __init__(self, real, vec):
13  """ Quaternion consists of a real scalar, and an imaginary 3-vector """
14  assert isinstance(vec, sympy.Matrix)
15  assert vec.shape == (3, 1), vec.shape
16  self.real = real
17  self.vec = vec
18 
19  def __mul__(self, right):
20  """ quaternion multiplication """
21  return Quaternion(self[3] * right[3] - self.vec.dot(right.vec),
22  self[3] * right.vec + right[3] * self.vec +
23  self.vec.cross(right.vec))
24 
25  def __add__(self, right):
26  """ quaternion multiplication """
27  return Quaternion(self[3] + right[3], self.vec + right.vec)
28 
29  def __neg__(self):
30  return Quaternion(-self[3], -self.vec)
31 
32  def __truediv__(self, scalar):
33  """ scalar division """
34  return Quaternion(self.real / scalar, self.vec / scalar)
35 
36  def __repr__(self):
37  return "( " + repr(self[3]) + " + " + repr(self.vec) + "i )"
38 
39  def __getitem__(self, key):
40  """ We use the following convention [vec0, vec1, vec2, real] """
41  assert (key >= 0 and key < 4)
42  if key == 3:
43  return self.real
44  else:
45  return self.vec[key]
46 
47  def squared_norm(self):
48  """ squared norm when considering the quaternion as 4-tuple """
49  return sophus.squared_norm(self.vec) + self.real**2
50 
51  def conj(self):
52  """ quaternion conjugate """
53  return Quaternion(self.real, -self.vec)
54 
55  def inv(self):
56  """ quaternion inverse """
57  return self.conj() / self.squared_norm()
58 
59  @staticmethod
60  def identity():
61  return Quaternion(1, sophus.Vector3(0, 0, 0))
62 
63  @staticmethod
64  def zero():
65  return Quaternion(0, sophus.Vector3(0, 0, 0))
66 
67  def subs(self, x, y):
68  return Quaternion(self.real.subs(x, y), self.vec.subs(x, y))
69 
70  def simplify(self):
71  v = sympy.simplify(self.vec)
72  return Quaternion(sympy.simplify(self.real),
73  sophus.Vector3(v[0], v[1], v[2]))
74 
75  def __eq__(self, other):
76  if isinstance(self, other.__class__):
77  return self.real == other.real and self.vec == other.vec
78  return False
79 
80  @staticmethod
81  def Da_a_mul_b(a, b):
82  """ derivatice of quaternion muliplication wrt left multiplier a """
83  v0 = b.vec[0]
84  v1 = b.vec[1]
85  v2 = b.vec[2]
86  y = b.real
87  return sympy.Matrix([[y, v2, -v1, v0],
88  [-v2, y, v0, v1],
89  [v1, -v0, y, v2],
90  [-v0, -v1, -v2, y]])
91 
92  @staticmethod
93  def Db_a_mul_b(a, b):
94  """ derivatice of quaternion muliplication wrt right multiplicand b """
95  u0 = a.vec[0]
96  u1 = a.vec[1]
97  u2 = a.vec[2]
98  x = a.real
99  return sympy.Matrix([[x, -u2, u1, u0],
100  [u2, x, -u0, u1],
101  [-u1, u0, x, u2],
102  [-u0, -u1, -u2, x]])
103 
104 
105 class TestQuaternion(unittest.TestCase):
106  def setUp(self):
107  x, u0, u1, u2 = sympy.symbols('x u0 u1 u2', real=True)
108  y, v0, v1, v2 = sympy.symbols('y v0 v1 v2', real=True)
109  u = sophus.Vector3(u0, u1, u2)
110  v = sophus.Vector3(v0, v1, v2)
111  self.a = Quaternion(x, u)
112  self.b = Quaternion(y, v)
113 
115  product = self.a * self.a.inv()
116  self.assertEqual(product.simplify(),
117  Quaternion.identity())
118  product = self.a.inv() * self.a
119  self.assertEqual(product.simplify(),
120  Quaternion.identity())
121 
122  def test_derivatives(self):
123  d = sympy.Matrix(4, 4, lambda r, c: sympy.diff(
124  (self.a * self.b)[r], self.a[c]))
125  self.assertEqual(d,
126  Quaternion.Da_a_mul_b(self.a, self.b))
127  d = sympy.Matrix(4, 4, lambda r, c: sympy.diff(
128  (self.a * self.b)[r], self.b[c]))
129  self.assertEqual(d,
130  Quaternion.Db_a_mul_b(self.a, self.b))
131 
132 
133 if __name__ == '__main__':
134  unittest.main()
135  print('hello')
sophus.quaternion.Quaternion.subs
def subs(self, x, y)
Definition: quaternion.py:67
sophus.quaternion.Quaternion.identity
def identity()
Definition: quaternion.py:60
sophus.quaternion.Quaternion.__add__
def __add__(self, right)
Definition: quaternion.py:25
sophus.quaternion.Quaternion.__getitem__
def __getitem__(self, key)
Definition: quaternion.py:39
sophus.quaternion.Quaternion.inv
def inv(self)
Definition: quaternion.py:55
sophus.quaternion.Quaternion.__mul__
def __mul__(self, right)
Definition: quaternion.py:19
sophus.quaternion.Quaternion.Db_a_mul_b
def Db_a_mul_b(a, b)
Definition: quaternion.py:93
sophus.quaternion.TestQuaternion.test_derivatives
def test_derivatives(self)
Definition: quaternion.py:122
sophus.quaternion.Quaternion
Definition: quaternion.py:9
sophus.quaternion.Quaternion.real
real
Definition: quaternion.py:16
sophus.quaternion.TestQuaternion.a
a
Definition: quaternion.py:111
sophus.quaternion.Quaternion.Da_a_mul_b
def Da_a_mul_b(a, b)
Definition: quaternion.py:81
sophus.quaternion.Quaternion.__init__
def __init__(self, real, vec)
Definition: quaternion.py:12
sophus.quaternion.TestQuaternion.test_muliplications
def test_muliplications(self)
Definition: quaternion.py:114
sophus.quaternion.Quaternion.simplify
def simplify(self)
Definition: quaternion.py:70
sophus.quaternion.Quaternion.squared_norm
def squared_norm(self)
Definition: quaternion.py:47
sophus.quaternion.Quaternion.conj
def conj(self)
Definition: quaternion.py:51
sophus.quaternion.Quaternion.vec
vec
Definition: quaternion.py:17
sophus.quaternion.Quaternion.__repr__
def __repr__(self)
Definition: quaternion.py:36
sophus.quaternion.Quaternion.zero
def zero()
Definition: quaternion.py:64
sophus.quaternion.TestQuaternion.b
b
Definition: quaternion.py:112
sophus.quaternion.Quaternion.__eq__
def __eq__(self, other)
Definition: quaternion.py:75
sophus.quaternion.TestQuaternion
Definition: quaternion.py:105
sophus.quaternion.Quaternion.__neg__
def __neg__(self)
Definition: quaternion.py:29
sophus.matrix.dot
def dot(left, right)
Definition: matrix.py:7
sophus.quaternion.Quaternion.__truediv__
def __truediv__(self, scalar)
Definition: quaternion.py:32
sophus.quaternion.TestQuaternion.setUp
def setUp(self)
Definition: quaternion.py:106


sophus
Author(s): Hauke Strasdat
autogenerated on Wed Mar 2 2022 01:01:47