chebyshev.lisp 3.82 KB
Newer Older
liam's avatar
liam committed
1 2
;; Chebyshev Approximations
;; Liam Healy Sat Nov 17 2007 - 20:36
3
;; Time-stamp: <2008-12-26 16:43:37EST chebyshev.lisp>
lhealy's avatar
lhealy committed
4
;; $Id$
liam's avatar
liam committed
5 6 7

(in-package :gsl)

8 9 10 11
;;;;****************************************************************************
;;;; Creation and calculation of Chebyshev series
;;;;****************************************************************************

12 13 14 15 16 17
(defmobject chebyshev "gsl_cheb"
  ((order sizet))
  "Chebyshev series"			; FDL
  "Make a Chebyshev series of specified order."
  "init"
  ((function :pointer) (lower-limit :double) (upper-limit :double)))
liam's avatar
liam committed
18 19 20 21 22 23 24 25 26

;;;;****************************************************************************
;;;; 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
;;; desired) in the form of #'evaluate-chebyshev.

Liam Healy's avatar
Liam Healy committed
27 28
(defmfun evaluate-chebyshev (chebyshev x &optional order)
  ("gsl_cheb_eval" "gsl_cheb_eval_n")
29 30
  ((((mpointer chebyshev) :pointer) (x :double))
   (((mpointer chebyshev) :pointer) (order sizet) (x :double)))
liam's avatar
liam committed
31
  :c-return :double
liam's avatar
liam committed
32
  :documentation			; FDL
Liam Healy's avatar
Liam Healy committed
33 34
  "Evaluate the Chebyshev series at a point x.  If order is supplied,
  evaluate to at most the given order.")
liam's avatar
liam committed
35

Liam Healy's avatar
Liam Healy committed
36 37
(defmfun evaluate-chebyshev-error (chebyshev x &optional order)
  ("gsl_cheb_eval_err" "gsl_cheb_eval_n_err")
38 39 40 41
  ((((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
42
  :documentation			; FDL
liam's avatar
liam committed
43
  "Evaluate the Chebyshev series at a point x, returning result and
Liam Healy's avatar
Liam Healy committed
44 45
   an estimate of its absolute error.  If order is supplied,
   evaluate to at most the given order.")
liam's avatar
liam committed
46 47 48 49 50

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

liam's avatar
liam committed
51
(defmfun derivative-chebyshev (derivative chebyshev)
liam's avatar
liam committed
52
  "gsl_cheb_calc_deriv"
53
  ((derivative :pointer) ((mpointer chebyshev) :pointer))
liam's avatar
liam committed
54
  :documentation			; FDL
liam's avatar
liam committed
55 56 57 58
  "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
59
(defmfun integral-chebyshev (integral chebyshev)
liam's avatar
liam committed
60
  "gsl_cheb_calc_integ"
61
  ((integral :pointer) ((mpointer chebyshev) :pointer))
liam's avatar
liam committed
62
  :documentation			; FDL
liam's avatar
liam committed
63 64 65 66 67 68 69 70 71 72 73
  "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
74
;;; to keep things sane.
liam's avatar
liam committed
75

liam's avatar
liam committed
76
(defun-single chebyshev-step (x) (if (< x 0.5d0) 0.25d0 0.75d0))
liam's avatar
liam committed
77

78
(defun chebyshev-table-example ()
79
  (let ((steps 100))
80
    (let ((cheb (make-chebyshev 40 chebyshev-step 0.0d0 1.0d0)))
81 82 83 84 85 86 87
      (dotimes (i steps)
	(let ((x (coerce (/ i steps) 'double-float)))
	  (format t "~&~a ~a ~a ~a"
		  x
		  (chebyshev-step x)
		  (evaluate-chebyshev cheb x 10)
		  (evaluate-chebyshev cheb x)))))))
88 89 90

(defun chebyshev-point-example (x)
  (check-type x double-float)
91 92 93
  (let ((cheb (make-chebyshev 40 chebyshev-step 0.0d0 1.0d0))
	 (deriv (make-chebyshev 40))
	 (integ (make-chebyshev 40)))
94 95
    (derivative-chebyshev deriv cheb)
    (integral-chebyshev integ cheb)
96 97 98 99
    (list
     (evaluate-chebyshev cheb x)
     (evaluate-chebyshev deriv x)
     (evaluate-chebyshev integ x))))
100

Liam Healy's avatar
Liam Healy committed
101 102
;;; Unit test
(save-test chebyshev
liam's avatar
liam committed
103
  (chebyshev-point-example 0.55d0))