chebyshev.lisp 4.04 KB
Newer Older
1 2
;; Chebyshev Approximations
;; Liam Healy Sat Nov 17 2007 - 20:36
3
;; Time-stamp: <2009-02-15 11:23:40EST chebyshev.lisp>
lhealy's avatar
lhealy committed
4
;; $Id$
liam's avatar
liam committed
5 6 7

(in-package :gsl)

8 9
;;; /usr/include/gsl/gsl_chebyshev.h

10 11 12 13
;;;;****************************************************************************
;;;; Creation and calculation of Chebyshev series
;;;;****************************************************************************

14 15
(defmobject chebyshev "gsl_cheb"
  ((order sizet))
16 17
  "Chebyshev series"
  :documentation			; FDL
18
  "Make a Chebyshev series of specified order."
19 20
  :superclasses (callback-included)
  :ci-class-slots (gsl-function nil (function))
21 22
  :initialize-suffix "init"
  :initialize-args
23 24 25
  ((callback :pointer) (lower-limit :double) (upper-limit :double))
  :singular (function))

liam's avatar
liam committed
26 27 28 29 30 31
;;;;****************************************************************************
;;;; Chebyshev series evaluation
;;;;****************************************************************************

;;; The functions that don't return are defined, but it is recommended
;;; to use the functions that do return error (and ignore it if
32
;;; desired) in the form of #'evaluate.
liam's avatar
liam committed
33

34
(defmfun evaluate ((object chebyshev) x &key order)
Liam Healy's avatar
Liam Healy committed
35
  ("gsl_cheb_eval" "gsl_cheb_eval_n")
36 37 38
  ((((mpointer object) :pointer) (x :double))
   (((mpointer object) :pointer) (order sizet) (x :double)))
  :definition :method
liam's avatar
liam committed
39
  :c-return :double
liam's avatar
liam committed
40
  :documentation			; FDL
Liam Healy's avatar
Liam Healy committed
41 42
  "Evaluate the Chebyshev series at a point x.  If order is supplied,
  evaluate to at most the given order.")
liam's avatar
liam committed
43

Liam Healy's avatar
Liam Healy committed
44 45
(defmfun evaluate-chebyshev-error (chebyshev x &optional order)
  ("gsl_cheb_eval_err" "gsl_cheb_eval_n_err")
46 47 48 49
  ((((mpointer chebyshev) :pointer) (x :double) (result :double)
    (abserr :double))
   (((mpointer chebyshev) :pointer) (order sizet) (x :double)
    (result :double) (abserr :double)))
liam's avatar
liam committed
50
  :documentation			; FDL
liam's avatar
liam committed
51
  "Evaluate the Chebyshev series at a point x, returning result and
Liam Healy's avatar
Liam Healy committed
52 53
   an estimate of its absolute error.  If order is supplied,
   evaluate to at most the given order.")
liam's avatar
liam committed
54 55 56 57 58

;;;;****************************************************************************
;;;; Derivatives and integrals
;;;;****************************************************************************

liam's avatar
liam committed
59
(defmfun derivative-chebyshev (derivative chebyshev)
liam's avatar
liam committed
60
  "gsl_cheb_calc_deriv"
61
  (((mpointer derivative) :pointer) ((mpointer chebyshev) :pointer))
liam's avatar
liam committed
62
  :documentation			; FDL
liam's avatar
liam committed
63 64 65 66
  "Compute the derivative of the Chebyshev series, storing
   the derivative coefficients in the previously allocated series.
   The two series must have been allocated with the same order.")

liam's avatar
liam committed
67
(defmfun integral-chebyshev (integral chebyshev)
liam's avatar
liam committed
68
  "gsl_cheb_calc_integ"
69
  (((mpointer integral) :pointer) ((mpointer chebyshev) :pointer))
liam's avatar
liam committed
70
  :documentation			; FDL
liam's avatar
liam committed
71 72 73 74 75 76 77 78 79 80 81
  "Compute the integral of the Chebyshev series, storing
   the integral coefficients in the previously allocated series.
   The two series must have been allocated with the same order.
   The lower limit of the integration is taken to be the left hand
   end of the range lower-limit.")

;;;;****************************************************************************
;;;; Example
;;;;****************************************************************************

;;; From Chap. 28.5, except I have set steps = 100 instead of 10000
liam's avatar
liam committed
82
;;; to keep things sane.
liam's avatar
liam committed
83

84
(defun chebyshev-step (x) (if (< x 0.5d0) 0.25d0 0.75d0))
liam's avatar
liam committed
85

86
(make-callbacks single-function chebyshev-step)
87

88
(defun chebyshev-table-example ()
89
  (let ((steps 100))
90
    (let ((cheb (make-chebyshev 40 'chebyshev-step 0.0d0 1.0d0)))
91 92 93 94 95
      (dotimes (i steps)
	(let ((x (coerce (/ i steps) 'double-float)))
	  (format t "~&~a ~a ~a ~a"
		  x
		  (chebyshev-step x)
96 97
		  (evaluate cheb x :order 10)
		  (evaluate cheb x)))))))
98 99 100

(defun chebyshev-point-example (x)
  (check-type x double-float)
101
  (let ((cheb (make-chebyshev 40 'chebyshev-step 0.0d0 1.0d0))
102 103
	(deriv (make-chebyshev 40))
	(integ (make-chebyshev 40)))
104 105
    (derivative-chebyshev deriv cheb)
    (integral-chebyshev integ cheb)
106
    (list
107 108 109
     (evaluate cheb x)
     (evaluate deriv x)
     (evaluate integ x))))
110

Liam Healy's avatar
Liam Healy committed
111 112
;;; Unit test
(save-test chebyshev
liam's avatar
liam committed
113
  (chebyshev-point-example 0.55d0))