# polynomials

• To: info-mcl@ministry.cambridge.apple.com
• Subject: polynomials
• From: deleeuw@galton.math.ucla.edu (Jan Deleeuw)
• Date: 20 Feb 1994 19:33:09 GMT
• Newsgroups: comp.lang.lisp, comp.lang.lisp.mcl
• Organization: UCLA Statistics Program
• Sender: news@math.ucla.edu
• Xref: news.cambridge.apple.com comp.lang.lisp:7164 comp.lang.lisp.mcl:4934

```Hello,

I am translating (part of) Stephen Moshier's Cephes Library from C to
CL. Since a lot of this is polynomial approximation, I need a really
efficient version of (poly x y) which implements Horner's rule. Here
x is the argument of the polynomial, and y is a list or vector of
coefficients (4-10 double-floats). I dont want to use assembler, because
of portability (yet). Some questions:
(a) Is there such code -- I have CL Math, but that seems tentative.
(b) Do I use a list or vector of coefficients.
(c) Is it advantageous to defconstant the coefficients, or do I just
pass the actual values to poly.
(d) Where do I put the declarations.

This is what I use now (from CL Math, but not using a macro)

(defun poly (var coefs)
(declare (double-float var) (type (list double-float *) coefs)
(optimize (safety 0) (space 0) (speed 3)))
(cond ((null coefs) 0.0)
((null (cdr coefs)) (car coefs))
(t (the double-float (+ (the double-float (car coefs))
(the double-float (* (the double-float var)
(the double-float (poly var (cdr coefs))))))))))

and here is an example of a function using poly

(defun stirf (x)
(declare (double-float x))
(let* (
(a (/ x))
(w (+ 1.0 (* a (poly a '(8.33333333333482257126e-2
3.47222221605458667310e-3
-2.68132617805781232825e-3
-2.29549961613378126380e-4
7.87311395793093628397e-4)))))
(y (exp x))
)
(declare (double-float a w y))
(if (> x 143.01608)
(let* (
(v (expt x (- 0.25 (* 0.5 x))))
(r (* v (/ v y)))
)
(declare (double-float v r))
(* w r 2.50662827463100050242e0))
(let (
(r (/ (expt x (- x 0.5)) y))
)
(declare (double-float r))
(* w r 2.50662827463100050242e0)))
))

All suggestions are welcome.
The stronger the criticism, the more welcome it is.

--- Jan

Jan de Leeuw; Dept Math UCLA; phone (310)-825-9550; fax (310)-206-6673
mail: 405 Hilgard Ave, Los Angeles, CA 90024-1555
INTERNET: deleeuw@laplace.stat.ucla.edu

--
Jan de Leeuw; Dept Math UCLA; phone (310)-825-9550; fax (310)-206-6673
mail: 405 Hilgard Ave, Los Angeles, CA 90024-1555
INTERNET: deleeuw@laplace.stat.ucla.edu

```