File tree 2 files changed +37
-5
lines changed
2 files changed +37
-5
lines changed Original file line number Diff line number Diff line change @@ -86,6 +86,8 @@ from flint.flintlib.functions.gr_domains cimport (
86
86
)
87
87
from flint.flintlib.functions.gr cimport (
88
88
gr_heap_init,
89
+ gr_set_d,
90
+ gr_set_other,
89
91
gr_set_str,
90
92
gr_get_str,
91
93
gr_set,
@@ -214,6 +216,24 @@ cdef class gr_ctx(flint_ctx):
214
216
py_val._init = True
215
217
return py_val
216
218
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
+
217
237
@cython.final
218
238
cdef inline gr from_str(self , s: str ):
219
239
cdef gr py_val
Original file line number Diff line number Diff line change @@ -224,11 +224,23 @@ cdef class gr_ctx(flint_ctx):
224
224
def __call__(self , arg ) -> gr:
225
225
"""Create a new element of the domain.
226
226
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 ?
232
244
try :
233
245
return self .from_str(str (arg))
234
246
except AssertionError :
You can’t perform that action at this time.
0 commit comments