Commit 6f3227de authored by liam's avatar liam
Browse files

Unified def-gsl-function works for both single-argument functions

(numerical-integration) and multiple-argument functions
(monte-carlo).


git-svn-id: svn+ssh://pop/opt/space/mathematics/gsl/trunk@3182 a3d8a0fb-c1db-0310-ace7-a616afeb9e30
parent 376ac857
......@@ -3,7 +3,7 @@
; description: Monte Carlo Integration
; date: Sat Feb 3 2007 - 17:42
; author: Liam Healy
; modified: Sun Feb 4 2007 - 16:28
; modified: Sun Feb 11 2007 - 11:08
;********************************************************
;;; $Id: $
......@@ -23,27 +23,12 @@
(cffi:get-callback ,function)
(cffi:foreign-slot-value ,name 'monte-function 'dimensions)
,number-of-arguments
;; As in numerical-integration, we'll pass the parmeters
;; As in numerical-integration, we'll pass the parameters
;; in with a closure.
(cffi:foreign-slot-value ,name 'monte-function 'parameters)
(cffi:null-pointer))
,@body))
;;; Modify def-gsl-function in numerical-integration.lisp to work with
;;; multivariable arguments.
(export 'def-gsl-function-multi)
(defmacro def-gsl-function-multi (name &body body)
"Define a GSL function of one C array to be used in
numerical integration functions. Arguments are specified
by (argument n) where n=0, 1,... Parameters (non
integration variables) may be passed by using a lexical closure. "
(let ((arg (gensym "MCARG")))
`(cffi:defcallback ,name :double
((,arg :pointer) (params :pointer))
(declare (ignore params))
(macrolet ((argument (n) `(cffi:mem-aref ,',arg :double ,n)))
,@body))))
;;;;****************************************************************************
;;;; PLAIN Monte Carlo
;;;;****************************************************************************
......@@ -329,9 +314,9 @@
;;; Example from Sec. 23.5
;;; This is a function that occurs in random walk studies.
(def-gsl-function-multi monte-carlo-g
(def-gsl-function monte-carlo-g (x y z)
(* (/ (expt pi 3))
(/ (- 1 (* (cos (argument 0)) (cos (argument 1)) (cos (argument 2)))))))
(/ (- 1 (* (cos x) (cos y) (cos z))))))
(defun random-walk-plain-example (&optional (nsamples 500000))
(with-monte-carlo-plain (ws 3)
......
......@@ -3,7 +3,7 @@
; description: Numerical integration
; date: Wed Jul 5 2006 - 23:14
; author: Liam M. Healy
; modified: Tue Jul 11 2006 - 23:17
; modified: Sun Feb 11 2007 - 11:05
;********************************************************
;;; $Id: $
......@@ -18,15 +18,26 @@
(params :pointer))
(export 'def-gsl-function)
(defmacro def-gsl-function
(name integration-variable &body body)
"Define a GSL function of one double-float to be used in
numerical integration functions. Parameters (non
integration variables) may be passed by using a lexical closure. "
`(cffi:defcallback ,name :double
((,integration-variable :double) (params :pointer))
(declare (ignore params))
,@body))
(defmacro def-gsl-function (name arg &body body)
"Define a GSL (C) function of either one argument of type
double (if arg is a symbol), or a C array of doubles
(if arg is a list), for GSL numerical
integration functions.
Parameters (non integration variables) may be passed by
using a lexical closure."
(let ((argvec (gensym "MCARG")))
`(cffi:defcallback ,name :double
(,(if (listp arg)
`(,argvec :pointer)
`(,arg :double))
(params :pointer))
(declare (ignore params))
,@(if (listp arg)
`((symbol-macrolet
,(loop for i from 0 for a in arg
collect `(,a (cffi:mem-aref ,argvec :double ,i)))
,@body))
body))))
(defun make-gsl-function (function)
"Make a function for GSL to integrate."
......
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