-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic.hpp
163 lines (143 loc) · 4.9 KB
/
Basic.hpp
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
#ifndef BASIC_H
#define BASIC_H
#include <ctime>
#include <algorithm> // for max_element
#include <cmath> // for sqrt
#include "Matrix.hpp"
namespace Lee{
template<typename T, int N>
Matrix<T, N, N> eye(){
Matrix<T, N, N> m;
for(int i = 0; i != N; ++i)
m(i, i) = 1;
return m;
}
template<typename T, int M, int N>
Matrix<T, M, N> rand(){
Matrix<T, M, N> tmp;
std::srand(time(nullptr));
for(int i = 0; i != M; ++i)
for(int j = 0; j != N; ++j)
tmp(i, j) = std::rand()%20+1;
return tmp;
}
template<typename T, int M, int N>
T max(const Matrix<T, M, N> &m){
return *std::max_element(m.cbegin(), m.cend());
}
template<typename T, int M, int N>
Matrix<T, M, N> abs(const Matrix<T, M, N> &m){
Matrix<T, M, N> res = m;
for(int i = 0; i < M; ++i)
for(int j = 0; j < N; ++j)
res(i, j) = ((m(i, j) > 0) ? m(i, j) : -m(i, j));
return res;
}
template<typename T, int N>
Matrix<T, N, N>& power(Matrix<T, N, N> &m, int k){
while(--k){
m = m*m;
}
return m;
}
template<typename T, int M, int N>
void Matrix<T, M, N>::permute(int r1, int r2){
if(r1 >= M || r2 >= M) throw std::out_of_range("Matrix index");
for(int j = 0; j != N; ++j){
std::swap((*this)(r1, j), (*this)(r2, j));
}
}
template<typename T, int M, int N>
int rank(const Matrix<T, M, N> &m){
return pivot(m).size();
}
template<typename T, int M, int N>
Matrix<T, M-1, N-1> left(const Matrix<T, M, N> &m, int ii, int jj){
if(M<1 || N<1 || ii<0 || ii>=M || jj<0 || jj>=N) throw std::out_of_range("Matrix index");
Matrix<T, M-1, N-1> res;
for(int i = 0; i != M-1; ++i)
for(int j = 0; j != N-1; ++j){
if(j>=jj && i<ii) res(i, j) = m(i, j+1);
else if(i>=ii && j<jj) res(i, j) = m(i+1, j);
else if(i>=ii && j>=jj) res(i, j) = m(i+1, j+1);
else res(i, j) = m(i, j);
}
return res;
}
template<typename T, int N>
T det(Matrix<T, N, N> m){
if(N==1) return m(0, 0);
double num = 0;
int cntr = 0, cntc = 0;
for(int i = 0; i != N; ++i) if(!m(i, 0)) ++cntr;
for(int i = 0; i != N; ++i) if(!m(0, i)) ++cntc;
if(cntr > cntc)
{
for(int i = 0; i != N; ++i){
if(!m(i, 0)) num += 0;
else num += (m(i, 0)*cofactor(m, i, 0));
}
}
else{
for(int i = 0; i != N; ++i){
if(!m(0, i)) num += 0;
else num += (m(0, i)*cofactor(m, 0, i));
}
}
return num;
}
template<>
double det<double, 1>(Matrix<double, 1, 1> m){
return m(0, 0);
}
template<typename T, int N>
T cofactor(Matrix<T, N, N> m, int i, int j){
return std::pow(-1, i+j)*det(left(m, i, j));
}
template<typename T, int N>
Matrix<T, N, N> adj(const Matrix<T, N, N> &m){
Matrix<T, N, N> res;
for(int i = 0; i != N; ++i)
for(int j = 0; j != N; ++j)
res(i, j) = cofactor(m, j, i);
return res;
}
template<typename T, int N>
Matrix<T, N, N> inv(Matrix<T, N, N> &m){
Matrix<T, N, N> res;
if(m.is_invertible()){
if(m.is_diagonal()){
for(int i = 0; i < N; ++i)
res(i, i) = 1/m(i, i);
}
else{
Matrix<T, N, 2*N> aug = col_cat<T, N, 2*N>(m, eye<T, N>());
aug = rref(aug);
res = col_split<T, N, N>(aug, N, 2*N-1);
}
}
return res;
}
template<typename T, int M, int N>
Matrix<T, N, 1> least_square(const Matrix<T, M, N> &A, const Matrix<T, M, 1> &b){
Lee::Matrix<double, N, N> S = transpose(A)*A;
Lee::Matrix<double, N, 1> x = inv(S)*transpose(A)*b;
Lee::Matrix<double, M, 1> p = A*x;
Lee::Matrix<double, M, 1> e = b-p;
Lee::Matrix<double, M, M> P = A*inv(S)*transpose(A);
// cout << "A and b \n";
// cout << A << "\n";
// cout << b << "\n";
// cout << "solution to x: \n" << x << "\n";
// cout << "the error: \n" << e << "\n";
// cout << "the projection matrix:\n" << P << "\n";
// cout << "the transpose matrix:\n" << transpose(P) << "\n";
// cout << std::boolalpha << (P == transpose(P)) << (P*P == P) << (P(0, 0) == transpose(P)(0, 0));
return x;
}
template<typename T, int N>
T norm2(const Matrix<T, N, 1> &m){
return sqrt((transpose(m)*m)(0, 0));
}
}
#endif