/** * Set the value at the given index, without checking bounds * * @param row an int row index into the receiver * @param column an int column index into the receiver * @param value a double value to set */ @Override public void setQuick(int row, int column, double value) { base.setQuick(rowPivot[row], columnPivot[column], value); }
@Override public void setQuick(int row, int column, double value) { matrix.setQuick(offset[ROW] + row, offset[COL] + column, value); }
/** * Set the value at the given index, without checking bounds * * @param index an int index into the receiver * @param value a double value to set */ @Override public void setQuick(int index, double value) { matrix.setQuick(row + rowStride * index, column + columnStride * index, value); }
@Override public void setQuick(int row, int column, double value) { m.setQuick(column, row, value); }
static Matrix addLambdaTimesNuiTimesE(Matrix matrix, double lambda, int nui) { Preconditions.checkArgument(matrix.numCols() == matrix.numRows(), "Must be a Square Matrix"); double lambdaTimesNui = lambda * nui; int numCols = matrix.numCols(); for (int n = 0; n < numCols; n++) { matrix.setQuick(n, n, matrix.getQuick(n, n) + lambdaTimesNui); } return matrix; }
/** * Return the block diagonal eigenvalue matrix * * @return D */ public Matrix getD() { Matrix x = new DenseMatrix(n, n); x.assign(0); x.viewDiagonal().assign(d); for (int i = 0; i < n; i++) { double v = e.getQuick(i); if (v > 0) { x.setQuick(i, i + 1, v); } else if (v < 0) { x.setQuick(i, i - 1, v); } } return x; }
/** * Used internally by assign() to update multiple indices and values at once. * Only really useful for sparse vectors (especially SequentialAccessSparseVector). * <p> * If someone ever adds a new type of sparse vectors, this method must merge (index, value) pairs into the vector. * * @param updates a mapping of indices to values to merge in the vector. */ @Override public void mergeUpdates(OrderedIntDoubleMapping updates) { int[] indices = updates.getIndices(); double[] values = updates.getValues(); for (int i = 0; i < updates.getNumMappings(); ++i) { matrix.setQuick(row + rowStride * indices[i], column + columnStride * indices[i], values[i]); } } }
@Override public Matrix transpose() { int rows = rowSize(); int columns = columnSize(); Matrix result = like(columns, rows); for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { result.setQuick(col, row, getQuick(row, col)); } } return result; }
@Override public Matrix divide(double x) { Matrix result = like(); for (int row = 0; row < rowSize(); row++) { for (int col = 0; col < columnSize(); col++) { result.setQuick(row, col, getQuick(row, col) / x); } } return result; }
@Override public Matrix plus(double x) { Matrix result = like(); int rows = rowSize(); int columns = columnSize(); for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { result.setQuick(row, col, getQuick(row, col) + x); } } return result; }
@Override public Matrix times(double x) { Matrix result = like(); int rows = rowSize(); int columns = columnSize(); for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { result.setQuick(row, col, getQuick(row, col) * x); } } return result; }
@Override public Matrix matrixFactory(double[][] values) { Matrix matrix = new SparseMatrix(values.length, values[0].length); for (int row = 0; row < matrix.rowSize(); row++) { for (int col = 0; col < matrix.columnSize(); col++) { matrix.setQuick(row, col, values[row][col]); } } return matrix; }
@Override public Matrix matrixFactory(double[][] values) { Matrix matrix = new SparseColumnMatrix(values.length, values[0].length); for (int row = 0; row < matrix.rowSize(); row++) { for (int col = 0; col < matrix.columnSize(); col++) { matrix.setQuick(row, col, values[row][col]); } } return matrix; }
@Override public Matrix matrixFactory(double[][] values) { Matrix matrix = new SparseRowMatrix(values.length, values[0].length); for (int row = 0; row < matrix.rowSize(); row++) { for (int col = 0; col < matrix.columnSize(); col++) { matrix.setQuick(row, col, values[row][col]); } } return matrix; }
@Override public Matrix assignColumn(int column, Vector other) { if (rowSize() != other.size()) { throw new CardinalityException(rowSize(), other.size()); } for (int row = 0; row < rowSize(); row++) { matrix.setQuick(row + offset[ROW], column + offset[COL], other .getQuick(row)); } return this; }
@Override public Matrix assignRow(int row, Vector other) { if (columnSize() != other.size()) { throw new CardinalityException(columnSize(), other.size()); } for (int col = 0; col < columnSize(); col++) { matrix .setQuick(row + offset[ROW], col + offset[COL], other.getQuick(col)); } return this; }
@Test public void testSetQuick() { for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { test.setQuick(row, col, 1.23); assertEquals("value[" + row + "][" + col + ']', 1.23, test.getQuick( row, col), EPSILON); } } }
@Test public void testSetQuick() { for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { test.setQuick(row, col, 1.23); assertEquals("value[" + row + "][" + col + ']', 1.23, test.getQuick( row, col), EPSILON); } } }
@Override public Matrix minus(Matrix other) { 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()); } Matrix result = like(); for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { result.setQuick(row, col, getQuick(row, col) - other.getQuick(row, col)); } } return result; }
@Override public Matrix plus(Matrix other) { 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()); } Matrix result = like(); for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { result.setQuick(row, col, getQuick(row, col) + other.getQuick(row, col)); } } return result; }