| 67 | |
| 68 | template <typename matrix_type> |
| 69 | void test_qr ( const matrix_type& m) |
| 70 | { |
| 71 | typedef typename matrix_type::type type; |
| 72 | const type eps = 10*max(abs(m))*sqrt(std::numeric_limits<type>::epsilon()); |
| 73 | dlog << LDEBUG << "test_qr(): " << m.nr() << " x " << m.nc() << " eps: " << eps; |
| 74 | print_spinner(); |
| 75 | |
| 76 | |
| 77 | qr_decomposition<matrix_type> test(m); |
| 78 | |
| 79 | |
| 80 | DLIB_TEST(test.nr() == m.nr()); |
| 81 | DLIB_TEST(test.nc() == m.nc()); |
| 82 | |
| 83 | |
| 84 | type temp; |
| 85 | DLIB_TEST_MSG( (temp= max(abs(test.get_q()*test.get_r() - m))) < eps,temp); |
| 86 | |
| 87 | // none of the matrices we should be passing in to test_qr() should be non-full rank. |
| 88 | DLIB_TEST(test.is_full_rank() == true); |
| 89 | |
| 90 | if (m.nr() == m.nc()) |
| 91 | { |
| 92 | matrix<type> m2; |
| 93 | matrix<type,0,1> col; |
| 94 | |
| 95 | m2 = identity_matrix<type>(m.nr()); |
| 96 | DLIB_TEST_MSG(equal(m*test.solve(m2), m2,eps),max(abs(m*test.solve(m2)- m2))); |
| 97 | m2 = randmat<type>(m.nr(),5); |
| 98 | DLIB_TEST_MSG(equal(m*test.solve(m2), m2,eps),max(abs(m*test.solve(m2)- m2))); |
| 99 | m2 = randmat<type>(m.nr(),1); |
| 100 | DLIB_TEST_MSG(equal(m*test.solve(m2), m2,eps),max(abs(m*test.solve(m2)- m2))); |
| 101 | col = randmat<type>(m.nr(),1); |
| 102 | DLIB_TEST_MSG(equal(m*test.solve(col), col,eps),max(abs(m*test.solve(m2)- m2))); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | DLIB_TEST_MSG(dlib::equal(pinv(m), test.solve(identity_matrix<type>(m.nr())), eps), |
| 107 | max(abs(pinv(m) - test.solve(identity_matrix<type>(m.nr())))) ); |
| 108 | } |
| 109 | |
| 110 | // now make us a non-full rank matrix |
| 111 | if (m.nc() > 1) |
| 112 | { |
| 113 | matrix<type> sm(m); |
| 114 | set_colm(sm,0) = colm(sm,1); |
| 115 | |
| 116 | qr_decomposition<matrix_type> test2(sm); |
| 117 | DLIB_TEST_MSG( (temp= max(abs(test.get_q()*test.get_r() - m))) < eps,temp); |
| 118 | |
| 119 | if (test2.nc() < 100) |
| 120 | { |
| 121 | DLIB_TEST_MSG(test2.is_full_rank() == false,"eps: " << eps); |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| 126 | } |