-
Notifications
You must be signed in to change notification settings - Fork 2
/
finite_difference.h
41 lines (31 loc) · 1.09 KB
/
finite_difference.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
#pragma once
#include <functional>
#include "matrix.h"
namespace tiny_sqp_solver
{
template <int Inputs, int Outputs>
Matrix<Inputs, Outputs> differentiate(const std::function<Matrix<Outputs>(const Matrix<Inputs> &)> f,
const Matrix<Inputs> &x, double epsilon = 1e-5)
{
Matrix<Inputs, Outputs> D;
Matrix<Inputs> perturbation = Zeros<Inputs>();
for (int i = 0; i < Outputs; ++i)
{
for (int j = 0; j < Inputs; ++j)
{
perturbation(j) = epsilon / 2.0;
double high = f(x + perturbation)(i);
double low = f(x - perturbation)(i);
D(j, i) = (high - low) / epsilon;
perturbation(j) = 0;
}
}
return D;
}
template <int Inputs>
Matrix<Inputs, Inputs> twice_differentiate(const std::function<Matrix<1>(const Matrix<Inputs> &)> f,
const Matrix<Inputs> &x, double epsilon = 1e-5)
{
return differentiate<Inputs, Inputs>([&f](const Matrix<Inputs> &x) { return differentiate<Inputs, 1>(f, x); }, x);
}
}; // namespace tiny_sqp_solver