How to raise double to array powers in Eigen

I have a very simple problem that should have a simple answer as well, but so far no luck.

I need to raise a double value to an array of integer powers, in c++ this would look something like

for (int i = 0; i < N; ++i)
{
    y[i] = pow(delta, l[i]);
}

But in Eigen I have tried a number of variants on this theme, without success.

I have an Eigen::ArrayXi l_intE which maps an STL vector l_int .

This doesn't work:

Eigen::pow(double, l_intE)

since the pow() function wants an array as the first input, and then to raise the array to a constant power.

One nasty workaround that I was able to get working (this can't be the right way) is to do this:

(log(delta)*l_doubleE).exp()

which does the conversion

pow(delta,L) -> exp(L*log(delta))

and I have to convert the integers to doubles. This can't help performance either since pow(double, int) is significantly faster than pow(double, double) in general. Although, maybe that isn't true with SSE in Eigen?

Anyway, clarity here would be greatly appreciated.


What about simply multiplying each y[i] with itself l[i] times?

y.fill(1);
for (ArrayXi e = l; (e>0).any(); e -= 1)
    y = (e>0).select(delta*y, y);

Note that after every multiplication we subtract one from the exponents and exit the loop when all of them are zero or less. Since this algorithm modifies the array of exponents in order to keep track of the number of multiplications, we copy it in the initialization clause of the for loop. If it is fine to destroy l along the way, this copy could be omitted.

链接地址: http://www.djcxy.com/p/85710.html

上一篇: Fortran在进行大量计算时比C更容易优化?

下一篇: 如何在Eigen中加倍阵列力量