/** * Gets a whole column of the matrix as a double array. */ public double[] getColumn(int col) { double[] column = new double[numRows]; int offset = translate(0, col, numRows); for (int i = 0; i < column.length; i++) { column[i] = matrix[offset + i]; } return column; }
@Override public double get(int row, int col) { return matrix[translate(row, col, numRows)]; }
/** * Sets the row to a given double array. */ public void setRow(int row, double[] value) { for (int i = 0; i < value.length; i++) { this.matrix[translate(row, i, numRows)] = value[i]; } }
@Override public void set(int row, int col, double value) { this.matrix[translate(row, col, numRows)] = value; }
/** * Sets the column to a given double array. */ public void setColumn(int col, double[] values) { int offset = translate(0, col, numRows); System.arraycopy(values, 0, matrix, offset, values.length); }
@Override public double max(int column) { double max = Double.MIN_VALUE; int offset = translate(0, column, numRows); for (int i = 0; i < getRowCount(); i++) { double d = matrix[offset + i]; if (d > max) { max = d; } } return max; }
@Override public double min(int column) { double min = Double.MAX_VALUE; int offset = translate(0, column, numRows); for (int i = 0; i < getRowCount(); i++) { double d = matrix[offset + i]; if (d < min) { min = d; } } return min; }
@Override public DenseDoubleMatrix transpose() { DenseDoubleMatrix m = new DenseDoubleMatrix(this.numColumns, this.numRows); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { m.set(j, i, this.matrix[translate(i, j, numRows)]); } } return m; }
@Override public DoubleMatrix subtract(DoubleMatrix other) { DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { m.set(i, j, this.matrix[translate(i, j, numRows)] - other.get(i, j)); } } return m; }
@Override public DoubleMatrix add(DoubleMatrix other) { DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { m.set(i, j, this.matrix[translate(i, j, numRows)] + other.get(i, j)); } } return m; }
@Override public DoubleMatrix divide(DoubleMatrix other) { DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { m.set(i, j, this.matrix[translate(i, j, numRows)] / other.get(i, j)); } } return m; }
@Override public DoubleVector multiplyVectorColumn(DoubleVector v) { DoubleVector vector = new DenseDoubleVector(this.getColumnCount()); for (int col = 0; col < numColumns; col++) { double sum = 0.0d; for (int row = 0; row < numRows; row++) { sum += (matrix[translate(row, col, numRows)] * v.get(row)); } vector.set(col, sum); } return vector; }
@Override public DoubleVector multiplyVectorRow(DoubleVector v) { DoubleVector vector = new DenseDoubleVector(this.getRowCount()); for (int row = 0; row < numRows; row++) { double sum = 0.0d; if (v.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = v.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); sum += (matrix[translate(row, next.getIndex(), numRows)] * next .getValue()); } } else { for (int col = 0; col < numColumns; col++) { sum += (matrix[translate(row, col, numRows)] * v.get(col)); } } vector.set(row, sum); } return vector; }