c++ - Efficient Multinomial result -


i've got multinomial class private:

unsigned int power; double *factors; 

i'd know if there more efficient way calculate multinomial's result given argument?

my current code is:

double multinomial::calculatefor(double x) const{     double sum = this->factors[0];     double prod = 1;     for(size_t = 1; <= this->power; i++){         prod *= x;         if(this->factors[i]){             sum += this->factors[i] * prod;         }     }     return sum; } 

i see 2 ways speed computation up.

  1. conditional branches can slow computations on modern pipelined processors, may avoiding conditional speed things up. see why processing sorted array faster unsorted array? of explanation.

  2. using horner's method saves on number of multiplications.

put together, these lead to:

double multinomial::calculatefor(double x) const {     double sum = this->factors[this->power];     (size_t = this->power; > 0; )         sum = this->factors[--i] + sum * x;     return sum; } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -