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 b cdef class LaurentPolynomial_mpair(Commu 
    215215            ans._mon = self._mon.emul(n)
    216216        return ans
    217217
     218    def __getitem__(self, n):
     219        r"""
     220        Return the coefficient of `x^n = x_1^{n_1} \cdots x_k^{n_k}` where
     221        `n` is a tuple of length `k` and `k` is the number of variables.
     222
     223        If the number of inputs is not equal to the number of variables, this
     224        raises a ``TypeError``.
     225
     226        EXAMPLES::
     227
     228            sage: P.<x,y,z> = LaurentPolynomialRing(QQ)
     229            sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 + x*z; f
     230            -x^6 + x*z - 7*x^-2*y^3 + 5*x^-2*y + x^-3*y^2
     231            sage: f[6,0,0]
     232            -1
     233            sage: f[-2,3,0]
     234            -7
     235            sage: f[-1,4,2]
     236            0
     237            sage: f[1,0,1]
     238            1
     239            sage: f[6]
     240            Traceback (most recent call last):
     241            ...
     242            TypeError: Must have exactly 3 inputs
     243            sage: f[6,0]
     244            Traceback (most recent call last):
     245            ...
     246            TypeError: Must have exactly 3 inputs
     247            sage: f[6,0,0,0]
     248            Traceback (most recent call last):
     249            ...
     250            TypeError: Must have exactly 3 inputs
     251        """
     252        if isinstance(n, slice):
     253            raise TypeError("Multivariate Laurent polynomials are not iterable")
     254        if not isinstance(n, tuple) or len(n) != self.parent().ngens():
     255            raise TypeError("Must have exactly %s inputs"%self.parent().ngens())
     256        cdef ETuple t = ETuple(n)
     257        if self._prod is None:
     258            self._compute_polydict()
     259        if t not in self._prod.exponents():
     260            return self.parent().base_ring().zero_element()
     261        return self._prod[t]
     262
     263    def __iter__(self):
     264        """
     265        Iterate through all terms by returning a list of the coefficient and
     266        the corresponding monomial.
     267
     268        EXAMPLES::
     269
     270            sage: P.<x,y> = LaurentPolynomialRing(QQ)
     271            sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3
     272            sage: list(f) # indirect doctest
     273            [(-1, x^6), (1, x^-3*y^2), (5, x^-2*y), (-7, x^-2*y^3)]
     274        """
     275        if self._prod is None:
     276            self._compute_polydict()
     277        for c, exps in self._prod.list():
     278            prod = self.parent().one_element()
     279            for i in range(len(exps)):
     280                prod *= self.parent().gens()[i]**exps[i]
     281            yield (c, prod)
     282
     283    def monomials(self):
     284        """
     285        Return the list of monomials in ``self``.
     286
     287        EXAMPLES::
     288
     289            sage: P.<x,y> = LaurentPolynomialRing(QQ)
     290            sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3
     291            sage: f.monomials()
     292            [x^6, x^-3*y^2, x^-2*y, x^-2*y^3]
     293        """
     294        L = []
     295        if self._prod is None:
     296            self._compute_polydict()
     297        for c, exps in self._prod.list():
     298            prod = self.parent().one_element()
     299            for i in range(len(exps)):
     300                prod *= self.parent().gens()[i]**exps[i]
     301            L.append(prod)
     302        return L
     303
     304    def monomial_coefficient(self, mon):
     305        """
     306        Return the coefficient in the base ring of the monomial ``mon`` in
     307        ``self``, where ``mon`` must have the same parent as ``self``.
     308
     309        This function contrasts with the function :meth:`coefficient()`
     310        which returns the coefficient of a monomial viewing this
     311        polynomial in a polynomial ring over a base ring having fewer
     312        variables.
     313
     314        INPUT:
     315
     316        - ``mon`` - a monomial
     317
     318        .. SEEALSO::
     319
     320            For coefficients in a base ring of fewer variables, see
     321            :meth:`coefficient()`.
     322
     323        EXAMPLES::
     324
     325            sage: P.<x,y> = LaurentPolynomialRing(QQ)
     326            sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3
     327            sage: f.monomial_coefficient(x^-2*y^3)
     328            -7
     329            sage: f.monomial_coefficient(x^2)
     330            0
     331        """
     332        if mon.parent() != self.parent():
     333            raise TypeError("Input must have the same parent")
     334        if self._prod is None:
     335            self._compute_polydict()
     336        if (<LaurentPolynomial_mpair>mon)._prod is None:
     337            mon._compute_polydict()
     338        return self.parent().base_ring()( self._prod.monomial_coefficient(
     339                        (<LaurentPolynomial_mpair>mon)._prod.dict()) )
     340
     341    def constant_coefficient(self):
     342        """
     343        Return the constant coefficient of ``self``.
     344
     345        EXAMPLES::
     346
     347            sage: P.<x,y> = LaurentPolynomialRing(QQ)
     348            sage: f = (y^2 - x^9 - 7*x*y^2 + 5*x*y)*x^-3; f
     349            -x^6 - 7*x^-2*y^2 + 5*x^-2*y + x^-3*y^2
     350            sage: f.constant_coefficient()
     351            0
     352            sage: f = (x^3 + 2*x^-2*y+y^3)*y^-3; f
     353            x^3*y^-3 + 1 + 2*x^-2*y^-2
     354            sage: f.constant_coefficient()
     355            1
     356        """
     357        return self[(0,)*self.parent().ngens()]
     358
    218359    def coefficient(self, mon):
    219         """
    220         Return the coefficient of mon in self, where mon must have the
    221         same parent as self.  The coefficient is defined as follows.
    222         If f is this polynomial, then the coefficient is the sum T/mon
    223         where the sum is over terms T in f that are exactly divisible
    224         by mon.
     360        r"""
     361        Return the coefficient of ``mon`` in ``self``, where ``mon`` must
     362        have the same parent as ``self``.
    225363
    226         A monomial m(x,y) 'exactly divides' f(x,y) if m(x,y)|f(x,y)
    227         and neither x*m(x,y) nor y*m(x,y) divides f(x,y).
     364        The coefficient is defined as follows. If `f` is this polynomial, then
     365        the coefficient `c_m` is sum:
     366
     367        .. MATH::
     368
     369            c_m := \sum_T \frac{T}{m}
     370
     371        where the sum is over terms `T` in `f` that are exactly divisible
     372        by `m`.
     373
     374        A monomial `m(x,y)` 'exactly divides' `f(x,y)` if `m(x,y) | f(x,y)`
     375        and neither `x \cdot m(x,y)` nor `y \cdot m(x,y)` divides `f(x,y)`.
    228376
    229377        INPUT:
    230378       
    cdef class LaurentPolynomial_mpair(Commu 
    232380
    233381        OUTPUT:
    234382
    235         element of the parent of self
    236        
     383        Element of the parent of ``self``.
     384
     385        .. NOTE::
     386
     387            To get the constant coefficient, call
     388            :meth:`constant_coefficient()`.
     389
    237390        EXAMPLES::
    238391
    239392            sage: P.<x,y> = LaurentPolynomialRing(QQ)
    240393
    241         The coefficient returned is an element of the parent of self; in
    242         this case, P.
     394        The coefficient returned is an element of the parent of ``self``; in
     395        this case, ``P``.
    243396
    244397        ::
    245398       
    cdef class LaurentPolynomial_mpair(Commu 
    278431        Return the nonzero coefficients of this polynomial in a list.
    279432        The returned list is decreasingly ordered by the term ordering
    280433        of ``self.parent()``.
    281        
     434
    282435        EXAMPLES::
    283436
    284437            sage: L.<x,y,z> = LaurentPolynomialRing(QQ,order='degrevlex')