quasi.lisp 3.52 KB
Newer Older
1
2
;; Quasi-random sequences in arbitrary dimensions.
;; Liam Healy, Sun Jul 16 2006 - 15:54
Liam Healy's avatar
Liam Healy committed
3
;; Time-stamp: <2009-12-21 10:21:25EST quasi.lisp>
lhealy's avatar
lhealy committed
4
;; $Id$
5
6
7

(in-package :gsl)

8
9
;;; /usr/include/gsl/gsl_qrng.h

10
11
12
(defmobject quasi-random-number-generator
    "gsl_qrng"
  ((rng-type :pointer) (dimension :uint))
Liam Healy's avatar
Liam Healy committed
13
14
  "quasi random number generator"
  :documentation			; FDL
15
  "Make and optionally initialize the generator q to its starting point.
16
   Note that quasi-random sequences do not use a seed and always produce
17
   the same set of values."
18
19
  :initialize-suffix "init"
  :ri-c-return :void
Liam Healy's avatar
Liam Healy committed
20
  :initialize-args nil)
21

liam's avatar
liam committed
22
(defmfun qrng-get (generator return-vector)
23
  "gsl_qrng_get"
24
  (((mpointer generator) :pointer) ((c-pointer return-vector) :pointer))
Liam Healy's avatar
Liam Healy committed
25
26
  :outputs (return-vector)
  :return (return-vector)
27
28
  :documentation			; FDL
  "Store the next point from the sequence generator q
29
   in the array.  The space available for it must match the
Liam Healy's avatar
Liam Healy committed
30
31
   dimension of the generator.  The point will lie in the range
   0 < x_i < 1 for each x_i.")
32

33
34
(defmfun name ((instance quasi-random-number-generator))
  "gsl_qrng_name" (((mpointer instance) :pointer))
Liam Healy's avatar
Liam Healy committed
35
  :definition :method
36
  :c-return :string)
37

liam's avatar
liam committed
38
(defmfun rng-state ((instance quasi-random-number-generator))
39
  "gsl_qrng_state" (((mpointer instance) :pointer))
Liam Healy's avatar
Liam Healy committed
40
  :definition :method
Liam Healy's avatar
Liam Healy committed
41
  :c-return :pointer
42
  :export nil
43
  :index gsl-random-state)
44

Liam Healy's avatar
Liam Healy committed
45
(defmfun size ((instance quasi-random-number-generator))
46
  "gsl_qrng_size" (((mpointer instance) :pointer))
Liam Healy's avatar
Liam Healy committed
47
  :definition :method
Liam Healy's avatar
Liam Healy committed
48
  :c-return sizet
49
  :export nil
50
  :index gsl-random-state)
51

Liam Healy's avatar
Liam Healy committed
52
(defmfun c-array:copy-to-destination
Liam Healy's avatar
Liam Healy committed
53
54
    ((source quasi-random-number-generator)
     (destination quasi-random-number-generator))
55
  "gsl_qrng_memcpy"
56
  (((mpointer destination) :pointer) ((mpointer source) :pointer))
Liam Healy's avatar
Liam Healy committed
57
  :definition :method
Liam Healy's avatar
Liam Healy committed
58
  :index copy
59
60
61
62
  :documentation			; FDL
  "Copy the quasi-random sequence generator src into the
   pre-existing generator dest, making dest into an exact copy
   of src.  The two generators must be of the same type.")
63

Liam Healy's avatar
Liam Healy committed
64
(defmfun copy-making-destination ((instance quasi-random-number-generator))
65
  "gsl_qrng_clone" (((mpointer instance) :pointer))
Liam Healy's avatar
Liam Healy committed
66
  :definition :method
Liam Healy's avatar
Liam Healy committed
67
68
  :c-return :pointer
  :index copy)
69

70
(def-rng-type +niederreiter2+
71
    ;; FDL
72
    "Described in Bratley, Fox, Niederreiter,
73
     ACM Trans. Model. Comp. Sim. 2, 195 (1992). It is
74
75
76
     valid up to 12 dimensions."
  "gsl_qrng_niederreiter_2")

77
(def-rng-type +sobol+
78
    ;; FDL
79
    "This generator uses the Sobol sequence described in Antonov, Saleev,
liam's avatar
liam committed
80
    USSR Comput. Maths. Math. Phys. 19, 252 (1980). It is valid up to
81
82
83
    40 dimensions."
  "gsl_qrng_sobol")

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(def-rng-type +halton+ 
    ;; FDL
    "The Halton sequence described in J.H. Halton, Numerische
     Mathematik 2, 84-90 (1960) and B. Vandewoestyne and R. Cools
     Computational and Applied Mathematics 189, 1&2, 341-361 (2006). They
     are valid up to 1229 dimensions. "
  "gsl_qrng_halton"
  (1 11))

(def-rng-type +reverse-halton+ 
    ;; FDL
    "The reverse Halton sequence described in J.H. Halton, Numerische
     Mathematik 2, 84-90 (1960) and B. Vandewoestyne and R. Cools
     Computational and Applied Mathematics 189, 1&2, 341-361 (2006). They
     are valid up to 1229 dimensions. "
  "gsl_qrng_reversehalton"
  (1 11))

102
;;; Examples and unit test
Liam Healy's avatar
Liam Healy committed
103
(save-test quasi-random-number-generators
104
  ;; This example is given in the GSL documentation
105
  (let ((gen (make-quasi-random-number-generator +sobol+ 2))
106
	  (vec (make-marray 'double-float :dimensions 2)))
107
108
     (loop repeat 5
	   do (qrng-get gen vec)
Liam Healy's avatar
Liam Healy committed
109
	   append (coerce (cl-array vec) 'list))))