-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmat_ref.cpp
More file actions
272 lines (234 loc) · 5.19 KB
/
Copy pathmat_ref.cpp
File metadata and controls
272 lines (234 loc) · 5.19 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "PAZ_Math"
paz::MatRef::MatRef(const double* ptr, std::size_t origRows, std::size_t
origCols, std::size_t blockRows, std::size_t blockCols) : _begin({ptr, 0,
static_cast<iterator::difference_type>(origRows), static_cast<iterator::
difference_type>(blockRows)}), _origCols(origCols), _blockCols(blockCols) {}
paz::MatRef::MatRef(const Mat& m) : MatRef(m.data(), m.rows(), m.cols(), m.
rows(), m.cols()) {}
double paz::MatRef::det() const
{
return Mat(*this).det();
}
paz::Mat paz::MatRef::inv() const
{
return Mat(*this).inv();
}
paz::Mat paz::MatRef::solve(const Mat& b) const
{
return Mat(*this).solve(b);
}
paz::Mat paz::MatRef::chol() const
{
return Mat(*this).chol();
}
paz::Mat paz::MatRef::cholUpdate(const Mat& m, double a) const
{
return Mat(*this).cholUpdate(m, a);
}
paz::Vec paz::MatRef::eig() const
{
return Mat(*this).eig();
}
paz::Vec paz::MatRef::eig(Mat& vecs) const
{
return Mat(*this).eig(vecs);
}
void paz::MatRef::qr(Mat& q, Mat& r) const
{
Mat(*this).qr(q, r);
}
void paz::MatRef::qr(Mat& q, Mat& r, std::vector<std::size_t>& p) const
{
Mat(*this).qr(q, r, p);
}
paz::Mat paz::MatRef::trans() const
{
return Mat(*this).trans();
}
paz::Vec paz::MatRef::diag() const
{
if(rows() != cols() || empty())
{
throw std::runtime_error("Matrix must be square.");
}
Vec res(rows());
for(std::size_t i = 0; i < rows(); ++i)
{
res(i) = operator()(i, i);
}
return res;
}
paz::Mat paz::MatRef::rep(std::size_t m, std::size_t n) const
{
Mat res(m*rows(), n*cols());
for(std::size_t i = 0; i < m*n; ++i)
{
std::copy(begin(), end(), res.begin() + rows()*cols()*i);
}
return res;
}
double paz::MatRef::normSq() const
{
return dot(*this);
}
double paz::MatRef::norm() const
{
return std::sqrt(normSq());
}
double paz::MatRef::sum() const
{
return std::accumulate(begin(), end(), 0.);
}
paz::Vec paz::MatRef::rowSum() const
{
Vec res = Vec::Zero(rows());
for(std::size_t i = 0; i < rows(); ++i)
{
for(std::size_t j = 0; j < cols(); ++j)
{
res(i) += operator()(i, j);
}
}
return res;
}
paz::Mat paz::MatRef::colSum() const
{
Mat res = Mat::Zero(1, cols());
for(std::size_t i = 0; i < cols(); ++i)
{
for(std::size_t j = 0; j < rows(); ++j)
{
res(0, i) += operator()(j, i);
}
}
return res;
}
double paz::MatRef::min() const
{
return *std::min_element(begin(), end());
}
double paz::MatRef::max() const
{
return *std::max_element(begin(), end());
}
paz::Mat paz::MatRef::normalized() const
{
Mat m = *this;
m /= norm();
return m;
}
paz::Mat paz::MatRef::prod(const MatRef& rhs) const // elementwise
{
if(rows() != rhs.rows() || cols() != rhs.cols())
{
throw std::runtime_error("Matrix dimensions do not match.");
}
Mat m = *this;
for(std::size_t i = 0; i < size(); ++i)
{
m(i) *= rhs(i);
}
return m;
}
paz::Mat paz::MatRef::quot(const MatRef& rhs) const // elementwise
{
if(rows() != rhs.rows() || cols() != rhs.cols())
{
throw std::runtime_error("Matrix dimensions do not match.");
}
Mat m = *this;
for(std::size_t i = 0; i < size(); ++i)
{
m(i) /= rhs(i);
}
return m;
}
paz::Mat paz::MatRef::operator*(const MatRef& rhs) const
{
Mat m = *this;
m *= rhs;
return m;
}
paz::Mat paz::MatRef::operator+(const MatRef& rhs) const
{
Mat m = *this;
m += rhs;
return m;
}
paz::Mat paz::MatRef::operator-(const MatRef& rhs) const
{
Mat m = *this;
m -= rhs;
return m;
}
paz::Mat paz::MatRef::operator*(double rhs) const
{
Mat m = *this;
m *= rhs;
return m;
}
paz::Mat paz::MatRef::operator/(double rhs) const
{
Mat m = *this;
m /= rhs;
return m;
}
paz::Mat paz::MatRef::operator-() const
{
return -Mat(*this);
}
double paz::MatRef::dot(const MatRef& rhs) const
{
if(rows() != rhs.rows() || cols() != rhs.cols())
{
throw std::runtime_error("Matrices must have the same dimensions.");
}
double res = 0.;
for(std::size_t i = 0; i < size(); ++i)
{
res += operator()(i)*rhs(i);
}
return res;
}
paz::Vec paz::MatRef::cross(const MatRef& rhs) const
{
return Mat(*this).cross(rhs);
}
paz::MatRef paz::MatRef::block(std::size_t startRow, std::size_t startCol, std::
size_t numRows, std::size_t numCols) const
{
if(startRow + numRows > rows() || startCol + numCols > cols())
{
throw std::runtime_error("Block is out of range.");
}
return MatRef(_begin.ptr + startRow + _begin.origRows*startCol, _begin.
origRows, _origCols, numRows, numCols);
}
paz::MatRef paz::MatRef::row(std::size_t m) const
{
return block(m, 0, 1, cols());
}
paz::MatRef paz::MatRef::col(std::size_t n) const
{
return block(0, n, rows(), 1);
}
bool paz::MatRef::hasNan() const
{
for(auto n : *this)
{
if(std::isnan(n))
{
return true;
}
}
return false;
}
paz::Mat paz::MatRef::abs() const
{
Mat m(rows(), cols());
for(std::size_t i = 0; i < size(); ++i)
{
m._vals[i] = std::abs(operator()(i));
}
return m;
}