This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Mixing vector scalar types when subtracting two vectors

Tags: None
(comma "," separated)
normvcr
Registered Member
Posts
12
Karma
0
OS
I am trying to pull ADOL-C into Eigen (forward and reverse), and am having a problem getting Eigen to subtract a vector of scalar type double from a vector of scalar type adouble.:
Code: Select all
Eigen/src/Core/SelfCwiseBinaryOp.h:131:7: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY

The comment in CwiseBinaryOp.h says:
Code: Select all
// we require Lhs and Rhs to have the same scalar type. Currently there is no example of a binary functor
// that would take two operands of different types. If there were such an example, then this check should be
// moved to the BinaryOp functors, on a per-case basis. This would however require a change in the BinaryOp functors, as
// currently they take only one typename Scalar template parameter.
// It is tempting to always allow mixing different types but remember that this is often impossible in the vectorized paths.
// So allowing mixing different types gives very unexpected errors when enabling vectorization, when the user tries to
// add together a float matrix and a double matrix.

I want to turn off this checkkng on the part of Eigen, since ADOL-C already checks that operations make sense at the scalar level.

Any suggestions?
normvcr
Registered Member
Posts
12
Karma
0
OS
I have simplified the issue by phrasing the problem purely in terms of Eigen -- no external packages.
Here is code that compile/links and runs correctly:
Code: Select all
#include <Eigen/Core>
#include <iostream>
using namespace std;

int main( int argc, char* argv[] )
{
  double  t0 = 3;
  float t1 = 4;
  double t2 = t1 + t0;

  cout << "t0= " << t0 << endl
       << "t1= " << t1 << endl
       << "t1+t0= " << t2 << endl;

#if 0
  Eigen::VectorXd u0(1); u0 << t0;
  Eigen::VectorXf u1(1); u1 << t1;
  Eigen::VectorXd u2 = u1 + u0;
#endif

  return 0;
}// main

This codes adds a float to a double, and the compiler correctly does the promotions/conversions.
However, if you try to do the same thing with Eigen vectors, by changing #if 0 to #if 1, you get
the following error:
Eigen/src/Core/CwiseBinaryOp.h:141:7: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY

Surely, Eigen allows adding a vector of floats to a vector of doubles. What am I doing wrong?
Thank you.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
It's forbidden by Eigen because this would result is pretty inefficient code. If you really want to do so, you have to cast explicitly:

u0 + u1.cast<double>()
normvcr
Registered Member
Posts
12
Karma
0
OS
Could this not be handled by Traits?
Have this optionally be a Warning instead of an Error?

In my situation, it is actually inefficient to cast the VectorXd<double>
to be a VectorXd<OTHER>. It would be much more efficient to let the
scalars interact as different types, and let them combine efficiently
into an OTHER, without ever casting the double to an OTHER. There
is already a way to combine the two scalars:
Code: Select all
OTHER operator+( double val1, const OTHER& val2 )
OTHER operator+( OTHER& val1, const double val2 )

OTHER is a complex class, part of the automatic differentiation (AD) methodology.

Thank you,
Norm
normvcr
Registered Member
Posts
12
Karma
0
OS
To work around not being able to add a Vector of one type to a Vector of another type, I am coding up simplistic operators that take the particular vectors of mixed types that I am concerned with, and this allows the code to compile. E.g.
Code: Select all
typedef Eigen::Matrix< OTHER, Eigen::Dynamic, 1 > VectorType;
VectorType operator+( const VectorXd& val1, const VectorType& val2 );

I am still interested, though, to work through this issue with the Eigen team, and see whether there is a better way to deal with this.

Thank you.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]