[mlpack-svn] r10739 - mlpack/trunk/src/mlpack/core/metrics

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Dec 13 03:59:09 EST 2011


Author: rcurtin
Date: 2011-12-13 03:59:08 -0500 (Tue, 13 Dec 2011)
New Revision: 10739

Added:
   mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp
Removed:
   mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp
Modified:
   mlpack/trunk/src/mlpack/core/metrics/CMakeLists.txt
   mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
   mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
   mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp
Log:
Allow sparse vectors to be passed to metrics (this should get a bit of speedup).
Now we leave the computation up to Armadillo.


Modified: mlpack/trunk/src/mlpack/core/metrics/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/CMakeLists.txt	2011-12-13 08:35:27 UTC (rev 10738)
+++ mlpack/trunk/src/mlpack/core/metrics/CMakeLists.txt	2011-12-13 08:59:08 UTC (rev 10739)
@@ -4,7 +4,7 @@
 # Anything not in this list will not be compiled into MLPACK.
 set(SOURCES
   lmetric.hpp
-  lmetric.cpp
+  lmetric_impl.hpp
   mahalanobis_distance.hpp
   mahalanobis_distance_impl.hpp
 )

Deleted: mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp	2011-12-13 08:35:27 UTC (rev 10738)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp	2011-12-13 08:59:08 UTC (rev 10739)
@@ -1,76 +0,0 @@
-/**
- * @file lmetric.cpp
- * @author Ryan Curtin
- *
- * Implementation of template specializations of LMetric class.
- */
-#include "lmetric.hpp"
-
-namespace mlpack {
-namespace metric {
-
-// L1-metric specializations; the root doesn't matter.
-template<>
-double LMetric<1, true>::Evaluate(const arma::vec& a, const arma::vec& b)
-{
-  double sum = 0;
-  for (size_t i = 0; i < a.n_elem; i++)
-    sum += fabs(a[i] - b[i]);
-
-  return sum;
-}
-
-template<>
-double LMetric<1, false>::Evaluate(const arma::vec& a, const arma::vec& b)
-{
-  double sum = 0;
-  for (size_t i = 0; i < a.n_elem; i++)
-    sum += fabs(a[i] - b[i]);
-
-  return sum;
-}
-
-// L2-metric specializations.
-template<>
-double LMetric<2, true>::Evaluate(const arma::vec& a, const arma::vec& b)
-{
-  double sum = 0;
-  for (size_t i = 0; i < a.n_elem; i++)
-    sum += pow(a[i] - b[i], 2.0); // fabs() not necessary when squaring.
-
-  return sqrt(sum);
-}
-
-template<>
-double LMetric<2, false>::Evaluate(const arma::vec& a, const arma::vec& b)
-{
-  double sum = 0;
-  for (size_t i = 0; i < a.n_elem; i++)
-    sum += pow(a[i] - b[i], 2.0);
-
-  return sum;
-}
-
-// L3-metric specialization (not very likely to be used, but just in case).
-template<>
-double LMetric<3, true>::Evaluate(const arma::vec& a, const arma::vec& b)
-{
-  double sum = 0;
-  for (size_t i = 0; i < a.n_elem; i++)
-    sum += pow(fabs(a[i] - b[i]), 3.0);
-
-  return pow(sum, 1.0 / 3.0);
-}
-
-template<>
-double LMetric<3, false>::Evaluate(const arma::vec& a, const arma::vec& b)
-{
-  double sum = 0;
-  for (size_t i = 0; i < a.n_elem; i++)
-    sum += pow(fabs(a[i] - b[i]), 3.0);
-
-  return sum;
-}
-
-}; // namespace metric
-}; // namespace mlpack

Modified: mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp	2011-12-13 08:35:27 UTC (rev 10738)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp	2011-12-13 08:59:08 UTC (rev 10739)
@@ -63,31 +63,10 @@
   /**
    * Computes the distance between two points.
    */
-  static double Evaluate(const arma::vec& a, const arma::vec& b);
+  template<typename VecType>
+  static double Evaluate(const VecType& a, const VecType& b);
 };
 
