|
12 | 12 |
|
13 | 13 | #include "pycppad/cast.hpp" |
14 | 14 |
|
| 15 | +namespace eigenpy { |
| 16 | + |
| 17 | +template <typename Scalar, typename To> |
| 18 | +struct cast<::CppAD::cg::CG<Scalar>, To> |
| 19 | +{ |
| 20 | + typedef ::CppAD::cg::CG<Scalar> From; |
| 21 | + static To run(const From & from) { |
| 22 | + return ::pycppad::internal::Cast<From, To>::run(from); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +template <typename From, typename Scalar> |
| 27 | +struct cast<From,::CppAD::cg::CG<Scalar>> |
| 28 | +{ |
| 29 | + typedef ::CppAD::cg::CG<Scalar> To; |
| 30 | + static To run(const From & from) { |
| 31 | + return To(static_cast<Scalar>(from)); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +namespace internal { |
| 36 | + |
| 37 | +template <typename Scalar> |
| 38 | +struct getitem<::CppAD::cg::CG<Scalar>> { |
| 39 | + |
| 40 | + typedef ::CppAD::cg::CG<Scalar> CG; |
| 41 | + |
| 42 | + static PyObject* run(void* data, void* /* arr */) { |
| 43 | + CG & cg = *static_cast<CG*>(data); |
| 44 | + |
| 45 | + if(!cg.isValueDefined()) // not initialized |
| 46 | + cg.setValue(static_cast<Scalar>(0)); |
| 47 | + |
| 48 | + bp::object m(cg); |
| 49 | + Py_INCREF(m.ptr()); |
| 50 | + return m.ptr(); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +} // namespace internal |
| 55 | +} // namespace eigenpy |
| 56 | + |
15 | 57 | namespace pycppad |
16 | 58 | { |
17 | 59 | namespace internal |
@@ -126,6 +168,15 @@ namespace pycppad |
126 | 168 |
|
127 | 169 | eigenpy::registerNewType<CG>(); |
128 | 170 | eigenpy::registerCommonUfunc<CG>(); |
| 171 | + |
| 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); |
129 | 180 | } |
130 | 181 | }; |
131 | 182 |
|
|
0 commit comments