@Override public int[] rowIndices() { int[] x = new int[getRowCount()]; for (int i = 0; i < getRowCount(); i++) x[i] = i; return x; }
@Override public double[][] toArray() { double[][] mat = new double[getRowCount()][getColumnCount()]; int index = 0; for (int col = 0; col < getColumnCount(); col++) { for (int row = 0; row < getRowCount(); row++) { mat[row][col] = matrix[index++]; } } return mat; }
@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; }
/** * 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)); } } }
/** * Reads a dense matrix from the given input stream. * * @param in the input stream. * @return a new dense matrix from the stream. * @throws IOException in case of an IO error. */ public static DenseDoubleMatrix readDenseMatrix(DataInput in) throws IOException { DenseDoubleMatrix mat = new DenseDoubleMatrix(in.readInt(), in.readInt()); for (int row = 0; row < mat.getRowCount(); row++) { for (int col = 0; col < mat.getColumnCount(); col++) { mat.set(row, col, in.readDouble()); } } return mat; }
@Override public DoubleMatrix divide(DoubleVector vec) { DoubleMatrix cop = new DenseDoubleMatrix(this.getRowCount(), this.getColumnCount()); for (int i = 0; i < this.getColumnCount(); i++) { cop.setColumnVector(i, getColumnVector(i).divide(vec)); } return cop; }
@Override public DenseDoubleMatrix subtract(DoubleVector vec) { DenseDoubleMatrix cop = new DenseDoubleMatrix(this.getRowCount(), this.getColumnCount()); for (int i = 0; i < this.getColumnCount(); i++) { cop.setColumnVector(i, getColumnVector(i).subtract(vec)); } return cop; }
@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; }
/** * @return a log'd matrix that was guarded against edge cases of the * logarithm. */ public static DoubleMatrix logMatrix(DoubleMatrix input) { DenseDoubleMatrix log = new DenseDoubleMatrix(input.getRowCount(), input.getColumnCount()); for (int row = 0; row < log.getRowCount(); row++) { for (int col = 0; col < log.getColumnCount(); col++) { double d = input.get(row, col); log.set(row, col, guardedLogarithm(d)); } } return log; }
/** * Creates a new matrix consisting out of polynomials of the input matrix.<br/> * Considering you want to do a 2 polynomial out of 3 columns you get:<br/> * (SEED: x^1 | y^1 | z^1 )| x^2 | y^2 | z^2 for the columns of the returned * matrix. * * @param seed matrix to add polynoms of it. * @param num how many polynoms, 2 for quadratic, 3 for cubic and so forth. * @return the new matrix. */ public static DenseDoubleMatrix createPolynomials(DenseDoubleMatrix seed, int num) { if (num == 1) return seed; DenseDoubleMatrix m = new DenseDoubleMatrix(seed.getRowCount(), seed.getColumnCount() * num); int index = 0; for (int c = 0; c < m.getColumnCount(); c += num) { double[] column = seed.getColumn(index++); m.setColumn(c, column); for (int i = 2; i < num + 1; i++) { DoubleVector pow = new DenseDoubleVector(column).pow(i); m.setColumn(c + i - 1, pow.toArray()); } } return m; }