public void GeneralizedEigenvalueDecompositionConstructorTest()
{
// Suppose we have the following
// matrices A and B shown below:
double[,] A =
{
{ 1, 2, 3},
{ 8, 1, 4},
{ 3, 2, 3}
};
double[,] B =
{
{ 5, 1, 1},
{ 1, 5, 1},
{ 1, 1, 5}
};
// Now, suppose we would like to find values for λ
// that are solutions for the equation det(A - λB) = 0
// For this, we can use a Generalized Eigendecomposition
var gevd = new GeneralizedEigenvalueDecomposition(A, B);
// Now, if A and B are Hermitian and B is positive
// -definite, then the eigenvalues λ will be real:
double[] lambda = gevd.RealEigenvalues;
// Check if they are indeed a solution:
for (int i = 0; i < lambda.Length; i++)
{
// Compute the determinant equation show above
double det = Matrix.Determinant(A.Subtract(lambda[i].Multiply(B))); // almost zero
Assert.IsTrue(det < 1e-6);
}
double[,] expectedVectors =
{
{ 0.427490473174445, -0.459244062074000, -0.206685960405416 },
{ 1, 1, -1},
{ 0.615202547759401, -0.152331764458173, 0.779372135871111}
};
double[,] expectedValues =
{
{1.13868666711946, 0, 0 },
{0, -0.748168231839396, 0},
{0, 0, -0.104804149565775}
};
Assert.IsTrue(Matrix.IsEqual(gevd.Eigenvectors, expectedVectors, 0.00000000001));
Assert.IsTrue(Matrix.IsEqual(gevd.DiagonalMatrix, expectedValues, 0.00000000001));
}