/** * Construct from native array * @param values the array of values */ public SparseDoubleFV(double[] values) { this.values = new SparseBinSearchDoubleArray(values); }
/** * Construct from an existing array of values * @param values the values */ public SparseBinSearchDoubleArray(double[] values) { this(values.length); for (int i=0; i<values.length; i++) { if (values[i] != 0) set(i, values[i]); } }
@Override public double increment(int key, double value) { if (key < 0 || key >= length) throw new IndexOutOfBoundsException(Integer.toString(key)); int spot = Arrays.binarySearch(keys, 0, used, key); if (spot >= 0) return values[spot] += value; return update(-1 - spot, key, value); }
@Override public Iterable<DualEntry> unionEntries(final SparseDoubleArray otherArray) { if (otherArray instanceof SparseBinSearchDoubleArray) { return unionEntries((SparseBinSearchDoubleArray) otherArray); } return super.unionEntries(otherArray); }
@Override public double set(int key, double value) { if (key < 0 || key >= length) throw new IndexOutOfBoundsException(Integer.toString(key)); int spot = Arrays.binarySearch(keys, 0, used, key); if (spot >= 0) return values[spot] = value; else return update(-1 - spot, key, value); }
/** * Construct empty FV with given number of bins * @param nbins the number of bins in each dimension */ public SparseDoubleFV(int nbins) { values = new SparseBinSearchDoubleArray(nbins); }
/** * Construct from native array * @param values the array of values */ public SparseDoubleFV(double[] values) { this.values = new SparseBinSearchDoubleArray(values); }
/** * Construct empty FV with given number of bins * @param nbins the number of bins in each dimension */ public SparseDoubleFV(int nbins) { values = new SparseBinSearchDoubleArray(nbins); }
@Override public SparseDoubleArray copy() { SparseBinSearchDoubleArray copy = new SparseBinSearchDoubleArray(length); copy.used = used; copy.keys = Arrays.copyOf(keys, keys.length); copy.values = Arrays.copyOf(values, values.length); return copy; }
@Override public void readBinary(DataInput in) throws IOException { values = new SparseBinSearchDoubleArray(0); values.readBinary(in); }
@Override public void readASCII(Scanner in) throws IOException { values = new SparseBinSearchDoubleArray(0); values.readASCII(in); }
@Override public void readASCII(Scanner in) throws IOException { values = new SparseBinSearchDoubleArray(0); values.readASCII(in); }
@Override public void readBinary(DataInput in) throws IOException { values = new SparseBinSearchDoubleArray(0); values.readBinary(in); }
/** * Convert a {@link ch.akuhn.matrix.SparseVector} to a * {@link SparseDoubleArray}. * * @param row * the vector to convert * @return the converted vector */ public static SparseDoubleArray sparseVectorToSparseArray( ch.akuhn.matrix.SparseVector row) { final SparseDoubleArray sda = new SparseBinSearchDoubleArray( row.size(), row.used(), row.keys(), row.values()); return sda; }