test_quaternion.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. from sympy.core.function import diff
  2. from sympy.core.function import expand
  3. from sympy.core.numbers import (E, I, Rational, pi)
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import (Symbol, symbols)
  6. from sympy.functions.elementary.complexes import (Abs, conjugate, im, re, sign)
  7. from sympy.functions.elementary.exponential import log
  8. from sympy.functions.elementary.miscellaneous import sqrt
  9. from sympy.functions.elementary.trigonometric import (acos, asin, cos, sin, atan2, atan)
  10. from sympy.integrals.integrals import integrate
  11. from sympy.matrices.dense import Matrix
  12. from sympy.simplify import simplify
  13. from sympy.simplify.trigsimp import trigsimp
  14. from sympy.algebras.quaternion import Quaternion
  15. from sympy.testing.pytest import raises
  16. from itertools import permutations, product
  17. w, x, y, z = symbols('w:z')
  18. phi = symbols('phi')
  19. def test_quaternion_construction():
  20. q = Quaternion(w, x, y, z)
  21. assert q + q == Quaternion(2*w, 2*x, 2*y, 2*z)
  22. q2 = Quaternion.from_axis_angle((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3),
  23. pi*Rational(2, 3))
  24. assert q2 == Quaternion(S.Half, S.Half,
  25. S.Half, S.Half)
  26. M = Matrix([[cos(phi), -sin(phi), 0], [sin(phi), cos(phi), 0], [0, 0, 1]])
  27. q3 = trigsimp(Quaternion.from_rotation_matrix(M))
  28. assert q3 == Quaternion(
  29. sqrt(2)*sqrt(cos(phi) + 1)/2, 0, 0, sqrt(2 - 2*cos(phi))*sign(sin(phi))/2)
  30. nc = Symbol('nc', commutative=False)
  31. raises(ValueError, lambda: Quaternion(w, x, nc, z))
  32. def test_quaternion_construction_norm():
  33. q1 = Quaternion(*symbols('a:d'))
  34. q2 = Quaternion(w, x, y, z)
  35. assert expand((q1*q2).norm()**2 - (q1.norm()**2 * q2.norm()**2)) == 0
  36. q3 = Quaternion(w, x, y, z, norm=1)
  37. assert (q1 * q3).norm() == q1.norm()
  38. def test_to_and_from_Matrix():
  39. q = Quaternion(w, x, y, z)
  40. q_full = Quaternion.from_Matrix(q.to_Matrix())
  41. q_vect = Quaternion.from_Matrix(q.to_Matrix(True))
  42. assert (q - q_full).is_zero_quaternion()
  43. assert (q.vector_part() - q_vect).is_zero_quaternion()
  44. def test_product_matrices():
  45. q1 = Quaternion(w, x, y, z)
  46. q2 = Quaternion(*(symbols("a:d")))
  47. assert (q1 * q2).to_Matrix() == q1.product_matrix_left * q2.to_Matrix()
  48. assert (q1 * q2).to_Matrix() == q2.product_matrix_right * q1.to_Matrix()
  49. R1 = (q1.product_matrix_left * q1.product_matrix_right.T)[1:, 1:]
  50. R2 = simplify(q1.to_rotation_matrix()*q1.norm()**2)
  51. assert R1 == R2
  52. def test_quaternion_axis_angle():
  53. test_data = [ # axis, angle, expected_quaternion
  54. ((1, 0, 0), 0, (1, 0, 0, 0)),
  55. ((1, 0, 0), pi/2, (sqrt(2)/2, sqrt(2)/2, 0, 0)),
  56. ((0, 1, 0), pi/2, (sqrt(2)/2, 0, sqrt(2)/2, 0)),
  57. ((0, 0, 1), pi/2, (sqrt(2)/2, 0, 0, sqrt(2)/2)),
  58. ((1, 0, 0), pi, (0, 1, 0, 0)),
  59. ((0, 1, 0), pi, (0, 0, 1, 0)),
  60. ((0, 0, 1), pi, (0, 0, 0, 1)),
  61. ((1, 1, 1), pi, (0, 1/sqrt(3),1/sqrt(3),1/sqrt(3))),
  62. ((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3), pi*2/3, (S.Half, S.Half, S.Half, S.Half))
  63. ]
  64. for axis, angle, expected in test_data:
  65. assert Quaternion.from_axis_angle(axis, angle) == Quaternion(*expected)
  66. def test_quaternion_axis_angle_simplification():
  67. result = Quaternion.from_axis_angle((1, 2, 3), asin(4))
  68. assert result.a == cos(asin(4)/2)
  69. assert result.b == sqrt(14)*sin(asin(4)/2)/14
  70. assert result.c == sqrt(14)*sin(asin(4)/2)/7
  71. assert result.d == 3*sqrt(14)*sin(asin(4)/2)/14
  72. def test_quaternion_complex_real_addition():
  73. a = symbols("a", complex=True)
  74. b = symbols("b", real=True)
  75. # This symbol is not complex:
  76. c = symbols("c", commutative=False)
  77. q = Quaternion(w, x, y, z)
  78. assert a + q == Quaternion(w + re(a), x + im(a), y, z)
  79. assert 1 + q == Quaternion(1 + w, x, y, z)
  80. assert I + q == Quaternion(w, 1 + x, y, z)
  81. assert b + q == Quaternion(w + b, x, y, z)
  82. raises(ValueError, lambda: c + q)
  83. raises(ValueError, lambda: q * c)
  84. raises(ValueError, lambda: c * q)
  85. assert -q == Quaternion(-w, -x, -y, -z)
  86. q1 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
  87. q2 = Quaternion(1, 4, 7, 8)
  88. assert q1 + (2 + 3*I) == Quaternion(5 + 7*I, 2 + 5*I, 0, 7 + 8*I)
  89. assert q2 + (2 + 3*I) == Quaternion(3, 7, 7, 8)
  90. assert q1 * (2 + 3*I) == \
  91. Quaternion((2 + 3*I)*(3 + 4*I), (2 + 3*I)*(2 + 5*I), 0, (2 + 3*I)*(7 + 8*I))
  92. assert q2 * (2 + 3*I) == Quaternion(-10, 11, 38, -5)
  93. q1 = Quaternion(1, 2, 3, 4)
  94. q0 = Quaternion(0, 0, 0, 0)
  95. assert q1 + q0 == q1
  96. assert q1 - q0 == q1
  97. assert q1 - q1 == q0
  98. def test_quaternion_evalf():
  99. assert (Quaternion(sqrt(2), 0, 0, sqrt(3)).evalf() ==
  100. Quaternion(sqrt(2).evalf(), 0, 0, sqrt(3).evalf()))
  101. assert (Quaternion(1/sqrt(2), 0, 0, 1/sqrt(2)).evalf() ==
  102. Quaternion((1/sqrt(2)).evalf(), 0, 0, (1/sqrt(2)).evalf()))
  103. def test_quaternion_functions():
  104. q = Quaternion(w, x, y, z)
  105. q1 = Quaternion(1, 2, 3, 4)
  106. q0 = Quaternion(0, 0, 0, 0)
  107. assert conjugate(q) == Quaternion(w, -x, -y, -z)
  108. assert q.norm() == sqrt(w**2 + x**2 + y**2 + z**2)
  109. assert q.normalize() == Quaternion(w, x, y, z) / sqrt(w**2 + x**2 + y**2 + z**2)
  110. assert q.inverse() == Quaternion(w, -x, -y, -z) / (w**2 + x**2 + y**2 + z**2)
  111. assert q.inverse() == q.pow(-1)
  112. raises(ValueError, lambda: q0.inverse())
  113. assert q.pow(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2*w*x, 2*w*y, 2*w*z)
  114. assert q**(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2*w*x, 2*w*y, 2*w*z)
  115. assert q1.pow(-2) == Quaternion(
  116. Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225))
  117. assert q1**(-2) == Quaternion(
  118. Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225))
  119. assert q1.pow(-0.5) == NotImplemented
  120. raises(TypeError, lambda: q1**(-0.5))
  121. assert q1.exp() == \
  122. Quaternion(E * cos(sqrt(29)),
  123. 2 * sqrt(29) * E * sin(sqrt(29)) / 29,
  124. 3 * sqrt(29) * E * sin(sqrt(29)) / 29,
  125. 4 * sqrt(29) * E * sin(sqrt(29)) / 29)
  126. assert q1._ln() == \
  127. Quaternion(log(sqrt(30)),
  128. 2 * sqrt(29) * acos(sqrt(30)/30) / 29,
  129. 3 * sqrt(29) * acos(sqrt(30)/30) / 29,
  130. 4 * sqrt(29) * acos(sqrt(30)/30) / 29)
  131. assert q1.pow_cos_sin(2) == \
  132. Quaternion(30 * cos(2 * acos(sqrt(30)/30)),
  133. 60 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29,
  134. 90 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29,
  135. 120 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29)
  136. assert diff(Quaternion(x, x, x, x), x) == Quaternion(1, 1, 1, 1)
  137. assert integrate(Quaternion(x, x, x, x), x) == \
  138. Quaternion(x**2 / 2, x**2 / 2, x**2 / 2, x**2 / 2)
  139. assert Quaternion.rotate_point((1, 1, 1), q1) == (S.One / 5, 1, S(7) / 5)
  140. n = Symbol('n')
  141. raises(TypeError, lambda: q1**n)
  142. n = Symbol('n', integer=True)
  143. raises(TypeError, lambda: q1**n)
  144. assert Quaternion(22, 23, 55, 8).scalar_part() == 22
  145. assert Quaternion(w, x, y, z).scalar_part() == w
  146. assert Quaternion(22, 23, 55, 8).vector_part() == Quaternion(0, 23, 55, 8)
  147. assert Quaternion(w, x, y, z).vector_part() == Quaternion(0, x, y, z)
  148. assert q1.axis() == Quaternion(0, 2*sqrt(29)/29, 3*sqrt(29)/29, 4*sqrt(29)/29)
  149. assert q1.axis().pow(2) == Quaternion(-1, 0, 0, 0)
  150. assert q0.axis().scalar_part() == 0
  151. assert (q.axis() == Quaternion(0,
  152. x/sqrt(x**2 + y**2 + z**2),
  153. y/sqrt(x**2 + y**2 + z**2),
  154. z/sqrt(x**2 + y**2 + z**2)))
  155. assert q0.is_pure() is True
  156. assert q1.is_pure() is False
  157. assert Quaternion(0, 0, 0, 3).is_pure() is True
  158. assert Quaternion(0, 2, 10, 3).is_pure() is True
  159. assert Quaternion(w, 2, 10, 3).is_pure() is None
  160. assert q1.angle() == atan(sqrt(29))
  161. assert q.angle() == atan2(sqrt(x**2 + y**2 + z**2), w)
  162. assert Quaternion.arc_coplanar(q1, Quaternion(2, 4, 6, 8)) is True
  163. assert Quaternion.arc_coplanar(q1, Quaternion(1, -2, -3, -4)) is True
  164. assert Quaternion.arc_coplanar(q1, Quaternion(1, 8, 12, 16)) is True
  165. assert Quaternion.arc_coplanar(q1, Quaternion(1, 2, 3, 4)) is True
  166. assert Quaternion.arc_coplanar(q1, Quaternion(w, 4, 6, 8)) is True
  167. assert Quaternion.arc_coplanar(q1, Quaternion(2, 7, 4, 1)) is False
  168. assert Quaternion.arc_coplanar(q1, Quaternion(w, x, y, z)) is None
  169. raises(ValueError, lambda: Quaternion.arc_coplanar(q1, q0))
  170. assert Quaternion.vector_coplanar(
  171. Quaternion(0, 8, 12, 16),
  172. Quaternion(0, 4, 6, 8),
  173. Quaternion(0, 2, 3, 4)) is True
  174. assert Quaternion.vector_coplanar(
  175. Quaternion(0, 0, 0, 0), Quaternion(0, 4, 6, 8), Quaternion(0, 2, 3, 4)) is True
  176. assert Quaternion.vector_coplanar(
  177. Quaternion(0, 8, 2, 6), Quaternion(0, 1, 6, 6), Quaternion(0, 0, 3, 4)) is False
  178. assert Quaternion.vector_coplanar(
  179. Quaternion(0, 1, 3, 4),
  180. Quaternion(0, 4, w, 6),
  181. Quaternion(0, 6, 8, 1)) is None
  182. raises(ValueError, lambda:
  183. Quaternion.vector_coplanar(q0, Quaternion(0, 4, 6, 8), q1))
  184. assert Quaternion(0, 1, 2, 3).parallel(Quaternion(0, 2, 4, 6)) is True
  185. assert Quaternion(0, 1, 2, 3).parallel(Quaternion(0, 2, 2, 6)) is False
  186. assert Quaternion(0, 1, 2, 3).parallel(Quaternion(w, x, y, 6)) is None
  187. raises(ValueError, lambda: q0.parallel(q1))
  188. assert Quaternion(0, 1, 2, 3).orthogonal(Quaternion(0, -2, 1, 0)) is True
  189. assert Quaternion(0, 2, 4, 7).orthogonal(Quaternion(0, 2, 2, 6)) is False
  190. assert Quaternion(0, 2, 4, 7).orthogonal(Quaternion(w, x, y, 6)) is None
  191. raises(ValueError, lambda: q0.orthogonal(q1))
  192. assert q1.index_vector() == Quaternion(
  193. 0, 2*sqrt(870)/29,
  194. 3*sqrt(870)/29,
  195. 4*sqrt(870)/29)
  196. assert Quaternion(0, 3, 9, 4).index_vector() == Quaternion(0, 3, 9, 4)
  197. assert Quaternion(4, 3, 9, 4).mensor() == log(sqrt(122))
  198. assert Quaternion(3, 3, 0, 2).mensor() == log(sqrt(22))
  199. assert q0.is_zero_quaternion() is True
  200. assert q1.is_zero_quaternion() is False
  201. assert Quaternion(w, 0, 0, 0).is_zero_quaternion() is None
  202. def test_quaternion_conversions():
  203. q1 = Quaternion(1, 2, 3, 4)
  204. assert q1.to_axis_angle() == ((2 * sqrt(29)/29,
  205. 3 * sqrt(29)/29,
  206. 4 * sqrt(29)/29),
  207. 2 * acos(sqrt(30)/30))
  208. assert (q1.to_rotation_matrix() ==
  209. Matrix([[Rational(-2, 3), Rational(2, 15), Rational(11, 15)],
  210. [Rational(2, 3), Rational(-1, 3), Rational(2, 3)],
  211. [Rational(1, 3), Rational(14, 15), Rational(2, 15)]]))
  212. assert (q1.to_rotation_matrix((1, 1, 1)) ==
  213. Matrix([
  214. [Rational(-2, 3), Rational(2, 15), Rational(11, 15), Rational(4, 5)],
  215. [Rational(2, 3), Rational(-1, 3), Rational(2, 3), S.Zero],
  216. [Rational(1, 3), Rational(14, 15), Rational(2, 15), Rational(-2, 5)],
  217. [S.Zero, S.Zero, S.Zero, S.One]]))
  218. theta = symbols("theta", real=True)
  219. q2 = Quaternion(cos(theta/2), 0, 0, sin(theta/2))
  220. assert trigsimp(q2.to_rotation_matrix()) == Matrix([
  221. [cos(theta), -sin(theta), 0],
  222. [sin(theta), cos(theta), 0],
  223. [0, 0, 1]])
  224. assert q2.to_axis_angle() == ((0, 0, sin(theta/2)/Abs(sin(theta/2))),
  225. 2*acos(cos(theta/2)))
  226. assert trigsimp(q2.to_rotation_matrix((1, 1, 1))) == Matrix([
  227. [cos(theta), -sin(theta), 0, sin(theta) - cos(theta) + 1],
  228. [sin(theta), cos(theta), 0, -sin(theta) - cos(theta) + 1],
  229. [0, 0, 1, 0],
  230. [0, 0, 0, 1]])
  231. def test_rotation_matrix_homogeneous():
  232. q = Quaternion(w, x, y, z)
  233. R1 = q.to_rotation_matrix(homogeneous=True) * q.norm()**2
  234. R2 = simplify(q.to_rotation_matrix(homogeneous=False) * q.norm()**2)
  235. assert R1 == R2
  236. def test_quaternion_rotation_iss1593():
  237. """
  238. There was a sign mistake in the definition,
  239. of the rotation matrix. This tests that particular sign mistake.
  240. See issue 1593 for reference.
  241. See wikipedia
  242. https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix
  243. for the correct definition
  244. """
  245. q = Quaternion(cos(phi/2), sin(phi/2), 0, 0)
  246. assert(trigsimp(q.to_rotation_matrix()) == Matrix([
  247. [1, 0, 0],
  248. [0, cos(phi), -sin(phi)],
  249. [0, sin(phi), cos(phi)]]))
  250. def test_quaternion_multiplication():
  251. q1 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
  252. q2 = Quaternion(1, 2, 3, 5)
  253. q3 = Quaternion(1, 1, 1, y)
  254. assert Quaternion._generic_mul(S(4), S.One) == 4
  255. assert (Quaternion._generic_mul(S(4), q1) ==
  256. Quaternion(12 + 16*I, 8 + 20*I, 0, 28 + 32*I))
  257. assert q2.mul(2) == Quaternion(2, 4, 6, 10)
  258. assert q2.mul(q3) == Quaternion(-5*y - 4, 3*y - 2, 9 - 2*y, y + 4)
  259. assert q2.mul(q3) == q2*q3
  260. z = symbols('z', complex=True)
  261. z_quat = Quaternion(re(z), im(z), 0, 0)
  262. q = Quaternion(*symbols('q:4', real=True))
  263. assert z * q == z_quat * q
  264. assert q * z == q * z_quat
  265. def test_issue_16318():
  266. #for rtruediv
  267. q0 = Quaternion(0, 0, 0, 0)
  268. raises(ValueError, lambda: 1/q0)
  269. #for rotate_point
  270. q = Quaternion(1, 2, 3, 4)
  271. (axis, angle) = q.to_axis_angle()
  272. assert Quaternion.rotate_point((1, 1, 1), (axis, angle)) == (S.One / 5, 1, S(7) / 5)
  273. #test for to_axis_angle
  274. q = Quaternion(-1, 1, 1, 1)
  275. axis = (-sqrt(3)/3, -sqrt(3)/3, -sqrt(3)/3)
  276. angle = 2*pi/3
  277. assert (axis, angle) == q.to_axis_angle()
  278. def test_to_euler():
  279. q = Quaternion(w, x, y, z)
  280. q_normalized = q.normalize()
  281. seqs = ['zxy', 'zyx', 'zyz', 'zxz']
  282. seqs += [seq.upper() for seq in seqs]
  283. for seq in seqs:
  284. euler_from_q = q.to_euler(seq)
  285. q_back = simplify(Quaternion.from_euler(euler_from_q, seq))
  286. assert q_back == q_normalized
  287. def test_to_euler_iss24504():
  288. """
  289. There was a mistake in the degenerate case testing
  290. See issue 24504 for reference.
  291. """
  292. q = Quaternion.from_euler((phi, 0, 0), 'zyz')
  293. assert trigsimp(q.to_euler('zyz'), inverse=True) == (phi, 0, 0)
  294. def test_to_euler_numerical_singilarities():
  295. def test_one_case(angles, seq):
  296. q = Quaternion.from_euler(angles, seq)
  297. assert q.to_euler(seq) == angles
  298. # symmetric
  299. test_one_case((pi/2, 0, 0), 'zyz')
  300. test_one_case((pi/2, 0, 0), 'ZYZ')
  301. test_one_case((pi/2, pi, 0), 'zyz')
  302. test_one_case((pi/2, pi, 0), 'ZYZ')
  303. # asymmetric
  304. test_one_case((pi/2, pi/2, 0), 'zyx')
  305. test_one_case((pi/2, -pi/2, 0), 'zyx')
  306. test_one_case((pi/2, pi/2, 0), 'ZYX')
  307. test_one_case((pi/2, -pi/2, 0), 'ZYX')
  308. def test_to_euler_options():
  309. def test_one_case(q):
  310. angles1 = Matrix(q.to_euler(seq, True, True))
  311. angles2 = Matrix(q.to_euler(seq, False, False))
  312. angle_errors = simplify(angles1-angles2).evalf()
  313. for angle_error in angle_errors:
  314. # forcing angles to set {-pi, pi}
  315. angle_error = (angle_error + pi) % (2 * pi) - pi
  316. assert angle_error < 10e-7
  317. for xyz in ('xyz', 'XYZ'):
  318. for seq_tuple in permutations(xyz):
  319. for symmetric in (True, False):
  320. if symmetric:
  321. seq = ''.join([seq_tuple[0], seq_tuple[1], seq_tuple[0]])
  322. else:
  323. seq = ''.join(seq_tuple)
  324. for elements in product([-1, 0, 1], repeat=4):
  325. q = Quaternion(*elements)
  326. if not q.is_zero_quaternion():
  327. test_one_case(q)