Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #19540: _factorial_ for terms
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrenn committed Feb 15, 2016
1 parent 5a4adf0 commit d9f2796
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/sage/rings/asymptotic/term_monoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,30 @@ def variable_names(self):
return self.growth.variable_names()


def _factorial_(self):
r"""
Return the factorial of this generic term.
OUTPUT:
A term.
TESTS::
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: T = GenericTermMonoid(GrowthGroup('x^QQ'), QQ)
sage: T.an_element()._factorial_()
Traceback (most recent call last):
...
NotImplementedError: Cannot build the factorial of
Generic Term with growth x^(1/2).
"""
raise NotImplementedError(
'Cannot build the factorial of {}.'.format(self))



class GenericTermMonoid(sage.structure.unique_representation.UniqueRepresentation,
sage.structure.parent.Parent):
r"""
Expand Down Expand Up @@ -2379,6 +2403,35 @@ def _substitute_(self, rules):
substitute_raise_exception(self, e)


def _factorial_(self):
r"""
Return the factorial of this O-term if it is constant
(i.e., has growth `1`).
OUTPUT:
A term.
TESTS::
sage: from sage.rings.asymptotic.term_monoid import TermMonoid
sage: T = TermMonoid('O', 'z^QQ', QQ)
sage: T(1)._factorial_()
O(1)
sage: T('z^(3/2)')._factorial_()
Traceback (most recent call last):
...
ValueError: Cannot build the factorial of O(z^(3/2))
since it has growth != 1.
"""
if not self.growth.is_one():
raise ValueError(
'Cannot build the factorial of {} since it has growth '
'!= 1.'.format(self))

return self


class OTermMonoid(GenericTermMonoid):
r"""
Parent for asymptotic big `O`-terms.
Expand Down Expand Up @@ -3538,6 +3591,39 @@ def _substitute_(self, rules):
substitute_raise_exception(self, e)


def _factorial_(self):
r"""
Return the factorial of this exact term if it is constant
(i.e., has growth `1`).
OUTPUT:
A term.
TESTS::
sage: from sage.rings.asymptotic.term_monoid import TermMonoid
sage: T = TermMonoid('exact', 'z^QQ', QQ)
sage: T('4')._factorial_()
24
sage: T('1/2')._factorial_()
1/2*sqrt(pi)
sage: T('4*z^(3/2)')._factorial_()
Traceback (most recent call last):
...
ValueError: Cannot build the factorial of 4*z^(3/2)
since it has growth != 1.
"""
if not self.growth.is_one():
raise ValueError(
'Cannot build the factorial of {} since it has growth '
'!= 1.'.format(self))

from sage.functions.other import factorial
return self.parent()._create_element_in_extension_(
self.growth, factorial(self.coefficient))


class ExactTermMonoid(TermWithCoefficientMonoid):
r"""
Parent for asymptotic exact terms, implemented in
Expand Down

0 comments on commit d9f2796

Please sign in to comment.