-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_operations.cpp
59 lines (49 loc) · 1.51 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
#include "functions.h"
/**
* \brief Заполняет матрицу A для алгоритма одномерной линейной регрессии
* \param width Ширина окна в линеаризации
* \returns матрицу A
*/
int** const getMatrixA(const int width) {
int** A = new int*[2];
A[0] = new int[width / 2 + 1];
A[1] = new int[width / 2 + 1];
for (int i = 0; i <= width / 2; ++i) {
A[0][i] = 1;
A[1][i] = i + 1;
}
return A;
}
/**
* \brief Просчитывает матрицу (A' * A)^(-1)
* \param width Ширина окна в линеаризации
* \returns матрицу (A' * A)^(-1)
*/
double** const getInverseSquareA(const int width) {
int** const A = getMatrixA(width);
int squareA[2][2] = { { 0, 0 },{ 0, 0 } };
for (int i = 0; i < 2; ++i) {
for (int k = 0; k < 2; ++k) {
for (int j = 0; j <= width / 2; ++j) {
squareA[i][k] += A[i][j] * A[k][j];
}
}
}
int determinant = squareA[0][0] * squareA[1][1] - squareA[0][1] * squareA[1][0];
double** inverseASquare = new double*[2];
inverseASquare[0] = new double[2];
inverseASquare[1] = new double[2];
inverseASquare[0][0] = squareA[1][1];
inverseASquare[0][1] = -squareA[0][1];
inverseASquare[1][0] = squareA[0][0];
inverseASquare[1][1] = -squareA[1][0];
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
inverseASquare[i][j] /= determinant;
}
}
delete[] A[0];
delete[] A[1];
delete[] A;
return inverseASquare;
}