[mlpack-svn] r10352 - in mlpack/trunk/src/mlpack/core: . kernels metrics tree
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Nov 23 01:56:21 EST 2011
Author: rcurtin
Date: 2011-11-23 01:56:20 -0500 (Wed, 23 Nov 2011)
New Revision: 10352
Added:
mlpack/trunk/src/mlpack/core/metrics/
mlpack/trunk/src/mlpack/core/metrics/CMakeLists.txt
mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp
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
Removed:
mlpack/trunk/src/mlpack/core/kernels/lmetric.cpp
mlpack/trunk/src/mlpack/core/kernels/lmetric.hpp
mlpack/trunk/src/mlpack/core/kernels/mahalanobis_distance.hpp
mlpack/trunk/src/mlpack/core/kernels/mahalanobis_distance_impl.hpp
Modified:
mlpack/trunk/src/mlpack/core/CMakeLists.txt
mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt
mlpack/trunk/src/mlpack/core/tree/bounds.hpp
mlpack/trunk/src/mlpack/core/tree/dballbound.hpp
mlpack/trunk/src/mlpack/core/tree/dballbound_impl.hpp
Log:
Move distance functions and metrics into metrics/, and update the build
accordingly.
Modified: mlpack/trunk/src/mlpack/core/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/CMakeLists.txt 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/CMakeLists.txt 2011-11-23 06:56:20 UTC (rev 10352)
@@ -5,6 +5,7 @@
io
kernels
math
+ metrics
optimizers
tree
utilities
Modified: mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt 2011-11-23 06:56:20 UTC (rev 10352)
@@ -5,10 +5,6 @@
set(SOURCES
gaussian_kernel.hpp
linear_kernel.hpp
- lmetric.hpp
- lmetric.cpp
- mahalanobis_distance.hpp
- mahalanobis_distance_impl.hpp
cosine_distance.hpp
cosine_distance.cpp
)
Deleted: mlpack/trunk/src/mlpack/core/kernels/lmetric.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/lmetric.cpp 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/kernels/lmetric.cpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -1,70 +0,0 @@
-/***
- * @file lmetric.cc
- * @author Ryan Curtin
- *
- * Implementation of template specializations of LMetric class.
- */
-#include "lmetric.hpp"
-
-namespace mlpack {
-namespace kernel {
-
-// 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 kernel
-}; // namespace mlpack
Deleted: mlpack/trunk/src/mlpack/core/kernels/lmetric.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/lmetric.hpp 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/kernels/lmetric.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -1,109 +0,0 @@
-/**
- * @file lmetric.h
- * @author Ryan Curtin
- *
- * Generalized L-metric, allowing both squared distances to be returned as well
- * as non-squared distances. The squared distances are faster to compute.
- *
- * This also gives several convenience typedefs for commonly used L-metrics.
- */
-#ifndef __MLPACK_CORE_KERNELS_LMETRIC_H
-#define __MLPACK_CORE_KERNELS_LMETRIC_H
-
-#include <mlpack/core.h>
-
-namespace mlpack {
-namespace kernel {
-
-/**
- * The L_p metric for arbitrary integer p, with an option to take the root.
- *
- * This class implements the standard L_p metric for two arbitary vectors @f$ x
- * @f$ and @f$ y @f$ of dimensionality @f$ n @f$:
- *
- * @f[
- * d(x, y) = \left( \sum_{i = 1}^{n} | x_i - y_i |^p \right)^{\frac{1}{p}}.
- * @f]
- *
- * The value of p is given as a template parameter.
- *
- * In addition, the function @f$ d(x, y) @f$ can be simplified, neglecting the
- * p-root calculation. This is done by specifying the t_take_root template
- * parameter to be false. Then,
- *
- * @f[
- * d(x, y) = \sum_{i = 1}^{n} | x_i - y_i |^p
- * @f]
- *
- * It is faster to compute that distance, so t_take_root is by default off.
- *
- * A few convenience typedefs are given:
- *
- * - ManhattanDistance
- * - EuclideanDistance
- * - SquaredEuclideanDistance
- *
- * @tparam t_pow Power of metric; i.e. t_pow = 1 gives the L1-norm (Manhattan
- * distance).
- * @tparam t_take_root If true, the t_pow'th root of the result is taken before
- * it is returned. In the case of the L2-norm (t_pow = 2), when t_take_root
- * is true, the squared L2 distance is returned. It is slightly faster to set
- * t_take_root = false, because one fewer call to pow() is required.
- */
-template<int t_pow, bool t_take_root = false>
-class LMetric {
- public:
- /***
- * Default constructor does nothing, but is required to satisfy the Kernel
- * policy.
- */
- LMetric() { }
-
- /**
- * Computes the distance between two points.
- */
- static double Evaluate(const arma::vec& a, const arma::vec& 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.
-
-/***
- * The Manhattan (L1) distance.
- */
-typedef LMetric<1, false> ManhattanDistance;
-
-/***
- * The squared Euclidean (L2) distance.
- */
-typedef LMetric<2, false> SquaredEuclideanDistance;
-
-/***
- * The Euclidean (L2) distance.
- */
-typedef LMetric<2, true> EuclideanDistance;
-
-}; // namespace kernel
-}; // namespace mlpack
-
-#endif
Deleted: mlpack/trunk/src/mlpack/core/kernels/mahalanobis_distance.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/mahalanobis_distance.hpp 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/kernels/mahalanobis_distance.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -1,96 +0,0 @@
-/***
- * @file mahalanobis_dstance.h
- * @author Ryan Curtin
- *
- * The Mahalanobis distance.
- */
-#ifndef __MLPACK_CORE_KERNELS_MAHALANOBIS_DISTANCE_H
-#define __MLPACK_CORE_KERNELS_MAHALANOBIS_DISTANCE_H
-
-#include <mlpack/core.h>
-
-namespace mlpack {
-namespace kernel {
-
-/**
- * The Mahalanobis distance, which is essentially a stretched Euclidean
- * distance. Given a square covariance matrix @f$ Q @f$ of size @f$ d @f$ x
- * @f$ d @f$, where @f$ d @f$ is the dimensionality of the points it will be
- * evaluating, and given two vectors @f$ x @f$ and @f$ y @f$ also of
- * dimensionality @f$ d @f$,
- *
- * @f[
- * d(x, y) = \sqrt{(x - y)^T Q (x - y)}
- * @f]
- *
- * where Q is the covariance matrix.
- *
- * Because each evaluation multiplies (x_1 - x_2) by the covariance matrix, it
- * may be much quicker to use an LMetric and simply stretch the actual dataset
- * itself before performing any evaluations. However, this class is provided
- * for convenience.
- *
- * Similar to the LMetric class, this offers a template parameter t_take_root
- * which, when set to false, will instead evaluate the distance
- *
- * @f[
- * d(x, y) = (x - y)^T Q (x - y)
- * @f]
- *
- * which is faster to evaluate.
- *
- * @tparam t_take_root If true, takes the root of the output. It is slightly
- * faster to leave this at the default of false.
- */
-template<bool t_take_root = false>
-class MahalanobisDistance {
- public:
- /**
- * Initialize the Mahalanobis distance with the empty matrix as covariance.
- * Because we don't actually know the size of the vectors we will be using, we
- * delay creation of the covariance matrix until evaluation.
- */
- MahalanobisDistance() : covariance_(0, 0) { }
-
- /**
- * Initialize the Mahalanobis distance with the given covariance matrix. The
- * given covariance matrix will be copied (this is not optimal).
- *
- * @param covariance The covariance matrix to use for this distance.
- */
- MahalanobisDistance(const arma::mat& covariance) : covariance_(covariance) { }
-
- /**
- * Evaluate the distance between the two given points using this Mahalanobis
- * distance.
- *
- * @param a First vector.
- * @param b Second vector.
- */
- double Evaluate(const arma::vec& a, const arma::vec& b);
-
- /**
- * Access the covariance matrix.
- *
- * @return Constant reference to the covariance matrix.
- */
- const arma::mat& GetCovariance() const { return covariance_; }
-
- /**
- * Modify the covariance matrix.
- *
- * @return Reference to the covariance matrix.
- */
- arma::mat& GetCovariance() { return covariance_; }
-
- private:
- //! The covariance matrix associated with this distance.
- arma::mat covariance_;
-};
-
-}; // namespace kernel
-}; // namespace mlpack
-
-#include "mahalanobis_distance_impl.hpp"
-
-#endif
Deleted: mlpack/trunk/src/mlpack/core/kernels/mahalanobis_distance_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/mahalanobis_distance_impl.hpp 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/kernels/mahalanobis_distance_impl.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -1,49 +0,0 @@
-/***
- * @file mahalanobis_distance.cc
- * @author Ryan Curtin
- *
- * Implementation of the Mahalanobis distance.
- */
-#ifndef __MLPACK_CORE_KERNELS_MAHALANOBIS_DISTANCE_IMPL_H
-#define __MLPACK_CORE_KERNELS_MAHALANOBIS_DISTANCE_IMPL_H
-
-#include "mahalanobis_distance.hpp"
-
-namespace mlpack {
-namespace kernel {
-
-/**
- * Specialization for non-rooted case.
- */
-template<>
-double MahalanobisDistance<false>::Evaluate(const arma::vec& a,
- const arma::vec& b) {
- // Check if covariance matrix has been initialized.
- if (covariance_.n_rows == 0)
- covariance_ = arma::eye<arma::mat>(a.n_elem, a.n_elem);
-
- arma::vec m = (a - b);
- arma::mat out = trans(m) * covariance_ * m; // 1x1
- return out[0];
-}
-
-/**
- * Specialization for rooted case. This requires one extra evaluation of
- * sqrt().
- */
-template<>
-double MahalanobisDistance<true>::Evaluate(const arma::vec& a,
- const arma::vec& b) {
- // Check if covariance matrix has been initialized.
- if (covariance_.n_rows == 0)
- covariance_ = arma::eye<arma::mat>(a.n_elem, a.n_elem);
-
- arma::vec m = (a - b);
- arma::mat out = trans(m) * covariance_ * m; // 1x1;
- return sqrt(out[0]);
-}
-
-};
-};
-
-#endif
Added: mlpack/trunk/src/mlpack/core/metrics/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/CMakeLists.txt (rev 0)
+++ mlpack/trunk/src/mlpack/core/metrics/CMakeLists.txt 2011-11-23 06:56:20 UTC (rev 10352)
@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 2.8)
+
+# Define the files we need to compile.
+# Anything not in this list will not be compiled into MLPACK.
+set(SOURCES
+ lmetric.hpp
+ lmetric.cpp
+ mahalanobis_distance.hpp
+ mahalanobis_distance_impl.hpp
+)
+
+# add directory name to sources
+set(DIR_SRCS)
+foreach(file ${SOURCES})
+ set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
+endforeach()
+# Append sources (with directory name) to list of all MLPACK sources (used at
+# the parent scope).
+set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
Added: mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric.cpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -0,0 +1,70 @@
+/***
+ * @file lmetric.cc
+ * @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 kernel
+}; // namespace mlpack
Added: mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -0,0 +1,109 @@
+/**
+ * @file lmetric.h
+ * @author Ryan Curtin
+ *
+ * Generalized L-metric, allowing both squared distances to be returned as well
+ * as non-squared distances. The squared distances are faster to compute.
+ *
+ * This also gives several convenience typedefs for commonly used L-metrics.
+ */
+#ifndef __MLPACK_CORE_METRICS_LMETRIC_HPP
+#define __MLPACK_CORE_METRICS_LMETRIC_HPP
+
+#include <mlpack/core.h>
+
+namespace mlpack {
+namespace metric {
+
+/**
+ * The L_p metric for arbitrary integer p, with an option to take the root.
+ *
+ * This class implements the standard L_p metric for two arbitary vectors @f$ x
+ * @f$ and @f$ y @f$ of dimensionality @f$ n @f$:
+ *
+ * @f[
+ * d(x, y) = \left( \sum_{i = 1}^{n} | x_i - y_i |^p \right)^{\frac{1}{p}}.
+ * @f]
+ *
+ * The value of p is given as a template parameter.
+ *
+ * In addition, the function @f$ d(x, y) @f$ can be simplified, neglecting the
+ * p-root calculation. This is done by specifying the t_take_root template
+ * parameter to be false. Then,
+ *
+ * @f[
+ * d(x, y) = \sum_{i = 1}^{n} | x_i - y_i |^p
+ * @f]
+ *
+ * It is faster to compute that distance, so t_take_root is by default off.
+ *
+ * A few convenience typedefs are given:
+ *
+ * - ManhattanDistance
+ * - EuclideanDistance
+ * - SquaredEuclideanDistance
+ *
+ * @tparam t_pow Power of metric; i.e. t_pow = 1 gives the L1-norm (Manhattan
+ * distance).
+ * @tparam t_take_root If true, the t_pow'th root of the result is taken before
+ * it is returned. In the case of the L2-norm (t_pow = 2), when t_take_root
+ * is true, the squared L2 distance is returned. It is slightly faster to set
+ * t_take_root = false, because one fewer call to pow() is required.
+ */
+template<int t_pow, bool t_take_root = false>
+class LMetric {
+ public:
+ /***
+ * Default constructor does nothing, but is required to satisfy the Kernel
+ * policy.
+ */
+ LMetric() { }
+
+ /**
+ * Computes the distance between two points.
+ */
+ static double Evaluate(const arma::vec& a, const arma::vec& 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.
+
+/***
+ * The Manhattan (L1) distance.
+ */
+typedef LMetric<1, false> ManhattanDistance;
+
+/***
+ * The squared Euclidean (L2) distance.
+ */
+typedef LMetric<2, false> SquaredEuclideanDistance;
+
+/***
+ * The Euclidean (L2) distance.
+ */
+typedef LMetric<2, true> EuclideanDistance;
+
+}; // namespace distance
+}; // namespace mlpack
+
+#endif
Added: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -0,0 +1,96 @@
+/***
+ * @file mahalanobis_dstance.h
+ * @author Ryan Curtin
+ *
+ * The Mahalanobis distance.
+ */
+#ifndef __MLPACK_CORE_METRICS_MAHALANOBIS_DISTANCE_HPP
+#define __MLPACK_CORE_METRICS_MAHALANOBIS_DISTANCE_HPP
+
+#include <mlpack/core.h>
+
+namespace mlpack {
+namespace metric {
+
+/**
+ * The Mahalanobis distance, which is essentially a stretched Euclidean
+ * distance. Given a square covariance matrix @f$ Q @f$ of size @f$ d @f$ x
+ * @f$ d @f$, where @f$ d @f$ is the dimensionality of the points it will be
+ * evaluating, and given two vectors @f$ x @f$ and @f$ y @f$ also of
+ * dimensionality @f$ d @f$,
+ *
+ * @f[
+ * d(x, y) = \sqrt{(x - y)^T Q (x - y)}
+ * @f]
+ *
+ * where Q is the covariance matrix.
+ *
+ * Because each evaluation multiplies (x_1 - x_2) by the covariance matrix, it
+ * may be much quicker to use an LMetric and simply stretch the actual dataset
+ * itself before performing any evaluations. However, this class is provided
+ * for convenience.
+ *
+ * Similar to the LMetric class, this offers a template parameter t_take_root
+ * which, when set to false, will instead evaluate the distance
+ *
+ * @f[
+ * d(x, y) = (x - y)^T Q (x - y)
+ * @f]
+ *
+ * which is faster to evaluate.
+ *
+ * @tparam t_take_root If true, takes the root of the output. It is slightly
+ * faster to leave this at the default of false.
+ */
+template<bool t_take_root = false>
+class MahalanobisDistance {
+ public:
+ /**
+ * Initialize the Mahalanobis distance with the empty matrix as covariance.
+ * Because we don't actually know the size of the vectors we will be using, we
+ * delay creation of the covariance matrix until evaluation.
+ */
+ MahalanobisDistance() : covariance_(0, 0) { }
+
+ /**
+ * Initialize the Mahalanobis distance with the given covariance matrix. The
+ * given covariance matrix will be copied (this is not optimal).
+ *
+ * @param covariance The covariance matrix to use for this distance.
+ */
+ MahalanobisDistance(const arma::mat& covariance) : covariance_(covariance) { }
+
+ /**
+ * Evaluate the distance between the two given points using this Mahalanobis
+ * distance.
+ *
+ * @param a First vector.
+ * @param b Second vector.
+ */
+ double Evaluate(const arma::vec& a, const arma::vec& b);
+
+ /**
+ * Access the covariance matrix.
+ *
+ * @return Constant reference to the covariance matrix.
+ */
+ const arma::mat& GetCovariance() const { return covariance_; }
+
+ /**
+ * Modify the covariance matrix.
+ *
+ * @return Reference to the covariance matrix.
+ */
+ arma::mat& GetCovariance() { return covariance_; }
+
+ private:
+ //! The covariance matrix associated with this distance.
+ arma::mat covariance_;
+};
+
+}; // namespace distance
+}; // namespace mlpack
+
+#include "mahalanobis_distance_impl.hpp"
+
+#endif
Added: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -0,0 +1,49 @@
+/***
+ * @file mahalanobis_distance.cc
+ * @author Ryan Curtin
+ *
+ * Implementation of the Mahalanobis distance.
+ */
+#ifndef __MLPACK_CORE_METRICS_MAHALANOBIS_DISTANCE_IMPL_HPP
+#define __MLPACK_CORE_METRICS_MAHALANOBIS_DISTANCE_IMPL_HPP
+
+#include "mahalanobis_distance.hpp"
+
+namespace mlpack {
+namespace metric {
+
+/**
+ * Specialization for non-rooted case.
+ */
+template<>
+double MahalanobisDistance<false>::Evaluate(const arma::vec& a,
+ const arma::vec& b) {
+ // Check if covariance matrix has been initialized.
+ if (covariance_.n_rows == 0)
+ covariance_ = arma::eye<arma::mat>(a.n_elem, a.n_elem);
+
+ arma::vec m = (a - b);
+ arma::mat out = trans(m) * covariance_ * m; // 1x1
+ return out[0];
+}
+
+/**
+ * Specialization for rooted case. This requires one extra evaluation of
+ * sqrt().
+ */
+template<>
+double MahalanobisDistance<true>::Evaluate(const arma::vec& a,
+ const arma::vec& b) {
+ // Check if covariance matrix has been initialized.
+ if (covariance_.n_rows == 0)
+ covariance_ = arma::eye<arma::mat>(a.n_elem, a.n_elem);
+
+ arma::vec m = (a - b);
+ arma::mat out = trans(m) * covariance_ * m; // 1x1;
+ return sqrt(out[0]);
+}
+
+}; // namespace distance
+}; // namespace mlpack
+
+#endif
Modified: mlpack/trunk/src/mlpack/core/tree/bounds.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/bounds.hpp 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/tree/bounds.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -14,7 +14,8 @@
#include <mlpack/core/math/math_misc.hpp>
#include <mlpack/core/math/range.hpp>
-#include <mlpack/core/kernels/lmetric.hpp>
+#include <mlpack/core/metrics/lmetric.hpp>
+
#include "hrectbound.hpp"
#include "periodichrectbound.hpp"
#include "dballbound.hpp"
Modified: mlpack/trunk/src/mlpack/core/tree/dballbound.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/dballbound.hpp 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/tree/dballbound.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -12,7 +12,7 @@
#include <mlpack/core.h>
#include <mlpack/core/math/math_misc.hpp>
-#include <mlpack/core/kernels/lmetric.hpp>
+#include <mlpack/core/metrics/lmetric.hpp>
namespace mlpack {
namespace bound {
@@ -25,7 +25,7 @@
* To initialize this, set the radius with @c set_radius
* and set the point by initializing @c point() directly.
*/
-template<typename TMetric = mlpack::kernel::SquaredEuclideanDistance, typename TPoint = arma::vec>
+template<typename TMetric = mlpack::metric::SquaredEuclideanDistance, typename TPoint = arma::vec>
class DBallBound {
public:
typedef TPoint Point;
Modified: mlpack/trunk/src/mlpack/core/tree/dballbound_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/dballbound_impl.hpp 2011-11-22 19:18:32 UTC (rev 10351)
+++ mlpack/trunk/src/mlpack/core/tree/dballbound_impl.hpp 2011-11-23 06:56:20 UTC (rev 10352)
@@ -11,9 +11,6 @@
#include <mlpack/core.h>
-// Awaiting transition
-#include <mlpack/core/kernels/lmetric.hpp>
-
namespace mlpack {
namespace bound {
More information about the mlpack-svn
mailing list