Skip to content

Commit

Permalink
34000: adapt polynomials to the changes in PolyDict
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed Feb 22, 2023
1 parent 43bb1fc commit f6864e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/sage/rings/polynomial/laurent_polynomial.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ Elements of Laurent polynomial rings

from sage.rings.integer cimport Integer
from sage.categories.map cimport Map
from sage.structure.element import is_Element, coerce_binop
from sage.structure.element import is_Element, coerce_binop, parent
from sage.structure.factorization import Factorization
from sage.misc.derivative import multi_derivative
from sage.rings.polynomial.polydict cimport monomial_exponent
from sage.rings.polynomial.polynomial_element import Polynomial
from sage.rings.polynomial.polynomial_ring import is_PolynomialRing
from sage.structure.richcmp cimport richcmp, rich_to_bool
Expand Down Expand Up @@ -2519,19 +2520,18 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial):
sage: f.monomial_coefficient(x + y)
Traceback (most recent call last):
...
ValueError: input must be a monomial
ValueError: not a monomial
"""
if mon.parent() != self._parent:
if parent(mon) != self._parent:
raise TypeError("input must have the same parent")
cdef LaurentPolynomial_mpair m = <LaurentPolynomial_mpair> mon
if m._prod is None:
m._compute_polydict()
if len(m._prod) != 1:
raise ValueError("input must be a monomial")
if self._prod is None:
self._compute_polydict()
c = self._prod.monomial_coefficient(m._prod.dict())
return self._parent.base_ring()(c)
exp = monomial_exponent(m._prod)
zero = self._parent.base_ring().zero()
return self._prod.get(exp, zero)

def constant_coefficient(self):
"""
Expand Down
28 changes: 20 additions & 8 deletions src/sage/rings/polynomial/multi_polynomial_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

import operator

from sage.structure.element import CommutativeRingElement, coerce_binop, get_coercion_model
from sage.structure.element import CommutativeRingElement, coerce_binop, get_coercion_model, parent
from sage.misc.misc_c import prod
import sage.rings.integer
import sage.rings.integer_ring
Expand Down Expand Up @@ -260,6 +260,8 @@ def number_of_terms(self):

def __neg__(self):
"""
Return the negative of ``self``.
EXAMPLES::
sage: R.<x,y> = QQbar[]
Expand All @@ -272,6 +274,8 @@ def __neg__(self):

def _add_(self, right):
"""
Return the sum of ``self`` and ``right``.
EXAMPLES::
sage: R.<x,y> = QQbar[]
Expand All @@ -284,6 +288,8 @@ def _add_(self, right):

def _sub_(self, right):
"""
Return the difference between ``self`` and ``right``.
EXAMPLES::
sage: R.<x,y> = QQbar[]
Expand All @@ -296,6 +302,8 @@ def _sub_(self, right):

def _mul_(self, right):
"""
Return the product between ``self`` and ``right``.
EXAMPLES::
sage: R.<x,y> = QQbar[]
Expand Down Expand Up @@ -440,7 +448,8 @@ def __init__(self, parent, x):
True
"""
if not isinstance(x, polydict.PolyDict):
x = polydict.PolyDict(x, remove_zero=True)
x = polydict.PolyDict(x)
x.remove_zeros()
MPolynomial_element.__init__(self, parent, x)

def _new_constant_poly(self, x, P):
Expand Down Expand Up @@ -783,10 +792,11 @@ def monomial_coefficient(self, mon):
sage: f.monomial_coefficient(x)
-a
"""
if not (isinstance(mon, MPolynomial) and mon.parent() is self.parent() and mon.is_monomial()):
raise TypeError("mon must be a monomial in the parent of self.")
R = self.parent().base_ring()
return R(self.element().monomial_coefficient(mon.element().dict()))
if parent(mon) is not self.parent():
raise TypeError("mon must be a monomial in the parent of self")
exp = polydict.monomial_exponent(mon.element())
zero = self.parent().base_ring().zero()
return self.element().get(exp, zero)

def dict(self):
"""
Expand Down Expand Up @@ -1871,7 +1881,8 @@ def _derivative(self, var=None):
# var is not a generator; do term-by-term differentiation recursively
# var may be, for example, a generator of the base ring
d = dict([(e, x._derivative(var)) for (e, x) in self.dict().items()])
d = polydict.PolyDict(d, remove_zero=True)
d = polydict.PolyDict(d)
d.remove_zeros()
return MPolynomial_polydict(P, d)

# differentiate w.r.t. indicated variable
Expand Down Expand Up @@ -1965,7 +1976,8 @@ def integral(self, var=None):
# var may be, for example, a generator of the base ring
d = {e: x.integral(var)
for e, x in self.dict().items()}
d = polydict.PolyDict(d, remove_zero=True)
d = polydict.PolyDict(d)
d.remove_zeros()
else:
# integrate w.r.t. indicated variable
d = self.element().integral_i(index)
Expand Down

0 comments on commit f6864e2

Please sign in to comment.