Ask Your Question
1

mutivariate polynomial with a single variable

asked 2023-08-17 19:20:47 +0200

Max Alekseyev gravatar image

updated 2023-08-17 19:22:17 +0200

How can I convert a univariate polynomial into a multivariate one? The only idea I have is introducing a dummy (silent) variable, but is there a more straightforward way?

PS. The reason for using such a conversion is that the multivariate polynomial ring has some methods (such as .reduce()) that are unavailable for univariate polynomials.

edit retag flag offensive close merge delete

Comments

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 with x. Alternatively find better alternatives (like quo_rem instead of reduce?) in the single variable case.

John Palmieri gravatar imageJohn Palmieri ( 2023-08-17 19:41:48 +0200 )edit

Let's assume that polynomial comes from some function and (for simplicity) it's defined as an element of QQ['x']. The function quo_rem takes a single argument while reduce 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.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-08-17 20:01:35 +0200 )edit

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 defining x to be one of the generators: after one or two lines of setup at the beginning, you are working the context you want.

John Palmieri gravatar imageJohn Palmieri ( 2023-08-17 20:20:53 +0200 )edit

3 Answers

Sort by » oldest newest most voted
1

answered 2023-08-18 16:37:28 +0200

slelievre gravatar image

updated 2023-08-18 23:40:21 +0200

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
edit flag offensive delete link more

Comments

Yes, I have found it. The conversion is still looks ugly though. I was hoping for a method similar to .univariate_polynomial() doing the opposite conversion.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-08-18 18:12:41 +0200 )edit

I've requested the .multivariate_polynomial()method in https://github.com/sagemath/sage/issu...

Max Alekseyev gravatar imageMax Alekseyev ( 2023-10-16 22:41:00 +0200 )edit
1

answered 2023-08-17 22:03:45 +0200

Max Alekseyev gravatar image

Let say we have

R.<x> = QQ[]
f = x^2 + x + 1

My ugly solution is a conversion:

g = PolynomialRing( f.base_ring(), 2, str(f.variables()[0]) + ',dummy' )(f)

for which we have:

print(g.parent())
Multivariate Polynomial Ring in x, dummy over Rational Field
edit flag offensive delete link more
0

answered 2023-08-18 19:08:11 +0200

Why not just start with

R.<x,dummy> = QQ[]

and then any polynomials you later construct in x are automatically multivariate? I must be missing something about the workflow here.

edit flag offensive delete link more

Comments

It was just an example. In the actual application the polynomial comes from elsewhere and I need to convert it.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-08-18 19:21:25 +0200 )edit

Hence the very first question I asked: "How are you defining your polynomials in the first place?"

John Palmieri gravatar imageJohn Palmieri ( 2023-08-18 20:47:59 +0200 )edit

I'm not defining it but getting as an input (say).

Max Alekseyev gravatar imageMax Alekseyev ( 2023-08-18 21:01:27 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-08-17 19:20:47 +0200

Seen: 121 times

Last updated: Aug 18 '23