Repository navigation
Expand file tree
/
Copy pathMatrix.cpp
More file actions
346 lines (289 loc) · 8.68 KB
/
Copy pathMatrix.cpp
File metadata and controls
346 lines (289 loc) · 8.68 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// Created by gosha on 10/26/2022.
//
#include "headers/Matrix.h"
#include <iostream>
#include <cstddef>
#include <cassert>
#include <utility> //std::swap
#include <sstream>
#include <regex>
class RangeException : public std::exception {
protected:
std::string error_info;
public:
RangeException(std::string in) : exception() {
error_info = in;
}
void print() const {
std::cout << this->error_info << "\n";
}
};
Matrix::Matrix() //Конструктор по умолчанию
: rows(0), columns(0), size(0), number(0), data(nullptr) {
}
Matrix::Matrix(std::size_t r, std::size_t c) //Конструктор инициализации
: rows(r), columns(c), size(r * c) {
if (0 == columns && rows != 0) {
columns = rows;
rows = 1;
size = columns;
}
data = new float *[rows];
for (std::size_t i = 0; i < rows; ++i) {
data[i] = new float[columns];
}
}
Matrix::Matrix(const Matrix &M) //Конструктор копирования
: rows(M.rows), columns(M.columns), size(M.size), number(M.number), data(new float *[M.rows]) {
for (std::size_t i = 0; i < rows; ++i) {
data[i] = new float[columns];
for (std::size_t j = 0; j < columns; ++j) {
data[i][j] = M.data[i][j];
}
}
}
Matrix &Matrix::operator+=(const Matrix &M) {
if (size == M.size) {
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t j = 0; j < columns; ++j) {
data[i][j] += M.data[i][j];
}
}
}
return *this;
}
Matrix &Matrix::operator-=(const Matrix &M) {
if (size == M.size) {
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t j = 0; j < columns; ++j) {
data[i][j] -= M.data[i][j];
}
}
}
return *this;
}
Matrix &Matrix::operator*=(const Matrix &M) {
if (this->columns == M.rows) {
Matrix tmp(rows, M.columns);
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t k = 0; k < M.columns; ++k) {
for (std::size_t j = 0; j < columns; ++j) {
tmp(i, k) += data[i][j] * M.data[j][k];
}
}
}
for (std::size_t i = 0; i < rows; ++i) {
delete[] data[i];
}
delete[] data;
rows = tmp.rows;
columns = tmp.columns;
size = tmp.size;
data = new float *[rows];
for (std::size_t i = 0; i < rows; ++i) {
data[i] = new float[columns];
for (std::size_t j = 0; j < columns; ++j) {
data[i][j] = tmp.data[i][j];
}
}
return *this;
} else {
throw RangeException(std::string("columns < rows"));
}
}
Matrix &Matrix::operator*=(const size_t &M) {
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t j = 0; j < columns; ++j) {
data[i][j] *= M;
}
}
return *this;
}
Matrix &Matrix::operator*=(const float &M) {
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t j = 0; j < columns; ++j) {
data[i][j] = data[i][j] * M;
}
}
return *this;
}
Matrix &Matrix::operator=(const Matrix &M) {
if (&M == this) return *this;
for (std::size_t i = 0; i < rows; ++i)
delete[] data[i];
delete[] data;
rows = M.rows;
columns = M.columns;
size = M.size;
data = new float *[rows];
for (std::size_t i = 0; i < rows; ++i) {
data[i] = new float[columns];
for (std::size_t j = 0; j < columns; ++j) {
data[i][j] = M.data[i][j];
}
}
return *this;
}
bool Matrix::operator==(const Matrix &M) {
bool marker = true;
if (size == M.size) {
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t j = 0; j < columns; ++j) {
if (data[i][j] != M.data[i][j]) {
marker = false;
}
}
}
} else return false;
return marker;
}
bool Matrix::operator!=(const Matrix &M) {
bool marker = false;
if (size == M.size) {
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t j = 0; j < columns; ++j) {
if (data[i][j] != M.data[i][j]) {
marker = true;
}
}
}
} else return true;
return marker;
}
Matrix::~Matrix() {
for (std::size_t i = 0; i < rows; ++i) {
delete[] data[i];
}
delete[] data;
}
void Matrix::Print() {
std::cout << "\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
std::cout << data[i][j] << "\t";
}
std::cout << "\n";
}
}
Matrix &Matrix::Transpose() {
Matrix Tmp(columns, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
Tmp.data[j][i] = data[i][j];
}
}
for (std::size_t i = 0; i < rows; ++i) {
delete[] data[i];
}
delete[] data;
rows = Tmp.rows;
columns = Tmp.columns;
size = Tmp.size;
data = new float *[rows];
for (std::size_t i = 0; i < rows; ++i) {
data[i] = new float[columns];
for (std::size_t j = 0; j < columns; ++j) {
data[i][j] = Tmp.data[i][j];
}
}
return *this;
}
float Matrix::Determinant() {
float result = 0;
if (rows == columns) {
if (this->size > 4) {
for (int k = 0; k < columns; ++k) {
Matrix tmp(rows - 1, columns - 1);
int step = 0;
for (int j = 0; j < tmp.columns; ++j) {
if (step == k) step++;
for (int i = 0; i < tmp.rows; ++i) {
tmp(i, j) = data[i + 1][step];
}
step++;
}
if (k % 2 == 0) {
result += tmp.Determinant() * data[0][k];
} else {
result += tmp.Determinant() * (-1 * data[0][k]);
}
}
} else {
result = (data[0][0] * data[1][1]) - (data[0][1] * data[1][0]);
return result;
}
} else throw RangeException(std::string("Matrix is not cubic"));
return result;
}
void Matrix::Swap_Rows(std::size_t i, std::size_t j) {
assert(i >= 0 && i < rows && "The Row index is out of range!\n");
assert(j >= 0 && j < rows && "The row index is out of range!\n");
if (i == j) { return; }
std::swap(data[i], data[j]);
}
float &Matrix::operator()(std::size_t i, std::size_t j) {
return data[i][j];
}
const float &Matrix::operator()(std::size_t i, std::size_t j) const {
return data[i][j];
}
//vector represented as 1xn matrix, thus operator(with single parameter) returns elements from first row when we use it with mxn (m!=1) matrix
float &Matrix::operator()(std::size_t i) {
return data[0][i];
}
const float &Matrix::operator()(std::size_t i) const {
return data[0][i];
}
std::size_t Matrix::get_rows() const { return rows; }
std::size_t Matrix::get_columns() const { return columns; }
std::size_t Matrix::get_size() const { return size; }
std::size_t Matrix::get_number() const { return number; }
void Matrix::set_number(std::size_t n) { number = n; }
std::istream &operator>>(std::istream &is, Matrix &m) {
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.columns; j++) {
std::string input;
is >> input;
std::regex rational_regex("[[:digit:]]+\\/[[:digit:]]");
if (std::regex_match(input, rational_regex)) {
std::istringstream rational_is(input);
rational r;
rational_is >> r;
m.data[i][j] = r;
} else {
std::istringstream float_is(input);
float f;
float_is >> f;
m.data[i][j] = f;
}
}
}
return is;
}
std::ostream &operator<<(std::ostream &os, const Matrix &m) {
std::ostringstream output_text; // Declare an output stringstream
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.columns; j++) {
output_text << m.data[i][j] << ' ';
}
output_text << '\n';
}
os << output_text.str();
return os;
}
Result::Result() : Matrix(), state("") {
}
Result::Result(std::string s, std::size_t r, std::size_t c)
: Matrix(r, c), state(s) {
}
Result::Result(const Result &R)
: Matrix(R), state(R.state) {
}
Result &Result::operator=(const Result &R) {
Matrix::operator=(R);
state = R.state;
return *this;
}
void Result::set_state(std::string s) { state = s; }
std::string Result::get_state() const { return state; }