-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_sub.hpp
174 lines (153 loc) · 4.43 KB
/
matrix_sub.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
// José Joaquín Zubieta Rico
//
// This small library supports matrix multiplication and
#ifndef JZR_MATRIXSUB_H
#define JZR_MATRIXSUB_H 1
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
namespace matrixlibsub {
template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
class Matrix {
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)
{
}
Matrix(Matrix<T> 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);
}
Matrix<T> operator*(Matrix<T> 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;
}
Matrix<T> operator+(Matrix<T> const& rhs)
{
if (this->size() != rhs.size()) {
throw std::logic_error("Matrix sum with different sizes");
}
Matrix<T> result(_rows, rhs.columns());
for (std::size_t i = 0; i < _rows; ++i) {
for (std::size_t j = 0; j < _columns; ++j) {
result(i, j) = _data[i * _columns + j] + rhs(i, j);
}
}
return result;
}
Matrix<T> operator-(Matrix<T> const& rhs)
{
if (this->size() != rhs.size()) {
throw std::logic_error("Matrix sum with different sizes");
}
Matrix<T> result(_rows, rhs.columns());
for (std::size_t i = 0; i < _rows; ++i) {
for (std::size_t j = 0; j < _columns; ++j) {
result(i, j) = _data[i * _columns + j] - rhs(i, j);
}
}
return result;
}
Matrix<T> &operator+=(Matrix<T> const& rhs)
{
if (this->size() != rhs.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] += rhs(i, j);
}
}
return *this;
}
Matrix<T> &operator-=(Matrix<T> const& rhs)
{
if (this->size() != rhs.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] -= rhs(i, j);
}
}
return *this;
}
friend std::ostream& operator<<(std::ostream& out, Matrix<T> const& rhs)
{
for (std::size_t i = 0; i < rhs.rows(); ++i) {
for (std::size_t j = 0; j < rhs.columns(); ++j) {
out << rhs(i, j);
if (j < rhs.columns() - 1) {
out << " ";
}
}
if (i < rhs.rows() - 1) {
out << std::endl;
}
}
return out;
}
};
}
#endif