This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
Bug 1222 - AutoDiffScalar doesn't work with operator+ returning ADS<DerType&>
Summary: AutoDiffScalar doesn't work with operator+ returning ADS<DerType&>
Status: RESOLVED FIXED
Alias: None
Product: Eigen
Classification: Unclassified
Component: Unsupported modules (show other bugs)
Version: 3.3 (current stable)
Hardware: All All
: Normal Unknown
Assignee: Nobody
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-10 10:50 UTC by Daniel Vollmer
Modified: 2019-12-04 15:48 UTC (History)
2 users (show)



Attachments

Description Daniel Vollmer 2016-05-10 10:50:01 UTC
The following example fails to compile:

#include "Eigen/Core"
#include "unsupported/Eigen/AutoDiff"

int main (int argc, char const *argv[])
{
  typedef Eigen::AutoDiffScalar<Eigen::Vector3d> AD;
  
  const double _cv1_3 = 1.0;
  const AD chi_3 = 1.0;
  // this line doesn't work, because operator+ returns ADS<DerType&>, which then cannot be converted to ADS<DerType>
  const AD denom = chi_3 + _cv1_3;
  
  return 0;
}

I'm not sure what the intent is for operator+ (and -) to return ADS<DerType&> (probably to reuse the original derivatives?), but then the conversion from ADS<DerType&> to ADS<DerType> doesn't seem allowed, the intended constructor fails due to the unexpected reference in OtherDerType.
Comment 1 Daniel Vollmer 2016-05-10 15:01:21 UTC
Applying the following patch seems to work around the problem for me:

--- a/AutoDiffScalar.h	2016-05-10 04:24:55.000000000 +0200
+++ b/AutoDiffScalar.h	2016-05-10 16:47:36.000000000 +0200
@@ -101,7 +101,7 @@
     template<typename OtherDerType>
     AutoDiffScalar(const AutoDiffScalar<OtherDerType>& other
 #ifndef EIGEN_PARSED_BY_DOXYGEN
-    , typename internal::enable_if<internal::is_same<Scalar,typename OtherDerType::Scalar>::value,void*>::type = 0
+    , typename internal::enable_if<internal::is_same<Scalar, typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar>::value,void*>::type = 0
 #endif
     )
       : m_value(other.value()), m_derivatives(other.derivatives())
Comment 2 Gael Guennebaud 2016-05-18 14:04:20 UTC
Thanks for the patch, but it should be OtherDerType instead of DerType. Also, this issue applies to 3.3 only.

Fix: https://bitbucket.org/eigen/eigen/commits/e6a5d4ea379c
Backport of the unit test to 3.2: https://bitbucket.org/eigen/eigen/commits/638f9632e24c
Comment 3 Daniel Vollmer 2016-05-18 14:17:08 UTC
Ah yes, I had taken the most recent AutoDiffScalar from the default branch to get the fix to http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1189 which might be why I saw this problem using an otherwise vanilla Eigen 3.2.

Thanks!
Comment 4 Nobody 2019-12-04 15:48:00 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/1222.

Note You need to log in before you can comment on or make changes to this bug.