Commit cd6a85f8 authored by Liam Healy's avatar Liam Healy

Keyword arguments to defmobject

Change optional arguments to defmobject to keyword arguments, and add
:gsl-version argument that will create the maker function to signal an
error if the installed version of GSL is too old.
parent b134ed94
;; Basis splines.
;; Liam Healy 2008-02-18 14:43:20EST basis-splines.lisp
;; Time-stamp: <2008-12-26 18:25:41EST basis-splines.lisp>
;; Time-stamp: <2009-01-25 10:11:48EST basis-splines.lisp>
;; $Id$
(in-package :gsl)
......@@ -11,7 +11,8 @@
(defmobject basis-spline "gsl_bspline"
((order sizet) (number-of-breakpoints sizet))
"basis spline" ; FDL
"basis spline"
:documentation ; FDL
"Allocate a workspace for computing B-splines. The number of
breakpoints is given by number-of-breakpoints. This leads to n =
nbreak + k - 2 basis functions where k = order. Cubic B-splines are
......
;; Monte Carlo Integration
;; Liam Healy Sat Feb 3 2007 - 17:42
;; Time-stamp: <2009-01-24 19:32:18EST monte-carlo.lisp>
;; Time-stamp: <2009-01-25 10:21:38EST monte-carlo.lisp>
;; $Id$
(in-package :gsl)
......@@ -17,10 +17,11 @@
(defmobject monte-carlo-plain
"gsl_monte_plain"
((dim sizet))
"plain Monte Carlo integration" ; FDL
"plain Monte Carlo integration"
:documentation ; FDL
"Make and initialize a workspace for Monte Carlo integration in dim dimensions."
"init"
nil)
:initialize-suffix "init"
:initialize-args nil)
(cffi:defcstruct plain-state
(dim sizet)
......@@ -59,12 +60,13 @@
(defmobject monte-carlo-miser
"gsl_monte_miser"
((dim sizet))
"miser Monte Carlo integration" ; FDL
"miser Monte Carlo integration"
:documentation ; FDL
"Make and initialize a workspace for Monte Carlo integration in
dim dimensions. The workspace is used to maintain
the state of the integration."
"init"
nil)
:initialize-suffix "init"
:initialize-args nil)
(cffi:defcstruct miser-state
(min-calls sizet)
......@@ -133,12 +135,13 @@
(defmobject monte-carlo-vegas
"gsl_monte_vegas"
((dim sizet))
"vegas Monte Carlo integration" ; FDL
"vegas Monte Carlo integration"
:documentation ; FDL
"Make and initialize a workspace for Monte Carlo integration in
dim dimensions. The workspace is used to maintain
the state of the integration. Returns a pointer to vegas-state."
"init"
nil)
:initialize-suffix "init"
:initialize-args nil)
(cffi:defcstruct vegas-state
;; grid
......
;; Numerical integration
;; Liam Healy, Wed Jul 5 2006 - 23:14
;; Time-stamp: <2009-01-24 14:48:39EST numerical-integration.lisp>
;; Time-stamp: <2009-01-25 10:16:31EST numerical-integration.lisp>
;; $Id$
;;; To do: QAWS, QAWO, QAWF, more tests
......@@ -40,9 +40,10 @@
(defmobject integration-workspace
"gsl_integration_workspace" ((size sizet))
"integration workspace" ; FDL
"integration workspace"
:documentation ; FDL
"Make a workspace sufficient to hold n double
precision intervals, their integration results and error estimates.")
precision intervals, their integration results and error estimates.")
(cffi:defcenum integrate-method
:gauss15 :gauss21 :gauss31
......
;; Chebyshev Approximations
;; Liam Healy Sat Nov 17 2007 - 20:36
;; Time-stamp: <2009-01-24 12:56:27EST chebyshev.lisp>
;; Time-stamp: <2009-01-25 10:12:45EST chebyshev.lisp>
;; $Id$
(in-package :gsl)
......@@ -13,9 +13,11 @@
(defmobject chebyshev "gsl_cheb"
((order sizet))
"Chebyshev series" ; FDL
"Chebyshev series"
:documentation ; FDL
"Make a Chebyshev series of specified order."
"init"
:initialize-suffix "init"
:initialize-args
((function :pointer) (lower-limit :double) (upper-limit :double)))
;;;;****************************************************************************
......
;; Eigenvectors and eigenvalues
;; Liam Healy, Sun May 21 2006 - 19:52
;; Time-stamp: <2009-01-12 10:22:00EST eigensystems.lisp>
;; Time-stamp: <2009-01-25 10:37:09EST eigensystems.lisp>
;; $Id$
(in-package :gsl)
......@@ -17,15 +17,17 @@
(defmobject eigen-symm
"gsl_eigen_symm" ((n sizet))
"symmetric eigenvalue workspace" ; FDL
"symmetric eigenvalue workspace"
:documentation ; FDL
"Make a workspace for computing eigenvalues of
n-by-n real symmetric matrices. The size of the workspace
is O(2n).")
;; V 1.9
(defmobject eigen-nonsymm
"gsl_eigen_nonsymm" ((n sizet))
"non-symmetric eigenvalue workspace" ; FDL
"non-symmetric eigenvalue workspace"
:gsl-version (1 9)
:documentation ; FDL
"Make a workspace for computing eigenvalues of
n-by-n real non-symmetric matrices. The size of the workspace
is O(2n).")
......@@ -41,15 +43,17 @@
(defmobject eigen-symmv
"gsl_eigen_symmv" ((n sizet))
"symmetric eigensystem workspace" ; FDL
"symmetric eigensystem workspace"
:documentation ; FDL
"Make a workspace for computing eigenvalues and
eigenvectors of n-by-n real symmetric matrices. The size of
the workspace is O(4n).")
;; V 1.9
(defmobject eigen-nonsymmv
"gsl_eigen_nonsymmv" ((n sizet))
"non-symmetric eigenvalue workspace" ; FDL
"non-symmetric eigenvalue workspace"
:gsl-version (1 9)
:documentation ; FDL
"Make a workspace for computing for computing eigenvalues and
eigenvectors of n-by-n real nonsymmetric matrices. The size of the
workspace is O(5n).")
......@@ -57,13 +61,15 @@
(defmobject eigen-herm
"gsl_eigen_herm" ((n sizet))
"Hermitian eigenvalue workspace" ; FDL
:documentation ; FDL
"Make a workspace for computing eigenvalues of
n-by-n complex Hermitian matrices. The size of the workspace
is O(3n).")
(defmobject eigen-hermv
"gsl_eigen_hermv" ((n sizet))
"Hermitian eigensystem workspace" ; FDL
"Hermitian eigensystem workspace"
:documentation ; FDL
"Make a workspace for computing eigenvalues and
eigenvectors of n-by-n complex hermitian matrices. The size of
the workspace is O(5n).")
......
;; Discrete Hankel Transforms.
;; Liam Healy, Sat Dec 8 2007 - 16:50
;; Time-stamp: <2008-12-26 18:46:20EST hankel.lisp>
;; Time-stamp: <2009-01-25 10:00:41EST hankel.lisp>
;; $Id$
(in-package :gsl)
......@@ -14,10 +14,11 @@
(defmobject hankel "gsl_dht"
((size sizet))
"discrete Hankel Transform"
:documentation
"Allocate a Discrete Hankel transform object of the given size and
optionally initialize the transform for the given values of nu and x."
"init"
((nu :double) (xmax :double)))
:initialize-suffix "init"
:initialize-args ((nu :double) (xmax :double)))
(defmfun apply-hankel (hankel array-in array-out)
"gsl_dht_apply"
......
;; The histogram structure
;; Liam Healy, Mon Jan 1 2007 - 11:32
;; Time-stamp: <2009-01-11 22:40:23EST histogram.lisp>
;; Time-stamp: <2009-01-25 10:03:15EST histogram.lisp>
;; $Id$
(in-package :gsl)
......@@ -14,16 +14,15 @@
"gsl_histogram"
((number-of-bins sizet))
"one-dimensional histogram, including bin boundaries and bin contents."
NIL
"set_ranges"
(((c-pointer ranges) :pointer) ((dim0 ranges) sizet)))
:initialize-suffix "set_ranges"
:initialize-args (((c-pointer ranges) :pointer) ((dim0 ranges) sizet)))
(defmobject histogram2d
"gsl_histogram2d"
((number-of-bins-x sizet) (number-of-bins-y sizet))
"two-dimensional histogram, including bin boundaries and bin contents."
NIL
"set_ranges"
:initialize-suffix "set_ranges"
:initialize-args
(((c-pointer x-ranges) :pointer) ((dim0 x-ranges) sizet)
((c-pointer y-ranges) :pointer) ((dim0 y-ranges) sizet)))
......
;; Histogram probability distribution.
;; Liam Healy, Mon Jan 1 2007 - 17:51
;; Time-stamp: <2008-12-26 18:38:41EST probability-distribution.lisp>
;; Time-stamp: <2009-01-25 10:04:23EST probability-distribution.lisp>
;; $Id$
(in-package :gsl)
......@@ -12,23 +12,25 @@
"gsl_histogram_pdf"
((number-of-bins sizet))
"one-dimensional histogram PDF"
:documentation
"Optionally initialize the probability distribution pdf with the contents
of the histogram. If any of the bins are negative then an
input-domain error is signalled because a probability distribution
cannot contain negative values."
"init"
(((mpointer histogram) :pointer)))
:initialize-suffix "init"
:initialize-args (((mpointer histogram) :pointer)))
(defmobject histogram2d-pdf
"gsl_histogram2d_pdf"
((number-of-bins-x sizet) (number-of-bins-y sizet))
"two-dimensional histogram PDF"
:documentation
"Optionally initialize the probability distribution pdf with the contents
of the histogram. If any of the bins are negative then an
input-domain error is signalled because a probability distribution
cannot contain negative values."
"init"
(((mpointer histogram) :pointer)))
:initialize-suffix "init"
:initialize-args (((mpointer histogram) :pointer)))
(export 'sample)
(defgeneric sample (pdf value)
......
;; Helpers that define a single GSL function interface
;; Liam Healy 2009-01-07 22:02:20EST defmfun-single.lisp
;; Time-stamp: <2009-01-15 22:27:56EST defmfun-single.lisp>
;; Time-stamp: <2009-01-25 10:37:13EST defmfun-single.lisp>
;; $Id: $
(in-package :gsl)
......@@ -81,7 +81,7 @@
qualifier gsl-version &allow-other-keys)
key-args
(declare (ignorable inputs outputs)) ; workaround for compiler errors that don't see it's used
(if (or (not gsl-version) (apply 'have-at-least-gsl-version gsl-version))
(if (have-at-least-gsl-version gsl-version)
`(,definition
,@(when (and name (not (defgeneric-method-p name)))
(list name))
......
;; Macros to interface GSL functions, including definitions necessary for defmfun.
;; Liam Healy
;; Time-stamp: <2009-01-06 18:49:56EST interface.lisp>
;; Time-stamp: <2009-01-25 10:30:39EST interface.lisp>
;; $Id$
(in-package :gsl)
......@@ -143,13 +143,14 @@
(map-name '*gsl-version* "gsl_version")
(export '*gsl-version*)
(defun have-at-least-gsl-version (major minor)
(defun have-at-least-gsl-version (major-minor)
"The GSL version currently running is at least the specified
major/minor version."
(let* ((sep-pos (position #\. *gsl-version*))
(my-major
(read-from-string *gsl-version* nil nil :end sep-pos))
(my-minor
(read-from-string *gsl-version* nil nil :start (1+ sep-pos))))
(and (>= my-major major)
(>= my-minor minor))))
(or (null major-minor)
(let* ((sep-pos (position #\. *gsl-version*))
(my-major
(read-from-string *gsl-version* nil nil :end sep-pos))
(my-minor
(read-from-string *gsl-version* nil nil :start (1+ sep-pos))))
(and (>= my-major (first major-minor))
(>= my-minor (second major-minor))))))
;; Definition of GSL objects and ways to use them.
;; Liam Healy, Sun Dec 3 2006 - 10:21
;; Time-stamp: <2009-01-21 22:38:01EST mobject.lisp>
;; Time-stamp: <2009-01-25 10:35:08EST mobject.lisp>
;; $Id$
;;; GSL objects are represented in GSLL as and instance of a 'mobject.
......@@ -22,8 +22,8 @@
(defmacro defmobject
(class prefix allocation-args description
&optional docstring initialize-suffix initialize-args arglists-function
inputs)
&key documentation initialize-suffix initialize-args arglists-function
inputs gsl-version)
"Define the class, the allocate, initialize-instance and
reinitialize-instance methods, and the make-* function for the GSL object."
;; If prefix is a list, the first is the actual prefix, and the
......@@ -40,82 +40,89 @@
(cl-initialize-args
(variables-used-in-c-arguments initialize-args))
(realprefix (if (listp prefix) (first prefix) prefix)))
`(progn
(defclass ,class (mobject)
()
(:documentation
,(format nil "The GSL representation of the ~a." description)))
(defmfun allocate ((object ,class) &key ,@cl-alloc-args)
,(if (listp prefix) (second prefix) (format nil "~a_alloc" prefix))
,allocation-args
,@(if (and (listp prefix) (third prefix))
`(:inputs ,(third prefix)))
:definition :method
:c-return :pointer
:index ,maker)
(defmethod initialize-instance :after
((object ,class) &key mpointer ,@(union cl-alloc-args cl-initialize-args))
,@(let ((not-alloc (set-difference cl-initialize-args cl-alloc-args)))
(when not-alloc)
`((declare (ignore ,@not-alloc))))
(unless mpointer
(setf mpointer
(allocate object ,@(symbol-keyword-symbol cl-alloc-args))
(slot-value object 'mpointer) mpointer))
(tg:finalize object
(lambda ()
(cffi:foreign-funcall
,(format nil "~a_free" realprefix)
:pointer mpointer :void))))
,@(when initialize-suffix
`((defmfun reinitialize-instance
((object ,class) &key ,@cl-initialize-args)
,(format nil "~a_~a" realprefix
(if (listp initialize-suffix)
(first initialize-suffix)
initialize-suffix))
(((mpointer object) :pointer) ,@initialize-args)
:definition :method
:qualifier :after
,@(when (listp initialize-suffix)
`(:c-return ,(second initialize-suffix)))
:return (object)
,@(when inputs `(:inputs ,inputs))
:export nil
:index (reinitialize-instance ,class))))
(export ',maker)
(defun ,maker
,(if arglists
(first arglists)
`(,@cl-alloc-args
,@(when initialize-args
(append
(list '&optional
(list (first cl-initialize-args) nil settingp))
(rest cl-initialize-args)))))
,(format nil "Create the GSL object representing a ~a (class ~a).~@[~&~a~]"
description class docstring)
(let ((object
(make-instance
',class
,@(if arglists
(second arglists)
(symbol-keyword-symbol cl-alloc-args)))))
;; There is an initialization step
(if (have-at-least-gsl-version gsl-version)
`(progn
(defclass ,class (mobject)
()
(:documentation
,(format nil "The GSL representation of the ~a." description)))
(defmfun allocate ((object ,class) &key ,@cl-alloc-args)
,(if (listp prefix) (second prefix) (format nil "~a_alloc" prefix))
,allocation-args
,@(if (and (listp prefix) (third prefix))
`(:inputs ,(third prefix)))
:definition :method
:c-return :pointer
:index ,maker)
(defmethod initialize-instance :after
((object ,class) &key mpointer ,@(union cl-alloc-args cl-initialize-args))
,@(let ((not-alloc (set-difference cl-initialize-args cl-alloc-args)))
(when not-alloc)
`((declare (ignore ,@not-alloc))))
(unless mpointer
(setf mpointer
(allocate object ,@(symbol-keyword-symbol cl-alloc-args))
(slot-value object 'mpointer) mpointer))
(tg:finalize object
(lambda ()
(cffi:foreign-funcall
,(format nil "~a_free" realprefix)
:pointer mpointer :void))))
,@(when initialize-suffix
(if initialize-args ; with arguments
`((when ,settingp
(reinitialize-instance
object
,@(if arglists
(third arglists)
(symbol-keyword-symbol cl-initialize-args)))))
'((reinitialize-instance object)))) ; without arguments
object)))))
`((defmfun reinitialize-instance
((object ,class) &key ,@cl-initialize-args)
,(format nil "~a_~a" realprefix
(if (listp initialize-suffix)
(first initialize-suffix)
initialize-suffix))
(((mpointer object) :pointer) ,@initialize-args)
:definition :method
:qualifier :after
,@(when (listp initialize-suffix)
`(:c-return ,(second initialize-suffix)))
:return (object)
,@(when inputs `(:inputs ,inputs))
:export nil
:index (reinitialize-instance ,class))))
(export ',maker)
(defun ,maker
,(if arglists
(first arglists)
`(,@cl-alloc-args
,@(when initialize-args
(append
(list '&optional
(list (first cl-initialize-args) nil settingp))
(rest cl-initialize-args)))))
,(format
nil "Create the GSL object representing a ~a (class ~a).~@[~&~a~]"
description class documentation)
(let ((object
(make-instance
',class
,@(if arglists
(second arglists)
(symbol-keyword-symbol cl-alloc-args)))))
;; There is an initialization step
,@(when initialize-suffix
(if initialize-args ; with arguments
`((when ,settingp
(reinitialize-instance
object
,@(if arglists
(third arglists)
(symbol-keyword-symbol cl-initialize-args)))))
'((reinitialize-instance object)))) ; without arguments
object)))
`(defun ,maker (&rest args)
(declare (ignore args))
(error 'obsolete-gsl-version
:name ',class :gsl-name ,realprefix
:gsl-version ',gsl-version)))))
(defun symbol-keyword-symbol (symbol)
(if (listp symbol)
......
;; Interpolation allocation, initialization, and freeing.
;; Liam Healy, Sun Nov 4 2007 - 17:24
;; Time-stamp: <2008-12-26 18:19:19EST interpolation.lisp>
;; Time-stamp: <2009-01-25 10:14:30EST interpolation.lisp>
;; $Id$
(in-package :gsl)
......@@ -12,27 +12,33 @@
;;; so they need not be supplied on each call.
(defmobject interpolation "gsl_interp"
((type :pointer) (size sizet))
"interpolation" ; FDL
"interpolation"
:documentation ; FDL
"Make an interpolation object of type for size data-points,
and optionally initialize the interpolation object interp for the
data (xa,ya) where xa and ya are vectors. The interpolation object does not save
the data arrays xa and ya and only stores the static state
computed from the data. The xa data array is always assumed to be
strictly ordered; the behavior for other arrangements is not defined."
"init"
:initialize-suffix "init"
:initialize-args
(((c-pointer xa) :pointer) ((c-pointer ya) :pointer) ((dim0 xa) sizet))
:arglists-function
(lambda (set)
`((type &optional xa-or-size (ya nil ,set))
(:type type :size (if ,set (dim0 ya) xa-or-size))
(:xa xa-or-size :ya ya))))
`((type &optional xa-or-size (ya nil ,set))
(:type type :size (if ,set (dim0 ya) xa-or-size))
(:xa xa-or-size :ya ya))))
(defmobject spline "gsl_spline"
((type :pointer) (size sizet))
"spline" ; FDL
"spline"
:documentation ; FDL
"Make an interpolation object of type for size data-points."
"init"
:initialize-suffix "init"
:initialize-args
(((c-pointer xa) :pointer) ((c-pointer ya) :pointer) ((dim0 xa) sizet))
:arglists-function
(lambda (set)
`((type &optional xa-or-size (ya nil ,set))
(:type type :size (if ,set (dim0 ya) xa-or-size))
(:xa xa-or-size :ya ya))))
`((type &optional xa-or-size (ya nil ,set))
(:type type :size (if ,set (dim0 ya) xa-or-size))
(:xa xa-or-size :ya ya))))
;; Index lookup and acceleration
;; Liam Healy, Sun Nov 4 2007 - 18:09
;; Time-stamp: <2008-12-26 16:41:21EST lookup.lisp>
;; Time-stamp: <2009-01-25 10:14:47EST lookup.lisp>
;; $Id$
(in-package :gsl)
(defmobject acceleration "gsl_interp_accel"
()
"acceleration for interpolation" ; FDL
"acceleration for interpolation"
:documentation ; FDL
"Make an accelerator object, which is a
kind of iterator for interpolation lookups. It tracks the state of
lookups, thus allowing for application of various acceleration
......
;; Adaptive step-size control
;; Liam Healy 2008-02-17 17:30:04EST control.lisp
;; Time-stamp: <2009-01-20 19:39:08EST control.lisp>
;; Time-stamp: <2009-01-25 10:01:56EST control.lisp>
;; $Id$
(in-package :gsl)
#|
(defmobject standard-control ("gsl_odeiv_control_standard" "new")
((absolute-error :double) (relative-error :double)
(y-scaling :double) (dydt-scaling :double))
"standard control for ordinary differential equations"
"")
|#
(defmfun new-standard-control (absolute-error relative-error y-scaling dydt-scaling)
"gsl_odeiv_control_standard_new"
((absolute-error :double) (relative-error :double)
......
;; Evolution functions for ODE integration.
;; Liam Healy, Sun Sep 30 2007 - 14:31
;; Time-stamp: <2009-01-20 22:42:27EST evolution.lisp>
;; Time-stamp: <2009-01-25 10:00:41EST evolution.lisp>
;; $Id$
(in-package :gsl)
......@@ -8,8 +8,10 @@
(defmobject ode-evolution "gsl_odeiv_evolve"
((dimensions sizet))
"evolution for ordinary differential equations"
:documentation
"Make an object to advance the ODE solution."
"reset" nil)
:initialize-suffix "reset"
:initialize-args nil)
(defmfun apply-evolution
(evolve control step dydt time max-time step-size y)
......
;; Stepping functions for ODE systems.
;; Liam Healy, Mon Sep 24 2007 - 21:33
;; Time-stamp: <2009-01-20 19:09:55EST stepping.lisp>
;; Time-stamp: <2009-01-25 10:00:40EST stepping.lisp>
;; $Id$
(in-package :gsl)
......@@ -8,12 +8,14 @@
(defmobject ode-stepper "gsl_odeiv_step"
((type :pointer) (dimensions sizet))
"stepper for ordinary differential equations"
:documentation
"Make a stepper for ordinary differential equations. The type is
one of the GSL-supplied types defined in stepping.lisp, and
dimensions is the number of dependent variables. This instance
should be reinitialized whenever the next use of it will not be a
continuation of a previous step."
"reset" nil)
:initialize-suffix "reset"
:initialize-args nil)
(defmfun name ((object ode-stepper))
"gsl_odeiv_step_name"
......
;; Discrete random variables
;; Liam Healy, Sat Nov 11 2006 - 21:51
;; Time-stamp: <2008-12-29 22:33:10EST discrete.lisp>
;; Time-stamp: <2009-01-25 09:54:54EST discrete.lisp>
;; $Id$
(in-package :gsl)
......@@ -10,7 +10,8 @@
(defmobject discrete-random
("gsl_ran_discrete" "gsl_ran_discrete_preproc" (probabilities))
(((dim0 probabilities) sizet) ((c-pointer probabilities) :pointer))
"lookup table for the discrete random number generator" ; FDL
"lookup table for the discrete random number generator"
:documentation ; FDL
"Make a structure that contains the lookup