File: convert.gi

package info (click to toggle)
gap-polymaking 0.8.8-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 816 kB
  • sloc: xml: 682; javascript: 155; makefile: 105; perl: 24; sh: 2
file content (420 lines) | stat: -rw-r--r-- 13,127 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#############################################################################
##
#W convert.gi            polymaking Package      Marc Roeder
##
##  

##
##
#Y   Copyright (C) 2006 Marc Roeder 
#Y 
#Y This program is free software; you can redistribute it and/or 
#Y modify it under the terms of the GNU General Public License 
#Y as published by the Free Software Foundation; either version 2 
#Y of the License, or (at your option) any later version. 
#Y 
#Y This program is distributed in the hope that it will be useful, 
#Y but WITHOUT ANY WARRANTY; without even the implied warranty of 
#Y MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
#Y GNU General Public License for more details. 
#Y 
#Y You should have received a copy of the GNU General Public License 
#Y along with this program; if not, write to the Free Software 
#Y Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
##
InstallMethod(ConvertPolymakeOutputToGapNotation,[IsString],
        function(string)
    local   split,  blockpositions,  splitblocks,  returnlist,  
            splitblock,  name,  rest,  object;
    
    
    splitblocks:=SplitPolymakeOutputStringIntoBlocks(string);
    returnlist:=[];
    for splitblock in splitblocks
      do
        name:=NormalizedWhitespace(splitblock[1]);
        if Size(splitblock) < 2
           then
            Info(InfoPolymaking,2,"No data for ", name,". Record not updated for this block");
            UpdatePolymakeFailReason(Concatenation("polymake returned ",name," empty"));
            Add(returnlist, rec(name:=name,object:=fail));
            continue;
        fi;
        rest:=splitblock{[2..Maximum(Size(splitblock),2)]};
        if rest[Size(rest)]=""
           then
            Unbind(rest[Size(rest)]);
        fi;
#        Apply(rest,NormalizedWhitespace);
        
        if rest=["==UNDEF=="]
           then
            Info(InfoPolymaking,1,Concatenation("polymake declares object ",name," undefined"));
            UpdatePolymakeFailReason(Concatenation("polymake declares ",name," undefined"));
            Add(returnlist,rec(name:=name,object:=fail));
        elif IsBound(ObjectConverters.(name))
           then
            object:=ObjectConverters.(name)(rest);
            if object<>fail 
               then
                Add(returnlist,rec(name:=name,object:=object));
            else
                Info(InfoPolymaking,1,"Conversion failed. Maybe invalid output from polymake. Record not updated for this block");
                Add(returnlist, rec(name:=name,object:=fail));
            fi; 
        else
            Info(InfoPolymaking,1,"No Method to convert ", name,". Record not updated for this block");
            UpdatePolymakeFailReason(Concatenation("polymaking doesn't know how to convert ",name," to GAP"));
            Add(returnlist, rec(name:=name,object:=fail));
        fi;
    od;
    return returnlist; 
end);


#
#First the method to split polymake's output into different blocks by
# recognizing lines which may be keyword-lines
###############

InstallMethod(SplitPolymakeOutputStringIntoBlocks,[IsString],
        function(string)
    local   blocks,  newblock,  lines,  line;
    
    blocks:=[];
    newblock:=[];
    lines:=SplitString(string,"\n");
    while lines<>[]
      do
        line:=Remove(lines,1);
        if not IsEmptyString(line) 
           then
            if IsUpperAlphaChar(line[1]) 
               and ForAll(line,c->IsUpperAlphaChar(c) or c in ['_','-','>'] or IsDigitChar(c))
               then
                if blocks=[] and newblock=[]
                   then
                    newblock:=[MapKeyWordFromPolymakeFormat(line)];
                else
                    Add(blocks,ShallowCopy(newblock));
                    newblock:=[MapKeyWordFromPolymakeFormat(line)];
                fi;
            else
                Add(newblock,line);
            fi;
        fi;
    od; 
    if newblock<>[]
       then
        Add(blocks,newblock);
    fi;
    return blocks;
end);


#############################################################################
##
## update the fail reason in case of syntax errors:
##
InstallMethod(ConverterSyntaxError,[IsString],
        function(methname)
    UpdatePolymakeFailReason(
            Concatenation("input for converter method ",methname, " syntactically incorrect (cast an error and went into break loop)")
            );
    Error("incorrect input");
end);


