-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
343 lines (294 loc) · 9.64 KB
/
main.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <iostream>
#include <cmath>
using namespace std;
long long determ(int *ptr, int rows, int columns);
void inverse (int* ptr, long double* ptr_inv, int rows, int columns, long long determinant);
long long approx (long double x);
void operation (int *ptrA, int *ptrB, int Arows, int Acolumns, int Brows, int Bcolumns, char op);
void input (int* ptr, int rows, int columns);
int main()
{
int x = 0;
int Arows, Acolumns, Brows, Bcolumns;
//Specifying the dimensions
cout << "Please enter dimensions of Matrix A:\n";
cin >> Arows >> Acolumns;
cout << "Please enter dimensions of Matrix B:\n";
cin >> Brows >> Bcolumns;
int Amat[Arows][Acolumns], Bmat[Brows][Bcolumns];
//Elements of Matrix A
cout << "Please enter values of Matrix A:\n";
input(&Amat[0][0], Arows, Acolumns);
//Elements of Matrix B
cout << "Please enter values of Matrix B:\n";
input(&Bmat[0][0], Brows, Bcolumns);
while (x != 7)
{
cout << "Please choose operation type(1: A+B, 2: A-B, 3: AxB, 4: A*inverse(B), 5: |A|, 6: |B|, 7: quit):\n";
cin >> x;
if (x == 1)
//Addition
{
if ((Arows == Brows) && (Acolumns == Bcolumns))
{
operation(&Amat[0][0], &Bmat[0][0], Arows, Acolumns, Brows, Bcolumns, '+');
}
else
{
cout << "The operation you chose is invalid for the given matrices.\n";
}
}
else if (x == 2)
{
//Subtraction
if ((Arows == Brows) && (Acolumns == Bcolumns))
{
operation(&Amat[0][0], &Bmat[0][0], Arows, Acolumns, Brows, Bcolumns, '-');
}
else
{
cout << "The operation you chose is invalid for the given matrices.\n";
}
}
else if (x == 3)
{
if (Acolumns == Brows)
{
operation(&Amat[0][0], &Bmat[0][0], Arows, Acolumns, Brows, Bcolumns, 'x');
}
else
{
cout << "The operation you chose is invalid for the given matrices.\n";
}
}
if (x == 4)
{
// Pointer to first element in the matrix in order to pass it to the function
int* ptr = & Bmat[0][0];
/*
Failure to get inverse is due to:
- No of rows is not equal to no of columns
- Determinant = 0
And to multiply A with inverse B, A columns must be equal to Brows
*/
long long determinant_value = determ(ptr, Brows, Bcolumns);
if((determ(ptr, Brows, Bcolumns) != 0) && (Brows == Bcolumns) && (Acolumns == Brows))
{
long double Binverse[Brows][Bcolumns];
//Getting the inverse from function inverse
//By passing pointer to 1st element and no. of rows and columns as well as determinant value
inverse(ptr, &Binverse[0][0], Brows, Bcolumns, determinant_value);
//Multiplying matrix A by inverted matrix B
long double sum = 0;
for (int i = 0; i < Arows; i++)
{
for (int j = 0; j < Bcolumns; j++)
{
sum = 0;
for(int k = 0; k < Acolumns; k++)
sum += Amat[i][k] * Binverse[k][j];
cout << approx(sum) << " ";
}
cout << endl;
}
}
else
{
cout << "The operation you chose is invalid for the given matrices.\n";
}
}
if (x == 5)
{
if (Arows == Acolumns)
{
int* ptr = & Amat[0][0];
cout << determ(ptr, Arows, Acolumns) << endl;
}
else
{
cout << "The operation you chose is invalid for the given matrices.\n";
}
}
if (x == 6)
{
if (Brows == Bcolumns)
{
int* ptr = & Bmat[0][0];
cout << approx(determ(ptr, Brows, Bcolumns)) << endl;
}
else
{
cout << "The operation you chose is invalid for the given matrices.\n";
}
}
}
cout << "Thank you!";
}
long long determ(int *ptr, int rows, int columns)
{
if(rows == 1)
{
return *ptr;
}
long long det = 0;
if(rows == 2)
{ //finding determinant for 2x2 matrix
det = ptr[0] * ptr[3] - ptr[1] * ptr[2];
return det;
}
//Re-Assembling the matrix in a 2D array from its pointer
int arr[rows][columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
arr[i][j]= ptr[i*columns +j];
}
}
int small[rows - 1][columns - 1];
for(int k = 0; k < columns; k++)
{
for (int i = 1; i < rows; i++)
{
int l = 0;
for (int j = 0; j < columns ; j++)
if( j != k)
{
small[i-1][l] = arr[i][j];
l++;
}
}
//Sign rule
if(k % 2 == 0)
{
det += arr[0][k]* determ(&small[0][0], rows - 1, columns - 1);
}
else
det -= arr[0][k]* determ(&small[0][0], rows - 1, columns - 1);
}
return det;
}
void inverse (int* ptr, long double* ptr_inv, int rows, int columns, long long determinant)
{
//assembling the matrix in a 2D array from the pointer
int arr[rows][columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
arr[i][j]= ptr[i*columns +j];
}
}
/*Declaring two arrays, one for the inverse(final result)
And one for the minor(to be sent to determinant function)
*/
int minor[rows - 1][columns - 1];
//looping through elements of the original array to get cofactor for each one
int minor_row, minor_column;
for(int k = 0; k < rows; k++)
{
for(int l = 0; l < columns; l++)
{
minor_row = 0, minor_column = 0;
// looping through elements of the original array to gather elements for the minor array
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
//Extracting elements from the original array to minor array
if((i != k) && (j != l))
{
//incrementing the index of rows for minor array when we reach last column
if(minor_column == columns - 1)
{
minor_column = 0;
minor_row += 1;
}
minor[minor_row][minor_column] = arr[i][j];
//incrementing the index of columns while staying in the same row
minor_column++;
}
}
}
int a = rows - 1;
if((k+l) % 2 == 0)
{
ptr_inv[k + l*columns] = (determ(&minor[0][0], a, a))/(long double)determinant;
}
else
{
ptr_inv[k + l*columns] = -(determ(&minor[0][0], a, a))/(long double)(determinant);
}
}
}
return;
}
long long approx (long double x)
{
if (x < 0)
{
if((long long)(x - 0.5) == -0)
return 0;
else
return (long long)(x - 0.5);
}
if (x > 0)
return (long long)(x + 0.5);
//In case x equals 0
return x;
}
//Function for addition, subtraction and multiplication
void operation (int *ptrA, int *ptrB, int Arows, int Acolumns, int Brows, int Bcolumns, char op)
{
//In case of addition
if (op == '+')
{
for (int i = 0; i < Arows; i++)
{
for (int j = 0; j < Acolumns; j++)
{
cout << ptrA[i * Acolumns + j] + ptrB[i * Acolumns + j] << " ";
}
cout << endl;
}
}
//In case of subtraction
else if (op == '-')
{
for (int i = 0; i < Arows; i++)
{
for (int j = 0; j < Acolumns; j++)
{
cout << ptrA[i * Acolumns + j] - ptrB[i * Acolumns + j] << " ";
}
cout << endl;
}
}
//In case of multiplication
else if (op == 'x')
{
long double sum;
for (int i = 0; i < Arows; i++)
{
for (int j = 0; j < Bcolumns; j++)
{
sum = 0;
for(int k = 0; k < Acolumns; k++)
sum += ptrA[i * Acolumns + k] * ptrB[k* Bcolumns + j];
cout << approx(sum) << " ";
}
cout << endl;
}
}
}
//Getting input from user to the array
void input (int* ptr, int rows, int columns)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cin >> ptr[i * columns + j];
}
}
}