1 | // Copyright (C) 2013-2015 Conrad Sanderson |
---|
2 | // Copyright (C) 2013-2015 NICTA (www.nicta.com.au) |
---|
3 | // |
---|
4 | // This Source Code Form is subject to the terms of the Mozilla Public |
---|
5 | // License, v. 2.0. If a copy of the MPL was not distributed with this |
---|
6 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
---|
7 | |
---|
8 | |
---|
9 | //! \addtogroup arma_rng_cxx11 |
---|
10 | //! @{ |
---|
11 | |
---|
12 | |
---|
13 | #if defined(ARMA_USE_CXX11) |
---|
14 | |
---|
15 | |
---|
16 | class arma_rng_cxx11 |
---|
17 | { |
---|
18 | public: |
---|
19 | |
---|
20 | typedef std::mt19937_64::result_type seed_type; |
---|
21 | |
---|
22 | inline void set_seed(const seed_type val); |
---|
23 | |
---|
24 | arma_inline int randi_val(); |
---|
25 | arma_inline double randu_val(); |
---|
26 | arma_inline double randn_val(); |
---|
27 | |
---|
28 | template<typename eT> |
---|
29 | arma_inline void randn_dual_val(eT& out1, eT& out2); |
---|
30 | |
---|
31 | template<typename eT> |
---|
32 | inline void randi_fill(eT* mem, const uword N, const int a, const int b); |
---|
33 | |
---|
34 | inline static int randi_max_val(); |
---|
35 | |
---|
36 | template<typename eT> |
---|
37 | inline void randg_fill(eT* mem, const uword N, const double a, const double b); |
---|
38 | |
---|
39 | |
---|
40 | private: |
---|
41 | |
---|
42 | arma_aligned std::mt19937_64 engine; // typedef for std::mersenne_twister_engine with preset parameters |
---|
43 | |
---|
44 | arma_aligned std::uniform_int_distribution<int> i_distr; // by default uses a=0, b=std::numeric_limits<int>::max() |
---|
45 | |
---|
46 | arma_aligned std::uniform_real_distribution<double> u_distr; // by default uses [0,1) interval |
---|
47 | |
---|
48 | arma_aligned std::normal_distribution<double> n_distr; // by default uses mean=0.0 and stddev=1.0 |
---|
49 | }; |
---|
50 | |
---|
51 | |
---|
52 | |
---|
53 | inline |
---|
54 | void |
---|
55 | arma_rng_cxx11::set_seed(const arma_rng_cxx11::seed_type val) |
---|
56 | { |
---|
57 | engine.seed(val); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | arma_inline |
---|
63 | int |
---|
64 | arma_rng_cxx11::randi_val() |
---|
65 | { |
---|
66 | return i_distr(engine); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | arma_inline |
---|
72 | double |
---|
73 | arma_rng_cxx11::randu_val() |
---|
74 | { |
---|
75 | return u_distr(engine); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | arma_inline |
---|
81 | double |
---|
82 | arma_rng_cxx11::randn_val() |
---|
83 | { |
---|
84 | return n_distr(engine); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | template<typename eT> |
---|
90 | arma_inline |
---|
91 | void |
---|
92 | arma_rng_cxx11::randn_dual_val(eT& out1, eT& out2) |
---|
93 | { |
---|
94 | out1 = eT( n_distr(engine) ); |
---|
95 | out2 = eT( n_distr(engine) ); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | template<typename eT> |
---|
101 | inline |
---|
102 | void |
---|
103 | arma_rng_cxx11::randi_fill(eT* mem, const uword N, const int a, const int b) |
---|
104 | { |
---|
105 | std::uniform_int_distribution<int> i_distr(a, b); |
---|
106 | |
---|
107 | for(uword i=0; i<N; ++i) |
---|
108 | { |
---|
109 | mem[i] = eT(i_distr(engine)); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | inline |
---|
116 | int |
---|
117 | arma_rng_cxx11::randi_max_val() |
---|
118 | { |
---|
119 | return std::numeric_limits<int>::max(); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | template<typename eT> |
---|
125 | inline |
---|
126 | void |
---|
127 | arma_rng_cxx11::randg_fill(eT* mem, const uword N, const double a, const double b) |
---|
128 | { |
---|
129 | std::gamma_distribution<double> g_distr(a,b); |
---|
130 | |
---|
131 | for(uword i=0; i<N; ++i) |
---|
132 | { |
---|
133 | mem[i] = eT(g_distr(engine)); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | #endif |
---|
139 | |
---|
140 | |
---|
141 | //! @} |
---|