Unverified Commit a77d983b authored by Rohan Budhiraja's avatar Rohan Budhiraja Committed by GitHub
Browse files

Add codegen/autodiff support for eigen coeff-wise min and max operations (#1234)

* autodiff/cppad: fix eigen min check

* codegen/cppadcg: fix call to eigen min and max functions

* autodiff/cppad.hpp: add eigen::internal::pmin specialization

* codegn/cppadcg: eigen::internal::pmin/pmax specialization

* autodiff/cppad/minmax: create a file to deal with coeff wise min/max operation in eigen.
This file needs to be loaded before Eigen/Core is called, to load the correct environment for autodiff/codegen.
Otherwise, std::min/max is read by the compiler.

* autodiff/casadi: fix header

* traits.hpp: create traits file, which is independent from eigen

* codegen/cppadcg, autodiff/cppad.hpp: include minmax.hpp

* math/fwd: use traits.hpp instead of fwd.hpp. avoids loading eigen

* utils/static-if: use traits.hpp to avoid using eigen

* include pinocchio/fwd.hpp header
	modified:   src/multibody/joint/joint-free-flyer.hpp
	modified:   src/multibody/joint/joint-revolute-unbounded.hpp
	modified:   src/spatial/inertia.hpp

* add boost-mpl-container-limit header in codegen and autodiff files
	modified:   src/autodiff/cppad.hpp
	modified:   src/codegen/cppadcg.hpp

* add pinocchio/fwd.hpp header where missing

* add copyright license

* fwd.hpp: move enums to fwd.hpp

* [unittest/cppad-basic] add testing for cppad support for eigen min and max

* autodiff/cppad: fix inclusion order of minmax

* src, unittest: fix header inclusion order.

* cppad.hpp: add min/max computation in cppad. simplify implementation
parent d0a170d1
......@@ -118,6 +118,9 @@ namespace Eigen
} // namespace Eigen
// Source from #include <cppad/example/cppad_eigen.hpp>
#include "pinocchio/utils/static-if.hpp"
namespace CppAD
{
// functions that return references
......@@ -131,6 +134,23 @@ namespace CppAD
{ return CppAD::AD<Base>(0.); }
template <class Base> AD<Base> abs2(const AD<Base>& x)
{ return x * x; }
template<typename Scalar>
AD<Scalar> min(const AD<Scalar>& x, const AD<Scalar>& y)
{
using ::pinocchio::internal::if_then_else;
using ::pinocchio::internal::LT;
return if_then_else(LT, y, x, y, x);
}
template<typename Scalar>
AD<Scalar> max(const AD<Scalar>& x, const AD<Scalar>& y)
{
using ::pinocchio::internal::if_then_else;
using ::pinocchio::internal::LT;
return if_then_else(LT, x, y, y, x);
}
} // namespace CppAD
#include "pinocchio/utils/static-if.hpp"
......
......@@ -171,6 +171,61 @@ BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
BOOST_CHECK(NearEqual(dz[0],std::cos(x0),eps99,eps99));
}
BOOST_AUTO_TEST_CASE(test_eigen_min)
{
using CppAD::AD;
typedef double Scalar;
typedef AD<double> ADScalar;
Eigen::Matrix<ADScalar, Eigen::Dynamic, 1> ad_X;
Eigen::Matrix<ADScalar, Eigen::Dynamic, 1> ad_Y;
ad_X.resize(2);
ad_Y.resize(2);
Eigen::Matrix2d x_test(-1,1);
Eigen::Matrix2d y_test = x_test.array().min(Scalar(0.));
CppAD::Independent(ad_X);
//Function
ad_Y = ad_X.array().min(Scalar(0.));
CppAD::ADFun<Scalar> ad_fun(ad_X,ad_Y);
CPPAD_TESTVECTOR(Scalar) x((size_t)2);
Eigen::Map<Eigen::Matrix2d>(x.data(),2,1) = x_test;
CPPAD_TESTVECTOR(Scalar) y = ad_fun.Forward(0,x);
BOOST_CHECK(Eigen::Map<Eigen::Matrix2d>(y.data(),2,1).isApprox(y_test));
}
BOOST_AUTO_TEST_CASE(test_eigen_max)
{
using CppAD::AD;
typedef double Scalar;
typedef AD<double> ADScalar;
Eigen::Matrix<ADScalar, Eigen::Dynamic, 1> ad_X;
Eigen::Matrix<ADScalar, Eigen::Dynamic, 1> ad_Y;
ad_X.resize(2);
ad_Y.resize(2);
Eigen::Matrix2d x_test(-1,1);
Eigen::Matrix2d y_test = x_test.array().max(Scalar(0.));
CppAD::Independent(ad_X);
//Function
ad_Y = ad_X.array().max(Scalar(0.));
CppAD::ADFun<Scalar> ad_fun(ad_X,ad_Y);
CPPAD_TESTVECTOR(Scalar) x((size_t)2);
Eigen::Map<Eigen::Matrix2d>(x.data(),2,1) = x_test;
CPPAD_TESTVECTOR(Scalar) y = ad_fun.Forward(0,x);
BOOST_CHECK(Eigen::Map<Eigen::Matrix2d>(y.data(),2,1).isApprox(y_test));
}
BOOST_AUTO_TEST_CASE(test_eigen_support)
{
using namespace CppAD;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment