Skip to content

Commit

Permalink
Merge pull request #22 from jcarpent/devel
Browse files Browse the repository at this point in the history
Fix cast and getitem for CppAD::cg::CG
  • Loading branch information
jcarpent authored Jun 2, 2022
2 parents 6b97b07 + 57a7e7d commit b13e04e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2021 INRIA
# Copyright (c) 2021-2022 INRIA
#

CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions include/pycppad/ad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ namespace pycppad

eigenpy::registerNewType<AD>();
eigenpy::registerCommonUfunc<AD>();

eigenpy::registerCast<AD,double>(false);
eigenpy::registerCast<double,AD>(true);
eigenpy::registerCast<AD,float>(false);
eigenpy::registerCast<float,AD>(true);
eigenpy::registerCast<AD,long>(false);
eigenpy::registerCast<long,AD>(true);
eigenpy::registerCast<AD,int>(false);
eigenpy::registerCast<int,AD>(true);
}
};
}
Expand Down
26 changes: 25 additions & 1 deletion include/pycppad/cast.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/*
* Copyright 2021 INRIA
* Copyright 2021-2022 INRIA
*/

#ifndef __pycppad_cast_hpp__
#define __pycppad_cast_hpp__

#include "pycppad/fwd.hpp"

#include <eigenpy/user-type.hpp>

namespace pycppad
{
namespace internal
Expand Down Expand Up @@ -42,4 +44,26 @@ namespace pycppad
} // namespace internal
} // namespace pycppad

namespace eigenpy {

template <typename Scalar, typename To>
struct cast<::CppAD::AD<Scalar>, To>
{
typedef ::CppAD::AD<Scalar> From;
static To run(const From & from) {
return ::pycppad::internal::Cast<From, To>::run(from);
}
};

template <typename From, typename Scalar>
struct cast<From,::CppAD::AD<Scalar>>
{
typedef ::CppAD::AD<Scalar> To;
static To run(const From & from) {
return To(static_cast<Scalar>(from));
}
};

} // namespace eigenpy

#endif //#ifndef __pycppad_cast_hpp__
51 changes: 51 additions & 0 deletions include/pycppad/codegen/cg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,48 @@

#include "pycppad/cast.hpp"

namespace eigenpy {

template <typename Scalar, typename To>
struct cast<::CppAD::cg::CG<Scalar>, To>
{
typedef ::CppAD::cg::CG<Scalar> From;
static To run(const From & from) {
return ::pycppad::internal::Cast<From, To>::run(from);
}
};

template <typename From, typename Scalar>
struct cast<From,::CppAD::cg::CG<Scalar>>
{
typedef ::CppAD::cg::CG<Scalar> To;
static To run(const From & from) {
return To(static_cast<Scalar>(from));
}
};

namespace internal {

template <typename Scalar>
struct getitem<::CppAD::cg::CG<Scalar>> {

typedef ::CppAD::cg::CG<Scalar> CG;

static PyObject* run(void* data, void* /* arr */) {
CG & cg = *static_cast<CG*>(data);

if(!cg.isValueDefined()) // not initialized
cg.setValue(static_cast<Scalar>(0));

bp::object m(cg);
Py_INCREF(m.ptr());
return m.ptr();
}
};

} // namespace internal
} // namespace eigenpy

namespace pycppad
{
namespace internal
Expand Down Expand Up @@ -126,6 +168,15 @@ namespace pycppad

eigenpy::registerNewType<CG>();
eigenpy::registerCommonUfunc<CG>();

eigenpy::registerCast<CG,double>(false);
eigenpy::registerCast<double,CG>(true);
eigenpy::registerCast<CG,float>(false);
eigenpy::registerCast<float,CG>(true);
eigenpy::registerCast<CG,long>(false);
eigenpy::registerCast<long,CG>(true);
eigenpy::registerCast<CG,int>(false);
eigenpy::registerCast<int,CG>(true);
}
};

Expand Down

0 comments on commit b13e04e

Please sign in to comment.