简体   繁体   English

用Eigen创建简单矩阵?

[英]Creating simple matrices with Eigen?

I'm using the Eigen library to create and manipulate some matrices in C++. 我正在使用Eigen库在C ++中创建和处理一些矩阵。 Eigen is installed (Ubuntu 16.04) and seems to be working. 已安装Eigen(Ubuntu 16.04),并且似乎可以正常工作。 However, when I declare a matrix as part of a class in an external file and #include the necessary files, it fails. 但是,当我在外部文件中将矩阵声明为类的一部分并#include必要的文件时,它会失败。 My KalmanFilter.h header file: 我的KalmanFilter.h头文件:

#include <Eigen/Dense>
using Eigen::MatrixXd;
class KalmanFilter {
public:
  KalmanFilter(double, double);
  double initialX, initialY;
  MatrixXd m;
};

My KalmanFilter.cpp file: 我的KalmanFilter.cpp文件:

#include <Eigen/Dense>
#include "KalmanFilter.h"
KalmanFilter::KalmanFilter(double inX, double inY) {
  initialX = inX;
  initialY = inY;
  m(2, 1);
  m << initialX, initialY;
}

And of course my main.cpp: 当然是我的main.cpp:

#include <Eigen/Dense>
#include "Utilities/KalmanFilter.h"
int main() {
  double a, b;
  a = 1.0;
  b = 2.0;
  KalmanFilter KF(a, b);
}

Everything compiles all right, but running it results in an assertion error: 一切都可以编译,但是运行它会导致声明错误:

main: /usr/local/include/Eigen/src/Core/DenseCoeffsBase.h:365: Eigen::DenseCoeffsBase<Derived, 1>::Scalar& Eigen::DenseCoeffsBase<Derived, 1>::operator()(Eigen::Index, Eigen::Index) [with Derived = Eigen::Matrix<double, -1, -1>; Eigen::DenseCoeffsBase<Derived, 1>::Scalar = double; Eigen::Index=long int]: Assertion 'row >= 0 && rows() && col >= 0 && col < cols()' failed. Aborted.

If I put MatrixXd m(2, 1); 如果我把MatrixXd m(2, 1); inside my KalmanFiter.cpp file (re-declaring that it's a MatrixXd) the resulting compilation runs, but the m matrix is empty (it exists, but apparently the next line that's supposed to initialize it fails silently). 在我的KalmanFiter.cpp文件中(重新声明它是MatrixXd),运行了编译后的结果,但是m矩阵为空(它存在,但显然下一个初始化它的行会无声地失败)。 I'm almost positive that Eigen is installed correctly, because declaring and initializing the same MatrixXd matrix inside my main.cpp works just fine. 我几乎肯定Eigen已正确安装,因为在main.cpp中声明并初始化相同的MatrixXd矩阵就可以了。

What am I missing here? 我在这里想念什么?

m(2, 1); this doesn't do what you think it does. 这并没有按照您的想法做。 It doesn't create the object, it's a syntax to get the coefficient at given position ( operator() ), so your matrix m is empty, and you try to retrieve the element. 它不会创建对象,而是一种在给定位置( operator() )获取系数的语法,因此您的矩阵m为空,然后尝试检索该元素。

The syntax seems the same, but the placement makes a great deal of a difference. 语法似乎相同,但是位置差异很大。

You need to initialize member object in the member initialization list: 您需要在成员初始化列表中初始化成员对象:

KalmanFilter::KalmanFilter(double inX, double inY) : m(2, 1) {
//                                                   ^^^^^^^
  initialX = inX;
  initialY = inY;
  m << initialX, initialY;
}

The problem is due to the line in KalmanFilter.cpp : 问题是由于KalmanFilter.cpp的行:

m(2, 1);

That doesn't resize the matrix as I assume you assume it does. 这不会像我假设的那样调整矩阵的大小。 Replace it with m.resize(2, 1); 将其替换为m.resize(2, 1); and try again. 然后再试一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM