C++QEDCore  v2 Milestone 10
a framework for simulating open quantum dynamics – core
Randomized.h
Go to the documentation of this file.
1 // Copyright András Vukics 2006–2014. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt)
3 // -*- C++ -*-
4 #ifndef CPPQEDCORE_UTILS_RANDOMIZED_H_INCLUDED
5 #define CPPQEDCORE_UTILS_RANDOMIZED_H_INCLUDED
6 
7 #include "RandomizedFwd.h"
8 
9 #include "ArrayTraits.h"
10 #include "ComplexExtensions.h"
11 #include "Exception.h"
12 
13 #include "core_config.h"
14 
15 #include <boost/range/algorithm/generate.hpp>
16 
17 #include <boost/bind.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/utility.hpp>
20 
21 #ifndef DO_NOT_USE_BOOST_SERIALIZATION
22 #include <boost/serialization/string.hpp>
23 #include <boost/serialization/split_member.hpp>
24 #endif // DO_NOT_USE_BOOST_SERIALIZATION
25 
26 
28 namespace randomized {
29 
31 {
32 public:
33  RNGStateParsingException(const std::string& tag) : cpputils::TaggedException(tag) {}
34 };
35 
36 
38 
43 class Randomized : private boost::noncopyable
44 {
45 public:
46  typedef boost::shared_ptr<Randomized> Ptr;
47 
48  virtual ~Randomized() {}
49 
50  double operator()() {return doSample();}
51 
52  const dcomp dcompRan();
53 
54 private:
55  virtual double doSample() = 0;
56 
57 #ifndef DO_NOT_USE_BOOST_SERIALIZATION
58 
59  friend class boost::serialization::access;
60 
61  template<class Archive>
62  void save(Archive& ar, const unsigned int /* version */) const
63  {
64  const std::string state(getState()), id(getImplID());
65  ar & state & id;
66  }
67 
68  template<class Archive>
69  void load(Archive& ar, const unsigned int /* version */)
70  {
71  std::string state, id;
72  ar & state & id;
73  if (id!=getImplID()) throw RNGStateParsingException("Wrong implementation ID, expected "+id+", found "+getImplID());
74  setState(state);
75  }
76 
77  BOOST_SERIALIZATION_SPLIT_MEMBER()
78 
79 #endif // DO_NOT_USE_BOOST_SERIALIZATION
80 
81  virtual const std::string getState() const = 0;
82  virtual void setState(const std::string&) = 0;
83 
84  virtual const std::string getImplID() const = 0;
85 
86 };
87 
88 
90 template<typename D>
91 inline
92 const D sample(Randomized::Ptr ran);
93 
95 template<>
96 inline
97 const double sample<double>(Randomized::Ptr ran)
98 {
99  return ran->operator()();
100 }
101 
103 template<>
104 inline
105 const dcomp sample<dcomp >(Randomized::Ptr ran)
106 {
107  return ran->dcompRan();
108 }
109 
110 
112 class Maker
113 {
114 public:
115  virtual const Randomized::Ptr operator()(unsigned long seed) const = 0;
116 
117  virtual ~Maker() {}
118 
119 };
120 
121 
123 class MakerGSL : public Maker
124 {
125 public:
126  const Randomized::Ptr operator()(unsigned long seed) const;
127 
128 };
129 
130 
131 
133 template<typename A>
134 const Randomized::Ptr fillWithRandom(A& data, Randomized::Ptr ran)
135 {
136  boost::generate(data,boost::bind(sample<typename cpputils::ElementType<A>::type>,ran));
137  return ran;
138 }
139 
140 
142 template<typename A>
143 const Randomized::Ptr fillWithRandom(A& data, unsigned long seed=1001ul, const Maker& maker=MakerGSL())
144 {
145  Randomized::Ptr ran(maker(seed));
146  return fillWithRandom(data,ran);
147 }
148 
149 
150 } // randomized
151 
152 #endif // CPPQEDCORE_UTILS_RANDOMIZED_H_INCLUDED
boost::shared_ptr< const Base > Ptr
Convenience typedef.
Definition: BinarySystem.h:17
the randomized-bundle
Definition: Randomized.h:28
template metafunction returning (by convention, as a member typedef type) the type of elements of the...
Definition: ArrayTraits.h:23
Class reporting also the “what-ness” of the exception.
Definition: Exception.h:25
const dcomp dcompRan()
sampling of a uniform distribution over unit square on the complex plane
Declarations of traits functions for adapting array types to generic functions.
const Randomized::Ptr fillWithRandom(A &data, Randomized::Ptr ran)
Fills an array with random data taking a Randomized as parameter.
Definition: Randomized.h:134
Factory class for Randomized types.
Definition: Randomized.h:112
Additional helpers for dcomp.
std::complex< double > dcomp
Double-precision complex number.
A common interface for random-number generators.
Definition: Randomized.h:43
Defines tentative base classes for the exception classes of the framework.
double operator()()
sampling of uniform distribution over the interval [0:1)
Definition: Randomized.h:50
Implements Maker by returning a class implementing the Randomized interface by GSL ...
Definition: Randomized.h:123