[mlpack-svn] r12125 - mlpack/trunk/src/mlpack/core/metrics
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Mar 29 15:08:08 EDT 2012
Author: jcline3
Date: 2012-03-29 15:08:07 -0400 (Thu, 29 Mar 2012)
New Revision: 12125
Modified:
mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp
mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp
Log:
Update metrics to support distinct vector types as arguments.
This let's you do something like:
matrix x;
x.zeros(5,5);
vec y;
y.zeros(5);
double dist = Evaluate(x.col(0), y);
Modified: mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp 2012-03-29 17:09:59 UTC (rev 12124)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp 2012-03-29 19:08:07 UTC (rev 12125)
@@ -63,8 +63,8 @@
/**
* Computes the distance between two points.
*/
- template<typename VecType>
- static double Evaluate(const VecType& a, const VecType& b);
+ template<typename VecType1, typename VecType2>
+ static double Evaluate(const VecType1& a, const VecType2& b);
};
// Convenience typedefs.
Modified: mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp 2012-03-29 17:09:59 UTC (rev 12124)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp 2012-03-29 19:08:07 UTC (rev 12125)
@@ -15,9 +15,9 @@
// 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)
+template<typename VecType1, typename VecType2>
+double LMetric<t_pow, t_take_root>::Evaluate(const VecType1& a,
+ const VecType2& b)
{
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
@@ -31,38 +31,38 @@
// L1-metric specializations; the root doesn't matter.
template<>
-template<typename VecType>
-double LMetric<1, true>::Evaluate(const VecType& a, const VecType& b)
+template<typename VecType1, typename VecType2>
+double LMetric<1, true>::Evaluate(const VecType1& a, const VecType2& b)
{
return accu(abs(a - b));
}
template<>
-template<typename VecType>
-double LMetric<1, false>::Evaluate(const VecType& a, const VecType& b)
+template<typename VecType1, typename VecType2>
+double LMetric<1, false>::Evaluate(const VecType1& a, const VecType2& b)
{
return accu(abs(a - b));
}
// L2-metric specializations.
template<>
-template<typename VecType>
-double LMetric<2, true>::Evaluate(const VecType& a, const VecType& b)
+template<typename VecType1, typename VecType2>
+double LMetric<2, true>::Evaluate(const VecType1& a, const VecType2& b)
{
return sqrt(accu(square(a - b)));
}
template<>
-template<typename VecType>
-double LMetric<2, false>::Evaluate(const VecType& a, const VecType& b)
+template<typename VecType1, typename VecType2>
+double LMetric<2, false>::Evaluate(const VecType1& a, const VecType2& 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)
+template<typename VecType1, typename VecType2>
+double LMetric<3, true>::Evaluate(const VecType1& a, const VecType2& b)
{
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
@@ -72,8 +72,8 @@
}
template<>
-template<typename VecType>
-double LMetric<3, false>::Evaluate(const VecType& a, const VecType& b)
+template<typename VecType1, typename VecType2>
+double LMetric<3, false>::Evaluate(const VecType1& a, const VecType2& b)
{
return accu(pow(abs(a - b), 3.0));
}
Modified: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp 2012-03-29 17:09:59 UTC (rev 12124)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp 2012-03-29 19:08:07 UTC (rev 12125)
@@ -68,8 +68,8 @@
* @param a First vector.
* @param b Second vector.
*/
- template<typename VecType>
- double Evaluate(const VecType& a, const VecType& b);
+ template<typename VecType1, typename VecType2>
+ double Evaluate(const VecType1& a, const VecType2& 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 2012-03-29 17:09:59 UTC (rev 12124)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp 2012-03-29 19:08:07 UTC (rev 12125)
@@ -16,9 +16,9 @@
* Specialization for non-rooted case.
*/
template<>
-template<typename VecType>
-double MahalanobisDistance<false>::Evaluate(const VecType& a,
- const VecType& b)
+template<typename VecType1, typename VecType2>
+double MahalanobisDistance<false>::Evaluate(const VecType1& a,
+ const VecType2& b)
{
// Check if covariance matrix has been initialized.
if (covariance.n_rows == 0)
@@ -34,9 +34,9 @@
* sqrt().
*/
template<>
-template<typename VecType>
-double MahalanobisDistance<true>::Evaluate(const VecType& a,
- const VecType& b)
+template<typename VecType1, typename VecType2>
+double MahalanobisDistance<true>::Evaluate(const VecType1& a,
+ const VecType2& b)
{
// Check if covariance matrix has been initialized.
if (covariance.n_rows == 0)
More information about the mlpack-svn
mailing list