Skip to content

Commit 1c88ec3

Browse files
committed
Revert "Remove in-place add, sub, and mul"
This reverts commit 8f76c6a.
1 parent 424dc4f commit 1c88ec3

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/flint/test/test_all.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,6 +2845,8 @@ def quick_poly():
28452845
assert raises(lambda: mpoly({(0, 0): 2, (0, 1): 2, (1, 0): 3, (2, 2): 4}) + None, TypeError)
28462846
assert raises(lambda: None + mpoly({(0, 0): 2, (0, 1): 2, (1, 0): 3, (2, 2): 4}), TypeError)
28472847
assert raises(lambda: quick_poly() + P(ctx=ctx1), IncompatibleContextError)
2848+
assert raises(lambda: quick_poly().__iadd__(P(ctx=ctx1)), IncompatibleContextError)
2849+
assert quick_poly().__iadd__(None) is NotImplemented
28482850

28492851
assert quick_poly() - mpoly({(0, 0): 5, (0, 1): 6, (1, 0): 7, (2, 2): 8}) \
28502852
== mpoly({(0, 0): -4, (0, 1): -4, (1, 0): -4, (2, 2): -4})
@@ -2858,6 +2860,8 @@ def quick_poly():
28582860
assert raises(lambda: quick_poly() - None, TypeError)
28592861
assert raises(lambda: None - quick_poly(), TypeError)
28602862
assert raises(lambda: quick_poly() - P(ctx=ctx1), IncompatibleContextError)
2863+
assert raises(lambda: quick_poly().__isub__(P(ctx=ctx1)), IncompatibleContextError)
2864+
assert quick_poly().__isub__(None) is NotImplemented
28612865

