@Override public <M extends Number> Vector<N> multTransposed(Vector<M> x) { assert (m == cols()); double[] result = new double[m]; Arrays.fill(result, m * defaultValue); for (int i = 0; i < arr.length; i++) { for (Map.Entry<Integer, N> e : arr[i].entrySet()) { result[e.getKey().intValue()] += x.doubleValue(i) * e.getValue().doubleValue() - defaultValue; } } return using.make(result); }
@Override public double[][] toDoubleArray() { double[][] d = new double[rows()][cols()]; for (int i = 0; i < rows(); i++) { for (int j = 0; j < rows(); j++) { d[i][j] = doubleValue(i, j); } } return d; } }
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 <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); }
/** * 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); }