Skip to content

Commit c9de856

Browse files
committed
Prefer cdef to def, removes runtime reflection in operands
1 parent 7a3076c commit c9de856

File tree

6 files changed

+372
-214
lines changed

6 files changed

+372
-214
lines changed

src/flint/flint_base/flint_base.pxd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ cdef class flint_mpoly(flint_elem):
1717
cdef _add_scalar_(self, other)
1818
cdef _sub_scalar_(self, other)
1919
cdef _mul_scalar_(self, other)
20-
cdef _pow_(self, other)
2120

2221
cdef _add_mpoly_(self, other)
2322
cdef _sub_mpoly_(self, other)
@@ -28,7 +27,15 @@ cdef class flint_mpoly(flint_elem):
2827
cdef _truediv_mpoly_(self, other)
2928
cdef _mod_mpoly_(self, other)
3029

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)
3135
cdef _rtruediv_mpoly_(self, other)
36+
cdef _rmod_mpoly_(self, other)
37+
38+
cdef _pow_(self, other)
3239

3340
cdef _iadd_scalar_(self, other)
3441
cdef _isub_scalar_(self, other)

src/flint/flint_base/flint_base.pyx

Lines changed: 112 additions & 75 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.
@@ -414,9 +413,6 @@ cdef class flint_mpoly(flint_elem):
414413
cdef _mul_scalar_(self, other):
415414
return NotImplemented
416415

417-
cdef _pow_(self, other):
418-
return NotImplemented
419-
420416
cdef _add_mpoly_(self, other):
421417
return NotImplemented
422418

@@ -435,10 +431,28 @@ cdef class flint_mpoly(flint_elem):
435431
cdef _truediv_mpoly_(self, other):
436432
return NotImplemented
437433

434+
cdef _mod_mpoly_(self, other):
435+
return NotImplemented
436+
437+
cdef _rsub_scalar_(self, other):
438+
return NotImplemented
439+
440+
cdef _rsub_mpoly_(self, other):
441+
return NotImplemented
442+
443+
cdef _rdivmod_mpoly_(self, other):
444+
return NotImplemented
445+
446+
cdef _rfloordiv_mpoly_(self, other):
447+
return NotImplemented
448+
438449
cdef _rtruediv_mpoly_(self, other):
439450
return NotImplemented
440451

441-
cdef _mod_mpoly_(self, other):
452+
cdef _rmod_mpoly_(self, other):
453+
return NotImplemented
454+
455+
cdef _pow_(self, other):
442456
return NotImplemented
443457

444458
cdef _iadd_scalar_(self, other):
@@ -471,32 +485,15 @@ cdef class flint_mpoly(flint_elem):
471485
return self._add_scalar_(other)
472486

473487
def __radd__(self, other):
474-
return self.__add__(other)
475-
476-
def iadd(self, other):
477-
"""
478-
In-place addition, mutates self.
479-
480-
>>> from flint import Ordering, fmpz_mpoly_ctx
481-
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
482-
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
483-
>>> f
484-
4*x0*x1 + 2*x0 + 3*x1
485-
>>> f.iadd(5)
486-
>>> f
487-
4*x0*x1 + 2*x0 + 3*x1 + 5
488-
489-
"""
490488
if typecheck(other, type(self)):
491489
self.context().compatible_context_check(other.context())
492-
self._iadd_mpoly_(other)
493-
return
490+
return self._add_mpoly_(other)
494491

495-
other_scalar = self.context().any_as_scalar(other)
496-
if other_scalar is NotImplemented:
497-
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
498495

499-
self._iadd_scalar_(other_scalar)
496+
return self._add_scalar_(other)
500497

501498
def __sub__(self, other):
502499
if typecheck(other, type(self)):
@@ -510,32 +507,15 @@ cdef class flint_mpoly(flint_elem):
510507
return self._sub_scalar_(other)
511508

512509
def __rsub__(self, other):
513-
return -self.__sub__(other)
514-
515-
def isub(self, other):
516-
"""
517-
In-place subtraction, mutates self.
518-
519-
>>> from flint import Ordering, fmpz_mpoly_ctx
520-
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
521-
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
522-
>>> f
523-
4*x0*x1 + 2*x0 + 3*x1
524-
>>> f.isub(5)
525-
>>> f
526-
4*x0*x1 + 2*x0 + 3*x1 - 5
527-
528-
"""
529510
if typecheck(other, type(self)):
530511
self.context().compatible_context_check(other.context())
531-
self._isub_mpoly_(other)
532-
return
512+
return self._rsub_mpoly_(other)
533513

534-
other_scalar = self.context().any_as_scalar(other)
535-
if other_scalar is NotImplemented:
536-
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
537517

538-
self._isub_scalar_(other_scalar)
518+
return self._rsub_scalar_(other)
539519

540520
def __mul__(self, other):
541521
if typecheck(other, type(self)):
@@ -549,32 +529,15 @@ cdef class flint_mpoly(flint_elem):
549529
return self._mul_scalar_(other)
550530

551531
def __rmul__(self, other):
552-
return self.__mul__(other)
553-
554-
def imul(self, other):
555-
"""
556-
In-place multiplication, mutates self.
557-
558-
>>> from flint import Ordering, fmpz_mpoly_ctx
559-
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
560-
>>> f = ctx.from_dict({(1, 0): 2, (0, 1): 3, (1, 1): 4})
561-
>>> f
562-
4*x0*x1 + 2*x0 + 3*x1
563-
>>> f.imul(2)
564-
>>> f
565-
8*x0*x1 + 4*x0 + 6*x1
566-
567-
"""
568532
if typecheck(other, type(self)):
569533
self.context().compatible_context_check(other.context())
570-
self._imul_mpoly_(other)
571-
return
534+
return self._mul_mpoly_(other)
572535

573-
other_scalar = self.context().any_as_scalar(other)
574-
if other_scalar is NotImplemented:
575-
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
576539

577-
self._imul_scalar_(other_scalar)
540+
return self._mul_scalar_(other)
578541

579542
def __pow__(self, other, modulus):
580543
if modulus is not None:
@@ -611,7 +574,7 @@ cdef class flint_mpoly(flint_elem):
611574

612575
other = self.context().scalar_as_mpoly(other)
613576
other._division_check(self)
614-
return other._divmod_mpoly_(self)
577+
return self._rdivmod_mpoly_(other)
615578

616579
def __truediv__(self, other):
617580
if typecheck(other, type(self)):
@@ -634,7 +597,6 @@ cdef class flint_mpoly(flint_elem):
634597

635598
other = self.context().scalar_as_mpoly(other)
636599
other._division_check(self)
637-
# return other._truediv_mpoly_(self)
638600
return self._rtruediv_mpoly_(other)
639601

640602
def __floordiv__(self, other):
@@ -658,7 +620,7 @@ cdef class flint_mpoly(flint_elem):
658620

659621
other = self.context().scalar_as_mpoly(other)
660622
other._division_check(self)
661-
return other._floordiv_mpoly_(self)
623+
return self._rfloordiv_mpoly_(other)
662624

663625
def __mod__(self, other):
664626
if typecheck(other, type(self)):
@@ -681,7 +643,82 @@ cdef class flint_mpoly(flint_elem):
681643

682644
other = self.context().scalar_as_mpoly(other)
683645
other._division_check(self)
684-
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)
685722

686723
def __contains__(self, x):
687724
"""

0 commit comments

Comments
 (0)