Sparse matrices: matrices in which most elements are 0 (this is the main sequence of lines)
Ternary representation method for sparse matrices:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/E1/wKioL1cLsP7QRQIfAAAHqmH0VZY633.png "title=" DSG title. png "alt=" Wkiol1clsp7qrqifaaahqmh0vzy633.png "/>
Type structure:
Template <typename t>struct triple{int _row;int _col; T _value;}; Template <typename T>class Sparsematrix{public:sparsematrix<t>::sparsematrix (); Sparsematrix (const t* Array, size_t row, size_t col, const t& Invalid); ~sparsematrix (); void Display () const; Sparsematrix<t> Transport () const; Sparsematrix<t> fasttransport () const;protected:vector<triple<t>> _array;size_t _rowCount;size_t _colcount; T _invalid;};
Code to implement compressed storage:
Sparse Matrix Template <typename t>sparsematrix<t>::sparsematrix () {}template <typename t>sparsematrix<t>::sparsematrix (const t* array, size_t row, size_t col, const t& invalid): _rowcount (Row), _colcount (col), _invalid (invalid) { ASSERT (Array);for (size_t i = 0; i < row; ++i) {for (size_t &NBSP;J&NBSP;=&NBSP;0;&NBSP;J&NBSP;<&NBSP;COL;&NBSP;++J) {if (array[i*col + j] != Invalid) {this->_array.push_back ({ i, j, array[i*col + j] });}}} Template <typename t>sparsematrix<t>::~sparsematrix () {}template <typename t >void sparsematrix<t>::D isplay () const{size_t size = this->_array.size (); size_t iCount = 0;for (size_t i = 0; i < this->_rowcount; ++i) {for (size_t j = 0; j < this->_colcount; ++j) {if (icount < size & & i == this->_array[icount]._row && j == this->_array[ Icount]._col) {cout << this->_array[icount]._value << " "; ++iCount;} else{cout << this->_invalid << " ";}} Cout << endl;}}
Transpose of sparse matrices:
1) Sequence increment transpose method: Find all elements of line I: Scan the ternary table A from start to finish, find out the ternary group of all _col==i, transpose and put into the ternary Group table B. The code is implemented as follows:
Template <typename t>sparsematrix<t> sparsematrix<t>::transport () const{sparsematrix<t> ret ; ret._rowcount = This->_colcount;ret._colcount = This->_rowcount;ret._invalid = this->_invalid;size_t size = This->_array.size (); for (size_t col = 0, col < this->_colcount; ++col) {for (size_t iCount = 0; iCount < size; ++icount) {if (This->_array[icount]._col = = col) {ret._array.push_back ({this->_array[icount]._col, this->_ Array[icount]._row, This->_array[icount]._value});}} return ret;}
2) One position quick Transpose method
In Method 1, in order to make the ternary table B of the transpose matrix continue to be stored sequentially, the ternary table A of the matrix being transpose must be scanned multiple times. In order to be able to position the elements of the ternary table A at once to the correct position of the triples B, the following data needs to be calculated beforehand:
i) to transpose matrix ternary table a the total number of non-0 elements in each column, that is, the total number of 0 elements of each row of the matrix ternary element B after the transpose
II) The first non-0 element in each column of the matrix to be placed in the correct position in the ternary table B, that is, the correct position of the first non-0 element in the triples B in each row of the transpose matrix
For this purpose, you need to set two arrays for num[] and pos[], where Num[col] is used to hold the total number of elements in the tuple table a col 0 element, Pos[col] is used to store the first non-0 element in the Col column of the previous Ternary group table A in the tuple table B storage location.
Num[col] Calculation method: The ternary table A is scanned once, for the element in which the column number is col, the corresponding num array is labeled COL element plus 1.
Pos[col] Method of calculation:
i) pos[0] = 0, which represents the subscript value of the first non-0 element of the ternary table A with a column value of 0 in the Triples table B.
II) Pos[col] = Pos[col-1] + num[col-1], wherein 1<=col<a.size ();
eg
0 1 9 0 0 0 0
0 0 0 0 0 0 0
3 0 0 0 0 4 0
0 0 2 0 0 0 0
0 8 0 0 0 0 0
5 0 0 7 0 0 0
| col |
0 |
1 |
2 |
3 |
4 |
|
6 |
| num[col] |
2 |
2 |
2 |
1 |
0 |
|
0 |
| Pos[col] |
0 |
2 |
4 |
6 |
7 |
7 |
8 |
Code implementation:
Template <typename t>sparsematrix<t> sparsematrix<t>::transport () const{ sparsematrix<t> ret;ret._rowcount = this->_colcount;ret._colcount = this- >_rowcount;ret._invalid = this->_invalid;size_t size = this->_array.size (); for (Size_t col = 0; col < this->_colcount; ++col) {for ( Size_t icount = 0; icount < size; ++icount) {if (this->_array[ Icount]._col == col) {ret._array.push_back ({ this->_array[icount]._col, this->_array[ icount]._row, this->_array[icount]._value });}} Return ret;} Template <typename t>sparsematrix<t> sparsematrix<t>::fasttransport () const{ sparsematrix<t> ret;ret._rowcount = this->_colcount;ret._colcount = this- >_rowcount;ret._invalid = this->_invalid;size_t size = this->_array.size (); ret._array.resize (size); Vector<int> num (this->_ ColCount); Vector<int> pos (this->_colcount); //pos[i] = pos[i-1]+num[i-1] i >0for (size_t i = 0; i < size; ++i) {++num[this->_array[i]._ Col];} for (Size_t col = 1; col < this->_colcount; ++col) {Pos[col] &NBSP;=&NBSP;POS[COL&NBSP;-&NBSP;1]&NBSP;+&NBSP;NUM[COL&NBSP;-&NBSP;1];} for (size_t i = 0; i < size; ++i) {Ret._array[pos[this->_array[i] . _col]++] = { this->_array[i]._col, this->_array[i]._row, this->_array[i]._ value };} Return ret;}
Operation Result:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/E4/wKiom1cLvL2gXwaYAAA58epvyB0736.png "title="]$40pk % (h9[' u6k}) jcjwh6.png "alt=" Wkiom1clvl2gxwayaaa58epvyb0736.png "/>
Compressed storage and transpose of sparse matrices