Java Code Examples for org.apache.commons.math3.linear.RealMatrix#getColumnDimension()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#getColumnDimension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MathObjectAsserts.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void assertRealMatrixEquals(final RealMatrix actual, final RealMatrix expected,
                                          final double relativeTolerance, final double absoluteTolerance) {
    Assert.assertNotNull(expected);
    Assert.assertNotNull(actual);
    Assert.assertEquals(expected.getRowDimension(), actual.getRowDimension());
    Assert.assertEquals(expected.getColumnDimension(), actual.getColumnDimension());
    final int rowDimension = expected.getRowDimension();
    final int colDimension = expected.getColumnDimension();
    for (int i = 0; i < rowDimension; i++) {
        for (int j = 0; j < colDimension; j++) {
            assertDoubleEquals(expected.getEntry(i, j), actual.getEntry(i, j), relativeTolerance, absoluteTolerance,
                    String.format("Different entries at (%d, %d)", i, j));
        }
    }
    Assert.assertEquals(1.0, 1.0, 1.0);
}
 
Example 2
Source File: CorrelationAnalysisEngine.java    From TomboloDigitalConnector with MIT License 6 votes vote down vote up
/**
 * Calculates pearson correlation between pair of columns in the in the matrix, assuming that there is a strict
 * one to one relationship between the matrix columns and the field specifications in the list.
 *
 * Writes the correlation, pValue and standard error to a file using JSON format.
 *
 * @param matrix the input matrix where fields are represented by as columns and subjects by rows
 * @param fields a list of field specifications for which the correlations are to be calculated
 * @param correlationAnalysisOutputPath is the file to which the results are written
 * @throws Exception
 */
public static void calculateAndOutputCorrelations(RealMatrix matrix, List<FieldRecipe> fields,
                                                   String correlationAnalysisOutputPath) throws Exception {
    PearsonsCorrelation correlation = new PearsonsCorrelation(matrix);
    RealMatrix correlationMatrix = correlation.getCorrelationMatrix();
    RealMatrix pValueMatrix = correlation.getCorrelationPValues();
    RealMatrix standardErrorMatrix = correlation.getCorrelationStandardErrors();

    // Output the correlation analysis
    JSONArray correlationArray = new JSONArray();
    for (int i=0; i<correlationMatrix.getRowDimension(); i++){
        for (int j=0; j<correlationMatrix.getColumnDimension(); j++){
            JSONObject correlationObject = new JSONObject();
            correlationObject.put("xFieldLabel", fields.get(i).toField().getLabel());
            correlationObject.put("yFieldLabel", fields.get(j).toField().getLabel());
            correlationObject.put("correlationCoefficient", correlationMatrix.getEntry(i,j));
            correlationObject.put("pValue", pValueMatrix.getEntry(i,j));
            correlationObject.put("standardError", standardErrorMatrix.getEntry(i,j));
            correlationArray.add(correlationObject);
        }
    }
    Writer writer = new OutputStreamWriter(new FileOutputStream(correlationAnalysisOutputPath), "UTF-8");
    writer.write(correlationArray.toJSONString());
    writer.flush();
    writer.close();
}
 
Example 3
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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(&middot,&middot;)</code> is the correlation coefficient and
 * <code>s(&middot;)</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;
}
 
Example 4
Source File: BiDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void checkBiDiagonal(RealMatrix m) {
    final int rows = m.getRowDimension();
    final int cols = m.getColumnDimension();
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            if (rows < cols) {
                if ((i < j) || (i > j + 1)) {
                    Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
                }
            } else {
                if ((i < j - 1) || (i > j)) {
                    Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
                }
            }
        }
    }
}
 
Example 5
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Matrix representing the element-wise square root of m.
 */
