Skip to content

Commit 9f74e62

Browse files
committed
fix: add __rdunder__ fmpz_poly, fmpq_poly and nmp_poly
1 parent 6946479 commit 9f74e62

File tree

3 files changed

+185
-104
lines changed

3 files changed

+185
-104
lines changed

src/flint/fmpq_poly.pyx

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -176,70 +176,98 @@ cdef class fmpq_poly(flint_poly):
176176
fmpq_poly_neg(res.val, self.val)
177177
return res
178178

179-
def __add__(s, t):
179+
def _add_(s, t):
180180
cdef fmpq_poly r
181-
s = any_as_fmpq_poly(s)
182-
if s is NotImplemented:
183-
return s
184181
t = any_as_fmpq_poly(t)
185182
if t is NotImplemented:
186183
return t
187184
r = fmpq_poly.__new__(fmpq_poly)
188185
fmpq_poly_add(r.val, (<fmpq_poly>s).val, (<fmpq_poly>t).val)
189186
return r
190187

191-
def __sub__(s, t):
188+
def __add__(s, t):
189+
return s._add_(t)
190+
191+
def __radd__(s, t):
192+
return s._add_(t)
193+
194+
def _sub_(s, t):
192195
cdef fmpq_poly r
193-
s = any_as_fmpq_poly(s)
194-
if s is NotImplemented:
195-
return s
196-
t = any_as_fmpq_poly(t)
197-
if t is NotImplemented:
198-
return t
199196
r = fmpq_poly.__new__(fmpq_poly)
200197
fmpq_poly_sub(r.val, (<fmpq_poly>s).val, (<fmpq_poly>t).val)
201198
return r
202199

203-
def __mul__(s, t):
200+
def __sub__(s, t):
201+
t = any_as_fmpq_poly(t)
202+
if t is NotImplemented:
203+
return t
204+
return s._sub_(t)
205+
206+
def __rsub__(s, t):
207+
t = any_as_fmpq_poly(t)
208+
if t is NotImplemented:
209+
return t
210+
return t._sub_(s)
211+
212+
def _mul_(s, t):
204213
cdef fmpq_poly r
205-
s = any_as_fmpq_poly(s)
206-
if s is NotImplemented:
207-
return s
208214
t = any_as_fmpq_poly(t)
209215
if t is NotImplemented:
210216
return t
211217
r = fmpq_poly.__new__(fmpq_poly)
212218
fmpq_poly_mul(r.val, (<fmpq_poly>s).val, (<fmpq_poly>t).val)
213219
return r
214220

215-
def __floordiv__(s, t):
221+
def __mul__(s, t):
222+
return s._mul_(t)
223+
224+
def __rmul__(s, t):
225+
return s._mul_(t)
226+
227+
def _floordiv_(s, t):
216228
cdef fmpq_poly r
217-
s = any_as_fmpq_poly(s)
218-
if s is NotImplemented:
219-
return s
220-
t = any_as_fmpq_poly(t)
221-
if t is NotImplemented:
222-
return t
223229
if fmpq_poly_is_zero((<fmpq_poly>t).val):
224230
raise ZeroDivisionError("fmpq_poly division by 0")
225231
r = fmpq_poly.__new__(fmpq_poly)
226232
fmpq_poly_div(r.val, (<fmpq_poly>s).val, (<fmpq_poly>t).val)
227233
return r
228234

229-
def __mod__(s, t):
235+
def __floordiv__(s, t):
236+
cdef fmpq_poly r
237+
t = any_as_fmpq_poly(t)
238+
if t is NotImplemented:
239+
return t
240+
return s._floordiv_(t)
241+
242+
def __rfloordiv__(s, t):
230243
cdef fmpq_poly r
231-
s = any_as_fmpq_poly(s)
232-
if s is NotImplemented:
233-
return s
234244
t = any_as_fmpq_poly(t)
235245
if t is NotImplemented:
236246
return t
247+
return t._floordiv_(s)
248+
249+
def _mod_(s, t):
250+
cdef fmpq_poly r
237251
if fmpq_poly_is_zero((<fmpq_poly>t).val):
238252
raise ZeroDivisionError("fmpq_poly division by 0")
239253
r = fmpq_poly.__new__(fmpq_poly)
240254
fmpq_poly_rem(r.val, (<fmpq_poly>s).val, (<fmpq_poly>t).val)
241255
return r
242256

