-1

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?

quant
  • 21,507
  • 32
  • 115
  • 211

1 Answers1

1

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));
ggael
  • 28,425
  • 2
  • 65
  • 71