Question: How to repeatedly read input and write output?

August 04 2014 liushunyi 25
Maple 17

0

Dear all,

I want to compute the charateristic polynomials of some matrices (the number of matrices is more than 10000), and write the coefficients of the resulting polynomials to a text file. The entries of the given matrices are stored in a text file. More specifically, suppose that M_1, M_2 and M_3 are three matrices, and the entries of them are stored in a text file named "data.txt" in the following form:

1 2 3

4 5 6

7 8 9

 

1 -1 0

2 3 6

-3 0 2

 

2 1 -1

1 -1 -2

0 1 2

The resulting output file should be of the following format:

1, -15, 18, 0

1, -6, 13, -28

1, -3, 1, 3

I don't know how to repeatedly read the data from the given text file. I think the procedure should be as follows.

>restart:

>with(LinearAlgebra):

>with(FileTools:-Text):

>fid:=Open("data.txt"):

>fid_1:=Open("data_1.txt"): #data_1 is the output file

>ReadFile(fid); #since the number of matrices is very large, I want the data to be read once

>numlines:=CountLines(fid);

>for i from 1 to numlines do

       here I need repeatedly read the data to a matrix M (I don't know how to do);

       P:=CharacteristicPolynomial(M,x);

       writedata[APPEND]("data_1.txt",map(i-> coeff(p, x, i), [seq(i, i = 0 .. degree(p, x))]));  #this statement cannot write the coefficients of a polynomial in the same row

>end do;

>Close(fid):

>Close(fid_1):

Now as I'm pretty new to Maple, I don't know any better and can't seem to get any information out of the help documents. 

Thanks a lot.

Please Wait...