This is my code:
这是我的代码:
MatrixXd A(3,3);
A<<1,2,3,4,5,6,7,8,9;
MatrixXd b(3,3);
b = (A.array() == A.array()).matrix();
cout<<b<<endl;
It show that something wrong with (A.array() == A.array()).matrix()
. This is the error message:
它表明(A.array() = A.array())).matrix()有问题。这是错误信息:
In file included from /home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/Core:254:0,
from /home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/Dense:1,
from /home/biss/Desktop/self-driving-car/term2/kalman-Filter/cpp_normal_op/clion_c/main.cpp:2:
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/Assign.h: In instantiation of ‘Derived& Eigen::DenseBase<Derived>::lazyAssign(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::MatrixWrapper<const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<double, (Eigen::internal::ComparisonName)0u>, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> > > >; Derived = Eigen::Matrix<double, -1, -1>]’:
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/PlainObjectBase.h:414:30: required from ‘Derived& Eigen::PlainObjectBase<Derived>::lazyAssign(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::MatrixWrapper<const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<double, (Eigen::internal::ComparisonName)0u>, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> > > >; Derived = Eigen::Matrix<double, -1, -1>]’
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/Assign.h:527:123: required from ‘static Derived& Eigen::internal::assign_selector<Derived, OtherDerived, false, false>::run(Derived&, const OtherDerived&) [with Derived = Eigen::Matrix<double, -1, -1>; OtherDerived = Eigen::MatrixWrapper<const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<double, (Eigen::internal::ComparisonName)0u>, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> > > >]’
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/PlainObjectBase.h:653:72: required from ‘Derived& Eigen::PlainObjectBase<Derived>::_set_noalias(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::MatrixWrapper<const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<double, (Eigen::internal::ComparisonName)0u>, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> > > >; Derived = Eigen::Matrix<double, -1, -1>]’
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/PlainObjectBase.h:638:114: required from ‘void Eigen::PlainObjectBase<Derived>::_set_selector(const OtherDerived&, const Eigen::internal::false_type&) [with OtherDerived = Eigen::MatrixWrapper<const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<double, (Eigen::internal::ComparisonName)0u>, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> > > >; Derived = Eigen::Matrix<double, -1, -1>]’
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/PlainObjectBase.h:630:20: required from ‘Derived& Eigen::PlainObjectBase<Derived>::_set(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::MatrixWrapper<const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<double, (Eigen::internal::ComparisonName)0u>, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> > > >; Derived = Eigen::Matrix<double, -1, -1>]’
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/Matrix.h:172:24: required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::operator=(const Eigen::MatrixBase<OtherDerived>&) [with OtherDerived = Eigen::MatrixWrapper<const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<double, (Eigen::internal::ComparisonName)0u>, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> >, const Eigen::ArrayWrapper<Eigen::Matrix<double, -1, -1> > > >; _Scalar = double; int _Rows = -1; int _Cols = -1; int _Options = 0; int _MaxRows = -1; int _MaxCols = -1]’
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/cpp_normal_op/clion_c/main.cpp:23:7: required from here
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/util/StaticAssert.h:32:40: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
^
/home/biss/Desktop/self-driving-car/term2/kalman-Filter/Eigen/src/Core/Assign.h:500:3: note: in expansion of macro ‘EIGEN_STATIC_ASSERT’
EIGEN_STATIC_ASSERT(SameType,YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
^
CMakeFiles/clion_c.dir/build.make:62: recipe for target 'CMakeFiles/clion_c.dir/main.cpp.o' failed
make[3]: *** [CMakeFiles/clion_c.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/clion_c.dir/all' failed
make[2]: *** [CMakeFiles/clion_c.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/clion_c.dir/rule' failed
make[1]: *** [CMakeFiles/clion_c.dir/rule] Error 2
Makefile:118: recipe for target 'clion_c' failed
make: *** [clion_c] Error 2
However, If I change my code:
但是,如果我改变我的代码:
MatrixXd A(3,3);
A<<1,2,3,4,5,6,7,8,9;
MatrixXd b(3,3);
b = (A.array() * A.array()).matrix();
cout<<b<<endl;
It can run ok.
它可以运行。
1 4 9
16 25 36
49 64 81
If I want to do this operation:(A.array() == A.array()).matrix()
, what should I do?
如果我想做这个操作:(A.array() = A.array()).matrix(),我该怎么做?
1 个解决方案
#1
4
Lets break it down a bit. (A.array() == A.array())
represents the (2D) array with a boolean showing element-wise equality. If you were to write
让我们把它分解一下。(a .array() = a .array())表示(2D)数组,布尔值表示元素相等。如果你要写作的话。
std::cout << (A.array() == A.array());
you would get
你会得到
1 1 1
1 1 1
1 1 1
as you're asking if A
equals itself and it happens not to have any NANs. The error message that you got says error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
. Which means exactly that. You cannot assign a MatrixXf
to a MatrixXd
either. There is no implicit casting. So, to make it work, you want to write
当你问A是否等于它的时候它碰巧没有任何的南。您得到的错误消息是error:静态断言失败:you_mixed_difference _numeric_types__you_need_use_the_cast_method_of_matrixbase_to_numeric_types_explicit。这意味着。你也不能将矩阵赋值给矩阵。没有隐式铸造。所以,为了让它起作用,你想写。
b = (A.array() == A.array()).cast<double>().matrix();
which explicitly casts the booleans to doubles. I'm pretty sure that's not exactly what you want to do, but that is what is written in you're question (hopefully because it's an incomplete MCVE).
它显式地将布尔值转换为双精度。我很确定这不是你想要做的,但这是你的问题中写的(希望是因为它是不完整的MCVE)。
#1
4
Lets break it down a bit. (A.array() == A.array())
represents the (2D) array with a boolean showing element-wise equality. If you were to write
让我们把它分解一下。(a .array() = a .array())表示(2D)数组,布尔值表示元素相等。如果你要写作的话。
std::cout << (A.array() == A.array());
you would get
你会得到
1 1 1
1 1 1
1 1 1
as you're asking if A
equals itself and it happens not to have any NANs. The error message that you got says error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
. Which means exactly that. You cannot assign a MatrixXf
to a MatrixXd
either. There is no implicit casting. So, to make it work, you want to write
当你问A是否等于它的时候它碰巧没有任何的南。您得到的错误消息是error:静态断言失败:you_mixed_difference _numeric_types__you_need_use_the_cast_method_of_matrixbase_to_numeric_types_explicit。这意味着。你也不能将矩阵赋值给矩阵。没有隐式铸造。所以,为了让它起作用,你想写。
b = (A.array() == A.array()).cast<double>().matrix();
which explicitly casts the booleans to doubles. I'm pretty sure that's not exactly what you want to do, but that is what is written in you're question (hopefully because it's an incomplete MCVE).
它显式地将布尔值转换为双精度。我很确定这不是你想要做的,但这是你的问题中写的(希望是因为它是不完整的MCVE)。