Look at the data structure write code (20) sparse matrix (sequential storage mode)

Source: Internet
Author: User
Tags mul

When the useful information for matrices is very few, we consider compressing the matrix for storage. This involves special matrices and sparse matrices.

The special matrix refers to a certain regular matrix, the matrix we only store some useful information, the rest of the information can be obtained by the conversion formula. For example, symmetric matrices, we store elements below the main diagonal (including the main diagonal) by row, and the rest of the elements can be evaluated by the following formula.


Sparse matrix, refers to the matter does not have a certain regularity of the matrix, and the total number of useful information/matrix total is less than or equal to 0.05, we call the sparse matrix.

The following code gives the matrix fast transpose and matrix multiplication code for the "Sequential storage of row logical links" method of sparse matrices.


Please point out the code bug, the code below

Sparse Matrix.cpp: Defines the entry point of the console application. The sequential storage method for sparse matrices (row logical link data table) #include "stdafx.h" #define Matrix_max_size10000#define matrix_max_row500enum E_state{e_ State_error = 0,E_STATE_OK = 1,};typedef int elementtype;//matrix element struct struct matrixdata{int row;//row int col;//column ElementType data;//data};struct smatrix{matrixdata base[matrix_max_size];int rpos[matrix_max_row];//stores the position of the first element of each row in the array, and retains the last row of the Boundary value int rownum;//number of rows int colnum;//column number int totalnum;//total};void smatrixinit (Smatrix * matrix,int * base,int row,int col) { Matrix->rownum = Row;matrix->colnum = Col;int Totalnum = 0;for (int i = 0; i < row; i++) {int lastrowtotal = Total num;//records the total number of rows from the first line to the previous row for (int j = 0; J < Col; J + +) {ElementType data = base[i*col+j];if (data! = 0) {matrix->base[total Num].row = I;matrix->base[totalnum].col = J;matrix->base[totalnum].data = data;totalnum++;}} Matrix->rpos[i] = lastrowtotal;} Matrix->rpos[row] = totalnum;//for matrix multiplication, add boundary condition matrix->totalnum = totalnum;} Normal transpose time complexity O (M.colnum * m.totalnum)//Traverse Matrix M,Add the first column to the last column of the matrix element to the T matrix void Smatrixtranspose (Smatrix M,smatrix * t) {t->totalnum = M.totalnum;t->rownum = M.colnum; T->colnum = M.rownum;int total = 0;for (int i = 0; i < M.colnum; i++) {for (int j = 0; J < M.totalnum; J + +) {Matrix Data data = m.base[j];if (Data.col = = i) {t->base[total].row = Data.col;t->base[total].col = data.row;t->base[ Total].data = data.data;total++;}}} Fast transpose time complexity of O (M.totalnum + m.colnum) void Smatrixquicktranspose (Smatrix M,smatrix * t) {t->totalnum = m.totalnum;t-& Gt;rownum = M.colnum;t->colnum = m.rownum;if (M.totalnum = = 0) {return;} int Col[matrix_max_row] = {0};for (int i = 0; i < m.totalnum; i++)//COMPUTE matrix M Number of elements per column {Matrixdata data = M.base[i];col[da ta.col+1]++;} Computes the position of the starting element in the array for each column of the matrix M for (int i = 1; i < M.colnum; i++) {col[i] = Col[i-1] + col[i];} for (int i = 0; i < M.totalnum; i++) {Matrixdata data = m.base[i];int Colbase = Col[data.col];t->base[colbase].col = Data.row;t->base[colbase].row = Data.col;t->base[colbasE].data = data.data;col[data.col]++;} Finally set each line element address t->rpos[0] = 0;for (int i = 1; i < t->rownum; i++) {t->rpos[i] = col[i-1];}} The Matrix multiplies e_state Smatrixmult (smatrix m1,smatrix M2,smatrix * result) {if (m1.colnum! = m2.rownum)//excludes illegal cases: {return e_state_error;} Result->rownum = M1.rownum;result->colnum = M2.colnum;result->totalnum = 0;if (M1.totalNum * m2.totalNum! = 0) {f  or (int m1row = 0; M1row < m1.rownum; m1row++) {int m1end = M1.rpos[m1row+1];int Colcount[matrix_max_row] = {0};for (int M1start = M1.rpos[m1row]; M1start < M1end; m1start++) {Matrixdata M1data = m1.base[m1start];int col = m1data.col;for (int m2start = M2.rpos[col]; M2start < M2.rPos [Col+1]; m2start++) {Matrixdata m2data = M2.base[m2start];colcount[m2data.col] + = M1data.data * m2data.data;}} Result->rpos[m1row] = result->totalnum;for (int col = 0; col < m2.colnum; col++) {if (Colcount[col]! = 0) {result-& Gt;base[result->totalnum].col = Col;result->base[result->totalnum].row = M1Row;Result->base[result->totalnum].data = Colcount[col];result->totalnum + +;}}} Result->rpos[result->rownum] = result->totalnum;} return E_STATE_OK;} Traversal matrix void Smatrictraverse (Smatrix matrix) {int rowNum = 0;printf ("--------------traversal begins------------------------\ n"); for (int i = 0; i < Matrix.totalnum; i++) {Matrixdata data = Matrix.base[i];p rintf ("%d rows%d:%d\n", Data.row+1,data.col +1,data.data);} printf ("--------------traversal end------------------------\ n");} int initdata[5][10] = {{1,0,0,0,0,0,0,0,0,0},{0,0,2,0,0,0,0,0,5,0},{0,0,0,3,0,0,0,0,0,0},{0,2,0,0,0,0,0,0,0,0},{ 1,0,0,0,0,0,0,0,0,9}};int initData2 [10][2] = {{1,0},{0,0},{0,0},{0,0},{0,0},{0,6},{0,0},{0,0},{5,0},{0,0},};int _ Tmain (int argc, _tchar* argv[]) {Smatrix matrix; Smatrixinit (&matrix, (int *) initdata,5,10); Smatrictraverse (Matrix);p rintf ("--------------normal transpose-----------------------\ n"); Smatrix Tmatrix; Smatrixtranspose (Matrix,&tmatrix); Smatrictraverse (Tmatrix);p rintf ("--------------quick transpose-----------------------\ n"); SmaTrix Qtmatrix; Smatrixquicktranspose (Matrix,&qtmatrix); Smatrictraverse (Qtmatrix);p rintf ("--------------matrix multiplied-----------------------\ n"); Smatrix m2; Smatrixinit (&m2, (int *) initdata2,10,2); Smatrix Mul; Smatrixmult (Matrix,m2,&mul); Smatrictraverse (mul); return 0;}
Run the code,



Look at the data structure write code (20) sparse matrix (sequential storage mode)

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.