1 """ run with: python3 -m sophus.quaternion """
10 """ Quaternion class """
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
20 """ quaternion multiplication """
22 self[3] * right.vec + right[3] * self.
vec +
23 self.
vec.cross(right.vec))
26 """ quaternion multiplication """
33 """ scalar division """
37 return "( " + repr(self[3]) +
" + " + repr(self.
vec) +
"i )"
40 """ We use the following convention [vec0, vec1, vec2, real] """
41 assert (key >= 0
and key < 4)
48 """ squared norm when considering the quaternion as 4-tuple """
49 return sophus.squared_norm(self.
vec) + self.
real**2
52 """ quaternion conjugate """
56 """ quaternion inverse """
71 v = sympy.simplify(self.
vec)
73 sophus.Vector3(v[0], v[1], v[2]))
76 if isinstance(self, other.__class__):
77 return self.
real == other.real
and self.
vec == other.vec
82 """ derivatice of quaternion muliplication wrt left multiplier a """
87 return sympy.Matrix([[y, v2, -v1, v0],
94 """ derivatice of quaternion muliplication wrt right multiplicand b """
99 return sympy.Matrix([[x, -u2, u1, u0],
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)
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())
123 d = sympy.Matrix(4, 4,
lambda r, c: sympy.diff(
124 (self.
a * self.
b)[r], self.
a[c]))
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]))
130 Quaternion.Db_a_mul_b(self.
a, self.
b))
133 if __name__ ==
'__main__':