This repository has been archived by the owner on Nov 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LeastSquareApprox.cpp
83 lines (75 loc) · 2.39 KB
/
LeastSquareApprox.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
//
// algorithm - some algorithms in "Introduction to Algorithms", third edition
// Copyright (C) 2018 lxylxy123456
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef MAIN
#define MAIN
#define MAIN_LeastSquareApprox
#endif
#ifndef FUNC_LeastSquareApprox
#define FUNC_LeastSquareApprox
#include "utils.h"
#include "LUPSolve.cpp"
template <typename T>
Matrix<T> LeastSquareApprox(Matrix<T>& A, Matrix<T>& y) {
Matrix<T> AT = A.transpose();
Matrix<T> b = SquareMatrixMultiply(AT, y, (T) 0);
Matrix<T> ATA = SquareMatrixMultiply(AT, A, (T) 0);
PT pi = LUPDecomposition(ATA);
Matrix<T> c = LUPSolve(ATA, ATA, pi, b);
return c;
}
#endif
#ifdef MAIN_LeastSquareApprox
template <typename T>
void main_T(const size_t n, const size_t m) {
std::vector<int> int_a, int_b;
random_integers(int_a, 0, m, m * n);
random_integers(int_b, 0, m, m);
std::vector<T> buf_a(m * n), buf_b(m);
for (size_t i = 0; i < int_a.size(); i++)
buf_a[i] = int_a[i];
for (size_t i = 0; i < int_b.size(); i++)
buf_b[i] = int_b[i];
Matrix<T> A(m, n, buf_a);
Matrix<T> b(m, 1, buf_b);
std::cout << A << std::endl;
Matrix<T> ans1(b), ans2(n, 0);
Matrix<T> x = LeastSquareApprox(A, b);
ans2 = ans2.concat_h(x);
Matrix<T> bb = SquareMatrixMultiply(A, x, (T) 0);
ans1 = ans1.concat_h(bb);
for (size_t i = 0; i < m; i++) {
output_integers(ans1[i], "\t");
}
std::cout << std::endl;
for (size_t i = 0; i < n; i++) {
std::cout << "\t";
output_integers(ans2[i], "\t");
}
std::cout << std::endl;
}
int main(int argc, char *argv[]) {
const size_t type = get_argv(argc, argv, 1, 0);
const size_t m = get_argv(argc, argv, 2, 10);
const size_t n = get_argv(argc, argv, 3, 5);
if (!type)
main_T<double>(n, m);
else
main_T<Fraction<int>>(n, m);
return 0;
}
#endif