/** * <p> * Puts all the real eigenvectors into the columns of a matrix. If an eigenvalue is imaginary * then the corresponding eigenvector will have zeros in its column. * </p> * * @param eig An eigenvalue decomposition which has already decomposed a matrix. * @return An m by m matrix containing eigenvectors in its columns. */ public static FMatrixRMaj createMatrixV(EigenDecomposition_F32 <FMatrixRMaj> eig) { int N = eig.getNumberOfEigenvalues(); FMatrixRMaj V = new FMatrixRMaj(N, N); for (int i = 0; i < N; i++) { Complex_F32 c = eig.getEigenvalue(i); if (c.isReal()) { FMatrixRMaj v = eig.getEigenVector(i); if (v != null) { for (int j = 0; j < N; j++) { V.set(j, i, v.get(j, 0)); } } } } return(V); }
/** * <p> * Computes a metric which measures the the quality of an eigen value decomposition. If a * value is returned that is close to or smaller than 1e-15 then it is within machine precision. * </p> * <p> * EVD quality is defined as:<br> * <br> * Quality = ||A*V - V*D|| / ||A*V||. * </p> * * @param orig The original matrix. Not modified. * @param eig EVD of the original matrix. Not modified. * @return The quality of the decomposition. */ public static float quality(FMatrixRMaj orig, EigenDecomposition_F32 <FMatrixRMaj> eig) { FMatrixRMaj A = orig; FMatrixRMaj V = EigenOps_FDRM.createMatrixV(eig); FMatrixRMaj D = EigenOps_FDRM.createMatrixD(eig); // L = A*V FMatrixRMaj L = new FMatrixRMaj(A.numRows, V.numCols); CommonOps_FDRM.mult(A, V, L); // R = V*D FMatrixRMaj R = new FMatrixRMaj(V.numRows, D.numCols); CommonOps_FDRM.mult(V, D, R); FMatrixRMaj diff = new FMatrixRMaj(L.numRows, L.numCols); CommonOps_FDRM.subtract(L, R, diff); float top = NormOps_FDRM.normF(diff); float bottom = NormOps_FDRM.normF(L); float error = top / bottom; return(error); }
/** * * @param computeVectors * @param tol Tolerance for a matrix being symmetric */ public SwitchingEigenDecomposition_FDRM(int matrixSize, bool computeVectors, float tol) { symmetricAlg = DecompositionFactory_FDRM.eig(matrixSize, computeVectors, true); generalAlg = DecompositionFactory_FDRM.eig(matrixSize, computeVectors, false); this.computeVectors = computeVectors; this.tol = tol; }
/** * <p> * Checks to see if the matrix is positive semidefinite: * </p> * <p> * x<sup>T</sup> A x ≥ 0<br> * for all x where x is a non-zero vector and A is a symmetric matrix. * </p> * * @param A square symmetric matrix. Not modified. * * @return True if it is positive semidefinite and false if it is not. */ public static bool isPositiveSemidefinite(FMatrixRMaj A) { if (!isSquare(A)) { return(false); } EigenDecomposition_F32 <FMatrixRMaj> eig = DecompositionFactory_FDRM.eig(A.numCols, false); if (eig.inputModified()) { A = (FMatrixRMaj)A.copy(); } eig.decompose(A); for (int i = 0; i < A.numRows; i++) { Complex_F32 v = eig.getEigenvalue(i); if (v.getReal() < 0) { return(false); } } return(true); }
/** * <p> * A diagonal matrix where real diagonal element contains a real eigenvalue. If an eigenvalue * is imaginary then zero is stored in its place. * </p> * * @param eig An eigenvalue decomposition which has already decomposed a matrix. * @return A diagonal matrix containing the eigenvalues. */ public static FMatrixRMaj createMatrixD(EigenDecomposition_F32 <FMatrixRMaj> eig) { int N = eig.getNumberOfEigenvalues(); FMatrixRMaj D = new FMatrixRMaj(N, N); for (int i = 0; i < N; i++) { Complex_F32 c = eig.getEigenvalue(i); if (c.isReal()) { D.set(i, i, c.real); } } return(D); }