# Revision history [back]

### bell_polynomial(n,k).coefficients() 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]


[[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?

 2 retagged tmonteil 26523 ●30 ●191 ●498 http://wiki.sagemath.o...

### bell_polynomial(n,k).coefficients() 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]


[[1]]