Ask Your Question

Revision history [back]

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.

sage: R.<x> = QQ[]
sage: f = x^2 + x + 1
sage: g = PolynomialRing(f.base_ring(), 1, str(f.variables()[0]))(f)
sage: g
x^2 + x + 1
sage: parent(g)
Multivariate Polynomial Ring in x over Rational Field

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