[mlpack-svn] r13524 - mlpack/trunk/src/mlpack/core/metrics
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Sep 10 15:27:45 EDT 2012
Author: rcurtin
Date: 2012-09-10 15:27:44 -0400 (Mon, 10 Sep 2012)
New Revision: 13524
Modified:
mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp
Log:
Add constructor which automatically creates identity covariance, and don't check
if covariance is initialized when calling Evaluate().
Modified: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp 2012-09-10 19:17:14 UTC (rev 13523)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp 2012-09-10 19:27:44 UTC (rev 13524)
@@ -48,12 +48,20 @@
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.
+ * Don't call Evaluate() until you set the covariance with Covariance()!
*/
MahalanobisDistance() { }
/**
+ * Initialize the Mahalanobis distance with the identity matrix of the given
+ * dimensionality.
+ *
+ * @param dimensionality Dimesnsionality of the covariance matrix.
+ */
+ MahalanobisDistance(const size_t dimensionality) :
+ covariance(arma::eye<arma::mat>(dimensionality, dimensionality)) { }
+
+ /**
* Initialize the Mahalanobis distance with the given covariance matrix. The
* given covariance matrix will be copied (this is not optimal).
*
@@ -63,7 +71,9 @@
/**
* Evaluate the distance between the two given points using this Mahalanobis
- * distance.
+ * distance. If the covariance matrix has not been set (i.e. if you used the
+ * empty constructor and did not later modify the covariance matrix), calling
+ * this method will probably result in a crash.
*
* @param a First vector.
* @param b Second vector.
Modified: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp 2012-09-10 19:17:14 UTC (rev 13523)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp 2012-09-10 19:27:44 UTC (rev 13524)
@@ -20,10 +20,6 @@
double MahalanobisDistance<false>::Evaluate(const VecType1& a,
const VecType2& 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];
More information about the mlpack-svn
mailing list