proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
finite-difference.hpp
Go to the documentation of this file.
1
3#pragma once
4
6
7namespace proxsuite {
8namespace nlp {
9namespace autodiff {
10
11enum FDLevel {
12 TOC1 = 0,
13 TOC2 = 1
14};
15
23
24namespace internal {
25
26template <typename Scalar> struct finite_difference_impl {
28
29 static void computeJacobian(const ManifoldAbstractTpl<Scalar> &space,
30 const BaseFunctionTpl<Scalar> &func,
31 const Scalar fd_eps, const ConstVectorRef &x,
32 MatrixRef Jout) {
33 VectorXs ei(func.ndx());
34 VectorXs xplus = space.neutral();
35 VectorXs xminus = space.neutral();
36 ei.setZero();
37 for (int i = 0; i < func.ndx(); i++) {
38 ei(i) = fd_eps;
39 space.integrate(x, ei, xplus);
40 space.integrate(x, -ei, xminus);
41 Jout.col(i) = (func(xplus) - func(xminus)) / (2 * fd_eps);
42 ei(i) = 0.;
43 }
44 }
45
46 static void vectorHessianProduct(const ManifoldAbstractTpl<Scalar> &space,
47 const C1FunctionTpl<Scalar> &func,
48 const Scalar fd_eps, const ConstVectorRef &x,
49 const ConstVectorRef &v, MatrixRef Hout) {
50 VectorXs ei(func.ndx());
51 VectorXs xplus = space.neutral();
52 VectorXs xminus = space.neutral();
53 MatrixXs Jplus(func.nr(), func.ndx());
54 MatrixXs Jminus(func.nr(), func.ndx());
55 Jplus.setZero();
56 Jminus.setZero();
57 ei.setZero();
58
59 for (int i = 0; i < func.ndx(); i++) {
60 ei(i) = fd_eps;
61 space.integrate(x, ei, xplus);
62 space.integrate(x, -ei, xminus);
63 func.computeJacobian(xplus, Jplus);
64 func.computeJacobian(xminus, Jminus);
65 Hout.col(i) = ((Jplus - Jminus) / (2 * fd_eps)).transpose() * v;
66 ei(i) = 0.;
67 }
68 }
69};
70
71} // namespace internal
72
73template <typename Scalar, FDLevel n = TOC1> struct finite_difference_wrapper;
74
78template <typename _Scalar>
79struct finite_difference_wrapper<_Scalar, TOC1> : C1FunctionTpl<_Scalar> {
80 using Scalar = _Scalar;
82
85
87 const FuncType &func;
89
90 using Base::computeJacobian;
91
93 const FuncType &func, const Scalar fd_eps)
94 : Base(space, func.nr()), space(space), func(func), fd_eps(fd_eps) {}
95
96 VectorXs operator()(const ConstVectorRef &x) const override {
97 return func(x);
98 }
99
100 void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const override {
101 return internal::finite_difference_impl<Scalar>::computeJacobian(
102 space, func, fd_eps, x, Jout);
103 }
104};
105
112template <typename _Scalar>
113struct finite_difference_wrapper<_Scalar, TOC2> : C2FunctionTpl<_Scalar> {
114 using Scalar = _Scalar;
116
119
123
124 using Base::computeJacobian;
125
127 const FuncType &func, const Scalar fd_eps)
128 : Base(space, func.nr()), space(space), func(func), fd_eps(fd_eps) {}
129
130 VectorXs operator()(const ConstVectorRef &x) const override {
131 return func(x);
132 }
133
134 void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const override {
135 func.computeJacobian(x, Jout);
136 }
137
138 void vectorHessianProduct(const ConstVectorRef &x, const ConstVectorRef &v,
139 MatrixRef Hout) const override {
140 internal::finite_difference_impl<Scalar>::vectorHessianProduct(
141 space, func, fd_eps, x, v, Hout);
142 }
143};
144
145} // namespace autodiff
146} // namespace nlp
147} // namespace proxsuite
Base definitions for function classes.
#define PROXSUITE_NLP_DYNAMIC_TYPEDEFS(Scalar)
Definition math.hpp:26
@ FORWARD
Forward finite differences .
@ CENTRAL
Central finite differences .
@ BACKWARD
Backward finite differences .
@ TOC2
Cast to a function.
@ TOC1
Cast to a function.
Main package namespace.
Definition bcl-params.hpp:5
Base function type.
Definition fwd.hpp:70
int ndx() const
Get input manifold's tangent space dimension.
int nr() const
Get function codimension.
Differentiable function, with method for the Jacobian.
Definition fwd.hpp:73
virtual void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const =0
Jacobian matrix of the constraint function.
Twice-differentiable function, with method Jacobian and vector-hessian product evaluation.
Definition fwd.hpp:76
virtual VectorXs neutral() const
Get the neutral element from the manifold (if this makes sense).
void integrate(const ConstVectorRef &x, const ConstVectorRef &v, VectorRef out) const
Manifold integration operation .
VectorXs operator()(const ConstVectorRef &x) const override
Evaluate the residual at a given point x.
finite_difference_wrapper(const ManifoldAbstractTpl< Scalar > &space, const FuncType &func, const Scalar fd_eps)
void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const override
Jacobian matrix of the constraint function.
VectorXs operator()(const ConstVectorRef &x) const override
Evaluate the residual at a given point x.
void vectorHessianProduct(const ConstVectorRef &x, const ConstVectorRef &v, MatrixRef Hout) const override
Vector-hessian product.
void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const override
Jacobian matrix of the constraint function.
finite_difference_wrapper(const ManifoldAbstractTpl< Scalar > &space, const FuncType &func, const Scalar fd_eps)