A hypergeometric series

Mathematica's

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


as well as Maple's

seq(round(evalf(2*hypergeom([-n+1,2-n],[2],-1),100)),n=0..46);


1, 2, 2, 0, -2, 0, 4, 0, -10, ...   https://oeis.org/A182122


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 http://oeis.org/A005043

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:

maxima_calculus('makelist((-1)^n*hypergeometric([-n,1/2],[2],4),n,0,29)')

edit retag 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)

( 2014-09-23 15:31:31 -0500 )edit
1

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

( 2014-09-24 05:33:51 -0500 )edit

Sort by » oldest newest most voted

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 http://sagemath.org/doc/reference/fun...

more

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)

more

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

( 2014-09-24 12:25:52 -0500 )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?

( 2014-09-24 14:30:52 -0500 )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)

( 2014-09-24 19:04:22 -0500 )edit

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

( 2014-09-24 19:06:14 -0500 )edit

Fix is in http://trac.sagemath.org/ticket/17066 Please review.

( 2014-09-30 11:52:47 -0500 )edit

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.

more

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

( 2014-09-23 16:21:26 -0500 )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: http://fredrik-j.blogspot.de/2010/01/accurate-hypergeometric-functions-for.html .

( 2014-09-24 10:47:19 -0500 )edit
1
( 2014-09-24 11:03:36 -0500 )edit

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

( 2014-09-24 13:40:35 -0500 )edit