@Override public int put(int idx, int value) { return v.put(idx, value); }
@Override public double put(int idx, double value) { return v.put(idx, value); }
@Override public Double put(Integer idx, Double n) { return v.put(idx, n); }
@Override public void set(int i, int j, double v) { if (arr[i] == null) { arr[i] = using.make(n, defaultValue); } arr[i].put(j, using.valueOf(v)); }
@Override public void set(int i, int j, int v) { if (arr[i] == null) { arr[i] = using.make(n, defaultValue); } arr[i].put(j, using.valueOf(v)); }
@Override public void set(int i, int j, N v) { if (arr[i] == null) { arr[i] = using.make(n, defaultValue); } arr[i].put(j, v); }
private <O extends Number> Vector<O> invMap(Vector<O> x, Factory<O> using) { final Vector<O> x2 = using.make(map.length, 0.0); for (Map.Entry<Integer, O> e : x.entrySet()) { final int i = e.getKey(); if (invMap[i] >= 0) { x2.put(invMap[i], e.getValue()); } } return x2; }
@Override public Vector<Double> fromString(String s, int n) throws VectorFormatException { String[] entries = s.split(","); final Vector<Double> v = make(n, 0.0); 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 { v.put(new Integer(values[0]), new Double(values[1])); } catch (NumberFormatException x) { throw new VectorFormatException(x); } } return v; }
@Override public <M extends Number, O extends Number> Vector<O> mult(Vector<M> x, Factory<O> using) { final Vector<O> x2 = using.make(A.rows(), 0.0); for (Map.Entry<Integer, M> e : x.entrySet()) { x2.put(map[e.getKey()], e.getValue().doubleValue()); } return invMap(A.mult(x2, using), using); }
y.put(i, sum / row_i.getDouble());
/** * Generate the transpose of this matrix * * @param using {@link AS_INT_ARRAY} or {@link AS_REAL_ARRAY} as appropriate * @return The transposed matrix */ @Override public SparseMatrix<N> transpose() { final SparseMatrix<N> t = new SparseMatrix<N>(n, m, defaultValue, using); for (int i = 0; i < m; i++) { if (arr[i] != null) { for (Map.Entry<Integer, N> e : arr[i].entrySet()) { final int j = e.getKey(); if (t.arr[j] == null) { t.arr[j] = using.make(m, defaultValue); } t.arr[j].put(i, e.getValue()); } } } return t; }
@Override public <M extends Number> Vector<N> multTransposed(Vector<M> x) { final Vector<N> x2 = A.factory().make(A.rows(), 0.0); for (Map.Entry<Integer, M> e : x.entrySet()) { x2.put(map[e.getKey()], e.getValue().doubleValue()); } return invMap(A.multTransposed(x2), A.factory()); }
/** * 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; }
/** * 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<Integer> fromArray(int[][] arrs) { if (arrs.length == 0) { return new SparseMatrix<Integer>(0, 0, AS_INTS); } final SparseMatrix<Integer> mat = new SparseMatrix<Integer>(arrs.length, arrs[0].length, AS_INTS); 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 SparseIntArray(arrs[i].length); } mat.arr[i].put(j, arrs[i][j]); } } } return mat; }
@Override public <M extends Number, O extends Number> Vector<O> mult(Vector<M> x, Vectors.Factory<O> using) { assert (x.length() == alpha.length); final Vector<O> product = using.make(alpha.length, 0.0); for (int i = 0; i < alpha.length; i++) { double value = 0.0; if (i > 0) { value += x.doubleValue(i - 1) * beta[i - 1]; } value += x.doubleValue(i) * alpha[i]; if (i < beta.length) { value += x.doubleValue(i + 1) * beta[i]; } product.put(i, value); } return product; }
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; }