-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_exp.hpp
277 lines (238 loc) · 6.26 KB
/
matrix_exp.hpp
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
// José Joaquín Zubieta Rico
//
// This small library supports matrix multiplication and
#ifndef JZR_MATRIX_H
#define JZR_MATRIX_H 1
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
namespace matrixlib {
template <typename E>
class _MatrixExpr {
public:
typedef E value_type;
std::size_t size() const
{
return static_cast<E const&>(*this).size();
}
std::size_t rows() const
{
return static_cast<E const&>(*this).rows();
}
std::size_t columns() const
{
return static_cast<E const&>(*this).columns();
}
auto operator()(std::size_t row, std::size_t column) const
{
return static_cast<E const&>(*this)(row, column);
}
friend std::ostream& operator<<(std::ostream& out, _MatrixExpr<E> const& expr)
{
for (std::size_t i = 0; i < expr.rows(); ++i) {
for (std::size_t j = 0; j < expr.columns(); ++j) {
out << expr(i, j);
if (j < expr.columns() - 1) {
out << " ";
}
}
if (i < expr.rows() - 1) {
out << std::endl;
}
}
return out;
}
};
template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
class Matrix : public _MatrixExpr<Matrix<T>> {
private:
std::size_t _rows;
std::size_t _columns;
std::vector<T> _data;
public:
typedef T value_type;
Matrix(std::size_t rows, std::size_t columns, T const& value = T())
: _rows(rows)
, _columns(columns)
, _data(rows * columns, value)
{
}
Matrix()
: Matrix(0, 0)
{
}
template <typename U>
Matrix(_MatrixExpr<U> const& expr)
: _rows(expr.rows())
, _columns(expr.columns())
, _data(expr.size())
{
for (std::size_t i = 0; i < expr.rows(); ++i) {
for (std::size_t j = 0; j < expr.columns(); ++j) {
_data[i * _columns + j] = expr(i, j);
}
}
}
template <typename U>
Matrix(_MatrixExpr<U> const& expr, T const& value)
: _rows(expr.rows())
, _columns(expr.columns())
, _data(expr.size())
{
for (std::size_t i = 0; i < expr.rows(); ++i) {
for (std::size_t j = 0; j < expr.columns(); ++j) {
_data[i * _columns + j] = value;
}
}
}
std::size_t rows() const
{
return _rows;
}
std::size_t columns() const
{
return _columns;
}
std::size_t size() const
{
return _data.size();
}
T& operator()(std::size_t row, std::size_t column)
{
return _data[row * _columns + column];
}
T operator()(std::size_t row, std::size_t column) const
{
return _data[row * _columns + column];
}
T& at(std::size_t row, std::size_t column)
{
if (row >= _rows || column >= _columns) {
throw std::out_of_range("Element index out of range");
}
return (*this)(row, column);
}
T at(std::size_t row, std::size_t column) const
{
return this->at(row, column);
}
template <typename E>
Matrix<T> operator*(_MatrixExpr<E> const& expr) const
{
if (_columns != expr.rows()) {
throw std::logic_error("Matrix multiplication size mismatch");
}
Matrix<T> result(_rows, expr.columns());
for (std::size_t i = 0; i < _rows; ++i) {
for (std::size_t j = 0; j < expr.columns(); ++j) {
for (std::size_t k = 0; k < _columns; ++k) {
result(i, j) += _data[i * _columns + k] * expr(k, j);
}
}
}
return result;
}
template <typename E>
Matrix<T> &operator+=(_MatrixExpr<E> const& expr)
{
if (this->size() != expr.size()) {
throw std::logic_error("Matrix sum with different sizes");
}
for (std::size_t i = 0; i < _rows; ++i) {
for (std::size_t j = 0; j < _columns; ++j) {
_data[i * _columns + j] += expr(i, j);
}
}
return *this;
}
template <typename E>
Matrix<T> &operator-=(_MatrixExpr<E> const& expr)
{
if (this->size() != expr.size()) {
throw std::logic_error("Matrix sum with different sizes");
}
for (std::size_t i = 0; i < _rows; ++i) {
for (std::size_t j = 0; j < _columns; ++j) {
_data[i * _columns + j] -= expr(i, j);
}
}
return *this;
}
};
template <typename T, typename U>
class _MatrixDiff : public _MatrixExpr<_MatrixDiff<T, U>> {
private:
T const& _first;
U const& _second;
public:
_MatrixDiff(T const& lhs, U const& rhs)
: _first(lhs)
, _second(rhs)
{
if (_first.size() != _second.size()) {
throw std::logic_error("Matrix sum with different sizes");
}
}
std::size_t size() const
{
return _first.size();
}
std::size_t rows() const
{
return _first.rows();
}
std::size_t columns() const
{
return _first.rows();
}
auto operator()(std::size_t row, std::size_t column) const
{
return _first(row, column) - _second(row, column);
}
};
template <typename T, typename U>
_MatrixDiff<T, U>
operator-(T const& rhs, U const& lhs)
{
return _MatrixDiff<T, U>(rhs, lhs);
}
template <typename T, typename U>
class _MatrixSum : public _MatrixExpr<_MatrixSum<T, U>> {
private:
T const& _first;
U const& _second;
public:
_MatrixSum(T const& lhs, U const& rhs)
: _first(lhs)
, _second(rhs)
{
if (_first.size() != _second.size()) {
throw std::logic_error("Matrix sum with different sizes");
}
}
std::size_t size() const
{
return _first.size();
}
std::size_t rows() const
{
return _first.rows();
}
std::size_t columns() const
{
return _first.rows();
}
auto operator()(std::size_t row, std::size_t column) const
{
return _first(row, column) + _second(row, column);
}
};
template <typename T, typename U>
_MatrixSum<T, U>
operator+(T const& rhs, U const& lhs)
{
return _MatrixSum<T, U>(rhs, lhs);
}
}
#endif