简体   繁体   中英

C++ function that takes args Eigen sparse matrices

Eigen library provides/suggests numerous ways to pass a dense matrix in a function, so that it works for a different types that share the same base, and avoids copying (ie Ref<>, template expressions).

However, I haven't found anything equivalent for sparse matrices in either eigen documentation or online.

I basically have the following question: How can I write a function with a generic interface so it can be called with both SparseMatrix or MappedSparseMatrix objects, of potentially different template arguments, without copying?

I have tried template expression of SparseMatrixBase withe derived arguments but I couldn't make it work.

A simple example code will be appreciated.

Simply write a template function taking any SparseMatrixBase<Derived> , for instance:

template<typename Derived>
void foo(const SparseMatrixBase<Derived> &a_mat) {
    const Derived &mat(a_mat.derived());
    SparseMatrix<typename Derived::Scalar> tr_mat = mat.transpose();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM