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