@Override public <M extends Number> void sub(Matrix<M> matrix) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { data[i][j] -= matrix.doubleValue(i, j); } } }
@Override public <M extends Number> void add(Matrix<M> matrix) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { data[i][j] += matrix.doubleValue(i, j); } } }
@Override public double doubleValue(int i, int j) { return A.doubleValue(map[i], map[j]); }
@Override public <M extends Number> void sub(Matrix<M> matrix) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { data[i][j] -= matrix.doubleValue(i, j); } } }
@Override public <M extends Number> void add(Matrix<M> matrix) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { data[i][j] += matrix.doubleValue(i, j); } } }
alpha[i] += matrix.doubleValue(i, i); if (i < alpha.length - 1) { if (matrix.doubleValue(i, i + 1) == matrix.doubleValue(i + 1, i)) { beta[i] += matrix.doubleValue(i, i + 1); } else { throw new RuntimeException("Adding non tridiagonal matrix to tridiagonal matrix!"); if (Math.abs(i - j) > 1 && matrix.doubleValue(i, j) > 1e-6) { throw new RuntimeException("Adding non tridiagonal matrix to tridiagonal matrix!");
alpha[i] -= matrix.doubleValue(i, i); if (i < alpha.length - 1) { if (matrix.doubleValue(i, i + 1) == matrix.doubleValue(i + 1, i)) { beta[i] -= matrix.doubleValue(i, i + 1); } else { throw new RuntimeException("Adding non tridiagonal matrix to tridiagonal matrix!"); if (Math.abs(i - j) > 1 && matrix.doubleValue(i, j) > 1e-6) { throw new RuntimeException("Adding non tridiagonal matrix to tridiagonal matrix!");
@Override public <M extends Number> Matrix<Double> product(Matrix<M> B) { if (this.cols() != B.rows()) { throw new IllegalArgumentException("Matrix dimensions not suitable for product"); } double[][] res = new double[this.rows()][B.cols()]; for (int i = 0; i < this.rows(); i++) { for (int k = 0; k < B.cols(); k++) { res[i][k] = (i > 0 ? beta[i - 1] * B.doubleValue(i - 1, k) : 0.0) + alpha[i] * B.doubleValue(i, k) + (i + 1 != this.rows() ? beta[i] * B.doubleValue(i + 1, k) : 0.0); } } return new DoubleArrayMatrix(res); } }
@Override public <M extends Number> Matrix<Double> product(Matrix<M> B) { if (this.cols() != B.rows()) { throw new IllegalArgumentException("Matrix dimensions not suitable for product"); } double[][] res = new double[this.rows()][B.cols()]; for (int i = 0; i < this.rows(); i++) { for (int j = 0; j < this.cols(); j++) { for (int k = 0; k < B.cols(); k++) { res[i][k] += data[i][j] * B.doubleValue(j, k); } } } return new DoubleArrayMatrix(res); }
@Override public <M extends Number> Matrix<Integer> product(Matrix<M> B) { if (this.cols() != B.rows()) { throw new IllegalArgumentException("Matrix dimensions not suitable for product"); } int[][] res = new int[this.rows()][B.cols()]; for (int i = 0; i < this.rows(); i++) { for (int j = 0; j < this.cols(); j++) { for (int k = 0; k < B.cols(); k++) { res[i][k] += data[i][j] * B.doubleValue(j, k); } } } return new IntArrayMatrix(res); }
@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); }
@Override public <M extends Number> void sub(Matrix<M> matrix) { assert (m == matrix.rows()); assert (n == matrix.cols()); if (matrix instanceof SparseMatrix) { final Vector<M>[] arr2 = ((SparseMatrix<M>) matrix).arr; for (int i = 0; i < m; i++) { if (arr[i] != null && arr2 != null) { arr[i].sub(arr2[i]); } else if (arr2 != null) { arr[i] = using.make(n, defaultValue); arr[i].sub(arr2[i]); } } } else { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { row(i).sub(j, matrix.doubleValue(i, j)); } } } }
@Override public <M extends Number> void add(Matrix<M> matrix) { assert (m == matrix.rows()); assert (n == matrix.cols()); if (matrix instanceof SparseMatrix) { final Vector<M>[] arr2 = ((SparseMatrix<M>) matrix).arr; for (int i = 0; i < m; i++) { if (arr[i] != null && arr2 != null) { arr[i].add(arr2[i]); } else if (arr2 != null) { arr[i] = using.make(n, defaultValue); arr[i].add(arr2[i]); } } } else { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { row(i).add(j, matrix.doubleValue(i, j)); } } } }