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

division operator with mixed types

Tags: None
(comma "," separated)
pappujadav
Registered Member
Posts
3
Karma
0

division operator with mixed types

Thu Oct 31, 2013 7:16 pm
Code: Select all
template <typename Derived1,typename Derived2>
typename Derived1::Array my_saxpy(const double& a,const ArrayBase<Derived1>& x,const ArrayBase<Derived2>& y)
{
  ArrayXXd a_array;
  a_array = ArrayXXd::Constant(x.rows(),x.cols(),a);
  return a_array/x + y;
}

int main()
{
  int N(800);
  double a(2.);
  ArrayXXcd x(N,N),y(N,N);
  x.setOnes();
  y.setOnes();
  std::cout << "my_saxpy(a,x,y)(0,0) = " << (my_saxpy(a,x,y))(0,0) << std::endl;
 
}


The above code snippet shows a simple function to calculate (a/x+y) for the ArrayXXcd class. The program fails to compile with the error:

error: static assertion failed with "YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY"
EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename Rhs::Scalar);


If instead, I try and compile the function with the usual SAXPY function == a*x+y, the code works perfectly fine. It appears that the division operator is not properly overloaded when complex types are mixed with reals. I have also tried the cast() method using .cast<std::complex<double>>() but I get a 'type does not exist error'. Can you suggest a work around?

Thanks
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
What about:

a * x.inverse() + y
pappujadav
Registered Member
Posts
3
Karma
0
Thanks gael!

Your suggestion works. However, I tried playing around with the expression. I run into the same problem when I try and calculate:


Code: Select all
a_array + x + y;


What do I do here? Is there a general way to make complex types play nice with real ones?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
yes, it's currently limited to products. Again, no need to use a_array, complex<double>(a) + x + y should work. For an expression, a_array.cast<complex<double> >() will do the conversion.
pappujadav
Registered Member
Posts
3
Karma
0
Much appreciated!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]