### CSC matrix algorithm for find the index of real matrix

Hi everybody,
I've created a CSC (compressed storage columns) Matrix , for store sparse matrix!
in similar way I've created a CRS Matrix (compressed storage row). Both of them stored all the non zero element of sparse matrix in a vector A and the difference is illustred in this link : http://www.cs.colostate.edu/~mcrob/toolbox/c++/sparseMatrix/sparse_matrix_compression.html

by the way for CRS matrix i found this alghoritm to find the corrispondent index in the original matrix (in this way I can easily print out the whole matrix)

 ``12345678910111213141516171819202122232425`` ``````template inline auto CSRmatrix::findIndex(std::size_t row, std::size_t col) const noexcept { auto jit = std::find(ja_.begin()+ia_.at(row) , ja_.begin()+ia_.at(row+1),col ); return static_cast(std::distance(ja_.begin(), jit) ); } /********************* operator overload */ template inline const T CSRmatrix::operator()(const std::size_t row, const std::size_t col) const noexcept { // if(row == col) // return a_.at(ia_.at(row)); const auto j = findIndex(row,col); if(j < ia_.at(row+1)) return a_.at(j); else return 0.0 ; }``````

Now I want to find out the same way for CSC matrix ... could you help me ? i was looking here but i didn't get the point http://www.scipy-lectures.org/advanced/scipy_sparse/csc_matrix.html

the constructor is done in this way :
 ``123456789101112131415161718192021222324252627282930`` ``````template< typename T> constexpr CSCmatrix::CSCmatrix( std::initializer_list> row) noexcept { this->rows = row.size(); auto il = *(row.begin()); this->cols = il.size(); itype i=1 , j=0 , w=0; ja_.resize(cols+1); ja_[0] = 0; std::vector> temp( row ); //w =0 ; for(size_t c = 0; c < temp[0].size(); c++) { w = 0; for(size_t r = 0; r < temp.size(); r++) { if(temp[r][c] != 0) { a_.push_back( temp[r][c] ) ; ia_.push_back(c); w++ ; } } ja_[i] = ja_[i-1] + w ; i++ ; } CSCprint(); } ``````

and the matrix is given in this form
 ``12`` ``````CSCmatrix cscm1 = {{11,12,13,14,0,0},{0,22,23,0,0,0},{0,0,33,34,35,36},{0,0,0,44,45,0}, {0,0,0,0,0,56},{0,0,0,0,0,66}};``````

I'm sorry i found the error ! : ` ia_.push_back(c); // wrong !! should push_back r ! ` then for find the index
 ``123456789`` ``````// template inline auto CSCmatrix::findIndex(std::size_t row, std::size_t col) const noexcept { auto ijt = std::find(ia_.begin()+ja_.at(col) , ia_.begin()+ja_.at(col+1), row ); return static_cast(std::distance(ia_.begin(), ijt ) ); }``````

Last edited on
typical sparse matrix storage consists of a list of row,column, value. Any value not in the list is assumed zero. If the matrix isnt really sparse, this wastes space, of course. You may figure out a clever scheme for doing this another way (there are some) but whatever you do must be easily reversible to produce the real matrix or to operate on the sparse matrix as if fully filled out.