[mlpack-svn] r10232 - mlpack/trunk/src/mlpack/tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Nov 10 10:54:00 EST 2011
Author: rcurtin
Date: 2011-11-10 10:53:59 -0500 (Thu, 10 Nov 2011)
New Revision: 10232
Added:
mlpack/trunk/src/mlpack/tests/allkfn_test.cpp
mlpack/trunk/src/mlpack/tests/allknn_test.cpp
mlpack/trunk/src/mlpack/tests/emst_test.cpp
mlpack/trunk/src/mlpack/tests/infomax_ica_test.cpp
mlpack/trunk/src/mlpack/tests/kernel_pca_test.cpp
mlpack/trunk/src/mlpack/tests/kernel_test.cpp
mlpack/trunk/src/mlpack/tests/nbc_test.cpp
mlpack/trunk/src/mlpack/tests/nca_test.cpp
mlpack/trunk/src/mlpack/tests/ridge_regression_test.cpp
mlpack/trunk/src/mlpack/tests/sort_policy_test.cpp
mlpack/trunk/src/mlpack/tests/svm_test.cpp
Removed:
mlpack/trunk/src/mlpack/tests/allkfn_test.cc
mlpack/trunk/src/mlpack/tests/allknn_test.cc
mlpack/trunk/src/mlpack/tests/emst_test.cc
mlpack/trunk/src/mlpack/tests/infomax_ica_test.cc
mlpack/trunk/src/mlpack/tests/kernel_pca_test.cc
mlpack/trunk/src/mlpack/tests/kernel_test.cc
mlpack/trunk/src/mlpack/tests/nbc_test.cc
mlpack/trunk/src/mlpack/tests/nca_test.cc
mlpack/trunk/src/mlpack/tests/ridge_regression_test.cc
mlpack/trunk/src/mlpack/tests/sort_policy_test.cc
mlpack/trunk/src/mlpack/tests/svm_test.cc
Modified:
mlpack/trunk/src/mlpack/tests/CMakeLists.txt
mlpack/trunk/src/mlpack/tests/aug_lagrangian_test.cpp
mlpack/trunk/src/mlpack/tests/cli_test.cpp
mlpack/trunk/src/mlpack/tests/lbfgs_test.cpp
mlpack/trunk/src/mlpack/tests/lin_alg_test.cpp
mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp
mlpack/trunk/src/mlpack/tests/load_save_test.cpp
mlpack/trunk/src/mlpack/tests/math_test.cpp
mlpack/trunk/src/mlpack/tests/mlpack_test.cpp
mlpack/trunk/src/mlpack/tests/nnsvm_test.cpp
mlpack/trunk/src/mlpack/tests/save_restore_utility_test.cpp
mlpack/trunk/src/mlpack/tests/tree_test.cpp
mlpack/trunk/src/mlpack/tests/union_find_test.cpp
Log:
Update all tests to new formatting standards and make them all .cpp not .cc.
Modified: mlpack/trunk/src/mlpack/tests/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/tests/CMakeLists.txt 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/CMakeLists.txt 2011-11-10 15:53:59 UTC (rev 10232)
@@ -12,23 +12,24 @@
# test executable
add_executable(mlpack_test
mlpack_test.cpp
- allkfn_test.cc
- allknn_test.cc
+ allkfn_test.cpp
+ allknn_test.cpp
cli_test.cpp
- emst_test.cc
- infomax_ica_test.cc
- kernel_test.cc
+ emst_test.cpp
+# hmm_test.cpp
+ infomax_ica_test.cpp
+ kernel_test.cpp
lin_alg_test.cpp
linear_regression_test.cpp
load_save_test.cpp
math_test.cpp
- nbc_test.cc
- nca_test.cc
+ nbc_test.cpp
+ nca_test.cpp
nnsvm_test.cpp
- ridge_regression_test.cc
+ ridge_regression_test.cpp
save_restore_utility_test.cpp
- sort_policy_test.cc
- svm_test.cc
+ sort_policy_test.cpp
+ svm_test.cpp
tree_test.cpp
union_find_test.cpp
)
Deleted: mlpack/trunk/src/mlpack/tests/allkfn_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/allkfn_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/allkfn_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,434 +0,0 @@
-/**
- * @file allkfn_test.cc
- *
- * Test file for AllkFN class
- */
-#include <mlpack/core.h>
-#include <mlpack/methods/neighbor_search/neighbor_search.h>
-#include <boost/test/unit_test.hpp>
-
-
-using namespace mlpack;
-using namespace mlpack::neighbor;
-
-BOOST_AUTO_TEST_SUITE(AllKfnTest);
- /***
- * Simple furthest-neighbors test with small, synthetic dataset. This is an
- * exhaustive test, which checks that each method for performing the calculation
- * (dual-tree, single-tree, naive) produces the correct results. An
- * eleven-point dataset and the ten furthest neighbors are taken. The dataset
- * is in one dimension for simplicity -- the correct functionality of distance
- * functions is not tested here.
- */
- BOOST_AUTO_TEST_CASE(exhaustive_synthetic_test) {
- // Set up our data.
- arma::mat data(1, 11);
- data[0] = 0.05; // Row addressing is unnecessary (they are all 0).
- data[1] = 0.35;
- data[2] = 0.15;
- data[3] = 1.25;
- data[4] = 5.05;
- data[5] = -0.22;
- data[6] = -2.00;
- data[7] = -1.30;
- data[8] = 0.45;
- data[9] = 0.90;
- data[10] = 1.00;
-
- // We will loop through three times, one for each method of performing the
- // calculation. We'll always use 10 neighbors, so set that parameter.
- CLI::GetParam<int>("neighbor_search/k") = 10;
- for (int i = 0; i < 3; i++) {
- AllkFN* allkfn;
- arma::mat data_mutable = data;
- switch(i) {
- case 0: // Use the dual-tree method.
- allkfn = new AllkFN(data_mutable);
- break;
- case 1: // Use the single-tree method.
- CLI::GetParam<bool>("neighbor_search/single_mode") = true;
- allkfn = new AllkFN(data_mutable);
- break;
- case 2: // Use the naive method.
- CLI::GetParam<bool>("neighbor_search/single_mode") = false;
- CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- allkfn = new AllkFN(data_mutable);
- break;
- }
-
- // Now perform the actual calculation.
- arma::Mat<size_t> neighbors;
- arma::mat distances;
- allkfn->ComputeNeighbors(neighbors, distances);
-
- // Now the exhaustive check for correctness. This will be long. We must
- // also remember that the distances returned are squared distances. As a
- // result, distance comparisons are written out as (distance * distance) for
- // readability.
-
- // Neighbors of point 0.
- BOOST_REQUIRE(neighbors(9, 0) == 2);
- BOOST_REQUIRE_CLOSE(distances(9, 0), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(8, 0) == 5);
- BOOST_REQUIRE_CLOSE(distances(8, 0), (0.27 * 0.27), 1e-5);
- BOOST_REQUIRE(neighbors(7, 0) == 1);
- BOOST_REQUIRE_CLOSE(distances(7, 0), (0.30 * 0.30), 1e-5);
- BOOST_REQUIRE(neighbors(6, 0) == 8);
- BOOST_REQUIRE_CLOSE(distances(6, 0), (0.40 * 0.40), 1e-5);
- BOOST_REQUIRE(neighbors(5, 0) == 9);
- BOOST_REQUIRE_CLOSE(distances(5, 0), (0.85 * 0.85), 1e-5);
- BOOST_REQUIRE(neighbors(4, 0) == 10);
- BOOST_REQUIRE_CLOSE(distances(4, 0), (0.95 * 0.95), 1e-5);
- BOOST_REQUIRE(neighbors(3, 0) == 3);
- BOOST_REQUIRE_CLOSE(distances(3, 0), (1.20 * 1.20), 1e-5);
- BOOST_REQUIRE(neighbors(2, 0) == 7);
- BOOST_REQUIRE_CLOSE(distances(2, 0), (1.35 * 1.35), 1e-5);
- BOOST_REQUIRE(neighbors(1, 0) == 6);
- BOOST_REQUIRE_CLOSE(distances(1, 0), (2.05 * 2.05), 1e-5);
- BOOST_REQUIRE(neighbors(0, 0) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 0), (5.00 * 5.00), 1e-5);
-
- // Neighbors of point 1.
- BOOST_REQUIRE(neighbors(9, 1) == 8);
- BOOST_REQUIRE_CLOSE(distances(9, 1), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(8, 1) == 2);
- BOOST_REQUIRE_CLOSE(distances(8, 1), (0.20 * 0.20), 1e-5);
- BOOST_REQUIRE(neighbors(7, 1) == 0);
- BOOST_REQUIRE_CLOSE(distances(7, 1), (0.30 * 0.30), 1e-5);
- BOOST_REQUIRE(neighbors(6, 1) == 9);
- BOOST_REQUIRE_CLOSE(distances(6, 1), (0.55 * 0.55), 1e-5);
- BOOST_REQUIRE(neighbors(5, 1) == 5);
- BOOST_REQUIRE_CLOSE(distances(5, 1), (0.57 * 0.57), 1e-5);
- BOOST_REQUIRE(neighbors(4, 1) == 10);
- BOOST_REQUIRE_CLOSE(distances(4, 1), (0.65 * 0.65), 1e-5);
- BOOST_REQUIRE(neighbors(3, 1) == 3);
- BOOST_REQUIRE_CLOSE(distances(3, 1), (0.90 * 0.90), 1e-5);
- BOOST_REQUIRE(neighbors(2, 1) == 7);
- BOOST_REQUIRE_CLOSE(distances(2, 1), (1.65 * 1.65), 1e-5);
- BOOST_REQUIRE(neighbors(1, 1) == 6);
- BOOST_REQUIRE_CLOSE(distances(1, 1), (2.35 * 2.35), 1e-5);
- BOOST_REQUIRE(neighbors(0, 1) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 1), (4.70 * 4.70), 1e-5);
-
- // Neighbors of point 2.
- BOOST_REQUIRE(neighbors(9, 2) == 0);
- BOOST_REQUIRE_CLOSE(distances(9, 2), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(8, 2) == 1);
- BOOST_REQUIRE_CLOSE(distances(8, 2), (0.20 * 0.20), 1e-5);
- BOOST_REQUIRE(neighbors(7, 2) == 8);
- BOOST_REQUIRE_CLOSE(distances(7, 2), (0.30 * 0.30), 1e-5);
- BOOST_REQUIRE(neighbors(6, 2) == 5);
- BOOST_REQUIRE_CLOSE(distances(6, 2), (0.37 * 0.37), 1e-5);
- BOOST_REQUIRE(neighbors(5, 2) == 9);
- BOOST_REQUIRE_CLOSE(distances(5, 2), (0.75 * 0.75), 1e-5);
- BOOST_REQUIRE(neighbors(4, 2) == 10);
- BOOST_REQUIRE_CLOSE(distances(4, 2), (0.85 * 0.85), 1e-5);
- BOOST_REQUIRE(neighbors(3, 2) == 3);
- BOOST_REQUIRE_CLOSE(distances(3, 2), (1.10 * 1.10), 1e-5);
- BOOST_REQUIRE(neighbors(2, 2) == 7);
- BOOST_REQUIRE_CLOSE(distances(2, 2), (1.45 * 1.45), 1e-5);
- BOOST_REQUIRE(neighbors(1, 2) == 6);
- BOOST_REQUIRE_CLOSE(distances(1, 2), (2.15 * 2.15), 1e-5);
- BOOST_REQUIRE(neighbors(0, 2) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 2), (4.90 * 4.90), 1e-5);
-
- // Neighbors of point 3.
- BOOST_REQUIRE(neighbors(9, 3) == 10);
- BOOST_REQUIRE_CLOSE(distances(9, 3), (0.25 * 0.25), 1e-5);
- BOOST_REQUIRE(neighbors(8, 3) == 9);
- BOOST_REQUIRE_CLOSE(distances(8, 3), (0.35 * 0.35), 1e-5);
- BOOST_REQUIRE(neighbors(7, 3) == 8);
- BOOST_REQUIRE_CLOSE(distances(7, 3), (0.80 * 0.80), 1e-5);
- BOOST_REQUIRE(neighbors(6, 3) == 1);
- BOOST_REQUIRE_CLOSE(distances(6, 3), (0.90 * 0.90), 1e-5);
- BOOST_REQUIRE(neighbors(5, 3) == 2);
- BOOST_REQUIRE_CLOSE(distances(5, 3), (1.10 * 1.10), 1e-5);
- BOOST_REQUIRE(neighbors(4, 3) == 0);
- BOOST_REQUIRE_CLOSE(distances(4, 3), (1.20 * 1.20), 1e-5);
- BOOST_REQUIRE(neighbors(3, 3) == 5);
- BOOST_REQUIRE_CLOSE(distances(3, 3), (1.47 * 1.47), 1e-5);
- BOOST_REQUIRE(neighbors(2, 3) == 7);
- BOOST_REQUIRE_CLOSE(distances(2, 3), (2.55 * 2.55), 1e-5);
- BOOST_REQUIRE(neighbors(1, 3) == 6);
- BOOST_REQUIRE_CLOSE(distances(1, 3), (3.25 * 3.25), 1e-5);
- BOOST_REQUIRE(neighbors(0, 3) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 3), (3.80 * 3.80), 1e-5);
-
- // Neighbors of point 4.
- BOOST_REQUIRE(neighbors(9, 4) == 3);
- BOOST_REQUIRE_CLOSE(distances(9, 4), (3.80 * 3.80), 1e-5);
- BOOST_REQUIRE(neighbors(8, 4) == 10);
- BOOST_REQUIRE_CLOSE(distances(8, 4), (4.05 * 4.05), 1e-5);
- BOOST_REQUIRE(neighbors(7, 4) == 9);
- BOOST_REQUIRE_CLOSE(distances(7, 4), (4.15 * 4.15), 1e-5);
- BOOST_REQUIRE(neighbors(6, 4) == 8);
- BOOST_REQUIRE_CLOSE(distances(6, 4), (4.60 * 4.60), 1e-5);
- BOOST_REQUIRE(neighbors(5, 4) == 1);
- BOOST_REQUIRE_CLOSE(distances(5, 4), (4.70 * 4.70), 1e-5);
- BOOST_REQUIRE(neighbors(4, 4) == 2);
- BOOST_REQUIRE_CLOSE(distances(4, 4), (4.90 * 4.90), 1e-5);
- BOOST_REQUIRE(neighbors(3, 4) == 0);
- BOOST_REQUIRE_CLOSE(distances(3, 4), (5.00 * 5.00), 1e-5);
- BOOST_REQUIRE(neighbors(2, 4) == 5);
- BOOST_REQUIRE_CLOSE(distances(2, 4), (5.27 * 5.27), 1e-5);
- BOOST_REQUIRE(neighbors(1, 4) == 7);
- BOOST_REQUIRE_CLOSE(distances(1, 4), (6.35 * 6.35), 1e-5);
- BOOST_REQUIRE(neighbors(0, 4) == 6);
- BOOST_REQUIRE_CLOSE(distances(0, 4), (7.05 * 7.05), 1e-5);
-
- // Neighbors of point 5.
- BOOST_REQUIRE(neighbors(9, 5) == 0);
- BOOST_REQUIRE_CLOSE(distances(9, 5), (0.27 * 0.27), 1e-5);
- BOOST_REQUIRE(neighbors(8, 5) == 2);
- BOOST_REQUIRE_CLOSE(distances(8, 5), (0.37 * 0.37), 1e-5);
- BOOST_REQUIRE(neighbors(7, 5) == 1);
- BOOST_REQUIRE_CLOSE(distances(7, 5), (0.57 * 0.57), 1e-5);
- BOOST_REQUIRE(neighbors(6, 5) == 8);
- BOOST_REQUIRE_CLOSE(distances(6, 5), (0.67 * 0.67), 1e-5);
- BOOST_REQUIRE(neighbors(5, 5) == 7);
- BOOST_REQUIRE_CLOSE(distances(5, 5), (1.08 * 1.08), 1e-5);
- BOOST_REQUIRE(neighbors(4, 5) == 9);
- BOOST_REQUIRE_CLOSE(distances(4, 5), (1.12 * 1.12), 1e-5);
- BOOST_REQUIRE(neighbors(3, 5) == 10);
- BOOST_REQUIRE_CLOSE(distances(3, 5), (1.22 * 1.22), 1e-5);
- BOOST_REQUIRE(neighbors(2, 5) == 3);
- BOOST_REQUIRE_CLOSE(distances(2, 5), (1.47 * 1.47), 1e-5);
- BOOST_REQUIRE(neighbors(1, 5) == 6);
- BOOST_REQUIRE_CLOSE(distances(1, 5), (1.78 * 1.78), 1e-5);
- BOOST_REQUIRE(neighbors(0, 5) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 5), (5.27 * 5.27), 1e-5);
-
- // Neighbors of point 6.
- BOOST_REQUIRE(neighbors(9, 6) == 7);
- BOOST_REQUIRE_CLOSE(distances(9, 6), (0.70 * 0.70), 1e-5);
- BOOST_REQUIRE(neighbors(8, 6) == 5);
- BOOST_REQUIRE_CLOSE(distances(8, 6), (1.78 * 1.78), 1e-5);
- BOOST_REQUIRE(neighbors(7, 6) == 0);
- BOOST_REQUIRE_CLOSE(distances(7, 6), (2.05 * 2.05), 1e-5);
- BOOST_REQUIRE(neighbors(6, 6) == 2);
- BOOST_REQUIRE_CLOSE(distances(6, 6), (2.15 * 2.15), 1e-5);
- BOOST_REQUIRE(neighbors(5, 6) == 1);
- BOOST_REQUIRE_CLOSE(distances(5, 6), (2.35 * 2.35), 1e-5);
- BOOST_REQUIRE(neighbors(4, 6) == 8);
- BOOST_REQUIRE_CLOSE(distances(4, 6), (2.45 * 2.45), 1e-5);
- BOOST_REQUIRE(neighbors(3, 6) == 9);
- BOOST_REQUIRE_CLOSE(distances(3, 6), (2.90 * 2.90), 1e-5);
- BOOST_REQUIRE(neighbors(2, 6) == 10);
- BOOST_REQUIRE_CLOSE(distances(2, 6), (3.00 * 3.00), 1e-5);
- BOOST_REQUIRE(neighbors(1, 6) == 3);
- BOOST_REQUIRE_CLOSE(distances(1, 6), (3.25 * 3.25), 1e-5);
- BOOST_REQUIRE(neighbors(0, 6) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 6), (7.05 * 7.05), 1e-5);
-
- // Neighbors of point 7.
- BOOST_REQUIRE(neighbors(9, 7) == 6);
- BOOST_REQUIRE_CLOSE(distances(9, 7), (0.70 * 0.70), 1e-5);
- BOOST_REQUIRE(neighbors(8, 7) == 5);
- BOOST_REQUIRE_CLOSE(distances(8, 7), (1.08 * 1.08), 1e-5);
- BOOST_REQUIRE(neighbors(7, 7) == 0);
- BOOST_REQUIRE_CLOSE(distances(7, 7), (1.35 * 1.35), 1e-5);
- BOOST_REQUIRE(neighbors(6, 7) == 2);
- BOOST_REQUIRE_CLOSE(distances(6, 7), (1.45 * 1.45), 1e-5);
- BOOST_REQUIRE(neighbors(5, 7) == 1);
- BOOST_REQUIRE_CLOSE(distances(5, 7), (1.65 * 1.65), 1e-5);
- BOOST_REQUIRE(neighbors(4, 7) == 8);
- BOOST_REQUIRE_CLOSE(distances(4, 7), (1.75 * 1.75), 1e-5);
- BOOST_REQUIRE(neighbors(3, 7) == 9);
- BOOST_REQUIRE_CLOSE(distances(3, 7), (2.20 * 2.20), 1e-5);
- BOOST_REQUIRE(neighbors(2, 7) == 10);
- BOOST_REQUIRE_CLOSE(distances(2, 7), (2.30 * 2.30), 1e-5);
- BOOST_REQUIRE(neighbors(1, 7) == 3);
- BOOST_REQUIRE_CLOSE(distances(1, 7), (2.55 * 2.55), 1e-5);
- BOOST_REQUIRE(neighbors(0, 7) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 7), (6.35 * 6.35), 1e-5);
-
- // Neighbors of point 8.
- BOOST_REQUIRE(neighbors(9, 8) == 1);
- BOOST_REQUIRE_CLOSE(distances(9, 8), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(8, 8) == 2);
- BOOST_REQUIRE_CLOSE(distances(8, 8), (0.30 * 0.30), 1e-5);
- BOOST_REQUIRE(neighbors(7, 8) == 0);
- BOOST_REQUIRE_CLOSE(distances(7, 8), (0.40 * 0.40), 1e-5);
- BOOST_REQUIRE(neighbors(6, 8) == 9);
- BOOST_REQUIRE_CLOSE(distances(6, 8), (0.45 * 0.45), 1e-5);
- BOOST_REQUIRE(neighbors(5, 8) == 10);
- BOOST_REQUIRE_CLOSE(distances(5, 8), (0.55 * 0.55), 1e-5);
- BOOST_REQUIRE(neighbors(4, 8) == 5);
- BOOST_REQUIRE_CLOSE(distances(4, 8), (0.67 * 0.67), 1e-5);
- BOOST_REQUIRE(neighbors(3, 8) == 3);
- BOOST_REQUIRE_CLOSE(distances(3, 8), (0.80 * 0.80), 1e-5);
- BOOST_REQUIRE(neighbors(2, 8) == 7);
- BOOST_REQUIRE_CLOSE(distances(2, 8), (1.75 * 1.75), 1e-5);
- BOOST_REQUIRE(neighbors(1, 8) == 6);
- BOOST_REQUIRE_CLOSE(distances(1, 8), (2.45 * 2.45), 1e-5);
- BOOST_REQUIRE(neighbors(0, 8) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 8), (4.60 * 4.60), 1e-5);
-
- // Neighbors of point 9.
- BOOST_REQUIRE(neighbors(9, 9) == 10);
- BOOST_REQUIRE_CLOSE(distances(9, 9), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(8, 9) == 3);
- BOOST_REQUIRE_CLOSE(distances(8, 9), (0.35 * 0.35), 1e-5);
- BOOST_REQUIRE(neighbors(7, 9) == 8);
- BOOST_REQUIRE_CLOSE(distances(7, 9), (0.45 * 0.45), 1e-5);
- BOOST_REQUIRE(neighbors(6, 9) == 1);
- BOOST_REQUIRE_CLOSE(distances(6, 9), (0.55 * 0.55), 1e-5);
- BOOST_REQUIRE(neighbors(5, 9) == 2);
- BOOST_REQUIRE_CLOSE(distances(5, 9), (0.75 * 0.75), 1e-5);
- BOOST_REQUIRE(neighbors(4, 9) == 0);
- BOOST_REQUIRE_CLOSE(distances(4, 9), (0.85 * 0.85), 1e-5);
- BOOST_REQUIRE(neighbors(3, 9) == 5);
- BOOST_REQUIRE_CLOSE(distances(3, 9), (1.12 * 1.12), 1e-5);
- BOOST_REQUIRE(neighbors(2, 9) == 7);
- BOOST_REQUIRE_CLOSE(distances(2, 9), (2.20 * 2.20), 1e-5);
- BOOST_REQUIRE(neighbors(1, 9) == 6);
- BOOST_REQUIRE_CLOSE(distances(1, 9), (2.90 * 2.90), 1e-5);
- BOOST_REQUIRE(neighbors(0, 9) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 9), (4.15 * 4.15), 1e-5);
-
- // Neighbors of point 10.
- BOOST_REQUIRE(neighbors(9, 10) == 9);
- BOOST_REQUIRE_CLOSE(distances(9, 10), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(8, 10) == 3);
- BOOST_REQUIRE_CLOSE(distances(8, 10), (0.25 * 0.25), 1e-5);
- BOOST_REQUIRE(neighbors(7, 10) == 8);
- BOOST_REQUIRE_CLOSE(distances(7, 10), (0.55 * 0.55), 1e-5);
- BOOST_REQUIRE(neighbors(6, 10) == 1);
- BOOST_REQUIRE_CLOSE(distances(6, 10), (0.65 * 0.65), 1e-5);
- BOOST_REQUIRE(neighbors(5, 10) == 2);
- BOOST_REQUIRE_CLOSE(distances(5, 10), (0.85 * 0.85), 1e-5);
- BOOST_REQUIRE(neighbors(4, 10) == 0);
- BOOST_REQUIRE_CLOSE(distances(4, 10), (0.95 * 0.95), 1e-5);
- BOOST_REQUIRE(neighbors(3, 10) == 5);
- BOOST_REQUIRE_CLOSE(distances(3, 10), (1.22 * 1.22), 1e-5);
- BOOST_REQUIRE(neighbors(2, 10) == 7);
- BOOST_REQUIRE_CLOSE(distances(2, 10), (2.30 * 2.30), 1e-5);
- BOOST_REQUIRE(neighbors(1, 10) == 6);
- BOOST_REQUIRE_CLOSE(distances(1, 10), (3.00 * 3.00), 1e-5);
- BOOST_REQUIRE(neighbors(0, 10) == 4);
- BOOST_REQUIRE_CLOSE(distances(0, 10), (4.05 * 4.05), 1e-5);
-
- // Clean the memory.
- delete allkfn;
- }
- }
-
- /***
- * Test the dual-tree furthest-neighbors method with the naive method. This
- * uses both a query and reference dataset.
- *
- * Errors are produced if the results are not identical.
- */
- BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_1) {
- arma::mat data_for_tree_;
-
- // Hard-coded filename: bad!
- if (!data::Load("test_data_3_1000.csv", data_for_tree_))
- BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
-
- // Set up matrices to work with.
- arma::mat dual_query(data_for_tree_);
- arma::mat dual_references(data_for_tree_);
- arma::mat naive_query(data_for_tree_);
- arma::mat naive_references(data_for_tree_);
-
- CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
- CLI::GetParam<bool>("neighbor_search/single_mode") = false;
- AllkFN allkfn_(dual_query, dual_references);
-
- CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- AllkFN naive_(naive_query, naive_references);
-
- arma::Mat<size_t> resulting_neighbors_tree;
- arma::mat distances_tree;
- allkfn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
-
- arma::Mat<size_t> resulting_neighbors_naive;
- arma::mat distances_naive;
- naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
-
- for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
- BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- }
- }
-
- /**
- * Test the dual-tree furthest-neighbors method with the naive method. This
- * uses only a reference dataset.
- *
- * Errors are produced if the results are not identical.
- */
- BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_2) {
- arma::mat data_for_tree_;
-
- // Hard-coded filename: bad!
- // Code duplication: also bad!
- if (!data::Load("test_data_3_1000.csv", data_for_tree_))
- BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
-
- // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
- arma::mat dual_references(data_for_tree_);
- arma::mat naive_references(data_for_tree_);
-
- CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
- CLI::GetParam<bool>("neighbor_search/single_mode") = false;
- AllkFN allkfn_(dual_references);
-
- CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- AllkFN naive_(naive_references);
-
- arma::Mat<size_t> resulting_neighbors_tree;
- arma::mat distances_tree;
- allkfn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
-
- arma::Mat<size_t> resulting_neighbors_naive;
- arma::mat distances_naive;
- naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
-
- for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
- BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- }
- }
-
- /**
- * Test the single-tree furthest-neighbors method with the naive method. This
- * uses only a reference dataset.
- *
- * Errors are produced if the results are not identical.
- */
- BOOST_AUTO_TEST_CASE(single_tree_vs_naive) {
- arma::mat data_for_tree_;
-
- // Hard-coded filename: bad!
- // Code duplication: also bad!
- if (!data::Load("test_data_3_1000.csv", data_for_tree_))
- BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
-
- arma::mat single_query(data_for_tree_);
- arma::mat naive_query(data_for_tree_);
-
- CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
- CLI::GetParam<bool>("neighbor_search/single_mode") = true;
- AllkFN allkfn_(single_query);
-
- CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- CLI::GetParam<bool>("neighbor_search/single_mode") = false;
- AllkFN naive_(naive_query);
-
- arma::Mat<size_t> resulting_neighbors_tree;
- arma::mat distances_tree;
- allkfn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
-
- arma::Mat<size_t> resulting_neighbors_naive;
- arma::mat distances_naive;
- naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
-
- for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
- BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- }
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/allkfn_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/allkfn_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/allkfn_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/allkfn_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,443 @@
+/**
+ * @file allkfn_test.cpp
+ *
+ * Tests for AllkFN (all-k-furthest-neighbors).
+ */
+#include <mlpack/core.h>
+#include <mlpack/methods/neighbor_search/neighbor_search.h>
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace mlpack::neighbor;
+
+BOOST_AUTO_TEST_SUITE(AllkFNTest);
+
+/**
+ * Simple furthest-neighbors test with small, synthetic dataset. This is an
+ * exhaustive test, which checks that each method for performing the calculation
+ * (dual-tree, single-tree, naive) produces the correct results. An
+ * eleven-point dataset and the ten furthest neighbors are taken. The dataset
+ * is in one dimension for simplicity -- the correct functionality of distance
+ * functions is not tested here.
+ */
+BOOST_AUTO_TEST_CASE(exhaustive_synthetic_test)
+{
+ // Set up our data.
+ arma::mat data(1, 11);
+ data[0] = 0.05; // Row addressing is unnecessary (they are all 0).
+ data[1] = 0.35;
+ data[2] = 0.15;
+ data[3] = 1.25;
+ data[4] = 5.05;
+ data[5] = -0.22;
+ data[6] = -2.00;
+ data[7] = -1.30;
+ data[8] = 0.45;
+ data[9] = 0.90;
+ data[10] = 1.00;
+
+ // We will loop through three times, one for each method of performing the
+ // calculation. We'll always use 10 neighbors, so set that parameter.
+ CLI::GetParam<int>("neighbor_search/k") = 10;
+ for (int i = 0; i < 3; i++)
+ {
+ AllkFN* allkfn;
+ arma::mat data_mutable = data;
+ switch(i)
+ {
+ case 0: // Use the dual-tree method.
+ allkfn = new AllkFN(data_mutable);
+ break;
+ case 1: // Use the single-tree method.
+ CLI::GetParam<bool>("neighbor_search/single_mode") = true;
+ allkfn = new AllkFN(data_mutable);
+ break;
+ case 2: // Use the naive method.
+ CLI::GetParam<bool>("neighbor_search/single_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
+ allkfn = new AllkFN(data_mutable);
+ break;
+ }
+
+ // Now perform the actual calculation.
+ arma::Mat<size_t> neighbors;
+ arma::mat distances;
+ allkfn->ComputeNeighbors(neighbors, distances);
+
+ // Now the exhaustive check for correctness. This will be long. We must
+ // also remember that the distances returned are squared distances. As a
+ // result, distance comparisons are written out as (distance * distance) for
+ // readability.
+
+ // Neighbors of point 0.
+ BOOST_REQUIRE(neighbors(9, 0) == 2);
+ BOOST_REQUIRE_CLOSE(distances(9, 0), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 0) == 5);
+ BOOST_REQUIRE_CLOSE(distances(8, 0), (0.27 * 0.27), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 0) == 1);
+ BOOST_REQUIRE_CLOSE(distances(7, 0), (0.30 * 0.30), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 0) == 8);
+ BOOST_REQUIRE_CLOSE(distances(6, 0), (0.40 * 0.40), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 0) == 9);
+ BOOST_REQUIRE_CLOSE(distances(5, 0), (0.85 * 0.85), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 0) == 10);
+ BOOST_REQUIRE_CLOSE(distances(4, 0), (0.95 * 0.95), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 0) == 3);
+ BOOST_REQUIRE_CLOSE(distances(3, 0), (1.20 * 1.20), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 0) == 7);
+ BOOST_REQUIRE_CLOSE(distances(2, 0), (1.35 * 1.35), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 0) == 6);
+ BOOST_REQUIRE_CLOSE(distances(1, 0), (2.05 * 2.05), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 0) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 0), (5.00 * 5.00), 1e-5);
+
+ // Neighbors of point 1.
+ BOOST_REQUIRE(neighbors(9, 1) == 8);
+ BOOST_REQUIRE_CLOSE(distances(9, 1), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 1) == 2);
+ BOOST_REQUIRE_CLOSE(distances(8, 1), (0.20 * 0.20), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 1) == 0);
+ BOOST_REQUIRE_CLOSE(distances(7, 1), (0.30 * 0.30), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 1) == 9);
+ BOOST_REQUIRE_CLOSE(distances(6, 1), (0.55 * 0.55), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 1) == 5);
+ BOOST_REQUIRE_CLOSE(distances(5, 1), (0.57 * 0.57), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 1) == 10);
+ BOOST_REQUIRE_CLOSE(distances(4, 1), (0.65 * 0.65), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 1) == 3);
+ BOOST_REQUIRE_CLOSE(distances(3, 1), (0.90 * 0.90), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 1) == 7);
+ BOOST_REQUIRE_CLOSE(distances(2, 1), (1.65 * 1.65), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 1) == 6);
+ BOOST_REQUIRE_CLOSE(distances(1, 1), (2.35 * 2.35), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 1) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 1), (4.70 * 4.70), 1e-5);
+
+ // Neighbors of point 2.
+ BOOST_REQUIRE(neighbors(9, 2) == 0);
+ BOOST_REQUIRE_CLOSE(distances(9, 2), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 2) == 1);
+ BOOST_REQUIRE_CLOSE(distances(8, 2), (0.20 * 0.20), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 2) == 8);
+ BOOST_REQUIRE_CLOSE(distances(7, 2), (0.30 * 0.30), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 2) == 5);
+ BOOST_REQUIRE_CLOSE(distances(6, 2), (0.37 * 0.37), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 2) == 9);
+ BOOST_REQUIRE_CLOSE(distances(5, 2), (0.75 * 0.75), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 2) == 10);
+ BOOST_REQUIRE_CLOSE(distances(4, 2), (0.85 * 0.85), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 2) == 3);
+ BOOST_REQUIRE_CLOSE(distances(3, 2), (1.10 * 1.10), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 2) == 7);
+ BOOST_REQUIRE_CLOSE(distances(2, 2), (1.45 * 1.45), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 2) == 6);
+ BOOST_REQUIRE_CLOSE(distances(1, 2), (2.15 * 2.15), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 2) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 2), (4.90 * 4.90), 1e-5);
+
+ // Neighbors of point 3.
+ BOOST_REQUIRE(neighbors(9, 3) == 10);
+ BOOST_REQUIRE_CLOSE(distances(9, 3), (0.25 * 0.25), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 3) == 9);
+ BOOST_REQUIRE_CLOSE(distances(8, 3), (0.35 * 0.35), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 3) == 8);
+ BOOST_REQUIRE_CLOSE(distances(7, 3), (0.80 * 0.80), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 3) == 1);
+ BOOST_REQUIRE_CLOSE(distances(6, 3), (0.90 * 0.90), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 3) == 2);
+ BOOST_REQUIRE_CLOSE(distances(5, 3), (1.10 * 1.10), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 3) == 0);
+ BOOST_REQUIRE_CLOSE(distances(4, 3), (1.20 * 1.20), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 3) == 5);
+ BOOST_REQUIRE_CLOSE(distances(3, 3), (1.47 * 1.47), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 3) == 7);
+ BOOST_REQUIRE_CLOSE(distances(2, 3), (2.55 * 2.55), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 3) == 6);
+ BOOST_REQUIRE_CLOSE(distances(1, 3), (3.25 * 3.25), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 3) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 3), (3.80 * 3.80), 1e-5);
+
+ // Neighbors of point 4.
+ BOOST_REQUIRE(neighbors(9, 4) == 3);
+ BOOST_REQUIRE_CLOSE(distances(9, 4), (3.80 * 3.80), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 4) == 10);
+ BOOST_REQUIRE_CLOSE(distances(8, 4), (4.05 * 4.05), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 4) == 9);
+ BOOST_REQUIRE_CLOSE(distances(7, 4), (4.15 * 4.15), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 4) == 8);
+ BOOST_REQUIRE_CLOSE(distances(6, 4), (4.60 * 4.60), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 4) == 1);
+ BOOST_REQUIRE_CLOSE(distances(5, 4), (4.70 * 4.70), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 4) == 2);
+ BOOST_REQUIRE_CLOSE(distances(4, 4), (4.90 * 4.90), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 4) == 0);
+ BOOST_REQUIRE_CLOSE(distances(3, 4), (5.00 * 5.00), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 4) == 5);
+ BOOST_REQUIRE_CLOSE(distances(2, 4), (5.27 * 5.27), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 4) == 7);
+ BOOST_REQUIRE_CLOSE(distances(1, 4), (6.35 * 6.35), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 4) == 6);
+ BOOST_REQUIRE_CLOSE(distances(0, 4), (7.05 * 7.05), 1e-5);
+
+ // Neighbors of point 5.
+ BOOST_REQUIRE(neighbors(9, 5) == 0);
+ BOOST_REQUIRE_CLOSE(distances(9, 5), (0.27 * 0.27), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 5) == 2);
+ BOOST_REQUIRE_CLOSE(distances(8, 5), (0.37 * 0.37), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 5) == 1);
+ BOOST_REQUIRE_CLOSE(distances(7, 5), (0.57 * 0.57), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 5) == 8);
+ BOOST_REQUIRE_CLOSE(distances(6, 5), (0.67 * 0.67), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 5) == 7);
+ BOOST_REQUIRE_CLOSE(distances(5, 5), (1.08 * 1.08), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 5) == 9);
+ BOOST_REQUIRE_CLOSE(distances(4, 5), (1.12 * 1.12), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 5) == 10);
+ BOOST_REQUIRE_CLOSE(distances(3, 5), (1.22 * 1.22), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 5) == 3);
+ BOOST_REQUIRE_CLOSE(distances(2, 5), (1.47 * 1.47), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 5) == 6);
+ BOOST_REQUIRE_CLOSE(distances(1, 5), (1.78 * 1.78), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 5) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 5), (5.27 * 5.27), 1e-5);
+
+ // Neighbors of point 6.
+ BOOST_REQUIRE(neighbors(9, 6) == 7);
+ BOOST_REQUIRE_CLOSE(distances(9, 6), (0.70 * 0.70), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 6) == 5);
+ BOOST_REQUIRE_CLOSE(distances(8, 6), (1.78 * 1.78), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 6) == 0);
+ BOOST_REQUIRE_CLOSE(distances(7, 6), (2.05 * 2.05), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 6) == 2);
+ BOOST_REQUIRE_CLOSE(distances(6, 6), (2.15 * 2.15), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 6) == 1);
+ BOOST_REQUIRE_CLOSE(distances(5, 6), (2.35 * 2.35), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 6) == 8);
+ BOOST_REQUIRE_CLOSE(distances(4, 6), (2.45 * 2.45), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 6) == 9);
+ BOOST_REQUIRE_CLOSE(distances(3, 6), (2.90 * 2.90), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 6) == 10);
+ BOOST_REQUIRE_CLOSE(distances(2, 6), (3.00 * 3.00), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 6) == 3);
+ BOOST_REQUIRE_CLOSE(distances(1, 6), (3.25 * 3.25), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 6) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 6), (7.05 * 7.05), 1e-5);
+
+ // Neighbors of point 7.
+ BOOST_REQUIRE(neighbors(9, 7) == 6);
+ BOOST_REQUIRE_CLOSE(distances(9, 7), (0.70 * 0.70), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 7) == 5);
+ BOOST_REQUIRE_CLOSE(distances(8, 7), (1.08 * 1.08), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 7) == 0);
+ BOOST_REQUIRE_CLOSE(distances(7, 7), (1.35 * 1.35), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 7) == 2);
+ BOOST_REQUIRE_CLOSE(distances(6, 7), (1.45 * 1.45), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 7) == 1);
+ BOOST_REQUIRE_CLOSE(distances(5, 7), (1.65 * 1.65), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 7) == 8);
+ BOOST_REQUIRE_CLOSE(distances(4, 7), (1.75 * 1.75), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 7) == 9);
+ BOOST_REQUIRE_CLOSE(distances(3, 7), (2.20 * 2.20), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 7) == 10);
+ BOOST_REQUIRE_CLOSE(distances(2, 7), (2.30 * 2.30), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 7) == 3);
+ BOOST_REQUIRE_CLOSE(distances(1, 7), (2.55 * 2.55), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 7) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 7), (6.35 * 6.35), 1e-5);
+
+ // Neighbors of point 8.
+ BOOST_REQUIRE(neighbors(9, 8) == 1);
+ BOOST_REQUIRE_CLOSE(distances(9, 8), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 8) == 2);
+ BOOST_REQUIRE_CLOSE(distances(8, 8), (0.30 * 0.30), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 8) == 0);
+ BOOST_REQUIRE_CLOSE(distances(7, 8), (0.40 * 0.40), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 8) == 9);
+ BOOST_REQUIRE_CLOSE(distances(6, 8), (0.45 * 0.45), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 8) == 10);
+ BOOST_REQUIRE_CLOSE(distances(5, 8), (0.55 * 0.55), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 8) == 5);
+ BOOST_REQUIRE_CLOSE(distances(4, 8), (0.67 * 0.67), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 8) == 3);
+ BOOST_REQUIRE_CLOSE(distances(3, 8), (0.80 * 0.80), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 8) == 7);
+ BOOST_REQUIRE_CLOSE(distances(2, 8), (1.75 * 1.75), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 8) == 6);
+ BOOST_REQUIRE_CLOSE(distances(1, 8), (2.45 * 2.45), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 8) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 8), (4.60 * 4.60), 1e-5);
+
+ // Neighbors of point 9.
+ BOOST_REQUIRE(neighbors(9, 9) == 10);
+ BOOST_REQUIRE_CLOSE(distances(9, 9), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 9) == 3);
+ BOOST_REQUIRE_CLOSE(distances(8, 9), (0.35 * 0.35), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 9) == 8);
+ BOOST_REQUIRE_CLOSE(distances(7, 9), (0.45 * 0.45), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 9) == 1);
+ BOOST_REQUIRE_CLOSE(distances(6, 9), (0.55 * 0.55), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 9) == 2);
+ BOOST_REQUIRE_CLOSE(distances(5, 9), (0.75 * 0.75), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 9) == 0);
+ BOOST_REQUIRE_CLOSE(distances(4, 9), (0.85 * 0.85), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 9) == 5);
+ BOOST_REQUIRE_CLOSE(distances(3, 9), (1.12 * 1.12), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 9) == 7);
+ BOOST_REQUIRE_CLOSE(distances(2, 9), (2.20 * 2.20), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 9) == 6);
+ BOOST_REQUIRE_CLOSE(distances(1, 9), (2.90 * 2.90), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 9) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 9), (4.15 * 4.15), 1e-5);
+
+ // Neighbors of point 10.
+ BOOST_REQUIRE(neighbors(9, 10) == 9);
+ BOOST_REQUIRE_CLOSE(distances(9, 10), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 10) == 3);
+ BOOST_REQUIRE_CLOSE(distances(8, 10), (0.25 * 0.25), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 10) == 8);
+ BOOST_REQUIRE_CLOSE(distances(7, 10), (0.55 * 0.55), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 10) == 1);
+ BOOST_REQUIRE_CLOSE(distances(6, 10), (0.65 * 0.65), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 10) == 2);
+ BOOST_REQUIRE_CLOSE(distances(5, 10), (0.85 * 0.85), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 10) == 0);
+ BOOST_REQUIRE_CLOSE(distances(4, 10), (0.95 * 0.95), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 10) == 5);
+ BOOST_REQUIRE_CLOSE(distances(3, 10), (1.22 * 1.22), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 10) == 7);
+ BOOST_REQUIRE_CLOSE(distances(2, 10), (2.30 * 2.30), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 10) == 6);
+ BOOST_REQUIRE_CLOSE(distances(1, 10), (3.00 * 3.00), 1e-5);
+ BOOST_REQUIRE(neighbors(0, 10) == 4);
+ BOOST_REQUIRE_CLOSE(distances(0, 10), (4.05 * 4.05), 1e-5);
+
+ // Clean the memory.
+ delete allkfn;
+ }
+}
+
+/**
+ * Test the dual-tree furthest-neighbors method with the naive method. This
+ * uses both a query and reference dataset.
+ *
+ * Errors are produced if the results are not identical.
+ */
+BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_1)
+{
+ arma::mat data_for_tree_;
+
+ // Hard-coded filename: bad!
+ if (!data::Load("test_data_3_1000.csv", data_for_tree_))
+ BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+
+ // Set up matrices to work with.
+ arma::mat dual_query(data_for_tree_);
+ arma::mat dual_references(data_for_tree_);
+ arma::mat naive_query(data_for_tree_);
+ arma::mat naive_references(data_for_tree_);
+
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/single_mode") = false;
+ AllkFN allkfn_(dual_query, dual_references);
+
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
+ AllkFN naive_(naive_query, naive_references);
+
+ arma::Mat<size_t> resulting_neighbors_tree;
+ arma::mat distances_tree;
+ allkfn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+
+ arma::Mat<size_t> resulting_neighbors_naive;
+ arma::mat distances_naive;
+ naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+
+ for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++)
+ {
+ BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+ BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+ }
+}
+
+/**
+ * Test the dual-tree furthest-neighbors method with the naive method. This
+ * uses only a reference dataset.
+ *
+ * Errors are produced if the results are not identical.
+ */
+BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_2)
+{
+ arma::mat data_for_tree_;
+
+ // Hard-coded filename: bad!
+ // Code duplication: also bad!
+ if (!data::Load("test_data_3_1000.csv", data_for_tree_))
+ BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+
+ // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
+ arma::mat dual_references(data_for_tree_);
+ arma::mat naive_references(data_for_tree_);
+
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/single_mode") = false;
+ AllkFN allkfn_(dual_references);
+
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
+ AllkFN naive_(naive_references);
+
+ arma::Mat<size_t> resulting_neighbors_tree;
+ arma::mat distances_tree;
+ allkfn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+
+ arma::Mat<size_t> resulting_neighbors_naive;
+ arma::mat distances_naive;
+ naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+
+ for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++)
+ {
+ BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+ BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+ }
+}
+
+/**
+ * Test the single-tree furthest-neighbors method with the naive method. This
+ * uses only a reference dataset.
+ *
+ * Errors are produced if the results are not identical.
+ */
+BOOST_AUTO_TEST_CASE(single_tree_vs_naive)
+{
+ arma::mat data_for_tree_;
+
+ // Hard-coded filename: bad!
+ // Code duplication: also bad!
+ if (!data::Load("test_data_3_1000.csv", data_for_tree_))
+ BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+
+ arma::mat single_query(data_for_tree_);
+ arma::mat naive_query(data_for_tree_);
+
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/single_mode") = true;
+ AllkFN allkfn_(single_query);
+
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
+ CLI::GetParam<bool>("neighbor_search/single_mode") = false;
+ AllkFN naive_(naive_query);
+
+ arma::Mat<size_t> resulting_neighbors_tree;
+ arma::mat distances_tree;
+ allkfn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+
+ arma::Mat<size_t> resulting_neighbors_naive;
+ arma::mat distances_naive;
+ naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+
+ for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++)
+ {
+ BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+ BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+ }
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/allknn_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/allknn_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/allknn_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,442 +0,0 @@
-/**
- * @file allknn_test.cc
- *
- * Test file for AllkNN class
- */
-#include <mlpack/core.h>
-#include <mlpack/methods/neighbor_search/neighbor_search.h>
-#include <boost/test/unit_test.hpp>
-
-using namespace mlpack;
-using namespace mlpack::neighbor;
-
-BOOST_AUTO_TEST_SUITE(AllKnnTest);
-
- /***
- * Simple nearest-neighbors test with small, synthetic dataset. This is an
- * exhaustive test, which checks that each method for performing the calculation
- * (dual-tree, single-tree, naive) produces the correct results. An
- * eleven-point dataset and the ten nearest neighbors are taken. The dataset is
- * in one dimension for simplicity -- the correct functionality of distance
- * functions is not tested here.
- */
- BOOST_AUTO_TEST_CASE(exhaustive_synthetic_test) {
- // Set up our data.
- arma::mat data(1, 11);
- data[0] = 0.05; // Row addressing is unnecessary (they are all 0).
- data[1] = 0.35;
- data[2] = 0.15;
- data[3] = 1.25;
- data[4] = 5.05;
- data[5] = -0.22;
- data[6] = -2.00;
- data[7] = -1.30;
- data[8] = 0.45;
- data[9] = 0.90;
- data[10] = 1.00;
-
- // We will loop through three times, one for each method of performing the
- // calculation. We'll always use 10 neighbors, so set that parameter.
- CLI::GetParam<int>("neighbor_search/k") = 10;
- for (int i = 0; i < 3; i++) {
- AllkNN* allknn;
- arma::mat data_mutable = data;
- switch(i) {
- case 0: // Use the dual-tree method.
- allknn = new AllkNN(data_mutable);
- break;
- case 1: // Use the single-tree method.
- CLI::GetParam<bool>("neighbor_search/single_mode") = true;
- allknn = new AllkNN(data_mutable);
- break;
- case 2: // Use the naive method.
- CLI::GetParam<bool>("neighbor_search/single_mode") = false;
- CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- allknn = new AllkNN(data_mutable);
- break;
- }
-
- // Now perform the actual calculation.
- arma::Mat<size_t> neighbors;
- arma::mat distances;
- allknn->ComputeNeighbors(neighbors, distances);
-
- // Now the exhaustive check for correctness. This will be long. We must
- // also remember that the distances returned are squared distances. As a
- // result, distance comparisons are written out as (distance * distance) for
- // readability.
-
- // Neighbors of point 0.
- BOOST_REQUIRE(neighbors(0, 0) == 2);
- BOOST_REQUIRE_CLOSE(distances(0, 0), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(1, 0) == 5);
- BOOST_REQUIRE_CLOSE(distances(1, 0), (0.27 * 0.27), 1e-5);
- BOOST_REQUIRE(neighbors(2, 0) == 1);
- BOOST_REQUIRE_CLOSE(distances(2, 0), (0.30 * 0.30), 1e-5);
- BOOST_REQUIRE(neighbors(3, 0) == 8);
- BOOST_REQUIRE_CLOSE(distances(3, 0), (0.40 * 0.40), 1e-5);
- BOOST_REQUIRE(neighbors(4, 0) == 9);
- BOOST_REQUIRE_CLOSE(distances(4, 0), (0.85 * 0.85), 1e-5);
- BOOST_REQUIRE(neighbors(5, 0) == 10);
- BOOST_REQUIRE_CLOSE(distances(5, 0), (0.95 * 0.95), 1e-5);
- BOOST_REQUIRE(neighbors(6, 0) == 3);
- BOOST_REQUIRE_CLOSE(distances(6, 0), (1.20 * 1.20), 1e-5);
- BOOST_REQUIRE(neighbors(7, 0) == 7);
- BOOST_REQUIRE_CLOSE(distances(7, 0), (1.35 * 1.35), 1e-5);
- BOOST_REQUIRE(neighbors(8, 0) == 6);
- BOOST_REQUIRE_CLOSE(distances(8, 0), (2.05 * 2.05), 1e-5);
- BOOST_REQUIRE(neighbors(9, 0) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 0), (5.00 * 5.00), 1e-5);
-
- // Neighbors of point 1.
- BOOST_REQUIRE(neighbors(0, 1) == 8);
- BOOST_REQUIRE_CLOSE(distances(0, 1), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(1, 1) == 2);
- BOOST_REQUIRE_CLOSE(distances(1, 1), (0.20 * 0.20), 1e-5);
- BOOST_REQUIRE(neighbors(2, 1) == 0);
- BOOST_REQUIRE_CLOSE(distances(2, 1), (0.30 * 0.30), 1e-5);
- BOOST_REQUIRE(neighbors(3, 1) == 9);
- BOOST_REQUIRE_CLOSE(distances(3, 1), (0.55 * 0.55), 1e-5);
- BOOST_REQUIRE(neighbors(4, 1) == 5);
- BOOST_REQUIRE_CLOSE(distances(4, 1), (0.57 * 0.57), 1e-5);
- BOOST_REQUIRE(neighbors(5, 1) == 10);
- BOOST_REQUIRE_CLOSE(distances(5, 1), (0.65 * 0.65), 1e-5);
- BOOST_REQUIRE(neighbors(6, 1) == 3);
- BOOST_REQUIRE_CLOSE(distances(6, 1), (0.90 * 0.90), 1e-5);
- BOOST_REQUIRE(neighbors(7, 1) == 7);
- BOOST_REQUIRE_CLOSE(distances(7, 1), (1.65 * 1.65), 1e-5);
- BOOST_REQUIRE(neighbors(8, 1) == 6);
- BOOST_REQUIRE_CLOSE(distances(8, 1), (2.35 * 2.35), 1e-5);
- BOOST_REQUIRE(neighbors(9, 1) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 1), (4.70 * 4.70), 1e-5);
-
- // Neighbors of point 2.
- BOOST_REQUIRE(neighbors(0, 2) == 0);
- BOOST_REQUIRE_CLOSE(distances(0, 2), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(1, 2) == 1);
- BOOST_REQUIRE_CLOSE(distances(1, 2), (0.20 * 0.20), 1e-5);
- BOOST_REQUIRE(neighbors(2, 2) == 8);
- BOOST_REQUIRE_CLOSE(distances(2, 2), (0.30 * 0.30), 1e-5);
- BOOST_REQUIRE(neighbors(3, 2) == 5);
- BOOST_REQUIRE_CLOSE(distances(3, 2), (0.37 * 0.37), 1e-5);
- BOOST_REQUIRE(neighbors(4, 2) == 9);
- BOOST_REQUIRE_CLOSE(distances(4, 2), (0.75 * 0.75), 1e-5);
- BOOST_REQUIRE(neighbors(5, 2) == 10);
- BOOST_REQUIRE_CLOSE(distances(5, 2), (0.85 * 0.85), 1e-5);
- BOOST_REQUIRE(neighbors(6, 2) == 3);
- BOOST_REQUIRE_CLOSE(distances(6, 2), (1.10 * 1.10), 1e-5);
- BOOST_REQUIRE(neighbors(7, 2) == 7);
- BOOST_REQUIRE_CLOSE(distances(7, 2), (1.45 * 1.45), 1e-5);
- BOOST_REQUIRE(neighbors(8, 2) == 6);
- BOOST_REQUIRE_CLOSE(distances(8, 2), (2.15 * 2.15), 1e-5);
- BOOST_REQUIRE(neighbors(9, 2) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 2), (4.90 * 4.90), 1e-5);
-
- // Neighbors of point 3.
- BOOST_REQUIRE(neighbors(0, 3) == 10);
- BOOST_REQUIRE_CLOSE(distances(0, 3), (0.25 * 0.25), 1e-5);
- BOOST_REQUIRE(neighbors(1, 3) == 9);
- BOOST_REQUIRE_CLOSE(distances(1, 3), (0.35 * 0.35), 1e-5);
- BOOST_REQUIRE(neighbors(2, 3) == 8);
- BOOST_REQUIRE_CLOSE(distances(2, 3), (0.80 * 0.80), 1e-5);
- BOOST_REQUIRE(neighbors(3, 3) == 1);
- BOOST_REQUIRE_CLOSE(distances(3, 3), (0.90 * 0.90), 1e-5);
- BOOST_REQUIRE(neighbors(4, 3) == 2);
- BOOST_REQUIRE_CLOSE(distances(4, 3), (1.10 * 1.10), 1e-5);
- BOOST_REQUIRE(neighbors(5, 3) == 0);
- BOOST_REQUIRE_CLOSE(distances(5, 3), (1.20 * 1.20), 1e-5);
- BOOST_REQUIRE(neighbors(6, 3) == 5);
- BOOST_REQUIRE_CLOSE(distances(6, 3), (1.47 * 1.47), 1e-5);
- BOOST_REQUIRE(neighbors(7, 3) == 7);
- BOOST_REQUIRE_CLOSE(distances(7, 3), (2.55 * 2.55), 1e-5);
- BOOST_REQUIRE(neighbors(8, 3) == 6);
- BOOST_REQUIRE_CLOSE(distances(8, 3), (3.25 * 3.25), 1e-5);
- BOOST_REQUIRE(neighbors(9, 3) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 3), (3.80 * 3.80), 1e-5);
-
- // Neighbors of point 4.
- BOOST_REQUIRE(neighbors(0, 4) == 3);
- BOOST_REQUIRE_CLOSE(distances(0, 4), (3.80 * 3.80), 1e-5);
- BOOST_REQUIRE(neighbors(1, 4) == 10);
- BOOST_REQUIRE_CLOSE(distances(1, 4), (4.05 * 4.05), 1e-5);
- BOOST_REQUIRE(neighbors(2, 4) == 9);
- BOOST_REQUIRE_CLOSE(distances(2, 4), (4.15 * 4.15), 1e-5);
- BOOST_REQUIRE(neighbors(3, 4) == 8);
- BOOST_REQUIRE_CLOSE(distances(3, 4), (4.60 * 4.60), 1e-5);
- BOOST_REQUIRE(neighbors(4, 4) == 1);
- BOOST_REQUIRE_CLOSE(distances(4, 4), (4.70 * 4.70), 1e-5);
- BOOST_REQUIRE(neighbors(5, 4) == 2);
- BOOST_REQUIRE_CLOSE(distances(5, 4), (4.90 * 4.90), 1e-5);
- BOOST_REQUIRE(neighbors(6, 4) == 0);
- BOOST_REQUIRE_CLOSE(distances(6, 4), (5.00 * 5.00), 1e-5);
- BOOST_REQUIRE(neighbors(7, 4) == 5);
- BOOST_REQUIRE_CLOSE(distances(7, 4), (5.27 * 5.27), 1e-5);
- BOOST_REQUIRE(neighbors(8, 4) == 7);
- BOOST_REQUIRE_CLOSE(distances(8, 4), (6.35 * 6.35), 1e-5);
- BOOST_REQUIRE(neighbors(9, 4) == 6);
- BOOST_REQUIRE_CLOSE(distances(9, 4), (7.05 * 7.05), 1e-5);
-
- // Neighbors of point 5.
- BOOST_REQUIRE(neighbors(0, 5) == 0);
- BOOST_REQUIRE_CLOSE(distances(0, 5), (0.27 * 0.27), 1e-5);
- BOOST_REQUIRE(neighbors(1, 5) == 2);
- BOOST_REQUIRE_CLOSE(distances(1, 5), (0.37 * 0.37), 1e-5);
- BOOST_REQUIRE(neighbors(2, 5) == 1);
- BOOST_REQUIRE_CLOSE(distances(2, 5), (0.57 * 0.57), 1e-5);
- BOOST_REQUIRE(neighbors(3, 5) == 8);
- BOOST_REQUIRE_CLOSE(distances(3, 5), (0.67 * 0.67), 1e-5);
- BOOST_REQUIRE(neighbors(4, 5) == 7);
- BOOST_REQUIRE_CLOSE(distances(4, 5), (1.08 * 1.08), 1e-5);
- BOOST_REQUIRE(neighbors(5, 5) == 9);
- BOOST_REQUIRE_CLOSE(distances(5, 5), (1.12 * 1.12), 1e-5);
- BOOST_REQUIRE(neighbors(6, 5) == 10);
- BOOST_REQUIRE_CLOSE(distances(6, 5), (1.22 * 1.22), 1e-5);
- BOOST_REQUIRE(neighbors(7, 5) == 3);
- BOOST_REQUIRE_CLOSE(distances(7, 5), (1.47 * 1.47), 1e-5);
- BOOST_REQUIRE(neighbors(8, 5) == 6);
- BOOST_REQUIRE_CLOSE(distances(8, 5), (1.78 * 1.78), 1e-5);
- BOOST_REQUIRE(neighbors(9, 5) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 5), (5.27 * 5.27), 1e-5);
-
- // Neighbors of point 6.
- BOOST_REQUIRE(neighbors(0, 6) == 7);
- BOOST_REQUIRE_CLOSE(distances(0, 6), (0.70 * 0.70), 1e-5);
- BOOST_REQUIRE(neighbors(1, 6) == 5);
- BOOST_REQUIRE_CLOSE(distances(1, 6), (1.78 * 1.78), 1e-5);
- BOOST_REQUIRE(neighbors(2, 6) == 0);
- BOOST_REQUIRE_CLOSE(distances(2, 6), (2.05 * 2.05), 1e-5);
- BOOST_REQUIRE(neighbors(3, 6) == 2);
- BOOST_REQUIRE_CLOSE(distances(3, 6), (2.15 * 2.15), 1e-5);
- BOOST_REQUIRE(neighbors(4, 6) == 1);
- BOOST_REQUIRE_CLOSE(distances(4, 6), (2.35 * 2.35), 1e-5);
- BOOST_REQUIRE(neighbors(5, 6) == 8);
- BOOST_REQUIRE_CLOSE(distances(5, 6), (2.45 * 2.45), 1e-5);
- BOOST_REQUIRE(neighbors(6, 6) == 9);
- BOOST_REQUIRE_CLOSE(distances(6, 6), (2.90 * 2.90), 1e-5);
- BOOST_REQUIRE(neighbors(7, 6) == 10);
- BOOST_REQUIRE_CLOSE(distances(7, 6), (3.00 * 3.00), 1e-5);
- BOOST_REQUIRE(neighbors(8, 6) == 3);
- BOOST_REQUIRE_CLOSE(distances(8, 6), (3.25 * 3.25), 1e-5);
- BOOST_REQUIRE(neighbors(9, 6) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 6), (7.05 * 7.05), 1e-5);
-
- // Neighbors of point 7.
- BOOST_REQUIRE(neighbors(0, 7) == 6);
- BOOST_REQUIRE_CLOSE(distances(0, 7), (0.70 * 0.70), 1e-5);
- BOOST_REQUIRE(neighbors(1, 7) == 5);
- BOOST_REQUIRE_CLOSE(distances(1, 7), (1.08 * 1.08), 1e-5);
- BOOST_REQUIRE(neighbors(2, 7) == 0);
- BOOST_REQUIRE_CLOSE(distances(2, 7), (1.35 * 1.35), 1e-5);
- BOOST_REQUIRE(neighbors(3, 7) == 2);
- BOOST_REQUIRE_CLOSE(distances(3, 7), (1.45 * 1.45), 1e-5);
- BOOST_REQUIRE(neighbors(4, 7) == 1);
- BOOST_REQUIRE_CLOSE(distances(4, 7), (1.65 * 1.65), 1e-5);
- BOOST_REQUIRE(neighbors(5, 7) == 8);
- BOOST_REQUIRE_CLOSE(distances(5, 7), (1.75 * 1.75), 1e-5);
- BOOST_REQUIRE(neighbors(6, 7) == 9);
- BOOST_REQUIRE_CLOSE(distances(6, 7), (2.20 * 2.20), 1e-5);
- BOOST_REQUIRE(neighbors(7, 7) == 10);
- BOOST_REQUIRE_CLOSE(distances(7, 7), (2.30 * 2.30), 1e-5);
- BOOST_REQUIRE(neighbors(8, 7) == 3);
- BOOST_REQUIRE_CLOSE(distances(8, 7), (2.55 * 2.55), 1e-5);
- BOOST_REQUIRE(neighbors(9, 7) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 7), (6.35 * 6.35), 1e-5);
-
- // Neighbors of point 8.
- BOOST_REQUIRE(neighbors(0, 8) == 1);
- BOOST_REQUIRE_CLOSE(distances(0, 8), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(1, 8) == 2);
- BOOST_REQUIRE_CLOSE(distances(1, 8), (0.30 * 0.30), 1e-5);
- BOOST_REQUIRE(neighbors(2, 8) == 0);
- BOOST_REQUIRE_CLOSE(distances(2, 8), (0.40 * 0.40), 1e-5);
- BOOST_REQUIRE(neighbors(3, 8) == 9);
- BOOST_REQUIRE_CLOSE(distances(3, 8), (0.45 * 0.45), 1e-5);
- BOOST_REQUIRE(neighbors(4, 8) == 10);
- BOOST_REQUIRE_CLOSE(distances(4, 8), (0.55 * 0.55), 1e-5);
- BOOST_REQUIRE(neighbors(5, 8) == 5);
- BOOST_REQUIRE_CLOSE(distances(5, 8), (0.67 * 0.67), 1e-5);
- BOOST_REQUIRE(neighbors(6, 8) == 3);
- BOOST_REQUIRE_CLOSE(distances(6, 8), (0.80 * 0.80), 1e-5);
- BOOST_REQUIRE(neighbors(7, 8) == 7);
- BOOST_REQUIRE_CLOSE(distances(7, 8), (1.75 * 1.75), 1e-5);
- BOOST_REQUIRE(neighbors(8, 8) == 6);
- BOOST_REQUIRE_CLOSE(distances(8, 8), (2.45 * 2.45), 1e-5);
- BOOST_REQUIRE(neighbors(9, 8) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 8), (4.60 * 4.60), 1e-5);
-
- // Neighbors of point 9.
- BOOST_REQUIRE(neighbors(0, 9) == 10);
- BOOST_REQUIRE_CLOSE(distances(0, 9), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(1, 9) == 3);
- BOOST_REQUIRE_CLOSE(distances(1, 9), (0.35 * 0.35), 1e-5);
- BOOST_REQUIRE(neighbors(2, 9) == 8);
- BOOST_REQUIRE_CLOSE(distances(2, 9), (0.45 * 0.45), 1e-5);
- BOOST_REQUIRE(neighbors(3, 9) == 1);
- BOOST_REQUIRE_CLOSE(distances(3, 9), (0.55 * 0.55), 1e-5);
- BOOST_REQUIRE(neighbors(4, 9) == 2);
- BOOST_REQUIRE_CLOSE(distances(4, 9), (0.75 * 0.75), 1e-5);
- BOOST_REQUIRE(neighbors(5, 9) == 0);
- BOOST_REQUIRE_CLOSE(distances(5, 9), (0.85 * 0.85), 1e-5);
- BOOST_REQUIRE(neighbors(6, 9) == 5);
- BOOST_REQUIRE_CLOSE(distances(6, 9), (1.12 * 1.12), 1e-5);
- BOOST_REQUIRE(neighbors(7, 9) == 7);
- BOOST_REQUIRE_CLOSE(distances(7, 9), (2.20 * 2.20), 1e-5);
- BOOST_REQUIRE(neighbors(8, 9) == 6);
- BOOST_REQUIRE_CLOSE(distances(8, 9), (2.90 * 2.90), 1e-5);
- BOOST_REQUIRE(neighbors(9, 9) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 9), (4.15 * 4.15), 1e-5);
-
- // Neighbors of point 10.
- BOOST_REQUIRE(neighbors(0, 10) == 9);
- BOOST_REQUIRE_CLOSE(distances(0, 10), (0.10 * 0.10), 1e-5);
- BOOST_REQUIRE(neighbors(1, 10) == 3);
- BOOST_REQUIRE_CLOSE(distances(1, 10), (0.25 * 0.25), 1e-5);
- BOOST_REQUIRE(neighbors(2, 10) == 8);
- BOOST_REQUIRE_CLOSE(distances(2, 10), (0.55 * 0.55), 1e-5);
- BOOST_REQUIRE(neighbors(3, 10) == 1);
- BOOST_REQUIRE_CLOSE(distances(3, 10), (0.65 * 0.65), 1e-5);
- BOOST_REQUIRE(neighbors(4, 10) == 2);
- BOOST_REQUIRE_CLOSE(distances(4, 10), (0.85 * 0.85), 1e-5);
- BOOST_REQUIRE(neighbors(5, 10) == 0);
- BOOST_REQUIRE_CLOSE(distances(5, 10), (0.95 * 0.95), 1e-5);
- BOOST_REQUIRE(neighbors(6, 10) == 5);
- BOOST_REQUIRE_CLOSE(distances(6, 10), (1.22 * 1.22), 1e-5);
- BOOST_REQUIRE(neighbors(7, 10) == 7);
- BOOST_REQUIRE_CLOSE(distances(7, 10), (2.30 * 2.30), 1e-5);
- BOOST_REQUIRE(neighbors(8, 10) == 6);
- BOOST_REQUIRE_CLOSE(distances(8, 10), (3.00 * 3.00), 1e-5);
- BOOST_REQUIRE(neighbors(9, 10) == 4);
- BOOST_REQUIRE_CLOSE(distances(9, 10), (4.05 * 4.05), 1e-5);
-
- // Clean the memory.
- delete allknn;
- }
- }
-
- /**
- * Test the dual-tree nearest-neighbors method with the naive method. This
- * uses both a query and reference dataset.
- *
- * Errors are produced if the results are not identical.
- */
- BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_1) {
- arma::mat data_for_tree_;
-
- // Hard-coded filename: bad!
- if (!data::Load("test_data_3_1000.csv", data_for_tree_))
- BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
-
- // Set up matrices to work with.
- arma::mat dual_query(data_for_tree_);
- arma::mat dual_references(data_for_tree_);
- arma::mat naive_query(data_for_tree_);
- arma::mat naive_references(data_for_tree_);
-
- // Reset parameters from last test.
- CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
- CLI::GetParam<bool>("neighbor_search/single_mode") = false;
-
- AllkNN allknn_(dual_query, dual_references);
-
- // Set parameters right for naive run.
- CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- AllkNN naive_(naive_query, naive_references);
-
- arma::Mat<size_t> resulting_neighbors_tree;
- arma::mat distances_tree;
- allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
-
- arma::Mat<size_t> resulting_neighbors_naive;
- arma::mat distances_naive;
- naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
-
- for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
- BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- }
- }
-
- /**
- * Test the dual-tree nearest-neighbors method with the naive method. This uses
- * only a reference dataset.
- *
- * Errors are produced if the results are not identical.
- */
- BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_2) {
- arma::mat data_for_tree_;
-
- // Hard-coded filename: bad!
- // Code duplication: also bad!
- if (!data::Load("test_data_3_1000.csv", data_for_tree_))
- BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
-
- // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
- arma::mat dual_query(data_for_tree_);
- arma::mat naive_query(data_for_tree_);
-
- // Reset parameters from last test.
- CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
- CLI::GetParam<bool>("neighbor_search/single_mode") = false;
- AllkNN allknn_(dual_query);
-
- // Set naive mode.
- CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- AllkNN naive_(naive_query);
-
- arma::Mat<size_t> resulting_neighbors_tree;
- arma::mat distances_tree;
- allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
-
- arma::Mat<size_t> resulting_neighbors_naive;
- arma::mat distances_naive;
- naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
-
- for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
- BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- }
- }
-
- /**
- * Test the single-tree nearest-neighbors method with the naive method. This
- * uses only a reference dataset.
- *
- * Errors are produced if the results are not identical.
- */
- BOOST_AUTO_TEST_CASE(single_tree_vs_naive) {
- arma::mat data_for_tree_;
-
- // Hard-coded filename: bad!
- // Code duplication: also bad!
- if (!data::Load("test_data_3_1000.csv", data_for_tree_))
- BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
-
- // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
- arma::mat single_query(data_for_tree_);
- arma::mat naive_query(data_for_tree_);
-
- // Reset parameters from last test.
- CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
- CLI::GetParam<bool>("neighbor_search/single_mode") = true;
- AllkNN allknn_(single_query);
-
- // Set up computation for naive mode.
- CLI::GetParam<bool>("neighbor_search/single_mode") = false;
- CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- AllkNN naive_(naive_query);
-
- arma::Mat<size_t> resulting_neighbors_tree;
- arma::mat distances_tree;
- allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
-
- arma::Mat<size_t> resulting_neighbors_naive;
- arma::mat distances_naive;
- naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
-
- for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
- BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- }
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/allknn_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/allknn_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/allknn_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/allknn_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,451 @@
+/**
+ * @file allknn_test.cpp
+ *
+ * Test file for AllkNN class.
+ */
+#include <mlpack/core.h>
+#include <mlpack/methods/neighbor_search/neighbor_search.h>
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace mlpack::neighbor;
+
+BOOST_AUTO_TEST_SUITE(AllkNNTest);
+
+/**
+ * Simple nearest-neighbors test with small, synthetic dataset. This is an
+ * exhaustive test, which checks that each method for performing the calculation
+ * (dual-tree, single-tree, naive) produces the correct results. An
+ * eleven-point dataset and the ten nearest neighbors are taken. The dataset is
+ * in one dimension for simplicity -- the correct functionality of distance
+ * functions is not tested here.
+ */
+BOOST_AUTO_TEST_CASE(exhaustive_synthetic_test)
+{
+ // Set up our data.
+ arma::mat data(1, 11);
+ data[0] = 0.05; // Row addressing is unnecessary (they are all 0).
+ data[1] = 0.35;
+ data[2] = 0.15;
+ data[3] = 1.25;
+ data[4] = 5.05;
+ data[5] = -0.22;
+ data[6] = -2.00;
+ data[7] = -1.30;
+ data[8] = 0.45;
+ data[9] = 0.90;
+ data[10] = 1.00;
+
+ // We will loop through three times, one for each method of performing the
+ // calculation. We'll always use 10 neighbors, so set that parameter.
+ CLI::GetParam<int>("neighbor_search/k") = 10;
+ for (int i = 0; i < 3; i++)
+ {
+ AllkNN* allknn;
+ arma::mat data_mutable = data;
+ switch(i)
+ {
+ case 0: // Use the dual-tree method.
+ allknn = new AllkNN(data_mutable);
+ break;
+ case 1: // Use the single-tree method.
+ CLI::GetParam<bool>("neighbor_search/single_mode") = true;
+ allknn = new AllkNN(data_mutable);
+ break;
+ case 2: // Use the naive method.
+ CLI::GetParam<bool>("neighbor_search/single_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
+ allknn = new AllkNN(data_mutable);
+ break;
+ }
+
+ // Now perform the actual calculation.
+ arma::Mat<size_t> neighbors;
+ arma::mat distances;
+ allknn->ComputeNeighbors(neighbors, distances);
+
+ // Now the exhaustive check for correctness. This will be long. We must
+ // also remember that the distances returned are squared distances. As a
+ // result, distance comparisons are written out as (distance * distance) for
+ // readability.
+
+ // Neighbors of point 0.
+ BOOST_REQUIRE(neighbors(0, 0) == 2);
+ BOOST_REQUIRE_CLOSE(distances(0, 0), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 0) == 5);
+ BOOST_REQUIRE_CLOSE(distances(1, 0), (0.27 * 0.27), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 0) == 1);
+ BOOST_REQUIRE_CLOSE(distances(2, 0), (0.30 * 0.30), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 0) == 8);
+ BOOST_REQUIRE_CLOSE(distances(3, 0), (0.40 * 0.40), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 0) == 9);
+ BOOST_REQUIRE_CLOSE(distances(4, 0), (0.85 * 0.85), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 0) == 10);
+ BOOST_REQUIRE_CLOSE(distances(5, 0), (0.95 * 0.95), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 0) == 3);
+ BOOST_REQUIRE_CLOSE(distances(6, 0), (1.20 * 1.20), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 0) == 7);
+ BOOST_REQUIRE_CLOSE(distances(7, 0), (1.35 * 1.35), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 0) == 6);
+ BOOST_REQUIRE_CLOSE(distances(8, 0), (2.05 * 2.05), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 0) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 0), (5.00 * 5.00), 1e-5);
+
+ // Neighbors of point 1.
+ BOOST_REQUIRE(neighbors(0, 1) == 8);
+ BOOST_REQUIRE_CLOSE(distances(0, 1), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 1) == 2);
+ BOOST_REQUIRE_CLOSE(distances(1, 1), (0.20 * 0.20), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 1) == 0);
+ BOOST_REQUIRE_CLOSE(distances(2, 1), (0.30 * 0.30), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 1) == 9);
+ BOOST_REQUIRE_CLOSE(distances(3, 1), (0.55 * 0.55), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 1) == 5);
+ BOOST_REQUIRE_CLOSE(distances(4, 1), (0.57 * 0.57), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 1) == 10);
+ BOOST_REQUIRE_CLOSE(distances(5, 1), (0.65 * 0.65), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 1) == 3);
+ BOOST_REQUIRE_CLOSE(distances(6, 1), (0.90 * 0.90), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 1) == 7);
+ BOOST_REQUIRE_CLOSE(distances(7, 1), (1.65 * 1.65), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 1) == 6);
+ BOOST_REQUIRE_CLOSE(distances(8, 1), (2.35 * 2.35), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 1) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 1), (4.70 * 4.70), 1e-5);
+
+ // Neighbors of point 2.
+ BOOST_REQUIRE(neighbors(0, 2) == 0);
+ BOOST_REQUIRE_CLOSE(distances(0, 2), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 2) == 1);
+ BOOST_REQUIRE_CLOSE(distances(1, 2), (0.20 * 0.20), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 2) == 8);
+ BOOST_REQUIRE_CLOSE(distances(2, 2), (0.30 * 0.30), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 2) == 5);
+ BOOST_REQUIRE_CLOSE(distances(3, 2), (0.37 * 0.37), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 2) == 9);
+ BOOST_REQUIRE_CLOSE(distances(4, 2), (0.75 * 0.75), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 2) == 10);
+ BOOST_REQUIRE_CLOSE(distances(5, 2), (0.85 * 0.85), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 2) == 3);
+ BOOST_REQUIRE_CLOSE(distances(6, 2), (1.10 * 1.10), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 2) == 7);
+ BOOST_REQUIRE_CLOSE(distances(7, 2), (1.45 * 1.45), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 2) == 6);
+ BOOST_REQUIRE_CLOSE(distances(8, 2), (2.15 * 2.15), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 2) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 2), (4.90 * 4.90), 1e-5);
+
+ // Neighbors of point 3.
+ BOOST_REQUIRE(neighbors(0, 3) == 10);
+ BOOST_REQUIRE_CLOSE(distances(0, 3), (0.25 * 0.25), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 3) == 9);
+ BOOST_REQUIRE_CLOSE(distances(1, 3), (0.35 * 0.35), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 3) == 8);
+ BOOST_REQUIRE_CLOSE(distances(2, 3), (0.80 * 0.80), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 3) == 1);
+ BOOST_REQUIRE_CLOSE(distances(3, 3), (0.90 * 0.90), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 3) == 2);
+ BOOST_REQUIRE_CLOSE(distances(4, 3), (1.10 * 1.10), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 3) == 0);
+ BOOST_REQUIRE_CLOSE(distances(5, 3), (1.20 * 1.20), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 3) == 5);
+ BOOST_REQUIRE_CLOSE(distances(6, 3), (1.47 * 1.47), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 3) == 7);
+ BOOST_REQUIRE_CLOSE(distances(7, 3), (2.55 * 2.55), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 3) == 6);
+ BOOST_REQUIRE_CLOSE(distances(8, 3), (3.25 * 3.25), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 3) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 3), (3.80 * 3.80), 1e-5);
+
+ // Neighbors of point 4.
+ BOOST_REQUIRE(neighbors(0, 4) == 3);
+ BOOST_REQUIRE_CLOSE(distances(0, 4), (3.80 * 3.80), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 4) == 10);
+ BOOST_REQUIRE_CLOSE(distances(1, 4), (4.05 * 4.05), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 4) == 9);
+ BOOST_REQUIRE_CLOSE(distances(2, 4), (4.15 * 4.15), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 4) == 8);
+ BOOST_REQUIRE_CLOSE(distances(3, 4), (4.60 * 4.60), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 4) == 1);
+ BOOST_REQUIRE_CLOSE(distances(4, 4), (4.70 * 4.70), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 4) == 2);
+ BOOST_REQUIRE_CLOSE(distances(5, 4), (4.90 * 4.90), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 4) == 0);
+ BOOST_REQUIRE_CLOSE(distances(6, 4), (5.00 * 5.00), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 4) == 5);
+ BOOST_REQUIRE_CLOSE(distances(7, 4), (5.27 * 5.27), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 4) == 7);
+ BOOST_REQUIRE_CLOSE(distances(8, 4), (6.35 * 6.35), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 4) == 6);
+ BOOST_REQUIRE_CLOSE(distances(9, 4), (7.05 * 7.05), 1e-5);
+
+ // Neighbors of point 5.
+ BOOST_REQUIRE(neighbors(0, 5) == 0);
+ BOOST_REQUIRE_CLOSE(distances(0, 5), (0.27 * 0.27), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 5) == 2);
+ BOOST_REQUIRE_CLOSE(distances(1, 5), (0.37 * 0.37), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 5) == 1);
+ BOOST_REQUIRE_CLOSE(distances(2, 5), (0.57 * 0.57), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 5) == 8);
+ BOOST_REQUIRE_CLOSE(distances(3, 5), (0.67 * 0.67), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 5) == 7);
+ BOOST_REQUIRE_CLOSE(distances(4, 5), (1.08 * 1.08), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 5) == 9);
+ BOOST_REQUIRE_CLOSE(distances(5, 5), (1.12 * 1.12), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 5) == 10);
+ BOOST_REQUIRE_CLOSE(distances(6, 5), (1.22 * 1.22), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 5) == 3);
+ BOOST_REQUIRE_CLOSE(distances(7, 5), (1.47 * 1.47), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 5) == 6);
+ BOOST_REQUIRE_CLOSE(distances(8, 5), (1.78 * 1.78), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 5) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 5), (5.27 * 5.27), 1e-5);
+
+ // Neighbors of point 6.
+ BOOST_REQUIRE(neighbors(0, 6) == 7);
+ BOOST_REQUIRE_CLOSE(distances(0, 6), (0.70 * 0.70), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 6) == 5);
+ BOOST_REQUIRE_CLOSE(distances(1, 6), (1.78 * 1.78), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 6) == 0);
+ BOOST_REQUIRE_CLOSE(distances(2, 6), (2.05 * 2.05), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 6) == 2);
+ BOOST_REQUIRE_CLOSE(distances(3, 6), (2.15 * 2.15), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 6) == 1);
+ BOOST_REQUIRE_CLOSE(distances(4, 6), (2.35 * 2.35), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 6) == 8);
+ BOOST_REQUIRE_CLOSE(distances(5, 6), (2.45 * 2.45), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 6) == 9);
+ BOOST_REQUIRE_CLOSE(distances(6, 6), (2.90 * 2.90), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 6) == 10);
+ BOOST_REQUIRE_CLOSE(distances(7, 6), (3.00 * 3.00), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 6) == 3);
+ BOOST_REQUIRE_CLOSE(distances(8, 6), (3.25 * 3.25), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 6) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 6), (7.05 * 7.05), 1e-5);
+
+ // Neighbors of point 7.
+ BOOST_REQUIRE(neighbors(0, 7) == 6);
+ BOOST_REQUIRE_CLOSE(distances(0, 7), (0.70 * 0.70), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 7) == 5);
+ BOOST_REQUIRE_CLOSE(distances(1, 7), (1.08 * 1.08), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 7) == 0);
+ BOOST_REQUIRE_CLOSE(distances(2, 7), (1.35 * 1.35), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 7) == 2);
+ BOOST_REQUIRE_CLOSE(distances(3, 7), (1.45 * 1.45), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 7) == 1);
+ BOOST_REQUIRE_CLOSE(distances(4, 7), (1.65 * 1.65), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 7) == 8);
+ BOOST_REQUIRE_CLOSE(distances(5, 7), (1.75 * 1.75), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 7) == 9);
+ BOOST_REQUIRE_CLOSE(distances(6, 7), (2.20 * 2.20), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 7) == 10);
+ BOOST_REQUIRE_CLOSE(distances(7, 7), (2.30 * 2.30), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 7) == 3);
+ BOOST_REQUIRE_CLOSE(distances(8, 7), (2.55 * 2.55), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 7) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 7), (6.35 * 6.35), 1e-5);
+
+ // Neighbors of point 8.
+ BOOST_REQUIRE(neighbors(0, 8) == 1);
+ BOOST_REQUIRE_CLOSE(distances(0, 8), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 8) == 2);
+ BOOST_REQUIRE_CLOSE(distances(1, 8), (0.30 * 0.30), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 8) == 0);
+ BOOST_REQUIRE_CLOSE(distances(2, 8), (0.40 * 0.40), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 8) == 9);
+ BOOST_REQUIRE_CLOSE(distances(3, 8), (0.45 * 0.45), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 8) == 10);
+ BOOST_REQUIRE_CLOSE(distances(4, 8), (0.55 * 0.55), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 8) == 5);
+ BOOST_REQUIRE_CLOSE(distances(5, 8), (0.67 * 0.67), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 8) == 3);
+ BOOST_REQUIRE_CLOSE(distances(6, 8), (0.80 * 0.80), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 8) == 7);
+ BOOST_REQUIRE_CLOSE(distances(7, 8), (1.75 * 1.75), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 8) == 6);
+ BOOST_REQUIRE_CLOSE(distances(8, 8), (2.45 * 2.45), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 8) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 8), (4.60 * 4.60), 1e-5);
+
+ // Neighbors of point 9.
+ BOOST_REQUIRE(neighbors(0, 9) == 10);
+ BOOST_REQUIRE_CLOSE(distances(0, 9), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 9) == 3);
+ BOOST_REQUIRE_CLOSE(distances(1, 9), (0.35 * 0.35), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 9) == 8);
+ BOOST_REQUIRE_CLOSE(distances(2, 9), (0.45 * 0.45), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 9) == 1);
+ BOOST_REQUIRE_CLOSE(distances(3, 9), (0.55 * 0.55), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 9) == 2);
+ BOOST_REQUIRE_CLOSE(distances(4, 9), (0.75 * 0.75), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 9) == 0);
+ BOOST_REQUIRE_CLOSE(distances(5, 9), (0.85 * 0.85), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 9) == 5);
+ BOOST_REQUIRE_CLOSE(distances(6, 9), (1.12 * 1.12), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 9) == 7);
+ BOOST_REQUIRE_CLOSE(distances(7, 9), (2.20 * 2.20), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 9) == 6);
+ BOOST_REQUIRE_CLOSE(distances(8, 9), (2.90 * 2.90), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 9) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 9), (4.15 * 4.15), 1e-5);
+
+ // Neighbors of point 10.
+ BOOST_REQUIRE(neighbors(0, 10) == 9);
+ BOOST_REQUIRE_CLOSE(distances(0, 10), (0.10 * 0.10), 1e-5);
+ BOOST_REQUIRE(neighbors(1, 10) == 3);
+ BOOST_REQUIRE_CLOSE(distances(1, 10), (0.25 * 0.25), 1e-5);
+ BOOST_REQUIRE(neighbors(2, 10) == 8);
+ BOOST_REQUIRE_CLOSE(distances(2, 10), (0.55 * 0.55), 1e-5);
+ BOOST_REQUIRE(neighbors(3, 10) == 1);
+ BOOST_REQUIRE_CLOSE(distances(3, 10), (0.65 * 0.65), 1e-5);
+ BOOST_REQUIRE(neighbors(4, 10) == 2);
+ BOOST_REQUIRE_CLOSE(distances(4, 10), (0.85 * 0.85), 1e-5);
+ BOOST_REQUIRE(neighbors(5, 10) == 0);
+ BOOST_REQUIRE_CLOSE(distances(5, 10), (0.95 * 0.95), 1e-5);
+ BOOST_REQUIRE(neighbors(6, 10) == 5);
+ BOOST_REQUIRE_CLOSE(distances(6, 10), (1.22 * 1.22), 1e-5);
+ BOOST_REQUIRE(neighbors(7, 10) == 7);
+ BOOST_REQUIRE_CLOSE(distances(7, 10), (2.30 * 2.30), 1e-5);
+ BOOST_REQUIRE(neighbors(8, 10) == 6);
+ BOOST_REQUIRE_CLOSE(distances(8, 10), (3.00 * 3.00), 1e-5);
+ BOOST_REQUIRE(neighbors(9, 10) == 4);
+ BOOST_REQUIRE_CLOSE(distances(9, 10), (4.05 * 4.05), 1e-5);
+
+ // Clean the memory.
+ delete allknn;
+ }
+}
+
+/**
+ * Test the dual-tree nearest-neighbors method with the naive method. This
+ * uses both a query and reference dataset.
+ *
+ * Errors are produced if the results are not identical.
+ */
+BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_1)
+{
+ arma::mat data_for_tree_;
+
+ // Hard-coded filename: bad!
+ if (!data::Load("test_data_3_1000.csv", data_for_tree_))
+ BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+
+ // Set up matrices to work with.
+ arma::mat dual_query(data_for_tree_);
+ arma::mat dual_references(data_for_tree_);
+ arma::mat naive_query(data_for_tree_);
+ arma::mat naive_references(data_for_tree_);
+
+ // Reset parameters from last test.
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/single_mode") = false;
+
+ AllkNN allknn_(dual_query, dual_references);
+
+ // Set parameters right for naive run.
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
+ AllkNN naive_(naive_query, naive_references);
+
+ arma::Mat<size_t> resulting_neighbors_tree;
+ arma::mat distances_tree;
+ allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+
+ arma::Mat<size_t> resulting_neighbors_naive;
+ arma::mat distances_naive;
+ naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+
+ for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++)
+ {
+ BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+ BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+ }
+}
+
+/**
+ * Test the dual-tree nearest-neighbors method with the naive method. This uses
+ * only a reference dataset.
+ *
+ * Errors are produced if the results are not identical.
+ */
+BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_2)
+{
+ arma::mat data_for_tree_;
+
+ // Hard-coded filename: bad!
+ // Code duplication: also bad!
+ if (!data::Load("test_data_3_1000.csv", data_for_tree_))
+ BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+
+ // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
+ arma::mat dual_query(data_for_tree_);
+ arma::mat naive_query(data_for_tree_);
+
+ // Reset parameters from last test.
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/single_mode") = false;
+ AllkNN allknn_(dual_query);
+
+ // Set naive mode.
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
+ AllkNN naive_(naive_query);
+
+ arma::Mat<size_t> resulting_neighbors_tree;
+ arma::mat distances_tree;
+ allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+
+ arma::Mat<size_t> resulting_neighbors_naive;
+ arma::mat distances_naive;
+ naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+
+ for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++)
+ {
+ BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+ BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+ }
+}
+
+/**
+ * Test the single-tree nearest-neighbors method with the naive method. This
+ * uses only a reference dataset.
+ *
+ * Errors are produced if the results are not identical.
+ */
+BOOST_AUTO_TEST_CASE(single_tree_vs_naive)
+{
+ arma::mat data_for_tree_;
+
+ // Hard-coded filename: bad!
+ // Code duplication: also bad!
+ if (!data::Load("test_data_3_1000.csv", data_for_tree_))
+ BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+
+ // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
+ arma::mat single_query(data_for_tree_);
+ arma::mat naive_query(data_for_tree_);
+
+ // Reset parameters from last test.
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/single_mode") = true;
+ AllkNN allknn_(single_query);
+
+ // Set up computation for naive mode.
+ CLI::GetParam<bool>("neighbor_search/single_mode") = false;
+ CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
+ AllkNN naive_(naive_query);
+
+ arma::Mat<size_t> resulting_neighbors_tree;
+ arma::mat distances_tree;
+ allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+
+ arma::Mat<size_t> resulting_neighbors_naive;
+ arma::mat distances_naive;
+ naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+
+ for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++)
+ {
+ BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+ BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+ }
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/aug_lagrangian_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/aug_lagrangian_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/aug_lagrangian_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,4 +1,4 @@
-/***
+/**
* @file aug_lagrangian_test.cc
* @author Ryan Curtin
*
@@ -16,93 +16,98 @@
BOOST_AUTO_TEST_SUITE(AugLagrangianTest);
- /***
- * Tests the Augmented Lagrangian optimizer using the
- * AugmentedLagrangianTestFunction class.
- */
- BOOST_AUTO_TEST_CASE(aug_lagrangian_test_function) {
- // The choice of 10 memory slots is arbitrary.
- AugLagrangianTestFunction f;
- AugLagrangian<AugLagrangianTestFunction> aug(f, 10);
+/**
+ * Tests the Augmented Lagrangian optimizer using the
+ * AugmentedLagrangianTestFunction class.
+ */
+BOOST_AUTO_TEST_CASE(aug_lagrangian_test_function)
+{
+ // The choice of 10 memory slots is arbitrary.
+ AugLagrangianTestFunction f;
+ AugLagrangian<AugLagrangianTestFunction> aug(f, 10);
- arma::vec coords = f.GetInitialPoint();
+ arma::vec coords = f.GetInitialPoint();
- if(!aug.Optimize(0, coords))
- BOOST_FAIL("Optimization reported failure.");
+ if(!aug.Optimize(0, coords))
+ BOOST_FAIL("Optimization reported failure.");
- double final_value = f.Evaluate(coords);
+ double final_value = f.Evaluate(coords);
- BOOST_REQUIRE_CLOSE(final_value, 70, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[0], 1, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[1], 4, 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(final_value, 70, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[0], 1, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[1], 4, 1e-5);
+}
- /***
- * Tests the Augmented Lagrangian optimizer using the Gockenbach function.
- */
- BOOST_AUTO_TEST_CASE(gockenbach_function) {
- GockenbachFunction f;
- AugLagrangian<GockenbachFunction> aug(f, 10);
+/**
+ * Tests the Augmented Lagrangian optimizer using the Gockenbach function.
+ */
+BOOST_AUTO_TEST_CASE(gockenbach_function)
+{
+ GockenbachFunction f;
+ AugLagrangian<GockenbachFunction> aug(f, 10);
- arma::vec coords = f.GetInitialPoint();
+ arma::vec coords = f.GetInitialPoint();
- if(!aug.Optimize(0, coords))
- BOOST_FAIL("Optimization reported failure.");
+ if(!aug.Optimize(0, coords))
+ BOOST_FAIL("Optimization reported failure.");
- double final_value = f.Evaluate(coords);
+ double final_value = f.Evaluate(coords);
- BOOST_REQUIRE_CLOSE(final_value, 29.633926, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[0], 0.12288178, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[1], -1.10778185, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[2], 0.015099932, 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(final_value, 29.633926, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[0], 0.12288178, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[1], -1.10778185, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[2], 0.015099932, 1e-5);
+}
- /***
- * Extremely simple test case for the Lovasz theta SDP.
- */
- BOOST_AUTO_TEST_CASE(extremely_simple_lovasz_theta_sdp) {
- // Manually input the single edge.
- arma::mat edges = "0; 1";
+/**
+ * Extremely simple test case for the Lovasz theta SDP.
+ */
+BOOST_AUTO_TEST_CASE(extremely_simple_lovasz_theta_sdp)
+{
+ // Manually input the single edge.
+ arma::mat edges = "0; 1";
- LovaszThetaSDP ltsdp(edges);
- AugLagrangian<LovaszThetaSDP> aug(ltsdp, 10);
+ LovaszThetaSDP ltsdp(edges);
+ AugLagrangian<LovaszThetaSDP> aug(ltsdp, 10);
- arma::mat coords = ltsdp.GetInitialPoint();
+ arma::mat coords = ltsdp.GetInitialPoint();
- if (!aug.Optimize(0, coords))
- BOOST_FAIL("Optimization reported failure.");
+ if (!aug.Optimize(0, coords))
+ BOOST_FAIL("Optimization reported failure.");
- double final_value = ltsdp.Evaluate(coords);
+ double final_value = ltsdp.Evaluate(coords);
- arma::mat X = trans(coords) * coords;
+ arma::mat X = trans(coords) * coords;
- BOOST_CHECK_CLOSE(final_value, -1.0, 1e-5);
+ BOOST_CHECK_CLOSE(final_value, -1.0, 1e-5);
- BOOST_CHECK_CLOSE(X(0, 0) + X(1, 1), 1.0, 1e-5);
- BOOST_CHECK_SMALL(X(0, 1), 1e-8);
- BOOST_CHECK_SMALL(X(1, 0), 1e-8);
- }
+ BOOST_CHECK_CLOSE(X(0, 0) + X(1, 1), 1.0, 1e-5);
+ BOOST_CHECK_SMALL(X(0, 1), 1e-8);
+ BOOST_CHECK_SMALL(X(1, 0), 1e-8);
+}
- /***
- * Tests the Augmented Lagrangian optimizer on the Lovasz theta SDP, using the
- * hamming_10_2 dataset, just like in the paper by Monteiro and Burer.
- *
- BOOST_AUTO_TEST_CASE(lovasz_theta_johnson8_4_4) {
- arma::mat edges;
- // Hardcoded filename: bad!
- data::Load("MANN-a27.csv", edges);
+/**
+ * Tests the Augmented Lagrangian optimizer on the Lovasz theta SDP, using the
+ * hamming_10_2 dataset, just like in the paper by Monteiro and Burer.
+ *
+BOOST_AUTO_TEST_CASE(lovasz_theta_johnson8_4_4)
+{
+ arma::mat edges;
+ // Hardcoded filename: bad!
+ data::Load("MANN-a27.csv", edges);
- LovaszThetaSDP ltsdp(edges);
- AugLagrangian<LovaszThetaSDP> aug(ltsdp, 10);
+ LovaszThetaSDP ltsdp(edges);
+ AugLagrangian<LovaszThetaSDP> aug(ltsdp, 10);
- arma::mat coords = ltsdp.GetInitialPoint();
+ arma::mat coords = ltsdp.GetInitialPoint();
- if(!aug.Optimize(0, coords))
- BOOST_FAIL("Optimization reported failure.");
+ if(!aug.Optimize(0, coords))
+ BOOST_FAIL("Optimization reported failure.");
- double final_value = ltsdp.Evaluate(coords);
+ double final_value = ltsdp.Evaluate(coords);
- BOOST_REQUIRE_CLOSE(final_value, -14.0, 1e-5);
- }
- */
+ BOOST_REQUIRE_CLOSE(final_value, -14.0, 1e-5);
+}
+ */
+
BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/cli_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/cli_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/cli_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,5 +1,5 @@
-/***
- * @file io_test.cc
+/**
+ * @file cli_test.cpp
* @author Matthew Amidon, Ryan Curtin
*
* Test for the CLI input parameter system.
@@ -16,7 +16,6 @@
#define DEFAULT_INT 42
-//Include unit tests down here since we just have to
#include <boost/test/unit_test.hpp>
#define BASH_RED "\033[0;31m"
@@ -30,210 +29,213 @@
BOOST_AUTO_TEST_SUITE(CLITest);
- /***
- * @brief Tests that inserting elements into an OptionsHierarchy
- * properly updates the tree.
- *
- * @return True indicating all is well with OptionsHierarchy
- */
- BOOST_AUTO_TEST_CASE(TestHierarchy) {
- OptionsHierarchy tmp = OptionsHierarchy("UTest");
- std::string testName = std::string("UTest/test");
- std::string testDesc = std::string("Test description.");
- std::string testTID = TYPENAME(int);
+/**
+ * Tests that inserting elements into an OptionsHierarchy properly updates the
+ * tree.
+ */
+BOOST_AUTO_TEST_CASE(TestHierarchy)
+{
+ OptionsHierarchy tmp = OptionsHierarchy("UTest");
+ std::string testName = std::string("UTest/test");
+ std::string testDesc = std::string("Test description.");
+ std::string testTID = TYPENAME(int);
- // Check that the hierarchy is properly named.
- std::string str = std::string("UTest");
- OptionsData node = tmp.GetNodeData();
+ // Check that the hierarchy is properly named.
+ std::string str = std::string("UTest");
+ OptionsData node = tmp.GetNodeData();
- BOOST_REQUIRE_EQUAL(str.compare(node.node), 0);
- // Check that inserting a node actually inserts the node.
- // Note, that since all versions of append simply call the most qualified
- // overload, we will only test that one.
- tmp.AppendNode(testName, testTID, testDesc);
- BOOST_REQUIRE(tmp.FindNode(testName) != NULL);
+ BOOST_REQUIRE_EQUAL(str.compare(node.node), 0);
+ // Check that inserting a node actually inserts the node.
+ // Note, that since all versions of append simply call the most qualified
+ // overload, we will only test that one.
+ tmp.AppendNode(testName, testTID, testDesc);
+ BOOST_REQUIRE(tmp.FindNode(testName) != NULL);
- // Now check that the inserted node has the correct data.
- OptionsHierarchy* testHierarchy = tmp.FindNode(testName);
- OptionsData testData;
- if (testHierarchy != NULL) {
- node = testHierarchy->GetNodeData();
+ // Now check that the inserted node has the correct data.
+ OptionsHierarchy* testHierarchy = tmp.FindNode(testName);
+ OptionsData testData;
+ if (testHierarchy != NULL)
+ {
+ node = testHierarchy->GetNodeData();
- BOOST_REQUIRE(testName.compare(node.node) == 0);
- BOOST_REQUIRE(testDesc.compare(node.desc) == 0);
- BOOST_REQUIRE(testTID.compare(node.tname) == 0);
- }
+ BOOST_REQUIRE(testName.compare(node.node) == 0);
+ BOOST_REQUIRE(testDesc.compare(node.desc) == 0);
+ BOOST_REQUIRE(testTID.compare(node.tname) == 0);
}
+}
- /***
- * @brief Tests that CLI works as intended, namely that CLI::Add
- * propagates successfully.
- *
- * @return True indicating all is well with CLI::Add, false otherwise.
- */
- BOOST_AUTO_TEST_CASE(TestCLIAdd) {
- // Check that the CLI::HasParam returns false if no value has been specified
- // on the commandline and ignores any programmatical assignments.
- CLI::Add<bool>("bool", "True or False", "global");
- BOOST_REQUIRE_EQUAL(CLI::HasParam("global/bool"), false);
- CLI::GetParam<bool>("global/bool") = true;
- // CLI::HasParam should return true.
- BOOST_REQUIRE_EQUAL(CLI::HasParam("global/bool"), true);
+/**
+ * Tests that CLI works as intended, namely that CLI::Add propagates
+ * successfully.
+ */
+BOOST_AUTO_TEST_CASE(TestCLIAdd)
+{
+ // Check that the CLI::HasParam returns false if no value has been specified
+ // on the commandline and ignores any programmatical assignments.
+ CLI::Add<bool>("bool", "True or False", "global");
+ BOOST_REQUIRE_EQUAL(CLI::HasParam("global/bool"), false);
+ CLI::GetParam<bool>("global/bool") = true;
+ // CLI::HasParam should return true.
+ BOOST_REQUIRE_EQUAL(CLI::HasParam("global/bool"), true);
- //Check the description of our variable.
- BOOST_REQUIRE_EQUAL(CLI::GetDescription("global/bool").compare(
+ // Check the description of our variable.
+ BOOST_REQUIRE_EQUAL(CLI::GetDescription("global/bool").compare(
std::string("True or False")) , 0);
- // Check that SanitizeString is sanitary.
- std::string tmp = CLI::SanitizeString("/foo/bar/fizz");
- BOOST_REQUIRE_EQUAL(tmp.compare(std::string("foo/bar/fizz/")),0);
- }
+ // Check that SanitizeString is sanitary.
+ std::string tmp = CLI::SanitizeString("/foo/bar/fizz");
+ BOOST_REQUIRE_EQUAL(tmp.compare(std::string("foo/bar/fizz/")),0);
+}
- /***
- * Test the output of CLI. We will pass bogus input to a stringstream so that
- * none of it gets to the screen.
- */
- BOOST_AUTO_TEST_CASE(TestPrefixedOutStreamBasic) {
- std::stringstream ss;
- PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
+/**
+ * Test the output of CLI. We will pass bogus input to a stringstream so that
+ * none of it gets to the screen.
+ */
+BOOST_AUTO_TEST_CASE(TestPrefixedOutStreamBasic)
+{
+ std::stringstream ss;
+ PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
- pss << "This shouldn't break anything" << std::endl;
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR "This shouldn't break anything\n");
+ pss << "This shouldn't break anything" << std::endl;
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR "This shouldn't break anything\n");
- ss.str("");
- pss << "Test the new lines...";
- pss << "shouldn't get 'Info' here." << std::endl;
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR
- "Test the new lines...shouldn't get 'Info' here.\n");
+ ss.str("");
+ pss << "Test the new lines...";
+ pss << "shouldn't get 'Info' here." << std::endl;
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR
+ "Test the new lines...shouldn't get 'Info' here.\n");
- pss << "But now I should." << std::endl << std::endl;
- pss << "";
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR
- "Test the new lines...shouldn't get 'Info' here.\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR "But now I should.\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR "\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR "");
- }
+ pss << "But now I should." << std::endl << std::endl;
+ pss << "";
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR
+ "Test the new lines...shouldn't get 'Info' here.\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR "But now I should.\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR "\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR "");
+}
- /**
- * @brief Tests that the various PARAM_* macros work properly
- * @return True indicating that all is well with CLI & Options.
- */
- BOOST_AUTO_TEST_CASE(TestOption) {
- // This test will involve creating an option, and making sure CLI reflects
- // this.
- PARAM(int, "test", "test desc", "test_parent", DEFAULT_INT, false);
+/**
+ * Tests that the various PARAM_* macros work properly.
+ */
+BOOST_AUTO_TEST_CASE(TestOption)
+{
+ // This test will involve creating an option, and making sure CLI reflects
+ // this.
+ PARAM(int, "test", "test desc", "test_parent", DEFAULT_INT, false);
- // Does CLI reflect this?
- BOOST_REQUIRE_EQUAL(CLI::HasParam("test_parent/test"), true);
+ // Does CLI reflect this?
+ BOOST_REQUIRE_EQUAL(CLI::HasParam("test_parent/test"), true);
- std::string desc = std::string("test desc");
+ std::string desc = std::string("test desc");
- BOOST_REQUIRE_EQUAL(CLI::GetDescription("test_parent/test"), "test desc");
- BOOST_REQUIRE_EQUAL(CLI::GetParam<int>("test_parent/test"), DEFAULT_INT);
- }
+ BOOST_REQUIRE_EQUAL(CLI::GetDescription("test_parent/test"), "test desc");
+ BOOST_REQUIRE_EQUAL(CLI::GetParam<int>("test_parent/test"), DEFAULT_INT);
+}
- /***
- * Ensure that a Boolean option which we define is set correctly.
- */
- BOOST_AUTO_TEST_CASE(TestBooleanOption) {
- PARAM_FLAG("flag_test", "flag test description", "test_parent");
+/**
+ * Ensure that a Boolean option which we define is set correctly.
+ */
+BOOST_AUTO_TEST_CASE(TestBooleanOption)
+{
+ PARAM_FLAG("flag_test", "flag test description", "test_parent");
- BOOST_REQUIRE_EQUAL(CLI::HasParam("test_parent/flag_test"), false);
+ BOOST_REQUIRE_EQUAL(CLI::HasParam("test_parent/flag_test"), false);
- BOOST_REQUIRE_EQUAL(CLI::GetDescription("test_parent/flag_test"),
- "flag test description");
+ BOOST_REQUIRE_EQUAL(CLI::GetDescription("test_parent/flag_test"),
+ "flag test description");
- // Now check that CLI reflects that it is false by default.
- BOOST_REQUIRE_EQUAL(CLI::GetParam<bool>("test_parent/flag_test"), false);
- }
+ // Now check that CLI reflects that it is false by default.
+ BOOST_REQUIRE_EQUAL(CLI::GetParam<bool>("test_parent/flag_test"), false);
+}
+/**
+ * Test that we can correctly output Armadillo objects to PrefixedOutStream
+ * objects.
+ */
+BOOST_AUTO_TEST_CASE(TestArmadilloPrefixedOutStream)
+{
+ // We will test this with both a vector and a matrix.
+ arma::vec test("1.0 1.5 2.0 2.5 3.0 3.5 4.0");
- /***
- * Test that we can correctly output Armadillo objects to PrefixedOutStream
- * objects.
- */
- BOOST_AUTO_TEST_CASE(TestArmadilloPrefixedOutStream) {
- // We will test this with both a vector and a matrix.
- arma::vec test("1.0 1.5 2.0 2.5 3.0 3.5 4.0");
+ std::stringstream ss;
+ PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
- std::stringstream ss;
- PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
+ pss << test;
+ // This should result in nothing being on the current line (since it clears
+ // it).
+ BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR " 1.0000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 1.5000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 2.0000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 2.5000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 3.0000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 3.5000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 4.0000\n");
- pss << test;
- // This should result in nothing being on the current line (since it clears
- // it).
- BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR " 1.0000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 1.5000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 2.0000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 2.5000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 3.0000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 3.5000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 4.0000\n");
+ ss.str("");
+ pss << trans(test);
+ // This should result in there being stuff on the line.
+ BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR
+ " 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000\n");
- ss.str("");
- pss << trans(test);
- // This should result in there being stuff on the line.
- BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR
- " 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000\n");
+ arma::mat test2("1.0 1.5 2.0; 2.5 3.0 3.5; 4.0 4.5 4.99999");
+ ss.str("");
+ pss << test2;
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 1.0000 1.5000 2.0000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 2.5000 3.0000 3.5000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 4.0000 4.5000 5.0000\n");
- arma::mat test2("1.0 1.5 2.0; 2.5 3.0 3.5; 4.0 4.5 4.99999");
- ss.str("");
- pss << test2;
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR " 1.0000 1.5000 2.0000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 2.5000 3.0000 3.5000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 4.0000 4.5000 5.0000\n");
+ // Try and throw a curveball by not clearing the line before outputting
+ // something else. The PrefixedOutStream should not force Armadillo objects
+ // onto their own lines.
+ ss.str("");
+ pss << "hello" << test2;
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR "hello 1.0000 1.5000 2.0000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 2.5000 3.0000 3.5000\n"
+ BASH_GREEN "[INFO ] " BASH_CLEAR " 4.0000 4.5000 5.0000\n");
+}
- // Try and throw a curveball by not clearing the line before outputting
- // something else. The PrefixedOutStream should not force Armadillo objects
- // onto their own lines.
- ss.str("");
- pss << "hello" << test2;
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR "hello 1.0000 1.5000 2.0000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 2.5000 3.0000 3.5000\n"
- BASH_GREEN "[INFO ] " BASH_CLEAR " 4.0000 4.5000 5.0000\n");
- }
+/**
+ * Test that we can correctly output things in general.
+ */
+BOOST_AUTO_TEST_CASE(TestPrefixedOutStream)
+{
+ std::stringstream ss;
+ PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
- /***
- * Test that we can correctly output things in general.
- */
- BOOST_AUTO_TEST_CASE(TestPrefixedOutStream) {
- std::stringstream ss;
- PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
+ pss << "hello world I am ";
+ pss << 7;
- pss << "hello world I am ";
- pss << 7;
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR "hello world I am 7");
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR "hello world I am 7");
+ pss << std::endl;
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR "hello world I am 7\n");
- pss << std::endl;
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR "hello world I am 7\n");
+ ss.str("");
+ pss << std::endl;
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR "\n");
+}
- ss.str("");
- pss << std::endl;
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR "\n");
- }
+/**
+ * Test format modifiers.
+ */
+BOOST_AUTO_TEST_CASE(TestPrefixedOutStreamModifiers)
+{
+ std::stringstream ss;
+ PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
- /***
- * Test format modifiers.
- */
- BOOST_AUTO_TEST_CASE(TestPrefixedOutStreamModifiers) {
- std::stringstream ss;
- PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
+ pss << "I have a precise number which is ";
+ pss << std::setw(6) << std::setfill('0') << 156;
- pss << "I have a precise number which is ";
- pss << std::setw(6) << std::setfill('0') << 156;
+ BOOST_REQUIRE_EQUAL(ss.str(),
+ BASH_GREEN "[INFO ] " BASH_CLEAR
+ "I have a precise number which is 000156");
+}
- BOOST_REQUIRE_EQUAL(ss.str(),
- BASH_GREEN "[INFO ] " BASH_CLEAR
- "I have a precise number which is 000156");
- }
-
BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/emst_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/emst_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/emst_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,135 +0,0 @@
-/**
- * @file emst_test.cc
- *
- * Test file for EMST methods
- */
-#include <mlpack/core.h>
-#include <mlpack/methods/emst/dtb.hpp>
-#include <boost/test/unit_test.hpp>
-
-using namespace mlpack;
-using namespace mlpack::emst;
-
-BOOST_AUTO_TEST_SUITE(EmstTest);
- /***
- * Simple emst test with small, synthetic dataset. This is an
- * exhaustive test, which checks that each method for performing the calculation
- * (dual-tree, single-tree, naive) produces the correct results. The dataset is
- * in one dimension for simplicity -- the correct functionality of distance
- * functions is not tested here.
- */
- BOOST_AUTO_TEST_CASE(exhaustive_synthetic_test) {
- // Set up our data.
- arma::mat data(1, 11);
- data[0] = 0.05; // Row addressing is unnecessary (they are all 0).
- data[1] = 0.37;
- data[2] = 0.15;
- data[3] = 1.25;
- data[4] = 5.05;
- data[5] = -0.22;
- data[6] = -2.00;
- data[7] = -1.30;
- data[8] = 0.45;
- data[9] = 0.91;
- data[10] = 1.00;
-
-
- // Now perform the actual calculation.
- arma::mat results;
-
-
- DualTreeBoruvka dtb;
- dtb.Init(data);
- dtb.ComputeMST(results);
-
- // Now the exhaustive check for correctness.
-
- BOOST_REQUIRE(results(0, 0) == 1);
- BOOST_REQUIRE(results(0, 1) == 8);
- BOOST_REQUIRE_CLOSE(results(0, 2), 0.08, 1e-5);
-
- BOOST_REQUIRE(results(1, 0) == 9);
- BOOST_REQUIRE(results(1, 1) == 10);
- BOOST_REQUIRE_CLOSE(results(1, 2), 0.09, 1e-5);
-
- BOOST_REQUIRE(results(2, 0) == 0);
- BOOST_REQUIRE(results(2, 1) == 2);
- BOOST_REQUIRE_CLOSE(results(2, 2), 0.1, 1e-5);
-
- BOOST_REQUIRE(results(3, 0) == 1);
- BOOST_REQUIRE(results(3, 1) == 2);
- BOOST_REQUIRE_CLOSE(results(3, 2), 0.22, 1e-5);
-
- BOOST_REQUIRE(results(4, 0) == 3);
- BOOST_REQUIRE(results(4, 1) == 10);
- BOOST_REQUIRE_CLOSE(results(4, 2), 0.25, 1e-5);
-
- BOOST_REQUIRE(results(5, 0) == 0);
- BOOST_REQUIRE(results(5, 1) == 5);
- BOOST_REQUIRE_CLOSE(results(5, 2), 0.27, 1e-5);
-
- BOOST_REQUIRE(results(6, 0) == 8);
- BOOST_REQUIRE(results(6, 1) == 9);
- BOOST_REQUIRE_CLOSE(results(6, 2), 0.46, 1e-5);
-
- BOOST_REQUIRE(results(7, 0) == 6);
- BOOST_REQUIRE(results(7, 1) == 7);
- BOOST_REQUIRE_CLOSE(results(7, 2), 0.7, 1e-5);
-
- BOOST_REQUIRE(results(8, 0) == 5);
- BOOST_REQUIRE(results(8, 1) == 7);
- BOOST_REQUIRE_CLOSE(results(8, 2), 1.08, 1e-5);
-
- BOOST_REQUIRE(results(9, 0) == 3);
- BOOST_REQUIRE(results(9, 1) == 4);
- BOOST_REQUIRE_CLOSE(results(9, 2), 3.8, 1e-5);
-
-
- }
-
- /**
- * Test the dual tree method against the naive computation.
- *
- * Errors are produced if the results are not identical.
- */
- BOOST_AUTO_TEST_CASE(dual_tree_vs_naive) {
- arma::mat input_data;
-
- // Hard-coded filename: bad!
- // Code duplication: also bad!
- if (!data::Load("test_data_3_1000.csv", input_data))
- BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
-
- // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
- arma::mat dual_data = arma::trans(input_data);
- arma::mat naive_data = arma::trans(input_data);
-
- // Reset parameters from last test.
- DualTreeBoruvka dtb;
- dtb.Init(dual_data);
-
- arma::mat dual_results;
- dtb.ComputeMST(dual_results);
-
- // Set naive mode.
- CLI::GetParam<bool>("naive/do_naive") = true;
-
- DualTreeBoruvka dtb_naive;
- dtb_naive.Init(naive_data);
-
- arma::mat naive_results;
- dtb_naive.ComputeMST(naive_results);
-
- BOOST_REQUIRE(dual_results.n_cols == naive_results.n_cols);
- BOOST_REQUIRE(dual_results.n_rows == naive_results.n_rows);
-
- for (size_t i = 0; i < dual_results.n_rows; i++) {
-
- BOOST_REQUIRE(dual_results(i,0) == naive_results(i,0));
- BOOST_REQUIRE(dual_results(i,1) == naive_results(i,1));
- BOOST_REQUIRE_CLOSE(dual_results(i,2), naive_results(i,2), 1e-5);
-
- }
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/emst_test.cpp (from rev 10220, mlpack/trunk/src/mlpack/tests/emst_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/emst_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/emst_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,132 @@
+/**
+ * @file emst_test.cpp
+ *
+ * Test file for EMST methods.
+ */
+#include <mlpack/core.h>
+#include <mlpack/methods/emst/dtb.hpp>
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace mlpack::emst;
+
+BOOST_AUTO_TEST_SUITE(EMSTTest);
+
+/**
+ * Simple emst test with small, synthetic dataset. This is an
+ * exhaustive test, which checks that each method for performing the calculation
+ * (dual-tree, single-tree, naive) produces the correct results. The dataset is
+ * in one dimension for simplicity -- the correct functionality of distance
+ * functions is not tested here.
+ */
+BOOST_AUTO_TEST_CASE(exhaustive_synthetic_test)
+{
+ // Set up our data.
+ arma::mat data(1, 11);
+ data[0] = 0.05; // Row addressing is unnecessary (they are all 0).
+ data[1] = 0.37;
+ data[2] = 0.15;
+ data[3] = 1.25;
+ data[4] = 5.05;
+ data[5] = -0.22;
+ data[6] = -2.00;
+ data[7] = -1.30;
+ data[8] = 0.45;
+ data[9] = 0.91;
+ data[10] = 1.00;
+
+ // Now perform the actual calculation.
+ arma::mat results;
+
+ DualTreeBoruvka dtb;
+ dtb.Init(data);
+ dtb.ComputeMST(results);
+
+ // Now the exhaustive check for correctness.
+ BOOST_REQUIRE(results(0, 0) == 1);
+ BOOST_REQUIRE(results(0, 1) == 8);
+ BOOST_REQUIRE_CLOSE(results(0, 2), 0.08, 1e-5);
+
+ BOOST_REQUIRE(results(1, 0) == 9);
+ BOOST_REQUIRE(results(1, 1) == 10);
+ BOOST_REQUIRE_CLOSE(results(1, 2), 0.09, 1e-5);
+
+ BOOST_REQUIRE(results(2, 0) == 0);
+ BOOST_REQUIRE(results(2, 1) == 2);
+ BOOST_REQUIRE_CLOSE(results(2, 2), 0.1, 1e-5);
+
+ BOOST_REQUIRE(results(3, 0) == 1);
+ BOOST_REQUIRE(results(3, 1) == 2);
+ BOOST_REQUIRE_CLOSE(results(3, 2), 0.22, 1e-5);
+
+ BOOST_REQUIRE(results(4, 0) == 3);
+ BOOST_REQUIRE(results(4, 1) == 10);
+ BOOST_REQUIRE_CLOSE(results(4, 2), 0.25, 1e-5);
+
+ BOOST_REQUIRE(results(5, 0) == 0);
+ BOOST_REQUIRE(results(5, 1) == 5);
+ BOOST_REQUIRE_CLOSE(results(5, 2), 0.27, 1e-5);
+
+ BOOST_REQUIRE(results(6, 0) == 8);
+ BOOST_REQUIRE(results(6, 1) == 9);
+ BOOST_REQUIRE_CLOSE(results(6, 2), 0.46, 1e-5);
+
+ BOOST_REQUIRE(results(7, 0) == 6);
+ BOOST_REQUIRE(results(7, 1) == 7);
+ BOOST_REQUIRE_CLOSE(results(7, 2), 0.7, 1e-5);
+
+ BOOST_REQUIRE(results(8, 0) == 5);
+ BOOST_REQUIRE(results(8, 1) == 7);
+ BOOST_REQUIRE_CLOSE(results(8, 2), 1.08, 1e-5);
+
+ BOOST_REQUIRE(results(9, 0) == 3);
+ BOOST_REQUIRE(results(9, 1) == 4);
+ BOOST_REQUIRE_CLOSE(results(9, 2), 3.8, 1e-5);
+}
+
+/**
+ * Test the dual tree method against the naive computation.
+ *
+ * Errors are produced if the results are not identical.
+ */
+BOOST_AUTO_TEST_CASE(dual_tree_vs_naive)
+{
+ arma::mat input_data;
+
+ // Hard-coded filename: bad!
+ // Code duplication: also bad!
+ if (!data::Load("test_data_3_1000.csv", input_data))
+ BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+
+ // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
+ arma::mat dual_data = arma::trans(input_data);
+ arma::mat naive_data = arma::trans(input_data);
+
+ // Reset parameters from last test.
+ DualTreeBoruvka dtb;
+ dtb.Init(dual_data);
+
+ arma::mat dual_results;
+ dtb.ComputeMST(dual_results);
+
+ // Set naive mode.
+ CLI::GetParam<bool>("naive/do_naive") = true;
+
+ DualTreeBoruvka dtb_naive;
+ dtb_naive.Init(naive_data);
+
+ arma::mat naive_results;
+ dtb_naive.ComputeMST(naive_results);
+
+ BOOST_REQUIRE(dual_results.n_cols == naive_results.n_cols);
+ BOOST_REQUIRE(dual_results.n_rows == naive_results.n_rows);
+
+ for (size_t i = 0; i < dual_results.n_rows; i++)
+ {
+ BOOST_REQUIRE(dual_results(i,0) == naive_results(i,0));
+ BOOST_REQUIRE(dual_results(i,1) == naive_results(i,1));
+ BOOST_REQUIRE_CLOSE(dual_results(i,2), naive_results(i,2), 1e-5);
+ }
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/infomax_ica_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/infomax_ica_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/infomax_ica_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,66 +0,0 @@
-/**
- * @file main.cc
- *
- * Test driver for our infomax ICA method.
- */
-#include <mlpack/core.h>
-
-#include <mlpack/methods/infomax_ica/infomax_ica.h>
-
-using namespace mlpack;
-using namespace infomax_ica;
-
-#include <boost/test/unit_test.hpp>
-
-void testSQRTM(InfomaxICA& icab_, arma::mat& m) {
- icab_.sqrtm(m);
-}
-
-BOOST_AUTO_TEST_SUITE(InfomaxIcaTest);
-
- BOOST_AUTO_TEST_CASE(SqrtM) {
- arma::mat testdatab_;
- double lambdab_ = 0.001;
- int bb_ = 5;
- double epsilonb_ = 0.001;
-
- data::Load("fake.csv", testdatab_);
-
- InfomaxICA icab_(lambdab_, bb_, epsilonb_);
-
- arma::mat intermediateb = icab_.sampleCovariance(testdatab_);
- testSQRTM(icab_, intermediateb);
- }
-
- BOOST_AUTO_TEST_CASE(TestCov) {
- arma::mat testdata_;
-
- double lambda_ = 0.001;
- int b_ = 5;
- double epsilon_ = 0.001;
-
- // load some test data that has been verified using the matlab
- // implementation of infomax
- data::Load("fake.csv", testdata_);
-
- InfomaxICA ica_(lambda_, b_, epsilon_);
- ica_.sampleCovariance(testdata_);
- }
-
- BOOST_AUTO_TEST_CASE(TestICA) {
- arma::mat testdata_;
- double lambda_ = 0.001;
- int b_ = 5;
- double epsilon_ = 0.001;
-
- // load some test data that has been verified using the matlab
- // implementation of infomax
- data::Load("fake.csv", testdata_);
-
- InfomaxICA ica_(lambda_, b_, epsilon_);
- arma::mat unmixing;
- ica_.applyICA(testdata_);
- ica_.getUnmixing(unmixing);
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/infomax_ica_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/infomax_ica_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/infomax_ica_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/infomax_ica_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,69 @@
+/**
+ * @file infomax_ica_test.cpp
+ *
+ * Test for the infomax ICA method.
+ */
+#include <mlpack/core.h>
+#include <mlpack/methods/infomax_ica/infomax_ica.h>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace infomax_ica;
+
+void testSQRTM(InfomaxICA& icab_, arma::mat& m)
+{
+ icab_.sqrtm(m);
+}
+
+BOOST_AUTO_TEST_SUITE(InfomaxICATest);
+
+BOOST_AUTO_TEST_CASE(SqrtM)
+{
+ arma::mat testdatab_;
+ double lambdab_ = 0.001;
+ int bb_ = 5;
+ double epsilonb_ = 0.001;
+
+ data::Load("fake.csv", testdatab_);
+
+ InfomaxICA icab_(lambdab_, bb_, epsilonb_);
+
+ arma::mat intermediateb = icab_.sampleCovariance(testdatab_);
+ testSQRTM(icab_, intermediateb);
+}
+
+BOOST_AUTO_TEST_CASE(TestCov)
+{
+ arma::mat testdata_;
+
+ double lambda_ = 0.001;
+ int b_ = 5;
+ double epsilon_ = 0.001;
+
+ // load some test data that has been verified using the matlab
+ // implementation of infomax
+ data::Load("fake.csv", testdata_);
+
+ InfomaxICA ica_(lambda_, b_, epsilon_);
+ ica_.sampleCovariance(testdata_);
+}
+
+BOOST_AUTO_TEST_CASE(TestICA)
+{
+ arma::mat testdata_;
+ double lambda_ = 0.001;
+ int b_ = 5;
+ double epsilon_ = 0.001;
+
+ // load some test data that has been verified using the matlab
+ // implementation of infomax
+ data::Load("fake.csv", testdata_);
+
+ InfomaxICA ica_(lambda_, b_, epsilon_);
+ arma::mat unmixing;
+ ica_.applyICA(testdata_);
+ ica_.getUnmixing(unmixing);
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/kernel_pca_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/kernel_pca_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/kernel_pca_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,80 +0,0 @@
-/*
- * =====================================================================================
- *
- * Filename: kernel_pca_test.cc
- *
- * Description:
- *
- * Version: 1.0
- * Created: 01/09/2008 11:26:48 AM EST
- * Revision: none
- * Compiler: gcc
- *
- * Author: Nikolaos Vasiloglou (NV), nvasil at ieee.org
- * Company: Georgia Tech Fastlab-ESP Lab
- *
- * =====================================================================================
- */
-#include <mlpack/methods/kernel_pca/kernel_pca.h>
-#include <vector>
-#include <mlpack/core.h>
-#include <boost/test/unit_test.hpp>
-
-BOOST_AUTO_TEST_SUITE(KernelPCATest);
-
- BOOST_AUTO_TEST_CASE(TestGeneralKernelPCA) {
- arma::mat eigen_vectors;
- arma::vec eigen_values;
-
- KernelPCA engine_;
- KernelPCA::GaussianKernel kernel_;
- engine_.Init("test_data_3_1000.csv", 5, 20);
- engine_.ComputeNeighborhoods();
- double bandwidth;
- engine_.EstimateBandwidth(&bandwidth);
- kernel_.set(bandwidth);
- engine_.LoadAffinityMatrix();
- engine_.ComputeGeneralKernelPCA(kernel_, 15,
- &eigen_vectors,
- &eigen_values);
- engine_.SaveToTextFile("results", eigen_vectors, eigen_values);
- }
-
- BOOST_AUTO_TEST_CASE(TestLLE) {
- arma::mat eigen_vectors;
- arma::vec eigen_values;
- KernelPCA engine_;
- KernelPCA::GaussianKernel kernel_;
- engine_.Init("test_data_3_1000.csv", 5, 20);
- engine_.ComputeNeighborhoods();
- engine_.LoadAffinityMatrix();
- engine_.ComputeLLE(2,
- &eigen_vectors,
- &eigen_values);
- engine_.SaveToTextFile("results", eigen_vectors, eigen_values);
- }
-
- BOOST_AUTO_TEST_CASE (TestSpectralRegression) {
- KernelPCA engine_;
- KernelPCA::GaussianKernel kernel_;
- engine_.Init("test_data_3_1000.csv", 5, 20);
- engine_.ComputeNeighborhoods();
- double bandwidth;
- engine_.EstimateBandwidth(&bandwidth);
- kernel_.set(bandwidth);
- engine_.LoadAffinityMatrix();
- std::map<size_t, size_t> data_label;
- for(size_t i=0; i<20; i++) {
- data_label[math::RandInt(0, engine_->data_.n_cols())] =
- math::RandInt(0 ,2);
- }
- arma::mat embedded_coordinates;
- arma::vec eigenvalues;
- engine_.ComputeSpectralRegression(kernel_,
- data_label,
- &embedded_coordinates,
- &eigenvalues);
- engine_.SaveToTextFile("results", embedded_coordinates, eigenvalues);
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/kernel_pca_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/kernel_pca_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/kernel_pca_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/kernel_pca_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,73 @@
+/**
+ * @file kernel_pca_test.cpp
+ *
+ * Test for Kernel PCA.
+ */
+#include <mlpack/core.h>
+#include <mlpack/methods/kernel_pca/kernel_pca.h>
+#include <vector>
+
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_SUITE(KernelPCATest);
+
+BOOST_AUTO_TEST_CASE(TestGeneralKernelPCA)
+{
+ arma::mat eigen_vectors;
+ arma::vec eigen_values;
+
+ KernelPCA engine_;
+ KernelPCA::GaussianKernel kernel_;
+ engine_.Init("test_data_3_1000.csv", 5, 20);
+ engine_.ComputeNeighborhoods();
+ double bandwidth;
+ engine_.EstimateBandwidth(&bandwidth);
+ kernel_.set(bandwidth);
+ engine_.LoadAffinityMatrix();
+ engine_.ComputeGeneralKernelPCA(kernel_, 15,
+ &eigen_vectors,
+ &eigen_values);
+ engine_.SaveToTextFile("results", eigen_vectors, eigen_values);
+}
+
+BOOST_AUTO_TEST_CASE(TestLLE)
+{
+ arma::mat eigen_vectors;
+ arma::vec eigen_values;
+ KernelPCA engine_;
+ KernelPCA::GaussianKernel kernel_;
+ engine_.Init("test_data_3_1000.csv", 5, 20);
+ engine_.ComputeNeighborhoods();
+ engine_.LoadAffinityMatrix();
+ engine_.ComputeLLE(2,
+ &eigen_vectors,
+ &eigen_values);
+ engine_.SaveToTextFile("results", eigen_vectors, eigen_values);
+}
+
+BOOST_AUTO_TEST_CASE (TestSpectralRegression)
+{
+ KernelPCA engine_;
+ KernelPCA::GaussianKernel kernel_;
+ engine_.Init("test_data_3_1000.csv", 5, 20);
+ engine_.ComputeNeighborhoods();
+ double bandwidth;
+ engine_.EstimateBandwidth(&bandwidth);
+ kernel_.set(bandwidth);
+ engine_.LoadAffinityMatrix();
+ std::map<size_t, size_t> data_label;
+ for (size_t i = 0; i < 20; i++)
+ {
+ data_label[math::RandInt(0, engine_->data_.n_cols())] =
+ math::RandInt(0 ,2);
+ }
+ arma::mat embedded_coordinates;
+ arma::vec eigenvalues;
+ engine_.ComputeSpectralRegression(kernel_,
+ data_label,
+ &embedded_coordinates,
+ &eigenvalues);
+ engine_.SaveToTextFile("results", embedded_coordinates, eigenvalues);
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/kernel_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/kernel_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/kernel_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,228 +0,0 @@
-/***
- * @file kernel_test.cc
- * @author Ryan Curtin
- *
- * Tests for the various kernel classes.
- */
-
-#include <mlpack/core/kernels/lmetric.hpp>
-#include <mlpack/core/kernels/mahalanobis_distance.hpp>
-#include <mlpack/core/kernels/cosine_distance.hpp>
-#include <mlpack/core/kernels/gaussian_kernel.hpp>
-#include <mlpack/core/kernels/linear_kernel.hpp>
-
-#include <boost/test/unit_test.hpp>
-
-using namespace mlpack;
-using namespace mlpack::kernel;
-
-BOOST_AUTO_TEST_SUITE(KernelTest);
-
- /***
- * Basic test of the Manhattan distance.
- */
- BOOST_AUTO_TEST_CASE(manhattan_distance) {
- // A couple quick tests.
- arma::vec a = "1.0 3.0 4.0";
- arma::vec b = "3.0 3.0 5.0";
-
- BOOST_REQUIRE_CLOSE(ManhattanDistance::Evaluate(a, b), 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(ManhattanDistance::Evaluate(b, a), 3.0, 1e-5);
-
- // Check also for when the root is taken (should be the same).
- BOOST_REQUIRE_CLOSE((LMetric<1, true>::Evaluate(a, b)), 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE((LMetric<1, true>::Evaluate(b, a)), 3.0, 1e-5);
- }
-
- /***
- * Basic test of squared Euclidean distance.
- */
- BOOST_AUTO_TEST_CASE(squared_euclidean_distance) {
- // Sample 2-dimensional vectors.
- arma::vec a = "1.0 2.0";
- arma::vec b = "0.0 -2.0";
-
- BOOST_REQUIRE_CLOSE(SquaredEuclideanDistance::Evaluate(a, b), 17.0, 1e-5);
- BOOST_REQUIRE_CLOSE(SquaredEuclideanDistance::Evaluate(b, a), 17.0, 1e-5);
- }
-
- /***
- * Basic test of Euclidean distance.
- */
- BOOST_AUTO_TEST_CASE(euclidean_distance) {
- arma::vec a = "1.0 3.0 5.0 7.0";
- arma::vec b = "4.0 0.0 2.0 0.0";
-
- BOOST_REQUIRE_CLOSE(EuclideanDistance::Evaluate(a, b), sqrt(76.0), 1e-5);
- BOOST_REQUIRE_CLOSE(EuclideanDistance::Evaluate(b, a), sqrt(76.0), 1e-5);
- }
-
- /***
- * Arbitrary test case for coverage.
- */
- BOOST_AUTO_TEST_CASE(arbitrary_case) {
- arma::vec a = "3.0 5.0 6.0 7.0";
- arma::vec b = "1.0 2.0 1.0 0.0";
-
- BOOST_REQUIRE_CLOSE(LMetric<3>::Evaluate(a, b), 503.0, 1e-5);
- BOOST_REQUIRE_CLOSE(LMetric<3>::Evaluate(b, a), 503.0, 1e-5);
-
- BOOST_REQUIRE_CLOSE((LMetric<3, true>::Evaluate(a, b)), 7.95284762, 1e-5);
- BOOST_REQUIRE_CLOSE((LMetric<3, true>::Evaluate(b, a)), 7.95284762, 1e-5);
- }
-
- /***
- * Make sure two vectors of all zeros return zero distance, for a few different
- * powers.
- */
- BOOST_AUTO_TEST_CASE(lmetric_zeros) {
- arma::vec a(250);
- a.fill(0.0);
-
- // We cannot use a loop because compilers seem to be unable to unroll the loop
- // and realize the variable actually is knowable at compile-time.
- BOOST_REQUIRE((LMetric<1, false>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<1, true>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<2, false>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<2, true>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<3, false>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<3, true>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<4, false>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<4, true>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<5, false>::Evaluate(a, a)) == 0);
- BOOST_REQUIRE((LMetric<5, true>::Evaluate(a, a)) == 0);
- }
-
- /***
- * Simple test of Mahalanobis distance with unset covariance matrix.
- */
- BOOST_AUTO_TEST_CASE(md_unset_covariance) {
- MahalanobisDistance<false> md;
- arma::vec a = "1.0 2.0 2.0 3.0";
- arma::vec b = "0.0 0.0 1.0 3.0";
-
- BOOST_REQUIRE_CLOSE(md.Evaluate(a, b), 6.0, 1e-5);
- BOOST_REQUIRE_CLOSE(md.Evaluate(b, a), 6.0, 1e-5);
- }
-
- /***
- * Simple test of Mahalanobis distance with unset covariance matrix and
- * t_take_root set to true.
- */
- BOOST_AUTO_TEST_CASE(md_root_unset_covariance) {
- MahalanobisDistance<true> md;
- arma::vec a = "1.0 2.0 2.5 5.0";
- arma::vec b = "0.0 2.0 0.5 8.0";
-
- BOOST_REQUIRE_CLOSE(md.Evaluate(a, b), sqrt(14.0), 1e-5);
- BOOST_REQUIRE_CLOSE(md.Evaluate(b, a), sqrt(14.0), 1e-5);
- }
-
- /***
- * Simple test with diagonal covariance matrix.
- */
- BOOST_AUTO_TEST_CASE(md_diagonal_covariance) {
- arma::mat cov = arma::eye<arma::mat>(5, 5);
- cov(0, 0) = 2.0;
- cov(1, 1) = 0.5;
- cov(2, 2) = 3.0;
- cov(3, 3) = 1.0;
- cov(4, 4) = 1.5;
- MahalanobisDistance<false> md(cov);
-
- arma::vec a = "1.0 2.0 2.0 4.0 5.0";
- arma::vec b = "2.0 3.0 1.0 1.0 0.0";
-
- BOOST_REQUIRE_CLOSE(md.Evaluate(a, b), 52.0, 1e-5);
- BOOST_REQUIRE_CLOSE(md.Evaluate(b, a), 52.0, 1e-5);
- }
-
- /***
- * More specific case with more difficult covariance matrix.
- */
- BOOST_AUTO_TEST_CASE(md_full_covariance) {
- arma::mat cov = "1.0 2.0 3.0 4.0;"
- "0.5 0.6 0.7 0.1;"
- "3.4 4.3 5.0 6.1;"
- "1.0 2.0 4.0 1.0;";
- MahalanobisDistance<false> md(cov);
-
- arma::vec a = "1.0 2.0 2.0 4.0";
- arma::vec b = "2.0 3.0 1.0 1.0";
-
- BOOST_REQUIRE_CLOSE(md.Evaluate(a, b), 15.7, 1e-5);
- BOOST_REQUIRE_CLOSE(md.Evaluate(b, a), 15.7, 1e-5);
- }
-
- /***
- * Simple test case for the cosine distance.
- */
- BOOST_AUTO_TEST_CASE(cosine_distance_same_angle) {
- arma::vec a = "1.0 2.0 3.0";
- arma::vec b = "2.0 4.0 6.0";
-
- BOOST_REQUIRE_SMALL(CosineDistance::Evaluate(a, b), 1e-5);
- BOOST_REQUIRE_SMALL(CosineDistance::Evaluate(b, a), 1e-5);
- }
-
- /***
- * Now let's have them be orthogonal.
- */
- BOOST_AUTO_TEST_CASE(cosine_distance_orthogonal) {
- arma::vec a = "0.0 1.0";
- arma::vec b = "1.0 0.0";
-
- BOOST_REQUIRE_CLOSE(CosineDistance::Evaluate(a, b), 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(CosineDistance::Evaluate(b, a), 1.0, 1e-5);
- }
-
- /***
- * Some random angle test.
- */
- BOOST_AUTO_TEST_CASE(cosine_distance_random_test) {
- arma::vec a = "0.1 0.2 0.3 0.4 0.5";
- arma::vec b = "1.2 1.0 0.8 -0.3 -0.5";
-
- BOOST_REQUIRE_CLOSE(CosineDistance::Evaluate(a, b), 1 - 0.1385349024, 1e-5);
- BOOST_REQUIRE_CLOSE(CosineDistance::Evaluate(b, a), 1 - 0.1385349024, 1e-5);
- }
-
- /***
- * Linear Kernel test.
- */
- BOOST_AUTO_TEST_CASE(linear_kernel) {
- arma::vec a = ".2 .3 .4 .1";
- arma::vec b = ".56 .21 .623 .82";
-
- LinearKernel lk;
- BOOST_REQUIRE_CLOSE(lk.Evaluate(a,b), .5062, 1e-5);
- BOOST_REQUIRE_CLOSE(lk.Evaluate(b,a), .5062, 1e-5);
- }
-
- /***
- * Linear Kernel test, orthogonal vectors.
- */
- BOOST_AUTO_TEST_CASE(linear_kernel_orthogonal) {
- arma::vec a = "1 0 0";
- arma::vec b = "0 0 1";
-
- LinearKernel lk;
- BOOST_REQUIRE_SMALL(lk.Evaluate(a,b), 1e-5);
- BOOST_REQUIRE_SMALL(lk.Evaluate(b,a), 1e-5);
- }
-
- BOOST_AUTO_TEST_CASE(gaussian_kernel) {
- arma::vec a = "1 0 0";
- arma::vec b = "0 1 0";
- arma::vec c = "0 0 1";
-
- GaussianKernel gk(.5);
- BOOST_REQUIRE_CLOSE(gk.Evaluate(a,b), .018315638888734, 1e-5);
- BOOST_REQUIRE_CLOSE(gk.Evaluate(b,a), .018315638888734, 1e-5);
- BOOST_REQUIRE_CLOSE(gk.Evaluate(a,c), .018315638888734, 1e-5);
- BOOST_REQUIRE_CLOSE(gk.Evaluate(c,a), .018315638888734, 1e-5);
- BOOST_REQUIRE_CLOSE(gk.Evaluate(b,c), .018315638888734, 1e-5);
- BOOST_REQUIRE_CLOSE(gk.Evaluate(c,b), .018315638888734, 1e-5);
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/kernel_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/kernel_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/kernel_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/kernel_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,242 @@
+/**
+ * @file kernel_test.cpp
+ * @author Ryan Curtin
+ *
+ * Tests for the various kernel classes.
+ */
+#include <mlpack/core/kernels/lmetric.hpp>
+#include <mlpack/core/kernels/mahalanobis_distance.hpp>
+#include <mlpack/core/kernels/cosine_distance.hpp>
+#include <mlpack/core/kernels/gaussian_kernel.hpp>
+#include <mlpack/core/kernels/linear_kernel.hpp>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace mlpack::kernel;
+
+BOOST_AUTO_TEST_SUITE(KernelTest);
+
+/**
+ * Basic test of the Manhattan distance.
+ */
+BOOST_AUTO_TEST_CASE(manhattan_distance)
+{
+ // A couple quick tests.
+ arma::vec a = "1.0 3.0 4.0";
+ arma::vec b = "3.0 3.0 5.0";
+
+ BOOST_REQUIRE_CLOSE(ManhattanDistance::Evaluate(a, b), 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(ManhattanDistance::Evaluate(b, a), 3.0, 1e-5);
+
+ // Check also for when the root is taken (should be the same).
+ BOOST_REQUIRE_CLOSE((LMetric<1, true>::Evaluate(a, b)), 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE((LMetric<1, true>::Evaluate(b, a)), 3.0, 1e-5);
+}
+
+/**
+ * Basic test of squared Euclidean distance.
+ */
+BOOST_AUTO_TEST_CASE(squared_euclidean_distance)
+{
+ // Sample 2-dimensional vectors.
+ arma::vec a = "1.0 2.0";
+ arma::vec b = "0.0 -2.0";
+
+ BOOST_REQUIRE_CLOSE(SquaredEuclideanDistance::Evaluate(a, b), 17.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(SquaredEuclideanDistance::Evaluate(b, a), 17.0, 1e-5);
+}
+
+/**
+ * Basic test of Euclidean distance.
+ */
+BOOST_AUTO_TEST_CASE(euclidean_distance)
+{
+ arma::vec a = "1.0 3.0 5.0 7.0";
+ arma::vec b = "4.0 0.0 2.0 0.0";
+
+ BOOST_REQUIRE_CLOSE(EuclideanDistance::Evaluate(a, b), sqrt(76.0), 1e-5);
+ BOOST_REQUIRE_CLOSE(EuclideanDistance::Evaluate(b, a), sqrt(76.0), 1e-5);
+}
+
+/**
+ * Arbitrary test case for coverage.
+ */
+BOOST_AUTO_TEST_CASE(arbitrary_case)
+{
+ arma::vec a = "3.0 5.0 6.0 7.0";
+ arma::vec b = "1.0 2.0 1.0 0.0";
+
+ BOOST_REQUIRE_CLOSE(LMetric<3>::Evaluate(a, b), 503.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(LMetric<3>::Evaluate(b, a), 503.0, 1e-5);
+
+ BOOST_REQUIRE_CLOSE((LMetric<3, true>::Evaluate(a, b)), 7.95284762, 1e-5);
+ BOOST_REQUIRE_CLOSE((LMetric<3, true>::Evaluate(b, a)), 7.95284762, 1e-5);
+}
+
+/**
+ * Make sure two vectors of all zeros return zero distance, for a few different
+ * powers.
+ */
+BOOST_AUTO_TEST_CASE(lmetric_zeros)
+{
+ arma::vec a(250);
+ a.fill(0.0);
+
+ // We cannot use a loop because compilers seem to be unable to unroll the loop
+ // and realize the variable actually is knowable at compile-time.
+ BOOST_REQUIRE((LMetric<1, false>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<1, true>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<2, false>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<2, true>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<3, false>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<3, true>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<4, false>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<4, true>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<5, false>::Evaluate(a, a)) == 0);
+ BOOST_REQUIRE((LMetric<5, true>::Evaluate(a, a)) == 0);
+}
+
+/**
+ * Simple test of Mahalanobis distance with unset covariance matrix.
+ */
+BOOST_AUTO_TEST_CASE(md_unset_covariance)
+{
+ MahalanobisDistance<false> md;
+ arma::vec a = "1.0 2.0 2.0 3.0";
+ arma::vec b = "0.0 0.0 1.0 3.0";
+
+ BOOST_REQUIRE_CLOSE(md.Evaluate(a, b), 6.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(md.Evaluate(b, a), 6.0, 1e-5);
+}
+
+/**
+ * Simple test of Mahalanobis distance with unset covariance matrix and
+ * t_take_root set to true.
+ */
+BOOST_AUTO_TEST_CASE(md_root_unset_covariance)
+{
+ MahalanobisDistance<true> md;
+ arma::vec a = "1.0 2.0 2.5 5.0";
+ arma::vec b = "0.0 2.0 0.5 8.0";
+
+ BOOST_REQUIRE_CLOSE(md.Evaluate(a, b), sqrt(14.0), 1e-5);
+ BOOST_REQUIRE_CLOSE(md.Evaluate(b, a), sqrt(14.0), 1e-5);
+}
+
+/**
+ * Simple test with diagonal covariance matrix.
+ */
+BOOST_AUTO_TEST_CASE(md_diagonal_covariance)
+{
+ arma::mat cov = arma::eye<arma::mat>(5, 5);
+ cov(0, 0) = 2.0;
+ cov(1, 1) = 0.5;
+ cov(2, 2) = 3.0;
+ cov(3, 3) = 1.0;
+ cov(4, 4) = 1.5;
+ MahalanobisDistance<false> md(cov);
+
+ arma::vec a = "1.0 2.0 2.0 4.0 5.0";
+ arma::vec b = "2.0 3.0 1.0 1.0 0.0";
+
+ BOOST_REQUIRE_CLOSE(md.Evaluate(a, b), 52.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(md.Evaluate(b, a), 52.0, 1e-5);
+}
+
+/**
+ * More specific case with more difficult covariance matrix.
+ */
+BOOST_AUTO_TEST_CASE(md_full_covariance)
+{
+ arma::mat cov = "1.0 2.0 3.0 4.0;"
+ "0.5 0.6 0.7 0.1;"
+ "3.4 4.3 5.0 6.1;"
+ "1.0 2.0 4.0 1.0;";
+ MahalanobisDistance<false> md(cov);
+
+ arma::vec a = "1.0 2.0 2.0 4.0";
+ arma::vec b = "2.0 3.0 1.0 1.0";
+
+ BOOST_REQUIRE_CLOSE(md.Evaluate(a, b), 15.7, 1e-5);
+ BOOST_REQUIRE_CLOSE(md.Evaluate(b, a), 15.7, 1e-5);
+}
+
+/**
+ * Simple test case for the cosine distance.
+ */
+BOOST_AUTO_TEST_CASE(cosine_distance_same_angle)
+{
+ arma::vec a = "1.0 2.0 3.0";
+ arma::vec b = "2.0 4.0 6.0";
+
+ BOOST_REQUIRE_SMALL(CosineDistance::Evaluate(a, b), 1e-5);
+ BOOST_REQUIRE_SMALL(CosineDistance::Evaluate(b, a), 1e-5);
+}
+
+/**
+ * Now let's have them be orthogonal.
+ */
+BOOST_AUTO_TEST_CASE(cosine_distance_orthogonal)
+{
+ arma::vec a = "0.0 1.0";
+ arma::vec b = "1.0 0.0";
+
+ BOOST_REQUIRE_CLOSE(CosineDistance::Evaluate(a, b), 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(CosineDistance::Evaluate(b, a), 1.0, 1e-5);
+}
+
+/**
+ * Some random angle test.
+ */
+BOOST_AUTO_TEST_CASE(cosine_distance_random_test)
+{
+ arma::vec a = "0.1 0.2 0.3 0.4 0.5";
+ arma::vec b = "1.2 1.0 0.8 -0.3 -0.5";
+
+ BOOST_REQUIRE_CLOSE(CosineDistance::Evaluate(a, b), 1 - 0.1385349024, 1e-5);
+ BOOST_REQUIRE_CLOSE(CosineDistance::Evaluate(b, a), 1 - 0.1385349024, 1e-5);
+}
+
+/**
+ * Linear Kernel test.
+ */
+BOOST_AUTO_TEST_CASE(linear_kernel)
+{
+ arma::vec a = ".2 .3 .4 .1";
+ arma::vec b = ".56 .21 .623 .82";
+
+ LinearKernel lk;
+ BOOST_REQUIRE_CLOSE(lk.Evaluate(a,b), .5062, 1e-5);
+ BOOST_REQUIRE_CLOSE(lk.Evaluate(b,a), .5062, 1e-5);
+}
+
+/**
+ * Linear Kernel test, orthogonal vectors.
+ */
+BOOST_AUTO_TEST_CASE(linear_kernel_orthogonal)
+{
+ arma::vec a = "1 0 0";
+ arma::vec b = "0 0 1";
+
+ LinearKernel lk;
+ BOOST_REQUIRE_SMALL(lk.Evaluate(a,b), 1e-5);
+ BOOST_REQUIRE_SMALL(lk.Evaluate(b,a), 1e-5);
+}
+
+BOOST_AUTO_TEST_CASE(gaussian_kernel)
+{
+ arma::vec a = "1 0 0";
+ arma::vec b = "0 1 0";
+ arma::vec c = "0 0 1";
+
+ GaussianKernel gk(.5);
+ BOOST_REQUIRE_CLOSE(gk.Evaluate(a,b), .018315638888734, 1e-5);
+ BOOST_REQUIRE_CLOSE(gk.Evaluate(b,a), .018315638888734, 1e-5);
+ BOOST_REQUIRE_CLOSE(gk.Evaluate(a,c), .018315638888734, 1e-5);
+ BOOST_REQUIRE_CLOSE(gk.Evaluate(c,a), .018315638888734, 1e-5);
+ BOOST_REQUIRE_CLOSE(gk.Evaluate(b,c), .018315638888734, 1e-5);
+ BOOST_REQUIRE_CLOSE(gk.Evaluate(c,b), .018315638888734, 1e-5);
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/lbfgs_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/lbfgs_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/lbfgs_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,104 +1,110 @@
-/***
- * @file lbfgs_test.cc
+/**
+ * @file lbfgs_test.cpp
*
* Tests the L-BFGS optimizer on a couple test functions.
*
* @author Ryan Curtin (gth671b at mail.gatech.edu)
*/
-
#include <mlpack/core.h>
#include <mlpack/core/optimizers/lbfgs/lbfgs.hpp>
#include <mlpack/core/optimizers/lbfgs/test_functions.hpp>
+
#include <boost/test/unit_test.hpp>
using namespace mlpack::optimization;
using namespace mlpack::optimization::test;
-BOOST_AUTO_TEST_SUITE(LbfgsTest);
+BOOST_AUTO_TEST_SUITE(LBFGSTest);
- /***
- * Tests the L-BFGS optimizer using the Rosenbrock Function.
- */
- BOOST_AUTO_TEST_CASE(rosenbrock_function) {
- RosenbrockFunction f;
- L_BFGS<RosenbrockFunction> lbfgs(f, 10);
+/**
+ * Tests the L-BFGS optimizer using the Rosenbrock Function.
+ */
+BOOST_AUTO_TEST_CASE(rosenbrock_function)
+{
+ RosenbrockFunction f;
+ L_BFGS<RosenbrockFunction> lbfgs(f, 10);
- arma::vec coords = f.GetInitialPoint();
- if(!lbfgs.Optimize(0, coords))
- BOOST_FAIL("L-BFGS optimization reported failure.");
+ arma::vec coords = f.GetInitialPoint();
+ if(!lbfgs.Optimize(0, coords))
+ BOOST_FAIL("L-BFGS optimization reported failure.");
- double final_value = f.Evaluate(coords);
+ double final_value = f.Evaluate(coords);
- BOOST_REQUIRE_SMALL(final_value, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[0], 1, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[1], 1, 1e-5);
- }
+ BOOST_REQUIRE_SMALL(final_value, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[0], 1, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[1], 1, 1e-5);
+}
- /***
- * Tests the L-BFGS optimizer using the Wood Function.
- */
- BOOST_AUTO_TEST_CASE(wood_function) {
- WoodFunction f;
- L_BFGS<WoodFunction> lbfgs(f, 10);
+/**
+ * Tests the L-BFGS optimizer using the Wood Function.
+ */
+BOOST_AUTO_TEST_CASE(wood_function)
+{
+ WoodFunction f;
+ L_BFGS<WoodFunction> lbfgs(f, 10);
+ arma::vec coords = f.GetInitialPoint();
+ if(!lbfgs.Optimize(0, coords))
+ BOOST_FAIL("L-BFGS optimization reported failure.");
+
+ double final_value = f.Evaluate(coords);
+
+ BOOST_REQUIRE_SMALL(final_value, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[0], 1, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[1], 1, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[2], 1, 1e-5);
+ BOOST_REQUIRE_CLOSE(coords[3], 1, 1e-5);
+}
+
+/**
+ * Tests the L-BFGS optimizer using the generalized Rosenbrock function. This
+ * is actually multiple tests, increasing the dimension by powers of 2, from 4
+ * dimensions to 1024 dimensions.
+ */
+BOOST_AUTO_TEST_CASE(generalized_rosenbrock_function)
+{
+ for (int i = 2; i < 10; i++)
+ {
+ // Dimension: powers of 2
+ int dim = std::pow(2, i);
+
+ GeneralizedRosenbrockFunction f(dim);
+ L_BFGS<GeneralizedRosenbrockFunction> lbfgs(f, 20);
+
arma::vec coords = f.GetInitialPoint();
if(!lbfgs.Optimize(0, coords))
BOOST_FAIL("L-BFGS optimization reported failure.");
double final_value = f.Evaluate(coords);
+ // Test the output to make sure it is correct.
BOOST_REQUIRE_SMALL(final_value, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[0], 1, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[1], 1, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[2], 1, 1e-5);
- BOOST_REQUIRE_CLOSE(coords[3], 1, 1e-5);
+ for (int j = 0; j < dim; j++)
+ BOOST_REQUIRE_CLOSE(coords[j], 1, 1e-5);
}
+}
- /***
- * Tests the L-BFGS optimizer using the generalized Rosenbrock function. This
- * is actually multiple tests, increasing the dimension by powers of 2, from 4
- * dimensions to 1024 dimensions.
- */
- BOOST_AUTO_TEST_CASE(generalized_rosenbrock_function) {
- for (int i = 2; i < 10; i++) {
- // Dimension: powers of 2
- int dim = std::pow(2, i);
+/**
+ * Tests the L-BFGS optimizer using the Rosenbrock-Wood combined function. This
+ * is a test on optimizing a matrix of coordinates.
+ */
+BOOST_AUTO_TEST_CASE(rosenbrock_wood_function)
+{
+ RosenbrockWoodFunction f;
+ L_BFGS<RosenbrockWoodFunction> lbfgs(f, 10);
- GeneralizedRosenbrockFunction f(dim);
- L_BFGS<GeneralizedRosenbrockFunction> lbfgs(f, 20);
+ arma::mat coords = f.GetInitialPoint();
+ if(!lbfgs.Optimize(0, coords))
+ BOOST_FAIL("L-BFGS optimization reported failure.");
- arma::vec coords = f.GetInitialPoint();
- if(!lbfgs.Optimize(0, coords))
- BOOST_FAIL("L-BFGS optimization reported failure.");
+ double final_value = f.Evaluate(coords);
- double final_value = f.Evaluate(coords);
-
- // Test the output to make sure it is correct.
- BOOST_REQUIRE_SMALL(final_value, 1e-5);
- for (int j = 0; j < dim; j++)
- BOOST_REQUIRE_CLOSE(coords[j], 1, 1e-5);
- }
- };
-
- /***
- * Tests the L-BFGS optimizer using the Rosenbrock-Wood combined function. This
- * is a test on optimizing a matrix of coordinates.
- */
- BOOST_AUTO_TEST_CASE(rosenbrock_wood_function) {
- RosenbrockWoodFunction f;
- L_BFGS<RosenbrockWoodFunction> lbfgs(f, 10);
-
- arma::mat coords = f.GetInitialPoint();
- if(!lbfgs.Optimize(0, coords))
- BOOST_FAIL("L-BFGS optimization reported failure.");
-
- double final_value = f.Evaluate(coords);
-
- BOOST_REQUIRE_SMALL(final_value, 1e-5);
- for (int row = 0; row < 4; row++) {
- BOOST_REQUIRE_CLOSE((coords(row, 0)), 1, 1e-5);
- BOOST_REQUIRE_CLOSE((coords(row, 1)), 1, 1e-5);
- }
+ BOOST_REQUIRE_SMALL(final_value, 1e-5);
+ for (int row = 0; row < 4; row++)
+ {
+ BOOST_REQUIRE_CLOSE((coords(row, 0)), 1, 1e-5);
+ BOOST_REQUIRE_CLOSE((coords(row, 1)), 1, 1e-5);
}
+}
BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/lin_alg_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/lin_alg_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/lin_alg_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,5 +1,5 @@
-/***
- * lin_alg_test.cc
+/**
+ * lin_alg_test.cpp
*
* Simple tests for things in the linalg__private namespace.
* Partly so I can be sure that my changes are working.
@@ -8,121 +8,129 @@
* @author Ryan Curtin
*/
#include <mlpack/core.h>
-
#include <mlpack/methods/fastica/lin_alg.hpp>
+#include <boost/test/unit_test.hpp>
+
using namespace arma;
using namespace mlpack;
-using namespace fastica;
-using namespace linalg__private;
+using namespace mlpack::fastica;
+using namespace mlpack::fastica::linalg__private;
-#include <boost/test/unit_test.hpp>
-
BOOST_AUTO_TEST_SUITE(LinAlgTest);
- /***
- * Test for linalg__private::Center(). There are no edge cases here, so we'll
- * just try it once for now.
- */
- BOOST_AUTO_TEST_CASE(TestCenterA) {
- mat tmp(5, 5);
- // [[0 0 0 0 0]
- // [1 2 3 4 5]
- // [2 4 6 8 10]
- // [3 6 9 12 15]
- // [4 8 12 16 20]]
- for (int row = 0; row < 5; row++) {
- for (int col = 0; col < 5; col++)
- tmp(row, col) = row * (col + 1);
- }
+/**
+ * Test for linalg__private::Center(). There are no edge cases here, so we'll
+ * just try it once for now.
+ */
+BOOST_AUTO_TEST_CASE(TestCenterA)
+{
+ mat tmp(5, 5);
+ // [[0 0 0 0 0]
+ // [1 2 3 4 5]
+ // [2 4 6 8 10]
+ // [3 6 9 12 15]
+ // [4 8 12 16 20]]
+ for (int row = 0; row < 5; row++)
+ for (int col = 0; col < 5; col++)
+ tmp(row, col) = row * (col + 1);
- mat tmp_out;
- Center(tmp, tmp_out);
+ mat tmp_out;
+ Center(tmp, tmp_out);
- // average should be
- // [[0 3 6 9 12]]'
- // so result should be
- // [[ 0 0 0 0 0]
- // [-2 -1 0 1 2 ]
- // [-4 -2 0 2 4 ]
- // [-6 -3 0 3 6 ]
- // [-8 -4 0 4 8]]
- for (int row = 0; row < 5; row++) {
- for (int col = 0; col < 5; col++) {
- BOOST_REQUIRE_CLOSE(tmp_out(row, col), (double) (col - 2) * row, 1e-5);
- }
- }
- }
+ // average should be
+ // [[0 3 6 9 12]]'
+ // so result should be
+ // [[ 0 0 0 0 0]
+ // [-2 -1 0 1 2 ]
+ // [-4 -2 0 2 4 ]
+ // [-6 -3 0 3 6 ]
+ // [-8 -4 0 4 8]]
+ for (int row = 0; row < 5; row++)
+ for (int col = 0; col < 5; col++)
+ BOOST_REQUIRE_CLOSE(tmp_out(row, col), (double) (col - 2) * row, 1e-5);
+}
- BOOST_AUTO_TEST_CASE(TestCenterB) {
- mat tmp(5, 6);
- for (int row = 0; row < 5; row++) {
- for (int col = 0; col < 6; col++)
- tmp(row, col) = row * (col + 1);
- }
+BOOST_AUTO_TEST_CASE(TestCenterB)
+{
+ mat tmp(5, 6);
+ for (int row = 0; row < 5; row++)
+ for (int col = 0; col < 6; col++)
+ tmp(row, col) = row * (col + 1);
- mat tmp_out;
- Center(tmp, tmp_out);
+ mat tmp_out;
+ Center(tmp, tmp_out);
- // average should be
- // [[0 3.5 7 10.5 14]]'
- // so result should be
- // [[ 0 0 0 0 0 0 ]
- // [-2.5 -1.5 -0.5 0.5 1.5 2.5]
- // [-5 -3 -1 1 3 5 ]
- // [-7.5 -4.5 -1.5 1.5 1.5 4.5]
- // [-10 -6 -2 2 6 10 ]]
- for (int row = 0; row < 5; row++) {
- for (int col = 0; col < 6; col++) {
- BOOST_REQUIRE_CLOSE(tmp_out(row, col), (double) (col - 2.5) * row, 1e-5);
- }
- }
- }
+ // average should be
+ // [[0 3.5 7 10.5 14]]'
+ // so result should be
+ // [[ 0 0 0 0 0 0 ]
+ // [-2.5 -1.5 -0.5 0.5 1.5 2.5]
+ // [-5 -3 -1 1 3 5 ]
+ // [-7.5 -4.5 -1.5 1.5 1.5 4.5]
+ // [-10 -6 -2 2 6 10 ]]
+ for (int row = 0; row < 5; row++)
+ for (int col = 0; col < 6; col++)
+ BOOST_REQUIRE_CLOSE(tmp_out(row, col), (double) (col - 2.5) * row, 1e-5);
+}
- BOOST_AUTO_TEST_CASE(TestWhitenUsingEig) {
- // After whitening using eigendecomposition, the covariance of
- // our matrix will be I (or something very close to that).
- // We are loading a matrix from an external file... bad choice.
- mat tmp, tmp_centered, whitened, whitening_matrix;
+BOOST_AUTO_TEST_CASE(TestWhitenUsingEig)
+{
+ // After whitening using eigendecomposition, the covariance of
+ // our matrix will be I (or something very close to that).
+ // We are loading a matrix from an external file... bad choice.
+ mat tmp, tmp_centered, whitened, whitening_matrix;
- data::Load("trainSet.csv", tmp);
- Center(tmp, tmp_centered);
- WhitenUsingEig(tmp_centered, whitened, whitening_matrix);
+ data::Load("trainSet.csv", tmp);
+ Center(tmp, tmp_centered);
+ WhitenUsingEig(tmp_centered, whitened, whitening_matrix);
- mat newcov = ccov(whitened);
- for (int row = 0; row < 5; row++) {
- for (int col = 0; col < 5; col++) {
- if (row == col) {
- // diagonal will be 0 in the case of any zero-valued eigenvalues
- // (rank-deficient covariance case)
- if (std::abs(newcov(row, col)) > 1e-10)
- BOOST_REQUIRE_CLOSE(newcov(row, col), 1.0, 1e-10);
- } else {
- BOOST_REQUIRE_SMALL(newcov(row, col), 1e-10);
- }
+ mat newcov = ccov(whitened);
+ for (int row = 0; row < 5; row++)
+ {
+ for (int col = 0; col < 5; col++)
+ {
+ if (row == col)
+ {
+ // diagonal will be 0 in the case of any zero-valued eigenvalues
+ // (rank-deficient covariance case)
+ if (std::abs(newcov(row, col)) > 1e-10)
+ BOOST_REQUIRE_CLOSE(newcov(row, col), 1.0, 1e-10);
}
+ else
+ {
+ BOOST_REQUIRE_SMALL(newcov(row, col), 1e-10);
+ }
}
}
+}
- BOOST_AUTO_TEST_CASE(TestOrthogonalize) {
- // Generate a random matrix; then, orthogonalize it and test if it's
- // orthogonal.
- mat tmp, orth;
- data::Load("fake.csv", tmp);
- Orthogonalize(tmp, orth);
+BOOST_AUTO_TEST_CASE(TestOrthogonalize)
+{
+ // Generate a random matrix; then, orthogonalize it and test if it's
+ // orthogonal.
+ mat tmp, orth;
+ data::Load("fake.csv", tmp);
+ Orthogonalize(tmp, orth);
- // test orthogonality
- mat test = ccov(orth);
- double ival = test(0, 0);
- for (size_t row = 0; row < test.n_rows; row++) {
- for (size_t col = 0; col < test.n_cols; col++) {
- if (row == col) {
- if (std::abs(test(row, col)) > 1e-10)
- BOOST_REQUIRE_CLOSE(test(row, col), ival, 1e-10);
- } else {
- BOOST_REQUIRE_SMALL(test(row, col), 1e-10);
- }
+ // test orthogonality
+ mat test = ccov(orth);
+ double ival = test(0, 0);
+ for (size_t row = 0; row < test.n_rows; row++)
+ {
+ for (size_t col = 0; col < test.n_cols; col++)
+ {
+ if (row == col)
+ {
+ if (std::abs(test(row, col)) > 1e-10)
+ BOOST_REQUIRE_CLOSE(test(row, col), ival, 1e-10);
}
+ else
+ {
+ BOOST_REQUIRE_SMALL(test(row, col), 1e-10);
+ }
}
}
+}
+
BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,3 +1,8 @@
+/**
+ * @file linear_regression_test.cpp
+ *
+ * Test for linear regression.
+ */
#include <mlpack/core.h>
#include <mlpack/methods/linear_regression/linear_regression.hpp>
@@ -5,53 +10,53 @@
BOOST_AUTO_TEST_SUITE(LinearRegressionTest);
- /**
- * Creates two 10x3 random matrices and one 10x1 "results" matrix.
- * Finds B in y=BX with one matrix, then predicts against the other.
- */
- BOOST_AUTO_TEST_CASE(LinearRegressionTest)
- {
- // Predictors and points are 100x3 matrices.
- arma::mat predictors(3, 10);
- arma::mat points(3, 10);
+/**
+ * Creates two 10x3 random matrices and one 10x1 "results" matrix.
+ * Finds B in y=BX with one matrix, then predicts against the other.
+ */
+BOOST_AUTO_TEST_CASE(LinearRegressionTest)
+{
+ // Predictors and points are 100x3 matrices.
+ arma::mat predictors(3, 10);
+ arma::mat points(3, 10);
- // Responses is the "correct" value for each point in predictors and points.
- arma::colvec responses(10);
+ // Responses is the "correct" value for each point in predictors and points.
+ arma::colvec responses(10);
- // The values we get back when we predict for points.
- arma::rowvec predictions(10);
+ // The values we get back when we predict for points.
+ arma::rowvec predictions(10);
- // We'll randomly select some coefficients for the linear response.
- arma::vec coeffs;
- coeffs.randu(4);
+ // We'll randomly select some coefficients for the linear response.
+ arma::vec coeffs;
+ coeffs.randu(4);
- // Now generate each point.
- for (size_t row = 0; row < 3; row++)
- predictors.row(row) = arma::linspace<arma::rowvec>(0, 9, 10);
+ // Now generate each point.
+ for (size_t row = 0; row < 3; row++)
+ predictors.row(row) = arma::linspace<arma::rowvec>(0, 9, 10);
- points = predictors;
+ points = predictors;
- // Now add a small amount of noise to each point.
- for (size_t elem = 0; elem < points.n_elem; elem++)
- {
- // Max added noise is 0.02.
- points[elem] += ((double) rand() / (double) INT_MAX) / 50.0;
- predictors[elem] += ((double) rand() / (double) INT_MAX) / 50.0;
- }
+ // Now add a small amount of noise to each point.
+ for (size_t elem = 0; elem < points.n_elem; elem++)
+ {
+ // Max added noise is 0.02.
+ points[elem] += ((double) rand() / (double) INT_MAX) / 50.0;
+ predictors[elem] += ((double) rand() / (double) INT_MAX) / 50.0;
+ }
- // Generate responses.
- for (size_t elem = 0; elem < responses.n_elem; elem++)
- responses[elem] = coeffs[0] +
- dot(coeffs.rows(1, 3), arma::ones<arma::rowvec>(3) * elem);
+ // Generate responses.
+ for (size_t elem = 0; elem < responses.n_elem; elem++)
+ responses[elem] = coeffs[0] +
+ dot(coeffs.rows(1, 3), arma::ones<arma::rowvec>(3) * elem);
- // Initialize and predict
- mlpack::linear_regression::LinearRegression lr(predictors, responses);
- lr.predict(predictions, points);
+ // Initialize and predict
+ mlpack::linear_regression::LinearRegression lr(predictors, responses);
+ lr.predict(predictions, points);
- // Output result and verify we have less than 5% error from "correct" value
- // for each point
- for(size_t i = 0; i < predictions.n_cols; ++i)
- BOOST_REQUIRE_SMALL(predictions(i) - responses(i), .05);
- }
+ // Output result and verify we have less than 5% error from "correct" value
+ // for each point
+ for(size_t i = 0; i < predictions.n_cols; ++i)
+ BOOST_REQUIRE_SMALL(predictions(i) - responses(i), .05);
+}
BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/load_save_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/load_save_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/load_save_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -13,301 +13,316 @@
using namespace mlpack;
-BOOST_AUTO_TEST_SUITE(LoadSaveTests);
+BOOST_AUTO_TEST_SUITE(LoadSaveTest);
- /**
- * Make sure failure occurs when no extension given.
- */
- BOOST_AUTO_TEST_CASE(NoExtensionLoad) {
- arma::mat out;
- BOOST_REQUIRE(data::Load("noextension", out) == false);
- }
+/**
+ * Make sure failure occurs when no extension given.
+ */
+BOOST_AUTO_TEST_CASE(NoExtensionLoad)
+{
+ arma::mat out;
+ BOOST_REQUIRE(data::Load("noextension", out) == false);
+}
- /**
- * Make sure failure occurs when no extension given.
- */
- BOOST_AUTO_TEST_CASE(NoExtensionSave) {
- arma::mat out;
- BOOST_REQUIRE(data::Save("noextension", out) == false);
- }
+/**
+ * Make sure failure occurs when no extension given.
+ */
+BOOST_AUTO_TEST_CASE(NoExtensionSave)
+{
+ arma::mat out;
+ BOOST_REQUIRE(data::Save("noextension", out) == false);
+}
- /**
- * Make sure load fails if the file does not exist.
- */
- BOOST_AUTO_TEST_CASE(NotExistLoad) {
- arma::mat out;
- BOOST_REQUIRE(data::Load("nonexistentfile_______________.csv", out) == false);
- }
+/**
+ * Make sure load fails if the file does not exist.
+ */
+BOOST_AUTO_TEST_CASE(NotExistLoad)
+{
+ arma::mat out;
+ BOOST_REQUIRE(data::Load("nonexistentfile_______________.csv", out) == false);
+}
- /**
- * Make sure a CSV is loaded correctly.
- */
- BOOST_AUTO_TEST_CASE(LoadCSVTest) {
- std::fstream f;
- f.open("test_file.csv", std::fstream::out);
+/**
+ * Make sure a CSV is loaded correctly.
+ */
+BOOST_AUTO_TEST_CASE(LoadCSVTest)
+{
+ std::fstream f;
+ f.open("test_file.csv", std::fstream::out);
- f << "1, 2, 3, 4" << std::endl;
- f << "5, 6, 7, 8" << std::endl;
+ f << "1, 2, 3, 4" << std::endl;
+ f << "5, 6, 7, 8" << std::endl;
- f.close();
+ f.close();
- arma::mat test;
- BOOST_REQUIRE(data::Load("test_file.csv", test) == true);
+ arma::mat test;
+ BOOST_REQUIRE(data::Load("test_file.csv", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.csv");
- }
+ // Remove the file.
+ remove("test_file.csv");
+}
- /**
- * Make sure a CSV is saved correctly.
- */
- BOOST_AUTO_TEST_CASE(SaveCSVTest) {
- arma::mat test = "1 5;"
- "2 6;"
- "3 7;"
- "4 8;";
+/**
+ * Make sure a CSV is saved correctly.
+ */
+BOOST_AUTO_TEST_CASE(SaveCSVTest)
+{
+ arma::mat test = "1 5;"
+ "2 6;"
+ "3 7;"
+ "4 8;";
- BOOST_REQUIRE(data::Save("test_file.csv", test) == true);
+ BOOST_REQUIRE(data::Save("test_file.csv", test) == true);
- // Load it in and make sure it is the same.
- BOOST_REQUIRE(data::Load("test_file.csv", test) == true);
+ // Load it in and make sure it is the same.
+ BOOST_REQUIRE(data::Load("test_file.csv", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.csv");
- }
+ // Remove the file.
+ remove("test_file.csv");
+}
- /**
- * Make sure arma_ascii is loaded correctly.
- */
- BOOST_AUTO_TEST_CASE(LoadArmaASCIITest) {
- arma::mat test = "1 5;"
- "2 6;"
- "3 7;"
- "4 8;";
+/**
+ * Make sure arma_ascii is loaded correctly.
+ */
+BOOST_AUTO_TEST_CASE(LoadArmaASCIITest)
+{
+ arma::mat test = "1 5;"
+ "2 6;"
+ "3 7;"
+ "4 8;";
- arma::mat testTrans = trans(test);
- BOOST_REQUIRE(testTrans.save("test_file.txt", arma::arma_ascii));
+ arma::mat testTrans = trans(test);
+ BOOST_REQUIRE(testTrans.save("test_file.txt", arma::arma_ascii));
- BOOST_REQUIRE(data::Load("test_file.txt", test) == true);
+ BOOST_REQUIRE(data::Load("test_file.txt", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.txt");
- }
+ // Remove the file.
+ remove("test_file.txt");
+}
- /**
- * Make sure a CSV is saved correctly.
- */
- BOOST_AUTO_TEST_CASE(SaveArmaASCIITest) {
- arma::mat test = "1 5;"
- "2 6;"
- "3 7;"
- "4 8;";
+/**
+ * Make sure a CSV is saved correctly.
+ */
+BOOST_AUTO_TEST_CASE(SaveArmaASCIITest)
+{
+ arma::mat test = "1 5;"
+ "2 6;"
+ "3 7;"
+ "4 8;";
- BOOST_REQUIRE(data::Save("test_file.txt", test) == true);
+ BOOST_REQUIRE(data::Save("test_file.txt", test) == true);
- // Load it in and make sure it is the same.
- BOOST_REQUIRE(data::Load("test_file.txt", test) == true);
+ // Load it in and make sure it is the same.
+ BOOST_REQUIRE(data::Load("test_file.txt", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.txt");
- }
+ // Remove the file.
+ remove("test_file.txt");
+}
- /**
- * Make sure raw_ascii is loaded correctly.
- */
- BOOST_AUTO_TEST_CASE(LoadRawASCIITest) {
- std::fstream f;
- f.open("test_file.txt", std::fstream::out);
+/**
+ * Make sure raw_ascii is loaded correctly.
+ */
+BOOST_AUTO_TEST_CASE(LoadRawASCIITest)
+{
+ std::fstream f;
+ f.open("test_file.txt", std::fstream::out);
- f << "1 2 3 4" << std::endl;
- f << "5 6 7 8" << std::endl;
+ f << "1 2 3 4" << std::endl;
+ f << "5 6 7 8" << std::endl;
- f.close();
+ f.close();
- arma::mat test;
- BOOST_REQUIRE(data::Load("test_file.txt", test) == true);
+ arma::mat test;
+ BOOST_REQUIRE(data::Load("test_file.txt", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.txt");
- }
+ // Remove the file.
+ remove("test_file.txt");
+}
- /**
- * Make sure CSV is loaded correctly as .txt.
- */
- BOOST_AUTO_TEST_CASE(LoadCSVTxtTest) {
- std::fstream f;
- f.open("test_file.txt", std::fstream::out);
+/**
+ * Make sure CSV is loaded correctly as .txt.
+ */
+BOOST_AUTO_TEST_CASE(LoadCSVTxtTest)
+{
+ std::fstream f;
+ f.open("test_file.txt", std::fstream::out);
- f << "1, 2, 3, 4" << std::endl;
- f << "5, 6, 7, 8" << std::endl;
+ f << "1, 2, 3, 4" << std::endl;
+ f << "5, 6, 7, 8" << std::endl;
- f.close();
+ f.close();
- arma::mat test;
- BOOST_REQUIRE(data::Load("test_file.txt", test) == true);
+ arma::mat test;
+ BOOST_REQUIRE(data::Load("test_file.txt", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.txt");
- }
+ // Remove the file.
+ remove("test_file.txt");
+}
- /**
- * Make sure arma_binary is loaded correctly.
- */
- BOOST_AUTO_TEST_CASE(LoadArmaBinaryTest) {
- arma::mat test = "1 5;"
- "2 6;"
- "3 7;"
- "4 8;";
+/**
+ * Make sure arma_binary is loaded correctly.
+ */
+BOOST_AUTO_TEST_CASE(LoadArmaBinaryTest)
+{
+ arma::mat test = "1 5;"
+ "2 6;"
+ "3 7;"
+ "4 8;";
- arma::mat testTrans = trans(test);
- BOOST_REQUIRE(testTrans.quiet_save("test_file.bin", arma::arma_binary)
- == true);
+ arma::mat testTrans = trans(test);
+ BOOST_REQUIRE(testTrans.quiet_save("test_file.bin", arma::arma_binary)
+ == true);
- // Now reload through our interface.
- BOOST_REQUIRE(data::Load("test_file.bin", test) == true);
+ // Now reload through our interface.
+ BOOST_REQUIRE(data::Load("test_file.bin", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.bin");
- }
+ // Remove the file.
+ remove("test_file.bin");
+}
- /**
- * Make sure arma_binary is saved correctly.
- */
- BOOST_AUTO_TEST_CASE(SaveArmaBinaryTest) {
- arma::mat test = "1 5;"
- "2 6;"
- "3 7;"
- "4 8;";
+/**
+ * Make sure arma_binary is saved correctly.
+ */
+BOOST_AUTO_TEST_CASE(SaveArmaBinaryTest)
+{
+ arma::mat test = "1 5;"
+ "2 6;"
+ "3 7;"
+ "4 8;";
- BOOST_REQUIRE(data::Save("test_file.bin", test) == true);
+ BOOST_REQUIRE(data::Save("test_file.bin", test) == true);
- BOOST_REQUIRE(data::Load("test_file.bin", test) == true);
+ BOOST_REQUIRE(data::Load("test_file.bin", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.bin");
- }
+ // Remove the file.
+ remove("test_file.bin");
+}
- /**
- * Make sure raw_binary is loaded correctly.
- */
- BOOST_AUTO_TEST_CASE(LoadRawBinaryTest) {
- arma::mat test = "1 2;"
- "3 4;"
- "5 6;"
- "7 8;";
+/**
+ * Make sure raw_binary is loaded correctly.
+ */
+BOOST_AUTO_TEST_CASE(LoadRawBinaryTest)
+{
+ arma::mat test = "1 2;"
+ "3 4;"
+ "5 6;"
+ "7 8;";
- arma::mat testTrans = trans(test);
- BOOST_REQUIRE(testTrans.quiet_save("test_file.bin", arma::raw_binary)
- == true);
+ arma::mat testTrans = trans(test);
+ BOOST_REQUIRE(testTrans.quiet_save("test_file.bin", arma::raw_binary)
+ == true);
- // Now reload through our interface.
- BOOST_REQUIRE(data::Load("test_file.bin", test) == true);
+ // Now reload through our interface.
+ BOOST_REQUIRE(data::Load("test_file.bin", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 1);
- BOOST_REQUIRE_EQUAL(test.n_cols, 8);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 1);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 8);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.bin");
- }
+ // Remove the file.
+ remove("test_file.bin");
+}
- /**
- * Make sure load as PGM is successful.
- */
- BOOST_AUTO_TEST_CASE(LoadPGMBinaryTest) {
- arma::mat test = "1 5;"
- "2 6;"
- "3 7;"
- "4 8;";
+/**
+ * Make sure load as PGM is successful.
+ */
+BOOST_AUTO_TEST_CASE(LoadPGMBinaryTest)
+{
+ arma::mat test = "1 5;"
+ "2 6;"
+ "3 7;"
+ "4 8;";
- arma::mat testTrans = trans(test);
- BOOST_REQUIRE(testTrans.quiet_save("test_file.pgm", arma::pgm_binary)
- == true);
+ arma::mat testTrans = trans(test);
+ BOOST_REQUIRE(testTrans.quiet_save("test_file.pgm", arma::pgm_binary)
+ == true);
- // Now reload through our interface.
- BOOST_REQUIRE(data::Load("test_file.pgm", test) == true);
+ // Now reload through our interface.
+ BOOST_REQUIRE(data::Load("test_file.pgm", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.pgm");
- }
+ // Remove the file.
+ remove("test_file.pgm");
+}
- /**
- * Make sure save as PGM is successful.
- */
- BOOST_AUTO_TEST_CASE(SavePGMBinaryTest) {
- arma::mat test = "1 5;"
- "2 6;"
- "3 7;"
- "4 8;";
+/**
+ * Make sure save as PGM is successful.
+ */
+BOOST_AUTO_TEST_CASE(SavePGMBinaryTest)
+{
+ arma::mat test = "1 5;"
+ "2 6;"
+ "3 7;"
+ "4 8;";
- BOOST_REQUIRE(data::Save("test_file.pgm", test) == true);
+ BOOST_REQUIRE(data::Save("test_file.pgm", test) == true);
- // Now reload through our interface.
- BOOST_REQUIRE(data::Load("test_file.pgm", test) == true);
+ // Now reload through our interface.
+ BOOST_REQUIRE(data::Load("test_file.pgm", test) == true);
- BOOST_REQUIRE_EQUAL(test.n_rows, 4);
- BOOST_REQUIRE_EQUAL(test.n_cols, 2);
+ BOOST_REQUIRE_EQUAL(test.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(test.n_cols, 2);
- for (int i = 0; i < 8; i++)
- BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
+ for (int i = 0; i < 8; i++)
+ BOOST_REQUIRE_CLOSE(test[i], (double) (i + 1), 1e-5);
- // Remove the file.
- remove("test_file.pgm");
- }
+ // Remove the file.
+ remove("test_file.pgm");
+}
+
BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/math_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/math_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/math_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,4 +1,4 @@
-/***
+/**
* @file math_test.cpp
* @author Ryan Curtin
*
@@ -13,511 +13,511 @@
BOOST_AUTO_TEST_SUITE(MathTest);
- /***
- * Verify that the empty constructor creates an empty range.
- */
- BOOST_AUTO_TEST_CASE(RangeEmptyConstructor)
- {
- Range x = Range();
+/**
+ * Verify that the empty constructor creates an empty range.
+ */
+BOOST_AUTO_TEST_CASE(RangeEmptyConstructor)
+{
+ Range x = Range();
- // Just verify that it is empty.
- BOOST_REQUIRE_GT(x.lo, x.hi);
- }
+ // Just verify that it is empty.
+ BOOST_REQUIRE_GT(x.lo, x.hi);
+}
- /***
- * Verify that the point constructor correctly creates a range that is just a
- * point.
- */
- BOOST_AUTO_TEST_CASE(RangePointConstructor)
- {
- Range x(10.0);
+/**
+ * Verify that the point constructor correctly creates a range that is just a
+ * point.
+ */
+BOOST_AUTO_TEST_CASE(RangePointConstructor)
+{
+ Range x(10.0);
- BOOST_REQUIRE_CLOSE(x.lo, x.hi, 1e-25);
- BOOST_REQUIRE_SMALL(x.width(), 1e-5);
- BOOST_REQUIRE_CLOSE(x.lo, 10.0, 1e-25);
- BOOST_REQUIRE_CLOSE(x.hi, 10.0, 1e-25);
- }
+ BOOST_REQUIRE_CLOSE(x.lo, x.hi, 1e-25);
+ BOOST_REQUIRE_SMALL(x.width(), 1e-5);
+ BOOST_REQUIRE_CLOSE(x.lo, 10.0, 1e-25);
+ BOOST_REQUIRE_CLOSE(x.hi, 10.0, 1e-25);
+}
- /***
- * Verify that the range constructor correctly creates the range.
- */
- BOOST_AUTO_TEST_CASE(RangeConstructor)
- {
- Range x(0.5, 5.5);
+/**
+ * Verify that the range constructor correctly creates the range.
+ */
+BOOST_AUTO_TEST_CASE(RangeConstructor)
+{
+ Range x(0.5, 5.5);
- BOOST_REQUIRE_CLOSE(x.lo, 0.5, 1e-25);
- BOOST_REQUIRE_CLOSE(x.hi, 5.5, 1e-25);
- }
+ BOOST_REQUIRE_CLOSE(x.lo, 0.5, 1e-25);
+ BOOST_REQUIRE_CLOSE(x.hi, 5.5, 1e-25);
+}
- /***
- * Test that we get the width correct.
- */
- BOOST_AUTO_TEST_CASE(RangeWidth)
- {
- Range x(0.0, 10.0);
+/**
+ * Test that we get the width correct.
+ */
+BOOST_AUTO_TEST_CASE(RangeWidth)
+{
+ Range x(0.0, 10.0);
- BOOST_REQUIRE_CLOSE(x.width(), 10.0, 1e-20);
+ BOOST_REQUIRE_CLOSE(x.width(), 10.0, 1e-20);
- // Make it empty.
- x.hi = 0.0;
+ // Make it empty.
+ x.hi = 0.0;
- BOOST_REQUIRE_SMALL(x.width(), 1e-5);
+ BOOST_REQUIRE_SMALL(x.width(), 1e-5);
- // Make it negative.
- x.hi = -2.0;
+ // Make it negative.
+ x.hi = -2.0;
- BOOST_REQUIRE_SMALL(x.width(), 1e-5);
+ BOOST_REQUIRE_SMALL(x.width(), 1e-5);
- // Just one more test.
- x.lo = -5.2;
- x.hi = 5.2;
+ // Just one more test.
+ x.lo = -5.2;
+ x.hi = 5.2;
- BOOST_REQUIRE_CLOSE(x.width(), 10.4, 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(x.width(), 10.4, 1e-5);
+}
- /**
- * Test that we get the midpoint correct.
- */
- BOOST_AUTO_TEST_CASE(RangeMidpoint)
- {
- Range x(0.0, 10.0);
+/**
+ * Test that we get the midpoint correct.
+ */
+BOOST_AUTO_TEST_CASE(RangeMidpoint)
+{
+ Range x(0.0, 10.0);
- BOOST_REQUIRE_CLOSE(x.mid(), 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(x.mid(), 5.0, 1e-5);
- x.lo = -5.0;
+ x.lo = -5.0;
- BOOST_REQUIRE_CLOSE(x.mid(), 2.5, 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(x.mid(), 2.5, 1e-5);
+}
- /***
- * Test that we can expand to include other ranges correctly.
- */
- BOOST_AUTO_TEST_CASE(RangeIncludeOther)
- {
- // We need to test both |= and |.
- // We have three cases: non-overlapping; overlapping; equivalent, and then a
- // couple permutations (switch left with right and make sure it still works).
- Range x(0.0, 2.0);
- Range y(3.0, 5.0);
+/**
+ * Test that we can expand to include other ranges correctly.
+ */
+BOOST_AUTO_TEST_CASE(RangeIncludeOther)
+{
+ // We need to test both |= and |.
+ // We have three cases: non-overlapping; overlapping; equivalent, and then a
+ // couple permutations (switch left with right and make sure it still works).
+ Range x(0.0, 2.0);
+ Range y(3.0, 5.0);
- Range z(0.0, 2.0); // Used for operator|=().
- Range w;
- z |= y;
- w = x | y;
+ Range z(0.0, 2.0); // Used for operator|=().
+ Range w;
+ z |= y;
+ w = x | y;
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 5.0, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 5.0, 1e-5);
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 5.0, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 5.0, 1e-5);
- // Switch operator precedence.
- z = y;
- z |= x;
- w = y | x;
+ // Switch operator precedence.
+ z = y;
+ z |= x;
+ w = y | x;
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 5.0, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 5.0, 1e-5);
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 5.0, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 5.0, 1e-5);
- // Now make them overlapping.
- x = Range(0.0, 3.5);
- y = Range(3.0, 4.0);
+ // Now make them overlapping.
+ x = Range(0.0, 3.5);
+ y = Range(3.0, 4.0);
- z = x;
- z |= y;
- w = x | y;
+ z = x;
+ z |= y;
+ w = x | y;
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 4.0, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 4.0, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
- // Switch operator precedence.
- z = y;
- z |= x;
- w = y | x;
+ // Switch operator precedence.
+ z = y;
+ z |= x;
+ w = y | x;
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 4.0, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 4.0, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
- // Now the equivalent case.
- x = Range(0.0, 2.0);
- y = Range(0.0, 2.0);
+ // Now the equivalent case.
+ x = Range(0.0, 2.0);
+ y = Range(0.0, 2.0);
- z = x;
- z |= y;
- w = x | y;
+ z = x;
+ z |= y;
+ w = x | y;
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 2.0, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 2.0, 1e-5);
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 2.0, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 2.0, 1e-5);
- z = y;
- z |= x;
- w = y | x;
+ z = y;
+ z |= x;
+ w = y | x;
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 2.0, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 2.0, 1e-5);
- }
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 2.0, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 2.0, 1e-5);
+}
- /***
- * Test that we can 'and' ranges correctly.
- */
- BOOST_AUTO_TEST_CASE(RangeIntersectOther)
- {
- // We need to test both &= and &.
- // We have three cases: non-overlapping, overlapping; equivalent, and then a
- // couple permutations (switch left with right and make sure it still works).
- Range x(0.0, 2.0);
- Range y(3.0, 5.0);
+/**
+ * Test that we can 'and' ranges correctly.
+ */
+BOOST_AUTO_TEST_CASE(RangeIntersectOther)
+{
+ // We need to test both &= and &.
+ // We have three cases: non-overlapping, overlapping; equivalent, and then a
+ // couple permutations (switch left with right and make sure it still works).
+ Range x(0.0, 2.0);
+ Range y(3.0, 5.0);
- Range z(0.0, 2.0);
- Range w;
- z &= y;
- w = x & y;
+ Range z(0.0, 2.0);
+ Range w;
+ z &= y;
+ w = x & y;
- BOOST_REQUIRE_SMALL(z.width(), 1e-5);
- BOOST_REQUIRE_SMALL(w.width(), 1e-5);
+ BOOST_REQUIRE_SMALL(z.width(), 1e-5);
+ BOOST_REQUIRE_SMALL(w.width(), 1e-5);
- // Reverse operator precedence.
- z = y;
- z &= x;
- w = y & x;
+ // Reverse operator precedence.
+ z = y;
+ z &= x;
+ w = y & x;
- BOOST_REQUIRE_SMALL(z.width(), 1e-5);
- BOOST_REQUIRE_SMALL(w.width(), 1e-5);
+ BOOST_REQUIRE_SMALL(z.width(), 1e-5);
+ BOOST_REQUIRE_SMALL(w.width(), 1e-5);
- // Now make them overlapping.
- x = Range(0.0, 3.5);
- y = Range(3.0, 4.0);
+ // Now make them overlapping.
+ x = Range(0.0, 3.5);
+ y = Range(3.0, 4.0);
- z = x;
- z &= y;
- w = x & y;
+ z = x;
+ z &= y;
+ w = x & y;
- BOOST_REQUIRE_CLOSE(z.lo, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 3.5, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 3.5, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.lo, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 3.5, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 3.5, 1e-5);
- // Reverse operator precedence.
- z = y;
- z &= x;
- w = y & x;
+ // Reverse operator precedence.
+ z = y;
+ z &= x;
+ w = y & x;
- BOOST_REQUIRE_CLOSE(z.lo, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 3.5, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 3.5, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.lo, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 3.5, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 3.5, 1e-5);
- // Now make them equivalent.
- x = Range(2.0, 4.0);
- y = Range(2.0, 4.0);
+ // Now make them equivalent.
+ x = Range(2.0, 4.0);
+ y = Range(2.0, 4.0);
- z = x;
- z &= y;
- w = x & y;
+ z = x;
+ z &= y;
+ w = x & y;
- BOOST_REQUIRE_CLOSE(z.lo, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(z.lo, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
+}
- /**
- * Test multiplication of a range with a double.
- */
- BOOST_AUTO_TEST_CASE(RangeMultiply)
- {
- // We need to test both * and *=, as well as both cases of *.
- // We'll try with a couple of numbers: -1, 0, 2.
- // And we'll have a couple of cases for bounds: strictly less than zero;
- // including zero; and strictly greater than zero.
- //
- // So, nine total cases.
- Range x(-5.0, -3.0);
- Range y(-5.0, -3.0);
- Range z;
- Range w;
+/**
+ * Test multiplication of a range with a double.
+ */
+BOOST_AUTO_TEST_CASE(RangeMultiply)
+{
+ // We need to test both * and *=, as well as both cases of *.
+ // We'll try with a couple of numbers: -1, 0, 2.
+ // And we'll have a couple of cases for bounds: strictly less than zero;
+ // including zero; and strictly greater than zero.
+ //
+ // So, nine total cases.
+ Range x(-5.0, -3.0);
+ Range y(-5.0, -3.0);
+ Range z;
+ Range w;
- y *= -1.0;
- z = x * -1.0;
- w = -1.0 * x;
+ y *= -1.0;
+ z = x * -1.0;
+ w = -1.0 * x;
- BOOST_REQUIRE_CLOSE(y.lo, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(y.hi, 5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.lo, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.lo, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.hi, 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.lo, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 5.0, 1e-5);
- y = x;
- y *= 0.0;
- z = x * 0.0;
- w = 0.0 * x;
+ y = x;
+ y *= 0.0;
+ z = x * 0.0;
+ w = 0.0 * x;
- BOOST_REQUIRE_SMALL(y.lo, 1e-5);
- BOOST_REQUIRE_SMALL(y.hi, 1e-5);
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_SMALL(z.hi, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_SMALL(w.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(y.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(y.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(z.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(w.hi, 1e-5);
- y = x;
- y *= 2.0;
- z = x * 2.0;
- w = 2.0 * x;
+ y = x;
+ y *= 2.0;
+ z = x * 2.0;
+ w = 2.0 * x;
- BOOST_REQUIRE_CLOSE(y.lo, -10.0, 1e-5);
- BOOST_REQUIRE_CLOSE(y.hi, -6.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.lo, -10.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, -6.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, -10.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, -6.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.lo, -10.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.hi, -6.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.lo, -10.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, -6.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, -10.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, -6.0, 1e-5);
- x = Range(-2.0, 2.0);
- y = x;
+ x = Range(-2.0, 2.0);
+ y = x;
- y *= -1.0;
- z = x * -1.0;
- w = -1.0 * x;
+ y *= -1.0;
+ z = x * -1.0;
+ w = -1.0 * x;
- BOOST_REQUIRE_CLOSE(y.lo, -2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(y.hi, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.lo, -2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, -2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.lo, -2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.hi, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.lo, -2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, -2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 2.0, 1e-5);
- y = x;
- y *= 0.0;
- z = x * 0.0;
- w = 0.0 * x;
+ y = x;
+ y *= 0.0;
+ z = x * 0.0;
+ w = 0.0 * x;
- BOOST_REQUIRE_SMALL(y.lo, 1e-5);
- BOOST_REQUIRE_SMALL(y.hi, 1e-5);
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_SMALL(z.hi, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_SMALL(w.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(y.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(y.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(z.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(w.hi, 1e-5);
- y = x;
- y *= 2.0;
- z = x * 2.0;
- w = 2.0 * x;
+ y = x;
+ y *= 2.0;
+ z = x * 2.0;
+ w = 2.0 * x;
- BOOST_REQUIRE_CLOSE(y.lo, -4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(y.hi, 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.lo, -4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, -4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.lo, -4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.hi, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.lo, -4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, -4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
- x = Range(3.0, 5.0);
+ x = Range(3.0, 5.0);
- y = x;
- y *= -1.0;
- z = x * -1.0;
- w = -1.0 * x;
+ y = x;
+ y *= -1.0;
+ z = x * -1.0;
+ w = -1.0 * x;
- BOOST_REQUIRE_CLOSE(y.lo, -5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(y.hi, -3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.lo, -5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, -3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, -5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, -3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.lo, -5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.hi, -3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.lo, -5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, -3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, -5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, -3.0, 1e-5);
- y = x;
- y *= 0.0;
- z = x * 0.0;
- w = 0.0 * x;
+ y = x;
+ y *= 0.0;
+ z = x * 0.0;
+ w = 0.0 * x;
- BOOST_REQUIRE_SMALL(y.lo, 1e-5);
- BOOST_REQUIRE_SMALL(y.hi, 1e-5);
- BOOST_REQUIRE_SMALL(z.lo, 1e-5);
- BOOST_REQUIRE_SMALL(z.hi, 1e-5);
- BOOST_REQUIRE_SMALL(w.lo, 1e-5);
- BOOST_REQUIRE_SMALL(w.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(y.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(y.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(z.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(z.hi, 1e-5);
+ BOOST_REQUIRE_SMALL(w.lo, 1e-5);
+ BOOST_REQUIRE_SMALL(w.hi, 1e-5);
- y = x;
- y *= 2.0;
- z = x * 2.0;
- w = 2.0 * x;
+ y = x;
+ y *= 2.0;
+ z = x * 2.0;
+ w = 2.0 * x;
- BOOST_REQUIRE_CLOSE(y.lo, 6.0, 1e-5);
- BOOST_REQUIRE_CLOSE(y.hi, 10.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.lo, 6.0, 1e-5);
- BOOST_REQUIRE_CLOSE(z.hi, 10.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.lo, 6.0, 1e-5);
- BOOST_REQUIRE_CLOSE(w.hi, 10.0, 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(y.lo, 6.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(y.hi, 10.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.lo, 6.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(z.hi, 10.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.lo, 6.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(w.hi, 10.0, 1e-5);
+}
- /**
- * Test equality operator.
- */
- BOOST_AUTO_TEST_CASE(RangeEquality)
- {
- // Three cases: non-overlapping, overlapping, equivalent. We should also
- // consider empty ranges, which are not necessarily equal...
- Range x(0.0, 2.0);
- Range y(3.0, 5.0);
+/**
+ * Test equality operator.
+ */
+BOOST_AUTO_TEST_CASE(RangeEquality)
+{
+ // Three cases: non-overlapping, overlapping, equivalent. We should also
+ // consider empty ranges, which are not necessarily equal...
+ Range x(0.0, 2.0);
+ Range y(3.0, 5.0);
- // These are odd calls, but we don't want to use operator!= here.
- BOOST_REQUIRE_EQUAL((x == y), false);
- BOOST_REQUIRE_EQUAL((y == x), false);
+ // These are odd calls, but we don't want to use operator!= here.
+ BOOST_REQUIRE_EQUAL((x == y), false);
+ BOOST_REQUIRE_EQUAL((y == x), false);
- y = Range(1.0, 3.0);
+ y = Range(1.0, 3.0);
- BOOST_REQUIRE_EQUAL((x == y), false);
- BOOST_REQUIRE_EQUAL((y == x), false);
+ BOOST_REQUIRE_EQUAL((x == y), false);
+ BOOST_REQUIRE_EQUAL((y == x), false);
- y = Range(0.0, 2.0);
+ y = Range(0.0, 2.0);
- BOOST_REQUIRE_EQUAL((x == y), true);
- BOOST_REQUIRE_EQUAL((y == x), true);
+ BOOST_REQUIRE_EQUAL((x == y), true);
+ BOOST_REQUIRE_EQUAL((y == x), true);
- x = Range(1.0, -1.0); // Empty.
- y = Range(1.0, -1.0); // Also empty.
+ x = Range(1.0, -1.0); // Empty.
+ y = Range(1.0, -1.0); // Also empty.
- BOOST_REQUIRE_EQUAL((x == y), true);
- BOOST_REQUIRE_EQUAL((y == x), true);
+ BOOST_REQUIRE_EQUAL((x == y), true);
+ BOOST_REQUIRE_EQUAL((y == x), true);
- // No need to test what it does if the empty ranges are different "ranges"
- // because we are not forcing behavior for that.
- }
+ // No need to test what it does if the empty ranges are different "ranges"
+ // because we are not forcing behavior for that.
+}
- /**
- * Test inequality operator.
- */
- BOOST_AUTO_TEST_CASE(RangeInequality)
- {
- // We will use the same three cases as the RangeEquality test.
- Range x(0.0, 2.0);
- Range y(3.0, 5.0);
+/**
+ * Test inequality operator.
+ */
+BOOST_AUTO_TEST_CASE(RangeInequality)
+{
+ // We will use the same three cases as the RangeEquality test.
+ Range x(0.0, 2.0);
+ Range y(3.0, 5.0);
- // Again, odd calls, but we want to force use of operator!=.
- BOOST_REQUIRE_EQUAL((x != y), true);
- BOOST_REQUIRE_EQUAL((y != x), true);
+ // Again, odd calls, but we want to force use of operator!=.
+ BOOST_REQUIRE_EQUAL((x != y), true);
+ BOOST_REQUIRE_EQUAL((y != x), true);
- y = Range(1.0, 3.0);
+ y = Range(1.0, 3.0);
- BOOST_REQUIRE_EQUAL((x != y), true);
- BOOST_REQUIRE_EQUAL((y != x), true);
+ BOOST_REQUIRE_EQUAL((x != y), true);
+ BOOST_REQUIRE_EQUAL((y != x), true);
- y = Range(0.0, 2.0);
+ y = Range(0.0, 2.0);
- BOOST_REQUIRE_EQUAL((x != y), false);
- BOOST_REQUIRE_EQUAL((y != x), false);
+ BOOST_REQUIRE_EQUAL((x != y), false);
+ BOOST_REQUIRE_EQUAL((y != x), false);
- x = Range(1.0, -1.0); // Empty.
- y = Range(1.0, -1.0); // Also empty.
+ x = Range(1.0, -1.0); // Empty.
+ y = Range(1.0, -1.0); // Also empty.
- BOOST_REQUIRE_EQUAL((x != y), false);
- BOOST_REQUIRE_EQUAL((y != x), false);
- }
+ BOOST_REQUIRE_EQUAL((x != y), false);
+ BOOST_REQUIRE_EQUAL((y != x), false);
+}
- /**
- * Test strict less-than operator.
- */
- BOOST_AUTO_TEST_CASE(RangeStrictLessThan)
- {
- // Three cases: non-overlapping, overlapping, and equivalent.
- Range x(0.0, 2.0);
- Range y(3.0, 5.0);
+/**
+ * Test strict less-than operator.
+ */
+BOOST_AUTO_TEST_CASE(RangeStrictLessThan)
+{
+ // Three cases: non-overlapping, overlapping, and equivalent.
+ Range x(0.0, 2.0);
+ Range y(3.0, 5.0);
- BOOST_REQUIRE_EQUAL((x < y), true);
- BOOST_REQUIRE_EQUAL((y < x), false);
+ BOOST_REQUIRE_EQUAL((x < y), true);
+ BOOST_REQUIRE_EQUAL((y < x), false);
- y = Range(1.0, 3.0);
+ y = Range(1.0, 3.0);
- BOOST_REQUIRE_EQUAL((x < y), false);
- BOOST_REQUIRE_EQUAL((y < x), false);
+ BOOST_REQUIRE_EQUAL((x < y), false);
+ BOOST_REQUIRE_EQUAL((y < x), false);
- y = Range(0.0, 2.0);
+ y = Range(0.0, 2.0);
- BOOST_REQUIRE_EQUAL((x < y), false);
- BOOST_REQUIRE_EQUAL((y < x), false);
- }
+ BOOST_REQUIRE_EQUAL((x < y), false);
+ BOOST_REQUIRE_EQUAL((y < x), false);
+}
- /**
- * Test strict greater-than operator.
- */
- BOOST_AUTO_TEST_CASE(RangeStrictGreaterThan)
- {
- // Three cases: non-overlapping, overlapping, and equivalent.
- Range x(0.0, 2.0);
- Range y(3.0, 5.0);
+/**
+ * Test strict greater-than operator.
+ */
+BOOST_AUTO_TEST_CASE(RangeStrictGreaterThan)
+{
+ // Three cases: non-overlapping, overlapping, and equivalent.
+ Range x(0.0, 2.0);
+ Range y(3.0, 5.0);
- BOOST_REQUIRE_EQUAL((x > y), false);
- BOOST_REQUIRE_EQUAL((y > x), true);
+ BOOST_REQUIRE_EQUAL((x > y), false);
+ BOOST_REQUIRE_EQUAL((y > x), true);
- y = Range(1.0, 3.0);
+ y = Range(1.0, 3.0);
- BOOST_REQUIRE_EQUAL((x > y), false);
- BOOST_REQUIRE_EQUAL((y > x), false);
+ BOOST_REQUIRE_EQUAL((x > y), false);
+ BOOST_REQUIRE_EQUAL((y > x), false);
- y = Range(0.0, 2.0);
+ y = Range(0.0, 2.0);
- BOOST_REQUIRE_EQUAL((x > y), false);
- BOOST_REQUIRE_EQUAL((y > x), false);
- }
+ BOOST_REQUIRE_EQUAL((x > y), false);
+ BOOST_REQUIRE_EQUAL((y > x), false);
+}
- /**
- * Test the Contains() operator.
- */
- BOOST_AUTO_TEST_CASE(RangeContains)
- {
- // We have three Range cases: strictly less than 0; overlapping 0; and
- // strictly greater than 0. Then the numbers we check can be the same three
- // cases, including one greater than and one less than the range. This should
- // be about 15 total cases.
- Range x(-2.0, -1.0);
+/**
+ * Test the Contains() operator.
+ */
+BOOST_AUTO_TEST_CASE(RangeContains)
+{
+ // We have three Range cases: strictly less than 0; overlapping 0; and
+ // strictly greater than 0. Then the numbers we check can be the same three
+ // cases, including one greater than and one less than the range. This should
+ // be about 15 total cases.
+ Range x(-2.0, -1.0);
- BOOST_REQUIRE(!x.Contains(-3.0));
- BOOST_REQUIRE(x.Contains(-2.0));
- BOOST_REQUIRE(x.Contains(-1.5));
- BOOST_REQUIRE(x.Contains(-1.0));
- BOOST_REQUIRE(!x.Contains(-0.5));
- BOOST_REQUIRE(!x.Contains(0.0));
- BOOST_REQUIRE(!x.Contains(1.0));
+ BOOST_REQUIRE(!x.Contains(-3.0));
+ BOOST_REQUIRE(x.Contains(-2.0));
+ BOOST_REQUIRE(x.Contains(-1.5));
+ BOOST_REQUIRE(x.Contains(-1.0));
+ BOOST_REQUIRE(!x.Contains(-0.5));
+ BOOST_REQUIRE(!x.Contains(0.0));
+ BOOST_REQUIRE(!x.Contains(1.0));
- x = Range(-1.0, 1.0);
+ x = Range(-1.0, 1.0);
- BOOST_REQUIRE(!x.Contains(-2.0));
- BOOST_REQUIRE(x.Contains(-1.0));
- BOOST_REQUIRE(x.Contains(0.0));
- BOOST_REQUIRE(x.Contains(1.0));
- BOOST_REQUIRE(!x.Contains(2.0));
+ BOOST_REQUIRE(!x.Contains(-2.0));
+ BOOST_REQUIRE(x.Contains(-1.0));
+ BOOST_REQUIRE(x.Contains(0.0));
+ BOOST_REQUIRE(x.Contains(1.0));
+ BOOST_REQUIRE(!x.Contains(2.0));
- x = Range(1.0, 2.0);
+ x = Range(1.0, 2.0);
- BOOST_REQUIRE(!x.Contains(-1.0));
- BOOST_REQUIRE(!x.Contains(0.0));
- BOOST_REQUIRE(!x.Contains(0.5));
- BOOST_REQUIRE(x.Contains(1.0));
- BOOST_REQUIRE(x.Contains(1.5));
- BOOST_REQUIRE(x.Contains(2.0));
- BOOST_REQUIRE(!x.Contains(2.5));
+ BOOST_REQUIRE(!x.Contains(-1.0));
+ BOOST_REQUIRE(!x.Contains(0.0));
+ BOOST_REQUIRE(!x.Contains(0.5));
+ BOOST_REQUIRE(x.Contains(1.0));
+ BOOST_REQUIRE(x.Contains(1.5));
+ BOOST_REQUIRE(x.Contains(2.0));
+ BOOST_REQUIRE(!x.Contains(2.5));
- // Now let's try it on an empty range.
- x = Range();
+ // Now let's try it on an empty range.
+ x = Range();
- BOOST_REQUIRE(!x.Contains(-10.0));
- BOOST_REQUIRE(!x.Contains(0.0));
- BOOST_REQUIRE(!x.Contains(10.0));
+ BOOST_REQUIRE(!x.Contains(-10.0));
+ BOOST_REQUIRE(!x.Contains(0.0));
+ BOOST_REQUIRE(!x.Contains(10.0));
- // And an infinite range.
- x = Range(-DBL_MAX, DBL_MAX);
+ // And an infinite range.
+ x = Range(-DBL_MAX, DBL_MAX);
- BOOST_REQUIRE(x.Contains(-10.0));
- BOOST_REQUIRE(x.Contains(0.0));
- BOOST_REQUIRE(x.Contains(10.0));
- }
+ BOOST_REQUIRE(x.Contains(-10.0));
+ BOOST_REQUIRE(x.Contains(0.0));
+ BOOST_REQUIRE(x.Contains(10.0));
+}
BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/mlpack_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/mlpack_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/mlpack_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,3 +1,9 @@
-#define BOOST_TEST_DYN_LINK
+/**
+ * @file mlpack_test.cpp
+ *
+ * Simple file defining the name of the overall test for MLPACK. Each
+ * individual test is contained in its own file.
+ */
#define BOOST_TEST_MODULE MLPACKTest
+
#include <boost/test/unit_test.hpp>
Deleted: mlpack/trunk/src/mlpack/tests/nbc_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/nbc_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/nbc_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,56 +0,0 @@
-#include <mlpack/core.h>
-#include <mlpack/methods/naive_bayes/simple_nbc.h>
-#include <boost/test/unit_test.hpp>
-
-using namespace mlpack;
-using namespace naive_bayes;
-
-BOOST_AUTO_TEST_SUITE(NBCTest);
-
- BOOST_AUTO_TEST_CASE(SimpleNBCTest) {
- const char* filename_train_ = "trainSet.csv";
- const char* filename_test_ = "testSet.csv";
- const char* train_result_ = "trainRes.csv";
- const char* test_result_ = "testRes.csv";
- size_t number_of_classes_ = 2;
-
- arma::mat train_data, train_res, calc_mat;
- data::Load(filename_train_, train_data, true);
- data::Load(train_result_, train_res, true);
-
- CLI::GetParam<int>("nbc/classes") = number_of_classes_;
- SimpleNaiveBayesClassifier nbc_test_(train_data);
-
- size_t number_of_features = nbc_test_.means_.n_rows;
- calc_mat.zeros(2 * number_of_features + 1, number_of_classes_);
-
- for (size_t i = 0; i < number_of_features; i++) {
- for (size_t j = 0; j < number_of_classes_; j++) {
- calc_mat(i, j) = nbc_test_.means_(i, j);
- calc_mat(i + number_of_features, j) = nbc_test_.variances_(i, j);
- }
- }
- for (size_t i = 0; i < number_of_classes_; i++)
- calc_mat(2 * number_of_features, i) = nbc_test_.class_probabilities_(i);
-
- for(size_t i = 0; i < calc_mat.n_rows; i++) {
- for(size_t j = 0; j < number_of_classes_; j++) {
- BOOST_REQUIRE_CLOSE(train_res(i, j) + .00001, calc_mat(i, j), .01);
- }
- }
-
- arma::mat test_data, test_res;
- arma::vec test_res_vec, calc_vec;
- data::Load(filename_test_, test_data, true);
- data::Load(test_result_, test_res, true);
-
- nbc_test_.Classify(test_data, calc_vec);
-
- size_t number_of_datum = test_data.n_cols;
- test_res_vec = test_res.col(0);
-
- for(size_t i = 0; i < number_of_datum; i++)
- BOOST_REQUIRE_EQUAL(test_res_vec(i), calc_vec(i));
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/nbc_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/nbc_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/nbc_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/nbc_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,64 @@
+/**
+ * @file nbc_test.cpp
+ *
+ * Test for the Naive Bayes classifier.
+ */
+#include <mlpack/core.h>
+#include <mlpack/methods/naive_bayes/simple_nbc.h>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace naive_bayes;
+
+BOOST_AUTO_TEST_SUITE(NBCTest);
+
+BOOST_AUTO_TEST_CASE(SimpleNBCTest)
+{
+ const char* filename_train_ = "trainSet.csv";
+ const char* filename_test_ = "testSet.csv";
+ const char* train_result_ = "trainRes.csv";
+ const char* test_result_ = "testRes.csv";
+ size_t number_of_classes_ = 2;
+
+ arma::mat train_data, train_res, calc_mat;
+ data::Load(filename_train_, train_data, true);
+ data::Load(train_result_, train_res, true);
+
+ CLI::GetParam<int>("nbc/classes") = number_of_classes_;
+ SimpleNaiveBayesClassifier nbc_test_(train_data);
+
+ size_t number_of_features = nbc_test_.means_.n_rows;
+ calc_mat.zeros(2 * number_of_features + 1, number_of_classes_);
+
+ for (size_t i = 0; i < number_of_features; i++)
+ {
+ for (size_t j = 0; j < number_of_classes_; j++)
+ {
+ calc_mat(i, j) = nbc_test_.means_(i, j);
+ calc_mat(i + number_of_features, j) = nbc_test_.variances_(i, j);
+ }
+ }
+
+ for (size_t i = 0; i < number_of_classes_; i++)
+ calc_mat(2 * number_of_features, i) = nbc_test_.class_probabilities_(i);
+
+ for(size_t i = 0; i < calc_mat.n_rows; i++)
+ for(size_t j = 0; j < number_of_classes_; j++)
+ BOOST_REQUIRE_CLOSE(train_res(i, j) + .00001, calc_mat(i, j), .01);
+
+ arma::mat test_data, test_res;
+ arma::vec test_res_vec, calc_vec;
+ data::Load(filename_test_, test_data, true);
+ data::Load(test_result_, test_res, true);
+
+ nbc_test_.Classify(test_data, calc_vec);
+
+ size_t number_of_datum = test_data.n_cols;
+ test_res_vec = test_res.col(0);
+
+ for(size_t i = 0; i < number_of_datum; i++)
+ BOOST_REQUIRE_EQUAL(test_res_vec(i), calc_vec(i));
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/nca_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/nca_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/nca_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,167 +0,0 @@
-/**
- * @file nca_test.cc
- * @author Ryan Curtin
- *
- * Unit tests for Neighborhood Components Analysis and related code (including
- * the softmax error function).
- */
-#include <mlpack/core.h>
-#include <mlpack/core/kernels/lmetric.hpp>
-#include <mlpack/methods/nca/nca.h>
-#include <boost/test/unit_test.hpp>
-
-using namespace mlpack;
-using namespace mlpack::kernel;
-using namespace mlpack::nca;
-
-//
-// Tests for the SoftmaxErrorFunction
-//
-
-BOOST_AUTO_TEST_SUITE(NcaTest);
-
- /***
- * The Softmax error function should return the identity matrix as its initial
- * point.
- */
- BOOST_AUTO_TEST_CASE(softmax_initial_point) {
- // Cheap fake dataset.
- arma::mat data;
- data.randu(5, 5);
- arma::uvec labels;
- labels.zeros(5);
-
- SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
-
- // Verify the initial point is the identity matrix.
- arma::mat initial_point = sef.GetInitialPoint();
- for (int row = 0; row < 5; row++) {
- for (int col = 0; col < 5; col++) {
- if (row == col)
- BOOST_REQUIRE_CLOSE(initial_point(row, col), 1.0, 1e-5);
- else
- BOOST_REQUIRE(initial_point(row, col) == 0.0);
- }
- }
- }
-
- /***
- * On a simple fake dataset, ensure that the initial function evaluation is
- * correct.
- */
- BOOST_AUTO_TEST_CASE(softmax_initial_evaluation) {
- // Useful but simple dataset with six points and two classes.
- arma::mat data = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
- " 1.0 0.0 -1.0 1.0 0.0 -1.0 ";
- arma::uvec labels = " 0 0 0 1 1 1 ";
-
- SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
-
- double objective = sef.Evaluate(arma::eye<arma::mat>(2, 2));
-
- // Result painstakingly calculated by hand by rcurtin (recorded forever in his
- // notebook). As a result of lack of precision of the by-hand result, the
- // tolerance is fairly high.
- BOOST_REQUIRE_CLOSE(objective, -1.5115, 0.01);
- }
-
- /***
- * On a simple fake dataset, ensure that the initial gradient evaluation is
- * correct.
- */
- BOOST_AUTO_TEST_CASE(softmax_initial_gradient) {
- // Useful but simple dataset with six points and two classes.
- arma::mat data = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
- " 1.0 0.0 -1.0 1.0 0.0 -1.0 ";
- arma::uvec labels = " 0 0 0 1 1 1 ";
-
- SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
-
- arma::mat gradient;
- sef.Gradient(arma::eye<arma::mat>(2, 2), gradient);
-
- // Results painstakingly calculated by hand by rcurtin (recorded forever in
- // his notebook). As a result of lack of precision of the by-hand result, the
- // tolerance is fairly high.
- BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.089766, 0.05);
- BOOST_REQUIRE(gradient(1, 0) == 0.0);
- BOOST_REQUIRE(gradient(0, 1) == 0.0);
- BOOST_REQUIRE_CLOSE(gradient(1, 1), 1.63823, 0.01);
- }
-
- /***
- * On optimally separated datasets, ensure that the objective function is
- * optimal (equal to the negative number of points).
- */
- BOOST_AUTO_TEST_CASE(softmax_optimal_evaluation) {
- // Simple optimal dataset.
- arma::mat data = " 500 500 -500 -500;"
- " 1 0 1 0 ";
- arma::uvec labels = " 0 0 1 1 ";
-
- SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
-
- double objective = sef.Evaluate(arma::eye<arma::mat>(2, 2));
-
- // Use a very close tolerance for optimality; we need to be sure this function
- // gives optimal results correctly.
- BOOST_REQUIRE_CLOSE(objective, -4.0, 1e-10);
- }
-
- /***
- * On optimally separated datasets, ensure that the gradient is zero.
- */
- BOOST_AUTO_TEST_CASE(softmax_optimal_gradient) {
- // Simple optimal dataset.
- arma::mat data = " 500 500 -500 -500;"
- " 1 0 1 0 ";
- arma::uvec labels = " 0 0 1 1 ";
-
- SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
-
- arma::mat gradient;
- sef.Gradient(arma::eye<arma::mat>(2, 2), gradient);
-
- BOOST_REQUIRE(gradient(0, 0) == 0.0);
- BOOST_REQUIRE(gradient(0, 1) == 0.0);
- BOOST_REQUIRE(gradient(1, 0) == 0.0);
- BOOST_REQUIRE(gradient(1, 1) == 0.0);
- }
-
- //
- // Tests for the NCA algorithm.
- //
-
- /***
- * On our simple dataset, ensure that the NCA algorithm fully separates the
- * points.
- */
- BOOST_AUTO_TEST_CASE(nca_simple_dataset) {
- // Useful but simple dataset with six points and two classes.
- arma::mat data = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
- " 1.0 0.0 -1.0 1.0 0.0 -1.0 ";
- arma::uvec labels = " 0 0 0 1 1 1 ";
-
- NCA<SquaredEuclideanDistance> nca(data, labels);
-
- arma::mat output_matrix;
- nca.LearnDistance(output_matrix);
-
- // Ensure that the objective function is better now.
- SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
-
- double init_obj = sef.Evaluate(arma::eye<arma::mat>(2, 2));
- double final_obj = sef.Evaluate(output_matrix);
- arma::mat final_gradient;
- sef.Gradient(output_matrix, final_gradient);
-
- // final_obj must be less than init_obj.
- BOOST_REQUIRE_LT(final_obj, init_obj);
- // Verify that final objective is optimal.
- BOOST_REQUIRE_CLOSE(final_obj, -6.0, 1e-8);
- // The solution is not unique, so the best we can do is ensure the gradient
- // norm is close to 0.
- BOOST_REQUIRE_LT(arma::norm(final_gradient, 2), 1e-10);
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/nca_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/nca_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/nca_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/nca_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,176 @@
+/**
+ * @file nca_test.cpp
+ * @author Ryan Curtin
+ *
+ * Unit tests for Neighborhood Components Analysis and related code (including
+ * the softmax error function).
+ */
+#include <mlpack/core.h>
+#include <mlpack/core/kernels/lmetric.hpp>
+#include <mlpack/methods/nca/nca.h>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace mlpack::kernel;
+using namespace mlpack::nca;
+
+//
+// Tests for the SoftmaxErrorFunction
+//
+
+BOOST_AUTO_TEST_SUITE(NCATest);
+
+/**
+ * The Softmax error function should return the identity matrix as its initial
+ * point.
+ */
+BOOST_AUTO_TEST_CASE(softmax_initial_point)
+{
+ // Cheap fake dataset.
+ arma::mat data;
+ data.randu(5, 5);
+ arma::uvec labels;
+ labels.zeros(5);
+
+ SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
+
+ // Verify the initial point is the identity matrix.
+ arma::mat initial_point = sef.GetInitialPoint();
+ for (int row = 0; row < 5; row++)
+ {
+ for (int col = 0; col < 5; col++)
+ {
+ if (row == col)
+ BOOST_REQUIRE_CLOSE(initial_point(row, col), 1.0, 1e-5);
+ else
+ BOOST_REQUIRE(initial_point(row, col) == 0.0);
+ }
+ }
+}
+
+/***
+ * On a simple fake dataset, ensure that the initial function evaluation is
+ * correct.
+ */
+BOOST_AUTO_TEST_CASE(softmax_initial_evaluation)
+{
+ // Useful but simple dataset with six points and two classes.
+ arma::mat data = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
+ " 1.0 0.0 -1.0 1.0 0.0 -1.0 ";
+ arma::uvec labels = " 0 0 0 1 1 1 ";
+
+ SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
+
+ double objective = sef.Evaluate(arma::eye<arma::mat>(2, 2));
+
+ // Result painstakingly calculated by hand by rcurtin (recorded forever in his
+ // notebook). As a result of lack of precision of the by-hand result, the
+ // tolerance is fairly high.
+ BOOST_REQUIRE_CLOSE(objective, -1.5115, 0.01);
+}
+
+/**
+ * On a simple fake dataset, ensure that the initial gradient evaluation is
+ * correct.
+ */
+BOOST_AUTO_TEST_CASE(softmax_initial_gradient)
+{
+ // Useful but simple dataset with six points and two classes.
+ arma::mat data = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
+ " 1.0 0.0 -1.0 1.0 0.0 -1.0 ";
+ arma::uvec labels = " 0 0 0 1 1 1 ";
+
+ SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
+
+ arma::mat gradient;
+ sef.Gradient(arma::eye<arma::mat>(2, 2), gradient);
+
+ // Results painstakingly calculated by hand by rcurtin (recorded forever in
+ // his notebook). As a result of lack of precision of the by-hand result, the
+ // tolerance is fairly high.
+ BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.089766, 0.05);
+ BOOST_REQUIRE(gradient(1, 0) == 0.0);
+ BOOST_REQUIRE(gradient(0, 1) == 0.0);
+ BOOST_REQUIRE_CLOSE(gradient(1, 1), 1.63823, 0.01);
+}
+
+/**
+ * On optimally separated datasets, ensure that the objective function is
+ * optimal (equal to the negative number of points).
+ */
+BOOST_AUTO_TEST_CASE(softmax_optimal_evaluation)
+{
+ // Simple optimal dataset.
+ arma::mat data = " 500 500 -500 -500;"
+ " 1 0 1 0 ";
+ arma::uvec labels = " 0 0 1 1 ";
+
+ SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
+
+ double objective = sef.Evaluate(arma::eye<arma::mat>(2, 2));
+
+ // Use a very close tolerance for optimality; we need to be sure this function
+ // gives optimal results correctly.
+ BOOST_REQUIRE_CLOSE(objective, -4.0, 1e-10);
+}
+
+/**
+ * On optimally separated datasets, ensure that the gradient is zero.
+ */
+BOOST_AUTO_TEST_CASE(softmax_optimal_gradient)
+{
+ // Simple optimal dataset.
+ arma::mat data = " 500 500 -500 -500;"
+ " 1 0 1 0 ";
+ arma::uvec labels = " 0 0 1 1 ";
+
+ SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
+
+ arma::mat gradient;
+ sef.Gradient(arma::eye<arma::mat>(2, 2), gradient);
+
+ BOOST_REQUIRE(gradient(0, 0) == 0.0);
+ BOOST_REQUIRE(gradient(0, 1) == 0.0);
+ BOOST_REQUIRE(gradient(1, 0) == 0.0);
+ BOOST_REQUIRE(gradient(1, 1) == 0.0);
+}
+
+//
+// Tests for the NCA algorithm.
+//
+
+/**
+ * On our simple dataset, ensure that the NCA algorithm fully separates the
+ * points.
+ */
+BOOST_AUTO_TEST_CASE(nca_simple_dataset)
+{
+ // Useful but simple dataset with six points and two classes.
+ arma::mat data = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
+ " 1.0 0.0 -1.0 1.0 0.0 -1.0 ";
+ arma::uvec labels = " 0 0 0 1 1 1 ";
+
+ NCA<SquaredEuclideanDistance> nca(data, labels);
+
+ arma::mat output_matrix;
+ nca.LearnDistance(output_matrix);
+
+ // Ensure that the objective function is better now.
+ SoftmaxErrorFunction<SquaredEuclideanDistance> sef(data, labels);
+
+ double init_obj = sef.Evaluate(arma::eye<arma::mat>(2, 2));
+ double final_obj = sef.Evaluate(output_matrix);
+ arma::mat final_gradient;
+ sef.Gradient(output_matrix, final_gradient);
+
+ // final_obj must be less than init_obj.
+ BOOST_REQUIRE_LT(final_obj, init_obj);
+ // Verify that final objective is optimal.
+ BOOST_REQUIRE_CLOSE(final_obj, -6.0, 1e-8);
+ // The solution is not unique, so the best we can do is ensure the gradient
+ // norm is close to 0.
+ BOOST_REQUIRE_LT(arma::norm(final_gradient, 2), 1e-10);
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/nnsvm_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/nnsvm_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/nnsvm_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,288 +1,290 @@
/**
* @file nnsvm_test.cc
*
- * Test file for NNSVM class
+ * Test file for NNSVM class.
*/
#include <iostream>
#include <mlpack/methods/nnsvm/nnsvm.hpp>
#include <mlpack/core/kernels/linear_kernel.hpp>
+
#include <boost/test/unit_test.hpp>
using namespace mlpack;
using namespace mlpack::nnsvm;
-BOOST_AUTO_TEST_SUITE(NnsvmTest);
+BOOST_AUTO_TEST_SUITE(NNSVMTest);
- /***
- * simple nonnegative SVM test with small, synthetic dataset
- */
- BOOST_AUTO_TEST_CASE(linear_kernel_test_1)
- {
- // initialize the matrix
- arma::mat data;
+/**
+ * Simple nonnegative SVM test with small, synthetic dataset.
+ */
+BOOST_AUTO_TEST_CASE(linear_kernel_test_1)
+{
+ // Initialize the matrix.
+ arma::mat data;
- data << -1.0 << 1.0 << 1.0 << arma::endr
- << -2.0 << 2.0 << 1.0 << arma::endr
- << -3.0 << 3.0 << 1.0 << arma::endr
- << -4.0 << 4.0 << 1.0 << arma::endr
- << 1.0 << -1.0 << 0.0 << arma::endr
- << 2.0 << -2.0 << 0.0 << arma::endr
- << 3.0 << -3.0 << 0.0 << arma::endr
- << 4.0 << -4.0 << 0.0 << arma::endr;
+ data << -1.0 << 1.0 << 1.0 << arma::endr
+ << -2.0 << 2.0 << 1.0 << arma::endr
+ << -3.0 << 3.0 << 1.0 << arma::endr
+ << -4.0 << 4.0 << 1.0 << arma::endr
+ << 1.0 << -1.0 << 0.0 << arma::endr
+ << 2.0 << -2.0 << 0.0 << arma::endr
+ << 3.0 << -3.0 << 0.0 << arma::endr
+ << 4.0 << -4.0 << 0.0 << arma::endr;
- // test the linear kernel
- NNSVM<kernel::LinearKernel> nnsvm;
+ // Test the linear kernel.
+ NNSVM<kernel::LinearKernel> nnsvm;
- nnsvm.InitTrain(data, 2);
- double calculatedThreshold = nnsvm.getThreshold();
- size_t calculatedSupportVectorCount = nnsvm.getSupportVectorCount();
- const arma::vec calculatedSupportVectorCoefficients =
- nnsvm.getSupportVectorCoefficients();
- const arma::vec calculatedWeightVector = nnsvm.getWeightVector();
+ nnsvm.InitTrain(data, 2);
+ double calculatedThreshold = nnsvm.getThreshold();
+ size_t calculatedSupportVectorCount = nnsvm.getSupportVectorCount();
+ const arma::vec calculatedSupportVectorCoefficients =
+ nnsvm.getSupportVectorCoefficients();
+ const arma::vec calculatedWeightVector = nnsvm.getWeightVector();
- // check for correctness on the linear kernel
- BOOST_REQUIRE(calculatedSupportVectorCount == 3);
- BOOST_REQUIRE_CLOSE(calculatedThreshold, -1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[0],
- 3.7499785159728178, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[1],
- 6.2500214840271884, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[2], -10.000, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedWeightVector[0], 0.00000000, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedWeightVector[1], 0.00000000, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedWeightVector[2], 0.00000000, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedWeightVector[3], 0.00017187221748210524, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedWeightVector[4], 0.00000000, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedWeightVector[5], 0.00000000, 1e-5);
- BOOST_REQUIRE_CLOSE(calculatedWeightVector[6], 0.00000000, 1e-5);
+ // Check for correctness on the linear kernel.
+ BOOST_REQUIRE(calculatedSupportVectorCount == 3);
+ BOOST_REQUIRE_CLOSE(calculatedThreshold, -1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[0],
+ 3.7499785159728178, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[1],
+ 6.2500214840271884, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[2], -10.000, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedWeightVector[0], 0.00000000, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedWeightVector[1], 0.00000000, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedWeightVector[2], 0.00000000, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedWeightVector[3], 0.00017187221748210524, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedWeightVector[4], 0.00000000, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedWeightVector[5], 0.00000000, 1e-5);
+ BOOST_REQUIRE_CLOSE(calculatedWeightVector[6], 0.00000000, 1e-5);
- }
- //BOOST_AUTO_TEST_CASE(linear_kernel_test_2)
- //{
- // // initialize the matrix
- // arma::mat data;
- //
- // data << 2.8532 << 0.6808 << -18 << 6.5 << -26.23 << -273.67 << 3.1747
- // << 1.4824 << 2.0161 << -11.142 << -31.166 << 0 << -1.0324
- // << -8.2685 << 3.48 << 8.545 << 6.575 << -7.89 << 1.9919 << -7
- // << 1 << arma::endr
- // << 0.0578 << 1.3971 << -24 << -12.1 << -100.13 << -553.18 << 2.3804
- // << -0.5607 << 4.5496 << -24.574 << -24.968 << -1 << -13.534 << -31.45
- // << 1.54 << -13.05 << -11.725 << -2.55 << 1.8087 << 10 << 1
- // << arma::endr
- // << 0.1804 << 1.6302 << -26 << -6.4 << -81.28 << -529.73 << 2.0123
- // << 1.0011 << 2.1322 << -38.049 << -22.548 << -1 << -17.921
- // << -29.786 << -4.2 << -5.885 << -12.065 << -0.18 << 3.7498
- // << 8 << 1 << arma::endr
- // << 2.2685 << 0.77 << -26 << -5 << -63.74 << -619.41 << 3.79 << 1.4824
- // << 2.6626 << -11.553 << -10.084 << -1 << -23.293 << -24.595 << 5.06
- // << 1.81 << -6.8 << -2.74 << 1.4367 << 2 << 1 << arma::endr
- // << 1.1094 << 1.3512 << -27 << -5.9 << -97.98 << -888.82 << 4.7719
- // << -0.8833 << 7.0048 << -41.996 << -25.473 << 0 << -17.197 << -31.864
- // << 2.96 << -12.92 << -21.625 << -4.66 << 4.1161 << 14 << 1
- // << arma::endr
- // << -0.7448 << 0.639 << -33 << -4.3 << -121.87 << -1240.2 << 2.574
- // << -11.96 << -1.4605 << -23.163 << -41.738 << 0 << -26.078
- // << -65.459 << -3.46 << -43.665 << -47.125 << 0.36 << 1.5793
- // << 11 << 1 << arma::endr
- // << 2.1242 << 1.0001 << -27 << -1.5 << -85.01 << -753.81 << 5.8026
- // << 0.4733 << 7.3244 << -28.602 << -22.096 << -1 << -25.72 << -5.058
- // << 1.96 << -2.715 << -36.435 << 6.23 << 4.0664 << 4 << 1 << arma::endr
- // << 1.5147 << 1.5999 << -30 << 9.5 << -48.64 << -552.72 << 6.8646
- // << -2.7514 << 9.765 << -32.567 << -26.009 << -1 << -21.244 << -42.178
- // << -4.38 << -14.655 << -35.8 << -8.5 << 3.5578 << -3 << 1
- // << arma::endr
- // << 0.2762 << 0.4498 << -33 << -14.5 << -108.06 << -1848.7 << 3.3279
- // << -4.7404 << 8.3787 << -69.738 << -45.989 << -2 << -20.988 << -87.26
- // << 5.26 << -41.045 << -46.53 << 2.92 << 0.2006 << 17 << 1
- // << arma::endr
- // << -0.8361 << 0.5906 << -35 << -9.9 << -123.2 << -1535.6 << 1.7498
- // << -9.412 << 4.8324 << -61.991 << -45.081 << -1 << -28.325 << -85.874
- // << -10.38 << -36.53 << -51.315 << -4.12 << 1.5208 << 28 << 1
- // << arma::endr
- // << 3.4862 << 0.1746 << -8 << -12.6 << -48.05 << -74.518 << -5.3072 << 0
- // << -0.6322 << 14.706 << 4.6319 << -1 << -5.1945 << -10.268 << -13.8
- // << -18.11 << -2.625 << -9.7 << 0.0399 << -11 << 0 << arma::endr
- // << -1.332 << -0.5399 << 6 << -11.5 << -7.1 << -41.538 << -0.4827
- // << 0.1494 << 2.6331 << 13.711 << -20.669 << 1 << -2.307 << -16.457
- // << -5.3 << -21.65 << 3.57 << 6.82 << -1.4101 << -6 << 0
- // << arma::endr
- // << 0.6689 << 0.1837 << 2 << -1.9 << 10.22 << 176.41 << -1.2294 << 0
- // << -1.7885 << -16.553 << -2.9748 << 0 << 9.304 << 10.097 << -4.32
- // << -7.325 << -6.375 << 13.96 << -0.1116 << 25 << 0 << arma::endr
- // << -0.9639 << -0.6035 << 15 << 17.7 << 82.64 << 849.54 << 1.7272
- // << 2.3738 << -2.3887 << -4.4992 << 31.715 << -1 << 18.632 << -4.4991
- // << 13 << 8.955 << 12.725 << -19.04 << 0.218 << -24 << 0 << arma::endr
- // << -3.5669 << -0.4114 << 19 << 11.8 << 84.35 << 749.12 << -3.8668
- // << 1.9631 << 3.2414 << 25.695 << 28.835 << 1 << 30.208 << 34.002
- // << 14.78 << 46.875 << 24.84 << 8.1 << -1.7294 << -7 << 0 << arma::endr
- // << 1.5881 << -0.023 << 24 << 6.8 << 108.42 << 1255.7 << 2.0539 << 12.079
- // << 1.7954 << 21.008 << 33.312 << 0 << 27.819 << 22.083 << 15.94
- // << 71.565 << 49.805 << 3.94 << 0.3416 << -12 << 0 << arma::endr
- // << -1.5349 << -0.5047 << 10 << -12.2 << 12.26 << 476.02 << -4.4551 << 0
- // << -7.213 << 7.56 << 2.4955 << 0 << 18.81 << -13.185 << -11 << 9.86
- // << -12.555 << -3.63 << -2.4305 << -7 << 0 << arma::endr
- // << 1.9474 << -1.9588 << 26 << 29.8 << 156.44 << 1885.6 << 1.7485
- // << 12.592 << -4.1963 << 17.719 << 45.104 << 0 << 29.739 << 82.289
- // << 17 << 62.15 << 41.805 << -13.57 << 1.4464 << -48 << 0
- // << arma::endr
- // << 1.9065 << -0.1575 << 24 << -8.5 << 59.85 << 577 << -6.8442 << -0.4728
- // << -1.5632 << 19.76 << 13.641 << -2 << 32.432 << 34.894 << -0.9
- // << 52.775 << 23.125 << -24 << -8.2289 << -18 << 0 << arma::endr
- // << -0.6037 << -0.4235 << 21 << 18.6 << 137.71 << 1506.4 << -0.9627
- // << 11.03 << -2.7523 << 47.119 << 58.999 << 1 << 21.233 << 74.764
- // << 21.96 << 72.49 << 54.675 << -10.04 << 1.916 << -12 << 0
- // << arma::endr;
- //
- // // test the linear kernel
- // NNSVM<SVMLinearKernel>* nnsvm = new NNSVM<SVMLinearKernel>();
- //
- // nnsvm.InitTrain(data, 2);
- // double calculatedThreshold = nnsvm.getThreshold();
- // size_t calculatedSupportVectorCount = nnsvm.getSupportVectorCount();
- // const arma::vec calculatedSupportVectorCoefficients =
- // nnsvm.getSupportVectorCoefficients();
- // const arma::vec calculatedWeightVector = nnsvm.getWeightVector();
- //
- // // check for correctness on the linear kernel
- // BOOST_REQUIRE(calculatedSupportVectorCount == 6);
- // BOOST_REQUIRE_CLOSE(calculatedThreshold, -39.793784137999701, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[0],
- // 0.06875628588658407, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[1],
- // 3.1607079296638054, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[2],
- // 0.0039166013511236601, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[3],
- // 3.7130510722124139, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[4],
- // 0.43343566949350276, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[5],
- // -10.000, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedWeightVector[0], 0.00000000, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedWeightVector[1], 0.00000000, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedWeightVector[2], 0.00000000, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedWeightVector[3], 0.00000000, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedWeightVector[4], 0.053222444790909262, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedWeightVector[5], 0.00000000, 1e-5);
- // BOOST_REQUIRE_CLOSE(calculatedWeightVector[6], 0.00000000, 1e-5);
- //
- //}
- ///***
- // * Test the dual-tree nearest-neighbors method with the naive method. This
- // * uses both a query and reference dataset.
- // *
- // * Errors are produced if the results are not identical.
- // */
- //BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_1)
- //{
- // arma::mat data_for_tree_;
- //
- // // Hard-coded filename: bad!
- // if (data::Load("test_data_3_1000.csv", data_for_tree_) != true)
- // {
- // BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
- // }
- //
- // // Set up matrices to work with.
- // arma::mat dual_query(data_for_tree_);
- // arma::mat dual_references(data_for_tree_);
- // arma::mat naive_query(data_for_tree_);
- // arma::mat naive_references(data_for_tree_);
- //
- // AllkNN allknn_(dual_query, dual_references, 20, 5);
- // AllkNN naive_(naive_query, naive_references, 1 /* leaf_size ignored */, 5,
- // AllkNN::NAIVE);
- //
- // arma::Col<size_t> resulting_neighbors_tree;
- // arma::vec distances_tree;
- // allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
- //
- // arma::Col<size_t> resulting_neighbors_naive;
- // arma::vec distances_naive;
- // naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
- //
- // for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++)
- // {
- // BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- // BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- // }
- //}
- //
- ///***
- // * Test the dual-tree nearest-neighbors method with the naive method. This uses
- // * only a reference dataset.
- // *
- // * Errors are produced if the results are not identical.
- // */
- //BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_2)
- //{
- // arma::mat data_for_tree_;
- //
- // // Hard-coded filename: bad!
- // // Code duplication: also bad!
- // if (data::Load("test_data_3_1000.csv", data_for_tree_) != true)
- // {
- // BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
- // }
- //
- // // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
- // arma::mat dual_query(data_for_tree_);
- // arma::mat naive_query(data_for_tree_);
- //
- // AllkNN allknn_(dual_query, 20, 1);
- // AllkNN naive_(naive_query, 1 /* leaf_size ignored with naive */, 1,
- // AllkNN::NAIVE);
- //
- // arma::Col<size_t> resulting_neighbors_tree;
- // arma::vec distances_tree;
- // allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
- //
- // arma::Col<size_t> resulting_neighbors_naive;
- // arma::vec distances_naive;
- // naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
- //
- // for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
- // BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- // BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- // }
- //}
- //
- ///***
- // * Test the single-tree nearest-neighbors method with the naive method. This
- // * uses only a reference dataset.
- // *
- // * Errors are produced if the results are not identical.
- // */
- //BOOST_AUTO_TEST_CASE(single_tree_vs_naive)
- //{
- // arma::mat data_for_tree_;
- //
- // // Hard-coded filename: bad!
- // // Code duplication: also bad!
- // if (data::Load("test_data_3_1000.csv", data_for_tree_) != true)
- // BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
- //
- // // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
- // arma::mat single_query(data_for_tree_);
- // arma::mat naive_query(data_for_tree_);
- //
- // AllkNN allknn_(single_query, 20, 5, AllkNN::MODE_SINGLE);
- // AllkNN naive_(naive_query, 1 /* leaf_size ignored with naive */, 5,
- // AllkNN::NAIVE);
- //
- // arma::Col<size_t> resulting_neighbors_tree;
- // arma::vec distances_tree;
- // allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
- //
- // arma::Col<size_t> resulting_neighbors_naive;
- // arma::vec distances_naive;
- // naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
- //
- // for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
- // BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
- // BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
- // }
- //}
+}
+//BOOST_AUTO_TEST_CASE(linear_kernel_test_2)
+//{
+// // initialize the matrix
+// arma::mat data;
+//
+// data << 2.8532 << 0.6808 << -18 << 6.5 << -26.23 << -273.67 << 3.1747
+// << 1.4824 << 2.0161 << -11.142 << -31.166 << 0 << -1.0324
+// << -8.2685 << 3.48 << 8.545 << 6.575 << -7.89 << 1.9919 << -7
+// << 1 << arma::endr
+// << 0.0578 << 1.3971 << -24 << -12.1 << -100.13 << -553.18 << 2.3804
+// << -0.5607 << 4.5496 << -24.574 << -24.968 << -1 << -13.534 << -31.45
+// << 1.54 << -13.05 << -11.725 << -2.55 << 1.8087 << 10 << 1
+// << arma::endr
+// << 0.1804 << 1.6302 << -26 << -6.4 << -81.28 << -529.73 << 2.0123
+// << 1.0011 << 2.1322 << -38.049 << -22.548 << -1 << -17.921
+// << -29.786 << -4.2 << -5.885 << -12.065 << -0.18 << 3.7498
+// << 8 << 1 << arma::endr
+// << 2.2685 << 0.77 << -26 << -5 << -63.74 << -619.41 << 3.79 << 1.4824
+// << 2.6626 << -11.553 << -10.084 << -1 << -23.293 << -24.595 << 5.06
+// << 1.81 << -6.8 << -2.74 << 1.4367 << 2 << 1 << arma::endr
+// << 1.1094 << 1.3512 << -27 << -5.9 << -97.98 << -888.82 << 4.7719
+// << -0.8833 << 7.0048 << -41.996 << -25.473 << 0 << -17.197 << -31.864
+// << 2.96 << -12.92 << -21.625 << -4.66 << 4.1161 << 14 << 1
+// << arma::endr
+// << -0.7448 << 0.639 << -33 << -4.3 << -121.87 << -1240.2 << 2.574
+// << -11.96 << -1.4605 << -23.163 << -41.738 << 0 << -26.078
+// << -65.459 << -3.46 << -43.665 << -47.125 << 0.36 << 1.5793
+// << 11 << 1 << arma::endr
+// << 2.1242 << 1.0001 << -27 << -1.5 << -85.01 << -753.81 << 5.8026
+// << 0.4733 << 7.3244 << -28.602 << -22.096 << -1 << -25.72 << -5.058
+// << 1.96 << -2.715 << -36.435 << 6.23 << 4.0664 << 4 << 1 << arma::endr
+// << 1.5147 << 1.5999 << -30 << 9.5 << -48.64 << -552.72 << 6.8646
+// << -2.7514 << 9.765 << -32.567 << -26.009 << -1 << -21.244 << -42.178
+// << -4.38 << -14.655 << -35.8 << -8.5 << 3.5578 << -3 << 1
+// << arma::endr
+// << 0.2762 << 0.4498 << -33 << -14.5 << -108.06 << -1848.7 << 3.3279
+// << -4.7404 << 8.3787 << -69.738 << -45.989 << -2 << -20.988 << -87.26
+// << 5.26 << -41.045 << -46.53 << 2.92 << 0.2006 << 17 << 1
+// << arma::endr
+// << -0.8361 << 0.5906 << -35 << -9.9 << -123.2 << -1535.6 << 1.7498
+// << -9.412 << 4.8324 << -61.991 << -45.081 << -1 << -28.325 << -85.874
+// << -10.38 << -36.53 << -51.315 << -4.12 << 1.5208 << 28 << 1
+// << arma::endr
+// << 3.4862 << 0.1746 << -8 << -12.6 << -48.05 << -74.518 << -5.3072 << 0
+// << -0.6322 << 14.706 << 4.6319 << -1 << -5.1945 << -10.268 << -13.8
+// << -18.11 << -2.625 << -9.7 << 0.0399 << -11 << 0 << arma::endr
+// << -1.332 << -0.5399 << 6 << -11.5 << -7.1 << -41.538 << -0.4827
+// << 0.1494 << 2.6331 << 13.711 << -20.669 << 1 << -2.307 << -16.457
+// << -5.3 << -21.65 << 3.57 << 6.82 << -1.4101 << -6 << 0
+// << arma::endr
+// << 0.6689 << 0.1837 << 2 << -1.9 << 10.22 << 176.41 << -1.2294 << 0
+// << -1.7885 << -16.553 << -2.9748 << 0 << 9.304 << 10.097 << -4.32
+// << -7.325 << -6.375 << 13.96 << -0.1116 << 25 << 0 << arma::endr
+// << -0.9639 << -0.6035 << 15 << 17.7 << 82.64 << 849.54 << 1.7272
+// << 2.3738 << -2.3887 << -4.4992 << 31.715 << -1 << 18.632 << -4.4991
+// << 13 << 8.955 << 12.725 << -19.04 << 0.218 << -24 << 0 << arma::endr
+// << -3.5669 << -0.4114 << 19 << 11.8 << 84.35 << 749.12 << -3.8668
+// << 1.9631 << 3.2414 << 25.695 << 28.835 << 1 << 30.208 << 34.002
+// << 14.78 << 46.875 << 24.84 << 8.1 << -1.7294 << -7 << 0 << arma::endr
+// << 1.5881 << -0.023 << 24 << 6.8 << 108.42 << 1255.7 << 2.0539 << 12.079
+// << 1.7954 << 21.008 << 33.312 << 0 << 27.819 << 22.083 << 15.94
+// << 71.565 << 49.805 << 3.94 << 0.3416 << -12 << 0 << arma::endr
+// << -1.5349 << -0.5047 << 10 << -12.2 << 12.26 << 476.02 << -4.4551 << 0
+// << -7.213 << 7.56 << 2.4955 << 0 << 18.81 << -13.185 << -11 << 9.86
+// << -12.555 << -3.63 << -2.4305 << -7 << 0 << arma::endr
+// << 1.9474 << -1.9588 << 26 << 29.8 << 156.44 << 1885.6 << 1.7485
+// << 12.592 << -4.1963 << 17.719 << 45.104 << 0 << 29.739 << 82.289
+// << 17 << 62.15 << 41.805 << -13.57 << 1.4464 << -48 << 0
+// << arma::endr
+// << 1.9065 << -0.1575 << 24 << -8.5 << 59.85 << 577 << -6.8442 << -0.4728
+// << -1.5632 << 19.76 << 13.641 << -2 << 32.432 << 34.894 << -0.9
+// << 52.775 << 23.125 << -24 << -8.2289 << -18 << 0 << arma::endr
+// << -0.6037 << -0.4235 << 21 << 18.6 << 137.71 << 1506.4 << -0.9627
+// << 11.03 << -2.7523 << 47.119 << 58.999 << 1 << 21.233 << 74.764
+// << 21.96 << 72.49 << 54.675 << -10.04 << 1.916 << -12 << 0
+// << arma::endr;
+//
+// // test the linear kernel
+// NNSVM<SVMLinearKernel>* nnsvm = new NNSVM<SVMLinearKernel>();
+//
+// nnsvm.InitTrain(data, 2);
+// double calculatedThreshold = nnsvm.getThreshold();
+// size_t calculatedSupportVectorCount = nnsvm.getSupportVectorCount();
+// const arma::vec calculatedSupportVectorCoefficients =
+// nnsvm.getSupportVectorCoefficients();
+// const arma::vec calculatedWeightVector = nnsvm.getWeightVector();
+//
+// // check for correctness on the linear kernel
+// BOOST_REQUIRE(calculatedSupportVectorCount == 6);
+// BOOST_REQUIRE_CLOSE(calculatedThreshold, -39.793784137999701, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[0],
+// 0.06875628588658407, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[1],
+// 3.1607079296638054, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[2],
+// 0.0039166013511236601, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[3],
+// 3.7130510722124139, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[4],
+// 0.43343566949350276, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedSupportVectorCoefficients[5],
+// -10.000, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedWeightVector[0], 0.00000000, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedWeightVector[1], 0.00000000, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedWeightVector[2], 0.00000000, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedWeightVector[3], 0.00000000, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedWeightVector[4], 0.053222444790909262, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedWeightVector[5], 0.00000000, 1e-5);
+// BOOST_REQUIRE_CLOSE(calculatedWeightVector[6], 0.00000000, 1e-5);
+//
+//}
+///***
+// * Test the dual-tree nearest-neighbors method with the naive method. This
+// * uses both a query and reference dataset.
+// *
+// * Errors are produced if the results are not identical.
+// */
+//BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_1)
+//{
+// arma::mat data_for_tree_;
+//
+// // Hard-coded filename: bad!
+// if (data::Load("test_data_3_1000.csv", data_for_tree_) != true)
+// {
+// BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+// }
+//
+// // Set up matrices to work with.
+// arma::mat dual_query(data_for_tree_);
+// arma::mat dual_references(data_for_tree_);
+// arma::mat naive_query(data_for_tree_);
+// arma::mat naive_references(data_for_tree_);
+//
+// AllkNN allknn_(dual_query, dual_references, 20, 5);
+// AllkNN naive_(naive_query, naive_references, 1 /* leaf_size ignored */, 5,
+// AllkNN::NAIVE);
+//
+// arma::Col<size_t> resulting_neighbors_tree;
+// arma::vec distances_tree;
+// allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+//
+// arma::Col<size_t> resulting_neighbors_naive;
+// arma::vec distances_naive;
+// naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+//
+// for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++)
+// {
+// BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+// BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+// }
+//}
+//
+///***
+// * Test the dual-tree nearest-neighbors method with the naive method. This uses
+// * only a reference dataset.
+// *
+// * Errors are produced if the results are not identical.
+// */
+//BOOST_AUTO_TEST_CASE(dual_tree_vs_naive_2)
+//{
+// arma::mat data_for_tree_;
+//
+// // Hard-coded filename: bad!
+// // Code duplication: also bad!
+// if (data::Load("test_data_3_1000.csv", data_for_tree_) != true)
+// {
+// BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+// }
+//
+// // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
+// arma::mat dual_query(data_for_tree_);
+// arma::mat naive_query(data_for_tree_);
+//
+// AllkNN allknn_(dual_query, 20, 1);
+// AllkNN naive_(naive_query, 1 /* leaf_size ignored with naive */, 1,
+// AllkNN::NAIVE);
+//
+// arma::Col<size_t> resulting_neighbors_tree;
+// arma::vec distances_tree;
+// allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+//
+// arma::Col<size_t> resulting_neighbors_naive;
+// arma::vec distances_naive;
+// naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+//
+// for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
+// BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+// BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+// }
+//}
+//
+///***
+// * Test the single-tree nearest-neighbors method with the naive method. This
+// * uses only a reference dataset.
+// *
+// * Errors are produced if the results are not identical.
+// */
+//BOOST_AUTO_TEST_CASE(single_tree_vs_naive)
+//{
+// arma::mat data_for_tree_;
+//
+// // Hard-coded filename: bad!
+// // Code duplication: also bad!
+// if (data::Load("test_data_3_1000.csv", data_for_tree_) != true)
+// BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!");
+//
+// // Set up matrices to work with (may not be necessary with no ALIAS_MATRIX?).
+// arma::mat single_query(data_for_tree_);
+// arma::mat naive_query(data_for_tree_);
+//
+// AllkNN allknn_(single_query, 20, 5, AllkNN::MODE_SINGLE);
+// AllkNN naive_(naive_query, 1 /* leaf_size ignored with naive */, 5,
+// AllkNN::NAIVE);
+//
+// arma::Col<size_t> resulting_neighbors_tree;
+// arma::vec distances_tree;
+// allknn_.ComputeNeighbors(resulting_neighbors_tree, distances_tree);
+//
+// arma::Col<size_t> resulting_neighbors_naive;
+// arma::vec distances_naive;
+// naive_.ComputeNeighbors(resulting_neighbors_naive, distances_naive);
+//
+// for (size_t i = 0; i < resulting_neighbors_tree.n_elem; i++) {
+// BOOST_REQUIRE(resulting_neighbors_tree[i] == resulting_neighbors_naive[i]);
+// BOOST_REQUIRE_CLOSE(distances_tree[i], distances_naive[i], 1e-5);
+// }
+//}
+
BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/ridge_regression_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/ridge_regression_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/ridge_regression_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,91 +0,0 @@
-/*
- * =====================================================================================
- *
- * Filename: ridge_regression_test.cc
- *
- * Description:
- *
- * Version: 1.0
- * Created: 02/15/2009 01:34:25 PM EST
- * Revision: none
- * Compiler: gcc
- *
- * Author: Nikolaos Vasiloglou (NV), nvasil at ieee.org
- * Company: Georgia Tech Fastlab-ESP Lab
- *
- * =====================================================================================
- */
-#include <mlpack/core.h>
-#include <mlpack/methods/regression/ridge_regression.h>
-#include <mlpack/methods/regression/ridge_regression_util.h>
-
-#include <iostream>
-
-#include <boost/test/unit_test.hpp>
-
-using namespace mlpack;
-using namespace regression;
-
-BOOST_AUTO_TEST_SUITE(RidgeRegressoinTest);
-
- /*
- BOOST_AUTO_TEST_CASE(TestSVDNormalEquationRegressVersusSVDRegress) {
-
- arma::mat predictors_ = "1.2 4.2 2.1 0.3 4.2;"
- "3.1 1.1 4.7 1.8 0.4;"
- "2.5 3.3 9.1 7.4 0.1";
- arma::mat predictions_ = "0.4 0.33 0.8 1.4 3.3";
- arma::mat true_factors_ = "0; 0; 0";
-
- RidgeRegression engine_(predictors_, predictions_, true);
- engine_.SVDRegress(0);
- RidgeRegression svd_engine(predictors_, predictions_, false);
- svd_engine.SVDRegress(0);
- arma::mat factors, svd_factors;
-
- engine_.factors(&factors);
- svd_engine.factors(&svd_factors);
-
- for(size_t i=0; i<factors.n_rows; i++) {
- BOOST_REQUIRE_CLOSE(factors(i, 0), svd_factors(i, 0), 1e-5);
- }
- }
- */
-
- BOOST_AUTO_TEST_CASE(TestVIFBasedFeatureSelection) {
- // Craft a synthetic dataset in which the third dimension is
- // completely dependent on the first and the second.
- arma::mat synthetic_data;
- arma::mat synthetic_data_target_training_values;
- synthetic_data.zeros(4, 5);
- synthetic_data_target_training_values.zeros(1, 5);
- for(size_t i = 0; i < 5; i++) {
- synthetic_data(0, i) = i;
- synthetic_data(1, i) = 3 * i + 1;
- synthetic_data(2, i) = 4;
- synthetic_data(3, i) = 5;
- synthetic_data_target_training_values(0, i) = i;
- }
- arma::Col<size_t> predictor_indices;
- arma::Col<size_t> prune_predictor_indices;
- arma::Col<size_t> output_predictor_indices;
- predictor_indices.zeros(4);
- predictor_indices[0] = 0;
- predictor_indices[1] = 1;
- predictor_indices[2] = 2;
- predictor_indices[3] = 3;
- prune_predictor_indices = predictor_indices;
- RidgeRegression engine_(synthetic_data, predictor_indices,
- synthetic_data_target_training_values);
- engine_.FeatureSelectedRegression(predictor_indices,
- prune_predictor_indices,
- synthetic_data_target_training_values,
- &output_predictor_indices);
- std::cout << "Output indices: ";
- for(size_t i = 0; i < output_predictor_indices.n_elem; i++) {
- std::cout << output_predictor_indices[i] << ' ';
- }
- std::cout << std::endl;
- }
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/ridge_regression_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/ridge_regression_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/ridge_regression_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/ridge_regression_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,81 @@
+/**
+ * @file ridge_regression_test.cpp
+ *
+ * Test for ridge regression.
+ */
+#include <mlpack/core.h>
+#include <mlpack/methods/regression/ridge_regression.h>
+#include <mlpack/methods/regression/ridge_regression_util.h>
+
+#include <iostream>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace mlpack::regression;
+
+BOOST_AUTO_TEST_SUITE(RidgeRegressionTest);
+
+/*
+BOOST_AUTO_TEST_CASE(TestSVDNormalEquationRegressVersusSVDRegress)
+{
+ arma::mat predictors_ = "1.2 4.2 2.1 0.3 4.2;"
+ "3.1 1.1 4.7 1.8 0.4;"
+ "2.5 3.3 9.1 7.4 0.1";
+ arma::mat predictions_ = "0.4 0.33 0.8 1.4 3.3";
+ arma::mat true_factors_ = "0; 0; 0";
+
+ RidgeRegression engine_(predictors_, predictions_, true);
+ engine_.SVDRegress(0);
+ RidgeRegression svd_engine(predictors_, predictions_, false);
+ svd_engine.SVDRegress(0);
+ arma::mat factors, svd_factors;
+
+ engine_.factors(&factors);
+ svd_engine.factors(&svd_factors);
+
+ for(size_t i=0; i<factors.n_rows; i++)
+ BOOST_REQUIRE_CLOSE(factors(i, 0), svd_factors(i, 0), 1e-5);
+}
+ */
+
+BOOST_AUTO_TEST_CASE(TestVIFBasedFeatureSelection)
+{
+ // Craft a synthetic dataset in which the third dimension is
+ // completely dependent on the first and the second.
+ arma::mat synthetic_data;
+ arma::mat synthetic_data_target_training_values;
+ synthetic_data.zeros(4, 5);
+ synthetic_data_target_training_values.zeros(1, 5);
+
+ for(size_t i = 0; i < 5; i++)
+ {
+ synthetic_data(0, i) = i;
+ synthetic_data(1, i) = 3 * i + 1;
+ synthetic_data(2, i) = 4;
+ synthetic_data(3, i) = 5;
+ synthetic_data_target_training_values(0, i) = i;
+ }
+
+ arma::Col<size_t> predictor_indices;
+ arma::Col<size_t> prune_predictor_indices;
+ arma::Col<size_t> output_predictor_indices;
+ predictor_indices.zeros(4);
+ predictor_indices[0] = 0;
+ predictor_indices[1] = 1;
+ predictor_indices[2] = 2;
+ predictor_indices[3] = 3;
+ prune_predictor_indices = predictor_indices;
+ RidgeRegression engine_(synthetic_data, predictor_indices,
+ synthetic_data_target_training_values);
+ engine_.FeatureSelectedRegression(predictor_indices,
+ prune_predictor_indices,
+ synthetic_data_target_training_values,
+ &output_predictor_indices);
+ std::cout << "Output indices: ";
+ for(size_t i = 0; i < output_predictor_indices.n_elem; i++)
+ std::cout << output_predictor_indices[i] << ' ';
+ std::cout << std::endl;
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/save_restore_utility_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/save_restore_utility_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/save_restore_utility_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,151 +1,156 @@
-/***
+/**
* @file save_restore_model_test.cpp
* @author Neil Slagle
*
* Here we have tests for the SaveRestoreModel class.
*/
-
#include <mlpack/core/utilities/save_restore_utility.hpp>
#include <boost/test/unit_test.hpp>
+
#define ARGSTR(a) a,#a
BOOST_AUTO_TEST_SUITE(SaveRestoreUtilityTests);
- /***
- * Exhibit proper save restore utility usage
- * of child class proper usage
- */
- class SaveRestoreTest
+/*
+ * Exhibit proper save restore utility usage of child class proper usage.
+ */
+class SaveRestoreTest
+{
+ private:
+ size_t anInt;
+ SaveRestoreUtility saveRestore;
+
+ public:
+ SaveRestoreTest()
{
- private:
- size_t anInt;
- SaveRestoreUtility saveRestore;
- public:
- SaveRestoreTest()
- {
- saveRestore = SaveRestoreUtility();
- }
- bool SaveModel (std::string filename)
- {
- saveRestore.SaveParameter (anInt, "anInt");
- return saveRestore.WriteFile (filename);
- }
- bool LoadModel (std::string filename)
- {
- bool success = saveRestore.ReadFile (filename);
- if (success)
- {
- anInt = saveRestore.LoadParameter (anInt, "anInt");
- }
- return success;
- }
- const size_t AnInt () { return anInt; }
- void AnInt (size_t s) { this->anInt = s; }
- };
+ saveRestore = SaveRestoreUtility();
+ }
- /***
- * Perform a save and restore on basic types.
- */
- BOOST_AUTO_TEST_CASE(save_basic_types)
+ bool SaveModel (std::string filename)
{
- bool b = false;
- char c = 67;
- unsigned u = 34;
- size_t s = 12;
- short sh = 100;
- int i = -23;
- float f = -2.34f;
- double d = 3.14159;
- std::string cc = "Hello world!";
+ saveRestore.SaveParameter (anInt, "anInt");
+ return saveRestore.WriteFile (filename);
+ }
- SaveRestoreUtility* sRM = new SaveRestoreUtility();
+ bool LoadModel (std::string filename)
+ {
+ bool success = saveRestore.ReadFile (filename);
+ if (success)
+ {
+ anInt = saveRestore.LoadParameter (anInt, "anInt");
+ }
+ return success;
+ }
- sRM->SaveParameter (ARGSTR(b));
- sRM->SaveParameter (ARGSTR(c));
- sRM->SaveParameter (ARGSTR(u));
- sRM->SaveParameter (ARGSTR(s));
- sRM->SaveParameter (ARGSTR(sh));
- sRM->SaveParameter (ARGSTR(i));
- sRM->SaveParameter (ARGSTR(f));
- sRM->SaveParameter (ARGSTR(d));
- sRM->SaveParameter (ARGSTR(cc));
- sRM->WriteFile ("test_basic_types.xml");
+ const size_t AnInt () { return anInt; }
+ void AnInt (size_t s) { this->anInt = s; }
+};
- sRM->ReadFile ("test_basic_types.xml");
+/**
+ * Perform a save and restore on basic types.
+ */
+BOOST_AUTO_TEST_CASE(save_basic_types)
+{
+ bool b = false;
+ char c = 67;
+ unsigned u = 34;
+ size_t s = 12;
+ short sh = 100;
+ int i = -23;
+ float f = -2.34f;
+ double d = 3.14159;
+ std::string cc = "Hello world!";
- bool b2 = sRM->LoadParameter (ARGSTR(b));
- char c2 = sRM->LoadParameter (ARGSTR(c));
- unsigned u2 = sRM->LoadParameter (ARGSTR(u));
- size_t s2 = sRM->LoadParameter (ARGSTR(s));
- short sh2 = sRM->LoadParameter (ARGSTR(sh));
- int i2 = sRM->LoadParameter (ARGSTR(i));
- float f2 = sRM->LoadParameter (ARGSTR(f));
- double d2 = sRM->LoadParameter (ARGSTR(d));
- std::string cc2 = sRM->LoadParameter (ARGSTR(cc));
+ SaveRestoreUtility* sRM = new SaveRestoreUtility();
- BOOST_REQUIRE (b == b2);
- BOOST_REQUIRE (c == c2);
- BOOST_REQUIRE (u == u2);
- BOOST_REQUIRE (s == s2);
- BOOST_REQUIRE (sh == sh2);
- BOOST_REQUIRE (i == i2);
- BOOST_REQUIRE (cc == cc2);
- BOOST_REQUIRE_CLOSE (f, f2, 1e-5);
- BOOST_REQUIRE_CLOSE (d, d2, 1e-5);
+ sRM->SaveParameter(ARGSTR(b));
+ sRM->SaveParameter(ARGSTR(c));
+ sRM->SaveParameter(ARGSTR(u));
+ sRM->SaveParameter(ARGSTR(s));
+ sRM->SaveParameter(ARGSTR(sh));
+ sRM->SaveParameter(ARGSTR(i));
+ sRM->SaveParameter(ARGSTR(f));
+ sRM->SaveParameter(ARGSTR(d));
+ sRM->SaveParameter(ARGSTR(cc));
+ sRM->WriteFile("test_basic_types.xml");
- delete sRM;
- }
+ sRM->ReadFile("test_basic_types.xml");
- /***
- * Test the arma::mat functionality.
- */
- BOOST_AUTO_TEST_CASE(save_arma_mat)
- {
- arma::mat matrix;
- matrix << 1.2 << 2.3 << -0.1 << arma::endr
- << 3.5 << 2.4 << -1.2 << arma::endr
- << -0.1 << 3.4 << -7.8 << arma::endr;
- SaveRestoreUtility* sRM = new SaveRestoreUtility();
+ bool b2 = sRM->LoadParameter(ARGSTR(b));
+ char c2 = sRM->LoadParameter(ARGSTR(c));
+ unsigned u2 = sRM->LoadParameter(ARGSTR(u));
+ size_t s2 = sRM->LoadParameter(ARGSTR(s));
+ short sh2 = sRM->LoadParameter(ARGSTR(sh));
+ int i2 = sRM->LoadParameter(ARGSTR(i));
+ float f2 = sRM->LoadParameter(ARGSTR(f));
+ double d2 = sRM->LoadParameter(ARGSTR(d));
+ std::string cc2 = sRM->LoadParameter(ARGSTR(cc));
- sRM->SaveParameter (ARGSTR (matrix));
+ BOOST_REQUIRE(b == b2);
+ BOOST_REQUIRE(c == c2);
+ BOOST_REQUIRE(u == u2);
+ BOOST_REQUIRE(s == s2);
+ BOOST_REQUIRE(sh == sh2);
+ BOOST_REQUIRE(i == i2);
+ BOOST_REQUIRE(cc == cc2);
+ BOOST_REQUIRE_CLOSE(f, f2, 1e-5);
+ BOOST_REQUIRE_CLOSE(d, d2, 1e-5);
- sRM->WriteFile ("test_arma_mat_type.xml");
+ delete sRM;
+}
- sRM->ReadFile ("test_arma_mat_type.xml");
+/**
+ * Test the arma::mat functionality.
+ */
+BOOST_AUTO_TEST_CASE(save_arma_mat)
+{
+ arma::mat matrix;
+ matrix << 1.2 << 2.3 << -0.1 << arma::endr
+ << 3.5 << 2.4 << -1.2 << arma::endr
+ << -0.1 << 3.4 << -7.8 << arma::endr;
- arma::mat matrix2 = sRM->LoadParameter (ARGSTR (matrix));
+ SaveRestoreUtility* sRM = new SaveRestoreUtility();
- for (size_t row = 0; row < matrix.n_rows; ++row)
+ sRM->SaveParameter(ARGSTR(matrix));
+
+ sRM->WriteFile("test_arma_mat_type.xml");
+
+ sRM->ReadFile("test_arma_mat_type.xml");
+
+ arma::mat matrix2 = sRM->LoadParameter(ARGSTR(matrix));
+
+ for (size_t row = 0; row < matrix.n_rows; ++row)
+ {
+ for (size_t column = 0; column < matrix.n_cols; ++column)
{
- for (size_t column = 0; column < matrix.n_cols; ++column)
- {
- BOOST_REQUIRE_CLOSE(matrix(row,column), matrix2(row,column), 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(matrix(row,column), matrix2(row,column), 1e-5);
}
-
- delete sRM;
}
- /***
- * Test SaveRestoreModel proper usage in child classes and loading from
- * separately defined objects
- */
- BOOST_AUTO_TEST_CASE(save_restore_model_child_class_usage)
- {
- SaveRestoreTest* saver = new SaveRestoreTest();
- SaveRestoreTest* loader = new SaveRestoreTest();
- size_t s = 1200;
- const char* filename = "anInt.xml";
- saver->AnInt (s);
- saver->SaveModel (filename);
- delete saver;
+ delete sRM;
+}
- loader->LoadModel (filename);
+/**
+ * Test SaveRestoreModel proper usage in child classes and loading from
+ * separately defined objects
+ */
+BOOST_AUTO_TEST_CASE(save_restore_model_child_class_usage)
+{
+ SaveRestoreTest* saver = new SaveRestoreTest();
+ SaveRestoreTest* loader = new SaveRestoreTest();
+ size_t s = 1200;
+ const char* filename = "anInt.xml";
- BOOST_REQUIRE (loader->AnInt () == s);
+ saver->AnInt(s);
+ saver->SaveModel(filename);
+ delete saver;
- delete loader;
- }
+ loader->LoadModel(filename);
+ BOOST_REQUIRE(loader->AnInt() == s);
+
+ delete loader;
+}
+
BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/sort_policy_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/sort_policy_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/sort_policy_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,310 +0,0 @@
-/**
- * @file sort_policy_test.cc
- * @author Ryan Curtin
- *
- * Tests for each of the implementations of the SortPolicy class.
- */
-#include <mlpack/core.h>
-#include <mlpack/core/tree/bounds.hpp>
-#include <mlpack/core/tree/binary_space_tree.hpp>
-
-// Classes to test.
-#include <mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp>
-#include <mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp>
-
-#include <boost/test/unit_test.hpp>
-
-using namespace mlpack;
-using namespace mlpack::neighbor;
-using namespace mlpack::bound;
-
-// Tests for NearestNeighborSort
-
-/***
- * Ensure the best distance for nearest neighbors is 0.
- */
-BOOST_AUTO_TEST_CASE(nns_best_distance) {
- BOOST_REQUIRE(NearestNeighborSort::BestDistance() == 0);
-}
-
-/***
- * Ensure the worst distance for nearest neighbors is DBL_MAX.
- */
-BOOST_AUTO_TEST_CASE(nns_worst_distance) {
- BOOST_REQUIRE(NearestNeighborSort::WorstDistance() == DBL_MAX);
-}
-
-/***
- * Make sure the comparison works for values strictly less than the reference.
- */
-BOOST_AUTO_TEST_CASE(nns_is_better_strict) {
- BOOST_REQUIRE(NearestNeighborSort::IsBetter(5.0, 6.0) == true);
-}
-
-/***
- * Warn in case the comparison is not strict.
- */
-BOOST_AUTO_TEST_CASE(nns_is_better_not_strict) {
- BOOST_WARN(NearestNeighborSort::IsBetter(6.0, 6.0) == true);
-}
-
-/***
- * A simple test case of where to insert when all the values in the list are
- * DBL_MAX.
- */
-BOOST_AUTO_TEST_CASE(nns_sort_distance_all_dbl_max) {
- arma::vec list(5);
- list.fill(DBL_MAX);
-
- // Should be inserted at the head of the list.
- BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 5.0) == 0);
-}
-
-/***
- * Another test case, where we are just putting the new value in the middle of
- * the list.
- */
-BOOST_AUTO_TEST_CASE(nns_sort_distance_2) {
- arma::vec list(3);
- list[0] = 0.66;
- list[1] = 0.89;
- list[2] = 1.14;
-
- // Run a couple possibilities through.
- BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 0.61) == 0);
- BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 0.76) == 1);
- BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 0.99) == 2);
- BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 1.22) ==
- (size_t() - 1));
-}
-
-/***
- * Very simple sanity check to ensure that bounds are working alright. We will
- * use a one-dimensional bound for simplicity.
- */
-BOOST_AUTO_TEST_CASE(nns_node_to_node_distance) {
- // Well, there's no easy way to make HRectBounds the way we want, so we have
- // to make them and then expand the region to include new points.
- tree::BinarySpaceTree<HRectBound<2>, arma::mat> node_one;
- arma::vec utility(1);
- utility[0] = 0;
-
- node_one.bound() = HRectBound<2>(1);
- node_one.bound() |= utility;
- utility[0] = 1;
- node_one.bound() |= utility;
-
- tree::BinarySpaceTree<HRectBound<2>, arma::mat> node_two;
- node_two.bound() = HRectBound<2>(1);
-
- utility[0] = 5;
- node_two.bound() |= utility;
- utility[0] = 6;
- node_two.bound() |= utility;
-
- // This should use the L2 squared distance.
- BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestNodeToNodeDistance(&node_one,
- &node_two), 16.0, 1e-5);
-
- // And another just to be sure, from the other side.
- node_two.bound().Clear();
- utility[0] = -2;
- node_two.bound() |= utility;
- utility[0] = -1;
- node_two.bound() |= utility;
-
- // Again, the distance is the L2 squared distance.
- BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestNodeToNodeDistance(&node_one,
- &node_two), 1.0, 1e-5);
-
- // Now, when the bounds overlap.
- node_two.bound().Clear();
- utility[0] = -0.5;
- node_two.bound() |= utility;
- utility[0] = 0.5;
- node_two.bound() |= utility;
-
- BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestNodeToNodeDistance(&node_one,
- &node_two), 0.0, 1e-5);
-}
-
-/***
- * Another very simple sanity check for the point-to-node case, again in one
- * dimension.
- */
-BOOST_AUTO_TEST_CASE(nns_point_to_node_distance) {
- // Well, there's no easy way to make HRectBounds the way we want, so we have
- // to make them and then expand the region to include new points.
- arma::vec utility(1);
- utility[0] = 0;
-
- tree::BinarySpaceTree<HRectBound<2>, arma::mat> node;
- node.bound() = HRectBound<2>(1);
- node.bound() |= utility;
- utility[0] = 1;
- node.bound() |= utility;
-
- arma::vec point(1);
- point[0] = -0.5;
-
- // The distance is the L2 squared distance.
- BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestPointToNodeDistance(point,
- &node), 0.25, 1e-5);
-
- // Now from the other side of the bound.
- point[0] = 1.5;
-
- BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestPointToNodeDistance(point,
- &node), 0.25, 1e-5);
-
- // And now when the point is inside the bound.
- point[0] = 0.5;
-
- BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestPointToNodeDistance(point,
- &node), 0.0, 1e-5);
-}
-
-// Tests for FurthestNeighborSort
-
-/***
- * Ensure the best distance for furthest neighbors is DBL_MAX.
- */
-BOOST_AUTO_TEST_CASE(fns_best_distance) {
- BOOST_REQUIRE(FurthestNeighborSort::BestDistance() == DBL_MAX);
-}
-
-/***
- * Ensure the worst distance for furthest neighbors is 0.
- */
-BOOST_AUTO_TEST_CASE(fns_worst_distance) {
- BOOST_REQUIRE(FurthestNeighborSort::WorstDistance() == 0);
-}
-
-/***
- * Make sure the comparison works for values strictly less than the reference.
- */
-BOOST_AUTO_TEST_CASE(fns_is_better_strict) {
- BOOST_REQUIRE(FurthestNeighborSort::IsBetter(5.0, 4.0) == true);
-}
-
-/***
- * Warn in case the comparison is not strict.
- */
-BOOST_AUTO_TEST_CASE(fns_is_better_not_strict) {
- BOOST_WARN(FurthestNeighborSort::IsBetter(6.0, 6.0) == true);
-}
-
-/***
- * A simple test case of where to insert when all the values in the list are
- * 0.
- */
-BOOST_AUTO_TEST_CASE(fns_sort_distance_all_zero) {
- arma::vec list(5);
- list.fill(0);
-
- // Should be inserted at the head of the list.
- BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 5.0) == 0);
-}
-
-/***
- * Another test case, where we are just putting the new value in the middle of
- * the list.
- */
-BOOST_AUTO_TEST_CASE(fns_sort_distance_2) {
- arma::vec list(3);
- list[0] = 1.14;
- list[1] = 0.89;
- list[2] = 0.66;
-
- // Run a couple possibilities through.
- BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 1.22) == 0);
- BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 0.93) == 1);
- BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 0.68) == 2);
- BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 0.62) ==
- (size_t() - 1));
-}
-
-/***
- * Very simple sanity check to ensure that bounds are working alright. We will
- * use a one-dimensional bound for simplicity.
- */
-BOOST_AUTO_TEST_CASE(fns_node_to_node_distance) {
- // Well, there's no easy way to make HRectBounds the way we want, so we have
- // to make them and then expand the region to include new points.
- arma::vec utility(1);
- utility[0] = 0;
-
- tree::BinarySpaceTree<HRectBound<2>, arma::mat> node_one;
- node_one.bound() = HRectBound<2>(1);
- node_one.bound() |= utility;
- utility[0] = 1;
- node_one.bound() |= utility;
-
- tree::BinarySpaceTree<HRectBound<2>, arma::mat> node_two;
- node_two.bound() = HRectBound<2>(1);
- utility[0] = 5;
- node_two.bound() |= utility;
- utility[0] = 6;
- node_two.bound() |= utility;
-
- // This should use the L2 squared distance.
- BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestNodeToNodeDistance(&node_one,
- &node_two), 36.0, 1e-5);
-
- // And another just to be sure, from the other side.
- node_two.bound().Clear();
- utility[0] = -2;
- node_two.bound() |= utility;
- utility[0] = -1;
- node_two.bound() |= utility;
-
- // Again, the distance is the L2 squared distance.
- BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestNodeToNodeDistance(&node_one,
- &node_two), 9.0, 1e-5);
-
- // Now, when the bounds overlap.
- node_two.bound().Clear();
- utility[0] = -0.5;
- node_two.bound() |= utility;
- utility[0] = 0.5;
- node_two.bound() |= utility;
-
- BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestNodeToNodeDistance(&node_one,
- &node_two), (1.5 * 1.5), 1e-5);
-}
-
-/***
- * Another very simple sanity check for the point-to-node case, again in one
- * dimension.
- */
-BOOST_AUTO_TEST_CASE(fns_point_to_node_distance) {
- // Well, there's no easy way to make HRectBounds the way we want, so we have
- // to make them and then expand the region to include new points.
- arma::vec utility(1);
- utility[0] = 0;
-
- tree::BinarySpaceTree<HRectBound<2>, arma::mat> node;
- node.bound() = HRectBound<2>(1);
- node.bound() |= utility;
- utility[0] = 1;
- node.bound() |= utility;
-
- arma::vec point(1);
- point[0] = -0.5;
-
- // The distance is the L2 squared distance.
- BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestPointToNodeDistance(point,
- &node), (1.5 * 1.5), 1e-5);
-
- // Now from the other side of the bound.
- point[0] = 1.5;
-
- BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestPointToNodeDistance(point,
- &node), (1.5 * 1.5), 1e-5);
-
- // And now when the point is inside the bound.
- point[0] = 0.5;
-
- BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestPointToNodeDistance(point,
- &node), 0.25, 1e-5);
-}
Copied: mlpack/trunk/src/mlpack/tests/sort_policy_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/sort_policy_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/sort_policy_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/sort_policy_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,330 @@
+/**
+ * @file sort_policy_test.cpp
+ * @author Ryan Curtin
+ *
+ * Tests for each of the implementations of the SortPolicy class.
+ */
+#include <mlpack/core.h>
+#include <mlpack/core/tree/bounds.hpp>
+#include <mlpack/core/tree/binary_space_tree.hpp>
+
+// Classes to test.
+#include <mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp>
+#include <mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace mlpack;
+using namespace mlpack::neighbor;
+using namespace mlpack::bound;
+
+BOOST_AUTO_TEST_SUITE(SortPolicyTest);
+
+// Tests for NearestNeighborSort
+
+/**
+ * Ensure the best distance for nearest neighbors is 0.
+ */
+BOOST_AUTO_TEST_CASE(nns_best_distance)
+{
+ BOOST_REQUIRE(NearestNeighborSort::BestDistance() == 0);
+}
+
+/**
+ * Ensure the worst distance for nearest neighbors is DBL_MAX.
+ */
+BOOST_AUTO_TEST_CASE(nns_worst_distance)
+{
+ BOOST_REQUIRE(NearestNeighborSort::WorstDistance() == DBL_MAX);
+}
+
+/**
+ * Make sure the comparison works for values strictly less than the reference.
+ */
+BOOST_AUTO_TEST_CASE(nns_is_better_strict)
+{
+ BOOST_REQUIRE(NearestNeighborSort::IsBetter(5.0, 6.0) == true);
+}
+
+/**
+ * Warn in case the comparison is not strict.
+ */
+BOOST_AUTO_TEST_CASE(nns_is_better_not_strict)
+{
+ BOOST_WARN(NearestNeighborSort::IsBetter(6.0, 6.0) == true);
+}
+
+/**
+ * A simple test case of where to insert when all the values in the list are
+ * DBL_MAX.
+ */
+BOOST_AUTO_TEST_CASE(nns_sort_distance_all_dbl_max)
+{
+ arma::vec list(5);
+ list.fill(DBL_MAX);
+
+ // Should be inserted at the head of the list.
+ BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 5.0) == 0);
+}
+
+/**
+ * Another test case, where we are just putting the new value in the middle of
+ * the list.
+ */
+BOOST_AUTO_TEST_CASE(nns_sort_distance_2)
+{
+ arma::vec list(3);
+ list[0] = 0.66;
+ list[1] = 0.89;
+ list[2] = 1.14;
+
+ // Run a couple possibilities through.
+ BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 0.61) == 0);
+ BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 0.76) == 1);
+ BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 0.99) == 2);
+ BOOST_REQUIRE(NearestNeighborSort::SortDistance(list, 1.22) ==
+ (size_t() - 1));
+}
+
+/**
+ * Very simple sanity check to ensure that bounds are working alright. We will
+ * use a one-dimensional bound for simplicity.
+ */
+BOOST_AUTO_TEST_CASE(nns_node_to_node_distance)
+{
+ // Well, there's no easy way to make HRectBounds the way we want, so we have
+ // to make them and then expand the region to include new points.
+ tree::BinarySpaceTree<HRectBound<2>, arma::mat> node_one;
+ arma::vec utility(1);
+ utility[0] = 0;
+
+ node_one.bound() = HRectBound<2>(1);
+ node_one.bound() |= utility;
+ utility[0] = 1;
+ node_one.bound() |= utility;
+
+ tree::BinarySpaceTree<HRectBound<2>, arma::mat> node_two;
+ node_two.bound() = HRectBound<2>(1);
+
+ utility[0] = 5;
+ node_two.bound() |= utility;
+ utility[0] = 6;
+ node_two.bound() |= utility;
+
+ // This should use the L2 squared distance.
+ BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestNodeToNodeDistance(&node_one,
+ &node_two), 16.0, 1e-5);
+
+ // And another just to be sure, from the other side.
+ node_two.bound().Clear();
+ utility[0] = -2;
+ node_two.bound() |= utility;
+ utility[0] = -1;
+ node_two.bound() |= utility;
+
+ // Again, the distance is the L2 squared distance.
+ BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestNodeToNodeDistance(&node_one,
+ &node_two), 1.0, 1e-5);
+
+ // Now, when the bounds overlap.
+ node_two.bound().Clear();
+ utility[0] = -0.5;
+ node_two.bound() |= utility;
+ utility[0] = 0.5;
+ node_two.bound() |= utility;
+
+ BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestNodeToNodeDistance(&node_one,
+ &node_two), 0.0, 1e-5);
+}
+
+/**
+ * Another very simple sanity check for the point-to-node case, again in one
+ * dimension.
+ */
+BOOST_AUTO_TEST_CASE(nns_point_to_node_distance)
+{
+ // Well, there's no easy way to make HRectBounds the way we want, so we have
+ // to make them and then expand the region to include new points.
+ arma::vec utility(1);
+ utility[0] = 0;
+
+ tree::BinarySpaceTree<HRectBound<2>, arma::mat> node;
+ node.bound() = HRectBound<2>(1);
+ node.bound() |= utility;
+ utility[0] = 1;
+ node.bound() |= utility;
+
+ arma::vec point(1);
+ point[0] = -0.5;
+
+ // The distance is the L2 squared distance.
+ BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestPointToNodeDistance(point,
+ &node), 0.25, 1e-5);
+
+ // Now from the other side of the bound.
+ point[0] = 1.5;
+
+ BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestPointToNodeDistance(point,
+ &node), 0.25, 1e-5);
+
+ // And now when the point is inside the bound.
+ point[0] = 0.5;
+
+ BOOST_REQUIRE_CLOSE(NearestNeighborSort::BestPointToNodeDistance(point,
+ &node), 0.0, 1e-5);
+}
+
+// Tests for FurthestNeighborSort
+
+/**
+ * Ensure the best distance for furthest neighbors is DBL_MAX.
+ */
+BOOST_AUTO_TEST_CASE(fns_best_distance)
+{
+ BOOST_REQUIRE(FurthestNeighborSort::BestDistance() == DBL_MAX);
+}
+
+/**
+ * Ensure the worst distance for furthest neighbors is 0.
+ */
+BOOST_AUTO_TEST_CASE(fns_worst_distance)
+{
+ BOOST_REQUIRE(FurthestNeighborSort::WorstDistance() == 0);
+}
+
+/**
+ * Make sure the comparison works for values strictly less than the reference.
+ */
+BOOST_AUTO_TEST_CASE(fns_is_better_strict)
+{
+ BOOST_REQUIRE(FurthestNeighborSort::IsBetter(5.0, 4.0) == true);
+}
+
+/**
+ * Warn in case the comparison is not strict.
+ */
+BOOST_AUTO_TEST_CASE(fns_is_better_not_strict)
+{
+ BOOST_WARN(FurthestNeighborSort::IsBetter(6.0, 6.0) == true);
+}
+
+/**
+ * A simple test case of where to insert when all the values in the list are
+ * 0.
+ */
+BOOST_AUTO_TEST_CASE(fns_sort_distance_all_zero)
+{
+ arma::vec list(5);
+ list.fill(0);
+
+ // Should be inserted at the head of the list.
+ BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 5.0) == 0);
+}
+
+/**
+ * Another test case, where we are just putting the new value in the middle of
+ * the list.
+ */
+BOOST_AUTO_TEST_CASE(fns_sort_distance_2)
+{
+ arma::vec list(3);
+ list[0] = 1.14;
+ list[1] = 0.89;
+ list[2] = 0.66;
+
+ // Run a couple possibilities through.
+ BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 1.22) == 0);
+ BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 0.93) == 1);
+ BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 0.68) == 2);
+ BOOST_REQUIRE(FurthestNeighborSort::SortDistance(list, 0.62) ==
+ (size_t() - 1));
+}
+
+/**
+ * Very simple sanity check to ensure that bounds are working alright. We will
+ * use a one-dimensional bound for simplicity.
+ */
+BOOST_AUTO_TEST_CASE(fns_node_to_node_distance)
+{
+ // Well, there's no easy way to make HRectBounds the way we want, so we have
+ // to make them and then expand the region to include new points.
+ arma::vec utility(1);
+ utility[0] = 0;
+
+ tree::BinarySpaceTree<HRectBound<2>, arma::mat> node_one;
+ node_one.bound() = HRectBound<2>(1);
+ node_one.bound() |= utility;
+ utility[0] = 1;
+ node_one.bound() |= utility;
+
+ tree::BinarySpaceTree<HRectBound<2>, arma::mat> node_two;
+ node_two.bound() = HRectBound<2>(1);
+ utility[0] = 5;
+ node_two.bound() |= utility;
+ utility[0] = 6;
+ node_two.bound() |= utility;
+
+ // This should use the L2 squared distance.
+ BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestNodeToNodeDistance(&node_one,
+ &node_two), 36.0, 1e-5);
+
+ // And another just to be sure, from the other side.
+ node_two.bound().Clear();
+ utility[0] = -2;
+ node_two.bound() |= utility;
+ utility[0] = -1;
+ node_two.bound() |= utility;
+
+ // Again, the distance is the L2 squared distance.
+ BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestNodeToNodeDistance(&node_one,
+ &node_two), 9.0, 1e-5);
+
+ // Now, when the bounds overlap.
+ node_two.bound().Clear();
+ utility[0] = -0.5;
+ node_two.bound() |= utility;
+ utility[0] = 0.5;
+ node_two.bound() |= utility;
+
+ BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestNodeToNodeDistance(&node_one,
+ &node_two), (1.5 * 1.5), 1e-5);
+}
+
+/**
+ * Another very simple sanity check for the point-to-node case, again in one
+ * dimension.
+ */
+BOOST_AUTO_TEST_CASE(fns_point_to_node_distance)
+{
+ // Well, there's no easy way to make HRectBounds the way we want, so we have
+ // to make them and then expand the region to include new points.
+ arma::vec utility(1);
+ utility[0] = 0;
+
+ tree::BinarySpaceTree<HRectBound<2>, arma::mat> node;
+ node.bound() = HRectBound<2>(1);
+ node.bound() |= utility;
+ utility[0] = 1;
+ node.bound() |= utility;
+
+ arma::vec point(1);
+ point[0] = -0.5;
+
+ // The distance is the L2 squared distance.
+ BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestPointToNodeDistance(point,
+ &node), (1.5 * 1.5), 1e-5);
+
+ // Now from the other side of the bound.
+ point[0] = 1.5;
+
+ BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestPointToNodeDistance(point,
+ &node), (1.5 * 1.5), 1e-5);
+
+ // And now when the point is inside the bound.
+ point[0] = 0.5;
+
+ BOOST_REQUIRE_CLOSE(FurthestNeighborSort::BestPointToNodeDistance(point,
+ &node), 0.25, 1e-5);
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Deleted: mlpack/trunk/src/mlpack/tests/svm_test.cc
===================================================================
--- mlpack/trunk/src/mlpack/tests/svm_test.cc 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/svm_test.cc 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,148 +0,0 @@
-#include <mlpack/core.h>
-
-#include <mlpack/methods/svm/svm.h>
-
-#include <boost/test/unit_test.hpp>
-
-using std::string;
-using namespace mlpack;
-using namespace mlpack::svm;
-
-// Create test data
-arma::mat matrix(20,3);
-bool first = true;
-
-/***
- *
- */
-/*
-BOOST_AUTO_TEST_CASE(test_test) {
- double x = 1.0, y = 1.0, z = 2.0;
-
- // We need to make sure someValue is 1.
- BOOST_REQUIRE_CLOSE(x, y, 1e-5);
- BOOST_REQUIRE_CLOSE(z, y, 1e-5);
-}
-*/
-
- //CLI::GetParam<>() = ;
-
-/**
- * Creates the data to train and test with and prints it to stdout.
- * Should only have any effect once.
- */
-void setup() {
- if(!first)
- return;
- first = false;
-
- CLI::GetParam<bool>("svm/shrink") = true;
- CLI::GetParam<double>("svm/epsilon") = .1;
- CLI::GetParam<double>("svm/sigma") = 1;
- // Protect the test from taking forever
- CLI::GetParam<size_t>("svm/n_iter") = 10000;
-
- matrix <<
- 7.19906628001437787e-01 << 1.83250823399634477e+00 << 0 << arma::endr <<
- 1.37899419263889733e+01 << 1.78198235122579263e+00 << 1 << arma::endr <<
- 6.68859485848275703e-01 << 2.14083320956715983e+00 << 0 << arma::endr <<
- 1.84729928795588165e+01 << 2.25024702760868101e+00 << 1 << arma::endr <<
- 9.22802773268335819e-01 << 1.61469358350834513e+00 << 0 << arma::endr <<
- 2.06209849662245204e-01 << 6.34699695340683490e-01 << 1 << arma::endr <<
- 4.01062068250524817e-01 << 1.65802752932441777e+00 << 0 << arma::endr <<
- 5.02985607135568635e+00 << 1.39976642741810831e+00 << 1 << arma::endr <<
- 3.66471199955079319e-01 << 1.62780588172739638e+00 << 0 << arma::endr <<
- 1.56912570240400999e+01 << 2.16941541650770953e+00 << 1 << arma::endr <<
- 9.98909584711729304e-01 << 2.00337906391517206e+00 << 0 << arma::endr <<
- 1.31430438780891912e+01 << 1.34410346059319719e+00 << 1 << arma::endr <<
- 3.41572957272442523e-01 << 1.16758463655951639e+00 << 0 << arma::endr <<
- 9.53941410851637528e-01 << 6.30271704462483373e-01 << 1 << arma::endr <<
- 7.07135529120981432e-01 << 2.17763537339756041e+00 << 0 << arma::endr <<
- 9.68899714280338742e+00 << 1.26922579378319256e+00 << 1 << arma::endr <<
- 9.82393905512240706e-01 << 2.36790583090293483e+00 << 0 << arma::endr <<
- 1.31583349281727973e+01 << 1.45115094722767868e+00 << 1 << arma::endr <<
- 3.80991188521027202e-01 << 9.05379134419085019e-01 << 0 << arma::endr <<
- 1.86057436180327755e+01 << 2.26941891469499968e+00 << 1 << arma::endr;
- matrix = trans(matrix);
-
- std::cout << matrix << std::endl;
-}
-
-/**
- * Compares predicted values with known values to see if the prediction/training works.
- *
- * @param: learner_typeid Magic number for selecting between classification and
- * regression.
- * @param: data The dataset with the data to predict with.
- * @param: svm The SVM class instance that has been trained for this data, et al.
- */
-template<typename T>
-void verify(size_t learner_typeid, arma::mat& data, SVM<T>& svm) {
- for(size_t i = 0; i < data.n_cols; i++) {
- arma::vec testvec = data.col(i);
-
- double predictedvalue = svm.Predict(learner_typeid, testvec);
- BOOST_REQUIRE_CLOSE(predictedvalue,
- data(data.n_rows-1,i),1e-6);
- }
-}
-
-BOOST_AUTO_TEST_SUITE(SvmTest);
-
- /**
- * Trains a classifier with a linear kernel and checks predictions against
- * classes.
- */
- //BOOST_AUTO_TEST_CASE(svm_classification_linear_kernel_test) {
- // setup();
- //
- // arma::mat trainingdata;
- // trainingdata = matrix;
- // SVM<SVMLinearKernel> svm;
- // svm.InitTrain(0,trainingdata); // 0 for classification
- // verify(0,trainingdata,svm);
- //}
- //
- /**
- * Trains a classifier with a gaussian kernel and checks predictions against
- * classes.
- */
- BOOST_AUTO_TEST_CASE(svm_classification_gaussian_kernel_test) {
- setup();
-
- arma::mat trainingdata;
- trainingdata = matrix;
- SVM<SVMRBFKernel> svm;
- svm.InitTrain(0,trainingdata); // 0 for classification
- verify(0,trainingdata,svm);
- }
-
- /**
- * Trains a classifier with a linear kernel and checks predictions against
- * classes, using regression. TODO: BROKEN
- */
- //BOOST_AUTO_TEST_CASE(svm_regression_linear_kernel_test) {
- // setup();
- //
- // arma::mat trainingdata;
- // trainingdata = matrix;
- // SVM<SVMLinearKernel> svm;
- // svm.InitTrain(1,trainingdata); // 0 for classification
- // //verify(1,trainingdata,svm);
- //}
-
- /**
- * Trains a classifier with a gaussian kernel and checks predictions against
- * classes, using regression. TODO: BROKEN
- */
- //BOOST_AUTO_TEST_CASE(svm_regression_gaussian_kernel_test) {
- // setup();
- //
- // arma::mat trainingdata;
- // trainingdata = matrix;
- // SVM<SVMRBFKernel> svm;
- // svm.InitTrain(1,trainingdata); // 0 for classification
- // //verify(1,trainingdata,svm);
- //}
-
-BOOST_AUTO_TEST_SUITE_END();
Copied: mlpack/trunk/src/mlpack/tests/svm_test.cpp (from rev 10204, mlpack/trunk/src/mlpack/tests/svm_test.cc)
===================================================================
--- mlpack/trunk/src/mlpack/tests/svm_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/svm_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -0,0 +1,140 @@
+/**
+ * @file svm_test.cpp
+ *
+ * Test for SVM.
+ */
+#include <mlpack/core.h>
+#include <mlpack/methods/svm/svm.h>
+
+#include <boost/test/unit_test.hpp>
+
+using std::string;
+using namespace mlpack;
+using namespace mlpack::svm;
+
+BOOST_AUTO_TEST_SUITE(SVMTest);
+
+// Create test data
+arma::mat matrix(20, 3);
+bool first = true;
+
+/**
+ * Creates the data to train and test with and prints it to stdout. Should only
+ * have any effect once.
+ */
+void setup()
+{
+ if(!first)
+ return;
+ first = false;
+
+ CLI::GetParam<bool>("svm/shrink") = true;
+ CLI::GetParam<double>("svm/epsilon") = .1;
+ CLI::GetParam<double>("svm/sigma") = 1;
+ // Protect the test from taking forever
+ CLI::GetParam<size_t>("svm/n_iter") = 10000;
+
+ matrix <<
+ 7.19906628001437787e-01 << 1.83250823399634477e+00 << 0 << arma::endr <<
+ 1.37899419263889733e+01 << 1.78198235122579263e+00 << 1 << arma::endr <<
+ 6.68859485848275703e-01 << 2.14083320956715983e+00 << 0 << arma::endr <<
+ 1.84729928795588165e+01 << 2.25024702760868101e+00 << 1 << arma::endr <<
+ 9.22802773268335819e-01 << 1.61469358350834513e+00 << 0 << arma::endr <<
+ 2.06209849662245204e-01 << 6.34699695340683490e-01 << 1 << arma::endr <<
+ 4.01062068250524817e-01 << 1.65802752932441777e+00 << 0 << arma::endr <<
+ 5.02985607135568635e+00 << 1.39976642741810831e+00 << 1 << arma::endr <<
+ 3.66471199955079319e-01 << 1.62780588172739638e+00 << 0 << arma::endr <<
+ 1.56912570240400999e+01 << 2.16941541650770953e+00 << 1 << arma::endr <<
+ 9.98909584711729304e-01 << 2.00337906391517206e+00 << 0 << arma::endr <<
+ 1.31430438780891912e+01 << 1.34410346059319719e+00 << 1 << arma::endr <<
+ 3.41572957272442523e-01 << 1.16758463655951639e+00 << 0 << arma::endr <<
+ 9.53941410851637528e-01 << 6.30271704462483373e-01 << 1 << arma::endr <<
+ 7.07135529120981432e-01 << 2.17763537339756041e+00 << 0 << arma::endr <<
+ 9.68899714280338742e+00 << 1.26922579378319256e+00 << 1 << arma::endr <<
+ 9.82393905512240706e-01 << 2.36790583090293483e+00 << 0 << arma::endr <<
+ 1.31583349281727973e+01 << 1.45115094722767868e+00 << 1 << arma::endr <<
+ 3.80991188521027202e-01 << 9.05379134419085019e-01 << 0 << arma::endr <<
+ 1.86057436180327755e+01 << 2.26941891469499968e+00 << 1 << arma::endr;
+ matrix = trans(matrix);
+
+ std::cout << matrix << std::endl;
+}
+
+/**
+ * Compares predicted values with known values to see if the prediction/training
+ * works.
+ *
+ * @param learner_typeid Magic number for selecting between classification and
+ * regression.
+ * @param data The dataset with the data to predict with.
+ * @param svm The SVM class instance that has been trained for this data, et al.
+ */
+template<typename T>
+void verify(size_t learner_typeid, arma::mat& data, SVM<T>& svm)
+{
+ for(size_t i = 0; i < data.n_cols; i++)
+ {
+ arma::vec testvec = data.col(i);
+
+ double predictedvalue = svm.Predict(learner_typeid, testvec);
+ BOOST_REQUIRE_CLOSE(predictedvalue, data(data.n_rows - 1, i), 1e-6);
+ }
+}
+
+/**
+ * Trains a classifier with a linear kernel and checks predictions against
+ * classes.
+ */
+//BOOST_AUTO_TEST_CASE(svm_classification_linear_kernel_test) {
+// setup();
+//
+// arma::mat trainingdata;
+// trainingdata = matrix;
+// SVM<SVMLinearKernel> svm;
+// svm.InitTrain(0,trainingdata); // 0 for classification
+// verify(0,trainingdata,svm);
+//}
+
+/**
+ * Trains a classifier with a gaussian kernel and checks predictions against
+ * classes.
+ */
+BOOST_AUTO_TEST_CASE(svm_classification_gaussian_kernel_test) {
+ setup();
+
+ arma::mat trainingdata;
+ trainingdata = matrix;
+ SVM<SVMRBFKernel> svm;
+ svm.InitTrain(0,trainingdata); // 0 for classification
+ verify(0,trainingdata,svm);
+}
+
+/**
+ * Trains a classifier with a linear kernel and checks predictions against
+ * classes, using regression. TODO: BROKEN
+ */
+//BOOST_AUTO_TEST_CASE(svm_regression_linear_kernel_test) {
+// setup();
+//
+// arma::mat trainingdata;
+// trainingdata = matrix;
+// SVM<SVMLinearKernel> svm;
+// svm.InitTrain(1,trainingdata); // 0 for classification
+// //verify(1,trainingdata,svm);
+//}
+
+/**
+ * Trains a classifier with a gaussian kernel and checks predictions against
+ * classes, using regression. TODO: BROKEN
+ */
+//BOOST_AUTO_TEST_CASE(svm_regression_gaussian_kernel_test) {
+// setup();
+//
+// arma::mat trainingdata;
+// trainingdata = matrix;
+// SVM<SVMRBFKernel> svm;
+// svm.InitTrain(1,trainingdata); // 0 for classification
+// //verify(1,trainingdata,svm);
+//}
+
+BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/tree_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/tree_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/tree_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,9 +1,8 @@
/**
- * @file uselapack_test.cc
+ * @file tree_test.cpp
*
- * Tests for LAPACK integration.
+ * Tests for tree-building methods.
*/
-
#include <mlpack/core/tree/bounds.hpp>
#include <mlpack/core/tree/binary_space_tree.hpp>
#include <mlpack/core/kernels/lmetric.hpp>
@@ -18,1021 +17,1059 @@
using namespace mlpack::bound;
BOOST_AUTO_TEST_SUITE(TreeTest);
- /***
- * Ensure that a bound, by default, is empty and has no dimensionality.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundEmptyConstructor) {
- HRectBound<2> b;
- BOOST_REQUIRE_EQUAL(b.dim(), 0);
- }
+/**
+ * Ensure that a bound, by default, is empty and has no dimensionality.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundEmptyConstructor)
+{
+ HRectBound<2> b;
- /***
- * Ensure that when we specify the dimensionality in the constructor, it is
- * correct, and the bounds are all the empty set.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundDimConstructor) {
- HRectBound<2> b(2); // We'll do this with 2 and 5 dimensions.
+ BOOST_REQUIRE_EQUAL(b.dim(), 0);
+}
- BOOST_REQUIRE_EQUAL(b.dim(), 2);
- BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
- BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
+/**
+ * Ensure that when we specify the dimensionality in the constructor, it is
+ * correct, and the bounds are all the empty set.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundDimConstructor)
+{
+ HRectBound<2> b(2); // We'll do this with 2 and 5 dimensions.
- b = HRectBound<2>(5);
+ BOOST_REQUIRE_EQUAL(b.dim(), 2);
+ BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
- BOOST_REQUIRE_EQUAL(b.dim(), 5);
- BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
- BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
- BOOST_REQUIRE_SMALL(b[2].width(), 1e-5);
- BOOST_REQUIRE_SMALL(b[3].width(), 1e-5);
- BOOST_REQUIRE_SMALL(b[4].width(), 1e-5);
- }
+ b = HRectBound<2>(5);
- /***
- * Test the copy constructor.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundCopyConstructor) {
- HRectBound<2> b(2);
- b[0] = Range(0.0, 2.0);
- b[1] = Range(2.0, 3.0);
+ BOOST_REQUIRE_EQUAL(b.dim(), 5);
+ BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(b[2].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(b[3].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(b[4].width(), 1e-5);
+}
- HRectBound<2> c(b);
+/**
+ * Test the copy constructor.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundCopyConstructor)
+{
+ HRectBound<2> b(2);
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(2.0, 3.0);
- BOOST_REQUIRE_EQUAL(c.dim(), 2);
- BOOST_REQUIRE_SMALL(c[0].lo, 1e-5);
- BOOST_REQUIRE_CLOSE(c[0].hi, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c[1].lo, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c[1].hi, 3.0, 1e-5);
- }
+ HRectBound<2> c(b);
- /***
- * Test the assignment operator.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundAssignmentOperator) {
- HRectBound<2> b(2);
- b[0] = Range(0.0, 2.0);
- b[1] = Range(2.0, 3.0);
+ BOOST_REQUIRE_EQUAL(c.dim(), 2);
+ BOOST_REQUIRE_SMALL(c[0].lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[0].hi, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[1].lo, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[1].hi, 3.0, 1e-5);
+}
- HRectBound<2> c(4);
+/**
+ * Test the assignment operator.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundAssignmentOperator)
+{
+ HRectBound<2> b(2);
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(2.0, 3.0);
- c = b;
+ HRectBound<2> c(4);
- BOOST_REQUIRE_EQUAL(c.dim(), 2);
- BOOST_REQUIRE_SMALL(c[0].lo, 1e-5);
- BOOST_REQUIRE_CLOSE(c[0].hi, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c[1].lo, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c[1].hi, 3.0, 1e-5);
- }
+ c = b;
- /***
- * Test that clearing the dimensions resets the bound to empty.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundClear) {
- HRectBound<2> b(2); // We'll do this with two dimensions only.
+ BOOST_REQUIRE_EQUAL(c.dim(), 2);
+ BOOST_REQUIRE_SMALL(c[0].lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[0].hi, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[1].lo, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[1].hi, 3.0, 1e-5);
+}
- b[0] = Range(0.0, 2.0);
- b[1] = Range(2.0, 4.0);
+/**
+ * Test that clearing the dimensions resets the bound to empty.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundClear)
+{
+ HRectBound<2> b(2); // We'll do this with two dimensions only.
- // Now we just need to make sure that we clear the range.
- b.Clear();
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(2.0, 4.0);
- BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
- BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
- }
+ // Now we just need to make sure that we clear the range.
+ b.Clear();
- /***
- * Ensure that we get the correct centroid for our bound.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundCentroid) {
- // Create a simple 3-dimensional bound.
- HRectBound<2> b(3);
+ BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
+}
- b[0] = Range(0.0, 5.0);
- b[1] = Range(-2.0, -1.0);
- b[2] = Range(-10.0, 50.0);
+/**
+ * Ensure that we get the correct centroid for our bound.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundCentroid)
+{
+ // Create a simple 3-dimensional bound.
+ HRectBound<2> b(3);
- arma::vec centroid;
+ b[0] = Range(0.0, 5.0);
+ b[1] = Range(-2.0, -1.0);
+ b[2] = Range(-10.0, 50.0);
- b.Centroid(centroid);
+ arma::vec centroid;
- BOOST_REQUIRE_EQUAL(centroid.n_elem, 3);
- BOOST_REQUIRE_CLOSE(centroid[0], 2.5, 1e-5);
- BOOST_REQUIRE_CLOSE(centroid[1], -1.5, 1e-5);
- BOOST_REQUIRE_CLOSE(centroid[2], 20.0, 1e-5);
- }
+ b.Centroid(centroid);
- /***
- * Ensure that we calculate the correct minimum distance between a point and a
- * bound.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundMinDistancePoint) {
- // We'll do the calculation in five dimensions, and we'll use three cases for
- // the point: point is outside the bound; point is on the edge of the bound;
- // point is inside the bound. In the latter two cases, the distance should be
- // zero.
- HRectBound<2> b(5);
+ BOOST_REQUIRE_EQUAL(centroid.n_elem, 3);
+ BOOST_REQUIRE_CLOSE(centroid[0], 2.5, 1e-5);
+ BOOST_REQUIRE_CLOSE(centroid[1], -1.5, 1e-5);
+ BOOST_REQUIRE_CLOSE(centroid[2], 20.0, 1e-5);
+}
- b[0] = Range(0.0, 2.0);
- b[1] = Range(1.0, 5.0);
- b[2] = Range(-2.0, 2.0);
- b[3] = Range(-5.0, -2.0);
- b[4] = Range(1.0, 2.0);
+/**
+ * Ensure that we calculate the correct minimum distance between a point and a
+ * bound.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundMinDistancePoint)
+{
+ // We'll do the calculation in five dimensions, and we'll use three cases for
+ // the point: point is outside the bound; point is on the edge of the bound;
+ // point is inside the bound. In the latter two cases, the distance should be
+ // zero.
+ HRectBound<2> b(5);
- arma::vec point = "-2.0 0.0 10.0 3.0 3.0";
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(1.0, 5.0);
+ b[2] = Range(-2.0, 2.0);
+ b[3] = Range(-5.0, -2.0);
+ b[4] = Range(1.0, 2.0);
- // This will be the Euclidean squared distance.
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 95.0, 1e-5);
+ arma::vec point = "-2.0 0.0 10.0 3.0 3.0";
- point = "2.0 5.0 2.0 -5.0 1.0";
+ // This will be the Euclidean squared distance.
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 95.0, 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
+ point = "2.0 5.0 2.0 -5.0 1.0";
- point = "1.0 2.0 0.0 -2.0 1.5";
+ BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
- }
+ point = "1.0 2.0 0.0 -2.0 1.5";
- /***
- * Ensure that we calculate the correct minimum distance between a bound and
- * another bound.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundMinDistanceBound) {
- // We'll do the calculation in five dimensions, and we can use six cases.
- // The other bound is completely outside the bound; the other bound is on the
- // edge of the bound; the other bound partially overlaps the bound; the other
- // bound fully overlaps the bound; the other bound is entirely inside the
- // bound; the other bound entirely envelops the bound.
- HRectBound<2> b(5);
+ BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
+}
- b[0] = Range(0.0, 2.0);
- b[1] = Range(1.0, 5.0);
- b[2] = Range(-2.0, 2.0);
- b[3] = Range(-5.0, -2.0);
- b[4] = Range(1.0, 2.0);
+/**
+ * Ensure that we calculate the correct minimum distance between a bound and
+ * another bound.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundMinDistanceBound)
+{
+ // We'll do the calculation in five dimensions, and we can use six cases.
+ // The other bound is completely outside the bound; the other bound is on the
+ // edge of the bound; the other bound partially overlaps the bound; the other
+ // bound fully overlaps the bound; the other bound is entirely inside the
+ // bound; the other bound entirely envelops the bound.
+ HRectBound<2> b(5);
- HRectBound<2> c(5);
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(1.0, 5.0);
+ b[2] = Range(-2.0, 2.0);
+ b[3] = Range(-5.0, -2.0);
+ b[4] = Range(1.0, 2.0);
- // The other bound is completely outside the bound.
- c[0] = Range(-5.0, -2.0);
- c[1] = Range(6.0, 7.0);
- c[2] = Range(-2.0, 2.0);
- c[3] = Range(2.0, 5.0);
- c[4] = Range(3.0, 4.0);
+ HRectBound<2> c(5);
- BOOST_REQUIRE_CLOSE(b.MinDistance(c), 22.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c.MinDistance(b), 22.0, 1e-5);
+ // The other bound is completely outside the bound.
+ c[0] = Range(-5.0, -2.0);
+ c[1] = Range(6.0, 7.0);
+ c[2] = Range(-2.0, 2.0);
+ c[3] = Range(2.0, 5.0);
+ c[4] = Range(3.0, 4.0);
- // The other bound is on the edge of the bound.
- c[0] = Range(-2.0, 0.0);
- c[1] = Range(0.0, 1.0);
- c[2] = Range(-3.0, -2.0);
- c[3] = Range(-10.0, -5.0);
- c[4] = Range(2.0, 3.0);
+ BOOST_REQUIRE_CLOSE(b.MinDistance(c), 22.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c.MinDistance(b), 22.0, 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(c), 1e-5);
- BOOST_REQUIRE_SMALL(c.MinDistance(b), 1e-5);
+ // The other bound is on the edge of the bound.
+ c[0] = Range(-2.0, 0.0);
+ c[1] = Range(0.0, 1.0);
+ c[2] = Range(-3.0, -2.0);
+ c[3] = Range(-10.0, -5.0);
+ c[4] = Range(2.0, 3.0);
- // The other bound partially overlaps the bound.
- c[0] = Range(-2.0, 1.0);
- c[1] = Range(0.0, 2.0);
- c[2] = Range(-2.0, 2.0);
- c[3] = Range(-8.0, -4.0);
- c[4] = Range(0.0, 4.0);
+ BOOST_REQUIRE_SMALL(b.MinDistance(c), 1e-5);
+ BOOST_REQUIRE_SMALL(c.MinDistance(b), 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(c), 1e-5);
- BOOST_REQUIRE_SMALL(c.MinDistance(b), 1e-5);
+ // The other bound partially overlaps the bound.
+ c[0] = Range(-2.0, 1.0);
+ c[1] = Range(0.0, 2.0);
+ c[2] = Range(-2.0, 2.0);
+ c[3] = Range(-8.0, -4.0);
+ c[4] = Range(0.0, 4.0);
- // The other bound fully overlaps the bound.
- BOOST_REQUIRE_SMALL(b.MinDistance(b), 1e-5);
- BOOST_REQUIRE_SMALL(c.MinDistance(c), 1e-5);
+ BOOST_REQUIRE_SMALL(b.MinDistance(c), 1e-5);
+ BOOST_REQUIRE_SMALL(c.MinDistance(b), 1e-5);
- // The other bound is entirely inside the bound / the other bound entirely
- // envelops the bound.
- c[0] = Range(-1.0, 3.0);
- c[1] = Range(0.0, 6.0);
- c[2] = Range(-3.0, 3.0);
- c[3] = Range(-7.0, 0.0);
- c[4] = Range(0.0, 5.0);
+ // The other bound fully overlaps the bound.
+ BOOST_REQUIRE_SMALL(b.MinDistance(b), 1e-5);
+ BOOST_REQUIRE_SMALL(c.MinDistance(c), 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(c), 1e-5);
- BOOST_REQUIRE_SMALL(c.MinDistance(b), 1e-5);
- }
+ // The other bound is entirely inside the bound / the other bound entirely
+ // envelops the bound.
+ c[0] = Range(-1.0, 3.0);
+ c[1] = Range(0.0, 6.0);
+ c[2] = Range(-3.0, 3.0);
+ c[3] = Range(-7.0, 0.0);
+ c[4] = Range(0.0, 5.0);
- /***
- * Ensure that we calculate the correct maximum distance between a bound and a
- * point. This uses the same test cases as the MinDistance test.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundMaxDistancePoint) {
- // We'll do the calculation in five dimensions, and we'll use three cases for
- // the point: point is outside the bound; point is on the edge of the bound;
- // point is inside the bound. In the latter two cases, the distance should be
- // zero.
- HRectBound<2> b(5);
+ BOOST_REQUIRE_SMALL(b.MinDistance(c), 1e-5);
+ BOOST_REQUIRE_SMALL(c.MinDistance(b), 1e-5);
+}
- b[0] = Range(0.0, 2.0);
- b[1] = Range(1.0, 5.0);
- b[2] = Range(-2.0, 2.0);
- b[3] = Range(-5.0, -2.0);
- b[4] = Range(1.0, 2.0);
+/**
+ * Ensure that we calculate the correct maximum distance between a bound and a
+ * point. This uses the same test cases as the MinDistance test.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundMaxDistancePoint)
+{
+ // We'll do the calculation in five dimensions, and we'll use three cases for
+ // the point: point is outside the bound; point is on the edge of the bound;
+ // point is inside the bound. In the latter two cases, the distance should be
+ // zero.
+ HRectBound<2> b(5);
- arma::vec point = "-2.0 0.0 10.0 3.0 3.0";
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(1.0, 5.0);
+ b[2] = Range(-2.0, 2.0);
+ b[3] = Range(-5.0, -2.0);
+ b[4] = Range(1.0, 2.0);
- // This will be the Euclidean squared distance.
- BOOST_REQUIRE_CLOSE(b.MaxDistance(point), 253.0, 1e-5);
+ arma::vec point = "-2.0 0.0 10.0 3.0 3.0";
- point = "2.0 5.0 2.0 -5.0 1.0";
+ // This will be the Euclidean squared distance.
+ BOOST_REQUIRE_CLOSE(b.MaxDistance(point), 253.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MaxDistance(point), 46.0, 1e-5);
+ point = "2.0 5.0 2.0 -5.0 1.0";
- point = "1.0 2.0 0.0 -2.0 1.5";
+ BOOST_REQUIRE_CLOSE(b.MaxDistance(point), 46.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MaxDistance(point), 23.25, 1e-5);
- }
+ point = "1.0 2.0 0.0 -2.0 1.5";
- /***
- * Ensure that we calculate the correct maximum distance between a bound and
- * another bound. This uses the same test cases as the MinDistance test.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundMaxDistanceBound) {
- // We'll do the calculation in five dimensions, and we can use six cases.
- // The other bound is completely outside the bound; the other bound is on the
- // edge of the bound; the other bound partially overlaps the bound; the other
- // bound fully overlaps the bound; the other bound is entirely inside the
- // bound; the other bound entirely envelops the bound.
- HRectBound<2> b(5);
+ BOOST_REQUIRE_CLOSE(b.MaxDistance(point), 23.25, 1e-5);
+}
- b[0] = Range(0.0, 2.0);
- b[1] = Range(1.0, 5.0);
- b[2] = Range(-2.0, 2.0);
- b[3] = Range(-5.0, -2.0);
- b[4] = Range(1.0, 2.0);
+/**
+ * Ensure that we calculate the correct maximum distance between a bound and
+ * another bound. This uses the same test cases as the MinDistance test.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundMaxDistanceBound)
+{
+ // We'll do the calculation in five dimensions, and we can use six cases.
+ // The other bound is completely outside the bound; the other bound is on the
+ // edge of the bound; the other bound partially overlaps the bound; the other
+ // bound fully overlaps the bound; the other bound is entirely inside the
+ // bound; the other bound entirely envelops the bound.
+ HRectBound<2> b(5);
- HRectBound<2> c(5);
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(1.0, 5.0);
+ b[2] = Range(-2.0, 2.0);
+ b[3] = Range(-5.0, -2.0);
+ b[4] = Range(1.0, 2.0);
- // The other bound is completely outside the bound.
- c[0] = Range(-5.0, -2.0);
- c[1] = Range(6.0, 7.0);
- c[2] = Range(-2.0, 2.0);
- c[3] = Range(2.0, 5.0);
- c[4] = Range(3.0, 4.0);
+ HRectBound<2> c(5);
- BOOST_REQUIRE_CLOSE(b.MaxDistance(c), 210.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c.MaxDistance(b), 210.0, 1e-5);
+ // The other bound is completely outside the bound.
+ c[0] = Range(-5.0, -2.0);
+ c[1] = Range(6.0, 7.0);
+ c[2] = Range(-2.0, 2.0);
+ c[3] = Range(2.0, 5.0);
+ c[4] = Range(3.0, 4.0);
- // The other bound is on the edge of the bound.
- c[0] = Range(-2.0, 0.0);
- c[1] = Range(0.0, 1.0);
- c[2] = Range(-3.0, -2.0);
- c[3] = Range(-10.0, -5.0);
- c[4] = Range(2.0, 3.0);
+ BOOST_REQUIRE_CLOSE(b.MaxDistance(c), 210.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c.MaxDistance(b), 210.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MaxDistance(c), 134.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c.MaxDistance(b), 134.0, 1e-5);
+ // The other bound is on the edge of the bound.
+ c[0] = Range(-2.0, 0.0);
+ c[1] = Range(0.0, 1.0);
+ c[2] = Range(-3.0, -2.0);
+ c[3] = Range(-10.0, -5.0);
+ c[4] = Range(2.0, 3.0);
- // The other bound partially overlaps the bound.
- c[0] = Range(-2.0, 1.0);
- c[1] = Range(0.0, 2.0);
- c[2] = Range(-2.0, 2.0);
- c[3] = Range(-8.0, -4.0);
- c[4] = Range(0.0, 4.0);
+ BOOST_REQUIRE_CLOSE(b.MaxDistance(c), 134.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c.MaxDistance(b), 134.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MaxDistance(c), 102.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c.MaxDistance(b), 102.0, 1e-5);
+ // The other bound partially overlaps the bound.
+ c[0] = Range(-2.0, 1.0);
+ c[1] = Range(0.0, 2.0);
+ c[2] = Range(-2.0, 2.0);
+ c[3] = Range(-8.0, -4.0);
+ c[4] = Range(0.0, 4.0);
- // The other bound fully overlaps the bound.
- BOOST_REQUIRE_CLOSE(b.MaxDistance(b), 46.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c.MaxDistance(c), 61.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b.MaxDistance(c), 102.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c.MaxDistance(b), 102.0, 1e-5);
- // The other bound is entirely inside the bound / the other bound entirely
- // envelops the bound.
- c[0] = Range(-1.0, 3.0);
- c[1] = Range(0.0, 6.0);
- c[2] = Range(-3.0, 3.0);
- c[3] = Range(-7.0, 0.0);
- c[4] = Range(0.0, 5.0);
+ // The other bound fully overlaps the bound.
+ BOOST_REQUIRE_CLOSE(b.MaxDistance(b), 46.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c.MaxDistance(c), 61.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MaxDistance(c), 100.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c.MaxDistance(b), 100.0, 1e-5);
+ // The other bound is entirely inside the bound / the other bound entirely
+ // envelops the bound.
+ c[0] = Range(-1.0, 3.0);
+ c[1] = Range(0.0, 6.0);
+ c[2] = Range(-3.0, 3.0);
+ c[3] = Range(-7.0, 0.0);
+ c[4] = Range(0.0, 5.0);
- // One last additional case. If the bound encloses only one point, the
- // maximum distance between it and itself is 0.
- HRectBound<2> d(2);
+ BOOST_REQUIRE_CLOSE(b.MaxDistance(c), 100.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c.MaxDistance(b), 100.0, 1e-5);
- d[0] = Range(2.0, 2.0);
- d[1] = Range(3.0, 3.0);
+ // One last additional case. If the bound encloses only one point, the
+ // maximum distance between it and itself is 0.
+ HRectBound<2> d(2);
- BOOST_REQUIRE_SMALL(d.MaxDistance(d), 1e-5);
- }
+ d[0] = Range(2.0, 2.0);
+ d[1] = Range(3.0, 3.0);
- /***
- * Ensure that the ranges returned by RangeDistance() are equal to the minimum
- * and maximum distance. We will perform this test by creating random bounds
- * and comparing the behavior to MinDistance() and MaxDistance() -- so this test
- * is assuming that those passed and operate correctly.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundRangeDistanceBound) {
- srand(time(NULL));
+ BOOST_REQUIRE_SMALL(d.MaxDistance(d), 1e-5);
+}
- for (int i = 0; i < 50; i++) {
- size_t dim = rand() % 20;
+/**
+ * Ensure that the ranges returned by RangeDistance() are equal to the minimum
+ * and maximum distance. We will perform this test by creating random bounds
+ * and comparing the behavior to MinDistance() and MaxDistance() -- so this test
+ * is assuming that those passed and operate correctly.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundRangeDistanceBound)
+{
+ srand(time(NULL));
- HRectBound<2> a(dim);
- HRectBound<2> b(dim);
+ for (int i = 0; i < 50; i++)
+ {
+ size_t dim = rand() % 20;
- // We will set the low randomly and the width randomly for each dimension of
- // each bound.
- arma::vec lo_a(dim);
- arma::vec width_a(dim);
+ HRectBound<2> a(dim);
+ HRectBound<2> b(dim);
- lo_a.randu();
- width_a.randu();
+ // We will set the low randomly and the width randomly for each dimension of
+ // each bound.
+ arma::vec lo_a(dim);
+ arma::vec width_a(dim);
- arma::vec lo_b(dim);
- arma::vec width_b(dim);
+ lo_a.randu();
+ width_a.randu();
- lo_b.randu();
- width_b.randu();
+ arma::vec lo_b(dim);
+ arma::vec width_b(dim);
- for (size_t j = 0; j < dim; j++) {
- a[j] = Range(lo_a[j], lo_a[j] + width_a[j]);
- b[j] = Range(lo_b[j], lo_b[j] + width_b[j]);
- }
+ lo_b.randu();
+ width_b.randu();
- // Now ensure that MinDistance and MaxDistance report the same.
- Range r = a.RangeDistance(b);
- Range s = b.RangeDistance(a);
+ for (size_t j = 0; j < dim; j++)
+ {
+ a[j] = Range(lo_a[j], lo_a[j] + width_a[j]);
+ b[j] = Range(lo_b[j], lo_b[j] + width_b[j]);
+ }
- BOOST_REQUIRE_CLOSE(r.lo, s.lo, 1e-5);
- BOOST_REQUIRE_CLOSE(r.hi, s.hi, 1e-5);
+ // Now ensure that MinDistance and MaxDistance report the same.
+ Range r = a.RangeDistance(b);
+ Range s = b.RangeDistance(a);
- BOOST_REQUIRE_CLOSE(r.lo, a.MinDistance(b), 1e-5);
- BOOST_REQUIRE_CLOSE(r.hi, a.MaxDistance(b), 1e-5);
+ BOOST_REQUIRE_CLOSE(r.lo, s.lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(r.hi, s.hi, 1e-5);
- BOOST_REQUIRE_CLOSE(s.lo, b.MinDistance(a), 1e-5);
- BOOST_REQUIRE_CLOSE(s.hi, b.MaxDistance(a), 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(r.lo, a.MinDistance(b), 1e-5);
+ BOOST_REQUIRE_CLOSE(r.hi, a.MaxDistance(b), 1e-5);
+
+ BOOST_REQUIRE_CLOSE(s.lo, b.MinDistance(a), 1e-5);
+ BOOST_REQUIRE_CLOSE(s.hi, b.MaxDistance(a), 1e-5);
}
+}
- /***
- * Ensure that the ranges returned by RangeDistance() are equal to the minimum
- * and maximum distance. We will perform this test by creating random bounds
- * and comparing the bheavior to MinDistance() and MaxDistance() -- so this test
- * is assuming that those passed and operate correctly. This is for the
- * bound-to-point case.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundRangeDistancePoint) {
- srand(time(NULL));
+/**
+ * Ensure that the ranges returned by RangeDistance() are equal to the minimum
+ * and maximum distance. We will perform this test by creating random bounds
+ * and comparing the bheavior to MinDistance() and MaxDistance() -- so this test
+ * is assuming that those passed and operate correctly. This is for the
+ * bound-to-point case.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundRangeDistancePoint)
+{
+ srand(time(NULL));
- for (int i = 0; i < 20; i++) {
- size_t dim = rand() % 20;
+ for (int i = 0; i < 20; i++)
+ {
+ size_t dim = rand() % 20;
- HRectBound<2> a(dim);
+ HRectBound<2> a(dim);
- // We will set the low randomly and the width randomly for each dimension of
- // each bound.
- arma::vec lo_a(dim);
- arma::vec width_a(dim);
+ // We will set the low randomly and the width randomly for each dimension of
+ // each bound.
+ arma::vec lo_a(dim);
+ arma::vec width_a(dim);
- lo_a.randu();
- width_a.randu();
+ lo_a.randu();
+ width_a.randu();
- for (size_t j = 0; j < dim; j++)
- a[j] = Range(lo_a[j], lo_a[j] + width_a[j]);
+ for (size_t j = 0; j < dim; j++)
+ a[j] = Range(lo_a[j], lo_a[j] + width_a[j]);
- // Now run the test on a few points.
- for (int j = 0; j < 10; j++) {
- arma::vec point(dim);
+ // Now run the test on a few points.
+ for (int j = 0; j < 10; j++)
+ {
+ arma::vec point(dim);
- point.randu();
+ point.randu();
- Range r = a.RangeDistance(point);
+ Range r = a.RangeDistance(point);
- BOOST_REQUIRE_CLOSE(r.lo, a.MinDistance(point), 1e-5);
- BOOST_REQUIRE_CLOSE(r.hi, a.MaxDistance(point), 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(r.lo, a.MinDistance(point), 1e-5);
+ BOOST_REQUIRE_CLOSE(r.hi, a.MaxDistance(point), 1e-5);
}
}
+}
- /***
- * Test that we can expand the bound to include a new point.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundOrOperatorPoint) {
- // Because this should be independent in each dimension, we can essentially
- // run five test cases at once.
- HRectBound<2> b(5);
+/**
+ * Test that we can expand the bound to include a new point.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundOrOperatorPoint)
+{
+ // Because this should be independent in each dimension, we can essentially
+ // run five test cases at once.
+ HRectBound<2> b(5);
- b[0] = Range(1.0, 3.0);
- b[1] = Range(2.0, 4.0);
- b[2] = Range(-2.0, -1.0);
- b[3] = Range(0.0, 0.0);
- b[4] = Range(); // Empty range.
+ b[0] = Range(1.0, 3.0);
+ b[1] = Range(2.0, 4.0);
+ b[2] = Range(-2.0, -1.0);
+ b[3] = Range(0.0, 0.0);
+ b[4] = Range(); // Empty range.
- arma::vec point = "2.0 4.0 2.0 -1.0 6.0";
+ arma::vec point = "2.0 4.0 2.0 -1.0 6.0";
- b |= point;
+ b |= point;
- BOOST_REQUIRE_CLOSE(b[0].lo, 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[0].hi, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[1].lo, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[1].hi, 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[2].lo, -2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[2].hi, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[3].lo, -1.0, 1e-5);
- BOOST_REQUIRE_SMALL(b[3].hi, 1e-5);
- BOOST_REQUIRE_CLOSE(b[4].lo, 6.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[4].hi, 6.0, 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(b[0].lo, 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[0].hi, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[1].lo, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[1].hi, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[2].lo, -2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[2].hi, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[3].lo, -1.0, 1e-5);
+ BOOST_REQUIRE_SMALL(b[3].hi, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[4].lo, 6.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[4].hi, 6.0, 1e-5);
+}
- /***
- * Test that we can expand the bound to include another bound.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundOrOperatorBound) {
- // Because this should be independent in each dimension, we can run many tests
- // at once.
- HRectBound<2> b(8);
+/**
+ * Test that we can expand the bound to include another bound.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundOrOperatorBound)
+{
+ // Because this should be independent in each dimension, we can run many tests
+ // at once.
+ HRectBound<2> b(8);
- b[0] = Range(1.0, 3.0);
- b[1] = Range(2.0, 4.0);
- b[2] = Range(-2.0, -1.0);
- b[3] = Range(4.0, 5.0);
- b[4] = Range(2.0, 4.0);
- b[5] = Range(0.0, 0.0);
- b[6] = Range();
- b[7] = Range(1.0, 3.0);
+ b[0] = Range(1.0, 3.0);
+ b[1] = Range(2.0, 4.0);
+ b[2] = Range(-2.0, -1.0);
+ b[3] = Range(4.0, 5.0);
+ b[4] = Range(2.0, 4.0);
+ b[5] = Range(0.0, 0.0);
+ b[6] = Range();
+ b[7] = Range(1.0, 3.0);
- HRectBound<2> c(8);
+ HRectBound<2> c(8);
- c[0] = Range(-3.0, -1.0); // Entirely less than the other bound.
- c[1] = Range(0.0, 2.0); // Touching edges.
- c[2] = Range(-3.0, -1.5); // Partially overlapping.
- c[3] = Range(4.0, 5.0); // Identical.
- c[4] = Range(1.0, 5.0); // Entirely enclosing.
- c[5] = Range(2.0, 2.0); // A single point.
- c[6] = Range(1.0, 3.0);
- c[7] = Range(); // Empty set.
+ c[0] = Range(-3.0, -1.0); // Entirely less than the other bound.
+ c[1] = Range(0.0, 2.0); // Touching edges.
+ c[2] = Range(-3.0, -1.5); // Partially overlapping.
+ c[3] = Range(4.0, 5.0); // Identical.
+ c[4] = Range(1.0, 5.0); // Entirely enclosing.
+ c[5] = Range(2.0, 2.0); // A single point.
+ c[6] = Range(1.0, 3.0);
+ c[7] = Range(); // Empty set.
- HRectBound<2> d = c;
+ HRectBound<2> d = c;
- b |= c;
- d |= b;
+ b |= c;
+ d |= b;
- BOOST_REQUIRE_CLOSE(b[0].lo, -3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[0].hi, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[0].lo, -3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[0].hi, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[0].lo, -3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[0].hi, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[0].lo, -3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[0].hi, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[1].lo, 0.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[1].hi, 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[1].lo, 0.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[1].hi, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[1].lo, 0.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[1].hi, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[1].lo, 0.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[1].hi, 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[2].lo, -3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[2].hi, -1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[2].lo, -3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[2].hi, -1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[2].lo, -3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[2].hi, -1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[2].lo, -3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[2].hi, -1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[3].lo, 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[3].hi, 5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[3].lo, 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[3].hi, 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[3].lo, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[3].hi, 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[3].lo, 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[3].hi, 5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[4].lo, 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[4].hi, 5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[4].lo, 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[4].hi, 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[4].lo, 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[4].hi, 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[4].lo, 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[4].hi, 5.0, 1e-5);
- BOOST_REQUIRE_SMALL(b[5].lo, 1e-5);
- BOOST_REQUIRE_CLOSE(b[5].hi, 2.0, 1e-5);
- BOOST_REQUIRE_SMALL(d[5].lo, 1e-5);
- BOOST_REQUIRE_CLOSE(d[5].hi, 2.0, 1e-5);
+ BOOST_REQUIRE_SMALL(b[5].lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[5].hi, 2.0, 1e-5);
+ BOOST_REQUIRE_SMALL(d[5].lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[5].hi, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[6].lo, 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[6].hi, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[6].lo, 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[6].hi, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[6].lo, 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[6].hi, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[6].lo, 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[6].hi, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[7].lo, 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b[7].hi, 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[7].lo, 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d[7].hi, 3.0, 1e-5);
- }
+ BOOST_REQUIRE_CLOSE(b[7].lo, 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b[7].hi, 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[7].lo, 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d[7].hi, 3.0, 1e-5);
+}
- /***
- * Test that the Contains() function correctly figures out whether or not a
- * point is in a bound.
- */
- BOOST_AUTO_TEST_CASE(HRectBoundContains) {
- // We can test a couple different points: completely outside the bound,
- // adjacent in one dimension to the bound, adjacent in all dimensions to the
- // bound, and inside the bound.
- HRectBound<2> b(3);
+/**
+ * Test that the Contains() function correctly figures out whether or not a
+ * point is in a bound.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundContains)
+{
+ // We can test a couple different points: completely outside the bound,
+ // adjacent in one dimension to the bound, adjacent in all dimensions to the
+ // bound, and inside the bound.
+ HRectBound<2> b(3);
- b[0] = Range(0.0, 2.0);
- b[1] = Range(0.0, 2.0);
- b[2] = Range(0.0, 2.0);
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(0.0, 2.0);
+ b[2] = Range(0.0, 2.0);
- // Completely outside the range.
- arma::vec point = "-1.0 4.0 4.0";
- BOOST_REQUIRE(!b.Contains(point));
+ // Completely outside the range.
+ arma::vec point = "-1.0 4.0 4.0";
+ BOOST_REQUIRE(!b.Contains(point));
- // Completely outside, but one dimension is in the range.
- point = "-1.0 4.0 1.0";
- BOOST_REQUIRE(!b.Contains(point));
+ // Completely outside, but one dimension is in the range.
+ point = "-1.0 4.0 1.0";
+ BOOST_REQUIRE(!b.Contains(point));
- // Outside, but one dimension is on the edge.
- point = "-1.0 0.0 3.0";
- BOOST_REQUIRE(!b.Contains(point));
+ // Outside, but one dimension is on the edge.
+ point = "-1.0 0.0 3.0";
+ BOOST_REQUIRE(!b.Contains(point));
- // Two dimensions are on the edge, but one is outside.
- point = "0.0 0.0 3.0";
- BOOST_REQUIRE(!b.Contains(point));
+ // Two dimensions are on the edge, but one is outside.
+ point = "0.0 0.0 3.0";
+ BOOST_REQUIRE(!b.Contains(point));
- // Completely on the edge (should be contained).
- point = "0.0 0.0 0.0";
- BOOST_REQUIRE(b.Contains(point));
+ // Completely on the edge (should be contained).
+ point = "0.0 0.0 0.0";
+ BOOST_REQUIRE(b.Contains(point));
- // Inside the range.
- point = "0.3 1.0 0.4";
- BOOST_REQUIRE(b.Contains(point));
- }
+ // Inside the range.
+ point = "0.3 1.0 0.4";
+ BOOST_REQUIRE(b.Contains(point));
+}
- BOOST_AUTO_TEST_CASE(TestBallBound) {
- DBallBound<> b1;
- DBallBound<> b2;
+BOOST_AUTO_TEST_CASE(TestBallBound)
+{
+ DBallBound<> b1;
+ DBallBound<> b2;
- // Create two balls with a center distance of 1 from each other.
- // Give the first one a radius of 0.3 and the second a radius of 0.4.
+ // Create two balls with a center distance of 1 from each other.
+ // Give the first one a radius of 0.3 and the second a radius of 0.4.
+ b1.center().set_size(3);
+ b1.center()[0] = 1;
+ b1.center()[1] = 2;
+ b1.center()[2] = 3;
+ b1.set_radius(0.3);
- b1.center().set_size(3);
- b1.center()[0] = 1;
- b1.center()[1] = 2;
- b1.center()[2] = 3;
- b1.set_radius(0.3);
+ b2.center().set_size(3);
+ b2.center()[0] = 1;
+ b2.center()[1] = 2;
+ b2.center()[2] = 4;
+ b2.set_radius(0.4);
- b2.center().set_size(3);
- b2.center()[0] = 1;
- b2.center()[1] = 2;
- b2.center()[2] = 4;
- b2.set_radius(0.4);
+ BOOST_REQUIRE_CLOSE(sqrt(b1.MinDistanceSq(b2)), 1-0.3-0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b1.RangeDistanceSq(b2).hi), 1+0.3+0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b1.RangeDistanceSq(b2).lo), 1-0.3-0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(b1.RangeDistance(b2).hi, 1+0.3+0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(b1.RangeDistance(b2).lo, 1-0.3-0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b1.MinToMidSq(b2)), 1-0.3, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b1.MinimaxDistanceSq(b2)), 1-0.3+0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b1.MidDistanceSq(b2)), 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b1.MinDistanceSq(b2)), 1-0.3-0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b1.RangeDistanceSq(b2).hi), 1+0.3+0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b1.RangeDistanceSq(b2).lo), 1-0.3-0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(b1.RangeDistance(b2).hi, 1+0.3+0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(b1.RangeDistance(b2).lo, 1-0.3-0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b1.MinToMidSq(b2)), 1-0.3, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b1.MinimaxDistanceSq(b2)), 1-0.3+0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b1.MidDistanceSq(b2)), 1.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.MinDistanceSq(b1)), 1-0.3-0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.MaxDistanceSq(b1)), 1+0.3+0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.RangeDistanceSq(b1).hi), 1+0.3+0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.RangeDistanceSq(b1).lo), 1-0.3-0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.MinToMidSq(b1)), 1-0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.MinimaxDistanceSq(b1)), 1-0.4+0.3, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.MidDistanceSq(b1)), 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.MinDistanceSq(b1)), 1-0.3-0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.MaxDistanceSq(b1)), 1+0.3+0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.RangeDistanceSq(b1).hi), 1+0.3+0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.RangeDistanceSq(b1).lo), 1-0.3-0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.MinToMidSq(b1)), 1-0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.MinimaxDistanceSq(b1)), 1-0.4+0.3, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.MidDistanceSq(b1)), 1.0, 1e-5);
+ BOOST_REQUIRE(b1.Contains(b1.center()));
+ BOOST_REQUIRE(!b1.Contains(b2.center()));
- BOOST_REQUIRE(b1.Contains(b1.center()));
- BOOST_REQUIRE(!b1.Contains(b2.center()));
+ BOOST_REQUIRE(!b2.Contains(b1.center()));
+ BOOST_REQUIRE(b2.Contains(b2.center()));
+ arma::vec b2point(3); // A point that's within the radius but not the center.
+ b2point[0] = 1.1;
+ b2point[1] = 2.1;
+ b2point[2] = 4.1;
- BOOST_REQUIRE(!b2.Contains(b1.center()));
- BOOST_REQUIRE(b2.Contains(b2.center()));
- arma::vec b2point(3); // a point that's within the radius bot not the center
- b2point[0] = 1.1;
- b2point[1] = 2.1;
- b2point[2] = 4.1;
+ BOOST_REQUIRE(b2.Contains(b2point));
- BOOST_REQUIRE(b2.Contains(b2point));
+ BOOST_REQUIRE_SMALL(sqrt(b1.MinDistanceSq(b1.center())), 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b1.MinDistanceSq(b2.center())), 1 - 0.3, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.MinDistanceSq(b1.center())), 1 - 0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b2.MaxDistanceSq(b1.center())), 1 + 0.4, 1e-5);
+ BOOST_REQUIRE_CLOSE(sqrt(b1.MaxDistanceSq(b2.center())), 1 + 0.3, 1e-5);
+}
- BOOST_REQUIRE_SMALL(sqrt(b1.MinDistanceSq(b1.center())), 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b1.MinDistanceSq(b2.center())), 1 - 0.3, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.MinDistanceSq(b1.center())), 1 - 0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b2.MaxDistanceSq(b1.center())), 1 + 0.4, 1e-5);
- BOOST_REQUIRE_CLOSE(sqrt(b1.MaxDistanceSq(b2.center())), 1 + 0.3, 1e-5);
- }
+/**
+ * Ensure that a bound, by default, is empty and has no dimensionality, and the
+ * box size vector is empty.
+ */
+BOOST_AUTO_TEST_CASE(PeriodicHRectBoundEmptyConstructor)
+{
+ PeriodicHRectBound<2> b;
- /***
- * Ensure that a bound, by default, is empty and has no dimensionality, and the
- * box size vector is empty.
- */
- BOOST_AUTO_TEST_CASE(PeriodicHRectBoundEmptyConstructor) {
- PeriodicHRectBound<2> b;
+ BOOST_REQUIRE_EQUAL(b.dim(), 0);
+ BOOST_REQUIRE_EQUAL(b.box().n_elem, 0);
+}
- BOOST_REQUIRE_EQUAL(b.dim(), 0);
- BOOST_REQUIRE_EQUAL(b.box().n_elem, 0);
- }
+/**
+ * Ensure that when we specify the dimensionality in the constructor, it is
+ * correct, and the bounds are all the empty set.
+ */
+BOOST_AUTO_TEST_CASE(PeriodicHRectBoundBoxConstructor)
+{
+ PeriodicHRectBound<2> b(arma::vec("5 6"));
- /***
- * Ensure that when we specify the dimensionality in the constructor, it is
- * correct, and the bounds are all the empty set.
- */
- BOOST_AUTO_TEST_CASE(PeriodicHRectBoundBoxConstructor) {
- PeriodicHRectBound<2> b(arma::vec("5 6"));
+ BOOST_REQUIRE_EQUAL(b.dim(), 2);
+ BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
+ BOOST_REQUIRE_EQUAL(b.box().n_elem, 2);
+ BOOST_REQUIRE_CLOSE(b.box()[0], 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b.box()[1], 6.0, 1e-5);
- BOOST_REQUIRE_EQUAL(b.dim(), 2);
- BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
- BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
- BOOST_REQUIRE_EQUAL(b.box().n_elem, 2);
- BOOST_REQUIRE_CLOSE(b.box()[0], 5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.box()[1], 6.0, 1e-5);
+ PeriodicHRectBound<2> d(arma::vec("2 3 4 5 6"));
- PeriodicHRectBound<2> d(arma::vec("2 3 4 5 6"));
+ BOOST_REQUIRE_EQUAL(d.dim(), 5);
+ BOOST_REQUIRE_SMALL(d[0].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(d[1].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(d[2].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(d[3].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(d[4].width(), 1e-5);
+ BOOST_REQUIRE_EQUAL(d.box().n_elem, 5);
+ BOOST_REQUIRE_CLOSE(d.box()[0], 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.box()[1], 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.box()[2], 4.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.box()[3], 5.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.box()[4], 6.0, 1e-5);
+}
- BOOST_REQUIRE_EQUAL(d.dim(), 5);
- BOOST_REQUIRE_SMALL(d[0].width(), 1e-5);
- BOOST_REQUIRE_SMALL(d[1].width(), 1e-5);
- BOOST_REQUIRE_SMALL(d[2].width(), 1e-5);
- BOOST_REQUIRE_SMALL(d[3].width(), 1e-5);
- BOOST_REQUIRE_SMALL(d[4].width(), 1e-5);
- BOOST_REQUIRE_EQUAL(d.box().n_elem, 5);
- BOOST_REQUIRE_CLOSE(d.box()[0], 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d.box()[1], 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d.box()[2], 4.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d.box()[3], 5.0, 1e-5);
- BOOST_REQUIRE_CLOSE(d.box()[4], 6.0, 1e-5);
- }
+/**
+ * Test the copy constructor.
+ *
+BOOST_AUTO_TEST_CASE(PeriodicHRectBoundCopyConstructor)
+{
+ PeriodicHRectBound<2> b(arma::vec("3 4"));
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(2.0, 3.0);
- /***
- * Test the copy constructor.
- *
- BOOST_AUTO_TEST_CASE(PeriodicHRectBoundCopyConstructor) {
- PeriodicHRectBound<2> b(arma::vec("3 4"));
- b[0] = Range(0.0, 2.0);
- b[1] = Range(2.0, 3.0);
+ PeriodicHRectBound<2> c(b);
- PeriodicHRectBound<2> c(b);
+ BOOST_REQUIRE_EQUAL(c.dim(), 2);
+ BOOST_REQUIRE_SMALL(c[0].lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[0].hi, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[1].lo, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[1].hi, 3.0, 1e-5);
+ BOOST_REQUIRE_EQUAL(c.box().n_elem, 2);
+ BOOST_REQUIRE_CLOSE(c.box()[0], 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c.box()[1], 4.0, 1e-5);
+}*/
- BOOST_REQUIRE_EQUAL(c.dim(), 2);
- BOOST_REQUIRE_SMALL(c[0].lo, 1e-5);
- BOOST_REQUIRE_CLOSE(c[0].hi, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c[1].lo, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c[1].hi, 3.0, 1e-5);
- BOOST_REQUIRE_EQUAL(c.box().n_elem, 2);
- BOOST_REQUIRE_CLOSE(c.box()[0], 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c.box()[1], 4.0, 1e-5);
- }*/
+/**
+ * Test the assignment operator.
+ *
+BOOST_AUTO_TEST_CASE(PeriodicHRectBoundAssignmentOperator)
+{
+ PeriodicHRectBound<2> b(arma::vec("3 4"));
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(2.0, 3.0);
- /***
- * Test the assignment operator.
- *
- BOOST_AUTO_TEST_CASE(PeriodicHRectBoundAssignmentOperator) {
- PeriodicHRectBound<2> b(arma::vec("3 4"));
- b[0] = Range(0.0, 2.0);
- b[1] = Range(2.0, 3.0);
+ PeriodicHRectBound<2> c(arma::vec("3 4 5 6"));
- PeriodicHRectBound<2> c(arma::vec("3 4 5 6"));
+ c = b;
- c = b;
+ BOOST_REQUIRE_EQUAL(c.dim(), 2);
+ BOOST_REQUIRE_SMALL(c[0].lo, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[0].hi, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[1].lo, 2.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c[1].hi, 3.0, 1e-5);
+ BOOST_REQUIRE_EQUAL(c.box().n_elem, 2);
+ BOOST_REQUIRE_CLOSE(c.box()[0], 3.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(c.box()[1], 4.0, 1e-5);
+}*/
- BOOST_REQUIRE_EQUAL(c.dim(), 2);
- BOOST_REQUIRE_SMALL(c[0].lo, 1e-5);
- BOOST_REQUIRE_CLOSE(c[0].hi, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c[1].lo, 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c[1].hi, 3.0, 1e-5);
- BOOST_REQUIRE_EQUAL(c.box().n_elem, 2);
- BOOST_REQUIRE_CLOSE(c.box()[0], 3.0, 1e-5);
- BOOST_REQUIRE_CLOSE(c.box()[1], 4.0, 1e-5);
- }*/
+/**
+ * Ensure that we can set the box size correctly.
+ *
+BOOST_AUTO_TEST_CASE(PeriodicHRectBoundSetBoxSize)
+{
+ PeriodicHRectBound<2> b(arma::vec("1 2"));
- /***
- * Ensure that we can set the box size correctly.
- *
- BOOST_AUTO_TEST_CASE(PeriodicHRectBoundSetBoxSize) {
- PeriodicHRectBound<2> b(arma::vec("1 2"));
+ b.SetBoxSize(arma::vec("10 12"));
- b.SetBoxSize(arma::vec("10 12"));
+ BOOST_REQUIRE_EQUAL(b.box().n_elem, 2);
+ BOOST_REQUIRE_CLOSE(b.box()[0], 10.0, 1e-5);
+ BOOST_REQUIRE_CLOSE(b.box()[1], 12.0, 1e-5);
+}*/
- BOOST_REQUIRE_EQUAL(b.box().n_elem, 2);
- BOOST_REQUIRE_CLOSE(b.box()[0], 10.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.box()[1], 12.0, 1e-5);
- }*/
+/**
+ * Ensure that we can clear the dimensions correctly. This does not involve the
+ * box size at all, so the test can be identical to the HRectBound test.
+ *
+BOOST_AUTO_TEST_CASE(PeriodicHRectBoundClear)
+{
+ // We'll do this with two dimensions only.
+ PeriodicHRectBound<2> b(arma::vec("5 5"));
- /***
- * Ensure that we can clear the dimensions correctly. This does not involve the
- * box size at all, so the test can be identical to the HRectBound test.
- *
- BOOST_AUTO_TEST_CASE(PeriodicHRectBoundClear) {
- // We'll do this with two dimensions only.
- PeriodicHRectBound<2> b(arma::vec("5 5"));
+ b[0] = Range(0.0, 2.0);
+ b[1] = Range(2.0, 4.0);
- b[0] = Range(0.0, 2.0);
- b[1] = Range(2.0, 4.0);
+ // Now we just need to make sure that we clear the range.
+ b.Clear();
- // Now we just need to make sure that we clear the range.
- b.Clear();
+ BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
+ BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
+}*/
- BOOST_REQUIRE_SMALL(b[0].width(), 1e-5);
- BOOST_REQUIRE_SMALL(b[1].width(), 1e-5);
- }*/
+/**
+ * Ensure that we get the correct centroid for our bound.
+ *
+BOOST_AUTO_TEST_CASE(PeriodicHRectBoundCentroid) {
+ // Create a simple 3-dimensional bound. The centroid is not affected by the
+ // periodic coordinates.
+ PeriodicHRectBound<2> b(arma::vec("100 100 100"));
- /***
- * Ensure that we get the correct centroid for our bound.
- *
- BOOST_AUTO_TEST_CASE(PeriodicHRectBoundCentroid) {
- // Create a simple 3-dimensional bound. The centroid is not affected by the
- // periodic coordinates.
- PeriodicHRectBound<2> b(arma::vec("100 100 100"));
+ b[0] = Range(0.0, 5.0);
+ b[1] = Range(-2.0, -1.0);
+ b[2] = Range(-10.0, 50.0);
- b[0] = Range(0.0, 5.0);
- b[1] = Range(-2.0, -1.0);
- b[2] = Range(-10.0, 50.0);
+ arma::vec centroid;
- arma::vec centroid;
+ b.Centroid(centroid);
- b.Centroid(centroid);
+ BOOST_REQUIRE_EQUAL(centroid.n_elem, 3);
+ BOOST_REQUIRE_CLOSE(centroid[0], 2.5, 1e-5);
+ BOOST_REQUIRE_CLOSE(centroid[1], -1.5, 1e-5);
+ BOOST_REQUIRE_CLOSE(centroid[2], 20.0, 1e-5);
+}*/
- BOOST_REQUIRE_EQUAL(centroid.n_elem, 3);
- BOOST_REQUIRE_CLOSE(centroid[0], 2.5, 1e-5);
- BOOST_REQUIRE_CLOSE(centroid[1], -1.5, 1e-5);
- BOOST_REQUIRE_CLOSE(centroid[2], 20.0, 1e-5);
- }*/
+/**
+ * Correctly calculate the minimum distance between the bound and a point in
+ * periodic coordinates. We have to account for the shifts necessary in
+ * periodic coordinates too, so that makes testing this a little more difficult.
+ *
+BOOST_AUTO_TEST_CASE(PeriodicHRectBoundMinDistancePoint)
+{
+ // First, we'll start with a simple 2-dimensional case where the point is
+ // inside the bound, then on the edge of the bound, then barely outside the
+ // bound. The box size will be large enough that this is basically the
+ // HRectBound case.
+ PeriodicHRectBound<2> b(arma::vec("100 100"));
- /***
- * Correctly calculate the minimum distance between the bound and a point in
- * periodic coordinates. We have to account for the shifts necessary in
- * periodic coordinates too, so that makes testing this a little more difficult.
- *
- BOOST_AUTO_TEST_CASE(PeriodicHRectBoundMinDistancePoint) {
- // First, we'll start with a simple 2-dimensional case where the point is
- // inside the bound, then on the edge of the bound, then barely outside the
- // bound. The box size will be large enough that this is basically the
- // HRectBound case.
- PeriodicHRectBound<2> b(arma::vec("100 100"));
+ b[0] = Range(0.0, 5.0);
+ b[1] = Range(2.0, 4.0);
- b[0] = Range(0.0, 5.0);
- b[1] = Range(2.0, 4.0);
+ // Inside the bound.
+ arma::vec point = "2.5 3.0";
- // Inside the bound.
- arma::vec point = "2.5 3.0";
+ BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
+ // On the edge.
+ point = "5.0 4.0";
- // On the edge.
- point = "5.0 4.0";
+ BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
+ // And just a little outside the bound.
+ point = "6.0 5.0";
- // And just a little outside the bound.
- point = "6.0 5.0";
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 2.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 2.0, 1e-5);
+ // Now we start to invoke the periodicity. This point will "alias" to (-1,
+ // -1).
+ point = "99.0 99.0";
- // Now we start to invoke the periodicity. This point will "alias" to (-1,
- // -1).
- point = "99.0 99.0";
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 10.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 10.0, 1e-5);
+ // We will perform several tests on a one-dimensional bound.
+ b = PeriodicHRectBound<2>(arma::vec("5.0"));
+ point.set_size(1);
- // We will perform several tests on a one-dimensional bound.
- b = PeriodicHRectBound<2>(arma::vec("5.0"));
- point.set_size(1);
+ b[0] = Range("2.0 4.0"); // Entirely inside box.
+ point[0] = 7.5; // Inside first right image of the box.
- b[0] = Range("2.0 4.0"); // Entirely inside box.
- point[0] = 7.5; // Inside first right image of the box.
+ BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
+ b[0] = Range("0.0 5.0"); // Fills box fully.
+ point[1] = 19.3; // Inside the box, which covers everything.
- b[0] = Range("0.0 5.0"); // Fills box fully.
- point[1] = 19.3; // Inside the box, which covers everything.
+ BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
+ b[0] = Range("-10.0 10.0"); // Larger than the box.
+ point[0] = -500.0; // Inside the box, which covers everything.
- b[0] = Range("-10.0 10.0"); // Larger than the box.
- point[0] = -500.0; // Inside the box, which covers everything.
+ BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
- BOOST_REQUIRE_SMALL(b.MinDistance(point), 1e-5);
+ b[0] = Range("-2.0 1.0"); // Crosses over an edge.
+ point[0] = 2.9; // The first right image of the bound starts at 3.0.
- b[0] = Range("-2.0 1.0"); // Crosses over an edge.
- point[0] = 2.9; // The first right image of the bound starts at 3.0.
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 0.01, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 0.01, 1e-5);
+ b[0] = Range("2.0 4.0"); // Inside box.
+ point[0] = 0.0; // Closest to the first left image of the bound.
- b[0] = Range("2.0 4.0"); // Inside box.
- point[0] = 0.0; // Closest to the first left image of the bound.
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 1.0, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 1.0, 1e-5);
+ b[0] = Range("0.0 2.0"); // On edge of box.
+ point[0] = 7.1; // 0.1 away from the first right image of the bound.
- b[0] = Range("0.0 2.0"); // On edge of box.
- point[0] = 7.1; // 0.1 away from the first right image of the bound.
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 0.01, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 0.01, 1e-5);
+ b[0] = Range("-10.0 10.0"); // Box is of infinite size.
+ point[0] = 810.0; // 800 away from the only image of the box.
- b[0] = Range("-10.0 10.0"); // Box is of infinite size.
- point[0] = 810.0; // 800 away from the only image of the box.
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 640000, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 640000, 1e-5);
+ b[0] = Range("2.0 4.0"); // Box size of -5 should function the same as 5.
+ point[0] = -10.8; // Should alias to 4.2.
- b[0] = Range("2.0 4.0"); // Box size of -5 should function the same as 5.
- point[0] = -10.8; // Should alias to 4.2.
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 0.04, 1e-5);
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 0.04, 1e-5);
+ // Switch our bound to a higher dimensionality. This should ensure that the
+ // dimensions are independent like they should be.
+ b = PeriodicHRectBound<2>(arma::vec("5.0 5.0 5.0 5.0 5.0 5.0 0.0 -5.0"));
- // Switch our bound to a higher dimensionality. This should ensure that the
- // dimensions are independent like they should be.
- b = PeriodicHRectBound<2>(arma::vec("5.0 5.0 5.0 5.0 5.0 5.0 0.0 -5.0"));
+ b[0] = Range("2.0 4.0"); // Entirely inside box.
+ b[1] = Range("0.0 5.0"); // Fills box fully.
+ b[2] = Range("-10.0 10.0"); // Larger than the box.
+ b[3] = Range("-2.0 1.0"); // Crosses over an edge.
+ b[4] = Range("2.0 4.0"); // Inside box.
+ b[5] = Range("0.0 2.0"); // On edge of box.
+ b[6] = Range("-10.0 10.0"); // Box is of infinite size.
+ b[7] = Range("2.0 4.0"); // Box size of -5 should function the same as 5.
- b[0] = Range("2.0 4.0"); // Entirely inside box.
- b[1] = Range("0.0 5.0"); // Fills box fully.
- b[2] = Range("-10.0 10.0"); // Larger than the box.
- b[3] = Range("-2.0 1.0"); // Crosses over an edge.
- b[4] = Range("2.0 4.0"); // Inside box.
- b[5] = Range("0.0 2.0"); // On edge of box.
- b[6] = Range("-10.0 10.0"); // Box is of infinite size.
- b[7] = Range("2.0 4.0"); // Box size of -5 should function the same as 5.
+ point.set_size(8);
+ point[0] = 7.5; // Inside first right image of the box.
+ point[1] = 19.3; // Inside the box, which covers everything.
+ point[2] = -500.0; // Inside the box, which covers everything.
+ point[3] = 2.9; // The first right image of the bound starts at 3.0.
+ point[4] = 0.0; // Closest to the first left image of the bound.
+ point[5] = 7.1; // 0.1 away from the first right image of the bound.
+ point[6] = 810.0; // 800 away from the only image of the box.
+ point[7] = -10.8; // Should alias to 4.2.
- point.set_size(8);
- point[0] = 7.5; // Inside first right image of the box.
- point[1] = 19.3; // Inside the box, which covers everything.
- point[2] = -500.0; // Inside the box, which covers everything.
- point[3] = 2.9; // The first right image of the bound starts at 3.0.
- point[4] = 0.0; // Closest to the first left image of the bound.
- point[5] = 7.1; // 0.1 away from the first right image of the bound.
- point[6] = 810.0; // 800 away from the only image of the box.
- point[7] = -10.8; // Should alias to 4.2.
+ BOOST_REQUIRE_CLOSE(b.MinDistance(point), 640001.06, 1e-10);
+}*/
- BOOST_REQUIRE_CLOSE(b.MinDistance(point), 640001.06, 1e-10);
- }*/
+/**
+ * It seems as though Bill has stumbled across a bug where
+ * BinarySpaceTree<>::count() returns something different than
+ * BinarySpaceTree<>::count_. So, let's build a simple tree and make sure they
+ * are the same.
+ */
+BOOST_AUTO_TEST_CASE(tree_count_mismatch)
+{
+ arma::mat dataset = "2.0 5.0 9.0 4.0 8.0 7.0;"
+ "3.0 4.0 6.0 7.0 1.0 2.0 ";
- /***
- * It seems as though Bill has stumbled across a bug where
- * BinarySpaceTree<>::count() returns something different than
- * BinarySpaceTree<>::count_. So, let's build a simple tree and make sure they
- * are the same.
- */
- BOOST_AUTO_TEST_CASE(tree_count_mismatch) {
- arma::mat dataset = "2.0 5.0 9.0 4.0 8.0 7.0;"
- "3.0 4.0 6.0 7.0 1.0 2.0 ";
+ // Leaf size of 1.
+ CLI::GetParam<int>("tree/leaf_size") = 1;
+ BinarySpaceTree<HRectBound<2> > root_node(dataset);
- // Leaf size of 1.
- CLI::GetParam<int>("tree/leaf_size") = 1;
- BinarySpaceTree<HRectBound<2> > root_node(dataset);
+ BOOST_REQUIRE(root_node.count() == 6);
+ BOOST_REQUIRE(root_node.left()->count() == 3);
+ BOOST_REQUIRE(root_node.left()->left()->count() == 2);
+ BOOST_REQUIRE(root_node.left()->left()->left()->count() == 1);
+ BOOST_REQUIRE(root_node.left()->left()->right()->count() == 1);
+ BOOST_REQUIRE(root_node.left()->right()->count() == 1);
+ BOOST_REQUIRE(root_node.right()->count() == 3);
+ BOOST_REQUIRE(root_node.right()->left()->count() == 2);
+ BOOST_REQUIRE(root_node.right()->left()->left()->count() == 1);
+ BOOST_REQUIRE(root_node.right()->left()->right()->count() == 1);
+ BOOST_REQUIRE(root_node.right()->right()->count() == 1);
+}
- BOOST_REQUIRE(root_node.count() == 6);
- BOOST_REQUIRE(root_node.left()->count() == 3);
- BOOST_REQUIRE(root_node.left()->left()->count() == 2);
- BOOST_REQUIRE(root_node.left()->left()->left()->count() == 1);
- BOOST_REQUIRE(root_node.left()->left()->right()->count() == 1);
- BOOST_REQUIRE(root_node.left()->right()->count() == 1);
- BOOST_REQUIRE(root_node.right()->count() == 3);
- BOOST_REQUIRE(root_node.right()->left()->count() == 2);
- BOOST_REQUIRE(root_node.right()->left()->left()->count() == 1);
- BOOST_REQUIRE(root_node.right()->left()->right()->count() == 1);
- BOOST_REQUIRE(root_node.right()->right()->count() == 1);
- }
+// Forward declaration of methods we need for the next test.
+template<typename TreeType>
+bool CheckPointBounds(TreeType* node, const arma::mat& data);
- // Forward declaration of methods we need for the next test.
- template<typename TreeType>
- bool CheckPointBounds(TreeType* node, const arma::mat& data);
+template<typename TreeType>
+void GenerateVectorOfTree(TreeType* node,
+ size_t depth,
+ std::vector<TreeType*>& v);
- template<typename TreeType>
- void GenerateVectorOfTree(TreeType* node,
- size_t depth,
- std::vector<TreeType*>& v);
+template<int t_pow>
+bool DoBoundsIntersect(HRectBound<t_pow>& a,
+ HRectBound<t_pow>& b,
+ size_t ia,
+ size_t ib);
- template<int t_pow>
- bool DoBoundsIntersect(HRectBound<t_pow>& a,
- HRectBound<t_pow>& b,
- size_t ia,
- size_t ib);
+/**
+ * Exhaustive kd-tree test based on #125.
+ *
+ * - Generate a random dataset of a random size.
+ * - Build a tree on that dataset.
+ * - Ensure all the permutation indices map back to the correct points.
+ * - Verify that each point is contained inside all of the bounds of its parent
+ * nodes.
+ * - Verify that each bound at a particular level of the tree does not overlap
+ * with any other bounds at that level.
+ *
+ * Then, we do that whole process a handful of times.
+ */
+BOOST_AUTO_TEST_CASE(kd_tree_test)
+{
+ typedef BinarySpaceTree<HRectBound<2> > TreeType;
- /***
- * Exhaustive kd-tree test based on #125.
- *
- * - Generate a random dataset of a random size.
- * - Build a tree on that dataset.
- * - Ensure all the permutation indices map back to the correct points.
- * - Verify that each point is contained inside all of the bounds of its parent
- * nodes.
- * - Verify that each bound at a particular level of the tree does not overlap
- * with any other bounds at that level.
- *
- * Then, we do that whole process a handful of times.
- */
- BOOST_AUTO_TEST_CASE(kd_tree_test) {
- typedef BinarySpaceTree<HRectBound<2> > TreeType;
+ size_t max_runs = 10; // Ten total tests.
+ size_t point_increments = 1000; // Range is from 2000 points to 11000.
- size_t max_runs = 10; // Ten total tests.
- size_t point_increments = 1000; // Range is from 2000 points to 11000.
+ // Generate the dataset.
+ srand(time(NULL));
- // Generate the dataset.
- srand(time(NULL));
+ // Reset the leaf size as other tests have been naughty.
+ // Also, a leaf size of 20 makes the test take too long.
+ CLI::GetParam<int>("tree/leaf_size") = 20;
- // Reset the leaf size as other tests have been naughty.
- // Also, a leaf size of 20 makes the test take too long.
- CLI::GetParam<int>("tree/leaf_size") = 20;
+ for(size_t run = 0; run < max_runs; run++)
+ {
+ size_t dimensions = run + 2;
+ size_t max_points = (run + 1) * point_increments;
- for(size_t run = 0; run < max_runs; run++) {
- size_t dimensions = run + 2;
- size_t max_points = (run + 1) * point_increments;
+ size_t size = max_points;
+ arma::mat dataset = arma::mat(dimensions, size);
+ arma::mat datacopy; // Used to test mappings.
- size_t size = max_points;
- arma::mat dataset = arma::mat(dimensions, size);
- arma::mat datacopy; // Used to test mappings.
+ // Mappings for post-sort verification of data.
+ std::vector<size_t> new_to_old;
+ std::vector<size_t> old_to_new;
- // Mappings for post-sort verification of data.
- std::vector<size_t> new_to_old;
- std::vector<size_t> old_to_new;
+ // Generate data.
+ dataset.randu();
+ datacopy = dataset; // Save a copy.
- // Generate data.
- dataset.randu();
- datacopy = dataset; // Save a copy.
+ // Build the tree itself.
+ TreeType root(dataset, new_to_old, old_to_new);
- // Build the tree itself.
- TreeType root(dataset, new_to_old, old_to_new);
+ // Ensure the size of the tree is correct.
+ BOOST_REQUIRE_EQUAL(root.count(), size);
- // Ensure the size of the tree is correct.
- BOOST_REQUIRE_EQUAL(root.count(), size);
-
- // Check the forward and backward mappings for correctness.
- for(size_t i = 0; i < size; i++) {
- for(size_t j = 0; j < dimensions; j++) {
- BOOST_REQUIRE_EQUAL(dataset(j, i), datacopy(j, new_to_old[i]));
- BOOST_REQUIRE_EQUAL(dataset(j, old_to_new[i]), datacopy(j, i));
- }
+ // Check the forward and backward mappings for correctness.
+ for(size_t i = 0; i < size; i++)
+ {
+ for(size_t j = 0; j < dimensions; j++)
+ {
+ BOOST_REQUIRE_EQUAL(dataset(j, i), datacopy(j, new_to_old[i]));
+ BOOST_REQUIRE_EQUAL(dataset(j, old_to_new[i]), datacopy(j, i));
}
+ }
- // Now check that each point is contained inside of all bounds above it.
- CheckPointBounds(&root, dataset);
+ // Now check that each point is contained inside of all bounds above it.
+ CheckPointBounds(&root, dataset);
- // Now check that no peers overlap.
- std::vector<TreeType*> v;
- GenerateVectorOfTree(&root, 1, v);
+ // Now check that no peers overlap.
+ std::vector<TreeType*> v;
+ GenerateVectorOfTree(&root, 1, v);
- // Start with the first pair.
- size_t depth = 2;
- // Compare each peer against every other peer.
- while (depth < v.size()) {
- for (size_t i = depth; i < 2 * depth && i < v.size(); i++)
- for (size_t j = i + 1; j < 2 * depth && j < v.size(); j++)
- if (v[i] != NULL && v[j] != NULL)
- BOOST_REQUIRE(!DoBoundsIntersect(v[i]->bound(), v[j]->bound(),
+ // Start with the first pair.
+ size_t depth = 2;
+ // Compare each peer against every other peer.
+ while (depth < v.size())
+ {
+ for (size_t i = depth; i < 2 * depth && i < v.size(); i++)
+ for (size_t j = i + 1; j < 2 * depth && j < v.size(); j++)
+ if (v[i] != NULL && v[j] != NULL)
+ BOOST_REQUIRE(!DoBoundsIntersect(v[i]->bound(), v[j]->bound(),
i, j));
- depth *= 2;
- }
+ depth *= 2;
}
-
- // Reset it to the default value at the end of the test.
- CLI::GetParam<int>("tree/leaf_size") = 20;
}
- // Recursively checks that each node contains all points that it claims to have.
- template<typename TreeType>
- bool CheckPointBounds(TreeType* node, const arma::mat& data) {
- if (node == NULL) // We have passed a leaf node.
- return true;
+ // Reset it to the default value at the end of the test.
+ CLI::GetParam<int>("tree/leaf_size") = 20;
+}
- TreeType* left = node->left();
- TreeType* right = node->right();
+// Recursively checks that each node contains all points that it claims to have.
+template<typename TreeType>
+bool CheckPointBounds(TreeType* node, const arma::mat& data)
+{
+ if (node == NULL) // We have passed a leaf node.
+ return true;
- size_t begin = node->begin();
- size_t count = node->count();
+ TreeType* left = node->left();
+ TreeType* right = node->right();
- // Check that each point which this tree claims is actually inside the tree.
- for (size_t index = begin; index < begin+count; index++) {
- if (!node->bound().Contains(data.col(index)))
- return false;
- }
+ size_t begin = node->begin();
+ size_t count = node->count();
- return CheckPointBounds(left, data) && CheckPointBounds(right, data);
+ // Check that each point which this tree claims is actually inside the tree.
+ for (size_t index = begin; index < begin+count; index++)
+ {
+ if (!node->bound().Contains(data.col(index)))
+ return false;
}
- template<int t_pow>
- bool DoBoundsIntersect(HRectBound<t_pow>& a,
- HRectBound<t_pow>& b,
- size_t ia,
- size_t ib) {
- size_t dimensionality = a.dim();
+ return CheckPointBounds(left, data) && CheckPointBounds(right, data);
+}
- Range r_a;
- Range r_b;
+template<int t_pow>
+bool DoBoundsIntersect(HRectBound<t_pow>& a,
+ HRectBound<t_pow>& b,
+ size_t ia,
+ size_t ib) {
+ size_t dimensionality = a.dim();
- for (size_t i = 0; i < dimensionality; i++) {
- r_a = a[i];
- r_b = b[i];
- if (r_a < r_b || r_a > r_b) // If a does not overlap b at all.
- return false;
- }
+ Range r_a;
+ Range r_b;
- return true;
+ for (size_t i = 0; i < dimensionality; i++)
+ {
+ r_a = a[i];
+ r_b = b[i];
+ if (r_a < r_b || r_a > r_b) // If a does not overlap b at all.
+ return false;
}
- template<typename TreeType>
- void GenerateVectorOfTree(TreeType* node,
- size_t depth,
- std::vector<TreeType*>& v) {
- if (node == NULL)
- return;
+ return true;
+}
- if (depth >= v.size())
- v.resize(2 * depth + 1, NULL); // Resize to right size; fill with NULL.
+template<typename TreeType>
+void GenerateVectorOfTree(TreeType* node,
+ size_t depth,
+ std::vector<TreeType*>& v)
+{
+ if (node == NULL)
+ return;
- v[depth] = node;
+ if (depth >= v.size())
+ v.resize(2 * depth + 1, NULL); // Resize to right size; fill with NULL.
- // Recurse to the left and right children.
- GenerateVectorOfTree(node->left(), depth * 2, v);
- GenerateVectorOfTree(node->right(), depth * 2 + 1, v);
+ v[depth] = node;
- return;
- }
+ // Recurse to the left and right children.
+ GenerateVectorOfTree(node->left(), depth * 2, v);
+ GenerateVectorOfTree(node->right(), depth * 2 + 1, v);
+
+ return;
+}
+
BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/union_find_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/union_find_test.cpp 2011-11-10 15:13:08 UTC (rev 10231)
+++ mlpack/trunk/src/mlpack/tests/union_find_test.cpp 2011-11-10 15:53:59 UTC (rev 10232)
@@ -1,11 +1,9 @@
/**
- * @file union_find_test.cc
- *
+ * @file union_find_test.cpp
* @author Bill March (march at gatech.edu)
*
* Unit tests for the Union-Find data structure.
*/
-
#include <mlpack/methods/emst/union_find.hpp>
#include <mlpack/core.h>
@@ -14,22 +12,25 @@
using namespace mlpack;
using namespace mlpack::emst;
-BOOST_AUTO_TEST_CASE(TestFind) {
+BOOST_AUTO_TEST_SUITE(UnionFindTest);
+
+BOOST_AUTO_TEST_CASE(TestFind)
+{
static const size_t test_size_ = 10;
UnionFind test_union_find_;
test_union_find_.Init(test_size_);
- for (size_t i = 0; i < test_size_; i++) {
+ for (size_t i = 0; i < test_size_; i++)
BOOST_REQUIRE(test_union_find_.Find(i) == i);
- }
- test_union_find_.Union(0,1);
+
+ test_union_find_.Union(0, 1);
test_union_find_.Union(1, 2);
BOOST_REQUIRE(test_union_find_.Find(2) == test_union_find_.Find(0));
-
}
-BOOST_AUTO_TEST_CASE(TestUnion) {
+BOOST_AUTO_TEST_CASE(TestUnion)
+{
static const size_t test_size_ = 10;
UnionFind test_union_find_;
test_union_find_.Init(test_size_);
@@ -46,3 +47,4 @@
BOOST_REQUIRE(test_union_find_.Find(6) == test_union_find_.Find(3));
}
+BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-svn
mailing list