summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRelease Manager <release@sagemath.org>2016-02-16 18:03:23 +0100
committerVolker Braun <vbraun.name@gmail.com>2016-02-16 18:03:23 +0100
commit28db6f605cf167d3c8a59fdef21e1ad59307348d (patch)
tree5cb940f9f18515f66393e25a158a21f8a35751f4
parentTrac #20057: Add iterator to DisjointSet class (diff)
parentTrac #10519: trivial ReST error (diff)
Trac #10519: analytic combinatorics: new code for computing asymptotics for multivariate generating functions
This code is a collection of functions designed to compute asymptotics of Maclaurin coefficients of certain classes of multivariate generating functions. The main function asymptotics() returns the first `N` terms of the asymptotic expansion of the Maclaurin coefficients `F_{n\alpha}` of the multivariate meromorphic function `F=G/H` as `n\to\infty`. It assumes that `F` is holomorphic in a neighborhood of the origin, that `H` is a polynomial, and that asymptotics in the direction of `\alpha` (a tuple of positive integers) are controlled by smooth or multiple points. URL: http://trac.sagemath.org/10519 Reported by: araichev Ticket author(s): Daniel Krenn, Alex Raichev Reviewer(s): Daniel Krenn, David Loeffler, Travis Scrimshaw
-rw-r--r--src/doc/en/reference/asymptotic/index.rst1
-rw-r--r--src/sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py4388
2 files changed, 4389 insertions, 0 deletions
diff --git a/src/doc/en/reference/asymptotic/index.rst b/src/doc/en/reference/asymptotic/index.rst
index 86ef7bb..1dcf205 100644
--- a/src/doc/en/reference/asymptotic/index.rst
+++ b/src/doc/en/reference/asymptotic/index.rst
@@ -64,5 +64,6 @@ Asymptotic Expansions --- Table of Contents
sage/rings/asymptotic/growth_group_cartesian
sage/rings/asymptotic/term_monoid
sage/rings/asymptotic/misc
+ sage/rings/asymptotic/asymptotics_multivariate_generating_functions
.. include:: ../footer.txt
diff --git a/src/sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py b/src/sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py
new file mode 100644
index 0000000..1ab30d0
--- /dev/null
+++ b/src/sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py
@@ -0,0 +1,4388 @@
+r"""
+Asymptotics of Multivariate Generating Series
+
+Let `F(x) = \sum_{\nu \in \NN^d} F_{\nu} x^\nu` be a multivariate power series
+with complex coefficients that converges in a neighborhood of the origin.
+Assume that `F = G/H` for some functions `G` and `H` holomorphic in a
+neighborhood of the origin. Assume also that `H` is a polynomial.
+
+This computes asymptotics for the coefficients `F_{r \alpha}` as `r \to \infty`
+with `r \alpha \in \NN^d` for `\alpha` in a permissible subset of `d`-tuples of
+positive reals. More specifically, it computes arbitrary terms of the
+asymptotic expansion for `F_{r \alpha}` when the asymptotics are controlled by
+a strictly minimal multiple point of the alegbraic variety `H = 0`.
+
+The algorithms and formulas implemented here come from [RaWi2008a]_
+and [RaWi2012]_. For a general reference take a look in the book [PeWi2013].
+
+.. [AiYu1983] I.A. Aizenberg and A.P. Yuzhakov.
+ *Integral representations and residues in multidimensional complex analysis*.
+ Translations of Mathematical Monographs, **58**. American Mathematical
+ Society, Providence, RI. (1983). x+283 pp. ISBN: 0-8218-4511-X.
+
+.. [Raic2012] Alexander Raichev.
+ *Leinartas's partial fraction decomposition*.
+ :arxiv:`1206.4740`.
+
+.. [RaWi2008a] Alexander Raichev and Mark C. Wilson. *Asymptotics of
+ coefficients of multivariate generating functions: improvements for
+ smooth points*, Electronic Journal of Combinatorics, Vol. 15 (2008).
+ R89 :arxiv:`0803.2914`.
+
+.. [RaWi2012] Alexander Raichev and Mark C. Wilson. *Asymptotics of
+ coefficients of multivariate generating functions: improvements for
+ smooth points*. Online Journal of Analytic Combinatorics.
+ Issue 6, (2011). :arxiv:`1009.5715`.
+
+.. [PeWi2013] Robin Pemantle and Mark C. Wilson.
+ *Analytic Combinatorics in Several Variables*.
+ Cambridge University Press, 2013.
+
+
+Introductory Examples
+=====================
+
+::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+
+A univariate smooth point example::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (x - 1/2)^3
+ sage: Hfac = H.factor()
+ sage: G = -1/(x + 3)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F
+ (-1/(x + 3), [(x - 1/2, 3)])
+ sage: alpha = [1]
+ sage: decomp = F.asymptotic_decomposition(alpha)
+ sage: decomp
+ (0, []) +
+ (-1/2*(x^2 + 6*x + 9)*r^2/(x^5 + 9*x^4 + 27*x^3 + 27*x^2)
+ - 1/2*(5*x^2 + 24*x + 27)*r/(x^5 + 9*x^4 + 27*x^3 + 27*x^2)
+ - 3*(x^2 + 3*x + 3)/(x^5 + 9*x^4 + 27*x^3 + 27*x^2),
+ [(x - 1/2, 1)])
+ sage: F1 = decomp[1]
+ sage: p = {x: 1/2}
+ sage: asy = F1.asymptotics(p, alpha, 3)
+ sage: asy
+ (8/343*(49*r^2 + 161*r + 114)*2^r, 2, 8/7*r^2 + 184/49*r + 912/343)
+ sage: F.relative_error(asy[0], alpha, [1, 2, 4, 8, 16], asy[1])
+ [((1,), 7.555555556, [7.556851312], [-0.0001714971672]),
+ ((2,), 14.74074074, [14.74052478], [0.00001465051901]),
+ ((4,), 35.96502058, [35.96501458], [1.667911934e-7]),
+ ((8,), 105.8425656, [105.8425656], [4.399565380e-11]),
+ ((16,), 355.3119534, [355.3119534], [0.0000000000])]
+
+Another smooth point example (Example 5.4 of [RaWi2008a]_)::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: q = 1/2
+ sage: qq = q.denominator()
+ sage: H = 1 - q*x + q*x*y - x^2*y
+ sage: Hfac = H.factor()
+ sage: G = (1 - q*x)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: alpha = list(qq*vector([2, 1 - q]))
+ sage: alpha
+ [4, 1]
+ sage: I = F.smooth_critical_ideal(alpha)
+ sage: I
+ Ideal (y^2 - 2*y + 1, x + 1/4*y - 5/4) of
+ Multivariate Polynomial Ring in x, y over Rational Field
+ sage: s = solve([SR(z) for z in I.gens()],
+ ....: [SR(z) for z in R.gens()], solution_dict=true)
+ sage: s == [{SR(x): 1, SR(y): 1}]
+ True
+ sage: p = s[0]
+ sage: asy = F.asymptotics(p, alpha, 1, verbose=True)
+ Creating auxiliary functions...
+ Computing derivatives of auxiallary functions...
+ Computing derivatives of more auxiliary functions...
+ Computing second order differential operator actions...
+ sage: asy
+ (1/24*2^(2/3)*(sqrt(3) + 4/(sqrt(3) + I) + I)*gamma(1/3)/(pi*r^(1/3)),
+ 1,
+ 1/24*2^(2/3)*(sqrt(3) + 4/(sqrt(3) + I) + I)*gamma(1/3)/(pi*r^(1/3)))
+ sage: r = SR('r')
+ sage: tuple((a*r^(1/3)).full_simplify() / r^(1/3) for a in asy) # make nicer coefficients
+ (1/12*sqrt(3)*2^(2/3)*gamma(1/3)/(pi*r^(1/3)),
+ 1,
+ 1/12*sqrt(3)*2^(2/3)*gamma(1/3)/(pi*r^(1/3)))
+ sage: F.relative_error(asy[0], alpha, [1, 2, 4, 8, 16], asy[1])
+ [((4, 1), 0.1875000000, [0.1953794675...], [-0.042023826...]),
+ ((8, 2), 0.1523437500, [0.1550727862...], [-0.017913673...]),
+ ((16, 4), 0.1221771240, [0.1230813519...], [-0.0074009592...]),
+ ((32, 8), 0.09739671811, [0.09768973377...], [-0.0030084757...]),
+ ((64, 16), 0.07744253816, [0.07753639308...], [-0.0012119297...])]
+
+A multiple point example (Example 6.5 of [RaWi2012]_)::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - 2*x - y)**2 * (1 - x - 2*y)**2
+ sage: Hfac = H.factor()
+ sage: G = 1/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F
+ (1, [(x + 2*y - 1, 2), (2*x + y - 1, 2)])
+ sage: I = F.singular_ideal()
+ sage: I
+ Ideal (x - 1/3, y - 1/3) of
+ Multivariate Polynomial Ring in x, y over Rational Field
+ sage: p = {x: 1/3, y: 1/3}
+ sage: F.is_convenient_multiple_point(p)
+ (True, 'convenient in variables [x, y]')
+ sage: alpha = (var('a'), var('b'))
+ sage: decomp = F.asymptotic_decomposition(alpha); decomp
+ (0, []) +
+ (-1/9*(2*b^2*x^2 - 5*a*b*x*y + 2*a^2*y^2)*r^2/(x^2*y^2)
+ - 1/9*(6*b*x^2 - 5*(a + b)*x*y + 6*a*y^2)*r/(x^2*y^2)
+ - 1/9*(4*x^2 - 5*x*y + 4*y^2)/(x^2*y^2),
+ [(x + 2*y - 1, 1), (2*x + y - 1, 1)])
+ sage: F1 = decomp[1]
+ sage: F1.asymptotics(p, alpha, 2)
+ (-3*((2*a^2 - 5*a*b + 2*b^2)*r^2 + (a + b)*r + 3)*((1/3)^(-a)*(1/3)^(-b))^r,
+ (1/3)^(-a)*(1/3)^(-b), -3*(2*a^2 - 5*a*b + 2*b^2)*r^2 - 3*(a + b)*r - 9)
+ sage: alpha = [4, 3]
+ sage: decomp = F.asymptotic_decomposition(alpha)
+ sage: F1 = decomp[1]
+ sage: asy = F1.asymptotics(p, alpha, 2)
+ sage: asy
+ (3*(10*r^2 - 7*r - 3)*2187^r, 2187, 30*r^2 - 21*r - 9)
+ sage: F.relative_error(asy[0], alpha, [1, 2, 4, 8], asy[1])
+ [((4, 3), 30.72702332, [0.0000000000], [1.000000000]),
+ ((8, 6), 111.9315678, [69.00000000], [0.3835519207]),
+ ((16, 12), 442.7813138, [387.0000000], [0.1259793763]),
+ ((32, 24), 1799.879232, [1743.000000], [0.03160169385])]
+
+TESTS::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: H = (1 - 2*x - y) * (1 - x - 2*y)
+ sage: G = 1
+ sage: Hfac = H.factor()
+ sage: G = G / Hfac.unit()
+ sage: F = FFPD(G, Hfac); F
+ (1, [(x + 2*y - 1, 1), (2*x + y - 1, 1)])
+ sage: p = {x: 1, y: 1}
+ sage: alpha = [1, 1]
+ sage: F.asymptotics(p, alpha, 1)
+ (1/3, 1, 1/3)
+
+::
+
+ sage: R.<x,y,t> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: H = (1 - y) * (1 + x^2) * (1 - t*(1 + x^2 + x*y^2))
+ sage: G = (1 + x) * (1 + x^2 - x*y^2)
+ sage: Hfac = H.factor()
+ sage: G = G / Hfac.unit()
+ sage: F = FFPD(G, Hfac); F
+ (-x^2*y^2 + x^3 - x*y^2 + x^2 + x + 1,
+ [(y - 1, 1), (x^2 + 1, 1), (x*y^2*t + x^2*t + t - 1, 1)])
+ sage: p = {x: 1, y: 1, t: 1/3}
+ sage: alpha = [1, 1, 1]
+ sage: F.asymptotics_multiple(p, alpha, 1, var('r')) # not tested - see #19989
+
+
+Various
+=======
+
+AUTHORS:
+
+- Alexander Raichev (2008)
+- Daniel Krenn (2014, 2016)
+
+
+Classes and Methods
+===================
+"""
+#*****************************************************************************
+# Copyright (C) 2008 Alexander Raichev <tortoise.said@gmail.com>
+# Copyright (C) 2014, 2016 Daniel Krenn <dev@danielkrenn.at>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+# http://www.gnu.org/licenses/
+#*****************************************************************************
+
+
+from functools import total_ordering
+from itertools import combinations_with_replacement
+from sage.structure.element import RingElement
+from sage.structure.unique_representation import UniqueRepresentation
+from sage.rings.ring import Ring
+from sage.calculus.var import var
+from sage.calculus.functional import diff
+from sage.symbolic.ring import SR
+from sage.misc.misc_c import prod
+from sage.rings.integer import Integer
+from sage.rings.integer_ring import ZZ
+from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
+from sage.categories.rings import Rings
+
+
+@total_ordering
+class FractionWithFactoredDenominator(RingElement):
+ r"""
+ This element represents a fraction with a factored polynomial
+ denominator. See also its parent
+ :class:`FractionWithFactoredDenominatorRing` for details.
+
+ Represents a fraction with factored polynomial denominator (FFPD)
+ `p/(q_1^{e_1} \cdots q_n^{e_n})` by storing the parts `p` and
+ `[(q_1, e_1), \ldots, (q_n, e_n)]`.
+ Here `q_1, \ldots, q_n` are elements of a 0- or multi-variate factorial
+ polynomial ring `R` , `q_1, \ldots, q_n` are distinct irreducible elements
+ of `R` , `e_1, \ldots, e_n` are positive integers, and `p` is a function
+ of the indeterminates of `R` (e.g., a Sage symbolic expression). An
+ element `r` with no polynomial denominator is represented as ``(r, [])``.
+
+ INPUT:
+
+ - ``numerator`` -- an element `p`; this can be of any ring from which
+ parent's base has coercion in
+ - ``denominator_factored`` -- a list of the form
+ `[(q_1, e_1), \ldots, (q_n, e_n)]`, where the `q_1, \ldots, q_n` are
+ distinct irreducible elements of `R` and the `e_i` are positive
+ integers
+ - ``reduce`` -- (optional) if ``True``, then represent
+ `p/(q_1^{e_1} \cdots q_n^{e_n})` in lowest terms, otherwise
+ this won't attempt to divide `p` by any of the `q_i`
+
+ OUTPUT:
+
+ An element representing the rational expression
+ `p/(q_1^{e_1} \cdots q_n^{e_n})`.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df)
+ sage: f
+ (1, [(y, 1), (x*y + 1, 1)])
+ sage: ff = FFPD(x, df, reduce=False)
+ sage: ff
+ (x, [(y, 1), (x, 1), (x*y + 1, 1)])
+
+ sage: f = FFPD(x + y, [(x + y, 1)])
+ sage: f
+ (1, [])
+
+ ::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + 1/(3*x^2 + 1)
+ sage: FFPD(f)
+ (5*x^7 - 5*x^6 + 5/3*x^5 - 5/3*x^4 + 2*x^3 - 2/3*x^2 + 1/3*x - 1/3,
+ [(x - 1, 1), (x, 1), (x^2 + 1/3, 1)])
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: f = 2*y/(5*(x^3 - 1)*(y + 1))
+ sage: FFPD(f)
+ (2/5*y, [(y + 1, 1), (x - 1, 1), (x^2 + x + 1, 1)])
+
+ sage: p = 1/x^2
+ sage: q = 3*x**2*y
+ sage: qs = q.factor()
+ sage: f = FFPD(p/qs.unit(), qs)
+ sage: f
+ (1/3/x^2, [(y, 1), (x, 2)])
+
+ sage: f = FFPD(cos(x)*x*y^2, [(x, 2), (y, 1)])
+ sage: f
+ (x*y^2*cos(x), [(y, 1), (x, 2)])
+
+ sage: G = exp(x + y)
+ sage: H = (1 - 2*x - y) * (1 - x - 2*y)
+ sage: a = FFPD(G/H)
+ sage: a
+ (e^(x + y), [(x + 2*y - 1, 1), (2*x + y - 1, 1)])
+ sage: a.denominator_ring
+ Multivariate Polynomial Ring in x, y over Rational Field
+ sage: b = FFPD(G, H.factor())
+ sage: b
+ (e^(x + y), [(x + 2*y - 1, 1), (2*x + y - 1, 1)])
+ sage: b.denominator_ring
+ Multivariate Polynomial Ring in x, y over Rational Field
+
+ Singular throws a 'not implemented' error when trying to factor in
+ a multivariate polynomial ring over an inexact field::
+
+ sage: R.<x,y> = PolynomialRing(CC)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = (x + 1)/(x*y*(x*y + 1)^2)
+ sage: FFPD(f)
+ Traceback (most recent call last):
+ ...
+ TypeError: Singular error:
+ ? not implemented
+ ? error occurred in or before STDIN line ...:
+ `def sage...=factorize(sage...);`
+
+ AUTHORS:
+
+ - Alexander Raichev (2012-07-26)
+ - Daniel Krenn (2014-12-01)
+ """
+ def __init__(self, parent, numerator, denominator_factored, reduce=True):
+ r"""
+ Initialize ``self``.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df)
+ sage: TestSuite(f).run()
+ """
+ super(FractionWithFactoredDenominator, self).__init__(parent)
+
+ from sage.rings.semirings.non_negative_integer_semiring import NN
+ self._numerator = parent._numerator_ring(numerator)
+ self._denominator_factored = list(
+ (parent._denominator_ring(d), NN(n))
+ for d, n in denominator_factored)
+
+ R = self.denominator_ring
+ if numerator in R and reduce:
+ # Reduce fraction if possible.
+ numer = R(self._numerator)
+ df = self._denominator_factored
+ new_df = []
+ for (q, e) in df:
+ ee = e
+ quo, rem = numer.quo_rem(q)
+ while rem == 0 and ee > 0:
+ ee -= 1
+ numer = quo
+ quo, rem = numer.quo_rem(q)
+ if ee > 0:
+ new_df.append((q, ee))
+ self._numerator = numer
+ self._denominator_factored = new_df
+
+
+ def numerator(self):
+ r"""
+ Return the numerator of ``self``.
+
+ OUTPUT:
+
+ The numerator.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F.numerator()
+ -e^y
+ """
+ return self._numerator
+
+
+ def denominator(self):
+ r"""
+ Return the denominator of ``self``.
+
+ OUTPUT:
+
+ The denominator (i.e., the product of the factored denominator).
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F.denominator()
+ x^3*y^2 + 2*x^3*y + x^2*y^2 + x^3 - 2*x^2*y - x*y^2 - 3*x^2 - 2*x*y
+ - y^2 + 3*x + 2*y - 1
+ """
+ return prod(q ** e for q, e in self.denominator_factored())
+
+
+ def __cmp__(self, other):
+ r"""
+ Compares two elements.
+
+ INPUT:
+
+ - ``other`` -- element to compare with ``self``
+
+ OUTPUT:
+
+ A comparison value.
+
+ TESTS::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = FFPD(x*y, [(x-1, 1), (y-2, 2)])
+ sage: g = FFPD(x, [(x-1, 1), (y-2, 2)])
+ sage: f.__cmp__(f)
+ 0
+ sage: f.__cmp__(g)
+ 1
+ """
+ return cmp(self.numerator() * other.denominator(),
+ other.numerator() * self.denominator())
+
+
+ def denominator_factored(self):
+ r"""
+ Return the factorization in ``self.denominator_ring`` of the denominator of
+ ``self`` but without the unit part.
+
+ OUTPUT:
+
+ The factored denominator as a list of tuple ``(f, m)``, where `f` is
+ a factor and `m` its multiplicity.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F.denominator_factored()
+ [(x - 1, 1), (x*y + x + y - 1, 2)]
+ """
+ return self._denominator_factored
+
+
+ @property
+ def denominator_ring(self):
+ r"""
+ Return the ring of the denominator.
+
+ OUTPUT:
+
+ A ring.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F.denominator_ring
+ Multivariate Polynomial Ring in x, y over Rational Field
+ sage: F = FFPD(G/H)
+ sage: F
+ (e^y, [(x - 1, 1), (x*y + x + y - 1, 2)])
+ sage: F.denominator_ring
+ Multivariate Polynomial Ring in x, y over Rational Field
+ """
+ return self.parent()._denominator_ring
+
+
+ @property
+ def numerator_ring(self):
+ r"""
+ Return the ring of the numerator.
+
+ OUTPUT:
+
+ A ring.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F.numerator_ring
+ Symbolic Ring
+ sage: F = FFPD(G/H)
+ sage: F
+ (e^y, [(x - 1, 1), (x*y + x + y - 1, 2)])
+ sage: F.numerator_ring
+ Symbolic Ring
+ """
+ return self.parent()._numerator_ring
+
+
+ def dimension(self):
+ r"""
+ Return the number of indeterminates of ``self.denominator_ring``.
+
+ OUTPUT:
+
+ An integer.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F.dimension()
+ 2
+ """
+ from sage.rings.polynomial.polynomial_ring import is_PolynomialRing
+ from sage.rings.polynomial.multi_polynomial_ring_generic import is_MPolynomialRing
+ R = self.denominator_ring
+ if is_PolynomialRing(R) or is_MPolynomialRing(R):
+ return R.ngens()
+ raise NotImplementedError('only polynomial rings are supported as base')
+
+
+ def quotient(self):
+ r"""
+ Convert ``self`` into a quotient.
+
+ OUTPUT:
+
+ An element.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - x - y - x*y)**2*(1-x)
+ sage: Hfac = H.factor()
+ sage: G = exp(y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F
+ (-e^y, [(x - 1, 1), (x*y + x + y - 1, 2)])
+ sage: F.quotient()
+ -e^y/(x^3*y^2 + 2*x^3*y + x^2*y^2 + x^3 - 2*x^2*y - x*y^2 - 3*x^2 -
+ 2*x*y - y^2 + 3*x + 2*y - 1)
+ """
+ return self.numerator() / self.denominator()
+
+
+ def _repr_(self):
+ r"""
+ Return a string representation of ``self``.
+
+ OUTPUT:
+
+ A string.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = FFPD(x + y, [(y, 1), (x, 1)])
+ sage: f
+ (x + y, [(y, 1), (x, 1)])
+ """
+ return repr((self.numerator(), self.denominator_factored()))
+
+
+ def __eq__(self, other):
+ r"""
+ Tests for equality of the given elements (with taking care of
+ different parents by using the coercion model).
+
+ INPUT:
+
+ - ``other`` -- object to compare with ``self``
+
+ OUTPUT:
+
+ ``True`` or ``False``.
+
+ TESTS::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = FFPD(x, [])
+ sage: f == x
+ True
+ """
+ from sage.structure.element import have_same_parent
+ if have_same_parent(self, other):
+ return self._eq_(other)
+
+ from sage.structure.element import get_coercion_model
+ import operator
+ try:
+ return get_coercion_model().bin_op(self, other, operator.eq)
+ except TypeError:
+ return False
+
+
+ def _eq_(self, other):
+ r"""
+ Two FFPD instances are equal iff they represent the same
+ fraction.
+
+ INPUT:
+
+ - ``other`` -- object to compare with ``self``
+
+ OUTPUT:
+
+ ``True`` or ``False``.
+
+ It can be assumed that ``self`` and ``other`` have the same parent.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df)
+ sage: ff = FFPD(x, df, reduce=False)
+ sage: f == ff
+ True
+ sage: g = FFPD(y, df)
+ sage: g == f
+ False
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: G = exp(x + y)
+ sage: H = (1 - 2*x - y) * (1 - x - 2*y)
+ sage: a = FFPD(G/H)
+ sage: b = FFPD(G, H.factor())
+ sage: bool(a == b)
+ True
+ """
+ return self.quotient() == other.quotient()
+
+
+ def __ne__(self, other):
+ r"""
+ Tests for nonequality of the given elements.
+
+ INPUT:
+
+ - ``other`` -- object to compare with ``self``
+
+ OUTPUT:
+
+ ``True`` or ``False``.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df)
+ sage: ff = FFPD(x, df, reduce=False)
+ sage: f != ff
+ False
+ sage: g = FFPD(y, df)
+ sage: g != f
+ True
+ """
+ return not (self == other)
+
+
+ def __lt__(self, other):
+ r"""
+ FFPD ``A`` is less than FFPD ``B`` iff
+ (the denominator factorization of ``A`` is shorter than that of ``B``)
+ of (the denominator factorization lengths are equal and
+ the denominator of ``A`` is less than that of ``B`` in their ring) or
+ (the denominator factorization lengths are equal and the
+ denominators are equal and the numerator of ``A`` is less than that
+ of ``B`` in their ring).
+
+ INPUT:
+
+ - ``other`` -- object to compare with ``self``.
+
+ OUTPUT:
+
+ ``True`` or ``False``.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: df = [x, 1], [y, 1], [x*y+1, 1]
+ sage: f = FFPD(x, df); f
+ (1, [(y, 1), (x*y + 1, 1)])
+ sage: ff = FFPD(x, df, reduce=False); ff
+ (x, [(y, 1), (x, 1), (x*y + 1, 1)])
+ sage: g = FFPD(y, df)
+ sage: h = FFPD(exp(x), df)
+ sage: i = FFPD(sin(x + 2), df)
+ sage: f < ff
+ True
+ sage: f < g
+ True
+ sage: g < h
+ True
+ sage: h < i
+ False
+ """
+ sn = self.numerator()
+ on = other.numerator()
+ sdf = self.denominator_factored()
+ odf = other.denominator_factored()
+ sd = self.denominator()
+ od = other.denominator()
+
+ return bool(len(sdf) < len(odf) or
+ (len(sdf) == len(odf) and sd < od) or
+ (len(sdf) == len(odf) and sd == od and sn < on))
+
+
+ def univariate_decomposition(self):
+ r"""
+ Return the usual univariate partial fraction decomposition
+ of ``self``.
+
+ Assume that the numerator of ``self`` lies in the same univariate
+ factorial polynomial ring as the factors of the denominator.
+
+ Let `f = p/q` be a rational expression where `p` and `q` lie in a
+ univariate factorial polynomial ring `R`.
+ Let `q_1^{e_1} \cdots q_n^{e_n}` be the
+ unique factorization of `q` in `R` into irreducible factors.
+ Then `f` can be written uniquely as:
+
+ .. MATH::
+
+ (*) \quad p_0 + \sum_{i=1}^{m} \frac{p_i}{q_i^{e_i}},
+
+ for some `p_j \in R`.
+ We call `(*)` the *usual partial fraction decomposition* of `f`.
+
+ .. NOTE::
+
+ This partial fraction decomposition can be computed using
+ :meth:`~sage.symbolic.expression.Expression.partial_fraction` or
+ :meth:`~sage.categories.quotient_fields.QuotientFields.ElementMethods.partial_fraction_decomposition`
+ as well. However, here we use the already obtained/cached
+ factorization of the denominator. This gives a speed up for
+ non-small instances.
+
+ OUTPUT:
+
+ An instance of :class:`FractionWithFactoredDenominatorSum`.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+
+ One variable::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + 1/(3*x^2 + 1)
+ sage: f
+ (15*x^7 - 15*x^6 + 5*x^5 - 5*x^4 + 6*x^3 - 2*x^2 + x - 1)/(3*x^4 -
+ 3*x^3 + x^2 - x)
+ sage: decomp = FFPD(f).univariate_decomposition()
+ sage: decomp
+ (5*x^3, []) +
+ (1, [(x - 1, 1)]) +
+ (1, [(x, 1)]) +
+ (1/3, [(x^2 + 1/3, 1)])
+ sage: decomp.sum().quotient() == f
+ True
+
+ One variable with numerator in symbolic ring::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + exp(x)/(3*x^2 + 1)
+ sage: f
+ (5*x^5 - 5*x^4 + 2*x - 1)/(x^2 - x) + e^x/(3*x^2 + 1)
+ sage: decomp = FFPD(f).univariate_decomposition()
+ sage: decomp
+ (0, []) +
+ (15/4*x^7 - 15/4*x^6 + 5/4*x^5 - 5/4*x^4 + 3/2*x^3 + 1/4*x^2*e^x -
+ 3/4*x^2 - 1/4*x*e^x + 1/2*x - 1/4, [(x - 1, 1)]) +
+ (-15*x^7 + 15*x^6 - 5*x^5 + 5*x^4 - 6*x^3 -
+ x^2*e^x + 3*x^2 + x*e^x - 2*x + 1, [(x, 1)]) +
+ (1/4*(15*x^7 - 15*x^6 + 5*x^5 - 5*x^4 + 6*x^3 + x^2*e^x -
+ 3*x^2 - x*e^x + 2*x - 1)*(3*x - 1), [(x^2 + 1/3, 1)])
+
+ One variable over a finite field::
+
+ sage: R.<x> = PolynomialRing(GF(2))
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + 1/(3*x^2 + 1)
+ sage: f
+ (x^6 + x^4 + 1)/(x^3 + x)
+ sage: decomp = FFPD(f).univariate_decomposition()
+ sage: decomp
+ (x^3, []) + (1, [(x, 1)]) + (x, [(x + 1, 2)])
+ sage: decomp.sum().quotient() == f
+ True
+
+ One variable over an inexact field::
+
+ sage: R.<x> = PolynomialRing(CC)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 5*x^3 + 1/x + 1/(x-1) + 1/(3*x^2 + 1)
+ sage: f
+ (15.0000000000000*x^7 - 15.0000000000000*x^6 + 5.00000000000000*x^5
+ - 5.00000000000000*x^4 + 6.00000000000000*x^3
+ - 2.00000000000000*x^2 + x - 1.00000000000000)/(3.00000000000000*x^4
+ - 3.00000000000000*x^3 + x^2 - x)
+ sage: decomp = FFPD(f).univariate_decomposition()
+ sage: decomp
+ (5.00000000000000*x^3, []) +
+ (1.00000000000000, [(x - 1.00000000000000, 1)]) +
+ (-0.288675134594813*I, [(x - 0.577350269189626*I, 1)]) +
+ (1.00000000000000, [(x, 1)]) +
+ (0.288675134594813*I, [(x + 0.577350269189626*I, 1)])
+ sage: decomp.sum().quotient() == f # Rounding error coming
+ False
+
+ TESTS::
+
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: f = exp(x) / (x^2-x)
+ sage: f
+ e^x/(x^2 - x)
+ sage: FFPD(f).univariate_decomposition()
+ (0, []) + (e^x, [(x - 1, 1)]) + (-e^x, [(x, 1)])
+
+ AUTHORS:
+
+ - Robert Bradshaw (2007-05-31)
+ - Alexander Raichev (2012-06-25)
+ - Daniel Krenn (2014-12-01)
+ """
+ if self.dimension() > 1:
+ return FractionWithFactoredDenominatorSum([self])
+
+ R = self.denominator_ring
+ p = self.numerator()
+ q = self.denominator()
+ try:
+ whole, p = R(p).quo_rem(q)
+ mn = R.one()
+ except (TypeError, ValueError):
+ whole = R(0)
+ mn = p
+ p = R.one()
+ df = self.denominator_factored()
+ decomp = [self.parent()(whole, [])]
+ denominator = prod(b**n for b, n in df)
+ for a, m in df:
+ am = a**m
+ q, r = denominator.quo_rem(am)
+ assert r==0
+ numer = p * q.inverse_mod(am) % am
+ # The inverse exists because the product and a**m
+ # are relatively prime.
+ decomp.append(self.parent()(mn * numer, [(a, m)]))
+ return FractionWithFactoredDenominatorSum(decomp)
+
+
+ def nullstellensatz_certificate(self):
+ r"""
+ Return a Nullstellensatz certificate of ``self`` if it exists.
+
+ Let `[(q_1, e_1), \ldots, (q_n, e_n)]` be the denominator
+ factorization of ``self``. The Nullstellensatz certificate is
+ a list of polynomials `h_1, \ldots, h_m` in ``self.denominator_ring``
+ that satisfies `h_1 q_1 + \cdots + h_m q_n = 1` if it exists.
+
+ .. NOTE::
+
+ Only works for multivariate base rings.
+
+ OUTPUT:
+
+ A list of polynomials or ``None`` if no Nullstellensatz
+ certificate exists.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: G = sin(x)
+ sage: H = x^2 * (x*y + 1)
+ sage: f = FFPD(G, H.factor())
+ sage: L = f.nullstellensatz_certificate()
+ sage: L
+ [y^2, -x*y + 1]
+ sage: df = f.denominator_factored()
+ sage: sum([L[i]*df[i][0]**df[i][1] for i in xrange(len(df))]) == 1
+ True
+
+ ::
+
+ sage: f = 1/(x*y)
+ sage: L = FFPD(f).nullstellensatz_certificate()
+ sage: L is None
+ True
+ """
+ R = self.denominator_ring
+ df = self.denominator_factored()
+ J = R.ideal([q ** e for q, e in df])
+ if R.one() in J:
+ return R.one().lift(J)
+ return None
+
+
+ def nullstellensatz_decomposition(self):
+ r"""
+ Return a Nullstellensatz decomposition of ``self``.
+
+ Let `f = p/q` where `q` lies in a `d` -variate polynomial ring
+ `K[X]` for some field `K` and `d \geq 1`.
+ Let `q_1^{e_1} \cdots q_n^{e_n}` be the
+ unique factorization of `q` in `K[X]` into irreducible factors and
+ let `V_i` be the algebraic variety `\{x \in L^d \mid q_i(x) = 0\}`
+ of `q_i` over the algebraic closure `L` of `K`.
+ By [Raic2012]_, `f` can be written as
+
+ .. MATH::
+
+ (*) \quad \sum_A \frac{p_A}{\prod_{i \in A} q_i^{e_i}},
+
+ where the `p_A` are products of `p` and elements in `K[X]` and
+ the sum is taken over all subsets
+ `A \subseteq \{1, \ldots, m\}` such that
+ `\bigcap_{i\in A} T_i \neq \emptyset`.
+
+ We call `(*)` a *Nullstellensatz decomposition* of `f`.
+ Nullstellensatz decompositions are not unique.
+
+ The algorithm used comes from [Raic2012]_.
+
+ .. NOTE::
+
+ Recursive. Only works for multivariate ``self``.
+
+ OUTPUT:
+
+ An instance of :class:`FractionWithFactoredDenominatorSum`.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import *
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 1/(x*(x*y + 1))
+ sage: decomp = FFPD(f).nullstellensatz_decomposition()
+ sage: decomp
+ (0, []) + (1, [(x, 1)]) + (-y, [(x*y + 1, 1)])
+ sage: decomp.sum().quotient() == f
+ True
+ sage: [r.nullstellensatz_certificate() is None for r in decomp]
+ [True, True, True]
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: G = sin(y)
+ sage: H = x*(x*y + 1)
+ sage: f = FFPD(G, H.factor())
+ sage: decomp = f.nullstellensatz_decomposition()
+ sage: decomp
+ (0, []) + (sin(y), [(x, 1)]) + (-y*sin(y), [(x*y + 1, 1)])
+ sage: bool(decomp.sum().quotient() == G/H)
+ True
+ sage: [r.nullstellensatz_certificate() is None for r in decomp]
+ [True, True, True]
+ """
+ L = self.nullstellensatz_certificate()
+ if L is None:
+ # No decomposing possible.
+ return FractionWithFactoredDenominatorSum([self])
+
+ # Otherwise decompose recursively.
+ decomp = FractionWithFactoredDenominatorSum()
+ p = self.numerator()
+ df = self.denominator_factored()
+ m = len(df)
+ iteration1 = FractionWithFactoredDenominatorSum(
+ [self.parent()(p * L[i], [df[j] for j in xrange(m) if j != i])
+ for i in xrange(m) if L[i] != 0])
+
+ # Now decompose each FFPD of iteration1.
+ for r in iteration1:
+ decomp.extend(r.nullstellensatz_decomposition())
+
+ # Simplify and return result.
+ return decomp._combine_like_terms_().whole_and_parts()
+
+
+ def algebraic_dependence_certificate(self):
+ r"""
+ Return the algebraic dependence certificate of ``self``.
+
+ The algebraic dependence certificate is the ideal `J` of
+ annihilating polynomials for the set of polynomials
+ ``[q^e for (q, e) in self.denominator_factored()]``,
+ which could be the zero ideal.
+ The ideal `J` lies in a polynomial ring over the field
+ ``self.denominator_ring.base_ring()`` that has
+ ``m = len(self.denominator_factored())`` indeterminates.
+
+ OUTPUT:
+
+ An ideal.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 1/(x^2 * (x*y + 1) * y^3)
+ sage: ff = FFPD(f)
+ sage: J = ff.algebraic_dependence_certificate(); J
+ Ideal (1 - 6*T2 + 15*T2^2 - 20*T2^3 + 15*T2^4 - T0^2*T1^3 -
+ 6*T2^5 + T2^6) of Multivariate Polynomial Ring in
+ T0, T1, T2 over Rational Field
+ sage: g = J.gens()[0]
+ sage: df = ff.denominator_factored()
+ sage: g(*(q**e for q, e in df)) == 0
+ True
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: G = exp(x + y)
+ sage: H = x^2 * (x*y + 1) * y^3
+ sage: ff = FFPD(G, H.factor())
+ sage: J = ff.algebraic_dependence_certificate(); J
+ Ideal (1 - 6*T2 + 15*T2^2 - 20*T2^3 + 15*T2^4 - T0^2*T1^3 -
+ 6*T2^5 + T2^6) of Multivariate Polynomial Ring in
+ T0, T1, T2 over Rational Field
+ sage: g = J.gens()[0]
+ sage: df = ff.denominator_factored()
+ sage: g(*(q**e for q, e in df)) == 0
+ True
+
+ ::
+
+ sage: f = 1/(x^3 * y^2)
+ sage: J = FFPD(f).algebraic_dependence_certificate()
+ sage: J
+ Ideal (0) of Multivariate Polynomial Ring in T0, T1 over Rational Field
+
+ ::
+
+ sage: f = sin(1)/(x^3 * y^2)
+ sage: J = FFPD(f).algebraic_dependence_certificate()
+ sage: J
+ Ideal (0) of Multivariate Polynomial Ring in T0, T1 over Rational Field
+ """
+ R = self.denominator_ring
+ df = self.denominator_factored()
+ if not df:
+ return R.ideal() # The zero ideal.
+ m = len(df)
+ F = R.base_ring()
+ Xs = list(R.gens())
+ d = len(Xs)
+
+ # Expand R by 2 * m new variables.
+ S = 'S'
+ while S in [str(x) for x in Xs]:
+ S = S + 'S'
+ Ss = [S + str(i) for i in xrange(m)]
+ T = 'T'
+ while T in [str(x) for x in Xs]:
+ T = T + 'T'
+ Ts = [T + str(i) for i in xrange(m)]
+
+ Vs = [str(x) for x in Xs] + Ss + Ts
+ RR = PolynomialRing(F, Vs)
+ Xs = RR.gens()[:d]
+ Ss = RR.gens()[d: d + m]
+ Ts = RR.gens()[d + m: d + 2 * m]
+
+ # Compute the appropriate elimination ideal.
+ J = RR.ideal([Ss[j] - RR(df[j][0]) for j in xrange(m)] +
+ [Ss[j] ** df[j][1] - Ts[j] for j in xrange(m)])
+ J = J.elimination_ideal(Xs + Ss)
+
+ # Coerce J into the polynomial ring in the indeteminates Ts[m:].
+ # I choose the negdeglex order because i find it useful in my work.
+ RRR = PolynomialRing(F, [str(t) for t in Ts], order='negdeglex')
+ return RRR.ideal(J)
+
+
+ def algebraic_dependence_decomposition(self, whole_and_parts=True):
+ r"""
+ Return an algebraic dependence decomposition of ``self``.
+
+ Let `f = p/q` where `q` lies in a `d`-variate polynomial ring
+ `K[X]` for some field `K`.
+ Let `q_1^{e_1} \cdots q_n^{e_n}` be the
+ unique factorization of `q` in `K[X]` into irreducible factors and
+ let `V_i` be the algebraic variety `\{x \in L^d \mid q_i(x) = 0\}`
+ of `q_i` over the algebraic closure `L` of `K`.
+ By [Raic2012]_, `f` can be written as
+
+ .. MATH::
+
+ (*) \quad \sum_A \frac{p_A}{\prod_{i \in A} q_i^{b_i}},
+
+ where the `b_i` are positive integers, each `p_A` is a products
+ of `p` and an element in `K[X]`,
+ and the sum is taken over all subsets
+ `A \subseteq \{1, \ldots, m\}` such that `|A| \leq d` and
+ `\{q_i \mid i \in A\}` is algebraically independent.
+
+ We call `(*)` an *algebraic dependence decomposition* of `f`.
+ Algebraic dependence decompositions are not unique.
+
+ The algorithm used comes from [Raic2012]_.
+
+ OUTPUT:
+
+ An instance of :class:`FractionWithFactoredDenominatorSum`.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 1/(x^2 * (x*y + 1) * y^3)
+ sage: ff = FFPD(f)
+ sage: decomp = ff.algebraic_dependence_decomposition()
+ sage: decomp
+ (0, []) + (-x, [(x*y + 1, 1)]) +
+ (x^2*y^2 - x*y + 1, [(y, 3), (x, 2)])
+ sage: decomp.sum().quotient() == f
+ True
+ sage: for r in decomp:
+ ....: J = r.algebraic_dependence_certificate()
+ ....: J is None or J == J.ring().ideal() # The zero ideal
+ True
+ True
+ True
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: G = sin(x)
+ sage: H = x^2 * (x*y + 1) * y^3
+ sage: f = FFPD(G, H.factor())
+ sage: decomp = f.algebraic_dependence_decomposition()
+ sage: decomp
+ (0, []) + (x^4*y^3*sin(x), [(x*y + 1, 1)]) +
+ (-(x^5*y^5 - x^4*y^4 + x^3*y^3 - x^2*y^2 + x*y - 1)*sin(x),
+ [(y, 3), (x, 2)])
+ sage: bool(decomp.sum().quotient() == G/H)
+ True
+ sage: for r in decomp:
+ ....: J = r.algebraic_dependence_certificate()
+ ....: J is None or J == J.ring().ideal()
+ True
+ True
+ True
+ """
+ J = self.algebraic_dependence_certificate()
+ if not J:
+ # No decomposing possible.
+ return FractionWithFactoredDenominatorSum([self])
+
+ # Otherwise decompose recursively.
+ decomp = FractionWithFactoredDenominatorSum()
+ p = self.numerator()
+ df = self.denominator_factored()
+ m = len(df)
+ g = J.gens()[0] # An annihilating polynomial for df.
+ new_vars = J.ring().gens()
+ # Note that each new_vars[j] corresponds to df[j] such that
+ # g([q**e for q, e in df]) = 0.
+ # Assuming here that g.parent() has negdeglex term order
+ # so that g.lt() is indeed the monomial we want below.
+ # Use g to rewrite r into a sum of FFPDs,
+ # each with < m distinct denominator factors.
+ gg = (g.lt() - g) / (g.lc())
+ numers = map(prod, zip(gg.coefficients(), gg.monomials()))
+ e = list(g.lt().exponents())[0: m]
+ denoms = [(new_vars[j], e[0][j] + 1) for j in xrange(m)]
+ # Write r in terms of new_vars,
+ # cancel factors in the denominator, and combine like terms.
+ FFPD = FractionWithFactoredDenominatorRing(J.ring())
+ iteration1_temp = FractionWithFactoredDenominatorSum(
+ [FFPD(a, denoms) for a in numers])._combine_like_terms_()
+ # Substitute in df.
+ qpowsub = {new_vars[j]: df[j][0] ** df[j][1] for j in xrange(m)}
+ iteration1 = FractionWithFactoredDenominatorSum()
+ for r in iteration1_temp:
+ num1 = p * J.ring()(r.numerator()).subs(qpowsub)
+ denoms1 = []
+ for q, e in r.denominator_factored():
+ j = new_vars.index(q)
+ denoms1.append((df[j][0], df[j][1] * e))
+ iteration1.append(self.parent()(num1, denoms1))
+ # Now decompose each FFPD of iteration1.
+ for r in iteration1:
+ decomp.extend(r.algebraic_dependence_decomposition())
+
+ # Simplify and return result.
+ return decomp._combine_like_terms_().whole_and_parts()
+
+
+ def leinartas_decomposition(self):
+ r"""
+ Return a Leinartas decomposition of ``self``.
+
+ Let `f = p/q` where `q` lies in a `d` -variate polynomial
+ ring `K[X]` for some field `K`.
+ Let `q_1^{e_1} \cdots q_n^{e_n}` be the
+ unique factorization of `q` in `K[X]` into irreducible factors and
+ let `V_i` be the algebraic variety
+ `\{x\in L^d \mid q_i(x) = 0\}` of `q_i` over the algebraic closure
+ `L` of `K`. By [Raic2012]_, `f` can be written as
+
+ .. MATH::
+
+ (*) \quad \sum_A \frac{p_A}{\prod_{i \in A} q_i^{b_i}},
+
+ where the `b_i` are positive integers, each `p_A` is a product of
+ `p` and an element of `K[X]`, and the sum is taken over all
+ subsets `A \subseteq \{1, \ldots, m\}` such that
+
+ 1. `|A| \le d`,
+ 2. `\bigcap_{i\in A} T_i \neq \emptyset`, and
+ 3. `\{q_i \mid i\in A\}` is algebraically independent.
+
+ In particular, any rational expression in `d` variables
+ can be represented as a sum of rational expressions
+ whose denominators each contain at most `d` distinct irreducible
+ factors.
+
+ We call `(*)` a *Leinartas decomposition* of `f`.
+ Leinartas decompositions are not unique.
+
+ The algorithm used comes from [Raic2012]_.
+
+ OUTPUT:
+
+ An instance of :class:`FractionWithFactoredDenominatorSum`.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = (x^2 + 1)/((x + 2)*(x - 1)*(x^2 + x + 1))
+ sage: decomp = FFPD(f).leinartas_decomposition()
+ sage: decomp
+ (0, []) + (2/9, [(x - 1, 1)]) +
+ (-5/9, [(x + 2, 1)]) + (1/3*x, [(x^2 + x + 1, 1)])
+ sage: decomp.sum().quotient() == f
+ True
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 1/x + 1/y + 1/(x*y + 1)
+ sage: decomp = FFPD(f).leinartas_decomposition()
+ sage: decomp
+ (0, []) + (1, [(x*y + 1, 1)]) + (x + y, [(y, 1), (x, 1)])
+ sage: decomp.sum().quotient() == f
+ True
+ sage: def check_decomp(r):
+ ....: L = r.nullstellensatz_certificate()
+ ....: J = r.algebraic_dependence_certificate()
+ ....: return L is None and (J is None or J == J.ring().ideal())
+ sage: all(check_decomp(r) for r in decomp)
+ True
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: f = sin(x)/x + 1/y + 1/(x*y + 1)
+ sage: G = f.numerator()
+ sage: H = R(f.denominator())
+ sage: ff = FFPD(G, H.factor())
+ sage: decomp = ff.leinartas_decomposition()
+ sage: decomp
+ (0, []) +
+ (-(x*y^2*sin(x) + x^2*y + x*y + y*sin(x) + x)*y, [(y, 1)]) +
+ ((x*y^2*sin(x) + x^2*y + x*y + y*sin(x) + x)*x*y, [(x*y + 1, 1)]) +
+ (x*y^2*sin(x) + x^2*y + x*y + y*sin(x) + x, [(y, 1), (x, 1)])
+ sage: bool(decomp.sum().quotient() == f)
+ True
+ sage: all(check_decomp(r) for r in decomp)
+ True
+
+ ::
+
+ sage: R.<x,y,z>= PolynomialRing(GF(2, 'a'))
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 1/(x * y * z * (x*y + z))
+ sage: decomp = FFPD(f).leinartas_decomposition()
+ sage: decomp
+ (0, []) + (1, [(z, 2), (x*y + z, 1)]) +
+ (1, [(z, 2), (y, 1), (x, 1)])
+ sage: decomp.sum().quotient() == f
+ True
+ """
+ if self.dimension() == 1:
+ # Sage's lift() function doesn't work in
+ # univariate polynomial rings.
+ # So nullstellensatz_decomposition() won't work.
+ # Can use algebraic_dependence_decomposition(),
+ # which is sufficient.
+ # temp = FractionWithFactoredDenominatorSum([self])
+ # Alternatively can use univariate_decomposition(),
+ # which is more efficient.
+ return self.univariate_decomposition()
+ temp = self.nullstellensatz_decomposition()
+ decomp = FractionWithFactoredDenominatorSum()
+ for r in temp:
+ decomp.extend(r.algebraic_dependence_decomposition())
+
+ # Simplify and return result.
+ return decomp._combine_like_terms_().whole_and_parts()
+
+
+ def cohomology_decomposition(self):
+ r"""
+ Return the cohomology decomposition of ``self``.
+
+ Let `p / (q_1^{e_1} \cdots q_n^{e_n})` be the fraction represented
+ by ``self`` and let `K[x_1, \ldots, x_d]` be the polynomial ring
+ in which the `q_i` lie.
+ Assume that `n \leq d` and that the gradients of the `q_i` are linearly
+ independent at all points in the intersection
+ `V_1 \cap \ldots \cap V_n` of the algebraic varieties
+ `V_i = \{x \in L^d \mid q_i(x) = 0 \}`, where `L` is the algebraic
+ closure of the field `K`.
+ Return a :class:`FractionWithFactoredDenominatorSum`
+ `f` such that the differential form
+ `f dx_1 \wedge \cdots \wedge dx_d` is de Rham cohomologous to the
+ differential form
+ `p / (q_1^{e_1} \cdots q_n^{e_n}) dx_1 \wedge \cdots \wedge dx_d`
+ and such that the denominator of each summand of `f` contains
+ no repeated irreducible factors.
+
+ The algorithm used here comes from the proof of Theorem 17.4 of
+ [AiYu1983]_.
+
+ OUTPUT:
+
+ An instance of :class:`FractionWithFactoredDenominatorSum`.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: f = 1/(x^2 + x + 1)^3
+ sage: decomp = FFPD(f).cohomology_decomposition()
+ sage: decomp
+ (0, []) + (2/3, [(x^2 + x + 1, 1)])
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: FFPD(1, [(x, 1), (y, 2)]).cohomology_decomposition()
+ (0, [])
+
+ sage: p = 1
+ sage: qs = [(x*y - 1, 1), (x**2 + y**2 - 1, 2)]
+ sage: f = FFPD(p, qs)
+ sage: f.cohomology_decomposition()
+ (0, []) + (4/3*x*y + 4/3, [(x^2 + y^2 - 1, 1)]) +
+ (1/3, [(x*y - 1, 1), (x^2 + y^2 - 1, 1)])
+ """
+ from sage.calculus.functions import jacobian
+ from sage.arith.all import xgcd
+ from sage.sets.set import Set
+
+ R = self.denominator_ring
+ df = self.denominator_factored()
+ n = len(df)
+ if sum(e for (q, e) in df) <= n:
+ # No decomposing possible.
+ return FractionWithFactoredDenominatorSum([self])
+
+ # Otherwise decompose recursively.
+ decomp = FractionWithFactoredDenominatorSum()
+ p = self.numerator()
+ qs = [q for (q, e) in df]
+ X = sorted(R.gens())
+ var_sets_n = Set(X).subsets(n)
+ Par = self.parent()
+
+ # Compute Jacobian determinants for qs.
+ dets = []
+ for v in var_sets_n:
+ # Sort v according to the term order of R.
+ x = sorted(v)
+ jac = jacobian(qs, x)
+ dets.append(R(jac.determinant()))
+
+ # Get a Nullstellensatz certificate for qs and dets.
+ if self.dimension() == 1:
+ # Sage's lift() function doesn't work in
+ # univariate polynomial rings.
+ # So use xgcd(), which does the same thing in this case.
+ # Note that by assumption qs and dets have length 1.
+ L = xgcd(qs[0], dets[0])[1:]
+ else:
+ L = R.one().lift(R.ideal(qs + dets))
+
+ # Do first iteration of decomposition.
+ iteration1 = FractionWithFactoredDenominatorSum()
+ # Contributions from qs.
+ for i in xrange(n):
+ if L[i] == 0:
+ continue
+ # Cancel one df[i] from denominator.
+ new_df = [list(t) for t in df]
+ if new_df[i][1] > 1:
+ new_df[i][1] -= 1
+ else:
+ del new_df[i]
+ iteration1.append(Par(p * L[i], new_df))
+
+ # Contributions from dets.
+ # Compute each contribution's cohomologous form using
+ # the least index j such that new_df[j][1] > 1.
+ # Know such an index exists by first 'if' statement at
+ # the top.
+ for j in xrange(n):
+ if df[j][1] > 1:
+ J = j
+ break
+ new_df = [list(t) for t in df]
+ new_df[J][1] -= 1
+ for k in xrange(var_sets_n.cardinality()):
+ if L[n + k] == 0:
+ continue
+ # Sort variables according to the term order of R.
+ x = sorted(var_sets_n[k])
+ # Compute Jacobian in the Symbolic Ring.
+ jac = jacobian([SR(p * L[n + k])] +
+ [SR(qs[j]) for j in xrange(n) if j != J],
+ [SR(xx) for xx in x])
+ det = jac.determinant()
+ psign = permutation_sign(x, X)
+ iteration1.append(Par((-1) ** J * det / (psign * new_df[J][1]),
+ new_df))
+
+ # Now decompose each FFPD of iteration1.
+ for r in iteration1:
+ decomp.extend(r.cohomology_decomposition())
+
+ # Simplify and return result.
+ return decomp._combine_like_terms_().whole_and_parts()
+
+
+ def asymptotic_decomposition(self, alpha, asy_var=None):
+ r"""
+ Return the asymptotic decomposition of ``self``.
+
+ The asymptotic decomposition of `F` is a sum that has the
+ same asymptotic expansion as `f` in the direction ``alpha``
+ but each summand has a denominator factorization of the form
+ `[(q_1, 1), \ldots, (q_n, 1)]`, where `n` is at most the
+ :meth:`dimension` of `F`.
+
+ INPUT:
+
+ - ``alpha`` -- a `d`-tuple of positive integers or symbolic variables
+ - ``asy_var`` -- (default: ``None``) a symbolic variable with
+ respect to which to compute asymptotics;
+ if ``None`` is given, we set ``asy_var = var('r')``
+
+ OUTPUT:
+
+ An instance of :class:`FractionWithFactoredDenominatorSum`.
+
+ The output results from a Leinartas decomposition followed by a
+ cohomology decomposition.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: f = (x^2 + 1)/((x - 1)^3*(x + 2))
+ sage: F = FFPD(f)
+ sage: alpha = [var('a')]
+ sage: F.asymptotic_decomposition(alpha)
+ (0, []) +
+ (1/54*(5*a^2*x^2 + 2*a^2*x + 11*a^2)*r^2/x^2
+ - 1/54*(5*a*x^2 - 2*a*x - 33*a)*r/x^2 + 11/27/x^2, [(x - 1, 1)]) +
+ (-5/27, [(x + 2, 1)])
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - 2*x -y)*(1 - x -2*y)**2
+ sage: Hfac = H.factor()
+ sage: G = 1/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: alpha = var('a, b')
+ sage: F.asymptotic_decomposition(alpha)
+ (0, []) +
+ (1/3*(2*b*x - a*y)*r/(x*y) + 1/3*(2*x - y)/(x*y),
+ [(x + 2*y - 1, 1), (2*x + y - 1, 1)])
+ """
+ R = self.denominator_ring
+ d = self.dimension()
+ n = len(self.denominator_factored())
+ X = [SR(x) for x in R.gens()]
+
+ # Reduce number of distinct factors in denominator of self
+ # down to at most d.
+ decomp1 = FractionWithFactoredDenominatorSum([self])
+ if n > d:
+ decomp1 = decomp1[0].leinartas_decomposition()
+
+ # Reduce to no repeated factors in denominator of each element
+ # of decomp1.
+ # Compute the cohomology decomposition for each
+ # Cauchy differential form generated by each element of decomp.
+ if asy_var is None:
+ asy_var = var('r')
+ cauchy_stuff = prod([X[j] ** (-alpha[j] * asy_var - 1)
+ for j in xrange(d)])
+ decomp2 = FractionWithFactoredDenominatorSum()
+ for f in decomp1:
+ ff = self.parent()(f.numerator() * cauchy_stuff,
+ f.denominator_factored())
+ decomp2.extend(ff.cohomology_decomposition())
+ decomp2 = decomp2._combine_like_terms_()
+
+ # Divide out cauchy_stuff from integrands.
+ decomp3 = FractionWithFactoredDenominatorSum()
+ for f in decomp2:
+ ff = self.parent()((f.numerator() /
+ cauchy_stuff).simplify_full().collect(asy_var),
+ f.denominator_factored())
+ decomp3.append(ff)
+
+ return decomp3
+
+
+ def asymptotics(self, p, alpha, N, asy_var=None, numerical=0,
+ verbose=False):
+ r"""
+ Return the asymptotics in the given direction.
+
+ This function returns the first `N` terms (some of which could be
+ zero) of the asymptotic expansion of the Maclaurin ray coefficients
+ `F_{r \alpha}` of the function `F` represented by ``self`` as
+ `r \to \infty`, where `r` is ``asy_var`` and ``alpha`` is a tuple
+ of positive integers of length `d` which is ``self.dimension()``.
+ Assume that
+
+ - `F` is holomorphic in a neighborhood of the origin;
+ - the unique factorization of the denominator `H` of `F` in the local
+ algebraic ring at `p` equals its unique factorization in the local
+ analytic ring at `p`;
+ - the unique factorization of `H` in the local algebraic ring at `p`
+ has at most ``d`` irreducible factors, none of which are repeated
+ (one can reduce to this case via :meth:`asymptotic_decomposition()`);
+ - `p` is a convenient strictly minimal smooth or multiple point
+ with all nonzero coordinates that is critical and nondegenerate
+ for ``alpha``.
+
+ The algorithms used here come from [RaWi2008a]_ and [RaWi2012]_.
+
+ INPUT:
+
+ - ``p`` -- a dictionary with keys that can be coerced to equal
+ ``self.denominator_ring.gens()``
+ - ``alpha`` -- a tuple of length ``self.dimension()`` of
+ positive integers or, if `p` is a smooth point,
+ possibly of symbolic variables
+ - ``N`` -- a positive integer
+ - ``asy_var`` -- (default: ``None``) a symbolic variable for the
+ asymptotic expansion; if ``none`` is given, then
+ ``var('r')`` will be assigned
+ - ``numerical`` -- (default: 0) a natural number;
+ if ``numerical`` is greater than 0, then return a numerical
+ approximation of `F_{r \alpha}` with ``numerical`` digits of
+ precision; otherwise return exact values
+ - ``verbose`` -- (default: ``False``) print the current state of
+ the algorithm
+
+ OUTPUT:
+
+ The tuple ``(asy, exp_scale, subexp_part)``.
+ Here ``asy`` is the sum of the first `N` terms (some of which might
+ be 0) of the asymptotic expansion of `F_{r\alpha}` as `r \to \infty`;
+ ``exp_scale**r`` is the exponential factor of ``asy``;
+ ``subexp_part`` is the subexponential factor of ``asy``.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+
+ A smooth point example::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - x - y - x*y)**2
+ sage: Hfac = H.factor()
+ sage: G = 1/Hfac.unit()
+ sage: F = FFPD(G, Hfac); print(F)
+ (1, [(x*y + x + y - 1, 2)])
+ sage: alpha = [4, 3]
+ sage: decomp = F.asymptotic_decomposition(alpha); decomp
+ (0, []) + (-3/2*r*(y + 1)/y - 1/2*(y + 1)/y, [(x*y + x + y - 1, 1)])
+ sage: F1 = decomp[1]
+ sage: p = {y: 1/3, x: 1/2}
+ sage: asy = F1.asymptotics(p, alpha, 2, verbose=True)
+ Creating auxiliary functions...
+ Computing derivatives of auxiallary functions...
+ Computing derivatives of more auxiliary functions...
+ Computing second order differential operator actions...
+ sage: asy
+ (1/6000*(3600*sqrt(5)*sqrt(3)*sqrt(2)*sqrt(r)/sqrt(pi)
+ + 463*sqrt(5)*sqrt(3)*sqrt(2)/(sqrt(pi)*sqrt(r)))*432^r,
+ 432,
+ 3/5*sqrt(5)*sqrt(3)*sqrt(2)*sqrt(r)/sqrt(pi)
+ + 463/6000*sqrt(5)*sqrt(3)*sqrt(2)/(sqrt(pi)*sqrt(r)))
+ sage: F.relative_error(asy[0], alpha, [1, 2, 4, 8, 16], asy[1])
+ [((4, 3), 2.083333333, [2.092576110], [-0.0044365330...]),
+ ((8, 6), 2.787374614, [2.790732875], [-0.0012048112...]),
+ ((16, 12), 3.826259447, [3.827462310], [-0.0003143703...]),
+ ((32, 24), 5.328112821, [5.328540787], [-0.0000803222...]),
+ ((64, 48), 7.475927885, [7.476079664], [-0.0000203023...])]
+
+ A multiple point example::
+
+ sage: R.<x,y,z>= PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (4 - 2*x - y - z)**2*(4 - x - 2*y - z)
+ sage: Hfac = H.factor()
+ sage: G = 16/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F
+ (-16, [(x + 2*y + z - 4, 1), (2*x + y + z - 4, 2)])
+ sage: alpha = [3, 3, 2]
+ sage: decomp = F.asymptotic_decomposition(alpha); decomp
+ (0, []) +
+ (16*r*(4*y - 3*z)/(y*z) + 16*(2*y - z)/(y*z),
+ [(x + 2*y + z - 4, 1), (2*x + y + z - 4, 1)])
+ sage: F1 = decomp[1]
+ sage: p = {x: 1, y: 1, z: 1}
+ sage: asy = F1.asymptotics(p, alpha, 2, verbose=True) # long time
+ Creating auxiliary functions...
+ Computing derivatives of auxiliary functions...
+ Computing derivatives of more auxiliary functions...
+ Computing second-order differential operator actions...
+ sage: asy # long time
+ (4/3*sqrt(3)*sqrt(r)/sqrt(pi) + 47/216*sqrt(3)/(sqrt(pi)*sqrt(r)),
+ 1, 4/3*sqrt(3)*sqrt(r)/sqrt(pi) + 47/216*sqrt(3)/(sqrt(pi)*sqrt(r)))
+ sage: F.relative_error(asy[0], alpha, [1, 2, 4, 8], asy[1]) # long time
+ [((3, 3, 2), 0.9812164307, [1.515572606], [-0.54458543...]),
+ ((6, 6, 4), 1.576181132, [1.992989399], [-0.26444185...]),
+ ((12, 12, 8), 2.485286378, [2.712196351], [-0.091301338...]),
+ ((24, 24, 16), 3.700576827, [3.760447895], [-0.016178847...])]
+ """
+ R = self.denominator_ring
+
+ # Coerce keys of p into R.
+ p = coerce_point(R, p)
+
+ if asy_var is None:
+ asy_var = var('r')
+ d = self.dimension()
+ X = list(R.gens())
+ alpha = list(alpha)
+ df = self.denominator_factored()
+ n = len(df) # Number of smooth factors
+
+ # Find greatest i such that X[i] is a convenient coordinate,
+ # that is, such that for all (h, e) in df, we have
+ # (X[i]*diff(h, X[i])).subs(p) != 0.
+ # Assuming such an i exists.
+ i = d - 1
+ while 0 in [(X[i] * diff(h, X[i])).subs(p) for (h, e) in df]:
+ i -= 1
+ coordinate = i
+
+ if n == 1:
+ # Smooth point.
+ return self.asymptotics_smooth(p, alpha, N, asy_var, coordinate,
+ numerical, verbose=verbose)
+
+ # Multiple point.
+ return self.asymptotics_multiple(p, alpha, N, asy_var, coordinate,
+ numerical, verbose=verbose)
+
+
+ def asymptotics_smooth(self, p, alpha, N, asy_var, coordinate=None,
+ numerical=0, verbose=False):
+ r"""
+ Return the asymptotics in the given direction of a smooth point.
+
+ This is the same as :meth:`asymptotics()`, but only in the
+ case of a convenient smooth point.
+
+ The formulas used for computing the asymptotic expansions are
+ Theorems 3.2 and 3.3 [RaWi2008a]_ with the exponent of `H`
+ equal to 1. Theorem 3.2 is a specialization of Theorem 3.4
+ of [RaWi2012]_ with `n = 1`.
+
+ INPUT:
+
+ - ``p`` -- a dictionary with keys that can be coerced to equal
+ ``self.denominator_ring.gens()``
+ - ``alpha`` -- a tuple of length ``d = self.dimension()`` of
+ positive integers or, if `p` is a smooth point,
+ possibly of symbolic variables
+ - ``N`` -- a positive integer
+ - ``asy_var`` -- (optional; default: ``None``) a symbolic variable;
+ the variable of the asymptotic expansion,
+ if none is given, ``var('r')`` will be assigned
+ - ``coordinate`` -- (optional; default: ``None``) an integer in
+ `\{0, \ldots, d-1\}` indicating a convenient coordinate to base
+ the asymptotic calculations on; if ``None`` is assigned, then
+ choose ``coordinate=d-1``
+ - ``numerical`` -- (optional; default: 0) a natural number;
+ if numerical is greater than 0, then return a numerical approximation
+ of the Maclaurin ray coefficients of ``self`` with ``numerical``
+ digits of precision; otherwise return exact values
+ - ``verbose`` -- (default: ``False``) print the current state of
+ the algorithm
+
+ OUTPUT:
+
+ The asymptotic expansion.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: H = 2 - 3*x
+ sage: Hfac = H.factor()
+ sage: G = 1/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F
+ (-1/3, [(x - 2/3, 1)])
+ sage: alpha = [2]
+ sage: p = {x: 2/3}
+ sage: asy = F.asymptotics_smooth(p, alpha, 3, asy_var=var('r'))
+ sage: asy
+ (1/2*(9/4)^r, 9/4, 1/2)
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: H = 1-x-y-x*y
+ sage: Hfac = H.factor()
+ sage: G = 1/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: alpha = [3, 2]
+ sage: p = {y: 1/2*sqrt(13) - 3/2, x: 1/3*sqrt(13) - 2/3}
+ sage: F.asymptotics_smooth(p, alpha, 2, var('r'), numerical=3, verbose=True)
+ Creating auxiliary functions...
+ Computing derivatives of auxiallary functions...
+ Computing derivatives of more auxiliary functions...
+ Computing second order differential operator actions...
+ (71.2^r*(0.369/sqrt(r) - 0.018.../r^(3/2)), 71.2, 0.369/sqrt(r) - 0.018.../r^(3/2))
+
+ sage: q = 1/2
+ sage: qq = q.denominator()
+ sage: H = 1 - q*x + q*x*y - x^2*y
+ sage: Hfac = H.factor()
+ sage: G = (1 - q*x)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: alpha = list(qq*vector([2, 1 - q]))
+ sage: alpha
+ [4, 1]
+ sage: p = {x: 1, y: 1}
+ sage: F.asymptotics_smooth(p, alpha, 5, var('r'), verbose=True) # not tested (140 seconds)
+ Creating auxiliary functions...
+ Computing derivatives of auxiallary functions...
+ Computing derivatives of more auxiliary functions...
+ Computing second order differential operator actions...
+ (1/12*sqrt(3)*2^(2/3)*gamma(1/3)/(pi*r^(1/3))
+ - 1/96*sqrt(3)*2^(1/3)*gamma(2/3)/(pi*r^(5/3)),
+ 1,
+ 1/12*sqrt(3)*2^(2/3)*gamma(1/3)/(pi*r^(1/3))
+ - 1/96*sqrt(3)*2^(1/3)*gamma(2/3)/(pi*r^(5/3)))
+ """
+ from sage.calculus.functions import jacobian
+ from sage.calculus.var import function
+ from sage.functions.other import factorial, gamma, sqrt
+ from sage.functions.log import exp, log
+ from sage.matrix.constructor import matrix
+ from sage.modules.free_module_element import vector
+ from sage.symbolic.constants import pi
+ from sage.symbolic.relation import solve
+ from sage.rings.all import CC
+ from sage.rings.rational_field import QQ
+
+ R = self.denominator_ring
+ d = self.dimension()
+ I = sqrt(-ZZ.one())
+ # Coerce everything into the Symbolic Ring.
+ X = [SR(x) for x in R.gens()]
+ G = SR(self.numerator())
+ H = SR(self.denominator())
+ p = {SR(x): p[x] for x in R.gens()}
+ alpha = [SR(a) for a in alpha]
+
+ # Put given convenient coordinate at end of variable list.
+ if coordinate is not None:
+ x = X.pop(coordinate)
+ X.append(x)
+ a = alpha.pop(coordinate)
+ alpha.append(a)
+
+ # Deal with the simple univariate case first.
+ # Same as the multiple point case with n == d.
+ # but with a negative sign.
+ # I'll just past the code from the multiple point case.
+ if d == 1:
+ det = jacobian(H, X).subs(p).determinant().abs()
+ exp_scale = prod([(p[X[i]] ** (-alpha[i])).subs(p)
+ for i in xrange(d)])
+ subexp_part = -G.subs(p) / (det * prod(p.values()))
+ if numerical:
+ exp_scale = exp_scale.n(digits=numerical)
+ subexp_part = subexp_part.n(digits=numerical)
+ return (exp_scale ** asy_var * subexp_part, exp_scale, subexp_part)
+
+ # If p is a tuple of rationals, then compute with it directly.
+ # Otherwise, compute symbolically and plug in p at the end.
+ if vector(p.values()) in QQ ** d:
+ P = p
+ else:
+ sP = [var('p' + str(j)) for j in xrange(d)]
+ P = {X[j]: sP[j] for j in xrange(d)}
+ p = {sP[j]: p[X[j]] for j in xrange(d)}
+
+ # Setup.
+ if verbose:
+ print("Creating auxiliary functions...")
+ # Implicit functions.
+ h = function('h')(*tuple(X[:d - 1]))
+ U = function('U')(*tuple(X))
+ # All other functions are defined in terms of h, U, and
+ # explicit functions.
+ Gcheck = -G / U * (h / X[d - 1])
+ A = Gcheck.subs({X[d - 1]: ZZ.one() / h}) / h
+ t = 't'
+ L = [str(elt) for elt in X]
+ while t in L:
+ t = t + 't'
+ T = [var(t + str(i)) for i in xrange(d - 1)]
+ e = {X[i]: P[X[i]] * exp(I * T[i]) for i in xrange(d - 1)}
+ ht = h.subs(e)
+ At = A.subs(e)
+ Phit = (-log(P[X[d - 1]] * ht) +
+ I * sum([alpha[i] / alpha[d - 1] * T[i]
+ for i in xrange(d - 1)]))
+ Tstar = {t: ZZ.zero() for t in T}
+ # Store h and U and all their derivatives evaluated at P.
+ atP = P.copy()
+ atP.update({h.subs(P): ZZ.one() / P[X[d - 1]]})
+
+ # Compute the derivatives of h up to order 2 * N, evaluate at P,
+ # and store in atP.
+ # Keep a copy of unevaluated h derivatives for use in the case
+ # d = 2 and v > 2 below.
+ hderivs1 = {} # First derivatives of h.
+ for i in xrange(d - 1):
+ s = solve(diff(H.subs({X[d - 1]: ZZ.one() / h}), X[i]),
+ diff(h, X[i]))[0].rhs().simplify()
+ hderivs1.update({diff(h, X[i]): s})
+ atP.update({diff(h, X[i]).subs(P): s.subs(P).subs(atP)})
+ hderivs = diff_all(h, X[0: d - 1], 2 * N, sub=hderivs1, rekey=h)
+ for k in hderivs.keys():
+ atP.update({k.subs(P): hderivs[k].subs(atP)})
+
+ # Compute the derivatives of U up to order 2 * N and evaluate at P.
+ # To do this, differentiate H = U*Hcheck over and over, evaluate at P,
+ # and solve for the derivatives of U at P.
+ # Need the derivatives of H with short keys to pass on
+ # to diff_prod later.
+ Hderivs = diff_all(H, X, 2 * N, ending=[X[d - 1]], sub_final=P)
+ if verbose:
+ print("Computing derivatives of auxiallary functions...")
+ # For convenience in checking if all the nontrivial derivatives of U
+ # at p are zero a few line below, store the value of U(p) in atP
+ # instead of in Uderivs.
+ Uderivs = {}
+ atP.update({U.subs(P): diff(H, X[d - 1]).subs(P)})
+ end = [X[d - 1]]
+ Hcheck = X[d - 1] - ZZ.one() / h
+ k = H.polynomial(CC).degree() - 1
+ if k == 0:
+ # Then we can conclude that all higher derivatives of U are zero.
+ for l in xrange(1, 2 * N + 1):
+ for s in combinations_with_replacement(X, l):
+ Uderivs[diff(U, list(s)).subs(P)] = ZZ.zero()
+ elif k > 0 and k < 2 * N:
+ all_zero = True
+ Uderivs = diff_prod(Hderivs, U, Hcheck, X,
+ range(1, k + 1), end, Uderivs, atP)
+ # Check for a nonzero U derivative.
+ if any(u for u in Uderivs.values()):
+ all_zero = False
+ if all_zero:
+ # Then, using a proposition at the end of [RaWi2012], we can
+ # conclude that all higher derivatives of U are zero.
+ for l in xrange(k + 1, 2 * N + 1):
+ for s in combinations_with_replacement(X, l):
+ Uderivs.update({diff(U, list(s)).subs(P): ZZ.zero()})
+ else:
+ # Have to compute the rest of the derivatives.
+ Uderivs = diff_prod(Hderivs, U, Hcheck, X,
+ range(k + 1, 2 * N + 1), end, Uderivs, atP)
+ else:
+ Uderivs = diff_prod(Hderivs, U, Hcheck, X,
+ range(1, 2 * N + 1), end, Uderivs, atP)
+ atP.update(Uderivs)
+
+ # In general, this algorithm is not designed to handle the case of a
+ # singular Phit''(Tstar).
+ # However, when d = 2 the algorithm can cope.
+ if d == 2:
+ # Compute v, the order of vanishing at Tstar of Phit.
+ # It is at least 2.
+ v = Integer(2)
+ Phitderiv = diff(Phit, T[0], 2)
+ splat = Phitderiv.subs(Tstar).subs(atP).subs(p).simplify()
+ while splat == 0:
+ v += 1
+ if v > 2 * N:
+ # Then need to compute more derivatives of h for atP.
+ hderivs.update({diff(h, X[0], v):
+ diff(hderivs[diff(h, X[0], v - 1)],
+ X[0]).subs(hderivs1)})
+ atP.update({diff(h, X[0], v).subs(P):
+ hderivs[diff(h, X[0], v)].subs(atP)})
+ Phitderiv = diff(Phitderiv, T[0])
+ splat = Phitderiv.subs(Tstar).subs(atP).subs(p).simplify()
+
+ if d == 2 and v > 2:
+ t = T[0] # Simplify variable names.
+ a = splat / factorial(v)
+ Phitu = Phit - a * t ** v
+
+ # Compute all partial derivatives of At and Phitu
+ # up to orders 2*(N - 1) and 2*(N - 1) + v, respectively,
+ # in case v is even.
+ # Otherwise, compute up to orders N - 1 and N - 1 + v,
+ # respectively.
+ # To speed up later computations,
+ # create symbolic functions AA and BB
+ # to stand in for the expressions At and Phitu, respectively.
+ if verbose:
+ print("Computing derivatives of more auxiliary functions...")
+ AA = function('AA')(t)
+ BB = function('BB')(t)
+ if v.mod(2) == 0:
+ At_derivs = diff_all(At, T, 2 * N - 2, sub=hderivs1,
+ sub_final=[Tstar, atP], rekey=AA)
+ Phitu_derivs = diff_all(Phitu, T, 2 * N - 2 +v,
+ sub=hderivs1, sub_final=[Tstar, atP],
+ zero_order=v + 1, rekey=BB)
+ else:
+ At_derivs = diff_all(At, T, N - 1, sub=hderivs1,
+ sub_final=[Tstar, atP], rekey=AA)
+ Phitu_derivs = diff_all(Phitu, T, N - 1 + v,
+ sub=hderivs1, sub_final=[Tstar, atP],
+ zero_order=v + 1 , rekey=BB)
+ AABB_derivs = At_derivs
+ AABB_derivs.update(Phitu_derivs)
+ AABB_derivs[AA] = At.subs(Tstar).subs(atP)
+ AABB_derivs[BB] = Phitu.subs(Tstar).subs(atP)
+ if verbose:
+ print("Computing second order differential operator actions...")
+ DD = diff_op_simple(AA, BB, AABB_derivs, t, v, a, N)
+
+ # Plug above into asymptotic formula.
+ L = []
+ if v.mod(2) == 0:
+ for k in xrange(N):
+ L.append(sum([(-1) ** l * gamma((2 * k + v * l + 1) / v) /
+ (factorial(l) * factorial(2 * k + v * l)) *
+ DD[(k, l)] for l in xrange(0, 2 * k + 1)]))
+ chunk = (a ** (-1 / v) / (pi * v) *
+ sum([alpha[d - 1] ** (-(2 * k + 1) / v) *
+ L[k] * asy_var ** (-(2 * k + 1) / v)
+ for k in xrange(N)]))
+ else:
+ zeta = exp(I * pi / (2 * v))
+ for k in xrange(N):
+ L.append(sum([(-1) ** l * gamma((k + v * l + 1) / v) /
+ (factorial(l) * factorial(k + v * l)) *
+ (zeta ** (k + v * l + 1) +
+ (-1) ** (k + v * l) *
+ zeta ** (-(k + v * l + 1))) *
+ DD[(k, l)] for l in xrange(0, k + 1)]))
+ chunk = (abs(a) ** (-1 / v) / (2 * pi * v) *
+ sum([alpha[d - 1] ** (-(k + 1) / v) *
+ L[k] * asy_var ** (-(k + 1) / v)
+ for k in xrange(N)]))
+
+ # Asymptotics for d >= 2 case.
+ # A singular Phit''(Tstar) will cause a crash in this case.
+ else:
+ Phit1 = jacobian(Phit, T).subs(hderivs1)
+ a = jacobian(Phit1, T).subs(hderivs1).subs(Tstar).subs(atP)
+ a_inv = a.inverse()
+ Phitu = (Phit - (1 / QQ(2)) * matrix([T]) *
+ a * matrix([T]).transpose())
+ Phitu = Phitu[0][0]
+ # Compute all partial derivatives of At and Phitu up to
+ # orders 2 * N-2 and 2 * N, respectively.
+ # Take advantage of the fact that At and Phitu
+ # are sufficiently differentiable functions so that mixed partials
+ # are equal. Thus only need to compute representative partials.
+ # Choose nondecreasing sequences as representative differentiation-
+ # order sequences.
+ # To speed up later computations,
+ # create symbolic functions AA and BB
+ # to stand in for the expressions At and Phitu, respectively.
+ if verbose:
+ print("Computing derivatives of more auxiliary functions...")
+ AA = function('AA')(*tuple(T))
+ At_derivs = diff_all(At, T, 2 * N - 2, sub=hderivs1,
+ sub_final=[Tstar, atP], rekey=AA)
+ BB = function('BB')(*tuple(T))
+ Phitu_derivs = diff_all(Phitu, T, 2 * N, sub=hderivs1,
+ sub_final=[Tstar, atP], rekey=BB, zero_order=3)
+ AABB_derivs = At_derivs
+ AABB_derivs.update(Phitu_derivs)
+ AABB_derivs[AA] = At.subs(Tstar).subs(atP)
+ AABB_derivs[BB] = Phitu.subs(Tstar).subs(atP)
+ if verbose:
+ print("Computing second order differential operator actions...")
+ DD = diff_op(AA, BB, AABB_derivs, T, a_inv, 1 , N)
+
+ # Plug above into asymptotic formula.
+ L = []
+ for k in xrange(N):
+ L.append(sum([DD[(0, k, l)] / ((-1) ** k * 2 ** (l + k) *
+ factorial(l) * factorial(l + k))
+ for l in xrange(0, 2 * k + 1)]))
+ chunk = sum([(2 * pi) ** ((1 - d) / Integer(2)) *
+ a.determinant() ** (-ZZ.one() / Integer(2)) *
+ alpha[d - 1] ** ((ZZ.one() - d) / Integer(2) - k) *
+ L[k] *
+ asy_var ** ((ZZ.one() - d) / Integer(2) - k)
+ for k in xrange(N)])
+
+ chunk = chunk.subs(p).simplify()
+ coeffs = chunk.coefficients(asy_var)
+ coeffs.reverse()
+ coeffs = coeffs[:N]
+ if numerical:
+ subexp_part = sum([co[0].subs(p).n(digits=numerical) *
+ asy_var ** co[1] for co in coeffs])
+ exp_scale = prod([(P[X[i]] ** (-alpha[i])).subs(p)
+ for i in xrange(d)]).n(digits=numerical)
+ else:
+ subexp_part = sum([co[0].subs(p) * asy_var ** co[1]
+ for co in coeffs])
+ exp_scale = prod([(P[X[i]] ** (-alpha[i])).subs(p)
+ for i in xrange(d)])
+ return (exp_scale ** asy_var * subexp_part, exp_scale, subexp_part)
+
+
+ def asymptotics_multiple(self, p, alpha, N, asy_var, coordinate=None,
+ numerical=0, verbose=False):
+ r"""
+ Return the asymptotics in the given direction of a multiple
+ point nondegenerate for ``alpha``.
+
+ This is the same as :meth:`asymptotics`, but only in the case
+ of a convenient multiple point nondegenerate for ``alpha``.
+ Assume also that ``self.dimension >= 2`` and that the
+ ``p.values()`` are not symbolic variables.
+
+ The formulas used for computing the asymptotic expansion are
+ Theorem 3.4 and Theorem 3.7 of [RaWi2012]_.
+
+ INPUT:
+
+ - ``p`` -- a dictionary with keys that can be coerced to equal
+ ``self.denominator_ring.gens()``
+ - ``alpha`` -- a tuple of length ``d = self.dimension()`` of
+ positive integers or, if `p` is a smooth point,
+ possibly of symbolic variables
+ - ``N`` -- a positive integer
+ - ``asy_var`` -- (optional; default: ``None``) a symbolic variable;
+ the variable of the asymptotic expansion,
+ if none is given, ``var('r')`` will be assigned
+ - ``coordinate`` -- (optional; default: ``None``) an integer in
+ `\{0, \ldots, d-1\}` indicating a convenient coordinate to base
+ the asymptotic calculations on; if ``None`` is assigned, then
+ choose ``coordinate=d-1``
+ - ``numerical`` -- (optional; default: 0) a natural number;
+ if numerical is greater than 0, then return a numerical approximation
+ of the Maclaurin ray coefficients of ``self`` with ``numerical``
+ digits of precision; otherwise return exact values
+ - ``verbose`` -- (default: ``False``) print the current state of
+ the algorithm
+
+ OUTPUT:
+
+ The asymptotic expansion.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y,z>= PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: H = (4 - 2*x - y - z)*(4 - x -2*y - z)
+ sage: Hfac = H.factor()
+ sage: G = 16/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F
+ (16, [(x + 2*y + z - 4, 1), (2*x + y + z - 4, 1)])
+ sage: p = {x: 1, y: 1, z: 1}
+ sage: alpha = [3, 3, 2]
+ sage: F.asymptotics_multiple(p, alpha, 2, var('r'), verbose=True) # long time
+ Creating auxiliary functions...
+ Computing derivatives of auxiliary functions...
+ Computing derivatives of more auxiliary functions...
+ Computing second-order differential operator actions...
+ (4/3*sqrt(3)/(sqrt(pi)*sqrt(r)) - 25/216*sqrt(3)/(sqrt(pi)*r^(3/2)),
+ 1,
+ 4/3*sqrt(3)/(sqrt(pi)*sqrt(r)) - 25/216*sqrt(3)/(sqrt(pi)*r^(3/2)))
+
+ sage: H = (1 - x*(1 + y))*(1 - z*x**2*(1 + 2*y))
+ sage: Hfac = H.factor()
+ sage: G = 1/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F
+ (1, [(x*y + x - 1, 1), (2*x^2*y*z + x^2*z - 1, 1)])
+ sage: p = {x: 1/2, z: 4/3, y: 1}
+ sage: alpha = [8, 3, 3]
+ sage: F.asymptotics_multiple(p, alpha, 2, var('r'), coordinate=1, verbose=True) # long time
+ Creating auxiliary functions...
+ Computing derivatives of auxiliary functions...
+ Computing derivatives of more auxiliary functions...
+ Computing second-order differential operator actions...
+ (1/172872*108^r*(24696*sqrt(7)*sqrt(3)/(sqrt(pi)*sqrt(r))
+ - 1231*sqrt(7)*sqrt(3)/(sqrt(pi)*r^(3/2))),
+ 108,
+ 1/7*sqrt(7)*sqrt(3)/(sqrt(pi)*sqrt(r))
+ - 1231/172872*sqrt(7)*sqrt(3)/(sqrt(pi)*r^(3/2)))
+
+ ::
+
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: H = (1 - 2*x - y) * (1 - x - 2*y)
+ sage: Hfac = H.factor()
+ sage: G = exp(x + y)/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: F
+ (e^(x + y), [(x + 2*y - 1, 1), (2*x + y - 1, 1)])
+ sage: p = {x: 1/3, y: 1/3}
+ sage: alpha = (var('a'), var('b'))
+ sage: F.asymptotics_multiple(p, alpha, 2, var('r')) # long time
+ (3*((1/3)^(-a)*(1/3)^(-b))^r*e^(2/3), (1/3)^(-a)*(1/3)^(-b), 3*e^(2/3))
+ """
+ from itertools import product
+ from sage.calculus.functions import jacobian
+ from sage.calculus.var import function
+ from sage.combinat.combinat import stirling_number1
+ from sage.functions.log import exp, log
+ from sage.functions.other import factorial, sqrt
+ from sage.matrix.constructor import matrix
+ from sage.misc.mrange import xmrange
+ from sage.modules.free_module_element import vector
+ from sage.rings.all import CC
+ from sage.rings.arith import binomial
+ from sage.rings.rational_field import QQ
+ from sage.symbolic.constants import pi
+ from sage.symbolic.relation import solve
+
+ R = self.denominator_ring
+
+ # Coerce keys of p into R.
+ p = coerce_point(R, p)
+
+ d = self.dimension()
+ I = sqrt(-ZZ.one())
+ # Coerce everything into the Symbolic Ring.
+ X = [SR(x) for x in R.gens()]
+ G = SR(self.numerator())
+ H = [SR(h) for (h, e) in self.denominator_factored()]
+ Hprod = prod(H)
+ n = len(H)
+ P = {SR(x): p[x] for x in R.gens()}
+ Sstar = self._crit_cone_combo(p, alpha, coordinate)
+
+ # Put the given convenient variable at end of variable list.
+ if coordinate is not None:
+ x = X.pop(coordinate)
+ X.append(x)
+ a = alpha.pop(coordinate)
+ alpha.append(a)
+
+ # Case n = d.
+ if n == d:
+ det = jacobian(H, X).subs(P).determinant().abs()
+ exp_scale = prod([(P[X[i]] ** (-alpha[i])).subs(P)
+ for i in xrange(d)])
+ subexp_part = G.subs(P) / (det * prod(P.values()))
+ if numerical:
+ exp_scale = exp_scale.n(digits=numerical)
+ subexp_part = subexp_part.n(digits=numerical)
+ return (exp_scale ** asy_var * subexp_part, exp_scale, subexp_part)
+
+ # Case n < d.
+ # If P is a tuple of rationals, then compute with it directly.
+ # Otherwise, compute symbolically and plug in P at the end.
+ if vector(P.values()) not in QQ ** d:
+ sP = [var('p' + str(j)) for j in xrange(d)]
+ P = {X[j]: sP[j] for j in xrange(d)}
+ p = {sP[j]: p[X[j]] for j in xrange(d)}
+
+ # Setup.
+ if verbose:
+ print("Creating auxiliary functions...")
+ # Create T and S variables.
+ t = 't'
+ L = [str(elt) for elt in X]
+ while t in L:
+ t = t + 't'
+ T = [var(t + str(i)) for i in xrange(d - 1)]
+ s = 's'
+ while s in L:
+ s = s + 't'
+ S = [var(s + str(i)) for i in xrange(n - 1)]
+ Sstar = {S[j]: Sstar[j] for j in xrange(n - 1)}
+ thetastar = {t: ZZ.zero() for t in T}
+ thetastar.update(Sstar)
+ # Create implicit functions.
+ h = [function('h' + str(j))(*tuple(X[:d - 1])) for j in xrange(n)]
+ U = function('U')(*tuple(X))
+ # All other functions are defined in terms of h, U, and
+ # explicit functions.
+ Hcheck = prod([X[d - 1] - ZZ.one() / h[j] for j in xrange(n)])
+ Gcheck = -G / U * prod([-h[j] / X[d - 1] for j in xrange(n)])
+ A = [(-1) ** (n - 1) * X[d - 1] ** (-n + j) *
+ diff(Gcheck.subs({X[d - 1]: ZZ.one() / X[d - 1]}), X[d - 1], j)
+ for j in xrange(n)]
+ e = {X[i]: P[X[i]] * exp(I * T[i]) for i in xrange(d - 1)}
+ ht = [hh.subs(e) for hh in h]
+ hsumt = (sum([S[j] * ht[j] for j in xrange(n - 1)]) +
+ (ZZ.one() - sum(S)) * ht[n - 1])
+ At = [AA.subs(e).subs({X[d - 1]: hsumt}) for AA in A]
+ Phit = (-log(P[X[d - 1]] * hsumt) +
+ I * sum([alpha[i] / alpha[d - 1] * T[i]
+ for i in xrange(d - 1)]))
+ # atP Stores h and U and all their derivatives evaluated at C.
+ atP = P.copy()
+ atP.update({hh.subs(P): ZZ.one() / P[X[d - 1]] for hh in h})
+
+ # Compute the derivatives of h up to order 2 * N and evaluate at P.
+ hderivs1 = {} # First derivatives of h.
+ for (i, j) in xmrange([d - 1, n], tuple):
+ s = solve(diff(H[j].subs({X[d - 1]: ZZ.one() / h[j]}), X[i]),
+ diff(h[j], X[i]))[0].rhs().simplify()
+ hderivs1.update({diff(h[j], X[i]): s})
+ atP.update({diff(h[j], X[i]).subs(P): s.subs(P).subs(atP)})
+ hderivs = diff_all(h, X[0:d - 1], 2 * N, sub=hderivs1, rekey=h)
+ for k in hderivs.keys():
+ atP.update({k.subs(P): hderivs[k].subs(atP)})
+
+ # Compute the derivatives of U up to order 2 * N - 2 + min{n, N} - 1 and
+ # evaluate at P.
+ # To do this, differentiate H = U*Hcheck over and over, evaluate at P,
+ # and solve for the derivatives of U at P.
+ # Need the derivatives of H with short keys to pass on to
+ # diff_prod later.
+ if verbose:
+ print("Computing derivatives of auxiliary functions...")
+ m = min(n, N)
+ end = [X[d-1] for j in xrange(n)]
+ Hprodderivs = diff_all(Hprod, X, 2 * N - 2 + n, ending=end, sub_final=P)
+ atP.update({U.subs(P): diff(Hprod, X[d - 1], n).subs(P)/factorial(n)})
+ Uderivs = {}
+ k = Hprod.polynomial(CC).degree() - n
+ if k == 0:
+ # Then we can conclude that all higher derivatives of U are zero.
+ for l in xrange(1, 2 * N - 2 + m):
+ for s in combinations_with_replacement(X, l):
+ Uderivs[diff(U, list(s)).subs(P)] = ZZ.zero()
+ elif k > 0 and k < 2 * N - 2 + m - 1:
+ all_zero = True
+ Uderivs = diff_prod(Hprodderivs, U, Hcheck, X,
+ range(1, k + 1), end, Uderivs, atP)
+ # Check for a nonzero U derivative.
+ if any(u for u in Uderivs.values()):
+ all_zero = False
+ if all_zero:
+ # Then all higher derivatives of U are zero.
+ for l in xrange(k + 1, 2 * N - 2 + m):
+ for s in combinations_with_replacement(X, l):
+ Uderivs.update({diff(U, list(s)).subs(P): ZZ.zero()})
+ else:
+ # Have to compute the rest of the derivatives.
+ Uderivs = diff_prod(Hprodderivs, U, Hcheck, X,
+ range(k + 1, 2 * N - 2 + m), end,
+ Uderivs, atP)
+ else:
+ Uderivs = diff_prod(Hprodderivs, U, Hcheck, X,
+ range(1, 2 * N - 2 + m), end, Uderivs, atP)
+ atP.update(Uderivs)
+ Phit1 = jacobian(Phit, T + S).subs(hderivs1)
+ a = jacobian(Phit1, T + S).subs(hderivs1).subs(thetastar).subs(atP)
+ a_inv = a.inverse()
+ Phitu = (Phit - (1 / Integer(2)) * matrix([T + S]) * a *
+ matrix([T + S]).transpose())
+ Phitu = Phitu[0][0]
+
+ # Compute all partial derivatives of At and Phitu up to orders 2 * N - 2
+ # and 2 * N, respectively. Take advantage of the fact that At and Phitu
+ # are sufficiently differentiable functions so that mixed partials
+ # are equal. Thus only need to compute representative partials.
+ # Choose nondecreasing sequences as representative differentiation-
+ # order sequences.
+ # To speed up later computations, create symbolic functions AA and BB
+ # to stand in for the expressions At and Phitu respectively.
+ if verbose:
+ print("Computing derivatives of more auxiliary functions...")
+ AA = [function('A' + str(j))(*tuple(T + S)) for j in xrange(n)]
+ At_derivs = diff_all(At, T + S, 2 * N - 2, sub=hderivs1,
+ sub_final=[thetastar, atP], rekey=AA)
+ BB = function('BB')(*tuple(T + S))
+ Phitu_derivs = diff_all(Phitu, T + S, 2 * N, sub=hderivs1,
+ sub_final=[thetastar, atP], rekey=BB, zero_order=3)
+ AABB_derivs = At_derivs
+ AABB_derivs.update(Phitu_derivs)
+ for j in xrange(n):
+ AABB_derivs[AA[j]] = At[j].subs(thetastar).subs(atP)
+ AABB_derivs[BB] = Phitu.subs(thetastar).subs(atP)
+
+ if verbose:
+ print("Computing second-order differential operator actions...")
+ DD = diff_op(AA, BB, AABB_derivs, T + S, a_inv, n, N)
+ L = {}
+ for (j, k) in product(xrange(min(n, N)), xrange(max(0, N - 1 - n), N)):
+ if j + k <= N - 1:
+ L[(j, k)] = sum([DD[(j, k, l)] / ((-1) ** k * 2 ** (k + l) *
+ factorial(l) *
+ factorial(k + l))
+ for l in xrange(2 * k + 1)])
+ det = (a.determinant() ** (-1 / Integer(2)) *
+ (2 * pi) ** ((n - d) / Integer(2)))
+ chunk = det * sum([(alpha[d - 1] * asy_var) ** ((n - d) /
+ Integer(2) - q) *
+ sum([L[(j, k)] * binomial(n - 1, j) *
+ stirling_number1(n - j, n + k - q) *
+ (-1) ** (q - j - k)
+ for (j, k) in product(xrange(min(n - 1, q) + 1),
+ xrange(max(0, q - n),
+ q + 1))
+ if j + k <= q])
+ for q in xrange(N)])
+ chunk = chunk.subs(P).simplify()
+ coeffs = chunk.coefficients(asy_var)
+ coeffs.reverse()
+ coeffs = coeffs[:N]
+ if numerical:
+ subexp_part = sum([co[0].subs(p).n(digits=numerical) *
+ asy_var ** co[1]
+ for co in coeffs])
+ exp_scale = prod([(P[X[i]] ** (-alpha[i])).subs(p)
+ for i in xrange(d)]).n(digits=numerical)
+ else:
+ subexp_part = sum([co[0].subs(p) * asy_var ** co[1]
+ for co in coeffs])
+ exp_scale = prod([(P[X[i]] ** (-alpha[i])).subs(p)
+ for i in xrange(d)])
+ return (exp_scale ** asy_var * subexp_part, exp_scale, subexp_part)
+
+
+ def _crit_cone_combo(self, p, alpha, coordinate=None):
+ r"""
+ Return an auxiliary point associated to the multiple
+ point ``p`` of the factors ``self``.
+
+ INPUT:
+
+ - ``p`` -- a dictionary with keys that can be coerced to equal
+ ``self.denominator_ring.gens()``
+ - ``alpha`` -- a list of rationals
+
+ OUTPUT:
+
+ A solution of the matrix equation `y \Gamma = \alpha^{\prime}` for `y`,
+ where `\Gamma` is the matrix given by
+ ``[direction(v) for v in self.log_grads(p)]`` and
+ `\alpha^{\prime}` is ``direction(alpha)``.
+
+ .. SEEALSO::
+
+ :func:`direction`
+
+ .. NOTE::
+
+ For internal use by
+ :meth:`FractionWithFactoredDenominator.asymptotics_multiple()`.
+
+ .. NOTE::
+
+ Use this function only when `\Gamma` is well-defined and
+ there is a unique solution to the matrix equation
+ `y \Gamma = \alpha'`. Fails otherwise.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: p = exp(x)
+ sage: df = [(1 - 2*x - y, 1), (1 - x - 2*y, 1)]
+ sage: f = FFPD(p, df)
+ sage: p = {x: 1/3, y: 1/3}
+ sage: alpha = (var('a'), var('b'))
+ sage: f._crit_cone_combo(p, alpha)
+ [1/3*(2*a - b)/b, -2/3*(a - 2*b)/b]
+ """
+ from sage.matrix.constructor import matrix
+ from sage.symbolic.relation import solve
+
+ # Assuming here that each log_grads(f) has nonzero final component.
+ # Then 'direction' will not throw a division by zero error.
+ R = self.denominator_ring
+
+ # Coerce keys of p into R.
+ p = coerce_point(R, p)
+
+ d = self.dimension()
+ n = len(self.denominator_factored())
+ Gamma = matrix([direction(v, coordinate) for v in self.log_grads(p)])
+ beta = direction(alpha, coordinate)
+ # solve_left() fails when working in SR :-(.
+ # So use solve() instead.
+ # Gamma.solve_left(vector(beta))
+ V = [var('sss' + str(i)) for i in range(n)]
+ M = matrix(V) * Gamma
+ eqns = [M[0][j] == beta[j] for j in range(d)]
+ s = solve(eqns, V, solution_dict=True)[0] # Assume a unique solution.
+ return [s[v] for v in V]
+
+
+ def grads(self, p):
+ r"""
+ Return a list of the gradients of the polynomials
+ ``[q for (q, e) in self.denominator_factored()]`` evalutated at ``p``.
+
+ INPUT:
+
+ - ``p`` -- (optional; default: ``None``) a dictionary whose keys are
+ the generators of ``self.denominator_ring``
+
+ OUTPUT:
+
+ A list.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: p = exp(x)
+ sage: df = [(x^3 + 3*y^2, 5), (x*y, 2), (y, 1)]
+ sage: f = FFPD(p, df)
+ sage: f
+ (e^x, [(y, 1), (x*y, 2), (x^3 + 3*y^2, 5)])
+ sage: R.gens()
+ (x, y)
+ sage: p = None
+ sage: f.grads(p)
+ [(0, 1), (y, x), (3*x^2, 6*y)]
+
+ sage: p = {x: sqrt(2), y: var('a')}
+ sage: f.grads(p)
+ [(0, 1), (a, sqrt(2)), (6, 6*a)]
+ """
+ R = self.denominator_ring
+
+ # Coerce keys of p into R.
+ p = coerce_point(R, p)
+
+ X = R.gens()
+ d = self.dimension()
+ H = [h for (h, e) in self.denominator_factored()]
+ n = len(H)
+ return [tuple([diff(H[i], X[j]).subs(p) for j in xrange(d)])
+ for i in xrange(n)]
+
+
+ def log_grads(self, p):
+ r"""
+ Return a list of the logarithmic gradients of the polynomials
+ ``[q for (q, e) in self.denominator_factored()]`` evalutated at ``p``.
+
+ The logarithmic gradient of a function `f` at point `p` is the
+ vector `(x_1 \partial_1 f(x), \ldots, x_d \partial_d f(x) )`
+ evaluated at `p`.
+
+ INPUT:
+
+ - ``p`` -- (optional; default: ``None``) a dictionary whose keys
+ are the generators of ``self.denominator_ring``
+
+ OUTPUT:
+
+ A list.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R, SR)
+ sage: p = exp(x)
+ sage: df = [(x^3 + 3*y^2, 5), (x*y, 2), (y, 1)]
+ sage: f = FFPD(p, df)
+ sage: f
+ (e^x, [(y, 1), (x*y, 2), (x^3 + 3*y^2, 5)])
+ sage: R.gens()
+ (x, y)
+ sage: p = None
+ sage: f.log_grads(p)
+ [(0, y), (x*y, x*y), (3*x^3, 6*y^2)]
+
+ sage: p = {x: sqrt(2), y: var('a')}
+ sage: f.log_grads(p)
+ [(0, a), (sqrt(2)*a, sqrt(2)*a), (6*sqrt(2), 6*a^2)]
+ """
+ R = self.denominator_ring
+
+ # Coerce keys of p into R.
+ p = coerce_point(R, p)
+
+ X = R.gens()
+ d = self.dimension()
+ H = [h for (h, e) in self.denominator_factored()]
+ n = len(H)
+ return [tuple([(X[j] * diff(H[i], X[j])).subs(p) for j in xrange(d)])
+ for i in xrange(n)]
+
+
+ def critical_cone(self, p, coordinate=None):
+ r"""
+ Return the critical cone of the convenient multiple point ``p``.
+
+ INPUT:
+
+ - ``p`` -- a dictionary with keys that can be coerced to equal
+ ``self.denominator_ring.gens()`` and values in a field
+ - ``coordinate`` -- (optional; default: ``None``) a natural number
+
+ OUTPUT:
+
+ A list of vectors.
+
+ This list of vectors generate the critical cone of ``p`` and
+ the cone itself, which is ``None`` if the values of ``p`` don't lie in
+ `\QQ`. Divide logarithmic gradients by their component ``coordinate``
+ entries. If ``coordinate = None``, then search from `d-1` down to 0
+ for the first index ``j`` such that for all ``i`` we have
+ ``self.log_grads()[i][j] != 0`` and set ``coordinate = j``.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y,z> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: G = 1
+ sage: H = (1 - x*(1 + y)) * (1 - z*x**2*(1 + 2*y))
+ sage: Hfac = H.factor()
+ sage: G = 1/Hfac.unit()
+ sage: F = FFPD(G, Hfac)
+ sage: p = {x: 1/2, y: 1, z: 4/3}
+ sage: F.critical_cone(p)
+ ([(2, 1, 0), (3, 1, 3/2)], 2-d cone in 3-d lattice N)
+ """
+ from sage.geometry.cone import Cone
+
+ R = self.denominator_ring
+
+ # Coerce keys of p into R.
+ p = coerce_point(R, p)
+
+ d = self.dimension()
+ lg = self.log_grads(p)
+ n = len(lg)
+ if coordinate not in xrange(d):
+ # Search from d-1 down to 0 for a coordinate j such that
+ # for all i we have lg[i][j] != 0.
+ # One is guaranteed to exist in the case of a convenient multiple
+ # point.
+ for j in reversed(xrange(d)):
+ if 0 not in [lg[i][j] for i in xrange(n)]:
+ coordinate = j
+ break
+ Gamma = [direction(v, coordinate) for v in lg]
+ try:
+ cone = Cone(Gamma)
+ except TypeError:
+ cone = None
+ return (Gamma, cone)
+
+
+ def is_convenient_multiple_point(self, p):
+ r"""
+ Tests if ``p`` is a convenient multiple point of ``self``.
+
+ In case ``p`` is a convenient multiple point, ``verdict = True`` and
+ ``comment`` is a string stating which variables it's convenient to use.
+ In case ``p`` is not, ``verdict = False`` and ``comment`` is a string
+ explaining why ``p`` fails to be a convenient multiple point.
+
+ See [RaWi2012]_ for more details.
+
+ INPUT:
+
+ - ``p`` -- a dictionary with keys that can be coerced to equal
+ ``self.denominator_ring.gens()``
+
+ OUTPUT:
+
+ A pair ``(verdict, comment)``.
+
+ EXAMPLES::
+
+ sage: from sage.rings.asymptotic.asymptotics_multivariate_generating_functions import FractionWithFactoredDenominatorRing
+ sage: R.<x,y,z> = PolynomialRing(QQ)
+ sage: FFPD = FractionWithFactoredDenominatorRing(R)
+ sage: H = (1 - x*(1 + y)) * (1 - z*x**2*(1 + 2*y))
+ sage: df = H.factor()
+ sage: G = 1 / df.unit()
+ sage: F = FFPD(G, df)
+ sage: p1 = {x: 1/2, y: 1, z: 4/3}
+ sage: p2 = {x: 1, y: 2, z: 1/2}
+ sage: F.is_convenient_multiple_point(p1)
+ (True, 'convenient in variables [x, y]')
+ sage: F.is_convenient_multiple_point(p2)
+ (False, 'not a singular point')
+ """
+ from sage.combinat.subset import Subsets
+ from sage.matrix.constructor import matrix
+
+ R = self.denominator_ring
+
+ # Coerce keys of p into R.
+ p = coerce_point(R,