简体   繁体   English

如何在C ++中自己的类中正确使用矩阵(本征库)?

[英]How to correctly use a matrix (Eigen library) in my own class in C++?

I really tried to solve this problem and I have read a lot of QAs here on Stackoverflow but somehow nothing really helped. 我确实试图解决此问题,并且在Stackoverflow上阅读了很多QA,但实际上并没有任何帮助。

I am trying to implement a Class with its own matrices and vectors from the Eigen library. 我正在尝试使用Eigen库中的矩阵和向量来实现Class。 I am using Code::Blocks with GNU GCC Compiler. 我在GNU GCC编译器中使用Code :: Blocks。

Here is a simple example of what I mean, but it is not the exact code, because I am using more matrices and vectors and of other sizes (4x4 Matrices, 2x4 Matrices, 4x1 Vectors and 2x1 Vectors): 这是我的意思的简单示例,但这不是确切的代码,因为我使用了更多的矩阵和向量以及其他大小(4x4矩阵,2x4矩阵,4x1向量和2x1向量):

    class MYCLASS{
        private:

            VectorXd x;
            MatrixXd A;

        public:

            MYCLASS(double, double, double, double);
            double get_matval();

    };

    MYCLASS::MYCLASS(double deltaT, double q_var, double r1_var, double r2_var){

        x_m(0)=q_var;
        x_m(1)=r1_var;
        x_m(2)=r2_var;
        x_m(3)=0.0;

        A(0,0)= deltaT;
        A(0,1)= 0.0;
        A(1,0)= 0.0;
        A(1,1)= 0.0;

    }

    double MYCLASS::get_matval(){

        return A(0,0);

    }

1. When I create an object of MYCLASS, like this: 1.创建MYCLASS对象时,如下所示:

MYCLASS myobject(10, 0.5, 0.1, 0.75);

==> Compilation is good, but when it runs, then the program somehow crashes with the following exact description (keep in mind I have other matrices and vectors): ==>编译是好的,但是当它运行时,程序将以以下确切描述崩溃(请记住,我还有其他矩阵和向量):

Assertion failed: index >= 0 && index < size(), file F:....../Eigen/src/Core/DenseCoeffsBase.h, line 425 断言失败:索引> = 0 &&索引<size(),文件F:...... / Eigen / src / Core / DenseCoeffsBase.h,第425行

This application has requested the Runtime to terminate it in an unusual way. 该应用程序已请求运行时以一种异常方式终止它。 Please contact... 请联系...

Process returned 3 (0x3) execution time: 2.131 s 进程返回3(0x3)执行时间:2.131 s

2. If I then put at the beginning of the constructor this: 2.如果我然后将其放在构造函数的开头,则:

    MYCLASS::MYCLASS(double deltaT, double q_var, double r1_var, double r2_var){

            VectorXd x(4);
            MatrixXd A(2,2);

            x_m(0)=q_var;
            x_m(1)=r1_var;

            ...and so on...

==> then when constructing the object ==> no error ==>然后在构造对象时==>没有错误

==> but when I then want to access A(0,0) by using the function get_matval, like this: ==>但是当我随后想要使用函数get_matval访问A(0,0)时,如下所示:

MYCLASS myobject(10, 0.5, 0.1, 0.75);

double myvar = myobject.get_matval();

...it crashes again with the same error message. ...它再次崩溃,并显示相同的错误消息。

Could you guys please help? 你们能帮忙吗?

Even though you found a solution already, here is how to properly initialize (a limited number of) Eigen matrices: 即使您已经找到解决方案,也可以通过以下方法正确初始化(有限数量的)本征矩阵:

MYCLASS::MYCLASS(double deltaT, double q_var, double r1_var, double r2_var)
                : x(4), A(2,2) { // use initializer list to construct x and A
    x << q_var, r1_var, r2_var, 0.0;
    A << deltaT, 0.0, 0.0, 0.0;
}

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

相关问题 在C ++的Eigen库中,我该如何解决:无效使用不完整类型&#39;const class Eigen :: MatrixSquareRootReturnValue <Eigen::Matrix<float, -1, -1> &gt;” - In C++'s Eigen library, how do I solve: invalid use of incomplete type ‘const class Eigen::MatrixSquareRootReturnValue<Eigen::Matrix<float, -1, -1> >’ 如何正确初始化,分配动态Eigen矩阵并将其用作C ++中的类成员? - How to correctly initialize, assign and use a dynamic Eigen matrix as a class member in C++? 如何在C ++中使用特征库执行矩阵矩阵除法 - How to perform matrix matrix division using eigen library in C++ 如何在Dev C ++中使用Eigen库? - How use Eigen library in Dev C++? c++中如何用eigen库导入矩阵行情文件 - How to import matrix market files with eigen library in c++ 使用特征矩阵库进行 C++ 代码转换 - C++ Code Translation with Eigen Matrix Library 如何在Eclipse(Linux)中使用自己的C ++书面库? - How to use my own written library in C++ in eclipse (linux)? 如何使用我自己的库C ++ Ubuntu - how to use my own library c++ ubuntu 如何正确使用cv :: Mat和Eigen :: Matrix? (OpenCV +本征) - How to use cv::Mat and Eigen::Matrix correctly? (OpenCV + Eigen) 如何使用线性代数的C ++模板库Eigen? - How to use Eigen, the C++ template library for linear algebra?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM