Skip to content

Commit

Permalink
provide monomial_coefficients for all kinds of polynomials
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Oct 1, 2024
1 parent 1a94d8f commit 4517525
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/sage/rings/polynomial/laurent_polynomial.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -850,10 +850,15 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial):
y^3*t^-9 + 3*x^3*y^2*t^-6 + 3*x^6*y*t^-3 + x^9 + t^2
sage: f.dict()
{-9: y^3, -6: 3*x^3*y^2, -3: 3*x^6*y, 0: x^9, 2: 1}
sage: f.monomial_coefficients()
{-9: y^3, -6: 3*x^3*y^2, -3: 3*x^6*y, 0: x^9, 2: 1}
"""
cdef dict d = self.__u.dict()
return {k+self.__n: d[k] for k in d}

monomial_coefficients = dict

def coefficients(self):
"""
Return the nonzero coefficients of ``self``.
Expand Down
5 changes: 5 additions & 0 deletions src/sage/rings/polynomial/laurent_polynomial_mpair.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,16 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial):
sage: f = 4*x^7*z^-1 + 3*x^3*y + 2*x^4*z^-2 + x^6*y^-7
sage: sorted(f.dict().items())
[((3, 1, 0), 3), ((4, 0, -2), 2), ((6, -7, 0), 1), ((7, 0, -1), 4)]
sage: sorted(f.monomial_coefficients().items())
[((3, 1, 0), 3), ((4, 0, -2), 2), ((6, -7, 0), 1), ((7, 0, -1), 4)]
"""
if self._prod is None:
self._compute_polydict()
return < dict > self._prod.dict()

monomial_coefficients = dict

def _fraction_pair(self):
"""
Return one representation of ``self`` as a pair
Expand Down
11 changes: 11 additions & 0 deletions src/sage/rings/polynomial/multi_polynomial_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,17 @@ def dict(self):
"""
Return underlying dictionary with keys the exponents and values
the coefficients of this polynomial.
EXAMPLES::
sage: # needs sage.rings.number_field
sage: R.<x,y,z> = PolynomialRing(QQbar, order='lex')
sage: f = (x^1*y^5*z^2 + x^2*z + x^4*y^1*z^3)
sage: f.dict()
{(1, 5, 2): 1, (2, 0, 1): 1, (4, 1, 3): 1}
sage: f.monomial_coefficients() # needs sage.rings.number_field
{(1, 5, 2): 1, (2, 0, 1): 1, (4, 1, 3): 1}
"""
return self.element().dict()

Expand Down
7 changes: 6 additions & 1 deletion src/sage/rings/polynomial/multi_polynomial_libsingular.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2992,9 +2992,12 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
EXAMPLES::
sage: R.<x,y,z> = QQ[]
sage: f=2*x*y^3*z^2 + 1/7*x^2 + 2/3
sage: f = 2*x*y^3*z^2 + 1/7*x^2 + 2/3
sage: f.dict()
{(0, 0, 0): 2/3, (1, 3, 2): 2, (2, 0, 0): 1/7}
sage: f.monomial_coefficients()
{(0, 0, 0): 2/3, (1, 3, 2): 2, (2, 0, 0): 1/7}
"""
cdef poly *p
cdef ring *r = self._parent_ring
Expand All @@ -3016,6 +3019,8 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
p = pNext(p)
return pd

monomial_coefficients = dict

def iterator_exp_coeff(self, as_ETuples=True):
"""
Iterate over ``self`` as pairs of ((E)Tuple, coefficient).
Expand Down
5 changes: 5 additions & 0 deletions src/sage/rings/polynomial/ore_polynomial_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2493,6 +2493,9 @@ cdef class OrePolynomial_generic_dense(OrePolynomial):
sage: a = x^2012 + t*x^1006 + t^3 + 2*t
sage: a.dict()
{0: t^3 + 2*t, 1006: t, 2012: 1}
sage: a.monomial_coefficients()
{0: t^3 + 2*t, 1006: t, 2012: 1}
"""
cdef dict X = {}
cdef list Y = (<OrePolynomial_generic_dense>self)._coeffs
Expand All @@ -2503,6 +2506,8 @@ cdef class OrePolynomial_generic_dense(OrePolynomial):
X[i] = c
return X

monomial_coefficients = dict

cpdef Integer degree(self):
r"""
Return the degree of ``self``.
Expand Down
3 changes: 3 additions & 0 deletions src/sage/rings/polynomial/plural.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,9 @@ cdef class NCPolynomial_plural(RingElement):
sage: f = (2*x*y^3*z^2 + (7)*x^2 + (3))
sage: f.dict()
{(0, 0, 0): 3, (1, 2, 3): 2, (2, 0, 0): 7}
sage: f.monomial_coefficients()
{(0, 0, 0): 3, (1, 2, 3): 2, (2, 0, 0): 7}
"""
cdef poly *p
cdef ring *r
Expand Down
3 changes: 3 additions & 0 deletions src/sage/rings/polynomial/polynomial_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4475,6 +4475,9 @@ cdef class Polynomial(CommutativePolynomial):
sage: f = x^3 + -1/7*x + 13
sage: f.dict()
{0: 13, 1: -1/7, 3: 1}
sage: f.monomial_coefficients()
{0: 13, 1: -1/7, 3: 1}
"""
cdef dict X = {}
cdef list Y = self.list(copy=False)
Expand Down
5 changes: 5 additions & 0 deletions src/sage/rings/polynomial/polynomial_element_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,14 @@ def dict(self):
sage: d[0] = 10
sage: f.dict()
{0: 5, 1997: 1, 10000: 7}
sage: f.monomial_coefficients()
{0: 5, 1997: 1, 10000: 7}
"""
return dict(self.__coeffs)

monomial_coefficients = dict

def coefficients(self, sparse=True):
"""
Return the coefficients of the monomials appearing in ``self``.
Expand Down

0 comments on commit 4517525

Please sign in to comment.