m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
9.2 KiB

6 months ago
  1. from sympy.core.function import diff
  2. from sympy.core.numbers import (E, I, Rational, pi)
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import (Symbol, symbols)
  5. from sympy.functions.elementary.complexes import (Abs, conjugate, im, re, sign)
  6. from sympy.functions.elementary.exponential import log
  7. from sympy.functions.elementary.miscellaneous import sqrt
  8. from sympy.functions.elementary.trigonometric import (acos, asin, cos, sin)
  9. from sympy.integrals.integrals import integrate
  10. from sympy.matrices.dense import Matrix
  11. from sympy.simplify.trigsimp import trigsimp
  12. from sympy.algebras.quaternion import Quaternion
  13. from sympy.testing.pytest import raises
  14. w, x, y, z = symbols('w:z')
  15. phi = symbols('phi')
  16. def test_quaternion_construction():
  17. q = Quaternion(w, x, y, z)
  18. assert q + q == Quaternion(2*w, 2*x, 2*y, 2*z)
  19. q2 = Quaternion.from_axis_angle((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3),
  20. pi*Rational(2, 3))
  21. assert q2 == Quaternion(S.Half, S.Half,
  22. S.Half, S.Half)
  23. M = Matrix([[cos(phi), -sin(phi), 0], [sin(phi), cos(phi), 0], [0, 0, 1]])
  24. q3 = trigsimp(Quaternion.from_rotation_matrix(M))
  25. assert q3 == Quaternion(sqrt(2)*sqrt(cos(phi) + 1)/2, 0, 0, sqrt(2 - 2*cos(phi))*sign(sin(phi))/2)
  26. nc = Symbol('nc', commutative=False)
  27. raises(ValueError, lambda: Quaternion(w, x, nc, z))
  28. def test_quaternion_axis_angle():
  29. test_data = [ # axis, angle, expected_quaternion
  30. ((1, 0, 0), 0, (1, 0, 0, 0)),
  31. ((1, 0, 0), pi/2, (sqrt(2)/2, sqrt(2)/2, 0, 0)),
  32. ((0, 1, 0), pi/2, (sqrt(2)/2, 0, sqrt(2)/2, 0)),
  33. ((0, 0, 1), pi/2, (sqrt(2)/2, 0, 0, sqrt(2)/2)),
  34. ((1, 0, 0), pi, (0, 1, 0, 0)),
  35. ((0, 1, 0), pi, (0, 0, 1, 0)),
  36. ((0, 0, 1), pi, (0, 0, 0, 1)),
  37. ((1, 1, 1), pi, (0, 1/sqrt(3),1/sqrt(3),1/sqrt(3))),
  38. ((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3), pi*2/3, (S.Half, S.Half, S.Half, S.Half))
  39. ]
  40. for axis, angle, expected in test_data:
  41. assert Quaternion.from_axis_angle(axis, angle) == Quaternion(*expected)
  42. def test_quaternion_axis_angle_simplification():
  43. result = Quaternion.from_axis_angle((1, 2, 3), asin(4))
  44. assert result.a == cos(asin(4)/2)
  45. assert result.b == sqrt(14)*sin(asin(4)/2)/14
  46. assert result.c == sqrt(14)*sin(asin(4)/2)/7
  47. assert result.d == 3*sqrt(14)*sin(asin(4)/2)/14
  48. def test_quaternion_complex_real_addition():
  49. a = symbols("a", complex=True)
  50. b = symbols("b", real=True)
  51. # This symbol is not complex:
  52. c = symbols("c", commutative=False)
  53. q = Quaternion(w, x, y, z)
  54. assert a + q == Quaternion(w + re(a), x + im(a), y, z)
  55. assert 1 + q == Quaternion(1 + w, x, y, z)
  56. assert I + q == Quaternion(w, 1 + x, y, z)
  57. assert b + q == Quaternion(w + b, x, y, z)
  58. raises(ValueError, lambda: c + q)
  59. raises(ValueError, lambda: q * c)
  60. raises(ValueError, lambda: c * q)
  61. assert -q == Quaternion(-w, -x, -y, -z)
  62. q1 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
  63. q2 = Quaternion(1, 4, 7, 8)
  64. assert q1 + (2 + 3*I) == Quaternion(5 + 7*I, 2 + 5*I, 0, 7 + 8*I)
  65. assert q2 + (2 + 3*I) == Quaternion(3, 7, 7, 8)
  66. assert q1 * (2 + 3*I) == \
  67. Quaternion((2 + 3*I)*(3 + 4*I), (2 + 3*I)*(2 + 5*I), 0, (2 + 3*I)*(7 + 8*I))
  68. assert q2 * (2 + 3*I) == Quaternion(-10, 11, 38, -5)
  69. q1 = Quaternion(1, 2, 3, 4)
  70. q0 = Quaternion(0, 0, 0, 0)
  71. assert q1 + q0 == q1
  72. assert q1 - q0 == q1
  73. assert q1 - q1 == q0
  74. def test_quaternion_evalf():
  75. assert Quaternion(sqrt(2), 0, 0, sqrt(3)).evalf() == Quaternion(sqrt(2).evalf(), 0, 0, sqrt(3).evalf())
  76. assert Quaternion(1/sqrt(2), 0, 0, 1/sqrt(2)).evalf() == Quaternion((1/sqrt(2)).evalf(), 0, 0, (1/sqrt(2)).evalf())
  77. def test_quaternion_functions():
  78. q = Quaternion(w, x, y, z)
  79. q1 = Quaternion(1, 2, 3, 4)
  80. q0 = Quaternion(0, 0, 0, 0)
  81. assert conjugate(q) == Quaternion(w, -x, -y, -z)
  82. assert q.norm() == sqrt(w**2 + x**2 + y**2 + z**2)
  83. assert q.normalize() == Quaternion(w, x, y, z) / sqrt(w**2 + x**2 + y**2 + z**2)
  84. assert q.inverse() == Quaternion(w, -x, -y, -z) / (w**2 + x**2 + y**2 + z**2)
  85. assert q.inverse() == q.pow(-1)
  86. raises(ValueError, lambda: q0.inverse())
  87. assert q.pow(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2*w*x, 2*w*y, 2*w*z)
  88. assert q**(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2*w*x, 2*w*y, 2*w*z)
  89. assert q1.pow(-2) == Quaternion(Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225))
  90. assert q1**(-2) == Quaternion(Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225))
  91. assert q1.pow(-0.5) == NotImplemented
  92. raises(TypeError, lambda: q1**(-0.5))
  93. assert q1.exp() == \
  94. Quaternion(E * cos(sqrt(29)),
  95. 2 * sqrt(29) * E * sin(sqrt(29)) / 29,
  96. 3 * sqrt(29) * E * sin(sqrt(29)) / 29,
  97. 4 * sqrt(29) * E * sin(sqrt(29)) / 29)
  98. assert q1._ln() == \
  99. Quaternion(log(sqrt(30)),
  100. 2 * sqrt(29) * acos(sqrt(30)/30) / 29,
  101. 3 * sqrt(29) * acos(sqrt(30)/30) / 29,
  102. 4 * sqrt(29) * acos(sqrt(30)/30) / 29)
  103. assert q1.pow_cos_sin(2) == \
  104. Quaternion(30 * cos(2 * acos(sqrt(30)/30)),
  105. 60 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29,
  106. 90 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29,
  107. 120 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29)
  108. assert diff(Quaternion(x, x, x, x), x) == Quaternion(1, 1, 1, 1)
  109. assert integrate(Quaternion(x, x, x, x), x) == \
  110. Quaternion(x**2 / 2, x**2 / 2, x**2 / 2, x**2 / 2)
  111. assert Quaternion.rotate_point((1, 1, 1), q1) == (S.One / 5, 1, S(7) / 5)
  112. n = Symbol('n')
  113. raises(TypeError, lambda: q1**n)
  114. n = Symbol('n', integer=True)
  115. raises(TypeError, lambda: q1**n)
  116. def test_quaternion_conversions():
  117. q1 = Quaternion(1, 2, 3, 4)
  118. assert q1.to_axis_angle() == ((2 * sqrt(29)/29,
  119. 3 * sqrt(29)/29,
  120. 4 * sqrt(29)/29),
  121. 2 * acos(sqrt(30)/30))
  122. assert q1.to_rotation_matrix() == Matrix([[Rational(-2, 3), Rational(2, 15), Rational(11, 15)],
  123. [Rational(2, 3), Rational(-1, 3), Rational(2, 3)],
  124. [Rational(1, 3), Rational(14, 15), Rational(2, 15)]])
  125. assert q1.to_rotation_matrix((1, 1, 1)) == Matrix([[Rational(-2, 3), Rational(2, 15), Rational(11, 15), Rational(4, 5)],
  126. [Rational(2, 3), Rational(-1, 3), Rational(2, 3), S.Zero],
  127. [Rational(1, 3), Rational(14, 15), Rational(2, 15), Rational(-2, 5)],
  128. [S.Zero, S.Zero, S.Zero, S.One]])
  129. theta = symbols("theta", real=True)
  130. q2 = Quaternion(cos(theta/2), 0, 0, sin(theta/2))
  131. assert trigsimp(q2.to_rotation_matrix()) == Matrix([
  132. [cos(theta), -sin(theta), 0],
  133. [sin(theta), cos(theta), 0],
  134. [0, 0, 1]])
  135. assert q2.to_axis_angle() == ((0, 0, sin(theta/2)/Abs(sin(theta/2))),
  136. 2*acos(cos(theta/2)))
  137. assert trigsimp(q2.to_rotation_matrix((1, 1, 1))) == Matrix([
  138. [cos(theta), -sin(theta), 0, sin(theta) - cos(theta) + 1],
  139. [sin(theta), cos(theta), 0, -sin(theta) - cos(theta) + 1],
  140. [0, 0, 1, 0],
  141. [0, 0, 0, 1]])
  142. def test_quaternion_rotation_iss1593():
  143. """
  144. There was a sign mistake in the definition,
  145. of the rotation matrix. This tests that particular sign mistake.
  146. See issue 1593 for reference.
  147. See wikipedia
  148. https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix
  149. for the correct definition
  150. """
  151. q = Quaternion(cos(phi/2), sin(phi/2), 0, 0)
  152. assert(trigsimp(q.to_rotation_matrix()) == Matrix([
  153. [1, 0, 0],
  154. [0, cos(phi), -sin(phi)],
  155. [0, sin(phi), cos(phi)]]))
  156. def test_quaternion_multiplication():
  157. q1 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
  158. q2 = Quaternion(1, 2, 3, 5)
  159. q3 = Quaternion(1, 1, 1, y)
  160. assert Quaternion._generic_mul(4, 1) == 4
  161. assert Quaternion._generic_mul(4, q1) == Quaternion(12 + 16*I, 8 + 20*I, 0, 28 + 32*I)
  162. assert q2.mul(2) == Quaternion(2, 4, 6, 10)
  163. assert q2.mul(q3) == Quaternion(-5*y - 4, 3*y - 2, 9 - 2*y, y + 4)
  164. assert q2.mul(q3) == q2*q3
  165. z = symbols('z', complex=True)
  166. z_quat = Quaternion(re(z), im(z), 0, 0)
  167. q = Quaternion(*symbols('q:4', real=True))
  168. assert z * q == z_quat * q
  169. assert q * z == q * z_quat
  170. def test_issue_16318():
  171. #for rtruediv
  172. q0 = Quaternion(0, 0, 0, 0)
  173. raises(ValueError, lambda: 1/q0)
  174. #for rotate_point
  175. q = Quaternion(1, 2, 3, 4)
  176. (axis, angle) = q.to_axis_angle()
  177. assert Quaternion.rotate_point((1, 1, 1), (axis, angle)) == (S.One / 5, 1, S(7) / 5)
  178. #test for to_axis_angle
  179. q = Quaternion(-1, 1, 1, 1)
  180. axis = (-sqrt(3)/3, -sqrt(3)/3, -sqrt(3)/3)
  181. angle = 2*pi/3
  182. assert (axis, angle) == q.to_axis_angle()