-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
55 lines (47 loc) · 1.62 KB
/
test.cpp
File metadata and controls
55 lines (47 loc) · 1.62 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
#include <stdio.h>
#include <string.h>
#include <memory>
#include <set>
#include <stack>
#include <cmath>
#include <sstream>
#include "FuncWrapper.h"
template<typename ValueType>
ValueType func(ValueType x) {
return 1 + x / 2;
}
template<typename ValueType>
ValueType func1(ValueType x) {
return 1 + x * x;
}
template<typename ValueType>
ValueType func2(ValueType x) {
// 1. 3*x*x
// 2. 6*x
// 3. 6
return 1 + x * x * x;
}
template<typename ValueType>
ValueType func3(ValueType x) {
return x * log(x);
}
template<typename ValueType>
ValueType func4(ValueType x) {
return x*exp(x);
}
template<typename ValueType>
ValueType func5(ValueType x) {
return (1 - exp(-x)) / (1 + exp(-x));
}
int main() {
printf("grad(1 + x / 2) 0.5: %f \n", (float)grad(func<Box<float>>)(100));
printf("grad(grad(1 + x * x)) 2: %d\n", (int)grad(grad(func1<Box<int>>))(100));// 2
printf("grad(grad(grad(1 + x * x * x))) 6: %d\n", (int)grad(grad(grad(func2<Box<int>>)))(100));// 6
printf("grad(x * log(x)) 5.60517019: %f\n", (float)grad(func3<Box<float>>)(100));// 5.60517019
printf("grad(grad(x * log(x))) 0.01: %f\n", (float)grad(grad(func3<Box<float>>))(100));// 0.01
printf("grad(x*exp(x)) 5.436563: %f\n", (float)grad(func4<Box<float>>)(1));// 5.436563
printf("grad(grad(x*exp(x))) 8.154845: %f\n", (float)grad(grad(func4<Box<float>>))(1));// 8.154845
printf("grad( (1 - exp(-x)) / (1 + exp(-x)) ) 0.209987: %f\n", (float)grad(func5<Box<float>>)(-2));// 0.209987
printf("grad(grad( (1 - exp(-x)) / (1 + exp(-x)) )) 0.159925: %f\n", (float)grad(grad(func5<Box<float>>))(-2));// 0.159925
return 0;
}