| 257 | |
| 258 | template <typename kernel_type> |
| 259 | void test_kcentroid_with_offset_linear_kernel( |
| 260 | ) |
| 261 | /*! |
| 262 | requires |
| 263 | - kernel_type::sample_type == a matrix<double,4,1> |
| 264 | - kernel_type == a kernel that just computes a dot product |
| 265 | between its inputs + some constant. I.e. a linear kernel |
| 266 | wrapped by offset_kernel |
| 267 | ensures |
| 268 | - tests the kcentroid object with the given kernel |
| 269 | !*/ |
| 270 | { |
| 271 | // Here we declare that our samples will be 2 dimensional column vectors. |
| 272 | typedef typename kernel_type::sample_type sample_type; |
| 273 | |
| 274 | kernel_type k; |
| 275 | kcentroid<kernel_type> test(k,0.001,20); |
| 276 | |
| 277 | sample_type temp, temp2, temp3; |
| 278 | |
| 279 | matrix<double,5,1> val, val2; |
| 280 | |
| 281 | const double b = std::sqrt(k.offset); |
| 282 | |
| 283 | temp = 2,0,0,0; |
| 284 | temp2 = 0; |
| 285 | val = 0; |
| 286 | DLIB_TEST(approx_equal(test(temp), dist(k,temp,val))); |
| 287 | DLIB_TEST(approx_equal(test(temp2), dist(k,temp2,val))); |
| 288 | DLIB_TEST(approx_equal(test.squared_norm(), length_squared(val))); |
| 289 | |
| 290 | |
| 291 | temp2 = 0; |
| 292 | |
| 293 | // make test store the point(0,0,0,0,b) |
| 294 | val = 0,0,0,0,b; |
| 295 | test.train(temp2, 0,1); |
| 296 | |
| 297 | temp = 2,0,0,0; |
| 298 | dlog << LDEBUG << test(temp) ; |
| 299 | dlog << LDEBUG << "squared_norm(): " << test.squared_norm() ; |
| 300 | |
| 301 | DLIB_TEST(approx_equal(test(temp), dist(k,temp,val))); |
| 302 | DLIB_TEST(approx_equal(test(temp2), dist(k,temp2,val))); |
| 303 | DLIB_TEST_MSG(approx_equal(test.get_distance_function()(temp2), dist(k,temp2,val), 1e-6), |
| 304 | test.get_distance_function()(temp2) - dist(k,temp2,val) << " compare to: " << |
| 305 | test(temp2) - dist(k,temp2,val) |
| 306 | ); |
| 307 | DLIB_TEST(approx_equal(test.squared_norm(), length_squared(val))); |
| 308 | |
| 309 | |
| 310 | // make test store the point(0,0,0,0,0) |
| 311 | val = 0,0,0,0,0; |
| 312 | test.train(temp2, 1,-1); |
| 313 | DLIB_TEST(approx_equal(test(temp), dist(k,temp,val))); |
| 314 | DLIB_TEST(approx_equal(test(temp2), dist(k,temp2,val))); |
| 315 | DLIB_TEST_MSG(approx_equal(test.get_distance_function()(temp2), dist(k,temp2,val)), |
| 316 | test.get_distance_function()(temp2) - dist(k,temp2,val) << " compare to: " << |
nothing calls this directly
no test coverage detected