#############################################################################
# Now the different conversion methods:
#############################################################################
##
## Basics, numbers and bools:
##
InstallMethod(ConvertPolymakeNumber,[IsString],
        function(string)
    local   sstring,  denom,  enum;
    if '.' in string
       then
        Info(InfoPolymaking,1,"Warning!converting a floating point number"); 
        sstring:=SplitString(string,".");
        denom:=10^(Length(sstring[2]));
        enum:=Int(Concatenation(sstring));
        return enum/denom;
    elif string=""
      then
        ConverterSyntaxError("ConvertPolymakeNumber");
        return fail;
    else
        return Rat(string);
    fi;
end);


InstallMethod(ConvertPolymakeScalarToGAP,[IsDenseList],
        function(stringlist)
    if Size(stringlist)<>1
       then
        ConverterSyntaxError("ConvertPolymakeScalarToGAP");
        return fail;    
    else
        return ConvertPolymakeNumber(stringlist[1]);
    fi;
end);


InstallMethod(ConvertPolymakeBoolToGAP,[IsDenseList],
        function(stringlist)
    if Size(stringlist)<>1
       then
        ConverterSyntaxError("ConvertPolymakeBoolToGAP");
        return fail;
    elif stringlist[1]="1" or stringlist[1]="true"
      then 
        return true;
    elif stringlist[1]="0" or stringlist[1]="false"
      then
        return false;
    else
        UpdatePolymakeFailReason("Boolean conversion failed, the returned value wasn't 0, 1, true, or false. Error cast");
        Error("Conversion failed");
    fi;
end);

InstallMethod(ConvertPolymakeDescriptionToGAP,[IsDenseList],
        function(stringlist)
    return JoinStringsWithSeparator(stringlist," ");
end);

#############################################################################
##
## MATRICES AND LISTS OF SETS:
##
## Unfortunately some of the keywords that return matrices
## might also return lists of sets (this is a polytope/topaz problem).
## So we try to guess the type if necessary.
##
InstallMethod(ConvertPolymakeMatrixOrListOfSetsToGAP,[IsDenseList],
        function(stringlist)
    
    if ForAll(stringlist,i->i[1]='{')
      then
        return ConvertPolymakeListOfSetsToGAP(stringlist);     
    elif ForAll(stringlist,i->not ('{' in i or '}' in i))
      then
            return ConvertPolymakeMatrixToGAP(stringlist);
    else
        UpdatePolymakeFailReason("ConvertPolymakeMatrixOrListOfSetsToGAP couldn't decide what to do. Error cast");
        Error("don't know if this is a matrix or a list of sets ");
        return fail;
    fi;
end);


InstallMethod(ConvertPolymakeMatrixOrListOfSetsToGAPPlusOne,[IsDenseList],
        function(stringlist)
    
    if ForAll(stringlist,i->i[1]='{')
      then
        return ConvertPolymakeListOfSetsToGAPPlusOne(stringlist);     
    elif ForAll(stringlist,i->not ('{' in i or '}' in i))
      then
            return ConvertPolymakeMatrixToGAP(stringlist);
    else
        UpdatePolymakeFailReason("ConvertPolymakeMatrixOrListOfSetsToGAP couldn't decide what to do. Error cast");
        Error("don't know if this is a matrix or a list of sets ");
        return fail;
    fi;
end);

                
InstallMethod(ConvertPolymakeMatrixToGAP,[IsDenseList],
        function(stringlist)
    local returnmatrix, string, vector;
    returnmatrix:=[];
    for string in stringlist
      do
        vector:=SplitString(string," ");
        Apply(vector,ConvertPolymakeNumber);
        Add(returnmatrix,vector);
    od;
    return returnmatrix;
end);


InstallMethod(ConvertPolymakeMatrixToGAPKillOnes,[IsDenseList],
        function(stringlist)
    local   returnmatrix;
    returnmatrix:=ConvertPolymakeMatrixToGAP(stringlist);
    if not IsMatrix(returnmatrix) 
       or Size(returnmatrix[1])<2
       or not ForAll(returnmatrix,i->i[1]=1)
       then
        Error("returnmatrix is not a matrix or too small");
        UpdatePolymakeFailReason("Error in ConvertPolymakeMatrixToGAPKillOnes. The returned object is either not a matrix, has fewer than 2 columns or doesn't have all ones in the first column. (Error cast)");
        return fail;
    fi;
    if returnmatrix<>fail
       then
        return List(returnmatrix,i->i{[2..Size(i)]});    #kill leading ones
    else
        return fail;
    fi;
end);

