forked from asevko/Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixClass.h
More file actions
198 lines (177 loc) · 6.33 KB
/
Copy pathMatrixClass.h
File metadata and controls
198 lines (177 loc) · 6.33 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
/***
* @author Aleksey Sevko
*/
#ifndef IMAGECOMPRESSION_MATRIXCLASS_H
#define IMAGECOMPRESSION_MATRIXCLASS_H
#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
/// sizeX = n, sizeY = m
class MatrixClass {
std::vector<double> matrix;
unsigned int sizeX, sizeY;
public:
MatrixClass() : sizeX(0), sizeY(0){}
MatrixClass (unsigned int x, unsigned int y)
: sizeX(x), sizeY(y) {
matrix.resize(sizeX * sizeY);
std::fill(matrix.begin(), matrix.end(), 0);
}
explicit MatrixClass (std::vector<double> vector)
: sizeX(1), sizeY(static_cast<unsigned int>(vector.size())) {
matrix.resize(sizeX * sizeY);
std::copy(vector.begin(), vector.end(), matrix.begin());
}
MatrixClass (const MatrixClass& other){
std::vector<double>().swap(this->matrix);
if (this->matrix.size() != other.matrix.size()) {
this->matrix.resize(other.sizeX * other.sizeY);
}
this->sizeX = other.sizeX;
this->sizeY = other.sizeY;
std::copy(other.matrix.begin(), other.matrix.end(), this->matrix.begin());
}
MatrixClass (std::vector<double> vector, unsigned int n)
: sizeX(n), sizeY(n) {
matrix.resize(sizeX * sizeY);
std::copy(vector.begin(), vector.end(), matrix.begin());
}
MatrixClass (const MatrixClass m, unsigned int n)
: sizeX(n), sizeY(n) {
matrix.resize(sizeX * sizeY);
std::copy(m.matrix.begin(), m.matrix.end(), this->matrix.begin());
}
~MatrixClass() {
std::vector<double>().swap(matrix);
}
double& operator()(unsigned int x, unsigned int y) {
if (x >= sizeX || y >= sizeY) {
throw std::out_of_range("Bad access");
}
return matrix[sizeX * y + x];
}
const double& operator()(unsigned int x, unsigned int y) const {
if (x >= sizeX || y >= sizeY) {
throw std::out_of_range("Bad access");
}
return matrix[sizeX * y + x];
}
MatrixClass& operator*(const double value) {
std::transform(this->matrix.begin(), this->matrix.end(), this->matrix.begin(),
std::bind1st(std::multiplies<double>(),value));
return *this;
}
friend MatrixClass operator-(const MatrixClass &l, const MatrixClass &r) {
if (l.sizeX != r.sizeX
|| l.sizeY != r.sizeY) {
throw std::logic_error("Different matrix sizes");
}
MatrixClass result(l.sizeX, l.sizeY);
auto n = static_cast<unsigned int>(l.matrix.size());
for (unsigned int i = 0; i < n; i ++) {
result.matrix[i] = l.matrix[i] - r.matrix[i];
}
return result;
}
friend MatrixClass operator+(const MatrixClass &l, const MatrixClass &r) {
if (l.sizeX != r.sizeX
|| l.sizeY != r.sizeY) {
throw std::logic_error("Different matrix sizes");
}
MatrixClass result(l.sizeX, l.sizeY);
auto n = static_cast<unsigned int>(l.matrix.size());
for (unsigned int i = 0; i < n; i ++) {
result.matrix[i] = l.matrix[i] + r.matrix[i];
}
return result;
}
MatrixClass& operator+(const double& value) {
auto n = static_cast<unsigned int>(this->matrix.size());
for (unsigned int i = 0; i < n; i ++) {
this->matrix[i] += value;
}
return *this;
}
MatrixClass& operator=(const MatrixClass& other) {
if (this != &other) {
std::vector<double>().swap(this->matrix);
if (this->matrix.size() != other.matrix.size()) {
this->matrix.resize(other.sizeX * other.sizeY);
}
this->sizeX = other.sizeX;
this->sizeY = other.sizeY;
std::copy(other.matrix.begin(), other.matrix.end(), this->matrix.begin());
}
return *this;
}
friend MatrixClass operator *(const MatrixClass &l, const MatrixClass &r) {
if (l.sizeY != r.sizeX) {
throw std::logic_error("Wrong sizes");
}
MatrixClass result(l.sizeX, r.sizeY);
for (unsigned int i = 0; i < result.sizeX; i++) {
for (unsigned int j = 0; j < result.sizeY; j++){
double sum = 0;
for (unsigned int k = 0; k < r.sizeX; k++){
sum += l(i,k) * r(k, j);
}
result(i, j) = sum;
}
}
return result;
}
friend MatrixClass operator ||(const MatrixClass &l, const MatrixClass &r) {
if (l.sizeX != r.sizeX) {
throw std::logic_error("Different matrix sizes");
}
MatrixClass result = l;
result.resize(1, l.sizeY + r.sizeY);
for (unsigned int i = l.sizeY, j = 0; j < r.sizeY; i++, j++) {
result.matrix[i] = r.matrix[j];
}
return result;
}
friend MatrixClass operator+=(const MatrixClass &l, const MatrixClass &r) {
if (l.sizeX != r.sizeX
|| l.sizeY != r.sizeY) {
throw std::logic_error("Different matrix sizes");
}
MatrixClass result(l.sizeX, r.sizeY);
auto n = static_cast<unsigned int>(l.matrix.size());
for (unsigned int i = 0; i < n; i ++) {
result.matrix[i] = l.matrix[i] + r.matrix[i];
}
return result;
}
friend bool operator == (const MatrixClass &l, const MatrixClass &r) {
double e = 0.00001;
if (l.sizeX != r.sizeX
|| l.sizeY != r.sizeY) {
return false;
}
MatrixClass result(l.sizeX, l.sizeY);
unsigned int n = l.matrix.size();
for (unsigned int i = 0; i < n; i ++) {
if (abs(l.matrix[i] - r.matrix[i]) > e ) {
return false;
}
}
return true;
}
friend bool operator !=(const MatrixClass &l, const MatrixClass &r) {
return !(l == r);
}
void show();
void visualize();
void beautifulVisualization(const double& n);
MatrixClass transpose();
MatrixClass& activationFunction();
void insert(const double& value);
void nullifyMainDiagonal();
void resize(unsigned int sizeX, unsigned int sizeY);
double sumABS();
virtual int getX() const { return this->sizeX; };
virtual int getY() const { return this->sizeY; };
};
#endif //IMAGECOMPRESSION_MATRIXCLASS_H