-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.cpp
More file actions
43 lines (41 loc) · 1.14 KB
/
Copy pathMath.cpp
File metadata and controls
43 lines (41 loc) · 1.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
#include "math.hpp"
void utils::Math::multiplyMatrix(Matrix *a, Matrix *b, Matrix *c) {
for (int i = 0; i < a->getNumRows(); i++) {
for (int j = 0; j < b->getNumCols(); j++) {
for (int k = 0; k < b->getNumRows(); k++) {
double p = a->getValue(i, k) * b->getValue(k, j);
double newVal = c->getValue(i, j) + p;
c->setValue(i, j, newVal);
}
//std::cout << "multi\n" << c->getValue(i, j) << std::endl;
c->setValue(i, j, c->getValue(i, j));
}
}
}
void utils::Math::softMaxMatrix(Matrix *a, Matrix *b, Matrix *c) {
for (int i = 0; i < a->getNumRows(); i++) {
for (int j = 0; j < b->getNumCols(); j++) {
double max, sum = 0, emax;
for (int k = 0; k < b->getNumRows(); k++) {
//cout << "numrowsK\t" << k << endl;
double p = a->getValue(i, k) * b->getValue(k, j);
if (k == 0) {
max = p;
emax = exp(max);
}
if (p > max) {
max = p;
emax = exp(max);
}
sum += exp(p);
}
//cout << exp(max) << "\t" << sum << endl;
if (sum != 0)
c->setValue(i, j, emax / sum);
else
c->setValue(i, j, emax);
if (c->getValue(i, j) >= 1)
c->setValue(i, j, .999);
}
}
}