CKM
CKM

Reputation: 1971

Addition and subtraction not working with Armadillo sparse matrices

I don't understand why + and - operations are not working on Armadillo sparse matrices while * and / are working correctly. (As per doc, + and - should work as well link).

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include<armadillo>  

using namespace std;
using namespace arma;

int main(int argc, char** argv) {
    sp_mat A(5,6);
    A(0,0) = 1;
    A(1,0) = 2;
    cout << 2 + A << endl;
    return 0;
}

see the error below.

In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
             from /usr/include/c++/4.8/bits/char_traits.h:39,
             from /usr/include/c++/4.8/ios:40,
             from /usr/include/c++/4.8/ostream:38,
             from /usr/include/c++/4.8/iostream:39,
             from demo.cpp:1:
   /usr/include/c++/4.8/bits/stl_iterator.h:327:5: note:            template<class _Iterator> typename std::reverse_iterator<_Iterator>::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator-(const reverse_iterator<_Iterator>& __x,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:327:5: note:   template argument deduction/substitution failed:
demo.cpp:28:9: note:   mismatched types ‘const std::reverse_iterator<_Iterator>’ and ‘int’
 cout<<2-A<<endl;

Upvotes: 0

Views: 1742

Answers (3)

hbrerkere
hbrerkere

Reputation: 1605

Adding a scalar to a matrix is equivalent to adding the scalar to every element in the matrix. In a sparse matrix, most of the elements are zero, and are not explicitly stored, which significantly reduces memory usage.

So, adding a scalar to a sparse matrix is ill advised, as in effect it would turn a sparse matrix into a dense matrix, which defeats the purpose of having a sparse matrix in the first place (reducing memory use).

Given the above observation, it looks like Armadillo developers prevented this problem from happening by simply not defining the addition of scalars to sparse matrices. Adding scalars to dense matrices works perfectly fine.

Upvotes: 6

downhillFromHere
downhillFromHere

Reputation: 1977

It's simply not supported (yet?). There's still hope since the doc says that support for sparse matrices in this version is preliminary.

In the mean time, you can make use of the in-place addition and substraction, through a submatrix view, e.g.,

using namespace arma;
Mat<double> m(5, 6, fill::ones);
SpMat<double> spm(m);
spm(span::all, span::all) += 2;

Upvotes: 1

UpAndAdam
UpAndAdam

Reputation: 5467

It says right on the documents for their API what '+' and '-' are set to do. I think you should read the documentation, figure out what is you are trying to do, and ask that question instead.

http://arma.sourceforge.net/docs.html#SpMat

operators: + - * / % == != <= >= < >
Overloaded operators for mat, vec, rowvec and cube classes

Meanings:

'+' Addition of two objects
'-' Subtraction of one object from another or negation of an object
'/' Element-wise division of an object by another object or a scalar
'*' Matrix multiplication of two objects; not applicable to the cube class unless multiplying a cube by a scalar

Based on your error it's pretty obvious that they didn't define the operator for a sp_mat and a scalar, they defined it for a sp_mat and an sp_mat.

I'm not pretending to be a master of this topic; I just read the API. You should probably take a look at the headers.

Upvotes: 0

Related Questions