-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
370 lines (347 loc) · 9.14 KB
/
Matrix.cpp
File metadata and controls
370 lines (347 loc) · 9.14 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// Created by anna_seli on 13/06/2021.
//
#include "Matrix.h"
#define ALLOC_FAILURE "can't allocate matrix"
#define INVALID_ROW_OR_COLS "invalid number of rows and cols"
#define INVALID_FILE "can't read entire file"
#define INVALID_DIM_1 "matrix dimensions less then 1"
#define INVALID_DIM_2 "matrix dimensions doesn't match"
#define INVALID_DIM_3 "matrix dimensions aren't the same"
#define INVALID_INDEX_1 "indices not in range"
#define INVALID_INDEX_2 "index not in range"
#define EXIT_FAILURE 1
/*
* matrix constructor
* @param rows number of rows in new matrix
* @param cols number of cls in new matrix
*/
Matrix::Matrix(int rows, int cols)
: num_of_rows(rows), num_of_cols(cols)
{
if ((rows < 1) || (cols < 1))
{
std::cerr << INVALID_ROW_OR_COLS << std::endl;
exit(EXIT_FAILURE);
}
int size = rows * cols;
matrix_data = new float[size];
if (!matrix_data)
{
std::cerr << ALLOC_FAILURE << std::endl;
exit(EXIT_FAILURE);
}
// initializing all elements to 0.
for (int i = 0; i < size; i++)
{
matrix_data[i] = 0.0;
}
}
/*
* default matrix constructor
*/
Matrix::Matrix()
: Matrix(1, 1){}
/*
* copy constructor
*/
Matrix::Matrix(const Matrix &source_matrix)
: num_of_rows(source_matrix.num_of_rows),
num_of_cols(source_matrix.num_of_cols)
{
// allocate new matrix
matrix_data = new float[num_of_rows * num_of_cols];
if (!matrix_data)
{
std::cerr << ALLOC_FAILURE << std::endl;
exit(EXIT_FAILURE);
}
// copy elements from source matrix
for (auto i = 0; i < num_of_rows * num_of_cols; i++)
{
matrix_data[i] = source_matrix.matrix_data[i];
}
}
/*
* destructor
*/
Matrix::~Matrix()
{
delete [] matrix_data;
matrix_data = nullptr;
}
/*
* getter function for rows
*/
int Matrix::get_rows() const
{
return num_of_rows;
}
/*
* getter function for columns
*/
int Matrix::get_cols() const
{
return num_of_cols;
}
/*
* transpose matrix function
* @return reference to this matrix to transpose
*/
Matrix& Matrix::transpose()
{
// copy old matrix
Matrix copy_matrix = Matrix(*this);
// change rows and cols of current matrix
int temp_rows = num_of_rows;
num_of_rows = num_of_cols;
num_of_cols = temp_rows;
for (auto i = 0; i < num_of_rows; i++)
{
for (auto j = 0; j < num_of_cols; j++)
{
matrix_data[i * num_of_cols + j] = copy_matrix.matrix_data
[j * num_of_rows + i];
}
}
return *this;
}
/*
* transform matrix to a vector
* @return vectorized this matrix
*/
Matrix &Matrix::vectorize()
{
num_of_rows = num_of_rows * num_of_cols;
num_of_cols = 1;
return *this;
}
/*
* function that prints all elements in matrix
*/
void Matrix::plain_print() const
{
for (auto i = 0; i < num_of_rows; i++)
{
for (auto j = 0; j < num_of_cols; j++)
{
std::cout << matrix_data[i * num_of_cols + j] << " " << std::endl;
}
}
}
/*
* function that multiplies elements in 2 matrices element-wise
* @param another_matrix the second matrix to dot with the this matrix
*/
Matrix Matrix::dot(const Matrix &another_matrix) const
{
Matrix new_matrix (num_of_rows, num_of_cols);
for (auto i = 0; i < num_of_rows * num_of_cols; i++)
{
new_matrix.matrix_data[i] = matrix_data[i] *
another_matrix.matrix_data[i];
}
return new_matrix;
}
/*
* function that calculates the euclidean norm of the matrix
*/
float Matrix::norm() const
{
float sum = 0;
for (auto i = 0; i < num_of_rows; i++)
{
for (auto j = 0; j < num_of_cols; j++)
{
sum += std::pow(matrix_data[i * num_of_cols + j], 2);
}
}
return std::sqrt(sum);
}
/*
* rea from file operation for getting image of matrix
* @param is the input stream
* @param matrix the matrix to get image to
* @return reference to the stream
*/
std::istream &read_binary_file(std::istream &is, const Matrix &matrix)
{
if (!is)
{
std::cerr << INVALID_FILE << std::endl;
exit(EXIT_FAILURE);
}
unsigned long length = matrix.num_of_rows * matrix.num_of_cols *
sizeof(float);
is.read((char *) (matrix.matrix_data), length);
return is;
}
/*
* plus operator that adds elements in 2 matrices element-wise
* @param another_matrix the second matrix to plus to the this matrix
*/
Matrix Matrix::operator+(const Matrix &another_matrix) const
{
if ((num_of_rows != another_matrix.num_of_rows) || (num_of_cols != another_matrix.num_of_cols))
{
std::cerr << INVALID_DIM_3 << std::endl;
exit(EXIT_FAILURE);
}
Matrix new_matrix (num_of_rows, num_of_cols);
for (auto i = 0; i < num_of_rows * num_of_cols; i++)
{
new_matrix.matrix_data[i] = matrix_data[i] +
another_matrix.matrix_data[i];
}
return new_matrix;
}
/*
* assignment operator that assigns matrix to the this matrix
* @param another_matrix the matrix to assign
* @return a reference to he this matrix that was assigned
*/
Matrix& Matrix::operator=(const Matrix &another_matrix)
{
if (this == &another_matrix)
{
return *this;
}
num_of_rows = another_matrix.num_of_rows;
num_of_cols = another_matrix.num_of_cols;
delete [] matrix_data;
int size = num_of_rows * num_of_cols;
matrix_data = new float[size];
for (auto i = 0; i < size; i++)
{
matrix_data[i] = another_matrix.matrix_data[i];
}
return *this;
}
/*
* matrix multiplication as in linear algebra
* @param another_matrix a matrix to be multiplied to the right with the
* this matrix
* @return product matrix
*/
Matrix Matrix::operator*(const Matrix &another_matrix) const
{
if (num_of_cols != another_matrix.num_of_rows)
{
std::cerr << INVALID_DIM_2 << std::endl;
exit(EXIT_FAILURE);
}
Matrix new_matrix (num_of_rows, another_matrix.num_of_cols);
for (auto i = 0; i < num_of_rows; i++)
{
for (auto j = 0; j < another_matrix.num_of_cols; j++)
{
float sum = 0;
for (auto k = 0; k < num_of_cols; k++)
{
float a = matrix_data[i * num_of_cols + k];
float b = another_matrix.matrix_data
[k * another_matrix.num_of_cols + j];
sum += (a * b);
}
new_matrix.matrix_data[i * another_matrix.num_of_cols + j] = sum;
}
}
return new_matrix;
}
/*
* right multiplication of scalar by matrix
* @param c scalar
* @return product matrix
*/
Matrix Matrix::operator*(float c) const
{
Matrix new_matrix (num_of_rows, num_of_cols);
int size = num_of_rows * num_of_cols;
for (auto i = 0; i < size; i++)
{
new_matrix.matrix_data[i] = matrix_data[i] * c;
}
return new_matrix;
}
/*
* left multiplication of scalar by matrix
* @param c scalar
* @param another_matrix matrix o be calculated
* @return product matrix
*/
Matrix operator*(float c, const Matrix &another_matrix)
{
//return this->operator*(another_matrix, c);
Matrix new_matrix (another_matrix.num_of_rows, another_matrix.num_of_cols);
int size = another_matrix.num_of_rows * another_matrix.num_of_cols;
for (auto i = 0; i < size; i++)
{
new_matrix.matrix_data[i] = c * another_matrix.matrix_data[i];
}
return new_matrix;
}
/*
* plus assign operator that adds elements in 2 matrices element-wise
* and assigns the this matrix to the added matrix - self with a new one
* @param another_matrix the second matrix to plus to the this matrix
* @return a reference to the this transformed matrix
*/
Matrix &Matrix::operator+=(const Matrix &another_matrix)
{
*this = *this + another_matrix;
return *this;
}
/*
* parenthesis function that gets the (i,j) value of the matrix
* @param i number of row
* @param j number of col
* @return the float value in the matrix
*/
float &Matrix::operator()(int i, int j) const
{
if ((i < 0) || (j < 0) || (i >= num_of_rows) || (j >= num_of_cols))
{
std::cerr << INVALID_INDEX_1 << std::endl;
exit(EXIT_FAILURE);
}
return matrix_data[i * num_of_cols + j];
}
/*
* square parenthesis function that gets the i value of the matrix
* @param i index number
* @return the float value in the matrix
*/
float &Matrix::operator[](int i) const
{
if ((i < 0) || (i >= num_of_rows * num_of_cols))
{
std::cerr << INVALID_INDEX_2 << std::endl;
exit(EXIT_FAILURE);
}
return matrix_data[i];
}
/*
* insert operation for printing image of matrix
* @param os the output stream
* @param matrix the matrix to print
* @return reference to the stream
*/
std::ostream &operator<<(std::ostream &os, const Matrix &matrix)
{
for (auto i = 0; i < matrix.num_of_rows; i++)
{
for (auto j = 0; j < matrix.num_of_cols; j++)
{
if (matrix.matrix_data[i * matrix.num_of_cols + j] >= 0.1)
{
os << " " << std::endl;
}
else
{
os << "**" << std::endl;
}
}
os << std::endl;
}
return os;
}