Differences
This shows you the differences between two versions of the page.
— | user_guide:tutorials:latest:data [2023/11/06 10:57] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Save and load data in polymake ====== | ||
+ | |||
+ | In polymake there are different ways to save and load data depending on the type and the format of the data. We distinguish between `big' objects (Polytope, Matroid, | ||
+ | |||
+ | ===== Handling polymake objects ===== | ||
+ | |||
+ | Let us take this nice example object: | ||
+ | |||
+ | <code perl> | ||
+ | > $p = cube(3); | ||
+ | </ | ||
+ | To store big objects use the command | ||
+ | |||
+ | <code perl> | ||
+ | > save($p," | ||
+ | </ | ||
+ | This will create a file in JSON format. An existing files will be silently overwritten. | ||
+ | |||
+ | To load a big object from such a file use the command | ||
+ | |||
+ | <code perl> | ||
+ | > $p=load(" | ||
+ | </ | ||
+ | If you did not start '' | ||
+ | |||
+ | <code perl> | ||
+ | $p=load(" | ||
+ | </ | ||
+ | TAB completion like in a usual UNIX shell supports you in navigating through the file system. | ||
+ | |||
+ | **Note:** If you load a polymake object and compute new properties, these properties will automatically be added to the original file at the end of the session. You can suppress this with the command | ||
+ | |||
+ | <code perl> | ||
+ | > $p-> | ||
+ | </ | ||
+ | called prior to leaving the session (but after the last computation with $p). | ||
+ | |||
+ | If you want to store a collection of objects into a single file, there is an [[tarballs|extra tutorial]] for you. | ||
+ | |||
+ | ===== Handling complex data types ===== | ||
+ | |||
+ | Apart from big objects, you can also persistently store arbitrary data structures like matrices or graphs format via '' | ||
+ | |||
+ | <code perl> | ||
+ | > $s=new Set< | ||
+ | > save_data($s, | ||
+ | </ | ||
+ | To load data objects from such a file use the command | ||
+ | |||
+ | <code perl> | ||
+ | > $s=load_data(" | ||
+ | </ | ||
+ | In fact, the command pairs '' | ||
+ | |||
+ | ===== Pretty formatting of JSON files ===== | ||
+ | |||
+ | Files produced by '' | ||
+ | |||
+ | <code perl> | ||
+ | > save($p," | ||
+ | </ | ||
+ | ===== Saving visualized objects ===== | ||
+ | |||
+ | Furthermore, | ||
+ | |||
+ | To save the cube visualized via JReality in a new file called '' | ||
+ | |||
+ | <code perl> | ||
+ | jreality(cube(3)-> | ||
+ | </ | ||
+ | To save the cube as a TiKz file named '' | ||
+ | |||
+ | <code perl> | ||
+ | tikz(cube(3)-> | ||
+ | </ | ||
+ | ===== Handling arbitrary files ===== | ||
+ | |||
+ | Of course, it is also possible to load data from files in other formats. For this purpose use the standard Perl functions for reading and writing. Here is an example: | ||
+ | |||
+ | Assume you want to load some points stored in the file points.txt which looks like this: 1 0 0 0 1 1 0 0 1 0 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 For the sake of the example, let's create this file: | ||
+ | |||
+ | <code perl> | ||
+ | > open(my $f, ">", | ||
+ | </ | ||
+ | To read this file try the following: | ||
+ | |||
+ | <code perl> | ||
+ | > open(INPUT, "<", | ||
+ | > while(< | ||
+ | > print $_; | ||
+ | > } | ||
+ | > close(INPUT); | ||
+ | 1 0 0 0 | ||
+ | 1 1 0 0 | ||
+ | 1 0 1 0 | ||
+ | 1 1 1 0 | ||
+ | 1 0 0 1 | ||
+ | 1 1 0 1 | ||
+ | 1 0 1 1 | ||
+ | 1 1 1 1 | ||
+ | </ | ||
+ | '' | ||
+ | |||
+ | A reasonable task could be to store the points from the file as a matrix. This can be done immediately, | ||
+ | |||
+ | <code perl> | ||
+ | > open(INPUT, "<", | ||
+ | > $matrix=new Matrix< | ||
+ | > close(INPUT); | ||
+ | > print $matrix; | ||
+ | 1 0 0 0 | ||
+ | 1 1 0 0 | ||
+ | 1 0 1 0 | ||
+ | 1 1 1 0 | ||
+ | 1 0 0 1 | ||
+ | 1 1 0 1 | ||
+ | 1 0 1 1 | ||
+ | 1 1 1 1 | ||
+ | </ | ||
+ | |||