|
| 1 | +from flint.flintlib.functions.fmpz cimport fmpz_abs, fmpz_root, fmpz_set |
| 2 | +from flint.types.fmpz cimport fmpz, any_as_fmpz |
| 3 | +from flint.utils.typecheck cimport typecheck |
| 4 | + |
| 5 | +cdef class qfb: |
| 6 | + """ |
| 7 | + The qfb type represents definite binary quadratic forms |
| 8 | + over Z, with composition, inverse and power operations |
| 9 | + compatible with the class group of a given discriminant. |
| 10 | +
|
| 11 | + Some operations require the form to be primitive. |
| 12 | + """ |
| 13 | + def __cinit__(self): |
| 14 | + qfb_init(self.val) |
| 15 | + self.D = fmpz(0) |
| 16 | + |
| 17 | + def __dealloc__(self): |
| 18 | + qfb_clear(self.val) |
| 19 | + |
| 20 | + def __init__(self, a, b, c): |
| 21 | + a_fmpz = any_as_fmpz(a) |
| 22 | + b_fmpz = any_as_fmpz(b) |
| 23 | + c_fmpz = any_as_fmpz(c) |
| 24 | + if a_fmpz is NotImplemented: |
| 25 | + raise TypeError(f"Incorrect type {type(a)} for qfb coefficient") |
| 26 | + if b_fmpz is NotImplemented: |
| 27 | + raise TypeError(f"Incorrect type {type(b)} for qfb coefficient") |
| 28 | + if c_fmpz is NotImplemented: |
| 29 | + raise TypeError(f"Incorrect type {type(c)} for qfb coefficient") |
| 30 | + fmpz_set(self.val[0].a, (<fmpz>a_fmpz).val) |
| 31 | + fmpz_set(self.val[0].b, (<fmpz>b_fmpz).val) |
| 32 | + fmpz_set(self.val[0].c, (<fmpz>c_fmpz).val) |
| 33 | + D = fmpz() |
| 34 | + qfb_discriminant(D.val, self.val) |
| 35 | + self.D = D |
| 36 | + |
| 37 | + def __repr__(self): |
| 38 | + a, b, c = self.coefficients() |
| 39 | + return f"qfb({a}, {b}, {c})" |
| 40 | + |
| 41 | + def __eq__(self, other): |
| 42 | + if self is other: |
| 43 | + return True |
| 44 | + |
| 45 | + if typecheck(other, qfb): |
| 46 | + return bool(qfb_equal(self.val, (<qfb>other).val)) |
| 47 | + |
| 48 | + return False |
| 49 | + |
| 50 | + def __mul__(q1, q2): |
| 51 | + "Returns a reduced form equivalent to the composition of q1 and q2" |
| 52 | + if not q1.is_primitive(): |
| 53 | + raise ValueError(f"{q1} is not primitive") |
| 54 | + |
| 55 | + cdef qfb res = qfb.__new__(qfb) |
| 56 | + cdef fmpz_t L |
| 57 | + fmpz_abs(L, q1.D.val) |
| 58 | + fmpz_root(L, L, 4) |
| 59 | + qfb_nucomp(res.val, q1.val, (<qfb>q2).val, q1.D.val, L) |
| 60 | + qfb_reduce(res.val, res.val, q1.D.val) |
| 61 | + res.D = q1.D |
| 62 | + return res |
| 63 | + |
| 64 | + def __pow__(q, e, mod): |
| 65 | + "Returns a reduced form equivalent to the e-th iterated composition of q" |
| 66 | + if mod is not None: |
| 67 | + raise NotImplementedError("modular exponentiation") |
| 68 | + |
| 69 | + if not q.is_primitive(): |
| 70 | + raise ValueError(f"{q} is not primitive") |
| 71 | + |
| 72 | + e_fmpz = any_as_fmpz(e) |
| 73 | + if e_fmpz is NotImplemented: |
| 74 | + raise TypeError(f"exponent cannot be cast to an fmpz type: {e}") |
| 75 | + |
| 76 | + # qfb_pow does not support negative exponents and will loop forever |
| 77 | + # if a negative integer is provided. |
| 78 | + e_abs = abs(e_fmpz) |
| 79 | + |
| 80 | + cdef qfb res = qfb.__new__(qfb) |
| 81 | + qfb_pow(res.val, q.val, q.D.val, (<fmpz>e_abs).val) |
| 82 | + if e_fmpz < 0: |
| 83 | + qfb_inverse(res.val, res.val) |
| 84 | + res.D = q.D |
| 85 | + return res |
| 86 | + |
| 87 | + def coefficients(self): |
| 88 | + """ |
| 89 | + Returns coefficients (a, b, c) of the form as a polynomial q(x,y)=ax²+bxy+cy² |
| 90 | + """ |
| 91 | + a = fmpz() |
| 92 | + fmpz_set(a.val, self.val[0].a) |
| 93 | + b = fmpz() |
| 94 | + fmpz_set(b.val, self.val[0].b) |
| 95 | + c = fmpz() |
| 96 | + fmpz_set(c.val, self.val[0].c) |
| 97 | + return a, b, c |
| 98 | + |
| 99 | + def discriminant(self): |
| 100 | + return self.D |
| 101 | + |
| 102 | + def is_reduced(self): |
| 103 | + return bool(qfb_is_reduced(self.val)) |
| 104 | + |
| 105 | + def is_primitive(self): |
| 106 | + return bool(qfb_is_primitive(self.val)) |
| 107 | + |
| 108 | + def inverse(self): |
| 109 | + cdef qfb res = qfb.__new__(qfb) |
| 110 | + qfb_inverse(res.val, self.val) |
| 111 | + res.D = self.D |
| 112 | + return res |
| 113 | + |
| 114 | + def reduce(self): |
| 115 | + cdef qfb res = qfb.__new__(qfb) |
| 116 | + qfb_reduce(res.val, self.val, self.D.val) |
| 117 | + res.D = self.D |
| 118 | + return res |
| 119 | + |
| 120 | + @classmethod |
| 121 | + def prime_form(cls, D, p): |
| 122 | + """ |
| 123 | + Returns the unique reduced form with 0 < b ≤ p. Requires that p is prime. |
| 124 | + """ |
| 125 | + |
| 126 | + d_fmpz = any_as_fmpz(D) |
| 127 | + p_fmpz = any_as_fmpz(p) |
| 128 | + |
| 129 | + cdef qfb res = qfb.__new__(qfb) |
| 130 | + qfb_prime_form(res.val, (<fmpz>d_fmpz).val, (<fmpz>p_fmpz).val) |
| 131 | + res.D = d_fmpz |
| 132 | + return res |
0 commit comments