Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
;; Evaluation of interpolation functions. ;; Evaluation of interpolation functions.
;; Liam Healy Sun Nov 4 2007 - 18:40 ;; 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) (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" "gsl_interp_eval"
(((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double) (((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double)
((mpointer acceleration) :pointer)) ((mpointer acceleration) :pointer))
...@@ -24,7 +26,8 @@ ...@@ -24,7 +26,8 @@
xa and ya and the accelerator acceleration.")) xa and ya and the accelerator acceleration."))
(defmfun evaluate-derivative (defmfun evaluate-derivative
((interpolation interpolation) x &key xa ya acceleration) ((interpolation interpolation) x
&key xa ya (acceleration (make-acceleration)))
"gsl_interp_eval_deriv" "gsl_interp_eval_deriv"
(((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double) (((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double)
((mpointer acceleration) :pointer)) ((mpointer acceleration) :pointer))
...@@ -40,7 +43,7 @@ ...@@ -40,7 +43,7 @@
xa and ya and the accelerator acceleration.")) xa and ya and the accelerator acceleration."))
(defmfun evaluate-second-derivative (defmfun evaluate-second-derivative
((interpolation interpolation) x &key xa ya acceleration) ((interpolation interpolation) x &key xa ya (acceleration (make-acceleration)))
"gsl_interp_eval_deriv2" "gsl_interp_eval_deriv2"
(((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double) (((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (x :double)
((mpointer acceleration) :pointer)) ((mpointer acceleration) :pointer))
...@@ -56,7 +59,8 @@ ...@@ -56,7 +59,8 @@
data arrays xa and ya and the accelerator 'acceleration.")) data arrays xa and ya and the accelerator 'acceleration."))
(defmfun evaluate-integral (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" "gsl_interp_eval_integ"
(((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer) (((mpointer interpolation) :pointer) (xa :pointer) (ya :pointer)
(low :double) (high :double) ((mpointer acceleration) :pointer)) (low :double) (high :double) ((mpointer acceleration) :pointer))
...@@ -65,25 +69,28 @@ ...@@ -65,25 +69,28 @@
:c-return :double) :c-return :double)
;;; Spline ;;; Spline
(defmfun evaluate ((spline spline) x &key acceleration) (defmfun evaluate ((spline spline) x &key (acceleration (make-acceleration)))
"gsl_spline_eval" "gsl_spline_eval"
(((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer)) (((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer))
:definition :method :definition :method
:c-return :double) :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" "gsl_spline_eval_deriv"
(((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer)) (((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer))
:definition :method :definition :method
:c-return :double) :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" "gsl_spline_eval_deriv2"
(((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer)) (((mpointer spline) :pointer) (x :double) ((mpointer acceleration) :pointer))
:definition :method :definition :method
:c-return :double) :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" "gsl_spline_eval_integ"
(((mpointer spline) :pointer) (low :double) (high :double) (((mpointer spline) :pointer) (low :double) (high :double)
((mpointer acceleration) :pointer)) ((mpointer acceleration) :pointer))
......
;; Example spline ;; Example spline
;; Liam Healy, Sat Nov 10 2007 - 21:18 ;; Liam Healy, Sat Nov 10 2007 - 21:18
;; Time-stamp: <2009-02-16 09:46:57EST spline-example.lisp> ;; Time-stamp: <2009-11-11 23:19:51EST spline-example.lisp>
;; $Id$
(in-package :gsl) (in-package :gsl)
(defun spline-example (&optional (step 0.01d0)) (defun spline-example (&optional (step 0.01d0))
"The first example in Sec. 26.7 of the GSL manual." "The first example in Sec. 26.7 of the GSL manual."
(let* ((acc (make-acceleration)) (let* ((xarr
(xarr
(make-marray (make-marray
'double-float 'double-float
:initial-contents :initial-contents
...@@ -22,30 +20,25 @@ ...@@ -22,30 +20,25 @@
collect (+ i (cos (expt i 2)))))) collect (+ i (cos (expt i 2))))))
(spline (make-spline +cubic-spline-interpolation+ xarr yarr))) (spline (make-spline +cubic-spline-interpolation+ xarr yarr)))
(loop for xi from (maref xarr 0) below (maref xarr 9) by step (loop for xi from (maref xarr 0) below (maref xarr 9) by step
collect (list xi (evaluate spline xi :acceleration acc))))) collect (list xi (evaluate spline xi)))))
(save-test interpolation (spline-example 0.1d0))
(defun evaluate-integral-example (&optional (intervals 4)) (defun evaluate-integral-example (&optional (intervals 4))
"Evaluate integral of sin(x) in interval 0-pi. sin(x) is tabulated "Evaluate integral of sin(x) in interval 0-pi. sin(x) is tabulated
over a 0-2pi interval and interpolated with over a 0-2pi interval and interpolated with
+periodic-cubic-spline-interpolation+" +periodic-cubic-spline-interpolation+"
(let* ((nodes (1+ intervals)) (let* ((xarr
(max-node (1- nodes)) (loop with step = (/ (* 2.0 pi) intervals)
(xarr for i from 0 upto intervals
(loop
with step = (/ (* 2.0 pi) intervals)
for i from 0 upto max-node
collect (* i step))) collect (* i step)))
(xmarr (make-marray 'double-float :initial-contents xarr)) (xmarr (make-marray 'double-float :initial-contents xarr))
;; cannot use (loop for x on (cl-array xmarr)...) -- c function gives error (ymarr
(ymarr (make-marray 'double-float :initial-contents (mapcar 'sin xarr)))
(make-marray 'double-float :initial-contents (spline
(loop for x in xarr (make-spline +periodic-cubic-spline-interpolation+ xmarr ymarr)))
collect (sin x)))) (evaluate-integral spline 0d0
(acc (make-acceleration)) #+clisp (coerce pi 'double-float) ;; pi=3.14..L0 on clisp
(spline (make-spline +periodic-cubic-spline-interpolation+ xmarr ymarr))) #-clisp pi)))
(evaluate-integral spline 0d0
#+clisp(coerce pi 'double-float) ;; pi=3.14..L0 on clisp (save-test interpolation
#-clisp pi (spline-example 0.1d0)
:acceleration acc))) (evaluate-integral-example 100))
...@@ -3,7 +3,10 @@ ...@@ -3,7 +3,10 @@
(in-package :gsl) (in-package :gsl)
(LISP-UNIT:DEFINE-TEST INTERPOLATION (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 (LIST 0.0d0 1.0d0) (LIST (LIST 0.0d0 1.0d0)
(LIST 0.1d0 1.069722939658712d0) (LIST 0.1d0 1.069722939658712d0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment