Java Code Examples for org.apache.commons.math3.linear.RealMatrix#add()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#add() . 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: CheckUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**Same as checkMmul, but for matrix addition */
public static boolean checkAdd(INDArray first, INDArray second, double maxRelativeDifference,
                double minAbsDifference) {
    RealMatrix rmFirst = convertToApacheMatrix(first);
    RealMatrix rmSecond = convertToApacheMatrix(second);

    INDArray result = first.add(second);
    RealMatrix rmResult = rmFirst.add(rmSecond);

    if (!checkShape(rmResult, result))
        return false;
    boolean ok = checkEntries(rmResult, result, maxRelativeDifference, minAbsDifference);
    if (!ok) {
        INDArray onCopies = Shape.toOffsetZeroCopy(first).add(Shape.toOffsetZeroCopy(second));
        printFailureDetails(first, second, rmResult, result, onCopies, "add");
    }
    return ok;
}
 
Example 2
Source File: CheckUtil.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**Same as checkMmul, but for matrix addition */
public static boolean checkAdd(INDArray first, INDArray second, double maxRelativeDifference,
                double minAbsDifference) {
    RealMatrix rmFirst = convertToApacheMatrix(first);
    RealMatrix rmSecond = convertToApacheMatrix(second);

    INDArray result = first.add(second);
    RealMatrix rmResult = rmFirst.add(rmSecond);

    if (!checkShape(rmResult, result))
        return false;
    boolean ok = checkEntries(rmResult, result, maxRelativeDifference, minAbsDifference);
    if (!ok) {
        INDArray onCopies = Shape.toOffsetZeroCopy(first).add(Shape.toOffsetZeroCopy(second));
        printFailureDetails(first, second, rmResult, result, onCopies, "add");
    }
    return ok;
}
 
Example 3
Source File: Math.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
public static double[] rlsInterpolation(double[] x, double[] y, int pow) {
	if (pow < 1) {
		return null;
	}

	double[] coeffs = new double[pow + 1];
	double d = 1000d;
	for (int i = 0; i < pow + 1; i++) {
		coeffs[i] = 0d;
	}
	double[][] pMtx = new double[pow + 1][pow + 1];
	for (int i = 0; i < pow + 1; i++) {
		for (int j = 0; j < pow + 1; j++) {
			pMtx[i][j] = (i == j) ? d : 0;
		}
	}

	RealMatrix wV = new Array2DRowRealMatrix(coeffs);
	RealMatrix pM = new Array2DRowRealMatrix(pMtx);

	for (int k = 0; k < x.length; k++) {
		double xx = x[k];
		double yy = y[k];

		RealMatrix xV = new Array2DRowRealMatrix(pow + 1, 1);

		double aPow = 1;
		for (int i = 0; i < pow + 1; i++) {
			xV.setEntry(i, 0, aPow);
			aPow *= xx;
		}

		double alpha = yy - wV.transpose().multiply(xV).getEntry(0, 0);
		RealMatrix gV = pM.multiply(xV).scalarMultiply(1 / (1d + xV.transpose().multiply(pM).multiply(xV).getEntry(0, 0)));
		pM = pM.subtract(gV.multiply(xV.transpose()).multiply(pM));
		wV = wV.add(gV.scalarMultiply(alpha));
	}
	return wV.getColumn(0);
}
 
Example 4
Source File: GaussianProcess.java    From BLELocalization with MIT License 5 votes vote down vote up
public GaussianProcess fit(double[][] X, double[][] Y){
	int ns = X.length;
	this.X = X;
	this.Y = Y;

	// Compute Gram Matrix (Symmetric matrix)
	K = computeGramMatrix(X);
	RealMatrix KMat = MatrixUtils.createRealMatrix(K);
	RealMatrix sigma_n2I = MatrixUtils.createRealIdentityMatrix(ns).scalarMultiply(sigmaN*sigmaN);
	RealMatrix Ky = KMat.add(sigma_n2I);
	this.Ky = Ky.getData();
	LUDecomposition LUDecomp = new  LUDecomposition(Ky);
	detKy = LUDecomp.getDeterminant();

	RealMatrix invKyMat = LUDecomp.getSolver().getInverse();
	invKy = invKyMat.getData();

	// Precompute dY = Y - mX
	int ny=Y[0].length;
	this.mX = new double[ns][ny];
	this.dY = new double[ns][ny];
	for(int i=0; i<ns; i++){
		for(int j=0; j<ny; j++){
			mX[i][j] = meanFunc(X[i],j);
			dY[i][j] = Y[i][j]-mX[i][j];
		}
	}
	
	if(optConstVar==1){
		invKyDY = computeInvKyDY(invKy, dY);
	}
	
	return this;
}
 
Example 5
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 6
Source File: GMMTrainer.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private RealMatrix computeCov(int k, RealVector mean, double sumGamma){
    RealMatrix res = new Array2DRowRealMatrix(data.getColumnDimension(),data.getColumnDimension());
    for (int i=0;i<data.getRowDimension();i++){
        res = res.add(data.getRowVector(i).outerProduct(data.getRowVector(i)).scalarMultiply(gammas[i][k]));
    }
    return res.scalarMultiply(1/sumGamma).subtract(mean.outerProduct(mean));
}
 
Example 7
Source File: WeightedIntDiGraphTest.java    From pacaya with Apache License 2.0 5 votes vote down vote up
@Test
public void testSumWalks() {
    RealMatrix m = new Array2DRowRealMatrix(new double[][] {
        { 0.3, 0.0, 0.3 },
        { 0.1, 0.6, 0.7 },
        { 0.0, 0.4, 0.2 },
    });
    RealMatrix sum = MatrixUtils.createRealIdentityMatrix(3);
    for (int i = 0; i < 2000; i++) {
        sum = sum.add(m.power(i + 1));
    }
    RealVector ones = new ArrayRealVector(3, 1.0);
    // this is how I'm computing the reference (different from the methods implementation)
    double expectedSumWalks = sum.preMultiply(ones).dotProduct(ones);
    RealVector s = new ArrayRealVector(new double[] {1, 2, 3});
    RealVector t = new ArrayRealVector(new double[] {5, 7, 4});
    assertEquals(127.49999999999903, expectedSumWalks, tol);
    assertEquals(127.49999999999903, WeightedIntDiGraph.sumWalks(m, null, null), tol);
    assertEquals(127.49999999999903, WeightedIntDiGraph.sumWalks(m, null, ones), tol);
    assertEquals(127.49999999999903, WeightedIntDiGraph.sumWalks(m, ones, null), tol);

    assertTrue(TestUtils.checkThrows(() -> {
        WeightedIntDiGraph.sumWalks(
            new Array2DRowRealMatrix(
                    new double[][] {
                        { 0.3, 0.0, 0.3, 0 },
                        { 0.1, 0.6, 0.7, 0 },
                        { 0.0, 0.4, 0.2, 0 }}),
            null,
            null);
        }, InputMismatchException.class));                
    assertEquals(699.9999999999948, WeightedIntDiGraph.sumWalks(m, null, t), tol);
    assertEquals(274.99999999999795, WeightedIntDiGraph.sumWalks(m, s, null), tol);
    assertEquals(1509.9999999999886, WeightedIntDiGraph.sumWalks(m, s, t), tol);

}
 
Example 8
Source File: GMMTrainer.java    From pyramid with Apache License 2.0 4 votes vote down vote up
private RealMatrix stablize(RealMatrix cov){
    return cov.add(stabilizer);
}