6

I was watching a lecture and got confused over a slide. This is what it says:

Consider a polynomial - first representation

$$P = 2 + 4x^{3} + 8x^{6} + 7x^{25} + 6x^{99}$$

enter image description here

  • The space complexity is 100 memory locations.
  • The time complexity is:
    • Access lookup operations: 100
    • Multiplication operations: 4 + 7 + 26 + 100 = 137
    • Addition operations: 5
    • Total operations: 242

I don't get how the number of multiplication operations are counted. For example, if we consider $x^{3}$ then it should have just two multiplications, right? Such as $x \times x \times x$, isn't it?

Addition operation is 5 or 4...I think its 4?

Shashi
  • 161
  • 1
  • 3

4 Answers4

5

I am not sure what the slide was intended for, so I just gave a straight answer to your question in a comment, viz that the slide really intended 3 multiplications for $4x^3=4\times x\times x\times x$, and 4 additions to add the five terms.

Now of course, there are better ways to do that. Various powers of $x$ can be obtained in multiplying other powers of $x$ together using the rule $x^m\times x^n=x^{m+n}$.

The method of repeated squaring presente by David Richerby in his answer is a systematic way to get high powers of $x$ as fast as possible. However, when you need a few specific powers, you can try to adapt it to compute just these powers with as few multiplications as possible.

So you can compute, for example:

$\begin{align} x^2&=x\times x \\ x^3&=x^2\times x \\ x^6&=x^3\times x^3 \\ x^{12}&=x^6\times x^6 \\ x^{24}&=x^{12}\times x^{12} \\ x^{25}&=x^{24}\times x \\ x^{49}&=x^{24}\times x^{25} \\ x^{50}&=x^{25}\times x^{25} \\ x^{99}&=x^{49}\times x^{50} \end{align}$

Hence you get all needed powers of $x$ with only 9 multiplications of powers of $x$, to which you have to add 4 multiplications for the coefficients, which makes a total of 13 multiplications. The number of additions remains at 4.

But you can do a bit better using a technique that is most efficient when all powers of $x$ are being used, rather than having a sparse polynomial. The technique, known as Horner's method, consists in factorizing some powers of the variable, when it helps the computation.

In the case of your sparse polynomial, you may notice that $7x^{25}+6x^{99}=x^{25}(7+6x^{74})$. Hence rather than computing $x^{50}$ and $x^{99}$, you compute only

$x^{74}=x^{49}\times x^{25}$ and then $x^{25}(7+6x^{74})$.

This saves one multiplication, because both $7x^{25}+6x^{99}$ and $x^{25}(7+6x^{74})$ take exactly 2 multiplications and one addition, assuming the powers of $x$ are already computed, but computing the needed powers takes one less multiplication.

So you are down to a total of 12 multiplications and 4 additions.

I do not see any way to do better on this example, but I have no systematic algorithm to check that. However, I am rather confident that someone must have worked on it.

BTW, what was the theme of the set of slides?

babou
  • 19,645
  • 43
  • 77
3

Yes, there's an error on the slide: as you say, there are only four addition operations (count the plus signs!) and, e.g., computing $4x^3 = 4\times x\times x\times x$ requires only three multiplications, not four.

However, there is a more significant point to be made, which is that computing, say, $8x^6$ does not require six multiplications. We have $8x^6 = 8\times z\times z\times z$, where $z = x^2$. This requires only four multiplications: one to computeĀ $z$ and then three more to compute $8z^3$. And $x^{99} = z_0\times z_1 \times z_5\times z_6$, where $$z_i = x^{2^i} = \begin{cases} x &\text{if $i=0$}\\ z_{i-1}^2 &\text{otherwise.}\end{cases}$$ So this can be computed with only nine multiplications (six to produce $z_1, \dots, z_6$ and three more to compute $x^{99}$ from those. This technique is known as exponentiation by (repeated) squaring.

David Richerby
  • 82,470
  • 26
  • 145
  • 239
0

I think they are multiplying the constants too.

  1. 4*x...*x (3 times)= 3
  2. 8*x*....*x(6 times)= 6
  3. 7*x*....*x(25 times)= 25
  4. 6*x*....*x (99 times)= 99

Addition operations are 4.

Sagnik
  • 904
  • 7
  • 20
0

The claims of this slide seem bizarre.

First, even using repeated multiplication to calculate powers, x^99 requires 98 multiplications, and 6x^99 requires 99. Not 100.

But then you would use the result of x^25, so x^99 would only require 74 more multiplications. With 98 multiplications, you get all the required powers, and with the four coefficients, it makes a total of 102 multiplications and 4 additions.

But nobody would calculate x^99 that way. See babou's post.

Now if all coefficients were non-zero, it seems their method would use about 5,000 multiplications, which is just ridiculous.

gnasher729
  • 32,238
  • 36
  • 56