@Override public double[] asDoubleVector() { double [] d = new double[values.length]; for (Entry e : values.entries()) { d[e.index] = e.value; } return d; }
/** * 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; }
@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; }
/** * Add the values in the given vector to a copy of * this array and return the result * @param vector the vector to add * @return the resultant vector */ public SparseDoubleArray add(SparseDoubleArray vector) { return copy().addInplace(vector); }
@Override public double compare(final SparseDoubleArray h1, final SparseDoubleArray h2) { if (h1.length != h2.length) throw new IllegalArgumentException("Vectors have differing lengths"); int d = 0; for (SparseDoubleArray.DualEntry e : h1.unionEntries(h2)) if (e.value != e.otherValue) d++; return d; } },
@Override public int computeHashCode(SparseDoubleArray array) { return (array.get(dimension) - shift) >= 0 ? 1 : 0; } }
@Override public void setFromDouble(int i, double v) { values.set(i, (double) v); }
fv.values.increment(assignments[i], ((byte[]) distances)[i]); fv.values.increment(assignments[i], ((short[]) distances)[i]); fv.values.increment(assignments[i], ((int[]) distances)[i]); fv.values.increment(assignments[i], ((long[]) distances)[i]); fv.values.increment(assignments[i], ((float[]) distances)[i]); fv.values.increment(assignments[i], ((double[]) distances)[i]);
/** * @return the density of the array (number of used elements divided * by array length) */ public float density() { return (float)used() / (float)length; }
/** * Concatenate multiple arrays into a single new array. * * @param arrays the arrays to concatenate. * @return the new concatenated array */ public static SparseDoubleArray concatenateArrays(SparseDoubleArray... arrays) { SparseDoubleArray concat = arrays[0].copy(); for (int i=1; i<arrays.length; i++) { if (arrays[i] != null) { int oldLength = concat.length; concat.setLength(oldLength + arrays[i].length); for (Entry e : arrays[i].entries()) concat.set(e.index + oldLength, e.value); } } return concat; }
@Override public double compare(final SparseDoubleArray h1, final SparseDoubleArray h2) { if (h1.length != h2.length) throw new IllegalArgumentException("Vectors have differing lengths"); double d = 0; for (SparseDoubleArray.DualEntry e : h1.unionEntries(h2)) { d += (e.value * e.otherValue); } return d; } }
@Override public int computeHashCode(SparseDoubleArray array) { return (array.get(dimension) - shift) >= 0 ? 1 : 0; } }
@Override public void setFromDouble(int i, double v) { values.set(i, (double) v); }
fv.values.increment(assignments[i], ((byte[]) distances)[i]); fv.values.increment(assignments[i], ((short[]) distances)[i]); fv.values.increment(assignments[i], ((int[]) distances)[i]); fv.values.increment(assignments[i], ((long[]) distances)[i]); fv.values.increment(assignments[i], ((float[]) distances)[i]); fv.values.increment(assignments[i], ((double[]) distances)[i]);
@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; } }