-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_operation.h
205 lines (178 loc) · 6.3 KB
/
matrix_operation.h
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Created by liuxi on 2018/12/30.
//
#ifndef NUMOPT3_MATRIX_OPERATION_H
#define NUMOPT3_MATRIX_OPERATION_H
#include "matrix.h"
template <template <typename> typename M1, \
typename T1, typename Func,\
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto reduce_vertical(const M1<T1>& mat, const Func& func) {
using RT = decltype(func(T1(),T1()));
Matrix<RT> c(1,mat.cols());
int rn = mat.rows();
int cn = mat.cols();
for(int ci=0;ci<cn;ci++) {
RT rt = mat(0,ci);
for(int ri=1;ri<rn;ri++) {
rt=func(rt,mat(ri,ci));
}
c(0,ci)=rt;
}
return c;
}
template <template <typename> typename M1, \
typename T1, typename Func,\
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto reduce_horizontal(const M1<T1>& mat, const Func& func) {
using RT = decltype(func(T1(),T1()));
Matrix<RT> c(mat.rows(),1);
int rn = mat.rows();
int cn = mat.cols();
for(int ri=0;ri<rn;ri++) {
RT rt = mat(ri,0);
for(int ci=1;ci<cn;ci++) {
rt=func(rt,mat(ri,ci));
}
c(ri,0)=rt;
}
return c;
}
template <template <typename> typename M1, \
typename T1, typename Func,\
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto reduce_all(const M1<T1>& mat, const Func& func) {
using RT = decltype(func(T1(),T1()));
int n = mat.count();
RT rt = mat(0);
for(int i=1;i<n;i++) rt = func(rt,mat(i));
return Matrix<RT>(1,1,rt);
}
enum class REDUCE_DIRECTION {
HORIZONTAL,
VERTICAL,
ALL
};
template <template <typename> typename M1, \
typename T1, typename Func,\
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto reduce(const M1<T1>& mat, const Func& func, REDUCE_DIRECTION rd) {
switch (rd) {
case REDUCE_DIRECTION::HORIZONTAL:
return reduce_horizontal(mat,func);
break;
case REDUCE_DIRECTION::VERTICAL:
return reduce_vertical(mat,func);
break;
case REDUCE_DIRECTION::ALL:
return reduce_all(mat,func);
break;
}
}
template <template <typename> typename M1, typename T1, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto sum(const M1<T1>& mat, REDUCE_DIRECTION rd) {
return reduce(mat,[](const T1&t1,const T1&t2){return t1+t2;},rd);
}
template <template <typename> typename M1, typename T1, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto magnitude(const M1<T1>& mat) {
return sqrt(sum(mat*mat,REDUCE_DIRECTION::ALL)(0));
}
template <template <typename> typename M1, typename T1, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto all(const M1<T1>& mat, REDUCE_DIRECTION rd) {
return reduce(mat,[](const T1&t1,const T1&t2){return t1 && t2;},rd);
}
template <template <typename> typename M1, typename T1, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto any(const M1<T1>& mat, REDUCE_DIRECTION rd) {
return reduce(mat,[](const T1&t1,const T1&t2){return t1 || t2;},rd);
}
template <template <typename> typename M1,template <typename> typename M2, \
typename T1, typename T2, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (!std::is_base_of_v<IsMatrix, T2>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>) \
&& (std::is_base_of_v<IMatrix<T2>, M2<T2>>),int>> \
Matrix<decltype(T1()*T2())> matmul(const M1<T1>&m1,const M2<T2>&m2) {
assert(m1.cols()==m2.rows());
using RT = decltype(T1()*T2());
Matrix<RT> ret(m1.rows(),m2.cols());
int cn = ret.cols();
int rn = ret.rows();
int kn = m1.cols();
for(int r=0;r<rn;r++) {
for(int c=0;c<cn;c++) {
RT rt(0);
for(int k=0;k<kn;k++) rt += m1(r,k)*m2(k,c);
ret(r,c)=rt;
}
}
return ret;
}
template <template <typename> typename M1, typename T1, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
void swap_row(const M1<T1>& mat, int r1, int r2) {
int cn = mat.cols();
T1 t;
for(int c=0;c<cn;c++) {
t = mat(r1,c);
mat(r1,c) = mat(r2,c);
mat(r2,c) = t;
}
}
#include <iostream>
template <template <typename> typename M1, typename T1, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
auto inverse(const M1<T1>& mat) {
assert(mat.rows()==mat.cols());
int n = mat.rows();
Matrix<T1> b(n,2*n);
b(Range(Point{0,0},Size{n,n})) = mat;
b(Range(Point{n,0},Size{n,n})) = Matrix<T1>::eye(n);
//std::cout<<b<<std::endl;
for(int r=0;r<n;r++) {
T1 max_ = b(r,r);
int max_r = r;
for(int i=r;i<n;i++)
if(max_<b(i,r)) {
max_=b(i,r);
max_r = i;
}
//std::cout<<"max:"<<max_<<" row:"<<max_r<<std::endl;
if(max_r!=r) swap_row(b,max_r,r);
//std::cout<<b<<std::endl;
b.row(r)/=max_;
//std::cout<<b<<std::endl;
for(int i=r+1;i<n;i++) b.row(i)-=b.row(r)*b(i,r);
for(int i=r-1;i>=0;i--) b.row(i)-=b.row(r)*b(i,r);
//std::cout<<b<<std::endl;
}
auto re = b(Range(Point{n,0},Size{n,n})).copy();
//std::cout<<re<<std::endl;
//std::cout<<matmul(mat,re)<<std::endl;
return re;
}
#endif //NUMOPT3_MATRIX_OPERATION_H