@Override public int computeHashCode(SparseIntArray array) { double dp = 0; for (Entry e : array.entries()) dp += r[e.index] * e.value; return dp >= 0 ? 1 : 0; } }
@Override public int computeHashCode(SparseIntArray array) { double dp = 0; for (Entry e : array.entries()) dp += r[e.index] * e.value; return dp >= 0 ? 1 : 0; } }
@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 int sumValues() { int sum = 0; for (Entry e : entries()) sum += 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 largest value in the array */ public int maxValue() { int max = -Integer.MAX_VALUE; for (Entry e : entries()) { if (e.value > max) max = e.value; } return max; }
/** * @return the smallest value in the array */ public int minValue() { int min = Integer.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 int sumValuesSquared() { int sum = 0; for (Entry e : entries()) sum += e.value * e.value; return sum; }
/** * @return the index of the smallest value in the array */ public int minIndex() { int min = Integer.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() { int max = -Integer.MAX_VALUE; int index = 0; for (Entry e : entries()) { if (e.value > max) { max = e.value; index = e.index; } } return index; }
/** * Convert this sparse array to a dense array. * <p> * Be aware that calling this method results in an array of {@link #length} * being created. This could be very bad for performance. * * @return the dense array representation of this feature. */ public int[] toArray() { final int[] array = new int[length]; for (final Entry e : entries()) { array[e.index] = e.value; } return array; }
@Override public int computeHashCode(SparseIntArray 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(SparseIntArray 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 + " "); } }
/** * Add the values in the given vector to * this vector and return this * @param vector the vector to add * @return this */ public SparseIntArray addInplace(SparseIntArray vector) { for (Entry e : vector.entries()) { increment(e.index, e.value); } return this; }
/** * Subtract the values in the given vector from * this array and return this * @param vector the vector to add * @return this */ public SparseIntArray subtractInplace(SparseIntArray vector) { for (Entry e : vector.entries()) { increment(e.index, (int)(-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.writeInt(e.value); } }
@Override public SparseIntArray concatenate(List<SparseIntArray> ins) { final SparseIntArray 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 SparseIntArray multiplyInplace(double value) { for (Entry e : entries()) { if (this.isUsed(e.index)) set(e.index, (int)(value * e.value)); } return this; }
@Override public SparseIntArray concatenate(SparseIntArray... ins) { final SparseIntArray 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; }