Example usage for org.apache.commons.math3.linear SingularMatrixException getMessage

List of usage examples for org.apache.commons.math3.linear SingularMatrixException getMessage

Introduction

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

Prototype

@Override
public String getMessage() 

Source Link

Usage

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

/**
 * @see http://en.wikipedia.org/wiki/Machine_epsilon#Approximation_using_Java
 *///  w w w.  j  a  va  2s  .c  om
//   public static final float calculateFloatMachineEpsilon() {
//      float eps = 1.0f;
//      do {
//         eps /= 2.0f;
//      } while ((float) (1.0 + (eps / 2.0)) != 1.0);
//      Log.d(MainActivity.JOPTIMIZER_LOGTAG,"Calculated float machine epsilon: " + eps);
//      return eps;
//   }

public static RealMatrix squareMatrixInverse(RealMatrix M) throws SingularMatrixException {
    if (!M.isSquare()) {
        throw new IllegalArgumentException("Not square matrix!");
    }

    // try commons-math cholesky
    try {
        CholeskyDecomposition cd = new CholeskyDecomposition(M);
        return cd.getSolver().getInverse();
    } catch (SingularMatrixException e) {
        throw e;
    } catch (Exception e) {
        Log.d(MainActivity.JOPTIMIZER_LOGTAG, e.getMessage());
    }

    // try joptimizer cholesky
    try {
        CholeskyFactorization cd = new CholeskyFactorization(M.getData());
        double[][] MInv = cd.getInverse();
        return MatrixUtils.createRealMatrix(MInv);
    } catch (Exception e) {
        Log.d(MainActivity.JOPTIMIZER_LOGTAG, e.getMessage());
    }

    // try LU
    try {
        LUDecomposition ld = new LUDecomposition(M);
        return ld.getSolver().getInverse();
    } catch (SingularMatrixException e) {
        throw e;
    } catch (Exception e) {
        Log.d(MainActivity.JOPTIMIZER_LOGTAG, e.getMessage());
    }

    // try QR
    try {
        QRDecomposition qr = new org.apache.commons.math3.linear.QRDecomposition(M);
        return qr.getSolver().getInverse();
    } catch (SingularMatrixException e) {
        throw e;
    } catch (Exception e) {
        Log.d(MainActivity.JOPTIMIZER_LOGTAG, e.getMessage());
    }

    return null;
}