polynomial.lisp 7.35 KB
Newer Older
1 2
;; Polynomials
;; Liam Healy, Tue Mar 21 2006 - 18:33
Liam Healy's avatar
Liam Healy committed
3
;; Time-stamp: <2009-01-31 19:34:56EST polynomial.lisp>
lhealy's avatar
lhealy committed
4
;; $Id$
5 6 7

(in-package :gsl)

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

10 11
;;; Provide autotranslation from CL pure arrays?
;;; Divided differences not complete/tested.
12 13 14 15 16

;;;;****************************************************************************
;;;; Polynomial Evaluation
;;;;****************************************************************************

Liam Healy's avatar
Liam Healy committed
17 18 19 20 21 22 23 24 25 26 27
(defmfun evaluate
    ((coefficients vector-double-float) (x float) &key divided-difference)
  ("gsl_poly_eval" "gsl_poly_dd_eval")
  ((((c-pointer coefficients) :pointer) ((dim0 coefficients) sizet)
    (x :double))
   (((c-pointer divided-difference) :pointer)
    ((c-pointer coefficients) :pointer)
    ((dim0 coefficients) sizet)
    (x :double)))
  :definition :method
  :inputs (coefficients divided-difference)
28
  :c-return :double
29
  :documentation			; FDL
30
  "Evaluate the polyonomial with coefficients at the point x.")
31

Liam Healy's avatar
Liam Healy committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#|
;;; These won't work until we can take returned complex (structs).
(defmfun evaluate
    ((coefficients vector-double-float) (x complex)
     &key &allow-other-keys)
  "gsl_poly_complex_eval"
  (((c-pointer coefficients) :pointer) ((dim0 coefficients) sizet)
   (x complex-double-c))
  :definition :method
  :gsl-version (1 11)
  :inputs (coefficients)
  :c-return complex-double-c
  :documentation			; FDL
  "Evaluate the polyonomial with coefficients at the complex value x.")

(defmfun evaluate
    ((coefficients vector-complex-double-float) (x complex)
     &key &allow-other-keys)
  "gsl_complex_poly_complex_eval"
  (((c-pointer coefficients) :pointer) ((dim0 coefficients) sizet)
   (x complex-double-c))
  :definition :method
  :gsl-version (1 11)
  :inputs (coefficients)
  :c-return complex-double-c
  :documentation			; FDL
  "Evaluate the polyonomial with coefficients at the complex value x.")
|#

61 62 63 64
;;;;****************************************************************************
;;;; Divided Difference Representation of Polynomials
;;;;****************************************************************************

