commit 8d5b29aedc22794698168850c8b53ec9b79602c5
parent e8e67b3fe8aa1d4b0e13f087b51d52e7b762203c
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 19 Jun 2015 09:13:21 +0200
Add the exponential distribution
Diffstat:
2 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/src/ssp.h b/src/ssp.h
@@ -199,6 +199,21 @@ ssp_rng_proxy_create_rng
struct ssp_rng** rng);
/*******************************************************************************
+ * Miscellaneous distributions
+ ******************************************************************************/
+/* Random variate from the exponential distribution with mean `mu':
+ * P(x) dx = 1 / mu * exp (-x/mu) dx */
+SSP_API double
+ssp_ran_exp
+ (struct ssp_rng* rng,
+ const double mu);
+
+SSP_API double
+ssp_ran_exp_pdf
+ (const double x,
+ const double mu);
+
+/*******************************************************************************
* Sphere distribution
******************************************************************************/
/* Uniform sampling of an unit sphere. The PDF of the generated sample is
diff --git a/src/ssp_rng.c b/src/ssp_rng.c
@@ -622,6 +622,38 @@ ssp_rng_entropy(const struct ssp_rng* rng)
return rng->type.entropy(rng->state);
}
+/*******************************************************************************
+ * Exported distributions
+ ******************************************************************************/
+class rng_cxx /* Wrap a SSP random generator into a CXX object */
+{
+public:
+ FINLINE rng_cxx(struct ssp_rng& rng) : rng(&rng) {}
+ FINLINE uint64_t operator()() { return ssp_rng_get(rng); }
+ FINLINE uint64_t min() const { return ssp_rng_min(rng); }
+ FINLINE uint64_t max() const { return ssp_rng_max(rng); }
+
+private:
+ ssp_rng* rng;
+};
+
+double
+ssp_ran_exp(struct ssp_rng* rng, const double mu)
+{
+ ASSERT(rng);
+ rng_cxx rng_cxx(*rng);
+ RAN_NAMESPACE::exponential_distribution<double> distribution(mu);
+ return distribution(rng_cxx);
+}
+
+double
+ssp_ran_pdf(const double x, const double mu)
+{
+ ASSERT(x >= 0);
+ return mu * exp(-x * mu);
+}
+
#ifdef COMPILER_CL
#pragma warning(pop)
#endif
+