Skip to content

Commit 4df40d4

Browse files
authored
Gsl rng refactor (#14)
* chores(refactor): RNG module * fix(doc): Improve GSL random module doc - Fix typo - Add gsl-rng-seed accessor
1 parent e42f7ca commit 4df40d4

7 files changed

Lines changed: 96 additions & 125 deletions

File tree

doc/source/gsl-rng.rst

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Random generator base class
102102

103103
.. code-block:: dylan
104104
105-
let r1 = make(<gsl-rng>, type: $gsl-rng-cmrg>, seed: 123456);
105+
let r1 = make(<gsl-rng>, type: $gsl-rng-cmrg, seed: 123456);
106106
let r2 = make(<gsl-rng>, seed: 123456);
107107
let r3 = make(<gsl-rng>);
108108
@@ -116,6 +116,8 @@ Random generator base class
116116

117117
- :func:`gsl-rng-uniform-integer`
118118

119+
- :meth:`gsl-rng-seed(<gsl-rng>)`
120+
119121
- :func:`gsl-rng-name`
120122

121123
- :func:`gsl-rng-min`
@@ -262,6 +264,31 @@ Auxiliary functions
262264
format-out("%s", r.gsl-rng-name);
263265
// rand
264266
267+
.. method:: gsl-rng-seed
268+
:specializer: <gsl-rng>
269+
270+
Returns the seed of the random number generator
271+
272+
:signature:
273+
274+
gsl-rng-seed (rng) => (seed)
275+
276+
:parameter rng:
277+
278+
An instance of :class:`<gsl-rng>`
279+
280+
:values seed:
281+
282+
An instance of :drm:`<integer>`
283+
284+
:example:
285+
286+
.. code-block:: dylan
287+
288+
let r = make(<gsl-rng>, seed: 1234);
289+
format-out("Seed: %d\n", r.gsl-rng-seed);
290+
// Seed: 1234
291+
265292
.. function:: gsl-rng-min
266293

267294
Returns the smallest value returned by the random number

src/lib/ffi/ffi-modules.dylan

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,9 @@ end module gsl-ffi-vector-impl;
567567

568568
define module gsl-ffi-rng
569569
create
570-
<gsl-rng-type*>,
570+
<gsl-rng-type*>;
571+
572+
create
571573
<gsl-rng-type**>,
572574
gsl-rng-types-setup,
573575
gsl-rng-env-setup,
@@ -590,7 +592,6 @@ define module gsl-ffi-rng
590592
gsl-rng-uniform,
591593
gsl-rng-uniform-int,
592594
gsl-rng-uniform-pos;
593-
594595
end module gsl-ffi-rng;
595596

596597
define module gsl-ffi-rng-impl

src/lib/gsl/gsl-modules.dylan

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,17 @@ Copyright: Copyright (C) 2026, Dylan Hackers. All rights reserved.
55
License: See License.txt in this distribution for details.
66

77
define module gsl-error
8-
98
use gsl-ffi-error,
109
export: all;
11-
12-
end module gsl-error;
10+
end module;
1311

1412
define module gsl-error-impl
15-
1613
use common-dylan;
1714
use gsl-ffi-error;
1815
use gsl-error;
19-
20-
end module gsl-error-impl;
16+
end module;
2117

2218
define module gsl-common
23-
2419
create
2520
<double-float?>,
2621
<vector-double-float>,
@@ -30,23 +25,18 @@ define module gsl-common
3025

3126
create
3227
with-c-double-array;
33-
34-
end module gsl-common;
28+
end module;
3529

3630
define module gsl-common-impl
37-
3831
use common-dylan;
3932
use c-ffi;
4033
use gsl-common;
41-
42-
end module gsl-common-impl;
34+
end module;
4335

4436
define module gsl-math
45-
4637
use gsl-ffi-math,
4738
export: all;
4839

49-
5040
// double float comparison
5141

5242
create
@@ -61,16 +51,14 @@ define module gsl-math
6151
infinity?,
6252
positive-infinity?,
6353
negative-infinity?;
64-
65-
end module gsl-math;
54+
end module;
6655

6756
define module gsl-math-impl
6857
use common-dylan;
6958
use c-ffi;
7059
use gsl-ffi-math;
71-
7260
use gsl-math;
73-
end module gsl-math-impl;
61+
end module;
7462

7563
define module gsl-complex
7664
create
@@ -159,23 +147,19 @@ define module gsl-complex
159147
gsl-complex-arcsech,
160148
gsl-complex-arccsch,
161149
gsl-complex-arccoth;
162-
163-
end module gsl-complex;
150+
end module;
164151

165152
define module gsl-complex-impl
166-
167153
use common-dylan;
168154
use c-ffi;
169155
use finalization;
170156
use gsl-ffi-complex,
171157
prefix: "ffi/";
172158
use gsl-complex;
173-
174-
end module gsl-complex-impl;
159+
end module;
175160

176161
define module gsl-vector
177-
178-
create
162+
create
179163
<gsl-vector>,
180164
gsl-vector,
181165
gsl-vector-copy,
@@ -203,28 +187,25 @@ define module gsl-vector
203187
create
204188
null?,
205189
non-negative?;
206-
207-
end module gsl-vector;
190+
end module;
208191

209192
define module gsl-vector-impl
210193
use common-dylan;
211194
use c-ffi;
212195
use finalization;
213196
use gsl-math,
214-
import: { *epsilon*,
215-
f= };
197+
import: { *epsilon*, f= };
216198
use gsl-ffi-vector,
217199
prefix: "ffi/";
218-
219200
use gsl-vector;
201+
220202
export
221203
%gsl-vector,
222204
%gsl-vector-data,
223205
%gsl-vector-data-setter;
224-
end module gsl-vector-impl;
206+
end module;
225207

226208
define module gsl-statistics
227-
228209
create
229210
mean,
230211
variance,
@@ -261,44 +242,28 @@ define module gsl-statistics
261242
sn,
262243
qn0,
263244
qn;
264-
265-
end module gsl-statistics;
245+
end module;
266246

267247
define module gsl-statistics-impl
268-
269248
use common-dylan;
270249
use c-ffi;
271-
272250
use gsl-common,
273-
import: { <double-float?>,
274-
<vector-double-float>,
275-
with-c-double-array };
276-
251+
import: { <double-float?>, <vector-double-float>, with-c-double-array };
277252
use gsl-vector,
278-
import: { <gsl-vector>,
279-
gsl-vector-stride },
253+
import: { <gsl-vector>, gsl-vector-stride },
280254
rename: { min-index => vector-min-index,
281-
max-index => vector-max-index,
255+
max-index => vector-max-index,
282256
minmax-index => vector-minmax-index };
283257
use gsl-vector-impl,
284-
import: { %gsl-vector,
285-
%gsl-vector-data,
286-
%gsl-vector-data-setter };
287-
258+
import: { %gsl-vector, %gsl-vector-data, %gsl-vector-data-setter };
288259
use gsl-ffi-statistics;
289260
use gsl-statistics;
290-
291261
end module;
292262

293263
define module gsl-running-statistics
294-
295-
create
296-
<rstat>;
297-
298-
create
299-
rstat-add!;
300-
301264
create
265+
<rstat>,
266+
rstat-add!,
302267
rstat-reset!;
303268

304269
create
@@ -322,23 +287,18 @@ define module gsl-running-statistics
322287
quantile-rstat-add!,
323288
quantile-rstat-reset!,
324289
quantile-rstat-get;
325-
326-
end module gsl-running-statistics;
290+
end module;
327291

328292
define module gsl-running-statistics-impl
329-
330293
use common-dylan;
331294
use c-ffi;
332295
use gsl-common;
333-
334296
use gsl-ffi-running-statistics,
335297
prefix: "ffi/";
336298
use gsl-running-statistics;
337-
338-
end module gsl-running-statistics-impl;
299+
end module;
339300

340301
define module gsl-rng
341-
342302
create
343303
gsl-rng-env-setup;
344304

@@ -418,14 +378,17 @@ define module gsl-rng
418378
$gsl-rng-vax,
419379
$gsl-rng-waterman14,
420380
$gsl-rng-zuf;
421-
422381
end module;
423382

424383
define module gsl-rng-impl
425384
use common-dylan;
385+
use format-out;
426386
use finalization;
387+
use uncommon-utils;
427388
use gsl-ffi-rng,
428389
prefix: "ffi/";
429-
use uncommon-utils;
430390
use gsl-rng;
431-
end module gsl-rng-impl;
391+
392+
export
393+
gsl-rng-ffi;
394+
end module;

src/lib/gsl/rng.dylan

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,74 +75,68 @@ define constant <gsl-rng-type>
7575
define constant $gsl-rng-types :: ffi/<gsl-rng-type**>
7676
= ffi/gsl-rng-types-setup();
7777

78-
define function gsl-rng-env-setup
79-
() => ()
80-
ffi/gsl-rng-env-setup();
81-
end;
78+
define constant gsl-rng-env-setup
79+
= ffi/gsl-rng-env-setup;
8280

8381
define class <gsl-rng> (<object>)
8482
constant slot gsl-rng-seed :: <integer>,
8583
init-keyword: seed:,
8684
init-value: ffi/gsl-rng-default-seed();
87-
slot %gsl-rng :: ffi/<gsl-rng*>;
85+
slot gsl-rng-ffi :: ffi/<gsl-rng*>;
8886
end;
8987

9088
define method initialize
91-
(rng :: <gsl-rng>,
92-
#key type :: false-or(<gsl-rng-type>) = #f)
89+
(r :: <gsl-rng>, #key type :: false-or(<gsl-rng-type>) = #f)
9390
=> ()
9491
drain-finalization-queue();
9592
next-method();
96-
9793
let rng-type :: ffi/<gsl-rng-type*>
98-
= if (~type)
99-
ffi/gsl-rng-default-type()
100-
else
94+
= if (type)
10195
$gsl-rng-types[type]
96+
else
97+
ffi/gsl-rng-default-type()
10298
end;
103-
104-
rng.%gsl-rng := ffi/gsl-rng-alloc(rng-type);
105-
106-
finalize-when-unreachable(rng);
99+
r.gsl-rng-ffi := ffi/gsl-rng-alloc(rng-type);
100+
ffi/gsl-rng-set(r.gsl-rng-ffi, r.gsl-rng-seed);
101+
finalize-when-unreachable(r);
107102
end;
108103

109104
define method finalize
110105
(r :: <gsl-rng>) => ()
111-
ffi/gsl-rng-free(r.%gsl-rng)
106+
ffi/gsl-rng-free(r.gsl-rng-ffi)
112107
end;
113108

114109
define function gsl-rng-name
115110
(r :: <gsl-rng>) => (_ :: <string>)
116-
ffi/gsl-rng-name(r.%gsl-rng)
111+
ffi/gsl-rng-name(r.gsl-rng-ffi)
117112
end;
118113

119114
define function gsl-rng-min
120115
(r :: <gsl-rng>) => (_ :: <integer>)
121-
ffi/gsl-rng-min(r.%gsl-rng)
116+
ffi/gsl-rng-min(r.gsl-rng-ffi)
122117
end;
123118

124119
define function gsl-rng-max
125120
(r :: <gsl-rng>) => (_ :: <integer>)
126-
ffi/gsl-rng-max(r.%gsl-rng)
121+
ffi/gsl-rng-max(r.gsl-rng-ffi)
127122
end;
128123

129124
define function gsl-rng-get
130125
(r :: <gsl-rng>) => (_ :: <integer>)
131-
ffi/gsl-rng-get(r.%gsl-rng)
126+
ffi/gsl-rng-get(r.gsl-rng-ffi)
132127
end;
133128

134129
define function gsl-rng-uniform
135130
(r :: <gsl-rng>) => (_ :: <float>)
136-
ffi/gsl-rng-uniform(r.%gsl-rng)
131+
ffi/gsl-rng-uniform(r.gsl-rng-ffi)
137132
end;
138133

139134
define function gsl-rng-uniform-positive
140135
(r :: <gsl-rng>) => (_ :: <float>)
141-
ffi/gsl-rng-uniform-pos(r.%gsl-rng)
136+
ffi/gsl-rng-uniform-pos(r.gsl-rng-ffi)
142137
end;
143138

144139
define function gsl-rng-uniform-int
145140
(r :: <gsl-rng>, n :: <integer>) => (_ :: <integer>)
146-
ffi/gsl-rng-uniform-int(r.%gsl-rng, n)
141+
ffi/gsl-rng-uniform-int(r.gsl-rng-ffi, n)
147142
end;
148-

0 commit comments

Comments
 (0)