Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Substitute square in polynomial

Suppose I have a univariate polynomial, say from QQ['x'], and for some reason I know that the odd coefficients will all be zero. So in fact this is a polynomial in x^2. What's the most elegant syntax to substitute some other value for x^2?

The best I could come up with so far is something like this:

sage: x = QQ['x'].0
sage: p = x^6 - 3*x^4 + 5*x^2 - 9
sage: assert not any(p[i] for i in range(1,p.degree()+1,2))
sage: p.parent([p[i] for i in range(0,p.degree()+1,2)])
x^3 - 3*x^2 + 5*x - 9
sage: _(7)
222

Of course, if the value I want to plug in comes from some field where I can compute square roots, I could simply plug one of those in, like p(sqrt(7)) in the example above, but I'm more interested in substituting a multivariate polynomial or something like this.

click to hide/show revision 2
retagged

Substitute square in polynomial

Suppose I have a univariate polynomial, say from QQ['x'], and for some reason I know that the odd coefficients will all be zero. So in fact this is a polynomial in x^2. What's the most elegant syntax to substitute some other value for x^2?

The best I could come up with so far is something like this:

sage: x = QQ['x'].0
sage: p = x^6 - 3*x^4 + 5*x^2 - 9
sage: assert not any(p[i] for i in range(1,p.degree()+1,2))
sage: p.parent([p[i] for i in range(0,p.degree()+1,2)])
x^3 - 3*x^2 + 5*x - 9
sage: _(7)
222

Of course, if the value I want to plug in comes from some field where I can compute square roots, I could simply plug one of those in, like p(sqrt(7)) in the example above, but I'm more interested in substituting a multivariate polynomial or something like this.