-
Notifications
You must be signed in to change notification settings - Fork 2
/
linalg_mat.h
318 lines (273 loc) · 8.75 KB
/
linalg_mat.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#if !defined(_LINALG_MAT_H)
#define _LINALG_MAT_H
#include <vector>
#include <functional>
#include <numeric>
#include <array>
#include <string>
#include <assert.h>
#include <math.h>
#include <sstream>
#include <iomanip>
#include "linalg_matrix_base.h"
namespace mxm
{
// forward declaration
size_t index1D(size_t i, size_t j, bool major, const Shape& shape);
template<typename DType> class Matrix;
template<typename DType> class MatrixRef;
class Block;
class AutoShape;
AutoShape fixRow(size_t n);
AutoShape fixCol(size_t n);
AutoShape square();
template<typename DType>
struct Traits<Matrix<DType>>
{
using EntryType = DType;
using ArithType = typename Traits<DType>::ArithType; // norm type
using DerefType = Matrix<DType>;
static constexpr bool referable = true;
// static std::string to_string(const Matrix<DType>& mat, size_t prec=6)
// {
// return Traits<MatrixBase<Matrix<DType>>>::to_string(mat, prec);
// }
};
// end forward declaration
template<typename DType>
class Matrix: public MatrixBase<Matrix<DType>>
{
protected:
Shape shape_;
std::vector<DType> data_;
bool major_;
public:
using ThisType = Matrix<DType>;
using EntryType = typename Traits<ThisType>::EntryType;
using ArithType = typename Traits<ThisType>::ArithType;
~Matrix(){}
// Constructors:
// Constructor 00: default
Matrix();
// Constructor 01: move initialize
Matrix(const AutoShape& _shape, std::vector<DType>&& data, bool major=ROW);
// Constructor 02:
Matrix(const AutoShape& _shape, const std::vector<DType>& data={}, bool major=ROW);
// Constructor 03: standard copy constructor
Matrix(const ThisType& rhs);
// Constructor 04: standard move constructor
Matrix(ThisType&& rhs);
// Constructor 05: type cast constructor
template<typename DeriveType> Matrix(const MatrixBase<DeriveType>& rhs);
// Constructor 06: construct from MatrixRef
Matrix(const MatrixRef<DType>& rhs);
// assignment operators
// copy assignment
void operator = (const ThisType& rhs);
// move assignment
void operator = (ThisType&& rhs);
//
// static polymorphism
const DType & operator () (size_t i, size_t j) const { return data_.at(index1D(i,j,major_, shape_)); }
DType & operator () (size_t i, size_t j) { return data_.at(index1D(i,j,major_, shape_)); }
size_t shape(size_t ax) const {return shape_[ax];}
const Shape& shape() const {return shape_;}
//
const bool majorAxis() const {return major_;}
MatrixRef<DType> operator () (const Block& b) { return MatrixBase<ThisType>::operator()(b); };
const MatrixRef<DType> operator () (const Block& b) const { return MatrixBase<ThisType>::operator()(b); };
const DType * data() const { return data_.data(); }
void reshape(const AutoShape& shape, bool major);
};
class AutoShape
{
public:
enum EnumDynamic{
eRowDefined = 0,
eColDefined = 1,
eFullyDefined,
eSquare,
eUndefined};
#if 0
AutoShape(const Shape& shape): shape_(shape), defined_{true, true} {}
AutoShape(std::initializer_list<size_t> shape): shape_{*shape.begin(), *(shape.begin() + 1)}, defined_{true, true} {}
AutoShape(size_t row, size_t col, bool row_def, bool col_def): shape_{row, col}, defined_{row_def, col_def} {}
#else
AutoShape(const Shape& shape): shape_(shape), state_(eFullyDefined) {}
AutoShape(std::initializer_list<size_t> shape): shape_{*shape.begin(), *(shape.begin() + 1)}, state_(eFullyDefined) { assert(2 == shape.size()); }
AutoShape(const EnumDynamic& state, size_t row, size_t col): shape_{row, col}, state_(state) {}
#endif
Shape deduct(size_t total_num) const;
private:
Shape shape_;
// std::array<bool, 2> defined_;
EnumDynamic state_;
};
//
// implementations
//
// 00
template<typename DType>
Matrix<DType>::Matrix(): shape_({0,0}), data_(0), major_(ROW)
{
}
// 01
template<typename DType>
Matrix<DType>::Matrix(const AutoShape& _shape, std::vector<DType>&& data, bool major)
:shape_(_shape.deduct(data.size())), data_(0), major_(major)
{
if(data.size() > 0) data_.swap(data);
else data_.resize(shape_[0] * shape_[1]);
}
// 02
template<typename DType>
Matrix<DType>::Matrix(const AutoShape& _shape, const std::vector<DType>& data, bool major)
:shape_(_shape.deduct(data.size())), data_(shape_[0] * shape_[1]), major_(major)
{
if(data.size() > 0)
data_ = data;
}
// 03
template<typename DType>
Matrix<DType>::Matrix(const ThisType& rhs)
:shape_(rhs.shape_), data_(rhs.data_), major_(rhs.major_)
{}
// 04
template<typename DType>
Matrix<DType>::Matrix(ThisType&& rhs)
:shape_(rhs.shape_), data_(std::move(rhs.data_)), major_(rhs.major_)
{
rhs.shape_ = {0,0};
}
// 05
template<typename DType>
template<typename DeriveType>
Matrix<DType>::Matrix(const MatrixBase<DeriveType>& rhs)
:shape_(static_cast<const DeriveType&>(rhs).shape()), data_(shape_[0] * shape_[1]), major_(ROW)
{
this->traverse([&](auto i, auto j){
(*this)(i,j) = DType(static_cast<const DeriveType&>(rhs)(i,j));
});
}
// 06
template<typename DType>
Matrix<DType>::Matrix(const MatrixRef<DType>& rhs)
{
if(rhs.owner() == this) // self assignment
{
if(rhs.majorAxis() != major_ && rhs.shape() == Shape{shape_[1], shape_[0]})
{//deal with `mat = mat.T();`
major_ = ! major_;
}else if(rhs.majorAxis() == major_ && rhs.shape() == shape_)
{// deal with `mat = mat(Block({0,end()},{0,end()}));
return ;
}else // deal with general cases like `mat = mat(Block({1,end()}, {1,end()})).T();`
{ // create temporary object to avoid memory overlap
ThisType tmp(rhs);
(*this) = std::move(tmp);
}
}else // rhs is unrelated with this
{
shape_ = rhs.shape();
data_.resize(shape_[0] * shape_[1]);
major_ = rhs.majorAxis();
this->traverse([&](size_t i, size_t j){ (*this)(i,j) = rhs(i,j); });
}
}
template<typename DType>
void Matrix<DType>::operator = (const Matrix<DType>& rhs)
{
if(&rhs == this) return; // deal with `mat = mat;`
shape_ = rhs.shape();
data_ = rhs.data_;
major_ = rhs.major_;
}
template<typename DType>
void Matrix<DType>::operator = (Matrix<DType>&& rhs)
{
if(&rhs == this) return; // deal with `mat = mat;`
shape_ = rhs.shape();
data_.swap(rhs.data_);
major_ = rhs.major_;
rhs.shape_ = {0,0};
}
//
// end of ref operation
template<typename DType>
void Matrix<DType>::reshape(const AutoShape& shape, bool major)
{
auto new_shape = shape.deduct(data_.size());
assert(new_shape[0] * new_shape[1] == data_.size());
shape_ = new_shape;
major_ = major;
}
inline size_t index1D(size_t i, size_t j, bool major, const Shape& shape)
{
if(major == ROW) return (i * shape[1] + j);
return (j * shape[0] + i);
}
template<typename EntryType, typename=void>
typename Matrix<EntryType>::ArithType
norm(const Matrix<EntryType>& mat)
{
using BaseType = MatrixBase<Matrix<EntryType>>;
return norm(static_cast<const BaseType&>(mat));
}
#if 0
template<typename EntryType, typename=void>
std::string
to_string(const Matrix<EntryType>& mat, size_t prec=6)
{
using BaseType = MatrixBase<Matrix<EntryType>>;
return to_string(static_cast<const BaseType&>(mat), prec);
}
#endif
#if 0
template<typename EntryType, typename=void>
Matrix<EntryType>&
normalize(Matrix<EntryType>& mat)
{
using ArithType = typename Matrix<EntryType>::ArithType;
mat *= Traits<ArithType>::inv(mxm::norm(mat));
return mat;
}
template<typename EntryType, typename=void>
Matrix<EntryType>
normalized(const Matrix<EntryType>& mat)
{
Matrix<EntryType> ret(mat);
return normalize(ret);
}
#endif
template<class T, class = void>
struct has_parenthesis_operator: std::false_type{};
template<class T>
struct has_parenthesis_operator<
T, typename std::void_t< decltype(std::declval<T>().operator()(0)) >
>: std::true_type{};
template<typename DeriveType,
std::enable_if_t<has_parenthesis_operator<typename Traits<DeriveType>::EntryType>::value, int >T=0>
Matrix<typename Traits<DeriveType>::ArithType> matrixAtPart(const DeriveType& mat, size_t k)
{
Matrix<typename Traits<DeriveType>::ArithType> ret(mat.shape());
ret.traverse([&](auto i, auto j){
ret(i,j) = mat(i,j)(k);
});
return ret;
}
template<typename DeriveType,
std::enable_if_t<std::is_floating_point<typename Traits<DeriveType>::EntryType>::value, int >T=0>
Matrix<typename Traits<DeriveType>::EntryType> matrixAtPart(const DeriveType& mat, size_t k)
{
Matrix<typename Traits<DeriveType>::ArithType> ret(mat.shape());
ret.traverse([&](auto i, auto j){
ret(i,j) = mat(i,j);
});
return ret;
}
using Mat = Matrix<float>;
} // namespace mxm
#ifdef MXM_HEADER_ONLY
// #include "linalg_mat_inl.h"
#endif
#endif // _LINALG_MAT_H