@Override public DoubleVector multiply(double scalar) { DoubleVector v = new SparseDoubleVector(this.dimension, this.vector.size()); Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() * scalar); } return v; }
@Override public DoubleVector apply(DoubleVectorFunction func) { SparseDoubleVector newV = new SparseDoubleVector(this.dimension, this.vector.size()); Iterator<DoubleVectorElement> iterate = this.iterate(); while (iterate.hasNext()) { DoubleVectorElement next = iterate.next(); double res = func.calculate(next.getIndex(), next.getValue()); newV.set(next.getIndex(), res); } return newV; }
@Override protected DoubleVectorElement computeNext() { if (index < getDimension()) { element.setIndex(index); element.setValue(get(index)); index++; return element; } else { return endOfData(); } }
@Override public String toString() { if (getLength() < 50) { return vector.toString(); } else { return getDimension() + "x1"; } }
@Override public double max() { double res = -Double.MAX_VALUE; Iterator<DoubleVectorElement> it = this.iterateNonZero(); int iterated = 0; while (it.hasNext()) { DoubleVectorElement e = it.next(); if (res < e.getValue()) { res = e.getValue(); } iterated++; } // at the end check for zero, because we have skipped zero elements if (iterated != getDimension() && res < 0d) { res = 0d; } return res; }
@Override public DoubleVector deepCopy() { return new SparseDoubleVector(this); }
@Override public void set(int row, int col, double value) { if (value != 0.0d) { SparseDoubleVector sparseDoubleVector = matrix.get(row); if (sparseDoubleVector == null) { sparseDoubleVector = new SparseDoubleVector(getColumnCount()); matrix.put(row, sparseDoubleVector); } sparseDoubleVector.set(col, value); } }
@Override public DoubleVector subtractFrom(double scalar) { DoubleVector v = new SparseDoubleVector(this.dimension, this.vector.size()); Iterator<DoubleVectorElement> it = iterate(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), scalar - e.getValue()); } return v; }
@Override public DoubleVector subtract(DoubleVector other) { DoubleVector result = new SparseDoubleVector(this.vector.fastDeepCopy(), this.getDimension()); Iterator<DoubleVectorElement> iter = other.iterateNonZero(); while (iter.hasNext()) { DoubleVectorElement e = iter.next(); int index = e.getIndex(); result.set(index, result.get(index) - e.getValue()); } return result; }
/** * Constructs a new {@link SparseDoubleVector}. * * @param arr the given vector to copy. */ public SparseDoubleVector(double[] arr) { this(arr.length); for (int i = 0; i < arr.length; i++) { set(i, arr[i]); } }
@Override public double sum() { double sum = 0.0d; Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); sum += e.getValue(); } return sum; }
@Override public DoubleVector divideFrom(DoubleVector vector) { DoubleVector v = new SparseDoubleVector(this.dimension, this.vector.size()); Iterator<DoubleVectorElement> it = vector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() / get(e.getIndex())); } return v; }
@Override public DoubleVector multiply(DoubleVector s) { // take a shortcut by just iterating over the non-zero elements of the // smaller vector of both multiplicants. DoubleVector smallestVector = s.getLength() < getLength() ? s : this; DoubleVector vec = new SparseDoubleVector(s.getDimension(), smallestVector.getLength()); DoubleVector largerVector = smallestVector == this ? s : this; Iterator<DoubleVectorElement> it = smallestVector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement next = it.next(); double otherValue = largerVector.get(next.getIndex()); vec.set(next.getIndex(), next.getValue() * otherValue); } return vec; }
@Override public DoubleMatrix subtractBy(double amount) { DoubleMatrix result = new SparseDoubleRowMatrix(this.getRowCount(), this.getColumnCount()); for (int row : this.matrix.keys()) { Iterator<DoubleVectorElement> iterate = matrix.get(row).iterate(); while (iterate.hasNext()) { DoubleVectorElement e = iterate.next(); result.set(row, e.getIndex(), amount - e.getValue()); } } return result; }
@Override public DoubleVector slice(int length) { return slice(0, length); }
@Override public double get(int row, int col) { SparseDoubleVector vector = matrix.get(row); if (vector == null) { return NOT_FLAGGED; } else { return vector.get(col); } }
@Override public double dot(DoubleVector s) { double dotProduct = 0.0d; // take a shortcut by just iterating over the non-zero elements of the // smaller vector of both multiplicants. DoubleVector smallestVector = s.getLength() < getLength() ? s : this; DoubleVector largerVector = smallestVector == this ? s : this; Iterator<DoubleVectorElement> it = smallestVector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement next = it.next(); double d = largerVector.get(next.getIndex()); dotProduct += d * next.getValue(); } return dotProduct; }
@Override public DoubleMatrix subtract(DoubleVector vec) { DoubleMatrix result = new SparseDoubleRowMatrix(this.getRowCount(), this.getColumnCount()); for (int row : this.matrix.keys()) { SparseDoubleVector rowVec = matrix.get(row); result.setRowVector(row, rowVec.subtract(vec.get(row))); } return result; }
@Override public void setRowVector(int rowIndex, DoubleVector row) { matrix.put(rowIndex, new SparseDoubleVector(row)); }
@Override public DoubleVector predict(DoubleVector features) { if (features.isSparse()) { SparseDoubleVector tmp = new SparseDoubleVector( features.getDimension() + 1); tmp.set(0, 1d); Iterator<DoubleVectorElement> iterateNonZero = features.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); tmp.set(next.getIndex() + 1, next.getValue()); } features = tmp; } else { features = new DenseDoubleVector(1d, features.toArray()); } return new DenseDoubleVector(new double[]{SIGMOID.get().apply( features.dot(theta))}); }