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

NullaryExpr with non constant functor

Tags: None
(comma "," separated)
mrincon
Registered Member
Posts
3
Karma
0
I'm new to c++ and Eigen. I would like to know if I can use NullaryExpr to fill a matrix with the results of a function that changes state everytime the () operator is used.

Thank you for your help.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
yes, this is how random is implemented.
mrincon
Registered Member
Posts
3
Karma
0
Thank you for the quick response. The problem is that when I try to compile my function I get the following error:

c:\program files (x86)\eigen-eigen-3.0.2\eigen\src/Core/CwiseNullaryOp.h (95): error C3848: expression having type 'const fill<Scalar>' would lose some const-volatile qualifiers in order to call 'double fill<Scalar>::operator ()(Eigen::DenseIndex &)'

Here is my code:

Code: Select all
template<typename Scalar>
class fill
{
public:
   fill(Scalar n_)
      : counter(n_)
   {}

   EIGEN_EMPTY_STRUCT_CTOR(fill)
   inline Scalar operator() (Eigen::DenseIndex& a) { return ++n; };  //generaters error
   //inline Scalar operator() (Eigen::DenseIndex& a) const { return 1; }; compiles OK
   
protected:
   Scalar counter;
};

namespace Eigen
{
namespace internal
{
   template<typename Scalar>
   struct functor_traits<fill<Scalar> >
   {
      enum { Cost = 1, PacketAccess = false, IsRepeatable = false};
   };
}
}

int main()
{
   Eigen::Matrix<double,5,5> x = Eigen::Matrix<double,5,5>::NullaryExpr( fill<double>(1.0) );
    return 0;
}


thank you again for your help. I suspect this is a silly question but I cannot figure it out.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I think that operator() has to be const qualified, and thus counter should be mutable.

btw, toy can use setLinSpace(...) to reproduce what you want to achieve.
mrincon
Registered Member
Posts
3
Karma
0
Thank you. I'm now using mutable and everything is working fine.


Bookmarks



Who is online

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