257+
def __mod__(s, t):
258+
cdef fmpq_poly r
259+
t = any_as_fmpq_poly(t)
260+
if t is NotImplemented:
261+
return t
262+
return s._mod_(t)
263+
264+
def __rmod__(s, t):
265+
cdef fmpq_poly r
266+
t = any_as_fmpq_poly(t)
267+
if t is NotImplemented:
268+
return t
269+
return t._mod_(s)
270+
243271
@staticmethod
244272
def _div_(fmpq_poly s, t):
245273
cdef fmpq_poly r
@@ -258,21 +286,27 @@ cdef class fmpq_poly(flint_poly):
258286
def __truediv__(s, t):
259287
return fmpq_poly._div_(s, t)
260288

261-
def __divmod__(s, t):
289+
def _divmod_(s, t):
262290
cdef fmpq_poly P, Q
263-
s = any_as_fmpq_poly(s)
264-
if s is NotImplemented:
265-
return s
266-
t = any_as_fmpq_poly(t)
267-
if t is NotImplemented:
268-
return t
269291
if fmpq_poly_is_zero((<fmpq_poly>t).val):
270292
raise ZeroDivisionError("fmpq_poly divmod by 0")
271293
P = fmpq_poly.__new__(fmpq_poly)
272294
Q = fmpq_poly.__new__(fmpq_poly)
273295
fmpq_poly_divrem(P.val, Q.val, (<fmpq_poly>s).val, (<fmpq_poly>t).val)
274296
return P, Q
275297

298+
def __divmod__(s, t):
299+
t = any_as_fmpq_poly(t)
300+
if t is NotImplemented:
301+
return t
302+
return s._divmod_(t)
303+
304+
def __rdivmod__(s, t):
305+
t = any_as_fmpq_poly(t)
306+
if t is NotImplemented:
307+
return t
308+
return t._divmod_(s)
309+
276310
def __pow__(fmpq_poly self, ulong exp, mod):
277311
cdef fmpq_poly res
278312
if mod is not None:

src/flint/fmpz_poly.pyx

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,85 +147,115 @@ cdef class fmpz_poly(flint_poly):
147147
fmpz_poly_neg(res.val, self.val)
148148
return res
149149

150-
def __add__(self, other):
150+
def _add_(self, other):
151151
cdef fmpz_poly res
152-
self = any_as_fmpz_poly(self)
153-
if self is NotImplemented:
154-
return self
155152
other = any_as_fmpz_poly(other)
156153
if other is NotImplemented:
157154
return other
158155
res = fmpz_poly.__new__(fmpz_poly)
159156
fmpz_poly_add(res.val, (<fmpz_poly>self).val, (<fmpz_poly>other).val)
160157
return res
161158

159+
def __add__(self, other):
160+
return self._add_(other)
161+
162+
def __radd__(self, other):
163+
return self._add_(other)
164+
162165
def __sub__(self, other):
163166
cdef fmpz_poly res
164-
self = any_as_fmpz_poly(self)
165-
if self is NotImplemented:
166-
return self
167167
other = any_as_fmpz_poly(other)
168168
if other is NotImplemented:
169169
return other
170170
res = fmpz_poly.__new__(fmpz_poly)
171171
fmpz_poly_sub(res.val, (<fmpz_poly>self).val, (<fmpz_poly>other).val)
172172
return res
173173

174-
def __mul__(self, other):
174+
def __rsub__(self, other):
175175
cdef fmpz_poly res
176-
self = any_as_fmpz_poly(self)
177-
if self is NotImplemented:
178-
return self
179176
other = any_as_fmpz_poly(other)
180177
if other is NotImplemented:
181178
return other
182179
res = fmpz_poly.__new__(fmpz_poly)
183-
fmpz_poly_mul(res.val, (<fmpz_poly>self).val, (<fmpz_poly>other).val)
180+
fmpz_poly_sub(res.val, (<fmpz_poly>other).val, (<fmpz_poly>self).val)
184181
return res
185182

