Skip to content

Commit 9abc8d2

Browse files
author
Jake Moss
committed
Generic add and divmod POC methods
1 parent f2fa712 commit 9abc8d2

File tree

2 files changed

+251
-141
lines changed

2 files changed

+251
-141
lines changed

src/flint/flint_base/flint_base.pyx

+60
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from flint.utils.typecheck cimport typecheck
1111
cimport libc.stdlib
1212

1313
from typing import Optional
14+
from flint.utils.flint_exceptions import IncompatibleContextError
1415

1516

1617
FLINT_BITS = _FLINT_BITS
@@ -369,6 +370,18 @@ cdef class flint_mpoly_context(flint_elem):
369370
nametup=ctx.names()
370371
)
371372

373+
def any_as_scalar(self, other):
374+
raise NotImplementedError("abstract method")
375+
376+
def scalar_as_mpoly(self, other):
377+
raise NotImplementedError("abstract method")
378+
379+
def compatible_context_check(self, other):
380+
if not typecheck(other, type(self)):
381+
raise TypeError(f"type {type(other)} is not {type(self)}")
382+
elif other is not self:
383+
raise IncompatibleContextError(f"{other} is not {self}")
384+
372385

373386
cdef class flint_mpoly(flint_elem):
374387
"""
@@ -381,6 +394,53 @@ cdef class flint_mpoly(flint_elem):
381394
def to_dict(self):
382395
return {self.monomial(i): self.coefficient(i) for i in range(len(self))}
383396

397+
def _division_check(self, other):
398+
if not other:
399+
raise ZeroDivisionError("nmod_mpoly division by zero")
400+
401+
def _add_scalar_(self, other):
402+
return NotImplemented
403+
404+
def _add_mpoly_(self, other):
405+
return NotImplemented
406+
407+
def __add__(self, other):
408+
if typecheck(other, type(self)):
409+
self.context().compatible_context_check(other.context())
410+
return self._add_mpoly_(other)
411+
412+
other = self.context().any_as_scalar(other)
413+
if other is NotImplemented:
414+
return NotImplemented
415+
416+
return self._add_scalar_(other)
417+
418+
def __radd__(self, other):
419+
return self.__add__(other)
420+
421+
def __divmod__(self, other):
422+
if typecheck(other, type(self)):
423+
self._division_check(other)
424+
self.context().compatible_context_check(other.context())
425+
return self._divmod_mpoly_(other)
426+
427+
other = self.context().any_as_scalar(other)
428+
if other is NotImplemented:
429+
return NotImplemented
430+
431+
self._division_check(other)
432+
other = self.context().scalar_as_mpoly(other)
433+
return self._divmod_mpoly_(other)
434+
435+
def __rdivmod__(self, other):
436+
other = self.context().any_as_scalar(other)
437+
if other is NotImplemented:
438+
return NotImplemented
439+
440+
self._division_check(self)
441+
other = self.context().scalar_as_mpoly(other)
442+
return other._divmod_mpoly_(self)
443+
384444
def __contains__(self, x):
385445
"""
386446
Returns True if `self` contains a term with exponent vector `x` and a non-zero coefficient.

0 commit comments

Comments
 (0)