如何添加一个特征密集矩阵的特征SparseMatrix?

时间:2014-03-01 10:34:38

标签: eigen eigen3

Eigen documentation表示支持常见的Sparse + Dense操作,但我找不到任何详细的例子。

例如:

  Eigen::SparseMatrix<int> x(10, 10);
  Eigen::Matrix<int, 10, 10> y;

  Eigen::Matrix<int, 10, 10> z = x + y;

给了我以下错误,我无法解决这个问题:

test_dense_sparse_sum.cpp: In function ‘int main()’:
test_dense_sparse_sum.cpp:33:38: error: no match for ‘operator+’ in ‘x + y’
test_dense_sparse_sum.cpp:33:38: note: candidates are:
/home/paulb/mylibs/eigen-3.2.0/Eigen/src/SparseCore/../plugins/CommonCwiseBinaryOps.h:27:1: note: template<class OtherDerived> const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<typename Eigen::internal::traits<T>::Scalar>, const Derived, const OtherDerived> Eigen::SparseMatrixBase::operator+(const Eigen::SparseMatrixBase<OtherDerived>&) const [with OtherDerived = OtherDerived, Derived = Eigen::SparseMatrix<int, 0, int>, typename Eigen::internal::traits<T>::Scalar = int]
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_iterator.h:327:5: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.h:2306:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:694:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:710:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.h:2343:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.h:2359:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/complex:321:5: note: template<class _Tp> std::complex<_Tp> std::operator+(const std::complex<_Tp>&, const std::complex<_Tp>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/complex:330:5: note: template<class _Tp> std::complex<_Tp> std::operator+(const std::complex<_Tp>&, const _Tp&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/complex:339:5: note: template<class _Tp> std::complex<_Tp> std::operator+(const _Tp&, const std::complex<_Tp>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/complex:440:5: note: template<class _Tp> std::complex<_Tp> std::operator+(const std::complex<_Tp>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_bvector.h:266:3: note: std::_Bit_iterator std::operator+(std::ptrdiff_t, const std::_Bit_iterator&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_bvector.h:266:3: note:   no known conversion for argument 1 from ‘Eigen::SparseMatrix<int, 0, int>’ to ‘std::ptrdiff_t {aka long int}’
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_bvector.h:352:3: note: std::_Bit_const_iterator std::operator+(std::ptrdiff_t, const std::_Bit_const_iterator&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_bvector.h:352:3: note:   no known conversion for argument 1 from ‘Eigen::SparseMatrix<int, 0, int>’ to ‘std::ptrdiff_t {aka long int}’

1 个答案:

答案 0 :(得分:1)

试试这个!

Eigen::SparseMatrix<int> x(10, 10);
Eigen::Matrix<int, 10, 10> y;

Eigen::Matrix<int, 10, 10> z = y;
x.addTo(z);  // z = x + y