polynomial.lisp 6.56 KB
Newer Older
1 2
;; Polynomials
;; Liam Healy, Tue Mar 21 2006 - 18:33
3
;; Time-stamp: <2008-12-30 21:33:37EST 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's avatar
liam committed
17
(defmfun polynomial-eval (coefficients x)
18
  "gsl_poly_eval"
19
  (((c-pointer coefficients) :pointer) ((dim0 coefficients) sizet) (x :double))
20 21
  :inputs (coefficients)
  :c-return :double
22
  :documentation			; FDL
23
  "Evaluate the polyonomial with coefficients at the point x.")
24

25 26 27 28
;;;;****************************************************************************
;;;; Divided Difference Representation of Polynomials
;;;;****************************************************************************

29
(defmfun divided-difference (dd xa ya)
30
  "gsl_poly_dd_init"
31 32 33 34 35 36 37 38 39 40
  (((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
41
   divided-differences of (xa,ya) are stored in the array
42
   dd, of the same length.")
43

liam's avatar
liam committed
44
(defmfun polynomial-eval-divided-difference (dd xa x)
45
  "gsl_poly_dd_eval"
46 47 48
  (((c-pointer dd) :pointer)
   ((c-pointer xa) :pointer)
   ((dim0 xa) sizet)
49
   (x :double))
50
  :inputs (dd xa)
51
  :c-return :double
52
  :documentation			; FDL
53
  "Evaluate the polynomial stored in divided-difference form
54
   in the arrays dd and xa at the point x.")
55

liam's avatar
liam committed
56
(defmfun taylor-divided-difference (coefs xp dd xa workspace)
57
  "gsl_poly_dd_taylor"
58
  (((c-pointer coefs) :pointer)
59
   (xp :double)
60 61 62 63 64 65
   ((c-pointer dd) :pointer)
   ((c-pointer xa) :pointer)
   ((dim0 xa) sizet)
   ((c-pointer workspace) :pointer))
  :inputs (coefs xa)
  :outputs (coefs)
66
  :documentation			; FDL
67 68
  "Convert the divided-difference representation of a
  polynomial to a Taylor expansion.  The divided-difference representation
69
  is supplied in the arrays dd and xa of the same length.
70
  On output the Taylor coefficients of the polynomial expanded about the
71
  point xp are stored in the array coefs which has the same length
72
  as xa and dd.  A workspace of that length must be provided.")
73 74 75 76 77

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

liam's avatar
liam committed
78
(defmfun solve-quadratic (a b c)
79
  "gsl_poly_solve_quadratic"
80
  ((a :double) (b :double) (c :double) (root1 :double) (root2 :double))
81 82
  :c-return :number-of-answers
  :documentation			; FDL
83
  "The real roots of the quadratic equation a x^2 + b x + c = 0.
84
   Two values are always returned; if the roots are not real, these
85
   values are NIL.")
86

liam's avatar
liam committed
87
(defmfun solve-quadratic-complex (a b c)
88
  "gsl_poly_complex_solve_quadratic"
89 90
  ((a :double) (b :double) (c :double)
   (root1 complex-double-c) (root2 complex-double-c))
91 92
  :c-return :number-of-answers
  :documentation			; FDL
93 94
  "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
95
   value returned will be NIL.") 
96 97 98 99 100

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

liam's avatar
liam committed
101
(defmfun solve-cubic (a b c)
102
  "gsl_poly_solve_cubic"
103 104
  ((a :double) (b :double) (c :double)
   (root1 :double) (root2 :double) (root3 :double))
105 106
  :c-return :number-of-answers
  :documentation			; FDL
107
  "Find the real roots of the cubic equation, x^3 + a x^2 + b x + c = 0
108 109
   with a leading coefficient of unity.  The roots are given
   in ascending order.  Three values are always returned;
110
   if a root is not real, the value returned for it will be NIL.")
111

liam's avatar
liam committed
112
(defmfun solve-cubic-complex (a b c)
113
  "gsl_poly_complex_solve_cubic"
114
  ((a :double) (b :double) (c :double)
115
   (root1 complex-double-c) (root2 complex-double-c) (root3 complex-double-c))
116 117
  :c-return :number-of-answers
  :documentation			; FDL
118
  "Find the complex roots of the cubic equation, x^3 + a x^2 + b x + c = 0
119
   with a leading coefficient of unity.  Three values are always returned;
120
   if a root does not exist, the value returned for it will be NIL.")
121 122 123 124 125

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

126 127 128
(defmobject polynomial-complex-workspace "gsl_poly_complex_workspace"
  ((n sizet))
  "complex workspace for polynomials")
129

130 131 132 133 134 135
(defmfun polynomial-solve
    (coefficients
     &optional
     (answer (make-marray '(complex double-float)
			  :dimensions (1- (total-size coefficients))))
     (workspace (make-polynomial-complex-workspace (total-size coefficients))))
136
  "gsl_poly_complex_solve"
137
  (((c-pointer coefficients) :pointer) ((dim0 coefficients) sizet)
138
   ((mpointer workspace) :pointer) ((c-pointer answer) :pointer))
139
  :inputs (coefficients)
140 141
  :outputs (answer)
  :return (answer)
142
  :documentation			; FDL
143 144 145 146 147 148 149
  "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.")
150

151 152 153 154
;;;;****************************************************************************
;;;; Examples and unit test
;;;;****************************************************************************

Liam Healy's avatar
Liam Healy committed
155
(save-test polynomial
156 157
 (let ((xa #m(0.0d0 1.0d0 2.0d0 3.0d0))
	(ya #m(2.5d0 7.2d0 32.7d0 91.0d0))
158
	(dd (make-marray 'double-float :dimensions 4)))
159 160 161 162 163 164
   (divided-difference dd xa ya)
   (list
    (polynomial-eval-divided-difference dd xa 0.0d0)
    (polynomial-eval-divided-difference dd xa 1.0d0)
    (polynomial-eval-divided-difference dd xa 2.0d0)
    (polynomial-eval-divided-difference dd xa 3.0d0)))
165
 (let ((vec #m(1.0d0 2.0d0 3.0d0)))
166 167 168 169 170 171 172
   (polynomial-eval vec -1.0d0))
 (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
173
 (cl-array (polynomial-solve #m(-1.0d0 0.0d0 0.0d0 0.0d0 0.0d0 1.0d0))))
Liam Healy's avatar
Liam Healy committed
174