# Ticket #14120: trac_14120-constant_coeff_laurent_poly-ts.patch

File trac_14120-constant_coeff_laurent_poly-ts.patch, 7.3 KB (added by Travis Scrimshaw, 10 years ago)
• ## sage/rings/polynomial/laurent_polynomial.pyx

# HG changeset patch
# User Travis Scrimshaw <tscrim@ucdavis.edu>
# Date 1360857734 28800
# Node ID df13db6ac022db2e97629395841435003f5e8d8e
# Parent 45bc9bdf399c925c404e486cc4857b9e3406fe1b
Trac #14120 - Added constant_coefficient() method to laruent_polynomial.

diff --git a/sage/rings/polynomial/laurent_polynomial.pyx b/sage/rings/polynomial/laurent_polynomial.pyx
 a cdef class LaurentPolynomial_mpair(Commu ans._mon = self._mon.emul(n) return ans def __getitem__(self, n): r""" Return the coefficient of x^n = x_1^{n_1} \cdots x_k^{n_k} where n is a tuple of length k and k is the number of variables. If the number of inputs is not equal to the number of variables, this raises a TypeError. EXAMPLES:: sage: P. = LaurentPolynomialRing(QQ) sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 + x*z; f -x^6 + x*z - 7*x^-2*y^3 + 5*x^-2*y + x^-3*y^2 sage: f[6,0,0] -1 sage: f[-2,3,0] -7 sage: f[-1,4,2] 0 sage: f[1,0,1] 1 sage: f[6] Traceback (most recent call last): ... TypeError: Must have exactly 3 inputs sage: f[6,0] Traceback (most recent call last): ... TypeError: Must have exactly 3 inputs sage: f[6,0,0,0] Traceback (most recent call last): ... TypeError: Must have exactly 3 inputs """ if isinstance(n, slice): raise TypeError("Multivariate Laurent polynomials are not iterable") if not isinstance(n, tuple) or len(n) != self.parent().ngens(): raise TypeError("Must have exactly %s inputs"%self.parent().ngens()) cdef ETuple t = ETuple(n) if self._prod is None: self._compute_polydict() if t not in self._prod.exponents(): return self.parent().base_ring().zero_element() return self._prod[t] def __iter__(self): """ Iterate through all terms by returning a list of the coefficient and the corresponding monomial. EXAMPLES:: sage: P. = LaurentPolynomialRing(QQ) sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 sage: list(f) # indirect doctest [(-1, x^6), (1, x^-3*y^2), (5, x^-2*y), (-7, x^-2*y^3)] """ if self._prod is None: self._compute_polydict() for c, exps in self._prod.list(): prod = self.parent().one_element() for i in range(len(exps)): prod *= self.parent().gens()[i]**exps[i] yield (c, prod) def monomials(self): """ Return the list of monomials in self. EXAMPLES:: sage: P. = LaurentPolynomialRing(QQ) sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 sage: f.monomials() [x^6, x^-3*y^2, x^-2*y, x^-2*y^3] """ L = [] if self._prod is None: self._compute_polydict() for c, exps in self._prod.list(): prod = self.parent().one_element() for i in range(len(exps)): prod *= self.parent().gens()[i]**exps[i] L.append(prod) return L def monomial_coefficient(self, mon): """ Return the coefficient in the base ring of the monomial mon in self, where mon must have the same parent as self. This function contrasts with the function :meth:coefficient() which returns the coefficient of a monomial viewing this polynomial in a polynomial ring over a base ring having fewer variables. INPUT: - mon - a monomial .. SEEALSO:: For coefficients in a base ring of fewer variables, see :meth:coefficient(). EXAMPLES:: sage: P. = LaurentPolynomialRing(QQ) sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 sage: f.monomial_coefficient(x^-2*y^3) -7 sage: f.monomial_coefficient(x^2) 0 """ if mon.parent() != self.parent(): raise TypeError("Input must have the same parent") if self._prod is None: self._compute_polydict() if (mon)._prod is None: mon._compute_polydict() return self.parent().base_ring()( self._prod.monomial_coefficient( (mon)._prod.dict()) ) def constant_coefficient(self): """ Return the constant coefficient of self. EXAMPLES:: sage: P. = LaurentPolynomialRing(QQ) sage: f = (y^2 - x^9 - 7*x*y^2 + 5*x*y)*x^-3; f -x^6 - 7*x^-2*y^2 + 5*x^-2*y + x^-3*y^2 sage: f.constant_coefficient() 0 sage: f = (x^3 + 2*x^-2*y+y^3)*y^-3; f x^3*y^-3 + 1 + 2*x^-2*y^-2 sage: f.constant_coefficient() 1 """ return self[(0,)*self.parent().ngens()] def coefficient(self, mon): """ Return the coefficient of mon in self, where mon must have the same parent as self.  The coefficient is defined as follows. If f is this polynomial, then the coefficient is the sum T/mon where the sum is over terms T in f that are exactly divisible by mon. r""" Return the coefficient of mon in self, where mon must have the same parent as self. A monomial m(x,y) 'exactly divides' f(x,y) if m(x,y)|f(x,y) and neither x*m(x,y) nor y*m(x,y) divides f(x,y). The coefficient is defined as follows. If f is this polynomial, then the coefficient c_m is sum: .. MATH:: c_m := \sum_T \frac{T}{m} where the sum is over terms T in f that are exactly divisible by m. A monomial m(x,y) 'exactly divides' f(x,y) if m(x,y) | f(x,y) and neither x \cdot m(x,y) nor y \cdot m(x,y) divides f(x,y). INPUT: cdef class LaurentPolynomial_mpair(Commu OUTPUT: element of the parent of self Element of the parent of self. .. NOTE:: To get the constant coefficient, call :meth:constant_coefficient(). EXAMPLES:: sage: P. = LaurentPolynomialRing(QQ) The coefficient returned is an element of the parent of self; in this case, P. The coefficient returned is an element of the parent of self; in this case, P. :: cdef class LaurentPolynomial_mpair(Commu Return the nonzero coefficients of this polynomial in a list. The returned list is decreasingly ordered by the term ordering of self.parent(). EXAMPLES:: sage: L. = LaurentPolynomialRing(QQ,order='degrevlex')