tomoto 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/ext/tomoto/ct.cpp +8 -4
  4. data/ext/tomoto/dmr.cpp +10 -4
  5. data/ext/tomoto/dt.cpp +13 -4
  6. data/ext/tomoto/extconf.rb +1 -1
  7. data/ext/tomoto/gdmr.cpp +14 -6
  8. data/ext/tomoto/hdp.cpp +9 -4
  9. data/ext/tomoto/hlda.cpp +9 -4
  10. data/ext/tomoto/hpa.cpp +9 -4
  11. data/ext/tomoto/lda.cpp +8 -4
  12. data/ext/tomoto/llda.cpp +8 -4
  13. data/ext/tomoto/mglda.cpp +11 -1
  14. data/ext/tomoto/pa.cpp +9 -4
  15. data/ext/tomoto/plda.cpp +8 -4
  16. data/ext/tomoto/slda.cpp +13 -5
  17. data/lib/tomoto/gdmr.rb +2 -2
  18. data/lib/tomoto/version.rb +1 -1
  19. data/vendor/EigenRand/EigenRand/Core.h +6 -1107
  20. data/vendor/EigenRand/EigenRand/Dists/Basic.h +490 -43
  21. data/vendor/EigenRand/EigenRand/Dists/Discrete.h +916 -285
  22. data/vendor/EigenRand/EigenRand/Dists/GammaPoisson.h +85 -36
  23. data/vendor/EigenRand/EigenRand/Dists/NormalExp.h +1038 -290
  24. data/vendor/EigenRand/EigenRand/EigenRand +2 -2
  25. data/vendor/EigenRand/EigenRand/Macro.h +4 -4
  26. data/vendor/EigenRand/EigenRand/MorePacketMath.h +54 -22
  27. data/vendor/EigenRand/EigenRand/MvDists/Multinomial.h +222 -0
  28. data/vendor/EigenRand/EigenRand/MvDists/MvNormal.h +492 -0
  29. data/vendor/EigenRand/EigenRand/PacketFilter.h +2 -2
  30. data/vendor/EigenRand/EigenRand/PacketRandomEngine.h +2 -2
  31. data/vendor/EigenRand/EigenRand/RandUtils.h +65 -11
  32. data/vendor/EigenRand/EigenRand/doc.h +142 -25
  33. data/vendor/EigenRand/LICENSE +1 -1
  34. data/vendor/EigenRand/README.md +109 -24
  35. data/vendor/tomotopy/README.kr.rst +27 -6
  36. data/vendor/tomotopy/README.rst +29 -8
  37. data/vendor/tomotopy/src/Labeling/FoRelevance.cpp +60 -12
  38. data/vendor/tomotopy/src/Labeling/FoRelevance.h +2 -2
  39. data/vendor/tomotopy/src/Labeling/Phraser.hpp +33 -21
  40. data/vendor/tomotopy/src/TopicModel/CT.h +8 -5
  41. data/vendor/tomotopy/src/TopicModel/CTModel.cpp +2 -6
  42. data/vendor/tomotopy/src/TopicModel/CTModel.hpp +29 -23
  43. data/vendor/tomotopy/src/TopicModel/DMR.h +33 -4
  44. data/vendor/tomotopy/src/TopicModel/DMRModel.cpp +2 -6
  45. data/vendor/tomotopy/src/TopicModel/DMRModel.hpp +231 -57
  46. data/vendor/tomotopy/src/TopicModel/DT.h +24 -5
  47. data/vendor/tomotopy/src/TopicModel/DTModel.cpp +2 -8
  48. data/vendor/tomotopy/src/TopicModel/DTModel.hpp +41 -28
  49. data/vendor/tomotopy/src/TopicModel/GDMR.h +31 -5
  50. data/vendor/tomotopy/src/TopicModel/GDMRModel.cpp +2 -7
  51. data/vendor/tomotopy/src/TopicModel/GDMRModel.hpp +211 -104
  52. data/vendor/tomotopy/src/TopicModel/HDP.h +11 -2
  53. data/vendor/tomotopy/src/TopicModel/HDPModel.cpp +2 -6
  54. data/vendor/tomotopy/src/TopicModel/HDPModel.hpp +52 -45
  55. data/vendor/tomotopy/src/TopicModel/HLDA.h +11 -2
  56. data/vendor/tomotopy/src/TopicModel/HLDAModel.cpp +2 -6
  57. data/vendor/tomotopy/src/TopicModel/HLDAModel.hpp +13 -16
  58. data/vendor/tomotopy/src/TopicModel/HPA.h +5 -2
  59. data/vendor/tomotopy/src/TopicModel/HPAModel.cpp +2 -6
  60. data/vendor/tomotopy/src/TopicModel/HPAModel.hpp +51 -21
  61. data/vendor/tomotopy/src/TopicModel/LDA.h +9 -2
  62. data/vendor/tomotopy/src/TopicModel/LDACVB0Model.hpp +8 -8
  63. data/vendor/tomotopy/src/TopicModel/LDAModel.cpp +2 -6
  64. data/vendor/tomotopy/src/TopicModel/LDAModel.hpp +70 -28
  65. data/vendor/tomotopy/src/TopicModel/LLDA.h +1 -2
  66. data/vendor/tomotopy/src/TopicModel/LLDAModel.cpp +2 -6
  67. data/vendor/tomotopy/src/TopicModel/LLDAModel.hpp +22 -12
  68. data/vendor/tomotopy/src/TopicModel/MGLDA.h +12 -3
  69. data/vendor/tomotopy/src/TopicModel/MGLDAModel.cpp +2 -10
  70. data/vendor/tomotopy/src/TopicModel/MGLDAModel.hpp +42 -19
  71. data/vendor/tomotopy/src/TopicModel/PA.h +9 -4
  72. data/vendor/tomotopy/src/TopicModel/PAModel.cpp +2 -6
  73. data/vendor/tomotopy/src/TopicModel/PAModel.hpp +48 -25
  74. data/vendor/tomotopy/src/TopicModel/PLDA.h +13 -2
  75. data/vendor/tomotopy/src/TopicModel/PLDAModel.cpp +2 -6
  76. data/vendor/tomotopy/src/TopicModel/PLDAModel.hpp +27 -19
  77. data/vendor/tomotopy/src/TopicModel/PT.h +12 -5
  78. data/vendor/tomotopy/src/TopicModel/PTModel.cpp +2 -3
  79. data/vendor/tomotopy/src/TopicModel/PTModel.hpp +29 -14
  80. data/vendor/tomotopy/src/TopicModel/SLDA.h +18 -6
  81. data/vendor/tomotopy/src/TopicModel/SLDAModel.cpp +2 -10
  82. data/vendor/tomotopy/src/TopicModel/SLDAModel.hpp +93 -43
  83. data/vendor/tomotopy/src/TopicModel/TopicModel.hpp +58 -23
  84. data/vendor/tomotopy/src/Utils/AliasMethod.hpp +6 -6
  85. data/vendor/tomotopy/src/Utils/Dictionary.h +11 -0
  86. data/vendor/tomotopy/src/Utils/SharedString.hpp +26 -1
  87. data/vendor/tomotopy/src/Utils/Trie.hpp +46 -21
  88. data/vendor/tomotopy/src/Utils/Utils.hpp +99 -14
  89. data/vendor/tomotopy/src/Utils/exception.h +1 -1
  90. data/vendor/tomotopy/src/Utils/math.h +5 -7
  91. data/vendor/tomotopy/src/Utils/serializer.hpp +329 -201
  92. data/vendor/tomotopy/src/Utils/text.hpp +8 -0
  93. data/vendor/tomotopy/src/Utils/tvector.hpp +49 -7
  94. metadata +9 -7
