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