Skip to content

Commit b13e04e

Browse files
authored
Merge pull request #22 from jcarpent/devel
Fix cast and getitem for CppAD::cg::CG
2 parents 6b97b07 + 57a7e7d commit b13e04e

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2021 INRIA
2+
# Copyright (c) 2021-2022 INRIA
33
#
44

55
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
@@ -51,7 +51,7 @@ ENDIF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS)
5151

5252
ADD_PROJECT_DEPENDENCY(cppad 20180000.0 REQUIRED PKG_CONFIG_REQUIRES "cppad >= 20180000.0")
5353
ADD_PROJECT_DEPENDENCY(Eigen3 REQUIRED PKG_CONFIG_REQUIRES "eigen3 >= 3.0.5")
54-
ADD_PROJECT_DEPENDENCY(eigenpy 2.6.6 REQUIRED)
54+
ADD_PROJECT_DEPENDENCY(eigenpy 2.7.6 REQUIRED)
5555

5656
SET(${PROJECT_NAME}_HEADERS
5757
include/${PROJECT_NAME}/fwd.hpp

include/pycppad/ad.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ namespace pycppad
116116

117117
eigenpy::registerNewType<AD>();
118118
eigenpy::registerCommonUfunc<AD>();
119+
120+
eigenpy::registerCast<AD,double>(false);
121+
eigenpy::registerCast<double,AD>(true);
122+
eigenpy::registerCast<AD,float>(false);
123+
eigenpy::registerCast<float,AD>(true);
124+
eigenpy::registerCast<AD,long>(false);
125+
eigenpy::registerCast<long,AD>(true);
126+
eigenpy::registerCast<AD,int>(false);
127+
eigenpy::registerCast<int,AD>(true);
119128
}
120129
};
121130
}

include/pycppad/cast.hpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/*
2-
* Copyright 2021 INRIA
2+
* Copyright 2021-2022 INRIA
33
*/
44

55
#ifndef __pycppad_cast_hpp__
66
#define __pycppad_cast_hpp__
77

88
#include "pycppad/fwd.hpp"
99

10+
#include <eigenpy/user-type.hpp>
11+
1012
namespace pycppad
1113
{
1214
namespace internal
@@ -42,4 +44,26 @@ namespace pycppad
4244
} // namespace internal
4345
} // namespace pycppad
4446

47+
namespace eigenpy {
48+
49+
template <typename Scalar, typename To>
50+
struct cast<::CppAD::AD<Scalar>, To>
51+
{
52+
typedef ::CppAD::AD<Scalar> From;
53+
static To run(const From & from) {
54+
return ::pycppad::internal::Cast<From, To>::run(from);
55+
}
56+
};
57+
58+
template <typename From, typename Scalar>
59+
struct cast<From,::CppAD::AD<Scalar>>
60+
{
61+
typedef ::CppAD::AD<Scalar> To;
62+
static To run(const From & from) {
63+
return To(static_cast<Scalar>(from));
64+
}
65+
};
66+
67+
} // namespace eigenpy
68+
4569
#endif //#ifndef __pycppad_cast_hpp__

include/pycppad/codegen/cg.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,48 @@
1212

1313
#include "pycppad/cast.hpp"
1414

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+
1557
namespace pycppad
1658
{
1759
namespace internal
@@ -126,6 +168,15 @@ namespace pycppad
126168

127169
eigenpy::registerNewType<CG>();
128170
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);
129180
}
130181
};
131182

0 commit comments

Comments
 (0)