Spectral Integral Suite in C++
tryNSLyap.cpp
Go to the documentation of this file.
1 
2 #include <fstream> // To output data to files
3 #include <iostream>
4 #include <stdio.h>
5 #include <sis.hpp>
6 
7 
8 using namespace std;
9 #ifndef CD_T
10 typedef complex<double> Cd_t;
11 #endif
12 extern "C" {
13  void sb04od_(char *REDUCE, char *TRANS, char *JOBD, int *M, int *N, double *A, int *LDA, double *B, int *LDB, double *C, int *LDC, double *D, int *LDD, double *E, int *LDE, double *F, int *LDF, double *SCALE, double *DIF, double *P, int *LDP, double *Q, int *LDQ, double *U, int *LDU, double *V, int *LDV, int* IWORK, double* DWORK, int* LDWORK, int* INFO);
14 }
15 
16 
17 
18 //#define DEBUG
19 
20 #include <lyap.h>
21 
22 int main()
23 {
24  Eigen::MatrixXd A(4,4), B(4,4), C(4,4), D(4,4), E(4,4), F(4,4), P(4,4), Q(4,4), U(4,4), V(4,4), A1(4,4), B1(4,4), C1(4,4), D1(4,4), E1(4,4), F1(4,4);
25  A = Eigen::MatrixXd::Random(4,4);
26  B = Eigen::MatrixXd::Random(4, 4);
27  C = Eigen::MatrixXd::Random(4, 4);
28  D = Eigen::MatrixXd::Random(4, 4);
29  E = Eigen::MatrixXd::Random(4, 4);
30  F = Eigen::MatrixXd::Random(4, 4);
31 
32  A1 = A;
33  B1 = B;
34  C1 = C;
35  D1 = D;
36  E1 = E;
37  F1 = F;
38 
39 
40  int sizeA = A.rows();
41  int lwork = -1; // set lwork to -1 to estimate workspace.
42  char reduce = 'R';
43  char trans = 'N';
44  char jobd = 'N';
45  int M = 4;
46  int N = 4;
47  int ldA = A.outerStride(); // ld for leading dimension
48  int ldB = B.outerStride();
49  int ldC = C.outerStride();
50  int ldD = D.outerStride();
51  int ldE = E.outerStride();
52  int ldF = F.outerStride();
53  int ldP = P.outerStride();
54  int ldQ = Q.outerStride();
55  int ldU = U.outerStride();
56  int ldV = V.outerStride();
57  double scale = 1.0;
58  double dif;
59  vector<int> iwork;
60  iwork.resize(M+N+6);
61  vector<double> dwork;
62  dwork.resize(1);
63  int ldwork = -1;
64  int info;
65  cout << __LINE__ << endl
66  << flush;
67 
68  // call this to estimate workspace
69  sb04od_(&reduce, &trans, &jobd, &M, &N, A.data(), &ldA, B.data(), &ldB, C.data(), &ldC, D.data(), &ldD, E.data(), &ldE, F.data(), &ldF, &scale, &dif, P.data(), &ldP, Q.data(), &ldQ, U.data(), &ldU, V.data(), &ldV, &iwork[0], &dwork[0], &ldwork, &info);
70 
71 
72  ldwork = dwork[0];
73  cout << "ldwork: "<< ldwork << endl;
74  dwork.resize(ldwork);
75  sb04od_(&reduce, &trans, &jobd, &M, &N, A.data(), &ldA, B.data(), &ldB, C.data(), &ldC, D.data(), &ldD, E.data(), &ldE, F.data(), &ldF, &scale, &dif, P.data(), &ldP, Q.data(), &ldQ, U.data(), &ldU, V.data(), &ldV, &iwork[0], &dwork[0], &ldwork, &info);
76  cout << __LINE__ << endl
77  << flush;
78 
79  cout << "Scale = " << scale << endl;
80  cout << "A = " << A << endl;
81  cout << "B = " << B << endl;
82  cout << "C = " << C << endl;
83  cout << "E = " << E << endl;
84  cout << "F = " << F << endl;
85  cout << "P = " << P << endl;
86  cout << "Q = " << Q << endl;
87  cout << __LINE__ << endl
88  << flush;
89 
90  cout << "First equation: " << A1 * C - F * B1 - scale * C1 << endl;
91  cout << "Second equation: " << D1 * C - F * E1 - scale * F1 << endl;
92  cout << __LINE__ << endl
93  << flush;
94 
95  char jobvl = 'N'; // Don't compute left evecs
96  char jobvr = 'V'; // Compute right evecs
97  std::complex<double> wkopt; // Eistimate optimum workspace
98  std::complex<double> *work; // allocate optimum workspace
99 
100  Eigen::Matrix<std::complex<double>, Eigen::Dynamic, Eigen::Dynamic> Ac(1000, 1000), Bc(1000, 1000), vl(1000, 1000), vr(1000, 1000), eigenvalues_temp(1000, 1), alpha_temp(1000, 1), beta_temp(1000, 1), alpha(1000, 1), beta(1000, 1);
101 
102  cout << __LINE__ << endl
103  << flush;
104 
105  Ac = Eigen::MatrixXcd::Random(1000, 1000);
106  Bc = Eigen::MatrixXcd::Random(1000, 1000);
107  //cout << "Ac = " << Ac << endl;
108  //cout << "Bc = " << Bc << endl;
109  int ldac = Ac.outerStride();
110  int ldbc = Bc.outerStride();
111 
112  // vl : left evecs, vr: right evecs.
113  int ldvl = vl.outerStride();
114  cout << "ldvl = " << ldvl << endl;
115  int ldvr = vr.outerStride();
116  double rwork[8 * 1000];
117 
118  char sort = 'S';
119  int sdim = 0;
120  lwork = -1;
121  vector<Cd_t> workc;
122  workc.resize(1);
123  bool *bwork;
124  bwork = new bool[1000];
125 
126  zgges_(&jobvl, &jobvr, &sort, &criteria_, &N, Ac.data(), &ldac, Bc.data(), &ldbc, &sdim, alpha.data(), beta.data(), vl.data(), &ldvl, vr.data(), &ldvr, &workc[0], &lwork, &rwork[0], bwork, &info);
127 
128  cout << workc[0];
129  lwork = int(real(workc[0]));
130  workc.resize(lwork);
131 
132  zgges_(&jobvl, &jobvr, &sort, &criteria_, &N, Ac.data(), &ldac, Bc.data(), &ldbc, &sdim, alpha.data(), beta.data(), vl.data(), &ldvl, vr.data(), &ldvr, &workc[0], &lwork, &rwork[0], bwork, &info);
133 
134  // cout << "Alpha: " << alpha << endl;
135  // cout << "beta: " << beta << endl;
136  cout << "info = " << info << endl;
137  delete bwork;
138 
139  Eigen::MatrixXcd AA(4,4), BB(4,4), QQ(4,4);
140  AA = Eigen::MatrixXcd::Random(4,4);
141  BB = Eigen::MatrixXcd::Random(4, 4);
142  QQ = Eigen::MatrixXcd::Random(4, 4);
143  Eigen::MatrixXcd X1 = dlyap(AA,BB,QQ);
144  // Check norm:
145  cout << "Testing dlyap: " << (AA*X1*BB - X1 + QQ).norm() << endl;
146 
147  Eigen::MatrixXcd X2 = lyap(AA,BB,Q);
148  // Check norm:
149  cout << "Testing lyap: " << (AA*X2 + X2*BB + Q).norm() << endl;
150  //Testing OrdQz:
151 
152  OrdQz qz(AA,BB);
153  cout << "qz info : " << qz.info << endl;
154  cout << "Test qz 1: " << (qz.VSL*qz.S*qz.VSR.adjoint() - AA).norm() << endl;
155  cout << "Test qz 2: " << (qz.VSL * qz.T * qz.VSR.adjoint() - BB).norm() << endl;
156  return 0;
157 }
Eigen::MatrixXcd lyap(Eigen::MatrixXcd A, Eigen::MatrixXcd B, Eigen::MatrixXcd Q)
Solves the Lyapunov/ sylvester equation: AX + XB + Q = 0.
Definition: lyap.h:133
bool criteria_(Cd_t alpha, Cd_t beta)
Definition: lyap.h:16
Definition: lyap.h:34
Definition: sis.hpp:260
Eigen::MatrixXcd S
Definition: lyap.h:36
int main()
Definition: tryNSLyap.cpp:22
int info
Definition: lyap.h:37
void sb04od_(char *REDUCE, char *TRANS, char *JOBD, int *M, int *N, double *A, int *LDA, double *B, int *LDB, double *C, int *LDC, double *D, int *LDD, double *E, int *LDE, double *F, int *LDF, double *SCALE, double *DIF, double *P, int *LDP, double *Q, int *LDQ, double *U, int *LDU, double *V, int *LDV, int *IWORK, double *DWORK, int *LDWORK, int *INFO)
Eigen::MatrixXcd VSR
Definition: lyap.h:36
Eigen::MatrixXcd T
Definition: lyap.h:36
void zgges_(char *JOBVSL, char *JOBVSR, char *SORT, bool(*SELCTG)(Cd_t a, Cd_t b), int *N, Cd_t *A, int *LDA, Cd_t *B, int *LDB, int *SDIM, Cd_t *ALPHA, Cd_t *BETA, Cd_t *VSL, int *LDVSL, Cd_t *VSR, int *LDVSR, Cd_t *WORK, int *LWORK, double *RWORK, bool *BWORK, int *INFO)
complex< double > Cd_t
Definition: tryNSLyap.cpp:10
Eigen::MatrixXcd VSL
Definition: lyap.h:36
int N
Specifies number of Chebyshev polynomials, default N = 31.
Definition: sis.hpp:472
Eigen::MatrixXcd dlyap(Eigen::MatrixXcd A, Eigen::MatrixXcd B, Eigen::MatrixXcd Q)
Solves the Lyapunov/ sylvester equation: AXB - X + Q = 0.
Definition: lyap.h:217
valarray< T > real(const valarray< complex< T > > &in)
real part of a complex valarray
Definition: sis.hpp:280