-
Notifications
You must be signed in to change notification settings - Fork 5
/
numerical_derivatives.h
77 lines (60 loc) · 1.3 KB
/
numerical_derivatives.h
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
#ifndef NUMERICAL_DERIVATIVES_H
#define NUMERICAL_DERIVATIVES_H
#include <TooN/TooN.h>
template<int S, class B, class Functor> TooN::Vector<S> debug_numerical_gradient(const Functor& f, const TooN::Vector<S,double, B>& x)
{
using namespace TooN;
Vector<S> grad(x.size());
Vector<S> xh=x;
const double h=1e-4;
for(int i=0; i < x.size(); i++)
{
xh[i] += h;
double fwd = f(xh);
xh[i] = x[i] - h;
double rev = f(xh);
grad[i] = (fwd - rev) / (2*h);
xh[i] = x[i];
}
return grad;
}
template<int S, class B, class Functor> TooN::Matrix<S> debug_numerical_hessian(const Functor& f, const TooN::Vector<S,double, B>& x)
{
using namespace TooN;
Matrix<S> hess(x.size(), x.size());
Vector<S> xh=x;
const double h=1e-3;
for(int i=0; i < x.size(); i++)
for(int j=i+1; j < x.size(); j++)
{
xh = x;
xh[i] += h;
xh[j] += h;
double a = f(xh);
xh = x;
xh[i] -= h;
xh[j] += h;
double b = f(xh);
xh = x;
xh[i] += h;
xh[j] -= h;
double c = f(xh);
xh = x;
xh[i] -= h;
xh[j] -= h;
double d = f(xh);
hess[i][j] = hess[j][i] = (a-b-c+d) / (4*h*h);
}
for(int i=0; i < x.size(); i++)
{
xh = x;
double b = f(xh);
xh[i] += h;
double a = f(xh);
xh[i] = x[i] - h;
double c = f(xh);
hess[i][i] = (a - 2*b + c) / (h*h);
}
return hess;
}
#endif