Re: [eigen] How to raise double to array powers in Eigen

[ Thread Index | Date Index | More Archives ]

On 19.08.2014 16:13, Ian Bell wrote:
New idea:
1. Cache the set of exponents in a std::map<int, double>

Just a warning: std::map is often very inefficient, as it requires multiple branches and indirect memory accesses. If the key is int, and the range is known and not too big, it is very often much more efficient to use a plain C-array or a std::vector.

int lo = 3, hi=17;  // calculate powers from 3 to 16
std::vector<double> powers(hi-lo);
double power=std::pow(base, lo);
for(int i=0; i<hi-lo; ++i) {
  power *= base;

// Access pow(base, ex) like this:
  double res = powers[ex-lo];

If the exponents are sorted, you can probably safe the extra table and simply accumulate the power variable as you go through the result array.


Dipl.-Inf., Dipl.-Math. Christoph Hertzberg
Cartesium 0.049
Universität Bremen
Enrique-Schmidt-Straße 5
28359 Bremen

Tel: +49 (421) 218-64252

Mail converted by MHonArc 2.6.19+