65
(defmfun divided-difference (dd xa ya)
66
  "gsl_poly_dd_init"
67 68 69 70 71 72 73 74 75 76
  (((c-pointer dd) :pointer)
   ((c-pointer xa) :pointer) ((c-pointer ya) :pointer)
   ((dim0 xa) sizet))
  :inputs (xa ya)
  :outputs (dd)
  :return (dd)
  :documentation			; FDL
  "Compute a divided-difference representation of the
   interpolating polynomial for the points (xa, ya) stored in
   the arrays of equal length.  On output the
77
   divided-differences of (xa,ya) are stored in the array
78
   dd, of the same length.")
79

liam's avatar
liam committed
80
(defmfun taylor-divided-difference (coefs xp dd xa workspace)
81
  "gsl_poly_dd_taylor"
82
  (((c-pointer coefs) :pointer)
83
   (xp :double)
84 85 86 87 88 89
   ((c-pointer dd) :pointer)
   ((c-pointer xa) :pointer)
   ((dim0 xa) sizet)
   ((c-pointer workspace) :pointer))
  :inputs (coefs xa)
  :outputs (coefs)
90
  :documentation			; FDL
91 92
  "Convert the divided-difference representation of a
  polynomial to a Taylor expansion.  The divided-difference representation
93
  is supplied in the arrays dd and xa of the same length.
94
  On output the Taylor coefficients of the polynomial expanded about the
95
  point xp are stored in the array coefs which has the same length
96
  as xa and dd.  A workspace of that length must be provided.")
97 98 99 100 101

;;;;****************************************************************************
;;;; Quadratic Equations
;;;;****************************************************************************

liam's avatar
liam committed
102
(defmfun solve-quadratic (a b c)
103
  "gsl_poly_solve_quadratic"
104
  ((a :double) (b :double) (c :double) (root1 :double) (root2 :double))
105 106
  :c-return :number-of-answers
  :documentation			; FDL
107
  "The real roots of the quadratic equation a x^2 + b x + c = 0.
108
   Two values are always returned; if the roots are not real, these
109
   values are NIL.")
110

liam's avatar
liam committed
111
(defmfun solve-quadratic-complex (a b c)
112
  "gsl_poly_complex_solve_quadratic"
113 114
  ((a :double) (b :double) (c :double)
   (root1 complex-double-c) (root2 complex-double-c))
115 116
  :c-return :number-of-answers
  :documentation			; FDL
117 118
  "The complex roots of the quadratic equation a x^2 + b x + c = 0.
   Two values are always returned; if a root does not exist, the
119
   value returned will be NIL.") 
120 121 122 123 124

;;;;****************************************************************************
;;;; Cubic Equations
;;;;****************************************************************************

liam's avatar
liam committed
125
(defmfun solve-cubic (a b c)
126
  "gsl_poly_solve_cubic"
127 128
  ((a :double) (b :double) (c :double)
   (root1 :double) (root2 :double) (root3 :double))
129 130
  :c-return :number-of-answers
  :documentation			; FDL
131
  "Find the real roots of the cubic equation, x^3 + a x^2 + b x + c = 0
132 133
   with a leading coefficient of unity.  The roots are given
   in ascending order.  Three values are always returned;
134
   if a root is not real, the value returned for it will be NIL.")
135

liam's avatar
liam committed
136
(defmfun solve-cubic-complex (a b c)
137
  "gsl_poly_complex_solve_cubic"
138
  ((a :double) (b :double) (c :double)
139
   (root1 complex-double-c) (root2 complex-double-c) (root3 complex-double-c))
140 141
  :c-return :number-of-answers
  :documentation			; FDL
142
  "Find the complex roots of the cubic equation, x^3 + a x^2 + b x + c = 0
143
   with a leading coefficient of unity.  Three values are always returned;
144
   if a root does not exist, the value returned for it will be NIL.")
145 146 147 148 149

;;;;****************************************************************************
;;;; General Polynomial Equations
;;;;****************************************************************************

150 151 152
(defmobject polynomial-complex-workspace "gsl_poly_complex_workspace"
  ((n sizet))
  "complex workspace for polynomials")
153

154 155 156 157 158 159
(defmfun polynomial-solve
    (coefficients
     &optional
     (answer (make-marray '(complex double-float)
			  :dimensions (1- (total-size coefficients))))
     (workspace (make-polynomial-complex-workspace (total-size coefficients))))
160
  "gsl_poly_complex_solve"
161
  (((c-pointer coefficients) :pointer) ((dim0 coefficients) sizet)
162
   ((mpointer workspace) :pointer) ((c-pointer answer) :pointer))
163
  :inputs (coefficients)
164 165
  :outputs (answer)
  :return (answer)
166
  :documentation			; FDL
167 168 169 170 171 172 173
  "Arguments are: a vector-double-float of coefficients, a complex
   vector of length one less than coefficients that will hold the
   answer, and a workspace made by make-polynomial-complex-workspace.
   The roots of the general polynomial 
   P(x) = a_0 + a_1 x + a_2 x^2 + ... + a_{n-1} x^{n-1} using 
   balanced-QR reduction of the companion matrix.  The coefficient of the
   highest order term must be non-zero.")
174

175 176 177 178
;;;;****************************************************************************
;;;; Examples and unit test
;;;;****************************************************************************

Liam Healy's avatar
Liam Healy committed
179
(save-test polynomial
180
 (let ((xa #m(0.0d0 1.0d0 2.0d0 3.0d0))
Liam Healy's avatar
Liam Healy committed
181 182
       (ya #m(2.5d0 7.2d0 32.7d0 91.0d0))
       (dd (make-marray 'double-float :dimensions 4)))
183 184
   (divided-difference dd xa ya)
   (list
Liam Healy's avatar
Liam Healy committed
185 186 187 188
    (evaluate xa 0.0d0 :divided-difference dd)
    (evaluate xa 1.0d0 :divided-difference dd)
    (evaluate xa 2.0d0 :divided-difference dd)
    (evaluate xa 3.0d0 :divided-difference dd)))
189
 (let ((vec #m(1.0d0 2.0d0 3.0d0)))
Liam Healy's avatar
Liam Healy committed
190
   (evaluate vec -1.0d0))
191 192 193 194 195 196
 (solve-quadratic 1.0d0 0.0d0 1.0d0)
 (solve-quadratic 1.0d0 -2.0d0 1.0d0)
 (solve-quadratic-complex 1.0d0 -2.0d0 1.0d0)
 (solve-cubic -6.0d0 -13.0d0 42.0d0)
 (solve-cubic-complex -1.0d0 1.0d0 -1.0d0)
 ;; Example from GSL manual
197
 (cl-array (polynomial-solve #m(-1.0d0 0.0d0 0.0d0 0.0d0 0.0d0 1.0d0))))
Liam Healy's avatar
Liam Healy committed
198