Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit 180990d

Browse files
committed
LeastSquareApprox.cpp
1 parent 8155c44 commit 180990d

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

LeastSquareApprox.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
| 28 | LUPSolve.cpp | LU Decomposition | 821 |
189189
| 28 | LUPSolve.cpp | LUP Decomposition | 824 |
190190
| 28 | MatrixInverse.cpp | Matrix Inverse | 828 |
191+
| 28 | LeastSquareApprox.cpp | Least Square Approx | 837 |
191192

192193
# Supplementary Files
193194
* `utils.h`: Utils

SquareMatrixMultiply.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ class Matrix {
130130
ans.rows += rhs.rows;
131131
return ans;
132132
}
133+
Matrix<T> transpose() {
134+
Matrix<T> ans(cols, rows, 0);
135+
for (size_t i = 0; i < cols; i++)
136+
for (size_t j = 0; j < rows; j++)
137+
ans[i][j] = (*this)[j][i];
138+
return ans;
139+
}
133140
void add_row(T T0) {
134141
rows += 1;
135142
data.push_back(MatrixRow<T>(cols, T0));

0 commit comments

Comments
 (0)