-
Notifications
You must be signed in to change notification settings - Fork 617
/
matrix_operations.cpp
236 lines (200 loc) · 4.64 KB
/
matrix_operations.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* C++ CODE TO SHOW DIFFERENT OPERATIONS ON 2D ARRAY:
Implementation of operations on two different matrices, say A and B, like -
1. Multiplication(A × B)
2. Subtraction(A - B)
3. Addition(A + B)
4. Inverse(A^(-1) and B^(-1)), by checking the order of matrix as well.
*/
#include<iostream>
using namespace std;
// Function to find the sum of two matrices
void addition_of_matrices(int rows, int cols, double a[][20],double b[][20])
{
int sum[rows][cols];
for(int i = 0; i < rows; i++)
{
for(int j =0; j < cols; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
for(int i = 0; i < rows; i++ )
{
for(int j =0; j < cols; j++)
{
cout << sum[i][j] << "\t";
}
cout << endl;
}
}
// Function to find the difference of two matrices
void subtraction_of_matrices(int rows, int cols, double a[][20],double b[][20])
{
int sub[rows][cols];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
sub[i][j] = a[i][j] - b[i][j];
}
}
for(int i = 0; i < rows; i++ )
{
for(int j =0; j < cols; j++)
{
cout << sub[i][j] << "\t";
}
cout << endl;
}
}
// Function to find the product of two matrices
void product_of_matrices(int rows, int cols, double a[][20],double b[][20])
{
int mul[rows][cols];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
mul[i][j] = 0;
for(int k=0; k < cols ; k++)
{
mul[i][j] += a[i][k] * b[k][j];
}
}
}
for(int i = 0; i < rows; i++ )
{
for(int j = 0; j < cols; j++)
{
cout << mul[i][j] << "\t";
}
cout << endl;
}
}
// Function to find the inverse of matrix
void inverse_of_matrices(int n, double a[][20])
{
double det = 0; // Determinant
double adj[n][n];
if(n == 2)
{
det = (a[0][0] * a[1][1] - a[1][0] * a[0][1]);
if(det == 0)
{
cout << " Inverse cannot be calculated! ";
return;
}
swap(a[0][0],a[1][1]);
a[0][1] = -a[0][1];
a[1][0] = -a[1][0];
for(int i = 0; i < 2 ; i++)
{
for(int j = 0; j < 2; j++)
{
cout << (a[i][j] / det) <<"\t";
}
cout << endl;
}
}
if(n == 3)
{
for(int i = 0;i<3;i++)
{
det += (a[0][i] * ((a[1][(i+1)%3] * a[2][(i+2)%3]) - (a[1][(i+2)%3] * a[2][(i+1)%3])));
}
if(det == 0)
{
cout << "Inverse cannot be calculated!";
return;
}
else
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout << ((a[(j+1)%3][(i+1)%3] * a[(j+2)%3][(i+2)%3]) - (a[(j+1)%3][(i+2)%3] * a[(j+2)%3][(i+1)%3]))/det << "\t";
}
cout << endl;
}
}
}
}
int main()
{
double a[20][20], b[20][20];
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;
cout << "\nEnter the elements of matrix A: "<<endl;
for(int i = 0; i < rows; i++ )
{
for(int j =0; j < cols; j++)
{
cin >> a[i][j];
}
}
cout << "\nEnter the elements of matrix B: "<<endl;
for(int i = 0; i < rows; i++ )
{
for(int j =0; j < cols; j++)
{
cin >> b[i][j];
}
}
cout<<"\nADDITION OF TWO MATRICES: "<<endl;
addition_of_matrices(rows,cols,a,b);
cout<<"\nSUBTRACTION OF TWO MATRICES: "<<endl;
subtraction_of_matrices(rows,cols,a,b);
if(rows == cols)
{
cout<<"\nMULTIPLICATION OF TWO MATRICES: "<<endl;
product_of_matrices(rows,cols,a,b);
// Formula for finding inverse of a matrix: A^(-1) = adj(A) / det(A)
cout<<"\nINVERSE OF MATRIX A: "<<endl;
inverse_of_matrices(rows,a);
}
return 0;
}
/* OUTPUT 1:
Enter the number of rows and columns: 2 2
Enter the elements of matrix A:
3 4 5 6
Enter the elements of matrix B:
1 5 -4 2
ADDITION OF TWO MATRICES:
4 9
1 8
SUBTRACTION OF TWO MATRICES:
2 -1
9 4
MULTIPLICATION OF TWO MATRICES:
-13 23
-19 37
INVERSE OF MATRIX A:
-3 2
2.5 -1.5
OUTPUT 2:
Enter the number of rows and columns: 3 3
Enter the elements of matrix A:
1 2 4 2 3 5 2 1 0
Enter the elements of matrix B:
1 4 6 0 0 2 3 5 2
ADDITION OF TWO MATRICES:
2 6 10
2 3 7
5 6 2
SUBTRACTION OF TWO MATRICES:
0 -2 -2
2 3 3
-1 -4 -2
MULTIPLICATION OF TWO MATRICES:
13 24 18
17 33 28
2 8 14
INVERSE OF MATRIX A:
5 -4 2
-10 8 -3
4 -3 1
Time Complexity: O(n^2)
Space Complexity: O(n^2) */