PageRenderTime 33ms CodeModel.GetById 30ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-29/SWIG/Examples/guile/matrix/matrix.c

#
C | 61 lines | 38 code | 22 blank | 1 comment | 8 complexity | a43c7a26c16911b1084e60b63692d1f5 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
 1/*   FILE : matrix.c : some simple 4x4 matrix operations */
 2#include <stdlib.h>
 3#include <stdio.h>
 4
 5double **new_matrix() {
 6
 7  int i;
 8  double **M;
 9
10  M = (double **) malloc(4*sizeof(double *));
11  M[0] = (double *) malloc(16*sizeof(double));
12  
13  for (i = 0; i < 4; i++) {
14    M[i] = M[0] + 4*i;
15  }
16  return M;
17}
18
19void destroy_matrix(double **M) {
20
21  free(M[0]);
22  free(M);
23
24}
25
26void print_matrix(double **M) {
27
28  int i,j;
29
30  for (i = 0; i < 4; i++) {
31    for (j = 0; j < 4; j++) {
32      printf("%10g ", M[i][j]);
33    }
34    printf("\n");
35  }
36
37}
38
39void mat_mult(double **m1, double **m2, double **m3) {
40
41  int i,j,k;
42  double temp[4][4];
43
44  for (i = 0; i < 4; i++) 
45    for (j = 0; j < 4; j++) {
46      temp[i][j] = 0;
47      for (k = 0; k < 4; k++) 
48	temp[i][j] += m1[i][k]*m2[k][j];
49    }
50
51  for (i = 0; i < 4; i++)
52    for (j = 0; j < 4; j++)
53      m3[i][j] = temp[i][j];
54}
55
56
57
58
59
60
61