-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inverse Of a nxn Matrix
100 lines (96 loc) · 2.01 KB
/
Inverse Of a nxn Matrix
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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
using namespace std;
float f(vector<float>v,int n)
{
float res;
if(n>2)
{
float sum=0;
float c=1;
for(int k=0;k<n;k++)
{
vector<float>v1;
for(int i=1;i<n;i++)
for(int j=0;j<n;j++)
if(j!=k)
{
v1.push_back(v[i*n+j]);
}
sum+=c*v[k]*f(v1,n-1);
c*=(-1);
}
return sum;
}
else if(n==2)
{
res=v[0]*v[3]-v[1]*v[2];
}
return res;
}
float *f1(vector<float>v,int n)
{
static float r[144];
if(n>2)
{
for(int k=0;k<n;k++)
for(int l=0;l<n;l++)
{
vector<float>v1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(j!=l && i!=k)
{
v1.push_back(v[i*n+j]);
}
r[k*n+l]=f(v1,n-1);
}
}
else if(n==2)
{
for(int i=0;i<4;i++)
r[i]=v[3-i];
}
return r;
}
int main()
{
int n;
cout<<"Enter the order of the square matrix: ";
cin>>n;
float a[n][n];
vector<float>v;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
cout << "Enter a["<<i+1<<"]["<<j+1<<"]: ";
cin >> a[i][j];
v.push_back(a[i][j]);
}
cout<<"\nGiven matrix is:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
float det=f(v,n);
if(det==0)
{
cout<<"\nInverse of the matrix does not exist.\nMatrix is singular.\n";
}
else
{
float *p=f1(v,n);
cout<<"\nInverse of the matrix is :\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<pow(-1,i+j)*p[j*n+i]/det<<" ";
cout<<endl;
}
}
return 0;
}