-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.cpp
87 lines (61 loc) · 1.8 KB
/
update.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
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
#include <GrAD/GrAD>
#include <iostream>
#include <vector>
#include <cstdlib>
using std::vector;
template<class T>
T simple_fn(T x, T y){
return exp(-pow(x,T(2.0))/y) + y;
}
vector<double> simple_true_gr(double x, double y){
vector<double> r(2);
r[0] = -2.0 * x * exp(-pow(x,2.0)/y) / y;
r[1] = pow(x,2.0) * exp(- pow(x,2.0)/y) / pow(y,2.0) + 1.0;
return r;
}
void update_grad(int seed){
using namespace GrAD;
vector<double> res(8);
clock_t t;
double t1, t2, t3;
srand(seed);
ADparlist<double>* grd = new ADparlist<double>();
vector<double> x0(2);
x0[0] = (rand() / (double)RAND_MAX);
x0[1] = (rand() / (double)RAND_MAX);
std::cout << std::endl << std::endl << "Running update example" << std::endl << std::endl;
std::cout << "values: \n";
std::cout << x0[0] << " " << x0[1] << "\n\n";
AD<double> x(x0[0]);
AD<double> y(x0[1]);
grd->Independent(x);
grd->Independent(y);
AD<double> z = x+y; //simple_fn(x,y);
double fn = z.fn();
std::cout << "Function value:\n";
std::cout << fn << "\n";
std::cout << "\n\n";
std::cout << "Gradient value:\n";
vector<double> gr = z.gr();
std::cout << gr[0] << " " << gr[1] << "\n\n\n";
x0[0] = (rand() / (double)RAND_MAX);
x0[1] = (rand() / (double)RAND_MAX);
z.update(x0);
std::cout << "New values: \n";
std::cout << x0[0] << " " << x0[1] << "\n\n";
std::cout << x.fn() << " " << y.fn() << "\n\n";
fn = z.fn();
std::cout << "New Function value:\n";
std::cout << fn << "\n";
std::cout << "\n\n";
std::cout << "New Gradient value:\n";
gr = z.gr();
std::cout << gr[0] << " " << gr[1] << "\n\n\n";
std::cout << std::endl << std::endl;
delete grd;
return;
}
int main(){
update_grad(123);
return 0;
}