Example usage for org.apache.commons.math.linear RealMatrix getColumnDimension

List of usage examples for org.apache.commons.math.linear RealMatrix getColumnDimension

Introduction

In this page you can find the example usage for org.apache.commons.math.linear RealMatrix getColumnDimension.

Prototype

int getColumnDimension();

Source Link

Document

Returns the number of columns in the matrix.

Usage

From source file:juicebox.tools.utils.juicer.arrowhead.DynamicProgrammingUtils.java

/**
 * Calculate cumulative sums across upper right matrix
 * iterative result is entry to left + entry below + orig value - diagonal down (else we double count)
 *
 * @param matrix/*from   w w  w .  j a  v a 2  s .  co  m*/
 * @param superDiagonal
 * @return
 */
public static RealMatrix sum(RealMatrix matrix, int superDiagonal) {

    int n = Math.min(matrix.getRowDimension(), matrix.getColumnDimension());

    RealMatrix diagMatrix = MatrixTools.cleanArray2DMatrix(matrix.getRowDimension(),
            matrix.getColumnDimension());
    if (superDiagonal <= 0) {
        diagMatrix = MatrixTools.extractDiagonal(matrix);
        superDiagonal = 1;
    }

    // d = distance from diagonal
    for (int d = superDiagonal; d < n; d++) {
        // i = row, column is i +d;

        for (int i = 0; i < n - d; i++) {
            diagMatrix.setEntry(i, i + d, diagMatrix.getEntry(i, i + d - 1) + diagMatrix.getEntry(i + 1, i + d)
                    + matrix.getEntry(i, i + d) - diagMatrix.getEntry(i + 1, i + d - 1));
        }
    }
    return diagMatrix;
}

From source file:juicebox.tools.utils.juicer.apa.APAUtils.java

/**
 * @param data/*  w w  w .jav  a2 s. c  o m*/
 * @return
 */
public static RealMatrix rankPercentile(RealMatrix data) {
    int n = data.getColumnDimension();
    StatPercentile percentile = new StatPercentile(MatrixTools.flattenedRowMajorOrderMatrix(data));

    RealMatrix matrix = new Array2DRowRealMatrix(n, n);
    for (int r = 0; r < n; r++) {
        for (int c = 0; c < n; c++) {
            double currValue = data.getEntry(r, c);
            if (currValue == 0) {
                matrix.setEntry(r, c, 0);
            } else {
                matrix.setEntry(r, c, percentile.evaluate(currValue));
            }
            //matrix.setEntry(r, c, percentile.evaluate());
        }
    }
    return matrix;
}

From source file:net.sf.maltcms.chromaui.features.spi.FeatureTable.java

public static RealMatrix normalize(RealMatrix sourceMatrix, boolean center, boolean normalize) {
    RealMatrix normalized = MatrixUtils.createRealMatrix(sourceMatrix.getRowDimension(),
            sourceMatrix.getColumnDimension());
    for (int col = 0; col < sourceMatrix.getColumnDimension(); col++) {
        double[] columnVector = sourceMatrix.getColumn(col);
        double mean = StatUtils.mean(columnVector);
        double stdev = Math.sqrt(StatUtils.variance(columnVector, mean));
        Logger.getLogger(FeatureTable.class.getName()).log(Level.INFO, "column {0}, mean={1} stdev={2}",
                new Object[] { col, mean, stdev });
        for (int j = 0; j < columnVector.length; j++) {
            normalized.setEntry(j, col, (sourceMatrix.getEntry(j, col) - mean) / stdev);
        }/*from w w  w  .  j a va  2 s .  c  om*/
    }
    return normalized;
}

From source file:gda.images.camera.Utilities.java

public static RealMatrix createMatrixFromProperty(String propName) {

     RealMatrixPropertyEditor mpe = new RealMatrixPropertyEditor();
     mpe.setAsText(LocalProperties.get(propName));
     RealMatrix axisOrientationMatrix = mpe.getValue();
     if (axisOrientationMatrix.getRowDimension() != 3 || axisOrientationMatrix.getColumnDimension() != 3) {
         throw new IllegalArgumentException("Axis orientation matrix is not 33: " + axisOrientationMatrix);
     }/*from w w  w  .  java 2 s.  c o  m*/

     return axisOrientationMatrix;
 }

