2 * 2 * 2 * 2 or pow(2, 4)?

O

Olumide

Hi ,

I hope this isn't too trivial to ask, but I'm evaluating a polynomial
that involve lots of terms like x^2y^2z^3, and although I can use the
power function pow(a, b), I wonder if just writing x*x*y*y*z*z*z for
example wouldn't be more efficient than pow(x, 2)*pow(y, 2)*pow(z,3).
Or is it all the same?

Thanks,

- Olumide
 
K

Kai-Uwe Bux

Olumide said:
I hope this isn't too trivial to ask, but I'm evaluating a polynomial
that involve lots of terms like x^2y^2z^3, and although I can use the
power function pow(a, b), I wonder if just writing x*x*y*y*z*z*z for
example wouldn't be more efficient than pow(x, 2)*pow(y, 2)*pow(z,3).
Or is it all the same?

There is no way to tell a priory. You have to measure.

However, if your polynomial has "lots of terms" like those, you might want
to try the Horner scheme of evaluating polynomials:

a_5 x^5 + a_4 x^4 + a_3 x^3 + a_2 x^2 + a_1 x^1 + a_0

can be rewritten as

( ( ( ( a_5 * x + a_4 ) * x + a_3 ) * x + a_2 ) * x + a_1 ) * x + a_0

which uses only 5 multiplications and 5 additions.


Best

Kai-Uwe Bux
 
D

Daniel Pitts

Kai-Uwe Bux said:
There is no way to tell a priory. You have to measure.

However, if your polynomial has "lots of terms" like those, you might want
to try the Horner scheme of evaluating polynomials:

a_5 x^5 + a_4 x^4 + a_3 x^3 + a_2 x^2 + a_1 x^1 + a_0

can be rewritten as

( ( ( ( a_5 * x + a_4 ) * x + a_3 ) * x + a_2 ) * x + a_1 ) * x + a_0

which uses only 5 multiplications and 5 additions.


Best

Kai-Uwe Bux
Which is also an easy way to convert a string representation of a number
into the number itself :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top