Code generation: evaluating polynomials Richard Fateman Computer Science Division, EECS University of California, Berkeley August 28, 2002 Abstract Writing a program to evaluate a given polynomial at a point can be done rather simply. If it has to be done repeatedly, efficiently, and/or accurately, a naive approach may not suffice. There is in fact a huge design space of how to approach this problem [12]. Depending upon one’s criteria, it may be worthwhile to use automated program-generation programs to write source code. Such generated code can be structured to exploit whatever special knowledge of the polynomial being evaluated may be available early, as well as the design requirements. This knowledge can include its degree, some or all of its coefficients (used in pre-computing coefficients of an auxiliary polynomial), special knowledge of the point at which it is evaluated (real or complex), special knowledge of the computer on which it is run (pipe-line depth and number of arithmetic units), the required error tolerance, and whether auxiliary information is needed, such as the first or second derivative of the polynomial. It is possible also to indicate exactly the number of arithmetic operations used, and in some cases, other information describing the efficiency of the code. In this paper we describe some program-generating programs that may be of assistance. All the code is available in an on-line appendix. Although the implementation language is ANSI standard Common Lisp, the target language is (your choice) of Lisp or C. 1 Introduction Most practitioners of scientific programming view evaluating a polynomial as quite rudimentary and not worth deep examination. Merely typing into Fortran something like y=3*x^2+4*x+1 would seem to do it, though many would immediately re-write this as y=1+x*(4+x*3) to save a multiplication. Writing a program that takes an array of double-float coefficients, the integer degree of the polynomial, and the point at which it is to be evaluated, is not that much harder, and Horner’s rule provides a good method. Given a programming language that deals nicely with data aggregates like lists, (e.g. Lisp) one can define a Horner’s rule program (defun ev(p x) (reduce #’(lambda( s r)(+ r (* x s))) p)) such that (ev ’(3 4 1) 7) evaluates the given polynomial at x=7. to 176. Do you care how accurate the answer is? If you are evaluating this polynomial to find a root using a Newton iteration, you will need to know its derivative. One can, in general, evaluate a derivative as part of the same computation. Our ability to satisfy such specific needs lies at the core of many more sophisticated routines. Most polynomial zero-finding routines eventually evaluate the polynomial. Backing off a bit from the specific objective of polynomials, we will examine code generation as a tradeoff between program re-use (taking a program from a library) and special single-purpose programs. Our objective is to provide a program framework, but not the object code per se. The framework is re-used to generate single- (or at least special-) purpose code. 1