-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.h
175 lines (133 loc) · 4.57 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
#pragma once
#include <vector>
#include <utility>
#include <iostream>
#include <functional>
template<class T>
class Matrix {
public:
#pragma region Constructors
Matrix() : rows_(0), cols_(0) {};
explicit Matrix(const std::vector<std::vector<T>>& other) : data_(other), rows_(other.size()) {
FitMatrix();
}
Matrix(const size_t rows, const size_t cols) : rows_(rows), cols_(cols) {
data_.assign(rows_, std::vector<T>(cols_));
}
Matrix(std::initializer_list<std::initializer_list<T>> list) : rows_(list.size()) {
size_t row_ind = 0;
data_.resize(rows_);
for (auto row_it = list.begin(); row_it != list.end(); ++row_it) {
data_[row_ind++] = std::vector<T>(*row_it);
}
FitMatrix();
}
Matrix(const Matrix<T>& other) : data_(other.data_), rows_(other.rows_), cols_(other.cols_) {}
template<class U>
Matrix(const Matrix<U>& other) : rows_(other.rows()), cols_(other.columns()) {
data_.resize(rows_);
for(size_t i = 0; i < rows_; ++i) {
data_[i].resize(cols_);
for(size_t j = 0; j < cols_; ++j) {
data_[i][j] = static_cast<T>(other(i,j));
}
}
}
Matrix(Matrix<T>&& other) noexcept : data_(std::exchange(other.data_, std::vector<std::vector<T>>())),
rows_(std::exchange(other.rows_, 0)),
cols_(std::exchange(other.cols_, 0)) {}
virtual ~Matrix() = default;
#pragma endregion
#pragma region Getters
[[nodiscard]] size_t rows() const noexcept {
return rows_;
}
[[nodiscard]] size_t columns() const noexcept {
return cols_;
}
T& operator()(size_t row, size_t col) {
if (row >= rows_ || col >= cols_) {
throw std::out_of_range("Index out of range");
}
return data_[row][col];
}
const T& operator()(size_t row, size_t col) const {
if (row >= rows_ || col >= cols_) {
throw std::out_of_range("Index out of range");
}
return data_[row][col];
}
#pragma endregion
#pragma region Operators
bool operator==(const Matrix<T>& other) const noexcept;
bool operator!=(const Matrix<T>& other) const noexcept;
Matrix<T> operator-() const {
Matrix<T> mat = *this;
return mat.ForEach([](size_t i, size_t j, T& elem) {
elem = -elem;
});
}
Matrix<T> operator+() const noexcept {
return *this;
}
template<class U>
Matrix<T> operator*(U scalar) const noexcept {
Matrix<T> mat = *this;
return mat.ForEach([&](size_t i, size_t j, T& elem) {
elem = elem * scalar;
});
}
Matrix<T> operator/(T scalar) const {
Matrix<T> mat = *this;
return mat.ForEach([&](size_t i, size_t j, T& elem) {
elem = elem / scalar;
});
}
Matrix<T> operator+(const Matrix& other) const;
Matrix<T> operator-(const Matrix& other) const {
return *this + -1 * other;
}
Matrix<T> operator*(const Matrix& other) const;
Matrix<T>& operator=(const Matrix<T>& other) noexcept {
return *this = Matrix<T>(other);
}
Matrix<T>& operator=(Matrix<T>&& other) noexcept {
swap(*this, other);
return *this;
}
Matrix<T>& operator+=(const Matrix<T>& other) {
return *this = *this + other;
}
Matrix<T>& operator-=(const Matrix<T>& other) {
return *this = *this - other;
}
Matrix<T>& operator*=(const Matrix<T>& other) {
return *this = *this * other;
}
Matrix<T>& operator*=(const T scalar) {
return *this = *this * scalar;
}
Matrix<T>& operator/=(const T scalar) {
return *this = *this / scalar;
}
#pragma endregion
Matrix<T>& ForEach(std::function<void(size_t, size_t, T&)> func);
Matrix<T>& ForRow(size_t row, std::function<void(size_t, T&)> func);
Matrix<T>& ForColumn(size_t col, std::function<void(size_t, T&)> func);
T Trace() const noexcept;
Matrix<T> Transposed() const noexcept;
#pragma region Friends
template<class U, class V>
friend Matrix<U> operator*(V scalar, const Matrix<U>& mat);
template<class U>
friend void swap(Matrix<U>& lhs, Matrix<U>& rhs);
template<class U>
friend std::ostream& operator<<(std::ostream& out, const Matrix<U>& matrix);
template<class V, class U>
friend Matrix<V> pow(const Matrix<V>& matrix, const U& power);
#pragma endregion
protected:
std::vector<std::vector<T>> data_;
size_t rows_, cols_{};
void FitMatrix();
};