Jun 24, 2009

eigen value and eigen vector

Theory
Rice university theory
Very useful
Examples

Here is the code to calculate the eigen value for 2x2 matrix

void calculateEigenValues( vector > matrix, vector & result) {

// matrix [][] = [ a b ]
// [ c d ]

double a, b, c,
d;
a = matrix[0][0];
b = matrix[0][1];
c = matrix[1][0];
d = matrix[1][1];

// find the det(M - lambda x I) = 0 equation solution
// final equation becomes
// lambda*lambda - (a+d) * lambda + (a*d - c*b) = 0;
double B = -(a + d);
double C = a*d - c*b;

// the quadratic solution = (-B +- sqrt(B*B - 4*C) )/2
double Q = B*B - 4*C;
double P = sqrt(Q);
cout<<"B "<
double firstSol = -B + P;
firstSol = firstSol/2;

double secondSol = -B - P;
secondSol = secondSol/2;

// sort them non decreasing order and push them into result vector
if ( secondSol < firstSol ) {
double temp = firstSol;
firstSol = secondSol;
secondSol = temp;
}


result.push_back(firstSol);
result.push_back(secondSol);

}

No comments: