Skip to content

Commit 5a4a2aa

Browse files
committed
fix: __rdunder__ acb_poly, acb_series, arb_poly, arb_series
1 parent 5082c78 commit 5a4a2aa

File tree

5 files changed

+382
-277
lines changed

5 files changed

+382
-277
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ MANIFEST
1515
*.egg-info
1616
.coverage
1717
*.swp
18+
.python-version

src/flint/acb_poly.pyx

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
cdef acb_poly_coerce_operands(x, y):
2-
if typecheck(x, acb_poly):
3-
if isinstance(y, (int, long, float, complex, fmpz, fmpq, arb, acb, fmpz_poly, fmpq_poly, arb_poly)):
4-
return x, acb_poly(y)
5-
else:
6-
if isinstance(x, (int, long, float, complex, fmpz, fmpq, arb, acb, fmpz_poly, fmpq_poly, arb_poly)):
7-
return acb_poly(x), y
2+
if isinstance(y, (int, long, float, complex, fmpz, fmpq, arb, acb, fmpz_poly, fmpq_poly, arb_poly)):
3+
return x, acb_poly(y)
84
return NotImplemented, NotImplemented
95

106
cdef acb_poly_set_list(acb_poly_t poly, list val, long prec):
@@ -188,76 +184,112 @@ cdef class acb_poly(flint_poly):
188184
return u
189185

190186
def __add__(s, t):
191-
if type(s) is type(t):
192-
u = acb_poly.__new__(acb_poly)
193-
acb_poly_add((<acb_poly>u).val, (<acb_poly>s).val, (<acb_poly>t).val, getprec())
194-
return u
187+
if not isinstance(t, acb_poly):
188+
s, t = acb_poly_coerce_operands(s, t)
189+
if s is NotImplemented:
190+
return s
191+
return s + t
192+
u = acb_poly.__new__(acb_poly)
193+
acb_poly_add((<acb_poly>u).val, (<acb_poly>s).val, (<acb_poly>t).val, getprec())
194+
return u
195+
196+
def __radd__(s, t):
195197
s, t = acb_poly_coerce_operands(s, t)
196198
if s is NotImplemented:
197199
return s
198-
return s + t
200+
return t + s
199201

200202
def __sub__(s, t):
201-
if type(s) is type(t):
202-
u = acb_poly.__new__(acb_poly)
203-
acb_poly_sub((<acb_poly>u).val, (<acb_poly>s).val, (<acb_poly>t).val, getprec())
204-
return u
203+
if not isinstance(t, acb_poly):
204+
s, t = acb_poly_coerce_operands(s, t)
205+
if s is NotImplemented:
206+
return s
207+
return s - t
208+
u = acb_poly.__new__(acb_poly)
209+
acb_poly_sub((<acb_poly>u).val, (<acb_poly>s).val, (<acb_poly>t).val, getprec())
210+
return u
211+
212+
def __rsub__(s, t):
205213
s, t = acb_poly_coerce_operands(s, t)
206214
if s is NotImplemented:
207215
return s
208-
return s - t
216+
return t - s
209217

210218
def __mul__(s, t):
211-
if type(s) is type(t):
212-
u = acb_poly.__new__(acb_poly)
213-
acb_poly_mul((<acb_poly>u).val, (<acb_poly>s).val, (<acb_poly>t).val, getprec())
214-
return u
219+
if not isinstance(t, acb_poly):
220+
s, t = acb_poly_coerce_operands(s, t)
221+
if s is NotImplemented:
222+
return s
223+
return s * t
224+
u = acb_poly.__new__(acb_poly)
225+
acb_poly_mul((<acb_poly>u).val, (<acb_poly>s).val, (<acb_poly>t).val, getprec())
226+
return u
227+
228+
def __rmul__(s, t):
215229
s, t = acb_poly_coerce_operands(s, t)
216230
if s is NotImplemented:
217231
return s
218-
return s * t
232+
return t * s
219233

