/** * Solve A*X = B * * @param B right hand side * @return solution if A is square, least squares solution otherwise */ public static double[][] solve(double[][] A, double[][] B) { final int rows = A.length, cols = A[0].length; return rows == cols // ? (new LUDecomposition(A, rows, cols)).solve(B) // : (new QRDecomposition(A, rows, cols)).solve(B); }
throw new IllegalArgumentException(ERR_MATRIX_DIMENSIONS); if(!this.isFullRank()) { throw new ArithmeticException(ERR_MATRIX_RANK_DEFICIENT);
/** * Find the inverse matrix. * * @return Inverse matrix * @throws ArithmeticException Matrix is rank deficient. */ public double[][] inverse() { return solveInplace(unitMatrix(m)); } }
/** * Matrix inverse or pseudoinverse * * @param A matrix to invert * @return inverse(A) if A is square, pseudoinverse otherwise. */ public static double[][] inverse(double[][] A) { final int rows = A.length, cols = A[0].length; return rows == cols // ? (new LUDecomposition(A, rows, cols)).inverse() // : (new QRDecomposition(A, rows, cols)).inverse(); }
/** * Testing the IsFullRank() method of the QRDDecomposition class. */ @Test public void testIsFullRank() { final double[][] m1 = { { 1, 3 }, // { -1, 4 }, // { 1, 1 } }; assertTrue(new QRDecomposition(m1).isFullRank()); final double[][] m2 = { { 1, 1 }, // { 1, 1 }, // { 2, 2 } }; assertFalse(new QRDecomposition(m2).isFullRank()); }
/** * Least squares solution of A*X = B * * @param B A Matrix with as many rows as A and any number of columns. * @return X that minimizes the two norm of Q*R*X-B. * @exception IllegalArgumentException Matrix row dimensions must agree. * @exception RuntimeException Matrix is rank deficient. */ public Matrix solve(Matrix B) { if(B.getRowDimensionality() != m) { throw new IllegalArgumentException("Matrix row dimensions must agree."); } if(!this.isFullRank()) { throw new RuntimeException("Matrix is rank deficient."); } // Copy right hand side int nx = B.getColumnDimensionality(); Matrix X = B.copy(); solveInplace(X.getArrayRef(), nx); return X.getMatrix(0, n - 1, 0, nx - 1); }
@Test public void testWikipedia() { double[][] m = { // { 12, -51, 4 }, // { 6, 167, -68 }, // { -4, 24, -41 }// }; QRDecomposition qr = new QRDecomposition(m); double[][] q = qr.getQ(), r = qr.getR(); // Check that Q^T Q = Unit, i.e. rotation factor. assertTrue(almostEquals(unitMatrix(q[0].length), transposeTimes(q, q))); checkTriangular(r); assertTrue("Not a proper decomposition.", almostEquals(m, times(q, r), 1e-13)); }
@Test public void testRank4() { double delta = 1e-14; double[][] m = transpose(new double[][] { // { 1, 1, 1, 1 + delta, 1, 1 }, // { 1, 1, 1, delta, 0, 0 }, // { 0, 0, 0, 1, 1, 1 }, // { 1, 0, 0, 1, 0, 0 }, // { 0, 0, 1, 0, 0, 1 } // }); QRDecomposition qr = new QRDecomposition(m); double[][] q = qr.getQ(), r = qr.getR(); assertTrue(almostEquals(unitMatrix(q[0].length), transposeTimes(q, q))); checkTriangular(r); assertTrue("Not a proper decomposition.", almostEquals(m, times(q, r), 1e-14)); assertEquals("Rank not as expected", 4, qr.rank(1e-14)); }
/** * Testing the getH method of the QRDDecomposition class, but * only testing if method runs without exception. */ @Test public void testGetH() { new QRDecomposition(M).getH(); }
/** * Testing the getQ method of the QRDDecomposition class. */ @Test public void testGetQ() { // assert that octave and numpy functions have almost same results assertTrue(almostEquals(Q_OCTAVE, Q_NUMPY, 1e-8)); final double[][] q = new QRDecomposition(M).getQ(); assertTrue(almostEquals(q, Q_OCTAVE, 1e-15)); assertTrue(almostEquals(q, Q_NUMPY, 1e-8)); }
/** * Testing the getR method of the QRDDecomposition class. */ @Test public void testGetR() { // assert that octave and numpy functions have almost same results assertTrue(almostEquals(R_OCTAVE, R_NUMPY, 1e-8)); final double[][] r = new QRDecomposition(M).getR(); assertTrue(almostEquals(r, R_OCTAVE, 1e-13)); assertTrue(almostEquals(r, R_NUMPY, 1e-8)); }
/** * Least squares solution of A*X = B * * @param B A Matrix with as many rows as A and any number of columns. * @return X that minimizes the two norm of Q*R*X-B. * @exception IllegalArgumentException Matrix row dimensions must agree. * @exception RuntimeException Matrix is rank deficient. */ public double[][] solve(double[][] B) { int rows = B.length; int cols = B[0].length; if(rows != m) { throw new IllegalArgumentException("Matrix row dimensions must agree."); } if(!this.isFullRank()) { throw new RuntimeException("Matrix is rank deficient."); } // Copy right hand side Matrix X = new Matrix(B).copy(); solveInplace(X.getArrayRef(), cols); return X.getMatrix(0, n - 1, 0, cols - 1).getArrayRef(); }
/** * Matrix inverse or pseudoinverse * * @param A matrix to invert * @return inverse(A) if A is square, pseudoinverse otherwise. */ public static double[][] inverse(double[][] A) { final int rows = A.length, cols = A[0].length; return rows == cols // ? (new LUDecomposition(A, rows, cols)).inverse() // : (new QRDecomposition(A, rows, cols)).inverse(); }
@Test public void testJamaExample() { double[][] M = transpose(new double[][] { { 1., 2., 3., 4. }, { 5., 6., 7., 8. }, { 9., 10., 11., 12. } }); QRDecomposition qr = new QRDecomposition(M); final double[][] q = qr.getQ(), r = qr.getR(); assertTrue(almostEquals(unitMatrix(q[0].length), transposeTimes(q, q))); checkTriangular(r); assertTrue("Not a proper decomposition.", almostEquals(M, times(q, r), 1e-14)); }
/** * Solve A*X = b * * @param b right hand side * @return solution if A is square, least squares solution otherwise */ public static double[] solve(double[][] A, double[] b) { final int rows = A.length, cols = A[0].length; return rows == cols // ? (new LUDecomposition(A, rows, cols)).solve(b) // : (new QRDecomposition(A, rows, cols)).solve(b); }
throw new IllegalArgumentException(ERR_MATRIX_DIMENSIONS); if(!this.isFullRank()) { throw new ArithmeticException(ERR_MATRIX_RANK_DEFICIENT);
/** * Least squares solution of A*X = B * * @param B The matrix B with as many rows as A and any number of columns. * @return X that minimizes the two norm of Q*R*X-B. * @throws IllegalArgumentException Matrix row dimensions must agree. * @throws ArithmeticException Matrix is rank deficient. */ public double[][] solve(double[][] B) { final double[][] sol = solveInplace(copy(B)); return n < sol.length ? Arrays.copyOf(sol, n) : sol; }
/** * Solve A*X = B * * @param B right hand side * @return solution if A is square, least squares solution otherwise */ public final Matrix solve(final Matrix B) { return (elements.length == columndimension ? (new LUDecomposition(this)).solve(B) : (new QRDecomposition(this)).solve(B)); }
throw new IllegalArgumentException(ERR_MATRIX_DIMENSIONS); if(!this.isFullRank()) { throw new ArithmeticException(ERR_MATRIX_RANK_DEFICIENT);
/** * Least squares solution of A*X = b * * @param b A column vector with as many rows as A. * @return X that minimizes the two norm of Q*R*X-b. * @throws IllegalArgumentException Matrix row dimensions must agree. * @throws ArithmeticException Matrix is rank deficient. */ public double[] solve(double[] b) { final double[] sol = solveInplace(copy(b)); return n < sol.length ? Arrays.copyOf(sol, n) : sol; }