Skip to content

Commit a05717c

Browse files
committed
[codegen/cg] define CGVisitor
1 parent c6cda31 commit a05717c

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

include/pycppad/codegen/cg.hpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2021 INRIA
3+
*/
4+
5+
#ifndef __pycppad_codegen_cg_hpp__
6+
#define __pycppad_codegen_cg_hpp__
7+
8+
#include <cppad/cg/cppadcg.hpp>
9+
10+
#include "eigenpy/user-type.hpp"
11+
#include "eigenpy/ufunc.hpp"
12+
13+
14+
namespace pycppad
15+
{
16+
namespace codegen {
17+
18+
namespace bp = boost::python;
19+
20+
template<typename Scalar>
21+
class CGVisitor
22+
: public bp::def_visitor< CGVisitor<Scalar> >
23+
{
24+
public:
25+
typedef ::CppAD::cg::CG<Scalar> CG;
26+
27+
template<class PyClass>
28+
void visit(PyClass& cl) const
29+
{
30+
cl
31+
.def(bp::init<>(bp::arg("self"),"Default constructor"))
32+
.def(bp::init<Scalar>(bp::args("self","value"),
33+
std::string("Constructor from a ").append(typeid(Scalar).name()).c_str()))
34+
.def(bp::init<CG>(bp::args("self","other"),"Copy constructor"))
35+
.def("isIdenticalZero", &CG::isIdenticalZero, bp::arg("self"))
36+
.def("isIdenticalOne", &CG::isIdenticalOne, bp::arg("self"))
37+
.def("isValueDefined", &CG::isValueDefined, bp::arg("self"))
38+
.def("isParameter", &CG::isParameter, bp::arg("self"))
39+
.def("isVariable", &CG::isVariable, bp::arg("self"))
40+
41+
.def(bp::self + bp::self)
42+
.def(bp::self - bp::self)
43+
.def(bp::self * bp::self)
44+
.def(bp::self / bp::self)
45+
.def(bp::self += bp::self)
46+
#ifdef __clang__
47+
#pragma GCC diagnostic push
48+
#pragma GCC diagnostic ignored "-Wself-assign-overloaded"
49+
#endif
50+
.def(bp::self /= bp::self)
51+
.def(bp::self -= bp::self) // See https://bugs.llvm.org/show_bug.cgi?id=43124 for the bug
52+
#ifdef __clang__
53+
#pragma GCC diagnostic pop
54+
#endif
55+
.def(bp::self *= bp::self)
56+
.add_property("value",
57+
bp::make_function(&CG::getValue,
58+
bp::return_value_policy<bp::copy_const_reference>()),
59+
&CG::setValue)
60+
.def("__str__",&print)
61+
.def("__repr__",&print)
62+
.add_property("__float__",
63+
bp::make_function(&CG::getValue,
64+
bp::return_value_policy<bp::copy_const_reference>()),
65+
&CG::setValue)
66+
.def("__int__",&__int__)
67+
;
68+
}
69+
70+
private:
71+
72+
static std::string print(const CG & self)
73+
{
74+
std::stringstream ss;
75+
ss << get_class_name() << "(" << self <<")";
76+
return ss.str();
77+
}
78+
79+
static int64_t __int__(const CG & self)
80+
{
81+
return static_cast<int>(self.getValue());
82+
}
83+
84+
protected:
85+
86+
static std::string & get_class_name()
87+
{
88+
static std::string class_name;
89+
return class_name;
90+
}
91+
92+
static void set_class_name(const std::string & class_name)
93+
{
94+
get_class_name() = class_name;
95+
}
96+
97+
public:
98+
99+
static void expose(const std::string & class_name = "CG")
100+
{
101+
set_class_name(class_name);
102+
bp::class_<CG>(class_name.c_str(),
103+
std::string("CG type corresponding to the scalar type ").append(typeid(Scalar).name()).c_str(),
104+
bp::no_init)
105+
.def(CGVisitor<Scalar>());
106+
107+
eigenpy::registerNewType<CG>();
108+
eigenpy::registerCommonUfunc<CG>();
109+
}
110+
};
111+
112+
}
113+
}
114+
#endif //#ifndef __pycppad_codegen_cg_hpp__

0 commit comments

Comments
 (0)