From 3a25234e00af84be7d8401028f061395f2f6af70 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Sun, 22 May 2022 20:24:32 +0200 Subject: [PATCH 1/4] core: specialized cast helper --- include/pycppad/cast.hpp | 26 +++++++++++++++++++++++++- include/pycppad/codegen/cg.hpp | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/include/pycppad/cast.hpp b/include/pycppad/cast.hpp index 7cd2b19..b679cf1 100644 --- a/include/pycppad/cast.hpp +++ b/include/pycppad/cast.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2021 INRIA + * Copyright 2021-2022 INRIA */ #ifndef __pycppad_cast_hpp__ @@ -7,6 +7,8 @@ #include "pycppad/fwd.hpp" +#include + namespace pycppad { namespace internal @@ -42,4 +44,26 @@ namespace pycppad } // namespace internal } // namespace pycppad +namespace eigenpy { + +template +struct cast<::CppAD::AD, To> +{ + typedef ::CppAD::AD From; + static To run(const From & from) { + return ::pycppad::internal::Cast::run(from); + } +}; + +template +struct cast> +{ + typedef ::CppAD::AD To; + static To run(const From & from) { + return To(static_cast(from)); + } +}; + +} // namespace eigenpy + #endif //#ifndef __pycppad_cast_hpp__ diff --git a/include/pycppad/codegen/cg.hpp b/include/pycppad/codegen/cg.hpp index 37700ff..69a8144 100644 --- a/include/pycppad/codegen/cg.hpp +++ b/include/pycppad/codegen/cg.hpp @@ -12,6 +12,28 @@ #include "pycppad/cast.hpp" +namespace eigenpy { + +template +struct cast<::CppAD::cg::CG, To> +{ + typedef ::CppAD::cg::CG From; + static To run(const From & from) { + return ::pycppad::internal::Cast::run(from); + } +}; + +template +struct cast> +{ + typedef ::CppAD::cg::CG To; + static To run(const From & from) { + return To(static_cast(from)); + } +}; + +} // namespace eigenpy + namespace pycppad { namespace internal From 2a7c119ed017b16c982a2d549eb3411f8fb8f82e Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Sun, 22 May 2022 20:24:47 +0200 Subject: [PATCH 2/4] core: registerCast --- include/pycppad/ad.hpp | 9 +++++++++ include/pycppad/codegen/cg.hpp | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/include/pycppad/ad.hpp b/include/pycppad/ad.hpp index e2a5680..95dca06 100644 --- a/include/pycppad/ad.hpp +++ b/include/pycppad/ad.hpp @@ -116,6 +116,15 @@ namespace pycppad eigenpy::registerNewType(); eigenpy::registerCommonUfunc(); + + eigenpy::registerCast(false); + eigenpy::registerCast(true); + eigenpy::registerCast(false); + eigenpy::registerCast(true); + eigenpy::registerCast(false); + eigenpy::registerCast(true); + eigenpy::registerCast(false); + eigenpy::registerCast(true); } }; } diff --git a/include/pycppad/codegen/cg.hpp b/include/pycppad/codegen/cg.hpp index 69a8144..2bc6944 100644 --- a/include/pycppad/codegen/cg.hpp +++ b/include/pycppad/codegen/cg.hpp @@ -148,6 +148,15 @@ namespace pycppad eigenpy::registerNewType(); eigenpy::registerCommonUfunc(); + + eigenpy::registerCast(false); + eigenpy::registerCast(true); + eigenpy::registerCast(false); + eigenpy::registerCast(true); + eigenpy::registerCast(false); + eigenpy::registerCast(true); + eigenpy::registerCast(false); + eigenpy::registerCast(true); } }; From d3708f942f852546674d789b959a4a9061bedb7d Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Sun, 22 May 2022 20:52:51 +0200 Subject: [PATCH 3/4] core/cg: template specialization of getitem --- include/pycppad/codegen/cg.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/pycppad/codegen/cg.hpp b/include/pycppad/codegen/cg.hpp index 2bc6944..b05962c 100644 --- a/include/pycppad/codegen/cg.hpp +++ b/include/pycppad/codegen/cg.hpp @@ -32,6 +32,26 @@ struct cast> } }; +namespace internal { + +template +struct getitem<::CppAD::cg::CG> { + + typedef ::CppAD::cg::CG CG; + + static PyObject* run(void* data, void* /* arr */) { + CG & cg = *static_cast(data); + + if(!cg.isValueDefined()) // not initialized + cg.setValue(static_cast(0)); + + bp::object m(cg); + Py_INCREF(m.ptr()); + return m.ptr(); + } +}; + +} // namespace internal } // namespace eigenpy namespace pycppad From 57a7e7d1a2b55f93beb870d56024da150a22fc3a Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Mon, 23 May 2022 10:56:02 +0200 Subject: [PATCH 4/4] cmake: set minimal eigenpy version --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8ae782..a52a4b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2021 INRIA +# Copyright (c) 2021-2022 INRIA # CMAKE_MINIMUM_REQUIRED(VERSION 3.1) @@ -51,7 +51,7 @@ ENDIF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS) ADD_PROJECT_DEPENDENCY(cppad 20180000.0 REQUIRED PKG_CONFIG_REQUIRES "cppad >= 20180000.0") ADD_PROJECT_DEPENDENCY(Eigen3 REQUIRED PKG_CONFIG_REQUIRES "eigen3 >= 3.0.5") -ADD_PROJECT_DEPENDENCY(eigenpy 2.6.6 REQUIRED) +ADD_PROJECT_DEPENDENCY(eigenpy 2.7.6 REQUIRED) SET(${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/fwd.hpp