Java Code Examples for org.apache.commons.math3.linear.CholeskyDecomposition#getL()

The following examples show how to use org.apache.commons.math3.linear.CholeskyDecomposition#getL() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinalgUtil.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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.
 *
 * @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_Double(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;
}
 
Example 2
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Function to compute Cholesky decomposition of the given input matrix. 
 * The input must be a real symmetric positive-definite matrix.
 * 
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 */
private static MatrixBlock computeCholesky(Array2DRowRealMatrix in) {
	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, 1e-14,
		CholeskyDecomposition.DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD);
	RealMatrix rmL = cholesky.getL();
	return DataConverter.convertToMatrixBlock(rmL.getData());
}
 
Example 3
Source File: SyntheticBeaconDataGenerator.java    From BLELocalization with MIT License 5 votes vote down vote up
public void fit(List<Location> locations){
	setLocations(locations);
	int n = locations.size();
	double[][] K = new double[n][n];

	for(int i=0; i<n; i++){
		Location loc1 = locations.get(i);
		double[] x1 = ModelAdaptUtils.locationToVec(loc1);
		for(int j=i; j<n; j++){
			Location loc2 = locations.get(j);
			double[] x2 = ModelAdaptUtils.locationToVec(loc2);
			double k =kernel.computeKernel(x1, x2);
			K[i][j] = k;
			K[j][i] = k;
		}
	}
	RealMatrix Kmat = MatrixUtils.createRealMatrix(K);
	RealMatrix lambdamat = MatrixUtils.createRealIdentityMatrix(n).scalarMultiply(sigma_n*sigma_n); //Tentative treatment

	RealMatrix Kymat = Kmat.add(lambdamat);

	CholeskyDecomposition chol = new CholeskyDecomposition(Kymat);
	RealMatrix Lmat = chol.getL();

	double[] normalRands = new double[n];
	for(int i=0; i<n; i++){
		normalRands[i] = rand.nextGaussian();
	}
	this.y = Lmat.operate(normalRands);

	RealMatrix invKymat = (new LUDecomposition(Kymat)).getSolver().getInverse();
	this.alphas = invKymat.operate(y);
}
 
Example 4
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Function to compute Cholesky decomposition of the given input matrix. 
 * The input must be a real symmetric positive-definite matrix.
 * 
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 */
private static MatrixBlock computeCholesky(Array2DRowRealMatrix in) {
	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, 1e-14,
		CholeskyDecomposition.DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD);
	RealMatrix rmL = cholesky.getL();
	return DataConverter.convertToMatrixBlock(rmL.getData());
}
 
Example 5
Source File: GaussianDistribution.java    From pyramid with Apache License 2.0 5 votes vote down vote up
void setCovariance(RealMatrix covariance) {
    this.covariance = covariance;
    CholeskyDecomposition decomposition = new CholeskyDecomposition(covariance);
    this.inverseCovariance = decomposition.getSolver().getInverse();
    RealMatrix lMatrix = decomposition.getL();
    double sum = 0;
    for (int i=0;i<lMatrix.getRowDimension();i++){
        sum += Math.log(lMatrix.getEntry(i,i));
    }
    this.logDeterminant = 2*sum;
}