5 #ifndef __pycppad_codegen_cg_hpp__
6 #define __pycppad_codegen_cg_hpp__
8 #include <cppad/cg/cppadcg.hpp>
10 #include <eigenpy/user-type.hpp>
11 #include <eigenpy/ufunc.hpp>
17 template <
typename Scalar,
typename To>
18 struct cast<::CppAD::cg::CG<Scalar>, To>
20 typedef ::CppAD::cg::CG<Scalar>
From;
22 return ::pycppad::internal::Cast<From, To>::run(from);
26 template <
typename From,
typename Scalar>
27 struct cast<From,::CppAD::cg::CG<Scalar>>
29 typedef ::CppAD::cg::CG<Scalar>
To;
30 static To run(
const From & from) {
31 return To(
static_cast<Scalar
>(from));
37 template <
typename Scalar>
38 struct getitem<::CppAD::cg::CG<Scalar>> {
40 typedef ::CppAD::cg::CG<Scalar>
CG;
42 static PyObject*
run(
void* data,
void* ) {
43 CG & cg = *
static_cast<CG*
>(data);
45 if(!cg.isValueDefined())
46 cg.setValue(
static_cast<Scalar
>(0));
62 template<
typename Scalar>
65 static const Scalar &
get(const ::CppAD::AD<::CppAD::cg::CG<Scalar>> & v)
67 return ::CppAD::Value<::CppAD::cg::CG<Scalar>>(v).getValue();
71 template<
typename Scalar,
typename To>
72 struct Cast<::CppAD::cg::CG<Scalar>,To>
74 typedef ::CppAD::cg::CG<Scalar>
From;
77 return static_cast<To
>(::CppAD::Value<From>(from).getValue());
85 namespace bp = boost::python;
87 template<
typename Scalar>
89 :
public bp::def_visitor< CGVisitor<Scalar> >
92 typedef ::CppAD::cg::CG<Scalar>
CG;
94 template<
class PyClass>
98 .def(bp::init<>(bp::arg(
"self"),
"Default constructor"))
99 .def(bp::init<Scalar>(bp::args(
"self",
"value"),
100 std::string(
"Constructor from a ").append(bp::type_id<Scalar>().name()).c_str()))
101 .def(bp::init<CG>(bp::args(
"self",
"other"),
"Copy constructor"))
102 .def(
"isIdenticalZero", &CG::isIdenticalZero, bp::arg(
"self"))
103 .def(
"isIdenticalOne", &CG::isIdenticalOne, bp::arg(
"self"))
104 .def(
"isValueDefined", &CG::isValueDefined, bp::arg(
"self"))
105 .def(
"isParameter", &CG::isParameter, bp::arg(
"self"))
106 .def(
"isVariable", &CG::isVariable, bp::arg(
"self"))
108 .def(bp::self + bp::self)
109 .def(bp::self - bp::self)
110 .def(bp::self * bp::self)
111 .def(bp::self / bp::self)
112 .def(bp::self += bp::self)
114 #pragma GCC diagnostic push
115 #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
117 .def(bp::self /= bp::self)
118 .def(bp::self -= bp::self)
120 #pragma GCC diagnostic pop
122 .def(bp::self *= bp::self)
123 .add_property(
"value",
124 bp::make_function(&CG::getValue,
125 bp::return_value_policy<bp::copy_const_reference>()),
127 .def(
"__str__",&print)
128 .def(
"__repr__",&print)
129 .add_property(
"__float__",
130 bp::make_function(&CG::getValue,
131 bp::return_value_policy<bp::copy_const_reference>()),
139 static std::string print(
const CG &
self)
141 std::stringstream ss;
150 static std::string class_name;
161 static void expose(
const std::string & class_name =
"CG")
164 bp::class_<CG>(class_name.c_str(),
165 std::string(
"CG type corresponding to the scalar type ").append(bp::type_id<Scalar>().name()).c_str(),
169 eigenpy::registerNewType<CG>();
170 eigenpy::registerCommonUfunc<CG>();
172 eigenpy::registerCast<CG,double>(
false);
173 eigenpy::registerCast<double,CG>(
true);
174 eigenpy::registerCast<CG,float>(
false);
175 eigenpy::registerCast<float,CG>(
true);
176 eigenpy::registerCast<CG,long>(
false);
177 eigenpy::registerCast<long,CG>(
true);
178 eigenpy::registerCast<CG,int>(
false);
179 eigenpy::registerCast<int,CG>(
true);
static void set_class_name(const std::string &class_name)
Definition: cg.hpp:154
::CppAD::cg::CG< Scalar > CG
Definition: cg.hpp:92
void visit(PyClass &cl) const
Definition: cg.hpp:95
static std::string & get_class_name()
Definition: cg.hpp:148
static void expose(const std::string &class_name="CG")
Definition: cg.hpp:161
static To run(const From &from)
Definition: cg.hpp:30
::CppAD::cg::CG< Scalar > To
Definition: cg.hpp:29
static To run(const From &from)
Definition: cg.hpp:21
::CppAD::cg::CG< Scalar > From
Definition: cg.hpp:20
static PyObject * run(void *data, void *)
Definition: cg.hpp:42
::CppAD::cg::CG< Scalar > CG
Definition: cg.hpp:40
static To run(const From &from)
Definition: cg.hpp:75
::CppAD::cg::CG< Scalar > From
Definition: cg.hpp:74
static const Scalar & get(const ::CppAD::AD<::CppAD::cg::CG< Scalar >> &v)
Definition: cg.hpp:65