Reading a txt file and building a matrix

Questions and problems about using polymake go here.
sofiag96
Posts: 2
Joined: 05 Jul 2022, 19:26

Reading a txt file and building a matrix

Postby sofiag96 » 05 Jul 2022, 19:38

Hi all,

I am still very new to Polymake (but have managed to do my first computations!) and I have a question regarding reading a .txt file. I have a txt file where each of its rows describes the vertices of a polytope, i.e., the file looks like this:

[[0,0],[1,0],[1,2]]
[[0,0],[1,0],[0,1],[1,1]]
...

I want to read the file line by line and create a polytope with the entry from each line. My reasoning is that I could create a matrix from each line, then insert a column of ones and build the polytope I want. So far, I have managed to read the file and compiled all lines of the file into an array @lines

If I now print the following

print "\$lines = $lines\n";

I obtain the correct result for each line where i is between 0 and the number of lines of the txt file. However, if I try to create a matrix using for example

my $m=new Matrix<Rational>(<$lines[0]>);
print $m;

this does not generate an ERROR but it does not print my matrix m. I am not sure what the error here is, but I believe it has something to do with the data type obtained from the txt file. For example, if I write

my $m=new Matrix<Rational>([[0,0],[1,0],[1,2]]);
print $m;

this describes precisely the object I want to obtain from the code above. Could you help me realize my mistake? Or is there an easier way of constructing a polytope from such a txt file?

Thank you!

User avatar
gawrilow
Main Author
Posts: 405
Joined: 25 Dec 2010, 17:40

Re: Reading a txt file and building a matrix

Postby gawrilow » 06 Jul 2022, 09:42

Operator <> in perl means reading text from a file handle. However, I suspect, your array @lines already contains the rows with numbers loaded from your file, you should simply pass it by reference:

new Matrix<Rational>(\@lines)

If $lines[$i] still contains a line of text just looking like perl data, you should "translate" it into real perl objects:

new Matrix<Rational>(eval $lines[$i])

sofiag96
Posts: 2
Joined: 05 Jul 2022, 19:26

Re: Reading a txt file and building a matrix

Postby sofiag96 » 06 Jul 2022, 16:38

This works perfectly! Thanks!


Return to “Helpdesk”