-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcppad_interface.hpp
46 lines (35 loc) · 1.71 KB
/
cppad_interface.hpp
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
#pragma once
#include "cppad/cppad.hpp"
#include "pyoptinterface/nlexpr.hpp"
#include "pyoptinterface/nleval.hpp"
using ADFunDouble = CppAD::ADFun<double>;
ADFunDouble dense_jacobian(const ADFunDouble &f);
using sparsity_pattern_t = CppAD::sparse_rc<std::vector<size_t>>;
struct JacobianHessianSparsityPattern
{
sparsity_pattern_t jacobian;
sparsity_pattern_t hessian;
sparsity_pattern_t reduced_hessian;
};
JacobianHessianSparsityPattern jacobian_hessian_sparsity(ADFunDouble &f,
HessianSparsityType hessian_sparsity);
// [p, x] -> Jacobian
ADFunDouble sparse_jacobian(const ADFunDouble &f, const sparsity_pattern_t &pattern_jac,
const std::vector<double> &x_values,
const std::vector<double> &p_values);
// [p, w, x] -> \Sigma w_i * Hessian_i
ADFunDouble sparse_hessian(const ADFunDouble &f, const sparsity_pattern_t &pattern_hes,
const sparsity_pattern_t &pattern_subset,
const std::vector<double> &x_values,
const std::vector<double> &p_values);
// Transform ExpressionGraph to CppAD function
ADFunDouble cppad_trace_function(const ExpressionGraph &graph,
const std::vector<ExpressionHandle> &outputs);
struct CppADAutodiffGraph
{
CppAD::cpp_graph f_graph, jacobian_graph, hessian_graph;
};
// Generate computational graph for the CppAD function (itself, Jacobian and Hessian)
// Analyze its sparsity as well
void cppad_autodiff(ADFunDouble &f, AutodiffSymbolicStructure &structure, CppADAutodiffGraph &graph,
const std::vector<double> &x_values, const std::vector<double> &p_values);