RDKit
Open-source cheminformatics and machine learning.
MetricFuncs.h
Go to the documentation of this file.
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 #ifndef __RD_METRICFUNCS_H__
11 #define __RD_METRICFUNCS_H__
12 #include <cmath>
13 #include <DataStructs/BitOps.h>
14 #include <RDGeneral/Invariant.h>
15 
16 namespace RDDataManip {
17 //! return the Euclidean distance between two vectors
18 template <typename T1, typename T2>
19 double EuclideanDistanceMetric(const T1 &v1, const T2 &v2, unsigned int dim) {
20  double dist = 0.0;
21  for (unsigned int i = 0; i < dim; i++) {
22  double diff = static_cast<double>(v1[i]) - static_cast<double>(v2[i]);
23  dist += (diff * diff);
24  }
25  return sqrt(dist);
26 };
27 
28 // FIX: there's no reason to have this tied to TanimotoSimilarity... could
29 // include
30 // a different sim function as a template param
31 //! return the Tanimoto distance (1-TanimotoSimilarity) between two bit vectors
32 template <typename T1, typename T2>
33 double TanimotoDistanceMetric(const T1 &bv1, const T2 &bv2, unsigned int dim) {
34  // the dim parameter is actually irrelevant here but we have to include it to
35  // deal with
36  // template version of setMetricFunc in MetricMatricCalc
37  RDUNUSED_PARAM(dim);
38  return (1.0 - SimilarityWrapper(
39  bv1, bv2,
40  (double (*)(const T1 &, const T2 &))TanimotoSimilarity));
41 };
42 
43 //! return the Tanimoto similarity between two bit vectors
44 template <typename T1, typename T2>
45 double TanimotoSimilarityMetric(const T1 &bv1, const T2 &bv2,
46  unsigned int dim) {
47  RDUNUSED_PARAM(dim);
48  return SimilarityWrapper(
49  bv1, bv2, (double (*)(const T1 &, const T2 &))TanimotoSimilarity);
50 };
51 }
52 
53 #endif
double EuclideanDistanceMetric(const T1 &v1, const T2 &v2, unsigned int dim)
return the Euclidean distance between two vectors
Definition: MetricFuncs.h:19
double TanimotoSimilarity(const T1 &bv1, const T2 &bv2)
returns the Tanimoto similarity between two bit vects
double TanimotoSimilarityMetric(const T1 &bv1, const T2 &bv2, unsigned int dim)
return the Tanimoto similarity between two bit vectors
Definition: MetricFuncs.h:45
#define RDUNUSED_PARAM(x)
Definition: Invariant.h:194
double TanimotoDistanceMetric(const T1 &bv1, const T2 &bv2, unsigned int dim)
return the Tanimoto distance (1-TanimotoSimilarity) between two bit vectors
Definition: MetricFuncs.h:33
Contains general bit-comparison and similarity operations.
double SimilarityWrapper(const T &bv1, const T &bv2, double(*metric)(const T &, const T &), bool returnDistance=false)
Definition: BitOps.h:30