@@ -2,8 +2,8 @@
2
2
  * @file Basic.h
3
3
  * @author bab2min (bab2min@gmail.com)
4
4
  * @brief
5
- * @version 0.2.0
6
- * @date 2020-06-22
5
+ * @version 0.3.0
6
+ * @date 2020-10-07
7
7
  *
8
8
  * @copyright Copyright (c) 2020
9
9
  *
@@ -14,7 +14,7 @@
14
14
 
15
15
  namespace Eigen
16
16
  {
17
- namespace internal
17
+ namespace Rand
18
18
  {
19
19
  namespace constant
20
20
  {
@@ -22,89 +22,536 @@ namespace Eigen
22
22
  static constexpr double e = 2.7182818284590452;
23
23
  }
24
24
 
25
- template<typename Scalar, typename Rng>
26
- struct scalar_randbits_op : public scalar_base_rng<Scalar, Rng>
25
+ /**
26
+ * @brief Base class of all univariate random generators
27
+ *
28
+ * @tparam DerivedGen
29
+ * @tparam Scalar
30
+ */
31
+ template<typename DerivedGen, typename Scalar>
32
+ class GenBase
27
33
  {
28
- static_assert(std::is_integral<Scalar>::value, "randBits needs integral types.");
34
+ public:
35
+ /**
36
+ * @brief generate random values from its distribution
37
+ *
38
+ * @tparam Derived
39
+ * @tparam Urng
40
+ * @param rows the number of rows being generated
41
+ * @param cols the number of columns being generated
42
+ * @param urng c++11-style random number generator
43
+ * @return
44
+ * a random matrix expression with a shape `(rows, cols)`
45
+ */
46
+ template<typename Derived, typename Urng>
47
+ inline const CwiseNullaryOp<internal::scalar_rng_adaptor<DerivedGen&, Scalar, Urng>, const Derived>
48
+ generate(Index rows, Index cols, Urng&& urng)
49
+ {
50
+ return {
51
+ rows, cols, { std::forward<Urng>(urng), static_cast<DerivedGen&>(*this) }
52
+ };
53
+ }
29
54
 
30
- using scalar_base_rng<Scalar, Rng>::scalar_base_rng;
55
+ /**
56
+ * @brief generate random values from its distribution
57
+ *
58
+ * @tparam Derived
59
+ * @tparam Urng
60
+ * @param o an instance of any type of Eigen::DenseBase
61
+ * @param urng c++11-style random number generator
62
+ * @return
63
+ * a random matrix expression of the same shape as `o`
64
+ */
65
+ template<typename Derived, typename Urng>
66
+ inline const CwiseNullaryOp<internal::scalar_rng_adaptor<DerivedGen&, Scalar, Urng>, const Derived>
67
+ generateLike(const Derived& o, Urng&& urng)
68
+ {
69
+ return {
70
+ o.rows(), o.cols(), { std::forward<Urng>(urng), static_cast<DerivedGen&>(*this) }
71
+ };
72
+ }
73
+ };
74
+
75
+ /**
76
+ * @brief Base class of all multivariate random vector generators
77
+ *
78
+ * @tparam DerivedGen
79
+ * @tparam _Scalar
80
+ * @tparam Dim
81
+ */
82
+ template<typename DerivedGen, typename _Scalar, Index Dim>
83
+ class MvVecGenBase
84
+ {
85
+ public:
86
+ /**
87
+ * @brief returns the dimensions of vectors to be generated
88
+ */
89
+ Index dims() const { return static_cast<DerivedGen&>(*this).dims(); }
31
90
 
32
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() () const
91
+ /**
92
+ * @brief generates multiple samples at once
93
+ *
94
+ * @tparam Urng
95
+ * @param urng c++11-style random number generator
96
+ * @param samples the number of samples to be generated
97
+ * @return
98
+ * a random matrix with a shape `(dim, samples)` which is consist of `samples` random vector columns
99
+ */
100
+ template<typename Urng>
101
+ inline Matrix<_Scalar, Dim, -1> generate(Urng&& urng, Index samples)
33
102
  {
34
- return pfirst(this->rng());
103
+ return static_cast<DerivedGen&>(*this).generatr(std::forward<Urng>(urng), samples);
35
104
  }
36
105
 
37
- template<typename Packet>
38
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp() const
106
+ /**
107
+ * @brief generates one sample
108
+ *
109
+ * @tparam Urng
110
+ * @param urng c++11-style random number generator
111
+ * @return a random vector with a shape `(dim,)`
112
+ */
113
+ template<typename Urng>
114
+ inline Matrix<_Scalar, Dim, 1> generate(Urng&& urng)
39
115
  {
40
- using RUtils = RawbitsMaker<Packet, Rng>;
41
- return RUtils{}.rawbits(this->rng);
116
+ return static_cast<DerivedGen&>(*this).generatr(std::forward<Urng>(urng));
42
117
  }
43
118
  };
44
119
 
45
- template<typename Scalar, typename Urng>
46
- struct functor_traits<scalar_randbits_op<Scalar, Urng> >
120
+ /**
121
+ * @brief Base class of all multivariate random matrix generators
122
+ *
123
+ * @tparam DerivedGen
124
+ * @tparam _Scalar
125
+ * @tparam Dim
126
+ */
127
+ template<typename DerivedGen, typename _Scalar, Index Dim>
128
+ class MvMatGenBase
47
129
  {
48
- enum { Cost = HugeCost, PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = false };
130
+ public:
131
+ /**
132
+ * @brief returns the dimensions of matrices to be generated
133
+ */
134
+ Index dims() const { return static_cast<DerivedGen&>(*this).dims(); }
135
+
136
+ /**
137
+ * @brief generates multiple samples at once
138
+ *
139
+ * @tparam Urng
140
+ * @param urng c++11-style random number generator
141
+ * @param samples the number of samples to be generated
142
+ * @return
143
+ * a random matrix with a shape `(dim, dim * samples)` which is `samples` random matrices concatenated along the column axis
144
+ */
145
+ template<typename Urng>
146
+ inline Matrix<_Scalar, Dim, -1> generate(Urng&& urng, Index samples)
147
+ {
148
+ return static_cast<DerivedGen&>(*this).generate(std::forward<Urng>(urng), samples);
149
+ }
150
+
151
+ /**
152
+ * @brief generates one sample
153
+ *
154
+ * @tparam Urng
155
+ * @param urng c++11-style random number generator
156
+ * @return a random matrix with a shape `(dim, dim)`
157
+ */
158
+ template<typename Urng>
159
+ inline Matrix<_Scalar, Dim, Dim> generate(Urng&& urng)
160
+ {
161
+ return static_cast<DerivedGen&>(*this).generate(std::forward<Urng>(urng));
162
+ }
49
163
  };
50
164
 
165
+ template<Index _alignment=0>
166
+ class CacheStore
167
+ {
168
+ protected:
169
+ enum { max_size = sizeof(internal::find_best_packet<float, -1>::type) };
170
+ int8_t raw_data[max_size + _alignment - 1] = { 0, };
171
+ void* aligned_ptr;
172
+
173
+ public:
174
+ CacheStore()
175
+ {
176
+ aligned_ptr = (void*)((((size_t)raw_data + _alignment - 1) / _alignment) * _alignment);
177
+ }
178
+
179
+ CacheStore(const CacheStore& c)
180
+ {
181
+ std::copy(c.raw_data, c.raw_data + max_size, raw_data);
182
+ aligned_ptr = (void*)((((size_t)raw_data + _alignment - 1) / _alignment) * _alignment);
183
+ }
184
+
185
+ CacheStore(CacheStore&& c)
186
+ {
187
+ std::copy(c.raw_data, c.raw_data + max_size, raw_data);
188
+ aligned_ptr = (void*)((((size_t)raw_data + _alignment - 1) / _alignment) * _alignment);
189
+ }
190
+
191
+ template<typename Ty>
192
+ Ty& get()
193
+ {
194
+ return *(Ty*)aligned_ptr;
195
+ }
51
196
 
52
- template<typename Scalar, typename Rng>
53
- struct scalar_uniform_real_op : public scalar_base_rng<Scalar, Rng>
197
+ template<typename Ty>
198
+ const Ty& get() const
199
+ {
200
+ return *(const Ty*)aligned_ptr;
201
+ }
202
+ };
203
+
204
+ template<>
205
+ class CacheStore<0>
54
206
  {
55
- static_assert(std::is_floating_point<Scalar>::value, "uniformReal needs floating point types.");
207
+ protected:
208
+ enum { max_size = sizeof(internal::find_best_packet<float, -1>::type) };
209
+ int8_t raw_data[max_size] = { 0, };
56
210
 
57
- using scalar_base_rng<Scalar, Rng>::scalar_base_rng;
211
+ public:
212
+ CacheStore()
213
+ {
214
+ }
58
215
 
59
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() () const
216
+ CacheStore(const CacheStore& c)
60
217
  {
61
- return bit_scalar<Scalar>{}.to_ur(pfirst(this->rng()));
218
+ std::copy(c.raw_data, c.raw_data + max_size, raw_data);
62
219
  }
63
220
 
64
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar nzur_scalar() const
221
+ CacheStore(CacheStore&& c)
65
222
  {
66
- return bit_scalar<Scalar>{}.to_nzur(pfirst(this->rng()));
223
+ std::copy(c.raw_data, c.raw_data + max_size, raw_data);
67
224
  }
68
225
 
69
- template<typename Packet>
70
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp() const
226
+ template<typename Ty>
227
+ Ty& get()
71
228
  {
72
- using RUtils = RandUtils<Packet, Rng>;
73
- return RUtils{}.uniform_real(this->rng);
229
+ return *(Ty*)raw_data;
230
+ }
231
+
232
+ template<typename Ty>
233
+ const Ty& get() const
234
+ {
235
+ return *(const Ty*)raw_data;
74
236
  }
75
237
  };
76
238
 
77
- template<typename Scalar, typename Urng>
78
- struct functor_traits<scalar_uniform_real_op<Scalar, Urng> >
239
+ using OptCacheStore = CacheStore<EIGEN_MAX_ALIGN_BYTES>;
240
+
241
+ /**
242
+ * @brief Generator of random bits for integral scalars
243
+ *
244
+ * @tparam _Scalar any integral type
245
+ */
246
+ template<typename _Scalar>
247
+ class RandbitsGen : public GenBase<RandbitsGen<_Scalar>, _Scalar>
79
248
  {
80
- enum { Cost = HugeCost, PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = false };
249
+ static_assert(std::is_integral<_Scalar>::value, "randBits needs integral types.");
250
+
251
+ public:
252
+ using Scalar = _Scalar;
253
+
254
+ template<typename Rng>
255
+ EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
256
+ {
257
+ using namespace Eigen::internal;
258
+ return pfirst(std::forward<Rng>(rng)());
259
+ }
260
+
261
+ template<typename Packet, typename Rng>
262
+ EIGEN_STRONG_INLINE const Packet packetOp(Rng&& rng)
263
+ {
264
+ using namespace Eigen::internal;
265
+ using RUtils = RawbitsMaker<Packet, Rng>;
266
+ return RUtils{}.rawbits(std::forward<Rng>(rng));
267
+ }
268
+ };
269
+
270
+ /**
271
+ * @brief Generator of reals in a range `[-1, 1]`
272
+ *
273
+ * @tparam _Scalar any real type
274
+ */
275
+ template<typename _Scalar>
276
+ class BalancedGen : public GenBase<BalancedGen<_Scalar>, _Scalar>
277
+ {
278
+ static_assert(std::is_floating_point<_Scalar>::value, "balanced needs floating point types.");
279
+
280
+ public:
281
+ using Scalar = _Scalar;
282
+
283
+ template<typename Rng>
284
+ EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
285
+ {
286
+ using namespace Eigen::internal;
287
+ return ((_Scalar)((int32_t)pfirst(std::forward<Rng>(rng)()) & 0x7FFFFFFF) / 0x7FFFFFFF) * 2 - 1;
288
+ }
289
+
290
+ template<typename Packet, typename Rng>
291
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(Rng&& rng)
292
+ {
293
+ using namespace Eigen::internal;
294
+ using RUtils = RandUtils<Packet, Rng>;
295
+ return RUtils{}.balanced(std::forward<Rng>(rng));
296
+ }
81
297
  };
82
298
 
83
- template<typename Scalar, typename Rng>
84
- struct scalar_balanced_op : public scalar_base_rng<Scalar, Rng>
299
+ /**
300
+ * @brief Generator of reals in a range `[0, 1)`
301
+ *
302
+ * @tparam _Scalar any real type
303
+ */
304
+ template<typename _Scalar>
305
+ class UniformRealGen : public GenBase<UniformRealGen<_Scalar>, _Scalar>
85
306
  {
86
- static_assert(std::is_floating_point<Scalar>::value, "balanced needs floating point types.");
307
+ static_assert(std::is_floating_point<_Scalar>::value, "uniformReal needs floating point types.");
87
308
 
88
- using scalar_base_rng<Scalar, Rng>::scalar_base_rng;
309
+ public:
310
+ using Scalar = _Scalar;
89
311
 
90
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() () const
312
+ template<typename Rng>
313
+ EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
91
314
  {
92
- return ((Scalar)((int32_t)pfirst(this->rng()) & 0x7FFFFFFF) / 0x7FFFFFFF) * 2 - 1;
315
+ using namespace Eigen::internal;
316
+ return bit_scalar<_Scalar>{}.to_ur(pfirst(std::forward<Rng>(rng)()));
93
317
  }
94
318
 
95
- template<typename Packet>
96
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp() const
319
+ template<typename Rng>
320
+ EIGEN_STRONG_INLINE const _Scalar nzur_scalar(Rng&& rng)
97
321
  {
322
+ using namespace Eigen::internal;
323
+ return bit_scalar<_Scalar>{}.to_nzur(pfirst(std::forward<Rng>(rng)()));
324
+ }
325
+
326
+ template<typename Packet, typename Rng>
327
+ EIGEN_STRONG_INLINE const Packet packetOp(Rng&& rng)
328
+ {
329
+ using namespace Eigen::internal;
98
330
  using RUtils = RandUtils<Packet, Rng>;
99
- return RUtils{}.balanced(this->rng);
331
+ return RUtils{}.uniform_real(std::forward<Rng>(rng));
100
332
  }
101
333
  };
102
334
 
103
- template<typename Scalar, typename Urng>
104
- struct functor_traits<scalar_balanced_op<Scalar, Urng> >
335
+
336
+ /**
337
+ * @brief Generator of Bernoulli distribution
338
+ *
339
+ * @tparam _Scalar
340
+ */
341
+ template<typename _Scalar>
342
+ class BernoulliGen : public GenBase<BernoulliGen<_Scalar>, _Scalar>
105
343
  {
106
- enum { Cost = HugeCost, PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = false };
344
+ uint32_t p;
345
+ public:
346
+ using Scalar = _Scalar;
347
+
348
+ BernoulliGen(double _p = 0.5)
349
+ {
350
+ eigen_assert(0 <= _p && _p <= 1 );
351
+ p = (uint32_t)(_p * 0x80000000);
352
+ }
353
+
354
+ BernoulliGen(const BernoulliGen&) = default;
355
+ BernoulliGen(BernoulliGen&&) = default;
356
+
357
+ BernoulliGen& operator=(const BernoulliGen&) = default;
358
+ BernoulliGen& operator=(BernoulliGen&&) = default;
359
+
360
+ template<typename Rng>
361
+ EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
362
+ {
363
+ using namespace Eigen::internal;
364
+ return (((uint32_t)pfirst(std::forward<Rng>(rng)()) & 0x7FFFFFFF) < p) ? 1 : 0;
365
+ }
366
+
367
+ template<typename Packet, typename Rng>
368
+ EIGEN_STRONG_INLINE const Packet packetOp(Rng&& rng)
369
+ {
370
+ using namespace Eigen::internal;
371
+ using IPacket = decltype(reinterpret_to_int(std::declval<Packet>()));
372
+ using RUtils = RawbitsMaker<IPacket, Rng>;
373
+ auto one = pset1<Packet>(1);
374
+ auto zero = pset1<Packet>(0);
375
+ auto r = RUtils{}.rawbits(std::forward<Rng>(rng));
376
+ r = pand(r, pset1<IPacket>(0x7FFFFFFF));
377
+ return pblendv(pcmplt(r, pset1<IPacket>(p)), one, zero);
378
+ }
107
379
  };
380
+
381
+
382
+ template<typename Derived, typename Urng>
383
+ using RandBitsType = CwiseNullaryOp<internal::scalar_rng_adaptor<RandbitsGen<typename Derived::Scalar>, typename Derived::Scalar, Urng, true>, const Derived>;
384
+
385
+ /**
386
+ * @brief generates integers with random bits
387
+ *
388
+ * @tparam Derived
389
+ * @tparam Urng
390
+ * @param rows the number of rows being generated
391
+ * @param cols the number of columns being generated
392
+ * @param urng c++11-style random number generator
393
+ * @return a random matrix expression with a shape (`rows`, `cols`)
394
+ *
395
+ * @see Eigen::Rand::RandbitsGen
396
+ */
397
+ template<typename Derived, typename Urng>
398
+ inline const RandBitsType<Derived, Urng>
399
+ randBits(Index rows, Index cols, Urng&& urng)
400
+ {
401
+ return {
402
+ rows, cols, { std::forward<Urng>(urng) }
403
+ };
404
+ }
405
+
406
+ /**
407
+ * @brief generates integers with random bits
408
+ *
409
+ * @tparam Derived
410
+ * @tparam Urng
411
+ * @param o an instance of any type of Eigen::DenseBase
412
+ * @param urng c++11-style random number generator
413
+ * @return a random matrix expression of the same shape as `o`
414
+ *
415
+ * @see Eigen::Rand::RandbitsGen
416
+ */
417
+ template<typename Derived, typename Urng>
418
+ inline const RandBitsType<Derived, Urng>
419
+ randBitsLike(Derived& o, Urng&& urng)
420
+ {
421
+ return {
422
+ o.rows(), o.cols(), { std::forward<Urng>(urng) }
423
+ };
424
+ }
425
+
426
+ template<typename Derived, typename Urng>
427
+ using BalancedType = CwiseNullaryOp<internal::scalar_rng_adaptor<BalancedGen<typename Derived::Scalar>, typename Derived::Scalar, Urng, true>, const Derived>;
428
+
429
+ /**
430
+ * @brief generates reals in a range `[-1, 1]`
431
+ *
432
+ * @tparam Derived a type of Eigen::DenseBase
433
+ * @tparam Urng
434
+ * @param rows the number of rows being generated
435
+ * @param cols the number of columns being generated
436
+ * @param urng c++11-style random number generator
437
+ * @return a random matrix expression with a shape (`rows`, `cols`)
438
+ *
439
+ * @see Eigen::Rand::BalancedGen
440
+ */
441
+ template<typename Derived, typename Urng>
442
+ inline const BalancedType<Derived, Urng>
443
+ balanced(Index rows, Index cols, Urng&& urng)
444
+ {
445
+ return {
446
+ rows, cols, { std::forward<Urng>(urng) }
447
+ };
448
+ }
449
+
450
+ /**
451
+ * @brief generates reals in a range `[-1, 1]`
452
+ *
453
+ * @tparam Derived
454
+ * @tparam Urng
455
+ * @param o an instance of any type of Eigen::DenseBase
456
+ * @param urng c++11-style random number generator
457
+ * @return a random matrix expression of the same shape as `o`
458
+ *
459
+ * @see Eigen::Rand::BalancedGen
460
+ */
461
+ template<typename Derived, typename Urng>
462
+ inline const BalancedType<Derived, Urng>
463
+ balancedLike(const Derived& o, Urng&& urng)
464
+ {
465
+ return {
466
+ o.rows(), o.cols(), { std::forward<Urng>(urng) }
467
+ };
468
+ }
469
+
470
+ template<typename Derived, typename Urng>
471
+ using UniformRealType = CwiseNullaryOp<internal::scalar_rng_adaptor<UniformRealGen<typename Derived::Scalar>, typename Derived::Scalar, Urng, true>, const Derived>;
472
+
473
+ /**
474
+ * @brief generates reals in a range `[0, 1)`
475
+ *
476
+ * @tparam Derived a type of Eigen::DenseBase
477
+ * @tparam Urng
478
+ * @param rows the number of rows being generated
479
+ * @param cols the number of columns being generated
480
+ * @param urng c++11-style random number generator
481
+ * @return a random matrix expression with a shape (`rows`, `cols`)
482
+ *
483
+ * @see Eigen::Rand::UniformRealGen
484
+ */
485
+ template<typename Derived, typename Urng>
486
+ inline const UniformRealType<Derived, Urng>
487
+ uniformReal(Index rows, Index cols, Urng&& urng)
488
+ {
489
+ return {
490
+ rows, cols, { std::forward<Urng>(urng) }
491
+ };
492
+ }
493
+
494
+ /**
495
+ * @brief generates reals in a range `[0, 1)`
496
+ *
497
+ * @tparam Derived
498
+ * @tparam Urng
499
+ * @param o an instance of any type of Eigen::DenseBase
500
+ * @param urng c++11-style random number generator
501
+ * @return a random matrix expression of the same shape as `o`
502
+ *
503
+ * @see Eigen::Rand::UniformRealGen
504
+ */
505
+ template<typename Derived, typename Urng>
506
+ inline const UniformRealType<Derived, Urng>
507
+ uniformRealLike(Derived& o, Urng&& urng)
508
+ {
509
+ return {
510
+ o.rows(), o.cols(), { std::forward<Urng>(urng) }
511
+ };
512
+ }
513
+
514
+ template<typename Derived, typename Urng>
515
+ using BernoulliType = CwiseNullaryOp<internal::scalar_rng_adaptor<BernoulliGen<typename Derived::Scalar>, typename Derived::Scalar, Urng, true>, const Derived>;
516
+
517
+ /**
518
+ * @brief generates 1 with probability `p` and 0 with probability `1 - p`
519
+ *
520
+ * @tparam Derived
521
+ * @tparam Urng
522
+ * @param rows the number of rows being generated
523
+ * @param cols the number of columns being generated
524
+ * @param urng c++11-style random number generator
525
+ * @param p a probability of generating 1
526
+ * @return a random matrix expression with a shape (`rows`, `cols`)
527
+ */
528
+ template<typename Derived, typename Urng>
529
+ inline const BernoulliType<Derived, Urng>
530
+ bernoulli(Index rows, Index cols, Urng&& urng, double p = 0.5)
531
+ {
532
+ return {
533
+ rows, cols, { std::forward<Urng>(urng), BernoulliGen<typename Derived::Scalar>{ p } }
534
+ };
535
+ }
536
+
537
+ /**
538
+ * @brief generates 1 with probability `p` and 0 with probability `1 - p`
539
+ *
540
+ * @tparam Derived
541
+ * @tparam Urng
542
+ * @param o an instance of any type of Eigen::DenseBase
543
+ * @param urng c++11-style random number generator
544
+ * @param p a probability of generating 1
545
+ * @return a random matrix expression of the same shape as `o`
546
+ */
547
+ template<typename Derived, typename Urng>
548
+ inline const BernoulliType<Derived, Urng>
549
+ bernoulli(Derived& o, Urng&& urng, double p = 0.5)
550
+ {
551
+ return {
552
+ o.rows(), o.cols(), { std::forward<Urng>(urng), BernoulliGen<typename Derived::Scalar>{ p } }
553
+ };
554
+ }
108
555
  }
109
556
  }
110
557