图片解析应用
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.

202 lines
5.8 KiB

  1. """Tests on algebraic numbers. """
  2. from sympy.core.containers import Tuple
  3. from sympy.core.numbers import (AlgebraicNumber, I, Rational)
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import Symbol
  6. from sympy.functions.elementary.miscellaneous import sqrt
  7. from sympy.polys.polytools import Poly
  8. from sympy.polys.numberfields.subfield import to_number_field
  9. from sympy.polys.polyclasses import DMP
  10. from sympy.polys.domains import QQ
  11. from sympy.polys.rootoftools import CRootOf
  12. from sympy.abc import x, y
  13. def test_AlgebraicNumber():
  14. minpoly, root = x**2 - 2, sqrt(2)
  15. a = AlgebraicNumber(root, gen=x)
  16. assert a.rep == DMP([QQ(1), QQ(0)], QQ)
  17. assert a.root == root
  18. assert a.alias is None
  19. assert a.minpoly == minpoly
  20. assert a.is_number
  21. assert a.is_aliased is False
  22. assert a.coeffs() == [S.One, S.Zero]
  23. assert a.native_coeffs() == [QQ(1), QQ(0)]
  24. a = AlgebraicNumber(root, gen=x, alias='y')
  25. assert a.rep == DMP([QQ(1), QQ(0)], QQ)
  26. assert a.root == root
  27. assert a.alias == Symbol('y')
  28. assert a.minpoly == minpoly
  29. assert a.is_number
  30. assert a.is_aliased is True
  31. a = AlgebraicNumber(root, gen=x, alias=Symbol('y'))
  32. assert a.rep == DMP([QQ(1), QQ(0)], QQ)
  33. assert a.root == root
  34. assert a.alias == Symbol('y')
  35. assert a.minpoly == minpoly
  36. assert a.is_number
  37. assert a.is_aliased is True
  38. assert AlgebraicNumber(sqrt(2), []).rep == DMP([], QQ)
  39. assert AlgebraicNumber(sqrt(2), ()).rep == DMP([], QQ)
  40. assert AlgebraicNumber(sqrt(2), (0, 0)).rep == DMP([], QQ)
  41. assert AlgebraicNumber(sqrt(2), [8]).rep == DMP([QQ(8)], QQ)
  42. assert AlgebraicNumber(sqrt(2), [Rational(8, 3)]).rep == DMP([QQ(8, 3)], QQ)
  43. assert AlgebraicNumber(sqrt(2), [7, 3]).rep == DMP([QQ(7), QQ(3)], QQ)
  44. assert AlgebraicNumber(
  45. sqrt(2), [Rational(7, 9), Rational(3, 2)]).rep == DMP([QQ(7, 9), QQ(3, 2)], QQ)
  46. assert AlgebraicNumber(sqrt(2), [1, 2, 3]).rep == DMP([QQ(2), QQ(5)], QQ)
  47. a = AlgebraicNumber(AlgebraicNumber(root, gen=x), [1, 2])
  48. assert a.rep == DMP([QQ(1), QQ(2)], QQ)
  49. assert a.root == root
  50. assert a.alias is None
  51. assert a.minpoly == minpoly
  52. assert a.is_number
  53. assert a.is_aliased is False
  54. assert a.coeffs() == [S.One, S(2)]
  55. assert a.native_coeffs() == [QQ(1), QQ(2)]
  56. a = AlgebraicNumber((minpoly, root), [1, 2])
  57. assert a.rep == DMP([QQ(1), QQ(2)], QQ)
  58. assert a.root == root
  59. assert a.alias is None
  60. assert a.minpoly == minpoly
  61. assert a.is_number
  62. assert a.is_aliased is False
  63. a = AlgebraicNumber((Poly(minpoly), root), [1, 2])
  64. assert a.rep == DMP([QQ(1), QQ(2)], QQ)
  65. assert a.root == root
  66. assert a.alias is None
  67. assert a.minpoly == minpoly
  68. assert a.is_number
  69. assert a.is_aliased is False
  70. assert AlgebraicNumber( sqrt(3)).rep == DMP([ QQ(1), QQ(0)], QQ)
  71. assert AlgebraicNumber(-sqrt(3)).rep == DMP([ QQ(1), QQ(0)], QQ)
  72. a = AlgebraicNumber(sqrt(2))
  73. b = AlgebraicNumber(sqrt(2))
  74. assert a == b
  75. c = AlgebraicNumber(sqrt(2), gen=x)
  76. assert a == b
  77. assert a == c
  78. a = AlgebraicNumber(sqrt(2), [1, 2])
  79. b = AlgebraicNumber(sqrt(2), [1, 3])
  80. assert a != b and a != sqrt(2) + 3
  81. assert (a == x) is False and (a != x) is True
  82. a = AlgebraicNumber(sqrt(2), [1, 0])
  83. b = AlgebraicNumber(sqrt(2), [1, 0], alias=y)
  84. assert a.as_poly(x) == Poly(x, domain='QQ')
  85. assert b.as_poly() == Poly(y, domain='QQ')
  86. assert a.as_expr() == sqrt(2)
  87. assert a.as_expr(x) == x
  88. assert b.as_expr() == sqrt(2)
  89. assert b.as_expr(x) == x
  90. a = AlgebraicNumber(sqrt(2), [2, 3])
  91. b = AlgebraicNumber(sqrt(2), [2, 3], alias=y)
  92. p = a.as_poly()
  93. assert p == Poly(2*p.gen + 3)
  94. assert a.as_poly(x) == Poly(2*x + 3, domain='QQ')
  95. assert b.as_poly() == Poly(2*y + 3, domain='QQ')
  96. assert a.as_expr() == 2*sqrt(2) + 3
  97. assert a.as_expr(x) == 2*x + 3
  98. assert b.as_expr() == 2*sqrt(2) + 3
  99. assert b.as_expr(x) == 2*x + 3
  100. a = AlgebraicNumber(sqrt(2))
  101. b = to_number_field(sqrt(2))
  102. assert a.args == b.args == (sqrt(2), Tuple(1, 0))
  103. b = AlgebraicNumber(sqrt(2), alias='alpha')
  104. assert b.args == (sqrt(2), Tuple(1, 0), Symbol('alpha'))
  105. a = AlgebraicNumber(sqrt(2), [1, 2, 3])
  106. assert a.args == (sqrt(2), Tuple(1, 2, 3))
  107. a = AlgebraicNumber(sqrt(2), [1, 2], "alpha")
  108. b = AlgebraicNumber(a)
  109. c = AlgebraicNumber(a, alias="gamma")
  110. assert a == b
  111. assert c.alias.name == "gamma"
  112. a = AlgebraicNumber(sqrt(2) + sqrt(3), [S(1)/2, 0, S(-9)/2, 0])
  113. b = AlgebraicNumber(a, [1, 0, 0])
  114. assert b.root == a.root
  115. assert a.to_root() == sqrt(2)
  116. assert b.to_root() == 2
  117. a = AlgebraicNumber(2)
  118. assert a.is_primitive_element is True
  119. def test_to_algebraic_integer():
  120. a = AlgebraicNumber(sqrt(3), gen=x).to_algebraic_integer()
  121. assert a.minpoly == x**2 - 3
  122. assert a.root == sqrt(3)
  123. assert a.rep == DMP([QQ(1), QQ(0)], QQ)
  124. a = AlgebraicNumber(2*sqrt(3), gen=x).to_algebraic_integer()
  125. assert a.minpoly == x**2 - 12
  126. assert a.root == 2*sqrt(3)
  127. assert a.rep == DMP([QQ(1), QQ(0)], QQ)
  128. a = AlgebraicNumber(sqrt(3)/2, gen=x).to_algebraic_integer()
  129. assert a.minpoly == x**2 - 12
  130. assert a.root == 2*sqrt(3)
  131. assert a.rep == DMP([QQ(1), QQ(0)], QQ)
  132. a = AlgebraicNumber(sqrt(3)/2, [Rational(7, 19), 3], gen=x).to_algebraic_integer()
  133. assert a.minpoly == x**2 - 12
  134. assert a.root == 2*sqrt(3)
  135. assert a.rep == DMP([QQ(7, 19), QQ(3)], QQ)
  136. def test_AlgebraicNumber_to_root():
  137. assert AlgebraicNumber(sqrt(2)).to_root() == sqrt(2)
  138. zeta5_squared = AlgebraicNumber(CRootOf(x**5 - 1, 4), coeffs=[1, 0, 0])
  139. assert zeta5_squared.to_root() == CRootOf(x**4 + x**3 + x**2 + x + 1, 1)
  140. zeta3_squared = AlgebraicNumber(CRootOf(x**3 - 1, 2), coeffs=[1, 0, 0])
  141. assert zeta3_squared.to_root() == -S(1)/2 - sqrt(3)*I/2
  142. assert zeta3_squared.to_root(radicals=False) == CRootOf(x**2 + x + 1, 0)