-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix.h
More file actions
368 lines (300 loc) · 11.2 KB
/
matrix.h
File metadata and controls
368 lines (300 loc) · 11.2 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// matrix.h
// SubmatrixQueries
//
// Created by Raphael Bost on 02/01/13.
// Copyright (c) 2013 Raphael Bost. All rights reserved.
//
#ifndef __SubmatrixQueries__matrix__
#define __SubmatrixQueries__matrix__
#include <valarray>
#include <iostream>
#include "debug_assert.h"
namespace matrix {
/*
* class Matrix
*
* The matrix virtual class provides the blueprint for the matrices operation we need.
* It also implements monotone and Monge property checkers.
*
*/
template <typename T>
class Matrix {
public:
virtual ~Matrix() {}
virtual size_t rows() const = 0;
virtual size_t cols() const = 0;
virtual T& operator()(size_t i, size_t j) = 0;
virtual T operator()(size_t i, size_t j) const = 0;
bool isMonotone() const;
bool isMonge() const;
bool isInverseMonge() const;
void print() const;
};
// Monotone & Monge propery
template <typename T> bool Matrix<T>::isMonotone() const
{
for (size_t i = 0; i < rows(); i++) {
for (size_t k = 0; k < cols(); k++) {
for (size_t j = i+1; j < rows(); j++) {
for (size_t l = k+1; l < cols(); l++) {
if (operator()(i,k) > operator()(i,l) || operator()(j,k) <= operator()(j,l)) {
return false;
}
}
}
}
}
return true;
}
template <typename T> bool Matrix<T>::isMonge() const
{
for (size_t i = 0; i < rows()-1; i++) {
for (size_t j = 0; j < cols()-1; j++) {
// equivalent to the regular definition but uses substractions to avoid overflows
if (operator()(i,j) - operator()(i+1,j) > operator()(i,j+1) - operator()(i+1,j+1)) {
return false;
}
}
}
return true;
}
template <typename T> bool Matrix<T>::isInverseMonge() const
{
for (size_t i = 0; i < rows()-1; i++) {
for (size_t j = 0; j < cols()-1; j++) {
// equivalent to the regular definition but uses substractions to avoid overflows
if (operator()(i,j) - operator()(i+1,j) < operator()(i,j+1) - operator()(i+1,j+1) ) {
return false;
}
}
}
return true;
}
template <typename T> void Matrix<T>::print() const
{
for (size_t i = 0; i < rows(); i++) {
for (size_t j = 0; j < cols(); j++) {
std::cout << (*this)(i,j) << " ; ";
}
std::cout<<"\n";
}
}
/* class SimpleMatrix
*
* This is a very simple matrix implemenation. Its purpose is to compare its performance
* with the ComplexMatrix class based on valarray.
*/
/*
* Apparently, for the creation of a big matrix, this version is faster but
* it uses a little bit more memory than the ComplexMatrix. Furthermore, for the
* naive algorithm, the access performances seem to be worse than the ComplexMatrix.
*/
template <typename T>
class SimpleMatrix : public Matrix<T>
{
private:
size_t _rows, _cols;
T **_data;
void initializeData()
{
_data = new T*[this->rows()];
for (size_t i = 0; i < this->rows(); i++) {
_data[i] = new T[this->cols()];
}
}
public:
SimpleMatrix(size_t rows, size_t cols) : _rows(rows), _cols(cols)
{
initializeData();
}
SimpleMatrix(size_t rows, size_t cols, T **data)
{
initializeData();
for (size_t i = 0; i < this->rows(); i++) {
for (size_t j = 0; j < this->rows(); j++) {
_data[i][j] = data[i][j];
}
}
}
SimpleMatrix(Matrix<T> *m) : _rows(m->rows()), _cols(m->cols())
{
initializeData();
for (size_t i = 0; i < this->rows(); i++) {
for (size_t j = 0; j < this->rows(); j++) {
_data[i][j] = (*m)(i,j);
}
}
}
~SimpleMatrix()
{
for (size_t i = 0; i < this->rows(); i++) {
delete [] _data[i];
}
delete [] _data;
}
size_t rows() const { return _rows; }
size_t cols() const { return _cols; }
T& operator()(size_t i, size_t j)
{
return _data[i][j];
}
T operator()(size_t i, size_t j) const
{
return _data[i][j];
}
};
/* class ComplexMatrix
* This is a basic implementation of matrices based on the STL's valarray.
* In the implementation of the SubmatrixQueries article, we are supposed to have a matrix implementation that
* can answer data queries on one entry in O(1) time. This is the case here.
*
*/
template <typename T>
class ComplexMatrix : public Matrix<T> {
public:
ComplexMatrix(size_t rows, size_t cols);
ComplexMatrix(size_t rows, size_t cols, std::valarray<T> data);
ComplexMatrix(Matrix<T> *m);
ComplexMatrix(ComplexMatrix<T> *m);
size_t rows() const;
size_t cols() const;
std::valarray<T> row(size_t r) const;
std::slice_array<T> row(size_t r);
std::valarray<T> row(size_t r, size_t start, size_t end) const;
std::slice_array<T> row(size_t r, size_t start, size_t end);
std::valarray<T> col(size_t c) const;
std::slice_array<T> col(size_t c);
std::valarray<T> col(size_t c, size_t start, size_t end) const;
std::slice_array<T> col(size_t c, size_t start, size_t end);
std::valarray<T> submatrix(size_t minRow, size_t maxRow, size_t minCol, size_t maxCol) const;
std::gslice_array<T> submatrix(size_t minRow, size_t maxRow, size_t minCol, size_t maxCol);
T& operator()(size_t i, size_t j);
T operator()(size_t i, size_t j) const;
ComplexMatrix<T> transpose() const;
private:
size_t _rows;
size_t _cols;
std::valarray<T> _data;
};
template <typename T> ComplexMatrix<T>::ComplexMatrix(size_t rows, size_t cols) : _rows(rows),
_cols(cols),
_data(rows * cols)
{
DEBUG_ASSERT(cols > 0);
}
template <typename T> ComplexMatrix<T>::ComplexMatrix(size_t rows, size_t cols, std::valarray<T> data) : _rows(rows),
_cols(cols),
_data(data)
{
DEBUG_ASSERT(rows > 0);
DEBUG_ASSERT(cols > 0);
}
template <typename T> ComplexMatrix<T>::ComplexMatrix(ComplexMatrix<T> *m) : _rows(m->rows()), _cols(m->cols()), _data(m->_data)
{
}
template <typename T> ComplexMatrix<T>::ComplexMatrix(Matrix<T> *m) : _rows(m->rows()), _cols(m->cols()), _data(m->rows() * m->cols())
{
for (size_t i = 0; i < this->rows(); i++) {
for (size_t j = 0; j < this->cols(); j++) {
(*this)(i,j) = (*m)(i,j);
}
}
}
// Accessors
template <typename T> size_t ComplexMatrix<T>::rows() const{
return _rows;
}
template <typename T> size_t ComplexMatrix<T>::cols() const{
return _cols;
}
template<typename T>
std::valarray<T> ComplexMatrix<T>::row(size_t r) const {
DEBUG_ASSERT(r >= 0 && r < rows());
return _data[std::slice(r * cols(), cols(), 1)];
}
template<typename T>
std::slice_array<T> ComplexMatrix<T>::row(size_t r) {
DEBUG_ASSERT(r >= 0 && r < rows());
return _data[std::slice(r * cols(), cols(), 1)];
}
template<typename T>
std::valarray<T> ComplexMatrix<T>::row(size_t r, size_t start, size_t end) const{
DEBUG_ASSERT(r >= 0 && r < rows());
return _data[std::slice(r*cols()+start,end-start+1,1)];
}
template<typename T>
std::slice_array<T> ComplexMatrix<T>::row(size_t r, size_t start, size_t end){
DEBUG_ASSERT(r >= 0 && r < rows());
return _data[std::slice(r*cols()+start,end-start+1,1)];
}
template<typename T>
std::valarray<T> ComplexMatrix<T>::col(size_t c) const {
DEBUG_ASSERT(c >= 0 && c < cols());
return _data[std::slice(c, rows(), cols())];
}
template<typename T>
std::slice_array<T> ComplexMatrix<T>::col(size_t c) {
DEBUG_ASSERT(c >= 0 && c < cols());
return _data[std::slice(c, rows(), cols())];
}
template<typename T>
std::valarray<T> ComplexMatrix<T>::col(size_t c, size_t start, size_t end) const{
DEBUG_ASSERT(c >= 0 && c < cols());
return _data[std::slice(c+start*cols(),end-start+1,cols())];
}
template<typename T>
std::slice_array<T> ComplexMatrix<T>::col(size_t c, size_t start, size_t end){
DEBUG_ASSERT(c >= 0 && c < cols());
return _data[std::slice(c+start*cols(),end-start+1,cols())];
}
template<typename T>
std::valarray<T> ComplexMatrix<T>::submatrix(size_t minRow, size_t maxRow, size_t minCol, size_t maxCol) const{
DEBUG_ASSERT(minRow <= maxRow);
DEBUG_ASSERT(minRow >= 0);
DEBUG_ASSERT(maxRow < rows());
DEBUG_ASSERT(minCol <= maxCol);
DEBUG_ASSERT(minCol >= 0);
DEBUG_ASSERT(maxCol < cols());
size_t start = minRow*cols() + minCol;
size_t lengths[]= {maxCol-minCol+1,maxRow-minRow+1};
size_t strides[]= {1,cols()};
return _data[std::gslice(start,std::valarray<size_t>(lengths,2),std::valarray<size_t>(strides,2))];
}
template<typename T>
std::gslice_array<T> ComplexMatrix<T>::submatrix(size_t minRow, size_t maxRow, size_t minCol, size_t maxCol){
DEBUG_ASSERT(minRow <= maxRow);
DEBUG_ASSERT(minRow >= 0);
DEBUG_ASSERT(maxRow < rows());
DEBUG_ASSERT(minCol <= maxCol);
DEBUG_ASSERT(minCol >= 0);
DEBUG_ASSERT(maxCol < cols());
size_t start = minRow*cols() + minCol;
size_t lengths[]= {maxCol-minCol+1,maxRow-minRow+1};
size_t strides[]= {1,cols()};
return _data[std::gslice(start,std::valarray<size_t>(lengths,2),std::valarray<size_t>(strides,2))];
}
// Operators
template <typename T> T& ComplexMatrix<T>::operator()(size_t i, size_t j)
{
DEBUG_ASSERT(i >= 0 && j >= 0);
DEBUG_ASSERT(i<rows() && j < cols());
return _data[i * _cols + j];
}
template <typename T> T ComplexMatrix<T>::operator()(size_t i, size_t j) const
{
DEBUG_ASSERT(i >= 0 && j >= 0);
DEBUG_ASSERT(i<rows() && j < cols());
return _data[i * _cols + j];
}
template <typename T> ComplexMatrix<T> ComplexMatrix<T>::transpose() const
{
ComplexMatrix<T> m = ComplexMatrix<T>(cols(), rows());
for (size_t i = 0; i < rows(); i++) {
std::valarray<T> r = row(i);
m.col(i) = r;
}
return m;
}
}
#endif /* defined(__SubmatrixQueries__matrix__) */