-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.cpp
581 lines (568 loc) · 18.4 KB
/
Functions.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "Classes.h"
#include "Exceptions.h"
int menuToDo(vector <Matrix> Matrixs, char* fileName, int sw1)
{
if (sw1 == 3)
{
Matrix M1;
ifstream fileTxt(fileName);
if (!fileTxt)
{
throw SuchFileDoesNotExist("\nSuch file does not exist\nCreate it first\n");
}
while (fileTxt)
{
Matrixs.push_back(M1 << fileTxt);
}
Matrixs.pop_back();
fileTxt.close();
}
if (sw1 == 4)
{
Matrix M1;
ifstream fileBin(fileName, ios::binary);
if (!fileBin)
throw SuchFileDoesNotExist("\nSuch file does not exist\nCreate it first\n");
while (fileBin)
{
Matrixs.push_back(M1.ReadBin(fileBin));
}
Matrixs.pop_back();
fileBin.close();
}
int sw;
int signal = 0;
do{
cout << "Choose what you want to do: " << endl;
cout << "1) Add new Matrix;" << endl;
cout << "2) Add new Elementary Matrix;" << endl;
cout << "3) Add new Diagonal Matrix;" << endl;
cout << "4) Add new Upper Triangle Matrix;" << endl;
cout << "5) Add new Down Triangle Matrix;" << endl;
cout << "6) Add new Simmetric Matrix;" << endl;
cout << "7) Add new Random Matrix;" << endl;
cout << "8) Delete Matrix;" << endl;
cout << "9) Print Matrix on display" << endl;
cout << "10) Summarise;" << endl;
cout << "11) Subtract;" << endl;
cout << "12) Multiply by number;" << endl;
cout << "13) Multiply by Matrix;" << endl;
cout << "14) Transpose Matrix;" << endl;
cout << "15) Rank;" << endl;
cout << "16) Inverted matrix;" << endl;
cout << "17) Minor;" << endl;
cout << "18) Adamar;" << endl;
cout << "19) Matrix trace;" << endl;
cout << "20) Square Matrix Determinant;" << endl;
cout << "21) Scalar;" << endl;
cout << "22) Outer;" << endl;
cout << "23) Norm;" << endl;
cout << "24) Max Norm;" << endl;
cout << "25) Mat norm;" << endl;
cout << "26) Vectors Angle;" << endl;
cout << "27) Clear console;" << endl;
cout << "28) Cross;" << endl;
cout << "0) Exit." << endl;
cin >> sw;
switch (sw) {
case 1: {
int n, m;
cout << "Input your Matrix dimention :";
cin >> n >> m;
Matrix M(n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
//M.Display(signal);
cin >> M.Element(i, j);
//system("cls");
}
}
M.Display(signal);
Matrixs.push_back(M);
break;
}
case 2: {
int n, m;
cout << "Input your Matrix dimention :";
cin >> n >> m;
Elementary M(n, m);
Matrixs.push_back(M);
break;
}
case 3: {
int n, m;
cout << "Input your Matrix dimention :";
cin >> n >> m;
Diagonal M(n, m);
for (int i = 0; i < n; i++) {
//M.Display(signal);
cin >> M.Element(i, i);
//system("cls");
}
M.Display(signal);
Matrixs.push_back(M);
break;
}
case 4:{
int n, m;
cout << "Input your Matrix dimention :";
cin >> n >> m;
UpperTriang M(n, m);
for (int i = n - 1; i > -1; i--) {
for (int j = 0; j <= i; j++) {
//M.Display(signal);
cin >> M.Element(i, j);
//system("cls");
}
}
M.Display(signal);
Matrixs.push_back(M);
break;
}
case 5:{
int n, m;
cout << "Input your Matrix dimention :";
cin >> n >> m;
DownTriang M(n, m);
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
//M.Display(signal);
cin >> M.Element(i, j);
//system("cls");
}
}
M.Display(signal);
Matrixs.push_back(M);
break;
}
case 6:{
int n, m;
cout << "Input your Matrix dimention :";
cin >> n >> m;
Simmetric M(n, m);
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
//M.Display(signal);
cin >> M.Element(i, j);
M.Element(j, i) = M.Element(i, j);
//system("cls");
}
}
M.Display(signal);
Matrixs.push_back(M);
break;
}
case 7:{
int n, m;
cout << "Input your Matrix dimention :";
cin >> n >> m;
Matrix M(n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
M.Element(i, j) = rand() % 10 + 1;
}
}
Matrixs.push_back(M);
break;
}
case 8: {
int j;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose Matrix you want to delete or '0' to go back to menu: " << endl;
cin >> j;
if (abs(j) > Matrixs.size() || j <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
if (Matrixs.size() == 1)
Matrixs.pop_back();
else {
Matrixs.erase(Matrixs.begin() + j - 1);
}
signal = 0;
break;
}
case 9: {
//system("cls");
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
for (int k = 0; k < Matrixs.size(); k++) {
cout << k + 1 << ") ";
signal = 1;
Matrixs.at(k).Display(signal);
}
signal = 0;
break;
}
case 10: {
int a, b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0 || Matrixs.size() == 1)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 2 Matrix you want to summarise: ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
Matrixs.at(a - 1) + Matrixs.at(b - 1);
signal = 0;
break;
}
case 11: {
int a, b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0 || Matrixs.size() == 1)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 2 Matrix you want to subtract: ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
for (int i = 0; i < Matrixs.size(); i++)
if (a != i || b != i)
Matrixs.at(a - 1) - Matrixs.at(b - 1);
signal = 0;
break;
}
case 12: {
int a;
double b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose Matrix you want to multiply and by what (can be double): ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
Matrixs.at(a - 1) * b;
signal = 0;
break;
}
case 13: {
int a, b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0 || Matrixs.size() == 1)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 2 Matrix you want to multiply: ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
Matrixs.push_back(Matrixs.at(a - 1) * Matrixs.at(b - 1));
signal = 0;
break;
}
case 14: {
int a;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 1 Matrix you want to transpose: ";
cin >> a;
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
Matrixs.push_back(Matrixs.at(a - 1).Transpose());
signal = 0;
break;
}
case 15: {
int a;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 1 Matrix which rank you want to know: ";
cin >> a;
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "\nRank = " << Matrixs.at(a - 1).Rank() << '\n' << endl;
signal = 0;
break;
}
case 16: {
int a;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 1 Matrix to invert: ";
cin >> a;
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
Matrixs.push_back(Matrixs.at(a - 1).InvertMat());
signal = 0;
break;
}
case 17: {
int a, i, j;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 1 Matrix view minor and 2 numbers (row and column): ";
cin >> a >> i >> j;
if (!Matrixs.at(a - 1).Element(i, j))
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << '\n' << endl;
Matrixs.at(a - 1).DisplayDet(i - 1, j - 1);
cout << '\n' << endl;
signal = 0;
break;
}
case 18: {
int a, b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0 || Matrixs.size() == 1)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 2 Matrix you want to multiply whit Adamar multiplication: ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
Matrixs.push_back(Matrixs.at(a - 1).Adamar(Matrixs.at(b - 1)));
signal = 0;
break;
}
case 19:{
int a;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 1 Matrix which trace you want to know: ";
cin >> a;
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "Choosen Matrix trace : " << Matrixs.at(a - 1).Trace() << endl;
signal = 0;
break;
}
case 20:{
int a;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 1 Matrix which Determinant you want to know: ";
cin >> a;
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
//Matrix Matr = Matrixs[a - 1];
cout << "\nChoosen Matrix Det : " << Matrixs[a - 1].DetByGauss() << '\n' << endl;
//Matr.~Matrix();
signal = 0;
break;
}
case 21:{
int a, b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0 || Matrixs.size() == 1)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 2 Vectors you want to scalar multiply: ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "\nScalar = " << Matrixs.at(a - 1).Scalar(Matrixs.at(b - 1)) << '\n' << endl;
signal = 0;
break;
}
case 22:{
int a, b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0 || Matrixs.size() == 1)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 2 Vectors you want to outer multiply: ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "\nExternal multiply: " << endl;
Matrixs.at(a - 1).Vect(Matrixs.at(b - 1)).Display(0);
signal = 0;
break;
}
case 23:{
int a;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose Vector that norm you want to know (Euclidean): ";
cin >> a;
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "\nNorm = " << Matrixs.at(a - 1).Norm() << '\n' << endl;
signal = 0;
break;
}
case 24:{
int a;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose Vector that norm you want to know (Max): ";
cin >> a;
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "\nMax norm = " << Matrixs.at(a - 1).MaxNorm() << '\n' << endl;
signal = 0;
break;
}
case 25:{
int a;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose Matrix that norm you want to know: ";
cin >> a;
if (abs(a) > Matrixs.size() || a <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "\nMatrix norm = " << Matrixs.at(a - 1).MatNorm() << '\n' << endl;
signal = 0;
break;
}
case 26:{
int a, b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0 || Matrixs.size() == 1)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 2 Vectors which angle you want to know: ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "\nAngle = " << Matrixs.at(a - 1).VectAngle(Matrixs.at(b - 1)) << '\n' << endl;
signal = 0;
break;
}
case 27: { system("cls");
break;
}
case 28:{
int a, b;
for (int i = 0; i < Matrixs.size(); i++) {
cout << i + 1 << ") ";
signal = 1;
Matrixs[i].Display(signal);
}
if (Matrixs.size() == 0 || Matrixs.size() == 1)
throw NotEnoughMatricies("\nNot enough matricies\nCreate them first\n");
cout << "Choose 2 Vectors to make cross: ";
cin >> a >> b;
if (a == b)
throw MatriciesShouldHaveDifferentNumbers("\nMatricies should have different numbers\n");
if (abs(a) > Matrixs.size() || a <= 0 || abs(b) > Matrixs.size() || b <= 0)
throw IncorrectNumberInput("\nIncorrect number input\n");
cout << "\nResulte = ";
Matrixs.at(a - 1).Cross(Matrixs.at(b - 1)).Display(0);
cout << endl;
signal = 0;
break;
}
case 0: {
char eXit;
do{
cout << "Exit will rewrite the file with given set of Matrixs. \nAre you sure you want to exit? y/n\n";
cin >> eXit;
if (eXit == 'n') break;
if (eXit == 'y') {
if (fileName == "file.txt"){
ofstream fileTxt_temp(fileName, ios_base::trunc);
for (int i = 0; i < Matrixs.size(); i++)
Matrixs.at(i) >> fileTxt_temp;
fileTxt_temp.close();
}
if (fileName == "file.bin"){
ofstream fileBin(fileName, ios_base::trunc | ios::binary);
for (int i = 0; i < Matrixs.size(); i++)
Matrixs.at(i).WriteBin(fileBin);
//fwrite('e', sizeof(char), 1, fileName);
fileBin.close();
}
return 0;
}
} while (eXit != 'n' || eXit != 'y');
}
default: return 0;
}
} while (sw != 0);
return 0;
}