@Override public final Vector times( final SparseVector vector) { vector.assertDimensionalityEquals(this.getNumColumns()); SparseVector result = new SparseVector(diagonal.length); vector.compress(); int[] locs = vector.getIndices(); for (int i = 0; i < locs.length; ++i) { result.setElement(locs[i], vector.getValues()[i] * diagonal[locs[i]]); } return result; }
@Override public final void dotTimesEquals( final DenseVector other) { this.assertSameDimensionality(other); compress(); for (int i = 0; i < values.length; ++i) { values[i] *= other.values[indices[i]]; } }
@Override public final Vector stack( final SparseVector other) { compress(); other.compress(); int len = dimensionality + other.dimensionality; int nnz = countNonZeros() + other.countNonZeros(); SparseVector result = new SparseVector(len); result.values = new double[nnz]; result.indices = new int[nnz]; int idx = 0; for (int i = 0; i < indices.length; ++i) { result.values[idx] = values[i]; result.indices[idx] = indices[i]; ++idx; } for (int i = 0; i < other.indices.length; ++i) { result.values[idx] = other.values[i]; result.indices[idx] = other.indices[i] + dimensionality; ++idx; } return result; }
@Override final public Vector scale( final double d) { compress(); SparseVector result = new SparseVector(this); for (int i = 0; i < result.values.length; ++i) { result.values[i] *= d; } return result; }
int len = values.length + other.getDimensionality(); int nnz = countNonZeros() + other.countNonZeros(); if (nnz > SparseVector.SPARSE_TO_DENSE_THRESHOLD * len) result = new SparseVector(len); other.compress(); int[] locs = other.getIndices(); double[] vals = other.getValues(); int idx = 0; final int otherDimensionality = other.getDimensionality(); for (int i = 0; i < otherDimensionality; ++i)
final SparseVector vector) vector.assertDimensionalityEquals(this.getNumColumns()); if (!isCompressed()) if (!vector.isCompressed()) vector.compress(); int[] vLocs = vector.getIndices(); double[] vVals = vector.getValues(); for (int i = 0; i < numRows; ++i) return new SparseVector(result);
@Override public final Matrix outerProduct( final SparseVector other) { int numRows = getDimensionality(); int numCols = other.getDimensionality(); SparseMatrix result = new SparseMatrix(numRows, numCols, true); other.compress(); int[] locs = other.getIndices(); double[] vals = other.getValues(); for (int i = 0; i < numRows; ++i) { SparseVector row = new SparseVector(numCols); for (int j = 0; j < locs.length; ++j) { row.setElement(locs[j], values[i] * vals[j]); } result.setRowInternal(i, row); } return result; }
@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; }
for (int i = 0; i < numRows; ++i) rows[i].compress(); nonZeroCount += rows[i].getIndices().length; for (int j = 0; j < rows[i].getIndices().length; ++j) values[curIdx] = rows[i].getValues()[j]; columnIndices[curIdx] = rows[i].getIndices()[j]; ++curIdx; rows[i].clear();
@Override final public Vector getRow( final int rowIndex) { if (rowIndex < 0 || rowIndex >= diagonal.length) { throw new ArrayIndexOutOfBoundsException("Input row index (" + rowIndex + ") is not within this " + diagonal.length + "x" + diagonal.length + " matrix"); } SparseVector result = new SparseVector(diagonal.length); result.setElement(rowIndex, diagonal[rowIndex]); return result; }
@Override public final void plusEquals( final SparseVector other) { this.assertSameDimensionality(other); other.compress(); int[] locs = other.getIndices(); double[] vals = other.getValues(); for (int i = 0; i < locs.length; ++i) { values[locs[i]] += vals[i]; } }
@Override public final void dotTimesEquals( final SparseVector other) { this.assertSameDimensionality(other); compress(); other.compress(); int nnz = numNonZeroAfterOp(other, SparseMatrix.Combiner.AND); double[] valsAfter = new double[nnz]; int[] locsAfter = new int[nnz]; int outidx = 0; int otheridx = 0; for (int i = 0; i < values.length; ++i) { while (other.indices[otheridx] < indices[i]) { ++otheridx; } if (other.indices[otheridx] == indices[i]) { valsAfter[outidx] = values[i] * other.values[otheridx]; locsAfter[outidx] = indices[i]; ++outidx; ++otheridx; } } values = valsAfter; indices = locsAfter; }
final double value) if (this.isCompressed()) decompress(); checkBounds(index); if (value == 0.0)
/** * This method is provided so that the calling programmer can explicitly * declare when a matrix should be decompressed from the compressed Yale * format. Note that the method will be automatically compressed and * decompressed when various methods are called (e.g., decompressed for set* * methods, compressed for times, dotTimes, etc.). */ final public void decompress() { if (!isCompressed()) { return; } for (int i = 0; i < getNumRows(); ++i) { rows[i].clear(); for (int j = firstIndicesForRows[i]; j < firstIndicesForRows[i + 1]; ++j) { rows[i].setElement(columnIndices[j], values[j]); } } values = null; firstIndicesForRows = columnIndices = null; }
final int index) checkBounds(index); if (isCompressed())
@Override public int getEntryCount() { this.compress(); return this.values.length; }
@Override public SparseVector createVectorCapacity( final int dimensionality, final int initialCapacity) { return new SparseVector(dimensionality); }
/** * {@inheritDoc} * * NOTE: Upon completion this is in the compressed Yale format. */ @Override final public void identity() { // First get rid of any values already in there for (int i = 0; i < numRows; ++i) { rows[i].clear(); } // Kill compressed representation if there ... update to new values int diagLen = (numRows <= numCols) ? numRows : numCols; values = new double[diagLen]; Arrays.fill(values, 1); firstIndicesForRows = getZeroToNMinusOneArray(numRows + 1); columnIndices = getZeroToNMinusOneArray(diagLen); // We just need to update the first indices for the rows if this is a // tall rectangle matrix for (int i = diagLen; i < numRows + 1; ++i) { firstIndicesForRows[i] = diagLen; } }
@Override public final double dotProduct( final SparseVector other) { return other.dotProduct(this); }
@Override public final double euclideanDistanceSquared( final SparseVector other) { return other.euclideanDistanceSquared(this); }