Re: [eigen] Subject: why are AutoDiff to value conversions disabled?

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hey!

If you have an autodiff type in your program they need to propagate through your program correctly.
Some pitfalls in doing so are conversions to integers and back or not "autodiff enabled" mathematical function e.g. if your implementation lacks an overloaded version of atan2(). If you don't have these conversion operators, this will result in a compile error directly pointing you to the issue.
If you enable these operators, C++ will implicity convert the autodiff type into its underlying value type at some points. Then your program will compile just fine but your derivatives will be wrong.

AFAIK there is now way to enable these operators just for use with static_cast<> and not enable them for implicit conversions.

The fix is rather easy, though: you just implement a function
 
template <class Scalar, class Scalar_Or_Autodiff>
Scalar value_cast(const Scalar_Or_Autodiff&);

and specialize it accordingly to just give back the Scalar or unwrap if needed.
Then you can just do value_cast<Scalar>(maybe_autodiff_scalar) everywhere. You can even get around the template parameter with some trait magic.

Regards,
Martin

Am 19.01.2017 um 08:17 schrieb Geoffrey Lalonde:
See the conversions commented out here: https://bitbucket.org/eigen/eigen/src/df279134beee3771e025160762e976680de90503/unsupported/Eigen/src/AutoDiff/AutoDiffScalar.h?at=default&fileviewer=file-view-default#AutoDiffScalar.h-149

Is there a good reason for this? I often find myself writing functions templated for both real scalars scalar or autodiff scalars, and if I want to read the real scalar value out of this templated code, I need to jump through some hoops that could be avoided by just using a static_cast<double>(maybe_autodiff_scalar) if these lines were uncommented.

Alternatively, is it possible to enable this conversion externally without modifying eigen's source but to still have it as work as transparently?

Thanks,
Geoff



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/