8 int** addMatrix(int **A, int **B, int rows, int cols)
14 C = calloc(cols, sizeof(int*));
16 C[i] = calloc(rows, sizeof(int));
20 C[i][j] = A[i][j] + B[i][j];
25 void addMatrixMod(int **A, int **B, int **C, int rows, int cols, int mod)
31 C[i][j] = (A[i][j] + B[i][j])%mod;
36 void addVectorMod(int *A, int *B, int *C, int length, int mod)
40 for(i=0; i<length; i++)
41 C[i] = (A[i] + B[i])%mod;
46 void scalarmultMatrix(int scalar, int **a, int **b, int rows, int cols)
50 //b = calloc(cols, sizeof(int*));
51 //for(i=0; i<cols; i++)
52 // b[i] = calloc(rows, sizeof(int));
56 b[i][j] = scalar*a[i][j];
60 int trace(int **a, int rows, int cols)
72 void transp(int **a, int **b, int rows, int cols)
76 for(i=0; i<cols; i++) {
83 void printVector(int* a, int length)
86 printf("Vector[%d]\n", length);
87 for(i=0; i<length; i++)
94 void printMatrix(int** a, int rows, int cols)
97 printf("Matrix[%d][%d]\n", rows, cols);
98 for(i=0; i<rows; i++) {
99 for(j=0; j<cols; j++) {
100 printf("%d ", a[i][j]);
106 int** multMatrix(int **A, int **B, int ro1, int co1, int ro2, int co2)
110 C = calloc(ro1, sizeof(int*));
112 C[i] = calloc(co2, sizeof(int));
114 for(i=0; i<ro1; i++) {
115 for(j=0; j<co2; j++) {
118 C[i][j] += A[i][k] * B[k][j];
126 void multMatrixMod(int **A, int **B, int **C, int ro1, int co1, int ro2, int co2, int mod)
130 for(i=0; i<ro1; i++) {
131 for(j=0; j<co2; j++) {
134 tmp += (A[i][k] * B[k][j]);
141 int** outerMatrix(int **A, int **B, int ro1, int co1, int ro2, int co2)
145 C = calloc(ro1*ro2, sizeof(int*));
146 for(i=0; i<ro1*ro2; i++)
147 C[i] = calloc(co1*co2, sizeof(int));
152 for(l=0; l<co2; l++) {
153 C[j+ro2*i][l+co2*k] = A[i][k]* B[j][l];
159 void outerMatrixMod(int **A, int **B, int **C, int ro1, int co1, int ro2, int co2, int mod)
163 //C = calloc(ro1*ro2, sizeof(int*));
164 //for(i=0; i<ro1*ro2; i++)
165 // C[i] = calloc(co1*co2, sizeof(int));
170 for(l=0; l<co2; l++) {
171 C[j+ro2*i][l+co2*k] = (A[i][k]* B[j][l])%mod;
176 void outerVectorMod(int *A, int *B, int *C, int ro1, int ro2, int mod)
182 C[j+ro2*i]= (A[i]* B[j])%mod;
186 void addSubMatrix(int **A, int **B, int ro1, int co1, int rooff1, int cooff1, int rooff2, int cooff2)
188 // rooff1 is row offset to start range ro1 in first matrix etc.
193 B[rooff2+i][cooff2+j] = A[rooff1+i][cooff1+j];
197 void appendBlockMatrix(int **A, int **B, int **C, int ro1, int co1, int ro2, int co2)
199 // rooff1 is row offset to start range ro1 in first matrix etc.
208 C[ro1+i][co1+j] = B[i][j];
213 void appendVector(int *A, int *B, int *C, int ro1, int ro2)
225 int dotProductMod(int *a, int *b, int length, int mod)
230 for(i=0; i<length; i++)
231 dotproduct += a[i]*b[i];
233 return(dotproduct%mod);
237 void deallocate_mem(int ***arr, int rows)
240 for(i=0; i<rows; i++)