Commit | Line | Data |
---|---|---|
2b829bbb | 1 | /* Copyright (C) 1999,2000,2001, 2003, 2005, 2006 Free Software Foundation, Inc. |
73be1d9e MV |
2 | * This library is free software; you can redistribute it and/or |
3 | * modify it under the terms of the GNU Lesser General Public | |
4 | * License as published by the Free Software Foundation; either | |
5 | * version 2.1 of the License, or (at your option) any later version. | |
e7a72986 | 6 | * |
73be1d9e | 7 | * This library is distributed in the hope that it will be useful, |
e7a72986 | 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
73be1d9e MV |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
10 | * Lesser General Public License for more details. | |
e7a72986 | 11 | * |
73be1d9e MV |
12 | * You should have received a copy of the GNU Lesser General Public |
13 | * License along with this library; if not, write to the Free Software | |
92205699 | 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 15 | */ |
e7a72986 | 16 | |
1bbd0b84 GB |
17 | |
18 | ||
e7a72986 MD |
19 | /* Author: Mikael Djurfeldt <djurfeldt@nada.kth.se> */ |
20 | ||
2fb8013c RB |
21 | #if HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
24 | ||
a0599745 | 25 | #include "libguile/_scm.h" |
e7a72986 | 26 | |
8db9cc6c | 27 | #include <gmp.h> |
7f146094 | 28 | #include <stdio.h> |
e7a72986 | 29 | #include <math.h> |
f34d19c7 | 30 | #include <string.h> |
a0599745 MD |
31 | #include "libguile/smob.h" |
32 | #include "libguile/numbers.h" | |
33 | #include "libguile/feature.h" | |
34 | #include "libguile/strings.h" | |
e9bfab50 | 35 | #include "libguile/unif.h" |
46d25cff | 36 | #include "libguile/srfi-4.h" |
a0599745 | 37 | #include "libguile/vectors.h" |
e7a72986 | 38 | |
a0599745 MD |
39 | #include "libguile/validate.h" |
40 | #include "libguile/random.h" | |
e7a72986 MD |
41 | |
42 | \f | |
43 | /* | |
44 | * A plugin interface for RNGs | |
45 | * | |
46 | * Using this interface, it is possible for the application to tell | |
47 | * libguile to use a different RNG. This is desirable if it is | |
48 | * necessary to use the same RNG everywhere in the application in | |
49 | * order to prevent interference, if the application uses RNG | |
50 | * hardware, or if the application has special demands on the RNG. | |
51 | * | |
52 | * Look in random.h and how the default generator is "plugged in" in | |
53 | * scm_init_random(). | |
54 | */ | |
55 | ||
92c2555f | 56 | scm_t_rng scm_the_rng; |
e7a72986 MD |
57 | |
58 | \f | |
59 | /* | |
60 | * The prepackaged RNG | |
61 | * | |
62 | * This is the MWC (Multiply With Carry) random number generator | |
63 | * described by George Marsaglia at the Department of Statistics and | |
64 | * Supercomputer Computations Research Institute, The Florida State | |
65 | * University (http://stat.fsu.edu/~geo). | |
66 | * | |
67 | * It uses 64 bits, has a period of 4578426017172946943 (4.6e18), and | |
68 | * passes all tests in the DIEHARD test suite | |
69 | * (http://stat.fsu.edu/~geo/diehard.html) | |
70 | */ | |
71 | ||
72 | #define A 2131995753UL | |
73 | ||
82893676 MG |
74 | #ifndef M_PI |
75 | #define M_PI 3.14159265359 | |
76 | #endif | |
77 | ||
4a9f83ff | 78 | #if SCM_HAVE_T_UINT64 |
e7a72986 MD |
79 | |
80 | unsigned long | |
92c2555f | 81 | scm_i_uniform32 (scm_t_i_rstate *state) |
e7a72986 | 82 | { |
4a9f83ff MD |
83 | scm_t_uint64 x = (scm_t_uint64) A * state->w + state->c; |
84 | scm_t_uint32 w = x & 0xffffffffUL; | |
e7a72986 MD |
85 | state->w = w; |
86 | state->c = x >> 32L; | |
87 | return w; | |
88 | } | |
89 | ||
90 | #else | |
91 | ||
92 | /* ww This is a portable version of the same RNG without 64 bit | |
93 | * * aa arithmetic. | |
94 | * ---- | |
95 | * xx It is only intended to provide identical behaviour on | |
96 | * xx platforms without 8 byte longs or long longs until | |
97 | * xx someone has implemented the routine in assembler code. | |
98 | * xxcc | |
99 | * ---- | |
100 | * ccww | |
101 | */ | |
102 | ||
103 | #define L(x) ((x) & 0xffff) | |
104 | #define H(x) ((x) >> 16) | |
105 | ||
106 | unsigned long | |
92c2555f | 107 | scm_i_uniform32 (scm_t_i_rstate *state) |
e7a72986 | 108 | { |
4a9f83ff MD |
109 | scm_t_uint32 x1 = L (A) * L (state->w); |
110 | scm_t_uint32 x2 = L (A) * H (state->w); | |
111 | scm_t_uint32 x3 = H (A) * L (state->w); | |
112 | scm_t_uint32 w = L (x1) + L (state->c); | |
113 | scm_t_uint32 m = H (x1) + L (x2) + L (x3) + H (state->c) + H (w); | |
114 | scm_t_uint32 x4 = H (A) * H (state->w); | |
e7a72986 MD |
115 | state->w = w = (L (m) << 16) + L (w); |
116 | state->c = H (x2) + H (x3) + x4 + H (m); | |
117 | return w; | |
118 | } | |
119 | ||
120 | #endif | |
121 | ||
122 | void | |
cc95e00a | 123 | scm_i_init_rstate (scm_t_i_rstate *state, const char *seed, int n) |
e7a72986 | 124 | { |
4a9f83ff MD |
125 | scm_t_uint32 w = 0L; |
126 | scm_t_uint32 c = 0L; | |
e7a72986 MD |
127 | int i, m; |
128 | for (i = 0; i < n; ++i) | |
129 | { | |
130 | m = i % 8; | |
131 | if (m < 4) | |
132 | w += seed[i] << (8 * m); | |
133 | else | |
134 | c += seed[i] << (8 * (m - 4)); | |
135 | } | |
8b3747f9 | 136 | if ((w == 0 && c == 0) || (w == -1 && c == A - 1)) |
e7a72986 MD |
137 | ++c; |
138 | state->w = w; | |
139 | state->c = c; | |
140 | } | |
141 | ||
92c2555f MV |
142 | scm_t_i_rstate * |
143 | scm_i_copy_rstate (scm_t_i_rstate *state) | |
e7a72986 | 144 | { |
67329a9e | 145 | scm_t_rstate *new_state = scm_malloc (scm_the_rng.rstate_size); |
e7a72986 MD |
146 | return memcpy (new_state, state, scm_the_rng.rstate_size); |
147 | } | |
148 | ||
149 | \f | |
150 | /* | |
151 | * Random number library functions | |
152 | */ | |
153 | ||
92c2555f | 154 | scm_t_rstate * |
cc95e00a | 155 | scm_c_make_rstate (const char *seed, int n) |
5ee11b7c | 156 | { |
67329a9e | 157 | scm_t_rstate *state = scm_malloc (scm_the_rng.rstate_size); |
5ee11b7c MD |
158 | state->reserved0 = 0; |
159 | scm_the_rng.init_rstate (state, seed, n); | |
160 | return state; | |
161 | } | |
162 | ||
2ade72d7 | 163 | |
92c2555f | 164 | scm_t_rstate * |
9b741bb6 | 165 | scm_c_default_rstate () |
2ade72d7 | 166 | #define FUNC_NAME "scm_c_default_rstate" |
9b741bb6 | 167 | { |
c5f268f8 | 168 | SCM state = SCM_VARIABLE_REF (scm_var_random_state); |
2ade72d7 DH |
169 | if (!SCM_RSTATEP (state)) |
170 | SCM_MISC_ERROR ("*random-state* contains bogus random state", SCM_EOL); | |
9b741bb6 MD |
171 | return SCM_RSTATE (state); |
172 | } | |
2ade72d7 DH |
173 | #undef FUNC_NAME |
174 | ||
9b741bb6 | 175 | |
e7a72986 | 176 | inline double |
92c2555f | 177 | scm_c_uniform01 (scm_t_rstate *state) |
e7a72986 | 178 | { |
5a92ddfd | 179 | double x = (double) scm_the_rng.random_bits (state) / (double) 0xffffffffUL; |
e7a72986 | 180 | return ((x + (double) scm_the_rng.random_bits (state)) |
5a92ddfd | 181 | / (double) 0xffffffffUL); |
e7a72986 MD |
182 | } |
183 | ||
184 | double | |
92c2555f | 185 | scm_c_normal01 (scm_t_rstate *state) |
e7a72986 MD |
186 | { |
187 | if (state->reserved0) | |
188 | { | |
189 | state->reserved0 = 0; | |
190 | return state->reserved1; | |
191 | } | |
192 | else | |
193 | { | |
194 | double r, a, n; | |
e7a72986 | 195 | |
9b741bb6 MD |
196 | r = sqrt (-2.0 * log (scm_c_uniform01 (state))); |
197 | a = 2.0 * M_PI * scm_c_uniform01 (state); | |
e7a72986 MD |
198 | |
199 | n = r * sin (a); | |
200 | state->reserved1 = r * cos (a); | |
5a92ddfd | 201 | state->reserved0 = 1; |
e7a72986 MD |
202 | |
203 | return n; | |
204 | } | |
205 | } | |
206 | ||
207 | double | |
92c2555f | 208 | scm_c_exp1 (scm_t_rstate *state) |
e7a72986 | 209 | { |
9b741bb6 | 210 | return - log (scm_c_uniform01 (state)); |
e7a72986 MD |
211 | } |
212 | ||
213 | unsigned char scm_masktab[256]; | |
214 | ||
215 | unsigned long | |
92c2555f | 216 | scm_c_random (scm_t_rstate *state, unsigned long m) |
e7a72986 MD |
217 | { |
218 | unsigned int r, mask; | |
219 | mask = (m < 0x100 | |
220 | ? scm_masktab[m] | |
221 | : (m < 0x10000 | |
5a92ddfd | 222 | ? scm_masktab[m >> 8] << 8 | 0xff |
e7a72986 | 223 | : (m < 0x1000000 |
5a92ddfd MD |
224 | ? scm_masktab[m >> 16] << 16 | 0xffff |
225 | : scm_masktab[m >> 24] << 24 | 0xffffff))); | |
e7a72986 MD |
226 | while ((r = scm_the_rng.random_bits (state) & mask) >= m); |
227 | return r; | |
228 | } | |
229 | ||
ea7f6344 MD |
230 | /* |
231 | SCM scm_c_random_bignum (scm_t_rstate *state, SCM m) | |
232 | ||
233 | Takes a random state (source of random bits) and a bignum m. | |
234 | Returns a bignum b, 0 <= b < m. | |
235 | ||
236 | It does this by allocating a bignum b with as many base 65536 digits | |
237 | as m, filling b with random bits (in 32 bit chunks) up to the most | |
238 | significant 1 in m, and, finally checking if the resultant b is too | |
239 | large (>= m). If too large, we simply repeat the process again. (It | |
240 | is important to throw away all generated random bits if b >= m, | |
241 | otherwise we'll end up with a distorted distribution.) | |
242 | ||
243 | */ | |
244 | ||
e7a72986 | 245 | SCM |
92c2555f | 246 | scm_c_random_bignum (scm_t_rstate *state, SCM m) |
e7a72986 | 247 | { |
969d3bd0 | 248 | SCM result = scm_i_mkbig (); |
969d3bd0 RB |
249 | const size_t m_bits = mpz_sizeinbase (SCM_I_BIG_MPZ (m), 2); |
250 | /* how many bits would only partially fill the last unsigned long? */ | |
0d79003d | 251 | const size_t end_bits = m_bits % (sizeof (unsigned long) * SCM_CHAR_BIT); |
969d3bd0 | 252 | unsigned long *random_chunks = NULL; |
0d79003d RB |
253 | const unsigned long num_full_chunks = |
254 | m_bits / (sizeof (unsigned long) * SCM_CHAR_BIT); | |
969d3bd0 RB |
255 | const unsigned long num_chunks = num_full_chunks + ((end_bits) ? 1 : 0); |
256 | ||
257 | /* we know the result will be this big */ | |
258 | mpz_realloc2 (SCM_I_BIG_MPZ (result), m_bits); | |
259 | ||
260 | random_chunks = | |
261 | (unsigned long *) scm_gc_calloc (num_chunks * sizeof (unsigned long), | |
262 | "random bignum chunks"); | |
263 | ||
372691d8 | 264 | do |
e7a72986 | 265 | { |
969d3bd0 RB |
266 | unsigned long *current_chunk = random_chunks + (num_chunks - 1); |
267 | unsigned long chunks_left = num_chunks; | |
969d3bd0 RB |
268 | |
269 | mpz_set_ui (SCM_I_BIG_MPZ (result), 0); | |
270 | ||
271 | if (end_bits) | |
272 | { | |
273 | /* generate a mask with ones in the end_bits position, i.e. if | |
274 | end_bits is 3, then we'd have a mask of ...0000000111 */ | |
275 | const unsigned long rndbits = scm_the_rng.random_bits (state); | |
0d79003d | 276 | int rshift = (sizeof (unsigned long) * SCM_CHAR_BIT) - end_bits; |
969d3bd0 RB |
277 | unsigned long mask = ((unsigned long) ULONG_MAX) >> rshift; |
278 | unsigned long highest_bits = rndbits & mask; | |
279 | *current_chunk-- = highest_bits; | |
280 | chunks_left--; | |
281 | } | |
282 | ||
283 | while (chunks_left) | |
284 | { | |
285 | /* now fill in the remaining unsigned long sized chunks */ | |
286 | *current_chunk-- = scm_the_rng.random_bits (state); | |
287 | chunks_left--; | |
288 | } | |
289 | mpz_import (SCM_I_BIG_MPZ (result), | |
290 | num_chunks, | |
291 | -1, | |
292 | sizeof (unsigned long), | |
293 | 0, | |
294 | 0, | |
295 | random_chunks); | |
372691d8 MD |
296 | /* if result >= m, regenerate it (it is important to regenerate |
297 | all bits in order not to get a distorted distribution) */ | |
298 | } while (mpz_cmp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (m)) >= 0); | |
969d3bd0 RB |
299 | scm_gc_free (random_chunks, |
300 | num_chunks * sizeof (unsigned long), | |
301 | "random bignum chunks"); | |
8db9cc6c | 302 | return scm_i_normbig (result); |
e7a72986 MD |
303 | } |
304 | ||
305 | /* | |
306 | * Scheme level representation of random states. | |
307 | */ | |
308 | ||
92c2555f | 309 | scm_t_bits scm_tc16_rstate; |
e7a72986 MD |
310 | |
311 | static SCM | |
92c2555f | 312 | make_rstate (scm_t_rstate *state) |
e7a72986 | 313 | { |
23a62151 | 314 | SCM_RETURN_NEWSMOB (scm_tc16_rstate, state); |
e7a72986 MD |
315 | } |
316 | ||
1be6b49c | 317 | static size_t |
e841c3e0 | 318 | rstate_free (SCM rstate) |
e7a72986 MD |
319 | { |
320 | free (SCM_RSTATE (rstate)); | |
dfd71aba | 321 | return 0; |
e7a72986 MD |
322 | } |
323 | ||
e7a72986 MD |
324 | /* |
325 | * Scheme level interface. | |
326 | */ | |
327 | ||
cc95e00a | 328 | SCM_GLOBAL_VARIABLE_INIT (scm_var_random_state, "*random-state*", scm_seed_to_random_state (scm_from_locale_string ("URL:http://stat.fsu.edu/~geo/diehard.html"))); |
e7a72986 | 329 | |
a1ec6916 | 330 | SCM_DEFINE (scm_random, "random", 1, 1, 0, |
1bbd0b84 | 331 | (SCM n, SCM state), |
34d19ef6 | 332 | "Return a number in [0, N).\n" |
d928e0b4 | 333 | "\n" |
9401323e NJ |
334 | "Accepts a positive integer or real n and returns a\n" |
335 | "number of the same type between zero (inclusive) and\n" | |
336 | "N (exclusive). The values returned have a uniform\n" | |
d928e0b4 GB |
337 | "distribution.\n" |
338 | "\n" | |
3b644514 MG |
339 | "The optional argument @var{state} must be of the type produced\n" |
340 | "by @code{seed->random-state}. It defaults to the value of the\n" | |
341 | "variable @var{*random-state*}. This object is used to maintain\n" | |
342 | "the state of the pseudo-random-number generator and is altered\n" | |
343 | "as a side effect of the random operation.") | |
1bbd0b84 | 344 | #define FUNC_NAME s_scm_random |
e7a72986 MD |
345 | { |
346 | if (SCM_UNBNDP (state)) | |
86d31dfe | 347 | state = SCM_VARIABLE_REF (scm_var_random_state); |
34d19ef6 | 348 | SCM_VALIDATE_RSTATE (2, state); |
e11e83f3 | 349 | if (SCM_I_INUMP (n)) |
e7a72986 | 350 | { |
e11e83f3 | 351 | unsigned long m = SCM_I_INUM (n); |
34d19ef6 | 352 | SCM_ASSERT_RANGE (1, n, m > 0); |
e11e83f3 | 353 | return scm_from_ulong (scm_c_random (SCM_RSTATE (state), m)); |
e7a72986 | 354 | } |
34d19ef6 | 355 | SCM_VALIDATE_NIM (1, n); |
e7a72986 | 356 | if (SCM_REALP (n)) |
d9a67fc4 MV |
357 | return scm_from_double (SCM_REAL_VALUE (n) |
358 | * scm_c_uniform01 (SCM_RSTATE (state))); | |
969d3bd0 | 359 | |
a55c2b68 MV |
360 | if (!SCM_BIGP (n)) |
361 | SCM_WRONG_TYPE_ARG (1, n); | |
9b741bb6 | 362 | return scm_c_random_bignum (SCM_RSTATE (state), n); |
e7a72986 | 363 | } |
1bbd0b84 | 364 | #undef FUNC_NAME |
e7a72986 | 365 | |
a1ec6916 | 366 | SCM_DEFINE (scm_copy_random_state, "copy-random-state", 0, 1, 0, |
1bbd0b84 | 367 | (SCM state), |
3b644514 | 368 | "Return a copy of the random state @var{state}.") |
1bbd0b84 | 369 | #define FUNC_NAME s_scm_copy_random_state |
e7a72986 MD |
370 | { |
371 | if (SCM_UNBNDP (state)) | |
86d31dfe | 372 | state = SCM_VARIABLE_REF (scm_var_random_state); |
34d19ef6 | 373 | SCM_VALIDATE_RSTATE (1, state); |
e7a72986 MD |
374 | return make_rstate (scm_the_rng.copy_rstate (SCM_RSTATE (state))); |
375 | } | |
1bbd0b84 | 376 | #undef FUNC_NAME |
e7a72986 | 377 | |
a1ec6916 | 378 | SCM_DEFINE (scm_seed_to_random_state, "seed->random-state", 1, 0, 0, |
1bbd0b84 | 379 | (SCM seed), |
3b644514 | 380 | "Return a new random state using @var{seed}.") |
1bbd0b84 | 381 | #define FUNC_NAME s_scm_seed_to_random_state |
5ee11b7c | 382 | { |
8824ac88 | 383 | SCM res; |
5ee11b7c MD |
384 | if (SCM_NUMBERP (seed)) |
385 | seed = scm_number_to_string (seed, SCM_UNDEFINED); | |
34d19ef6 | 386 | SCM_VALIDATE_STRING (1, seed); |
cc95e00a MV |
387 | res = make_rstate (scm_c_make_rstate (scm_i_string_chars (seed), |
388 | scm_i_string_length (seed))); | |
8824ac88 MV |
389 | scm_remember_upto_here_1 (seed); |
390 | return res; | |
391 | ||
5ee11b7c | 392 | } |
1bbd0b84 | 393 | #undef FUNC_NAME |
5ee11b7c | 394 | |
a1ec6916 | 395 | SCM_DEFINE (scm_random_uniform, "random:uniform", 0, 1, 0, |
1bbd0b84 | 396 | (SCM state), |
1e6808ea MG |
397 | "Return a uniformly distributed inexact real random number in\n" |
398 | "[0,1).") | |
1bbd0b84 | 399 | #define FUNC_NAME s_scm_random_uniform |
e7a72986 MD |
400 | { |
401 | if (SCM_UNBNDP (state)) | |
86d31dfe | 402 | state = SCM_VARIABLE_REF (scm_var_random_state); |
34d19ef6 | 403 | SCM_VALIDATE_RSTATE (1, state); |
d9a67fc4 | 404 | return scm_from_double (scm_c_uniform01 (SCM_RSTATE (state))); |
e7a72986 | 405 | } |
1bbd0b84 | 406 | #undef FUNC_NAME |
e7a72986 | 407 | |
a1ec6916 | 408 | SCM_DEFINE (scm_random_normal, "random:normal", 0, 1, 0, |
1bbd0b84 | 409 | (SCM state), |
1e6808ea MG |
410 | "Return an inexact real in a normal distribution. The\n" |
411 | "distribution used has mean 0 and standard deviation 1. For a\n" | |
412 | "normal distribution with mean m and standard deviation d use\n" | |
413 | "@code{(+ m (* d (random:normal)))}.") | |
1bbd0b84 | 414 | #define FUNC_NAME s_scm_random_normal |
afe5177e GH |
415 | { |
416 | if (SCM_UNBNDP (state)) | |
86d31dfe | 417 | state = SCM_VARIABLE_REF (scm_var_random_state); |
34d19ef6 | 418 | SCM_VALIDATE_RSTATE (1, state); |
d9a67fc4 | 419 | return scm_from_double (scm_c_normal01 (SCM_RSTATE (state))); |
afe5177e | 420 | } |
1bbd0b84 | 421 | #undef FUNC_NAME |
afe5177e | 422 | |
e7a72986 | 423 | static void |
46d25cff | 424 | vector_scale_x (SCM v, double c) |
e7a72986 | 425 | { |
46d25cff | 426 | size_t n; |
4057a3e0 | 427 | if (scm_is_simple_vector (v)) |
46d25cff | 428 | { |
4057a3e0 | 429 | n = SCM_SIMPLE_VECTOR_LENGTH (v); |
46d25cff | 430 | while (n-- > 0) |
4057a3e0 | 431 | SCM_REAL_VALUE (SCM_SIMPLE_VECTOR_REF (v, n)) *= c; |
46d25cff | 432 | } |
e7a72986 | 433 | else |
46d25cff MV |
434 | { |
435 | /* must be a f64vector. */ | |
4057a3e0 MV |
436 | scm_t_array_handle handle; |
437 | size_t i, len; | |
438 | ssize_t inc; | |
439 | double *elts; | |
440 | ||
441 | elts = scm_f64vector_writable_elements (v, &handle, &len, &inc); | |
442 | ||
443 | for (i = 0; i < len; i++, elts += inc) | |
444 | *elts *= c; | |
c8857a4d MV |
445 | |
446 | scm_array_handle_release (&handle); | |
46d25cff | 447 | } |
e7a72986 MD |
448 | } |
449 | ||
450 | static double | |
451 | vector_sum_squares (SCM v) | |
452 | { | |
453 | double x, sum = 0.0; | |
46d25cff | 454 | size_t n; |
4057a3e0 | 455 | if (scm_is_simple_vector (v)) |
46d25cff | 456 | { |
4057a3e0 | 457 | n = SCM_SIMPLE_VECTOR_LENGTH (v); |
46d25cff MV |
458 | while (n-- > 0) |
459 | { | |
4057a3e0 | 460 | x = SCM_REAL_VALUE (SCM_SIMPLE_VECTOR_REF (v, n)); |
46d25cff MV |
461 | sum += x * x; |
462 | } | |
463 | } | |
e7a72986 | 464 | else |
46d25cff MV |
465 | { |
466 | /* must be a f64vector. */ | |
4057a3e0 MV |
467 | scm_t_array_handle handle; |
468 | size_t i, len; | |
469 | ssize_t inc; | |
470 | const double *elts; | |
471 | ||
472 | elts = scm_f64vector_elements (v, &handle, &len, &inc); | |
473 | ||
474 | for (i = 0; i < len; i++, elts += inc) | |
46d25cff | 475 | { |
4057a3e0 | 476 | x = *elts; |
46d25cff MV |
477 | sum += x * x; |
478 | } | |
4057a3e0 | 479 | |
c8857a4d | 480 | scm_array_handle_release (&handle); |
46d25cff | 481 | } |
e7a72986 MD |
482 | return sum; |
483 | } | |
484 | ||
e7a72986 MD |
485 | /* For the uniform distribution on the solid sphere, note that in |
486 | * this distribution the length r of the vector has cumulative | |
487 | * distribution r^n; i.e., u=r^n is uniform [0,1], so r can be | |
488 | * generated as r=u^(1/n). | |
489 | */ | |
a1ec6916 | 490 | SCM_DEFINE (scm_random_solid_sphere_x, "random:solid-sphere!", 1, 1, 0, |
1bbd0b84 | 491 | (SCM v, SCM state), |
6efaeb35 KR |
492 | "Fills @var{vect} with inexact real random numbers the sum of\n" |
493 | "whose squares is less than 1.0. Thinking of @var{vect} as\n" | |
494 | "coordinates in space of dimension @var{n} @math{=}\n" | |
495 | "@code{(vector-length @var{vect})}, the coordinates are\n" | |
496 | "uniformly distributed within the unit @var{n}-sphere.") | |
1bbd0b84 | 497 | #define FUNC_NAME s_scm_random_solid_sphere_x |
e7a72986 | 498 | { |
e7a72986 | 499 | if (SCM_UNBNDP (state)) |
86d31dfe | 500 | state = SCM_VARIABLE_REF (scm_var_random_state); |
34d19ef6 | 501 | SCM_VALIDATE_RSTATE (2, state); |
e7a72986 | 502 | scm_random_normal_vector_x (v, state); |
46d25cff MV |
503 | vector_scale_x (v, |
504 | pow (scm_c_uniform01 (SCM_RSTATE (state)), | |
f160e709 | 505 | 1.0 / scm_c_generalized_vector_length (v)) |
46d25cff | 506 | / sqrt (vector_sum_squares (v))); |
e7a72986 MD |
507 | return SCM_UNSPECIFIED; |
508 | } | |
1bbd0b84 | 509 | #undef FUNC_NAME |
e7a72986 | 510 | |
a1ec6916 | 511 | SCM_DEFINE (scm_random_hollow_sphere_x, "random:hollow-sphere!", 1, 1, 0, |
1bbd0b84 | 512 | (SCM v, SCM state), |
d928e0b4 GB |
513 | "Fills vect with inexact real random numbers\n" |
514 | "the sum of whose squares is equal to 1.0.\n" | |
9401323e | 515 | "Thinking of vect as coordinates in space of\n" |
d928e0b4 | 516 | "dimension n = (vector-length vect), the coordinates\n" |
9401323e | 517 | "are uniformly distributed over the surface of the\n" |
72dd0a03 | 518 | "unit n-sphere.") |
1bbd0b84 | 519 | #define FUNC_NAME s_scm_random_hollow_sphere_x |
e7a72986 | 520 | { |
e7a72986 | 521 | if (SCM_UNBNDP (state)) |
86d31dfe | 522 | state = SCM_VARIABLE_REF (scm_var_random_state); |
34d19ef6 | 523 | SCM_VALIDATE_RSTATE (2, state); |
e7a72986 | 524 | scm_random_normal_vector_x (v, state); |
46d25cff | 525 | vector_scale_x (v, 1 / sqrt (vector_sum_squares (v))); |
e7a72986 MD |
526 | return SCM_UNSPECIFIED; |
527 | } | |
1bbd0b84 | 528 | #undef FUNC_NAME |
e7a72986 | 529 | |
1bbd0b84 | 530 | |
a1ec6916 | 531 | SCM_DEFINE (scm_random_normal_vector_x, "random:normal-vector!", 1, 1, 0, |
1bbd0b84 | 532 | (SCM v, SCM state), |
d928e0b4 GB |
533 | "Fills vect with inexact real random numbers that are\n" |
534 | "independent and standard normally distributed\n" | |
64ba8e85 | 535 | "(i.e., with mean 0 and variance 1).") |
1bbd0b84 | 536 | #define FUNC_NAME s_scm_random_normal_vector_x |
e7a72986 | 537 | { |
4057a3e0 MV |
538 | long i; |
539 | scm_t_array_handle handle; | |
540 | scm_t_array_dim *dim; | |
46d25cff | 541 | |
e7a72986 | 542 | if (SCM_UNBNDP (state)) |
86d31dfe | 543 | state = SCM_VARIABLE_REF (scm_var_random_state); |
34d19ef6 | 544 | SCM_VALIDATE_RSTATE (2, state); |
4057a3e0 | 545 | |
c8857a4d | 546 | scm_generalized_vector_get_handle (v, &handle); |
4057a3e0 MV |
547 | dim = scm_array_handle_dims (&handle); |
548 | ||
549 | if (scm_is_vector (v)) | |
46d25cff | 550 | { |
4057a3e0 MV |
551 | SCM *elts = scm_array_handle_writable_elements (&handle); |
552 | for (i = dim->lbnd; i <= dim->ubnd; i++, elts += dim->inc) | |
553 | *elts = scm_from_double (scm_c_normal01 (SCM_RSTATE (state))); | |
46d25cff | 554 | } |
e7a72986 | 555 | else |
46d25cff MV |
556 | { |
557 | /* must be a f64vector. */ | |
4057a3e0 MV |
558 | double *elts = scm_array_handle_f64_writable_elements (&handle); |
559 | for (i = dim->lbnd; i <= dim->ubnd; i++, elts += dim->inc) | |
560 | *elts = scm_c_normal01 (SCM_RSTATE (state)); | |
46d25cff | 561 | } |
4057a3e0 | 562 | |
c8857a4d MV |
563 | scm_array_handle_release (&handle); |
564 | ||
e7a72986 MD |
565 | return SCM_UNSPECIFIED; |
566 | } | |
1bbd0b84 | 567 | #undef FUNC_NAME |
e7a72986 | 568 | |
a1ec6916 | 569 | SCM_DEFINE (scm_random_exp, "random:exp", 0, 1, 0, |
1bbd0b84 | 570 | (SCM state), |
1e6808ea MG |
571 | "Return an inexact real in an exponential distribution with mean\n" |
572 | "1. For an exponential distribution with mean u use (* u\n" | |
573 | "(random:exp)).") | |
1bbd0b84 | 574 | #define FUNC_NAME s_scm_random_exp |
e7a72986 MD |
575 | { |
576 | if (SCM_UNBNDP (state)) | |
86d31dfe | 577 | state = SCM_VARIABLE_REF (scm_var_random_state); |
34d19ef6 | 578 | SCM_VALIDATE_RSTATE (1, state); |
d9a67fc4 | 579 | return scm_from_double (scm_c_exp1 (SCM_RSTATE (state))); |
e7a72986 | 580 | } |
1bbd0b84 | 581 | #undef FUNC_NAME |
e7a72986 MD |
582 | |
583 | void | |
584 | scm_init_random () | |
585 | { | |
586 | int i, m; | |
587 | /* plug in default RNG */ | |
92c2555f | 588 | scm_t_rng rng = |
e7a72986 | 589 | { |
92c2555f | 590 | sizeof (scm_t_i_rstate), |
e7a72986 MD |
591 | (unsigned long (*)()) scm_i_uniform32, |
592 | (void (*)()) scm_i_init_rstate, | |
92c2555f | 593 | (scm_t_rstate *(*)()) scm_i_copy_rstate |
e7a72986 MD |
594 | }; |
595 | scm_the_rng = rng; | |
596 | ||
e841c3e0 KN |
597 | scm_tc16_rstate = scm_make_smob_type ("random-state", 0); |
598 | scm_set_smob_free (scm_tc16_rstate, rstate_free); | |
e7a72986 MD |
599 | |
600 | for (m = 1; m <= 0x100; m <<= 1) | |
601 | for (i = m >> 1; i < m; ++i) | |
602 | scm_masktab[i] = m - 1; | |
603 | ||
a0599745 | 604 | #include "libguile/random.x" |
e7a72986 MD |
605 | |
606 | scm_add_feature ("random"); | |
607 | } | |
89e00824 ML |
608 | |
609 | /* | |
610 | Local Variables: | |
611 | c-file-style: "gnu" | |
612 | End: | |
613 | */ |