Example usage for org.apache.commons.math3.linear CholeskyDecomposition getL

List of usage examples for org.apache.commons.math3.linear CholeskyDecomposition getL

Introduction

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

Prototype

public RealMatrix getL() 

Source Link

Document

Returns the matrix L of the decomposition.

Usage

From source file:com.joptimizer.algebra.CholeskyDecompositionTest.java

/**
 * good decomposition./* w  w  w. j  av  a2  s . c  o m*/
 */
public void testDecomposition1() throws Exception {
    log.debug("testDecomposition1");
    RealMatrix P1 = new Array2DRowRealMatrix(new double[][] { { 8.08073550734687, 1.59028724315583 },
            { 1.59028724315583, 0.3250861184011492 } });
    CholeskyDecomposition cFact1 = new CholeskyDecomposition(P1);
    log.debug("L: " + cFact1.getL());
    log.debug("LT: " + cFact1.getLT());
    // check L.LT-Q=0
    RealMatrix P1Inv = cFact1.getL().multiply(cFact1.getLT());
    double norm1 = P1Inv.subtract(P1).getNorm();
    log.debug("norm1: " + norm1);
    assertTrue(norm1 < 1.E-12);
}

From source file:com.joptimizer.algebra.CholeskyDecompositionTest.java

/**
 * poor decomposition./*from   w  ww.ja v  a 2s  . com*/
 * rescaling can help in doing it better
 */
public void testDecomposition2() throws Exception {
    log.debug("testDecomposition2");
    RealMatrix P1 = new Array2DRowRealMatrix(new double[][] { { 8.185301256666552E9, 1.5977225251367908E9 },
            { 1.5977225251367908E9, 3.118660129093004E8 } });
    CholeskyDecomposition cFact1 = new CholeskyDecomposition(P1);
    log.debug("L: " + cFact1.getL());
    log.debug("LT: " + cFact1.getLT());
    // check L.LT-Q=0
    double norm1 = cFact1.getL().multiply(cFact1.getLT()).subtract(P1).getNorm();
    log.debug("norm1: " + norm1);
    assertTrue(norm1 < 1.E-5);

    //poor precision, try to make it better

    //geometric eigenvalues mean
    DescriptiveStatistics ds = new DescriptiveStatistics(new double[] { 8.5E9, 0.00572 });
    RealMatrix P2 = P1.scalarMultiply(1. / ds.getGeometricMean());
    CholeskyDecomposition cFact2 = new CholeskyDecomposition(P2);
    log.debug("L: " + cFact2.getL());
    log.debug("LT: " + cFact2.getLT());
    // check L.LT-Q=0
    double norm2 = cFact2.getL().multiply(cFact2.getLT()).subtract(P2).getNorm();
    log.debug("norm2: " + norm2);
    assertTrue(norm2 < Utils.getDoubleMachineEpsilon());
}

From source file:com.joptimizer.solvers.CholeskyTest.java

/**
 * poor decomposition./*from   ww  w . j av a2 s.c  o m*/
 * rescaling can help in doing it better
 */
public void testDecomposition2() throws Exception {
    log.debug("testDecomposition2");
    RealMatrix P1 = new Array2DRowRealMatrix(new double[][] { { 8.185301256666552E9, 1.5977225251367908E9 },
            { 1.5977225251367908E9, 3.118660129093004E8 } });
    CholeskyDecomposition cFact1 = new CholeskyDecomposition(P1);
    log.debug("L: " + cFact1.getL());
    log.debug("LT: " + cFact1.getLT());
    // check L.LT-Q=0
    double norm1 = cFact1.getL().multiply(cFact1.getLT()).subtract(P1).getNorm();
    log.debug("norm1: " + norm1);
    assertTrue(norm1 < 1.E-5);

    //poor precision, try to make it better

    //geometric eigenvalues mean
    DescriptiveStatistics ds = new DescriptiveStatistics(new double[] { 8.5E9, 0.00572 });
    RealMatrix P2 = P1.scalarMultiply(1. / ds.getGeometricMean());
    CholeskyDecomposition cFact2 = new CholeskyDecomposition(P2);
    log.debug("L: " + cFact2.getL());
    log.debug("LT: " + cFact2.getLT());
    // check L.LT-Q=0
    double norm2 = cFact2.getL().multiply(cFact2.getLT()).subtract(P2).getNorm();
    log.debug("norm2: " + norm2);
    assertTrue(norm2 < 1.E-9);
}

From source file:com.opengamma.strata.math.impl.linearalgebra.CholeskyDecompositionCommonsResult.java

/**
 * Constructor./*from  ww  w .  j  av a 2s  .com*/
 * @param ch The result of the Cholesky decomposition.
 */
public CholeskyDecompositionCommonsResult(CholeskyDecomposition ch) {
    ArgChecker.notNull(ch, "Cholesky decomposition");
    _determinant = ch.getDeterminant();
    _l = CommonsMathWrapper.unwrap(ch.getL());
    _lt = CommonsMathWrapper.unwrap(ch.getLT());
    _solver = ch.getSolver();
}

From source file:org.apache.sysml.runtime.matrix.data.LibCommonsMath.java

/**
 * Function to compute Cholesky decomposition of the given input matrix. 
 * The input must be a real symmetric positive-definite matrix.
 * /*from   w  w w  .  ja  v  a  2 s  .  c  om*/
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 * @throws DMLRuntimeException if DMLRuntimeException occurs
 */
private static MatrixBlock computeCholesky(Array2DRowRealMatrix in) throws DMLRuntimeException {
    if (!in.isSquare())
        throw new DMLRuntimeException("Input to cholesky() must be square matrix -- given: a "
                + in.getRowDimension() + "x" + in.getColumnDimension() + " matrix.");

    CholeskyDecomposition cholesky = new CholeskyDecomposition(in);
    RealMatrix rmL = cholesky.getL();

    return DataConverter.convertToMatrixBlock(rmL.getData());
}

From source file:org.eclipse.dataset.LinearAlgebra.java

/**
 * Calculate Cholesky decomposition A = L L^T
 * @param a// ww  w .  ja  v a  2 s . co m
 * @return L
 */
public static Dataset calcCholeskyDecomposition(Dataset a) {
    CholeskyDecomposition cd = new CholeskyDecomposition(createRealMatrix(a));
    return createDataset(cd.getL());
}

From source file:org.meteoinfo.math.linalg.LinalgUtil.java

/**
 * Calculates the Cholesky decomposition of a matrix.
 * The Cholesky decomposition of a real symmetric positive-definite matrix A consists of a 
 * lower triangular matrix L with same size such that: A = LLT. In a sense, this is 
 * the square root of A./*w w  w.ja v  a  2  s. co  m*/
 * @param a The given matrix.
 * @return Result array.
 */
public static Array cholesky(Array a) {
    Array r = Array.factory(DataType.DOUBLE, a.getShape());
    double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a);
    RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
    CholeskyDecomposition decomposition = new CholeskyDecomposition(matrix);
    RealMatrix L = decomposition.getL();
    int n = L.getColumnDimension();
    int m = L.getRowDimension();
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            r.setDouble(i * n + j, L.getEntry(i, j));
        }
    }

    return r;
}

From source file:org.ujmp.commonsmath.AbstractCommonsMathDenseDoubleMatrix2D.java

public Matrix chol() {
    try {// ww w  .j av a  2  s . c  o m
        CholeskyDecomposition chol = new CholeskyDecomposition(matrix);
        Matrix l = CommonsMathDenseDoubleMatrix2DFactory.INSTANCE.dense(chol.getL());
        return l;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}