chebyshev.lisp 4.46 KB
Newer Older
liam's avatar
liam committed
1 2
;; Chebyshev Approximations
;; Liam Healy Sat Nov 17 2007 - 20:36
3
;; Time-stamp: <2009-03-14 19:23:41EDT 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))
Liam Healy's avatar
Liam Healy committed
16 17
  "Chebyshev series"
  :documentation			; FDL
18
  "Make a Chebyshev series of specified order."
19
  :callbacks (callback gsl-function (function))
20
  :callback-dynamic ((function))
Liam Healy's avatar
Liam Healy committed
21 22
  :initialize-suffix "init"
  :initialize-args
23 24 25
  ((callback :pointer) (lower-limit :double) (upper-limit :double))
  :singular (function))

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
(defmfun order ((object chebyshev))
  "gsl_cheb_order"
  (((mpointer object) :pointer))
  :definition :method
  :c-return sizet
  :gsl-version (1 12))

(defmfun size (chebyshev)
  "gsl_cheb_size"
  (((mpointer chebyshev) :pointer))
  :c-return sizet
  :gsl-version (1 12))

(defmfun coefficients (chebyshev)
  "gsl_cheb_coeffs"
  (((mpointer chebyshev) :pointer))
  :c-return sizet
  :gsl-version (1 12))

liam's avatar
liam committed
45 46 47 48 49 50
;;;;****************************************************************************
;;;; 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
51
;;; desired) in the form of #'evaluate.
liam's avatar
liam committed
52

53
(defmfun evaluate ((object chebyshev) x &key order)
Liam Healy's avatar
Liam Healy committed
54
  ("gsl_cheb_eval" "gsl_cheb_eval_n")
55 56 57
  ((((mpointer object) :pointer) (x :double))
   (((mpointer object) :pointer) (order sizet) (x :double)))
  :definition :method
58
  :callback-object object
liam's avatar
liam committed
59
  :c-return :double
liam's avatar
liam committed
60
  :documentation			; FDL
Liam Healy's avatar
Liam Healy committed
61 62
  "Evaluate the Chebyshev series at a point x.  If order is supplied,
  evaluate to at most the given order.")
liam's avatar
liam committed
63

Liam Healy's avatar
Liam Healy committed
64 65
(defmfun evaluate-chebyshev-error (chebyshev x &optional order)
  ("gsl_cheb_eval_err" "gsl_cheb_eval_n_err")
66 67 68 69
  ((((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
70
  :documentation			; FDL
liam's avatar
liam committed
71
  "Evaluate the Chebyshev series at a point x, returning result and
Liam Healy's avatar
Liam Healy committed
72 73
   an estimate of its absolute error.  If order is supplied,
   evaluate to at most the given order.")
liam's avatar
liam committed
74 75 76 77 78

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

liam's avatar
liam committed
79
(defmfun derivative-chebyshev (derivative chebyshev)
liam's avatar
liam committed
80
  "gsl_cheb_calc_deriv"
81
  (((mpointer derivative) :pointer) ((mpointer chebyshev) :pointer))
liam's avatar
liam committed
82
  :documentation			; FDL
liam's avatar
liam committed
83 84 85 86
  "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
87
(defmfun integral-chebyshev (integral chebyshev)
liam's avatar
liam committed
88
  "gsl_cheb_calc_integ"
89
  (((mpointer integral) :pointer) ((mpointer chebyshev) :pointer))
liam's avatar
liam committed
90
  :documentation			; FDL
liam's avatar
liam committed
91 92 93 94 95 96 97 98 99 100 101
  "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
102
;;; to keep things sane.
liam's avatar
liam committed
103

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

106
;; (make-callbacks single-function chebyshev-step)
107

108
(defun chebyshev-table-example ()
109
  (let ((steps 100))
110
    (let ((cheb (make-chebyshev 40 'chebyshev-step 0.0d0 1.0d0)))
111 112 113 114 115
      (dotimes (i steps)
	(let ((x (coerce (/ i steps) 'double-float)))
	  (format t "~&~a ~a ~a ~a"
		  x
		  (chebyshev-step x)
116 117
		  (evaluate cheb x :order 10)
		  (evaluate cheb x)))))))
118 119 120

(defun chebyshev-point-example (x)
  (check-type x double-float)
121
  (let ((cheb (make-chebyshev 40 'chebyshev-step 0.0d0 1.0d0))
122 123
	(deriv (make-chebyshev 40))
	(integ (make-chebyshev 40)))
124 125
    (derivative-chebyshev deriv cheb)
    (integral-chebyshev integ cheb)
126
    (list
127 128 129
     (evaluate cheb x)
     (evaluate deriv x)
     (evaluate integ x))))
130

Liam Healy's avatar
Liam Healy committed
131 132
;;; Unit test
(save-test chebyshev
liam's avatar
liam committed
133
  (chebyshev-point-example 0.55d0))