@Override public DenseDoubleMatrix subtract(double amount) { double[] csjr = new double[this.numRows * this.numColumns]; for (int i = 0; i < matrix.length; i++) { csjr[i] = this.matrix[i] - amount; } return new DenseDoubleMatrix(csjr, this.numRows, this.numColumns, false); }
@Override public DoubleMatrix divide(double scalar) { double[] csjr = new double[this.numRows * this.numColumns]; for (int i = 0; i < matrix.length; i++) { csjr[i] = this.matrix[i] / scalar; } return new DenseDoubleMatrix(csjr, this.numRows, this.numColumns, false); }
@Override public DenseDoubleMatrix multiply(double scalar) { double[] csjr = new double[this.numRows * this.numColumns]; for (int i = 0; i < matrix.length; i++) { csjr[i] = this.matrix[i] * scalar; } return new DenseDoubleMatrix(csjr, this.numRows, this.numColumns, false); }
@Override public DenseDoubleMatrix subtractBy(double amount) { double[] csjr = new double[this.numRows * this.numColumns]; for (int i = 0; i < matrix.length; i++) { csjr[i] = amount - this.matrix[i]; } return new DenseDoubleMatrix(csjr, this.numRows, this.numColumns, false); }
HMM(int numVisibleStates, int numHiddenStates, long seed) { this.seed = seed; this.numVisibleStates = numVisibleStates; this.numHiddenStates = numHiddenStates; this.transitionProbabilityMatrix = new DenseDoubleMatrix(numHiddenStates, numHiddenStates); this.emissionProbabilityMatrix = new DenseDoubleMatrix(numHiddenStates, numVisibleStates); this.hiddenPriorProbability = new DenseDoubleVector(numHiddenStates); }
@Override public DoubleMatrix pow(double x) { double[] csjr = new double[this.numRows * this.numColumns]; for (int i = 0; i < matrix.length; i++) { if (x == 2d) { csjr[i] = this.matrix[i] * this.matrix[i]; } else { csjr[i] = FastMath.pow(this.matrix[i], x); } } return new DenseDoubleMatrix(csjr, this.numRows, this.numColumns, false); }
@Override public DoubleMatrix deepCopy() { return new DenseDoubleMatrix(toArray()); }
/** * Creates a [unitsRightLayer x (unitsLeftLayer + 1)] matrix of weights and * seed the values using the famous uniform distribution formula of LeCun. * Which is calculating the deviation of the weights by SQRT(6)/((num units * left layer)+(num units right layer)) and distributing them with zero mean. */ public WeightMatrix(int unitsLeftLayer, int unitsRightLayer) { this.weights = new DenseDoubleMatrix(unitsRightLayer, unitsLeftLayer + 1); double eInit = Math.sqrt(6) / Math.sqrt(unitsLeftLayer + unitsRightLayer); setWeightsUniformly(seedRandomGenerator(), eInit); }
/** * 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; }
@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 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 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; }
@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 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 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; }
protected DoubleMatrix newInstance(DoubleMatrix mat) { if (mat.isSparse()) { return new SparseDoubleRowMatrix(mat.getRowCount(), mat.getColumnCount()); } else { return new DenseDoubleMatrix(mat.getRowCount(), mat.getColumnCount()); } }
@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; }
/** * @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; }