Sage has both
- univariate polynomial rings
- multivariate polynomial rings in a single variable
Hopefully multivariate poylnomial rings in a single variable behave as you want.
Example.
Say we have defined
sage: x = polygen(QQ)
sage: f = x^2 + x + 1
sage: R = f.parent()
or
sage: R.<x> = QQ[]
sage: f = x^2 + x + 1
There are various ways to define the multivariate counterpart to f
.
Some ways straight from f
:
sage: g = PolynomialRing(f.base_ring(), 1, str(f.variables()[0]))(f)
sage: g = PolynomialRing(f.base_ring(), 1, str(f.parent().gen()))(f)
sage: g = PolynomialRing(f.base_ring(), 1, str(parent(f).gen()))(f)
sage: g = PolynomialRing(f.base_ring(), 1, f.variable_name())(f)
sage: g
x^2 + x + 1
sage: parent(g)
Multivariate Polynomial Ring in x over Rational Field
Some ways using R
:
sage: M = PolynomialRing(R.base_ring(), 1, str(R.gen()))
sage: M = PolynomialRing(R.base_ring(), 1, R.variable_name())
sage: g = M(f)
Helper function.
As you suggest, similar to g.univariate_polynomial()
,
one could expect f.multivariate_polynomial()
.
It might be worth opening a ticket to add such a method.
In the meantime, one can use a helper function:
def multivariate(f):
r"""
Return this polynomial in one variable as an element
in a multivariate polynomial ring with one variable.
EXAMPLES::
sage: x = polygen(QQ)
sage: f = x^2 + x + 1
sage: parent(f)
Univariate Polynomial Ring in x over Rational Field
sage: g = multivariate(f)
sage: parent(g)
Multivariate Polynomial Ring in x over Rational Field
sage: g
x^2 + x + 1
sage: f == g and g == f and g.univariate_polynomial() == f
True
"""
R = f.parent()
if str(R).startswith('Univariate'):
R = PolynomialRing(R.base_ring(), 1, R.variable_name())
f = R(f)
return f
How are you defining your polynomials in the first place? The dummy variable seems pretty straightforward to me: just set
R.<x,y> = ZZ[]
and do all of your computations withx
. Alternatively find better alternatives (likequo_rem
instead ofreduce
?) in the single variable case.Let's assume that polynomial comes from some function and (for simplicity) it's defined as an element of
QQ['x']
. The functionquo_rem
takes a single argument whilereduce
can do the job over multiple given polynomials. But most importantly, I don't want to make distinction between univariate and multivariate polynomials but rather treat the former as latter.To make sure that a polynomial
f
has the correct parent and therefore the desired methods, I don't see a more straightforward way than defining a multivariate polynomial ring and definingx
to be one of the generators: after one or two lines of setup at the beginning, you are working the context you want.