with_stdout, streams, & strings, was: f90 language translation example



On 7/13/07, Todd Hay <haymeister at gmail.com> wrote:

> I'd like to be able to write a file from maxima that I can paste directly
> into a fortran 90 subroutine. The 'f90' module seems to be suited for this
> but I don't know how to gain access to the string that f90(expr); prints to
> the screen (f90 always returns 'done' by design). I need access to the
> string so that I can manipulate it and concatenate another string to the
> expression before writing to the file.

Todd, I've (1) modified with_stdout to accept an argument which is a
Lisp stream (more or less an open file), and (2) made a simple-minded
interface to the Lisp functions for string output streams.

With those changes together, I think you can get what you've
described above, as shown in the example session below.
I've appended the changes as a patch, which I'll commit if I
don't hear any complaints.

HTH
Robert Dodier

PS. Example session:

(%i1) s : make_string_output_stream ();
(%o1)            #<OUTPUT STRING-OUTPUT-STREAM>
(%i2) load (f90);
(%o2) /home/robert/tmp/maxima-head/maxima/share/contrib/f90.lisp
(%i3) with_stdout (s, f90 (expand ((sin(a) - sin(b))^3)));
(%o3)                         done
(%i4) s1 : get_output_stream_string (s);
(%o4) -sin(b)**3+3*sin(a)*sin(b)**2-3*sin(a)**2*sin(b)+sin(a)**3
(%i5) with_stdout (s, f90 (expand ((cos(a) - cos(b))^3)));
(%o5)                         done
(%i6) s2 : get_output_stream_string (s);
(%o6) -cos(b)**3+3*cos(a)*cos(b)**2-3*cos(a)**2*cos(b)+cos(a)**3
(%i7) concat (s1, s2);
(%o7) -sin(b)**3+3*sin(a)*sin(b)**2-3*sin(a)**2*sin(b)+sin(a)**3
-cos(b)**3+3*cos(a)*cos(b)**2-3*cos(a)**2*cos(b)+cos(a)**3
-------------- next part --------------
A non-text attachment was scrubbed...
Name: with_stdout.patch
Type: application/octet-stream
Size: 3484 bytes
Desc: not available
Url : http://www.math.utexas.edu/pipermail/maxima/attachments/20070717/a23272d7/attachment.obj