/** * Compute the sum of values squared * @return the sum of all values */ public float sumValuesSquared() { float sum = 0; for (Entry e : entries()) sum += e.value * e.value; return sum; }
/** * Compute the dot product with another vector * @param vector the other vector * @return the dot product */ public double dotProduct(SparseFloatArray vector) { double sum = 0; int tused = this.used(); int vused = vector.used(); SparseFloatArray smaller = (tused < vused ? this : vector); SparseFloatArray larger = (tused < vused ? vector : this); for (Entry e : smaller.entries()) { sum += e.value * larger.get(e.index); } return sum; }
@Override public SparseFloatArray concatenate(SparseFloatArray... ins) { final SparseFloatArray 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; }
private void load(SparseFloatArray x, DataInputStream fs) throws IOException { int sz = 0; int msz = 1024; x.setLength(msz); final int npairs = fs.readInt(); if (npairs < 0) throw new AssertionError("bad format"); for (int i = 0; i < npairs; i++) { final int idx = fs.readInt(); final float val = fs.readFloat(); if (idx >= sz) sz = idx + 1; if (idx >= msz) { while (idx >= msz) msz += msz; x.setLength(msz); } x.set(idx, val); } x.compact(); }
for (final org.openimaj.util.array.SparseIntArray.Entry i : fv.values.entries()) { sum += (i.value * i.value); fv2.values.set(i.index, i.value); n[i.index] += i.value; for (final org.openimaj.util.array.SparseFloatArray.Entry i : fv2.values.entries()) { fv2.values.set(i.index, i.value / sum); for (final org.openimaj.util.array.SparseFloatArray.Entry i : fv.values.entries()) { fv.values.set(i.index, i.value * n[i.index]);
private static SparseFloatFV loadVector(File f, List<String> vocab) throws IOException { final String str = FileUtils.readall(f); final String[] terms = str.split(",\\s*"); final SparseFloatFV fv = new SparseFloatFV(vocab.size()); for (String term : terms) { term = term.trim(); if (term.length() < 1) continue; int idx = vocab.indexOf(term); if (idx == -1) { idx = vocab.size(); vocab.add(term); fv.values.setLength(idx + 1); fv.values.set(idx, 1); } else { fv.values.increment(idx, 1); } } return fv; } }
@Override public double compare(final SparseFloatArray h1, final SparseFloatArray h2) { if (h1.length != h2.length) throw new IllegalArgumentException("Vectors have differing lengths"); int d = 0; for (SparseFloatArray.DualEntry e : h1.unionEntries(h2)) if (e.value != e.otherValue) d++; return d; } },
@Override public int computeHashCode(SparseFloatArray array) { return (array.get(dimension) - shift) >= 0 ? 1 : 0; } }
@Override public void setFromDouble(int i, double v) { values.set(i, (float) v); }
final double d = x.dotProduct(x); if (d > 0 && d != 1.0) x.multiplyInplace(1.0 / Math.sqrt(d)); else ncount += 1; if (p_maxdim != null && x.size() > p_maxdim[0]) p_maxdim[0] = x.size();
/** * Copy this vector and multiply its values by a * scalar * @param value scalar multiplier * @return the multiplied vector */ public SparseFloatArray multiply(double value) { return copy().multiplyInplace(value); }
@Override public double compare(final SparseFloatArray h1, final SparseFloatArray h2) { float[] h1v = h1.values(); float[] h2v = h2.values(); TFloatHashSet union = new TFloatHashSet(h1v); union.addAll(h2v); if (h1v.length != h1.length || h2v.length != h2.length) union.add((float)0); TFloatHashSet intersection = new TFloatHashSet(h1v); intersection.retainAll(h2v); if (h1v.length != h1.length && h2v.length != h2.length) union.add((float)0); return 1.0 - (((double)intersection.size()) / (double)union.size()); } },
fv.values.setLength(vocab.size());
/** * @return the density of the array (number of used elements divided * by array length) */ public float density() { return (float)used() / (float)length; }
private void load(SparseFloatArray x, DataInputStream fs) throws IOException { int sz = 0; int msz = 1024; x.setLength(msz); final int npairs = fs.readInt(); if (npairs < 0) throw new AssertionError("bad format"); for (int i = 0; i < npairs; i++) { final int idx = fs.readInt(); final float val = fs.readFloat(); if (idx >= sz) sz = idx + 1; if (idx >= msz) { while (idx >= msz) msz += msz; x.setLength(msz); } x.set(idx, val); } x.compact(); }