Loading...
Searching...
No Matches
ad_fun.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2021-2022 INRIA
3 */
4
5#ifndef __pycppad_ad_fun_hpp__
6#define __pycppad_ad_fun_hpp__
7
8#include "pycppad/fwd.hpp"
9
10namespace pycppad
11{
12 namespace bp = boost::python;
13
14 template<typename Scalar>
16 : public bp::def_visitor< ADFunVisitor<Scalar> >
17 {
18
19 typedef ::CppAD::ADFun<Scalar> ADFun;
20 typedef Eigen::Matrix<::CppAD::AD<Scalar>, Eigen::Dynamic, 1> ADVector;
21 typedef Eigen::Ref<ADVector> RefADVector;
22 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> Vector;
23
24 public:
25
26 static Vector Forward_multiple(ADFun& f, size_t q , const Vector&xq) {
27 return f.Forward(q, xq);
28 }
29
30 template<class PyClass>
31 void visit(PyClass& cl) const
32 {
33
34 cl
35 .def("__init__",
36 bp::make_constructor(&constructor,bp::default_call_policies(),bp::args("x","y")))
37 //.def("swap", &ADFun::swap, bp::args("self", "f"))
38 .def("from_json", &ADFun::from_json, bp::args("self", "json"))
39 //.def("from_graph", &ADFun::from_graph, bp::args("self", "graph_obj"))
40 //.def("to_json", &ADFun::to_json, bp::arg("self"))
41 .def("size_order", &ADFun::size_order, bp::arg("self"))
42 .def("Dependent",&Dependent,
43 bp::args("self", "x", "y"))
44 .def("Forward", (Vector (ADFun::*)(size_t , size_t, const Vector&))(&ADFun::Forward),
45 bp::args("self", "q", "r", "x"))
46 .def("Forward", &Forward_multiple,
47 bp::args("self", "q", "xq"))
48 .def("Reverse", (Vector (ADFun::*)(size_t, const Vector&))(&ADFun::Reverse),
49 bp::args("self", "p", "v"))
50 .def("Reverse", (Vector (ADFun::*)(size_t, const Vector&))(&ADFun::Reverse),
51 bp::args("self", "p", "v"))
52 .def("Hessian", (Vector (ADFun::*)(const Vector&,const Vector&))(&ADFun::Hessian),
53 bp::args("self", "x", "w"))
54 .def("Hessian", (Vector (ADFun::*)(const Vector&, size_t))(&ADFun::Hessian),
55 bp::args("self", "x", "i"))
56 .def("Jacobian", (Vector (ADFun::*)(const Vector&))(&ADFun::Jacobian),
57 bp::args("self", "x"))
58 .def("optimize", &ADFun::optimize, bp::args("self", "options"))
59 ;
60 }
61
62 private:
63
64 static void Dependent(ADFun & self, RefADVector x, RefADVector y)
65 {
66 ADVector x_(x),y_(y);
67 self.Dependent(x_,y_);
68 x = x_; y = y_;
69 }
70
71 static ADFun* constructor(RefADVector x, RefADVector y)
72 {
73 ADVector x_(x),y_(y);
74 ADFun * f = new ADFun(x_,y_);
75 x = x_; y = y_;
76 return f;
77 }
78
79 protected:
80
81 static std::string & get_class_name()
82 {
83 static std::string class_name;
84 return class_name;
85 }
86
87 static void set_class_name(const std::string & class_name)
88 {
89 get_class_name() = class_name;
90 }
91
92
93 public:
94 static void expose(const std::string & class_name = "ADFun")
95 {
96 set_class_name(class_name);
97 bp::class_<ADFun, boost::noncopyable>(class_name.c_str(),
98 "Class used to hold function objects.\n\n",
99 bp::init<>())
100 .def(ADFunVisitor<Scalar>());
101
102 }
103 };
104}
105#endif //#ifndef __pycppad_ad_hpp__
Definition ad_fun.hpp:17
static void expose(const std::string &class_name="ADFun")
Definition ad_fun.hpp:94
void visit(PyClass &cl) const
Definition ad_fun.hpp:31
static Vector Forward_multiple(ADFun &f, size_t q, const Vector &xq)
Definition ad_fun.hpp:26
static void set_class_name(const std::string &class_name)
Definition ad_fun.hpp:87
static std::string & get_class_name()
Definition ad_fun.hpp:81
Definition ad.hpp:15