2014-03-01 147 views
1

Eigen documentation表示支持常见的Sparse + Dense操作,但我找不到任何详细的示例。如何使用特征密集矩阵添加特征SparseMatrix?

例如:

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}’ 
+0

这在本征的错误,记录在http://eigen.tuxfamily.org/bz/show_bug.cgi?id=632 –

回答

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 
+0

这是如何解决的任择议定书的问题? – sgress454

+0

对不起,我原来的评论中有一个错字。 addTo()函数解决了问题,而不是使用+运算符。 –

+0

命令“x.addTo(z)”也可以写为“z + = x”。 –