/// <summary>Eigen Decomposition.</summary> /// <param name="A">Input Matrix.</param> /// <returns>Tuple(Eigen Values, Eigen Vectors)</returns> public static Tuple <Vector, Matrix> Evd(Matrix A) { Evd eigs = new Evd(A); eigs.Compute(); return(new Tuple <Vector, Matrix>(eigs.Eigenvalues, eigs.Eigenvectors)); }
public void CanFactorizeRandomSymmetricMatrix(int order, bool diagonal) { // create random matrix var rand = Matrix.Rand(order, 1.13); // create a symmetric positive definite matrix var A = rand.T * rand; if (diagonal) { var diag = A[0, 0]; for (var i = 1; i < order; i++) A[i, i] = diag; } var evd = new Evd(A); // compute eigenvalues/eigenvectors evd.Compute(); var eigenvectors = evd.Eigenvectors; var eigenvalues = evd.Eigenvalues; foreach (var e in eigenvalues) Assert.False(double.IsNaN(e)); Assert.Equal(order, eigenvectors.Rows); Assert.Equal(order, eigenvectors.Cols); Assert.Equal(order, eigenvalues.Length); // make sure that A = V*λ*VT var computed = eigenvectors * eigenvalues.Diag() * eigenvectors.T; Assert.True(A.Equals(computed, 1e-10)); }
public void CanFactorizeRandomSymmetricMatrix(int order) { // create random matrix var rand = Matrix.Rand(order, 1.13); // create a symmetric positive definite matrix var A = rand.T * rand; var evd = new Evd(A); // compute eigenvalues/eigenvectors evd.compute(); var eigenvectors = evd.Eigenvectors; var eigenvalues = evd.Eigenvalues; Assert.AreEqual(order, eigenvectors.Rows); Assert.AreEqual(order, eigenvectors.Cols); Assert.AreEqual(order, eigenvalues.Length); // make sure that A = V*λ*VT var computed = eigenvectors * eigenvalues.Diag() * eigenvectors.T; Assert.IsTrue(A.Equals(computed, 1e-10)); }
/// <summary>Eigen Decomposition.</summary> /// <param name="A">Input Matrix.</param> /// <returns>Tuple(Eigen Values, Eigen Vectors)</returns> public static Tuple<Vector, Matrix> Evd(Matrix A) { var eigs = new Evd(A); eigs.compute(); return new Tuple<Vector, Matrix>(eigs.Eigenvalues, eigs.Eigenvectors); }