-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab_03.cpp
197 lines (177 loc) · 5.23 KB
/
Lab_03.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
// Варіант 14
#include <iostream>
#include <cmath>
#include <string>
const int MatrixSize = 3;
using namespace std;
void DisplayMatrixWithB(string msg, double const arrayA[][MatrixSize], double const arrayB[])
{
cout << msg << endl;
for (int i = 0; i < MatrixSize; i++)
{
for (int j = 0; j < MatrixSize; j++)
{
cout << arrayA[i][j] << "\t";
}
cout << arrayB[i] << endl;
}
}
void DisplayMatrix(string msg, double const arrayA[][MatrixSize])
{
cout << msg << endl;
for (int i = 0; i < MatrixSize; i++)
{
for (int j = 0; j < MatrixSize; j++)
{
cout << arrayA[i][j] << "\t";
}
cout << endl;
}
}
double determinant(double array[][MatrixSize])
{
double answer;
double diagonal = array[0][0] * array[1][1] * array[2][2];
double triangle1 = array[0][1] * array[1][2] * array[2][0];
double triangle2 = array[0][2] * array[1][0] * array[2][1];
double antidiagonal = array[0][2] * array[1][1] * array[2][0];
double antitriangle1 = array[0][0] * array[1][2] * array[2][1];
double antitriangle2 = array[1][0] * array[0][1] * array[2][2];
answer = diagonal + triangle1 + triangle2 - antidiagonal - antitriangle1 - antitriangle2;
return answer;
}
void copyMatrix(double const arrayA[][3], double tmpArray[][3])
{
for (int i = 0; i < MatrixSize; i++)
{
for (int j = 0; j < MatrixSize; j++)
{
tmpArray[i][j] = arrayA[i][j];
}
}
}
void Cramer(double arrayA[][MatrixSize], double arrayB[])
{
cout << "----------------------------------------------------------------\n\
\t\t\t Cramer's rule" << endl;
double tmparray[MatrixSize][MatrixSize];
double deltax, deltay, deltaz;
double det = determinant(arrayA);
cout << "Array determinant: " << det << endl;
copyMatrix(arrayA, tmparray);
for (int i = 0; i < MatrixSize; i++)
{
tmparray[i][0] = arrayB[i];
}
DisplayMatrix( "Delta x matrix: ", tmparray);
deltax = determinant(tmparray);
cout << "Delta x determinant: " << deltax << endl;
copyMatrix(arrayA, tmparray);
for (int i = 0; i < MatrixSize; i++)
{
tmparray[i][1] = arrayB[i];
}
DisplayMatrix("Delta y matrix: ", tmparray);
deltay = determinant(tmparray);
cout << "Delta y determinant: " << deltay << endl;
copyMatrix(arrayA, tmparray);
for (int i = 0; i < MatrixSize; i++)
{
tmparray[i][2] = arrayB[i];
}
DisplayMatrix("Delta z matrix: ", tmparray);
deltaz = determinant(tmparray);
cout << "Delta z determinant: " << deltaz << endl;
cout << "Result x1: " << deltax / det << endl;
cout << "Result x2: " << deltay / det << endl;
cout << "Result x3: " << deltaz / det << endl;
cout << "----------------------------------------------------------------" << endl;
}
double detMinor(double mas[][MatrixSize], int n, int m)
{
double minor[4];
int k = 0;
for (int i = 0; i < MatrixSize; ++i)
{
for (int j = 0; j < MatrixSize; ++j)
{
if ((i != n) && (j != m))
{
minor[k] = mas[i][j];
k++;
}
}
}
double det = minor[0] * minor[3] - minor[1] * minor[2];
return det;
}
void cofactor(double arr[][MatrixSize], double cofactorMatr[][MatrixSize])
{
double det = determinant(arr);
for (int i = 0; i < MatrixSize; i++)
{
for (int j = 0; j < MatrixSize; j++)
{
cofactorMatr[i][j] = pow(-1, i + j) * detMinor(arr, i, j) / det;
}
}
}
void multiplyMatrixes(double first[][MatrixSize], double second[], double result[3])
{
double sum = 0;
for (int i = 0; i < MatrixSize; ++i)
{
sum = 0;
for (int k = 0; k < MatrixSize; ++k)
{
sum += first[i][k] * second[k];
}
result[i] = sum;
}
}
void InverseMatrixMethod(double arrayA[][MatrixSize], double arrayB[]) {
cout << "----------------------------------------------------------------\n\
\t\t\t Inverse Matrix Method" << endl;
double arrayCof[3][3], transponsed[3][3], result[3];
cofactor(arrayA, arrayCof);
DisplayMatrix("Array^-1: ", arrayCof);
// transponse matrix
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
transponsed[i][j] = arrayCof[j][i];
}
}
multiplyMatrixes(transponsed, arrayB, result);
cout << "Result x1: " << result[0] << endl;
cout << "Result x2: " << result[1] << endl;
cout << "Result x3: " << result[2] << endl;
cout << "----------------------------------------------------------------" << endl;
}
int main()
{
double arrayA[MatrixSize][MatrixSize];
double arrayB[MatrixSize];
char way;
cout << "i - input, f - file" << endl;
cin >> way;
if (way == 'f') {
freopen("file.txt", "r", stdin);
}
for(int i = 0; i < MatrixSize; i++) {
for (int j = 0; j < MatrixSize; j++) {
cin >> arrayA[i][j];
}
}
for (int i = 0; i < 3; i++) {
cin >> arrayB[i];
}
if (!determinant(arrayA)){
cout << "Det is 0";
return 0;
}
Cramer(arrayA, arrayB);
InverseMatrixMethod(arrayA, arrayB);
return 0;
}