本文整理汇总了Java中org.apache.commons.math3.linear.BlockRealMatrix类的典型用法代码示例。如果您正苦于以下问题:Java BlockRealMatrix类的具体用法?Java BlockRealMatrix怎么用?Java BlockRealMatrix使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BlockRealMatrix类属于org.apache.commons.math3.linear包,在下文中一共展示了BlockRealMatrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: unflatten
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
@Nonnull
public static RealMatrix[] unflatten(@Nonnull final double[] data, final int rows,
final int cols, final int len) {
final RealMatrix[] grid = new RealMatrix[len];
int offset = 0;
for (int k = 0; k < len; k++) {
RealMatrix cell = new BlockRealMatrix(rows, cols);
grid[k] = cell;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (offset >= data.length) {
throw new IndexOutOfBoundsException("Offset " + offset
+ " exceeded data.length " + data.length);
}
double value = data[offset];
cell.setEntry(i, j, value);
offset++;
}
}
}
if (offset != data.length) {
throw new IllegalArgumentException("Invalid data for unflatten");
}
return grid;
}
示例2: combinedMatrices
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
@Nonnull
public static RealMatrix combinedMatrices(@Nonnull final RealMatrix[][] grid,
final int dimensions) {
Preconditions.checkArgument(grid.length >= 1, "The number of rows must be greather than 1");
Preconditions.checkArgument(grid[0].length >= 1,
"The number of cols must be greather than 1");
Preconditions.checkArgument(dimensions > 0, "Dimension should be more than 0: ", dimensions);
final int rows = grid.length;
final int cols = grid[0].length;
final RealMatrix combined = new BlockRealMatrix(rows * dimensions, cols * dimensions);
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
combined.setSubMatrix(grid[row][col].getData(), row * dimensions, col * dimensions);
}
}
return combined;
}
示例3: computeCovarianceMatrix
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
/**
* Compute a covariance matrix from a matrix whose columns represent
* covariates.
* @param matrix input matrix (must have at least one column and two rows)
* @param biasCorrected determines whether or not covariance estimates are bias-corrected
* @return covariance matrix
* @throws MathIllegalArgumentException if the matrix does not contain sufficient data
*/
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected)
throws MathIllegalArgumentException {
int dimension = matrix.getColumnDimension();
Variance variance = new Variance(biasCorrected);
RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension);
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < i; j++) {
double cov = covariance(matrix.getColumn(i), matrix.getColumn(j), biasCorrected);
outMatrix.setEntry(i, j, cov);
outMatrix.setEntry(j, i, cov);
}
outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
}
return outMatrix;
}
示例4: covarianceToCorrelation
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
/**
* Derives a correlation matrix from a covariance matrix.
*
* <p>Uses the formula <br/>
* <code>r(X,Y) = cov(X,Y)/s(X)s(Y)</code> where
* <code>r(·,·)</code> is the correlation coefficient and
* <code>s(·)</code> means standard deviation.</p>
*
* @param covarianceMatrix the covariance matrix
* @return correlation matrix
*/
public RealMatrix covarianceToCorrelation(RealMatrix covarianceMatrix) {
int nVars = covarianceMatrix.getColumnDimension();
RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
for (int i = 0; i < nVars; i++) {
double sigma = FastMath.sqrt(covarianceMatrix.getEntry(i, i));
outMatrix.setEntry(i, i, 1d);
for (int j = 0; j < i; j++) {
double entry = covarianceMatrix.getEntry(i, j) /
(sigma * FastMath.sqrt(covarianceMatrix.getEntry(j, j)));
outMatrix.setEntry(i, j, entry);
outMatrix.setEntry(j, i, entry);
}
}
return outMatrix;
}
示例5: test2dDoubleArray
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
@Test
public void test2dDoubleArray() {
final double[][] input = new double[][] {
new double[] {2.0, 1.0, 2.0},
new double[] {1.0, 2.0, 1.0},
new double[] {0.0, 0.0, 0.0}
};
final double[][] expected = new double[][] {
new double[] {1.0, 1.0 / 3.0, 1.0},
new double[] {1.0 / 3.0, 1.0, 1.0 / 3.0},
new double[] {1.0, 1.0 / 3.0, 1.0}};
Assert.assertEquals(correlation.computeCorrelationMatrix(input),
new BlockRealMatrix(expected));
}
示例6: testBlockMatrix
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
@Test
public void testBlockMatrix() {
final double[][] input = new double[][] {
new double[] {2.0, 1.0, 2.0},
new double[] {1.0, 2.0, 1.0},
new double[] {0.0, 0.0, 0.0}
};
final double[][] expected = new double[][] {
new double[] {1.0, 1.0 / 3.0, 1.0},
new double[] {1.0 / 3.0, 1.0, 1.0 / 3.0},
new double[] {1.0, 1.0 / 3.0, 1.0}};
Assert.assertEquals(
correlation.computeCorrelationMatrix(new BlockRealMatrix(input)),
new BlockRealMatrix(expected));
}
示例7: getCovariance
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
/**
* Forked from Apache Commons Math
*
* @param stream the stream
* @return covariance covariance
*/
public static RealMatrix getCovariance(final Supplier<Stream<double[]>> stream) {
final int dimension = stream.get().findAny().get().length;
final List<DoubleStatistics> statList = IntStream.range(0, dimension * dimension)
.mapToObj(i -> new DoubleStatistics()).collect(Collectors.toList());
stream.get().forEach(array -> {
for (int i = 0; i < dimension; i++) {
for (int j = 0; j <= i; j++) {
statList.get(i * dimension + j).accept(array[i] * array[j]);
}
}
RecycleBinLong.DOUBLES.recycle(array, array.length);
});
final RealMatrix covariance = new BlockRealMatrix(dimension, dimension);
for (int i = 0; i < dimension; i++) {
for (int j = 0; j <= i; j++) {
final double v = statList.get(i * dimension + j).getAverage();
covariance.setEntry(i, j, v);
covariance.setEntry(j, i, v);
}
}
return covariance;
}
示例8: nonZeroScoreTest
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
@Test
public void nonZeroScoreTest() {
List<MultivariateDistribution> listDist = new ArrayList<>(3);
double[] weights = {2. / 7, 3. / 7, 2. / 7};
double[][] distData = {
{1.5, 2}, {0.5, 0.4, 0.4, 0.5}, {2000},
{2, 0}, {0.3, 0, 0, 0.6}, {3000},
{4.5, 1}, {0.9, 0.2, 0.2, 0.3}, {2000}};
for (int i = 0; i < distData.length; i += 3) {
RealVector mean = new ArrayRealVector(distData[i + 0]);
double[][] covArray = new double[2][2];
covArray[0] = Arrays.copyOfRange(distData[i + 1], 0, 2);
covArray[1] = Arrays.copyOfRange(distData[i + 1], 2, 4);
RealMatrix cov = new BlockRealMatrix(covArray);
listDist.add(new MultivariateNormal(mean, cov));
}
Mixture mixture = new Mixture(listDist, weights);
assertEquals(0.155359, mixture.density(new ArrayRealVector(distData[0])), 1e-6);
assertEquals(0.162771, mixture.density(new ArrayRealVector(distData[3])), 1e-6);
assertEquals(0.094819, mixture.density(new ArrayRealVector(distData[6])), 1e-6);
}
示例9: sviStepTest
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
@Test
public void sviStepTest() {
assertEquals(0.9, VariationalInference.step(0, 1, 0.9), 1e-9);
assertEquals(19.0, VariationalInference.step(10, 20, 0.9), 1e-9);
assertEquals(12.0, VariationalInference.step(10, 20, 0.2), 1e-9);
double[] array1 = {1, 2, 6};
double[] array2 = {2, 3, 8};
double[] array3 = {5, 6, 14};
RealVector start = new ArrayRealVector(array1);
RealVector end = new ArrayRealVector(array3);
assertEquals(new ArrayRealVector(array2), VariationalInference.step(start, end, 0.25));
double[][] matrix1 = {{1, 2}, {3, 10}};
double[][] matrix2 = {{2, 6}, {3, 100}};
double[][] matrix3 = {{5, 18}, {3, 370}};
RealMatrix s = new BlockRealMatrix(matrix1);
RealMatrix e = new BlockRealMatrix(matrix3);
assertEquals(new BlockRealMatrix(matrix2), VariationalInference.step(s, e, 0.25));
}
示例10: addSummary
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
private void addSummary(String title, double[] sum1, double[] sum2)
{
BlockRealMatrix rm = new BlockRealMatrix(sum1.length, 2);
rm.setColumn(0, sum1);
rm.setColumn(1, sum2);
SpearmansCorrelation sr = new SpearmansCorrelation(rm);
PearsonsCorrelation p1 = sr.getRankCorrelation();
PearsonsCorrelation p2 = new PearsonsCorrelation(rm);
StringBuilder sb = new StringBuilder(title);
sb.append(sum1.length).append('\t');
sb.append(Utils.rounded(p1.getCorrelationMatrix().getEntry(0, 1))).append('\t');
sb.append(Utils.rounded(p1.getCorrelationPValues().getEntry(0, 1))).append('\t');
sb.append(Utils.rounded(p2.getCorrelationMatrix().getEntry(0, 1))).append('\t');
sb.append(Utils.rounded(p2.getCorrelationPValues().getEntry(0, 1)));
twSummary.append(sb.toString());
}
示例11: getCorrelationPValues
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
/**
* Returns a matrix of p-values associated with the (two-sided) null
* hypothesis that the corresponding correlation coefficient is zero.
* <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
* that a random variable distributed as <code>t<sub>n-2</sub></code> takes
* a value with absolute value greater than or equal to <br>
* <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
* <p>The values in the matrix are sometimes referred to as the
* <i>significance</i> of the corresponding correlation coefficients.</p>
*
* @return matrix of p-values
* @throws org.apache.commons.math3.exception.MaxCountExceededException
* if an error occurs estimating probabilities
*/
public RealMatrix getCorrelationPValues() {
TDistribution tDistribution = new TDistribution(nObs - 2);
int nVars = correlationMatrix.getColumnDimension();
double[][] out = new double[nVars][nVars];
for (int i = 0; i < nVars; i++) {
for (int j = 0; j < nVars; j++) {
if (i == j) {
out[i][j] = 0d;
} else {
double r = correlationMatrix.getEntry(i, j);
double t = FastMath.abs(r * FastMath.sqrt((nObs - 2)/(1 - r * r)));
out[i][j] = 2 * tDistribution.cumulativeProbability(-t);
}
}
}
return new BlockRealMatrix(out);
}
示例12: valueMatrixToRealMatrix
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
private static RealMatrix valueMatrixToRealMatrix(List<List<Double>> valueMatrix, List<FieldRecipe> fields){
RealMatrix matrix = new BlockRealMatrix(valueMatrix.size(), fields.size());
for(int i=0; i<valueMatrix.size(); i++){
// The i-th subject with non NaN values for all fields
for (int j=0; j<fields.size(); j++){
// j-th field
matrix.setEntry(i,j,valueMatrix.get(i).get(j));
}
}
return matrix;
}
示例13: computeCorrelationMatrix
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
/**
* Computes the Kendall's Tau rank correlation matrix for the columns of
* the input matrix.
*
* @param matrix matrix with columns representing variables to correlate
* @return correlation matrix
*/
public RealMatrix computeCorrelationMatrix(final RealMatrix matrix) {
int nVars = matrix.getColumnDimension();
RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
for (int i = 0; i < nVars; i++) {
for (int j = 0; j < i; j++) {
double corr = correlation(matrix.getColumn(i), matrix.getColumn(j));
outMatrix.setEntry(i, j, corr);
outMatrix.setEntry(j, i, corr);
}
outMatrix.setEntry(i, i, 1d);
}
return outMatrix;
}
示例14: computeCorrelationMatrix
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
/**
* Computes the correlation matrix for the columns of the
* input matrix, using {@link #correlation(double[], double[])}.
*
* Throws MathIllegalArgumentException if the matrix does not have at least
* two columns and two rows. Pairwise correlations are set to NaN if one
* of the correlates has zero variance.
*
* @param matrix matrix with columns representing variables to correlate
* @return correlation matrix
* @throws MathIllegalArgumentException if the matrix does not contain sufficient data
* @see #correlation(double[], double[])
*/
public RealMatrix computeCorrelationMatrix(RealMatrix matrix) {
checkSufficientData(matrix);
int nVars = matrix.getColumnDimension();
RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
for (int i = 0; i < nVars; i++) {
for (int j = 0; j < i; j++) {
double corr = correlation(matrix.getColumn(i), matrix.getColumn(j));
outMatrix.setEntry(i, j, corr);
outMatrix.setEntry(j, i, corr);
}
outMatrix.setEntry(i, i, 1d);
}
return outMatrix;
}
示例15: createRealMatrix
import org.apache.commons.math3.linear.BlockRealMatrix; //导入依赖的package包/类
protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {
double[][] matrixData = new double[nRows][nCols];
int ptr = 0;
for (int i = 0; i < nRows; i++) {
System.arraycopy(data, ptr, matrixData[i], 0, nCols);
ptr += nCols;
}
return new BlockRealMatrix(matrixData);
}