Как добавить собственную разреженную матрицу с собственной плотной матрицей?

В документации Eigen указано, что поддерживается стандартные операции Sparse + Dense, но я не мог t найти какие-либо подробные примеры.

Например:

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

person Paul Baltescu    schedule 01.03.2014    source источник
comment
Это ошибка в Eigen, зарегистрированная по адресу eigen.tuxfamily.org/bz/show_bug. .cgi?id=632   -  person Jitse Niesen    schedule 26.03.2014


Ответы (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
person Jeongseok Lee    schedule 25.03.2014
comment
Извините, в моем первоначальном комментарии была опечатка. Функция addTo() решает проблему вместо использования оператора +. - person Jeongseok Lee; 25.03.2014