Processing math: 100%

Details on the Hypergeometric Function Expansion Module

This page describes how the function hyperexpand() and related code work. For usage, see the documentation of the symplify module.

Hypergeometric Function Expansion Algorithm

This section describes the algorithm used to expand hypergeometric functions. Most of it is based on the papers [Roach1996] and [Roach1997].

Recall that the hypergeometric function is (initially) defined as

pFq(a1,,apb1,,bq|z)=n=0(a1)n(ap)n(b1)n(bq)nznn!.

It turns out that there are certain differential operators that can change the ap and pq parameters by integers. If a sequence of such operators is known that converts the set of indices a0r and b0s into ap and bq, then we shall say the pair ap,bq is reachable from a0r,b0s. Our general strategy is thus as follows: given a set ap,bq of parameters, try to look up an origin a0r,b0s for which we know an expression, and then apply the sequence of differential operators to the known expression to find an expression for the Hypergeometric function we are interested in.

Notation

In the following, the symbol a will always denote a numerator parameter and the symbol b will always denote a denominator parameter. The subscripts p,q,r,s denote vectors of that length, so e.g. ap denotes a vector of p numerator parameters. The subscripts i and j denote “running indices”, so they should usually be used in conjuction with a “for all i”. E.g. ai<4 for all i. Uppercase subscripts I and J denote a chosen, fixed index. So for example aI>0 is true if the inequality holds for the one index I we are currently interested in.

Incrementing and decrementing indices

Suppose ai0. Set A(ai)=zaiddz+1. It is then easy to show that A(ai)pFq(apbq|z)=pFq(ap+eibq|z), where ei is the i-th unit vector. Similarly for bj1 we set B(bj)=zbj1ddz+1 and find B(bj)pFq(apbq|z)=pFq(apbqei|z). Thus we can increment upper and decrement lower indices at will, as long as we don’t go through zero. The A(ai) and B(bj) are called shift operators.

It is also easy to show that ddzpFq(apbq|z)=a1apb1bqpFq(ap+1bq+1|z), where ap+1 is the vector a1+1,a2+1, and similarly for bq+1. Combining this with the shift operators, we arrive at one form of the Hypergeometric differential equation: [ddzqj=1B(bj)a1ap(b11)(bq1)pi=1A(ai)]pFq(apbq|z)=0. This holds if all shift operators are defined, i.e. if no ai=0 and no bj=1. Clearing denominators and multiplying through by z we arrive at the following equation: [zddzqj=1(zddz+bj1)zpi=1(zddz+ai)]pFq(apbq|z)=0. Even though our derivation does not show it, it can be checked that this equation holds whenever the pFq is defined.

Notice that, under suitable conditions on aI,bJ, each of the operators A(ai), B(bj) and zddz can be expressed in terms of A(aI) or B(bJ). Our next aim is to write the Hypergeometric differential equation as follows: [XA(aI)r]pFq(apbq|z)=0, for some operator X and some constant r to be determined. If r0, then we can write this as 1rXpFq(ap+eIbq|z)=pFq(apbq|z), and so 1rX undoes the shifting of A(aI), whence it will be called an inverse-shift operator.

Now A(aI) exists if aI0, and then zddz=aIA(aI)aI. Observe also that all the operators A(ai), B(bj) and zddz commute. We have pi=1(zddz+ai)=(pi=1,iI(zddz+ai))aIA(aI), so this gives us the first half of X. The other half does not have such a nice expression. We find zddzqj=1(zddz+bj1)=(aIA(aI)aI)qj=1(aIA(aI)aI+bj1). Since the first half had no constant term, we infer r=aIqj=1(bj1aI).

This tells us under which conditions we can “un-shift” A(aI), namely when aI0 and r0. Substituting aI1 for aI then tells us under what conditions we can decrement the index aI. Doing a similar analysis for B(aJ), we arrive at the following rules:

  • An index aI can be decremented if aI1 and aIbj for all bj.
  • An index bJ can be incremented if bJ1 and bJai for all ai.

