Matrix (Matrix) is a rectangular array of plural or Real numbers collection.
sparse matrices : Valid data is much less than invalid data.
Eg: invalid data specified is 0
1 0 0) 0 0
0 0 0) 0 2
0 3 0) 0 0
4 0 0) 0 0
The above matrix can be called a sparse matrix
we have seen and used matrices when we learn C language, in factit can be translated into a two-dimensional array in our programming language, because the sparse matrix has very few valid data, the full storage is very expensive, so we choose to store only its valid data bits , so we can use a one-dimensional array to store it directly, but we have to let others look at it and know it is a matrix, So we have to store it in a ternary group where it is located!
The ternary group is defined as follows:
Template<class t>struct triple{t _value;//valid value int _row;//The value of the row int _col;//the value column Triple (const t& data = T (), int m = 0, int n = 0): _value (data), _row (M), _col (n) {}};
The matrix is defined as follows:
Class Spamatrix{public:spamatrix (t* arr = NULL, int m = 0, int n = 0, int inva = 0): _row (M), _col (n), _invalid (inva) {int I Ndex = 0;for (int i = 0; i < m; i++) {for (int j = 0; J < N; j + +) {if (arr[i*n + j]! = _invalid) {_a.push_back (TRIPLE&L T T> (Arr[i*n + j], I, j)); _n + +;}}} private:vector< triple<t> > _a;//storage ternary int _invalid;//specified invalid value int _row;//matrix number of rows int _col;//matrix column};
In the matrix, we can often see an operation, that is, the matrix transpose, even if the a[i][j] = A[j][i], we previously in the storage matrix when the row-first storage method, but, after the transpose, our matrix is the original behavior of its column, how do we do it?
Method One: Normal transpose
* * We traverse the matrix, put the first column of data into the new matrix, then the second column, the third column, and so on, so that we can get a new matrix
Here is an example of the code (intercepting the important part of the code):
Normal transpose spamatrix<t>::spamatrix<t> Ordinarytrans () {int count = 0;int index = 0; Spamatrix<t> sp;//uses a temporary variable to store the transpose matrix for (int j = 0; J < _col; J + +) {for (int index = 0; index < _a.size (); index + +) {if (_a[index]._col = = j) {//per push only required column sp._a.push_back (_a[index]); sp._a[index]._row = _a[index]._col;sp._a[index ]._col = _a[index]._row;}}} Update the matrix column information after transpose sp._row = _col;sp._col = _row;return sp;}
Method Two: Quick transpose
* * We save the beginning and the number of valid data for each column of the original matrix, as the starting position and number of valid data for each row of the new matrix, when we want to get some data, we can get our new matrix by using the starting position of the previous line + the number of valid data.
Let's take a look at the code example:
Quick Transpose Spamatrix<t>::spamatrix<t> fasttrans () {//Start position of statistic valid data int *rowstart = new int[_col];//statistics The number of valid data for each row in the matrix after the transpose Int *rowcount = new int[_col];memset (rowcount, 0, sizeof (int) *_col); memset (rowstart, 0, sizeof (int) *_col); size_t index = 0;// set rowcountwhile (Index < _a.size ()) {rowcount[_a[index]._col]++;index++;} set rowstartrowstart[0] = 0;for (int i = 1; i < _col; i++) {rowstart[i] = rowstart[i - 1] + rowcount[i - 1];} Construct the matrix after transpose Spamatrix<t> sptrans;sptrans._row = _col;sptrans._col = _row;sptrans The ._n = _n;//index value has been changed and must be re-used to be equal to 0index = 0;//here the use of subscript access must open up space, but if you use Push_back () can not open up ~ Sptrans._a.resize (_a.size ()); while (index < _ A.size ()) {Int rowindex = _a[index]._col;//Here note reference int& rowst = rowstart[rowindex];sptrans._a[rowst]._value = _a[ Index]._value;sptrans._a[rowst]._col = _a[index]._row;sptrans._a[rowst]._row = _a[index]._ col;index++;//the location must be updated each time, otherwise error rowst++;} Return sptrans;}
The above is my own implementation of the transpose code, although the code slag, but there is a fan of confidence to show you, I asked you to fear 650) this.width=650; "Src=" Http://img.baidu.com/hi/bobo/B_0037.gif " alt= "B_0037.gif"/>
This article is from the "Zimomo" blog, make sure to keep this source http://zimomo.blog.51cto.com/10799874/1764814
Compressed storage and transpose algorithm for sparse matrices