@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); }
public static void denseInplaceDecomp(SparseMatrix<Double> a) { int n = a.rows(); int i, j, k; double sum; double[] diagonal = new double[n]; for (i = 0; i < n; i++) { System.err.print("."); for (j = i; j < n; j++) { for (sum = a.doubleValue(i, j), k = i - 1; k >= 0; k--) { sum -= a.doubleValue(i, k) * a.doubleValue(j, k); } if (i == j) { if (sum <= 0.0) { throw new IllegalArgumentException("Matrix not positive definite"); } diagonal[i] = Math.sqrt(sum); } else { a.set(j, i, sum / diagonal[i]); a.set(i, j, 0); } } } for (i = 0; i < n; i++) { a.set(i, i, diagonal[i]); } }
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; }
/** * Find the eigenvalues of sparse matrix by means of a Lanczos-QR method. * * @param A The matrix * @param epsilon The error rate * @return The eigenvalues of the matrix */ public static Solution qrSolve(SparseMatrix<Double> A, double epsilon) { assert (A.rows() == A.cols()); assert (A.isSymmetric()); // It turns out that Lanczos sucks at the easy columns so we remove these // on the first pass final TrivialEigenvalues<Double> trivial = TrivialEigenvalues.find(A, true); if (trivial.nonTrivial == null) { return new Solution(trivial.eigenvalues, new SequenceOfGivens()); } final TridiagonalMatrix tridiag = LanczosAlgorithm.lanczos(trivial.nonTrivial).tridiagonal(); return qrSolve(epsilon, tridiag, trivial); }