Combined with the conditions (stated above) for the existence of shift operators, we have thus established the rules of the game!

Reduction of Order

Notice that, quite trivially, if aI=bJ, we have pFq(apbq|z)=p1Fq1(apbq|z), where ap means ap with aI omitted, and similarly for bq. We call this reduction of order.

In fact, we can do even better. If aIbJZ>0, then it is easy to see that (aI)n(bJ)n is actually a polynomial in n. It is also easy to see that (zddz)kzn=nkzn. Combining these two remarks we find:

If aIbJZ>0, then there exists a polynomial p(n)=p0+p1n+ (of degree aIbJ) such that (aI)n(bJ)n=p(n) and pFq(apbq|z)=(p0+p1zddz+p2(zddz)2+)p1Fq1(apbq|z).

Thus any set of parameters ap,bq is reachable from a set of parameters cr,ds where cidjZ implies ci<dj. Such a set of parameters cr,ds is called suitable. Our database of known formulae should only contain suitable origins. The reasons are twofold: firstly, working from suitable origins is easier, and secondly, a formula for a non-suitable origin can be deduced from a lower order formula, and we should put this one into the database instead.

Moving Around in the Parameter Space

It remains to investigate the following question: suppose ap,bq and a0p,b0q are both suitable, and also aia0iZ, bjb0jZ. When is ap,bq reachable from a0p,b0q? It is clear that we can treat all parameters independently that are incongruent mod 1. So assume that ai and bj are congruent to r mod 1, for all i and j. The same then follows for a0i and b0j.

If r0, then any such ap,bq is reachable from any a0p,b0q. To see this notice that there exist constants c,c0, congruent mod 1, such that ai<c<bj for all i and j, and similarly a0i<c0<b0j. If n=cc0>0 then we first inverse-shift all the b0j n times up, and then similarly shift shift up all the a0i n times. If n<0 then we first inverse-shift down the a0i and then shift down the b0j. This reduces to the case c=c0. But evidently we can now shift or inverse-shift around the a0i arbitrarily so long as we keep them less than c, and similarly for the b0j so long as we keep them bigger than c. Thus ap,bq is reachable from a0p,b0q.

If r=0 then the problem is slightly more involved. WLOG no parameter is zero. We now have one additional complication: no parameter can ever move through zero. Hence ap,bq is reachable from a0p,b0q if and only if the number of ai<0 equals the number of a0i<0, and similarly for the bi and b0i. But in a suitable set of parameters, all bj>0! This is because the Hypergeometric function is undefined if one of the bj is a non-positive integer and all ai are smaller than the bj. Hence the number of bj0 is always zero.

We can thus associate to every suitable set of parameters ap,bq, where no ai=0, the following invariants:

  • For every r[0,1) the number αr of parameters air(mod1), and similarly the number βr of parameters bir(mod1).
  • The number γ of integers ai with ai<0.

The above reasoning shows that ap,bq is reachable from a0p,b0q if and only if the invariants αr,βr,γ all agree. Thus in particular “being reachable from” is a symmetric relation on suitable parameters without zeros.

Applying the Operators

If all goes well then for a given set of parameters we find an origin in our database for which we have a nice formula. We now have to apply (potentially) many differential operators to it. If we do this blindly then the result will be very messy. This is because with Hypergeometric type functions, the derivative is usually expressed as a sum of two contiguous functions. Hence if we compute N derivatives, then the answer will involve 2N contiguous functions! This is clearly undesirable. In fact we know from the Hypergeometric differential equation that we need at most max(p,q+1) contiguous functions to express all derivatives.

Hence instead of differentiating blindly, we will work with a C(z)-module basis: for an origin a0r,b0s we either store (for particularly pretty answers) or compute a set of N functions (typically N=max(r,s+1)) with the property that the derivative of any of them is a C(z)-linear combination of them. In formulae, we store a vector B of N functions, a matrix M and a vector C (the latter two with entries in C(z)), with the following properties:

  • rFs(a0rb0s|z)=CB
  • zddzB=MB.

