inverse of a matrix 3x3 c 2b 2b

Solutions on MaxInterview for inverse of a matrix 3x3 c 2b 2b by the best coders in the world

showing results for - "inverse of a matrix 3x3 c 2b 2b"
Isabel
14 Jan 2017
1#include<bits/stdc++.h>
2#include <iostream>
3#include <cmath>
4#include <iomanip>
5#define N 3
6using namespace std;
7
8void getCoFactor(int a[N][N],int b[N][N],int p,int q,int k){
9    int i,j,m,n;
10
11    m=0; // Intinializing the row for co-factor matrix;
12    n=0; // Intinializing the column for co-factor matrix;
13    for(i = 0;i < k;i++){
14        for(j=0;j<k;j++){
15                b[i][j] = 0;
16                if(i!=p && j!=q){
17                    b[m][n] = a[i][j];
18
19                    if(n < k-2)   // Increasing row index when
20                        n++;
21                    else{
22                        n = 0;   //increasing row index and resetting column index
23                        m++;
24                    }
25                }
26        }
27    }
28
29
30
31
32}
33
34int determinant(int a[N][N], int k){
35    int det;
36    if(k==1)
37        return a[0][0]; // Matrix order is 1
38
39    else{
40        int c,s=1,b[N][N],i,j,m,n;
41        det = 0;
42        for(c = 0;c < k;c++){
43
44            getCoFactor(a,b,0,c,k);
45
46            det = det + s*(a[0][c]* determinant(b,k-1));
47            s = -1*s;
48
49        }
50
51    }
52
53    return det;
54
55}
56
57
58
59void adjoint(int a[N][N], int adj[N][N], int k){
60
61    int s = 1;
62    int co_fact[N][N];
63
64    for(int i=0; i<k ; i++){
65        for(int j=0;j<k;j++){
66
67                getCoFactor(a,co_fact,i,j,k);
68
69                s = pow(-1,(i+j));
70                adj[j][i] = s*determinant(co_fact,k-1);
71
72        }
73    }
74
75
76
77
78
79}
80
81
82void Inverse(int A[N][N], float inverse[N][N],int k){
83
84      int deter = determinant(A,N);
85
86    int adj[N][N];
87    adjoint(A,adj,k); //Finding the adjoint of the matrix
88
89
90
91    for(int i=0; i<k ; i++){
92        for(int j=0;j<k;j++){
93                inverse[i][j] = adj[i][j]/(float)deter;
94        }
95
96
97    }
98
99}
100
101
102
103
104
105
106
107int main()
108{
109
110//    cout << "Enter the order of matrix:";
111//    cin >> N;
112    int A[N][N];
113
114    for(int i=0;i<N;i++){
115        for(int j=0;j<N;j++){
116        cout << "Enter matrix at a[" << i << "]" << "[" <<j << "]" << ": " ;
117        cin >> A[i][j];
118        }
119    }
120
121
122    int deter = determinant(A,N);
123
124
125    if(deter == 0){
126        cout << "Inverse does not exist." << endl;
127        return false;
128        }
129
130
131    float inverse[N][N];
132
133
134
135    Inverse(A,inverse,N);
136
137
138
139    for(int i=0;i<N;i++){
140        for(int j=0;j<N;j++){
141        cout <<  inverse[i][j] << " ";
142        }
143        cout <<  endl;
144    }
145
146
147
148
149
150
151
152
153
154    return 0;
155}
156
157//Sources
158//https://www.geeksforgeeks.org/adjoint-inverse-matrix/
159//https://scanftree.com/programs/c/c-program-to-find-the-inverse-of-the-matrix/
160//https://www.youtube.com/watch?v=qLDDwT-kPAk&t=801s
161
162
similar questions
queries leading to this page
inverse of a matrix 3x3 c 2b 2b