/** See if the compact format for Q works */ @Test public void checkCompactFormat() { int height = 10; int width = 5; QRDecomposition<DenseMatrix64F> alg = createQRDecomposition(); SimpleMatrix A = new SimpleMatrix(height, width); RandomMatrices.setRandom(A.getMatrix(), rand); alg.decompose(A.getMatrix()); SimpleMatrix Q = new SimpleMatrix(height, width); alg.getQ(Q.getMatrix(), true); // see if Q has the expected properties assertTrue(MatrixFeatures.isOrthogonal(Q.getMatrix(), 1e-6)); // try to extract it with the wrong dimensions Q = new SimpleMatrix(height, height); try { alg.getQ(Q.getMatrix(), true); fail("Didn't fail"); } catch (RuntimeException e) { } }
private void checkDecomposition(int height, int width, boolean compact) { QRDecomposition<DenseMatrix64F> alg = createQRDecomposition(); SimpleMatrix A = new SimpleMatrix(height, width); RandomMatrices.setRandom(A.getMatrix(), rand); assertTrue(alg.decompose(A.copy().getMatrix())); int minStride = Math.min(height, width); SimpleMatrix Q = new SimpleMatrix(height, compact ? minStride : height); alg.getQ(Q.getMatrix(), compact); SimpleMatrix R = new SimpleMatrix(compact ? minStride : height, width); alg.getR(R.getMatrix(), compact); // see if Q has the expected properties assertTrue(MatrixFeatures.isOrthogonal(Q.getMatrix(), 1e-6)); // UtilEjml.print(alg.getQR()); // Q.print(); // R.print(); // see if it has the expected properties DenseMatrix64F A_found = Q.mult(R).getMatrix(); EjmlUnitTests.assertEquals(A.getMatrix(), A_found, 1e-6); assertTrue(Q.transpose().mult(A).isIdentical(R, 1e-6)); }
/** Depending on if setZero being true or not the size of the R matrix changes */ @Test public void checkGetRInputSize() { int width = 5; int height = 10; QRDecomposition<DenseMatrix64F> alg = createQRDecomposition(); SimpleMatrix A = new SimpleMatrix(height, width); RandomMatrices.setRandom(A.getMatrix(), rand); alg.decompose(A.getMatrix()); // check the case where it creates the matrix first assertTrue(alg.getR(null, true).numRows == width); assertTrue(alg.getR(null, false).numRows == height); // check the case where a matrix is provided alg.getR(new DenseMatrix64F(width, width), true); alg.getR(new DenseMatrix64F(height, width), false); // check some negative cases try { alg.getR(new DenseMatrix64F(height, width), true); fail("Should have thrown an exception"); } catch (IllegalArgumentException e) { } try { alg.getR(new DenseMatrix64F(width - 1, width), false); fail("Should have thrown an exception"); } catch (IllegalArgumentException e) { } }
/** * See if passing in a matrix or not providing one to getQ and getR functions has the same result */ @Test public void checkGetNullVersusNot() { int width = 5; int height = 10; QRDecomposition<DenseMatrix64F> alg = createQRDecomposition(); SimpleMatrix A = new SimpleMatrix(height, width); RandomMatrices.setRandom(A.getMatrix(), rand); alg.decompose(A.getMatrix()); // get the results from a provided matrix DenseMatrix64F Q_provided = RandomMatrices.createRandom(height, height, rand); DenseMatrix64F R_provided = RandomMatrices.createRandom(height, width, rand); assertTrue(R_provided == alg.getR(R_provided, false)); assertTrue(Q_provided == alg.getQ(Q_provided, false)); // get the results when no matrix is provided DenseMatrix64F Q_null = alg.getQ(null, false); DenseMatrix64F R_null = alg.getR(null, false); // see if they are the same assertTrue(MatrixFeatures.isEquals(Q_provided, Q_null)); assertTrue(MatrixFeatures.isEquals(R_provided, R_null)); }