Then we can compute as many derivatives as we want and we will always end up with C(z)-linear combination of at most N special functions.

As hinted above, B, M and C can either all be stored (for particularly pretty answers) or computed from a single pFq formula.

Loose Ends

This describes the bulk of the hypergeometric function algorithm. There a few further tricks, described in the hyperexpand.py source file. The extension to Meijer G-functions is also described there.

Meijer G-Functions of Finite Confluence

Slater’s theorem essentially evaluates a G-function as a sum of residues. If all poles are simple, the resulting series can be recognised as hypergeometric series. Thus a G-function can be evaluated as a sum of Hypergeometric functions.

If the poles are not simple, the resulting series are not hypergeometric. This is known as the “confluent” or “logarithmic” case (the latter because the resulting series tend to contain logarithms). The answer depends in a complicated way on the multiplicities of various poles, and there is no accepted notation for representing it (as far as I know). However if there are only finitely many multiple poles, we can evaluate the G function as a sum of hypergeometric functions, plus finitely many extra terms. I could not find any good reference for this, which is why I work it out here.

Recall the general setup. We define

G(z)=12πiLmj=1Γ(bjs)nj=1Γ(1aj+s)qj=m+1Γ(1bj+s)pj=n+1Γ(ajs)zsds,

where L is a contour starting and ending at +, enclosing all of the poles of Γ(bjs) for j=1,,n once in the negative direction, and no other poles. Also the integral is assumed absolutely convergent.

In what follows, for any complex numbers a,b, we write ab(mod1) if and only if there exists an integer k such that ab=k. Thus there are double poles iff aiaj(mod1) for some ijn.

We now assume that whenever bjai(mod1) for im, j>n then bj<ai. This means that no quotient of the relevant gamma functions is a polynomial, and can always be achieved by “reduction of order”. Fix a complex number c such that {bi|bic(mod1),im} is not empty. Enumerate this set as b,b+k1,,b+ku, with ki non-negative integers. Enumerate similarly {aj|ajc(mod1),j>n} as b+l1,,b+lv. Then li>kj for all i,j. For finite confluence, we need to assume vu for all such c.

Let c1,,cw be distinct (mod1) and exhaust the congruence classes of the bi. I claim

G(z)=wj=1(Fj(z)+Rj(z)),

where Fj(z) is a hypergeometric function and Rj(z) is a finite sum, both to be specified later. Indeed corresponding to every cj there is a sequence of poles, at mostly finitely many of them multiple poles. This is where the j-th term comes from.

Hence fix again c, enumerate the relevant bi as b,b+k1,,b+ku. We will look at the aj corresponding to a+l1,,a+lu. The other ai are not treated specially. The corresponding gamma functions have poles at (potentially) s=b+r for r=0,1,. For rlu, pole of the integrand is simple. We thus set

R(z)=lu1r=0ress=r+b.

We finally need to investigate the other poles. Set r=lu+t, t0. A computation shows

Γ(kilut)Γ(lilut)=1(kilut)liki=(1)δi(luli+1)δi(luli+1)t(luki+1)t,

where δi=liki.

Also

Γ(bjlubt)=Γ(bjlub)(1)t(lu+b+1bj)t,
Γ(1aj+lu+b+t)=Γ(1aj+lu+b)(1aj+lu+b)t

and

ress=b+lu+tΓ(bs)=(1)lu+t(lu+t)!=(1)lulu!(1)t(lu+1)t.

Hence

ress=b+lu+t=zb+lu(1)lulu!ui=1(1)δi(luki+1)δinj=1Γ(1aj+lu+b)mj=1Γ(bjlub)pj=n+1Γ(ajlub)qj=m+1Γ(1bj+lu+b)×zt(1)t(lu+1)tui=1(luli+1)t(luki+1)tnj=1(1aj+lu+b)tpj=n+1(1)t(lu+b+1aj)tmj=1(1)t(lu+b+1bj)tqj=m+1(1bj+lu+b)t,

