Sparse matrix ternary sequential table storage representation//Xin Yang # include <stdio.h> #include <stdlib.h> #define MAXSIZE typedef int ELEMTYPE;TYPEDEF Struct{int i,j;//row subscript, column subscript elemtype e;//non 0 element value}triple;typedef struct{triple data[maxsize+1];//Not 0 Yuan ternary table, data[0] not used int mu, nu,tu;//the number of rows, columns, and non-0 elements of a matrix}tsmatrix;//create sparse matrix mint Createsmatrix (Tsmatrix *m) {int i,m,n; Elemtype e;int k;printf ("Please enter the number of rows of the matrix, number of columns, non-0 elements: (comma separated) \ n"), scanf ("%d,%d,%d", & (*m). Mu, & (*m). Nu, & (*m). Tu) ;(*m). data[0].i=0;for (i = 1; I <= (*m). Tu; i++) {do{printf ("Enter rows of%d non-0 elements in line order (1 ~%d)," "column (1 ~%d), element value: (comma separated) \ n", I, (*m). Mu, (*m). Nu); scanf ("%d,%d,%d", &m, &n, &e); K=0;if (M < 1 | | m > (*m). mu | | n < 1 | | n > ( *m). Nu) k=1;if (M < (*m). data[i-1].i | | M = = (*m). DATA[I-1].I && n <= (*m). data[i-1].j) k=1;} while (k);(*m). data[i].i = m;//row subscript (*m). DATA[I].J = n;//column subscript (*m). DATA[I].E = e;//The value corresponding to the subscript}return 1;} Destroys the sparse matrix M, all elements are empty void Destroysmatrix (Tsmatrix *m) {(*m). mu=0; (*m). nu=0; (*m). tu=0;} Output sparse matrix Mvoid Printsmatrix (Tsmatrix M) {int i;printf ("\ n%d rows,%d columns,%d non 0 elements. \ n ", M.mu, M.nu, m.tu);p rintf (" ======================\n ");p rintf ("%4s%4s%8s\n "," I "," J "," E ");p rintf (" ============= =========\n "), for (i=1;i<=m.tu;i++) printf ("%4d%4d%8d\n ", m.data[i].i, M.DATA[I].J, M.DATA[I].E);p rintf (" ======= ===============\n ");} Copied by sparse matrix M to get tint Copysmatrix (Tsmatrix M,tsmatrix *t) {(*t) =m;return 1;} Addsmatrix function to use int comp (int c1,int c2) {int i;if (C1<C2) I=1;else if (C1==C2) I=0;elsei=-1;return i;} Ask for two sparse matrices and Q=m+nint Addsmatrix (Tsmatrix m,tsmatrix n,tsmatrix *q) {Triple *mp,*me,*np,*ne,*qh,*qe;if (m.mu!=n.mu) Return 0;if (m.nu!=n.nu) return 0; (*q). Mu=m.mu; (*q). nu=m.nu; The initial value of the mp=&m.data[1];//MP to the non-0 element of the matrix M first address np=&n.data[1];//The initial value of the Np points to the non-0 element of the matrix N first address me=&m.data[m.tu];// Me points to the non-0-element end address of the matrix M ne=&n.data[n.tu];//Ne, which points to the non-0-element end address of the Matrix N qh=qe= (*q). data;//Qh, Qe's initial value points to the first address of the non-0 element of the matrix Q while (Mp <= Me && Np <= Ne) {qe++;switch (comp (mp->i,np->i)) {Case 1: *qe=*mp; Mp++;break;case 0://M, N-matrices are currently non-0-element rows equal and continue to compare columns SWITCH (Comp (mp->j,np->j)) {Case 1: *qe=*mp; Mp++;break;case 0: *QE=*MP; Qe->e+=np->e;if (! QE->E)//element value is 0, not deposited into the compression matrix qe--; mp++; Np++;break;case-1: *QE=*NP; np++;} Break;case-1: *QE=*NP; np++;}} The IF (mp>me)//matrix M elements are all processed while (Np<=ne) {QE++;*QE=*NP; np++;} The IF (np>ne)//matrix n elements are all processed while (mp<=me) {qe++;*qe=*mp; mp++;} (*q). Tu=qe-qh; The number of non-0 elements of the matrix Q return 1;} Find the difference of two sparse matrices Q=m-nint Subtsmatrix (Tsmatrix m,tsmatrix n,tsmatrix *q) {int i;for (i=1;i<=n.tu;i++) n.data[i].e*=-1; Addsmatrix (M,N,Q); return 1;} The product of the two sparse matrices q = M*nint Multsmatrix (tsmatrix m,tsmatrix N,tsmatrix *q) {//h,l are the row and column values of the matrix Q, QN is the number of non-0 elements of the matrix Q, and the initial value is 0 int i,j,h= m.mu,l=n.nu,qn=0; Elemtype *qe;if (M.NU!=N.MU) return 0; (*q). Mu=m.mu; (*q). nu=n.nu; Qe= (Elemtype *) malloc (h*l*sizeof (elemtype)); Qe is a temporary array of matrix Q//matrix Q for the element value of the row J of column i is stored in * (qe+ (i-1) *l+j-1) with an initial value of 0 for (i=0;i
A sparse matrix---C language for the data structure