@Override public DoubleVector deepCopy() { return new SequentialSparseDoubleVector(this); }
@Override public DoubleVector divide(double scalar) { DoubleVector result = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> iter = result.iterateNonZero(); while (iter.hasNext()) { DoubleVectorElement e = iter.next(); int index = e.getIndex(); result.set(index, e.getValue() / scalar); } return result; }
@Override public DoubleVector multiply(double scalar) { DoubleVector result = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> iter = result.iterateNonZero(); while (iter.hasNext()) { DoubleVectorElement e = iter.next(); int index = e.getIndex(); result.set(index, scalar * e.getValue()); } return result; }
@Override public DoubleVector divideFrom(double scalar) { DoubleVector result = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> iter = result.iterateNonZero(); while (iter.hasNext()) { DoubleVectorElement e = iter.next(); int index = e.getIndex(); result.set(index, scalar / e.getValue()); } return result; }
@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 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 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 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 log() { SequentialSparseDoubleVector v = new SequentialSparseDoubleVector(this); for (int i = 0; i < mapping.getNumMappings(); i++) { v.mapping.getValues()[i] = FastMath.log(mapping.getValues()[i]); } return v; }
@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; }
@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 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 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 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; }
@Override public DoubleVector sqrt() { SequentialSparseDoubleVector v = new SequentialSparseDoubleVector(this); for (int i = 0; i < mapping.getNumMappings(); i++) { v.mapping.getValues()[i] = FastMath.sqrt(mapping.getValues()[i]); } return v; }
@Override public DoubleVector exp() { SequentialSparseDoubleVector v = new SequentialSparseDoubleVector(this); for (int i = 0; i < mapping.getNumMappings(); i++) { v.mapping.getValues()[i] = FastMath.exp(mapping.getValues()[i]); } return v; }
@Override public DoubleVector abs() { SequentialSparseDoubleVector v = new SequentialSparseDoubleVector(this); for (int i = 0; i < mapping.getNumMappings(); i++) { v.mapping.getValues()[i] = FastMath.abs(mapping.getValues()[i]); } return v; }
@Override public DoubleVector pow(double x) { SequentialSparseDoubleVector v = new SequentialSparseDoubleVector(this); for (int i = 0; i < mapping.getNumMappings(); i++) { double value = mapping.getValues()[i]; if (x == 2d) { value = value * value; } else { value = FastMath.pow(value, x); } v.mapping.getValues()[i] = value; } return v; }
@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 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; }