-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths21_matrix_oop.cc
More file actions
345 lines (308 loc) · 8.93 KB
/
s21_matrix_oop.cc
File metadata and controls
345 lines (308 loc) · 8.93 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
#include "s21_matrix_oop.h"
namespace S21 {
// Constructors and Destructors
S21Matrix::S21Matrix() : rows_(0), cols_(0), matrix_(nullptr) {}
S21Matrix::S21Matrix(int rows, int cols) : rows_(rows), cols_(cols) {
AllocateMemoryForMatrix_();
}
S21Matrix::S21Matrix(const S21Matrix& other)
: rows_(other.rows_), cols_(other.cols_) {
AllocateMemoryForMatrix_();
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
matrix_[i][j] = other.matrix_[i][j];
}
}
}
S21Matrix::S21Matrix(S21Matrix&& other) noexcept
: rows_(other.rows_), cols_(other.cols_), matrix_(other.matrix_) {
other.rows_ = 0;
other.cols_ = 0;
other.matrix_ = nullptr;
}
S21Matrix::~S21Matrix() {
if (matrix_) {
for (int i = 0; i < rows_; ++i) {
delete[] matrix_[i];
}
delete[] matrix_;
}
}
// Maths and special methodes
bool S21Matrix::EqMatrix(const S21Matrix& other) const {
if (cols_ != other.cols_ || rows_ != other.rows_) {
return FAILURE;
};
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
if (std::abs(matrix_[i][j] - other.matrix_[i][j]) > EPS) {
return FAILURE;
}
}
}
return SUCCESS;
}
void S21Matrix::SumMatrix(const S21Matrix& other) {
if (rows_ != other.rows_ || cols_ != other.cols_) {
throw std::logic_error("Некорректное количество столбцов или строк!");
}
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
matrix_[i][j] += other.matrix_[i][j];
}
}
}
void S21Matrix::SubMatrix(const S21Matrix& other) {
if (rows_ != other.rows_ || cols_ != other.cols_) {
throw std::logic_error("Некорректное количество столбцов или строк!");
}
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
matrix_[i][j] -= other.matrix_[i][j];
}
}
}
void S21Matrix::MulNumber(double num) {
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
matrix_[i][j] *= num;
}
}
}
void S21Matrix::MulMatrix(const S21Matrix& other) {
if (cols_ != other.rows_) {
throw std::logic_error(
"Некорректное количество столбцов у матрицы A или строк у B, (AxB)!");
}
S21Matrix result(rows_, other.cols_);
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < other.cols_; ++j) {
for (int k = 0; k < cols_; k++) {
result.matrix_[i][j] += matrix_[i][k] * other.matrix_[k][j];
}
}
}
*this = std::move(result);
}
S21Matrix S21Matrix::Transpose() const {
S21Matrix result(cols_, rows_);
for (int i = 0; i < result.rows_; ++i) {
for (int j = 0; j < result.cols_; ++j) {
result.matrix_[i][j] = matrix_[j][i];
}
}
return result;
}
S21Matrix S21Matrix::CalcComplements() const {
if (cols_ != rows_) {
throw std::logic_error("Матрица не квадратная!");
}
S21Matrix result(rows_, cols_);
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
S21Matrix tmp = GetMinorOfMatrix_(i, j);
result.matrix_[i][j] = std::pow(-1, i + j) * tmp.Determinant();
}
}
return result;
}
double S21Matrix::Determinant() const {
if (cols_ != rows_) {
throw std::logic_error("Матрица не квадратная!");
}
double result = 0;
int size = rows_;
if (size == 1) {
result = matrix_[0][0];
} else if (size == 2) {
result = matrix_[0][0] * matrix_[1][1] - matrix_[1][0] * matrix_[0][1];
} else {
S21Matrix tmp = S21Matrix(size - 1, size - 1);
result = 0.0;
double sign = 1.0;
double det_tmp = 0.0;
for (int i = 0; i < size; i++) {
tmp = GetMinorOfMatrix_(0, i);
det_tmp = tmp.Determinant();
result += sign * matrix_[0][i] * det_tmp;
sign = -sign;
}
}
return result;
}
S21Matrix S21Matrix::InverseMatrix() const {
if (cols_ != rows_) {
throw std::logic_error("Матрица не квадратная!");
}
double det_result = this->Determinant();
if (std::abs(det_result) < EPS) {
throw std::logic_error("Матрица сингулярная!");
}
S21Matrix result = this->CalcComplements().Transpose() * (1.0 / det_result);
return result;
}
// Overloaded operators
S21Matrix S21Matrix::operator+(const S21Matrix& other) const {
S21Matrix result(*this); // компилятор создает этот объект вне этого
// метода return value opitimization
// S21Matrix result = *this;
// S21Matrix result = S21Matrix(*this);
result.SumMatrix(other);
return result;
}
S21Matrix S21Matrix::operator-(const S21Matrix& other) const {
S21Matrix result = *this;
result.SubMatrix(other);
return result;
}
S21Matrix S21Matrix::operator*(double num) const {
S21Matrix result = *this;
result.MulNumber(num);
return result;
}
S21Matrix S21Matrix::operator*(const S21Matrix& other) const {
S21Matrix result = *this;
result.MulMatrix(other);
return result;
}
bool S21Matrix::operator==(const S21Matrix& other) const {
bool result = this->EqMatrix(other);
return result;
}
void S21Matrix::Swap(S21Matrix& other) {
std::swap(matrix_, other.matrix_);
std::swap(rows_, other.rows_);
std::swap(cols_, other.cols_);
}
S21Matrix& S21Matrix::operator=(S21Matrix other) { // a = b = c
Swap(other);
return *this;
}
// S21Matrix& S21Matrix::operator=(S21Matrix&& other) {
// for (int i = 0; i < rows_; ++i) {
// delete[] matrix_[i];
// }
// delete[] matrix_;
// rows_ = other.rows_;
// cols_ = other.cols_;
// matrix_ = other.matrix_;
// other.rows_ = 0;
// other.cols_ = 0;
// other.matrix_ = nullptr;
// return *this;
// }
S21Matrix& S21Matrix::operator+=(const S21Matrix& other) { // (a = b) = c;
*this = *this + other;
return *this;
}
S21Matrix& S21Matrix::operator-=(const S21Matrix& other) {
*this = *this - other;
return *this;
}
S21Matrix& S21Matrix::operator*=(const S21Matrix& other) {
*this = (*this) * other;
return *this;
}
double& S21Matrix::operator()(int i, int j) {
if (i >= 0 && i < rows_ && j >= 0 && j < cols_) {
return matrix_[i][j];
} else {
throw std::out_of_range("Запрашиваемый индекс вне размерности матрицы!");
}
}
const double& S21Matrix::operator()(int i, int j) const {
if (i >= 0 && i < rows_ && j >= 0 && j < cols_) {
return matrix_[i][j];
} else {
throw std::out_of_range("Запрашиваемый индекс вне размерности матрицы!");
}
}
// Setters and Getters
int S21Matrix::GetCols() const { return cols_; }
int S21Matrix::GetRows() const { return rows_; }
void S21Matrix::SetCols(int cols) {
if (cols < 0) {
throw std::logic_error("Некорректное количество столбцов или строк!");
}
if (cols != cols_) {
S21Matrix tmp = S21Matrix(rows_, cols);
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < std::min(cols, cols_); j++) {
tmp.matrix_[i][j] = matrix_[i][j];
}
}
if (cols > cols_) {
for (int i = 0; i < rows_; i++) {
for (int j = cols_; j < cols; j++) {
tmp.matrix_[i][j] = 0;
}
}
}
*this = tmp;
}
}
void S21Matrix::SetRows(int rows) {
if (rows < 0) {
throw std::logic_error("Некорректное количество столбцов или строк!");
}
if (rows != rows_) {
S21Matrix tmp = S21Matrix(rows, cols_);
for (int i = 0; i < std::min(rows, rows_); i++) {
for (int j = 0; j < cols_; j++) {
tmp.matrix_[i][j] = matrix_[i][j];
}
}
if (rows > rows_) {
for (int i = rows_; i < rows; i++) {
for (int j = 0; j < cols_; j++) {
tmp.matrix_[i][j] = 0;
}
}
}
*this = tmp;
}
}
// Private Methodes
void S21Matrix::AllocateMemoryForMatrix_() {
if (rows_ < 1 || cols_ < 1) {
throw std::logic_error("Некорректное количество столбцов или строк!");
}
matrix_ = new double* [rows_] {};
int num_of_allocated_rows = 0;
try {
for (int i = 0; i < rows_; ++i) {
matrix_[i] = new double[cols_]{};
++num_of_allocated_rows;
}
} catch (std::bad_alloc& e) {
for (int i = 0; i < num_of_allocated_rows; ++i) {
delete[] matrix_[i];
}
delete[] matrix_;
throw;
}
}
S21Matrix S21Matrix::GetMinorOfMatrix_(int row, int column) const {
S21Matrix minor_of_matrix;
if (cols_ == 1 && rows_ == 1) {
minor_of_matrix = S21Matrix(rows_, cols_);
minor_of_matrix.matrix_[0][0] = matrix_[0][0];
return minor_of_matrix;
}
minor_of_matrix = S21Matrix(rows_ - 1, cols_ - 1);
int shift_i = 0;
for (int i = 0; i < rows_ - 1; i++) {
if (i == row) {
shift_i = 1;
}
int shift_j = 0;
for (int j = 0; j < cols_ - 1; j++) {
if (j == column) {
shift_j = 1;
}
minor_of_matrix.matrix_[i][j] = matrix_[i + shift_i][j + shift_j];
}
}
return minor_of_matrix;
}
} // namespace S21