Example usage for org.apache.commons.math3.linear MatrixUtils isSymmetric

List of usage examples for org.apache.commons.math3.linear MatrixUtils isSymmetric

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear MatrixUtils isSymmetric.

Prototype

public static boolean isSymmetric(RealMatrix matrix, double eps) 

Source Link

Document

Checks whether a matrix is symmetric.

Usage

From source file:com.joptimizer.util.CholeskyFactorization.java

/**
 * Cholesky factorization L of psd matrix, Q = L.LT
 *//*www .  j  a va 2 s .c  o m*/
private void factorize() throws Exception {
    if (!MatrixUtils.isSymmetric(new Array2DRowRealMatrix(Q), Utils.getDoubleMachineEpsilon())) {
        throw new Exception("Matrix is not symmetric");
    }

    int N = Q.length;
    double[][] L = new double[N][N];
    this.eigenvalues = new ArrayList<Double>();

    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= i; j++) {
            double sum = 0.0;
            for (int k = 0; k < j; k++) {
                sum += L[i][k] * L[j][k];
            }
            if (i == j) {
                double d = Math.sqrt(Q[i][i] - sum);
                if (Double.isNaN(d) || d * d < Utils.getDoubleMachineEpsilon()) {//d*d is a Q's eigenvalue
                    Log.w(MainActivity.JOPTIMIZER_LOGTAG, "Not positive eigenvalues: " + d * d);
                    throw new Exception("not positive definite matrix");
                }
                L[i][i] = d;
                this.eigenvalues.add(this.eigenvalues.size(), d * d);
            } else {
                L[i][j] = 1.0 / L[j][j] * (Q[i][j] - sum);
            }
        }
    }

    this.L = L;
}

From source file:org.pmad.gmm.MyEDC.java

/**
 * Calculates the eigen decomposition of the given real matrix.
 * <p>//  www  . j a va 2s.  co  m
 * Supports decomposition of a general matrix since 3.1.
 *
 * @param matrix Matrix to decompose.
 * @throws MaxCountExceededException if the algorithm fails to converge.
 * @throws MathArithmeticException if the decomposition of a general matrix
 * results in a matrix with zero norm
 * @since 3.1
 */
public MyEDC(final RealMatrix matrix) throws MathArithmeticException {
    final double symTol = 10 * matrix.getRowDimension() * matrix.getColumnDimension() * Precision.EPSILON;
    isSymmetric = MatrixUtils.isSymmetric(matrix, symTol);
    if (isSymmetric) {
        transformToTridiagonal(matrix);
        findEigenVectors(transformer.getQ().getData());
    } else {
        final SchurTransformer t = transformToSchur(matrix);
        findEigenVectorsFromSchur(t);
    }
}