/** * Simple copy constructor, does a deep copy of the given parameter. * * @param otherMatrix the other matrix. */ public DenseDoubleMatrix(double[][] otherMatrix) { this(otherMatrix.length, otherMatrix[0].length); for (int row = 0; row < otherMatrix.length; row++) { for (int col = 0; col < otherMatrix[row].length; col++) { set(row, col, otherMatrix[row][col]); } } }
/** * A distance matrix (NxN) based on n given points and a distance measurer. */ private DoubleMatrix generateDistanceMatrix(DistanceMeasurer measurer, List<DoubleVector> pointList) { final int n = pointList.size(); DenseDoubleMatrix matrix = new DenseDoubleMatrix(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { final double distance = measurer.measureDistance(pointList.get(i), pointList.get(j)); matrix.set(i, j, distance); } } return matrix; }
@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 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; }
/** * 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; }
/** * Generates a matrix out of a vector list. it treats the entries as rows and * the vector itself contains the values of the columns. * * @param vectorArray the list of vectors. */ public DenseDoubleMatrix(List<DoubleVector> vec) { this(vec.size(), vec.get(0).getDimension()); int row = 0; for (DoubleVector value : vec) { for (int col = 0; col < value.getDimension(); col++) { set(row, col, value.get(col)); } row++; } }
@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; }
/** * Generates a single-row matrix with the given first column value (usually * used as a bias) and the given vector as the appendant. * * @param firstColumnValue the value at dimension 0. * @param vector the vector for the following dimensions (1..n+1) */ public DenseDoubleMatrix(double firstColumnValue, DoubleVector vector) { this(1, vector.getDimension() + 1); set(0, 0, firstColumnValue); if (vector.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = vector.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); set(0, next.getIndex() + 1, next.getValue()); } } else { System.arraycopy(vector.toArray(), 0, this.matrix, 1, vector.getDimension()); } }
@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; }
/** * @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; }