coin-or / CppAD

A C++ Algorithmic Differentiation Package: Home Page
https://cppad.readthedocs.io
Other
446 stars 94 forks source link

Add std::isfinite specialization for Eigen 3.4 #142

Closed benmwebb closed 2 years ago

benmwebb commented 2 years ago

When using Eigen 3.4 it may attempt to call std::isfinite() on AD objects (see isfinite_impl() in Eigen/src/Core/MathFunctions.h). Provide a suitable specialization, similar to those provided for std::isinf and std::isnan in PR #140.

bradbell commented 2 years ago

I think the function below returns true when x is Nan and it should be false.

 template <class Base> bool isfinite(const CppAD::AD<Base> &x)
    {   return !isinf(x); }

I think this should work properly:

    template <class Base> bool isfinite(const CppAD::AD<Base> &x)
    {   return isfinite(CppAD::Value( CppAD::Var2Par(x) ) ); }
bradbell commented 2 years ago

I think the function below returns true when x is Nan and it should be false.

 template <class Base> bool isfinite(const CppAD::AD<Base> &x)
    {   return !isinf(x); }

I think this should work properly:

    template <class Base> bool isfinite(const CppAD::AD<Base> &x)
    {   return isfinite(CppAD::Value( CppAD::Var2Par(x) ) ); }
benmwebb commented 2 years ago

I think the function below returns true when x is Nan and it should be false.

You're right of course; your fix works for my example and I'll update the PR.