/** * Calculate A^TA where A is this matrix * * @param innerProduct The matrix to store the result in * @return */ public void selfInnerProduct(Matrix<N> innerProduct) { assert (innerProduct.cols() == n); assert (innerProduct.rows() == n); for (int k = 0; k < n; k++) { for (Map.Entry<Integer, N> e : arr[k].entrySet()) { final int j = e.getKey(); for (int i = 0; i < n; i++) { final double v = arr[k].doubleValue(i) * e.getValue().doubleValue(); if (v != 0) { innerProduct.add(i, j, v); } } } } }
/** * Calculate AA^T where A is this matrix * * @param outerProduct The matrix to store the result in * @return */ public void selfOuterProduct(Matrix<N> outerProduct) { assert (outerProduct.cols() == m); assert (outerProduct.rows() == m); for (int i = 0; i < m; i++) { for (Map.Entry<Integer, N> e : arr[i].entrySet()) { for (int j = 0; j < m; j++) { final double v = arr[j].doubleValue(e.getKey()) * e.getValue().doubleValue(); if (v != 0.0) { outerProduct.add(i, j, v); } } } } }
@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<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> void add(Matrix<M> matrix) { assert (matrix.rows() == alpha.length); assert (matrix.cols() == alpha.length); if (matrix instanceof TridiagonalMatrix) { final double[] alpha2 = ((TridiagonalMatrix) matrix).alpha;
@Override public <M extends Number> void sub(Matrix<M> matrix) { assert (matrix.rows() == alpha.length); assert (matrix.cols() == alpha.length); if (matrix instanceof TridiagonalMatrix) { final double[] alpha2 = ((TridiagonalMatrix) matrix).alpha;
/** * Given a symmetric sparse integer matrix compute a tri-diagonalization of * the matrix * * @param A The matrix as a row-wise array of sparse array * @return The diagonal and off-diagonal vector, in such a way that a matrix * B can be constructed such that B[i][i] = r[0][i], B[i][i+1] = r[1][i] and * B[i+1][i] = r[1][i] and B[i][j] = 0 otherwise. This matrix has the same * set of eigenvalues as A. */ public static Solution lanczos(Matrix<Double> A) { assert (A.rows() == A.cols()); assert (A.isSymmetric()); final int n = A.rows(); return lanczos(A.asVectorFunction(), randomUnit(n)); }
@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)); } } } }