ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 30 Oct 2014 16:12:44 +0100A simple hypergeometric function fails.https://ask.sagemath.org/question/24677/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);
and get the answer
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)]
gives the answer
[1, x, ..., x^n*hypergeometric((-n, -n-1),(2),1/x)]
This is really disappointing. Is there a workaround?
Wed, 29 Oct 2014 16:43:29 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/Answer by tmonteil for <p>There is a nice method to compute the Narayana polynomials.
With Maple we can write</p>
<pre><code>P := n -> simplify(hypergeom([-n,-n+1], [2], 1/x));
seq(expand(x^k*P(k)), k=0..5);
</code></pre>
<p>and get the answer </p>
<pre><code>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.
</code></pre>
<p>Trying the same with Sage</p>
<pre><code>P = lambda n: simplify(hypergeometric([-n,-n+1],[2], 1/x))
[expand(x^k*P(k)) for k in (0..5)]
</code></pre>
<p>gives the answer</p>
<pre><code>[1, x, ..., x^n*hypergeometric((-n, -n-1),(2),1/x)]
</code></pre>
<p>This is really disappointing. Is there a workaround? </p>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?answer=24680#post-id-24680You 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]
Wed, 29 Oct 2014 17:09:40 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?answer=24680#post-id-24680Comment by kcrisman for <p>You can try with sympy :</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24682#post-id-24682We *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 :(Wed, 29 Oct 2014 17:13:52 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24682#post-id-24682Comment by rws for <p>You can try with sympy :</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24684#post-id-24684That may be but, as my answer shows, for this problem Sage has all that's needed.Wed, 29 Oct 2014 17:19:04 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24684#post-id-24684Answer by rws for <p>There is a nice method to compute the Narayana polynomials.
With Maple we can write</p>
<pre><code>P := n -> simplify(hypergeom([-n,-n+1], [2], 1/x));
seq(expand(x^k*P(k)), k=0..5);
</code></pre>
<p>and get the answer </p>
<pre><code>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.
</code></pre>
<p>Trying the same with Sage</p>
<pre><code>P = lambda n: simplify(hypergeometric([-n,-n+1],[2], 1/x))
[expand(x^k*P(k)) for k in (0..5)]
</code></pre>
<p>gives the answer</p>
<pre><code>[1, x, ..., x^n*hypergeometric((-n, -n-1),(2),1/x)]
</code></pre>
<p>This is really disappointing. Is there a workaround? </p>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?answer=24683#post-id-24683As 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.Wed, 29 Oct 2014 17:17:15 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?answer=24683#post-id-24683Comment by kcrisman for <p>As I have said in your other hypergeometric question, there is no need for sympy nor Maxima:</p>
<pre><code>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]
</code></pre>
<p>Fixed in <a href="http://trac.sagemath.org/ticket/17066">http://trac.sagemath.org/ticket/17066</a> which needs review.</p>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24688#post-id-24688Wow, 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?)Wed, 29 Oct 2014 18:55:21 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24688#post-id-24688Answer by kcrisman for <p>There is a nice method to compute the Narayana polynomials.
With Maple we can write</p>
<pre><code>P := n -> simplify(hypergeom([-n,-n+1], [2], 1/x));
seq(expand(x^k*P(k)), k=0..5);
</code></pre>
<p>and get the answer </p>
<pre><code>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.
</code></pre>
<p>Trying the same with Sage</p>
<pre><code>P = lambda n: simplify(hypergeometric([-n,-n+1],[2], 1/x))
[expand(x^k*P(k)) for k in (0..5)]
</code></pre>
<p>gives the answer</p>
<pre><code>[1, x, ..., x^n*hypergeometric((-n, -n-1),(2),1/x)]
</code></pre>
<p>This is really disappointing. Is there a workaround? </p>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?answer=24681#post-id-24681Sage'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]
Wed, 29 Oct 2014 17:12:55 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?answer=24681#post-id-24681Comment by Peter Luschny for <p>Sage's <code>simplify()</code> command is <em>very</em> 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!</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24707#post-id-24707https://en.wikipedia.org/wiki/Constant_problem ?Thu, 30 Oct 2014 13:53:46 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24707#post-id-24707Comment by kcrisman for <p>Sage's <code>simplify()</code> command is <em>very</em> 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!</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24711#post-id-24711Or https://en.wikipedia.org/wiki/Richardson%27s_theorem - thanks!Thu, 30 Oct 2014 16:12:44 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24711#post-id-24711Comment by Peter Luschny for <p>Sage's <code>simplify()</code> command is <em>very</em> 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!</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24698#post-id-24698This 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.Thu, 30 Oct 2014 11:24:18 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24698#post-id-24698Comment by kcrisman for <p>Sage's <code>simplify()</code> command is <em>very</em> 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!</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24704#post-id-24704Like 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 :)Thu, 30 Oct 2014 13:13:12 +0100https://ask.sagemath.org/question/24677/a-simple-hypergeometric-function-fails/?comment=24704#post-id-24704