Read-only forum archive

Problem with writelist and list of matrices

Problem with writelist and list of matrices

Ravi · Sat Sep 12, 2009 8:55 am

I have a problem using the writelist function when I write a list of matrices to a file and want to recreate the list in memory by reading the file later on. Here is a simple example:

Code:
ring R = 0, (x,y,z), dp;
matrix A[3][3] = 0,1,0, -1,0,0, 0,0,1;      
matrix B[3][3] = 0,1,0,  0,0,1, 1,0,0;      
matrix C[3][3] = 0,1,0,  1,0,0, 0,0,1;
list L = A,B,C;
writelist("simple-write.txt", "simplist", L);


This creates the text file alright:
list simplist;
simplist[1]=matrix(
0,1,0,-1,0,0,0,0,1);
etc...

but when I try to read it and recreate simplist:

Code:
ring R = 0, (x,y,z), dp;
execute(read("simple-write.txt"));  // does not create the list simplist in memory


The error I get is:
Code:
? error occurred in Read-Oh.sing line 10: `0,1,0,-1,0,0,0,0,1);`
? wrong type declaration. type 'help matrix;'
? last reserved name was `matrix`
skipping text from `-` error at token `)`

According to the help files (D.2.4.9), this should create the object simplist. It works for other objects, but not for lists of matrices.
If someone can tell me what is going wrong, I shall be most grateful...

Ravi

Re: Problem with writelist and list of matrices

gorzel · Tue Sep 15, 2009 12:55 pm

The cast matrix is incomplete, dimensions nrows and ncols are missing.

To fix the problem for the datataypes matrix and intmat,
change the for-loop as follows

Code:
for( i=1;i<=size(L);i=i+1 )
   {
     if (typeof(L[i]) == "intmat")
     {
       write(fil,"   "+nam+"["+string(i)+"]="+
              "intmat(intvec("+string(L[i])+
          "),"+string(nrows(L[i]))+","+string(ncols(L[i]))+");");
     }
     if ( typeof(L[i])== "matrix")
     {
        write(fil,"   "+nam+"["+string(i)+"]="+
              "matrix(ideal("+string(L[i])+
          "),"+string(nrows(L[i]))+","+string(ncols(L[i]))+");");
     }
     else
     {
       write(fil,"   "+nam+"["+string(i)+"]="+typeof(L[i])+"(",string(L[i])+");");
     }
   }


(Note: Other datatypes as string also do not work properly.)

Re: Problem with writelist and list of matrices

Ravi · Tue Sep 15, 2009 3:39 pm

Thank you. It works now. To avoid defining the list every time I read the file, I added a declaration before your for loop:

write(fil, "list " + nam + ";");

Thanks again.

Ravi :)