-// Doxygen will not include this specialization.
-//! @cond
-
-// The implementation is not split into a _impl.h file because it is so simple;
-// the unspecialized implementation of the one function is given below.
-// Unspecialized implementation.  This should almost never be used...
-template<int t_pow, bool t_take_root>
-double LMetric<t_pow, t_take_root>::Evaluate(const arma::vec& a,
-                                             const arma::vec& b)
-{
-  double sum = 0;
-  for (size_t i = 0; i < a.n_elem; i++)
-    sum += pow(fabs(a[i] - b[i]), t_pow);
-
-  if (!t_take_root) // Suboptimal to have this here.
-    return sum;
-
-  return pow(sum, (1.0 / t_pow));
-}
-
-//! @endcond
-
 // Convenience typedefs.
 
 /***
@@ -108,4 +87,7 @@
 }; // namespace metric
 }; // namespace mlpack
 
+// Include implementation.
+#include "lmetric_impl.hpp"
+
 #endif

Copied: mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp (from rev 10737, mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp)
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp	2011-12-13 08:59:08 UTC (rev 10739)
@@ -0,0 +1,84 @@
+/**
+ * @file lmetric_impl.hpp
+ * @author Ryan Curtin
+ *
+ * Implementation of template specializations of LMetric class.
+ */
+#ifndef __MLPACK_CORE_METRICS_LMETRIC_IMPL_HPP
+#define __MLPACK_CORE_METRICS_LMETRIC_IMPL_HPP
+
+// In case it hasn't been included.
+#include "lmetric.hpp"
+
+namespace mlpack {
+namespace metric {
+
+// Unspecialized implementation.  This should almost never be used...
+template<int t_pow, bool t_take_root>
+template<typename VecType>
+double LMetric<t_pow, t_take_root>::Evaluate(const VecType& a,
+                                             const VecType& b)
+{
+  double sum = 0;
+  for (size_t i = 0; i < a.n_elem; i++)
+    sum += pow(fabs(a[i] - b[i]), t_pow);
+
+  if (!t_take_root) // Suboptimal to have this here.
+    return sum;
+
+  return pow(sum, (1.0 / t_pow));
+}
+
+// L1-metric specializations; the root doesn't matter.
+template<>
+template<typename VecType>
+double LMetric<1, true>::Evaluate(const VecType& a, const VecType& b)
+{
+  return accu(abs(a - b));
+}
+
+template<>
+template<typename VecType>
+double LMetric<1, false>::Evaluate(const VecType& a, const VecType& b)
+{
+  return accu(abs(a - b));
+}
+
+// L2-metric specializations.
+template<>
+template<typename VecType>
+double LMetric<2, true>::Evaluate(const VecType& a, const VecType& b)
+{
+  return sqrt(accu(square(a - b)));
+}
+
+template<>
+template<typename VecType>
+double LMetric<2, false>::Evaluate(const VecType& a, const VecType& b)
+{
+  return accu(square(a - b));
+}
+
+// L3-metric specialization (not very likely to be used, but just in case).
+template<>
+template<typename VecType>
+double LMetric<3, true>::Evaluate(const VecType& a, const VecType& b)
+{
+  double sum = 0;
+  for (size_t i = 0; i < a.n_elem; i++)
+    sum += pow(fabs(a[i] - b[i]), 3.0);
+
+  return pow(accu(pow(abs(a - b), 3.0)), 1.0 / 3.0);
+}
+
+template<>
+template<typename VecType>
+double LMetric<3, false>::Evaluate(const VecType& a, const VecType& b)
+{
+  return accu(pow(abs(a - b), 3.0));
+}
+
+}; // namespace metric
+}; // namespace mlpack
+
+#endif

Modified: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp	2011-12-13 08:35:27 UTC (rev 10738)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp	2011-12-13 08:59:08 UTC (rev 10739)
@@ -68,7 +68,8 @@
    * @param a First vector.
    * @param b Second vector.
    */
-  double Evaluate(const arma::vec& a, const arma::vec& b);
+  template<typename VecType>
+  double Evaluate(const VecType& a, const VecType& b);
 
   /**
    * Access the covariance matrix.

Modified: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp	2011-12-13 08:35:27 UTC (rev 10738)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp	2011-12-13 08:59:08 UTC (rev 10739)
@@ -16,8 +16,9 @@
  * Specialization for non-rooted case.
  */
 template<>
-double MahalanobisDistance<false>::Evaluate(const arma::vec& a,
-                                            const arma::vec& b)
+template<typename VecType>
+double MahalanobisDistance<false>::Evaluate(const VecType& a,
+                                            const VecType& b)
 {
   // Check if covariance matrix has been initialized.
   if (covariance.n_rows == 0)
@@ -33,8 +34,9 @@
  * sqrt().
  */
 template<>
-double MahalanobisDistance<true>::Evaluate(const arma::vec& a,
-                                           const arma::vec& b)
+template<typename VecType>
+double MahalanobisDistance<true>::Evaluate(const VecType& a,
+                                           const VecType& b)
 {
   // Check if covariance matrix has been initialized.
   if (covariance.n_rows == 0)




More information about the mlpack-svn mailing list