@Override public double[] asDoubleVector() { double [] d = new double[values.length]; for (Entry e : values.entries()) { d[e.index] = e.value; } return d; }
/** * Compute the sum of values * @return the sum of all values */ public double sumValues() { double sum = 0; for (Entry e : entries()) sum += e.value; return sum; }
@Override public int computeHashCode(SparseDoubleArray array) { double dp = 0; for (Entry e : array.entries()) dp += r[e.index] * e.value; return dp >= 0 ? 1 : 0; } }
@Override public int computeHashCode(SparseDoubleArray array) { double dp = 0; for (Entry e : array.entries()) dp += r[e.index] * e.value; return dp >= 0 ? 1 : 0; } }
/** * @return the largest value in the array */ public double maxValue() { double max = -Double.MAX_VALUE; for (Entry e : entries()) { if (e.value > max) max = e.value; } return max; }
/** * @return the smallest value in the array */ public double minValue() { double min = Double.MAX_VALUE; for (Entry e : entries()) { if (e.value < min) min = e.value; } return min; }
/** * Compute the sum of values squared * @return the sum of all values */ public double sumValuesSquared() { double sum = 0; for (Entry e : entries()) sum += e.value * e.value; return sum; }
@Override public double[] asDoubleVector() { double [] d = new double[values.length]; for (Entry e : values.entries()) { d[e.index] = e.value; } return d; }
/** * @return the index of the smallest value in the array */ public int minIndex() { double min = Double.MAX_VALUE; int index = 0; for (Entry e : entries()) { if (e.value < min) { min = e.value; index = e.index; } } return index; }
/** * @return the index of the largest value in the array */ public int maxIndex() { double max = -Double.MAX_VALUE; int index = 0; for (Entry e : entries()) { if (e.value > max) { max = e.value; index = e.index; } } return index; }
@Override public int computeHashCode(SparseDoubleArray array) { double val = 0; for (Entry e : array.entries()) { val += e.value * r[e.index]; } val = (val + b) / w; return (int) Math.floor(val); } }
@Override public int computeHashCode(SparseDoubleArray array) { double val = 0; for (Entry e : array.entries()) { val += e.value * r[e.index]; } val = (val + b) / w; return (int) Math.floor(val); } }
@Override public void writeASCII(PrintWriter out) throws IOException { out.print(this.length); out.print(" "); for (Entry e : entries()) { out.print(e.index + ":" + e.value + " "); } }
/** * Subtract the values in the given vector from * this array and return this * @param vector the vector to add * @return this */ public SparseDoubleArray subtractInplace(SparseDoubleArray vector) { for (Entry e : vector.entries()) { increment(e.index, (double)(-e.value)); } return this; }
/** * Add the values in the given vector to * this vector and return this * @param vector the vector to add * @return this */ public SparseDoubleArray addInplace(SparseDoubleArray vector) { for (Entry e : vector.entries()) { increment(e.index, e.value); } return this; }
@Override public void writeBinary(DataOutput out) throws IOException { out.writeInt(this.length); out.writeInt(this.used()); for (Entry e : entries()) { out.writeInt(e.index); out.writeDouble(e.value); } }
@Override public SparseDoubleArray concatenate(List<SparseDoubleArray> ins) { final SparseDoubleArray result = copy(); for (int i=0; i<ins.size(); i++) { final int oldLength = result.length; result.setLength(oldLength + ins.get(i).length); for (Entry e : ins.get(i).entries()) { result.set(e.index + oldLength, e.value); } } return result; } }
/** * Multiply the values inplace by a scalar and return this * @param value scalar multiplier * @return this */ public SparseDoubleArray multiplyInplace(double value) { for (Entry e : entries()) { if (this.isUsed(e.index)) set(e.index, (double)(value * e.value)); } return this; }
@Override public SparseDoubleArray concatenate(SparseDoubleArray... ins) { final SparseDoubleArray result = copy(); for (int i=0; i<ins.length; i++) { final int oldLength = result.length; result.setLength(oldLength + ins[i].length); for (Entry e : ins[i].entries()) { result.set(e.index + oldLength, e.value); } } return result; }
/** * Compute the dot product with another vector * @param vector the other vector * @return the dot product */ public double dotProduct(SparseDoubleArray vector) { double sum = 0; int tused = this.used(); int vused = vector.used(); SparseDoubleArray smaller = (tused < vused ? this : vector); SparseDoubleArray larger = (tused < vused ? vector : this); for (Entry e : smaller.entries()) { sum += e.value * larger.get(e.index); } return sum; }