This forum has been archived. All content is frozen. Please use KDE Discuss instead.

ComplexSchur method not working

Tags: complexschur complexschur complexschur
(comma "," separated)
malsharif
Registered Member
Posts
9
Karma
0

ComplexSchur method not working

Wed Apr 01, 2015 12:02 pm
I am implementing some control algorithms in Eclipse C++ using the Eigen library. My code works fine when still I don't use the ComplexSchur method in it, but when I did so and cleaned and built the application, binaries where not generated, and thus the app won't run. If I use the method without cleaning and with only building the project, I will still have the binaries but in the output I don't see the results of the ComplexSchur method ... as if the excution jumped over it.

Any help will be deeply appreciated.

This is my main.cpp file:

Code: Select all
#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;
}


Now, the ControllerCode2.cpp code:

Code: Select all
#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();

// THE PROBLEM IS IN THIS BLOCK, which is nothing but an example from the Eigen website:   http://eigen.tuxfamily.org/dox/classEigen_1_1ComplexSchur.html
      MatrixXcf A = MatrixXcf::Random(4,4);
      ComplexSchur<MatrixXcf> schur2(4);
      schur2.compute(A);
      cout << endl << "The matrix T in the decomposition of A is:" << endl << schur2.matrixT() << endl;
      schur2.compute(A.inverse());
      cout << "The matrix T in the decomposition of A^(-1) is:" << endl << schur2.matrixT() << endl;
//////////////////////////////////////////

    /****** 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();

}


my ControllerCode2.h code:

Code: Select all
#ifndef CONTROLLERCODE_H_
#define CONTROLLERCODE_H_

#include "System.h"

    void schur_eigen_test( System );

//};

#endif /* CONTROLLERCODE_H_ */


System.h code:

Code: Select all
// 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_ */


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot]