-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverse.c
128 lines (126 loc) · 2.77 KB
/
inverse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *allocate(int n)
{
double *p = (double *)malloc(n*sizeof(double));
for(int i=0; i<n; i++)
p[i] = 0;
return p;
}
double **allocate2(int m, int n)
{
double **p = (double**)malloc(m*sizeof(double*));
for(int i=0; i<m; i++)
p[i] = allocate(n);
return p;
}
double **copy(int n, double **arr)
{
double **m = allocate2(n,n);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
m[i][j] = arr[i][j];
return m;
}
void disp(int n, double **a)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
printf("%0.2lf\t", a[i][j]);
}
printf("\n");
}
printf("\n");
}
double determinant(int n, double **a)
{
double det=0;
if(n == 1)
{
return a[0][0];
}
for(int j=0;j <n; j++)
{
double **b = allocate2(n-1,n-1);
int jj=0, ix=0;
for(int ii=1; ii<n; ii++)
for(jj=0; jj<n; jj++)
{
if(jj == j)continue;
b[ix/(n-1)][ix%(n-1)] = a[ii][jj];
ix++;
}
int sgn = j&1? -1:1;
det += sgn * a[0][j] * determinant(n-1, b);
}
return det;
}
double** inverse(int n, double **a)
{
double **adj = allocate2(n,n), **inv;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
double **b = allocate2(n-1,n-1);
int ii=0, jj=0, ix=0;
for(ii=0; ii<n; ii++)
{
if(ii == i)continue;
for(jj=0; jj<n; jj++)
{
if(jj == j)continue;
b[ix/(n-1)][ix%(n-1)] = a[ii][jj];
ix++;
}
}
int sgn = (i+j)&1? -1:1;
adj[i][j] = sgn*determinant(n-1, b);
}
}
printf("Adjoint:\n");
disp(n, adj);
double Det = determinant(n,a);
inv = allocate2(n,n);
for(int i=0; i<n*n; i++)
{
inv[i/n][i%n] = adj[i%n][i/n];
inv[i/n][i%n] /= Det;
}
return inv;
}
void mul(int n, double **a, double **aug)
{
double sum = 0, **prod= allocate2(n,n);
for (int c = 0; c < n; c++)
for (int d = 0; d < n; d++)
{
for (int k = 0; k < n; k++)
sum = sum + a[c][k]*aug[k][d];
prod[c][d] = sum;
sum = 0;
}
disp(n, prod);
}
int main()
{
int n;
scanf("%d", &n);
double **m = allocate2(n,n), **inv = allocate2(n,n);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
scanf("%lf", &m[i][j]);
}
printf("Mat:\n");
disp(n, m);
inv = inverse(n, m);
printf("Inv:\n");
disp(n, inv);
printf("Multiplying:\n");
mul(n, m, inv);
printf("det : %lf", determinant(n, m));
}