L
LL
/* matrix_mult.h */
#include <stdio.h>
#include <stdlib.h>
typedef double matrix[50][50];
// Matrix multiplication
matrix* mm(matrix a, matrix b, int m, int r, int n) {
matrix *c=(matrix*)malloc(sizeof(matrix));
int i,j,k;
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
(*c)[k]=0;
for (j=0; j<r; j++) {
(*c)[k]+=a[j]*b[j][k];
}
}
}
return c;
}
void printm(matrix *a, int m, int n) {
int i,j;
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
printf("%lf ", (*a)[j]);
}
printf("\n");
}
}
/* sample_mm.c */
#include "matrix_mult.h"
int main() {
matrix sample_a={{1,0,0},{0,1,0},{0,0,1}}; // Identity matrix
matrix sample_b={{1,2,3},{4,5,6},{7,8,9}};
matrix *sample_m;
sample_m=mm(sample_a, sample_b, 3, 3, 3);
printm(sample_m, 3, 3);
}
// Output
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000
#include <stdio.h>
#include <stdlib.h>
typedef double matrix[50][50];
// Matrix multiplication
matrix* mm(matrix a, matrix b, int m, int r, int n) {
matrix *c=(matrix*)malloc(sizeof(matrix));
int i,j,k;
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
(*c)[k]=0;
for (j=0; j<r; j++) {
(*c)[k]+=a[j]*b[j][k];
}
}
}
return c;
}
void printm(matrix *a, int m, int n) {
int i,j;
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
printf("%lf ", (*a)[j]);
}
printf("\n");
}
}
/* sample_mm.c */
#include "matrix_mult.h"
int main() {
matrix sample_a={{1,0,0},{0,1,0},{0,0,1}}; // Identity matrix
matrix sample_b={{1,2,3},{4,5,6},{7,8,9}};
matrix *sample_m;
sample_m=mm(sample_a, sample_b, 3, 3, 3);
printm(sample_m, 3, 3);
}
// Output
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000