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

How to write class function for matrix products

Tags: None
(comma "," separated)
sweeneychris
Registered Member
Posts
8
Karma
0
I have a class that internally holds a sparse matrix. I'm having issues writing a method that can take a generic dense or sparse matrix as input and return the output. Ideally, this would also allow for all the lazy evaluation, etc. that Eigen does for efficiency.

So I have two questions: 1) Is there a way to write a templated function that can take dense OR sparse matrices as inputs? I.e. something that unifies MatrixBase and SparseMatrixBase? 2) Is there a way to preserve the lazy evaluation easily?

Ideally, my function would look like this and take in dense or sparse matrices:

template<typename SparseOrDenseMatrix>
inline SparseOrDenseMatrix ApplyLeft(const SparseOrDenseMatrix& x) const {
return sparse_matrix_ * x;
}

The "inline" is there just to emphasize that I would like to keep the lazy evaluation traits, I do not necessarily need that in practice. I've tried a few different return types, including Eigen::Product but I can't get it to compile without lots of template errors. There are some reasons we have for not exposing the underlying matrix publicly.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Better let the compiler figure out the returned type for you and implement a fully generic function, in c++11:

Code: Select all
template<typename T>
auto ApplyLeft(const T& x) -> decltype(x*x)
{
  return x*x;
}


with c++14 you can even omit the decltype thing.


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], daret, Google [Bot], Sogou [Bot], Yahoo [Bot]