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

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

Introduction

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

Prototype

public BlockRealMatrix(final int rows, final int columns) throws NotStrictlyPositiveException 

Source Link

Document

Create a new matrix with the supplied row and column dimensions.

Usage

From source file:org.glotaran.core.datadisplayers.multispec.MultiSpecEditorTopComponent.java

private Matrix[] calculateSVD() {
    //org.ujmp.core.util.UJMPSettings.setUseOjalgo(false);      
    RealMatrix mat = new BlockRealMatrix(data.getNt(), data.getNl());
    for (int i = 0; i < data.getNt(); i++) {
        for (int j = 0; j < data.getNl(); j++) {
            mat.setEntry(i, j, data.getPsisim()[j * data.getNt() + i]);
        }// w  w  w . j  av a 2  s  .  c o m
    }
    org.apache.commons.math3.linear.SingularValueDecomposition svd = new SingularValueDecomposition(mat);
    RealMatrix U = svd.getU();
    RealMatrix S = svd.getS();
    RealMatrix V = svd.getV();
    //This works only with UJMP 0.3.0 or higher
    //CommonsMathDenseDoubleMatrix2DFactory fac = CommonsMathDenseDoubleMatrix2DFactory.INSTANCE;
    //imSVDPanel.setSVDResults(new Matrix[]{fac.dense(U),fac.dense(S),fac.dense(V)};                

    // This uses too much memory, it seems the 'thin' boolean is not working well.
    //DefaultDenseDoubleMatrix2D newMatrix = new DefaultDenseDoubleMatrix2D(data.getPsisim(), data.getNt(),data.getNl());
    //SVD.SVDMatrix svdresult = new org.ujmp.core.doublematrix.calculation.general.decomposition.SVD.SVDMatrix(newMatrix, true, true, true);
    //SVDMatrix test = new SVDMatrix(newMatrix, thin, wantu, wantv);

    //imSVDPanel.initialiseSVDPlots(data.getNt(), data.getOriginalWidth(), data.getOriginalHeight(), data.getX(), data.getIntenceImX(), data.getIntenceImY());
    //return imSVDPanel.getSVDResults();
    return null;
}

From source file:org.knime.knip.core.algorithm.convolvers.KernelTools.java

/**
 * /*from w ww .  ja  v  a2 s .  c  o m*/
 * @param <R>
 * @param img A two dimensional image.
 * @return
 */
public static <R extends RealType<R>> RealMatrix toMatrix(final Img<R> img) {

    assert img.numDimensions() == 2;
    RealMatrix ret = new BlockRealMatrix((int) img.dimension(0), (int) img.dimension(1));
    Cursor<R> c = img.cursor();
    while (c.hasNext()) {
        c.fwd();
        ret.setEntry(c.getIntPosition(0), c.getIntPosition(1), c.get().getRealDouble());
    }
    return ret;
}

From source file:org.knime.knip.core.util.ApacheMathTools.java

/**
 * // w  w  w  .  j a  v a  2s .c o  m
 * @param <R>
 * @param img A two dimensional image.
 * @return
 */
public static <R extends RealType<R>> RealMatrix toMatrix(final Img<R> img) {

    assert img.numDimensions() == 2;
    final RealMatrix ret = new BlockRealMatrix((int) img.dimension(0), (int) img.dimension(1));
    final Cursor<R> c = img.cursor();
    while (c.hasNext()) {
        c.fwd();
        ret.setEntry(c.getIntPosition(0), c.getIntPosition(1), c.get().getRealDouble());
    }
    return ret;
}

From source file:org.nd4j.linalg.Nd4jTestsComparisonFortran.java

