Skip to content

Commit

Permalink
Trac #11719: Add is_monomial() to power series and laurent series rin…
Browse files Browse the repository at this point in the history
…g elements

It should be trivial to check if a power/laurent series ring element is
a monomial or not.  It is not.

URL: http://trac.sagemath.org/11719
Reported by: boothby
Ticket author(s): Tom Boothby, Ralf Stephan
Reviewer(s): Peter Bruin
  • Loading branch information
Release Manager authored and vbraun committed May 12, 2014
2 parents b9dfe03 + c3851c0 commit de4ce33
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/sage/rings/laurent_series_ring_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@ cdef class LaurentSeries(AlgebraElement):
"""
return self.__u.is_zero()

def is_monomial(self):
"""
Return True if this element is a monomial. That is, if self is
`x^n` for some integer `n`.
EXAMPLES::
sage: k.<z> = LaurentSeriesRing(QQ, 'z')
sage: (30*z).is_monomial()
False
sage: k(1).is_monomial()
True
sage: (z+1).is_monomial()
False
sage: (z^-2909).is_monomial()
True
sage: (3*z^-2909).is_monomial()
False
"""

return self.__u.is_monomial()

def __nonzero__(self):
return not not self.__u

Expand Down
44 changes: 44 additions & 0 deletions src/sage/rings/polynomial/laurent_polynomial.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,28 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial_generic):
self.__u *= c
return self

def is_monomial(self):
"""
Return True if this element is a monomial. That is, if self is
`x^n` for some integer `n`.
EXAMPLES::
sage: k.<z> = LaurentPolynomialRing(QQ)
sage: z.is_monomial()
True
sage: k(1).is_monomial()
True
sage: (z+1).is_monomial()
False
sage: (z^-2909).is_monomial()
True
sage: (38*z^-2909).is_monomial()
False
"""

return self.__u.is_monomial()

def __pow__(_self, r, dummy):
"""
EXAMPLES::
Expand Down Expand Up @@ -1867,6 +1889,28 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial_generic):
ans._poly -= right._poly
return ans

def is_monomial(self):
"""
Return True if this element is a monomial.
EXAMPLES::
sage: k.<y,z> = LaurentPolynomialRing(QQ)
sage: z.is_monomial()
True
sage: k(1).is_monomial()
True
sage: (z+1).is_monomial()
False
sage: (z^-2909).is_monomial()
True
sage: (38*z^-2909).is_monomial()
False
"""

d = self._poly.dict()
return len(d) == 1 and 1 in d.values()

cpdef ModuleElement _neg_(self):
"""
Returns -self.
Expand Down
22 changes: 22 additions & 0 deletions src/sage/rings/power_series_ring_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,28 @@ cdef class PowerSeries(AlgebraElement):
def __rshift__(self, n):
return self.parent()(self.polynomial() >> n, self.prec())

def is_monomial(self):
"""
Return True if this element is a monomial. That is, if self is
`x^n` for some non-negative integer `n`.
EXAMPLES::
sage: k.<z> = PowerSeriesRing(QQ, 'z')
sage: z.is_monomial()
True
sage: k(1).is_monomial()
True
sage: (z+1).is_monomial()
False
sage: (z^2909).is_monomial()
True
sage: (3*z^2909).is_monomial()
False
"""

return self.polynomial().is_monomial()

def is_square(self):
"""
Returns True if this function has a square root in this ring, e.g.
Expand Down

0 comments on commit de4ce33

Please sign in to comment.