chebyshev.lisp 3.86 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 18:21:11EST 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 16 17 18 19
(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
20 21 22 23 24 25 26 27 28

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

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

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

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

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

80
(defun chebyshev-table-example ()
81
  (let ((steps 100))
82
    (let ((cheb (make-chebyshev 40 chebyshev-step 0.0d0 1.0d0)))
83 84 85 86 87 88 89
      (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)))))))
90 91 92

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

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