private static RealMatrix sqrt(final RealMatrix m) {
    final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.sqrt(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 6
Source File: Cardumen_00163_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Row matrix representing the sums of the rows.
 */
private static RealMatrix sumRows(final RealMatrix m) {
    double[][] d = new double[1][m.getColumnDimension()];
    for (int c = 0; c < m.getColumnDimension(); c++) {
        double sum = 0;
        for (int r = 0; r < m.getRowDimension(); r++) {
            sum += m.getEntry(r, c);
        }
        d[0][c] = sum;
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 7
Source File: JGenProg2017_00111_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param k Diagonal position.
 * @return Upper triangular part of matrix.
 */
private static RealMatrix triu(final RealMatrix m, int k) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 8
Source File: Arja_0079_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 9
Source File: GamaIntMatrix.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public IMatrix fromApacheMatrix(final IScope scope, final RealMatrix rm) {
	if (rm == null) { return null; }
	final GamaFloatMatrix matrix = new GamaFloatMatrix(rm.getColumnDimension(), rm.getRowDimension());
	for (int i = 0; i < numCols; i++) {
		for (int j = 0; j < numRows; j++) {
			matrix.set(scope, i, j, rm.getEntry(j, i));
		}
	}
	return matrix;

}
 
Example 10
Source File: GamaFloatMatrix.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
IList<Double> fromApacheMatrixtoDiagList(final IScope scope, final RealMatrix rm) {
	final IList<Double> vals = GamaListFactory.create(Types.FLOAT);
	for (int i = 0; i < rm.getColumnDimension(); i++) {
		vals.add(rm.getEntry(i, i));
	}
	return vals;
}
 
Example 11
Source File: Arja_00181_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m
 *            Input matrix
 * @return Matrix representing the element-wise square root of m.
 */
private static RealMatrix sqrt(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.sqrt(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 12
Source File: Weight.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param weight Weight matrix.
 * @throws NonSquareMatrixException if the argument is not
 * a square matrix.
 */
public Weight(RealMatrix weight) {
    if (weight.getColumnDimension() != weight.getRowDimension()) {
        throw new NonSquareMatrixException(weight.getColumnDimension(),
                                           weight.getRowDimension());
    }

    weightMatrix = weight.copy();
}
 
Example 13
Source File: Elixir_0021_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param mat Input matrix.
 * @param n Number of row replicates.
 * @param m Number of column replicates.
 * @return a matrix which replicates the input matrix in both directions.
 */
private static RealMatrix repmat(final RealMatrix mat, int n, int m) {
    int rd = mat.getRowDimension();
    int cd = mat.getColumnDimension();
    double[][] d = new double[n * rd][m * cd];
    for (int r = 0; r < n * rd; r++) {
        for (int c = 0; c < m * cd; c++) {
            d[r][c] = mat.getEntry(r % rd, c % cd);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 14
Source File: Arja_0028_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 15
Source File: Cardumen_00257_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 16
Source File: 1_CMAESOptimizer.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 17
Source File: Arja_00154_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param mat Input matrix.
 * @param n Number of row replicates.
 * @param m Number of column replicates.
 * @return a matrix which replicates the input matrix in both directions.
 */
private static RealMatrix repmat(final RealMatrix mat, int n, int m) {
    int rd = mat.getRowDimension();
    int cd = mat.getColumnDimension();
    double[][] d = new double[n * rd][m * cd];
    for (int r = 0; r < n * rd; r++) {
        for (int c = 0; c < m * cd; c++) {
            d[r][c] = mat.getEntry(r % rd, c % cd);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 18
Source File: Arja_00154_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise square (^2) of m.
 */
private static RealMatrix square(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            double e = m.getEntry(r, c);
            d[r][c] = e * e;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 19
Source File: Weight.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param weight Weight matrix.
 * @throws NonSquareMatrixException if the argument is not
 * a square matrix.
 */
public Weight(RealMatrix weight) {
    if (weight.getColumnDimension() != weight.getRowDimension()) {
        throw new NonSquareMatrixException(weight.getColumnDimension(),
                                           weight.getRowDimension());
    }

    weightMatrix = weight.copy();
}
 
Example 20
Source File: Arja_00104_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return Matrix where the elements of m and n are element-wise divided.
 */
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}