From d9f2796d794e1e7c55e8fdb7cd352adf5f5ab9f5 Mon Sep 17 00:00:00 2001 From: Daniel Krenn Date: Mon, 15 Feb 2016 10:19:06 +0100 Subject: [PATCH] Trac #19540: _factorial_ for terms --- src/sage/rings/asymptotic/term_monoid.py | 86 ++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/sage/rings/asymptotic/term_monoid.py b/src/sage/rings/asymptotic/term_monoid.py index 529108c214d..dfb5f5ed949 100644 --- a/src/sage/rings/asymptotic/term_monoid.py +++ b/src/sage/rings/asymptotic/term_monoid.py @@ -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""" @@ -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. @@ -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