summaryrefslogtreecommitdiff
path: root/src/nrandom.c
diff options
context:
space:
mode:
authorzimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4>2013-05-31 13:34:12 +0000
committerzimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4>2013-05-31 13:34:12 +0000
commit0c511031fd130cee634f4e542aa733dd84bf6764 (patch)
treead4b18afda981490533ae82279c092e6494c64ed /src/nrandom.c
parentdd2f4304247faadff2998867161aabac576aa9ad (diff)
downloadmpfr-0c511031fd130cee634f4e542aa733dd84bf6764.tar.gz
GNU style
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@8556 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'src/nrandom.c')
-rw-r--r--src/nrandom.c118
1 files changed, 73 insertions, 45 deletions
diff --git a/src/nrandom.c b/src/nrandom.c
index 66a2b6565..c5e971aeb 100644
--- a/src/nrandom.c
+++ b/src/nrandom.c
@@ -36,82 +36,110 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
* the interface.
*/
-#include <random_deviate.h>
+#include "random_deviate.h"
/* Algorithm H: true with probability exp(-1/2). */
-static int H(gmp_randstate_t r,
- mpfr_random_deviate_t p, mpfr_random_deviate_t q) {
+static int
+H (gmp_randstate_t r, mpfr_random_deviate_t p, mpfr_random_deviate_t q)
+{
/* p and q are temporaries */
- mpfr_random_deviate_reset(p);
- if (mpfr_random_deviate_tstbit(p, 1, r)) return 1;
- for (;;) {
- mpfr_random_deviate_reset(q);
- if (!mpfr_random_deviate_less(q, p, r)) return 0;
- mpfr_random_deviate_reset(p);
- if (!mpfr_random_deviate_less(p, q, r)) return 1;
- }
+ mpfr_random_deviate_reset (p);
+ if (mpfr_random_deviate_tstbit (p, 1, r))
+ return 1;
+ for (;;)
+ {
+ mpfr_random_deviate_reset (q);
+ if (!mpfr_random_deviate_less (q, p, r))
+ return 0;
+ mpfr_random_deviate_reset (p);
+ if (!mpfr_random_deviate_less (p, q, r))
+ return 1;
+ }
}
/* Step N1: return n >= 0 with prob. exp(-n/2) * (1 - exp(-1/2)). */
-static unsigned long G(gmp_randstate_t r,
- mpfr_random_deviate_t p, mpfr_random_deviate_t q) {
+static unsigned long
+G (gmp_randstate_t r, mpfr_random_deviate_t p, mpfr_random_deviate_t q)
+{
/* p and q are temporaries */
unsigned long n = 0;
- while (H(r, p, q)) ++n;
+
+ while (H (r, p, q))
+ ++n;
return n;
}
/* Step N2: true with probability exp(-n/2). */
-static int P(unsigned long n, gmp_randstate_t r,
- mpfr_random_deviate_t p, mpfr_random_deviate_t q) {
+static int
+P (unsigned long n, gmp_randstate_t r,
+ mpfr_random_deviate_t p, mpfr_random_deviate_t q)
+{
/* p and q are temporaries */
- while (n--) {if (!H(r, p, q)) return 0;}
+ while (n--)
+ {
+ if (!H (r, p, q))
+ return 0;
+ }
return 1;
}
/* Algorithm C: return (-1, 0, 1) with prob (1/m, 1/m, 1-2/m). */
-static int C(unsigned long m, gmp_randstate_t r) {
- unsigned long n = gmp_urandomm_ui(r, m);
+static int
+C (unsigned long m, gmp_randstate_t r)
+{
+ unsigned long n = gmp_urandomm_ui (r, m);
return n == 0 ? -1 : (n == 1 ? 0 : 1);
}
/* Algorithm B: true with prob exp(-x * (2*k + x) / (2*k + 2)). */
-static int B(unsigned long k, mpfr_random_deviate_t x, gmp_randstate_t r,
- mpfr_random_deviate_t p, mpfr_random_deviate_t q) {
+static int
+B (unsigned long k, mpfr_random_deviate_t x, gmp_randstate_t r,
+ mpfr_random_deviate_t p, mpfr_random_deviate_t q)
+{
/* p and q are temporaries */
unsigned long n = 0, m = 2 * k + 2;
int f;
- for (;; ++n) {
- if ( ((f = k ? 0 : C(m, r)) < 0) ||
- (mpfr_random_deviate_reset(q),
- !mpfr_random_deviate_less(q, n ? p : x, r)) ||
- ((f = k ? C(m, r) : f) < 0) ||
- (f == 0 &&
- (mpfr_random_deviate_reset(p),
- !mpfr_random_deviate_less(p, x, r))) )
- break;
- mpfr_random_deviate_swap(p, q); /* an efficient way of doing p = q */
- }
+
+ for (;; ++n)
+ {
+ if ( ((f = k ? 0 : C (m, r)) < 0) ||
+ (mpfr_random_deviate_reset (q),
+ !mpfr_random_deviate_less (q, n ? p : x, r)) ||
+ ((f = k ? C (m, r) : f) < 0) ||
+ (f == 0 &&
+ (mpfr_random_deviate_reset (p),
+ !mpfr_random_deviate_less (p, x, r))) )
+ break;
+ mpfr_random_deviate_swap (p, q); /* an efficient way of doing p = q */
+ }
return (n & 1UL) == 0;
}
/* return a normal random deviate with mean 0 and variance 1 as a MPFR */
-int mpfr_nrandom(mpfr_t z, gmp_randstate_t r, mpfr_rnd_t rnd) {
+int
+mpfr_nrandom (mpfr_t z, gmp_randstate_t r, mpfr_rnd_t rnd)
+{
mpfr_random_deviate_t x, p, q;
int inex;
unsigned long k, j;
- mpfr_random_deviate_init(x);
- mpfr_random_deviate_init(p); mpfr_random_deviate_init(q);
- for (;;) {
- k = G(r, p, q); /* step 1 */
- if (!P(k * (k - 1), r, p, q)) continue; /* step 2 */
- mpfr_random_deviate_reset(x); /* step 3 */
- for (j = 0; j <= k && B(k, x, r, p, q); ++j); /* step 4 */
- if (j > k) break;
- }
- mpfr_random_deviate_clear(q); mpfr_random_deviate_clear(p);
+
+ mpfr_random_deviate_init (x);
+ mpfr_random_deviate_init (p);
+ mpfr_random_deviate_init (q);
+ for (;;)
+ {
+ k = G (r, p, q); /* step 1 */
+ if (!P (k * (k - 1), r, p, q))
+ continue; /* step 2 */
+ mpfr_random_deviate_reset (x); /* step 3 */
+ for (j = 0; j <= k && B (k, x, r, p, q); ++j); /* step 4 */
+ if (j > k)
+ break;
+ }
+ mpfr_random_deviate_clear (q);
+ mpfr_random_deviate_clear (p);
/* steps 5, 6, 7 */
- inex = mpfr_random_deviate_value(gmp_urandomb_ui(r, 1), k, x, z, r, rnd);
- mpfr_random_deviate_clear(x);
+ inex = mpfr_random_deviate_value (gmp_urandomb_ui (r, 1), k, x, z, r, rnd);
+ mpfr_random_deviate_clear (x);
return inex;
}