-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.h
196 lines (156 loc) · 3.87 KB
/
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
#ifndef VALIANT_PARSING_MATRIX_H
#define VALIANT_PARSING_MATRIX_H
#include <algorithm>
#include <iostream>
#include <functional>
#include <stdexcept>
#include <valarray>
using std::endl;
using std::istream;
using std::function;
using std::ostream;
using std::runtime_error;
using std::valarray;
template <typename T> struct matrix_view;
template <typename T>
struct matrix
{
int rows() const
{
return cells.size();
}
int cols() const
{
if (rows())
return cells[0].size();
else
return 0;
}
void init(int r, int s)
{
cells.resize(r, valarray<T>(s));
}
matrix() {}
matrix(int r, int s) { init(r, s); }
valarray<T>& operator[](int i)
{
return cells[i];
}
const valarray<T>& operator[](int i) const
{
return cells[i];
}
matrix without_range(int dr_beg, int dr_end, int dc_beg, int dc_end)
{
int newrows = rows() - (dr_end - dr_beg);
int newcols = cols() - (dc_end - dc_beg);
matrix new_cells;
new_cells.cells.resize(newrows);
for (int i = 0; i < newrows; ++i)
new_cells[i] = valarray<T>(newcols);
int citr = 0;
for (int i = 0; i < dc_beg; ++i)
{
for (int j = 0; j < newrows; ++j)
new_cells[j][citr] = cells[j + (j < dr_beg ? 0 : (dr_end - dr_beg))][i];
++ citr;
}
for (int i = dc_end; i < cols(); ++i)
{
for (int j = 0; j < newrows; ++j)
new_cells[j][citr] = cells[j + (j < dr_beg ? 0 : (dr_end - dr_beg))][i];
++ citr;
}
return new_cells;
}
valarray<valarray<T>> cells;
};
template <typename T>
class matrix_view
{
matrix<T> &m;
int r_beg, r_end, c_beg, c_end;
public:
matrix_view(matrix<T>& m, int r_beg, int r_end, int c_beg, int c_end)
: r_beg(r_beg), r_end(r_end), c_beg(c_beg), c_end(c_end), m(m)
{
}
matrix_view(matrix_view& m, int r_beg, int r_end, int c_beg, int c_end)
: r_beg(r_beg + m.r_beg), r_end(r_end + m.r_beg), c_beg(c_beg + m.c_beg), c_end(c_end + m.c_beg), m(m.m)
{
}
matrix_view(matrix<T>& m)
: r_beg(0), r_end(m.rows()), c_beg(0), c_end(m.cols()), m(m)
{
}
const T& operator()(int r, int s) const
{
return m.cells[r_beg + r][c_beg + s];
}
T& operator()(int r, int s)
{
return m.cells[r_beg + r][c_beg + s];
}
int rows() const
{
return r_end - r_beg;
}
int cols() const
{
return c_end - c_beg;
}
static matrix<T> slow_mul(const matrix_view<T>& a, const matrix_view<T>& b)
{
matrix<T> c(a.rows(), b.cols());
for (int i = 0; i < a.rows(); ++i)
for (int j = 0; j < b.cols(); ++j)
for (int k = 0; k < a.cols(); ++k)
c[i][j] += a(i, k) * b(k, j);
return c;
}
matrix<T> operator* (const matrix_view<T>& o)
{
return mul_op(*this, o);
}
matrix_view operator += (const matrix_view<T>& other)
{
for (int i = 0; i < other.rows(); ++i)
for (int j = 0; j < other.cols(); ++j)
{
(*this)(i, j) += other(i, j);
}
return *this;
}
function<matrix<T>(const matrix_view&, const matrix_view&)> mul_op = slow_mul;
void emplace(matrix_view<T> other, int at_r, int at_c)
{
for (int i = 0; i < other.rows(); ++i)
for (int j = 0; j < other.cols(); ++j)
(*this)(at_r + i, at_c + j) = other(i, j);
}
matrix<T> to_matrix()
{
matrix<T> r(rows(), cols());
for (int i = 0; i < rows(); ++i)
for (int j = 0; j < cols(); ++j)
r[i][j] = (*this)(i, j);
return r;
}
};
template <class T>
ostream& operator<< (ostream& ostr, const matrix_view<T> &mat)
{
for (int i = 0; i < mat.rows(); ++i, ostr << endl)
for (int j = 0; j < mat.cols(); ++j)
ostr << mat(i,j) << "\t\t\t";
return ostr;
}
template <class T>
ostream& operator<< (ostream& ostr, const matrix<T> &_mat)
{
matrix<T> __mat = _mat; // mat_view doesn't guarantee constness
matrix_view<T> mat(__mat);
ostr << mat;
return ostr;
}
#endif //VALIANT_PARSING_MATRIX_H