/** * Returns an empty matrix of the same underlying class as the receiver and of the specified * size. * * @param rows the int number of rows * @param columns the int number of columns */ @Override public Matrix like(int rows, int columns) { return new SparseRowMatrix(rows, columns); }
@Override public Matrix transpose() { SparseRowMatrix srm = new SparseRowMatrix(columns, rows); for (int i = 0; i < columns; i++) { Vector col = columnVectors[i]; if (col.getNumNonZeroElements() > 0) // this should already be optimized srm.assignRow(i, col); } return srm; }
@Override public Matrix like() { return new SparseRowMatrix(rowSize(), columnSize(), randomAccessRows); }
@Override public Matrix assignRow(int row, Vector other) { if (columnSize() != other.size()) { throw new CardinalityException(columnSize(), other.size()); } if (row < 0 || row >= rowSize()) { throw new IndexException(row, rowSize()); } rowVectors[row].assign(other); return this; }
@Test(timeout=50000) public void testTimesCorrect() { Random raw = RandomUtils.getRandom(); // build two large sequential sparse matrices and multiply them Matrix x = new SparseRowMatrix(100, 2000, false) .assign(Functions.random()); Matrix y = new SparseRowMatrix(2000, 100, false) .assign(Functions.random()); Matrix xd = new DenseMatrix(100, 2000).assign(x); Matrix yd = new DenseMatrix(2000, 100).assign(y); assertEquals(0, xd.times(yd).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); assertEquals(0, x.times(yd).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); assertEquals(0, xd.times(y).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); } }
@Override public Matrix times(Matrix other) { if (columnSize() != other.rowSize()) { throw new CardinalityException(columnSize(), other.rowSize()); SparseRowMatrix result = (SparseRowMatrix) like(rowSize(), other.columnSize()); if (other.viewRow(0).isDense()) { Matrix result = other.like(rowSize(), other.columnSize()); } else { SparseRowMatrix result = (SparseRowMatrix) like(rowSize(), other.columnSize());
@Override public Matrix assign(Matrix other, DoubleDoubleFunction function) { int rows = rowSize(); if (rows != other.rowSize()) { throw new CardinalityException(rows, other.rowSize()); int columns = columnSize(); if (columns != other.columnSize()) { throw new CardinalityException(columns, other.columnSize()); Vector.Element element = sparseRowIterator.next(); int col = element.index(); setQuick(row, col, function.apply(element.get(), other.getQuick(row, col))); setQuick(row, col, function.apply(getQuick(row, col), other.getQuick(row, col))); "implementation."); for (int col = 0; col < columns; col++) { setQuick(row, col, function.apply(getQuick(row, col), other.getQuick(row, col)));
public SparseRowMatrix(int rows, int columns, Vector[] vectors, boolean shallowCopy, boolean randomAccess) { super(rows, columns); this.randomAccessRows = randomAccess; this.rowVectors = vectors.clone(); for (int row = 0; row < rows; row++) { if (vectors[row] == null) { // TODO: this can't be right to change the argument vectors[row] = randomAccess ? new RandomAccessSparseVector(numCols(), 10) : new SequentialAccessSparseVector(numCols(), 10); } this.rowVectors[row] = shallowCopy ? vectors[row] : vectors[row].clone(); } }
@Override public Matrix assignColumn(int column, Vector other) { if (rowSize() != other.size()) { throw new CardinalityException(rowSize(), other.size()); } if (column < 0 || column >= columnSize()) { throw new IndexException(column, columnSize()); } for (int row = 0; row < rowSize(); row++) { rowVectors[row].setQuick(column, other.getQuick(row)); } return this; }
@Override public Matrix times(Matrix other) { if (columnSize() != other.rowSize()) { throw new CardinalityException(columnSize(), other.rowSize()); SparseRowMatrix result = (SparseRowMatrix) like(rowSize(), other.columnSize()); if (other.viewRow(0).isDense()) { Matrix result = other.like(rowSize(), other.columnSize()); } else { SparseRowMatrix result = (SparseRowMatrix) like(rowSize(), other.columnSize());
@Override public int[] getNumNondefaultElements() { int[] result = new int[2]; result[ROW] = rowVectors.length; for (int row = 0; row < rowSize(); row++) { result[COL] = Math.max(result[COL], rowVectors[row].getNumNondefaultElements()); } return result; }
public SparseRowMatrix(int rows, int columns, Vector[] vectors, boolean shallowCopy, boolean randomAccess) { super(rows, columns); this.randomAccessRows = randomAccess; this.rowVectors = vectors.clone(); for (int row = 0; row < rows; row++) { if (vectors[row] == null) { // TODO: this can't be right to change the argument vectors[row] = randomAccess ? new RandomAccessSparseVector(numCols(), 10) : new SequentialAccessSparseVector(numCols(), 10); } this.rowVectors[row] = shallowCopy ? vectors[row] : vectors[row].clone(); } }
@Override public Matrix like(int rows, int columns) { return new SparseRowMatrix(rows, columns, randomAccessRows); }
@Override public Matrix like() { return new SparseRowMatrix(rowSize(), columnSize(), randomAccessRows); }
@Override public Matrix transpose() { SparseRowMatrix srm = new SparseRowMatrix(columns, rows); for (int i = 0; i < columns; i++) { Vector col = columnVectors[i]; if (col.getNumNonZeroElements() > 0) // this should already be optimized srm.assignRow(i, col); } return srm; }
@Override public Matrix assignColumn(int column, Vector other) { if (rowSize() != other.size()) { throw new CardinalityException(rowSize(), other.size()); } if (column < 0 || column >= columnSize()) { throw new IndexException(column, columnSize()); } for (int row = 0; row < rowSize(); row++) { rowVectors[row].setQuick(column, other.getQuick(row)); } return this; }
public SparseRowMatrix(int rows, int columns, Vector[] vectors, boolean shallowCopy, boolean randomAccess) { super(rows, columns); this.randomAccessRows = randomAccess; this.rowVectors = vectors.clone(); for (int row = 0; row < rows; row++) { if (vectors[row] == null) { // TODO: this can't be right to change the argument vectors[row] = randomAccess ? new RandomAccessSparseVector(numCols(), 10) : new SequentialAccessSparseVector(numCols(), 10); } this.rowVectors[row] = shallowCopy ? vectors[row] : vectors[row].clone(); } }
/** * Return an empty matrix of the same underlying class as the receiver * * @return a Matrix */ @Override public Matrix like() { return new SparseRowMatrix(rowSize(), columnSize()); }