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

function taking both SparseMatrix and MappedSparseMatrix

Tags: sparse, ref, sparsematrix, mappedsparsematrix sparse, ref, sparsematrix, mappedsparsematrix sparse, ref, sparsematrix, mappedsparsematrix
(comma "," separated)
joaoruileal
Registered Member
Posts
18
Karma
0
OS
Hello,

Which argument type should I use to pass either a SparseMatrix<> or a MappedSparseMatrix<> to functions/methods?
I know dense matrices can use the Ref<> class, but it doesn't seem to work for sparse matrices.
Additionally, I cannot use templates since some methods will implement purely virtual methods.

For now I am considering using MappedSparseMatrix<> and converting any SparseMatrix<> to a MappedSparseMatrix<> since it only requires pointer copies.
Is there an alternative in Eigen?

Best regards,
João Leal
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Ref<SparseMatrix> is on the todo list. In the meantime, you can implement the MappedSparseMatrix version, and add an overload for SparseMatrix calling the MappedSparseMatrix method through the SparseMatrix::*Ptr() accessors.
joaoruileal
Registered Member
Posts
18
Karma
0
OS
Thank you for the reply!

I just realized that I have some issues doing operations with maps to const scalars and non-const.
Sometimes I have access to pointers of const doubles which could be used to create a MappedSparseMatrix, but then I get errors.

Here is an example.
Code: Select all
Eigen::MappedSparseMatrix<const double, Eigen::ColMajor, int> m(0, 0, 0, nullptr, nullptr, nullptr);
Eigen::Matrix<double, Eigen::Dynamic, 1> v;

Eigen::Matrix<double, Eigen::Dynamic, 1> v2 = m * v; // issue here

There is an issue determining the resulting data type.

I believe the important part of the error message is:
/usr/include/eigen3/Eigen/src/Core/ProductBase.h:27:98: error: no type named ‘ReturnType’ in ‘struct Eigen::internal::scalar_product_traits<const double, double>’
typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar;

What is the best way to define this information?
joaoruileal
Registered Member
Posts
18
Karma
0
OS
Currently I am using:
Code: Select all
namespace Eigen {
namespace internal {
template<typename T> struct scalar_product_traits<const T,T>
{
  enum {
    // Cost = NumTraits<T>::MulCost,
    Defined = 1
  };
  typedef T ReturnType;
};
}
}

Not sure If this will have unforeseen side effects.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
for the record, Ref<SparseMatrix<double> > and Ref<const SparseMatrix<double> > are now supported in the devel branch. Same for Map<SparseMatrix> and Map<const SparseMatrix>

MappedSparseMatrix is now deprecated and it does not accept const scalar types.


Bookmarks



Who is online

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