32 #ifndef __CUGAR_DISTRIBUTIONS_H 33 #define __CUGAR_DISTRIBUTIONS_H 36 #include <cugar/linalg/vector.h> 37 #include <cugar/linalg/matrix.h> 79 template <
typename Derived_type>
88 template <
typename Generator>
89 inline float next(Generator& gen)
const 91 return static_cast<const Derived_type*
>(
this)->map( gen.next() );
108 inline CUGAR_HOST_DEVICE
float map(
const float U)
const {
return m_b * (U*2.0f - 1.0f); }
113 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 115 return (x >= -m_b && x <= m_b) ? m_b * 2.0f : 0.0f;
129 inline CUGAR_HOST_DEVICE
float map(
const float U)
const 131 return asin( 0.5f * U ) * 2.0f / M_PIf;
136 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 138 if (x >= -1.0f && x <= 1.0f)
139 return M_PIf*0.25f * cosf( x * M_PIf*0.5f );
152 CUGAR_HOST_DEVICE
Pareto_distribution(
const float a,
const float min) : m_a( a ), m_inv_a( 1.0f / a ), m_min( min ) {}
157 inline CUGAR_HOST_DEVICE
float map(
const float U)
const 160 m_min / powf( (0.5f - U)*2.0f, m_inv_a ) :
161 -m_min / powf( (U - 0.5f)*2.0f, m_inv_a );
166 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 168 if (x >= -m_min && x <= m_min)
171 return 0.5f * m_a * powf( m_min, m_a ) / powf( fabsf(x), m_a + 1.0f );
190 m_a( a ), m_inv_a( 1.0f / a ), m_min( min ), m_max( max ),
191 m_min_a( powf( m_min, m_a ) ),
192 m_max_a( powf( m_max, m_a ) ) {}
197 inline CUGAR_HOST_DEVICE
float map(
const float U)
const 201 const float u = (0.5f - U)*2.0f;
202 return powf( -(u * m_max_a - u * m_min_a - m_max_a) / (m_max_a*m_min_a), -m_inv_a);
206 const float u = (U - 0.5f)*2.0f;
207 return -powf( -(u * m_max_a - u * m_min_a - m_max_a) / (m_max_a*m_min_a), -m_inv_a);
213 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 215 if (x >= -m_min && x <= m_min)
217 if (x <= -m_max || x >= m_max)
220 return 0.5f * m_a * m_min_a * powf( fabsf(x), -m_a - 1.0f ) / (1.0f - m_min_a / m_max_a);
242 m_ln( -logf(m_s2/m_s1) ) {}
250 m_ln(-logf(m_s2 / m_s1)) {}
255 inline CUGAR_HOST_DEVICE
float map(
const float U)
const 258 +m_s2 * expf( m_ln*(0.5f - U)*2.0f ) :
259 -m_s2 * expf( m_ln*(U - 0.5f)*2.0f );
264 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 282 x > +m_s2 ? 0.5f / (x * 2 * m_ln) :
283 x < -m_s2 ? -0.5f / (x * 2 * m_ln) :
306 inline CUGAR_HOST_DEVICE
float map(
const float U)
const 308 return m_gamma * tanf(
float(M_PI) * (U - 0.5f) );
313 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 315 return (m_gamma / (x*x + m_gamma*m_gamma)) / float(M_PI);
330 m_lambda( lambda ) {}
335 inline CUGAR_HOST_DEVICE
float map(
const float U)
const 337 const float eps = 1.0e-5f;
339 -logf( cugar::max( (0.5f - U)*2.0f, eps ) ) / m_lambda :
340 logf( cugar::max( (U - 0.5f)*2.0f, eps ) ) / m_lambda;
345 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 347 return 0.5f * m_lambda * expf( -m_lambda * fabsf(x) );
369 const float eps = 1.0e-5f;
370 const float r = m_sigma * sqrtf( - 2.0f * logf(cugar::max( uv[0], eps ) ) );
372 r * cosf( 2.0f *
float(M_PI) * uv[1] ),
373 r * sinf( 2.0f *
float(M_PI) * uv[1] ) );
380 return expf( -square_length(x) / (2.0f * m_sigma*m_sigma) ) / (2.0f * float(M_PI) * m_sigma*m_sigma);
391 enum MatrixType { COVARIANCE_MATRIX, PRECISION_MATRIX };
418 if (matrix_type == COVARIANCE_MATRIX)
422 invert( sigma, m_prec );
425 cholesky( sigma, m_chol );
428 m_norm = 1.0f / sqrtf( 2.0f *
float(M_PI) * det(sigma) );
436 invert( m_prec, sigma );
439 cholesky( sigma, m_chol );
442 m_norm = 1.0f / sqrtf( 2.0f *
float(M_PI) * det(sigma) );
451 m_mu( mu ), m_prec( prec )
453 cholesky( sigma, m_chol );
456 m_norm = 1.0f / sqrtf( 2.0f *
float(M_PI) * det(sigma) );
464 const float eps = 1.0e-5f;
465 const float r = sqrtf( - 2.0f * logf(cugar::max( uv[0], eps ) ) );
467 r * cosf( 2.0f *
float(M_PI) * uv[1] ),
468 r * sinf( 2.0f *
float(M_PI) * uv[1] ) );
470 return m_mu + m_chol * normal;
477 const float x2 = dot( x - m_mu, m_prec * (x - m_mu) );
478 return m_norm * expf( -0.5f * x2 );
500 invert( m_prec, sigma );
514 template <
typename Generator,
typename Distribution>
521 CUGAR_HOST_DEVICE
Transform_generator(Generator& gen,
const Distribution& dist) : m_gen( gen ), m_dist( dist ) {}
525 inline CUGAR_HOST_DEVICE
float next()
const 527 return m_dist.map( m_gen.next() );
532 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 534 return m_dist.density( x );
557 template <
typename Generator>
566 const Vector2f uv( random.next(), random.next() );
567 const float eps = 1.0e-5f;
568 const float r = m_sigma * sqrtf( - 2.0f * logf(cugar::max( uv[0], eps ) ) );
569 const float y0 = r * cosf( 2.0f *
float(M_PI) * uv[1] );
570 const float y1 = r * sinf( 2.0f *
float(M_PI) * uv[1] );
579 inline CUGAR_HOST_DEVICE
float density(
const float x)
const 581 const float SQRT_TWO_PI = sqrtf(2.0f * M_PIf);
582 return expf( -x*x/(2.0f*m_sigma*m_sigma) ) / (SQRT_TWO_PI*m_sigma);
CUGAR_HOST_DEVICE const Matrix2x2f & precision() const
Definition: distributions.h:490
CUGAR_HOST_DEVICE float map(const float U) const
Definition: distributions.h:255
CUGAR_HOST_DEVICE Vector2f map(const Vector2f uv) const
Definition: distributions.h:462
CUGAR_HOST_DEVICE float next(Generator &random)
Definition: distributions.h:558
Definition: distributions.h:146
CUGAR_HOST_DEVICE float map(const float U) const
Definition: distributions.h:157
Definition: distributions.h:83
CUGAR_HOST_DEVICE float density(const float x) const
Definition: distributions.h:136
CUGAR_HOST_DEVICE float density(const float x) const
Definition: distributions.h:579
Definition: distributions.h:234
CUGAR_HOST_DEVICE float density(const float x) const
Definition: distributions.h:313
CUGAR_HOST_DEVICE float density(const Vector2f x) const
Definition: distributions.h:378
Definition: distributions.h:389
CUGAR_HOST_DEVICE Vector2f map(const Vector2f uv) const
Definition: distributions.h:367
Definition: distributions.h:324
CUGAR_HOST_DEVICE Bounded_exponential(const float b)
Definition: distributions.h:239
CUGAR_HOST_DEVICE float density(const float x) const
Definition: distributions.h:213
float next(Generator &gen) const
Definition: distributions.h:89
CUGAR_HOST_DEVICE Vector2f mean() const
Definition: distributions.h:483
CUGAR_HOST_DEVICE Gaussian_distribution_2d()
Definition: distributions.h:398
CUGAR_HOST_DEVICE float density(const float x) const
Definition: distributions.h:166
CUGAR_HOST_DEVICE float map(const float U) const
Definition: distributions.h:335
CUGAR_HOST_DEVICE float map(const float U) const
Definition: distributions.h:197
CUGAR_HOST_DEVICE float density(const float x) const
Definition: distributions.h:264
CUGAR_HOST_DEVICE Gaussian_generator(const float sigma)
Definition: distributions.h:550
CUGAR_HOST_DEVICE Gaussian_distribution_symm_2d(const float sigma)
Definition: distributions.h:361
CUGAR_HOST_DEVICE Pareto_distribution(const float a, const float min)
Definition: distributions.h:152
CUGAR_HOST_DEVICE Bounded_exponential(const float b1, const float b2)
Definition: distributions.h:247
Definition: distributions.h:356
CUGAR_HOST_DEVICE Cauchy_distribution(const float gamma)
Definition: distributions.h:300
float random()
Definition: tiled_sampling.h:44
CUGAR_HOST_DEVICE Gaussian_distribution_2d(const Vector2f mu, const Matrix2x2f matrix, const MatrixType matrix_type=COVARIANCE_MATRIX)
Definition: distributions.h:415
CUGAR_HOST_DEVICE Exponential_distribution(const float lambda)
Definition: distributions.h:329
CUGAR_HOST_DEVICE const Matrix2x2f covariance() const
Definition: distributions.h:497
CUGAR_HOST_DEVICE Bounded_pareto_distribution(const float a, const float min, const float max)
Definition: distributions.h:189
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_HOST_DEVICE float density(const Vector2f x) const
Definition: distributions.h:475
Definition: distributions.h:182
CUGAR_HOST_DEVICE Gaussian_distribution_2d(const Vector2f mu, const Matrix2x2f sigma, const Matrix2x2f prec)
Definition: distributions.h:450
Definition: distributions.h:295
Definition: distributions.h:545
CUGAR_HOST_DEVICE float map(const float U) const
Definition: distributions.h:306
CUGAR_HOST_DEVICE float density(const float x) const
Definition: distributions.h:345
Defines several random samplers.
CUGAR_HOST_DEVICE float map(const float U) const
Definition: distributions.h:129
Definition: distributions.h:124