1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #include <stdio.h> #define MAXSIZE 1200 #define MAXRC 100 #define ElemType int typedef struct { int i, j; ElemType e; }Triple;
typedef struct { Triple data[MAXSIZE + 1]; int rpos[MAXRC + 1]; int mu, nu, tu; }RLSMatrix;
RLSMatrix MultSMatrix(RLSMatrix A, RLSMatrix B, RLSMatrix C) { if (A.nu != B.mu) return C; C.mu = A.mu; C.nu = B.nu; C.tu = 0; if (A.tu * B.tu == 0) return C; else { int arow; int ccol; for (arow = 1; arow <= A.mu; arow++) { int ctemp[MAXRC + 1]; for (int i = 0; i < MAXRC + 1; i++) { ctemp[i] = 0; } C.rpos[arow] = C.tu + 1; int tp; if (arow < A.mu) tp = A.rpos[arow + 1]; else tp = A.tu + 1;
int p; int brow; for (p = A.rpos[arow]; p < tp; p++) { brow = A.data[p].j; int t; if (brow < B.mu) t = B.rpos[brow + 1]; else t = B.tu + 1; int q; for (q = B.rpos[brow]; q < t; q++) { ccol = B.data[q].j; ctemp[ccol] += A.data[p].e * B.data[q].e; } } for (ccol = 1; ccol <= C.nu; ccol++) { if (ctemp[ccol]) { if (++C.tu > MAXSIZE) return C; else { C.data[C.tu].e = ctemp[ccol]; C.data[C.tu].i = arow; C.data[C.tu].j = ccol; } } } } return C; } }
int main(int argc, char* argv[]) { RLSMatrix M, N, T;
M.tu = 4; M.mu = 3; M.nu = 4;
M.rpos[1] = 1; M.rpos[2] = 3; M.rpos[3] = 4;
M.data[1].e = 3; M.data[1].i = 1; M.data[1].j = 1;
M.data[2].e = 5; M.data[2].i = 1; M.data[2].j = 4;
M.data[3].e = -1; M.data[3].i = 2; M.data[3].j = 2;
M.data[4].e = 2; M.data[4].i = 3; M.data[4].j = 1;
N.tu = 4; N.mu = 4; N.nu = 2;
N.rpos[1] = 1; N.rpos[2] = 2; N.rpos[3] = 3; N.rpos[4] = 5;
N.data[1].e = 2; N.data[1].i = 1; N.data[1].j = 2;
N.data[2].e = 1; N.data[2].i = 2; N.data[2].j = 1;
N.data[3].e = -2; N.data[3].i = 3; N.data[3].j = 1;
N.data[4].e = 4; N.data[4].i = 3; N.data[4].j = 2;
T = MultSMatrix(M, N, T); for (int i = 1; i <= T.tu; i++) { printf("(%d,%d,%d)\n", T.data[i].i, T.data[i].j, T.data[i].e); } return 0; }
|