MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: speed of multiplying polynomials

  • To: mathgroup at
  • Subject: [mg72566] Re: [mg72554] speed of multiplying polynomials
  • From: Carl Woll <carlw at>
  • Date: Mon, 8 Jan 2007 05:32:57 -0500 (EST)
  • References: <>

dmharvey at wrote:

>I'm trying to figure out how fast mathematica can multiply polynomials
>with integer coefficients. I don't know mathematica well at all, but I
>had a friend write me a mathematica program to test this. It look like
>on a regular desktop machine it can multiply e.g. degree 1000
>polynomials with coefficients in the range 0 <= x <= 1000 in about 1.3
>This is ludicrously slow compared to some other computer algebra
>systems, which can do this multiplication in about 0.0003
>seconds. I can't believe mathematica is in the order of 10000 times
>slower for this simple task. I think perhaps we are doing something
>wrong. Can anyone suggest a way of coaxing mathematica into doing this
>kind of arithmetic at a comparable pace?
>Many thanks
I think the problem is with the representation of a polynomial. Assuming 
we are dealing with univariate polynomials, we can represent the 
polynomial as a list of coefficients:

c = {900, 801, etc}

or as a polynomial with explicit multiplications and additions:

p = 900 + 801*x + etc.

If we work with lists, we can use ListConvolve to do the multiplication. 
If we work with polynomials, we will probably use Expand. Here is an 
example multiplying two polynomials in both ways. Here are the coefficients:

c1 = Table[Random[Integer, {0, 1000}], {1001}];
c2 = Table[Random[Integer, {0, 1000}], {1001}];

Here are the equivalent polynomials:

p1 = x^Range[0, 1000] . c1;
p2 = x^Range[0, 1000] . c2;

Here we time multiplication using ListConvolve:

c = ListConvolve[c1, c2, {1, -1}, 0]; // Timing
{0. Second, Null}

Here we time multiplication using Expand:

p = Expand[p1 p2]; // Timing
{2.25 Second, Null}

Finally, we check that the two approaches yield the same result:

c === CoefficientList[p, x]

So, if one is interested in multiplying large, dense univariate 
polynomials in Mathematica, the fastest approach is to use ListConvolve.

Carl Woll
Wolfram Research

  • Prev by Date: Re: question on Plot and Show plus Axis Labels:
  • Next by Date: Re: speed of multiplying polynomials
  • Previous by thread: speed of multiplying polynomials
  • Next by thread: Re: speed of multiplying polynomials