@Override public final Vector times( final DenseVector vector) { vector.assertDimensionalityEquals(this.getNumColumns()); DenseVector result = new DenseVector(diagonal.length); for (int i = 0; i < diagonal.length; ++i) { result.setElement(i, vector.get(i) * diagonal[i]); } return result; }
@Override public final void dotTimesEquals( final DenseVector other) { this.assertSameDimensionality(other); for (int i = 0; i < values.length; ++i) { values[i] *= other.values[i]; } }
/** * Copy constructor -- creates a deep copy of the input sparse vector. * * @param v The sparse vector to copy */ public SparseVector( final DenseVector v) { this.dimensionality = v.values.length; int nnz = v.countNonZeros(); values = new double[nnz]; indices = new int[nnz]; elements = new TreeMap<>(); int idx = 0; for (int i = 0; i < dimensionality; ++i) { double val = v.values[i]; if (val != 0) { values[idx] = val; indices[idx] = i; ++idx; } } }
@Override public final Matrix outerProduct( final DenseVector other) { int numRows = getDimensionality(); int numCols = other.getDimensionality(); final DenseVector[] rows = new DenseVector[numRows]; for (int i = 0; i < numRows; ++i) { DenseVector row = new DenseVector(numCols); for (int j = 0; j < numCols; ++j) { row.values[j] = values[i] * other.values[j]; } rows[i] = row; } return new DenseMatrix(rows); }
@Override final public Matrix transpose() { // It's the transpose of me int m = getNumColumns(); int n = getNumRows(); final DenseVector[] resultRows = new DenseVector[m]; for (int i = 0; i < m; ++i) { DenseVector row = new DenseVector(n); for (int j = 0; j < n; ++j) { row.setElement(j, get(j, i)); } resultRows[i] = row; } return new DenseMatrix(resultRows); }
Vector result; int len = dimensionality + other.values.length; int nnz = countNonZeros() + other.countNonZeros(); if (nnz > SPARSE_TO_DENSE_THRESHOLD * len) result = new DenseVector(len);
@Override public final Vector preTimes( final SparseVector vector) { vector.assertDimensionalityEquals(this.getNumRows()); DenseVector result = new DenseVector(getNumColumns()); vector.compress(); int[] locs = vector.getIndices(); double[] vals = vector.getValues(); final int numColumns = this.getNumColumns(); for (int i = 0; i < numColumns; ++i) { double entry = 0; for (int j = 0; j < locs.length; ++j) { entry += vals[j] * rows[locs[j]].get(i); } result.setElement(i, entry); } return result; }
@Override final public DenseVector createVector( final int dim) { return new DenseVector(dim); }
@Override final public Vector getColumn( final int columnIndex) { if (columnIndex < 0 || columnIndex >= getNumColumns()) { throw new ArrayIndexOutOfBoundsException("Input column index (" + columnIndex + ") is not within this " + getNumRows() + "x" + getNumColumns() + " matrix"); } DenseVector result = new DenseVector(getNumRows()); final int numRows = this.getNumRows(); for (int i = 0; i < numRows; ++i) { result.values[i] = rows[i].get(columnIndex); } return result; }
/** * {@inheritDoc} * * NOTE: Upon completion this is in the compressed Yale format. * * @return {@inheritDoc} */ @Override // Not final because this method is overridden by the Parallel implementation public Vector times( final DenseVector vector) { vector.assertDimensionalityEquals(this.getNumColumns()); if (!isCompressed()) { compress(); } DenseVector result = new DenseVector(numRows); for (int i = 0; i < numRows; ++i) { result.values[i] = 0; for (int j = firstIndicesForRows[i]; j < firstIndicesForRows[i + 1]; ++j) { result.values[i] += values[j] * vector.values[columnIndices[j]]; } } return result; }
DenseVector v = new DenseVector(m); int imax = Math.min(m, n); for (int i = 0; i < imax; ++i) v.outerProduct(v).scale(tau[i])));
/** * Package private optimized constructor that allows creation of a matrix * from the given rows. * * This is an optimized constructor for within package only. Don't call this * unless you are -super- careful. * * @param rows The array of rows for the matrix. Each row must be the same * length. */ DenseMatrix( final DenseVector[] rows) { super(); final int numRows = rows.length; if (numRows > 0) { final int numColumns = rows[0].getDimensionality(); for (int i = 1; i < numRows; i++) { rows[i].assertDimensionalityEquals(numColumns); } } this.rows = rows; initBlas(); }
@Override final public int getNumColumns() { return (rows == null) ? 0 : (rows.length == 0) ? 0 : rows[0].getDimensionality(); }
@Override public double get( final int rowIndex, final int columnIndex) { return this.rows[rowIndex].get(columnIndex); }
@Override final public void setElement(int rowIndex, int columnIndex, double value) { rows[rowIndex].setElement(columnIndex, value); }
@Override public final Matrix times( final DenseMatrix other) { this.assertMultiplicationDimensions(other); final DenseVector[] rows = new DenseVector[diagonal.length]; for (int i = 0; i < diagonal.length; ++i) { DenseVector v = other.row(i); rows[i] = (DenseVector) v.scale(diagonal[i]); } return new DenseMatrix(rows); }
@Override final public Matrix transpose() { // It's the transpose of me int m = getNumColumns(); int n = getNumRows(); final DenseVector[] resultRows = new DenseVector[m]; for (int i = 0; i < m; ++i) { DenseVector row = new DenseVector(n); for (int j = 0; j < n; ++j) { row.setElement(j, get(j, i)); } resultRows[i] = row; } return new DenseMatrix(resultRows); }
Vector result; int len = dimensionality + other.values.length; int nnz = countNonZeros() + other.countNonZeros(); if (nnz > SPARSE_TO_DENSE_THRESHOLD * len) result = new DenseVector(len);
@Override public final Vector preTimes( final SparseVector vector) { vector.assertDimensionalityEquals(this.getNumRows()); DenseVector result = new DenseVector(getNumColumns()); vector.compress(); int[] locs = vector.getIndices(); double[] vals = vector.getValues(); final int numColumns = this.getNumColumns(); for (int i = 0; i < numColumns; ++i) { double entry = 0; for (int j = 0; j < locs.length; ++j) { entry += vals[j] * rows[locs[j]].get(i); } result.setElement(i, entry); } return result; }
@Override final public Vector scale( final double d) { DenseVector result = new DenseVector(values.length); for (int i = 0; i < values.length; ++i) { result.values[i] = values[i] * d; } return result; }