@Override public String toString() { if (getLength() < 50) { return vector.toString(); } else { return getDimension() + "x1"; } }
@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 double sum() { double sum = 0.0d; for (int i = 0; i < mapping.getNumMappings(); i++) { sum += mapping.getValues()[i]; } return sum; }
@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 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 getRowVector(int row) { SparseDoubleVector v = matrix.get(row); if (v == null) { v = new SparseDoubleVector(getColumnCount()); matrix.put(row, v); } return v; }
public void increment(int index, double increment) { int offset = find(index); if (offset >= 0) { double newValue = values[offset] + increment; insertOrUpdateValueIfPresent(offset, newValue); } else { insertValueIfNotDefault(index, offset, increment); } }
@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 int[] columnIndices() { return fromUpTo(0, getColumnCount(), 1); }
@Override protected DoubleVectorElement computeNext() { if (index < getDimension()) { element.setIndex(index); element.setValue(get(index)); index++; return element; } else { return endOfData(); } }
/** * Constructs a new {@link SparseBitVector}. * * @param arr the given vector to copy. */ public SparseBitVector(double[] arr) { this(arr.length); for (int i = 0; i < arr.length; i++) { set(i, arr[i]); } }
/** * 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 DoubleVector deepCopy() { return new SparseDoubleVector(this); }
@Override public DoubleVector deepCopy() { return new SparseBitVector(this); }
/** * Constructs a new {@link SequentialSparseDoubleVector}. * * @param expectedLength the expected length of the vector * @param dimension the expected dimensionality of the vector. */ public SequentialSparseDoubleVector(int dimension, int expectedLength) { this.dimension = dimension; this.mapping = new OrderedIntDoubleMapping(expectedLength); }
/** * Constructs a new {@link SparseDoubleVector}. * * @param dimension the expected dimensionality of the vector. * @param expectedInserts the expected number of elements to be inserted. */ SparseDoubleVector(int dimension, int expectedInserts) { this.vector = new FastIntDoubleHashMap(expectedInserts); this.dimension = dimension; }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + vector.hashCode(); return result; }
/** * Constructs a new {@link SequentialSparseDoubleVector}. * * @param arr the given vector to copy. */ public SequentialSparseDoubleVector(double[] arr) { this(arr.length); for (int i = 0; i < arr.length; i++) { mapping.set(i, arr[i]); } }
@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; }
/** * Constructs a new {@link SequentialSparseDoubleVector}. * * @param dimension the expected dimensionality of the vector. */ public SequentialSparseDoubleVector(int dimension) { this.dimension = dimension; this.mapping = new OrderedIntDoubleMapping(); }