Skip to content

Commit 630e20c

Browse files
Merge pull request #192 from Jake-Moss/mpoly_update
Prefer cdef to def in mpoly operands
2 parents 920a3a2 + c9de856 commit 630e20c

File tree

6 files changed

+459
-254
lines changed

6 files changed

+459
-254
lines changed

src/flint/flint_base/flint_base.pxd

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,37 @@ cdef class flint_mpoly_context(flint_elem):
1414
cdef const char ** c_names
1515

1616
cdef class flint_mpoly(flint_elem):
17-
pass
17+
cdef _add_scalar_(self, other)
18+
cdef _sub_scalar_(self, other)
19+
cdef _mul_scalar_(self, other)
20+
21+
cdef _add_mpoly_(self, other)
22+
cdef _sub_mpoly_(self, other)
23+
cdef _mul_mpoly_(self, other)
24+
25+
cdef _divmod_mpoly_(self, other)
26+
cdef _floordiv_mpoly_(self, other)
27+
cdef _truediv_mpoly_(self, other)
28+
cdef _mod_mpoly_(self, other)
29+
30+
cdef _rsub_scalar_(self, other)
31+
cdef _rsub_mpoly_(self, other)
32+
33+
cdef _rdivmod_mpoly_(self, other)
34+
cdef _rfloordiv_mpoly_(self, other)
35+
cdef _rtruediv_mpoly_(self, other)
36+
cdef _rmod_mpoly_(self, other)
37+
38+
cdef _pow_(self, other)
39+
40+
cdef _iadd_scalar_(self, other)
41+
cdef _isub_scalar_(self, other)
42+
cdef _imul_scalar_(self, other)
43+
44+
cdef _iadd_mpoly_(self, other)
45+
cdef _isub_mpoly_(self, other)
46+
cdef _imul_mpoly_(self, other)
47+
1848

1949
cdef class flint_mat(flint_elem):
2050
pass

src/flint/flint_base/flint_base.pyx

Lines changed: 131 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ cdef class flint_scalar(flint_elem):
165165
return self._invert_()
166166

167167

