@Override public Vector<Double> make(int n, double defaultValue) { return new SparseRealArray(n, defaultValue, 1e-8); }
@Override public SparseRealArray clone() { return new SparseRealArray(n, defaultValue, epsilon, this); }
/** * 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; }
/** * 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; }
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; } }
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 {
/** * 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; }
public static Vector<Double> solveTOnDisk(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 {
/** * Convert a dense double array to a sparse matrix * * @param arrs The arrays, each element must be non-null and have same * length * @return The sparse matrix */ public static SparseMatrix<Double> fromArray(double[][] arrs) { if (arrs.length == 0) { return new SparseMatrix<Double>(0, 0, AS_SPARSE_REALS); } final SparseMatrix<Double> mat = new SparseMatrix<Double>(arrs.length, arrs[0].length, AS_SPARSE_REALS); for (int i = 0; i < arrs.length; i++) { assert (arrs[i].length == arrs[0].length); for (int j = 0; j < arrs[i].length; j++) { if (arrs[i][j] != 0.0) { if (mat.arr[i] == null) { mat.arr[i] = new SparseRealArray(arrs[i].length); } mat.arr[i].put(j, arrs[i][j]); } } } return mat; }
public static Vector<Double> solveT(SparseMatrix<Double> a, Vector<Double> b) { assert (a.cols() == b.length()); final int N = b.length(); Vector<Double> y = new SparseRealArray(N); for (int i = N - 1; i >= 0; i--) { double sum = b.doubleValue(i); for (int j = i + 1; j < N; j++) { sum -= a.doubleValue(j, i) * y.doubleValue(j); } y.put(i, sum / a.doubleValue(i, i)); } return y; }
public static Vector<Double> solve(SparseMatrix<Double> a, Vector<Double> b) { assert (a.cols() == b.length()); final int N = b.length(); Vector<Double> y = new SparseRealArray(N); for (int i = 0; i < N; i++) { double sum = b.doubleValue(i); for (int j = 0; j < i; j++) { sum -= a.doubleValue(i, j) * y.doubleValue(j); } y.put(i, sum / a.doubleValue(i, i)); } return y; }
@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; } }