Hello everyone

Lately I've developing a MATRIX class. It went fine (mostly), but now I'm working on a SPARSE MATRIX.(Those unfamiliar with sparse matrix, check the end for a quick explanation). My problem is the following:

When accessing element i,j of the matrix, the user should call something like A(i,j), i.e.

Also, the user could like to change the value of the element i,j, so he would callCode:`cout << A(i,j);`

The point is, in the first use, I can return the element itself and in the second I would have to return the reference to the element, so the user could change it.Code:`A(i,j) = 2;`

So I wrote the code:

I found out only now that the second function was never called. But in the case of the MATRIX, it meant nothing, for everything worked fine.Code:`double & MATRIX::operator () (int x, int y)`

{

return (val[(x - 1) * n_col + y - 1]); //matrix is stored using linearization strategy

}

double MATRIX::operator () (int x, int y) const

{

return (val[(x - 1) * n_col + y - 1]);

}

But in the case of the SPARSE matrix, my functions had to change a little. It became something like

Details have been hid to help visualization. As you can see, I must create a zero element and pass reference to it so the user can change (and I hope the user don't pass another 0.0). The mess here is that anytime it access an element to read, it creates a zero valued element in the matrix and stores it, and that what's wrong.Code:`double & SPARSE::operator () (int x, int y)`

{

//...

//Search for the element, if found return it, if not, create the element and pass the reference

CREATE (0.0, x, y, k);

return *(val.begin () + k);

}

double SPARSE::operator () (int x, int y) const

{

//...

//Search for the element, if found return it, if not, create the element and pass the reference

return 0.0;

}

If anyone can give me a tip on how to use the same function operator() to both tasks, I would be very happy. And if it's not possible, I would be happy to know that as well, thank you very much.

Nepper271.

SPARSE MATRIX

A matrix is said to be sparse if there are many zero elements in it, in fact, many more zeros than non-zeros. As this kind of matrix appears widely on various applications, it's fundamental to research better ways to deal with it. The first thing to consider is thatYou store only the non-zero elementos of the matrix and the position they are held. For instance, the matrixstoring all the elements of the matrix is not useful.

Code:`M =`

7 0 0

0 0 8

0 9 0

couldbe stored as

That's all for nowCode:`val = 7 8 9`

lin = 1 2 3

col = 1 3 2