Re: [eigen] How to raise double to array powers in Eigen |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
I suggest not find the max exponent in advance, but compute
higher powers ad hoc.
To achieve more encapsulation, we can implement this
functionality in a functor, and let ArrayBase::unaryExpr call it.
Examples of functors are in Eigen/src/Core/functors.
Cheers,
Chen-Pang
在 2014 八月 19 週二 16:30:23,Christoph Hertzberg 寫道:
> 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.
>
> E.g.
> 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) {
> powers[i]=power;
> 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.
>
>
> Christoph