ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 05 May 2015 14:15:00 -0500bell_polynomial(n,k).coefficients() raises an errorhttp://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/Consider the Bell polynomials:
for n in (0..4):
print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
Extracting the coefficients I expect this triangle:
[[1]]
[[0], [1]]
[[0], [1], [1]]
[[0], [1], [3], [1]]
[[0], [1], [3, 4], [6], [1]]
However this call (which looks very natural) does not work:
for n in (0..4):
print [bell_polynomial(n,k).coefficients() for k in (0..n)]
It gives the error message: AttributeError: 'sage.rings.rational.Rational' object has no attribute 'coefficients'. However I nowhere give a sage.rings.rational.Rational object as an input. My input is a polynomial if I can trust the name "bell_polynomial(n,k)". So may I consider this as an internal error/bug?
Ralf Stephan showed me a workaround:
def polynomial_coefficients(p):
if (isinstance(p, (int, Integer, Rational))):
return [p]
else:
return p.coefficients()
This gives indeed the expected output. However it might be even better to represent the null polynomial by the empty list (to comply with the understanding that only nonzero coefficients are shown).
if (isinstance(p, (int, Integer, Rational))):
if p == 0 return [] else return [p]
This leads to:
[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
Is this a workaround just to fit my possibly ill-guided expectations or is this a reasonable patch for the general case?
Or is it correct that an object which is called a polynomial raises an error if asked for the coefficients?Wed, 29 Apr 2015 11:45:27 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/Comment by Peter Luschny for <p>Consider the Bell polynomials:</p>
<pre><code>for n in (0..4):
print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
</code></pre>
<p>Extracting the coefficients I expect this triangle:</p>
<pre><code>[[1]]
[[0], [1]]
[[0], [1], [1]]
[[0], [1], [3], [1]]
[[0], [1], [3, 4], [6], [1]]
</code></pre>
<p>However this call (which looks very natural) does not work:</p>
<pre><code>for n in (0..4):
print [bell_polynomial(n,k).coefficients() for k in (0..n)]
</code></pre>
<p>It gives the error message: AttributeError: 'sage.rings.rational.Rational' object has no attribute 'coefficients'. However I nowhere give a sage.rings.rational.Rational object as an input. My input is a polynomial if I can trust the name "bell_polynomial(n,k)". So may I consider this as an internal error/bug?</p>
<p>Ralf Stephan showed me a workaround:</p>
<pre><code>def polynomial_coefficients(p):
if (isinstance(p, (int, Integer, Rational))):
return [p]
else:
return p.coefficients()
</code></pre>
<p>This gives indeed the expected output. However it might be even better to represent the null polynomial by the empty list (to comply with the understanding that only nonzero coefficients are shown).</p>
<pre><code>if (isinstance(p, (int, Integer, Rational))):
if p == 0 return [] else return [p]
</code></pre>
<p>This leads to:</p>
<pre><code>[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
</code></pre>
<p>Is this a workaround just to fit my possibly ill-guided expectations or is this a reasonable patch for the general case?
Or is it correct that an object which is called a polynomial raises an error if asked for the coefficients?</p>
http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26727#post-id-26727For what it's worth I describe a different implementation of the partial Bell polynomials (which might be more efficient) here: https://oeis.org/wiki/User:Peter_Luschny/MarginaliaTue, 05 May 2015 14:15:00 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26727#post-id-26727Comment by kcrisman for <p>Consider the Bell polynomials:</p>
<pre><code>for n in (0..4):
print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
</code></pre>
<p>Extracting the coefficients I expect this triangle:</p>
<pre><code>[[1]]
[[0], [1]]
[[0], [1], [1]]
[[0], [1], [3], [1]]
[[0], [1], [3, 4], [6], [1]]
</code></pre>
<p>However this call (which looks very natural) does not work:</p>
<pre><code>for n in (0..4):
print [bell_polynomial(n,k).coefficients() for k in (0..n)]
</code></pre>
<p>It gives the error message: AttributeError: 'sage.rings.rational.Rational' object has no attribute 'coefficients'. However I nowhere give a sage.rings.rational.Rational object as an input. My input is a polynomial if I can trust the name "bell_polynomial(n,k)". So may I consider this as an internal error/bug?</p>
<p>Ralf Stephan showed me a workaround:</p>
<pre><code>def polynomial_coefficients(p):
if (isinstance(p, (int, Integer, Rational))):
return [p]
else:
return p.coefficients()
</code></pre>
<p>This gives indeed the expected output. However it might be even better to represent the null polynomial by the empty list (to comply with the understanding that only nonzero coefficients are shown).</p>
<pre><code>if (isinstance(p, (int, Integer, Rational))):
if p == 0 return [] else return [p]
</code></pre>
<p>This leads to:</p>
<pre><code>[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
</code></pre>
<p>Is this a workaround just to fit my possibly ill-guided expectations or is this a reasonable patch for the general case?
Or is it correct that an object which is called a polynomial raises an error if asked for the coefficients?</p>
http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26684#post-id-26684My guess is that this is yet another example of not knowing what category a number belongs in. 1, 0, etc. are probably given as rationals or integers and not polynomials, because probably whoever implemented this just put them into that. Perhaps in that code for Bell polynomials all output should be explicitly polynomials (which polynomial ring, I don't know)?Wed, 29 Apr 2015 13:52:37 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26684#post-id-26684Answer by tmonteil for <p>Consider the Bell polynomials:</p>
<pre><code>for n in (0..4):
print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
</code></pre>
<p>Extracting the coefficients I expect this triangle:</p>
<pre><code>[[1]]
[[0], [1]]
[[0], [1], [1]]
[[0], [1], [3], [1]]
[[0], [1], [3, 4], [6], [1]]
</code></pre>
<p>However this call (which looks very natural) does not work:</p>
<pre><code>for n in (0..4):
print [bell_polynomial(n,k).coefficients() for k in (0..n)]
</code></pre>
<p>It gives the error message: AttributeError: 'sage.rings.rational.Rational' object has no attribute 'coefficients'. However I nowhere give a sage.rings.rational.Rational object as an input. My input is a polynomial if I can trust the name "bell_polynomial(n,k)". So may I consider this as an internal error/bug?</p>
<p>Ralf Stephan showed me a workaround:</p>
<pre><code>def polynomial_coefficients(p):
if (isinstance(p, (int, Integer, Rational))):
return [p]
else:
return p.coefficients()
</code></pre>
<p>This gives indeed the expected output. However it might be even better to represent the null polynomial by the empty list (to comply with the understanding that only nonzero coefficients are shown).</p>
<pre><code>if (isinstance(p, (int, Integer, Rational))):
if p == 0 return [] else return [p]
</code></pre>
<p>This leads to:</p>
<pre><code>[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
</code></pre>
<p>Is this a workaround just to fit my possibly ill-guided expectations or is this a reasonable patch for the general case?
Or is it correct that an object which is called a polynomial raises an error if asked for the coefficients?</p>
http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?answer=26686#post-id-26686The cause of your problem is that `bell_polynomial(0,0)` does not return `1` as a polynomial, but as a rational number.
sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Rational Field
I consider this as a bug : `bell_polynomial(n,k)` should always be an element of a polynomial ring, the doc claims explicitely that the output should be "a polynomial in `n-k+1` variables over `\QQ`". Thanks for reporting. This is now [trac ticket 18338](http://trac.sagemath.org/ticket/18338) (needs review).
With the patch applied, you get directly:
sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Univariate Polynomial Ring in x_1 over Rational Field
sage: for n in (0..4):
....: print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
sage: sage: for n in (0..4):
....: print [bell_polynomial(n,k).coefficients() for k in (0..n)]
[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
Wed, 29 Apr 2015 14:31:00 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?answer=26686#post-id-26686Comment by tmonteil for <p>The cause of your problem is that <code>bell_polynomial(0,0)</code> does not return <code>1</code> as a polynomial, but as a rational number.</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Rational Field
</code></pre>
<p>I consider this as a bug : <code>bell_polynomial(n,k)</code> should always be an element of a polynomial ring, the doc claims explicitely that the output should be "a polynomial in <code>n-k+1</code> variables over <code>\QQ</code>". Thanks for reporting. This is now <a href="http://trac.sagemath.org/ticket/18338">trac ticket 18338</a> (needs review).</p>
<p>With the patch applied, you get directly:</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Univariate Polynomial Ring in x_1 over Rational Field
sage: for n in (0..4):
....: print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
sage: sage: for n in (0..4):
....: print [bell_polynomial(n,k).coefficients() for k in (0..n)]
[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
</code></pre>
http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26691#post-id-26691The error is consistent in the sense that in the current implementation, it starts with the Python `int` 0, then it is (sometimes) added with the Sage rational `1`, then it is (sometimes) added with some genuine polynomial. The implementation of trac ticket 18338 solves this, you will get:
sage: bell_polynomial(3,0).parent()
Multivariate Polynomial Ring in x_1, x_2, x_3, x_4 over Rational FieldThu, 30 Apr 2015 05:11:48 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26691#post-id-26691Comment by Peter Luschny for <p>The cause of your problem is that <code>bell_polynomial(0,0)</code> does not return <code>1</code> as a polynomial, but as a rational number.</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Rational Field
</code></pre>
<p>I consider this as a bug : <code>bell_polynomial(n,k)</code> should always be an element of a polynomial ring, the doc claims explicitely that the output should be "a polynomial in <code>n-k+1</code> variables over <code>\QQ</code>". Thanks for reporting. This is now <a href="http://trac.sagemath.org/ticket/18338">trac ticket 18338</a> (needs review).</p>
<p>With the patch applied, you get directly:</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Univariate Polynomial Ring in x_1 over Rational Field
sage: for n in (0..4):
....: print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
sage: sage: for n in (0..4):
....: print [bell_polynomial(n,k).coefficients() for k in (0..n)]
[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
</code></pre>
http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26694#post-id-26694Thank you tmonteil! I agree absolutely with this. More in my answer at the track ticket.Thu, 30 Apr 2015 12:40:56 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26694#post-id-26694Comment by tmonteil for <p>The cause of your problem is that <code>bell_polynomial(0,0)</code> does not return <code>1</code> as a polynomial, but as a rational number.</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Rational Field
</code></pre>
<p>I consider this as a bug : <code>bell_polynomial(n,k)</code> should always be an element of a polynomial ring, the doc claims explicitely that the output should be "a polynomial in <code>n-k+1</code> variables over <code>\QQ</code>". Thanks for reporting. This is now <a href="http://trac.sagemath.org/ticket/18338">trac ticket 18338</a> (needs review).</p>
<p>With the patch applied, you get directly:</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Univariate Polynomial Ring in x_1 over Rational Field
sage: for n in (0..4):
....: print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
sage: sage: for n in (0..4):
....: print [bell_polynomial(n,k).coefficients() for k in (0..n)]
[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
</code></pre>
http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26692#post-id-26692By the way, as a user of Bell polynomials, would you mind if the indices of the variables start at 0, e.g. if `bell_polynomial(3,2) = 3*x_0*x_1` instead of `3*x_1*x_2` ? This woudl be more consistent with other polynomials in Sage.Thu, 30 Apr 2015 06:38:57 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26692#post-id-26692Comment by tmonteil for <p>The cause of your problem is that <code>bell_polynomial(0,0)</code> does not return <code>1</code> as a polynomial, but as a rational number.</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Rational Field
</code></pre>
<p>I consider this as a bug : <code>bell_polynomial(n,k)</code> should always be an element of a polynomial ring, the doc claims explicitely that the output should be "a polynomial in <code>n-k+1</code> variables over <code>\QQ</code>". Thanks for reporting. This is now <a href="http://trac.sagemath.org/ticket/18338">trac ticket 18338</a> (needs review).</p>
<p>With the patch applied, you get directly:</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Univariate Polynomial Ring in x_1 over Rational Field
sage: for n in (0..4):
....: print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
sage: sage: for n in (0..4):
....: print [bell_polynomial(n,k).coefficients() for k in (0..n)]
[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
</code></pre>
http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26690#post-id-26690As mentioned in the doc, the `coefficients()` method for polynomials provides the list of .**nonzero** coefficients, hence the expected result should be `[]`.Thu, 30 Apr 2015 05:09:39 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26690#post-id-26690Comment by PeterL for <p>The cause of your problem is that <code>bell_polynomial(0,0)</code> does not return <code>1</code> as a polynomial, but as a rational number.</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Rational Field
</code></pre>
<p>I consider this as a bug : <code>bell_polynomial(n,k)</code> should always be an element of a polynomial ring, the doc claims explicitely that the output should be "a polynomial in <code>n-k+1</code> variables over <code>\QQ</code>". Thanks for reporting. This is now <a href="http://trac.sagemath.org/ticket/18338">trac ticket 18338</a> (needs review).</p>
<p>With the patch applied, you get directly:</p>
<pre><code>sage: bell_polynomial(0,0)
1
sage: bell_polynomial(0,0).parent()
Univariate Polynomial Ring in x_1 over Rational Field
sage: for n in (0..4):
....: print [bell_polynomial(n,k) for k in (0..n)]
[1]
[0, x_1]
[0, x_2, x_1^2]
[0, x_3, 3*x_1*x_2, x_1^3]
[0, x_4, 3*x_2^2 + 4*x_1*x_3, 6*x_1^2*x_2, x_1^4]
sage: sage: for n in (0..4):
....: print [bell_polynomial(n,k).coefficients() for k in (0..n)]
[[1]]
[[], [1]]
[[], [1], [1]]
[[], [1], [3], [1]]
[[], [1], [3, 4], [6], [1]]
</code></pre>
http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26687#post-id-26687All polynomials with k=0 are affected, and the error is not even consistent: b = bell_polynomial(3,0); b.parent() leads to: AttributeError: 'int' object has no attribute 'parent'.
tmontail, is there a convention how the coefficients of the null polynomial are represented in Sage? [] or 0?Thu, 30 Apr 2015 00:51:40 -0500http://ask.sagemath.org/question/26682/bell_polynomialnkcoefficients-raises-an-error/?comment=26687#post-id-26687