4

I am relatively new to maxima. I want to know how to write an array into a text file using maxima.

phobos
  • 105
  • 1
  • 5
  • 10

4 Answers4

5

I know it's late in the game for the original post, but I'll leave this here in case someone finds it in a search.

Let A be a Lisp array, Maxima array, matrix, list, or nested list. Then:

write_data (A, "some_file.data");

Let S be an ouput stream (created by openw or opena). Then:

write_data (A, S);

Entering ?? numericalio at the input prompt, or ?? write_ or ?? read_, will show some info about this function and related ones.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
2

A bit more necroposting, as google leads here, but I haven't found it useful enough. I've needed to export it as following:

-0.8000,-0.8000,-0.2422,-0.242
-0.7942,-0.7942,-0.2387,-0.239
-0.7776,-0.7776,-0.2285,-0.228
-0.7514,-0.7514,-0.2124,-0.212
-0.7168,-0.7168,-0.1912,-0.191
-0.6750,-0.6750,-0.1655,-0.166
-0.6272,-0.6272,-0.1362,-0.136
-0.5746,-0.5746,-0.1039,-0.104

So I've found how to do this with printf:

with_stdout(filename, for i:1 thru length(z_points) do
        printf (true,"~,4f,~,4f,~,4f,~,3f~%",bot_points[i],bot_points[i],top_points[i],top_points[i]));
ProdoElmit
  • 1,067
  • 9
  • 22
2

I've never used maxima (or even heard of it), but a little Google searching out of curiousity turned up this: http://arachnoid.com/maxima/files_functions.html

From what I can gather, you should be able to do something like this:

stringout("my_new_file.txt",values);

It says the second parameter to the stringout function can be one or more of these:

  • input: all user entries since the beginning of the session.
  • values: all user variable and array assignments.
  • functions: all user-defined functions (including functions defined within any loaded packages).
  • all: all of the above. Such a list is normally useful only for editing and extraction of useful sections.

So by passing values it should save your array assignments to file.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
0

A bit cleaner variation on the @ProdoElmit's answer:

list : [1,2,3,4,5]$
with_stdout("file.txt", apply(print, list))$
/* 1 2 3 4 5 is then what appears in file.txt */

Here the trick with apply is needed as you probably don't want to have square brackets in your output, as is produced by print(list).

For a matrix to be printed out, I would have done the following:

m : matrix([1,2],[3,4])$
with_stdout("file.txt", for row in args(m) do apply(print, row))$
/* 1 2 
   3 4 
   is what you then have in file.txt */

Note that in my solution the values are separated with spaces and the format of your values is fixed to that provided by print. Another caveat is that there is a limit on the number of function parameters: for example, for me (GCL 2.6.12) my method does not work if length(list) > 64.

Andrii
  • 11
  • 3