Commit 6698e65b authored by Liam Healy's avatar Liam Healy

Random ported to ffa

The vector usage in random, plus a few sizet types, ported.  Not
tested.
parent 78c58a91
;; Data using ffa
;; Liam Healy 2008-04-06 21:23:41EDT data-ffa.lisp
;; Time-stamp: <2008-07-20 22:28:15EDT data.lisp>
;; Time-stamp: <2008-08-16 20:00:37EDT data.lisp>
;; $Id$
(in-package :gsl)
......@@ -50,6 +50,10 @@
"The first dimension of the object."
(first (dimensions object)))
(defun element-size (object)
"The size of each element as stored in C."
(cffi:foreign-type-size (cl-ffa (element-type object))))
;;;;****************************************************************************
;;;; Definition of specific data classes
;;;;****************************************************************************
......
;; Definition of GSLL system
;; Liam Healy
;; Time-stamp: <2008-08-16 18:24:16EDT gsll.asd>
;; Time-stamp: <2008-08-16 20:05:07EDT gsll.asd>
;; $Id$
(asdf:defsystem "gsll"
......@@ -93,7 +93,6 @@
(:file "eigensystems" :depends-on (init data))
;; Skip fft for now, I'm not sure how it works in C
(:file "numerical-integration" :depends-on (init))
#+no
(:module random
:depends-on (init)
:components
......
;; Dirichlet distribution
;; Liam Healy, Sun Oct 29 2006
;; Time-stamp: <2008-03-09 19:29:16EDT dirichlet.lisp>
;; Time-stamp: <2008-08-16 18:45:15EDT dirichlet.lisp>
;; $Id$
(in-package :gsl)
......@@ -8,10 +8,10 @@
(defmfun dirichlet (generator alpha theta)
"gsl_ran_dirichlet"
(((generator generator) :pointer)
((dim0 alpha) size)
((gsl-array alpha) :pointer)
((dim0 alpha) sizet)
((c-pointer alpha) :pointer)
;; theta had better be at least as long as alpha, or they'll be trouble
((gsl-array theta) :pointer))
((c-pointer theta) :pointer))
:c-return :void
:documentation ; FDL
"An array of K=(length alpha) random variates from a Dirichlet
......@@ -31,10 +31,10 @@
(defmfun dirichlet-pdf (alpha theta)
"gsl_ran_dirichlet_pdf"
(((1- (dim0 alpha)) size)
((gsl-array alpha) :pointer)
(((1- (dim0 alpha)) sizet)
((c-pointer alpha) :pointer)
;; theta had better be at least as long as alpha, or they'll be trouble
((gsl-array theta) :pointer))
((c-pointer theta) :pointer))
:c-return :double
:documentation ; FDL
"The probability density p(\theta_1, ... , \theta_K)
......@@ -43,10 +43,10 @@
(defmfun dirichlet-log-pdf (alpha theta)
"gsl_ran_dirichlet_lnpdf"
(((1- (dim0 alpha)) size)
((gsl-array alpha) :pointer)
(((1- (dim0 alpha)) sizet)
((c-pointer alpha) :pointer)
;; theta had better be at least as long as alpha, or they'll be trouble
((gsl-array theta) :pointer))
((c-pointer theta) :pointer))
:c-return :double
:documentation ; FDL
"The logarithm of the probability density
......
;; Discrete random variables
;; Liam Healy, Sat Nov 11 2006 - 21:51
;; Time-stamp: <2008-03-09 19:29:15EDT discrete.lisp>
;; Time-stamp: <2008-08-16 18:48:55EDT discrete.lisp>
;; $Id$
(in-package :gsl)
(cffi:defcstruct discrete-t
"Structure for Walker algorithm."
(K size)
(A :pointer)
(F :pointer))
(defgo-s (discrete-random probabilities) discrete-preprocess discrete-free)
(defmfun discrete-preprocess (probabilities)
"gsl_ran_discrete_preproc"
(((dim0 probabilities) size) ((gsl-array probabilities) :pointer))
(((dim0 probabilities) sizet) ((c-pointer probabilities) :pointer))
:c-return :pointer
:export nil
:index (letm discrete-random)
......@@ -38,14 +32,14 @@
(defmfun discrete (generator table)
"gsl_ran_discrete"
(((generator generator) :pointer) (table :pointer))
:c-return size
:c-return sizet
:documentation ; FDL
"Generate discrete random numbers after running #'discrete-preprocess;
the argument 'table is the value returned by #'discrete-preprocess.")
(defmfun discrete-pdf (k table)
"gsl_ran_discrete_pdf"
((k size) (table :pointer))
((k sizet) (table :pointer))
:c-return :double
:documentation ; FDL
"The probability P[k] of observing the variable k.
......
;; Generators of random numbers.
;; Liam Healy, Sat Jul 15 2006 - 14:43
;; Time-stamp: <2008-02-17 12:15:46EST generators.lisp>
;; Time-stamp: <2008-08-16 20:15:24EDT generators.lisp>
;; $Id$
(in-package :gsl)
......@@ -45,9 +45,11 @@
;;;; Initialization
;;;;****************************************************************************
;;; These should have defgenerics
(defmfun alloc ((generator random-number-generator))
"gsl_rng_alloc" (((rng-type generator) :pointer))
:type :method
:definition :method
:export t
:c-return (ptr :pointer)
:return ((progn (setf (generator generator) ptr) generator))
:documentation ; FDL
......@@ -60,7 +62,8 @@
(defmfun free ((generator random-number-generator))
"gsl_rng_free" (((generator generator) :pointer))
:type :method
:definition :method
:export t
:c-return :void
:after ((setf (generator generator) nil))
:documentation ; FDL
......@@ -150,7 +153,7 @@
(defmfun rng-name ((rng-instance random-number-generator))
"gsl_rng_name" (((generator rng-instance) :pointer))
:type :method
:definition :method
:c-return :string)
(defmfun rng-max (rng-instance)
......@@ -176,7 +179,7 @@
(defmfun rng-state ((rng-instance random-number-generator))
"gsl_rng_state" (((generator rng-instance) :pointer))
:c-return :pointer
:type :method
:definition :method
:index gsl-random-state)
(export 'rng-size)
......@@ -186,8 +189,8 @@
(defmfun rng-size ((rng-instance random-number-generator))
"gsl_rng_size" (((generator rng-instance) :pointer))
:c-return size
:type :method
:c-return sizet
:definition :method
:index gsl-random-state)
(export 'gsl-random-state)
......@@ -212,7 +215,7 @@
((destination random-number-generator) (source random-number-generator))
"gsl_rng_memcpy"
(((generator destination) :pointer) ((generator source) :pointer))
:type :method
:definition :method
:documentation ; FDL
"Copy the random number generator source into the
pre-existing generator destination,
......@@ -228,19 +231,19 @@
(defmfun clone-generator ((instance random-number-generator))
"gsl_rng_clone" (((generator instance) :pointer))
:c-return :pointer
:type :method)
:definition :method)
(defmfun write-binary
((object random-number-generator) stream)
"gsl_rng_fwrite"
((stream :pointer) ((generator object) :pointer))
:type :method)
:definition :method)
(defmfun read-binary
((object random-number-generator) stream)
"gsl_block_fread"
((stream :pointer) ((pointer object) :pointer))
:type :method)
:definition :method)
;;;;****************************************************************************
;;;; Examples and unit test
......
;; Multinomial distribution
;; Liam Healy, Sat Nov 25 2006 - 16:00
;; Time-stamp: <2008-03-09 19:29:15EDT multinomial.lisp>
;; Time-stamp: <2008-08-16 18:52:11EDT multinomial.lisp>
;; $Id$
(in-package :gsl)
......@@ -8,11 +8,11 @@
(defmfun multinomial (generator sum p n)
"gsl_ran_multinomial"
(((generator generator) :pointer)
((dim0 p) size)
(sum size)
((gsl-array p) :pointer)
((dim0 p) sizet)
(sum sizet)
((c-pointer p) :pointer)
;; technically, n should be a uint array, but integers work
((gsl-array n) :pointer))
((c-pointer n) :pointer))
:c-return :void
:documentation ; FDL
"Returns an array n of K random variates from a
......@@ -31,7 +31,7 @@
(defmfun multinomial-pdf (p n)
"gsl_ran_multinomial_pdf"
(((dim0 p) :uint) ((gsl-array p) :pointer) ((gsl-array n) :pointer))
(((dim0 p) sizet) ((c-pointer p) :pointer) ((c-pointer n) :pointer))
:c-return :double
:documentation ; FDL
"Compute the probability P(n_1, n_2, ..., n_K)
......@@ -40,7 +40,7 @@
(defmfun multinomial-log-pdf (p n)
"gsl_ran_multinomial_lnpdf"
(((dim0 p) :uint) ((gsl-array p) :pointer) ((gsl-array n) :pointer))
(((dim0 p) sizet) ((c-pointer p) :pointer) ((c-pointer n) :pointer))
:c-return :double
:documentation ; FDL
"Compute the natural logarithm of the probability P(n_1, n_2, ..., n_K)
......
;; Quasi-random sequences in arbitrary dimensions.
;; Liam Healy, Sun Jul 16 2006 - 15:54
;; Time-stamp: <2008-03-09 19:29:16EDT quasi.lisp>
;; Time-stamp: <2008-08-16 20:17:54EDT quasi.lisp>
;; $Id$
(in-package :gsl)
......@@ -30,7 +30,7 @@
(defmfun alloc ((generator quasi-random-number-generator))
"gsl_qrng_alloc"
(((rng-type generator) :pointer) ((qr-dimension generator) :uint))
:type :method
:definition :method
:c-return (ptr :pointer)
:return ((progn (setf (generator generator) ptr) generator))
:documentation ; FDL
......@@ -43,7 +43,7 @@
(defmfun free ((generator quasi-random-number-generator))
"gsl_qrng_free" (((generator generator) :pointer))
:type :method
:definition :method
:c-return :void
:after ((setf (generator generator) nil))
:documentation ; FDL
......@@ -59,30 +59,31 @@
(defmfun qrng-get (generator return-vector)
"gsl_qrng_get"
(((generator generator) :pointer) ((gsl-array return-vector) :pointer))
(((generator generator) :pointer) ((c-pointer return-vector) :pointer))
:outputs (return-vector)
:return (return-vector)
:documentation ; FDL
"Store the next point from the sequence generator q
in the array x. The space available for x must match the
dimension of the generator. The point x will lie in the range
0 < x_i < 1 for each x_i."
:invalidate (return-vector))
in the array. The space available fopr it must match the
dimension of the generator. The point will lie in the range
0 < x_i < 1 for each x_i.")
(defmfun rng-name ((instance quasi-random-number-generator))
"gsl_qrng_name" (((generator instance) :pointer))
:type :method
:definition :method
:c-return :string)
(defmfun rng-state ((instance quasi-random-number-generator))
"gsl_qrng_state" (((generator instance) :pointer))
:c-return :pointer
:type :method
:definition :method
:export nil
:index gsl-random-state)
(defmfun rng-size ((instance quasi-random-number-generator))
"gsl_qrng_size" (((generator instance) :pointer))
:c-return size
:type :method
:c-return sizet
:definition :method
:export nil
:index gsl-random-state)
......@@ -91,7 +92,7 @@
(source quasi-random-number-generator))
"gsl_qrng_memcpy"
(((generator destination) :pointer) ((generator source) :pointer))
:type :method
:definition :method
:documentation ; FDL
"Copy the quasi-random sequence generator src into the
pre-existing generator dest, making dest into an exact copy
......@@ -100,7 +101,7 @@
(defmfun clone-generator ((instance quasi-random-number-generator))
"gsl_qrng_clone" (((generator instance) :pointer))
:c-return :pointer
:type :method
:definition :method
:documentation ; FDL
"Create a new generator which is an exact copy of the original.
Don't use; use #'make-random-number-generator, #'copy instead.")
......
;; Random number generation
;; Liam Healy, Tue Jul 11 2006 - 23:39
;; Time-stamp: <2008-02-17 12:07:47EST rng-types.lisp>
;; Time-stamp: <2008-08-16 18:30:49EDT rng-types.lisp>
;; $Id$
;;; Random number generator types and information functions.
(in-package :gsl)
;;;;****************************************************************************
;;;; Random number generator types
;;;;****************************************************************************
;;; needed?
(cffi:defcstruct random-number-generator-type
(name :pointer)
(max :unsigned-long)
(min :unsigned-long)
(size size)
(set :pointer)
(get :pointer)
(get-double :pointer))
;;;;****************************************************************************
;;;; Auxiliary functions
;;;;****************************************************************************
......
;; Shuffling and sampling
;; Liam Healy, Sat Dec 2 2006 - 18:40
;; Time-stamp: <2008-02-17 16:31:02EST shuffling-sampling.lisp>
;; Time-stamp: <2008-08-16 18:55:38EDT shuffling-sampling.lisp>
;; $Id$
(in-package :gsl)
......@@ -10,7 +10,7 @@
(defmfun shuffle (generator base)
"gsl_ran_shuffle"
(((generator generator) :pointer)
((gsl-array base) :pointer) ((dim0 base) size) ((element-size base) size))
((c-pointer base) :pointer) ((dim0 base) sizet) ((element-size base) sizet))
:c-return :void
:documentation ; FDL
"Randomly shuffle the order of n objects, each of
......@@ -23,8 +23,8 @@
(defmfun choose-random (generator dest src)
"gsl_ran_choose"
(((generator generator) :pointer)
((gsl-array dest) :pointer) ((dim0 dest) size)
((gsl-array src) :pointer) ((dim0 src) size) ((element-size src) size))
((c-pointer dest) :pointer) ((dim0 dest) sizet)
((c-pointer src) :pointer) ((dim0 src) sizet) ((element-size src) sizet))
;; This is described in the GSL docs as returning int, but it does
;; not say what the return value means. Therefore, we ignore it.
:c-return :void
......@@ -45,8 +45,8 @@
(defmfun sample (generator dest src)
"gsl_ran_sample"
(((generator generator) :pointer)
((gsl-array dest) :pointer) ((dim0 dest) size)
((gsl-array src) :pointer) ((dim0 src) size) ((element-size src) size))
((c-pointer dest) :pointer) ((dim0 dest) sizet)
((c-pointer src) :pointer) ((dim0 src) sizet) ((element-size src) sizet))
:c-return :void
:documentation
"Like #'choose-random, but samples k items
......
;; Spherical Vector distribution
;; Liam Healy, Sun Oct 22 2006
;; Time-stamp: <2008-02-17 13:25:44EST spherical-vector.lisp>
;; Time-stamp: <2008-08-16 18:43:18EDT spherical-vector.lisp>
;; $Id$
(in-package :gsl)
......@@ -38,11 +38,13 @@
projected along any axis is actually uniform (this is only true for 3
dimensions).")
(defmfun direction-Nd (generator x)
(defmfun direction-Nd (generator vector)
"gsl_ran_dir_nd"
(((generator generator) :pointer) ((dim0 x) size) ((gsl-array x) :pointer))
(((generator generator) :pointer) ((dim0 vector) sizet)
((c-pointer vector) :pointer))
:c-return :void
:return (x)
:outputs (vector)
:return (vector)
:documentation ; FDL
"A random direction vector v = (x_1,x_2,...,x_n) in n dimensions,
where n is the length of the vector x passed in. The vector is normalized such that
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment