chebyshev.lisp 3.75 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 11:21:25EST 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 29 30
(defmfun evaluate-chebyshev (chebyshev x &optional order)
  ("gsl_cheb_eval" "gsl_cheb_eval_n")
  (((chebyshev :pointer) (x :double))
  ((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 38 39
(defmfun evaluate-chebyshev-error (chebyshev x &optional order)
  ("gsl_cheb_eval_err" "gsl_cheb_eval_n_err")
  (((chebyshev :pointer) (x :double) (result :double) (abserr :double))
   ((chebyshev :pointer) (order sizet) (x :double) (result :double) (abserr :double)))
liam's avatar
liam committed
40
  :documentation			; FDL
liam's avatar
liam committed
41
  "Evaluate the Chebyshev series at a point x, returning result and
Liam Healy's avatar
Liam Healy committed
42 43
   an estimate of its absolute error.  If order is supplied,
   evaluate to at most the given order.")
liam's avatar
liam committed
44 45 46 47 48

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

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

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

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

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

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