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

How to remember result of Array == Scalar ?

Tags: None
(comma "," separated)
smajewski
Registered Member
Posts
6
Karma
0
Hello, I have been trying to implement some ML algorithms using Eigen lately. For some of them, I need to remember the result of comparing an Array wit a Scalar.

when I do the following:
Code: Select all
VectorXd m;
loadData("Datafile", m);

ArrayXd arr;
cout << (m.array() == scalar) << "\n";


The program just writes a vector of ones and zeros, where ones are found in rows which had the values scalar in them in the begining.

Also, when I do the following:

Code: Select all
MatrixXd m;
loadData("Datafile", m);

ArrayXd arr;
arr = (m.array().sqrt());


There are no bugs and the array arr has appropriate values.

but when I do the following:
Code: Select all
MatrixXd m;
loadData("Datafile", m);

ArrayXd arr;
arr = (m.array() == 1.0);


I get a long line of errors form compiler :
Code: Select all
 /usr/local/include/Eigen/Core:276:0,
                 from /usr/local/include/Eigen/Dense:1,
                 from Etest.cpp:2:
/usr/local/include/Eigen/src/Core/Assign.h: In member function ‘Derived& Eigen::DenseBase<Derived>::lazyAssign(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<std::binder1st<std::equal_to<double> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -0x00000000000000001, 1> > >, Derived = Eigen::Array<double, -0x00000000000000001, 1>]’:
/usr/local/include/Eigen/src/Core/PlainObjectBase.h:393:46:   instantiated from ‘Derived& Eigen::PlainObjectBase<Derived>::lazyAssign(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<std::binder1st<std::equal_to<double> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -0x00000000000000001, 1> > >, Derived = Eigen::Array<double, -0x00000000000000001, 1>]’
/usr/local/include/Eigen/src/Core/Assign.h:522:123:   instantiated from ‘static Derived& Eigen::internal::assign_selector<Derived, OtherDerived, false, false>::run(Derived&, const OtherDerived&) [with Derived = Eigen::Array<double, -0x00000000000000001, 1>, OtherDerived = Eigen::CwiseUnaryOp<std::binder1st<std::equal_to<double> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -0x00000000000000001, 1> > >]’
/usr/local/include/Eigen/src/Core/PlainObjectBase.h:599:105:   instantiated from ‘Derived& Eigen::PlainObjectBase<Derived>::_set_noalias(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<std::binder1st<std::equal_to<double> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -0x00000000000000001, 1> > >, Derived = Eigen::Array<double, -0x00000000000000001, 1>]’
/usr/local/include/Eigen/src/Core/PlainObjectBase.h:584:102:   instantiated from ‘void Eigen::PlainObjectBase<Derived>::_set_selector(const OtherDerived&, const Eigen::internal::false_type&) [with OtherDerived = Eigen::CwiseUnaryOp<std::binder1st<std::equal_to<double> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -0x00000000000000001, 1> > >, Derived = Eigen::Array<double, -0x00000000000000001, 1>]’
/usr/local/include/Eigen/src/Core/PlainObjectBase.h:576:7:   instantiated from ‘Derived& Eigen::PlainObjectBase<Derived>::_set(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<std::binder1st<std::equal_to<double> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -0x00000000000000001, 1> > >, Derived = Eigen::Array<double, -0x00000000000000001, 1>]’
/usr/local/include/Eigen/src/Core/Array.h:89:30:   instantiated from ‘Eigen::Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& Eigen::Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::operator=(const Eigen::ArrayBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<std::binder1st<std::equal_to<double> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -0x00000000000000001, 1> > >, _Scalar = double, int _Rows = -0x00000000000000001, int _Cols = 1, int _Options = 0, int _MaxRows = -0x00000000000000001, int _MaxCols = 1, Eigen::Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> = Eigen::Array<double, -0x00000000000000001, 1>]’
Etest.cpp:30:45:   instantiated from here
/usr/local/include/Eigen/src/Core/Assign.h:493:3: error: ‘YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY’ is not a member of ‘Eigen::internal::static_assertion<false>’


I suppose the result of (m == scalar) has some different type, which cannot be cast to an ArrayXd, but I don't know what is this type. I couldn't find any references in the tutorial or documentation on how to deal with such a problem, please help.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
the result of a comparison is a bool, so you need an array of bool, ArrayXb.
smajewski
Registered Member
Posts
6
Karma
0
Thank you for your answer ggael.

Is there a nice way of casting it to double? Basically I have a Vector of doubles Y, and I want to get a Vector o values 0/1 indicating if values of Y are equal to a given double constant k. I also want this 0/1 values to be doubles, and possibly to do it with Eigen syntax for optimization, and not just use a for loop.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
of course: (whatever matrix or array expression).cast<double>()
smajewski
Registered Member
Posts
6
Karma
0
This is exactly what I was looking for and solves my problem elegantly! Thank you very much ggael!


Bookmarks



Who is online

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