Exporting functions (for use in other applications)



Max,

> sorry for bothering you another time. :-) 

No problem, feel free to ask as many questions as you like.
By the way what are the huge functions you mention below?
That sounds interesting.

> Is it possible to export a function for use in another application? I have
> some huge functions in maxima that I need in another application. And if
> possible I wouldn't want to have to type them in myself.

There is a function to generate Fortran in Maxima, 
but it doesn't recognize function declarations. Rats! 
Here is a cheezoid hack that might work for you:

--------------- begin file tmp-fortra.lisp ---------------
(setq *fortran-function* (get '$fortran 'mfexpr*))

(defmspec $fortran (expr)
  (let ((args (cdr expr)))
    (dolist (arg args)
      (cond
        ((eq (caar arg) 'mdefine) (fortran-function arg))
        (t (funcall *fortran-function* (list '($fortran) arg)))))))

(defun fortran-function (arg)
  (let* ((fdecl (second arg)) (fname (caar fdecl)) (fargs (cdr fdecl))
(fbody (third arg)))
    (fortran-print (cons (list (concat '$function\  (stripdollar
fname))) fargs))
    (fortran-print (list '(mequal) fname fbody))
    (fortran-print '$return)
    (fortran-print '$end)))
--------------- end file tmp-fortra.lisp ---------------

and here's what the output looks like:
load ("tmp-fortra.lisp");  loads the new functions.

--------------- begin quoted output ---------------
(%i1) load ("tmp-fortra.lisp");
(%o1)                    tmp-fortra.lisp
(%i2) e: (x + y)^10 $
(%i3) barf (x, y) := ''e $
(%i4) fortran (''%)$
      function barf(x,y)
      barf = (y+x)**10
      return
      end
(%i5) e: expand ((x + y)^10) $
(%i6) barf (x, y) := ''e $
(%i7) fortran (''%)$
      function barf(x,y)
      barf = y**10+10*x*y**9+45*x**2*y**8+120*x**3*y**7+210*x**4*y**6+25
     1   2*x**5*y**5+210*x**6*y**4+120*x**7*y**3+45*x**8*y**2+10*x**9*y+
     2   x**10
      return
      end
(%i8) with_stdout ("barf.f", fortran (''%o6));
(%o8)                         false
(%i9) 
[robert@chromium src]$ cat barf.f 
      function barf(x,y)
      barf = y**10+10*x*y**9+45*x**2*y**8+120*x**3*y**7+210*x**4*y**6+25
     1   2*x**5*y**5+210*x**6*y**4+120*x**7*y**3+45*x**8*y**2+10*x**9*y+
     2   x**10
      return
      end
--------------- end quoted output ---------------

Note the use of with_stdout to capture the output in a file.

> Strictly speaking I'd like to export a function as C code.
> 
> If I have: f(x)=x^2; in maxima,
> I'd like to get something like: double f(x) { return pow(x,2); }

3 ideas here:
(1) The gentran add-on library (maxima/share/contrib/gentran/).
The documentation claims gentran can output Fortran, C, and Ratfor,
but I can't make it work for me. Rats! Does anyone know 
more about gentran?
(2) Generate Fortran as above and then translate Fortran->C 
using f2c (search the web for f2c).
(3) just compile Fortran with a Fortran compiler (e.g. g77)
and link it with your C program. I think you need to fudge the
function calls in either direction. Sorry, I don't remember the details.

> I have found the save() function, but that saves the function 
> in some for me rather hard to parse format.

save stores stuff as Lisp expressions. That is more convenient 
for internal manipulations. Incidentally Maxima is written in Lisp.

Hope this helps,
Robert Dodier