[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: polynomials



In article <DELEEUW.94Feb20113309@galton.math.ucla.edu>,
Jan Deleeuw <deleeuw@galton.math.ucla.edu> wrote:
>Hello,

Here's one attempt at a faster version, which reverses the order
of coefficients. It uses iteration instead of recursion; the version
of poly that you posted wasn't tail recursive; you could make it 
tail-recusive by passing an accumulator but iteration seemed a more natural
way of expressing this problem. 

Be careful about changing the safety setting - remember 0.0 isn't a 
double-float. For yet more efficiency, you could turn poly into a macro;
just backquote and comma the code computing result.

(defun poly-2 (var coefs)
  "compute a polynomial in var using coefficients in coefs. Coefficients are
   in decending order - (poly-2 x (1 2 3 4)) == x^3 + 2x^2 + 3x +4"
  (declare (optimize  speed safety)
	   (double-float var))
  (do* ((result 0.0d0 (+ c (* result var)))
	(coefficients coefs (cdr coefficients))
	(c (car coefficients) (or (and coefficients (car coefficients))
				  0.0d0)))
       ((null coefficients) result)
       (declare (double-float result c))))
-- 
Hackers Local 42- National Union of Computer Operatives, Chapel Hill section
------------------------------------------------------------------------------
Tar Heel Information Services - Nothing but net!   | WAIS/Z39.50 spoken here
North Carolina - First in Usenet	| DoD #612 | Tel: +1-919-962-9107