@Override public Vector<Double> make(int n, double defaultValue) { return new SparseRealArray(n, defaultValue, 1e-8); }
/** * Subtract a value from a given index * * @param idx The index * @param i The value */ @Override public void sub(int idx, double i) { add(idx, -i); }
@Override public void multiply(double n) { for (Map.Entry<Integer, Double> e : entrySet()) { e.setValue(e.getValue() * n); } defaultValue *= n; }
/** * Create a sparse array from a dense array * * @param arr The dense array * @return The sparse array, with zeroes removed */ public static SparseRealArray fromArray(double[] arr) { SparseRealArray sa = new SparseRealArray(arr.length); for (int i = 0; i < arr.length; i++) { if (arr[i] != 0) { sa.put(i, arr[i]); } } return sa; }
public BetaLMImpl(SparseIntArray[][] x, int W) { this.x = x; this.W = W; this.mu = new SparseRealArray(W); this.mu_f = new SparseRealArray(W); this.sumMu2 = initMu(x, this.W); if (method == Method.DF_DICE || method == Method.DF_JACCARD) { df = null; this.dfSp = new SparseRealArray(W); for (int j = 0; j < x.length; j++) { for (int w : x[j][0].keySet()) { dfSp.inc(w); } } for (int w = 0; w < this.W; w++) { dfSp.divide(w, x.length); } } else { df = null; dfSp = null; } }
@Override public Vector<Double> subvector(int offset, int length) { final SparseRealArray sv = new SparseRealArray(n); final ObjectIterator<Entry> iter = int2DoubleEntrySet().fastIterator(); while(iter.hasNext()) { final Int2DoubleMap.Entry e = iter.next(); if(e.getIntKey() >= offset && e.getIntKey() < offset + length) { sv.put(e.getIntKey(), e.getDoubleValue()); } } return sv; } }
if (defaultValue == 0) { double innerProduct = 0; for (Map.Entry<Integer, Double> e : entrySet()) { innerProduct += e.getValue().doubleValue() * y2[e.getKey()]; double innerProduct = 0; for (int i = 0; i < n; i++) { innerProduct += y2[i] * get(i); if (defaultValue == 0) { double innerProduct = 0; for (Map.Entry<Integer, Double> e : entrySet()) { innerProduct += e.getValue().doubleValue() * y2[e.getKey()]; double innerProduct = 0; for (int i = 0; i < n; i++) { innerProduct += y2[i] * get(i); if (defaultValue == 0.0 || y.defaultValue().doubleValue() == 0.0) { double innerProduct = 0.0; if (this.size() <= y.size()) { for (Map.Entry<Integer, Double> e : entrySet()) { innerProduct += e.getValue().doubleValue() * y.doubleValue(e.getKey()); } else { for (Map.Entry<Integer, M> e : y.entrySet()) { innerProduct += e.getValue().doubleValue() * this.doubleValue(e.getKey()); double innerProduct = 0;
/** * Create from a string of the form "idx=val,...,idx=val" * * @param string The string * @param n The length of the array * @return The array object * @throws SparseArrayFormatException If the string is not formatted as a * sparse array */ public static SparseRealArray fromString(String string, int n) throws VectorFormatException { return fromString(string, n, 0.0, ","); }
@Override public Double value(int idx) { return get(idx); }
@Override public Vector<Double> make(double[] data) { return SparseRealArray.fromArray(data); }
/** * Multiple a value at a given index * * @param idx The index * @param i The value */ @Override public void multiply(int idx, double i) { if (this.containsKey(idx)) { final double val = super.get(idx); if (Math.abs(val * i - defaultValue) <= epsilon) { super.remove(idx); } else { super.put(idx, val * i); } } else { if (defaultValue != 0.0 && i != 0.0) { super.put(idx, defaultValue * i); } } }
/** * Convert to a sparse real array */ public SparseRealArray toRealArray() { final SparseRealArray sparseRealArray = new SparseRealArray(n, defaultValue, 0.0); for (Map.Entry<Integer, Integer> entry : entrySet()) { sparseRealArray.put(entry.getKey(), entry.getValue()); } return sparseRealArray; }
/** * Create from a string of the form "idx=val,...,idx=val" * * @param string The string * @param n The length of the array * @param defaultValue The default value * @return The array object * @throws SparseArrayFormatException If the string is not formatted as a * sparse array */ public static SparseRealArray fromString(String string, int n, double defaultValue) throws VectorFormatException { return fromString(string, n, defaultValue, ","); }
@Override public SparseRealArray clone() { return new SparseRealArray(n, defaultValue, epsilon, this); }
/** * Create from a string of the form "idx=val,...,idx=val" * * @param string The string * @param n The length of the array * @param defaultValue The default value * @param sep The seperator * @return The array object * @throws SparseArrayFormatException If the string is not formatted as a * sparse array */ public static SparseRealArray fromString(String string, int n, double defaultValue, String sep) throws VectorFormatException { String[] entries = string.split(sep); final SparseRealArray SparseRealArray = new SparseRealArray(n); for (String entry : entries) { if (entry.matches("\\s*")) { continue; } String[] values = entry.split("="); if (values.length != 2) { throw new VectorFormatException("Bad sparse array value " + entry); } try { SparseRealArray.put(new Integer(values[0]), new Double(values[1])); } catch (NumberFormatException x) { throw new VectorFormatException(x); } } return SparseRealArray; }
/** * Increment an index */ public void inc(int idx) { add(idx, 1); }
/** * Convert to a dense array */ public double[] toArray() { double[] arr = new double[n]; for (Map.Entry<Integer, Double> entry : entrySet()) { arr[entry.getKey()] = entry.getValue(); } return arr; }
@Override public Vector<Double> fromString(String s, int n) throws VectorFormatException { return SparseRealArray.fromString(s, n); }
public static Vector<Double> solveOnDisk(File matrixFile, int N, Vector<Double> b) { assert (N == b.length()); final byte[] buf = new byte[N*8]; Vector<Double> y = new SparseRealArray(N); RandomAccessFile raf = null; try {