Skip to content

Commit 7786fc0

Browse files
Merge pull request #254 from Tom-Hubrecht/gr-conversions
gr: Improve conversions
2 parents 9cb1e81 + 9dfcbf2 commit 7786fc0

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/flint/types/_gr.pxd

+20
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ from flint.flintlib.functions.gr_domains cimport (
8686
)
8787
from flint.flintlib.functions.gr cimport (
8888
gr_heap_init,
89+
gr_set_d,
90+
gr_set_other,
8991
gr_set_str,
9092
gr_get_str,
9193
gr_set,
@@ -214,6 +216,24 @@ cdef class gr_ctx(flint_ctx):
214216
py_val._init = True
215217
return py_val
216218

219+
@cython.final
220+
cdef inline gr from_d(self, double d):
221+
cdef gr py_val
222+
py_val = self.new_gr()
223+
err = gr_set_d(py_val.pval, d, self.ctx_t)
224+
if err != GR_SUCCESS:
225+
raise self._error(err, "Incorrect conversion from a double")
226+
return py_val
227+
228+
@cython.final
229+
cdef inline gr from_other(self, gr x):
230+
cdef gr py_val
231+
py_val = self.new_gr()
232+
err = gr_set_other(py_val.pval, x.pval, x.ctx.ctx_t, self.ctx_t)
233+
if err != GR_SUCCESS:
234+
raise self._error(err, "Incorrect conversion")
235+
return py_val
236+
217237
@cython.final
218238
cdef inline gr from_str(self, s: str):
219239
cdef gr py_val

src/flint/types/_gr.pyx

+17-5
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,23 @@ cdef class gr_ctx(flint_ctx):
224224
def __call__(self, arg) -> gr:
225225
"""Create a new element of the domain.
226226

227-
>>> from flint.types._gr import gr_fmpz_ctx
228-
>>> ctx = gr_fmpz_ctx
229-
>>> ctx(2)
230-
2
231-
"""
227+
>>> from flint.types._gr import gr_fmpz_ctx
228+
>>> ctx = gr_fmpz_ctx
229+
>>> ctx(2)
230+
2
231+
>>> ctx(18446744073709551615)
232+
18446744073709551615
233+
"""
234+
if isinstance(arg, gr):
235+
return self.from_other(arg)
236+
if type(arg) is int:
237+
try:
238+
return self.from_si(arg)
239+
except OverflowError:
240+
pass
241+
if type(arg) is float:
242+
return self.from_d(arg)
243+
# TODO: fmpz & fmpq ?
232244
try:
233245
return self.from_str(str(arg))
234246
except AssertionError:

0 commit comments

Comments
 (0)