/[cl-gsl]/cl-gsl/random-number-generator.lisp
ViewVC logotype

Contents of /cl-gsl/random-number-generator.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Mon May 16 01:24:19 2005 UTC (8 years, 11 months ago) by edenny
Branch: MAIN
CVS Tags: HEAD
Initial check in.
1 ;;;; -*- Mode: Lisp; Synatx: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;;
3 ;;;; Copyright (C) 2005 Edgar Denny <edgardenny@comcast.net>
4 ;;;; This file is part of CL-GSL.
5 ;;;;
6 ;;;; This program is free software; you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation; either version 2 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; This program is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with this program; if not, write to the Free Software
18 ;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 (in-package #:cl-gsl-rng)
21
22 (defstruct rng
23 ptr)
24
25 ;; ----------------------------------------------------------------------
26
27 (defun-foreign "wrap_gsl_rng_set_type"
28 ((tn :cstring))
29 gsl-rng-ptr)
30
31 (defun make-generator (name)
32 "Returns a newly created instance of a random number generator. NAME is
33 a string specifying the random number generator algorithm. Instance must
34 be destroyed with a call to FREE-GENERATOR."
35 (uffi:with-cstring (c-name name)
36 (make-rng :ptr (wrap-gsl-rng-set-type c-name))))
37
38 ;; ----------------------------------------------------------------------
39
40 (defun-foreign "gsl_rng_free"
41 ((r gsl-rng-ptr))
42 :void)
43
44 (defun free-generator (generator)
45 "Destroys the random number generator instance GENERATOR."
46 (assert (typep generator 'rng))
47 (gsl-rng-free (rng-ptr generator))
48 (setf (rng-ptr generator) nil))
49
50 ;; ----------------------------------------------------------------------
51
52 (defmacro with-generator ((generator name) &body body)
53 "Creates a new random number generator of type specified by the string
54 NAME, and binds it to GENERATOR. The generator is destroyed when the form is
55 exited."
56 `(let ((,generator (make-generator ,name)))
57 (unwind-protect
58 (progn ,@body)
59 (free-generator ,generator))))
60
61 ;; ----------------------------------------------------------------------
62 (defun-foreign "gsl_rng_set"
63 ((r gsl-rng-ptr)
64 (s size-t))
65 :void)
66
67 (defun seed (generator num)
68 "Initialize (or seed) the random number generator GENERATOR with the
69 integer NUM. Returns GENERATOR."
70 (assert (typep generator 'rng))
71 (assert (typep num 'integer))
72 (gsl-rng-set (rng-ptr generator) num)
73 generator)
74
75 ;; ----------------------------------------------------------------------
76
77 (defun-foreign "gsl_rng_get"
78 ((r gsl-rng-ptr))
79 size-t)
80
81 (defun get-integer (generator)
82 "Returns an integer unifromly distributed in the range [min,max].
83 The minimum and maximum values of the range depend upon the GENERATOR used."
84 (assert (typep generator 'rng))
85 (gsl-rng-get (rng-ptr generator)))
86
87 ;; ----------------------------------------------------------------------
88
89 (defun-foreign "gsl_rng_uniform"
90 ((r gsl-rng-ptr))
91 :double)
92
93 (defun get-double-float (generator)
94 "Returns a double-float uniformly distributed in the range [0,1].
95 The range includes 0.0 but excludes 1.0."
96 (assert (typep generator 'rng))
97 (gsl-rng-uniform (rng-ptr generator)))
98
99 ;; ----------------------------------------------------------------------
100
101 (defun-foreign "gsl_rng_uniform_pos"
102 ((r gsl-rng-ptr))
103 :double)
104
105 (defun get-double-float-pos (generator)
106 "Returns a positive double-float uniformly distributed in the range [0,1].
107 The range excludes both 0.0 and 1.0."
108 (assert (typep generator 'rng))
109 (gsl-rng-uniform-pos (rng-ptr generator)))
110
111 ;; ----------------------------------------------------------------------
112
113 (defun-foreign "gsl_rng_uniform_int"
114 ((r gsl-rng-ptr)
115 (n size-t))
116 size-t)
117
118 (defun get-integer-in-range (generator num)
119 "Returns an integer unifromly distributed in the range [0,(1- NUM)].
120 The minimum and maximum values of the range depend upon the GENERATOR used."
121 (assert (typep generator 'rng))
122 (assert (typep num 'integer))
123 (gsl-rng-uniform-int (rng-ptr generator) num))
124
125
126 ;; ----------------------------------------------------------------------
127
128 (defun-foreign "gsl_rng_name"
129 ((r gsl-rng-ptr))
130 :cstring)
131
132 (defun generator-name (generator)
133 "Returns a string giving the name of the random number generator GENERATOR."
134 (assert (typep generator 'rng))
135 (uffi:convert-from-cstring (gsl-rng-name (rng-ptr generator))))
136
137 ;; ----------------------------------------------------------------------
138
139 (defun-foreign "gsl_rng_max"
140 ((r gsl-rng-ptr))
141 size-t)
142
143 (defun generator-max (generator)
144 "Returns the largest value that (GET-INTEGER GENERATOR) can return."
145 (assert (typep generator 'rng))
146 (gsl-rng-max (rng-ptr generator)))
147
148 ;; ----------------------------------------------------------------------
149
150 (defun-foreign "gsl_rng_min"
151 ((r gsl-rng-ptr))
152 size-t)
153
154 (defun generator-min (generator)
155 "Returns the smallest value that (GET-INTEGER GENERATOR) can return."
156 (assert (typep generator 'rng))
157 (gsl-rng-min (rng-ptr generator)))
158
159 ;; ----------------------------------------------------------------------
160
161 (defun-foreign "gsl_rng_clone"
162 ((r gsl-rng-ptr))
163 gsl-rng-ptr)
164
165 (defun clone (generator)
166 "Returns a newly created copy of the random number generator GENERATOR."
167 (assert (typep generator 'rng))
168 (make-rng :ptr (gsl-rng-clone (rng-ptr generator))))
169
170 (defmacro with-clone ((generator-clone generator) &body body)
171 "Create a newly created random number generator which is an extact copy of
172 GENERATOR, and bind it to GENERATOR-CLONE. The generator is destroyed when
173 the form is exited."
174 `(let ((,generator-clone (clone ,generator)))
175 (unwind-protect
176 (progn ,@body)
177 (free-generator ,generator-clone))))
178
179 ;; ----------------------------------------------------------------------
180
181 (defun-foreign "wrap_gsl_rng_fwrite"
182 ((fn :cstring)
183 (r gsl-rng-ptr))
184 :int)
185
186 (defun write-state-to-file (file-name generator)
187 "Writes the random number state of the random number generator GENERATOR
188 to the file given by the string FILE-NAME. Returns 0 for success and
189 +EFAILED+ for failure."
190 (assert (typep generator 'rng))
191 (let ((status))
192 (uffi:with-cstring (c-file-name file-name)
193 (setq status (wrap-gsl-rng-fwrite c-file-name (rng-ptr generator))))
194 status))
195
196 ;; ----------------------------------------------------------------------
197
198 (defun-foreign "wrap_gsl_rng_fread"
199 ((fn :cstring)
200 (r gsl-rng-ptr))
201 :int)
202
203 (defun read-state-from-file (file-name generator)
204 "Reads the random number state of the random number generator GENERATOR
205 from the file given by the string FILE-NAME. Return 0 for success and
206 +EFAILED+ for failure. GENERATOR must be created with the correct random
207 number generator."
208 (assert (typep generator 'rng))
209 (let ((status))
210 (uffi:with-cstring (c-file-name file-name)
211 (setq status (wrap-gsl-rng-fread c-file-name (rng-ptr generator))))
212 status))

  ViewVC Help
Powered by ViewVC 1.1.5