Skip to content

Commit f8f0ffb

Browse files
committed
feat: add gr versions of fmpz and fmpq
1 parent b7af81c commit f8f0ffb

File tree

8 files changed

+537
-6
lines changed

8 files changed

+537
-6
lines changed

src/flint/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from .pyflint import *
22

3+
from .types.gr import gr_ctx, gr_fmpz_ctx, gr_fmpq_ctx
4+
35
from .types.fmpz import *
46
from .types.fmpz_poly import *
57
from .types.fmpz_mat import *

src/flint/flint_base/flint_base.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from flint.flintlib.types.mpoly cimport ordering_t
22

3+
cdef class flint_ctx:
4+
pass
5+
36
cdef class flint_elem:
47
pass
58

src/flint/flintlib/functions/gr_domains.pxd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ from flint.flintlib.types.gr cimport gr_ctx_t, truth_t
55
from flint.flintlib.types.mpoly cimport ordering_t
66

77

8-
9-
cdef extern from "flint/gr_domains.h":
8+
# XXX: Need to fix the header from gr_domain.h to gr.h:
9+
# Needs hard-coding in rst_to_pxd.py
10+
cdef extern from "flint/gr.h":
1011
int gr_ctx_cmp_coercion(gr_ctx_t ctx1, gr_ctx_t ctx2)
1112
truth_t gr_ctx_is_finite(gr_ctx_t ctx)
1213
truth_t gr_ctx_is_multiplicative_group(gr_ctx_t ctx)

src/flint/flintlib/types/flint.pxd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,23 @@ cdef extern from "flint/flint.h":
6161
#define SIZEOF_ULONG sizeof(ulong)
6262
"""
6363
int SIZEOF_ULONG
64+
65+
ctypedef struct __FLINT_FILE:
66+
pass
67+
ctypedef __FLINT_FILE FLINT_FILE
68+
6469
const char * FLINT_VERSION
6570
const int __FLINT_RELEASE
71+
6672
const int FLINT_BITS
73+
6774
ctypedef void * flint_rand_t
6875
void flint_rand_init(flint_rand_t state)
6976
void flint_rand_clear(flint_rand_t state)
77+
7078
void flint_set_num_threads(long)
7179
long flint_get_num_threads()
80+
7281
void flint_cleanup()
7382

7483
ctypedef struct nmod_t:

src/flint/flintlib/types/gr.pxd

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
from flint.flintlib.types.flint cimport (
2+
FLINT_FILE,
3+
slong,
4+
ulong,
5+
flint_bitcnt_t,
6+
)
7+
8+
19
cdef extern from "flint/gr.h":
210

311
cdef enum truth_t_enum:
412
T_TRUE
513
T_FALSE
6-
T_UNKOWN
14+
T_UNKNOWN
715

816
ctypedef truth_t_enum truth_t
917

18+
# Macros
19+
cdef int GR_SUCCESS
20+
cdef int GR_DOMAIN
21+
cdef int GR_UNABLE
22+
1023
ctypedef struct gr_stream_struct:
1124
FLINT_FILE * fp
1225
char * s
@@ -15,12 +28,10 @@ cdef extern from "flint/gr.h":
1528

1629
ctypedef gr_stream_struct gr_stream_t[1]
1730

18-
ctypedef int (*gr_funcptr)(void)
31+
ctypedef int (*gr_funcptr)()
1932

2033
cdef const int GR_CTX_STRUCT_DATA_BYTES
2134

22-
ctypedef int (*gr_funcptr)(void)
23-
2435
cdef struct gr_ctx_struct:
2536
char data[GR_CTX_STRUCT_DATA_BYTES]
2637
ulong which_ring

src/flint/types/gr.pxd

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)