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

returning different array type from function taking arbitrar

Tags: None
(comma "," separated)
abachrac
Registered Member
Posts
8
Karma
0
OS
I have a function that returns the rows and columns of a matrix selected by indicator arrays. It works and is happy as long as the input matrix has dynamic size, however I can't figure out how to make it work with fixed size matrices.

Code: Select all
template<typename DerivedData, typename DerivedInd>
typename DerivedData::PlainObject selectBlockByIndices(const Eigen::DenseBase<DerivedData> & m,
    const Eigen::DenseBase<DerivedInd> & rinds,
    const Eigen::DenseBase<DerivedInd> & cinds)
{
  typename DerivedData::PlainObject ret(rinds.rows(), cinds.rows());
  for (int r = 0; r < rinds.size(); r++) {
    for (int c = 0; c < cinds.size(); c++) {
      ret(r, c) = m(rinds(r), cinds(c));
    }
  }
  return ret;
}


The problem is, that regardless of what gets passed in, the function needs to return either the corresponding Matrix/Array expression with the same scalar type, but a dynamic size. As a result, the return type of "typename DerivedData::PlainObject" is incorrect, but I can't figure out what it should be changed to.

Returning "Eigen::Matrix<typename DerivedData::Scalar, Eigen::Dynamic, Eigen::Dynamic>" works, but forces the return type to always be a matrix instead of an array...

thanks!
-=Abe
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
There is no easy solution for that, here is one option:

Define two overloads for ArrayBase and MatrixBase (instead of DenseBase), and to keep the code factored, these overload will simply call a helper function. For instance the MatrixBase overload would looks like this:

Matrix<...> ret;
selectBlockByIndices_helper(m, rinds, cinds, ret);
return ret;

Another option would be to use meta programming:

typename internal::conditional<internal::is_same<internal::traits<DerivedData>::XprKind,ArrayXpr>, Array<....>, Matrix<....> >::type

that gives you either Array<...> or Matrix<....> depending on the expression kind of DerivedData.


Bookmarks



Who is online

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