f



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

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


0
50295 (322)
6/13/2008 1:30:50 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

2 Replies
4160 Views

Similar Articles

[PageSpeed] 3

Olumide wrote:

> 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
0
jkherciueh (3186)
6/13/2008 1:45:52 AM
Kai-Uwe Bux wrote:
> Olumide wrote:
> 
>> 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
Which is also an easy way to convert a string representation of a number 
into the number itself :-)

-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
0
6/14/2008 5:15:20 PM
Reply: