1 //
2 //  Copyright (C) 2003-2007 Rational Discovery LLC
3 //
4 //  @@ All Rights Reserved @@
5 //  This file is part of the RDKit.
6 //  The contents are covered by the terms of the BSD license
7 //  which is included in the file license.txt, found at the root
8 //  of the RDKit source tree.
9 //
10 #include <RDGeneral/export.h>
11 #ifndef __RD_METRICFUNCS_H__
12 #define __RD_METRICFUNCS_H__
13 #include <cmath>
14 #include <DataStructs/BitOps.h>
15 #include <RDGeneral/Invariant.h>
16 
17 namespace RDDataManip {
18 //! return the Euclidean distance between two vectors
19 template <typename T1, typename T2>
EuclideanDistanceMetric(const T1 & v1,const T2 & v2,unsigned int dim)20 double EuclideanDistanceMetric(const T1 &v1, const T2 &v2, unsigned int dim) {
21   double dist = 0.0;
22   for (unsigned int i = 0; i < dim; i++) {
23     double diff = static_cast<double>(v1[i]) - static_cast<double>(v2[i]);
24     dist += (diff * diff);
25   }
26   return sqrt(dist);
27 };
28 
29 // FIX: there's no reason to have this tied to TanimotoSimilarity... could
30 // include
31 // a different sim function as a template param
32 //! return the Tanimoto distance (1-TanimotoSimilarity) between two bit vectors
33 template <typename T1, typename T2>
TanimotoDistanceMetric(const T1 & bv1,const T2 & bv2,unsigned int dim)34 double TanimotoDistanceMetric(const T1 &bv1, const T2 &bv2, unsigned int dim) {
35   // the dim parameter is actually irrelevant here but we have to include it to
36   // deal with
37   // template version of setMetricFunc in MetricMatricCalc
38   RDUNUSED_PARAM(dim);
39   return (1.0 - SimilarityWrapper(
40                     bv1, bv2,
41                     (double (*)(const T1 &, const T2 &))TanimotoSimilarity));
42 };
43 
44 //! return the Tanimoto similarity between two bit vectors
45 template <typename T1, typename T2>
TanimotoSimilarityMetric(const T1 & bv1,const T2 & bv2,unsigned int dim)46 double TanimotoSimilarityMetric(const T1 &bv1, const T2 &bv2,
47                                 unsigned int dim) {
48   RDUNUSED_PARAM(dim);
49   return SimilarityWrapper(
50       bv1, bv2, (double (*)(const T1 &, const T2 &))TanimotoSimilarity);
51 };
52 }  // namespace RDDataManip
53 
54 #endif
55