#############################################################################
##
## Vectors
##
InstallMethod(ConvertPolymakeVectorToGAP,[IsDenseList],
        function(stringlist)
    local   vector;
    if Size(stringlist)<>1
       then
        ConverterSyntaxError("ConvertPolymakeVectorToGAP");
        return fail;
    else
        vector:=SplitString(stringlist[1]," ");
        Apply(vector,ConvertPolymakeNumber);
        return vector;
    fi;
end);
    

InstallMethod(ConvertPolymakeVectorToGAPKillOne,[IsDenseList],
        function(stringlist)
    local   vector;
    vector:=ConvertPolymakeVectorToGAP(stringlist);
    if not IsVector(vector) or Size(vector)<2
       or vector[1]<>1
       then
        Error("vector is not a vector or too small");
        UpdatePolymakeFailReason("Error in ConvertPolymakeVectorToGAPKillOne. The returned object is either not a vector or has fewer than 2 columns or doesn't start with 1. (Error cast)");
        return fail;
    fi;
    return vector{[2..Size(vector)]};
end);


InstallMethod(ConvertPolymakeIntVectorToGAPPlusOne,[IsDenseList],
        function(stringlist)
    local   vec;
    
    vec:=ConvertPolymakeVectorToGAP(stringlist);
    if not ForAll(vec,IsInt)
       then
        Error("Vector is not a vector of integers as expected");
        UpdatePolymakeFailReason("Error in ConvertPolymakeIntVectorToGAPPlusOne. The returned vector did not consist of integers. Error cast");
        return fail;
    else
        return vec+1;
    fi;
end);
        



#############################################################################
##
## Sets, Sets of Sets, Lists of Sets
##
InstallMethod(ConvertPolymakeSetToGAP,[IsDenseList],
        function(stringlist)
    local   entries;
    if not Size(stringlist)=1 and IsString(stringlist[1])
       then
        ConverterSyntaxError("ConvertPolymakeSetToGAP");
        return fail;
    else
        entries:=ReplacedString(ReplacedString(stringlist[1],"{",""),"}","");
        entries:=Set(SplitString(entries," "));
        RemoveSet(entries,"");
        return Set(entries,ConvertPolymakeNumber);
    fi;
end);


InstallMethod(ConvertPolymakeSetOfSetsToGAP,[IsDenseList],
        function(stringlist)    
    local   returnlist,  line,  newlist,  entry;
        
    returnlist:=[];
    if Size(stringlist)<> 1 or not IsString(stringlist[1])
       then
        ConverterSyntaxError("ConvertPolymakeSetOfSetsToGAP");
        return fail;
    fi;
    line:=stringlist[1];
    newlist:=ReplacedString(line{[2..Size(line)-1]},"} {","},{");
    newlist:=SplitString(newlist,",");
    for entry in newlist
      do
        Add(returnlist,ConvertPolymakeSetToGAP([entry]));
    od;
    return returnlist;
end);


InstallMethod(ConvertPolymakeListOfSetsToGAP,[IsDenseList],
        function(stringlist)
    return List(stringlist,s->ConvertPolymakeSetToGAP([s]));
end);


InstallMethod(ConvertPolymakeListOfSetsToGAPPlusOne,[IsDenseList],
        function(stringlist)
    return List(stringlist,s->ConvertPolymakeSetToGAP([s])+1);
end);


#############################################################################
##
## More complex stuff:
##

#############################################################################
##
## Graphs:
##
InstallMethod(ConvertPolymakeGraphToGAP,[IsDenseList],
        function(stringlist)
    local   edgelist,  edge,  vertices;
    edgelist:=[];
    for edge in stringlist
      do
#        verts:=ReplacedString(ReplacedString(edge,"{",""),"}","");
#        verts:=SplitString(verts," ");
        Add(edgelist,ConvertPolymakeSetToGAP(edge));
    od;
    vertices:=Set(Flat(edgelist));
    return rec(vertices:=vertices,edges:=Set(edgelist));
end);
    


#############################################################################
##
## Face Lattices:
##
InstallMethod(ConvertPolymakeFaceLatticeToGAP,[IsDenseList],
        function(stringlist)
    local   returnlist,  line,  faces;
    returnlist:=[];
    for line in stringlist
      do 
        if line<>"" and line[1]='{'
           then
            faces:=Set(ConvertPolymakeSetOfSetsToGAP([line]));
            Apply(faces,i->i+1);
            Add(returnlist,faces);
        fi;
    od;
    return returnlist;
end);



#############################################################################
##
#E  END of file convert.gi
##