@Override public Set<Integer> mapDocument(DoubleVector v) { Set<Integer> set = new HashSet<>(v.getLength()); Iterator<DoubleVectorElement> iterateNonZero = v.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); set.add(next.getIndex()); } return set; }
static double[] computeScores(int classes, DoubleVector features, DoubleMatrix weights) { double[] scores = new double[classes]; Iterator<DoubleVectorElement> iterateNonZero = features.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); for (int i = 0; i < scores.length; i++) { scores[i] += weights.get(i, next.getIndex()); } } return scores; }
@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 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 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 apply(DoubleVectorFunction func) { SparseBitVector newV = new SparseBitVector(getDimension()); 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 public DoubleVector abs() { DoubleVector v = new SparseDoubleVector(this); Iterator<DoubleVectorElement> it = v.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), FastMath.abs(e.getValue())); } return v; }
@Override public DoubleVector sqrt() { DoubleVector v = new SparseDoubleVector(this.dimension, this.vector.size()); Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), FastMath.sqrt(e.getValue())); } return v; }
@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 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 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 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; }
static boolean strictLower(DoubleVector upper, DoubleVector current) { Iterator<DoubleVectorElement> iterateNonZero = upper.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); if (current.get(next.getIndex()) > next.getValue()) return false; } return true; }
@Override public DoubleVector multiply(double scalar) { DoubleVector v = new SparseBitVector(getDimension()); Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() * scalar); } return v; }
@Override public DoubleVector add(double scalar) { DoubleVector v = new SparseBitVector(getDimension()); 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 SparseDoubleVector(this.dimension, this.vector.size()); 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 subtract(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(), e.getValue() - scalar); } 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; }
@Override public DoubleVector add(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(), 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; }