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