168-
169168
cdef class flint_poly(flint_elem):
170169
"""
171170
Base class for polynomials.
@@ -405,52 +404,73 @@ cdef class flint_mpoly(flint_elem):
405404
if not other:
406405
raise ZeroDivisionError("nmod_mpoly division by zero")
407406

408-
def _add_scalar_(self, other):
407+
cdef _add_scalar_(self, other):
408+
return NotImplemented
409+
410+
cdef _sub_scalar_(self, other):
411+
return NotImplemented
412+
413+
cdef _mul_scalar_(self, other):
414+
return NotImplemented
415+
416+
cdef _add_mpoly_(self, other):
417+
return NotImplemented
418+
419+
cdef _sub_mpoly_(self, other):
420+
return NotImplemented
421+
422+
cdef _mul_mpoly_(self, other):
409423
return NotImplemented
410424

411-
def _add_mpoly_(self, other):
425+
cdef _divmod_mpoly_(self, other):
412426
return NotImplemented
413427

414-
def _iadd_scalar_(self, other):
428+
cdef _floordiv_mpoly_(self, other):
415429
return NotImplemented
416430

417-
def _iadd_mpoly_(self, other):
431+
cdef _truediv_mpoly_(self, other):
418432
return NotImplemented
419433

420-
def _sub_scalar_(self, other):
434+
cdef _mod_mpoly_(self, other):
421435
return NotImplemented
422436

423-
def _sub_mpoly_(self, other):
437+
cdef _rsub_scalar_(self, other):
424438
return NotImplemented
425439

426-
def _isub_scalar_(self, other):
440+
cdef _rsub_mpoly_(self, other):
427441
return NotImplemented
428442

429-
def _isub_mpoly_(self, other):
443+
cdef _rdivmod_mpoly_(self, other):
430444
return NotImplemented
431445

432-
def _mul_scalar_(self, other):
446+
cdef _rfloordiv_mpoly_(self, other):
433447
return NotImplemented
434448

435-
def _imul_mpoly_(self, other):
449+
cdef _rtruediv_mpoly_(self, other):
436450
return NotImplemented
437451

438-
def _imul_scalar_(self, other):
452+
cdef _rmod_mpoly_(self, other):
439453
return NotImplemented
440454

441-
def _mul_mpoly_(self, other):
455+
cdef _pow_(self, other):
442456
return NotImplemented
443457

444-
def _pow_(self, other):
458+
cdef _iadd_scalar_(self, other):
445459
return NotImplemented
446460

447-
def _divmod_mpoly_(self, other):
461+
cdef _isub_scalar_(self, other):
448462
return NotImplemented
449463

450-
def _floordiv_mpoly_(self, other):
464+
cdef _imul_scalar_(self, other):
451465
return NotImplemented
452466

453-
def _truediv_mpoly_(self, other):
467+
cdef _iadd_mpoly_(self, other):
468+
return NotImplemented
469+
470+
cdef _isub_mpoly_(self, other):
471+
return NotImplemented
472+
473+
cdef _imul_mpoly_(self, other):
454474
return NotImplemented
455475

456476
def __add__(self, other):
@@ -465,32 +485,15 @@ cdef class flint_mpoly(flint_elem):
465485
return self._add_scalar_(other)
466486

467487
def __radd__(self, other):
468-
return self.__add__(other)
469-
470-
def iadd(self, other):
471-
"""
472-
In-place addition, mutates self.
473-
474-
>>> from flint import Ordering, fmpz_mpoly_ctx
475-
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
476-
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
477-
>>> f
478-
4*x0*x1 + 2*x0 + 3*x1
479-
>>> f.iadd(5)
480-
>>> f
481-
4*x0*x1 + 2*x0 + 3*x1 + 5
482-
483-
"""
484488
if typecheck(other, type(self)):
485489
self.context().compatible_context_check(other.context())
486-
self._iadd_mpoly_(other)
487-
return
490+
return self._add_mpoly_(other)
488491

489-
other_scalar = self.context().any_as_scalar(other)
490-
if other_scalar is NotImplemented:
491-
raise NotImplementedError(f"cannot add {type(self)} and {type(other)}")
492+
other = self.context().any_as_scalar(other)
493+
if other is NotImplemented:
494+
return NotImplemented
492495

493-
self._iadd_scalar_(other_scalar)
496+
return self._add_scalar_(other)
494497

495498
def __sub__(self, other):
496499
if typecheck(other, type(self)):
@@ -504,32 +507,15 @@ cdef class flint_mpoly(flint_elem):
504507
return self._sub_scalar_(other)
505508

506509
def __rsub__(self, other):
507-
return -self.__sub__(other)
508-
509-
def isub(self, other):
510-
"""
511-
In-place subtraction, mutates self.
512-
513-
>>> from flint import Ordering, fmpz_mpoly_ctx
514-
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
515-
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
516-
>>> f
517-
4*x0*x1 + 2*x0 + 3*x1
518-
>>> f.isub(5)
519-
>>> f
520-
4*x0*x1 + 2*x0 + 3*x1 - 5
521-
522-
"""
523510
if typecheck(other, type(self)):
524511
self.context().compatible_context_check(other.context())
525-
self._isub_mpoly_(other)
526-
return
512+
return self._rsub_mpoly_(other)
527513

528-
other_scalar = self.context().any_as_scalar(other)
529-
if other_scalar is NotImplemented:
530-
raise NotImplementedError(f"cannot subtract {type(self)} and {type(other)}")
514+
other = self.context().any_as_scalar(other)
515+
if other is NotImplemented:
516+
return NotImplemented
531517

532-
self._isub_scalar_(other_scalar)
518+
return self._rsub_scalar_(other)
533519

534520
def __mul__(self, other):
535521
if typecheck(other, type(self)):
@@ -543,32 +529,15 @@ cdef class flint_mpoly(flint_elem):
543529
return self._mul_scalar_(other)
544530

545531
def __rmul__(self, other):
546-
return self.__mul__(other)
547-
548-
def imul(self, other):
549-
"""
550-
In-place multiplication, mutates self.
551-
552-
>>> from flint import Ordering, fmpz_mpoly_ctx
553-
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
554-
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
555-
>>> f
556-
4*x0*x1 + 2*x0 + 3*x1
557-
>>> f.imul(2)
558-
>>> f
559-
8*x0*x1 + 4*x0 + 6*x1
560-
561-
"""
562532
if typecheck(other, type(self)):
563533
self.context().compatible_context_check(other.context())
564-
self._imul_mpoly_(other)
565-
return
534+
return self._mul_mpoly_(other)
566535

567-
other_scalar = self.context().any_as_scalar(other)
568-
if other_scalar is NotImplemented:
569-
raise NotImplementedError(f"cannot multiply {type(self)} and {type(other)}")
536+
other = self.context().any_as_scalar(other)
537+
if other is NotImplemented:
538+
return NotImplemented
570539

571-
self._imul_scalar_(other_scalar)
540+
return self._mul_scalar_(other)
572541

573542
def __pow__(self, other, modulus):
574543
if modulus is not None:
@@ -605,7 +574,7 @@ cdef class flint_mpoly(flint_elem):
605574

606575
other = self.context().scalar_as_mpoly(other)
607576
other._division_check(self)
608-
return other._divmod_mpoly_(self)
577+
return self._rdivmod_mpoly_(other)
609578

610579
def __truediv__(self, other):
611580
if typecheck(other, type(self)):
@@ -628,7 +597,7 @@ cdef class flint_mpoly(flint_elem):
628597

629598
other = self.context().scalar_as_mpoly(other)
630599
other._division_check(self)
631-
return other._truediv_mpoly_(self)
600+
return self._rtruediv_mpoly_(other)
632601

633602
def __floordiv__(self, other):
634603
if typecheck(other, type(self)):
@@ -651,7 +620,7 @@ cdef class flint_mpoly(flint_elem):
651620

652621
other = self.context().scalar_as_mpoly(other)
653622
other._division_check(self)
654-
return other._floordiv_mpoly_(self)
623+
return self._rfloordiv_mpoly_(other)
655624

656625
def __mod__(self, other):
657626
if typecheck(other, type(self)):
@@ -674,7 +643,82 @@ cdef class flint_mpoly(flint_elem):
674643

675644
other = self.context().scalar_as_mpoly(other)
676645
other._division_check(self)
677-
return other._mod_mpoly_(self)
646+
return self._rmod_mpoly_(other)
647+
648+
def iadd(self, other):
649+
"""
650+
In-place addition, mutates self.
651+
652+
>>> from flint import Ordering, fmpz_mpoly_ctx
653+
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
654+
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
655+
>>> f
656+
4*x0*x1 + 2*x0 + 3*x1
657+
>>> f.iadd(5)
658+
>>> f
659+
4*x0*x1 + 2*x0 + 3*x1 + 5
660+
661+
"""
662+
if typecheck(other, type(self)):
663+
self.context().compatible_context_check(other.context())
664+
self._iadd_mpoly_(other)
665+
return
666+
667+
other_scalar = self.context().any_as_scalar(other)
668+
if other_scalar is NotImplemented:
669+
raise NotImplementedError(f"cannot add {type(self)} and {type(other)}")
670+
671+
self._iadd_scalar_(other_scalar)
672+
673+
def isub(self, other):
674+
"""
675+
In-place subtraction, mutates self.
676+
677+
>>> from flint import Ordering, fmpz_mpoly_ctx
678+
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
679+
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
680+
>>> f
681+
4*x0*x1 + 2*x0 + 3*x1
682+
>>> f.isub(5)
683+
>>> f
684+
4*x0*x1 + 2*x0 + 3*x1 - 5
685+
686+
"""
687+
if typecheck(other, type(self)):
688+
self.context().compatible_context_check(other.context())
689+
self._isub_mpoly_(other)
690+
return
691+
692+
other_scalar = self.context().any_as_scalar(other)
693+
if other_scalar is NotImplemented:
694+
raise NotImplementedError(f"cannot subtract {type(self)} and {type(other)}")
695+
696+
self._isub_scalar_(other_scalar)
697+
698+
def imul(self, other):
699+
"""
700+
In-place multiplication, mutates self.
701+
702+
>>> from flint import Ordering, fmpz_mpoly_ctx
703+
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
704+
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
705+
>>> f
706+
4*x0*x1 + 2*x0 + 3*x1
707+
>>> f.imul(2)
708+
>>> f
709+
8*x0*x1 + 4*x0 + 6*x1
710+
711+
"""
712+
if typecheck(other, type(self)):
713+
self.context().compatible_context_check(other.context())
714+
self._imul_mpoly_(other)
715+
return
716+
717+
other_scalar = self.context().any_as_scalar(other)
718+
if other_scalar is NotImplemented:
719+
raise NotImplementedError(f"cannot multiply {type(self)} and {type(other)}")
720+
721+
self._imul_scalar_(other_scalar)
678722

679723
def __contains__(self, x):
680724
"""

0 commit comments

Comments
 (0)