where the means to omit the terms we treated specially.

We thus arrive at

F(z)=C×p+1Fq(1,(1+luli),(1+lu+bai)1+lu,(1+luki),(1+lu+bbi)|(1)pmnz),

where C designates the factor in the residue independent of t. (This result can also be written in slightly simpler form by converting all the lu etc back to ab, but doing so is going to require more notation still and is not helpful for computation.)

Extending The Hypergeometric Tables

Adding new formulae to the tables is straightforward. At the top of the file sympy/simplify/hyperexpand.py, there is a function called add_formulae(). Nested in it are defined two helpers, add(ap, bq, res) and addb(ap, bq, B, C, M), as well as dummys a, b, c, and z.

The first step in adding a new formula is by using add(ap, bq, res). This declares hyper(ap, bq, z) == res. Here ap and bq may use the dummys a, b, and c as free symbols. For example the well-known formula 0(a)nznn!=(1z)a is declared by the following line: add((-a, ), (), (1-z)**a).

From the information provided, the matrices B, C and M will be computed, and the formula is now available when expanding hypergeometric functions. Next the test file sympy/simplify/tests/test_hyperexpand.py should be run, in particular the test test_formulae(). This will test the newly added formula numerically. If it fails, there is (presumably) a typo in what was entered.

Since all newly-added formulae are probably relatively complicated, chances are that the automatically computed basis is rather suboptimal (there is no good way of testing this, other than observing very messy output). In this case the matrices B, C and M should be computed by hand. Then the helper addb can be used to declare a hypergeometric formula with hand-computed basis.

An example

Because this explanation so far might be very theoretical and difficult to understand, we walk through an explicit example now. We take the Fresnel function C(z) which obeys the following hypergeometric representation:

C(z)=z1F2(1412,54|π2z416).

First we try to add this formula to the lookup table by using the (simpler) function add(ap, bq, res). The first two arguments are simply the lists containing the parameter sets of 1F2. The res argument is a little bit more complicated. We only know C(z) in terms of 1F2(|f(z)) with f a function of z, in our case

f(z)=π2z416.

What we need is a formula where the hypergeometric function has only z as argument 1F2(|z). We introduce the new complex symbol w and search for a function g(w) such that

f(g(w))=w

holds. Then we can replace every z in C(z) by g(w). In the case of our example the function g could look like

g(w)=2πexp(iπ4)w14.

We get these functions mainly by guessing and testing the result. Hence we proceed by computing f(g(w)) (and simplifying naively)

f(g(w))=π2g(w)416=π2g(2πexp(iπ4)w14)416=π224π4exp(iπ4)4w14416=exp(iπ)w=w

and indeed get back w. (In case of branched functions we have to be aware of branch cuts. In that case we take w to be a positive real number and check the formula. If what we have found works for positive w, then just replace exp() inside any branched function by exp_polar() and what we get is right for all w.) Hence we can write the formula as

C(g(w))=g(w)1F2(1412,54|w).

and trivially

1F2(1412,54|w)=C(g(w))g(w)=C(2πexp(iπ4)w14)2πexp(iπ4)w14

which is exactly what is needed for the third paramenter, res, in add. Finally, the whole function call to add this rule to the table looks like:

add([S(1)/4],
    [S(1)/2, S(5)/4],
    fresnelc(exp(pi*I/4)*root(z,4)*2/sqrt(pi)) / (exp(pi*I/4)*root(z,4)*2/sqrt(pi))
   )

Using this rule we will find that it works but the results are not really nice in terms of simplicity and number of special function instances included. We can obtain much better results by adding the formula to the lookup table in another way. For this we use the (more complicated) function addb(ap, bq, B, C, M). The first two arguments are again the lists containing the parameter sets of 1F2. The remaining three are the matrices mentioned earlier on this page.

