12 #ifndef EIGENRAND_DISTS_BASIC_H
13 #define EIGENRAND_DISTS_BASIC_H
21 static constexpr
double pi = 3.1415926535897932;
22 static constexpr
double e = 2.7182818284590452;
31 template<
typename DerivedGen,
typename Scalar>
46 template<
typename Derived,
typename Urng>
47 inline const CwiseNullaryOp<internal::scalar_rng_adaptor<DerivedGen&, Scalar, Urng>,
const Derived>
51 rows, cols, { std::forward<Urng>(urng),
static_cast<DerivedGen&
>(*this) }
65 template<
typename Derived,
typename Urng>
66 inline const CwiseNullaryOp<internal::scalar_rng_adaptor<DerivedGen&, Scalar, Urng>,
const Derived>
70 o.rows(), o.cols(), { std::forward<Urng>(urng),
static_cast<DerivedGen&
>(*this) }
82 template<
typename DerivedGen,
typename _Scalar, Index Dim>
89 Index
dims()
const {
return static_cast<DerivedGen&
>(*this).dims(); }
100 template<
typename Urng>
101 inline Matrix<_Scalar, Dim, -1>
generate(Urng&& urng, Index samples)
103 return static_cast<DerivedGen&
>(*this).generatr(std::forward<Urng>(urng), samples);
113 template<
typename Urng>
114 inline Matrix<_Scalar, Dim, 1>
generate(Urng&& urng)
116 return static_cast<DerivedGen&
>(*this).generatr(std::forward<Urng>(urng));
127 template<
typename DerivedGen,
typename _Scalar, Index Dim>
134 Index
dims()
const {
return static_cast<DerivedGen&
>(*this).dims(); }
145 template<
typename Urng>
146 inline Matrix<_Scalar, Dim, -1>
generate(Urng&& urng, Index samples)
148 return static_cast<DerivedGen&
>(*this).generate(std::forward<Urng>(urng), samples);
158 template<
typename Urng>
159 inline Matrix<_Scalar, Dim, Dim>
generate(Urng&& urng)
161 return static_cast<DerivedGen&
>(*this).generate(std::forward<Urng>(urng));
165 template<Index _alignment=0>
169 enum { max_size =
sizeof(internal::find_best_packet<float, -1>::type) };
170 int8_t raw_data[max_size + _alignment - 1] = { 0, };
176 aligned_ptr = (
void*)((((
size_t)raw_data + _alignment - 1) / _alignment) * _alignment);
179 CacheStore(
const CacheStore& c)
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);
185 CacheStore(CacheStore&& c)
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);
191 template<
typename Ty>
194 return *(Ty*)aligned_ptr;
197 template<
typename Ty>
198 const Ty& get()
const
200 return *(
const Ty*)aligned_ptr;
208 enum { max_size =
sizeof(internal::find_best_packet<float, -1>::type) };
209 int8_t raw_data[max_size] = { 0, };
216 CacheStore(
const CacheStore& c)
218 std::copy(c.raw_data, c.raw_data + max_size, raw_data);
221 CacheStore(CacheStore&& c)
223 std::copy(c.raw_data, c.raw_data + max_size, raw_data);
226 template<
typename Ty>
229 return *(Ty*)raw_data;
232 template<
typename Ty>
233 const Ty& get()
const
235 return *(
const Ty*)raw_data;
239 using OptCacheStore = CacheStore<EIGEN_MAX_ALIGN_BYTES>;
246 template<
typename _Scalar>
249 static_assert(std::is_integral<_Scalar>::value,
"randBits needs integral types.");
252 using Scalar = _Scalar;
254 template<
typename Rng>
255 EIGEN_STRONG_INLINE
const _Scalar operator() (Rng&& rng)
257 using namespace Eigen::internal;
258 return pfirst(std::forward<Rng>(rng)());
261 template<
typename Packet,
typename Rng>
262 EIGEN_STRONG_INLINE
const Packet packetOp(Rng&& rng)
264 using namespace Eigen::internal;
265 using RUtils = RawbitsMaker<Packet, Rng>;
266 return RUtils{}.rawbits(std::forward<Rng>(rng));
275 template<
typename _Scalar>
278 static_assert(std::is_floating_point<_Scalar>::value,
"balanced needs floating point types.");
281 using Scalar = _Scalar;
283 template<
typename Rng>
284 EIGEN_STRONG_INLINE
const _Scalar operator() (Rng&& rng)
286 using namespace Eigen::internal;
287 return ((_Scalar)((int32_t)pfirst(std::forward<Rng>(rng)()) & 0x7FFFFFFF) / 0x7FFFFFFF) * 2 - 1;
290 template<
typename Packet,
typename Rng>
291 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Packet packetOp(Rng&& rng)
293 using namespace Eigen::internal;
294 using RUtils = RandUtils<Packet, Rng>;
295 return RUtils{}.balanced(std::forward<Rng>(rng));
304 template<
typename _Scalar>
307 static_assert(std::is_floating_point<_Scalar>::value,
"uniformReal needs floating point types.");
310 using Scalar = _Scalar;
312 template<
typename Rng>
313 EIGEN_STRONG_INLINE
const _Scalar operator() (Rng&& rng)
315 using namespace Eigen::internal;
316 return bit_scalar<_Scalar>{}.to_ur(pfirst(std::forward<Rng>(rng)()));
319 template<
typename Rng>
320 EIGEN_STRONG_INLINE
const _Scalar nzur_scalar(Rng&& rng)
322 using namespace Eigen::internal;
323 return bit_scalar<_Scalar>{}.to_nzur(pfirst(std::forward<Rng>(rng)()));
326 template<
typename Packet,
typename Rng>
327 EIGEN_STRONG_INLINE
const Packet packetOp(Rng&& rng)
329 using namespace Eigen::internal;
330 using RUtils = RandUtils<Packet, Rng>;
331 return RUtils{}.uniform_real(std::forward<Rng>(rng));
336 template<
typename Derived,
typename Urng>
337 using RandBitsType = CwiseNullaryOp<internal::scalar_rng_adaptor<RandbitsGen<typename Derived::Scalar>,
typename Derived::Scalar, Urng,
true>,
const Derived>;
351 template<
typename Derived,
typename Urng>
352 inline const RandBitsType<Derived, Urng>
356 rows, cols, { std::forward<Urng>(urng) }
371 template<
typename Derived,
typename Urng>
372 inline const RandBitsType<Derived, Urng>
376 o.rows(), o.cols(), { std::forward<Urng>(urng) }
380 template<
typename Derived,
typename Urng>
381 using BalancedType = CwiseNullaryOp<internal::scalar_rng_adaptor<BalancedGen<typename Derived::Scalar>,
typename Derived::Scalar, Urng,
true>,
const Derived>;
395 template<
typename Derived,
typename Urng>
396 inline const BalancedType<Derived, Urng>
400 rows, cols, { std::forward<Urng>(urng) }
415 template<
typename Derived,
typename Urng>
416 inline const BalancedType<Derived, Urng>
420 o.rows(), o.cols(), { std::forward<Urng>(urng) }
424 template<
typename Derived,
typename Urng>
425 using UniformRealType = CwiseNullaryOp<internal::scalar_rng_adaptor<UniformRealGen<typename Derived::Scalar>,
typename Derived::Scalar, Urng,
true>,
const Derived>;
439 template<
typename Derived,
typename Urng>
440 inline const UniformRealType<Derived, Urng>
444 rows, cols, { std::forward<Urng>(urng) }
459 template<
typename Derived,
typename Urng>
460 inline const UniformRealType<Derived, Urng>
464 o.rows(), o.cols(), { std::forward<Urng>(urng) }