Sparse Matrix and sparse matrix compression storage, Matrix

Source: Internet
Author: User

Sparse Matrix and sparse matrix compression storage, Matrix

The unprocessed sparse matrix is actually a special two-dimensional array. Most elements in the array are 0 or other types of illegal values, and there are only a few non-zero elements.

To achieve compression and storage, only non-zero elements of the sparse matrix can be stored. When you store a non-zero element in a sparse matrix, you must store the row and column numbers and element values of the element.

We can encapsulate a triple class to store these elements.

// Triples template <class T> struct Triple {size_t _ row; // row size_t _ col; // column T _ value; // value Triple <T> :: triple () // defines the construction function {} Triple (size_t row, size_t col, T value) without parameters: _ row (row), _ col (col ), _ value (value ){}};

Create a sparse matrix. Using containers, you can easily store these elements, which is equivalent to using a dynamic array for storage. It is required to store data in the row-first order, so that non-zero elements can be printed in order of the row and column when the sparse matrix is printed.

Template <class T> // use a container to compress the SparseMatrix for storage. <T>: SparseMatrix (const T * array, size_t row, size_t col, const T & invalid) // initialization: _ rowMatrix (row), _ colMatrix (col), _ invalid (invalid) {for (size_t I = 0; I <_ rowMatrix; ++ I) {for (size_t j = 0; j <_ colMatrix; ++ j) {if (array [I * col + j]! = Invalid) {Triple <T> cur (I, j, array [I * col + j]); _ array. push_back (cur );}}}}

Column order transpose Method: Transpose with the column order of the matrix. In this way, the triples container sequence obtained after transpose is stored with row-first. The time complexity is O (_ colMatrix * _ array. size ())

Template <class T> // transpose SparseMatrix in column order <T> SparseMatrix <T>: Transport () {assert (_ array. size ()! = 0); SparseMatrix <T> ret; ret. _ rowMatrix = _ colMatrix; ret. _ colMatrix = _ rowMatrix; ret. _ invalid = _ invalid; ret. _ array. reserve (this-> _ array. size (); for (size_t j = 0; j <_ colMatrix; j ++) {size_t index = 0; while (index <_ array. size () {if (_ array [index]. _ col = j) {Triple <T> tp (_ array [index]. _ col, _ array [index]. _ row, _ array [index]. _ value); ret. _ array. push_back (tp);} index ++;} if (this-> _ array. size () = ret. _ array. size () {break;} return ret ;}

  

Fast transpose: Determine the position of the first element in each column of the Matrix in the container in advance. When the sparse matrix is transposed, the elements are directly placed in the appropriate position of the new container by traversing the original container. The time complexity is O (_ colMatrix + _ array. size ())

Before transpose, you must first determine the number of non-zero elements in each column of the original matrix, and then find the correct position of non-zero elements in each column in the new container.

Set two integer arrays RowCounts [_ colMatrix] And RowStart [_ colMatrix] To respectively store the number of non-zero elements in each column of the triple container and the first non-zero element in each column in the new container. location.

RowStart [0] = 0; RowStart [col] = RowStart [col-1] + RowCounts [col-1];

Column number 0 1 2 3 4
RowCounts [col] 2 0 2 0 2
RowStart [col] 0 2 2 4 4
Template <class T> SparseMatrix <T>: FastTranaport () // fast transpose {assert (_ array. size ()! = 0); size_t index = 0; SparseMatrix <T> ret; ret. _ rowMatrix = _ colMatrix; ret. _ colMatrix = _ rowMatrix; ret. _ invalid = _ invalid; ret. _ array. resize (_ array. size (); int * RowCounts = new int [_ colMatrix]; int * RowStart = new int [_ colMatrix]; memset (RowCounts, 0, _ colMatrix * sizeof (int); memset (RowStart, 0, _ colMatrix * sizeof (int); for (size_t I = 0; I <_ array. size (); I ++) {RowCounts [_ array [I]. _ col] ++;} RowStart [0] = 0; for (size_t I = 1; I <_ colMatrix; I ++) {RowStart [I] = RowStart [I-1] + RowCounts [I-1];} Triple <T> tp; for (size_t I = 0; I <_ array. size (); I ++) {tp. _ row = _ array [I]. _ col; tp. _ col = _ array [I]. _ row; tp. _ value = _ array [I]. _ value; ret. _ array [RowStart [_ array [I]. _ col] ++] = tp;} delete [] RowCounts; delete [] RowStart; return ret ;}

  

 

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.