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

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

Introduction

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

Prototype

public CholeskyDecomposition(final RealMatrix matrix) 

Source Link

Document

Calculates the Cholesky decomposition of the given matrix.

Usage

From source file:edu.cmu.tetrad.util.TetradMatrix1.java

public TetradMatrix1 symmetricInverse() {
    if (!isSquare())
        throw new IllegalArgumentException();
    if (rows() == 0)
        return new TetradMatrix1(0, 0);

    // Using LUDecomposition.
    // other options: QRDecomposition, CholeskyDecomposition, EigenDecomposition, QRDecomposition,
    // RRQRDDecomposition, SingularValueDecomposition. Very cool. Also MatrixUtils.blockInverse,
    // though that can't handle matrices of size 1. Many ways to invert.

    // Note CholeskyDecomposition only takes inverses of symmetric matrices.
    return new TetradMatrix1(new CholeskyDecomposition(apacheData).getSolver().getInverse());
    //        return new TetradMatrix(new EigenDecomposition(apacheData).getSolver().getInverse());
    //        return new TetradMatrix(new QRDecomposition(apacheData).getSolver().getInverse());

    //        return new TetradMatrix(new SingularValueDecomposition(apacheData).getSolver().getInverse());
    //        return new TetradMatrix(new LUDecomposition(apacheData).getSolver().getInverse());
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.Experiments.java

private static void writeClustering(final MetricConstrainedKMeans kmeans, final File root, final int iter)
        throws FileNotFoundException {
    Csv.write(new PrintStream(new File(root, "M" + iter + ".csv")), kmeans.metric);
    {//from   w  w  w . jav a2 s .com
        final Csv.Writer writer = new Csv.Writer(new PrintStream(new File(root, "mu" + iter + ".csv")));
        for (int i = 0; i < kmeans.d; ++i) {
            for (int j = 0; j < kmeans.k; ++j) {
                writer.cell(kmeans.mu()[j].getEntry(i));
            }
            writer.newline();
        }
    }
    // Lt.operate( x ) maps x to the space defined by the metric
    final RealMatrix Lt = new CholeskyDecomposition(kmeans.metric).getLT();
    {
        final Csv.Writer writer = new Csv.Writer(new PrintStream(new File(root, "X" + iter + ".csv")));
        writer.cell("cluster").cell("label");
        for (int i = 0; i < kmeans.metric.getColumnDimension(); ++i) {
            writer.cell("x" + i);
        }
        for (int i = 0; i < kmeans.metric.getColumnDimension(); ++i) {
            writer.cell("Ax" + i);
        }
        writer.newline();
        for (int cluster = 0; cluster < kmeans.k; ++cluster) {
            for (int i = 0; i < kmeans.N; ++i) {
                if (kmeans.assignments()[i] == cluster) {
                    writer.cell(cluster);
                    final RealVector phi = kmeans.X_.get(i); //Phi.get( i );
                    writer.cell("?"); // TODO: write label
                    for (int j = 0; j < phi.getDimension(); ++j) {
                        writer.cell(phi.getEntry(j));
                    }
                    final RealVector trans = Lt.operate(phi);
                    for (int j = 0; j < trans.getDimension(); ++j) {
                        writer.cell(trans.getEntry(j));
                    }
                    writer.newline();
                }
            }
        }
    }
}

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. j  a v a2 s.com
 * @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.deeplearning4j.clustering.util.MathUtils.java

/**
 * This will return the cholesky decomposition of
 * the given matrix/*from  w w w . j  a v a 2  s .  c  o  m*/
 * @param m the matrix to convert
 * @return the cholesky decomposition of the given
 * matrix.
 * See:
 * http://en.wikipedia.org/wiki/Cholesky_decomposition
 * @throws NonSquareMatrixException
 */
public CholeskyDecomposition choleskyFromMatrix(RealMatrix m) throws Exception {
    return new CholeskyDecomposition(m);
}

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

/**
 * Calculate Cholesky decomposition A = L L^T
 * @param a//from   w  w w  .j a va 2  s.c  om
 * @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./* ww w .jav a 2  s.  c  om*/
 * @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.moeaframework.core.operator.real.AdaptiveMetropolisTest.java

/**
 * This is sample output from running Jasper Vrugt's AMALGAM codes in
 * MATLAB that is positive definite.//from  ww w . j ava 2s  . c o m
 */
@Test
public void testVrugtSample() {
    RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] {
            { 0.0000, 0.0002, -0.0006, 0.0008, 0.0001, 0.0018, 0.0001, 0.0003, 0.0006, -0.0035 },
            { 0.0934, -0.0010, -0.0062, 0.0001, 0.0024, 0.0017, 0.0019, 0.0004, 0.0008, -0.0067 },
            { 0.1435, -0.0047, 0.0007, -0.0051, -0.0049, 0.0054, 0.0052, 0.0018, 0.0016, 0.0014 },
            { 0.0026, 0.0001, -0.0001, 0.0002, 0.0001, 0.0004, -0.0002, -0.0002, 0.0000, -0.0010 },
            { 0.0262, 0.0028, -0.0003, 0.0019, -0.0013, 0.0002, -0.0009, -0.0004, -0.0012, -0.0021 },
            { 0.0432, 0.0005, -0.0006, 0.0008, 0.0001, 0.0018, -0.0015, 0.0003, 0.0006, -0.0035 },
            { 0.1220, -0.0028, -0.0082, 0.0006, 0.0033, -0.0044, -0.0019, -0.0033, -0.0029, 0.0010 },
            { 0.0657, -0.0049, -0.0005, 0.0004, 0.0016, 0.0020, -0.0010, 0.0047, -0.0047, -0.0033 },
            { 0.1594, 0.0001, -0.0001, 0.0026, 0.0058, 0.0004, -0.0001, -0.0002, 0.0000, -0.0018 },
            { 0.1329, 0.0057, -0.0010, -0.0053, 0.0026, 0.0048, 0.0061, 0.0019, 0.0023, -0.0020 },
            { 0.0998, 0.0029, 0.0036, -0.0014, 0.0015, 0.0018, -0.0035, 0.0006, -0.0013, 0.0083 },
            { 0.0160, -0.0003, -0.0009, 0.0008, 0.0006, -0.0003, -0.0010, 0.0008, -0.0005, -0.0033 },
            { 0.0061, -0.0001, -0.0001, -0.0001, 0.0001, 0.0001, 0.0001, 0.0000, 0.0000, -0.0003 } });

    double jumpRate = Math.pow(2.4 / Math.sqrt(10), 2.0);

    RealMatrix actual = new CholeskyDecomposition(
            new Covariance(matrix.scalarMultiply(jumpRate)).getCovarianceMatrix()).getLT();

    RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {
            { 0.0335, -0.0001, -0.0003, -0.0006, 0.0005, 0.0003, 0.0006, 0.0000, 0.0001, 0.0005 },
            { 0.0000, 0.0017, 0.0005, -0.0002, 0.0004, 0.0002, 0.0001, -0.0002, 0.0005, 0.0003 },
            { 0.0000, 0.0000, 0.0016, -0.0003, -0.0006, 0.0008, -0.0000, 0.0006, 0.0001, 0.0010 },
            { 0.0000, 0.0000, 0.0000, 0.0012, 0.0008, -0.0007, -0.0010, -0.0003, -0.0004, -0.0001 },
            { 0.0000, 0.0000, 0.0000, 0.0000, 0.0009, -0.0001, -0.0001, 0.0003, -0.0002, -0.0001 },
            { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0008, 0.0006, 0.0005, 0.0004, -0.0015 },
            { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0007, -0.0001, 0.0004, -0.0006 },
            { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0005, -0.0005, -0.0003 },
            { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0003, -0.0003 },
            { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0003 } });

    TestUtils.assertEquals(expected, actual, new AbsoluteError(0.0001));
}

