/** * Get a single row of the matrix as a double array. */ public double[] getRow(int row) { double[] rowArray = new double[getColumnCount()]; for (int i = 0; i < getColumnCount(); i++) { rowArray[i] = get(row, i); } return rowArray; }
/** * Writes a dense matrix to the given output stream. The layout is as simple * as in memory, first the dimension is written (row count, column count as * integer) afterwards there are following row*columns doubles in a row major * fashion. * * @param mat the dense matrix to serialize. * @param out the output. * @throws IOException in case of an IO error. */ public static void writeDenseMatrix(DenseDoubleMatrix mat, DataOutput out) throws IOException { out.writeInt(mat.getRowCount()); out.writeInt(mat.getColumnCount()); for (int row = 0; row < mat.getRowCount(); row++) { for (int col = 0; col < mat.getColumnCount(); col++) { out.writeDouble(mat.get(row, col)); } } }
@Override public DoubleMatrix slice(int rowOffset, int rowMax, int colOffset, int colMax) { DenseDoubleMatrix m = new DenseDoubleMatrix(rowMax - rowOffset, colMax - colOffset); for (int row = rowOffset; row < rowMax; row++) { for (int col = colOffset; col < colMax; col++) { m.set(row - rowOffset, col - colOffset, this.get(row, col)); } } return m; }
@Override public DoubleMatrix multiply(DoubleMatrix other) { int m = this.numRows; int n = this.numColumns; int p = other.getColumnCount(); DenseDoubleMatrix matrix = new DenseDoubleMatrix(m, p); for (int k = 0; k < n; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { matrix.set(i, j, matrix.get(i, j) + get(i, k) * other.get(k, j)); } } } return matrix; }
@Override public DoubleMatrix multiplyElementWise(DoubleMatrix other) { DenseDoubleMatrix matrix = new DenseDoubleMatrix(this.numRows, this.numColumns); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { matrix.set(i, j, this.get(i, j) * (other.get(i, j))); } } return matrix; }