valuate.sage 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # $ sage -sh
  2. # $ pip install tabulate
  3. from tabulate import tabulate
  4. # P = (2, 4)
  5. # ord_P(y - 2x) = 2
  6. # from Washington example 11.4 page 345
  7. K.<x, y> = GF(11)[]
  8. Px, Py = K(2), K(4)
  9. assert (3*Px^2 + 4) / (2*Py) == 2
  10. basis = [(x - Px), (y - Py), 1]
  11. # Return components for basis
  12. def decomp(f, basis):
  13. comps = []
  14. r = f
  15. for b in basis:
  16. a, r = r.quo_rem(b)
  17. comps.append(a)
  18. assert r == 0
  19. return comps
  20. def comp(comps, basis):
  21. return sum(a*b for a, b in zip(comps, basis))
  22. original_f = y - 2*x
  23. assert comp(decomp(original_f, basis), basis) == original_f
  24. # P = (a, b)
  25. # y² = x³ + Ax + B
  26. # (y - b)(y + b) = (x - a)³ + C(3,2)a(x - a)² + (3a² + A)(x - a)
  27. #
  28. # sage: ((x - a)^3 + binomial(3,2)*a*(x - a)^2 + (3*a^2 + A)*(x - a)).expand()
  29. # -a^3 + x^3 - A*a + A*x
  30. # But since (a, b) ∈ E(K) => b² = a³ + Aa + B
  31. # => B = b² - (a³ + Aa)
  32. #
  33. # So at every step we replace the component for (y - Py)
  34. # with the reduction to the component for (x - Px)
  35. EC_A = 4
  36. EC_B = 0
  37. EC = y^2 - x^3 - EC_A*x - EC_B
  38. # so we can replace (y - Py) with this
  39. b0, b1, b2 = basis
  40. Ef = b0^2 + binomial(3,2)*Px*b0^1 + (3*Px^2 + EC_A)
  41. Eg = (y + Py)
  42. assert EC == b1*Eg - b0*Ef
  43. # f / g
  44. # Technically we don't need g but we keep track of it anyway
  45. def apply_reduction(f, g, basis):
  46. #a1 = f[1]
  47. #f[1] = 0
  48. b0, b1, _ = basis
  49. # b1 == b0 * f / g
  50. # so we can replace c b1 with (cf/g) b0
  51. # a2 = 0
  52. assert f[2] == 0
  53. # note that
  54. # b1 = (f/g) b0
  55. # so
  56. # x = a0 b0 + a1 b1 + 0 b2
  57. # = (a0 + a1 f/g) b0
  58. # let a0 = p/q
  59. # x = (pg + a1 f)
  60. # ----------- b0
  61. # qg
  62. f[0] = f[0]*Eg + f[1]*Ef
  63. f[1] = 0
  64. g[0] *= Eg
  65. k = 1
  66. table = []
  67. table.append(("", "f", "g", "k"))
  68. def log(step_name, f, g, k):
  69. table.append((step_name, str(f), str(g), k))
  70. f = decomp(original_f, basis)
  71. g = [1]
  72. log("start", f, g, k)
  73. # Reduce
  74. apply_reduction(f, g, basis)
  75. log("reduce", f, g, k)
  76. f = f[0]
  77. # Decompose
  78. f = decomp(f, basis)
  79. log("decomp", f, g, k)
  80. assert comp(f, basis) == (x - 2)^2 - 5*(x - 2) - 2*(y - 4)
  81. assert f[2] == 0
  82. k += 1
  83. # Reduce
  84. apply_reduction(f, g, basis)
  85. log("reduce", f, g, k)
  86. f = f[0]
  87. # Decompose
  88. f = decomp(f, basis)
  89. log("decomp", f, g, k)
  90. # Program terminates because remainder is nonzero
  91. assert f[2] != 0
  92. print(f"basis = {basis}")
  93. print(tabulate(table))
  94. print(f"k = {k}")
  95. # Test final value is correct
  96. S = K.quotient(y^2 - x^3 - 4*x).fraction_field()
  97. f0, f1, f2 = f
  98. f = f0*b0 + f1*b1 + f2*b2
  99. g = g[0]
  100. fprime = b0^k * f/g
  101. assert fprime == S(original_f)
  102. # to convert fprime back again:
  103. # f, g = fprime.numerator().lift(), fprime.denominator().lift()
  104. # to get the parent ring use:
  105. # f.parent()
  106. # to move up the hierarchy
  107. # S.ring().cover_ring()
  108. assert g(Px, Py) != 0
  109. assert f(Px, Py) != 0
  110. assert b0(Px, Py) == 0