/** * Get an entry of the tableau. * @param row row index * @param column column index * @return entry at (row, column) */ protected final double getEntry(final int row, final int column) { return tableau.getEntry(row, column); }
public static void printApacheMatrix(RealMatrix matrix) { int nRows = matrix.getRowDimension(); int nCols = matrix.getColumnDimension(); System.out.println("Apache Commons RealMatrix: Shape: [" + nRows + "," + nCols + "]"); for (int i = 0; i < nRows; i++) { for (int j = 0; j < nCols; j++) { System.out.print(matrix.getEntry(i, j)); if (j != nCols - 1) System.out.print(", "); else System.out.println(); } } } }
/** * Copies a column from m1 to m2. * * @param m1 Source matrix. * @param col1 Source column. * @param m2 Target matrix. * @param col2 Target column. */ private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) { for (int i = 0; i < m1.getRowDimension(); i++) { m2.setEntry(i, col2, m1.getEntry(i, col1)); } }
/** * Copies a column from m1 to m2. * * @param m1 Source matrix. * @param col1 Source column. * @param m2 Target matrix. * @param col2 Target column. */ private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) { for (int i = 0; i < m1.getRowDimension(); i++) { m2.setEntry(i, col2, m1.getEntry(i, col1)); } }
/** * @param m Input matrix 1. * @param n Input matrix 2. * @return the matrix where the elements of m and n are element-wise multiplied. */ private static RealMatrix times(final RealMatrix m, final RealMatrix n) { 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] = m.getEntry(r, c) * n.getEntry(r, c); } } return new Array2DRowRealMatrix(d, false); }
/** * @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) { 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] = m.getEntry(r, c) / n.getEntry(r, c); } } return new Array2DRowRealMatrix(d, false); }
/** * @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) { 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] = m.getEntry(r, c) / n.getEntry(r, c); } } return new Array2DRowRealMatrix(d, false); }
/** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { for (int i = getNumObjectiveFunctions(); i < getWidth() - 1; i++) { final double entry = tableau.getEntry(0, i); if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; }
/** * @param m Input matrix. * @param cols Columns to select. * @return Matrix representing the selected columns. */ private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) { final double[][] d = new double[m.getRowDimension()][cols.length]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < cols.length; c++) { d[r][c] = m.getEntry(r, cols[c]); } } return new Array2DRowRealMatrix(d, false); }
/** * @param m Input matrix. * @param cols Columns to select. * @return Matrix representing the selected columns. */ private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) { final double[][] d = new double[m.getRowDimension()][cols.length]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < cols.length; c++) { d[r][c] = m.getEntry(r, cols[c]); } } return new Array2DRowRealMatrix(d, false); }
public static INDArray convertFromApacheMatrix(RealMatrix matrix) { int[] shape = new int[] {matrix.getRowDimension(), matrix.getColumnDimension()}; INDArray out = Nd4j.create(shape); for (int i = 0; i < shape[0]; i++) { for (int j = 0; j < shape[1]; j++) { double value = matrix.getEntry(i, j); out.putScalar(new int[] {i, j}, value); } } return out; }
/** {@inheritDoc} */ public RealVector getSigma(double covarianceSingularityThreshold) { final RealMatrix cov = this.getCovariances(covarianceSingularityThreshold); final int nC = cov.getColumnDimension(); final RealVector sig = new ArrayRealVector(nC); for (int i = 0; i < nC; ++i) { sig.setEntry(i, FastMath.sqrt(cov.getEntry(i,i))); } return sig; }
/** * @param m Input matrix. * @return Matrix representing the element-wise square of m. */ private static RealMatrix square(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++) { double e = m.getEntry(r, c); d[r][c] = e * e; } } return new Array2DRowRealMatrix(d, false); }
/** * @param m Input matrix. * @param k Diagonal position. * @return Upper triangular part of matrix. */ private static RealMatrix triu(final RealMatrix m, int k) { 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] = r <= c - k ? m.getEntry(r, c) : 0; } } return new Array2DRowRealMatrix(d, false); }
/** * @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] = FastMath.sqrt(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); }
/** * @param m Input matrix * @return Matrix representing the element-wise logarithm of m. */ private static RealMatrix log(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] = FastMath.log(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); }
/** * @param m Input matrix * @return Matrix representing the element-wise logarithm of m. */ private static RealMatrix log(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] = FastMath.log(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); }
/** * @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] = FastMath.sqrt(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); }
/** {@inheritDoc} */ public RealMatrix add(RealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkAdditionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) + m.getEntry(row, col)); } } return out; }
/** {@inheritDoc} */ public RealMatrix subtract(final RealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) - m.getEntry(row, col)); } } return out; }