@Override public int getLength() { return mapping.getNumMappings(); }
@Override public double sum() { double sum = 0.0d; for (int i = 0; i < mapping.getNumMappings(); i++) { sum += mapping.getValues()[i]; } return sum; }
@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 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 double min() { double res = Double.MAX_VALUE; for (int i = 0; i < mapping.getNumMappings(); i++) { if (res > mapping.getValues()[i]) { res = mapping.getValues()[i]; } } return res; }
double[] updateValues = updates.getValues(); int newNumMappings = numMappings + updates.getNumMappings(); int newCapacity = Math .max((int) (1.2 * newNumMappings), newNumMappings + 1); for (; i < numMappings && j < updates.getNumMappings(); ++k) { if (indices[i] < updateIndices[j]) { newIndices[k] = indices[i]; if (j < updates.getNumMappings()) { int delta = updates.getNumMappings() - j; System.arraycopy(updateIndices, j, newIndices, k, delta); System.arraycopy(updateValues, j, newValues, k, delta);
@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 int minIndex() { int index = 0; double res = Double.MAX_VALUE; for (int i = 0; i < mapping.getNumMappings(); i++) { if (res > mapping.getValues()[i]) { res = mapping.getValues()[i]; index = mapping.getIndices()[i]; } } return index; }
@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 int maxIndex() { int index = 0; double res = -Double.MAX_VALUE; for (int i = 0; i < mapping.getNumMappings(); i++) { if (res < mapping.getValues()[i]) { res = mapping.getValues()[i]; index = mapping.getIndices()[i]; } } return index; }
@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 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 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 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 protected final DoubleVectorElement computeNext() { if (currentIndex < mapping.getNumMappings()) { element.setIndex(mapping.getIndices()[currentIndex]); element.setValue(mapping.getValues()[currentIndex]); currentIndex++; return element; } else { return endOfData(); } }