Compact storage and fast transpose of sparse matrices

Source: Internet
Author: User
Tags compact

When a matrix is a sparse matrix, the number of valid data is much less than the invalid data, so if a matrix all storage will waste space, you can only store valid data, invalid data as a marker

The code is as follows:

#include  <iostream> #include  <vector>using namespace std;// You can use a ternary group to store the information for valid data template<class t>struct triple{ size_t _row; size_t _col;  t _value;};/ /coefficient Matrix class  template<class T> class SparseMatrix { public:   Sparsematrix (t* arr, size_t rowsize, size_t colsize, const t&  Invaild);   void display ();   sparsematrix<t>& transpose ();   Sparsematrix<t> fasttranspose ();  protected:  vector<triple<t>> _ array;  size_t _rowsize;  size_t _colsize;  t _invaild; }; The   //can be used sequentially to store valid data  template<class t> sparsematrix<t>::sparsematrix (T*  arr, size_t rowsize, size_t colsize, const t& invaild)    :_invaild (Invaild)   , _rowsize (rowsize)   ,_colsize (colsize)  {  size_t rpos = 0;   for (rpos = 0; rpos < rowsize; rpos++)   {   size_t  cpos = 0;   for (Cpos = 0; cpos < colsize; cpos + +)    {    if (arr[(rpos * colsize)  + cpos] !=  invaild)     {     Triple<T> t;      t._row = rpos;     t._col = cpos;      t._value = arr[(rpos * colsize)  + cpos];      _array.push_back (t);         }   }   } }  //print matrix  template <class t> void sparsematrix<t>: :D isplay ()  {   Size_t rpos = 0;  size_t index = 0;  for (rpos = 0 ;  rpos < _rowsize; rpos++)   {   size_t cpos = 0 ;       for (cpos = 0; cpos < _colsize; cpos++)    {    if ((Index < _array.size ())  &&  (RpoS  == _array[index]._row)  &&  (cpos == _array[index]._col)      {     cout<<_array[index]._value<< " ";      index++;    }    else     cout <<_invaild<< " ";   }   cout<<endl;  }   cout<<endl; }  //Transpose  template<class T>SparseMatrix<T>&  sparsematrix<t>:: Transpose () { size_t i = 0; size_t j = 0; for (i = 0;  i < _array.size ();  i++)  {  for (j = i+1; j <  _array.size ();  j++)   {   if (_array[i]._col > _array[j]._col)     swap (_array[i],_array[j]);   }  swap (_array[i]._row,_array[i]._col) ;  }  swap (_rowsize,_colsize);  return *this;} Quick Transpose Template<class t>sparsematrix<t> sparsematrix<t>::fasttranspose () { size _t* rowcounts = new size_t[_colsize]; size_t index = 0; for (Index  = 0; index < _array.size ();  index++)  {  rowcounts[_array[ index]._col]++;   //statistics The number of valid data for each row of the matrix after transpose  } size_t* rowstart = new  Size_t[_colsize]; rowstart[0] = 0; for (INDEX&Nbsp;= 1; index < _colsize; index++)  {  rowstart[index] =  rowcounts[index-1] + rowstart[index-1]; }   //the starting position of each row in the matrix in the compression matrix after the transpose of the statistics  vector <triple<t>> arr; size_t count = 0; for (size_t i = 0;  i < _colsize; i++)  {  for (index = 0; index <  _array.size ();  index++)   {   if (rowcounts[i] != 0)     {    if (_array[index]._col == rowstart[i])     {      triple<t> t;     t._col = _array[index]. _row;     t._row = _array[index]._col;     t._ Value = _array[index]._value;     arr.push_back (t);     }   }&nBsp;  else    break;  } } swap (_array,arr);  swap (_ rowsize,_colsize);  return *this;}


This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1750630

Compact storage and fast transpose of sparse matrices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.