forked from NatLabRockies/ssc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.cpp
More file actions
130 lines (95 loc) · 3.14 KB
/
function.cpp
File metadata and controls
130 lines (95 loc) · 3.14 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* This file is part of the SPLINTER library.
* Copyright (C) 2012 Bjarne Grimstad (bjarne.grimstad@gmail.com).
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "function.h"
#include "utilities.h"
namespace SPLINTER
{
double Function::eval(const std::vector<double> &x) const
{
auto denseX = vectorToDenseVector(x);
return eval(denseX);
}
std::vector<double> Function::evalJacobian(const std::vector<double> &x) const
{
auto denseX = vectorToDenseVector(x);
return denseVectorToVector(evalJacobian(denseX));
}
std::vector<std::vector<double>> Function::evalHessian(const std::vector<double> &x) const
{
auto denseX = vectorToDenseVector(x);
return denseMatrixToVectorVector(secondOrderCentralDifference(denseX));
}
std::vector<double> Function::centralDifference(const std::vector<double> &x) const
{
auto denseX = vectorToDenseVector(x);
auto dx = centralDifference(denseX);
return denseVectorToVector(dx);
}
std::vector<std::vector<double>> Function::secondOrderCentralDifference(const std::vector<double> &x) const
{
auto denseX = vectorToDenseVector(x);
DenseMatrix ddx = secondOrderCentralDifference(denseX);
return denseMatrixToVectorVector(ddx);
}
DenseMatrix Function::evalJacobian(DenseVector x) const
{
return centralDifference(x);
}
DenseMatrix Function::evalHessian(DenseVector x) const
{
auto vec = denseVectorToVector(x);
auto hessian = evalHessian(vec);
return vectorVectorToDenseMatrix(hessian);
}
DenseMatrix Function::centralDifference(DenseVector x) const
{
DenseMatrix dx(1, x.size());
double h = 1e-6; // perturbation step size
double hForward = 0.5*h;
double hBackward = 0.5*h;
for (unsigned int i = 0; i < getNumVariables(); ++i)
{
DenseVector xForward(x);
xForward(i) = xForward(i) + hForward;
DenseVector xBackward(x);
xBackward(i) = xBackward(i) - hBackward;
double yForward = eval(xForward);
double yBackward = eval(xBackward);
dx(i) = (yForward - yBackward)/(hBackward + hForward);
}
return dx;
}
DenseMatrix Function::secondOrderCentralDifference(DenseVector x) const
{
DenseMatrix ddx(getNumVariables(), getNumVariables());
double h = 1e-6; // perturbation step size
double hForward = 0.5*h;
double hBackward = 0.5*h;
for (size_t i = 0; i < getNumVariables(); ++i)
{
for (size_t j = 0; j < getNumVariables(); ++j)
{
DenseVector x0(x);
DenseVector x1(x);
DenseVector x2(x);
DenseVector x3(x);
x0(i) = x0(i) + hForward;
x0(j) = x0(j) + hForward;
x1(i) = x1(i) - hBackward;
x1(j) = x1(j) + hForward;
x2(i) = x2(i) + hForward;
x2(j) = x2(j) - hBackward;
x3(i) = x3(i) - hBackward;
x3(j) = x3(j) - hBackward;
ddx(i, j) = (eval(x0) - eval(x1) - eval(x2) + eval(x3)) / (h * h);
}
}
return ddx;
}
} // namespace SPLINTER