Skip to content

Commit f759cc5

Browse files
committed
fix: add __radd__ etc to fmpz
This is needed for Cython 3.x compatibility: https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html#arithmetic-methods Similar methods should be added to all python-flint types that have arithmetic operations.
1 parent 0d07fee commit f759cc5

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src/flint/fmpz.pyx

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ cdef class fmpz(flint_scalar):
189189
if ttype == FMPZ_TMP: fmpz_clear(tval)
190190
return u
191191

192+
def __radd__(s, t):
193+
cdef fmpz_struct tval[1]
194+
cdef int ttype = FMPZ_UNKNOWN
195+
u = NotImplemented
196+
ttype = fmpz_set_any_ref(tval, t)
197+
if ttype != FMPZ_UNKNOWN:
198+
u = fmpz.__new__(fmpz)
199+
fmpz_add((<fmpz>u).val, tval, (<fmpz>s).val)
200+
if ttype == FMPZ_TMP: fmpz_clear(tval)
201+
return u
202+
192203
def __sub__(s, t):
193204
cdef fmpz_struct sval[1]
194205
cdef fmpz_struct tval[1]
@@ -205,6 +216,17 @@ cdef class fmpz(flint_scalar):
205216
if ttype == FMPZ_TMP: fmpz_clear(tval)
206217
return u
207218

219+
def __rsub__(s, t):
220+
cdef fmpz_struct tval[1]
221+
cdef int ttype = FMPZ_UNKNOWN
222+
u = NotImplemented
223+
ttype = fmpz_set_any_ref(tval, t)
224+
if ttype != FMPZ_UNKNOWN:
225+
u = fmpz.__new__(fmpz)
226+
fmpz_sub((<fmpz>u).val, tval, (<fmpz>s).val)
227+
if ttype == FMPZ_TMP: fmpz_clear(tval)
228+
return u
229+
208230
def __mul__(s, t):
209231
cdef fmpz_struct sval[1]
210232
cdef fmpz_struct tval[1]
@@ -221,6 +243,17 @@ cdef class fmpz(flint_scalar):
221243
if ttype == FMPZ_TMP: fmpz_clear(tval)
222244
return u
223245

246+
def __rmul__(s, t):
247+
cdef fmpz_struct tval[1]
248+
cdef int ttype = FMPZ_UNKNOWN
249+
u = NotImplemented
250+
ttype = fmpz_set_any_ref(tval, t)
251+
if ttype != FMPZ_UNKNOWN:
252+
u = fmpz.__new__(fmpz)
253+
fmpz_mul((<fmpz>u).val, tval, (<fmpz>s).val)
254+
if ttype == FMPZ_TMP: fmpz_clear(tval)
255+
return u
256+
224257
def __floordiv__(s, t):
225258
cdef fmpz_struct sval[1]
226259
cdef fmpz_struct tval[1]
@@ -241,6 +274,21 @@ cdef class fmpz(flint_scalar):
241274
if ttype == FMPZ_TMP: fmpz_clear(tval)
242275
return u
243276

277+
def __rfloordiv__(s, t):
278+
cdef fmpz_struct tval[1]
279+
cdef int ttype = FMPZ_UNKNOWN
280+
u = NotImplemented
281+
ttype = fmpz_set_any_ref(tval, t)
282+
if ttype != FMPZ_UNKNOWN:
283+
if fmpz_is_zero((<fmpz>s).val):
284+
if ttype == FMPZ_TMP:
285+
fmpz_clear(tval)
286+
raise ZeroDivisionError("fmpz division by zero")
287+
u = fmpz.__new__(fmpz)
288+
fmpz_fdiv_q((<fmpz>u).val, tval, (<fmpz>s).val)
289+
if ttype == FMPZ_TMP: fmpz_clear(tval)
290+
return u
291+
244292
def __mod__(s, t):
245293
cdef fmpz_struct sval[1]
246294
cdef fmpz_struct tval[1]
@@ -261,6 +309,21 @@ cdef class fmpz(flint_scalar):
261309
if ttype == FMPZ_TMP: fmpz_clear(tval)
262310
return u
263311

312+
def __rmod__(s, t):
313+
cdef fmpz_struct tval[1]
314+
cdef int ttype = FMPZ_UNKNOWN
315+
u = NotImplemented
316+
ttype = fmpz_set_any_ref(tval, t)
317+
if ttype != FMPZ_UNKNOWN:
318+
if fmpz_is_zero((<fmpz>s).val):
319+
if ttype == FMPZ_TMP:
320+
fmpz_clear(tval)
321+
raise ZeroDivisionError("fmpz division by zero")
322+
u = fmpz.__new__(fmpz)
323+
fmpz_fdiv_r((<fmpz>u).val, tval, (<fmpz>s).val)
324+
if ttype == FMPZ_TMP: fmpz_clear(tval)
325+
return u
326+
264327
def __divmod__(s, t):
265328
cdef fmpz_struct sval[1]
266329
cdef fmpz_struct tval[1]
@@ -283,6 +346,23 @@ cdef class fmpz(flint_scalar):
283346
if ttype == FMPZ_TMP: fmpz_clear(tval)
284347
return u
285348

349+
def __rdivmod__(s, t):
350+
cdef fmpz_struct tval[1]
351+
cdef int ttype = FMPZ_UNKNOWN
352+
u = NotImplemented
353+
ttype = fmpz_set_any_ref(tval, t)
354+
if ttype != FMPZ_UNKNOWN:
355+
if fmpz_is_zero((<fmpz>s).val):
356+
if ttype == FMPZ_TMP:
357+
fmpz_clear(tval)
358+
raise ZeroDivisionError("fmpz division by zero")
359+
u1 = fmpz.__new__(fmpz)
360+
u2 = fmpz.__new__(fmpz)
361+
fmpz_fdiv_qr((<fmpz>u1).val, (<fmpz>u2).val, tval, (<fmpz>s).val)
362+
u = u1, u2
363+
if ttype == FMPZ_TMP: fmpz_clear(tval)
364+
return u
365+
286366
def __pow__(s, t, m):
287367
cdef fmpz_struct sval[1]
288368
cdef int stype = FMPZ_UNKNOWN
@@ -298,6 +378,21 @@ cdef class fmpz(flint_scalar):
298378
if stype == FMPZ_TMP: fmpz_clear(sval)
299379
return u
300380

381+
def __rpow__(s, t, m):
382+
cdef fmpz_struct tval[1]
383+
cdef int stype = FMPZ_UNKNOWN
384+
cdef ulong exp
385+
u = NotImplemented
386+
if m is not None:
387+
raise NotImplementedError("modular exponentiation")
388+
ttype = fmpz_set_any_ref(tval, t)
389+
if ttype != FMPZ_UNKNOWN:
390+
u = fmpz.__new__(fmpz)
391+
s_ulong = fmpz_get_ui(s.val)
392+
fmpz_pow_ui((<fmpz>u).val, tval, s_ulong)
393+
if ttype == FMPZ_TMP: fmpz_clear(tval)
394+
return u
395+
301396
def gcd(self, other):
302397
"""
303398
Returns the greatest common divisor of self and other.

0 commit comments

Comments
 (0)