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