Commit 7eb2b9cf authored by Liam Healy's avatar Liam Healy
Browse files

Clean up spline examples; default :acceleration argument

Clean up spline examples with simpler code and same results.
Functions in interpolation/evaluation.lisp with key argument
:acceleration defaults to (make-acceleration).  Test for
evaluate-integral-example added.
parent 4a3c7442
;; Evaluation of interpolation functions.
;; Liam Healy Sun Nov 4 2007 - 18:40
;; Time-stamp: <2009-01-28 19:22:15EST evaluation.lisp>
;; Time-stamp: <2009-11-11 23:18:50EST evaluation.lisp>
(in-package :gsl)
(defmfun evaluate ((interpolation interpolation) x &key xa ya acceleration)
(defmfun evaluate
((interpolation interpolation) x
&key xa ya (acceleration (make-acceleration)))
"gsl_interp_eval"
(((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double)
((mpointer acceleration) :pointer))
......@@ -24,7 +26,8 @@
xa and ya and the accelerator acceleration."))
(defmfun evaluate-derivative
((interpolation interpolation) x &key xa ya acceleration)
((interpolation interpolation) x
&key xa ya (acceleration (make-acceleration)))
"gsl_interp_eval_deriv"
(((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double)
((mpointer acceleration) :pointer))
......@@ -40,7 +43,7 @@
xa and ya and the accelerator acceleration."))
(defmfun evaluate-second-derivative
((interpolation interpolation) x &key xa ya acceleration)
((interpolation interpolation) x &key xa ya (acceleration (make-acceleration)))
"gsl_interp_eval_deriv2"
(((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double)
((mpointer acceleration) :pointer))
......@@ -56,7 +59,8 @@
data arrays xa and ya and the accelerator 'acceleration."))
(defmfun evaluate-integral
((interpolation interpolation) low high &key xa ya acceleration)
((interpolation interpolation) low high
&key xa ya (acceleration (make-acceleration)))
"gsl_interp_eval_integ"
(((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer)
(low :double) (high :double) ((mpointer acceleration) :pointer))
......@@ -65,25 +69,28 @@
:c-return :double)
;;; Spline
(defmfun evaluate ((spline spline) x &key acceleration)
(defmfun evaluate ((spline spline) x &key (acceleration (make-acceleration)))
"gsl_spline_eval"
(((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer))
:definition :method
:c-return :double)
(defmfun evaluate-derivative ((spline spline) x &key acceleration)
(defmfun evaluate-derivative
((spline spline) x &key (acceleration (make-acceleration)))
"gsl_spline_eval_deriv"
(((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer))
:definition :method
:c-return :double)
(defmfun evaluate-second-derivative ((spline spline) x &key acceleration)
(defmfun evaluate-second-derivative
((spline spline) x &key (acceleration (make-acceleration)))
"gsl_spline_eval_deriv2"
(((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer))
:definition :method
:c-return :double)
(defmfun evaluate-integral ((spline spline) low high &key acceleration)
(defmfun evaluate-integral
((spline spline) low high &key (acceleration (make-acceleration)))
"gsl_spline_eval_integ"
(((mpointer spline) :pointer) (low :double) (high :double)
((mpointer acceleration) :pointer))
......
;; Example spline
;; Liam Healy, Sat Nov 10 2007 - 21:18
;; Time-stamp: <2009-02-16 09:46:57EST spline-example.lisp>
;; $Id$
;; Time-stamp: <2009-11-11 23:19:51EST spline-example.lisp>
(in-package :gsl)
(defun spline-example (&optional (step 0.01d0))
"The first example in Sec. 26.7 of the GSL manual."
(let* ((acc (make-acceleration))
(xarr
(let* ((xarr
(make-marray
'double-float
:initial-contents
......@@ -22,30 +20,25 @@
collect (+ i (cos (expt i 2))))))
(spline (make-spline +cubic-spline-interpolation+ xarr yarr)))
(loop for xi from (maref xarr 0) below (maref xarr 9) by step
collect (list xi (evaluate spline xi :acceleration acc)))))
(save-test interpolation (spline-example 0.1d0))
collect (list xi (evaluate spline xi)))))
(defun evaluate-integral-example (&optional (intervals 4))
"Evaluate integral of sin(x) in interval 0-pi. sin(x) is tabulated
over a 0-2pi interval and interpolated with
+periodic-cubic-spline-interpolation+"
(let* ((nodes (1+ intervals))
(max-node (1- nodes))
(xarr
(loop
with step = (/ (* 2.0 pi) intervals)
for i from 0 upto max-node
over a 0-2pi interval and interpolated with
+periodic-cubic-spline-interpolation+"
(let* ((xarr
(loop with step = (/ (* 2.0 pi) intervals)
for i from 0 upto intervals
collect (* i step)))
(xmarr (make-marray 'double-float :initial-contents xarr))
;; cannot use (loop for x on (cl-array xmarr)...) -- c function gives error
(ymarr
(make-marray 'double-float :initial-contents
(loop for x in xarr
collect (sin x))))
(acc (make-acceleration))
(spline (make-spline +periodic-cubic-spline-interpolation+ xmarr ymarr)))
(evaluate-integral spline 0d0
#+clisp(coerce pi 'double-float) ;; pi=3.14..L0 on clisp
#-clisp pi
:acceleration acc)))
(ymarr
(make-marray 'double-float :initial-contents (mapcar 'sin xarr)))
(spline
(make-spline +periodic-cubic-spline-interpolation+ xmarr ymarr)))
(evaluate-integral spline 0d0
#+clisp (coerce pi 'double-float) ;; pi=3.14..L0 on clisp
#-clisp pi)))
(save-test interpolation
(spline-example 0.1d0)
(evaluate-integral-example 100))
......@@ -3,7 +3,10 @@
(in-package :gsl)
(LISP-UNIT:DEFINE-TEST INTERPOLATION
(LISP-UNIT::ASSERT-NUMERICAL-EQUAL
(LISP-UNIT:ASSERT-NUMERICAL-EQUAL
(LIST 1.9999999566460036d0)
(MULTIPLE-VALUE-LIST (EVALUATE-INTEGRAL-EXAMPLE 100)))
(LISP-UNIT:ASSERT-NUMERICAL-EQUAL
(LIST
(LIST (LIST 0.0d0 1.0d0)
(LIST 0.1d0 1.069722939658712d0)
......
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