Reading Files Sequentially



Hello Lorenzo,

> I have just started using Maxima and I have not found
> out exactly how to read a list of files (each of them
> simply contains numbers) and save them into an array.

OK, bear in mind that Maxima makes a distinction between
arrays and matrices. Arrays are not very well integrated
into Maxima; I'm pretty sure you want to work with matrices
and lists. I'll assume that in the following.

> Say that the files are named file1,file2...fileN and
> each of them has M entries. I need to read them to
> form an MxMNarray.

To read one file into a matrix, you can use the read_matrix
function in the numericalio package. You want to get numericalio.lisp:
http://cvs.sourceforge.net/viewcvs.py/maxima/maxima/share/contrib/numericalio/
It has been updated recently, please get the CVS version
(don't use the version bundled with Maxima 5.9.1).

Read one file --

  load ("numericalio.lisp");
  M1: read_matrix ("file1");

Read all files and construct a big matrix from them --

  N: 10; /* whatever -- it must be a known value */
  L: makelist (read_matrix (concat ("file", i)), i, 1, N);
  /* Now L[1], ..., L[N] are the matrices from file1, ..., fileN */
  M: apply (addcol, L);  /* paste together side by side */
  M: apply (addrow, L);  /* paste together one on top of another */

This last bit -- apply (addcol, L) -- will fail in GCL Maxima
if N is bigger than 64 (too many arguments in function call). Rats!

> Then; is Maxima smart enough to read numbers saved
> with the scientific notation, i.e. containing some
> power of 10 in their expression? (3E-3,-4E+2, etc).

Short answer: yes.

Long answer: the current revision of numericalio calls
scan-one-token-g to get the next item from the file.
So that establishes the capability of numericalio.
scan-one-token-g recognizes integers, floating point 
in xxx.yyy and xxx.yyyEzzz (also e, and D and d),
symbols (case sensitive), and "quoted strings".

numericalio allows comma , and pipe symbol | as separators
(in addition to allowing whitespace separated data),
e.g., read_matrix ("file1", 'csv) (for comma separated)
and read_matrix ("file1", 'pipe) (for pipe separated).

Hope this helps. Let me know if numericalio is not
working for you, I'll certainly try to help.

Robert Dodier


		
__________________________________ 
Discover Yahoo! 
Find restaurants, movies, travel and more fun for the weekend. Check it out! 
http://discover.yahoo.com/weekend.html