Re: how to keep up to a certain degree of a multivariate polynomial?

35 views
Skip to first unread message

Simon King

unread,
Jun 4, 2013, 2:11:42 AM6/4/13
to sage-s...@googlegroups.com
Hi!

On 2013-06-04, Sam math <hes...@gmail.com> wrote:
> How do I do this for a multivariate polynomial? It says O(.) is not defined...
>
> R.<x,y> = PolynomialRing(QQ)
>
> f = x^3*y^3 + x^2 * y^4 + x*y + x + y + 1
>
> How can I chop this polynomial up to a certain degree of x and y? I.e. I want to keep up until the second degree of x only (regardless of y).

I guess it depends on your application. If you *absolutely* need O(...),
and if your *really* want to bound only the x-degree and keep y untouched,
then you could define f as a *univariate* polynomial in x over a
polynomial ring in y:

sage: f = x^3*y^3 + x^2 * y^4 + x*y + x + y + 1
sage: f
y^3*x^3 + y^4*x^2 + (y + 1)*x + y + 1
sage: f + O(x^2)
y + 1 + (y + 1)*x + O(x^2)

Be warned, however, that arithmetic in a polynomial ring over a
polynomial ring will be dramatically slower than in a multi-variate
polynomial ring:

sage: R2.<x,y> = QQ[]
sage: g = x^3*y^3 + x^2 * y^4 + x*y + x + y + 1
sage: type(f)
sage.rings.polynomial.polynomial_element.Polynomial_generic_dense
sage: type(g)
sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular
sage: %timeit f*f
10000 loops, best of 3: 128 us per loop
sage: %timeit g*g
100000 loops, best of 3: 3.57 us per loop
sage: %timeit f+f
100000 loops, best of 3: 3.15 us per loop
sage: %timeit g+g
1000000 loops, best of 3: 1.47 us per loop

Best regards,
Simon

john_perry_usm

unread,
Jun 4, 2013, 2:20:41 PM6/4/13
to sage-s...@googlegroups.com
Here's a different approach, which is more efficient, but poses its own challenges:

  sage: I = R.ideal(x^2)
  sage: Q = R.quo(I)
  sage: f = Q(x^3*y^3 + x^2*y^4 + x*y + x + y + 1)
  sage: f
  xbar*ybar + xbar + ybar + 1

So, the variables look different, and with reason. But:

  sage: %timeit g*g
  625 loops, best of 3: 3.4 µs per loop
  sage: %timeit f*f
  625 loops, best of 3: 30.5 µs per loop
  sage: %timeit g+g
  625 loops, best of 3: 2.39 µs per loop
  sage: %timeit f+f
  625 loops, best of 3: 27.2 µs per loop

Multiplication is faster than Simon's solution, but addition is (curiously) much slower.

regards
john perry

On Monday, June 3, 2013 7:12:34 PM UTC-5, Sam math wrote:
I have a multivariate polynomial and want to keep only up to a certain degree. I already know how to do this for the univariate case.

For 1 variable, I'd do:

R.<x> = PolynomialRing(QQ)

f = x^4 + x^2 + x^3 + x + 1

f = f + O(x^3)

print f

#output would be 1 + x + x^2... which is what I want.

How do I do this for a multivariate polynomial? It says O(.) is not defined...

R.<x,y> = PolynomialRing(QQ)

f = x^3*y^3 + x^2 * y^4 + x*y + x + y + 1

How can I chop this polynomial up to a certain degree of x and y? I.e. I want to keep up until the second degree of x only (regardless of y).

I know one way I could go about this is to just redefine the polynomial by looping through the first few coefficients, but I'm looking for a more efficient/easier way.

Thanks!

Stephen Montgomery-Smith

unread,
Jun 4, 2013, 8:11:40 PM6/4/13
to sage-s...@googlegroups.com
> On Monday, June 3, 2013 7:12:34 PM UTC-5, Sam math wrote:
>
> I have a multivariate polynomial and want to keep only up to a
> certain degree. I already know how to do this for the univariate case.
>
> For 1 variable, I'd do:
>
> R.<x> = PolynomialRing(QQ)
>
> f = x^4 + x^2 + x^3 + x + 1
>
> f = f + O(x^3)
>
> print f
>
> #output would be 1 + x + x^2... which is what I want.
>
> How do I do this for a multivariate polynomial? It says O(.) is not
> defined...
>
> R.<x,y> = PolynomialRing(QQ)
>
> f = x^3*y^3 + x^2 * y^4 + x*y + x + y + 1
>
> How can I chop this polynomial up to a certain degree of x and y?
> I.e. I want to keep up until the second degree of x only (regardless
> of y).
>
> I know one way I could go about this is to just redefine the
> polynomial by looping through the first few coefficients, but I'm
> looking for a more efficient/easier way.
>
> Thanks!

I think what Sam Math wants to do is this: he has two multivariate
polynomials f and g, lets say in x and y. What he wants to do is to
multiply f*g, and then throw away all the terms of degree higher than,
say, x^100, or even all terms of degree higher than x^100 and y^200.

He doesn't necessarily want a beautiful solution. What he wants is an
efficient solution.

Maybe "looping through the first few coefficients" is the best way to
go. But any better solutions would be greatly appreciated.

leif

unread,
Jun 4, 2013, 8:41:18 PM6/4/13
to sage-s...@googlegroups.com
sage: f
x^3*y^3 + x^2*y^4 + x*y + x + y + 1
sage: f.truncate(x,3)
x^2*y^4 + x*y + x + y + 1
sage: f.truncate(x,3).truncate(y,3)
x*y + x + y + 1


-leif

--
() The ASCII Ribbon Campaign
/\ Help Cure HTML E-Mail

Reply all
Reply to author
Forward
0 new messages