-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyMatrix.cpp
More file actions
317 lines (274 loc) · 8.16 KB
/
Copy pathmyMatrix.cpp
File metadata and controls
317 lines (274 loc) · 8.16 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
#include "myMatrix.h"
// constructor and destructor
MyMatrix::MyMatrix() noexcept
: rows_(DEFAULT_MATRIX_SIZE), cols_(DEFAULT_MATRIX_SIZE),
matrix_(new double[DEFAULT_MATRIX_SIZE * DEFAULT_MATRIX_SIZE]{}) {}
MyMatrix::MyMatrix(int r, int c)
: rows_(r), cols_(c), matrix_(new double[r * c]{}) {
if (r < 1 || c < 1)
throw std::out_of_range("Rows and columns should be positive");
}
MyMatrix::MyMatrix(const MyMatrix &other)
: rows_{other.rows_}, cols_{other.cols_},
matrix_(new double[rows_ * cols_]) {
std::copy(other.matrix_, other.matrix_ + rows_ * cols_, matrix_);
}
MyMatrix::MyMatrix(MyMatrix &&other) noexcept
: rows_{other.rows_}, cols_{other.cols_}, matrix_(other.matrix_) {
other.matrix_ = nullptr;
other.rows_ = 0;
other.cols_ = 0;
}
MyMatrix::~MyMatrix() noexcept { delete[] matrix_; }
MyMatrix &MyMatrix::operator=(const MyMatrix &other) {
MyMatrix tmp(other);
*this = std::move(tmp);
return *this;
}
MyMatrix &MyMatrix::operator=(MyMatrix &&other) noexcept {
if (this != &other) {
delete[] matrix_;
rows_ = 0;
cols_ = 0;
matrix_ = nullptr;
std::swap(rows_, other.rows_);
std::swap(cols_, other.cols_);
std::swap(matrix_, other.matrix_);
}
return *this;
}
double &MyMatrix::operator()(int row, int col) & {
return const_cast<double &>(getElement(row, col));
}
const double &MyMatrix::operator()(int row, int col) const & {
return getElement(row, col);
}
bool MyMatrix::operator==(const MyMatrix &other) { return EqMatrix(other); }
MyMatrix MyMatrix::operator+(const MyMatrix &other) const {
MyMatrix res(*this);
res.SumMatrix(other);
return res;
}
MyMatrix MyMatrix::operator+=(const MyMatrix &other) {
SumMatrix(other);
return *this;
}
MyMatrix MyMatrix::operator-(const MyMatrix &other) const {
MyMatrix res(*this);
res.SubMatrix(other);
return res;
}
MyMatrix MyMatrix::operator-=(const MyMatrix &other) {
SubMatrix(other);
return *this;
}
MyMatrix MyMatrix::operator*(const double num) const noexcept {
MyMatrix res(*this);
res.MulNumber(num);
return res;
}
MyMatrix MyMatrix::operator*=(const double num) {
MulNumber(num);
return *this;
}
MyMatrix MyMatrix::operator*(const MyMatrix &other) const {
MyMatrix res(*this);
res.MulMatrix(other);
return res;
}
MyMatrix MyMatrix::operator*=(const MyMatrix &other) {
MulMatrix(other);
return *this;
}
bool MyMatrix::EqMatrix(const MyMatrix &other) {
bool result = true;
if (rows_ != other.rows_ || cols_ != other.cols_) {
result = false;
} else {
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
if (std::abs(other(i, j) - (*this)(i, j)) > precision)
result = false;
}
}
}
return result;
}
/*void MyMatrix::SumMatrix(const MyMatrix &other)
{
if(rows_ == other.GetRows() && cols_ == other.GetColumns())
{
for(int i = 0; i < rows_; i++){
for(int j = 0; j < cols_; j++){
(*this)(i, j) += other(i, j);
}
}
} else {
throw std::out_of_range("Incorrect input, matrix should have the same
size");
}
}*/
void MyMatrix::SumMatrix(const MyMatrix &other) {
if (rows_ == other.GetRows() && cols_ == other.GetColumns()) {
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
(*this)(i, j) += other(i, j);
}
}
} else {
throw std::out_of_range(
"Incorrect input, matrix should have the same size");
}
}
void MyMatrix::MulNumber(const double num) noexcept {
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
(*this)(i, j) *= num;
}
}
}
void MyMatrix::MulMatrix(const MyMatrix &other) {
if (cols_ == other.GetRows()) {
MyMatrix resultMatrix(rows_, other.GetColumns());
for (int i = 0; i < resultMatrix.GetRows(); i++) {
for (int j = 0; j < resultMatrix.GetColumns(); j++) {
for (int k = 0; k < cols_; k++) {
resultMatrix(i, j) += ((*this)(i, k) * other(k, j));
}
}
}
*this = std::move(resultMatrix);
} else {
throw std::logic_error(
"Incorrect input: number of columns in the first matrix should be"
"equal to number of rows in the second matrix");
}
}
MyMatrix MyMatrix::Transpose() const {
MyMatrix newMatrix(cols_, rows_);
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
newMatrix(j, i) = (*this)(i, j);
}
}
return newMatrix;
}
MyMatrix MyMatrix::getMinorMatrix(int rows, int columns) const {
if (this->rows_ == 1 && this->cols_ == 1)
return *this;
MyMatrix tmp(this->rows_ - 1, this->cols_ - 1);
int shift_row = 0;
int shift_col = 0;
for (int i = 0; i < this->rows_ - 1; i++) {
if (i == rows) {
shift_row = 1;
}
shift_col = 0;
for (int j = 0; j < this->cols_ - 1; j++) {
if (j == columns) {
shift_col = 1;
}
tmp(i, j) = (*this)(i + shift_row, j + shift_col);
}
}
return tmp;
}
//!
double MyMatrix::Determinant() const {
if (this->rows_ != this->cols_)
throw std::out_of_range("The matrix is not squared");
double result = 0;
if (this->rows_ == 1)
result = (*this)(0, 0);
else if (this->rows_ == 2)
result = (*this)(0, 0) * (*this)(1, 1) - (*this)(0, 1) * (*this)(1, 0);
else {
for (int j = 0; j < this->cols_; j++)
result += ((j << 31) >> 31 == 0 ? 1 : -1) *
getMinorMatrix(0, j).Determinant() * (*this)(0, j);
}
return result;
}
MyMatrix MyMatrix::CalcComplements() {
if (rows_ != cols_)
throw std::logic_error("Incorrect input, matrix should be squared");
MyMatrix result(rows_, cols_);
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
result(i, j) = (((i + j) << 31) >> 31 == 0 ? 1 : -1) *
getMinorMatrix(i, j).Determinant();
}
}
return result;
}
MyMatrix MyMatrix::InverseMatrix() const {
if (rows_ != cols_) {
throw std::logic_error("Incorrect input, matrix should be squared");
}
double det = Determinant();
if (det == 0.0 || abs(det) < precision) {
throw std::logic_error(
"Incorrect input, matrix is not invertible, determinant should be "
"non-zero");
}
return Transpose().CalcComplements() * (1.0 / det);
}
int MyMatrix::GetRows() const noexcept { return rows_; }
int MyMatrix::GetColumns() const noexcept { return cols_; }
const double &MyMatrix::getElement(int row, int col) const {
if (row >= rows_ || col >= cols_ || row < 0 || col < 0)
throw std::out_of_range("Incorrect input, index out of bounds");
return matrix_[row * cols_ + col];
}
void MyMatrix::SetRows(int new_rows) {
if (new_rows < 1)
throw std::length_error("Rows must be > 0");
if (new_rows == rows_)
return;
MyMatrix tmp(new_rows, cols_);
int min_rows = std::min(rows_, new_rows);
std::copy(matrix_, matrix_ + min_rows * cols_, tmp.matrix_);
*this = std::move(tmp);
}
void MyMatrix::SetColumns(int new_columns) {
if (new_columns < 1)
throw std::length_error("Column out of range; must be > 0");
if (new_columns == cols_)
return;
MyMatrix tmp(rows_, new_columns);
int min_cols = std::min(cols_, new_columns);
for (int i = 0; i < rows_; ++i) {
std::copy(matrix_ + i * cols_, matrix_ + i * cols_ + min_cols,
tmp.matrix_ + i * new_columns);
}
*this = std::move(tmp);
}
// adiitional methods
void MyMatrix::set(int row, int col, double value) {
if (row < 0 || row >= rows_ || col < 0 || col >= cols_)
throw std::out_of_range("Index out of bounds");
matrix_[row * cols_ + col] = value;
}
double MyMatrix::get(int row, int col) const {
if (row < 0 || row >= rows_ || col < 0 || col >= cols_)
throw std::out_of_range("Index out of bounds");
return matrix_[row * cols_ + col];
}
void MyMatrix::printMatrix() const {
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
std::cout << get(i, j) << " ";
}
std::cout << std::endl;
}
}
void MyMatrix::SubMatrix(const MyMatrix &other) {
if (rows_ != other.rows_ || cols_ != other.cols_) {
throw std::invalid_argument("Matrix dimensions must match for subtraction");
}
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
set(i, j, get(i, j) - other.get(i, j));
}
}
}