This repository has been archived by the owner on Oct 25, 2019. It is now read-only.
/
var_diagonal_matrix.h
238 lines (178 loc) · 8.57 KB
/
var_diagonal_matrix.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
/* =================================================================================================
(c - MIT) T.W.J. de Geus (Tom) | tom@geus.me | www.geus.me | github.com/tdegeus/cppmat
================================================================================================= */
#ifndef CPPMAT_VAR_DIAGONAL_MATRIX_H
#define CPPMAT_VAR_DIAGONAL_MATRIX_H
// -------------------------------------------------------------------------------------------------
#include "cppmat.h"
// -------------------------------------------------------------------------------------------------
namespace cppmat {
namespace diagonal {
// =================================================================================================
// cppmat::diagonal::matrix
// =================================================================================================
template<class X>
class matrix
{
protected:
size_t mSize=0; // total size == data.size()
static const size_t mRank=2; // rank (number of axes)
size_t N=0; // number of rows/columns
std::vector<X> mData; // data container
X mZero[1]; // pointer to a zero entry
bool mPeriodic=false; // if true: disable bounds-check where possible
public:
// constructor: default
matrix();
// constructor: allocate, don't initialize
matrix(size_t m, size_t n);
// constructor: copy from own class (with different type)
template<typename U, typename=typename std::enable_if<std::is_convertible<U,X>::value>::type>
matrix(const cppmat::diagonal::matrix<U> &A);
// constructor: copy from fixed size
template<size_t m, size_t n> matrix(const cppmat::tiny::diagonal::matrix<X,m,n> &A);
// constructor: copy from view
template<size_t m, size_t n> matrix(const cppmat::view::diagonal::matrix<X,m,n> &A);
// named constructor: initialize
static matrix<X> Random (size_t m, size_t n, X lower=(X)0, X upper=(X)1);
static matrix<X> Arange (size_t m, size_t n);
static matrix<X> Zero (size_t m, size_t n);
static matrix<X> Ones (size_t m, size_t n);
static matrix<X> Constant(size_t m, size_t n, X D);
static matrix<X> Copy (size_t m, size_t n, const std::vector<X> &D);
// named constructor: copy
template<typename Itr> static matrix<X> Copy (size_t m, size_t n, Itr first);
template<typename Itr> static matrix<X> Copy (size_t m, size_t n, Itr first, Itr last);
template<typename Itr> static matrix<X> CopyDense(size_t m, size_t n, Itr first);
template<typename Itr> static matrix<X> CopyDense(size_t m, size_t n, Itr first, Itr last);
// return plain storage as vector
template<typename U, typename=typename std::enable_if<std::is_convertible<U,X>::value>::type>
operator std::vector<U> () const;
// resize
void resize(size_t m, size_t n);
void resize(size_t m, size_t n, const X &D);
// modify bounds-checks
void setPeriodic(bool periodic);
// get dimensions
size_t size() const;
size_t rank() const;
size_t shape(int i) const;
size_t shape(size_t i) const;
std::vector<size_t> shape() const;
// index operators: access plain storage
X& operator[](size_t i);
const X& operator[](size_t i) const;
// index operators: access using matrix-indices
X& operator()(int a, int b);
const X& operator()(int a, int b) const;
// index operators: access using matrix-indices
template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
X& operator()(T a, T b);
template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
const X& operator()(T a, T b) const;
// index operators: matrix-indices -> plain storage (a,b -> i)
size_t compress(int a, int b) const;
// index operators: matrix-indices -> plain storage (a,b -> i)
template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
size_t compress(T a, T b) const;
// index operators: plain storage -> matrix-indices (i -> a,b)
std::vector<size_t> decompress(size_t i) const;
// pointer to data
X* data();
const X* data() const;
// iterator to first and last entry
auto begin();
auto begin() const;
auto end();
auto end() const;
// iterator to specific entry: access plain storage
auto index(size_t i);
auto index(size_t i) const;
// iterator to specific entry: access using matrix-indices
auto item(int a, int b);
auto item(int a, int b) const;
// iterator to specific entry: access using matrix-indices
template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
auto item(T a, T b);
template<typename T, typename=typename std::enable_if<std::is_unsigned<T>::value,void>::type>
auto item(T a, T b) const;
// initialization
void setRandom(X lower=(X)0, X upper=(X)1);
void setArange();
void setZero();
void setOnes();
void setConstant(X D);
template<typename Iterator> void setCopy (Iterator first);
template<typename Iterator> void setCopy (Iterator first, Iterator last);
template<typename Iterator> void setCopyDense(Iterator first);
template<typename Iterator> void setCopyDense(Iterator first, Iterator last);
// copy to target
template<typename Iterator> void copyTo (Iterator first) const;
template<typename Iterator> void copyTo (Iterator first, Iterator last) const;
template<typename Iterator> void copyToDense(Iterator first) const;
template<typename Iterator> void copyToDense(Iterator first, Iterator last) const;
// sign change
matrix<X> operator- () const;
matrix<X> operator+ () const;
// arithmetic operators
matrix<X>& operator*= (const cppmat::diagonal ::matrix<X> &B);
matrix<X>& operator+= (const cppmat::diagonal ::matrix<X> &B);
matrix<X>& operator-= (const cppmat::diagonal ::matrix<X> &B);
matrix<X>& operator*= (const X &B);
matrix<X>& operator/= (const X &B);
// extra arithmetic operators
matrix<X>& operator*= (const cppmat:: matrix<X> &B);
matrix<X>& operator/= (const cppmat:: matrix<X> &B);
matrix<X>& operator*= (const cppmat::symmetric::matrix<X> &B);
matrix<X>& operator/= (const cppmat::symmetric::matrix<X> &B);
// absolute value
matrix<X> abs() const;
// norm (sum of absolute values)
X norm() const;
// return the indices that would sort the matrix
matrix<size_t> argsort(bool ascending=true) const;
// location of the minimum/maximum: plain storage (use decompress to convert to indices)
size_t argmin() const;
size_t argmax() const;
// minimum
X min() const;
// maximum
X max() const;
// sum
X sum() const;
// mean
double mean() const;
// weighted average
double average(const matrix<X> &weights, bool norm=true) const;
// return matrix of booleans, based on condition
matrix<int> equal (const X &D) const;
matrix<int> not_equal (const X &D) const;
matrix<int> greater (const X &D) const;
matrix<int> greater_equal(const X &D) const;
matrix<int> less (const X &D) const;
matrix<int> less_equal (const X &D) const;
matrix<int> equal (const matrix<X> &D) const;
matrix<int> not_equal (const matrix<X> &D) const;
matrix<int> greater (const matrix<X> &D) const;
matrix<int> greater_equal(const matrix<X> &D) const;
matrix<int> less (const matrix<X> &D) const;
matrix<int> less_equal (const matrix<X> &D) const;
// find the plain storage indices of all non-zero entries
std::vector<size_t> where() const;
};
// equality operators
template<class X> bool operator!= (const matrix<X> &A, const matrix<X> &B);
template<class X> bool operator== (const matrix<X> &A, const matrix<X> &B);
// external arithmetic operators (cppmat::diagonal)
template<class X> matrix<X> operator* (const matrix<X> &A, const matrix<X> &B);
template<class X> matrix<X> operator+ (const matrix<X> &A, const matrix<X> &B);
template<class X> matrix<X> operator- (const matrix<X> &A, const matrix<X> &B);
template<class X> matrix<X> operator* (const matrix<X> &A, const X &B);
template<class X> matrix<X> operator/ (const matrix<X> &A, const X &B);
template<class X> matrix<X> operator* (const X &A, const matrix<X> &B);
// print operator
template<class X> std::ostream& operator<<(std::ostream& out, const matrix<X>& src);
// =================================================================================================
}} // namespace ...
// -------------------------------------------------------------------------------------------------
#endif