Loading [MathJax]/jax/output/SVG/config.js
Chemical Data Processing Library C++ API - Version 1.3.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SimilarityFunctions.hpp
Go to the documentation of this file.
1 /*
2  * SimilarityFunctions.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_DESCR_SIMILARITYFUNCTIONS_HPP
30 #define CDPL_DESCR_SIMILARITYFUNCTIONS_HPP
31 
32 #include <cstddef>
33 #include <cmath>
34 
35 #include "CDPL/Descr/APIPrefix.hpp"
36 #include "CDPL/Util/BitSet.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Descr
44  {
45 
66 
81  template <typename V>
82  inline double calcTanimotoSimilarity(const V& v1, const V& v2);
83 
104 
119  template <typename V>
120  inline double calcCosineSimilarity(const V& v1, const V& v2);
121 
143 
165 
186 
211  CDPL_DESCR_API double calcTverskySimilarity(const Util::BitSet& bs1, const Util::BitSet& bs2, double a, double b);
212 
232  CDPL_DESCR_API std::size_t calcHammingDistance(const Util::BitSet& bs1, const Util::BitSet& bs2);
233 
248  template <typename V>
249  inline double calcManhattanDistance(const V& v1, const V& v2);
250 
271 
286  template <typename V>
287  inline double calcEuclideanDistance(const V& v1, const V& v2);
288 
289  } // namespace Descr
290 } // namespace CDPL
291 
292 
293 // Implementation
294 
295 template <typename V>
296 inline double CDPL::Descr::calcTanimotoSimilarity(const V& v1, const V& v2)
297 {
298  double ep12 = innerProd(v1, v2);
299 
300  return (ep12 / (innerProd(v1, v1) + innerProd(v2, v2) - ep12));
301 }
302 
303 template <typename V>
304 inline double CDPL::Descr::calcCosineSimilarity(const V& v1, const V& v2)
305 {
306  return angleCos(v1, v2, double(norm2(v1) * norm2(v2)));
307 }
308 
309 template <typename V>
310 inline double CDPL::Descr::calcManhattanDistance(const V& v1, const V& v2)
311 {
312  return norm1(v1 - v2);
313 }
314 
315 template <typename V>
316 inline double CDPL::Descr::calcEuclideanDistance(const V& v1, const V& v2)
317 {
318  auto dv = v1 - v2;
319 
320  return std::sqrt(double(innerProd(dv, dv)));
321 }
322 
323 #endif // CDPL_DESCR_SIMILARITYFUNCTIONS_HPP
Definition of the type CDPL::Util::BitSet.
Definition of the preprocessor macro CDPL_DESCR_API.
#define CDPL_DESCR_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Definition of various vector expression types and operations.
constexpr unsigned int V
Specifies Vanadium.
Definition: AtomType.hpp:177
CDPL_DESCR_API double calcManhattanSimilarity(const Util::BitSet &bs1, const Util::BitSet &bs2)
Calculates the Manhattan Similarity [GSIM] of the bitsets bs1 and bs2.
CDPL_DESCR_API double calcCosineSimilarity(const Util::BitSet &bs1, const Util::BitSet &bs2)
Calculates the Cosine Similarity [WCOS] of the bitsets bs1 and bs2.
CDPL_DESCR_API double calcTverskySimilarity(const Util::BitSet &bs1, const Util::BitSet &bs2, double a, double b)
Calculates the Tversky Similarity [GSIM] of the bitsets bs1 and bs2.
CDPL_DESCR_API double calcTanimotoSimilarity(const Util::BitSet &bs1, const Util::BitSet &bs2)
Calculates the Tanimoto Similarity [CITB] of the bitsets bs1 and bs2.
CDPL_DESCR_API double calcDiceSimilarity(const Util::BitSet &bs1, const Util::BitSet &bs2)
Calculates the Dice Similarity [GSIM] of the bitsets bs1 and bs2.
CDPL_DESCR_API double calcEuclideanSimilarity(const Util::BitSet &bs1, const Util::BitSet &bs2)
Calculates the Euclidean Similarity [GSIM] of the bitsets bs1 and bs2.
CDPL_DESCR_API std::size_t calcHammingDistance(const Util::BitSet &bs1, const Util::BitSet &bs2)
Calculates the Hamming Distance [WHAM, CITB] between the bitsets bs1 and bs2.
CDPL_DESCR_API double calcEuclideanDistance(const Util::BitSet &bs1, const Util::BitSet &bs2)
Calculates the Euclidean Distance [CITB] between the bitsets bs1 and bs2.
double calcManhattanDistance(const V &v1, const V &v2)
Calculates the Manhattan Distance [MADI] between the vectors v1 and v2.
Definition: SimilarityFunctions.hpp:310
VectorInnerProduct< E1, E2 >::ResultType innerProd(const VectorExpression< E1 > &e1, const VectorExpression< E2 > &e2)
Definition: VectorExpression.hpp:504
QuaternionNorm2< E >::ResultType norm2(const QuaternionExpression< E > &e)
Definition: QuaternionExpression.hpp:804
VectorAngleCosine< E1, E2, T >::ResultType angleCos(const VectorExpression< E1 > &e1, const VectorExpression< E2 > &e2, const T &sd, bool clamp=true)
Definition: VectorExpression.hpp:511
MatrixNorm1< E >::ResultType norm1(const MatrixExpression< E > &e)
Definition: MatrixExpression.hpp:903
boost::dynamic_bitset BitSet
A dynamic bitset class.
Definition: BitSet.hpp:46
The namespace of the Chemical Data Processing Library.