test_basis.py 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from sympy.abc import x
  2. from sympy.core import S
  3. from sympy.core.numbers import AlgebraicNumber
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.polys import Poly, cyclotomic_poly
  6. from sympy.polys.domains import QQ
  7. from sympy.polys.matrices import DomainMatrix, DM
  8. from sympy.polys.numberfields.basis import round_two
  9. from sympy.testing.pytest import raises
  10. def test_round_two():
  11. # Poly must be irreducible, and over ZZ or QQ:
  12. raises(ValueError, lambda: round_two(Poly(x ** 2 - 1)))
  13. raises(ValueError, lambda: round_two(Poly(x ** 2 + sqrt(2))))
  14. # Test on many fields:
  15. cases = (
  16. # A couple of cyclotomic fields:
  17. (cyclotomic_poly(5), DomainMatrix.eye(4, QQ), 125),
  18. (cyclotomic_poly(7), DomainMatrix.eye(6, QQ), -16807),
  19. # A couple of quadratic fields (one 1 mod 4, one 3 mod 4):
  20. (x ** 2 - 5, DM([[1, (1, 2)], [0, (1, 2)]], QQ), 5),
  21. (x ** 2 - 7, DM([[1, 0], [0, 1]], QQ), 28),
  22. # Dedekind's example of a field with 2 as essential disc divisor:
  23. (x ** 3 + x ** 2 - 2 * x + 8, DM([[1, 0, 0], [0, 1, 0], [0, (1, 2), (1, 2)]], QQ).transpose(), -503),
  24. # A bunch of cubics with various forms for F -- all of these require
  25. # second or third enlargements. (Five of them require a third, while the rest require just a second.)
  26. # F = 2^2
  27. (x**3 + 3 * x**2 - 4 * x + 4, DM([((1, 2), (1, 4), (1, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -83),
  28. # F = 2^2 * 3
  29. (x**3 + 3 * x**2 + 3 * x - 3, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -108),
  30. # F = 2^3
  31. (x**3 + 5 * x**2 - x + 3, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -31),
  32. # F = 2^2 * 5
  33. (x**3 + 5 * x**2 - 5 * x - 5, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 1300),
  34. # F = 3^2
  35. (x**3 + 3 * x**2 + 5, DM([((1, 3), (1, 3), (1, 3)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -135),
  36. # F = 3^3
  37. (x**3 + 6 * x**2 + 3 * x - 1, DM([((1, 3), (1, 3), (1, 3)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 81),
  38. # F = 2^2 * 3^2
  39. (x**3 + 6 * x**2 + 4, DM([((1, 3), (2, 3), (1, 3)), (0, 1, 0), (0, 0, (1, 2))], QQ).transpose(), -108),
  40. # F = 2^3 * 7
  41. (x**3 + 7 * x**2 + 7 * x - 7, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), 49),
  42. # F = 2^2 * 13
  43. (x**3 + 7 * x**2 - x + 5, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -2028),
  44. # F = 2^4
  45. (x**3 + 7 * x**2 - 5 * x + 5, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -140),
  46. # F = 5^2
  47. (x**3 + 4 * x**2 - 3 * x + 7, DM([((1, 5), (4, 5), (4, 5)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -175),
  48. # F = 7^2
  49. (x**3 + 8 * x**2 + 5 * x - 1, DM([((1, 7), (6, 7), (2, 7)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 49),
  50. # F = 2 * 5 * 7
  51. (x**3 + 8 * x**2 - 2 * x + 6, DM([(1, 0, 0), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -14700),
  52. # F = 2^2 * 3 * 5
  53. (x**3 + 6 * x**2 - 3 * x + 8, DM([(1, 0, 0), (0, (1, 4), (1, 4)), (0, 0, 1)], QQ).transpose(), -675),
  54. # F = 2 * 3^2 * 7
  55. (x**3 + 9 * x**2 + 6 * x - 8, DM([(1, 0, 0), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), 3969),
  56. # F = 2^2 * 3^2 * 7
  57. (x**3 + 15 * x**2 - 9 * x + 13, DM([((1, 6), (1, 3), (1, 6)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -5292),
  58. # Polynomial need not be monic
  59. (5*x**3 + 5*x**2 - 10 * x + 40, DM([[1, 0, 0], [0, 1, 0], [0, (1, 2), (1, 2)]], QQ).transpose(), -503),
  60. # Polynomial can have non-integer rational coeffs
  61. (QQ(5, 3)*x**3 + QQ(5, 3)*x**2 - QQ(10, 3)*x + QQ(40, 3), DM([[1, 0, 0], [0, 1, 0], [0, (1, 2), (1, 2)]], QQ).transpose(), -503),
  62. )
  63. for f, B_exp, d_exp in cases:
  64. K = QQ.alg_field_from_poly(f)
  65. B = K.maximal_order().QQ_matrix
  66. d = K.discriminant()
  67. assert d == d_exp
  68. # The computed basis need not equal the expected one, but their quotient
  69. # must be unimodular:
  70. assert (B.inv()*B_exp).det()**2 == 1
  71. def test_AlgebraicField_integral_basis():
  72. alpha = AlgebraicNumber(sqrt(5), alias='alpha')
  73. k = QQ.algebraic_field(alpha)
  74. B0 = k.integral_basis()
  75. B1 = k.integral_basis(fmt='sympy')
  76. B2 = k.integral_basis(fmt='alg')
  77. assert B0 == [k([1]), k([S.Half, S.Half])]
  78. assert B1 == [1, S.Half + alpha/2]
  79. assert B2 == [k.ext.field_element([1]),
  80. k.ext.field_element([S.Half, S.Half])]