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