user357269 user357269 - 1 month ago 16x
C++ Question

Creating a permutation matix in Eigen

I'm writing a function that returns a permutation matrix. Inside, it does something like this

PermutationMatrix<Dynamic> my_favourite_permutation () {
Eigen::Matrix<uint, Dynamic, 1> x {4, 3, 1, 2, 0};
PermutationWrapper<Eigen::Matrix<uint, Dynamic, 1>> p
= PermutationWrapper<Eigen::Matrix<uint, Dynamic, 1>> (x);
return p;

The use case includes something like

auto p = my_favourite_permutation();
cout << p.toDenseMatrix() << endl;

Unfortunately the code above gets caught in a static assertion:

/usr/include/eigen3/Eigen/src/Core/util/StaticAssert.h:32: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);

Interestingly if instead of
return p;
I do
return p.inverse();
then it works


I don't know much about Eigen, but it seems that PermutationMatrix defaults to some other type of indices than uint. If you want to use uint, you have to specify it in the template parameter of the return type:

PermutationMatrix<Dynamic, Dynamic, uint> my_favourite_permutation() {