Ask Your Question

A hypergeometric series

asked 2014-09-23 14:39:52 +0200

Peter Luschny gravatar image

updated 2014-09-24 09:41:01 +0200


Table[2 HypergeometricPFQ[{-n+1,2-n},{2},-1],{n,0,46}]

as well as Maple's


lead to

1, 2, 2, 0, -2, 0, 4, 0, -10, ...

How can I compute this sequence with Sage?

A182122 = lambda n: 2*hypergeometric([-n+1,2-n],[2],-1)
[A182122(n).n(100) for n in (0..46)]

Unfortunately this was not successful.


This is not an isolated problem. I give a second example: Motzkin sums

A005043 = lambda n: (-1)^n*hypergeometric([-n,1/2],[2],4)
[Integer(A005043(n).n(100)) for n in (0..29)]

In this case there is this 'workaround':

A005043 = lambda n: (-1)^n*jacobi_P(n,1,-n-3/2,-7)/(n+1)
[Integer(A005043(n).n(100)) for n in (0..29)]

And Maxima gets it right too:

edit retag flag offensive close merge delete


looks like a bug in Maxima interface to me (or perhaps the options Sage starts Maxima with are causing this). (EDIT, nope, it has nothing to do with Maxima, it's mpmath)

Dima gravatar imageDima ( 2014-09-23 22:31:31 +0200 )edit

you can do the last line as sage: [(-1)^n*maxima_calculus.hgfred([-n,1/2],[2],4) for n in [0..29]]

Dima gravatar imageDima ( 2014-09-24 12:33:51 +0200 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2014-10-12 17:40:27 +0200

rws gravatar image

updated 2014-10-12 17:44:24 +0200

There is another way to solve it in Sage, and we can completely do away with the use of Maxima here:

sage: from sage.functions.hypergeometric import closed_form
sage: [2*closed_form(hypergeometric([1-n,2-n],[2],-1)) for n in [1..20]]
[2, 2, 0, -2, 0, 4, 0, -10, 0, 28, 0, -84, 0, 264, 0, -858, 0, 2860, 0, -9724]

Maxima is still 2x faster with n in [1..2000] though.

See documentation at

edit flag offensive delete link more

answered 2014-09-23 23:02:53 +0200

updated 2014-09-25 10:50:52 +0200

Looks like a bug in mpmath (or in mpmath interface). A workaround is to call Maxima directly (via its library interface), as follows:

sage: maxima_calculus('makelist(2*hypergeometric([-n+2,3-n],[2],-1),n,47)')

EDIT: the following is more Sage-like:

sage: [2*maxima_calculus.hgfred([1-n,2-n],[2],-1) for n in [0..46]]

EDIT': one particular issue with mpmath is that it tries evaluating up to correct relative precision (unless told not to), and this does not work when the actual value is 0. See discussion here.

edit flag offensive delete link more


for some reason, `maxima_calculus.hgfpoly([1-n,2-n],[2],-1)` only works here for n > 1!

Dima gravatar imageDima ( 2014-09-23 23:21:26 +0200 )edit

The mpmath problem was also an issue with the recent implementation of Legendre polynomials, but above n>1000, mind you. There is also a blogpost by Fredrik where he did some work on this: .

rws gravatar imagerws ( 2014-09-24 17:47:19 +0200 )edit
rws gravatar imagerws ( 2014-09-24 18:03:36 +0200 )edit

Ralf, thanks for bringing this to the attention of Fredrik.

Peter Luschny gravatar imagePeter Luschny ( 2014-09-24 20:40:35 +0200 )edit

answered 2014-09-24 17:38:09 +0200

nbruin gravatar image

updated 2014-09-25 01:31:46 +0200

There's definitely a bug in the numerical evaluation of hypergeometric functions here, but if you are interested in the integer values that this sequence takes, ".n()" by itself is a bit risky: You'd need to instruct it to produce a result to sufficient precision that rounding gives you the correct answer.

As was noted already, Maxima knows how to evaluate these hypergeometric functions properly and does so without numerical approximation. The easier way to get the sequence you want is to give maxima an opportunity to look at the expression, which is what "simplify" does. So:

sage: [A182122(n).simplify() for n in (0..15)]
[1, 2, 2, 0, -2, 0, 4, 0, -10, 0, 28, 0, -84, 0, 264, 0]

If you don't like simplify because it might do strange things, simply round-tripping to maxima works too:

sage: [ZZ(maxima_calculus(A182122(n))) for n in (0..15)]
[1, 2, 2, 0, -2, 0, 4, 0, -10, 0, 28, 0, -84, 0, 264, 0]

(and you'll get a sequence of integers that way, not a sequence of symbolic expressions that happen to be integers)

edit flag offensive delete link more


But doesn't simplify just round-trip to Maxima as well? Should there be any difference? Or would the types change or something?

kcrisman gravatar imagekcrisman ( 2014-09-24 19:25:52 +0200 )edit

"If you are interested in the integer values that this sequence takes, .n() is the wrong tool anyway." Why? If we can approximate this value fast numerically what is wrong doing so and then round to an integer? I think your proposed 'simplify'-method is *much* slower. No, I suspect the problem is the extension in the limit. Consider A = lambda x: -pi/(gamma(x/2)^2*x*sin(pi*(1+x/2))*gamma(2-x)). Now for instance limit(A(x), x = 10) gives the ValueError: "oo times number < oo not defined". But 2*limit(A(x), x = 10) exists and is 28. Another error? The same error in disguise?

Peter Luschny gravatar imagePeter Luschny ( 2014-09-24 21:30:52 +0200 )edit

@Luschny: yes, I've edited the answer to reflect this. There's a bit of extra work, though: you'd have to first bound the size of the answer and then determine a precision that will allow you to recognize the integer from it. And of course ensure that the algorithm actually produces an answer that is correct to the requested precision. If we're lucky, maxima is already using something efficient to evaluate these integer values. It certainly seems fairly competitive with the other maxima-based option: sage: %timeit [ZZ(maxima_calculus(A182122(n))) for n in [1..2000]] 1 loops, best of 3: 9.93 s per loop sage: %timeit [2*maxima_calculus.hgfred([1-n,2-n],[2],-1) for n in [1..2000]] 1 loops, best of 3: 24.1 s per loop So among the methods that readily seem to work, relying on what maxima does with `hypergeometric` doesn't ...(more)

nbruin gravatar imagenbruin ( 2014-09-25 02:04:22 +0200 )edit

@kcrisman: yes, you're right! I didn't know. I assumed some basic maxima tools would be called too.

nbruin gravatar imagenbruin ( 2014-09-25 02:06:14 +0200 )edit

Fix is in Please review.

rws gravatar imagerws ( 2014-09-30 18:52:47 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-09-23 14:39:52 +0200

Seen: 626 times

Last updated: Oct 12 '14