28622866
assert quick_poly() * mpoly({(1, 0): 5, (0, 1): 6}) \
28632867
== mpoly({
@@ -2879,6 +2883,8 @@ def quick_poly():
28792883
assert raises(lambda: quick_poly() * None, TypeError)
28802884
assert raises(lambda: None * quick_poly(), TypeError)
28812885
assert raises(lambda: quick_poly() * P(ctx=ctx1), IncompatibleContextError)
2886+
assert raises(lambda: quick_poly().__imul__(P(ctx=ctx1)), IncompatibleContextError)
2887+
assert quick_poly().__imul__(None) is NotImplemented
28822888

28832889
assert quick_poly() // mpoly({(1, 1): 1}) == mpoly({(1, 1): 4})
28842890
assert quick_poly() % mpoly({(1, 1): 1}) \

src/flint/types/fmpq_mpoly.pyx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ cdef class fmpq_mpoly(flint_mpoly):
346346
return res
347347
return NotImplemented
348348

349+
def __iadd__(self, other):
350+
if typecheck(other, fmpq_mpoly):
351+
if (<fmpq_mpoly>self).ctx is not (<fmpq_mpoly>other).ctx:
352+
raise IncompatibleContextError(f"{(<fmpq_mpoly>self).ctx} is not {(<fmpq_mpoly>other).ctx}")
353+
fmpq_mpoly_add((<fmpq_mpoly>self).val, (<fmpq_mpoly>self).val, (<fmpq_mpoly>other).val, self.ctx.val)
354+
return self
355+
else:
356+
other = any_as_fmpq(other)
357+
if other is not NotImplemented:
358+
fmpq_mpoly_add_fmpq((<fmpq_mpoly>self).val, (<fmpq_mpoly>self).val, (<fmpq>other).val, self.ctx.val)
359+
return self
360+
return NotImplemented
361+
349362
def __sub__(self, other):
350363
cdef fmpq_mpoly res
351364
if typecheck(other, fmpq_mpoly):
@@ -371,6 +384,19 @@ cdef class fmpq_mpoly(flint_mpoly):
371384
return -res
372385
return NotImplemented
373386

387+
def __isub__(self, other):
388+
if typecheck(other, fmpq_mpoly):
389+
if (<fmpq_mpoly>self).ctx is not (<fmpq_mpoly>other).ctx:
390+
raise IncompatibleContextError(f"{(<fmpq_mpoly>self).ctx} is not {(<fmpq_mpoly>other).ctx}")
391+
fmpq_mpoly_sub((<fmpq_mpoly>self).val, (<fmpq_mpoly>self).val, (<fmpq_mpoly>other).val, self.ctx.val)
392+
return self
393+
else:
394+
other = any_as_fmpq(other)
395+
if other is not NotImplemented:
396+
fmpq_mpoly_sub_fmpq((<fmpq_mpoly>self).val, (<fmpq_mpoly>self).val, (<fmpq>other).val, self.ctx.val)
397+
return self
398+
return NotImplemented
399+
374400
def __mul__(self, other):
375401
cdef fmpq_mpoly res
376402
if typecheck(other, fmpq_mpoly):
@@ -396,6 +422,19 @@ cdef class fmpq_mpoly(flint_mpoly):
396422
return res
397423
return NotImplemented
398424

425+
def __imul__(self, other):
426+
if typecheck(other, fmpq_mpoly):
427+
if (<fmpq_mpoly>self).ctx is not (<fmpq_mpoly>other).ctx:
428+
raise IncompatibleContextError(f"{(<fmpq_mpoly>self).ctx} is not {(<fmpq_mpoly>other).ctx}")
429+
fmpq_mpoly_mul((<fmpq_mpoly>self).val, (<fmpq_mpoly>self).val, (<fmpq_mpoly>other).val, self.ctx.val)
430+
return self
431+
else:
432+
other = any_as_fmpq(other)
433+
if other is not NotImplemented:
434+
fmpq_mpoly_scalar_mul_fmpq(self.val, (<fmpq_mpoly>self).val, (<fmpq>other).val, self.ctx.val)
435+
return self
436+
return NotImplemented
437+
399438
def __pow__(self, other, modulus):
400439
cdef fmpq_mpoly res
401440
if modulus is not None:

src/flint/types/fmpz_mpoly.pyx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,19 @@ cdef class fmpz_mpoly(flint_mpoly):
326326
return res
327327
return NotImplemented
328328

329+
def __iadd__(self, other):
330+
if typecheck(other, fmpz_mpoly):
331+
if (<fmpz_mpoly>self).ctx is not (<fmpz_mpoly>other).ctx:
332+
raise IncompatibleContextError(f"{(<fmpz_mpoly>self).ctx} is not {(<fmpz_mpoly>other).ctx}")
333+
fmpz_mpoly_add((<fmpz_mpoly>self).val, (<fmpz_mpoly>self).val, (<fmpz_mpoly>other).val, self.ctx.val)
334+
return self
335+
else:
336+
zval = any_as_fmpz(other)
337+
if zval is not NotImplemented:
338+
fmpz_mpoly_add_fmpz((<fmpz_mpoly>self).val, (<fmpz_mpoly>self).val, (<fmpz>zval).val, self.ctx.val)
339+
return self
340+
return NotImplemented
341+
329342
def __sub__(self, other):
330343
cdef fmpz_mpoly res
331344
if typecheck(other, fmpz_mpoly):
@@ -351,6 +364,19 @@ cdef class fmpz_mpoly(flint_mpoly):
351364
return -res
352365
return NotImplemented
353366

367+
def __isub__(self, other):
368+
if typecheck(other, fmpz_mpoly):
369+
if (<fmpz_mpoly>self).ctx is not (<fmpz_mpoly>other).ctx:
370+
raise IncompatibleContextError(f"{(<fmpz_mpoly>self).ctx} is not {(<fmpz_mpoly>other).ctx}")
371+
fmpz_mpoly_sub((<fmpz_mpoly>self).val, (<fmpz_mpoly>self).val, (<fmpz_mpoly>other).val, self.ctx.val)
372+
return self
373+
else:
374+
other = any_as_fmpz(other)
375+
if other is not NotImplemented:
376+
fmpz_mpoly_sub_fmpz((<fmpz_mpoly>self).val, (<fmpz_mpoly>self).val, (<fmpz>other).val, self.ctx.val)
377+
return self
378+
return NotImplemented
379+
354380
def __mul__(self, other):
355381
cdef fmpz_mpoly res
356382
if typecheck(other, fmpz_mpoly):
@@ -376,6 +402,19 @@ cdef class fmpz_mpoly(flint_mpoly):
376402
return res
377403
return NotImplemented
378404

405+
def __imul__(self, other):
406+
if typecheck(other, fmpz_mpoly):
407+
if (<fmpz_mpoly>self).ctx is not (<fmpz_mpoly>other).ctx:
408+
raise IncompatibleContextError(f"{(<fmpz_mpoly>self).ctx} is not {(<fmpz_mpoly>other).ctx}")
409+
fmpz_mpoly_mul((<fmpz_mpoly>self).val, (<fmpz_mpoly>self).val, (<fmpz_mpoly>other).val, self.ctx.val)
410+
return self
411+
else:
412+
other = any_as_fmpz(other)
413+
if other is not NotImplemented:
414+
fmpz_mpoly_scalar_mul_fmpz(self.val, (<fmpz_mpoly>self).val, (<fmpz>other).val, self.ctx.val)
415+
return self
416+
return NotImplemented
417+
379418
def __pow__(self, other, modulus):
380419
cdef fmpz_mpoly res
381420
if modulus is not None:

0 commit comments

Comments
 (0)