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

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

Introduction

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

Prototype

double[] getRow(int row) throws MatrixIndexException;

Source Link

Document

Returns the entries in row number row as an array.

Usage

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

/**
 * Flatten a 2D double matrix into a double array
 *
 * @param matrix/*from   ww  w  .j  a va 2 s  .  co m*/
 * @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:fi.smaa.libror.r.RHelperTest.java

@Test
public void testRArrayMatrixToRealMatrix() {
    double[] data = new double[] { 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 };
    RealMatrix mat = RHelper.rArrayMatrixToRealMatrix(data, 2);
    assertArrayEquals(new double[] { 1.0, 2.0, 3.0 }, mat.getRow(0), 0.0001);
    assertArrayEquals(new double[] { 4.0, 5.0, 6.0 }, mat.getRow(1), 0.0001);
}

From source file:fi.smaa.libror.UTAGMSSolverTest.java

@Test
public void testDominancePossibleRelation() throws InfeasibleConstraintsException {
    double[][] data = new double[][] { { 82, 94, 80, 91 }, { 59, 73, 72, 67 } };
    UTAGMSSolver s = new UTAGMSSolver(new RORModel(new PerformanceMatrix(new Array2DRowRealMatrix(data))));
    s.solve();//ww  w .ja  v  a2  s  . com
    RealMatrix posRel = s.getPossibleRelation();
    assertArrayEquals(new double[] { 1.0, 1.0 }, posRel.getRow(0), 0.001);
    assertArrayEquals(new double[] { 0.0, 1.0 }, posRel.getRow(1), 0.001);
}

From source file:fi.smaa.libror.UTAGMSSolverTest.java

@Test
public void testPossibleRelationResults() throws InfeasibleConstraintsException {
    solver.solve();/*from www.j  a  v a  2  s.  co  m*/
    RealMatrix nrel = solver.getPossibleRelation();
    assertArrayEquals(new double[] { 1.0, 1.0, 0.0 }, nrel.getRow(0), 0.0001);
    assertArrayEquals(new double[] { 1.0, 1.0, 0.0 }, nrel.getRow(1), 0.0001);
    assertArrayEquals(new double[] { 1.0, 1.0, 1.0 }, nrel.getRow(2), 0.0001);
}

From source file:fi.smaa.libror.UTAGMSSolverTest.java

@Test
public void testStrictPossibleRelationResults() throws InfeasibleConstraintsException {
    solver.setStrictValueFunctions(true);
    solver.solve();//from ww  w  . j a v a  2s  .  co m
    RealMatrix nrel = solver.getPossibleRelation();
    assertArrayEquals(new double[] { 1.0, 0.0, 0.0 }, nrel.getRow(0), 0.0001);
    assertArrayEquals(new double[] { 1.0, 1.0, 0.0 }, nrel.getRow(1), 0.0001);
    assertArrayEquals(new double[] { 1.0, 1.0, 1.0 }, nrel.getRow(2), 0.0001);
}

From source file:fi.smaa.libror.UTAGMSSolverTest.java

@Test
public void testNecessaryRelationResults() throws InfeasibleConstraintsException {
    solver.solve();// w  w w.  j  a v  a  2s.  c  o m
    RealMatrix nrel = solver.getNecessaryRelation();
    assertArrayEquals(new double[] { 1.0, 0.0, 0.0 }, nrel.getRow(0), 0.0001);
    assertArrayEquals(new double[] { 1.0, 1.0, 0.0 }, nrel.getRow(1), 0.0001);
    assertArrayEquals(new double[] { 1.0, 1.0, 1.0 }, nrel.getRow(2), 0.0001);
}

From source file:dr.math.Procrustes.java

/**
 * procrustinate a single set of coordinates
 * @param X/*from ww w  .  j a  v  a 2  s  .co  m*/
 */
public double[] procrustinate(double[] X) {
    if (X.length != columnDimension) {
        throw new IllegalArgumentException("X does not have the expected number of elements");
    }

    RealMatrix tmp = new Array2DRowRealMatrix(X);

    // rotate, scale and translate
    RealMatrix Xnew = tmp.multiply(R).scalarMultiply(s).add(T);

    return Xnew.getRow(0);
}

From source file:fi.smaa.libror.RORSMAATest.java

@Test
public void testPOIFirstRow() {
    RealMatrix poi = ror.getPOIs();
    assertArrayEquals(new double[] { 1.0, 0.7811, 1.0, 1.0, 0.9999, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
            1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }, poi.getRow(0), 0.02);
}

From source file:fi.smaa.libror.RORSMAATest.java

@Test
public void testRAIFirstRow() {
    RealMatrix rai = ror.getRAIs();
    assertArrayEquals(new double[] { 0.7811, 0.2188, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, rai.getRow(0), 0.02);
}

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

/**
 * calculate D upstream, directionality index upstream
 *
 * @param observed/*from www.  ja va  2 s .c  o  m*/
 * @param n
 * @param gap
 * @return dUpstream
 */
private RealMatrix calculateDirectionalityIndexUpstream(RealMatrix observed, int n, int gap) {

    RealMatrix dUpstream = MatrixTools.cleanArray2DMatrix(n);

    for (int i = 0; i < n; i++) {
        // choose smaller window of two: from 0 to (i-gap) or from (i+gap) to n
        int window = Math.min(n - (i + gap), i - gap);
        window = Math.min(window, n);

        if (window >= gap) {
            double[] row = observed.getRow(i);

            // in MATLAB second index inclusive, but for java need +1
            double[] A = Doubles
                    .toArray(Lists.reverse(Doubles.asList(Arrays.copyOfRange(row, i - window, i - gap + 1))));
            double[] B = Arrays.copyOfRange(row, i + gap, i + window + 1);

            double[] preference = new double[A.length];
            for (int j = 0; j < A.length; j++) {
                preference[j] = (A[j] - B[j]) / (A[j] + B[j]);
            }

            int index = 0;
            for (int j = i + gap; j < i + window + 1; j++) {
                dUpstream.setEntry(i, j, preference[index]);
                index++;
            }
        }
    }
    return dUpstream;
}