This forum has been archived. All content is frozen. Please use KDE Discuss instead.

cwisePow or equivalent

Tags: None
(comma "," separated)
MMU
Registered Member
Posts
10
Karma
0

cwisePow or equivalent

Thu Dec 01, 2011 8:37 pm
Greetings again, Sorry for flooding with multiple topics, but I am trying to convert some code over to C++ using Eigen and I am having trouble finding ways to do certain things.

I wish to create an array that is 10 raised to the power of all the values in another array. I.E. So where you can do pow(a, 10) to receive an array with all the values in "a" raised to 10, you could do pow(10, a) to receive an array that is 10 raised to the power of all the values in a. However the second version of pow is not overloaded and I am unsure how to replicate this functionality without explicitly looping with my own function.

I noticed a cwisePow in the "See Also" of some of the matrix function documentation which I could potentially use on a matrix/array of 10's, but it is not a hyperlink and it doesn't exist when I try to call it. I also thought of maybe using binaryExpr and passing the pow as a function pointer, but I can't get that to work.

I like how easy to use Eigen is compared to the other common matrix libraries for C++ but some functionality eludes me even after digging through the API so help is greatly appreciated! Thank you!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: cwisePow or equivalent

Fri Dec 02, 2011 7:35 am
you have to create a binary pow function (see the file src/Core/Functors.h for examples) and then call A.binaryExpr(B, your_pow_functor());
MMU
Registered Member
Posts
10
Karma
0

Re: cwisePow or equivalent

Fri Dec 02, 2011 1:31 pm
Ah that worked. Thanks. I thought maybe there was a way to do it without writing my own function but there wasn't. I got it working with this functor:
Code: Select all
template<typename Scalar> struct mypow{       
    EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& _x, const Scalar& _y) const
    {
        using std::pow;
        Scalar res = pow(_x, _y);
        return res;
    }
};


Called with the line:
Code: Select all
tens.binaryExpr(dataArray, mypow<double>())

And this successfully raised my array of 10's to the values in dataArray.

If there is a better way I am all ears :P

Thanks for the help.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: cwisePow or equivalent

Sat Dec 03, 2011 10:38 am
maybe using ptr_fun:

Code: Select all
using namespace std:

int n = ...;

ArrayXf::Constant(n, 10).binaryExpr(dataArray, std::ptr_fun(pow));
MMU
Registered Member
Posts
10
Karma
0

Re: cwisePow or equivalent

Mon Dec 05, 2011 1:20 pm
Using "ptr_fun" causes all kinds of errors because "mypow" is actually a struct with a constructor (I did this because I followed what is done in Functors.h and other examples in the documentation). If "mypow" was just a regular function ptr_fun would work (I have done this with _isnan to check if all elements are NaN and array.unaryExpr(ptr_fun(_isnan)) works).
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: cwisePow or equivalent

Mon Dec 05, 2011 3:43 pm
sure, my suggestion was just to avoid the need to define the functor...


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]