[ create a new paste ] login | about

Link: http://codepad.org/2tPTqE6f    [ raw code | output | fork ]

C, pasted on Nov 7:
#include <stdio.h>

#define M 3
#define N 2

void dump_mat(double *p, int m, int n) {
  int i, j;
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++)
      printf("%.2f ", *p++);
    putchar('\n');
  }
  putchar('\n');
}

void MatProduct(int mm, int nn, double a[][N], double b[][M], double c[][M]) {
  int m, n, k;
  for(n = 0; n < mm; n++)
    for(m = 0; m < mm; m++) {
      c[m][n] = 0.0;
      for(k = 0; k < nn; k++)
        c[m][n] += a[m][k] * b[k][n];
    }
}

int main() {
  static double x[M][N] = { {1, 2}, {3, 4}, {5, 6} };
  static double y[N][M] = { {1, 2, 3}, {4, 5, 6} };
  static double z[M][M];
  printf("x:\n"); dump_mat(&x[0][0], M, N);
  printf("y:\n"); dump_mat(&y[0][0], N, M);
  MatProduct(M, N, x, y, z);
  printf("z:\n"); dump_mat(&z[0][0], M, M);
  return 0;
}
/* end */


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
x:
1.00 2.00 
3.00 4.00 
5.00 6.00 

y:
1.00 2.00 3.00 
4.00 5.00 6.00 

z:
9.00 12.00 15.00 
19.00 26.00 33.00 
29.00 40.00 51.00 



Create a new paste based on this one


Comments: