-
Notifications
You must be signed in to change notification settings - Fork 0
/
DenseMatrixContainer.h
335 lines (307 loc) · 10.5 KB
/
DenseMatrixContainer.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ifndef _FususDenseMatrixContainer_
#define _FususDenseMatrixContainer_
#include <numeric>
#include <deque>
#include <type_traits>
namespace FususMatrix{
template <typename T>
using MyContainerType = typename std::conditional<
std::is_same<T, bool>::value,
std::deque<T>,
std::vector<T>
>::type;
// Dense Matrix Container.
//////////////////////////////////////////////////
template<typename T = double, std::size_t Dimension = 2>
class DenseMatrixContainer{
private:
MyContainerType<T> MyData; // Data of the Matrix
bool Transposed; // Transposed or not.
std::size_t MyDimension; // Dimension.
std::vector<std::size_t> SizesAlongEachDimension; // Sizes along each dimension.
std::vector<std::size_t> Strides; // Used to locate the elements of the matrix inside the 1-D vector container.
public:
// Constructor from the sizes along each dimension.
template<typename... Sizes>
DenseMatrixContainer(std::size_t dimension, Sizes... sizes)
: Transposed(false), MyDimension(dimension), SizesAlongEachDimension(dimension), Strides(dimension, 1){
if (sizeof...(sizes) == 0){ // If no sizes entered set them to zero.
for (std::size_t i = 0; i < dimension; ++i){
SizesAlongEachDimension[i] = 0;
};
MyData = MyContainerType<T>(1);// Such that size zero are only the scalars.
}
else{
SizesAlongEachDimension = { static_cast<std::size_t>(sizes)... };
std::size_t temp{ 1 };
for (auto i : SizesAlongEachDimension){
temp *= i;
};
MyData = MyContainerType<T>(temp);
};
assert(MyData.size() > 0 || dimension == 0);
// Such that a matrix of dimension zero can be used as a Scalar.
// Not sure yet what is the best idea to treat the degenerate cases.
if (Dimension == 0){
MyData = MyContainerType<T>(1);
};
// Initializing the strides
for (std::size_t i = 1; i < SizesAlongEachDimension.size(); ++i){
for (std::size_t j = 0; j < i; ++j){
Strides[j] *= SizesAlongEachDimension[i];
};
};
};
// Move constructor.
DenseMatrixContainer(DenseMatrixContainer&& other){
MyData.swap(other.MyData);
Transposed = other.Transposed;
MyDimension = other.MyDimension;
SizesAlongEachDimension.swap(other.SizesAlongEachDimension);
Strides.swap(other.Strides);
};
// Copy constructor
DenseMatrixContainer(const DenseMatrixContainer& other)
: MyData(other.MyData), Transposed(other.Transposed), MyDimension(other.MyDimension),
SizesAlongEachDimension(other.SizesAlongEachDimension), Strides(other.Strides){
};
// Swap.
void swap(DenseMatrixContainer& other){
MyData.swap(other.MyData);
Transposed = other.Transposed;
MyDimension = other.MyDimension;
SizesAlongEachDimension.swap(other.SizesAlongEachDimension);
Strides.swap(other.Strides);
};
// Size is size of represented data.
std::size_t size() const {
return MyData.size();
};
//Getter for all SizesAlongEachDimension
const std::vector<std::size_t> getSizesAlongEachDimension() const {
return SizesAlongEachDimension;
};
// Getter for sizes along each dimension.
std::size_t SizeAlongDimension(std::size_t dim) const {
return SizesAlongEachDimension[dim];
};
// Number of rows.
std::size_t rows() const {
if (Dimension > 0){
return SizeAlongDimension(0);
}
else{
return 0;
};
};
// Number of columns.
std::size_t columns() const {
if (Dimension > 0){
return SizeAlongDimension(1);
}
else{
return 0;
};
};
// Dimension getter.
std::size_t dimension() const {
return MyDimension;
};
// Index operator for constants and variables.
// Accesses the elements according to the linear order in the 1-D vector container.
T operator[](std::size_t index) const {
assert(index < size());
return MyData[index];
};
T& operator[](std::size_t index) {
assert(index < size());
return MyData[index];
};
// Unitary operators.
// Additive inverse of each element.
DenseMatrixContainer& operator-(){
for (std::size_t i = 0; i < MyData.size(); ++i){
MyData[i] = -MyData[i];
};
return *this;
};
// Multiplicative inverse of each element.
DenseMatrixContainer& reciprocals(){
for (std::size_t i = 0; i < MyData.size(); ++i){
MyData[i] = 1 / MyData[i];
};
return *this;
};
// Compound assignment operators.
DenseMatrixContainer& operator+=(const DenseMatrixContainer& X){
for (std::size_t i = 0; i < MyData.size(); ++i){
MyData[i] += X.MyData[i];
};
return *this;
};
DenseMatrixContainer& operator-=(const DenseMatrixContainer& X){
for (std::size_t i = 0; i < MyData.size(); ++i){
MyData[i] -= X.MyData[i];
};
return *this;
};
DenseMatrixContainer& operator*=(const DenseMatrixContainer& X){
for (std::size_t i = 0; i < MyData.size(); ++i){
MyData[i] *= X.MyData[i];
};
return *this;
};
DenseMatrixContainer& operator*=(const T& s){
for (std::size_t i = 0; i < MyData.size(); ++i){
MyData[i] += s;
};
return *this;
};
DenseMatrixContainer& operator/=(const DenseMatrixContainer& X){
for (std::size_t i = 0; i < MyData.size(); ++i){
MyData[i] /= X.MyData[i];
};
return *this;
};
// Weak transpose.
void transpose(){
assert(Dimension == 2);
Transposed = !Transposed;
};
// Transposing a contiguous minor of a source matrix to the minor of target matrix.
// Will be used for cache-oblivious strong transposition.
void minortranspose(DenseMatrixContainer<T, 2>& source, DenseMatrixContainer<T, 2>& target, std::size_t firstrow, std::size_t lastrow, std::size_t firstcolumn, std::size_t lastcolumn){
for (std::size_t i = firstrow; i < lastrow + 1; ++i){
for (std::size_t j = firstcolumn; j < lastcolumn + 1; ++j){
target(j, i) = source(i, j);
};
};
};
// Recursion for Cache-Oblivious transposition
void transposeRecursion(DenseMatrixContainer<T, 2>& source, DenseMatrixContainer<T, 2>& target, std::size_t firstrow, std::size_t lastrow, std::size_t firstcolumn, std::size_t lastcolumn){
std::size_t width{ lastrow - firstrow + 1 };
std::size_t hight{ lastcolumn - firstcolumn + 1 };
// Small minors get transposed with naive transposition
if ((width <= 3) || (hight <= 3)){
minortranspose(source, target, firstrow, lastrow, firstcolumn, lastcolumn);
}
else{
// The matrix is divided along its largest dimension and the pieces get recursively transposed
if (width >= hight){
std::size_t halfwidth{ width / 2 };
transposeRecursion(source, target, firstrow, halfwidth, firstcolumn, lastcolumn);
transposeRecursion(source, target, halfwidth + 1, lastrow, firstcolumn, lastcolumn);
}
else{
std::size_t halfhight{ hight / 2 };
transposeRecursion(source, target, firstrow, lastrow, firstcolumn, halfhight);
transposeRecursion(source, target, firstrow, lastrow, halfhight + 1, lastcolumn);
};
};
};
// Cache-oblivious algorithm for transposition.
// The matrix gets divided in half along its larger dimension.
// Then the two submatrices get transposed.
// Strong transposition. It actually changes the position of the entries of the matrix.
void strongTranspose(){
assert(Dimension == 2);
DenseMatrixContainer<T, 2> temp(Dimension, SizesAlongEachDimension[0], SizesAlongEachDimension[1]);
transposeRecursion(*this, temp, 0, SizesAlongEachDimension[0] - 1, 0, SizesAlongEachDimension[1] - 1);
temp.Transposed = false;
swap(temp);
};
// Accessing elements
// This computes the position of the components of the matrix within the 1-D vector container.
template<typename... Coordinates>
inline std::size_t ComputePosition(Coordinates... coordinates) const {
std::vector<std::size_t> coord({ static_cast<std::size_t>(coordinates)... });
return std::inner_product(coord.begin(), coord.end(), Strides.begin(), 0);
};
// Constant access.
template<typename FirstCoordinate, typename... RemainingCoordinates>
const T& operator()(FirstCoordinate i, RemainingCoordinates... coordinates) const {
if (Transposed){
return MyData[ComputePosition(coordinates..., i)];
}
else{
return MyData[ComputePosition(i, coordinates...)];
};
};
// Non-constant access.
template<typename FirstCoordinate, typename... RemainingCoordinates>
T& operator()(FirstCoordinate i, RemainingCoordinates... coordinates){
if (Transposed){
return MyData[ComputePosition(coordinates..., i)];
}
else{
return MyData[ComputePosition(i, coordinates...)];
};
};
void Multiply(DenseMatrixContainer<T> const& A, DenseMatrixContainer<T> const& B){
T ComponentOfProduct{ 0 };
for (std::size_t i = 0; i < A.rows(); ++i){
for (std::size_t j = 0; j < B.columns(); ++j){
for (std::size_t k = 0; k < A.columns(); ++k){
ComponentOfProduct += A(i, k) * B(k, j);
};
(*this)(i, j) = ComponentOfProduct;
ComponentOfProduct = 0;
};
};
};
// Checking if 'this' is a lower triangular matrix (container).
bool IsLowerTriangular(){
assert(Dimension == 2);
for (std::size_t i = 0; i < SizesAlongEachDimension[0]; ++i){
for (std::size_t j = i + 1; j<SizesAlongEachDimension[1]; ++j){
if (this->operator()(i, j) != 0){
return false;
};
};
};
return true;
};
// Checking if 'this' is an upper triangular matrix (container).
bool IsUpperTriangular(){
assert(Dimension == 2);
for (std::size_t i = 1; i < SizesAlongEachDimension[0]; ++i){
for (std::size_t j = 0; j < i; ++j){
if (this->operator()(i, j) != 0){
return false;
};
};
};
return true;
};
DenseMatrixContainer span(const DenseMatrixContainer& b){
assert(b.size() == SizesAlongEachDimension[1]);
bool WeCanSolveIt{false};
DenseMatrixContainer y(b);
if (IsLowerTriangular()){
for (std::size_t i = 0; i < y.size(); ++i){
for (std::size_t j = 0; j < i; ++j)
{
y[i] -= (*this)(i, j) * y[j];
}
y[i] /= (*this)(i, i);
};
return y;
};
if (IsUpperTriangular()){
for (std::size_t i = y.size() - 1; i > -1 ; --i){
for (std::size_t j = y.size(); j > i; --j)
{
y[i] -= (*this)(i, j) * y[j];
}
y[i] /= (*this)(i, i);
};
return y;
};
assert(WeCanSolveIt);
};
//
};// END DenseMatrixContainer class
//#include "DenseMatrixContainerForBool.h"
//
}// END namespace
#endif