Calculation of determinant of triple matrix (recursion)

Keywords: C

1. Specific ideas:

There are two main methods for calculating matrix determinants:

1. Recursive calculation according to the definition of residential determinant

2. First transform the matrix row to upper triangular matrix, and then find the determinant.

 

(I first thought about the transformation of row into triangle matrix, but I met some problems in the middle, so I wrote down the recursive method first, and then I will continue to update another method.)

In linear algebra, we have learned how to recursively find matrix determinants. The following figure:

Then, every algebraic cofactor can be regarded as "n-1-order submatrix" relative to "n-order parent matrix". Once again, the submatrix is expanded by rows or columns, which is the idea of recursively finding matrix determinants.

2. Triples and 2D arrays

Needless to say, a two-dimensional array is one-to-one corresponding to a matrix, and its representation is exactly the same.

A triple is a set of shapes such as ((r,c), d). We specify that (r,c) is the corresponding position of a number in a triple in a two-dimensional array, and D represents the value of the data.

The data structure of triples is as follows:

 1 typedef struct
 2 {
 3     int r;
 4     int c;
 5     int d;
 6 }TupNode;
 7 typedef struct
 8 {
 9     int rows;
10     int cols;
11     int nums;
12     TupNode data[Maxsize];
13 }TSMatrix;

The specific calculation determinant code is as follows:

 1 //Calculation matrix determinant
 2 int DatMat(TSMatrix t){
 3     if(t.cols!=t.rows){
 4         printf("This matrix can't be determinant!");
 5         return 0; 
 6     }
 7     else{
 8         int n=t.cols;
 9         int a[n][n];
10         //Convert triples to 2D arrays 
11         for(int i=0;i<n;i++){
12             for(int j=0;j<n;j++){
13                 a[i][j]=0;
14             }
15         }
16         for(int k=0;k<t.nums;k++){
17             int i = t.data[k].r;
18             int j = t.data[k].c;
19             a[i][j] = t.data[k].d;
20         }
21         if (n == 1){
22             return a[0][0];
23         }
24         else{
25             int b[n-1][n-1];//Establish n-1 Algebraic cofactor matrix of order bb 
26             int c[(n-1)*(n-1)];  
27             int sum = 0;//sum Is the value of the determinant 
28             TSMatrix t1;
29             
30             //Determinant based on the first column 
31             for(int l=0;l<n;l++){
32                 int m1=0;
33                 int m2=0; 
34                 for(int i =0;i<n;i++){
35                     for(int j=0;j<n;j++){
36                         if(i!=l&&j!=0){
37                             c[m1]=a[i][j];
38                             m1++;
39                         }
40                     }
41                 }
42                 for(int i =0;i<n-1;i++){
43                     for(int j=0;j<n-1;j++){
44                         b[i][j]=c[m2];
45                         m2++;
46                     } 
47                 }
48                 
49                 //Converting a two-dimensional array to a triple 
50                 t1.rows=n-1;
51                 t1.cols=n-1;
52                 t1.nums=0;
53                 for(int i=0;i<n-1;i++){
54                     for(int j=0;j<n-1;j++){
55                         if(b[i][j]!=0){
56                             t1.data[t1.nums].r=i;
57                             t1.data[t1.nums].c=j;
58                             t1.data[t1.nums].d=b[i][j];
59                             t1.nums++;
60                         }     
61                     }
62                 }
63                 sum+=a[l][0]*DatMat(t1)*pow(-1,l);//Finding the value of determinant by recursion 
64             }
65             return sum;
66         }
67     }
68 } 

Posted by El Zagna on Sun, 08 Dec 2019 07:06:26 -0800