@Override public DoubleVector deepCopy() { return new SequentialSparseDoubleVector(this); }
@Override public DoubleVector subtract(DoubleVector other) { SequentialSparseDoubleVector result = new SequentialSparseDoubleVector(this); if (other.isSparse() && other instanceof SequentialSparseDoubleVector) { SequentialSparseDoubleVector vec = (SequentialSparseDoubleVector) other; result.mapping.merge(vec.mapping, (l, r) -> l - r); } else { 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; }
@Override protected DoubleVectorElement computeNext() { if (index < getDimension()) { element.setIndex(index); element.setValue(get(index)); index++; return element; } else { return endOfData(); } }
@Override public DoubleVector apply(DoubleVectorFunction func) { SequentialSparseDoubleVector newV = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> iterate = iterate(); while (iterate.hasNext()) { DoubleVectorElement next = iterate.next(); double res = func.calculate(next.getIndex(), next.getValue()); newV.set(next.getIndex(), res); } return newV; }
@Override public DoubleVector add(double scalar) { DoubleVector v = new SequentialSparseDoubleVector(dimension, mapping.getNumMappings()); Iterator<DoubleVectorElement> it = iterate(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() + scalar); } return v; }
@Override public DoubleVector divide(DoubleVector vector) { DoubleVector v = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() / vector.get(e.getIndex())); } return v; }
@Override public DoubleVector add(DoubleVector other) { DoubleVector result = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> iter = other.iterateNonZero(); while (iter.hasNext()) { DoubleVectorElement e = iter.next(); int index = e.getIndex(); result.set(index, get(index) + e.getValue()); } return result; }
@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 SequentialSparseDoubleVector(s.getDimension()); 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 String toString() { if (getLength() < 50) { StringBuilder sb = new StringBuilder("["); for (int i = 0; i < mapping.getNumMappings(); i++) { sb.append(mapping.getIndices()[i]); sb.append('='); sb.append(mapping.getValues()[i]); if (i != mapping.getNumMappings() - 1) { sb.append(", "); } } sb.append(']'); return sb.toString(); } else { return getDimension() + "x1"; } }
@Override public DoubleVector slice(int length) { return slice(0, length); }
@Override public double[] toArray() { double[] d = new double[dimension]; Iterator<DoubleVectorElement> it = this.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); d[e.getIndex()] = e.getValue(); } return d; }
@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 double max() { double res = -Double.MAX_VALUE; for (int i = 0; i < mapping.getNumMappings(); i++) { if (res < mapping.getValues()[i]) { res = mapping.getValues()[i]; } } // at the end check for zero, because we have skipped zero elements if (mapping.getNumMappings() != getDimension() && res == 0d) { res = 0d; } return res; }
@Override public DoubleVector apply(DoubleVector other, DoubleDoubleVectorFunction func) { SequentialSparseDoubleVector newV = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> iterate = iterate(); while (iterate.hasNext()) { DoubleVectorElement next = iterate.next(); double res = func.calculate(next.getIndex(), next.getValue(), other.get(next.getIndex())); newV.set(next.getIndex(), res); } return newV; }
@Override public DoubleVector subtract(double scalar) { DoubleVector v = new SequentialSparseDoubleVector(dimension, mapping.getNumMappings()); Iterator<DoubleVectorElement> it = iterate(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() - scalar); } return v; }
@Override public DoubleVector slice(int start, int end) { DoubleVector nv = new SequentialSparseDoubleVector(end - start); Iterator<DoubleVectorElement> iterateNonZero = iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); if (next.getIndex() >= start && next.getIndex() < end) { nv.set(next.getIndex() - start, next.getValue()); } } return nv; }
@Override public DoubleVector divideFrom(DoubleVector vector) { DoubleVector v = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> it = vector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() / get(e.getIndex())); } return v; }
protected DoubleVector randomInitialize(int dimension) { if (sparseWeights) { return new SequentialSparseDoubleVector(dimension); } else { // if the dimension is too big, we don't want to waste time on generating // randoms if (dimension < (2 << 15)) { double[] array = new double[dimension]; for (int i = 0; i < array.length; i++) { array[i] = (random.nextDouble() * 2) - 1d; } return new DenseDoubleVector(array); } return new DenseDoubleVector(dimension); } }
@Override public DoubleVector subtractFrom(double scalar) { DoubleVector v = new SequentialSparseDoubleVector(dimension, mapping.getNumMappings()); Iterator<DoubleVectorElement> it = iterate(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), scalar - e.getValue()); } return v; }
@Override public DoubleVector sliceByLength(int start, int length) { DoubleVector nv = new SequentialSparseDoubleVector(length, length); Iterator<DoubleVectorElement> iterateNonZero = iterateNonZero(); final int endIndex = start + length; while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); if (next.getIndex() >= start && next.getIndex() < endIndex) { nv.set(next.getIndex() - start, next.getValue()); } } return nv; }