This is a feature of map used in SparseMatrix. When an element is not in
the map, the operator
map[i] = 0.
adds this element. One could use find to check if this element exist.
For example I have this function for a set but similar operation should
work for a map.
bool isDefined(const set<string> &opt, const string &name)
if (opt.find(name) == opt.end())
Well, it would be better to return an iterator to use it in the case the
element if found.
You can remove zero elements with erase as follows:
for (int i = 0; i < NRows(); ++i)
for (map<int, double>::iterator j = (*this)[i].begin(); j !=
if ((*j).second == 0.)
On 26.04.2013 23:44 Romain Veltz said the following:
> I am using the SparseMatrix class provided at
and I have an issue.
> It is said that when trying to access an element A[i][j] of a matrix A, if
> this element does not exist, then it is created.
> Here is my issue: I am going through a lot of elements, most of them are
> zero valued. Hence, by looking at
> double val = A[i][j];
> it will create a zero value at (i,j) if this element did not exist.
> Is it possible to remove it from the list then, ie to remove all zero
> Indeed, the memory storage ramps up when looking at different elements of
> the matrix...
> Thank you for your help,