From source file:juicebox.tools.utils.common.MatrixTools.java

/**
 * Flatten a 2D double matrix into a double array
 *
 * @param matrix/*w ww  .  ja v a 2 s. c  om*/
 * @return 1D double array in row major order
 */
public static double[] flattenedRowMajorOrderMatrix(RealMatrix matrix) {
    int n = matrix.getColumnDimension();
    int m = matrix.getRowDimension();
    int numElements = n * m;
    double[] flattenedMatrix = new double[numElements];

    int index = 0;
    for (int i = 0; i < m; i++) {
        System.arraycopy(matrix.getRow(i), 0, flattenedMatrix, index, n);
        index += n;
    }
    return flattenedMatrix;
}

From source file:juicebox.tools.utils.common.MatrixTools.java

/**
 * Returns the values along the diagonal of the matrix
 *
 * @param matrix/*from   ww  w  . j  av  a 2 s .  c o  m*/
 * @return diagonal
 */
public static RealMatrix extractDiagonal(RealMatrix matrix) {
    int n = Math.min(matrix.getColumnDimension(), matrix.getRowDimension());
    RealMatrix diagonal = MatrixTools.cleanArray2DMatrix(n);
    for (int i = 0; i < n; i++) {
        diagonal.setEntry(i, i, matrix.getEntry(i, i));
    }
    return diagonal;
}

From source file:juicebox.tools.utils.common.MatrixTools.java

/**
 * @return matrix flipped across the antidiagonal
 *///from   ww  w  . ja  va 2  s .  c  om
public static RealMatrix flipAcrossAntiDiagonal(RealMatrix matrix) {
    int n = Math.min(matrix.getColumnDimension(), matrix.getRowDimension());
    RealMatrix antiDiagFlippedMatrix = cleanArray2DMatrix(n, n);
    int maxIndex = n - 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            antiDiagFlippedMatrix.setEntry(maxIndex - j, maxIndex - i, matrix.getEntry(i, j));
        }
    }
    return antiDiagFlippedMatrix;
}

From source file:juicebox.tools.utils.common.MatrixTools.java

/**
 * @return matrix flipped Left-Right// ww w .ja va  2 s .  com
 */
public static RealMatrix flipLeftRight(RealMatrix matrix) {
    int r = matrix.getRowDimension(), c = matrix.getColumnDimension();
    RealMatrix leftRightFlippedMatrix = cleanArray2DMatrix(r, c);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            leftRightFlippedMatrix.setEntry(i, c - 1 - j, matrix.getEntry(i, j));
        }
    }
    return leftRightFlippedMatrix;
}

From source file:juicebox.tools.utils.common.MatrixTools.java

/**
 * @return matrix flipped Top-Bottom//from  www  . j av  a 2 s  .  c  o m
 */
public static RealMatrix flipTopBottom(RealMatrix matrix) {
    int r = matrix.getRowDimension(), c = matrix.getColumnDimension();
    RealMatrix topBottomFlippedMatrix = cleanArray2DMatrix(r, c);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            topBottomFlippedMatrix.setEntry(r - 1 - i, j, matrix.getEntry(i, j));
        }
    }
    return topBottomFlippedMatrix;
}

From source file:juicebox.tools.utils.common.MatrixTools.java

/**
 * Return sign of values in matrix:/* w w  w.  ja v a  2  s.  co  m*/
 * val > 0 : 1
 * val = 0 : 0
 * val < 0 : -1
 */
public static RealMatrix sign(RealMatrix matrix) {
    int r = matrix.getRowDimension();
    int c = matrix.getColumnDimension();
    RealMatrix signMatrix = cleanArray2DMatrix(r, c);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            double val = matrix.getEntry(i, j);
            if (val > 0) {
                signMatrix.setEntry(i, j, 1);
            } else if (val < 0) {
                signMatrix.setEntry(i, j, -1);
            }
        }
    }
    return signMatrix;
}