-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhite_box_code.cpp
371 lines (300 loc) · 8.42 KB
/
white_box_code.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
//======== Copyright (c) 2021, FIT VUT Brno, All rights reserved. ============//
//
// Purpose: White Box - matrix class code
//
// $NoKeywords: $ivs_project_1 $white_box_code.cpp
// $Author: JMENO PRIJMENI <xlogin00@stud.fit.vutbr.cz>
// $Date: $2021-01-04
//============================================================================//
/**
* @file white_box_code.cpp
* @author JMENO PRIJMENI
*
* @brief Definice metod tridy reprezentujici matici.
*/
#include <iostream>
#include <stdexcept>
#include "white_box_code.h"
Matrix::Matrix(): mRows(1), mCols(1)
{
matrix = std::vector<std::vector< double > >(1, std::vector<double>(1, 0));
}
Matrix::Matrix(size_t row, size_t col): mRows(row), mCols(col)
{
if(row < 1 || col < 1)
throw std::runtime_error("Minimalni velikost matice je 1x1");
matrix = std::vector<std::vector< double > >(row, std::vector<double>(col, 0));
}
Matrix::~Matrix()
{
}
bool Matrix::set(size_t row, size_t col, double value)
{
if(!checkIndexes(row, col))
return false;
matrix[row][col] = value;
return true;
}
bool Matrix::set(std::vector<std::vector< double > > values)
{
bool fit = true;
if(matrix.size() != values.size())
{
fit = false;
}
for(int r = 0; r < matrix.size(); r++)
{
if(values[r].size() != matrix[r].size())
{
fit = false;
}
}
if(!fit)
{
return fit;
}
for(int r = 0; r < matrix.size(); r++)
{
for(int c = 0; c < matrix[0].size(); c++)
{
matrix[r][c] = values[r][c];
}
}
return true;
}
double Matrix::get(size_t row, size_t col)
{
if(!checkIndexes(row, col))
throw std::runtime_error("Pristup k indexu mimo matici");
return matrix[row][col];
}
bool Matrix::operator==(const Matrix m) const
{
if(!checkEqualSize(m))
throw std::runtime_error("Matice musi mit stejnou velikost.");
for(int r = 0; r < matrix.size(); r++)
{
for (int c = 0; c < matrix[r].size(); c++)
{
if(matrix[r][c] != m.matrix[r][c])
return false;
}
}
return true;
}
Matrix Matrix::operator+(const Matrix m) const
{
if(!checkEqualSize(m))
throw std::runtime_error("Matice musi mit stejnou velikost.");
Matrix result = Matrix(matrix.size(), matrix[0].size());
for(int r = 0; r < matrix.size(); r++)
{
for(int c = 0; c < matrix[r].size(); c++)
{
result.set(r, c, matrix[r][c] + m.matrix[r][c]);
}
}
return result;
}
Matrix Matrix::operator*(const Matrix m) const
{
if(matrix[0].size() == m.matrix.size())
{
Matrix result = Matrix(matrix.size(), m.matrix[0].size());
for(int r = 0; r < matrix.size(); r++)
{
for(int c = 0; c < m.matrix[0].size(); c++)
{
for(int i = 0; i < matrix[r].size(); i++)
{
result.set(r, c, result.get(r, c) + matrix[r][i] * m.matrix[i][c]);
}
}
}
return result;
}
else
{
throw std::runtime_error("Prvni matice musi stejny pocet sloupcu jako druha radku.");
}
}
Matrix Matrix::operator*(const double value) const
{
Matrix result = Matrix(matrix.size(), matrix[0].size());
for(int r = 0; r < matrix.size(); r++)
{
for(int c = 0; c < matrix[r].size(); c++)
{
result.set(r, c, matrix[r][c] * value);
}
}
return result;
}
std::vector<double> Matrix::solveEquation(std::vector<double> b)
{
std::vector<double> res = std::vector<double>(matrix.size(), 0);
std::vector<std::vector<double> > temp =
std::vector<std::vector< double > >(matrix.size(), std::vector<double>(matrix.size(), 0));
if(matrix[0].size() != b.size())
throw std::runtime_error("Pocet prvku prave strany rovnice musi odpovidat poctu radku matice.");
if(!checkSquare())
throw std::runtime_error("Matice musi byt ctvercova.");
double determinatAll = determinant();
if(abs(determinatAll) < std::numeric_limits<double>::epsilon())
throw std::runtime_error("Matice je singularni.");
for(int i = 0; i < matrix.size(); i++)
{
for(int j = 0; j < matrix[i].size(); j++)
{
temp[i][j] = matrix[i][j];
}
}
for(int i = 0; i < matrix.size(); i++)
{
for(int k = 0; k < matrix[i].size(); k++)
{
temp[k][i] = b[k];
}
res[i] = deter(temp, temp.size())/determinatAll;
for(int k = 0; k < matrix[i].size(); k++)
temp[k][i] = matrix[k][i];
}
return res;
}
bool Matrix::checkIndexes(size_t row, size_t col)
{
if(row >= matrix.size() || col >= matrix[0].size())
return false;
return true;
}
bool Matrix::checkSquare()
{
if(matrix.size() == matrix[0].size())
return true;
return false;
}
bool Matrix::checkEqualSize(const Matrix m) const
{
if(m.matrix.size() == matrix.size() && m.matrix[0].size() == matrix[0].size())
return true;
return false;
}
double Matrix::determinant()
{
if(matrix.size() == 1)
{
return matrix[0][0];
}
else if(matrix.size() == 2)
{
return matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1];
}
else if(matrix.size() == 3)
{
return matrix[0][0]*matrix[1][1]*matrix[2][2] +
matrix[0][1]*matrix[1][2]*matrix[2][0] +
matrix[0][2]*matrix[1][0]*matrix[2][1] -
matrix[2][0]*matrix[1][1]*matrix[0][2] -
matrix[2][1]*matrix[1][2]*matrix[0][0] -
matrix[2][2]*matrix[0][1]*matrix[1][0];
}
else
{
return deter(matrix, matrix.size());
}
}
std::vector< std::vector<double> > getMinimo( std::vector< std::vector<double> > src, int I, int J, int ordSrc )
{
std::vector< std::vector<double> > minimo( ordSrc-1, std::vector<double> (ordSrc-1,0));
int rowCont = 0;
for(int i=0; i < ordSrc; i++)
{
int colCont = 0;
if(i != I)
{
for(int j=0; j < ordSrc; j++)
{
if(j != J)
{
minimo[rowCont][colCont] = src[i][j];
colCont++;
}
}
rowCont++;
}
}
return minimo;
}
double Matrix::deter(std::vector<std::vector<double> > m, size_t n)
{
if(n == 1)
return m[0][0];
if(n == 2)
{
double mainDiag = m[0][0] * m[1][1];
double negDiag = m[1][0] * m[0][1];
return mainDiag - negDiag;
}
else
{
double det = 0;
for(int J = 0; J < n; J++)
{
std::vector< std::vector<double> > min = getMinimo( m, 0, J, n);
if((J % 2) == 0)
{
det += m[0][J] * deter( min, n-1);
}
else
{
det -= m[0][J] * deter( min, n-1);
}
}
return det;
}
return 0;
}
Matrix Matrix::transpose()
{
Matrix transposedMatrix(mCols, mRows);
for(int r = 0; r < mRows; r++)
{
for(int c = 0; c < mCols; c++)
{
transposedMatrix.set(c,r, matrix[r][c]);
}
}
return transposedMatrix;
}
Matrix Matrix::inverse()
{
Matrix inversedMatrix(mRows, mCols);
if(!(mRows == 2 && mCols == 2) && !(mRows == 3 && mCols == 3))
{
throw std::runtime_error("Matice musi byt velikosti 2x2 nebo 3x3.");
}
double deter = determinant();
if( abs(deter) < std::numeric_limits<double>::epsilon() )
{
throw std::runtime_error("Matice je singularni.");
}
if(mRows == 2 && mCols == 2)
{
inversedMatrix.set(0, 0, matrix[1][1] / deter);
inversedMatrix.set(1, 0, -1.0 * matrix[1][0] / deter);
inversedMatrix.set(0, 1, -1.0 * matrix[0][1] / deter);
inversedMatrix.set(1, 1, matrix[0][0] / deter);
}
else
{
for(int r = 0; r < mRows; r++)
{
for(int c = 0; c < mCols; c++)
{
inversedMatrix.set(c, r, (matrix[(r+1)%3][(c+1)%3]*matrix[(r+2)%3][(c+2)%3] - matrix[(r+2)%3][(c+1)%3]*matrix[(r+1)%3][(c+2)%3]) / deter);
}
}
}
return inversedMatrix;
}
/*** Konec souboru white_box_code.cpp ***/