org.apache.commons.math3.linear.Array2DRowRealMatrix Java Examples

The following examples show how to use org.apache.commons.math3.linear.Array2DRowRealMatrix. 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: KalmanFilterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=MatrixDimensionMismatchException.class)
public void testTransitionControlMatrixMismatch() {
    
    // A and B matrix do not match in dimensions
    
    // A = [ 1 ]
    RealMatrix A = new Array2DRowRealMatrix(new double[] { 1d });
    // B = [ 1 1 ]
    RealMatrix B = new Array2DRowRealMatrix(new double[] { 1d, 1d });
    // H = [ 1 ]
    RealMatrix H = new Array2DRowRealMatrix(new double[] { 1d });
    // Q = [ 0 ]
    RealMatrix Q = new Array2DRowRealMatrix(new double[] { 0 });
    // R = [ 0 ]
    RealMatrix R = new Array2DRowRealMatrix(new double[] { 0 });

    ProcessModel pm
        = new DefaultProcessModel(A, B, Q,
                                  new ArrayRealVector(new double[] { 0 }), null);
    MeasurementModel mm = new DefaultMeasurementModel(H, R);
    new KalmanFilter(pm, mm);
    Assert.fail("transition and control matrix should not be compatible");
}
 
Example #2
Source File: OLSMultipleLinearRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Compute the "hat" matrix.
 * </p>
 * <p>The hat matrix is defined in terms of the design matrix X
 *  by X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup>
 * </p>
 * <p>The implementation here uses the QR decomposition to compute the
 * hat matrix as Q I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the
 * p-dimensional identity matrix augmented by 0's.  This computational
 * formula is from "The Hat Matrix in Regression and ANOVA",
 * David C. Hoaglin and Roy E. Welsch,
 * <i>The American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp. 17-22.
 * </p>
 * <p>Data for the model must have been successfully loaded using one of
 * the {@code newSampleData} methods before invoking this method; otherwise
 * a {@code NullPointerException} will be thrown.</p>
 *
 * @return the hat matrix
 * @throws NullPointerException unless method {@code newSampleData} has been
 * called beforehand.
 */
public RealMatrix calculateHat() {
    // Create augmented identity matrix
    RealMatrix Q = qr.getQ();
    final int p = qr.getR().getColumnDimension();
    final int n = Q.getColumnDimension();
    // No try-catch or advertised NotStrictlyPositiveException - NPE above if n < 3
    Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n);
    double[][] augIData = augI.getDataRef();
    for (int i = 0; i < n; i++) {
        for (int j =0; j < n; j++) {
            if (i == j && i < p) {
                augIData[i][j] = 1d;
            } else {
                augIData[i][j] = 0d;
            }
        }
    }

    // Compute and return Hat matrix
    // No DME advertised - args valid if we get here
    return Q.multiply(augI).multiply(Q.transpose());
}
 
Example #3
Source File: MeanShiftTests.java    From clust4j with Apache License 2.0 6 votes vote down vote up
@Test
public void MeanShiftTest1() {
	final double[][] train_array = new double[][] {
		new double[] {0.0,  1.0,  2.0,  3.0},
		new double[] {5.0,  4.3,  19.0, 4.0},
		new double[] {9.06, 12.6, 3.5,  9.0}
	};
	
	final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(train_array);
	
	MeanShift ms = new MeanShift(mat, new MeanShiftParameters(0.5)
			.setVerbose(true)).fit();
	System.out.println();
	
	assertTrue(ms.getNumberOfIdentifiedClusters() == 3);
	assertTrue(ms.getNumberOfNoisePoints() == 0);
}
 
Example #4
Source File: PipelineTest.java    From clust4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testB() {
	final double[][] data = new double[][] {
		new double[] {0.005, 	 0.182751,  0.1284},
		new double[] {3.65816,   0.29518,   2.123316},
		new double[] {4.1234,    0.27395,   1.8900002}
	};
	
	final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(data);
	final KMedoidsParameters planner = new KMedoidsParameters(2).setVerbose(true);
	
	// Build the pipeline
	final UnsupervisedPipeline<KMedoids> pipe = new UnsupervisedPipeline<KMedoids>(planner, 
		new PreProcessor[]{
			new StandardScaler(), 
			new MeanImputation(new MeanImputation.MeanImputationPlanner().setVerbose(true)) // Will create a warning
		});
	
	@SuppressWarnings("unused")
	KMedoids km = pipe.fit(mat); 
	System.out.println();
}
 
