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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
template<typename T>
inline auto CSRmatrix<T>::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::size_t>(std::distance(ja_.begin(), jit) );

}

/*********************            operator overload
                                                                                          */

template<typename T>
inline const T CSRmatrix<T>::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
thanks in advance

the constructor is done in this way :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template< typename T> 
constexpr CSCmatrix<T>::CSCmatrix( std::initializer_list<std::vector<T>> 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<std::vector<T>> 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
1
2
CSCmatrix<int> 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
1
2
3
4
5
6
7
8
9
//
template<typename T>
inline auto CSCmatrix<T>::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::size_t>(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.

Topic archived. No new replies allowed.