Re: [eigen] Copy into a MappedSparseMatrix

[ Thread Index | Date Index | More Archives ]

My version of Eigen is 3.2.4, so I think I need to use MappedSparseMatrix, for now.

I wrote the following method that works, based on your std::copy() suggestion:

namespace Eigen {

  template< typename _Scalar, int _Flags, typename _Index>
  MappedSparseMatrix< _Scalar, _Flags, _Index >&
  copy( const SparseMatrix< _Scalar, _Flags, _Index >& src,
    MappedSparseMatrix< _Scalar, _Flags, _Index >& dest )
    assert( src.isCompressed() );

    // The Flags already match for row/column major
    assert( dest.rows() == src.rows() &&
        dest.cols() == src.cols() &&
        dest.nonZeros() == src.nonZeros() );

    std::copy( src.valuePtr(),
           src.valuePtr() + src.nonZeros(),
           dest.valuePtr() );

    std::copy( src.innerIndexPtr(),
           src.innerIndexPtr() + src.nonZeros(),
           dest.innerIndexPtr() );

    std::copy( src.outerIndexPtr(),
           src.outerIndexPtr() + 1 + src.outerSize(),
           dest.outerIndexPtr() );

    return dest;
  }// operator=( MappedSparseMatrix, SparseMatrix )

So,  copy( a, b ) works, but copy( a*b, c ) does not work.  I get the

 error: no matching function for call to
 ‘copy(const Type, Eigen::MappedSparseMatrix<double, 1>&)’
       Eigen::copy( jmat.transpose() * jmat, hmat );
 note: candidate is:
note: template<class _Scalar, int _Flags, class _Index> Eigen::MappedSparseMatrix<_Scalar, _Flags, _Index>& Eigen::copy(const Eigen::SparseMatrix<_Scalar, _Options, _Index>&, Eigen::MappedSparseMatrix<_Scalar, _Flags, _Index>&)
   copy( const SparseMatrix< _Scalar, _Flags, _Index >& src,
: note: ‘const Type {aka const Eigen::SparseSparseProduct<const Eigen::Transpose<Eigen::MappedSparseMatrix<double, 1> >, Eigen::SparseMatrix<double, 0, int> >}’ is not derived from ‘const Eigen::SparseMatrix<_Scalar, _Options, _Index>’
       Eigen::copy( jmat.transpose() * jmat, hmat );

Could I have coded copy() in such a way to include SparseSparseProduct
as well as SparseMatrix as the src argument to copy?


On 07/16/2015 04:38 AM, Christoph Hertzberg wrote:
On 16.07.2015 at 07:35, Norman Goldstein wrote:
I have the following two matices:

MappedSparseMatrix<...> hmat;  // Points to some data off to the side

MappedSparseMatrix is deprecated, you should use Map<SparseMatrix<...> >

SparseMatrix<...> prod;                 // Contains data, same size and
structure as hmat

I want to copy the prod data over to where hmat is pointing:

hmat = prod;

I'm not sure that this is a good idea (what behavior would you expect, if the structure does not match?). If you are certain that the structure is the same, you could directly copy the contents of prod.valuePtr() to hmat.valuePtr() (using std::copy).


Mail converted by MHonArc 2.6.19+