Example #5
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #6
Source File: OLSMultipleLinearRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Compute the "hat" matrix.
 * </p>
 * <p>The hat matrix is defined in terms of the design matrix X
 *  by X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup>
 * </p>
 * <p>The implementation here uses the QR decomposition to compute the
 * hat matrix as Q I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the
 * p-dimensional identity matrix augmented by 0's.  This computational
 * formula is from "The Hat Matrix in Regression and ANOVA",
 * David C. Hoaglin and Roy E. Welsch,
 * <i>The American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp. 17-22.
 * </p>
 * <p>Data for the model must have been successfully loaded using one of
 * the {@code newSampleData} methods before invoking this method; otherwise
 * a {@code NullPointerException} will be thrown.</p>
 *
 * @return the hat matrix
 */
public RealMatrix calculateHat() {
    // Create augmented identity matrix
    RealMatrix Q = qr.getQ();
    final int p = qr.getR().getColumnDimension();
    final int n = Q.getColumnDimension();
    // No try-catch or advertised NotStrictlyPositiveException - NPE above if n < 3
    Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n);
    double[][] augIData = augI.getDataRef();
    for (int i = 0; i < n; i++) {
        for (int j =0; j < n; j++) {
            if (i == j && i < p) {
                augIData[i][j] = 1d;
            } else {
                augIData[i][j] = 0d;
            }
        }
    }

    // Compute and return Hat matrix
    // No DME advertised - args valid if we get here
    return Q.multiply(augI).multiply(Q.transpose());
}
 
Example #7
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #8
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param k Diagonal position.
 * @return Upper triangular part of matrix.
 */
private static RealMatrix triu(final RealMatrix m, int k) {
    final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #9
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise square (^2) of m.
 */
private static RealMatrix square(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            double e = m.getEntry(r, c);
            d[r][c] = e * e;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #10
Source File: MeanShiftTests.java    From clust4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMeanShiftMFE2() {
	boolean a = false;
	final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(MatUtils.randomGaussian(50, 2));
	MeanShift ms = new MeanShift(mat, 0.5);
	try {
		ms.getCentroids();
	} catch(ModelNotFitException m) {
		a = true;
	} finally {
		assertTrue(a);
	}
}
 
Example #11
Source File: GamaFloatMatrix.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
RealMatrix getRealMatrix() {
	final RealMatrix realMatrix = new Array2DRowRealMatrix(this.numRows, this.numCols);
	for (int i = 0; i < this.numRows; i++) {
		for (int j = 0; j < this.numCols; j++) {
			realMatrix.setEntry(i, j, this.get(null, j, i));
		}
	}
	return realMatrix;
}
 
Example #12
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return Matrix where the elements of m and n are element-wise divided.
 */
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #13
Source File: Arja_0028_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Row matrix representing the sums of the rows.
 */
private static RealMatrix sumRows(final RealMatrix m) {
    double[][] d = new double[1][m.getColumnDimension()];
    for (int c = 0; c < m.getColumnDimension(); c++) {
        double sum = 0;
        for (int r = 0; r < m.getRowDimension(); r++) {
            sum += m.getEntry(r, c);
        }
        d[0][c] = sum;
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #14
Source File: Elixir_0021_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param size Number of rows.
 * @param popSize Population size.
 * @return a 2-dimensional matrix of Gaussian random numbers.
 */
private RealMatrix randn1(int size, int popSize) {
    double[][] d = new double[size][popSize];
    for (int r = 0; r < size; r++) {
        for (int c = 0; c < popSize; c++) {
            d[r][c] = random.nextGaussian();
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #15
Source File: Cardumen_0037_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return the matrix where the elements of m and n are element-wise multiplied.
 */
private static RealMatrix times(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) * n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #16
Source File: Arja_00154_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #17
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Matrix representing the element-wise square root of m.
 */
private static RealMatrix sqrt(final RealMatrix m) {
    final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = FastMath.sqrt(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #18
Source File: Cardumen_00257_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #19
Source File: Math_20_CMAESOptimizer_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n Number of rows.
 * @param m Number of columns.
 * @return n-by-m matrix filled with 1.
 */
private static RealMatrix ones(int n, int m) {
    double[][] d = new double[n][m];
    for (int r = 0; r < n; r++) {
        Arrays.fill(d[r], 1.0);
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #20
Source File: PreProcessorTests.java    From clust4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCenterScale() {
	final double[][] data = new double[][] {
		new double[] {0.005, 	 0.182751,  0.1284},
		new double[] {3.65816,   0.29518,   2.123316},
		new double[] {4.1234,    0.27395,   1.8900002}
	};
	
	final Array2DRowRealMatrix d = new Array2DRowRealMatrix(data, false);
	final StandardScaler norm = new StandardScaler().fit(d);
	final RealMatrix normed = norm.transform(d);
	final double[][] operated = normed.getData();
	
	assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 0)), 0, 1e-12));
	assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 1)), 0, 1e-12));
	assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 2)), 0, 1e-12));
	
	assertTrue(Precision.equals(VecUtils.stdDev(MatUtils.getColumn(operated, 0)), 1, 1e-12));
	assertTrue(Precision.equals(VecUtils.stdDev(MatUtils.getColumn(operated, 1)), 1, 1e-12));
	assertTrue(Precision.equals(VecUtils.stdDev(MatUtils.getColumn(operated, 2)), 1, 1e-12));
	
	// test inverse transform
	assertTrue(MatUtils.equalsWithTolerance(data, norm.inverseTransform(normed).getData(), 1e-8));
	
	// test dim mismatch
	boolean a = false;
	try {
		norm.inverseTransform(TestSuite.getRandom(2, 2));
	} catch(DimensionMismatchException dim) { a = true; }
	finally { assertTrue(a); }
	
	// assert that fewer than two features will throw exception
	a = false;
	try {
		norm.fit(TestSuite.getRandom(1, 4));
	} catch(IllegalArgumentException i) {
		a = true;
	} finally { assertTrue(a); }
}
 