220234
def __floordiv__(s, t):
221-
if type(s) is type(t):
222-
q = acb_poly.__new__(acb_poly)
223-
r = acb_poly.__new__(acb_poly)
224-
if acb_poly_divrem((<acb_poly>q).val, (<acb_poly>r).val,
225-
(<acb_poly>s).val, (<acb_poly>t).val, getprec()):
226-
return q
227-
else:
228-
raise ZeroDivisionError("acb_poly leading coefficient must be nonzero")
235+
if not isinstance(t, acb_poly):
236+
s, t = acb_poly_coerce_operands(s, t)
237+
if s is NotImplemented:
238+
return s
239+
return s // t
240+
q = acb_poly.__new__(acb_poly)
241+
r = acb_poly.__new__(acb_poly)
242+
if acb_poly_divrem((<acb_poly>q).val, (<acb_poly>r).val,
243+
(<acb_poly>s).val, (<acb_poly>t).val, getprec()):
244+
return q
245+
else:
246+
raise ZeroDivisionError("acb_poly leading coefficient must be nonzero")
247+
248+
def __rfloordiv__(s, t):
229249
s, t = acb_poly_coerce_operands(s, t)
230250
if s is NotImplemented:
231251
return s
232-
return s // t
252+
return t // s
233253

234254
def __mod__(s, t):
235-
if type(s) is type(t):
236-
q = acb_poly.__new__(acb_poly)
237-
r = acb_poly.__new__(acb_poly)
238-
if acb_poly_divrem((<acb_poly>q).val, (<acb_poly>r).val,
239-
(<acb_poly>s).val, (<acb_poly>t).val, getprec()):
240-
return r
241-
else:
242-
raise ZeroDivisionError("acb_poly leading coefficient must be nonzero")
255+
if not isinstance(t, acb_poly):
256+
s, t = acb_poly_coerce_operands(s, t)
257+
if s is NotImplemented:
258+
return s
259+
return s % t
260+
q = acb_poly.__new__(acb_poly)
261+
r = acb_poly.__new__(acb_poly)
262+
if acb_poly_divrem((<acb_poly>q).val, (<acb_poly>r).val,
263+
(<acb_poly>s).val, (<acb_poly>t).val, getprec()):
264+
return r
265+
else:
266+
raise ZeroDivisionError("acb_poly leading coefficient must be nonzero")
267+
268+
def __rmod__(s, t):
243269
s, t = acb_poly_coerce_operands(s, t)
244270
if s is NotImplemented:
245271
return s
246-
return s % t
272+
return t % s
247273

248274
def __divmod__(s, t):
249-
if type(s) is type(t):
250-
q = acb_poly.__new__(acb_poly)
251-
r = acb_poly.__new__(acb_poly)
252-
if acb_poly_divrem((<acb_poly>q).val, (<acb_poly>r).val,
253-
(<acb_poly>s).val, (<acb_poly>t).val, getprec()):
254-
return q, r
255-
else:
256-
raise ZeroDivisionError("acb_poly leading coefficient must be nonzero")
275+
if not isinstance(t, acb_poly):
276+
s, t = acb_poly_coerce_operands(s, t)
277+
if s is NotImplemented:
278+
return s
279+
return divmod(s, t)
280+
q = acb_poly.__new__(acb_poly)
281+
r = acb_poly.__new__(acb_poly)
282+
if acb_poly_divrem((<acb_poly>q).val, (<acb_poly>r).val,
283+
(<acb_poly>s).val, (<acb_poly>t).val, getprec()):
284+
return q, r
285+
else:
286+
raise ZeroDivisionError("acb_poly leading coefficient must be nonzero")
287+
288+
def __rdivmod__(s, t):
257289
s, t = acb_poly_coerce_operands(s, t)
258290
if s is NotImplemented:
259291
return s
260-
return divmod(s, t)
292+
return divmod(t, s)
261293

262294
def __pow__(acb_poly s, ulong exp, mod):
263295
if mod is not None:

0 commit comments

Comments
 (0)