/** * @return the smallest value in the array */ public short minValue() { short min = Short.MAX_VALUE; for (Entry e : entries()) { if (e.value < min) min = e.value; } return min; }
/** * Compute the dot product with another vector * @param vector the other vector * @return the dot product */ public double dotProduct(SparseShortArray vector) { double sum = 0; int tused = this.used(); int vused = vector.used(); SparseShortArray smaller = (tused < vused ? this : vector); SparseShortArray larger = (tused < vused ? vector : this); for (Entry e : smaller.entries()) { sum += e.value * larger.get(e.index); } return sum; }
@Override public SparseShortArray concatenate(SparseShortArray... ins) { final SparseShortArray 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 SparseShortArray add(SparseShortArray vector) { return copy().addInplace(vector); }
@Override public double compare(final SparseShortArray h1, final SparseShortArray h2) { if (h1.length != h2.length) throw new IllegalArgumentException("Vectors have differing lengths"); double d = 0; for (SparseShortArray.DualEntry e : h1.unionEntries(h2)) { d += (e.value * e.otherValue); } return d; } }
@Override public double getAsDouble(int i) { return values.get(i); }
@Override public void setFromDouble(int i, double v) { values.set(i, (short) v); }
/** * @return the density of the array (number of used elements divided * by array length) */ public float density() { return (float)used() / (float)length; }
@Override public SparseShortFV concatenate(List<SparseShortFV> ins) { SparseShortArray [] insValues = new SparseShortArray[ins.size()]; for (int i=0; i<ins.size(); i++) insValues[i] = ins.get(i).values; SparseShortArray vals = values.concatenate(insValues); return new SparseShortFV(vals); }
@Override public double compare(final SparseShortArray h1, final SparseShortArray h2) { if (h1.length != h2.length) throw new IllegalArgumentException("Vectors have differing lengths"); int d = 0; for (SparseShortArray.DualEntry e : h1.unionEntries(h2)) if (e.value != e.otherValue) d++; return d; } },
@Override public int computeHashCode(SparseShortArray array) { return (array.get(dimension) - shift) >= 0 ? 1 : 0; } }
@Override public void setFromDouble(int i, double v) { values.set(i, (short) v); }
@Override public SparseShortFV concatenate(SparseShortFV... ins) { SparseShortArray [] insValues = new SparseShortArray[ins.length]; for (int i=0; i<ins.length; i++) insValues[i] = ins[i].values; SparseShortArray vals = values.concatenate(insValues); return new SparseShortFV(vals); }
/** * Compute the sum of values * @return the sum of all values */ public short sumValues() { short sum = 0; for (Entry e : entries()) sum += e.value; return sum; }