RDKit
Open-source cheminformatics and machine learning.
utils.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2002-2008 Greg Landrum and 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 //
11 #ifndef __RD_UTILS_H__
12 #define __RD_UTILS_H__
13 
14 #include "types.h"
15 #include <RDGeneral/Invariant.h>
17 #include <boost/random.hpp>
19 
20 namespace RDKit {
21 const int NUM_PRIMES_AVAIL =
22  1000; //!< the number of primes available and stored
24 
25 const int FILE_MAXLINE =
26  256; //!< an assumed maximum length for lines read from files
27 
28 //! \brief compute the product of the set of primes corresponding to the
29 //! values in an INT_VECT
30 double computeIntVectPrimesProduct(const INT_VECT &ring);
31 
32 //! floating point comparison with a tolerance
33 bool feq(double v1, double v2, double tol = 1e-4);
34 
35 typedef boost::minstd_rand rng_type;
36 typedef boost::uniform_int<> uniform_int;
37 typedef boost::uniform_real<> uniform_double;
38 typedef boost::variate_generator<rng_type &, uniform_int> int_source_type;
39 typedef boost::variate_generator<rng_type &, uniform_double> double_source_type;
40 
41 //! Optionally seed and return a reference to the global (Boost) random
42 //generator
43 rng_type &getRandomGenerator(int seed = -1);
44 
45 //! Return a random double value between 0.0 and 1.0
46 //! Optionally seed the random number generator
47 double getRandomVal(int seed = -1);
48 
49 //! return a reference to the global (Boost) random source
50 double_source_type &getDoubleRandomSource();
51 
52 template <class T>
53 unsigned int countSwapsToInterconvert(const T &ref, T probe) {
54  PRECONDITION(ref.size() == probe.size(), "size mismatch");
55  typename T::const_iterator refIt = ref.begin();
56  typename T::iterator probeIt = probe.begin();
57  typename T::iterator probeIt2;
58 
59  unsigned int nSwaps = 0;
60  while (refIt != ref.end()) {
61  if ((*probeIt) != (*refIt)) {
62  bool foundIt = false;
63  probeIt2 = probeIt;
64  while ((*probeIt2) != (*refIt) && probeIt2 != probe.end()) {
65  ++probeIt2;
66  }
67  if (probeIt2 != probe.end()) {
68  foundIt = true;
69  }
70  CHECK_INVARIANT(foundIt, "could not find probe element");
71 
72  std::swap(*probeIt, *probeIt2);
73  nSwaps++;
74  }
75  ++probeIt;
76  ++refIt;
77  }
78  return nSwaps;
79 }
80 }
81 
82 // contribution from dkoes
83 template <unsigned n>
84 inline double int_pow(double x) {
85  double half = int_pow<n / 2>(x);
86  if (n % 2 == 0) // even
87  return half * half;
88  else
89  return half * half * x;
90 }
91 
92 template <>
93 inline double int_pow<0>(double) {
94  return 1;
95 }
96 
97 template <>
98 inline double int_pow<1>(double x) {
99  return x; // this does a series of muls
100 }
101 
102 #endif
double computeIntVectPrimesProduct(const INT_VECT &ring)
compute the product of the set of primes corresponding to the values in an INT_VECT ...
boost::minstd_rand rng_type
Definition: utils.h:35
#define CHECK_INVARIANT(expr, mess)
Definition: Invariant.h:99
double getRandomVal(int seed=-1)
const int NUM_PRIMES_AVAIL
the number of primes available and stored
Definition: primes.h:14
double_source_type & getDoubleRandomSource()
return a reference to the global (Boost) random source
double int_pow< 0 >(double)
Definition: utils.h:93
std::vector< int > INT_VECT
Definition: types.h:190
boost::uniform_real uniform_double
Definition: utils.h:37
double int_pow< 1 >(double x)
Definition: utils.h:98
double int_pow(double x)
Definition: utils.h:84
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
const int FILE_MAXLINE
an assumed maximum length for lines read from files
Definition: utils.h:25
#define PRECONDITION(expr, mess)
Definition: Invariant.h:107
boost::variate_generator< rng_type &, uniform_double > double_source_type
Definition: utils.h:39
boost::variate_generator< rng_type &, uniform_int > int_source_type
Definition: utils.h:38
int firstThousandPrimes[NUM_PRIMES_AVAIL]
Definition: utils.h:23
unsigned int countSwapsToInterconvert(const T &ref, T probe)
Definition: utils.h:53
boost::uniform_int uniform_int
Definition: utils.h:36
bool feq(double v1, double v2, double tol=1e-4)
floating point comparison with a tolerance
rng_type & getRandomGenerator(int seed=-1)
Optionally seed and return a reference to the global (Boost) random.