forked from asevko/Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixClass.cpp
More file actions
94 lines (84 loc) · 2.49 KB
/
Copy pathMatrixClass.cpp
File metadata and controls
94 lines (84 loc) · 2.49 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
//
// Created by Alexei Sevko on 11/2/17.
//
#include "MatrixClass.h"
void MatrixClass::beautifulVisualization(const double &n) {
MatrixClass thisT = *this;
thisT = thisT.transpose();
MatrixClass toVisualize(thisT , (unsigned int) sqrt(n));
toVisualize.visualize();
}
void MatrixClass::visualize() {
for (unsigned int i = 0; i < sizeX; i++) {
for (unsigned int j = 0; j < sizeY; ++j) {
if (matrix[sizeX * i + j] > 0) {
std::cout << "#";
} else {
std::cout << ".";
}
if (j == sizeY - 1)
std::cout << std::endl;
}
}
}
void MatrixClass::show() {
for (unsigned int i = 0; i < sizeX; i++) {
for (unsigned int j = 0; j < sizeY; ++j) {
if (matrix[sizeX * i + j] < 0)
std::cout << matrix[sizeX * i + j] << " ";
else
std::cout << " " << matrix[sizeX * i + j] << " ";
if (j == sizeY - 1)
std::cout << std::endl;
}
}
}
MatrixClass MatrixClass::transpose() {
MatrixClass result(sizeY, sizeX);
for (unsigned int i = 0; i < result.sizeX; i++) {
for (unsigned int j = 0; j < result.sizeY; j++) {
result(i, j) = matrix[sizeX * i + j];
}
}
return result;
}
MatrixClass& MatrixClass::activationFunction() {
for (unsigned int i = 0; i < this->sizeX * this->sizeY; i ++) {
this->matrix[i] = tanh(this->matrix[i]);
}
return *this;
}
void MatrixClass::resize(unsigned int sizeX, unsigned int sizeY) {
if (sizeX < 1 || sizeY < 1 ||
sizeX < this->sizeX ||
sizeY < this->sizeY) {
throw std::logic_error("Negative matrix sizes");
}
this->sizeX = sizeX;
this->sizeY = sizeY;
this->matrix.resize(sizeX * sizeY);
}
void MatrixClass::insert(const double& value) {
this->sizeY += 1;
this->matrix.resize(this->sizeX * this->sizeY);
this->matrix.insert(this->matrix.begin(), value);
}
void MatrixClass::nullifyMainDiagonal() {
if (this->sizeX != this->sizeY) {
throw std::logic_error("Not square matrix");
}
for (unsigned int i = 0; i < this->sizeX; i++) {
for (unsigned int j = 0; j < this->sizeY; j++) {
if (i == j ) {
matrix[sizeX * i + j] = 0;
}
}
}
}
double MatrixClass::sumABS() {
double result = 0;
for (unsigned int i = 0; i < sizeX * sizeY; i++) {
result += abs(matrix[i]);
}
return result;
}