# A simple hypergeometric function fails.

There is a nice method to compute the Narayana polynomials. With Maple we can write

P := n -> simplify(hypergeom([-n,-n+1], [2], 1/x));
seq(expand(x^k*P(k)), k=0..5);


1,  x,  x^2+x,  x^3+3*x^2+x,  x^4+6*x^3+6*x^2+x,  x^5+10*x^4+20*x^3+10*x^2+x.


Trying the same with Sage

P = lambda n: simplify(hypergeometric([-n,-n+1],[2], 1/x))
[expand(x^k*P(k)) for k in (0..5)]


[1, x, ..., x^n*hypergeometric((-n, -n-1),(2),1/x)]


This is really disappointing. Is there a workaround?

edit retag close merge delete

Sort by » oldest newest most voted

Sage's simplify() command is very basic; it just sends to Maxima and back, which does simplify a few things. More generally, Sage has a lot of specific simplification routines from Maxima that you can use. In this case, we have success!

sage: P = lambda n: hypergeometric([-n,-n+1],[2], 1/x).simplify_hypergeometric()
sage: [expand(x^k*P(k)) for k in (0..5)]
[1,
x,
x^2 + x,
x^3 + 3*x^2 + x,
x^4 + 6*x^3 + 6*x^2 + x,
x^5 + 10*x^4 + 20*x^3 + 10*x^2 + x]

more

This is definitely not the way I expect the nomenclature to work in a object-oriented setup. Regardless of the object the function name should be object.simplify(). Depending on the type of the object simplify() chooses _internally_ the appropriate routine, which in this case is simplify_hypergeometric(), a function the user needs not to be aware of.

( 2014-10-30 05:24:18 -0500 )edit
1

Like most things based on Python, Sage is not "purely" object-oriented, or we wouldn't have functions. In any case, if you look at the documentation, the point is that there are many kinds of simplification, and we don't want to force the user to use ALL of them every time they want to simplify. The routine you would like is simplify_full() - except currently it doesn't have the simplify_hypergeometric inside of it, possibly for good reasons. In any case, "deciding" which routine should be chosen for a given symbolic expression consisting possibly of dozens of different kinds of functions is arbitrarily difficult in a computation sense. Even deciding whether a given expression is equal to zero is computationally infeasible, or so I'm told - I've never seen a specific reference :)

( 2014-10-30 07:13:12 -0500 )edit
1

https://en.wikipedia.org/wiki/Constant_problem ?

( 2014-10-30 07:53:46 -0500 )edit
( 2014-10-30 10:12:44 -0500 )edit

You can try with sympy :

sage: import sympy
sage: P = lambda n: sympy.hyper([-n,-n+1],[2], 1/x).simplify()
sage: [expand(x^k*P(k)) for k in (0..5)]
[1,
x,
x^2 + x,
x^3 + 3*x^2 + x,
x^4 + 6*x^3 + 6*x^2 + x,
x^5 + 10*x^4 + 20*x^3 + 10*x^2 + x]

more

We *really* need to start incorporating more Sympy routines as possible algorithms for Sage things. It has improved so much in the past five years, and I barely know how to use it nowadays... but it would be a lot of work to do :(

( 2014-10-29 11:13:52 -0500 )edit

That may be but, as my answer shows, for this problem Sage has all that's needed.

( 2014-10-29 11:19:04 -0500 )edit

As I have said in your other hypergeometric question, there is no need for sympy nor Maxima:

sage: from sage.functions.hypergeometric import closed_form
sage: P = lambda n: simplify(closed_form(hypergeometric([-n,-n+1],[2], 1/x)))
sage: [expand(x^k*P(k)) for k in (0..5)]
[1,
x,
x^2 + x,
x^3 + 3*x^2 + x,
x^4 + 6*x^3 + 6*x^2 + x,
x^5 + 10*x^4 + 20*x^3 + 10*x^2 + x]


Fixed in http://trac.sagemath.org/ticket/17066 which needs review.

more

Wow, I really did not realize how much stuff was in that ticket, this is awesome. Followup, then: how could a normal user find this using only tab-completion of globals (where I don't count sage.[tab] for obvious reasons)? Without that availability it's not as helpful. (Also, does expand use Maxima or only Pynac?)

( 2014-10-29 12:55:21 -0500 )edit