@Override public void dotDivideEquals( final Matrix other) { this.assertSameDimensions(other); // This is a dense loop since there is no sparsity in division. final int rowCount = this.getNumRows(); final int columnCount = this.getNumColumns(); for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { this.setElement(i, j, this.getElement(i, j) / other.getElement(i, j)); } } }
@Override public void decrement( final int row, final int column, final double value) { this.increment(row, column, -value); }
@Override public boolean equals( Object other ) { if (other == null) { return false; } else if (this == other) { return true; } else if (other instanceof Matrix) { return this.equals( (Matrix) other, 0.0 ); } else { return false; } }
@Override public void increment( final int row, final int column, final double value) { this.setElement(row, column, this.getElement(row, column) + value); }
@Override public Double set( final int i, final Double value) { final int row = i % this.rowCount; final int column = i / this.rowCount; final double previous = getElement(row, column); setElement(row, column, value); return previous; }
@Override public double[][] toArray() { final int rowCount = this.getNumRows(); final int columnCount = this.getNumColumns(); final double[][] result = new double[rowCount][columnCount]; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { result[i][j] = this.getElement(i, j); } } return result; }
@Override public double trace() { if (this.isSquare() == false) { throw new DimensionalityMismatchException( "Matrix must be square for trace()" ); } // find the min dimension... should be same, but this is more robust int M = this.getNumRows(); int N = this.getNumColumns(); int D = (M < N) ? M : N; double diagonal_sum = 0.0; for (int i = 0; i < D; i++) { diagonal_sum += this.getElement( i, i ); } return diagonal_sum; }
@Override public void assertSameDimensions( Matrix other ) { if (!this.checkSameDimensions(other)) { throw new DimensionalityMismatchException( "Matrices must have same dimensions: " + "(" + this.getNumRows() + "x" + this.getNumColumns() + ") !=" + "(" + other.getNumRows() + "x" + other.getNumColumns() + ")" ); } }
@Override public void assertMultiplicationDimensions( final Matrix postMultiplicationMatrix) { if (!this.checkMultiplicationDimensions(postMultiplicationMatrix)) { throw new DimensionalityMismatchException( "Matrices must have same internal dimensions: " + + this.getNumRows() + "x" + this.getNumColumns() + " != " + + postMultiplicationMatrix.getNumRows() + "x" + postMultiplicationMatrix.getNumColumns()); } }
if (this.getNumColumns() != other.getNumRows()) + this.getNumRows() + "x" + this.getNumColumns() + " with a matrix that is " + other.getNumRows() + "x" + other.getNumColumns()); final Matrix result = this.getMatrixFactory().createMatrix( this.getNumRows(), other.getNumColumns());
@Override public void plusEquals( final Matrix other) { // This is a generic implementation to support interoperability. // Sub-classes should make custom ones for performance. this.assertSameDimensions(other); for (final MatrixEntry entry : other) { this.increment(entry.getRowIndex(), entry.getColumnIndex(), entry.getValue()); } }
@Override public Vector sumOfColumns() { final RingAccumulator<Vector> accumulator = new RingAccumulator<Vector>(); final int numColumns = this.getNumColumns(); for (int j = 0; j < numColumns; j++) { accumulator.accumulate(this.getColumn(j)); } return accumulator.getSum(); }
@Override public void minusEquals( final Matrix other) { // This is a generic implementation to support interoperability. // Sub-classes should make custom ones for performance. this.assertSameDimensions(other); for (final MatrixEntry entry : other) { this.decrement(entry.getRowIndex(), entry.getColumnIndex(), entry.getValue()); } }
/** * Internal function that writes the row onto the destinationVector. * Gets the specified row from the zero-based index and returns a vector * that corresponds to that column * * @param rowIndex zero-based index into the matrix * @param destinationVector * Vector with elements equal to the number of columns in this */ protected void getRowInto( int rowIndex, Vector destinationVector ) { int N = this.getNumColumns(); if (N != destinationVector.getDimensionality()) { throw new DimensionalityMismatchException( N, destinationVector.getDimensionality() ); } for (int j = 0; j < N; j++) { destinationVector.setElement( j, this.getElement( rowIndex, j ) ); } }
/** * Internal function that writes the column onto the destinationVector. * Gets the specified column from the zero-based index and returns a * vector that corresponds to that column. * * @param columnIndex * zero-based index into the matrix * @param destinationVector * Vector with elements equal to the number of rows in this */ protected void getColumnInto( int columnIndex, Vector destinationVector ) { int M = this.getNumRows(); if (M != destinationVector.getDimensionality()) { throw new DimensionalityMismatchException( M, destinationVector.getDimensionality() ); } for (int i = 0; i < M; i++) { destinationVector.setElement( i, this.getElement( i, columnIndex ) ); } }
@Override public Matrix dotDivide( final Matrix other) { final Matrix result = this.clone(); result.dotDivideEquals(other); return result; }
@Override public void dotTimesEquals( final Matrix other) { if (other instanceof AbstractMTJMatrix) { this.dotTimesEquals((AbstractMTJMatrix) other); } else { super.dotTimesEquals(other); } }
@Override public Double get( final int i) { final int row = i % this.rowCount; final int column = i / this.rowCount; return getElement(row, column); }
@Override public void dotTimesEquals( final Matrix other) { // This is a generic implementation to support interoperability. // Sub-classes should make custom ones for performance. this.assertSameDimensions(other); for (final MatrixEntry entry : this) { entry.setValue(entry.getValue() * other.get(entry.getRowIndex(), entry.getColumnIndex())); } }
@Override public double[][] toArray() { final int rowCount = this.getNumRows(); final int columnCount = this.getNumColumns(); final double[][] result = new double[rowCount][columnCount]; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { result[i][j] = this.getElement(i, j); } } return result; }