|
| 1 | +// |
| 2 | +// algorithm - some algorithms in "Introduction to Algorithms", third edition |
| 3 | +// Copyright (C) 2018 lxylxy123456 |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as |
| 7 | +// published by the Free Software Foundation, either version 3 of the |
| 8 | +// License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | + |
| 19 | +#ifndef MAIN |
| 20 | +#define MAIN |
| 21 | +#define MAIN_LeastSquareApprox |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef FUNC_LeastSquareApprox |
| 25 | +#define FUNC_LeastSquareApprox |
| 26 | + |
| 27 | +#include "utils.h" |
| 28 | + |
| 29 | +#include "LUPSolve.cpp" |
| 30 | + |
| 31 | +template <typename T> |
| 32 | +Matrix<T> LeastSquareApprox(Matrix<T>& A, Matrix<T>& y) { |
| 33 | + Matrix<T> AT = A.transpose(); |
| 34 | + Matrix<T> b = SquareMatrixMultiply(AT, y, (T) 0); |
| 35 | + Matrix<T> ATA = SquareMatrixMultiply(AT, A, (T) 0); |
| 36 | + PT pi = LUPDecomposition(ATA); |
| 37 | + Matrix<T> c = LUPSolve(ATA, ATA, pi, b); |
| 38 | + return c; |
| 39 | +} |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifdef MAIN_LeastSquareApprox |
| 43 | +template <typename T> |
| 44 | +void main_T(const size_t n, const size_t m) { |
| 45 | + std::vector<int> int_a, int_b; |
| 46 | + random_integers(int_a, 0, m, m * n); |
| 47 | + random_integers(int_b, 0, m, m); |
| 48 | + std::vector<T> buf_a(m * n), buf_b(m); |
| 49 | + for (size_t i = 0; i < int_a.size(); i++) |
| 50 | + buf_a[i] = int_a[i]; |
| 51 | + for (size_t i = 0; i < int_b.size(); i++) |
| 52 | + buf_b[i] = int_b[i]; |
| 53 | + Matrix<T> A(m, n, buf_a); |
| 54 | + Matrix<T> b(m, 1, buf_b); |
| 55 | + std::cout << A << std::endl; |
| 56 | + Matrix<T> ans1(b), ans2(n, 0); |
| 57 | + Matrix<T> x = LeastSquareApprox(A, b); |
| 58 | + ans2 = ans2.concat_h(x); |
| 59 | + Matrix<T> bb = SquareMatrixMultiply(A, x, (T) 0); |
| 60 | + ans1 = ans1.concat_h(bb); |
| 61 | + for (size_t i = 0; i < m; i++) { |
| 62 | + output_integers(ans1[i], "\t"); |
| 63 | + } |
| 64 | + std::cout << std::endl; |
| 65 | + for (size_t i = 0; i < n; i++) { |
| 66 | + std::cout << "\t"; |
| 67 | + output_integers(ans2[i], "\t"); |
| 68 | + } |
| 69 | + std::cout << std::endl; |
| 70 | +} |
| 71 | + |
| 72 | +int main(int argc, char *argv[]) { |
| 73 | + const size_t type = get_argv(argc, argv, 1, 0); |
| 74 | + const size_t m = get_argv(argc, argv, 2, 10); |
| 75 | + const size_t n = get_argv(argc, argv, 3, 5); |
| 76 | + if (!type) |
| 77 | + main_T<double>(n, m); |
| 78 | + else |
| 79 | + main_T<Fraction<int>>(n, m); |
| 80 | + return 0; |
| 81 | +} |
| 82 | +#endif |
| 83 | + |
0 commit comments