From 14ff48d30c598efe42becf96c8bf35e25cbf52e2 Mon Sep 17 00:00:00 2001
From: Davis King <davis@dlib.net>
Date: Thu, 3 Jul 2008 22:56:25 +0000
Subject: [PATCH] Added the ability to compare kcentroid objects to each other

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402365
---
 dlib/svm/kcentroid.h          | 27 +++++++++++++++++++++++++++
 dlib/svm/kcentroid_abstract.h | 13 ++++++++++++-
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/dlib/svm/kcentroid.h b/dlib/svm/kcentroid.h
index c067ef9b..69a0b83a 100644
--- a/dlib/svm/kcentroid.h
+++ b/dlib/svm/kcentroid.h
@@ -73,6 +73,33 @@ namespace dlib
             bias = 0;
         }
 
+        scalar_type operator() (
+            const kcentroid& x
+        ) const
+        {
+            // make sure requires clause is not broken
+            DLIB_ASSERT(x.get_kernel() == get_kernel(),
+                "\tscalar_type kcentroid::operator()(const kcentroid& x)"
+                << "\n\tYou can only compare two kcentroid objects if they use the same kernel"
+                << "\n\tthis: " << this
+                );
+
+            scalar_type temp = 0;
+            for (unsigned long i = 0; i < alpha.size(); ++i)
+            {
+                for (unsigned long j = 0; j < x.alpha.size(); ++j)
+                {
+                    temp += alpha[i]*x.alpha[j]*kernel(dictionary[i], x.dictionary[j]);
+                }
+            }
+
+            temp = x.bias + bias - 2*temp;
+            if (temp > 0)
+                return std::sqrt(temp);
+            else
+                return 0;
+        }
+
         scalar_type operator() (
             const sample_type& x
         ) const
diff --git a/dlib/svm/kcentroid_abstract.h b/dlib/svm/kcentroid_abstract.h
index 97019bb2..4c6bfdb0 100644
--- a/dlib/svm/kcentroid_abstract.h
+++ b/dlib/svm/kcentroid_abstract.h
@@ -106,12 +106,23 @@ namespace dlib
                 - #samples_seen() == 0
         !*/
 
+        scalar_type operator() (
+            const kcentroid& x
+        ) const;
+        /*!
+            requires
+                - x.get_kernel() == get_kernel()
+            ensures
+                - returns the distance in kernel feature space between this centroid and the
+                  centroid represented by x.  
+        !*/
+
         scalar_type operator() (
             const sample_type& x
         ) const;
         /*!
             ensures
-                - returns the distance in feature space between the sample x and the
+                - returns the distance in kernel feature space between the sample x and the
                   current estimate of the centroid of the training samples given
                   to this object so far.
         !*/
-- 
2.18.0