How do I write methods that take the output of the .col() method as input? [duplicate]

StackOverflow https://stackoverflow.com/questions/23452708

  •  15-07-2023
  •  | 
  •  

Question

I have read the documentation on Writing Functions Taking Eigen Types as Parameters, and since all my Eigen containers are Arrays I decided to create a function prototype like this:

template <typename Derived>
void Threshold(Eigen::DenseBase<Derived>& params) 
// I tried void Threshold(Eigen::ArrayBase<Derived>& params) as well, same outcome
{
  params = (params >= 0.0f).template cast<float>();
}

The problem is, when I call this function like this:

Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic> arr;
// fill it with stuff
Threshold(arr.col(0)); // error!

I get the following error:

no known conversion for argument 1 from ‘Eigen::DenseBase<Eigen::Array<float, -1, -1>
>::ColXpr {aka Eigen::Block<Eigen::Array<float, -1, -1>, -1, 1, true>}’ to
‘Eigen::DenseBase<Eigen::Block<Eigen::Array<float, -1, -1>, -1, 1, true> >&’

Is my function template wrong? How do I pass the output of the .col() method of an Eigen::Array object to a function?

Was it helpful?

Solution

You are probably looking for the Ref<> class which will allow you to write a function that accepts both columns of a matrix and vectors without templates nor copies:

void threshold(Ref<ArrayXf> params) {
    params = (params >= 0 ).cast<float>();
}
ArrayXf  a;
ArrayXXf A;
/* ... */
threshold(a);
threshold(A.col(j));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top