We know that the n=max(p,q+1)-th derivative can be expressed as a linear combination of lower order derivatives. The matrix B contains the basis {B0,B1,} and is of shape n×1. The best way to get Bi is to take the first n=max(p,q+1) derivatives of the expression for pFq and take out usefull pieces. In our case we find that n=max(1,2+1)=3. For computing the derivatives, we have to use the operator zddz. The first basis element B0 is set to the expression for 1F2 from above:

B0=πexp(ıπ4)C(2πexp(ıπ4)z14)2z14

Next we compute zddzB0. For this we can directly use SymPy!

>>> from sympy import Symbol, sqrt, exp, I, pi, fresnelc, root, diff, expand
>>> z = Symbol("z")
>>> B0 = sqrt(pi)*exp(-I*pi/4)*fresnelc(2*root(z,4)*exp(I*pi/4)/sqrt(pi))/\
...          (2*root(z,4))
>>> z * diff(B0, z)
z*(cosh(2*sqrt(z))/(4*z) - sqrt(pi)*exp(-I*pi/4)*fresnelc(2*z**(1/4)*exp(I*pi/4)/sqrt(pi))/(8*z**(5/4)))
>>> expand(_)
cosh(2*sqrt(z))/4 - sqrt(pi)*exp(-I*pi/4)*fresnelc(2*z**(1/4)*exp(I*pi/4)/sqrt(pi))/(8*z**(1/4))

Formatting this result nicely we obtain

B1=14πexp(ıπ4)C(2πexp(ıπ4)z14)2z14+14cosh(2z)

Computing the second derivative we find

>>> from sympy import (Symbol, cosh, sqrt, pi, exp, I, fresnelc, root,
...                    diff, expand)
>>> z = Symbol("z")
>>> B1prime = cosh(2*sqrt(z))/4 - sqrt(pi)*exp(-I*pi/4)*\
...           fresnelc(2*root(z,4)*exp(I*pi/4)/sqrt(pi))/(8*root(z,4))
>>> z * diff(B1prime, z)
z*(-cosh(2*sqrt(z))/(16*z) + sinh(2*sqrt(z))/(4*sqrt(z)) + sqrt(pi)*exp(-I*pi/4)*fresnelc(2*z**(1/4)*exp(I*pi/4)/sqrt(pi))/(32*z**(5/4)))
>>> expand(_)
sqrt(z)*sinh(2*sqrt(z))/4 - cosh(2*sqrt(z))/16 + sqrt(pi)*exp(-I*pi/4)*fresnelc(2*z**(1/4)*exp(I*pi/4)/sqrt(pi))/(32*z**(1/4))

which can be printed as

B2=116πexp(ıπ4)C(2πexp(ıπ4)z14)2z14116cosh(2z)+14sinh(2z)z

We see the common pattern and can collect the pieces. Hence it makes sense to choose B1 and B2 as follows

B=(B0B1B2)=(πexp(ıπ4)C(2πexp(ıπ4)z14)2z14cosh(2z)sinh(2z)z)

(This is in contrast to the basis B=(B0,B1,B2) that would have been computed automatically if we used just add(ap, bq, res).)

Because it must hold that pFq(|z)=CB the entries of C are obviously

C=(100)

Finally we have to compute the entries of the 3×3 matrix M such that zddzB=MB holds. This is easy. We already computed the first part zddzB0 above. This gives us the first row of M. For the second row we have:

>>> from sympy import Symbol, cosh, sqrt, diff
>>> z = Symbol("z")
>>> B1 = cosh(2*sqrt(z))
>>> z * diff(B1, z)
sqrt(z)*sinh(2*sqrt(z))

and for the third one

>>> from sympy import Symbol, sinh, sqrt, expand, diff
>>> z = Symbol("z")
>>> B2 = sinh(2*sqrt(z))*sqrt(z)
>>> expand(z * diff(B2, z))
sqrt(z)*sinh(2*sqrt(z))/2 + z*cosh(2*sqrt(z))

Now we have computed the entries of this matrix to be

M=(141400010z12)

