Differences
This shows you the differences between two versions of the page.
— | user_guide:tutorials:latest:perl_intro [2023/11/06 10:57] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Using Perl within polymake ====== | ||
+ | |||
+ | The language that the interactive version of '' | ||
+ | |||
+ | ==== Standard data structures ==== | ||
+ | |||
+ | The Perl programming language originally provides three different data structures, scalars('' | ||
+ | |||
+ | < | ||
+ | > i=5; | ||
+ | polymake: | ||
+ | </ | ||
+ | Here are some simple commands illustrating how to use the different data structures: | ||
+ | |||
+ | == Scalars == | ||
+ | |||
+ | <code perl> | ||
+ | > $i=5; | ||
+ | > $j=6; | ||
+ | > $sum=$i+$j; print $sum; | ||
+ | 11 | ||
+ | </ | ||
+ | == Arrays == | ||
+ | |||
+ | <code perl> | ||
+ | > @array=(" | ||
+ | > push(@array," | ||
+ | > $first_entry=$array[0]; | ||
+ | > print join(" | ||
+ | > @array2=(3, | ||
+ | > print sort(@array2); | ||
+ | 3a b c daa | ||
+ | b | ||
+ | c | ||
+ | d1234 | ||
+ | </ | ||
+ | == Hashes == | ||
+ | |||
+ | <code perl> | ||
+ | > %hash=(); | ||
+ | > $hash{" | ||
+ | > $hash{" | ||
+ | > print keys %hash; | ||
+ | > print join(", | ||
+ | > print join(", | ||
+ | > %hash=(" | ||
+ | > %hash=(" | ||
+ | fourzerofour, | ||
+ | </ | ||
+ | ==== polymake-Perl ==== | ||
+ | |||
+ | In addition to the three standard data structures, the enriched version of '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | <code perl> | ||
+ | > $p = new Polytope(POINTS=> | ||
+ | </ | ||
+ | Note that the '' | ||
+ | |||
+ | <code perl> | ||
+ | > print $p; | ||
+ | Polymake:: | ||
+ | </ | ||
+ | In this case it is a '' | ||
+ | |||
+ | <code perl> | ||
+ | > print $p-> | ||
+ | Polytope< | ||
+ | </ | ||
+ | === "Small objects": | ||
+ | |||
+ | You can use objects that are inherited from the '' | ||
+ | |||
+ | == Arrays == | ||
+ | |||
+ | The small object '' | ||
+ | |||
+ | <code perl> | ||
+ | > @array=(" | ||
+ | > $arr1=new Array< | ||
+ | > $arr2=new Array< | ||
+ | > $arr3=new Array< | ||
+ | > $arr4=new Array< | ||
+ | > $arr5=new Array< | ||
+ | a b c3 2 50 1 2 30 1 2 3 40 1 2 3 4 | ||
+ | </ | ||
+ | You have random access: | ||
+ | |||
+ | <code perl> | ||
+ | > $arr5-> | ||
+ | > print $arr5; | ||
+ | 100 1 2 3 4 | ||
+ | </ | ||
+ | It is also possible to convert the '' | ||
+ | |||
+ | <code perl> | ||
+ | > @arr4=@{$arr4}; | ||
+ | 3 2 5 | ||
+ | </ | ||
+ | or simply | ||
+ | |||
+ | <code perl> | ||
+ | > @arr4=@$arr4; | ||
+ | </ | ||
+ | == Sets == | ||
+ | |||
+ | On '' | ||
+ | |||
+ | <code perl> | ||
+ | > $set=new Set< | ||
+ | > print $set-> | ||
+ | > @array_from_set=@$set; | ||
+ | {2 3 5}3 | ||
+ | </ | ||
+ | == Matrices == | ||
+ | |||
+ | Here is a simple way to initialize a matrix: | ||
+ | |||
+ | <code perl> | ||
+ | > $mat=new Matrix< | ||
+ | > print $mat; | ||
+ | 2 1 4 0 0 | ||
+ | 3 1 5 2 1 | ||
+ | 1 0 4 0 6 | ||
+ | </ | ||
+ | You could also define it by passing a reference to an ('' | ||
+ | |||
+ | <code perl> | ||
+ | > $row1=new Vector< | ||
+ | > $row2=new Vector< | ||
+ | > $row3=new Vector< | ||
+ | > @matrix_rows=($row1, | ||
+ | > $matrix_from_array=new Matrix< | ||
+ | </ | ||
+ | You can change a single entry of a matrix in the following way (if it is not already assigned to an immutable property like '' | ||
+ | |||
+ | <code perl> | ||
+ | > $mat-> | ||
+ | > print $mat-> | ||
+ | > print $mat, " | ||
+ | > $mat-> | ||
+ | > print $mat; | ||
+ | 7 | ||
+ | 2 1 4 0 0 | ||
+ | 3 7 5 2 1 | ||
+ | 1 0 4 0 6 | ||
+ | |||
+ | 2 1 4 0 0 | ||
+ | 3 7 8 2 1 | ||
+ | 1 0 4 0 6 | ||
+ | </ | ||
+ | A unit matrix of a certain dimension can be defined via the user-function '' | ||
+ | |||
+ | <code perl> | ||
+ | > $unit_mat=4*unit_matrix< | ||
+ | > print $unit_mat; | ||
+ | (3) (0 4) | ||
+ | (3) (1 4) | ||
+ | (3) (2 4) | ||
+ | </ | ||
+ | The reason for the " | ||
+ | |||
+ | <code perl> | ||
+ | > print ref($unit_mat); | ||
+ | Polymake:: | ||
+ | </ | ||
+ | However, some functions cannot deal with this special type of matrix. In this case it is necessary to transform the sparse matrix into a dense matrix first via: | ||
+ | |||
+ | <code perl> | ||
+ | > $dense=new Matrix< | ||
+ | 4 0 0 | ||
+ | 0 4 0 | ||
+ | 0 0 4 | ||
+ | </ | ||
+ | or just | ||
+ | |||
+ | <code perl> | ||
+ | > $dense2=dense($unit_mat); | ||
+ | 4 0 0 | ||
+ | 0 4 0 | ||
+ | 0 0 4 | ||
+ | </ | ||
+ | You can also work with matrices that have different types of coordinates like '' | ||
+ | |||
+ | <code perl> | ||
+ | > $m_rat=new Matrix< | ||
+ | > $m2=$mat/ | ||
+ | > $m_int=new Matrix< | ||
+ | 3/5 0 0 0 0 | ||
+ | 0 3/5 0 0 0 | ||
+ | 0 0 3/5 0 0 | ||
+ | 0 0 0 3/5 0 | ||
+ | 0 0 0 0 3/5 | ||
+ | |||
+ | 2 1 4 0 0 | ||
+ | 3 7 8 2 1 | ||
+ | 1 0 4 0 6 | ||
+ | 3/5 0 0 0 0 | ||
+ | 0 3/5 0 0 0 | ||
+ | 0 0 3/5 0 0 | ||
+ | 0 0 0 3/5 0 | ||
+ | 0 0 0 0 3/5 | ||
+ | |||
+ | 1 0 0 0 0 | ||
+ | 0 1 0 0 0 | ||
+ | 0 0 1 0 0 | ||
+ | 0 0 0 1 0 | ||
+ | 0 0 0 0 1 | ||
+ | |||
+ | </ | ||
+ | Sometimes there is incompatible types: | ||
+ | |||
+ | <code perl> | ||
+ | > $m3=$m_rat/ | ||
+ | C++/perl Interface module compilation failed; most likely due to a type mismatch. | ||
+ | Set the variable $Polymake:: | ||
+ | </ | ||
+ | The error message indicates that you need to convert the integer matrix to a rational matrix first: | ||
+ | |||
+ | <code perl> | ||
+ | > $m3=$m_rat/ | ||
+ | 3/5 0 0 0 0 | ||
+ | 0 3/5 0 0 0 | ||
+ | 0 0 3/5 0 0 | ||
+ | 0 0 0 3/5 0 | ||
+ | 0 0 0 0 3/5 | ||
+ | 1 0 0 0 0 | ||
+ | 0 1 0 0 0 | ||
+ | 0 0 1 0 0 | ||
+ | 0 0 0 1 0 | ||
+ | 0 0 0 0 1 | ||
+ | </ | ||
+ | By "/" | ||
+ | |||
+ | <code perl> | ||
+ | > $z_vec=zero_vector< | ||
+ | > $extended_matrix=($z_vec|$m_int); | ||
+ | 0 1 0 0 0 0 | ||
+ | 0 0 1 0 0 0 | ||
+ | 0 0 0 1 0 0 | ||
+ | 0 0 0 0 1 0 | ||
+ | 0 0 0 0 0 1 | ||
+ | </ | ||
+ | It is also possible to nest template parameters in any way you like, e.g. | ||
+ | |||
+ | <code perl> | ||
+ | > $set=new Set< | ||
+ | > $template_Ex=new Array< | ||
+ | {2 5 6} | ||
+ | {2 3 5} | ||
+ | Polymake:: | ||
+ | </ | ||
+ | However, if you use a template combination, | ||
+ | |||
+ | === "Big Objects": | ||
+ | |||
+ | A big object is an instance of a data type which represents a mathematical concept with clear semantics. They may have template parameters. | ||
+ | |||
+ | <code perl> | ||
+ | > $p=new Polytope< | ||
+ | > $lp=new LinearProgram< | ||
+ | </ | ||
+ | Big objects have properties which come with a type, which is either built-in or a small object type or a big object type, and which can be accessed using the '' | ||
+ | |||
+ | <code perl> | ||
+ | > # access the property named `LP`: | ||
+ | > $p-> | ||
+ | > # properties can have properties themselves. | ||
+ | > print $p-> | ||
+ | 4 | ||
+ | </ | ||
+ | Scalar properties can be used in arithmetic expressions right away. | ||
+ | |||
+ | <code perl> | ||
+ | > $i = ($p-> | ||
+ | > print $i; | ||
+ | 960 | ||
+ | </ | ||
+ | Check out the tutorial on properties to learn more about the way properties are used and computed. | ||
+ | |||
+ | ==== A small example script... ==== | ||
+ | |||
+ | ...to demonstrate the usage of '' | ||
+ | |||
+ | <code perl> | ||
+ | > ### load matrix from file | ||
+ | > open(INPUT, "< demo/ | ||
+ | > $matrix=new Matrix< | ||
+ | > close(INPUT); | ||
+ | > print $matrix; | ||
+ | > | ||
+ | > | ||
+ | > ### create a polytope from the matrix | ||
+ | > $p=new Polytope< | ||
+ | > print $p-> | ||
+ | > print $p->DIM; | ||
+ | > print $p-> | ||
+ | > | ||
+ | > | ||
+ | > ### print " | ||
+ | > for(my $i=0; | ||
+ | > | ||
+ | > print $i.": " | ||
+ | > } | ||
+ | > } | ||
+ | > | ||
+ | > | ||
+ | > ### put their indices in a set | ||
+ | > $s=new Set< | ||
+ | > for(my $i=0; | ||
+ | > | ||
+ | > | ||
+ | > } | ||
+ | > } | ||
+ | > | ||
+ | > | ||
+ | > ### iterate the set in two different ways | ||
+ | > foreach(@{$s}){ | ||
+ | > print $p-> | ||
+ | > } | ||
+ | > foreach my $index(@{$s}){ | ||
+ | > print $p-> | ||
+ | > } | ||
+ | > | ||
+ | > | ||
+ | > ### create a minor of the vertices matrix that only contains the simple ones | ||
+ | > $special_points=$p-> | ||
+ | -1 | ||
+ | </ | ||
+ | ==== Writing scripts ==== | ||
+ | |||
+ | Comprehensive information on how to use scripts within '' | ||
+ | |||