MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
determinant.cc
Go to the documentation of this file.
1 
7 //==============================================================================
8 // Recursive definition of determinate using expansion by minors.
9 //
10 // Notes: 1) arguments:
11 // a (double **) pointer to a pointer of an arbitrary square matrix
12 // n (int) dimension of the square matrix
13 //
14 // 2) Determinant is a recursive function, calling itself repeatedly
15 // each time with a sub-matrix of the original till a terminal
16 // 2X2 matrix is achieved and a simple determinat can be computed.
17 // As the recursion works backwards, cumulative determinants are
18 // found till untimately, the final determinate is returned to the
19 // initial function caller.
20 //
21 // 3) m is a matrix (4X4 in example) and m13 is a minor of it.
22 // A minor of m is a 3X3 in which a row and column of values
23 // had been excluded. Another minor of the submartix is also
24 // possible etc.
25 // m a b c d m13 . . . .
26 // e f g h e f . h row 1 column 3 is elminated
27 // i j k l i j . l creating a 3 X 3 sub martix
28 // m n o p m n . p
29 //
30 // 4) the following function finds the determinant of a matrix
31 // by recursively minor-ing a row and column, each time reducing
32 // the sub-matrix by one row/column. When a 2X2 matrix is
33 // obtained, the determinat is a simple calculation and the
34 // process of unstacking previous recursive calls begins.
35 //
36 // m n
37 // o p determinant = m*p - n*o
38 //
39 // 5) this function uses dynamic memory allocation on each call to
40 // build a m X m matrix this requires ** and * pointer variables
41 // First memory allocation is ** and gets space for a list of other
42 // pointers filled in by the second call to malloc.
43 //
44 // 6) C++ implements two dimensional arrays as an array of arrays
45 // thus two dynamic malloc's are needed and have corresponsing
46 // free() calles.
47 //
48 // 7) the final determinant value is the sum of sub determinants
49 //
50 //==============================================================================
51 #include <stdlib.h>
52 #include <math.h>
54 double Determinant(\
55  double **a ,\
56  int n )
57 {
58  int i,j,j1,j2 ; // general loop and matrix subscripts
59  double det = 0 ; // init determinant
60  double **m = NULL ; // pointer to pointers to implement 2d
61  // square array
62 
63  if (n < 1) { } // error condition, should never get here
64 
65  else if (n == 1) { // should not get here
66  det = a[0][0] ;
67  }
68 
69  else if (n == 2) { // basic 2X2 sub-matrix determinate
70  // definition. When n==2, this ends the
71  det = a[0][0] * a[1][1] - a[1][0] * a[0][1] ;// the recursion series
72  }
73 
74 
75  // recursion continues, solve next sub-matrix
76  else { // solve the next minor by building a
77  // sub matrix
78  det = 0 ; // initialize determinant of sub-matrix
79 
80  // for each column in sub-matrix
81  for (j1 = 0 ; j1 < n ; j1++) {
82  // get space for the pointer list
83  m = (double **) malloc((n-1)* sizeof(double *)) ;
84 
85  for (i = 0 ; i < n-1 ; i++)
86  m[i] = (double *) malloc((n-1)* sizeof(double)) ;
87  // i[0][1][2][3] first malloc
88  // m -> + + + + space for 4 pointers
89  // | | | | j second malloc
90  // | | | +-> _ _ _ [0] pointers to
91  // | | +----> _ _ _ [1] and memory for
92  // | +-------> _ a _ [2] 4 doubles
93  // +----------> _ _ _ [3]
94  //
95  // a[1][2]
96  // build sub-matrix with minor elements excluded
97  for (i = 1 ; i < n ; i++) {
98  j2 = 0 ; // start at first sum-matrix column position
99  // loop to copy source matrix less one column
100  for (j = 0 ; j < n ; j++) {
101  if (j == j1) continue ; // don't copy the minor column element
102 
103  m[i-1][j2] = a[i][j] ; // copy source element into new sub-matrix
104  // i-1 because new sub-matrix is one row
105  // (and column) smaller with excluded minors
106  j2++ ; // move to next sub-matrix column position
107  }
108  }
109 
110  det += pow(-1.0,1.0 + j1 + 1.0) * a[0][j1] * Determinant(m,n-1) ;
111  // sum x raised to y power
112  // recursively get determinant of next
113  // sub-matrix which is now one
114  // row & column smaller
115 
116  for (i = 0 ; i < n-1 ; i++) free(m[i]) ;// free the storage allocated to
117  // to this minor's set of pointers
118  free(m) ; // free the storage for the original
119  // pointer to pointer
120  }
121  }
122  return(det) ;
123 }
double Determinant(double **a, int n)
Recursive function for calculation of determinant.
Definition: determinant.cc:54
m
Bubble Growth Application Recipe.
Definition: bubbleGrowth.py:59