Note that the entries of C and M should typically be rational functions in z, with rational coefficients. This is all we need to do in order to add a new formula to the lookup table for hyperexpand.

Implemented Hypergeometric Formulae

A vital part of the algorithm is a relatively large table of hypergeometric function representations. The following automatically generated list contains all the representations implemented in SymPy (of course many more are derived from them). These formulae are mostly taken from [Luke1969] and [Prudnikov1990]. They are all tested numerically.

0F0(|z)=ez
1F0(a|z)=(z+1)a
2F1(a,a122a|z)=22a1(z+1+1)2a+1
2F1(1,12|z)=1zlog(z+1)
2F1(12,132|z)=1zatanh(z)
2F1(12,1232|z)=1zasin(z)
2F1(a,a+1212|z)=12(z+1)2a+12(z+1)2a
2F1(a,a12|z)=cos(2aasin(z))
2F1(1,132|z)=asin(z)zz+1
2F1(12,121|z)=2K(z)π
2F1(12,121|z)=2E(z)π
3F2(12,1,112,2|z)=2z3atanh(z)+2313zlog(z+1)
3F2(12,1,12,2|z)=(49169z)z+1+43zlog(12z+1+12)+169z
1F1(1b|z)=zb+1(b1)ezγ(b1,z)
1F1(a2a|z)=4a12za+12ez2Ia12(z2)Γ(a+12)
1F1(aa+1|z)=a(zeiπ)aγ(a,zeiπ)
1F1(1212|z)=ziπerf(zi)+ez
1F2(134,54|z)=πeiπ424z(isinh(2z)S(24zπeiπ4)+cosh(2z)C(24zπeiπ4))
2F2(12,a32,a+1|z)=aiπ1z2a1erf(zi)a(zeiπ)a2a1γ(a,zeiπ)
2F2(1,12,2|z)=1z(log(z)+Ei(z))γz
0F1(12|z)=cosh(2z)
0F1(b|z)=zb2+12Ib1(2z)Γ(b)
0F3(12,a,a+12|z)=22aza2+14(I2a1(44z)+J2a1(44z))Γ(2a)
0F3(a,a+12,2a|z)=(2zeiπ2)2a+1I2a1(224zeiπ4)J2a1(224zeiπ4)Γ2(2a)
1F2(aa12,2a|z)=24a1za+1Ia32(z)Ia12(z)Γ(a12)Γ(a+12)4a12za+12I2a12(z)Γ2(a+12)
1F2(12b,b+2|z)=πIb+1(z)Ib1(z)sin(bπ)(b+1)
1F2(1232,32|z)=12zShi(2z)
1F2(3432,74|z)=3π4z34e3π4iS(24zπeiπ4)
1F2(1412,54|z)=πeiπ424zC(24zπeiπ4)
2F3(a,a+122a,b,2ab+1|z)=(z2)2a+1I2ab(z)Ib1(z)Γ(b)Γ(2ab+1)
2F3(1,12,2,32|z)=1z(log(2z)+Chi(2z))γz
3F3(1,1,a2,2,a+1|z)=a(z)a(a1)2(Γ(a)Γ(a,z))+az(a22a+1)(a+1)(log(z)+E1(z)+γ)aezz(a22a+1)+az(a22a+1)

References

[Roach1996]Kelly B. Roach. Hypergeometric Function Representations. In: Proceedings of the 1996 International Symposium on Symbolic and Algebraic Computation, pages 301-308, New York, 1996. ACM.
[Roach1997]Kelly B. Roach. Meijer G Function Representations. In: Proceedings of the 1997 International Symposium on Symbolic and Algebraic Computation, pages 205-211, New York, 1997. ACM.
[Luke1969]Luke, Y. L. (1969), The Special Functions and Their Approximations, Volume 1.
[Prudnikov1990]A. P. Prudnikov, Yu. A. Brychkov and O. I. Marichev (1990). Integrals and Series: More Special Functions, Vol. 3, Gordon and Breach Science Publisher.