R = PolynomialRing(QQ, 'x')
f = R.random_element(degree = 3)
f.coeff(x^2)
now returns the error
AttributeError: 'Polynomial_rational_dense' object has no attribute
'coeff'
Of course, there is always the workaround by the complete list of
coefficients:
f.coeffs()[2]
but this certainly does not generalize to multivariate polynomials.
The code I was using until very recently (sage 4.3.1 I guess) was like
S = PolynomialRing(QQ, 'x, y')
g = S.random_element(degree = 3)
g.coeff(x^2)
and now returns
AttributeError:
'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'
object has no attribute 'coeff'
Again, there seems to be a workaround using g.coefficient(), but I am
unable handle this method even with the documentation provided, e.g.
for
g = 2*x*y^2 - 2*y^3 + x^2 + 3*y^2 - 4
g.coefficient({x:0, y:0}) == g
g.coefficient({x:1, y:2}) == g
g.coefficient({x:2, y:2}) == g
are all true?! And
g.coefficient(x^2)
returns the error
TypeError: The input degrees must be a dictionary of variables to
exponents.
although the documentation has as an example
sage: R.<x,y> = QQ[]
sage: f=(1+y+y^2)*(1+x+x^2)
sage: f.coefficient({x:0})
y^2 + y + 1
sage: f.coefficient([0,None])
y^2 + y + 1
sage: f.coefficient(x)
y^2 + y + 1
Am I missing something here?
Thanks,
Konstantin
On Feb 18, 9:15 am, zieglerk <konstantin.zieg...@gmail.com> wrote:
> I was pretty sure, that there was a method for polynomials to extract
> the coefficient of a certain monomial (say x^2). But the method I
> used for that before
>
> R = PolynomialRing(QQ, 'x')
> f = R.random_element(degree = 3)
> f.coeff(x^2)
>
> now returns the error
>
> AttributeError: 'Polynomial_rational_dense' object has no attribute
> 'coeff'
>
> Of course, there is always the workaround by the complete list of
> coefficients:
>
> f.coeffs()[2]
You can also use f[2] for this. But I agree that a method coeff() for
univariate polynomials would be intuitive and useful.
>
> but this certainly does not generalize to multivariate polynomials.
> The code I was using until very recently (sage 4.3.1 I guess) was like
I tried all the examples you give below in Sage-4.3.2 and they all
worked fine, and not as you report.
John Cremona
Thanks for the hint to use f[2] or even g[1,2] for multivariate
polynomials.
> > but this certainly does not generalize to multivariate polynomials.
> > The code I was using until very recently (sage 4.3.1 I guess) was like
>
> I tried all the examples you give below in Sage-4.3.2 and they all
> worked fine, and not as you report.
That's irritating. I am using the binaries for OpenSUSE 11.1 and just
to make sure, I downloaded them again, checked the MD5-sum, but still
with the same results/errors as described.
Maybe, I'll just wait for the next release. Or see if I can reproduce
the errors on the online notebook. But I have to go for now.
Thanks, again,
Konstantin
I think, I've spotted my mistake. It was in the way I created the
polynomial ring.
Is it true, that
R.<x,y> = QQ[]
is equivalent to
var('x,y')
R = PolynomialRing(QQ, 'x,y')
x,y = R.gens()
I would not know, how to find the documentation for the first command.
Thanks,
Konstantin
You can use preparse to show what really gets executed:
sage: preparse('R.<x,y> = QQ[]')
"R = QQ['x, y']; (x, y,) = R._first_ngens(2)"
--Mike
On Feb 23, 12:43 am, zieglerk <konstantin.zieg...@gmail.com> wrote:
> Is it true, that
>
> R.<x,y> = QQ[]
>
> is equivalent to
>
> var('x,y')
> R = PolynomialRing(QQ, 'x,y')
> x,y = R.gens()
As Mike has already pointed out, the line R.<x,y> = QQ[] only works
because of the sage preparser. So, it wouldn't work in a Python script
executed by Sage.
Note that the line var('x,y') is not needed at all, because later you
(re-)define x and y anyway. Note also that var('x,y') creates two
*symbolic expressions* x,y, while x,y=R.gens() creates two
*polynomials* x,y:
sage: var('x,y')
(x, y)
sage: type(y)
<type 'sage.symbolic.expression.Expression'>
sage: R = PolynomialRing(QQ, 'x,y')
sage: x,y = R.gens()
sage: type(y)
<type
'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
Symbolic expressions and polynomials are something completely
different, both regarding the implementation and the purpose. So, it
depends on your application which one you should use.
> I think, I've spotted my mistake. It was in the way I created the polynomial ring.
When you really first did var('x,y') and *immediately* afterwards
x,y=R.gens(), then it should just be fine. But if you defined other
things in between, then unintended behaviour may easily occur:
sage: var('x,y')
(x, y)
sage: p = x*y
sage: R = PolynomialRing(QQ, 'x,y')
sage: x,y = R.gens()
sage: p+y in R
False
This is since y (which by now is a polynomial) is first coerced into
the symbolic ring (the parent of p), so that the result of p+y also
lives in the symbolic ring but not in R:
sage: p.parent()
Symbolic Ring
sage: y.parent()
Multivariate Polynomial Ring in x, y over Rational Field
sage: (p+y).parent()
Symbolic Ring
Best regards,
Simon