使用特征库时的"required from here"

"required from here" when using Eigen library

本文关键字:required from here 特征      更新时间:2023-10-16

我正在使用Eigen库在Eclipse C++中实现一些控制算法。例如,当我想计算矩阵的特征值时,我会在代码行旁边得到一个感叹号"从这里开始需要"警告。我不知道该怎么解决。

这是我的主.cpp文件:

#include <iostream>
#include "System.h"
#include "ControllerCode2.h"
using namespace std;

int main(){
    int n = 3;   // # of states
    int m = 1;   // # of inputs
    int l = 1;   // # of outputs
    MatrixXd A(n, n), B(n,m), C(l,n), D(l,m), Q(n,n), R(m,m), Qe(n,n), Re(m,m);
    A << 0,  1,  0,
         0,  0,  1,
         0, -2, -3;
    B << 0,
         0,
         1;
    C << 1, 0, 0;
    D << 0;
    MatrixXd C_trans = C.transpose();
    Q = C_trans * C;
    R =  MatrixXd::Identity(m, m); // initially
    MatrixXd B_trans = B.transpose();
    Qe = B * B_trans;
    Re =  MatrixXd::Identity(m, m); // initially
    System sys = System(A, B, C, D);
    sys.set_covariance_matrices(R, Q);
    sys.set_noise_covariance_matrices(Re, Qe);
    schur_eigen_test(sys);
    return 5;
}

现在,ControllerCode2.cpp代码:

#include "ControllerCode2.h"
MatrixXd U11;
MatrixXd U21;
void schur_eigen_test( System G ){
    /****** Constructing the Hamiltonian Matrix ******/
    int n = G.A.rows();
    MatrixXd H(2*n, 2*n);          // the Hamiltonian matrix has the dimensions of 2n*2n where n is the number of states
    H.block(0,0,n,n)      = G.A;
    H.block(0,n,n,n)      = -1 * G.B * G.R.inverse() * G.B.transpose();
    H.block(n,0,n,n)      = -1 * G.Q;
    H.block(n,n,n,n)      = -1 * G.A.transpose();
    /****** Performing a real Schur decomposition on the square Hamiltonian matrix ******/
    RealSchur<MatrixXd> schur(H);
    MatrixXd U = schur.matrixU(); //The orthogonal matrix U
    MatrixXd T = schur.matrixT(); //The quasi-triangular matrix T

    /****** Find the eigenvalues and eigenvectors of the Hamiltonian matrix ******/
    EigenSolver<MatrixXd> H_eigen;        // create an EigenSolver Matrix
    H_eigen.compute(H, false);            // compute the eigenvalues ./and eigenvectors of matrix H
    MatrixXd H_eigenval = H_eigen.eigenvalues();
    //              //MatrixXd H_eigenvec = H_eigen.eigenvectors();

    /****** Select the eigenvectors (U11, U21) corresponding to the stable (with -ve real part) eigenvalues ******/
    U11 = U.block(0,0,n,n);
    U21 = U.block(n,0,n,n);
    /****** Calculate F ******/
    MatrixXd F = -1 * G.R.inverse() * G.B.transpose() * U21 * U11.inverse(); // transposeInPlace or transpose??
    //////// Extra: for output
    cout << endl << "H = " << endl << H << endl;
    cout << endl << "U schur(H) " << endl << U << endl;
    cout << endl << "T schur(H) " << endl << T << endl;
    cout << endl << "U*T*U.transpose() " << endl << U * T * U.transpose();
    //      cout << endl << "U.transpose() - U.inverse() " << endl << U.transpose() - U.inverse(); // = which proves that U is orthogonal, i.e. U.transpose() = U.inverse()
    //      EigenSolver<MatrixXd> H_eigen;        // create an EigenSolver Matrix
    //      H_eigen.compute(H, false);            // compute the eigenvalues and eigenvectors of matrix H
    //      MatrixXd H_eigenval = H_eigen.eigenvalues();
    //      cout << endl << "eigenvalues of H = " << endl << H_eigenval << endl;
}

我收到这条线旁边的警告:

MatrixXd H_eigenval = H_eigen.eigenvalues();

我的ControllerCode2.h代码:

#ifndef CONTROLLERCODE_H_
#define CONTROLLERCODE_H_
#include "System.h"
    void schur_eigen_test( System );
//};
#endif /* CONTROLLERCODE_H_ */

系统.h代码:

// include guard
#ifndef SYSTEM_H_
#define SYSTEM_H_
#include <Eigen/Dense>
#include <iostream>
#include <Eigen/Eigenvalues>
#include <iostream>
using namespace Eigen;
using namespace std;
using Eigen::MatrixXd;

class System {
public:
    MatrixXd A;
    MatrixXd B;
    MatrixXd C;
    MatrixXd D;
    MatrixXd Q;
    MatrixXd R;
    MatrixXd Re;
    MatrixXd Qe;

    System(MatrixXd a, MatrixXd b, MatrixXd c, MatrixXd d){
        A = a;
        B = b;
        C = c;
        D = d;
        //cout << A << endl << B << endl << C << endl;
    };
    void set_covariance_matrices(MatrixXd r, MatrixXd q){
        R = r;
        Q = q;
        //cout << R << endl << Q << endl;
    }
    void set_noise_covariance_matrices(MatrixXd re, MatrixXd qe){
        Re = re;
        Qe = qe;
        //cout << Re << endl << Qe << endl;
    }

    virtual ~System();
    // this function receives the 4 state space matrices and returns one plant matrix G
    MatrixXd setContSys(MatrixXd a, MatrixXd b, MatrixXd c, MatrixXd d);
};
#endif /* SYSTEM_H_ */

最后这是控制台中显示的结果:

15:12:51 **** Incremental Build of configuration Debug for project Controller ****
Info: Internal Builder is used for build
g++ "-IC:\Users\Alsharif\eigen" -O0 -g3 -Wall -c -fmessage-length=0 -o ControllerCode2.o "..\ControllerCode2.cpp" 
In file included from C:UsersAlsharifeigen/Eigen/Core:285:0,
                 from C:UsersAlsharifeigen/Eigen/Dense:1,
                 from ..System.h:12,
                 from ..ControllerCode2.h:11,
                 from ..ControllerCode2.cpp:8:
C:UsersAlsharifeigen/Eigen/src/Core/Matrix.h: In instantiation of 'Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const Eigen::MatrixBase<OtherDerived>&) [with OtherDerived = Eigen::Matrix<std::complex<double>, -1, 1>; _Scalar = double; int _Rows = -1; int _Cols = -1; int _Options = 0; int _MaxRows = -1; int _MaxCols = -1]':
..ControllerCode2.cpp:220:45:   required from here
C:UsersAlsharifeigen/Eigen/src/Core/util/StaticAssert.h:115:9: 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>'
         if (Eigen::internal::static_assertion<static_cast<bool>(CONDITION)>::MSG) {}
         ^
C:UsersAlsharifeigen/Eigen/src/Core/Matrix.h:326:7: note: in expansion of macro 'EIGEN_STATIC_ASSERT'
       EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
       ^
15:12:55 Build Finished (took 4s.567ms)

所以您得到了一个静态断言:YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY

我不是Eigen方面的专家,但我看到eigenvalues()返回const EigenvalueType&这是一种不同的矩阵类型,而不是用于实例化EigenSolver(在您的情况下是MatrixXd)的矩阵类型。

所以您应该使用正确的类型,或者显式转换矩阵,这就是静态断言所说的。