Changeset e57255 in git for Singular/LIB/polymake.lib


Ignore:
Timestamp:
Dec 16, 2013, 3:12:47 PM (10 years ago)
Author:
Oleksandr Motsak <motsak@…>
Branches:
(u'spielwiese', '17f1d200f27c5bd38f5dfc6e8a0879242279d1d8')
Children:
d186d3baff30f2e6e96b2a354ff156ec286fcb4ef24b9c65dd9d2b3d937e5841295580dedc2f7412
Parents:
27877c10cae6d5eb945d62a27d66dfdf8a5a47849e8ae12733559521215c7641624da588f2d8cdf3
Message:
Merge pull request #429 from YueRen/polymakelib

Polymakelib
File:
1 moved

Legend:

Unmodified
Added
Removed
  • Singular/LIB/polymake.lib

    r27877c re57255  
    1 ////
    2 version="version oldpolymake.lib 4.0.0.0 Jun_2013 "; // $Id$
     1version="version oldpolymake.lib 4.0.0.0 Jun_2013 ";
    32category="Tropical Geometry";
    43info="
    5 LIBRARY:   oldpolymake.lib  Computations with polytopes and fans,
    6                             interface to polymake and TOPCOM
     4LIBRARY:   polymake.lib    Computations with polytopes and fans,
     5                           interface to polymake and TOPCOM
    76AUTHOR:    Thomas Markwig,  email: keilen@mathematik.uni-kl.de
    87
     
    109   Most procedures will not work unless polymake or topcom is installed and
    1110   if so, they will only work with the operating system LINUX!
    12    For more detailed information see the following note or consult the
     11   For more detailed information see IMPORTANT NOTE respectively consult the
    1312   help string of the procedures.
    1413
    15 NOTE:
     14   The conventions used in this library for polytopes and fans, e.g. the
     15   length and labeling of their vertices resp. rays, differs from the conventions
     16   used in polymake and thus from the conventions used in the polymake
     17   extension polymake.so of Singular. We recommend to use the newer polymake.so
     18   whenever possible.
     19
     20IMPORTANT NOTE:
    1621   Even though this is a Singular library for computing polytopes and fans
    1722   such as the Newton polytope or the Groebner fan of a polynomial, most of
     
    1924@* - polymake by Ewgenij Gawrilow, TU Berlin and Michael Joswig, TU Darmstadt
    2025@*   (see http://www.math.tu-berlin.de/polymake/),
    21 @* respectively (only in the procedure triangularions) by the program
    22 @* - topcom by Joerg Rambau, Universitaet Bayreuth (see http://www.uni-bayreuth.de/
    23      departments/wirtschaftsmathematik/rambau/TOPCOM);
    24 @*   This library should rather be seen as an interface which allows to use a
     26@* respectively (only in the procedure triangulations) by the program
     27@* - topcom by Joerg Rambau, Universitaet Bayreuth (see
     28@*   http://www.uni-bayreuth.de/departments/wirtschaftsmathematik/rambau/TOPCOM);
     29@*   this library should rather be seen as an interface which allows to use a
    2530     (very limited) number of options which polymake respectively topcom offers
    2631     to compute with polytopes and fans and to make the results available in
     
    3237     independent of both, polymake and topcom.
    3338
    34 PROCEDURES:
    35   polymakePolytope()   computes the vertices of a polytope using polymake
    36   newtonPolytopeP()     computes the Newton polytope of a polynomial
    37   newtonPolytopeLP()   computes the lattice points of the Newton polytope
    38   normalFan()          computes the normal fan of a polytope
    39   groebnerFan()        computes the Groebner fan of a polynomial
    40   intmatToPolymake()   transforms an integer matrix into polymake format
    41   polymakeToIntmat()   transforms polymake output into an integer matrix
    42 
    43   triangulations()     computes all triangulations of a marked polytope
    44   secondaryPolytope()  computes the secondary polytope of a marked polytope
    45 
    46   secondaryFan()       computes the secondary fan of a marked polytope
    47 
    48   cycleLength()      computes the cycleLength of cycle
    49   splitPolygon()     splits a marked polygon into vertices, facets, interior points
    50   eta()              computes the eta-vector of a triangulation
    51   findOrientedBoundary()    computes the boundary of a convex hull
    52   cyclePoints()      computes lattice points connected to some lattice point
    53   latticeArea()      computes the lattice area of a polygon
    54   picksFormula()     computes the ingrediants of Pick's formula for a polygon
    55   ellipticNF()       computes the normal form of an elliptic polygon
    56   ellipticNFDB()     displays the 16 normal forms of elliptic polygons
    57 
    58   polymakeKeepTmpFiles()  determines if the files created in /tmp should be kept
     39PROCEDURES USING POLYMAKE:
     40  polymakePolytope()  computes the vertices of a polytope using polymake
     41  newtonPolytopeP()    computes the Newton polytope of a polynomial
     42  newtonPolytopeLP()  computes the lattice points of the Newton polytope
     43  normalFanL()         computes the normal fan of a polytope
     44  groebnerFan()       computes the Groebner fan of a polynomial
     45
     46PROCEDURES USING TOPCOM:
     47  triangulations()    computes all triangulations of a marked polytope
     48  secondaryPolytope() computes the secondary polytope of a marked polytope
     49
     50PROCEDURES USING POLYMAKE AND TOPCOM:
     51  secondaryFan()      computes the secondary fan of a marked polytope
     52
     53PROCEDURES CONERNED WITH PLANAR POLYGONS:
     54  cycleLength()    computes the cycleLength of cycle
     55  splitPolygon()   splits a marked polygon into vertices, facets, interior points
     56  eta()            computes the eta-vector of a triangulation
     57  findOrientedBoundary()  computes the boundary of a convex hull
     58  cyclePoints()    computes lattice points connected to some lattice point
     59  latticeArea()    computes the lattice area of a polygon
     60  picksFormula()   computes the ingrediants of Pick's formula for a polygon
     61  ellipticNF()     computes the normal form of an elliptic polygon
     62  ellipticNFDB()   displays the 16 normal forms of elliptic polygons
    5963
    6064KEYWORDS:    polytope; fan; secondary fan; secondary polytope; polymake;
     
    8589////////////////////////////////////////////////////////////////////////////////
    8690
     91static proc mod_init ()
     92{
     93  LIB "gfanlib.so";
     94  LIB "polymake.so";
     95}
    8796
    8897/////////////////////////////////////////////////////////////////////////////
     
    9099/////////////////////////////////////////////////////////////////////////////
    91100
    92 proc polymakePolytope (intmat polytop,list #)
    93 "USAGE:  polymakePolytope(polytope[,#]);   polytope list, # string
    94 ASSUME:  each row of polytope gives the coordinates of a lattice point of a
     101proc polymakePolytope (intmat points)
     102"USAGE:  polymakePolytope(points);   polytope intmat
     103ASSUME:  each row of points gives the coordinates of a lattice point of a
    95104         polytope with their affine coordinates as given by the output of
    96105         secondaryPolytope
    97106PURPOSE: the procedure calls polymake to compute the vertices of the polytope
    98107         as well as its dimension and information on its facets
    99 RETURN:  list L with four entries
     108RETURN:  list, L with four entries
    100109@*            L[1] : an integer matrix whose rows are the coordinates of vertices
    101110                     of the polytope
    102111@*            L[2] : the dimension of the polytope
    103 @*            L[3] : a list whose i-th entry explains to which vertices the
     112@*            L[3] : a list whose ith entry explains to which vertices the
    104113                     ith vertex of the Newton polytope is connected
    105114                     -- i.e. L[3][i] is an integer vector and an entry k in
    106115                        there means that the vertex L[1][i] is connected to the
    107116                        vertex L[1][k]
    108 @*            L[4] : an integer matrix whose rows mulitplied by
     117@*            L[4] : an matrix of type bigintmat whose rows mulitplied by
    109118                     (1,var(1),...,var(nvar)) give a linear system of equations
    110119                     describing the affine hull of the polytope,
     
    118127         convention which starts indexing its vertices by zero while we start
    119128         with one !
    120 @*    -  the procedure creates the file  /tmp/polytope.polymake which contains
    121          the polytope in polymake format; if you wish to use this for further
    122          computations with polymake, you have to use the procedure
    123          polymakeKeepTmpFiles in before
    124 @*    -  moreover, the procedure creates the file /tmp/polytope.output which
    125          it deletes again before ending
    126 @*    -  it is possible to provide an optional second argument a string
    127          which then will be used instead of 'polytope' in the name of the
    128          polymake output file
    129129EXAMPLE: example polymakePolytope;   shows an example"
    130130{
    131   // the header for the file secendarypolytope.polymake
    132   string sp="_application polytope
    133 _version 2.2
    134 _type RationalPolytope
    135 
    136 POINTS
    137 ";
     131  // add a first column to polytope as homogenising coordinate
     132  points=intmatAddFirstColumn(points,"points");
     133  polytope polytop=polytopeViaPoints(points);
     134  list graph=vertexAdjacencyGraph(polytop)[2];
    138135  int i,j;
    139   // set the name for the polymake output file
    140   if (size(#)>0)
    141   {
    142     if (typeof(#[1])=="string")
    143     {
    144       string dateiname=#[1];
    145     }
    146     else
    147     {
    148       string dateiname="polytope";
    149     }
    150   }
    151   else
    152   {
    153     string dateiname="polytope";
    154   }
    155   // create the lattice point list for polymake
    156   sp=sp+intmatToPolymake(polytop,"points");
    157   // initialise dateiname.polymake and compute the vertices
    158   write(":w /tmp/"+dateiname+".polymake",sp);
    159   system("sh","cd /tmp; polymake "+dateiname+".polymake VERTICES > "+dateiname+".output");
    160   string vertices=read("/tmp/"+dateiname+".output");
    161   system("sh","/bin/rm /tmp/"+dateiname+".output");
    162   intmat np=polymakeToIntmat(vertices,"affine");
    163   // compute the dimension
    164   system("sh","cd /tmp; polymake "+dateiname+".polymake DIM > "+dateiname+".output");
    165   string pdim=read("/tmp/"+dateiname+".output");
    166   system("sh","/bin/rm /tmp/"+dateiname+".output");
    167   pdim=pdim[5,size(pdim)-6];
    168   execute("int nd="+pdim+";");
    169   // compute the vertex-edge graph
    170   system("sh","cd /tmp; polymake "+dateiname+".polymake GRAPH > "+dateiname+".output");
    171   string vertexedgegraph=read("/tmp/"+dateiname+".output");
    172   system("sh","/bin/rm /tmp/"+dateiname+".output");
    173   vertexedgegraph=vertexedgegraph[7,size(vertexedgegraph)-8];
    174   string newveg;
    175   for (i=1;i<=size(vertexedgegraph);i++)
    176   {
    177     if (vertexedgegraph[i]=="{")
    178     {
    179       newveg=newveg+"intvec(";
    180     }
    181     else
    182     {
    183       if (vertexedgegraph[i]=="}")
    184       {
    185         newveg=newveg+"),";
    186       }
    187       else
    188       {
    189         if (vertexedgegraph[i]==" ")
    190         {
    191           newveg=newveg+",";
    192         }
    193         else
    194         {
    195           newveg=newveg+vertexedgegraph[i];
    196         }
    197       }
    198     }
    199   }
    200   newveg=newveg[1,size(newveg)-1];
    201   execute("list nveg="+newveg+";");
    202   // raise each entry in nveg by one
    203   for (i=1;i<=size(nveg);i++)
    204   {
    205     for (j=1;j<=size(nveg[i]);j++)
    206     {
    207       nveg[i][j]=nveg[i][j]+1;
    208     }
    209   }
    210   // compute the affine hull
    211   system("sh","cd /tmp; polymake "+dateiname+".polymake AFFINE_HULL > "+dateiname+".output");
    212   string equations=read("/tmp/"+dateiname+".output");
    213   system("sh","/bin/rm /tmp/"+dateiname+".output");
    214   if (size(equations)>14)
    215   {
    216     intmat neq=polymakeToIntmat(equations,"cleardenom");
    217   }
    218   else
    219   {
    220     intmat neq[1][ncols(polytop)+1];
    221   }
    222   // delete the tmp-files, if polymakekeeptmpfiles is not set
    223   if (defined(polymakekeeptmpfiles)==0)
    224   {
    225     system("sh","/bin/rm /tmp/"+dateiname+".polymake");
    226   }
    227   // return the files
    228   return(list(np,nd,nveg,neq));
     136  for (i=1;i<=size(graph);i++)
     137  {
     138    for (j=1;j<=size(graph[i]);j++)
     139    {
     140      graph[i][j]=graph[i][j]+1;
     141    }
     142  }
     143  return(list(intmatcoldelete(vertices(polytop),1),dimension(polytop),graph,equations(polytop)));
    229144}
    230145example
     
    247162   ring r=0,x(1..4),dp;
    248163   matrix M[5][1]=1,x(1),x(2),x(3),x(4);
    249    np[4]*M;
     164   intmat(np[4])*M;
    250165}
    251166
    252167/////////////////////////////////////////////////////////////////////////////
    253168
    254 proc newtonPolytopeP (poly f,list #)
    255 "USAGE: newtonPolytopeP(f[,#]);  f poly, # string
    256 RETURN: list L with four entries
     169proc newtonPolytopeP (poly f)
     170"USAGE: newtonPolytopeP(f);  f poly
     171RETURN: list, L with four entries
    257172@*            L[1] : an integer matrix whose rows are the coordinates of vertices
    258173                     of the Newton polytope of f
     
    263178                        there means that the vertex L[1][i] is
    264179                        connected to the vertex L[1][k]
    265 @*            L[4] : an integer matrix whose rows mulitplied by
     180@*            L[4] : an matrix of type bigintmat whose rows mulitplied by
    266181                     (1,var(1),...,var(nvar)) give a linear system of equations
    267182                     describing the affine hull of the Newton polytope, i.e. the
     
    269184NOTE: -  if we replace the first column of L[4] by zeros, i.e. if we move
    270185         the affine hull to the origin, then we get the equations for the
    271          orthogonal comploment of the linearity space of the normal fan dual
     186         orthogonal complement of the linearity space of the normal fan dual
    272187         to the Newton polytope, i.e. we get the EQUATIONS that
    273188         we need as input for polymake when computing the normal fan
     
    275190         TU Berlin and Michael Joswig, so it only works if polymake is installed;
    276191         see http://www.math.tu-berlin.de/polymake/
    277 @*    -  the procedure creates the file  /tmp/newtonPolytope.polymake which
    278          contains the polytope in polymake format and which can be used for
    279          further computations with polymake
    280 @*    -  moreover, the procedure creates the file /tmp/newtonPolytope.output
    281          and deletes it again before ending
    282 @*    -  it is possible to give as an optional second argument a string
    283          which then will be used instead of 'newtonPolytope' in the name of
    284          the polymake output file
    285192EXAMPLE: example newtonPolytopeP;   shows an example"
    286193{
     
    296203    f=f-lead(f);
    297204  }
    298   if (size(#)==0)
    299   {
    300     #[1]="newtonPolytope";
    301   }
    302205  // call polymakePolytope with exponents
    303   return(polymakePolytope(exponents,#));
     206  return(polymakePolytope(exponents));
    304207}
    305208example
     
    330233   // the Newton polytope is contained in the affine space given
    331234   //     by the equations
    332    np[4]*M;
     235   intmat(np[4])*M;
    333236}
    334237
     
    339242RETURN: list, the exponent vectors of the monomials occuring in f,
    340243              i.e. the lattice points of the Newton polytope of f
    341 EXAMPLE: example normalFan;   shows an example"
     244EXAMPLE: example newtonPolytopeLP;   shows an example"
    342245{
    343246  list np;
     
    363266/////////////////////////////////////////////////////////////////////////////
    364267
    365 proc normalFan (intmat vertices,intmat affinehull,list graph,int er,list #)
    366 "USAGE:  normalFan (vert,aff,graph,rays,[,#]);   vert,aff intmat,  graph list, rays int, # string
     268proc normalFanL (def vertices, def affinehull,list graph,int er,list #)
     269"USAGE:  normalFanL (vert,aff,graph,rays,[,#]);   vert,aff intmat,  graph list, rays int, # string
    367270ASSUME:  - vert is an integer matrix whose rows are the coordinate of
    368271           the vertices of a convex lattice polytope;
     
    396299@*       - in the optional argument # it is possible to hand over other names
    397300           for the variables to be used -- be careful, the format must be correct
    398            which is not tested, e.g. if you want the variable names to be
    399            u00,u10,u01,u11 then you must hand over the string 'u11,u10,u01,u11'
    400 EXAMPLE: example normalFan;   shows an example"
    401 {
     301           and that is not tested, e.g. if you want the variable names to be
     302           u00,u10,u01,u11 then you must hand over the string u11,u10,u01,u11
     303EXAMPLE: example normalFanL;   shows an example"
     304{
     305  if (typeof(affinehull) != "intmat" && typeof (affinehull) != "bigintmat")
     306  {
     307    ERROR("normalFanL: input affinehull has to be either intmat or bigintmat");
     308    list L;
     309    return (L);
     310  }
    402311  list ineq; // stores the inequalities of the cones
    403312  int i,j,k;
     
    431340    list pp;  // contain strings representing the inequalities
    432341              // describing the normal cone
    433     intmat ie[size(graph[i])][ncols(vertices)]; // contains the inequalities
    434                                                 // as rows
     342    if (typeof (vertices) == "intmat")
     343    {
     344      intmat ie[size(graph[i])][ncols(vertices)]; // contains the inequalities
     345    }                                             // as rows
     346    if (typeof (vertices) == "bigintmat")
     347    {
     348      bigintmat ie[size(graph[i])][ncols(vertices)]; // contains the inequalities
     349    }                                                // as rows
    435350    // consider all the vertices to which the ith vertex in the
    436351    // polytope is connected by an edge
     
    469384  }
    470385  // remove the first column of affine hull to compute the linearity space
    471   intmat linearity=intmatcoldelete(affinehull,1);
     386  bigintmat linearity[1][ncols(vertices)];
     387  if (nrows(affinehull)>0)
     388  {
     389    linearity=intmatcoldelete(affinehull,1);
     390  }
    472391  //////////////////////////////////////////////////////////////////
    473392  // Compute next the extreme rays of the cones
     
    476395  {
    477396    list extremerays; // keeps the result
    478     string polymake; // keeps polymake output
    479     // the header for ineq.polymake
    480     string head="_application polytope
    481 _version 2.2
    482 _type RationalPolytope
    483 
    484 INEQUALITIES
    485 ";
    486     // the tail for both polymake files
    487     string tail="
    488 EQUATIONS
    489 ";
    490     tail=tail+intmatToPolymake(linearity,"rays");
    491     string ungleichungen; // keeps the inequalities for the polymake code
     397    cone kegel;
     398    bigintmat linearspan=intmatAddFirstColumn(linearity,"rays");
    492399    intmat M; // the matrix keeping the inequalities
    493     // create the file ineq.output
    494     write(":w /tmp/ineq.output","");
    495     int dimension; // keeps the dimension of the intersection the
    496                    // bad cones with the u11tobeseencone
    497400    for (i=1;i<=size(ineq);i++)
    498401    {
    499       i,". Cone of ",nrows(vertices); // indicate how many
    500                                       // vertices have been dealt with
    501       ungleichungen=intmatToPolymake(ineq[i][1],"rays");
    502       // write the inequalities to ineq.polymake and call polymake
    503       write(":w /tmp/ineq.polymake",head+ungleichungen+tail);
    504       ungleichungen=""; // clear ungleichungen
    505       system("sh","cd /tmp; /bin/rm ineq.output; polymake ineq.polymake VERTICES > ineq.output");
    506       // read the result of polymake
    507       polymake=read("/tmp/ineq.output");
    508       intmat VERT=polymakeToIntmat(polymake,"affine");
    509       extremerays[i]=VERT;
    510       kill VERT;
     402      kegel=coneViaInequalities(intmatAddFirstColumn(ineq[i][1],"rays"),linearspan);
     403      extremerays[i]=intmatcoldelete(rays(kegel),1);
    511404    }
    512405    for (i=1;i<=size(ineq);i++)
     
    514407      ineq[i]=ineq[i]+list(extremerays[i]);
    515408    }
    516   }
    517   // delete the tmp-files, if polymakekeeptmpfiles is not set
    518   if (defined(polymakekeeptmpfiles)==0)
    519   {
    520     system("sh","/bin/rm /tmp/ineq.polymake");
    521     system("sh","/bin/rm /tmp/ineq.output");
    522409  }
    523410  // get the linearity space
     
    534421   list np=newtonPolytopeP(f);
    535422   // the Groebner fan of f, i.e. the normal fan of the Newton polytope
    536    list gf=normalFan(np[1],np[4],np[3],1,"x,y,z");
     423   list gf=normalFanL(np[1],np[4],np[3],1,"x,y,z");
    537424   // the number of cones in the Groebner fan of f is:
    538425   size(gf[1]);
     
    549436/////////////////////////////////////////////////////////////////////////////
    550437
    551 proc groebnerFan (poly f,list #)
    552 "USAGE:  groebnerFan(f[,#]);  f poly, # string
     438proc groebnerFan (poly f)
     439"USAGE:  groebnerFan(f);  f poly
    553440RETURN:  list, the ith entry of L[1] contains information about the ith cone
    554441               in the Groebner fan dual to the ith vertex in the Newton
     
    564451                      in each cone
    565452@*             L[3] = the Newton polytope of f in the format of the procedure
    566                       newtonPolytope
    567 @*             L[4] = integer matrix where each row represents the exponet
     453                      newtonPolytopeP
     454@*             L[4] = integer matrix where each row represents the exponent
    568455                      vector of one monomial occuring in the input polynomial
    569456NOTE: - if you have already computed the Newton polytope of f then you might want
    570         to use the procedure normalFan instead in order to avoid doing costly
     457        to use the procedure normalFanL instead in order to avoid doing costly
    571458        computation twice
    572459@*    - the procedure calls for its computation polymake by Ewgenij Gawrilow,
    573460        TU Berlin and Michael Joswig, so it only works if polymake is installed;
    574461        see http://www.math.tu-berlin.de/polymake/
    575 @*    - the procedure creates the file  /tmp/newtonPolytope.polymake which
    576         contains the Newton polytope of f in polymake format and which can
    577         be used for further computations with polymake
    578 @*    - it is possible to give as an optional second argument as string which
    579         then will be used instead of 'newtonPolytope' in the name of the
    580         polymake output file
    581462EXAMPLE: example groebnerFan;   shows an example"
    582463{
     
    591472    f=f-lead(f);
    592473  }
    593   if (size(#)==0)
    594   {
    595     #[1]="newtonPolytope";
    596   }
    597474  // call polymakePolytope with exponents
    598   list newtonp=polymakePolytope(exponents,"newtonPolytope");
     475  list newtonp=polymakePolytope(exponents);
    599476  // get the variables as string
    600477  string variablen;
     
    604481  }
    605482  variablen=variablen[1,size(variablen)-1];
    606   // call normalFan in order to compute the Groebner fan
    607   list gf=normalFan(newtonp[1],newtonp[4],newtonp[3],1,variablen);
     483  // call normalFanL in order to compute the Groebner fan
     484  list gf=normalFanL(newtonp[1],newtonp[4],newtonp[3],1,variablen);
    608485  // append newtonp to gf
    609486  gf[3]=newtonp;
     
    642519
    643520
    644 /////////////////////////////////////////////////////////////////////////////
    645 
    646 proc intmatToPolymake (intmat M,string art)
    647 "USAGE:  intmatToPolymake(M,art);  M intmat, art string
    648 ASSUME:  - M is an integer matrix which should be transformed into polymake
    649            format;
    650 @*       - art is one of the following strings:
    651 @*           + 'rays'   : indicating that a first column of 0's should be added
    652 @*           + 'points' : indicating that a first column of 1's should be added
    653 RETURN:  string, the matrix is transformed in a string and a first column has
    654                  been added
    655 EXAMPLE: example intmatToPolymake;   shows an example"
    656 {
    657   if (art=="rays")
    658   {
    659     string anf="0 ";
    660   }
    661   else
    662   {
    663     string anf="1 ";
    664   }
    665   string sp;
    666   int i,j;
    667   // create the lattice point list for polymake
    668   for (i=1;i<=nrows(M);i++)
    669   {
    670     sp=sp+anf;
    671     for (j=1;j<=ncols(M);j++)
    672     {
    673       sp=sp+string(M[i,j])+" ";
    674       if (j==ncols(M))
    675       {
    676         sp=sp+"
    677 ";
    678       }
    679     }
    680   }
    681   return(sp);
    682 }
    683 example
    684 {
    685    "EXAMPLE:";
    686    echo=2;
    687    intmat M[3][4]=1,2,3,4,5,6,7,8,9,10,11,12;
    688    intmatToPolymake(M,"rays");
    689    intmatToPolymake(M,"points");
    690 }
    691 
    692 /////////////////////////////////////////////////////////////////////////////
    693 
    694 proc polymakeToIntmat (string pm,string art)
    695 "USAGE:  polymakeToIntmat(pm,art);  pm, art string
    696 ASSUME:  pm is the result of calling polymake with one 'argument' like
    697          VERTICES, AFFINE_HULL, etc., so that the first row of the string is
    698          the name of the corresponding 'argument' and the further rows contain
    699          the result which consists of vectors either over the integers
    700          or over the rationals
    701 RETURN:  intmat, the rows of the matrix are basically the vectors in pm, starting
    702                  from the second row, where each row has been multiplied with the
    703                  lowest common multiple of the denominators of its entries as if
    704                  it is an integer matrix; moreover, if art=='affine', then
    705                  the first column is omitted since we only want affine
    706                  coordinates
    707 EXAMPLE: example polymakeToIntmat;   shows an example"
    708 {
    709   // we need a line break
    710   string zeilenumbruch="
    711 ";
    712   // remove the 'argment' name, i.e. the first row of pm
    713   while (pm[1]!=zeilenumbruch)
    714   {
    715     pm=stringdelete(pm,1);
    716   }
    717   pm=stringdelete(pm,1);
    718   // find out how many entries each vector has, namely one more
    719   // than 'spaces' in a row
    720   int i=1;
    721   int s=1;
    722   int z=1;
    723   while (pm[i]!=zeilenumbruch)
    724   {
    725     if (pm[i]==" ")
    726     {
    727       s++;
    728     }
    729     i++;
    730   }
    731   // if we want to have affine coordinates
    732   if (art=="affine")
    733   {
    734     s--; // then there is one column less
    735     // and the entry of the first column (in the first row) has to be removed
    736     while (pm[1]!=" ")
    737     {
    738       pm=stringdelete(pm,1);
    739     }
    740     pm=stringdelete(pm,1);
    741   }
    742   // we add two line breaks at the end in order to have this as
    743   // a stopping criterion
    744   pm=pm+zeilenumbruch+zeilenumbruch;
    745   // we now have to work through each row
    746   for (i=1;i<=size(pm);i++)
    747   {
    748     // if there are two consecutive line breaks we are done
    749     if ((pm[i]==zeilenumbruch) and (pm[i+1]==zeilenumbruch))
    750     {
    751       i=size(pm)+1;
    752     }
    753     else
    754     {
    755       // a line break has to be replaced by a comma
    756       if (pm[i]==zeilenumbruch)
    757       {
    758         z++;
    759         pm[i]=",";
    760         // if we want to have affine coordinates,
    761         // then we have to delete the first entry in each row
    762         if (art=="affine")
    763         {
    764          while (pm[i+1]!=" ")
    765          {
    766            pm=stringdelete(pm,i+1);
    767          }
    768          pm=stringdelete(pm,i+1);
    769         }
    770       }
    771       // a space has to be replaced by a comma
    772       if (pm[i]==" ")
    773       {
    774         pm[i]=",";
    775       }
    776     }
    777   }
    778   // if we have introduced superflous commata at the end, they should be removed
    779   while (pm[size(pm)]==",")
    780   {
    781     pm=stringdelete(pm,size(pm));
    782   }
    783   // since the matrix could be over the rationals,
    784   // we need a ring with rational coefficients
    785   ring zwischering=0,x,lp;
    786   // create the matrix with the elements of pm as entries
    787   execute("matrix mm["+string(z)+"]["+string(s)+"]="+pm+";");
    788   // transform this into an integer matrix
    789   matrix M[1][ncols(mm)]; // takes a row of mm
    790   int cm;  // takes a lowest common multiple
    791   // multiply each row by an integer such that its entries are integers
    792   for (int j=1;j<=nrows(mm);j++)
    793   {
    794     M=mm[j,1..ncols(mm)];
    795     cm=commondenominator(M);
    796     for (i=1;i<=ncols(mm);i++)
    797     {
    798       mm[j,i]=cm*mm[j,i];
    799     }
    800   }
    801   // transform the matrix mm into an integer matrix
    802   execute("intmat im["+string(z)+"]["+string(s)+"]="+string(mm)+";");
    803   return(im);
    804 }
    805 example
    806 {
    807    "EXAMPLE:";
    808    echo=2;
    809    // this is the usual output of some polymake computation
    810    string pm="VERTICES
    811 0 1 3 5/3 1/3 -1 -23/3 -1/3 5/3 1/3 1
    812 0 1 3 -23/3 5/3 1 5/3 1/3 1/3 -1/3 -1
    813 0 1 1 1/3 -1/3 -1 5/3 1/3 -23/3 5/3 3
    814 0 1 1 5/3 -23/3 3 1/3 5/3 -1/3 1/3 -1
    815 0 1 -1 1/3 5/3 3 -1/3 -23/3 1/3 5/3 1
    816 0 1 -1 -1/3 1/3 1 1/3 5/3 5/3 -23/3 3
    817 0 1 -1 1 3 -5 -1 3 -1 1 -1
    818 0 1 -1 -1 -1 -1 1 1 3 3 -5
    819 0 1 -5 3 1 -1 3 -1 1 -1 -1
    820 
    821 ";
    822    intmat PM=polymakeToIntmat(pm,"affine");
    823    // note that the first column has been removed, since we asked for
    824    // affine coordinates, and the denominators have been cleared
    825    print(PM);
    826 }
    827 
    828521///////////////////////////////////////////////////////////////////////////////
    829522/// PROCEDURES USING TOPCOM
    830523///////////////////////////////////////////////////////////////////////////////
    831524
    832 proc triangulations (list polygon)
    833 "USAGE:  triangulations(polygon); list polygon
     525proc triangulations (list polygon,list #)
     526"USAGE:  triangulations(polygon[,#]); list polygon, list #
    834527ASSUME:  polygon is a list of integer vectors of the same size representing
    835528         the affine coordinates of the lattice points
     
    846539       this  procedure; see
    847540@*     http://www.uni-bayreuth.de/departments/wirtschaftsmathematik/rambau/TOPCOM
     541@*   - if you only want to have the regular triangulations the procedure should
     542       be called with the string 'regular' as optional argument
    848543@*   - the procedure creates the files /tmp/triangulationsinput and
    849544       /tmp/triangulationsoutput;
     
    851546       output containing the triangulations of corresponding to points in the
    852547       format of points2triangs; if you wish to use this for further
    853        computations with topcom, you have to use the procedure
    854        polymakeKeepTmpFiles in before
     548       computations with topcom, you have to call the procedure with the
     549       string 'keepfiles' as optional argument
    855550@*   - note that an integer i in an integer vector representing a triangle
    856551       refers to the ith lattice point, i.e. polygon[i]; this convention is
     
    860555{
    861556  int i,j;
     557  // check for optional arguments
     558  int regular,keepfiles;
     559  if (size(#)>0)
     560  {
     561    for (i=1;i<=size(#);i++)
     562    {
     563      if (typeof(#[i])=="string")
     564      {
     565        if (#[i]=="keepfiles")
     566        {
     567          keepfiles=1;
     568        }
     569        if (#[i]=="regular")
     570        {
     571          regular=1;
     572        }
     573      }
     574    }
     575  }
    862576  // prepare the input for points2triangs by writing the input polygon in the
    863577  // necessary format
     
    875589  write(":w /tmp/triangulationsinput",spi);
    876590  // call points2triangs
    877   system("sh","cd /tmp; points2triangs < triangulationsinput > triangulationsoutput");
     591  if (regular==1) // compute only regular triangulations
     592  {
     593    system("sh","cd /tmp; points2triangs --regular < triangulationsinput > triangulationsoutput");
     594  }
     595  else // compute all triangulations
     596  {
     597    system("sh","cd /tmp; points2triangs < triangulationsinput > triangulationsoutput");
     598  }
    878599  string p2t=read("/tmp/triangulationsoutput"); // takes result of points2triangs
    879   // delete the tmp-files, if polymakekeeptmpfiles is not set
    880   if (defined(polymakekeeptmpfiles)==0)
     600  // delete the tmp-files, if no second argument is given
     601  if (keepfiles==0)
    881602  {
    882603    system("sh","cd /tmp; rm -f triangulationsinput; rm -f triangulationsoutput");
     
    953674            else
    954675            {
    955               np2t=np2t+p2t[i];
     676              if (p2t[i]=="[")
     677              {
     678                // in Topcom version 17.4 (and maybe also in earlier versions) the list
     679                // of triangulations is indexed starting with index 0, in Singular
     680                // we have to start with index 1
     681                np2t=np2t+p2t[i]+"1+";
     682              }
     683              else
     684              {
     685                np2t=np2t+p2t[i];
     686              }
    956687            }
    957688          }
     
    962693  list T;
    963694  execute(np2t);
     695  // depending on the version of Topcom, the list T has or has not an entry T[1]
     696  // if it has none, the entry should be removed
     697  while (typeof(T[1])=="none")
     698  {
     699    T=delete(T,1);
     700  }
    964701  // raise each index by one
    965702  for (i=1;i<=size(T);i++)
     
    1000737RETURN:  list, say L, such that:
    1001738@*             L[1] = intmat, each row gives the affine coordinates of a lattice
    1002                       point in the secondary polytope given by the marked polytope
    1003                       corresponding to polygon
     739                      point in the secondary polytope given by the marked
     740                      polytope corresponding to polygon
    1004741@*             L[2] = the list of corresponding triangulations
    1005742NOTE: if the triangluations are not handed over as optional argument the
     
    1114851       see http://www.math.tu-berlin.de/polymake/
    1115852@*   - in the optional argument # it is possible to hand over other names for
    1116        the variables to be used -- be careful, the format must be correct
    1117        which is not tested, e.g. if you want the variable names to be
     853       the variables to be used -- be careful, the format must be correct and
     854       that is not tested, e.g. if you want the variable names to be
    1118855       u00,u10,u01,u11 then you must hand over the string 'u11,u10,u01,u11'
    1119856@*   - if the triangluations are not handed over as optional argument the
     
    1135872  list sp=secondaryPolytope(polygon,triang);
    1136873  list spp=polymakePolytope(sp[1]);
    1137   list sf=normalFan(spp[1],spp[4],spp[3],1);
     874  list sf=normalFanL(spp[1],spp[4],spp[3],1);
    1138875  return(list(sf[1],sf[2],spp,triang));
    1139876}
     
    13781115@*       triang is a list of integer vectors all of size three describing a
    13791116         triangulation of the polygon described by polygon; if an entry of
    1380          triang is the vector (i,j,k) then the triangle is built from the vertices
     1117         triang is the vector (i,j,k) then the triangle is built by the vertices
    13811118         with indices i, j and k
    13821119RETURN:  intvec, the integer vector eta describing that vertex of the Newton
     
    19481685  // points and
    19491686  // the number b of boundary points are connected by the formula: A=b+2g-2
    1950   return(list(area,bdpts,(area-bdpts+2)/2));
     1687  return(list(area,bdpts,(area-bdpts+2) div 2));
    19511688}
    19521689example
     
    19731710"USAGE:  ellipticNF(polygon);   polygon list
    19741711ASSUME:  polygon is a list of integer vectors in the plane such that their
    1975          convex hull C has precisely one interior lattice point, i.e. C is the
     1712         convex hull C has precisely one interior lattice point; i.e. C is the
    19761713         Newton polygon of an elliptic curve
    19771714PURPOSE: compute the normal form of the polygon with respect to the unimodular
     
    19911728  int i;            // index
    19921729  intvec edge;      // stores the vector of an edge
    1993   intvec boundary;  // stores lattice lengths of the edges of the Newton cylce
     1730  intvec boundary;  // stores lattice lengths of the edges of the Newton cycle
    19941731  // find the vertices of the Newton cycle and order it clockwise
    19951732  list pg=findOrientedBoundary(polygon)[2];
     
    23022039
    23032040/////////////////////////////////////////////////////////////////////////////////
    2304 /// AUXILARY PROCEDURES
    2305 /////////////////////////////////////////////////////////////////////////////////
    2306 
    2307 proc polymakeKeepTmpFiles (int i)
    2308 "USAGE:  polymakeKeepTmpFiles(int i);   i int
    2309 PURPOSE: some procedures create files in the directory /tmp which are used for
    2310          computations with polymake respectively topcom; these will be removed
    2311          when the corresponding procedure is left; however, it might be
    2312          desireable to keep them for further computations with either polymake or
    2313          topcom; this can be achieved by this procedure; call the procedure as:
    2314 @*       - polymakeKeepTmpFiles(1); - then the files will be kept
    2315 @*       - polymakeKeepTmpFiles(0); - then files will be removed in the future
    2316 RETURN:  none"
    2317 {
    2318   if ((i==1) and (defined(polymakekeeptmpfiles)==0))
    2319   {
    2320     int polymakekeeptmpfiles;
    2321     export(polymakekeeptmpfiles);
    2322   }
    2323   if (i!=1)
    2324   {
    2325     if (defined(polymakekeeptmpfiles))
    2326     {
    2327       kill polymakekeeptmpfiles;
    2328     }
    2329   }
    2330 }
    2331 
    2332 
    2333 /////////////////////////////////////////////////////////////////////////////////
    23342041/////////////////////////////////////////////////////////////////////////////////
    23352042/// AUXILARY PROCEDURES, WHICH ARE DECLARED STATIC
     
    23642071}
    23652072
    2366 static proc intmatcoldelete (intmat w,int i)
     2073static proc intmatcoldelete (def w,int i)
    23672074"USAGE:      intmatcoldelete(w,i); w intmat, i int
    23682075RETURN:      intmat, the integer matrix w with the ith comlumn deleted
    2369 NOTE:        the procedure is called by intmatsort and normalFan"
    2370 {
    2371   if ((i<1) or (i>ncols(w)) or (ncols(w)==1))
    2372   {
    2373     return(w);
    2374   }
    2375   if (i==1)
    2376   {
    2377     intmat M[nrows(w)][ncols(w)-1]=w[1..nrows(w),2..ncols(w)];
    2378     return(M);
    2379   }
    2380   if (i==ncols(w))
    2381   {
    2382     intmat M[nrows(w)][ncols(w)-1]=w[1..nrows(w),1..ncols(w)-1];
    2383     return(M);
    2384   }
    2385   else
    2386   {
    2387     intmat M[nrows(w)][i-1]=w[1..nrows(w),1..i-1];
    2388     intmat N[nrows(w)][ncols(w)-i]=w[1..nrows(w),i+1..ncols(w)];
    2389     return(intmatconcat(M,N));
     2076NOTE:        the procedure is called by intmatsort and normalFanL"
     2077{
     2078  if (typeof(w)=="intmat")
     2079  {
     2080    if ((i<1) or (i>ncols(w)) or (ncols(w)==1))
     2081    {
     2082      return(w);
     2083    }
     2084    if (i==1)
     2085    {
     2086      intmat M[nrows(w)][ncols(w)-1]=w[1..nrows(w),2..ncols(w)];
     2087      return(M);
     2088    }
     2089    if (i==ncols(w))
     2090    {
     2091      intmat M[nrows(w)][ncols(w)-1]=w[1..nrows(w),1..ncols(w)-1];
     2092      return(M);
     2093    }
     2094    else
     2095    {
     2096      intmat M[nrows(w)][i-1]=w[1..nrows(w),1..i-1];
     2097      intmat N[nrows(w)][ncols(w)-i]=w[1..nrows(w),i+1..ncols(w)];
     2098      return(intmatconcat(M,N));
     2099    }
     2100  }
     2101  if (typeof(w)=="bigintmat")
     2102  {
     2103    if ((i<1) or (i>ncols(w)) or (ncols(w)==1))
     2104    {
     2105      return(w);
     2106    }
     2107    if (i==1)
     2108    {
     2109      bigintmat M[nrows(w)][ncols(w)-1]=w[1..nrows(w),2..ncols(w)];
     2110      return(M);
     2111    }
     2112    if (i==ncols(w))
     2113    {
     2114      bigintmat M[nrows(w)][ncols(w)-1]=w[1..nrows(w),1..ncols(w)-1];
     2115      return(M);
     2116    }
     2117    else
     2118    {
     2119      bigintmat MN[nrows(w)][ncols(w)-1];
     2120      MN[1..nrows(w),1..i-1]=w[1..nrows(w),1..i-1];
     2121      MN[1..nrows(w),i..ncols(w)-1]=w[1..nrows(w),i+1..ncols(w)];
     2122      return(MN);
     2123    }
     2124  } else
     2125  {
     2126    ERROR("intmatcoldelete: input matrix has to be of type intmat or bigintmat");
     2127    intmat M; return(M);
    23902128  }
    23912129}
     
    26832421  return(list(coord,coords,latex));
    26842422}
     2423
     2424static proc intmatAddFirstColumn (def M,string art)
     2425"USAGE:  intmatAddFirstColumn(M,art);  M intmat, art string
     2426ASSUME:  - M is an integer matrix where a first column of 0's or 1's should be added
     2427@*       - art is one of the following strings:
     2428@*         + 'rays'   : indicating that a first column of 0's should be added
     2429@*         + 'points' : indicating that a first column of 1's should be added
     2430RETURN:  intmat, a first column has been added to the matrix"
     2431{
     2432  if (typeof (M) == "intmat")
     2433  {
     2434    intmat N[nrows(M)][ncols(M)+1];
     2435    int i,j;
     2436    for (i=1;i<=nrows(M);i++)
     2437    {
     2438      if (art=="rays")
     2439      {
     2440        N[i,1]=0;
     2441      }
     2442      else
     2443      {
     2444        N[i,1]=1;
     2445      }
     2446      for (j=1;j<=ncols(M);j++)
     2447      {
     2448        N[i,j+1]=M[i,j];
     2449      }
     2450    }
     2451    return(N);
     2452  }
     2453  if (typeof (M) == "bigintmat")
     2454  {
     2455    bigintmat N[nrows(M)][ncols(M)+1];
     2456    int i,j;
     2457    for (i=1;i<=nrows(M);i++)
     2458    {
     2459      if (art=="rays")
     2460      {
     2461        N[i,1]=0;
     2462      }
     2463      else
     2464      {
     2465        N[i,1]=1;
     2466      }
     2467      for (j=1;j<=ncols(M);j++)
     2468      {
     2469        N[i,j+1]=M[i,j];
     2470      }
     2471    }
     2472    return(N);
     2473  }
     2474  else
     2475  {
     2476    ERROR ("intmatAddFirstColumn: input matrix has to be either intmat or bigintmat");
     2477    intmat N;
     2478    return (N);
     2479  }
     2480}
     2481
     2482
     2483
Note: See TracChangeset for help on using the changeset viewer.