Skip to content

Commit e2e1d1b

Browse files
authored
Merge pull request #4 from proyan/devel
Extend support for cppad-codegen
2 parents 8d0ffcc + 177a135 commit e2e1d1b

File tree

7 files changed

+82
-14
lines changed

7 files changed

+82
-14
lines changed

.github/workflows/cmake.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ jobs:
1919

2020
- name: Install Dependencies
2121
run: |
22-
sudo apt install libboost-all-dev libeigen3-dev
22+
sudo apt install libboost-python-dev libeigen3-dev
2323
. /etc/os-release
2424
sudo tee /etc/apt/sources.list.d/robotpkg.list <<EOF
2525
deb [arch=amd64] http://robotpkg.openrobots.org/wip/packages/debian/pub $UBUNTU_CODENAME robotpkg
2626
deb [arch=amd64] http://robotpkg.openrobots.org/packages/debian/pub $UBUNTU_CODENAME robotpkg
2727
EOF
2828
curl http://robotpkg.openrobots.org/packages/debian/robotpkg.key | sudo apt-key add -
2929
sudo apt update
30-
sudo apt install robotpkg-cppad robotpkg-py38-eigenpy
30+
sudo apt install robotpkg-cppad robotpkg-cppadcodegen robotpkg-py38-eigenpy
3131
3232
- name: Configure CMake
3333
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
@@ -38,7 +38,7 @@ jobs:
3838
export PYTHONPATH=$PYTHONPATH:/opt/openrobots/lib/python3/site-packages:/opt/openrobots/lib/python3/dist-packages
3939
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/openrobots/lib64:/opt/openrobots/lib/x86_64-linux-gnu:/opt/openrobots/lib/plugin:/opt/openrobots/lib
4040
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/openrobots/lib/pkgconfig:/opt/openrobots/share/pkgconfig:/opt/openrobots/lib/x86_64-linux-gnu/pkgconfig:/opt/openrobots/lib64/pkgconfig
41-
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DPYTHON_EXECUTABLE=$(which python3) -DCMAKE_PREFIX_PATH=/opt/openrobots
41+
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DPYTHON_EXECUTABLE=$(which python3) -DCMAKE_PREFIX_PATH=/opt/openrobots -DBUILD_CODEGEN_BINDINGS=ON
4242
4343
- name: Build
4444
# Build your program with the given configuration

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ SET(${PROJECT_NAME}_SOURCES
6767
IF(BUILD_CODEGEN_BINDINGS)
6868
LIST(APPEND ${PROJECT_NAME}_HEADERS
6969
include/${PROJECT_NAME}/codegen/cg.hpp
70+
include/${PROJECT_NAME}/codegen/ad.hpp
7071
include/${PROJECT_NAME}/codegen/cppadcg-scalar.hpp
7172
)
7273
ENDIF(BUILD_CODEGEN_BINDINGS)

include/pycppad/ad.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ namespace pycppad
1313
{
1414
namespace bp = boost::python;
1515

16-
template<typename Scalar>
16+
template<typename _Scalar>
1717
class ADVisitor
18-
: public bp::def_visitor< ADVisitor<Scalar> >
18+
: public bp::def_visitor< ADVisitor<_Scalar> >
1919
{
2020
public:
21+
typedef _Scalar Scalar;
2122
typedef ::CppAD::AD<Scalar> AD;
2223

2324
template<class PyClass>

include/pycppad/ad_fun.hpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace pycppad
3737
//.def("swap", &ADFun::swap, bp::args("self", "f"))
3838
.def("from_json", &ADFun::from_json, bp::args("self", "json"))
3939
//.def("from_graph", &ADFun::from_graph, bp::args("self", "graph_obj"))
40-
.def("to_json", &ADFun::to_json, bp::arg("self"))
40+
//.def("to_json", &ADFun::to_json, bp::arg("self"))
4141
.def("size_order", &ADFun::size_order, bp::arg("self"))
4242
.def("Dependent",&Dependent,
4343
bp::args("self", "x", "y"))
@@ -74,13 +74,28 @@ namespace pycppad
7474
return f;
7575
}
7676

77+
protected:
78+
79+
static std::string & get_class_name()
80+
{
81+
static std::string class_name;
82+
return class_name;
83+
}
84+
85+
static void set_class_name(const std::string & class_name)
86+
{
87+
get_class_name() = class_name;
88+
}
89+
90+
7791
public:
78-
static void expose()
92+
static void expose(const std::string & class_name = "ADFun")
7993
{
80-
bp::class_<ADFun, boost::noncopyable>("ADFun",
81-
"Class used to hold function objects.\n\n",
82-
bp::init<>())
83-
.def(ADFunVisitor<Scalar>());
94+
set_class_name(class_name);
95+
bp::class_<ADFun, boost::noncopyable>(class_name.c_str(),
96+
"Class used to hold function objects.\n\n",
97+
bp::init<>())
98+
.def(ADFunVisitor<Scalar>());
8499

85100
}
86101
};

include/pycppad/codegen/ad.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2021 INRIA
3+
*/
4+
5+
#ifndef __pycppad_codegen_ad_hpp__
6+
#define __pycppad_codegen_ad_hpp__
7+
8+
#include "pycppad/ad.hpp"
9+
10+
namespace pycppad
11+
{
12+
13+
template<>
14+
int64_t ADVisitor<::CppAD::cg::CG<double> >::__int__(const AD & self)
15+
{
16+
return static_cast<int>(::CppAD::Value<Scalar>(self).getValue());
17+
}
18+
}
19+
20+
#endif //#ifndef __pycppad_ad_hpp__

include/pycppad/codegen/cppadcg-scalar.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <eigenpy/eigenpy.hpp>
1010

1111
#include "pycppad/codegen/cg.hpp"
12+
#include "pycppad/codegen/ad.hpp"
13+
#include "pycppad/ad.hpp"
14+
#include "pycppad/independent.hpp"
15+
#include "pycppad/ad_fun.hpp"
1216

1317

1418
namespace pycppad
@@ -19,11 +23,23 @@ namespace pycppad
1923
void exposeCppADCGScalar()
2024
{
2125
typedef ::CppAD::cg::CG<Scalar> CGScalar;
26+
typedef ::CppAD::AD<CGScalar> ADCGScalar;
27+
typedef Eigen::Matrix<ADCGScalar,Eigen::Dynamic,1> VectorADCG;
28+
typedef Eigen::Matrix<ADCGScalar,1,Eigen::Dynamic> RowVectorADCG;
2229

2330
eigenpy::exposeType<CGScalar>();
2431
eigenpy::exposeType<CGScalar,Eigen::RowMajor>();
25-
32+
33+
eigenpy::exposeType<ADCGScalar>();
34+
eigenpy::exposeType<ADCGScalar,Eigen::RowMajor>();
35+
2636
CGVisitor<Scalar>::expose();
37+
38+
39+
pycppad::ADVisitor<CGScalar>::expose("ADCG");
40+
pycppad::ADFunVisitor<CGScalar>::expose("ADCGFun");
41+
pycppad::IndependentVisitor<VectorADCG>::expose("CGIndependent");
42+
pycppad::IndependentVisitor<RowVectorADCG>::expose("CGIndependent");
2743
}
2844
}
2945
}

include/pycppad/independent.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ namespace pycppad
2424
template<class PyClass>
2525
void visit(PyClass&) const
2626
{ }
27+
28+
protected:
29+
30+
static std::string & get_class_name()
31+
{
32+
static std::string class_name;
33+
return class_name;
34+
}
35+
36+
static void set_class_name(const std::string & class_name)
37+
{
38+
get_class_name() = class_name;
39+
}
40+
2741

2842
public:
2943

@@ -39,9 +53,10 @@ namespace pycppad
3953
return;
4054
}
4155

42-
static void expose()
56+
static void expose(const std::string & class_name = "Independent")
4357
{
44-
bp::def("Independent",&Independent,
58+
set_class_name(class_name);
59+
bp::def(class_name.c_str(),&Independent,
4560
(bp::arg("x"), bp::arg("abort_op_index") = 0, bp::arg("record_compare") = true),
4661
"define a variable as Independent."
4762
"Parameters:\n"

0 commit comments

Comments
 (0)