-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix.h
180 lines (149 loc) · 3.77 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
#ifndef MATX_H
#define MATX_H
#include <cmath>
#include <cstdlib>
#include <iterator>
#include <stdexcept>
#include <vector>
using std::vector;
template <class T>
class Matrix {
size_t _sizeN, _sizeM;
vector<vector<T>> _matrix;
const double ERROR = 1e-9;
inline bool Zero(double a) { return fabs(a) < ERROR; }
public:
Matrix(size_t sizeN, size_t sizeM) {
_sizeN = sizeN;
_sizeM = sizeM;
vector<T> sizeMVector(_sizeM, 0);
_matrix.resize(_sizeN);
for (size_t i = 0; i < _sizeN; i++) {
_matrix[i] = sizeMVector;
}
}
Matrix(size_t _sizeN = 1) : Matrix(_sizeN, _sizeN) {}
Matrix(vector<vector<T>>);
Matrix transpose();
size_t size();
size_t columnSize();
Matrix gaussianElimination();
Matrix gaussianElimination(vector<double>);
Matrix operator+(Matrix);
Matrix operator-(Matrix);
Matrix operator*(T);
Matrix operator*(Matrix);
vector<T> &operator[](size_t);
typename vector<vector<T>>::iterator begin();
typename vector<vector<T>>::iterator end();
const typename vector<vector<T>>::iterator begin() const;
const typename vector<vector<T>>::iterator end() const;
};
template <class T>
Matrix<T>::Matrix(vector<vector<T>> vec) {
_matrix = vec;
_sizeN = vec.size();
_sizeM = 0;
if (vec.size() > 0) {
size_t length = vec[0].size();
for (auto row : vec) {
if (length != row.size()) {
throw std::invalid_argument("Invalid matrix size");
}
}
_sizeM = length;
}
}
template <class T>
Matrix<T> Matrix<T>::transpose() {
Matrix resultMatrix(_sizeM, _sizeN);
for (size_t i = 0; i < _sizeN; i++) {
for (size_t j = 0; j < _sizeM; j++) {
resultMatrix[j][i] = _matrix[i][j];
}
}
return resultMatrix;
}
template <class T>
Matrix<T> Matrix<T>::operator+(Matrix<T> A) {
if (_sizeN != A._sizeN or _sizeM != A._sizeM) {
throw std::invalid_argument("Incompatible matrices sizes %d %d");
}
Matrix sum(_sizeN, _sizeM);
for (size_t i = 0; i < _sizeN; i++) {
for (size_t j = 0; j < _sizeM; j++) {
sum[i][j] = A[i][j] + _matrix[i][j];
}
}
return sum;
}
template <class T>
vector<T> &Matrix<T>::operator[](size_t i) {
if (i > _sizeN) {
throw std::invalid_argument("Index out of bounds");
}
return _matrix[i];
}
template <class T>
Matrix<T> Matrix<T>::operator-(Matrix<T> _matrix) {
Matrix err(_matrix._sizeN);
if (_sizeN == _matrix._sizeN && _sizeM == _matrix._sizeM) {
Matrix res(_matrix._sizeN, _matrix._sizeM);
for (size_t i = 0; i < _matrix._sizeN; i++) {
for (size_t j = 0; j < _matrix._sizeM; j++) {
res._matrix[i][j] = _matrix._matrix[i][j] - _matrix[i][j];
}
}
return res;
}
return err;
}
template <class T>
Matrix<T> Matrix<T>::operator*(T A) {
Matrix resultMatrix(_sizeN, _sizeM);
for (size_t i = 0; i < resultMatrix._sizeN; i++) {
for (size_t j = 0; j < resultMatrix._sizeM; j++) {
resultMatrix._matrix[i][j] = _matrix[i][j] * A;
}
}
return resultMatrix;
}
template <class T>
Matrix<T> Matrix<T>::operator*(Matrix<T> _matrix) {
Matrix C(_sizeN, _matrix._sizeM);
if (_sizeN == _matrix._sizeM && _sizeM == _matrix._sizeN) {
for (size_t i = 0; i < _sizeN; i++) {
for (size_t j = 0; j < _matrix._sizeM; j++) {
for (size_t k = 0; k < _sizeM; k++) {
C._matrix[i][j] += _matrix[i][k] * _matrix._matrix[k][j];
}
}
}
}
return C;
}
template <class T>
size_t Matrix<T>::size() {
return _sizeN;
}
template <class T>
size_t Matrix<T>::columnSize() {
return _sizeM;
}
template <class T>
typename vector<vector<T>>::iterator Matrix<T>::begin() {
return _matrix.begin();
}
template <class T>
typename vector<vector<T>>::iterator Matrix<T>::end() {
return _matrix.end();
}
template <class T>
const typename vector<vector<T>>::iterator Matrix<T>::begin() const {
return _matrix.begin();
}
template <class T>
const typename vector<vector<T>>::iterator Matrix<T>::end() const {
return _matrix.end();
}
#endif