Commit bcde4754 authored by Liam M. Healy's avatar Liam M. Healy
Browse files

Add Monte Carlo parameter setting

With GSL 1.13, the GSL API defines a way to set the parameters for the
MISER and VEGAS Monte Carlo methods. This commit defines two generic
functions, #'parameter and #'(setf parameter), to get and set the
parameters, respectively.  It loads but has not been tested.

The previous way of setting the parameters, with macros
> #'miser-parameter and #'vegas-parameter, are conditional to
> #+obsolete-gsl and so do not normally load.
parent 0f785ddd
Loading
Loading
Loading
Loading
+26 −2
Original line number Diff line number Diff line
;; CFFI-Grovel definitions for unix systems.
;; Liam Healy 2009-06-06 09:32:30EDT monte-carlo-structs.lisp
;; Time-stamp: <2012-01-13 12:01:40EST monte-carlo-structs.lisp>
;; Time-stamp: <2016-06-12 16:33:01EDT monte-carlo-structs.lisp>
;;
;; Copyright 2009, 2010 Liam M. Healy
;; Copyright 2009, 2010, 2012, 2016 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
@@ -61,6 +61,14 @@
  (hits-l "hits_l" :type :pointer)
  (hits-r "hits_r" :type :pointer))

;;; Added with GSL 1.13
(cstruct miser-params "gsl_monte_miser_params"
 (function-call-variance-estimation-fraction "estimate_frac" :type :double)
 (minimum-number-of-calls-for-variance-estimation "min_calls" :type :sizet)
 (minimum-calls-per-bisection "min_calls_per_bisection" :type :sizet)
 (alpha "alpha" :type :double)
 (dither "dither" :type :double))

(include "gsl/gsl_monte_vegas.h")

(cstruct vegas-state "gsl_monte_vegas_state"
@@ -84,3 +92,19 @@
  (verbose "verbose" :type :int)
  (iterations "iterations" :type :uint)
  (stage "stage" :type :int))

;;; Added with GSL 1.13
(cenum monte-carlo-vegas-mode
 ((:stratified "GSL_VEGAS_MODE_STRATIFIED"))
 ((:importance-only "GSL_VEGAS_MODE_IMPORTANCE_ONLY"))
 ((:importance "GSL_VEGAS_MODE_IMPORTANCE")))

;;; Added with GSL 1.13
(cstruct vegas-params "gsl_monte_vegas_params"
 (alpha "alpha" :type :double)
 (iterations "iterations" :type :sizet)
 (stage "stage" :type :int)
 (mode "mode" :type monte-carlo-vegas-mode)
 (verbose "verbose" :type :int)
 (output-stream "ostream" :type :pointer))
+54 −3
Original line number Diff line number Diff line
;; Monte Carlo Integration
;; Liam Healy Sat Feb  3 2007 - 17:42
;; Time-stamp: <2012-01-13 12:01:39EST monte-carlo.lisp>
;; Time-stamp: <2016-06-12 16:44:20EDT monte-carlo.lisp>
;;
;; Copyright 2007, 2008, 2009, 2011 Liam M. Healy
;; Copyright 2007, 2008, 2009, 2011, 2012, 2016 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
@@ -92,7 +92,32 @@
  :initialize-suffix "init"
  :initialize-args nil)

(export 'miser-parameter)
(defmfun parameter ((object monte-carlo-miser) parameter)
  "gsl_monte_miser_params_get"
  (((mpointer object) :pointer) (params miser-params))
  :definition :method
  :c-return :void
  :return ((cffi:foreign-slot-value params 'miser-params parameter))
  :gsl-version (1 13))

(defmfun set-mcm-parameters (object params)
  "gsl_monte_miser_params_set"
  (((mpointer object) :pointer) (params miser-params))
  :c-return :void
  :return (value)
  :gsl-version (1 13)
  :export nil
  :index (setf parameter))

(defmethod (setf parameter) (value (object monte-carlo-miser) parameter)
  (let ((current-params (parameter object)))
    (setf (cffi:foreign-slot-value object 'miser-params parameter)
	  value)
    (set-mcm-parameters value object parameter current-params)))

;;; As of GSL v1.13, the API above is used to get/set the MISER parameters, so the following is removed.
#+obsolete-gsl (export 'miser-parameter)
#+obsolete-gsl
(defmacro miser-parameter (workspace parameter)
  ;; FDL
  "Get or set with setf the parameter value for the MISER Monte Carlo
@@ -153,7 +178,33 @@
  :initialize-suffix "init"
  :initialize-args nil)

(defmfun parameter ((object monte-carlo-vegas) parameter)
  "gsl_monte_miser_params_get"
  (((mpointer object) :pointer) (params vegas-params))
  :definition :method
  :c-return :void
  :return ((cffi:foreign-slot-value params 'vegas-params parameter))
  :gsl-version (1 13))

(defmfun set-mcv-parameters (object params)
  "gsl_monte_miser_params_set"
  (((mpointer object) :pointer) (params vegas-params))
  :c-return :void
  :return (value)
  :gsl-version (1 13)
  :export nil
  :index (setf parameter))

(defmethod (setf parameter) (value (object monte-carlo-vegas) parameter)
  (let ((current-params (parameter object)))
    (setf (cffi:foreign-slot-value object 'vegas-params parameter)
	  value)
    (set-mcv-parameters value object parameter current-params)))

;;; As of GSL v1.13, the API above is used to get/set the VEGAS parameters, so the following is removed.
#+obsolete-gsl
(export 'vegas-parameter)
#+obsolete-gsl
(defmacro vegas-parameter (workspace parameter)
  ;; FDL
  "Get or set with setf the parameter value for the VEGAS Monte Carlo
+4 −3
Original line number Diff line number Diff line
;; Definition of GSLL system 
;; Liam Healy
;; Time-stamp: <2015-12-06 09:39:19EST gsll.asd>
;; Time-stamp: <2016-06-12 16:27:11EDT gsll.asd>
;;
;; Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2015 Liam M. Healy
;; Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2015, 2016 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
@@ -64,7 +64,8 @@
     (:file "defmfun-single"
      :depends-on ("init" "defmfun" "mobject" "callback"))
     (:file "body-expand" :depends-on ("init" "defmfun" "mobject" "callback"))
     (:file "generate-examples" :depends-on ("init"))))
     (:file "generate-examples" :depends-on ("init"))
     (:file "generic")))
   (:module floating-point
    :depends-on (init)
    :components

init/generic.lisp

0 → 100644
+29 −0
Original line number Diff line number Diff line
;; Define generic functions used across chapters
;; Liam Healy 2016-06-12 15:37:54EDT generic.lisp
;; Time-stamp: <2016-06-12 16:38:57EDT generic.lisp>

;; Copyright 2016 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(in-package :gsl)

(export '(parameter))

(defgeneric parameter (object parameter)
  (:documentation "Get the value of the GSL parameter from the GSL object."))

(defgeneric (setf parameter) (value object parameter)
  (:documentation "Set the value of the GSL parameter from the GSL object."))