-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linjal.cpp
287 lines (242 loc) · 7.16 KB
/
Linjal.cpp
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
#include "Linjal.h"
Matrix::Matrix(std::pair<unsigned int, unsigned int> dimensions) {
this->dimensions = dimensions;
data = new float[(size_t)dimensions.first * dimensions.second];
}
Matrix::Matrix(std::pair<unsigned int, unsigned int> dimensions, float value)
: Matrix(dimensions) {
for (unsigned int row = 0; row < dimensions.first; row++) {
for (unsigned int column = 0; column < dimensions.second; column++) {
operator()(row, column) = value;
}
}
}
Matrix::Matrix(unsigned int rows, unsigned int columns)
: Matrix(std::make_pair(rows, columns)) {}
Matrix::Matrix(unsigned int rows, unsigned int columns, float value)
: Matrix(std::make_pair(rows, columns), value) {}
Matrix::Matrix(const Matrix& other) {
dimensions = other.dimensions;
data = new float[(size_t)dimensions.first * dimensions.second];
std::memcpy(data, other.data, (size_t)dimensions.first * dimensions.second * sizeof(float));
}
Matrix::Matrix(Matrix&& other) noexcept {
dimensions = other.dimensions;
data = other.data;
other.dimensions = std::make_pair(0, 0);
other.data = nullptr;
}
Matrix::~Matrix() {
delete[] data;
}
unsigned int Matrix::calculateIndex(unsigned int row, unsigned int column) const {
return row * dimensions.second + column;
}
std::pair<unsigned int, unsigned int> Matrix::getDimensions() const {
return dimensions;
}
Matrix Matrix::getTranspose() const {
Matrix result(dimensions.second, dimensions.first);
for (unsigned int row = 0; row < dimensions.first; row++) {
for (unsigned int column = 0; column < dimensions.second; column++) {
result(column, row) = operator()(row, column);
}
}
return result;
}
Matrix& Matrix::operator=(const Matrix& other) noexcept {
if (this == &other) return *this;
delete[] data;
dimensions = other.dimensions;
data = new float[(size_t)dimensions.first * dimensions.second];
std::memcpy(data, other.data, (size_t)dimensions.first * dimensions.second * sizeof(float));
return *this;
}
Matrix& Matrix::operator=(Matrix&& other) noexcept {
if (this == &other) return *this;
delete[] data;
dimensions = other.dimensions;
data = other.data;
other.dimensions = std::make_pair(0, 0);
other.data = nullptr;
return *this;
}
float& Matrix::operator()(unsigned int row, unsigned int column) {
return data[calculateIndex(row, column)];
}
float Matrix::operator()(unsigned int row, unsigned int column) const {
return data[calculateIndex(row, column)];
}
Matrix Matrix::operator*(float scalar) const {
Matrix result = *this;
for (unsigned int row = 0; row < dimensions.first; row++) {
for (unsigned int column = 0; column < dimensions.second; column++) {
result(row, column) *= scalar;
}
}
return result;
}
Matrix& Matrix::operator*=(float scalar) {
*this = (*this)*scalar;
return *this;
}
Matrix Matrix::operator*(Matrix& other) const {
if (dimensions.second != other.dimensions.first) {
// Error: bad dimensions of operands
}
Matrix result(dimensions.first, other.dimensions.second);
for (unsigned int row = 0; row < result.dimensions.first; row++) {
for (unsigned int column = 0; column < result.dimensions.second; column++) {
result(row, column) = 0.0;
for (unsigned int i = 0; i < dimensions.second; i++) {
result(row, column) += operator()(row, i) * other(i, column);
}
}
}
return result;
}
Matrix Matrix::operator+(Matrix& other) const {
if (dimensions != other.dimensions) {
// Error: bad dimensions of operands
}
Matrix result(dimensions);
for (unsigned int row = 0; row < dimensions.first; row++) {
for (unsigned int column = 0; column < dimensions.second; column++) {
result(row, column) = operator()(row, column) + other(row, column);
}
}
return result;
}
Matrix Matrix::operator-() const {
Matrix result(dimensions);
for (unsigned int row = 0; row < dimensions.first; row++) {
for (unsigned int column = 0; column < dimensions.second; column++) {
result(row, column) = -operator()(row, column);
}
}
return result;
}
Matrix Matrix::operator-(Matrix& other) const {
return *this + (-other);
}
std::ostream& operator<<(std::ostream& os, const Matrix& m) {
std::pair<unsigned int, unsigned int> dimensions = m.getDimensions();
os << dimensions.first << " by " << dimensions.second << " matrix" << std::endl;
for (unsigned int row = 0; row < dimensions.first; row++) {
for (unsigned int column = 0; column < dimensions.second; column++)
os << m(row, column) << " ";
if (row != dimensions.first - 1)
os << std::endl;
}
return os;
}
Vector::Vector(unsigned int dimensions) {
this->dimensions = dimensions;
data = new float[dimensions];
}
Vector::Vector(unsigned int dimensions, float value) {
this->dimensions = dimensions;
data = new float[dimensions];
for (unsigned int d = 0; d < dimensions; d++) {
operator()(d) = value;
}
}
Vector::Vector(const Vector& other) {
dimensions = other.dimensions;
data = new float[dimensions];
std::memcpy(data, other.data, dimensions * sizeof(float));
}
Vector::Vector(Vector&& other) noexcept {
dimensions = other.dimensions;
data = other.data;
other.dimensions = 0;
other.data = nullptr;
}
Vector::~Vector() {
delete[] data;
}
unsigned int Vector::getDimensions() const {
return dimensions;
}
Vector& Vector::operator=(const Vector& other) noexcept {
if (this == &other) return *this;
delete[] data;
dimensions = other.dimensions;
data = new float[dimensions];
std::memcpy(data, other.data, dimensions * sizeof(float));
return *this;
}
Vector& Vector::operator=(Vector&& other) noexcept {
if (this == &other) return *this;
delete[] data;
dimensions = other.dimensions;
data = other.data;
other.dimensions = 0;
other.data = nullptr;
return *this;
}
float& Vector::operator()(unsigned int dimension) {
return data[dimension];
}
float Vector::operator()(unsigned int dimension) const {
return data[dimension];
}
float Vector::getMagnitude() const {
float sum = 0;
for (unsigned int d = 0; d < dimensions; d++) {
float temp = (*this)(d);
sum += temp * temp;
}
return sqrt(sum);
}
Vector Vector::operator*(float scalar) const {
Vector result = *this;
for (unsigned int d = 0; d < dimensions; d++) {
result(d) *= scalar;
}
return result;
}
Vector& Vector::operator*=(float scalar) {
*this = (*this) * scalar;
return *this;
}
Vector Vector::operator+(Vector& other) const {
if (dimensions != other.dimensions) {
// Error: bad dimensions of operands
}
Vector result = *this;
for (unsigned int d = 0; d < dimensions; d++) {
result(d) += other(d);
}
return result;
}
Vector Vector::operator-() const {
Vector result(dimensions);
for (unsigned int d = 0; d < dimensions; d++) {
result(d) = -operator()(d);
}
return result;
}
Vector Vector::operator-(Vector& other) const {
return *this + (-other);
}
float Vector::dotProduct(Vector& other) const {
if (dimensions != other.dimensions) {
// Error: bad dimensions of operands
}
float result = 0.0;
for (unsigned int d = 0; d < dimensions; d++) {
result += (*this)(d) * other(d);
}
return result;
}
std::ostream& operator<<(std::ostream& os, const Vector& v) {
unsigned int dimensions = v.getDimensions();
os << dimensions << "-dimensional vector" << std::endl;
for (unsigned int d = 0; d < dimensions; d++) {
os << v(d);
if (d != dimensions - 1)
os << std::endl;
}
return os;
}