Generating CL expressions



> Can I generate Lisp expressions from Maxima definitions?

Try the following:

  f(a,b):= (a+b)^2;

  translate(f);

Now to look at the Lisp definition:

  lisp_function(f):=(?print(?symbol\-function(f)),f);

  lisp_function(f);

(LAMBDA-BLOCK |$f| (|$a| |$b|)
  (DECLARE (SPECIAL |$b| |$a|))
  (POWER (ADD* |$a| |$b|) 2)) 

Note that Translate tries to preserve Maxima semantics.  This has many
implications.  If you don't declare the variables as numeric, it will
treat them as symbolic.  For interoperability with Maxima code, it
preserves the internal variable names "|$a|" etc. rather than simply
"a".  The functions Power and Add* are *symbolic* exponentiation and
addition.

To get an idea of the effect of declarations, I've give a few examples
below.  To make this more readable, I've replaced |$a| by a, etc. and
put everything in lowercase.

f(a,b):= ( modedeclare([a,b],none), (a+b)^2 $
translate(f)$
lisp_function(f))$

(lambda-block f (a b)
  (declare (special b a))
  (progn nil (power (add* a b) 2))) 

f(a,b):= ( modedeclare([a,b],number), (a+b)^2 $
translate(f)$
lisp_function(f))$

(lambda-block f (a b)
  (declare (special b a))
  (progn nil (power (plus a b) 2))) 

It uses Power (a Maxima function) rather than Expt (a Lisp function)
since the result of <number>^<number> in Maxima is not necessarily a
number, e.g. (-1)^(1/2) -- but you can override that with tr_exponent
(undocumented, even by describe("tr_") -- see the code).

f(a,b):= ( modedeclare([a,b],float), (a+b)^2 $
translate(f)$
lisp_function(f))$

(lambda-block f (a b)
  (declare (special b a) (flonum b a))
  (progn nil (expt$ (+$ a b) 2))) 

This is a bug in Translate: (expt$ -1 .5) returns a CL complex, but
Maxima can't handle those.

f(a,b):= ( modedeclare([a,b],fixnum), (a+b)^2 )$
translate(f)$
lisp_function(f)$

(lambda-block f (a b)
  (declare (special b a) (fixnum b a))
  (progn nil (power (f+ a b) 2))) 

f(a,b):= ( modedeclare([a,b],rational), (a+b)^2 $
translate(f)$
lisp_function(f))$

(lambda-block f (a b)
  (declare (special b a))
  (progn nil (power (rplus a b) 2)))