16double Function::eval(
const std::vector<double>& x)
const
22std::vector<double> Function::evalJacobian(
const std::vector<double>& x)
const
28std::vector<std::vector<double>> Function::evalHessian(
const std::vector<double>
35std::vector<double> Function::centralDifference(
const std::vector<double>& x)
39 auto dx = centralDifference(denseX);
43std::vector<std::vector<double>> Function::secondOrderCentralDifference(
44 const std::vector<double>& x)
const
47 DenseMatrix ddx = secondOrderCentralDifference(denseX);
51DenseMatrix Function::evalJacobian(DenseVector x)
const
53 return centralDifference(x);
56DenseMatrix Function::evalHessian(DenseVector x)
const
59 auto hessian = evalHessian(vec);
63DenseMatrix Function::centralDifference(DenseVector x)
const
65 DenseMatrix dx(1, x.size());
67 double hForward = 0.5 * h;
68 double hBackward = 0.5 * h;
70 for (
unsigned int i = 0;
i < getNumVariables(); ++
i)
72 DenseVector xForward(x);
73 xForward(
i) = xForward(
i) + hForward;
74 DenseVector xBackward(x);
75 xBackward(
i) = xBackward(
i) - hBackward;
76 double yForward = eval(xForward);
77 double yBackward = eval(xBackward);
78 dx(
i) = (yForward - yBackward) / (hBackward + hForward);
84DenseMatrix Function::secondOrderCentralDifference(DenseVector x)
const
86 DenseMatrix ddx(getNumVariables(), getNumVariables());
88 double hForward = 0.5 * h;
89 double hBackward = 0.5 * h;
91 for (
size_t i = 0;
i < getNumVariables(); ++
i)
93 for (
size_t j = 0; j < getNumVariables(); ++j)
99 x0(
i) = x0(
i) + hForward;
100 x0(j) = x0(j) + hForward;
101 x1(
i) = x1(
i) - hBackward;
102 x1(j) = x1(j) + hForward;
103 x2(
i) = x2(
i) + hForward;
104 x2(j) = x2(j) - hBackward;
105 x3(
i) = x3(
i) - hBackward;
106 x3(j) = x3(j) - hBackward;
107 ddx(
i, j) = (eval(x0) - eval(x1) - eval(x2) + eval(x3)) / (h * h);
DenseMatrix vectorVectorToDenseMatrix(const std::vector< std::vector< double > > &vec)
DenseVector vectorToDenseVector(const std::vector< double > &vec)
std::vector< std::vector< double > > denseMatrixToVectorVector(const DenseMatrix &mat)
std::vector< double > denseVectorToVector(const DenseVector &denseVec)