Re: [matrixprogramming] SparseMatrix : deallocating some elements

34 views

Evgenii Rudnyi

Apr 27, 2013, 3:19:58 AM4/27/13
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())
return false;
else
return true;
}

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:

void SparseMatrix::removeZeros()
{
for (int i = 0; i < NRows(); ++i)
{
for (map<int, double>::iterator j = (*this)[i].begin(); j !=
(*this)[i].end(); )
{
if ((*j).second == 0.)
(*this)[i].erase(j++);
else
++j;
}
}
setProperties();
}

Evgenii

On 26.04.2013 23:44 Romain Veltz said the following:
> Hi,
>
> I am using the SparseMatrix class provided at
> http://matrixprogramming.com/2011/06/class-sparsematrix 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
> elements?
>
> Indeed, the memory storage ramps up when looking at different elements of
> the matrix...
>
> Thank you for your help,
>
> Romain
>