Efficient Lisp functions -- using results of symbolic calculations numerically



On 8/31/07, Bruno Daniel <bruno.daniel at gmx.net> wrote:

> #'(LAMBDA (X)
>     (DECLARE (TYPE DOUBLE-FLOAT X) (VALUES DOUBLE-FLOAT)
>      (OPTIMIZE (SPEED 3) (SPACE 0) (SAFETY 0) (DEBUG 0)))
>     (+ (* 2 (COS (EXPT X 2))) (- (* (/ (EXPT X 2)) (SIN (EXPT X 2))))))

Bruno, as mentioned by Stavros, the Maxima-to-Lisp translator
can exploit some declarations to generate faster Lisp code.
Here is an example.

Without declaration for x:

g(x):=2*cos(x^2)-sin(x^2)/x^2$
translate (g);
:lisp #'$g
 =>
#<FUNCTION $G ($X) (DECLARE (IN-DEFUN $G) (SPECIAL $X))
  (BLOCK $G
   (ADD* (MUL* 2 (SIMPLIFY (LIST '(%COS) (POWER $X 2))))
    (*MMINUS (MUL* (POWER $X -2) (SIMPLIFY (LIST '(%SIN) (POWER $X 2)))))))>

With declaration for x:

h(x):=(mode_declare(x,float),2*cos(x^2)-sin(x^2)/x^2)$
translate (h);
:lisp #'$h
 =>
#<FUNCTION $H ($X) (DECLARE (IN-DEFUN $H) (SPECIAL $X) (FLONUM $X))
  (BLOCK $H
   (PROGN NIL (+ (* 2.0 (COS (EXPT$ $X 2))) (- (* (EXPT$ $X -2) (SIN
(EXPT$ $X 2)))))))>

The code generated for h is not so different from the code you
showed, and it runs just a little bit slower.

The Maxima translator has some known bugs, and hasn't received
much attention lately. Perhaps you would be interested in fixing
up the translator. That would be useful to many people.

best

Robert Dodier