|
1 | 1 | 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) |
8 | 4 | return NotImplemented, NotImplemented |
9 | 5 |
|
10 | 6 | cdef acb_poly_set_list(acb_poly_t poly, list val, long prec): |
@@ -188,76 +184,112 @@ cdef class acb_poly(flint_poly): |
188 | 184 | return u |
189 | 185 |
|
190 | 186 | 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): |
195 | 197 | s, t = acb_poly_coerce_operands(s, t) |
196 | 198 | if s is NotImplemented: |
197 | 199 | return s |
198 | | - return s + t |
| 200 | + return t + s |
199 | 201 |
|
200 | 202 | 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): |
205 | 213 | s, t = acb_poly_coerce_operands(s, t) |
206 | 214 | if s is NotImplemented: |
207 | 215 | return s |
208 | | - return s - t |
| 216 | + return t - s |
209 | 217 |
|
210 | 218 | 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): |
215 | 229 | s, t = acb_poly_coerce_operands(s, t) |
216 | 230 | if s is NotImplemented: |
217 | 231 | return s |
218 | | - return s * t |
| 232 | + return t * s |
219 | 233 |
|
220 | 234 | 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): |
229 | 249 | s, t = acb_poly_coerce_operands(s, t) |
230 | 250 | if s is NotImplemented: |
231 | 251 | return s |
232 | | - return s // t |
| 252 | + return t // s |
233 | 253 |
|
234 | 254 | 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): |
243 | 269 | s, t = acb_poly_coerce_operands(s, t) |
244 | 270 | if s is NotImplemented: |
245 | 271 | return s |
246 | | - return s % t |
| 272 | + return t % s |
247 | 273 |
|
248 | 274 | 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): |
257 | 289 | s, t = acb_poly_coerce_operands(s, t) |
258 | 290 | if s is NotImplemented: |
259 | 291 | return s |
260 | | - return divmod(s, t) |
| 292 | + return divmod(t, s) |
261 | 293 |
|
262 | 294 | def __pow__(acb_poly s, ulong exp, mod): |
263 | 295 | if mod is not None: |
|
0 commit comments