-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrices.cpp
More file actions
218 lines (156 loc) · 4.95 KB
/
matrices.cpp
File metadata and controls
218 lines (156 loc) · 4.95 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
#include <cstdlib>
#include <stdexcept>
#include <iostream>
#include "matrices.hpp"
matrix::matrix() noexcept : N_(0), M_(0), data_(nullptr) {
}
matrix::matrix(size_t N, size_t M) : N_(N), M_(M), data_(nullptr) {
if ((N == 0) ^ (M == 0))
throw std::invalid_argument("matrix sizes should be positive");
if (N * M / M != N)
throw std::invalid_argument("matrix size is too large (size_t overflow)");
data_ = new double[N * M];
}
matrix& matrix::resize(size_t N, size_t M) {
if (N == 0 || M == 0)
throw std::invalid_argument("matrix sizes should be positive!");
if (N * M / M != N)
throw std::invalid_argument("matrix size is too large (size_t overflow)");
delete[] data_;
N_ = N;
M_ = M;
data_ = new double[M * N];
return *this;
}
matrix::matrix(const matrix& m) : N_(m.N_), M_(m.M_) {
if (m.data_ == nullptr)
return;
data_ = new double[N_ * M_];
std::copy(data_, data_ + N_ * M_, m.data_);
}
matrix::matrix(matrix&& m) noexcept : N_(m.N_), M_(m.N_), data_(m.data_) {
m.data_ = nullptr;
}
matrix& matrix::operator=(matrix&& m) noexcept {
if (this == &m)
return *this;
delete[] data_;
N_ = m.N_;
M_ = m.M_;
data_ = m.data_;
m.data_ = nullptr;
m.N_ = 0;
m.M_ = 0;
return *this;
}
matrix& matrix::operator=(const matrix& m) {
if (this == &m)
return *this;
delete[] data_;
N_ = m.N_;
M_ = m.M_;
if (m.data_ == nullptr) {
data_ = nullptr;
return *this;
}
data_ = new double[N_ * M_];
std::copy(data_, data_ + N_ * M_, m.data_);
return *this;
}
double& matrix::operator()(const size_t i, const size_t j) {
if (data_ == nullptr)
throw std::runtime_error("use of uninitialized matrix");
if (i > N_ || j > M_)
throw std::out_of_range("incorrect matrix indexing");
return data_[i * M_ + j];
}
double matrix::operator()(const size_t i, const size_t j) const {
if (data_ == nullptr)
throw std::runtime_error("use of uninitialized matrix");
if (i > N_ || j > M_)
throw std::out_of_range("incorrect matrix indexing");
return data_[i * M_ + j];
}
matrix matrix::operator*(const matrix& m) const {
if (data_ == nullptr || m.data_ == nullptr)
throw std::runtime_error("use of uninitialized matrix in multiplication");
if (M_ != m.N_)
throw std::invalid_argument("matrix dimensions in multiplication are incompatible");
matrix res(N_, m.M_);
for (size_t i = 0; i < res.N_; i++)
for (size_t j = 0; j < res.M_; j++) {
double sum = 0;
for (size_t k = 0; k < M_; k++) {
sum += (*this)(i, k) * m(k, j);
}
res(i, j) = sum;
}
return res;
}
matrix& matrix::operator*=(const matrix& m) {
if (data_ == nullptr || m.data_ == nullptr)
throw std::runtime_error("use of uninitialized matrix in multiplication");
if (M_ != m.N_)
throw std::invalid_argument("matrix dimensions in multiplication are incompatible");
matrix res(N_, m.M_);
for (size_t i = 0; i < res.N_; i++)
for (size_t j = 0; j < res.M_; j++) {
double val = 0;
for (size_t k = 0; k < M_; k++) {
val += (*this)(i, k) * m(k, j);
}
res(i, j) = val;
}
*this = std::move(res);
return *this;
}
matrix matrix::operator+(const matrix& m) const {
if (data_ == nullptr || m.data_ == nullptr)
throw std::runtime_error("use of uninitialized matrix in add");
if (N_ != m.N_ || M_ != m.M_)
throw std::invalid_argument("matrix dimensions in add are incompatible");
auto res = *this;
res += m;
return res;
}
matrix& matrix::operator+=(const matrix& m) {
if (data_ == nullptr || m.data_ == nullptr)
throw std::runtime_error("use of uninitialized matrix in addition");
if (N_ != m.N_ || M_ != m.M_)
throw std::invalid_argument("matrix dimensions in addition are incompatible");
for (size_t i = 0; i < N_; i++)
for (size_t j = 0; j < M_; j++) {
(*this)(i, j) += m(i, j);
}
return *this;
}
matrix::~matrix() {
delete[] data_;
}
size_t matrix::get_rows() const noexcept {
return N_;
}
size_t matrix::get_columns() const noexcept {
return M_;
}
std::istream& operator>>(std::istream& stream, matrix& m) {
size_t N, M;
stream >> N >> M;
m.resize(N, M);
for (size_t i = 0; i < N; i++)
for (size_t j = 0; j < M; j++)
stream >> m(i, j);
return stream;
}
std::ostream& operator<<(std::ostream& stream, const matrix& m) {
if (m.get_rows() == 0)
return stream;
stream << m.get_rows() << " " << m.get_columns() << std::endl;
for (size_t i = 0; i < m.get_rows(); i++) {
stream << m(i, 0);
for (size_t j = 1; j < m.get_columns(); j++)
stream << " " << m(i, j);
stream << std::endl;
}
return stream;
}