-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
52 lines (45 loc) · 1.02 KB
/
utils.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
#include "utils.h"
#include <vector>
using namespace std;
int getRandom(int min, int max){
return rand() % (max - min + 1) + min;
}
/*
Returns the calculated sigmoid value 1/(1+e^-x)
Range is 0 to 1 non-inclusive
*/
double sigmoid(double x){
return 1 / (1 + exp(-1 * x));
}
double inverse_sigmoid(double x){
return -log((1-x)/x);
}
double sigmoid_prime(double x){
return sigmoid(x) * (1 - sigmoid(x));
}
/*
Returns a new vector of a + b
*/
vector<double> add(const vector<double> &a, const vector<double> &b){
vector<double> c;
if(a.size() != b.size()){
throw invalid_argument("cannot add vectors of different sizes");
}
for(int i = 0; i < (int)a.size(); i++){
c.push_back(a[i] + b[i]);
}
return c;
}
/*
Returns a new vector of a - b
*/
vector<double> subtract(const vector<double> &a, const vector<double> &b){
vector<double> c;
if(a.size() != b.size()){
throw invalid_argument("cannot subtract vectors of different sizes");
}
for(int i = 0; i < (int)a.size(); i++){
c.push_back(a[i] - b[i]);
}
return c;
}