186-
def __floordiv__(self, other):
183+
def _mul_(self, other):
187184
cdef fmpz_poly res
188-
self = any_as_fmpz_poly(self)
189-
if self is NotImplemented:
190-
return self
191185
other = any_as_fmpz_poly(other)
192186
if other is NotImplemented:
193187
return other
188+
res = fmpz_poly.__new__(fmpz_poly)
189+
fmpz_poly_mul(res.val, (<fmpz_poly>self).val, (<fmpz_poly>other).val)
190+
return res
191+
192+
def __mul__(self, other):
193+
return self._mul_(other)
194+
195+
def __rmul__(self, other):
196+
return self._mul_(other)
197+
198+
def _floordiv_(self, other):
199+
cdef fmpz_poly res
194200
if fmpz_poly_is_zero((<fmpz_poly>other).val):
195201
raise ZeroDivisionError("fmpz_poly division by 0")
196202
res = fmpz_poly.__new__(fmpz_poly)
197203
fmpz_poly_div(res.val, (<fmpz_poly>self).val, (<fmpz_poly>other).val)
198204
return res
199205

200-
def __mod__(self, other):
201-
cdef fmpz_poly res
202-
self = any_as_fmpz_poly(self)
203-
if self is NotImplemented:
204-
return self
206+
def __floordiv__(self, other):
207+
other = any_as_fmpz_poly(other)
208+
if other is NotImplemented:
209+
return other
210+
return self._floordiv_(other)
211+
212+
def __rfloordiv__(self, other):
205213
other = any_as_fmpz_poly(other)
206214
if other is NotImplemented:
207215
return other
216+
return other._floordiv_(self)
217+
218+
def _mod_(self, other):
219+
cdef fmpz_poly res
208220
if fmpz_poly_is_zero((<fmpz_poly>other).val):
209221
raise ZeroDivisionError("fmpz_poly division by 0")
210222
res = fmpz_poly.__new__(fmpz_poly)
211223
fmpz_poly_rem(res.val, (<fmpz_poly>self).val, (<fmpz_poly>other).val)
212224
return res
213225

214-
def __divmod__(self, other):
215-
cdef fmpz_poly P, Q
216-
self = any_as_fmpz_poly(self)
217-
if self is NotImplemented:
218-
return self
226+
def __mod__(self, other):
219227
other = any_as_fmpz_poly(other)
220228
if other is NotImplemented:
221229
return other
230+
return self._mod_(other)
231+
232+
def __rmod__(self, other):
233+
other = any_as_fmpz_poly(other)
234+
if other is NotImplemented:
235+
return other
236+
return other._mod_(self)
237+
238+
def _divmod_(self, other):
239+
cdef fmpz_poly P, Q
222240
if fmpz_poly_is_zero((<fmpz_poly>other).val):
223241
raise ZeroDivisionError("fmpz_poly divmod by 0")
224242
P = fmpz_poly.__new__(fmpz_poly)
225243
Q = fmpz_poly.__new__(fmpz_poly)
226244
fmpz_poly_divrem(P.val, Q.val, (<fmpz_poly>self).val, (<fmpz_poly>other).val)
227245
return P, Q
228246

247+
def __divmod__(self, other):
248+
other = any_as_fmpz_poly(other)
249+
if other is NotImplemented:
250+
return other
251+
return self._divmod_(other)
252+
253+
def __rdivmod__(self, other):
254+
other = any_as_fmpz_poly(other)
255+
if other is NotImplemented:
256+
return other
257+
return other._divmod_(self)
258+
229259
def __pow__(fmpz_poly self, ulong exp, mod):
230260
cdef fmpz_poly res
231261
if mod is not None:

0 commit comments

Comments
 (0)