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

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

Introduction

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

Prototype

void addToEntry(int row, int column, double increment) throws MatrixIndexException;

Source Link

Document

Change an entry in the specified row and column.

Usage

From source file:juicebox.data.HiCFileTools.java

/**
 * Extracts matrix from hic file for a specified region.
 * By default, only the top right part of the matrix is returned if the matrix is on the diagonal.
 *
 * @return section of the matrix//ww w  . j  av  a  2  s.  co m
 */
public static RealMatrix extractLocalBoundedRegion(MatrixZoomData zd, int binXStart, int binXEnd, int binYStart,
        int binYEnd, int numRows, int numCols, NormalizationType normalizationType) throws IOException {

    // numRows/numCols is just to ensure a set size in case bounds are approximate
    // left upper corner is reference for 0,0
    List<Block> blocks = new ArrayList<Block>();

    int numDataReadingErrors = 0;

    try {
        numDataReadingErrors += zd.addNormalizedBlocksToList(blocks, binXStart, binYStart, binXEnd, binYEnd,
                normalizationType);
    } catch (Exception e) {
        triggerNormError(normalizationType);
        if (HiCGlobals.printVerboseComments) {
            System.err.println("You do not have " + normalizationType
                    + " normalized maps available for this resolution/region:");
            System.err.println("x1 " + binXStart + " x2 " + binXEnd + " y1 " + binYStart + " y2 " + binYEnd
                    + " res " + zd.getBinSize());
            System.err.println(
                    "Map is likely too sparse or a different normalization/resolution should be chosen.");
            e.printStackTrace();
            System.exit(-6);
        }
    }

    if (HiCGlobals.printVerboseComments && numDataReadingErrors > 0) {
        //System.err.println(numDataReadingErrors + " errors while reading data from region. Map is likely too sparse");
        triggerNormError(normalizationType);
    }

    RealMatrix data = MatrixTools.cleanArray2DMatrix(numRows, numCols);

    if (blocks.size() > 0) {
        for (Block b : blocks) {
            if (b != null) {
                for (ContactRecord rec : b.getContactRecords()) {

                    int relativeX = rec.getBinX() - binXStart;
                    int relativeY = rec.getBinY() - binYStart;

                    if (relativeX >= 0 && relativeX < numRows) {
                        if (relativeY >= 0 && relativeY < numCols) {
                            data.addToEntry(relativeX, relativeY, rec.getCounts());
                        }
                    }
                }
            }
        }
    }
    // ~force cleanup
    blocks = null;

    return data;
}

From source file:eu.amidst.core.exponentialfamily.EF_Normal_NormalParents.java

/**
 * {@inheritDoc}//from  w  w w  .  j a  v a2 s . com
 */
@Override
public SufficientStatistics createInitSufficientStatistics() {
    CompoundVector vectorSS = this.createEmtpyCompoundVector();

    double[] Xarray = { 0.0 };

    double[] Yarray = this.parents.stream().mapToDouble(w -> 0.0).toArray();
    RealVector XYRealVector = new ArrayRealVector(Xarray, Yarray);
    vectorSS.setXYbaseVector(XYRealVector);

    RealMatrix covRealmatrix = new Array2DRowRealMatrix(Yarray.length + 1, Yarray.length + 1);

    //We perform the "laplace" correction in that way to break symmetric covariance matrixes.
    Random rand = new Random(0);
    for (int i = 0; i < Yarray.length + 1; i++) {
        for (int j = 0; j < Yarray.length + 1; j++) {
            covRealmatrix.addToEntry(i, j, rand.nextDouble() + 0.01);
        }
    }
    //covRealmatrix = covRealmatrix.scalarAdd(1.0);

    vectorSS.setcovbaseVector(covRealmatrix);

    return vectorSS;
}

From source file:org.mitre.math.linear.RealMatrixUtils.java

/**
 * Normalizes a matrix into its Z-scores.
 *//*w w w  .j  a  v a  2  s  . c  o  m*/
public void normalizeMatrix(RealMatrix matrix) {
    //int features = matrix.getRowDimension();
    //int samples = matrix.getColumnDimension();
    int m = matrix.getRowDimension();
    int n = matrix.getColumnDimension();

    // Normalize each row
    for (int i = 0; i < m; i++) {
        // would be easier/quicker if we can get the whole row as an array (or vector)
        RealMatrix subMatrix = matrix.getSubMatrix(i, i, 0, n - 1); // n - 1 for 0 based indexing
        double sum = norm1(subMatrix) / n;
        // minusEquals (subtractEquals) subMatrix
        for (int j = 0; j < n; j++) {
            subMatrix.addToEntry(0, j, -1.0 * sum);
        }
        double std = norm1(arrayTimes(subMatrix, subMatrix)) / n;
        timesEquals(subMatrix, 1.0 / std);

        //setSubMatrix(i,i, 0, samples -1, m)
        for (int j = 0; j < n; j++) {
            matrix.setEntry(i, j, subMatrix.getEntry(0, j));
        }
    }

    // Normalize each feature
    //for (int i = 0; i < features; i++) {
    //Matrix m = matrix.getMatrix(i, i, 0, samples - 1);
    //double sum = m.norm1() / samples;
    //double sum = norm1(m) / samples;
    //m.minusEquals(new Matrix(1, samples, sum));
    //double std = m.arrayTimes(m).norm1() / samples;
    //m.times(1.0 / std);
    //matrix.setMatrix(i, i, 0, samples - 1, m);

    //}
}