CoCoA:HowTo:Read Matrices

From ApCoCoAWiki

This article is at the moment stub. I just wrote the code, but it is not properly tested and an description how to open and close the file is missing. Also support for negative and rational numbers is missing and perhaps there is a problem with newlines instead of blanks as seperators. multiple blanks will cause problems, too.

About

At the CoCoA Discussion Board enrico asked how to read matrices, given in a text file, such that we have a CoCoA Matrix. His matrices consist of integers, seperated by blanks and newlines, but it should not be a problem to adapt this.

The idea behind this is to use the Get function, or more generall the IO interface of CoCoA 4.

Reading one digit

Get returns one charakter, then we can use Ascii to convert this charakter into the coresponding Ascii value. Now we subtract the Ascii value of '0' and have the character convertet into it's coresponding digit (in case it was a number). The code for this looks something like:

 D := Ascii(Get(F,1)) - Ascii('0');
 If D IsIn [0..9] then
   Return D;
 Else
   Return 0;
 EndIf;

code by dheldt 17:00, 13 Jul 2005 (CEST)

Reading one integer

As you can see, in case of having other charakters inside your matrix they are simply going to be replaced by 0. Now we want to read complete integers, instead of only one digit. But an integer is just a list of digits, so it is not that complicated after all. let us assume, the integers are seperated by blanks. than we do the following:

 Define Read_Integer(F)
   Int := 0;
   C   := Get(F,1);
   While Not C = ' ' Do
      D := C - Ascii('0');
      If D IsIn [0..9] then
        Int := Int*10 + D;
      EndIf;
      C := Get(F,1);
   EndWhile;
   Return Int;
 EndDefine

code by dheldt 17:00, 13 Jul 2005 (CEST)

As you can see, here I simply ignore charakters, that do not fit in my format. Now, we are able to read one integer.

Optionally you can use this code now, to add a signum in front of it to support negative numbers, too. Otherwise you can add something to find a '/' to support all rational numbers.

But now it is time to read the complete matrix:

Reading the complete matrix

A matrix is -more or less- just a rectangular thing filled up with numbers. so we simply have to read all this numbers and put them in a matrix.

This could look like:

 M := Mat([[Read_Integer(F) |I In 1..Columns]|J In 1..Rows]);

code by dheldt 17:00, 13 Jul 2005 (CEST)