DenseMatrix implements Serializable { public DenseMatrix solve(DenseMatrix matB) { assert (this.numRows() == matB.numRows()); if (this.m == this.n) { if (this.isSymmetric()) { DenseMatrix matA = this.clone(); DenseMatrix x = matB.clone(); LinearSolver.symmetricIndefiniteSolve(matA, x); return x; } else { DenseMatrix matA = this.clone(); DenseMatrix x = matB.clone(); LinearSolver.nonSymmetricSolve(matA, x); return x; } } else if (this.m > this.n) { DenseMatrix matA = this.clone(); DenseMatrix x = matB.clone(); LeastSquareSolver.solve(matA, x); return x.getSubMatrix(0, this.n, 0, matB.numCols()); } else { DenseMatrix matA = this.clone(); DenseMatrix x = new DenseMatrix(matA.numCols(), matB.numCols()); x.setSubMatrix(matB, 0, matB.numRows(), 0, matB.numCols()); LinearSolver.underDeterminedSolve(matA, x); return x; } } DenseMatrix();  DenseMatrix(int m, int n);  DenseMatrix(int m, int n, double[] data);  DenseMatrix(int m, int n, double[] data, boolean inRowMajor);  DenseMatrix(double[][] data); static DenseMatrix eye(int n); static DenseMatrix eye(int m, int n); static DenseMatrix zeros(int m, int n); static DenseMatrix ones(int m, int n); static DenseMatrix rand(int m, int n); static DenseMatrix randSymmetric(int n); double get(int i, int j); double[] getData(); double[][] getArrayCopy2D(); double[] getArrayCopy1D(boolean inRowMajor); double[] getRow(int row); double[] getColumn(int col); @Override DenseMatrix clone(); @Override boolean equals(Object o); DenseMatrix selectRows(int[] rows); DenseMatrix getSubMatrix(int m0, int m1, int n0, int n1); void setSubMatrix(DenseMatrix sub, int m0, int m1, int n0, int n1); void set(int i, int j, double s); void add(int i, int j, double s); boolean isSquare(); boolean isSymmetric(); int numRows(); int numCols(); double sum(); DenseMatrix scale(double v); void scaleEqual(double v); DenseMatrix plus(DenseMatrix mat); DenseMatrix plus(double alpha); void plusEquals(DenseMatrix mat); void plusEquals(double alpha); DenseMatrix minus(DenseMatrix mat); void minusEquals(DenseMatrix mat); DenseMatrix multiplies(DenseMatrix mat); DenseVector multiplies(DenseVector x); DenseVector multiplies(SparseVector x); DenseMatrix transpose(); @Override String toString(); double norm2(); double cond(); double det(); int rank(); DenseMatrix solve(DenseMatrix matB); DenseVector solve(DenseVector b); DenseMatrix solveLS(DenseMatrix matB); DenseVector solveLS(DenseVector b); DenseMatrix inverse();  }
@Test public void testSolve() throws Exception { { DenseMatrix matA = DenseMatrix.randSymmetric(5); DenseMatrix b = DenseMatrix.rand(5, 7); DenseMatrix x = matA.solve(b); DenseMatrix b0 = matA.multiplies(x); assertEqual2D(b.getArrayCopy2D(), b0.getArrayCopy2D()); } { DenseMatrix matA = DenseMatrix.rand(5, 5); DenseMatrix b = DenseMatrix.rand(5, 7); DenseMatrix x = matA.solve(b); DenseMatrix b0 = matA.multiplies(x); assertEqual2D(b.getArrayCopy2D(), b0.getArrayCopy2D()); } { DenseMatrix matA = DenseMatrix.rand(3, 5); DenseMatrix b = DenseMatrix.rand(3, 7); DenseMatrix x = matA.solve(b); DenseMatrix b0 = matA.multiplies(x); assertEqual2D(b.getArrayCopy2D(), b0.getArrayCopy2D()); } { DenseMatrix matA = DenseMatrix.rand(5, 3); DenseVector b = DenseVector.rand(5); DenseVector x = matA.solve(b); DenseVector r = matA.multiplies(x).minus(b); double nr = r.normL2Square(); for (int i = 0; i < 10; i++) { DenseVector x0 = DenseVector.rand(3); double nr0 = matA.multiplies(x0).minus(b).normL2(); Assert.assertTrue(nr <= nr0); } } }