#include <stdio.h>
#include <stdlib.h>

double **mat(int M, int N)
{
  int i;
  double **m;

#if 1
  m = (double **)malloc(sizeof(double *)*M);
  for (i=0; i<M; i++)
    m[i] = (double *)malloc(sizeof(double)*N);  
#else
  m = (double **)malloc(sizeof(double *)*M);
  m[0] = (double *)malloc(sizeof(double)*M*N);
  for (i=1; i<M; i++) {
    m[i] = m[i-1] + N;
  }
#endif
  return m;
}
void unit_mat(int M, int N, double **m)
{
  int i, j;

  for (i=0; i<M; i++)
    for (j=0; j<N; j++)
      m[i][j] = (i==j) ? 1 : 0;
}
/*
  a[M][N], b[M][L], c[M][N]: matrix
 */
void mat_mult_mat(int M, int N, int L, double **a, double **b, double **c)
{
  int i, j, k;

  for (i=0; i<M; i++)
    for (j=0; j<L; j++)
#if defined(UNSAFE)
      for (k=0; k<N; k++)
#else
      for (c[i][j] = 0, k=0; k<N; k++)
#endif
        c[i][j] += a[i][k]*b[k][j];
}

int main(void)
{
  int i, j;
  double **a = mat(3, 3);
  double **b = mat(3, 3);
  double **c = mat(3, 3);

  unit_mat(3, 3, a);
  unit_mat(3, 3, b);
  mat_mult_mat(3, 3, 3, a, b, c);
  for (i=0; i<3; i++) {
    for (j=0; j<3; j++)
      printf("%g ", c[i][j]);
    printf("\n");
  }
  return 0;
}
