translating formulas to Common Lisp



Tamas Papp <tkpapp at gmail.com> writes:
> Hi,
>
> Is there a quick & easy way to translate results of calculations to
> Common Lisp (or any Lisp) sexps?  Eg a+b*c would become (+ a (* b c)),
> etc.  I need this because I would like to use analytical results,
> calculated with Maxima, in a CL program, and I don't want to transcribe
> it manually if that can be avoided.
>
> Best,
>
> Tamas

If you use ?print(expr), this is equivalent to calling (print XX) in
Common Lisp, where XX is some cons representing expr. For example:

  (%i1) first(solve(x^3+3*x+4 = x, x));
                                                         sqrt(3) %i   1
                                                      2 (---------- - -)
             2 sqrt(29)     1/3    sqrt(3) %i   1            2        2
  (%o1) x = (---------- - 2)    (- ---------- - -) - ---------------------
                 3/2                   2        2       2 sqrt(29)     1/3
                3                                    3 (---------- - 2)
                                                            3/2
                                                           3
  (%i2) ?print(%o1)$

  ((MEQUAL SIMP) $X
   ((MPLUS SIMP)
    ((MTIMES SIMP)
     ((MEXPT SIMP)
      ((MPLUS SIMP) -2
       ((MTIMES SIMP) 2 ((MEXPT SIMP) 3 ((RAT SIMP) -3 2))
        ((MEXPT SIMP) 29 ((RAT SIMP) 1 2))))
      ((RAT SIMP) 1 3))
     ((MPLUS SIMP) ((RAT SIMP) -1 2)
      ((MTIMES SIMP) ((RAT SIMP) -1 2) ((MEXPT SIMP) 3 ((RAT SIMP) 1 2)) $%I)))
    ((MTIMES SIMP) ((RAT SIMP) -2 3)
     ((MEXPT SIMP)
      ((MPLUS SIMP) -2
       ((MTIMES SIMP) 2 ((MEXPT SIMP) 3 ((RAT SIMP) -3 2))
        ((MEXPT SIMP) 29 ((RAT SIMP) 1 2))))
      ((RAT SIMP) -1 3))
     ((MPLUS SIMP) ((RAT SIMP) -1 2)
      ((MTIMES SIMP) ((RAT SIMP) 1 2) ((MEXPT SIMP) 3 ((RAT SIMP) 1 2)) $%I))))) 

To translate to a formula you can use in Common Lisp, you'll need to
change MPLUS to +, MTIMES to * etc.

I presume that you have just a few such formulas. In which case, a
find+replace on the text is probably the most sensible. Trying to do it
programmatically, I can imagine I'd get tripped up by lots of corner
cases.

Rupert
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 315 bytes
Desc: not available
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20130719/e43ef00c/attachment.pgp>;