@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 Vector<Double> apply(Vector<Double> v) { return mult(v); } };
public static <N extends Number> TridiagonalMatrix tridiagonalize(Matrix<N> A) { assert (A.isSymmetric()); final int n = A.rows(); final DoubleArrayMatrix A2 = new DoubleArrayMatrix(n, n); A2.add(A); double[] alpha = new double[n]; double[] beta = new double[n - 1]; final RealVector x = new RealVector(n - k - 1); for (int j = k + 1; j < n; j++) { x.put(j - k - 1, A2.doubleValue(j, k)); for (int i = 0; i < n - k - 1; i++) { for (int j = 0; j < n - k - 1; j++) { w.data()[i] += 2 * A2.doubleValue(i + k + 1, j + k + 1) * v.doubleValue(j) / vtv; beta[k] -= A2.doubleValue(i + k, k) * ((i == 1 ? 1 : 0) - 2 * v.doubleValue(i - 1) * v.doubleValue(0) / vtv); A2.data()[i + k + 1][j + k + 1] -= v.doubleValue(i) * w.doubleValue(j) + w.doubleValue(i) * v.doubleValue(j); alpha[k] = A2.doubleValue(k, k); beta[n - 2] = A2.doubleValue(n - 1, n - 2); alpha[n - 2] = A2.doubleValue(n - 2, n - 2); alpha[n - 1] = A2.doubleValue(n - 1, n - 1);
@Override public Matrix<Double> transpose() { double[][] data2 = new double[n][m]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { data2[j][i] = data[i][j]; } } return new DoubleArrayMatrix(data2); }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final DoubleArrayMatrix other = (DoubleArrayMatrix) obj; if(other.rows() != this.rows()) { return false; } for (int i = 0; i < this.data.length; i++) { if (!Arrays.equals(this.data[i], other.data[i])) { return false; } } return true; } }
public static DoubleArrayMatrix make(int cols, double... data) { if (data.length % cols != 0) { throw new IllegalArgumentException("Number of data points does not divide number of cols"); } double[][] d2 = new double[data.length / cols][cols]; for (int i = 0; i < data.length; i++) { d2[i / cols][i % cols] = data[i]; } return new DoubleArrayMatrix(d2); }
@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 public <M extends Number> Vector<Double> mult(Vector<M> x) { return mult(x, Vectors.AS_REALS); }
@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; } }
@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<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); } }