List of usage examples for org.apache.commons.math3.linear RealMatrix getRowDimension
int getRowDimension();
From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java
/** * Returns the means of columns.//from ww w .j av a 2s .c om * * @param matrix The matrix of which the means of columns to be computed * @return A double array of column means */ public static double[] colMeans(RealMatrix matrix) { // Get the col sums: double[] retval = EMatrixUtils.colSums(matrix); // Iterate over return value and divide by the length: for (int i = 0; i < retval.length; i++) { retval[i] = retval[i] / matrix.getRowDimension(); } // Done, return col means: return retval; }
From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java
/** * Returns the sums of columns./*from w ww . java2s. c om*/ * * @param matrix The matrix of which the sums of columns to be computed * @return A double array of column sums */ public static double[] colSums(RealMatrix matrix) { // Declare and initialize the return value: double[] retval = new double[matrix.getColumnDimension()]; // Iterate over columns and compute totals: for (int col = 0; col < matrix.getColumnDimension(); col++) { for (int row = 0; row < matrix.getRowDimension(); row++) { retval[col] += matrix.getEntry(row, col); } } // Done, return col sums: return retval; }
From source file:jmbench.impl.runtime.CommonsMathAlgorithmFactory.java
public static DenseMatrix64F realToEjml(RealMatrix orig) { if (orig == null) return null; DenseMatrix64F ret = new DenseMatrix64F(orig.getRowDimension(), orig.getColumnDimension()); for (int i = 0; i < ret.numRows; i++) { for (int j = 0; j < ret.numCols; j++) { ret.set(i, j, orig.getEntry(i, j)); }// w w w . j ava 2 s. c om } return ret; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Returns the vec(matrix).//w w w .j a v a2 s . com * @param RealMatrix matrix * @return RealMatrix representing vec(matrix). */ public static RealMatrix getVecMatrix(RealMatrix matrix) { if (matrix == null) { throw new IllegalArgumentException("Null matrix not allowed for " + "getVecMatrix()"); } int numRows = matrix.getRowDimension(); int numCols = matrix.getColumnDimension(); RealMatrix vec = new Array2DRowRealMatrix(numRows * numCols, 1); int newRowNum = 0; //loop through each column for (int c = 0; c < numCols; c++) { //insert column values into new r x 1 matrix for (int r = 0; r < numRows; r++, newRowNum++) { vec.setEntry(newRowNum, 0, matrix.getEntry(r, c)); } } return vec; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Calculate the Kronecker product of two matrices * * @param matrix1 first matrix//from www . j a va 2 s . c o m * @param matrix2 second matrix * @return Kronecker product of matrix 1 and matrix 2 */ public static RealMatrix getKroneckerProduct(RealMatrix matrix1, RealMatrix matrix2) { if (matrix1 == null || matrix2 == null) throw new IllegalArgumentException("null input matrix"); int m1Rows = matrix1.getRowDimension(); int m1Cols = matrix1.getColumnDimension(); int m2Rows = matrix2.getRowDimension(); int m2Cols = matrix2.getColumnDimension(); double[][] productData = new double[m1Rows * m2Rows][m1Cols * m2Cols]; RealMatrix productMatrix = new Array2DRowRealMatrix(productData); for (int col = 0; col < m1Cols; col++) { for (int row = 0; row < m1Rows; row++) { productMatrix.setSubMatrix((matrix2.scalarMultiply(matrix1.getEntry(row, col))).getData(), row * m2Rows, col * m2Cols); } } return productMatrix; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Creates a matrix of equal dimension but with all non-diagonal * elements set to 0//from w w w. j a v a2s . c om * * @param matrix the matrix from which to extract the diagonal * @return diagonal matrix */ public static RealMatrix getDiagonalMatrix(RealMatrix matrix) throws IllegalArgumentException { if (matrix == null) throw new IllegalArgumentException("null input matrix"); double[][] zData = new double[matrix.getRowDimension()][matrix.getColumnDimension()]; for (int row = 0; row < matrix.getRowDimension(); row++) { for (int col = 0; col < matrix.getColumnDimension(); col++) { if (row == col) zData[row][col] = matrix.getEntry(row, col); else zData[row][col] = 0; } } return new Array2DRowRealMatrix(zData); }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Horizontally append two matrices/*w ww. j a v a 2 s . c om*/ * @param matrix * @param column * @return the combined matrix * @throws IllegalArgumentException */ public static RealMatrix getHorizontalAppend(RealMatrix m1, RealMatrix m2) throws IllegalArgumentException { if (m1 == null || m2 == null) throw new IllegalArgumentException("Missing required argument"); if (m1.getRowDimension() != m2.getRowDimension()) throw new IllegalArgumentException("Row dimensions must be equal"); RealMatrix newMatrix = new Array2DRowRealMatrix(m1.getRowDimension(), m1.getColumnDimension() + m2.getColumnDimension()); newMatrix.setSubMatrix(m1.getData(), 0, 0); newMatrix.setSubMatrix(m2.getData(), 0, m1.getColumnDimension()); return newMatrix; }
From source file:jmatbench.commonsmath.CommonsMathAlgorithmFactory.java
public static RowMajorMatrix realToEjml(RealMatrix orig) { if (orig == null) return null; RowMajorMatrix ret = new RowMajorMatrix(orig.getRowDimension(), orig.getColumnDimension()); for (int i = 0; i < ret.numRows; i++) { for (int j = 0; j < ret.numCols; j++) { ret.set(i, j, orig.getEntry(i, j)); }//www . j ava2 s. co m } return ret; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Find the maximum value in the matrix/* w w w. j a v a2 s.c o m*/ * @param matrix */ public static double getMaxValue(RealMatrix matrix) { double max; if (matrix == null) { max = Double.NaN; } else { max = Double.NEGATIVE_INFINITY; for (int r = 0; r < matrix.getRowDimension(); r++) { for (int c = 0; c < matrix.getColumnDimension(); c++) { double value = matrix.getEntry(r, c); if (value > max) { max = value; } } } } return max; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Return the element wise sum of squares * @param matrix input matrix/*w ww .jav a2 s . com*/ * @return sum of squares */ public static double getSumOfSquares(RealMatrix matrix) { if (matrix == null) { throw new IllegalArgumentException("Null matrix not allowed for " + "getSumOfSquares()"); } double sum = 0.0; for (int r = 0; r < matrix.getRowDimension(); r++) { for (int c = 0; c < matrix.getColumnDimension(); c++) { double value = matrix.getEntry(r, c); sum += value * value; } } return sum; }