public static <N extends Number> SparseMatrix<N> fromFile(File file, Vectors.Factory<N> using) throws IOException, VectorFormatException { final BufferedReader in = new BufferedReader(new FileReader(file)); String s; final int m = Integer.parseInt(in.readLine()); final int n = Integer.parseInt(in.readLine()); @SuppressWarnings("unchecked") final Vector<N>[] vectors = (Vector<N>[]) new Vector<?>[m]; int idx = 0; while ((s = in.readLine()) != null) { if (idx >= m) { throw new VectorFormatException("Too many lines"); } vectors[idx++] = using.fromString(s, n); } return new SparseMatrix<N>(n, vectors, using); }
/** * 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; }
/** * 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 @SuppressWarnings("unchecked") public <M extends Number, O extends Number> Matrix<O> outerProduct(Vector<M> y, Vectors.Factory<O> using) { if (using == Vectors.AS_INTS) { int[][] data2 = new int[n][y.length()]; for (Map.Entry<Integer, Integer> e : entrySet()) { for (int j = 0; j < y.length(); j++) { data2[e.getKey()][j] = e.getValue().intValue() * y.intValue(j); } } return (Matrix<O>) new IntArrayMatrix(data2); } else if (using == Vectors.AS_REALS) { double[][] data2 = new double[n][y.length()]; for (Map.Entry<Integer, Integer> e : entrySet()) { for (int j = 0; j < y.length(); j++) { data2[e.getKey()][j] = y.doubleValue(j) * e.getValue().intValue(); } } return (Matrix<O>) new DoubleArrayMatrix(data2); } else { final SparseMatrix<O> matrix = new SparseMatrix<O>(n, y.length(), using); for (Map.Entry<Integer, Integer> e : entrySet()) { for (Map.Entry<Integer, M> e2 : y.entrySet()) { matrix.set(e.getKey(), e2.getKey(), e2.getValue().doubleValue() * e.getKey().doubleValue()); } } return matrix; } }
@Override @SuppressWarnings("unchecked") public <M extends Number, O extends Number> Matrix<O> outerProduct(Vector<M> y, Vectors.Factory<O> using) { if (using == Vectors.AS_INTS) { int[][] data2 = new int[n][y.length()]; for (Map.Entry<Integer, Double> e : entrySet()) { for (int j = 0; j < y.length(); j++) { data2[e.getKey()][j] = (int) (e.getValue().doubleValue() * y.doubleValue(j)); } } return (Matrix<O>) new IntArrayMatrix(data2); } else if (using == Vectors.AS_REALS) { double[][] data2 = new double[n][y.length()]; for (Map.Entry<Integer, Double> e : entrySet()) { for (int j = 0; j < y.length(); j++) { data2[e.getKey()][j] = y.doubleValue(j) * e.getValue().doubleValue(); } } return (Matrix<O>) new DoubleArrayMatrix(data2); } else { final SparseMatrix<O> matrix = new SparseMatrix<O>(n, y.length(), using); for (Map.Entry<Integer, Double> e : entrySet()) { for (Map.Entry<Integer, M> e2 : y.entrySet()) { matrix.set(e.getKey(), e2.getKey(), e2.getValue().doubleValue() * e.getKey().doubleValue()); } } return matrix; } }
public static SparseMatrix<Double> decomp(SparseMatrix<Double> a, boolean complete) { int m = a.rows(); SparseMatrix<Double> l = new SparseMatrix<Double>(m, m, Vectors.AS_SPARSE_REALS); //automatically initialzed to 0's for (int i = 0; i < m; i++) { if (complete) { for (int k = 0; k < (i + 1); k++) { chol(l, k, i, a); } } else { final Vector<Double> l_i = l.row(i); for (int k : l_i.keySet()) { if (k >= (i + 1)) { break; } chol(l, k, i, a); } } } return l; }
@Override @SuppressWarnings("unchecked") public <M extends Number, O extends Number> Matrix<O> outerProduct(Vector<M> y, Factory<O> using) { if (using == Vectors.AS_INTS) { int[][] data2 = new int[data.length][y.length()]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < y.length(); j++) { data2[i][j] = (int) (data[i] * y.intValue(j)); } } return (Matrix<O>) new IntArrayMatrix(data2); } else if (using == Vectors.AS_REALS) { double[][] data2 = new double[data.length][y.length()]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < y.length(); j++) { data2[i][j] = y.doubleValue(j) * data[i]; } } return (Matrix<O>) new DoubleArrayMatrix(data2); } else { final SparseMatrix<O> matrix = new SparseMatrix<O>(data.length, y.length(), using); for (int i = 0; i < data.length; i++) { for (Map.Entry<Integer, M> e : y.entrySet()) { matrix.set(i, e.getKey(), e.getValue().doubleValue() * data[i]); } } return matrix; } }
@Override @SuppressWarnings("unchecked") public <M extends Number, O extends Number> Matrix<O> outerProduct(Vector<M> y, Factory<O> using) { if (using == Vectors.AS_INTS) { int[][] data2 = new int[data.length][y.length()]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < y.length(); j++) { data2[i][j] = data[i] * y.intValue(j); } } return (Matrix<O>) new IntArrayMatrix(data2); } else if (using == Vectors.AS_REALS) { double[][] data2 = new double[data.length][y.length()]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < y.length(); j++) { data2[i][j] = y.doubleValue(j) * data[i]; } } return (Matrix<O>) new DoubleArrayMatrix(data2); } else { final SparseMatrix<O> matrix = new SparseMatrix<O>(data.length, y.length(), using); for (int i = 0; i < data.length; i++) { for (Map.Entry<Integer, M> e : y.entrySet()) { matrix.set(i, e.getKey(), e.getValue().doubleValue() * data[i]); } } return matrix; } }
@Override public <M extends Number> Matrix<N> product(Matrix<M> B) { if (this.cols() != B.rows()) { throw new IllegalArgumentException("Matrix dimensions not suitable for product"); } if (defaultValue != 0.0 || (B instanceof SparseMatrix && ((SparseMatrix) B).defaultValue != 0.0)) { throw new UnsupportedOperationException(); } Vector<N>[] res = new Vector[this.rows()]; for (int i = 0; i < this.rows(); i++) { res[i] = using.make(B.cols(), 0.0); for (int j : this.arr[i].keySet()) { final Vector<M> r = B.row(j); for (int k : r.keySet()) { res[i].add(k, this.arr[i].doubleValue(j) * B.doubleValue(j, k)); } } } return new SparseMatrix<N>(this.rows(), res, using); }