From source file:org.ojalgo.benchmark.contestant.ACM.java

@Override
public BenchmarkContestant<RealMatrix>.HermitianSolver getHermitianSolver() {
    return new HermitianSolver() {

        @Override//from w ww. ja  va2s .  c  o m
        public RealMatrix apply(final RealMatrix body, final RealMatrix rhs) {

            final CholeskyDecomposition tmpCholesky = new CholeskyDecomposition(body);

            return tmpCholesky.getSolver().solve(rhs);
        }

    };
}

From source file:org.ojalgo.benchmark.lab.library.ACM.java

@Override
public ProducingBinaryMatrixMatrixOperation<RealMatrix, Array2DRowRealMatrix> getOperationEquationSystemSolver(
        final int numbEquations, final int numbVariables, final int numbSolutions, final boolean spd) {
    if (numbEquations == numbVariables) {
        if (spd) {
            return (body, rhs) -> {
                final CholeskyDecomposition cholesky = new CholeskyDecomposition(body);
                return cholesky.getSolver().solve(rhs);
            };/*  w w w  .  ja  v  a2  s.  c  o m*/
        } else {
            return (body, rhs) -> {
                final LUDecomposition lu = new LUDecomposition(body);
                return lu.getSolver().solve(rhs);
            };
        }
    } else if (numbEquations > numbVariables) {
        return (body, rhs) -> {
            final QRDecomposition qr = new QRDecomposition(body);
            return qr.getSolver().solve(rhs);
        };
    } else {
        return null;
    }
}

From source file:org.ojalgo.benchmark.lab.library.ACM.java

@Override
public MutatingUnaryMatrixOperation<RealMatrix, Array2DRowRealMatrix> getOperationInvert(final int dim,
        final boolean spd) {
    if (spd) {/* www  .  jav a2 s.c o m*/
        return (matA, result) -> {
            final CholeskyDecomposition chol = new CholeskyDecomposition(matA);
            this.copy(chol.getSolver().getInverse(), result);
        };
    } else {
        return (matA, result) -> {
            final LUDecomposition lu = new LUDecomposition(matA);
            this.copy(lu.getSolver().getInverse(), result);
        };
    }
}