random.hpp
Go to the documentation of this file.
1/*!
2 *
3 *
4 * \brief Generation of random variates on cpu
5 *
6 * \author O. Krause
7 * \date 2017
8 *
9 *
10 * \par Copyright 1995-2015 Shark Development Team
11 *
12 * <BR><HR>
13 * This file is part of Shark.
14 * <http://image.diku.dk/shark/>
15 *
16 * Shark is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Lesser General Public License as published
18 * by the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * Shark is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30#ifndef REMORA_KERNELS_DEFAULT_RANDOM_HPP
31#define REMORA_KERNELS_DEFAULT_RANDOM_HPP
32
33#include <random>
34#include <cmath>
35
36namespace remora{ namespace bindings{
37template<class V, class Rng>
38void generate_normal(
39 vector_expression<V, cpu_tag>& v,
40 Rng& rng,
41 typename V::value_type mean,
42 typename V::value_type variance
43) {
44 std::normal_distribution<typename V::value_type> dist(mean,std::sqrt(variance));
45 for(auto& val: v())
46 val = dist(rng);
47}
48
49template<class M, class Rng>
50void generate_normal(
51 matrix_expression<M, cpu_tag>& m,
52 Rng& rng,
53 typename M::value_type mean,
54 typename M::value_type variance
55) {
56 std::normal_distribution<typename M::value_type> dist(mean,std::sqrt(variance));
57 std::size_t size = M::orientation::index_M(m().size1(),m().size2());
58 for(std::size_t i = 0; i != size; ++i){
59 auto end = m().major_end(i);
60 for(auto pos = m().major_begin(i);pos != end; ++pos){
61 *pos = dist(rng);
62 }
63 }
64}
65
66template<class V, class Rng>
67void generate_uniform(
68 vector_expression<V, cpu_tag>& v,
69 Rng& rng,
70 typename V::value_type low,
71 typename V::value_type high
72) {
73 std::uniform_real_distribution<typename V::value_type> dist(low,high);
74 for(auto& val: v())
75 val = dist(rng);
76}
77
78template<class M, class Rng>
79void generate_uniform(
80 matrix_expression<M, cpu_tag>& m,
81 Rng& rng,
82 typename M::value_type low,
83 typename M::value_type high
84) {
85 std::uniform_real_distribution<typename M::value_type> dist(low,high);
86 std::size_t size = M::orientation::index_M(m().size1(),m().size2());
87 for(std::size_t i = 0; i != size; ++i){
88 auto end = m().major_end(i);
89 for(auto pos = m().major_begin(i);pos != end; ++pos){
90 *pos = dist(rng);
91 }
92 }
93}
94
95template<class V, class Rng>
96void generate_discrete(
97 vector_expression<V, cpu_tag>& v,
98 Rng& rng,
99 int low,
100 int high
101) {
102 std::uniform_int_distribution<int> dist(low,high);
103 for(auto& val: v())
104 val = dist(rng);
105}
106
107template<class M, class Rng>
108void generate_discrete(
109 matrix_expression<M, cpu_tag>& m,
110 Rng& rng,
111 int low,
112 int high
113) {
114 std::uniform_int_distribution<int> dist(low,high);
115 std::size_t size = M::orientation::index_M(m().size1(),m().size2());
116 for(std::size_t i = 0; i != size; ++i){
117 auto end = m().major_end(i);
118 for(auto pos = m().major_begin(i);pos != end; ++pos){
119 *pos = dist(rng);
120 }
121 }
122}
123
124}}
125#endif