Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gr: Improve conversions #254

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/flint/types/_gr.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ from flint.flintlib.functions.gr_domains cimport (
)
from flint.flintlib.functions.gr cimport (
gr_heap_init,
gr_set_d,
gr_set_other,
gr_set_str,
gr_get_str,
gr_set,
Expand Down Expand Up @@ -214,6 +216,24 @@ cdef class gr_ctx(flint_ctx):
py_val._init = True
return py_val

@cython.final
cdef inline gr from_d(self, double d):
cdef gr py_val
py_val = self.new_gr()
err = gr_set_d(py_val.pval, d, self.ctx_t)
if err != GR_SUCCESS:
raise self._error(err, "Incorrect conversion from a double")
return py_val

@cython.final
cdef inline gr from_other(self, gr x):
cdef gr py_val
py_val = self.new_gr()
err = gr_set_other(py_val.pval, x.pval, x.ctx.ctx_t, self.ctx_t)
if err != GR_SUCCESS:
raise self._error(err, "Incorrect conversion")
return py_val

@cython.final
cdef inline gr from_str(self, s: str):
cdef gr py_val
Expand Down
22 changes: 17 additions & 5 deletions src/flint/types/_gr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,23 @@ cdef class gr_ctx(flint_ctx):
def __call__(self, arg) -> gr:
"""Create a new element of the domain.

>>> from flint.types._gr import gr_fmpz_ctx
>>> ctx = gr_fmpz_ctx
>>> ctx(2)
2
"""
>>> from flint.types._gr import gr_fmpz_ctx
>>> ctx = gr_fmpz_ctx
>>> ctx(2)
2
>>> ctx(18446744073709551615)
18446744073709551615
"""
if isinstance(arg, gr):
return self.from_other(arg)
if type(arg) is int:
try:
return self.from_si(arg)
except OverflowError:
pass
if type(arg) is float:
return self.from_d(arg)
# TODO: fmpz & fmpq ?
try:
return self.from_str(str(arg))
except AssertionError:
Expand Down
Loading