# [Maxima] how to print algebraic expressions as an S-expression which is valid CL code

Barton Willis willisb at unk.edu
Sat Apr 11 11:15:42 CDT 2009

```Here are two possibilities.

(1) Use Maxima's translator: Create a file xxx.mac

f(chi,rho,lw,lf):= (
mode_declare([chi, rho, lw, lf], float),
((chi*lf+chi)*rho+(1-chi)*lw+chi*lf+1)/((chi*lf+chi)*rho+(lf+chi)*lw
+(1-chi)*lf));

>From a Maxima command line, translate the file using the command

> translate_file("xxx.mac");

The file xxx.LISP contains the Lisp code you want (the mode_declare is
important).

(2) Use the *light-weight* Maxima to CL translator:

<...deleted...>

(%o6) ((chi*lf+chi)*rho+(1-chi)*lw+chi*lf+1)/((chi*lf+chi)*rho+(lf+chi)*lw
+(1-chi)*lf)

(%i23) to_cl(%o6);

(/ (+ (* (+ (* CHI LF) CHI) RHO) (* (+ 1 (- CHI)) LW) (* CHI LF) 1)
(+ (* (+ 1 (* -1 CHI)) LF) (* (+ CHI LF) LW)
(* (+ CHI (* CHI LF)) RHO)))

If to_cl doesn't do exactly what you want, it's fairly easy to modify
the to_cl translator, I think.

Barton

-----maxima-bounces at math.utexas.edu wrote: -----

>How can I print algebraic expressions as an S-expression which is
>valid Common Lisp code?  For example,
>
>eq1: 1-rho*chi*(mu-1)=lw*beta*mu+q;
>eq2: q=lf*(1-beta)*mu;
>eq3: chi*(mu+1)=beta*mu+1-q;
>
>sol:solve([eq1, eq2, eq3], [mu,q,beta]);
>
>rhs(sol[1][1]); /* how to emit this as an S-expression? */
>
>gives
>
> (chi lf + chi) rho + (1 - chi) lw + chi lf + 1
>-------------------------------------------------
>(chi lf + chi) rho + (lf + chi) lw + (1 - chi) lf
>
>and I would like to have
>
>(/ (+ (* (+ (* chi lf) chi) rho) ...
>

```