Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:00:59

0001 /* rng/gsl_rng.h
0002  * 
0003  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2007 James Theiler, Brian Gough
0004  * 
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 3 of the License, or (at
0008  * your option) any later version.
0009  * 
0010  * This program is distributed in the hope that it will be useful, but
0011  * WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * General Public License for more details.
0014  * 
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #ifndef __GSL_RNG_H__
0021 #define __GSL_RNG_H__
0022 #include <stdlib.h>
0023 #include <gsl/gsl_types.h>
0024 #include <gsl/gsl_errno.h>
0025 #include <gsl/gsl_inline.h>
0026 
0027 #undef __BEGIN_DECLS
0028 #undef __END_DECLS
0029 #ifdef __cplusplus
0030 # define __BEGIN_DECLS extern "C" {
0031 # define __END_DECLS }
0032 #else
0033 # define __BEGIN_DECLS /* empty */
0034 # define __END_DECLS /* empty */
0035 #endif
0036 
0037 __BEGIN_DECLS
0038 
0039 typedef struct
0040   {
0041     const char *name;
0042     unsigned long int max;
0043     unsigned long int min;
0044     size_t size;
0045     void (*set) (void *state, unsigned long int seed);
0046     unsigned long int (*get) (void *state);
0047     double (*get_double) (void *state);
0048   }
0049 gsl_rng_type;
0050 
0051 typedef struct
0052   {
0053     const gsl_rng_type * type;
0054     void *state;
0055   }
0056 gsl_rng;
0057 
0058 
0059 /* These structs also need to appear in default.c so you can select
0060    them via the environment variable GSL_RNG_TYPE */
0061 
0062 GSL_VAR const gsl_rng_type *gsl_rng_borosh13;
0063 GSL_VAR const gsl_rng_type *gsl_rng_coveyou;
0064 GSL_VAR const gsl_rng_type *gsl_rng_cmrg;
0065 GSL_VAR const gsl_rng_type *gsl_rng_fishman18;
0066 GSL_VAR const gsl_rng_type *gsl_rng_fishman20;
0067 GSL_VAR const gsl_rng_type *gsl_rng_fishman2x;
0068 GSL_VAR const gsl_rng_type *gsl_rng_gfsr4;
0069 GSL_VAR const gsl_rng_type *gsl_rng_knuthran;
0070 GSL_VAR const gsl_rng_type *gsl_rng_knuthran2;
0071 GSL_VAR const gsl_rng_type *gsl_rng_knuthran2002;
0072 GSL_VAR const gsl_rng_type *gsl_rng_lecuyer21;
0073 GSL_VAR const gsl_rng_type *gsl_rng_minstd;
0074 GSL_VAR const gsl_rng_type *gsl_rng_mrg;
0075 GSL_VAR const gsl_rng_type *gsl_rng_mt19937;
0076 GSL_VAR const gsl_rng_type *gsl_rng_mt19937_1999;
0077 GSL_VAR const gsl_rng_type *gsl_rng_mt19937_1998;
0078 GSL_VAR const gsl_rng_type *gsl_rng_r250;
0079 GSL_VAR const gsl_rng_type *gsl_rng_ran0;
0080 GSL_VAR const gsl_rng_type *gsl_rng_ran1;
0081 GSL_VAR const gsl_rng_type *gsl_rng_ran2;
0082 GSL_VAR const gsl_rng_type *gsl_rng_ran3;
0083 GSL_VAR const gsl_rng_type *gsl_rng_rand;
0084 GSL_VAR const gsl_rng_type *gsl_rng_rand48;
0085 GSL_VAR const gsl_rng_type *gsl_rng_random128_bsd;
0086 GSL_VAR const gsl_rng_type *gsl_rng_random128_glibc2;
0087 GSL_VAR const gsl_rng_type *gsl_rng_random128_libc5;
0088 GSL_VAR const gsl_rng_type *gsl_rng_random256_bsd;
0089 GSL_VAR const gsl_rng_type *gsl_rng_random256_glibc2;
0090 GSL_VAR const gsl_rng_type *gsl_rng_random256_libc5;
0091 GSL_VAR const gsl_rng_type *gsl_rng_random32_bsd;
0092 GSL_VAR const gsl_rng_type *gsl_rng_random32_glibc2;
0093 GSL_VAR const gsl_rng_type *gsl_rng_random32_libc5;
0094 GSL_VAR const gsl_rng_type *gsl_rng_random64_bsd;
0095 GSL_VAR const gsl_rng_type *gsl_rng_random64_glibc2;
0096 GSL_VAR const gsl_rng_type *gsl_rng_random64_libc5;
0097 GSL_VAR const gsl_rng_type *gsl_rng_random8_bsd;
0098 GSL_VAR const gsl_rng_type *gsl_rng_random8_glibc2;
0099 GSL_VAR const gsl_rng_type *gsl_rng_random8_libc5;
0100 GSL_VAR const gsl_rng_type *gsl_rng_random_bsd;
0101 GSL_VAR const gsl_rng_type *gsl_rng_random_glibc2;
0102 GSL_VAR const gsl_rng_type *gsl_rng_random_libc5;
0103 GSL_VAR const gsl_rng_type *gsl_rng_randu;
0104 GSL_VAR const gsl_rng_type *gsl_rng_ranf;
0105 GSL_VAR const gsl_rng_type *gsl_rng_ranlux;
0106 GSL_VAR const gsl_rng_type *gsl_rng_ranlux389;
0107 GSL_VAR const gsl_rng_type *gsl_rng_ranlxd1;
0108 GSL_VAR const gsl_rng_type *gsl_rng_ranlxd2;
0109 GSL_VAR const gsl_rng_type *gsl_rng_ranlxs0;
0110 GSL_VAR const gsl_rng_type *gsl_rng_ranlxs1;
0111 GSL_VAR const gsl_rng_type *gsl_rng_ranlxs2;
0112 GSL_VAR const gsl_rng_type *gsl_rng_ranmar;
0113 GSL_VAR const gsl_rng_type *gsl_rng_slatec;
0114 GSL_VAR const gsl_rng_type *gsl_rng_taus;
0115 GSL_VAR const gsl_rng_type *gsl_rng_taus2;
0116 GSL_VAR const gsl_rng_type *gsl_rng_taus113;
0117 GSL_VAR const gsl_rng_type *gsl_rng_transputer;
0118 GSL_VAR const gsl_rng_type *gsl_rng_tt800;
0119 GSL_VAR const gsl_rng_type *gsl_rng_uni;
0120 GSL_VAR const gsl_rng_type *gsl_rng_uni32;
0121 GSL_VAR const gsl_rng_type *gsl_rng_vax;
0122 GSL_VAR const gsl_rng_type *gsl_rng_waterman14;
0123 GSL_VAR const gsl_rng_type *gsl_rng_zuf;
0124 
0125 const gsl_rng_type ** gsl_rng_types_setup(void);
0126 
0127 GSL_VAR const gsl_rng_type *gsl_rng_default;
0128 GSL_VAR unsigned long int gsl_rng_default_seed;
0129 
0130 gsl_rng *gsl_rng_alloc (const gsl_rng_type * T);
0131 int gsl_rng_memcpy (gsl_rng * dest, const gsl_rng * src);
0132 gsl_rng *gsl_rng_clone (const gsl_rng * r);
0133 
0134 void gsl_rng_free (gsl_rng * r);
0135 
0136 void gsl_rng_set (const gsl_rng * r, unsigned long int seed);
0137 unsigned long int gsl_rng_max (const gsl_rng * r);
0138 unsigned long int gsl_rng_min (const gsl_rng * r);
0139 const char *gsl_rng_name (const gsl_rng * r);
0140 
0141 int gsl_rng_fread (FILE * stream, gsl_rng * r);
0142 int gsl_rng_fwrite (FILE * stream, const gsl_rng * r);
0143 
0144 size_t gsl_rng_size (const gsl_rng * r);
0145 void * gsl_rng_state (const gsl_rng * r);
0146 
0147 void gsl_rng_print_state (const gsl_rng * r);
0148 
0149 const gsl_rng_type * gsl_rng_env_setup (void);
0150 
0151 INLINE_DECL unsigned long int gsl_rng_get (const gsl_rng * r);
0152 INLINE_DECL double gsl_rng_uniform (const gsl_rng * r);
0153 INLINE_DECL double gsl_rng_uniform_pos (const gsl_rng * r);
0154 INLINE_DECL unsigned long int gsl_rng_uniform_int (const gsl_rng * r, unsigned long int n);
0155 
0156 #ifdef HAVE_INLINE
0157 
0158 INLINE_FUN unsigned long int
0159 gsl_rng_get (const gsl_rng * r)
0160 {
0161   return (r->type->get) (r->state);
0162 }
0163 
0164 INLINE_FUN double
0165 gsl_rng_uniform (const gsl_rng * r)
0166 {
0167   return (r->type->get_double) (r->state);
0168 }
0169 
0170 INLINE_FUN double
0171 gsl_rng_uniform_pos (const gsl_rng * r)
0172 {
0173   double x ;
0174   do
0175     {
0176       x = (r->type->get_double) (r->state) ;
0177     }
0178   while (x == 0) ;
0179 
0180   return x ;
0181 }
0182 
0183 /* Note: to avoid integer overflow in (range+1) we work with scale =
0184    range/n = (max-min)/n rather than scale=(max-min+1)/n, this reduces
0185    efficiency slightly but avoids having to check for the out of range
0186    value.  Note that range is typically O(2^32) so the addition of 1
0187    is negligible in most usage. */
0188 
0189 INLINE_FUN unsigned long int
0190 gsl_rng_uniform_int (const gsl_rng * r, unsigned long int n)
0191 {
0192   unsigned long int offset = r->type->min;
0193   unsigned long int range = r->type->max - offset;
0194   unsigned long int scale;
0195   unsigned long int k;
0196 
0197   if (n > range || n == 0) 
0198     {
0199       GSL_ERROR_VAL ("invalid n, either 0 or exceeds maximum value of generator",
0200                      GSL_EINVAL, 0) ;
0201     }
0202 
0203   scale = range / n;
0204 
0205   do
0206     {
0207       k = (((r->type->get) (r->state)) - offset) / scale;
0208     }
0209   while (k >= n);
0210 
0211   return k;
0212 }
0213 #endif /* HAVE_INLINE */
0214 
0215 __END_DECLS
0216 
0217 #endif /* __GSL_RNG_H__ */