[mlpack-svn] r15424 - in mlpack/trunk/src/mlpack: core/metrics tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Jul 5 21:57:46 EDT 2013


Author: pram
Date: Fri Jul  5 21:57:46 2013
New Revision: 15424

Log:
Chebyshev distance (L-infinity distance) added to lmetric.hpp

Added:
   mlpack/trunk/src/mlpack/tests/metric_test.cpp
Modified:
   mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
   mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp
   mlpack/trunk/src/mlpack/tests/CMakeLists.txt

Modified: mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp	Fri Jul  5 21:57:46 2013
@@ -88,6 +88,11 @@
  */
 typedef LMetric<2, true> EuclideanDistance;
 
+/***
+ * The L-infinity distance
+ */
+typedef LMetric<INT_MAX, false> ChebyshevDistance;
+
 }; // namespace metric
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp	Fri Jul  5 21:57:46 2013
@@ -78,6 +78,14 @@
   return accu(pow(abs(a - b), 3.0));
 }
 
+// L-infinity (Chebyshev distance) specialization
+template<>
+template<typename VecType1, typename VecType2>
+double LMetric<INT_MAX, false>::Evaluate(const VecType1& a, const VecType2& b)
+{
+  return max(abs(a - b));
+}
+
 }; // namespace metric
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/tests/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/tests/CMakeLists.txt	(original)
+++ mlpack/trunk/src/mlpack/tests/CMakeLists.txt	Fri Jul  5 21:57:46 2013
@@ -25,6 +25,7 @@
   lrsdp_test.cpp
   lsh_test.cpp
   math_test.cpp
+  metric_test.cpp
   nbc_test.cpp
   nca_test.cpp
   nmf_test.cpp

Added: mlpack/trunk/src/mlpack/tests/metric_test.cpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/tests/metric_test.cpp	Fri Jul  5 21:57:46 2013
@@ -0,0 +1,85 @@
+/**
+ * @file metric_test.cpp
+ *
+ * Unit tests for the 'LMetric' class.
+ */
+#include <mlpack/core.hpp>
+#include <mlpack/core/metrics/lmetric.hpp>
+#include <boost/test/unit_test.hpp>
+#include "old_boost_test_definitions.hpp"
+
+using namespace std;
+using namespace mlpack::metric;
+
+BOOST_AUTO_TEST_SUITE(LMetricTest);
+
+BOOST_AUTO_TEST_CASE(L1MetricTest)
+{
+  arma::vec a1(5);
+  a1.randn();
+
+  arma::vec b1(5);
+  b1.randn();
+
+  arma::Col<size_t> a2(5);
+  a2 << 1 << 2 << 1 << 0 << 5;
+
+  arma::Col<size_t> b2(5);
+  b2 << 2 << 5 << 2 << 0 << 1;
+
+  ManhattanDistance lMetric;
+
+  BOOST_REQUIRE_CLOSE(arma::sum(arma::abs(a1 - b1)),
+                      lMetric.Evaluate(a1, b1), 1e-5);
+
+  BOOST_REQUIRE_CLOSE(arma::sum(arma::abs(a2 - b2)),
+                      lMetric.Evaluate(a2, b2), 1e-5);
+}
+
+BOOST_AUTO_TEST_CASE(L2MetricTest)
+{
+  arma::vec a1(5);
+  a1.randn();
+
+  arma::vec b1(5);
+  b1.randn();
+
+  arma::Col<size_t> a2(5);
+  a2 << 1 << 2 << 1 << 0 << 5;
+
+  arma::Col<size_t> b2(5);
+  b2 << 2 << 5 << 2 << 0 << 1;
+
+  EuclideanDistance lMetric;
+
+  BOOST_REQUIRE_CLOSE(sqrt(arma::sum(arma::square(a1 - b1))),
+                      lMetric.Evaluate(a1, b1), 1e-5);
+
+  BOOST_REQUIRE_CLOSE(sqrt(arma::sum(arma::square(a2 - b2))),
+                      lMetric.Evaluate(a2, b2), 1e-5);
+}
+
+BOOST_AUTO_TEST_CASE(LINFMetricTest)
+{
+  arma::vec a1(5);
+  a1.randn();
+
+  arma::vec b1(5);
+  b1.randn();
+
+  arma::Col<size_t> a2(5);
+  a2 << 1 << 2 << 1 << 0 << 5;
+
+  arma::Col<size_t> b2(5);
+  b2 << 2 << 5 << 2 << 0 << 1;
+
+  EuclideanDistance lMetric;
+
+  BOOST_REQUIRE_CLOSE(arma::max(arma::abs(a1 - b1)),
+                      lMetric.Evaluate(a1, b1), 1e-5);
+
+  BOOST_REQUIRE_CLOSE(arma::max(arma::abs(a2 - b2)),
+                      lMetric.Evaluate(a2, b2), 1e-5);
+}
+
+BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list