Example #21
Source File: Cardumen_00105_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #22
Source File: AffinityPropagationTests.java    From clust4j with Apache License 2.0 5 votes vote down vote up
@Test
public void AffinityPropKernelLoadTest() {
	final Array2DRowRealMatrix mat = getRandom(400, 10); // need to reduce size for travis CI
	new AffinityPropagation(mat, new AffinityPropagationParameters()
			.setMetric(new GaussianKernel())
			.setVerbose(true)).fit();
}
 
Example #23
Source File: Arja_00177_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #24
Source File: jKali_0024_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #25
Source File: Cardumen_00257_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n Number of rows.
 * @param m Number of columns.
 * @return n-by-m matrix filled with 1.
 */
private static RealMatrix ones(int n, int m) {
    double[][] d = new double[n][m];
    for (int r = 0; r < n; r++) {
        Arrays.fill(d[r], 1.0);
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #26
Source File: 1_CMAESOptimizer.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return Matrix where the elements of m and n are element-wise divided.
 */
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #27
Source File: JGenProg2017_00111_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #28
Source File: SegmentMergeUtilsUnitTest.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Genome makeGenome(final List<List<ReadCountRecord.SingleSampleRecord>> targetCoverages,
                                 final List<List<AllelicCount>> snpCounts) {
    final List<ReadCountRecord.SingleSampleRecord> records = new ArrayList<>();
    final List<AllelicCount> snps = new ArrayList<>();
    targetCoverages.stream().forEach(records::addAll);
    snpCounts.stream().forEach(snps::addAll);

    final List<Target> targets = records.stream().map(ReadCountRecord::getTarget).collect(Collectors.toList());
    final RealMatrix counts = new Array2DRowRealMatrix(targets.size(), 1);
    counts.setColumn(0, records.stream().mapToDouble(r -> r.getCount()).toArray());
    return new Genome(new ReadCountCollection(targets, Arrays.asList("sample"), counts), snps);
}
 
Example #29
Source File: Arja_00177_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise square (^2) of m.
 */
private static RealMatrix square(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            double e = m.getEntry(r, c);
            d[r][c] = e * e;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example #30
Source File: Cardumen_0037_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return Matrix where the elements of m and n are element-wise divided.
 */
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}