1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <complex>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
const double SMALL = 1.0E30; // used to stop dividebyzero
const double NEARZERO = 1.0E10; // interpretation of "zero"
using cmplx = complex<double>; // complex number
using vec = vector<cmplx>; // vector
using matrix = vector<vec>; // matrix (=collection of (row) vectors)
// Prototypes
matrix readMatrix();
int howBig( string filename );
void printMatrix( string title, const matrix &A );
matrix matMul( const matrix &A, const matrix &B );
matrix matSca( cmplx c, const matrix &A );
matrix matLin( cmplx a, const matrix &A, cmplx b, const matrix &B );
vec matVec( const matrix &A, const vec &V );
vec vecSca( cmplx c, const vec &V );
vec vecLin( cmplx a, const vec &U, cmplx b, const vec &V );
double vecNorm( const vec &V );
double matNorm( const matrix &A );
double subNorm( const matrix &T );
matrix identity( int N );
matrix hermitianTranspose( const matrix &A );
cmplx shift( const matrix &A );
void Hessenberg( const matrix &A, matrix &P, matrix &H );
void QRFactoriseGivens( const matrix &A, matrix &Q, matrix &R );
void QRHessenberg( const matrix &A, matrix &P, matrix &T );
bool eigenvectorUpper( const matrix &T, matrix &E );
//========
int main()
{
// Read matrix
matrix A = readMatrix();
printMatrix( "\nOriginal matrix:", A );
int N = A.size();
matrix P, T, E;
// Compute eigenvalues by QRHessenberg method.
// Gives a Schur decomposition: A = P T P1; P is unitary, T is uppertriangular
QRHessenberg( A, P, T );
cout << "\nEigenvalues by QR algorithm are:\n";
for ( int L = 0; L < N; L++ ) cout << T[L][L] << '\n';
// Compute eigenvectors
bool OK = eigenvectorUpper( T, E ); // Find the eigenvectors of T
E = matMul( P, E ); // Rotate eigenvectors to those of A
for ( int L = 0; L < N; L++ )
{
cmplx lambda = T[L][L];
vec V( N );
for ( int j = 0; j < N; j++ ) V[j] = E[j][L];
cout << "\n\nEigenvalue " << lambda << "\nEigenvector:\n";
for ( int j = 0; j < N; j++ ) cout << V[j] << '\n';
// Check matrix norm of A v  lambda v
cout << "Check error: " << vecNorm( vecLin( 1.0, matVec( A, V ), lambda, V ) ) << endl;
}
}
//========
matrix readMatrix() // Input the matrix
{
string filename = "matrix.dat";
// Determine how large the matrix is and set array sizes accordingly
int N = howBig( filename );
matrix A( N, vec(N) );
// Read from file
ifstream in( filename ); assert( in );
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < N; j++ ) in >> A[i][j];
}
in.close();
return A;
}
//========
int howBig( string filename ) // Reads one line to count the elements in it
{
string s;
ifstream in( filename ); assert( in );
getline( in, s );
in.close();
stringstream ss( s ); // Creates a stream from this line
int N = 0;
cmplx dummy;
while( ss >> dummy ) N++; // Increments N for as many values as present
return N;
}
//========
void printMatrix( string title, const matrix &A )
{
cout << title; if ( title != "" ) cout << '\n';
int m = A.size(), n = A[0].size(); // A is an m x n matrix
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < n; j++ )
{
double x = A[i][j].real(); if ( abs( x ) < NEARZERO ) x = 0.0;
double y = A[i][j].imag(); if ( abs( y ) < NEARZERO ) y = 0.0;
cout << cmplx( x, y ) << '\t';
}
cout << '\n';
}
}
//========
matrix matMul( const matrix &A, const matrix &B ) // Matrix times matrix
{
int mA = A.size(), nA = A[0].size();
int mB = B.size(), nB = B[0].size(); assert( mB == nA );
matrix C( mA, vec( nB, 0.0 ) );
for ( int i = 0; i < mA; i++ )
{
for ( int j = 0; j < nB; j++ )
{
for ( int k = 0; k < nA; k++ ) C[i][j] += A[i][k] * B[k][j];
}
}
return C;
}
//========
matrix matSca( cmplx c, const matrix &A ) // Scalar multiple of matrix
{
int m = A.size(), n = A[0].size();
matrix C = A;
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < n; j++ ) C[i][j] *= c;
}
return C;
}
//========
matrix matLin( cmplx a, const matrix &A, cmplx b, const matrix &B ) // Linear combination of matrices
{
int m = A.size(), n = A[0].size(); assert( B.size() == m && B[0].size() == n );
matrix C = matSca( a, A );
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < n; j++ ) C[i][j] += b * B[i][j];
}
return C;
}
//========
vec matVec( const matrix &A, const vec &V ) // Matrix times vector
{
int mA = A.size(), nA = A[0].size();
int mV = V.size(); assert( mV == nA );
vec C( mA, 0.0 );
for ( int i = 0; i < mA; i++ )
{
for ( int k = 0; k < nA; k++ ) C[i] += A[i][k] * V[k];
}
return C;
}
//========
vec vecSca( cmplx c, const vec &V ) // Scalar multiple of vector
{
int n = V.size();
vec W = V;
for ( int j = 0; j < n; j++ ) W[j] *= c;
return W;
}
//========
vec vecLin( cmplx a, const vec &U, cmplx b, const vec &V ) // Linear combination of vectors
{
int n = U.size(); assert( V.size() == n );
vec W = vecSca( a, U );
for ( int j = 0; j < n; j++ ) W[j] += b * V[j];
return W;
}
//========
double vecNorm( const vec &V ) // Complex vector norm
{
int n = V.size();
double result = 0.0;
for ( int j = 0; j < n; j++ ) result += norm( V[j] );
return sqrt( result );
}
//========
double matNorm( const matrix &A ) // Complex matrix norm
{
int m = A.size(), n = A[0].size();
double result = 0.0;
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < n; j++ ) result += norm( A[i][j] );
}
return sqrt( result );
}
//========
double subNorm( const matrix &T ) // Below leading diagonal of square matrix
{
int n = T.size(); assert( T[0].size() == n );
double result = 0.0;
for ( int i = 1; i < n; i++ )
{
for ( int j = 0; j < i; j++ ) result += norm( T[i][j] );
}
return sqrt( result );
}
//========
 