|
| 1 | +cimport cython |
| 2 | + |
| 3 | +from flint.flintlib.types.gr cimport ( |
| 4 | + truth_t, |
| 5 | + T_TRUE, |
| 6 | + T_FALSE, |
| 7 | + T_UNKNOWN, |
| 8 | + |
| 9 | + GR_SUCCESS, |
| 10 | + GR_DOMAIN, |
| 11 | + GR_UNABLE, |
| 12 | + |
| 13 | + gr_ctx_t, |
| 14 | + gr_ptr, |
| 15 | +) |
| 16 | +from flint.flintlib.functions.gr cimport ( |
| 17 | + gr_heap_init, |
| 18 | + gr_set_str, |
| 19 | + gr_get_str, |
| 20 | + |
| 21 | + gr_neg, |
| 22 | + gr_add, |
| 23 | + gr_add_si, |
| 24 | + gr_sub, |
| 25 | + gr_sub_si, |
| 26 | + gr_mul, |
| 27 | + gr_mul_si, |
| 28 | + gr_inv, |
| 29 | + gr_div, |
| 30 | + gr_div_si, |
| 31 | + gr_pow_si, |
| 32 | +) |
| 33 | + |
| 34 | +from flint.flint_base.flint_base cimport flint_ctx, flint_scalar |
| 35 | + |
| 36 | +from flint.utils.flint_exceptions import DomainError, UnableError |
| 37 | + |
| 38 | + |
| 39 | +cdef inline truth_to_py(truth_t t): |
| 40 | + if t == T_TRUE: |
| 41 | + return True |
| 42 | + elif t == T_FALSE: |
| 43 | + return False |
| 44 | + else: |
| 45 | + return None |
| 46 | + |
| 47 | + |
| 48 | +@cython.no_gc |
| 49 | +cdef class gr_ctx(flint_ctx): |
| 50 | + cdef gr_ctx_t ctx_t |
| 51 | + cdef bint _init |
| 52 | + |
| 53 | + @cython.final |
| 54 | + cdef inline gr new_gr(self): |
| 55 | + cdef gr py_val |
| 56 | + cdef gr_ptr pval |
| 57 | + pval = gr_heap_init(self.ctx_t) |
| 58 | + if pval == NULL: |
| 59 | + raise MemoryError("Failed to allocate memory for gr object") |
| 60 | + py_val = gr.__new__(gr) |
| 61 | + py_val.pval = pval |
| 62 | + py_val.ctx = self |
| 63 | + py_val._init = True |
| 64 | + return py_val |
| 65 | + |
| 66 | + @cython.final |
| 67 | + cdef inline gr from_str(self, s: str): |
| 68 | + cdef gr py_val |
| 69 | + cdef bytes b |
| 70 | + b = s.encode('utf-8') |
| 71 | + py_val = self.new_gr() |
| 72 | + err = gr_set_str(py_val.pval, b, self.ctx_t) |
| 73 | + if err != GR_SUCCESS: |
| 74 | + raise self._error(err, "Failed to parse string") |
| 75 | + return py_val |
| 76 | + |
| 77 | + @cython.final |
| 78 | + cdef inline str to_str(self, val: gr): |
| 79 | + cdef str py_str |
| 80 | + cdef char *s |
| 81 | + err = gr_get_str(&s, val.pval, self.ctx_t) |
| 82 | + if err != GR_SUCCESS: |
| 83 | + raise self._error(err, "Failed to convert to string") |
| 84 | + py_str = (<bytes>s).decode("utf-8") |
| 85 | + # flint_free(s) |
| 86 | + return py_str |
| 87 | + |
| 88 | + @cython.final |
| 89 | + cdef inline _error(self, int err, str msg): |
| 90 | + if err & GR_DOMAIN: |
| 91 | + return DomainError(msg) |
| 92 | + elif err & GR_UNABLE: |
| 93 | + return UnableError(msg) |
| 94 | + else: |
| 95 | + return AssertionError("Bad error code") |
| 96 | + |
| 97 | + |
| 98 | +@cython.no_gc |
| 99 | +cdef class _gr_fmpz_ctx(gr_ctx): |
| 100 | + |
| 101 | + @staticmethod |
| 102 | + cdef _gr_fmpz_ctx _new() |
| 103 | + |
| 104 | + |
| 105 | +@cython.no_gc |
| 106 | +cdef class _gr_fmpq_ctx(gr_ctx): |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + cdef _gr_fmpq_ctx _new() |
| 110 | + |
| 111 | + |
| 112 | +# The global contexts for use in cython code: |
| 113 | +cdef _gr_fmpz_ctx gr_fmpz_ctx_c |
| 114 | +cdef _gr_fmpq_ctx gr_fmpq_ctx_c |
| 115 | + |
| 116 | + |
| 117 | +@cython.no_gc |
| 118 | +cdef class gr(flint_scalar): |
| 119 | + cdef gr_ptr pval |
| 120 | + cdef gr_ctx ctx |
| 121 | + cdef bint _init |
| 122 | + |
| 123 | + @cython.final |
| 124 | + cdef inline _error(self, int err, str msg): |
| 125 | + return self.ctx._error(err, msg) |
0 commit comments