@Test
public void testGemvApacheCommons() {

    int[] rowsArr = new int[] { 4, 4, 4, 8, 8, 8 };
    int[] colsArr = new int[] { 2, 1, 10, 2, 1, 10 };

    for (int x = 0; x < rowsArr.length; x++) {
        int rows = rowsArr[x];
        int cols = colsArr[x];

        List<Pair<INDArray, String>> matrices = NDArrayCreationUtil.getAllTestMatricesWithShape(rows, cols,
                12345);// ww w.j a  v a 2 s.c o m
        List<Pair<INDArray, String>> vectors = NDArrayCreationUtil.getAllTestMatricesWithShape(cols, 1, 12345);

        for (int i = 0; i < matrices.size(); i++) {
            for (int j = 0; j < vectors.size(); j++) {

                Pair<INDArray, String> p1 = matrices.get(i);
                Pair<INDArray, String> p2 = vectors.get(j);
                String errorMsg = getTestWithOpsErrorMsg(i, j, "mmul", p1, p2);

                INDArray m = p1.getFirst();
                INDArray v = p2.getFirst();

                RealMatrix rm = new BlockRealMatrix(m.rows(), m.columns());
                for (int r = 0; r < m.rows(); r++) {
                    for (int c = 0; c < m.columns(); c++) {
                        double d = m.getDouble(r, c);
                        rm.setEntry(r, c, d);
                    }
                }

                RealMatrix rv = new BlockRealMatrix(cols, 1);
                for (int r = 0; r < v.rows(); r++) {
                    double d = v.getDouble(r, 0);
                    rv.setEntry(r, 0, d);
                }

                INDArray gemv = m.mmul(v);
                RealMatrix gemv2 = rm.multiply(rv);

                assertArrayEquals(new int[] { rows, 1 }, gemv.shape());
                assertArrayEquals(new int[] { rows, 1 },
                        new int[] { gemv2.getRowDimension(), gemv2.getColumnDimension() });

                //Check entries:
                for (int r = 0; r < rows; r++) {
                    double exp = gemv2.getEntry(r, 0);
                    double act = gemv.getDouble(r, 0);
                    assertEquals(errorMsg, exp, act, 1e-5);
                }
            }
        }
    }
}

From source file:org.opentestsystem.airose.linear.Matrices.java

public static Matrix create(int rows, int columns, MatrixTypeEnum type) {
    if (MatrixTypeEnum.DIAGONAL == type) {
        if (rows != columns)
            throw new MatrixCreationException("Rows != Columns. A diagonal matrix cannot be created.");
        return new Matrix(new DiagonalMatrix(rows));
    } else {//from  w ww . j  ava  2s .c  o  m
        return new Matrix(new BlockRealMatrix(rows, columns));
    }

}

From source file:org.ujmp.commonsmath.CommonsMathBlockDenseDoubleMatrix2D.java

public CommonsMathBlockDenseDoubleMatrix2D(long... size) {
    super(new BlockRealMatrix(MathUtil.longToInt(size[ROW]), MathUtil.longToInt(size[COLUMN])));
}

From source file:Pruning.Experiments.KendallsCorrelation.java

/**
 * Computes the Kendall's Tau rank correlation matrix for the columns of
 * the input matrix.// w w  w.j  a  v a2  s  .co  m
 *
 * @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), matrix.getColumn(i).length);
            outMatrix.setEntry(i, j, corr);
            outMatrix.setEntry(j, i, corr);
        }
        outMatrix.setEntry(i, i, 1d);
    }
    return outMatrix;
}

From source file:Rotationforest.Covariance.java

/**
 * Compute a covariance matrix from a matrix whose columns represent
 * covariates.//from w  w  w.  j ava2  s.  c om
 * @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)));
        outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
    }
    //return outMatrix;
    return outMatrix;

}

From source file:stats.KendallsCorrelation.java

/**
 * Computes the Kendall's Tau rank correlation matrix for the columns of the
 * input matrix.//from w  w w. j a va  2 s .  c  o  m
 *
 * @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)).getFirst();
            outMatrix.setEntry(i, j, corr);
            outMatrix.setEntry(j, i, corr);
        }
        outMatrix.setEntry(i, i, 1d);
    }
    return outMatrix;
}

From source file:stats.SpearmansCorrelation.java

/**
 * Applies rank transform to each of the columns of <code>matrix</code> using
 * the current <code>rankingAlgorithm</code>.
 *
 * @param matrix/*from w  ww .  ja  v a2  s .  c  om*/
 *          matrix to transform
 * @return a rank-transformed matrix
 */
private RealMatrix rankTransform(final RealMatrix matrix) {
    RealMatrix transformed = null;

    if (rankingAlgorithm instanceof NaturalRanking
            && ((NaturalRanking) rankingAlgorithm).getNanStrategy() == NaNStrategy.REMOVED) {
        final Set<Integer> nanPositions = new HashSet<Integer>();
        for (int i = 0; i < matrix.getColumnDimension(); i++) {
            nanPositions.addAll(getNaNPositions(matrix.getColumn(i)));
        }

        // if we have found NaN values, we have to update the matrix size
        if (!nanPositions.isEmpty()) {
            transformed = new BlockRealMatrix(matrix.getRowDimension() - nanPositions.size(),
                    matrix.getColumnDimension());
            for (int i = 0; i < transformed.getColumnDimension(); i++) {
                transformed.setColumn(i, removeValues(matrix.getColumn(i), nanPositions));
            }
        }
    }

    if (transformed == null) {
        transformed = matrix.copy();
    }

    for (int i = 0; i < transformed.getColumnDimension(); i++) {
        transformed.setColumn(i, rankingAlgorithm.rank(transformed.getColumn(i)));
    }

    return transformed;
}