`(A.array()== A.array())有什么问题。矩阵()`?

时间:2018-02-07 03:34:15

标签: c++ arrays matrix eigen

这是我的代码:

   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;

它表明(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

但是,如果我更改了我的代码:

   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;

它可以正常运行。

 1  4  9
16 25 36
49 64 81

如果我想执行此操作:(A.array() == A.array()).matrix(),我该怎么办?

1 个答案:

答案 0 :(得分:4)

让我们分解一下。 (A.array() == A.array())表示(2D)数组,其布尔值显示元素相等。如果你要写

std::cout << (A.array() == A.array());

你会得到

1 1 1
1 1 1
1 1 1

因为你问A是否等于自己而且它没有任何NAN。您收到的错误消息显示error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY。这意味着什么。您也无法将MatrixXf分配给MatrixXd。没有隐式投射。所以,要使它工作,你想写

   b = (A.array() == A.array()).cast<double>().matrix();

明确地将布尔值转换为双打。我很确定这不是你想要做的,但这就是你写的问题(希望因为它是一个不完整的MCVE)。