40#include "../../../codi.hpp"
41#include "../../expressions/lhsExpressionInterface.hpp"
42#include "../../misc/constructVector.hpp"
43#include "../../traits/gradientTraits.hpp"
44#include "../../traits/tapeTraits.hpp"
45#include "../data/dummy.hpp"
46#include "../data/hessian.hpp"
47#include "../data/jacobian.hpp"
48#include "tapeHelper.hpp"
64 template<
typename T_Func,
typename T_Type,
typename T_InputStore = std::vector<T_Type>,
65 typename T_OutputStore = std::vector<T_Type>>
101 template<
typename VecX,
typename VecY>
105 template<
typename VecX,
typename Jac,
typename VecY>
109 template<
typename VecX,
typename Hes,
typename VecY,
typename Jac>
115 template<
typename VecX>
119 template<
typename VecY>
131 template<
typename T_Func,
typename T_Type,
typename T_InputStore = std::vector<T_Type>,
132 typename T_OutputStore = std::vector<T_Type>>
148 template<
typename VecX>
151 for (
size_t j = 0; j < locX.size(); j += 1) {
152 this->
x[j] = locX[j];
157 template<
typename VecY>
160 for (
size_t i = 0; i < locY.size(); i += 1) {
166 template<
typename VecX,
typename VecY>
178 template<
typename VecX,
typename Jac,
typename VecY>
185 size_t constexpr VectorSizeFirstOrder = GradientTraits1st::dim;
187 for (
size_t j = 0; j < locX.size(); j += VectorSizeFirstOrder) {
188 for (
size_t vecPos = 0; vecPos < VectorSizeFirstOrder && j + vecPos < locX.size(); vecPos += 1) {
189 GradientTraits1st::at(this->
x[j + vecPos].gradient(), vecPos) = 1.0;
198 for (
size_t i = 0; i < this->
y.size(); i += 1) {
199 for (
size_t vecPos = 0; vecPos < VectorSizeFirstOrder && j + vecPos < locX.size(); vecPos += 1) {
200 wrapper(i, j + vecPos) = GradientTraits1st::at(this->
y[i].gradient(), vecPos);
204 for (
size_t vecPos = 0; vecPos < VectorSizeFirstOrder && j + vecPos < locX.size(); vecPos += 1) {
205 GradientTraits1st::at(this->
x[j + vecPos].gradient(), vecPos) = 0.0;
214 template<
typename VecX,
typename Hes,
typename VecY,
typename Jac>
219 size_t constexpr VectorSizeFirstOrder = GradientTraits1st::dim;
222 size_t constexpr VectorSizeSecondOrder = GradientTraits2nd::dim;
224 for (
size_t k = 0; k < locX.size(); k += VectorSizeFirstOrder) {
226 for (
size_t vecPos = 0; vecPos < VectorSizeFirstOrder && k + vecPos < locX.size(); vecPos += 1) {
227 GradientTraits1st::at(this->
x[k + vecPos].gradient(), vecPos).value() = 1.0;
231 for (
size_t j = k; j < locX.size(); j += VectorSizeSecondOrder) {
233 for (
size_t vecPos = 0; vecPos < VectorSizeSecondOrder && j + vecPos < locX.size(); vecPos += 1) {
234 GradientTraits2nd::at(this->
x[j + vecPos].value().gradient(), vecPos) = 1.0;
239 if (0 == j && 0 == k) {
244 for (
size_t i = 0; i < this->
y.size(); i += 1) {
245 for (
size_t vecPos1st = 0; vecPos1st < VectorSizeFirstOrder && k + vecPos1st < locX.size();
247 for (
size_t vecPos2nd = 0; vecPos2nd < VectorSizeSecondOrder && j + vecPos2nd < locX.size();
249 auto& firstGrad = GradientTraits1st::at(this->
y[i].gradient(), vecPos1st);
250 auto& secondGrad = GradientTraits2nd::at(firstGrad.gradient(), vecPos2nd);
252 hes(i, j + vecPos2nd, k + vecPos1st) = secondGrad;
253 hes(i, k + vecPos1st, j + vecPos2nd) = secondGrad;
258 for (
size_t vecPos = 0; vecPos < VectorSizeSecondOrder && j + vecPos < locX.size(); vecPos += 1) {
259 jac(i, j + vecPos) = GradientTraits2nd::at(this->
y[i].value().gradient(), vecPos);
265 for (
size_t vecPos = 0; vecPos < VectorSizeSecondOrder && j + vecPos < locX.size(); vecPos += 1) {
266 GradientTraits2nd::at(this->
x[j + vecPos].value().gradient(), vecPos) = 0.0;
271 for (
size_t vecPos = 0; vecPos < VectorSizeFirstOrder && k + vecPos < locX.size(); vecPos += 1) {
272 GradientTraits1st::at(this->
x[k + vecPos].gradient(), vecPos).value() = 0.0;
281 template<
typename T_Func,
typename T_Type,
typename T_InputStore = std::vector<T_Type>,
282 typename T_OutputStore = std::vector<T_Type>>
305 template<
typename VecX>
308 for (
size_t j = 0; j < locX.size(); j += 1) {
309 this->
x[j] = locX[j];
312 th.registerInput(this->
x[j]);
318 template<
typename VecY>
321 for (
size_t i = 0; i < this->
y.size(); i += 1) {
323 th.registerOutput(this->
y[i]);
331 template<
typename VecX,
typename VecY>
345 template<
typename VecX,
typename Jac,
typename VecY>
349 th.evalJacobian(jac);
353 template<
typename VecX,
typename Hes,
typename VecY,
typename Jac>
359 template<
typename VecX,
typename VecY>
381 template<
typename T_Func,
typename T_Type,
typename T_InputStore = std::vector<T_Type>,
382 typename T_OutputStore = std::vector<T_Type>>
402 template<
typename VecX,
typename Hes,
typename VecY,
typename Jac>
406 this->
th.evalHessian(hes, jac);
418 template<
typename T_Func,
typename T_Type,
typename T_InputStore = std::vector<T_Type>,
419 typename T_OutputStore = std::vector<T_Type>>
438 template<
typename VecX,
typename Hes,
typename VecY,
typename Jac>
449 template<
typename T_Func,
typename T_Type,
typename T_InputStore = std::vector<T_Type>,
450 typename T_OutputStore = std::vector<T_Type>,
typename =
void>
454 template<
typename T_Func,
typename T_Type,
typename T_InputStore,
typename T_OutputStore>
456 TapeTraits::EnableIfForwardTape<typename T_Type::Tape>>
462 template<
typename T_Func,
typename T_Type,
typename T_InputStore,
typename T_OutputStore>
464 TapeTraits::EnableIfJacobianTape<typename T_Type::Tape>>
467 T_OutputStore>::EvaluationHandleReverseJacobianTapes;
471 template<
typename T_Func,
typename T_Type,
typename T_InputStore,
typename T_OutputStore>
473 TapeTraits::EnableIfPrimalValueTape<typename T_Type::Tape>>
476 T_OutputStore>::EvaluationHandleReversePrimalValueTapes;
569 template<
typename VecIn,
typename VecOut>
582 template<
typename Func>
587 template<
typename Func>
594 template<
typename Func,
size_t m,
size_t n>
597 std::array<JacobianComputationType, m>>;
603 template<
typename Func,
size_t m,
size_t n>
606 std::array<HessianComputationType, m>>;
617 template<
typename Func>
632 template<
size_t m,
size_t n,
typename Func>
647 template<
typename Func>
662 template<
size_t m,
size_t n,
typename Func>
684 template<
typename Type,
typename Func>
706 template<
typename Type,
size_t m,
size_t n,
typename Func>
734 template<
typename Type,
typename InputStore,
typename OutputStore,
typename Func>
748 template<
typename T =
double>
760 template<
size_t m,
size_t n,
typename T =
double>
773 template<
typename T =
double>
785 template<
size_t m,
size_t n,
typename T =
double>
802 template<
typename Func,
typename VecX,
typename VecY>
820 template<
typename Func,
typename VecX,
typename Jac>
838 template<
typename Func,
typename VecX,
typename Hes>
858 template<
typename Func,
typename VecX,
typename VecY,
typename Jac>
878 template<
typename Func,
typename VecX,
typename VecY,
typename Hes>
900 template<
typename Func,
typename VecX,
typename VecY,
typename Jac,
typename Hes>
920 template<
typename Func,
typename VecX,
typename Jac,
typename Hes>
938 template<
typename Handle,
typename VecX,
typename VecY>
940 handle.computePrimal(x, y);
954 template<
typename Handle,
typename VecX,
typename Jac>
957 handle.computeJacobian(x, jac, dv);
971 template<
typename Handle,
typename VecX,
typename Hes>
975 handle.computeHessian(x, hes, dv, dj);
992 template<
typename Handle,
typename VecX,
typename VecY,
typename Jac>
994 handle.computeJacobian(x, jac, y);
1011 template<
typename Handle,
typename VecX,
typename VecY,
typename Hes>
1014 handle.computeHessian(x, hes, y, dj);
1033 template<
typename Handle,
typename VecX,
typename VecY,
typename Jac,
typename Hes>
1036 handle.computeHessian(x, hes, y, jac);
1054 template<
typename Handle,
typename VecX,
typename Jac,
typename Hes>
1057 handle.computeHessian(x, hes, dv, jac);
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:432
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
PassiveReal< Type > const & getPassiveValue(Type const &v)
Get the basic primal value of the type.
Definition realTraits.hpp:127
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
V constructVector(size_t const size)
Helper for the construction of vector types provided by the user.
Definition constructVector.hpp:83
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
static void computeHessian(Func func, VecIn &input, VecOut &output, Hes &hes, Jac &jac=StaticDummy< DummyJacobian >::dummy)
Compute the Hessian with multiple tape recordings and sweeps.
Definition algorithms.hpp:402
Fixed size vector mode implementation.
Definition direction.hpp:57
Dummy Jacobian. Has size zero and no logic in any call.
Definition jacobian.hpp:219
Dummy vector that provides a dummy element access and size function.
Definition dummy.hpp:54
Basic interface and data storage for all EvaluationHandle implementations.
Definition evaluationHelper.hpp:66
void eval()
Helper for the evaluation of the function object with the internal input and output vector.
Definition evaluationHelper.hpp:123
T_OutputStore OutputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:73
void setPrimalInputs(VecX const &locX)
Set the primal values from the user provided vector into the CoDiPack ones.
T_InputStore InputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:72
size_t m
Size of the output vector.
Definition evaluationHelper.hpp:77
void computePrimal(VecX const &locX, VecY &locY)
Perform a primal evaluation with the inputs provided in locX and store the result in locY.
void computeHessian(VecX const &locX, Hes &hes, VecY &locY, Jac &jac)
Perform a Hessian evaluation with the inputs provided in locX and store the result in hes,...
size_t n
Size of the input vector.
Definition evaluationHelper.hpp:78
OutputStore y
Storage for the output arguments.
Definition evaluationHelper.hpp:83
T_Func Func
See EvaluationHandleBase.
Definition evaluationHelper.hpp:70
DummyJacobian dummyJacobian
Used if no output is required.
Definition evaluationHelper.hpp:86
void getPrimalOutputs(VecY &locY)
Store the primal values from the CoDiPack vector into the user vector.
Func & func
The function object for the evaluations.
Definition evaluationHelper.hpp:80
DummyVector dummyVector
Used if no output is required.
Definition evaluationHelper.hpp:85
EvaluationHandleBase(Func &func, size_t m, size_t n)
Constructor.
Definition evaluationHelper.hpp:91
T_Type Type
See EvaluationHandleBase.
Definition evaluationHelper.hpp:71
InputStore x
Storage for the input arguments.
Definition evaluationHelper.hpp:82
void computeJacobian(VecX const &locX, Jac &jac, VecY &locY)
Perform a Jacobian evaluation with the inputs provided in locX and store the result in jac and locY.
Definition evaluationHelper.hpp:133
void computeHessian(VecX const &locX, Hes &hes, VecY &locY, Jac &jac)
Perform a Hessian evaluation with the inputs provided in locX and store the result in hes,...
Definition evaluationHelper.hpp:215
T_Func Func
See EvaluationHandleBase.
Definition evaluationHelper.hpp:137
T_OutputStore OutputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:140
T_InputStore InputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:139
void getPrimalOutputs(VecY &locY)
Store the primal values from the CoDiPack vector into the user vector.
Definition evaluationHelper.hpp:158
void computeJacobian(VecX const &locX, Jac &jac, VecY &locY)
Perform a Jacobian evaluation with the inputs provided in locX and store the result in jac and locY.
Definition evaluationHelper.hpp:179
T_Type Type
See EvaluationHandleBase.
Definition evaluationHelper.hpp:138
void setPrimalInputs(VecX const &locX)
Set the primal values from the user provided vector into the CoDiPack ones.
Definition evaluationHelper.hpp:149
void computePrimal(VecX const &locX, VecY &locY)
Perform a primal evaluation with the inputs provided in locX and store the result in locY.
Definition evaluationHelper.hpp:167
Implementation for reverse mode CoDiPack types of EvaluationHandleBase.
Definition evaluationHelper.hpp:283
EvaluationHandleReverseBase(Func &func, size_t m, size_t n)
Constructor.
Definition evaluationHelper.hpp:302
T_Func Func
See EvaluationHandleBase.
Definition evaluationHelper.hpp:287
T_Type Type
See EvaluationHandleBase.
Definition evaluationHelper.hpp:289
void setPrimalInputs(VecX const &locX, bool reg)
Set the primal values from the user provided vector into the CoDiPack ones.
Definition evaluationHelper.hpp:306
TapeHelper< Type > th
Manages the evaluations.
Definition evaluationHelper.hpp:297
T_OutputStore OutputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:291
void computePrimal(VecX const &locX, VecY &locY)
Perform a primal evaluation with the inputs provided in locX and store the result in locY.
Definition evaluationHelper.hpp:332
void computeHessian(VecX const &locX, Hes &hes, VecY &locY, Jac &jac)
Perform a Hessian evaluation with the inputs provided in locX and store the result in hes,...
void computeJacobian(VecX const &locX, Jac &jac, VecY &locY)
Perform a Jacobian evaluation with the inputs provided in locX and store the result in jac and locY.
Definition evaluationHelper.hpp:346
void recordTape(VecX const &locX, VecY &locY)
Helper function that records a new tape.
Definition evaluationHelper.hpp:360
T_InputStore InputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:290
void getPrimalOutputs(VecY &locY, bool reg)
Store the primal values from the CoDiPack vector into the user vector.
Definition evaluationHelper.hpp:319
Implementation for Jacobian reverse mode CoDiPack types of EvaluationHandleBase.
Definition evaluationHelper.hpp:421
T_InputStore InputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:426
T_Func Func
See EvaluationHandleBase.
Definition evaluationHelper.hpp:424
T_OutputStore OutputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:427
T_Type Type
See EvaluationHandleBase.
Definition evaluationHelper.hpp:425
void computeHessian(VecX const &locX, Hes &hes, VecY &locY, Jac &jac)
Perform a Hessian evaluation with the inputs provided in locX and store the result in hes,...
Definition evaluationHelper.hpp:439
Implementation of EvaluationHandleBase for primal value reverse mode CoDiPack types.
Definition evaluationHelper.hpp:384
void computeHessian(VecX const &locX, Hes &hes, VecY &locY, Jac &jac)
Perform a Hessian evaluation with the inputs provided in locX and store the result in hes,...
Definition evaluationHelper.hpp:403
T_OutputStore OutputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:391
T_Type Type
See EvaluationHandleBase.
Definition evaluationHelper.hpp:389
T_InputStore InputStore
See EvaluationHandleBase.
Definition evaluationHelper.hpp:390
T_Func Func
See EvaluationHandleBase.
Definition evaluationHelper.hpp:388
See EvaluationHandleBase.
Definition evaluationHelper.hpp:451
Evaluate the primal, Jacobian and Hessian of function objects.
Definition evaluationHelper.hpp:563
static void evalJacobian(Func &func, VecX const &x, size_t const ySize, Jac &jac)
Compute the Jacobian of the function object.
Definition evaluationHelper.hpp:821
static void evalPrimalAndJacobianAndHessian(Func &func, VecX const &x, VecY &y, Jac &jac, Hes &hes)
Compute the primal result, Jacobian, and Hessian of the function object.
Definition evaluationHelper.hpp:901
static Jacobian< T > createJacobian(size_t m, size_t n)
Create a Jacobian with the given size.
Definition evaluationHelper.hpp:749
void(*)(VecIn const &x, VecOut &y) FunctorInterface
Definition evaluationHelper.hpp:570
static void evalHandlePrimalAndJacobian(Handle &handle, VecX const &x, VecY &y, Jac &jac)
Compute the primal result and Jacobian of the function object stored in the handle.
Definition evaluationHelper.hpp:993
static DefaultHandleFixed2nd< Func, m, n > createHandleDefaultFixed2nd(Func &func)
Helper function for the creation of a default second order evaluation handle with a compile time vect...
Definition evaluationHelper.hpp:663
static void evalPrimalAndHessian(Func &func, VecX const &x, VecY &y, Hes &hes)
Compute the primal result and Hessian of the function object.
Definition evaluationHelper.hpp:879
static DefaultHandle2nd< Func > createHandleDefault2nd(Func &func, size_t m, size_t n)
Helper function for the creation of a default second order evaluation handle with a variable vector s...
Definition evaluationHelper.hpp:648
static void evalHandleJacobianAndHessian(Handle &handle, VecX const &x, Jac &jac, Hes &hes)
Compute the Hessian of the evaluation procedure in the function object.
Definition evaluationHelper.hpp:1055
static DefaultHandle< Func > createHandleDefault(Func &func, size_t m, size_t n)
Helper function for the creation of a default first order evaluation handle with a variable vector si...
Definition evaluationHelper.hpp:618
static Hessian< T > createHessian(size_t m, size_t n)
Create a Hessian with the given size.
Definition evaluationHelper.hpp:774
static void evalPrimal(Func &func, VecX const &x, VecY &y)
Perform a primal evaluation of the function object with the default first order type.
Definition evaluationHelper.hpp:803
static void evalHandleHessian(Handle &handle, VecX const &x, Hes &hes)
Compute the Hessian of the function object stored in the handle.
Definition evaluationHelper.hpp:972
static void evalHandlePrimalAndJacobianAndHessian(Handle &handle, VecX const &x, VecY &y, Jac &jac, Hes &hes)
Compute the primal result, Jacobian, and Hessian of the function object.
Definition evaluationHelper.hpp:1034
static EvaluationHandle< Func, Type > createHandle(Func &func, size_t m, size_t n)
Helper function for the creation of an evaluation handle with the specified CoDiPack type and a varia...
Definition evaluationHelper.hpp:685
static void evalHessian(Func &func, VecX const &x, size_t const ySize, Hes &hes)
Compute the Hessian of the function object.
Definition evaluationHelper.hpp:839
static Hessian< T, std::array< T, m *n *n > > createHessianFixed()
Create a Hessian with a compile time size.
Definition evaluationHelper.hpp:786
static void evalHandlePrimal(Handle &handle, VecX const &x, VecY &y)
Perform a primal evaluation of the function object stored in the handle.
Definition evaluationHelper.hpp:939
static void evalPrimalAndJacobian(Func &func, VecX const &x, VecY &y, Jac &jac)
Compute the primal result and Jacobian of the function object.
Definition evaluationHelper.hpp:859
static EvaluationHandle< Func, Type, InputStore, OutputStore > createHandleFull(Func &func, size_t m, size_t n)
Helper function for the creation of an evaluation handle with the specified CoDiPack type and storage...
Definition evaluationHelper.hpp:735
static void evalHandlePrimalAndHessian(Handle &handle, VecX const &x, VecY &y, Hes &hes)
Compute the primal result and Hessian of the function object.
Definition evaluationHelper.hpp:1012
static EvaluationHandle< Func, Type, std::array< Type, n >, std::array< Type, m > > createHandleFixed(Func &func)
Helper function for the creation of an evaluation handle with the specified CoDiPack type and a compi...
Definition evaluationHelper.hpp:707
static DefaultHandleFixed< Func, m, n > createHandleDefaultFixed(Func &func)
Helper function for the creation of a default first order evaluation handle with a compile time vecto...
Definition evaluationHelper.hpp:633
static void evalJacobianAndHessian(Func &func, VecX const &x, size_t ySize, Jac &jac, Hes &hes)
Compute the Jacobian and Hessian of the function object.
Definition evaluationHelper.hpp:921
static void evalHandleJacobian(Handle &handle, VecX const &x, Jac &jac)
Compute the Jacobian of the function object stored in the handle.
Definition evaluationHelper.hpp:955
static Jacobian< T, std::array< T, m *n > > createJacobianFixed()
Create a Jacobian with a compile time size.
Definition evaluationHelper.hpp:761
Common traits for all types used as gradients.
Definition gradientTraits.hpp:64
Default implementation of the Hessian interface.
Definition hessian.hpp:59
Wrapper for JacboianInterfaces that requires a passive value conversion.
Definition jacobian.hpp:186
Default implementation of the Jacobian interface.
Definition jacobian.hpp:60
See TapeHelperBase.
Definition tapeHelper.hpp:642