1 | /* |
---|
2 | * gfanlib_polyhedralfan.cpp |
---|
3 | * |
---|
4 | * Created on: Nov 16, 2010 |
---|
5 | * Author: anders |
---|
6 | */ |
---|
7 | |
---|
8 | #include <sstream> |
---|
9 | #include "gfanlib_polyhedralfan.h" |
---|
10 | #include "gfanlib_polymakefile.h" |
---|
11 | |
---|
12 | using namespace std; |
---|
13 | namespace gfan{ |
---|
14 | |
---|
15 | PolyhedralFan::PolyhedralFan(int ambientDimension): |
---|
16 | n(ambientDimension), |
---|
17 | symmetries(n) |
---|
18 | { |
---|
19 | } |
---|
20 | |
---|
21 | PolyhedralFan::PolyhedralFan(SymmetryGroup const &sym): |
---|
22 | n(sym.sizeOfBaseSet()), |
---|
23 | symmetries(sym) |
---|
24 | { |
---|
25 | |
---|
26 | } |
---|
27 | |
---|
28 | PolyhedralFan PolyhedralFan::fullSpace(int n) |
---|
29 | { |
---|
30 | PolyhedralFan ret(n); |
---|
31 | |
---|
32 | ZCone temp(n); |
---|
33 | temp.canonicalize(); |
---|
34 | ret.cones.insert(temp); |
---|
35 | |
---|
36 | return ret; |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | PolyhedralFan PolyhedralFan::facetsOfCone(ZCone const &c) |
---|
41 | { |
---|
42 | ZCone C(c); |
---|
43 | C.canonicalize(); |
---|
44 | PolyhedralFan ret(C.ambientDimension()); |
---|
45 | |
---|
46 | ZMatrix halfSpaces=C.getFacets(); |
---|
47 | |
---|
48 | for(int i=0;i<halfSpaces.getHeight();i++) |
---|
49 | { |
---|
50 | ZMatrix a(0,C.ambientDimension()); |
---|
51 | ZMatrix b(0,C.ambientDimension()); |
---|
52 | b.appendRow(halfSpaces[i]); |
---|
53 | ZCone N=intersection(ZCone(a,b),c); |
---|
54 | N.canonicalize(); |
---|
55 | ret.cones.insert(N); |
---|
56 | } |
---|
57 | return ret; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | int PolyhedralFan::getAmbientDimension()const |
---|
62 | { |
---|
63 | return n; |
---|
64 | } |
---|
65 | |
---|
66 | bool PolyhedralFan::isEmpty()const |
---|
67 | { |
---|
68 | return cones.empty(); |
---|
69 | } |
---|
70 | |
---|
71 | int PolyhedralFan::getMaxDimension()const |
---|
72 | { |
---|
73 | assert(!cones.empty()); |
---|
74 | |
---|
75 | return cones.begin()->dimension(); |
---|
76 | } |
---|
77 | |
---|
78 | int PolyhedralFan::getMinDimension()const |
---|
79 | { |
---|
80 | assert(!cones.empty()); |
---|
81 | |
---|
82 | return cones.rbegin()->dimension(); |
---|
83 | } |
---|
84 | |
---|
85 | /* |
---|
86 | PolyhedralFan refinement(const PolyhedralFan &a, const PolyhedralFan &b, int cutOffDimension, bool allowASingleConeOfCutOffDimension) |
---|
87 | { |
---|
88 | // fprintf(Stderr,"PolyhedralFan refinement: #A=%i #B=%i\n",a.cones.size(),b.cones.size()); |
---|
89 | int conesSkipped=0; |
---|
90 | int numberOfComputedCones=0; |
---|
91 | bool linealityConeFound=!allowASingleConeOfCutOffDimension; |
---|
92 | assert(a.getAmbientDimension()==b.getAmbientDimension()); |
---|
93 | |
---|
94 | PolyhedralFan ret(a.getAmbientDimension()); |
---|
95 | |
---|
96 | for(PolyhedralConeList::const_iterator A=a.cones.begin();A!=a.cones.end();A++) |
---|
97 | for(PolyhedralConeList::const_iterator B=b.cones.begin();B!=b.cones.end();B++) |
---|
98 | { |
---|
99 | PolyhedralCone c=intersection(*A,*B); |
---|
100 | int cdim=c.dimension(); |
---|
101 | // assert(cdim>=linealitySpaceDimension); |
---|
102 | bool thisIsLinealityCone=(cutOffDimension>=cdim); |
---|
103 | if((!thisIsLinealityCone)||(!linealityConeFound)) |
---|
104 | { |
---|
105 | numberOfComputedCones++; |
---|
106 | c.canonicalize(); |
---|
107 | ret.cones.insert(c); |
---|
108 | linealityConeFound=linealityConeFound || thisIsLinealityCone; |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | conesSkipped++; |
---|
113 | } |
---|
114 | } |
---|
115 | // fprintf(Stderr,"Number of skipped cones: %i, lineality cone found: %i\n",conesSkipped,linealityConeFound); |
---|
116 | // fprintf(Stderr,"Number of computed cones: %i, number of unique cones: %i\n",numberOfComputedCones,ret.cones.size()); |
---|
117 | |
---|
118 | return ret; |
---|
119 | } |
---|
120 | */ |
---|
121 | |
---|
122 | /* |
---|
123 | PolyhedralFan product(const PolyhedralFan &a, const PolyhedralFan &b) |
---|
124 | { |
---|
125 | PolyhedralFan ret(a.getAmbientDimension()+b.getAmbientDimension()); |
---|
126 | |
---|
127 | for(PolyhedralConeList::const_iterator A=a.cones.begin();A!=a.cones.end();A++) |
---|
128 | for(PolyhedralConeList::const_iterator B=b.cones.begin();B!=b.cones.end();B++) |
---|
129 | ret.insert(product(*A,*B)); |
---|
130 | |
---|
131 | return ret; |
---|
132 | } |
---|
133 | */ |
---|
134 | |
---|
135 | /*IntegerVectorList PolyhedralFan::getRays(int dim) |
---|
136 | { |
---|
137 | IntegerVectorList ret; |
---|
138 | for(PolyhedralConeList::iterator i=cones.begin();i!=cones.end();i++) |
---|
139 | { |
---|
140 | if(i->dimension()==dim) |
---|
141 | ret.push_back(i->getRelativeInteriorPoint()); |
---|
142 | } |
---|
143 | return ret; |
---|
144 | } |
---|
145 | */ |
---|
146 | |
---|
147 | /*IntegerVectorList PolyhedralFan::getRelativeInteriorPoints() |
---|
148 | { |
---|
149 | IntegerVectorList ret; |
---|
150 | for(PolyhedralConeList::iterator i=cones.begin();i!=cones.end();i++) |
---|
151 | { |
---|
152 | ret.push_back(i->getRelativeInteriorPoint()); |
---|
153 | } |
---|
154 | return ret; |
---|
155 | } |
---|
156 | */ |
---|
157 | |
---|
158 | /*PolyhedralCone const& PolyhedralFan::highestDimensionalCone()const |
---|
159 | { |
---|
160 | return *cones.rbegin(); |
---|
161 | } |
---|
162 | */ |
---|
163 | void PolyhedralFan::insert(ZCone const &c) |
---|
164 | { |
---|
165 | ZCone temp=c; |
---|
166 | temp.canonicalize(); |
---|
167 | cones.insert(temp); |
---|
168 | } |
---|
169 | |
---|
170 | void PolyhedralFan::remove(ZCone const &c) |
---|
171 | { |
---|
172 | cones.erase(c); |
---|
173 | } |
---|
174 | |
---|
175 | /* |
---|
176 | void PolyhedralFan::removeAllExcept(int a) |
---|
177 | { |
---|
178 | PolyhedralConeList::iterator i=cones.begin(); |
---|
179 | while(a>0) |
---|
180 | { |
---|
181 | if(i==cones.end())return; |
---|
182 | i++; |
---|
183 | a--; |
---|
184 | } |
---|
185 | cones.erase(i,cones.end()); |
---|
186 | } |
---|
187 | */ |
---|
188 | |
---|
189 | void PolyhedralFan::removeAllLowerDimensional() |
---|
190 | { |
---|
191 | if(!cones.empty()) |
---|
192 | { |
---|
193 | int d=getMaxDimension(); |
---|
194 | PolyhedralConeList::iterator i=cones.begin(); |
---|
195 | while(i!=cones.end() && i->dimension()==d)i++; |
---|
196 | cones.erase(i,cones.end()); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | PolyhedralFan PolyhedralFan::facetComplex()const |
---|
202 | { |
---|
203 | // fprintf(Stderr,"Computing facet complex...\n"); |
---|
204 | PolyhedralFan ret(n); |
---|
205 | |
---|
206 | for(PolyhedralConeList::iterator i=cones.begin();i!=cones.end();i++) |
---|
207 | { |
---|
208 | PolyhedralFan a=facetsOfCone(*i); |
---|
209 | for(PolyhedralConeList::const_iterator j=a.cones.begin();j!=a.cones.end();j++) |
---|
210 | ret.insert(*j); |
---|
211 | } |
---|
212 | // fprintf(Stderr,"Done computing facet complex.\n"); |
---|
213 | return ret; |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | /* |
---|
218 | PolyhedralFan PolyhedralFan::fullComplex()const |
---|
219 | { |
---|
220 | PolyhedralFan ret=*this; |
---|
221 | |
---|
222 | while(1) |
---|
223 | { |
---|
224 | log2 debug<<"looping"; |
---|
225 | bool doLoop=false; |
---|
226 | PolyhedralFan facets=ret.facetComplex(); |
---|
227 | log2 debug<<"number of facets"<<facets.size()<<"\n"; |
---|
228 | for(PolyhedralConeList::const_iterator i=facets.cones.begin();i!=facets.cones.end();i++) |
---|
229 | if(!ret.contains(*i)) |
---|
230 | { |
---|
231 | ret.insert(*i); |
---|
232 | doLoop=true; |
---|
233 | } |
---|
234 | if(!doLoop)break; |
---|
235 | } |
---|
236 | return ret; |
---|
237 | } |
---|
238 | */ |
---|
239 | |
---|
240 | |
---|
241 | #if 0 |
---|
242 | PolyhedralFan PolyhedralFan::facetComplexSymmetry(SymmetryGroup const &sym, bool keepRays, bool dropLinealitySpace)const |
---|
243 | { |
---|
244 | log1 fprintf(Stderr,"Computing facet complex...\n"); |
---|
245 | PolyhedralFan ret(n); |
---|
246 | |
---|
247 | vector<IntegerVector> relIntTable; |
---|
248 | vector<int> dimensionTable; |
---|
249 | |
---|
250 | if(keepRays) |
---|
251 | for(PolyhedralConeList::iterator i=cones.begin();i!=cones.end();i++) |
---|
252 | if(i->dimension()==i->dimensionOfLinealitySpace()+1) |
---|
253 | { |
---|
254 | relIntTable.push_back(i->getRelativeInteriorPoint()); |
---|
255 | dimensionTable.push_back(i->dimension()); |
---|
256 | ret.insert(*i); |
---|
257 | } |
---|
258 | |
---|
259 | for(PolyhedralConeList::iterator i=cones.begin();i!=cones.end();i++) |
---|
260 | { |
---|
261 | int iDim=i->dimension(); |
---|
262 | if(dropLinealitySpace && (i->dimension()==i->dimensionOfLinealitySpace()+1))continue; |
---|
263 | |
---|
264 | // i->findFacets(); |
---|
265 | IntegerVectorList normals=i->getHalfSpaces(); |
---|
266 | for(IntegerVectorList::const_iterator j=normals.begin();j!=normals.end();j++) |
---|
267 | { |
---|
268 | bool alreadyInRet=false; |
---|
269 | for(int l=0;l<relIntTable.size();l++) |
---|
270 | { |
---|
271 | if(dimensionTable[l]==iDim-1) |
---|
272 | for(SymmetryGroup::ElementContainer::const_iterator k=sym.elements.begin();k!=sym.elements.end();k++) |
---|
273 | { |
---|
274 | IntegerVector u=SymmetryGroup::compose(*k,relIntTable[l]); |
---|
275 | if((dotLong(*j,u)==0) && (i->contains(u))) |
---|
276 | { |
---|
277 | alreadyInRet=true; |
---|
278 | goto exitLoop; |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | exitLoop: |
---|
283 | if(!alreadyInRet) |
---|
284 | { |
---|
285 | IntegerVectorList equations=i->getEquations(); |
---|
286 | IntegerVectorList inequalities=i->getHalfSpaces(); |
---|
287 | equations.push_back(*j); |
---|
288 | PolyhedralCone c(inequalities,equations,n); |
---|
289 | c.canonicalize(); |
---|
290 | ret.insert(c); |
---|
291 | relIntTable.push_back(c.getRelativeInteriorPoint()); |
---|
292 | dimensionTable.push_back(c.dimension()); |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|
296 | log1 fprintf(Stderr,"Done computing facet complex.\n"); |
---|
297 | return ret; |
---|
298 | } |
---|
299 | #endif |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | ZMatrix PolyhedralFan::getRaysInPrintingOrder(bool upToSymmetry)const |
---|
304 | { |
---|
305 | /* |
---|
306 | * The ordering changed in this version. Previously the orbit representatives stored in "rays" were |
---|
307 | * just the first extreme ray from the orbit that appeared. Now it is gotten using "orbitRepresentative" |
---|
308 | * which causes the ordering in which the different orbits appear to change. |
---|
309 | */ |
---|
310 | if(cones.empty())return ZMatrix(0,n); |
---|
311 | ZMatrix generatorsOfLinealitySpace=cones.begin()->generatorsOfLinealitySpace();//all cones have the same lineality space |
---|
312 | |
---|
313 | |
---|
314 | std::set<ZVector> rays;//(this->getAmbientDimension()); |
---|
315 | // log1 fprintf(Stderr,"Computing rays of %i cones\n",cones.size()); |
---|
316 | for(PolyhedralConeList::const_iterator i=cones.begin();i!=cones.end();i++) |
---|
317 | { |
---|
318 | ZMatrix temp=i->extremeRays(&generatorsOfLinealitySpace); |
---|
319 | // std::cerr<<temp; |
---|
320 | for(int j=0;j<temp.getHeight();j++) |
---|
321 | rays.insert(symmetries.orbitRepresentative(temp[j])); |
---|
322 | } |
---|
323 | ZMatrix ret(0,getAmbientDimension()); |
---|
324 | if(upToSymmetry) |
---|
325 | for(set<ZVector>::const_iterator i=rays.begin();i!=rays.end();i++)ret.appendRow(*i); |
---|
326 | else |
---|
327 | for(set<ZVector>::const_iterator i=rays.begin();i!=rays.end();i++) |
---|
328 | { |
---|
329 | set<ZVector> thisOrbitsRays; |
---|
330 | for(SymmetryGroup::ElementContainer::const_iterator k=symmetries.elements.begin();k!=symmetries.elements.end();k++) |
---|
331 | { |
---|
332 | ZVector temp=k->apply(*i); |
---|
333 | thisOrbitsRays.insert(temp); |
---|
334 | } |
---|
335 | |
---|
336 | for(set<ZVector>::const_iterator i=thisOrbitsRays.begin();i!=thisOrbitsRays.end();i++)ret.appendRow(*i); |
---|
337 | } |
---|
338 | return ret; |
---|
339 | } |
---|
340 | |
---|
341 | |
---|
342 | |
---|
343 | /*MARKS CONES AS NONMAXIMAL IN THE SYMMETRIC COMPLEX IN WHICH THEY WILL BE INSERTED -not to be confused with the facet testing in the code |
---|
344 | */ |
---|
345 | static list<SymmetricComplex::Cone> computeFacets(SymmetricComplex::Cone const &theCone, ZMatrix const &rays, ZMatrix const &facetCandidates, SymmetricComplex const &theComplex/*, int linealityDim*/) |
---|
346 | { |
---|
347 | set<SymmetricComplex::Cone> ret; |
---|
348 | |
---|
349 | for(int i=0;i<facetCandidates.getHeight();i++) |
---|
350 | { |
---|
351 | set<int> indices; |
---|
352 | |
---|
353 | bool notAll=false; |
---|
354 | for(int j=0;j<theCone.indices.size();j++) |
---|
355 | if(dot(rays[theCone.indices[j]],facetCandidates[i]).sign()==0) |
---|
356 | indices.insert(theCone.indices[j]); |
---|
357 | else |
---|
358 | notAll=true; |
---|
359 | |
---|
360 | SymmetricComplex::Cone temp(indices,theCone.dimension-1,Integer(),false,theComplex); |
---|
361 | /* temp.multiplicity=0; |
---|
362 | temp.dimension=theCone.dimension-1; |
---|
363 | temp.setIgnoreSymmetry(true); |
---|
364 | */ |
---|
365 | if(notAll)ret.insert(temp); |
---|
366 | |
---|
367 | } |
---|
368 | // fprintf(Stderr,"HEJ!!!!\n"); |
---|
369 | |
---|
370 | list<SymmetricComplex::Cone> ret2; |
---|
371 | for(set<SymmetricComplex::Cone>::const_iterator i=ret.begin();i!=ret.end();i++) |
---|
372 | { |
---|
373 | bool isMaximal=true; |
---|
374 | |
---|
375 | /* if(i->indices.size()+linealityDim<i->dimension)//#3 |
---|
376 | isMaximal=false; |
---|
377 | else*/ |
---|
378 | for(set<SymmetricComplex::Cone>::const_iterator j=ret.begin();j!=ret.end();j++) |
---|
379 | if(i!=j && i->isSubsetOf(*j)) |
---|
380 | { |
---|
381 | isMaximal=false; |
---|
382 | break; |
---|
383 | } |
---|
384 | if(isMaximal) |
---|
385 | { |
---|
386 | SymmetricComplex::Cone temp(i->indexSet(),i->dimension,i->multiplicity,true,theComplex); |
---|
387 | temp.setKnownToBeNonMaximal(); // THIS IS WHERE WE SET THE CONES NON-MAXIMAL FLAG |
---|
388 | // temp.setIgnoreSymmetry(false); |
---|
389 | ret2.push_back(temp); |
---|
390 | } |
---|
391 | } |
---|
392 | return ret2; |
---|
393 | } |
---|
394 | |
---|
395 | void addFacesToSymmetricComplex(SymmetricComplex &c, ZCone const &cone, ZMatrix const &facetCandidates, ZMatrix const &generatorsOfLinealitySpace) |
---|
396 | { |
---|
397 | ZMatrix const &rays=c.getVertices(); |
---|
398 | std::set<int> indices; |
---|
399 | |
---|
400 | // for(int j=0;j<rays.getHeight();j++)if(cone.contains(rays[j]))indices.insert(j); |
---|
401 | |
---|
402 | ZMatrix l=cone.extremeRays(&generatorsOfLinealitySpace); |
---|
403 | for(int i=0;i<l.getHeight();i++)indices.insert(c.indexOfVertex(l[i])); |
---|
404 | |
---|
405 | addFacesToSymmetricComplex(c,indices,facetCandidates,cone.dimension(),cone.getMultiplicity()); |
---|
406 | } |
---|
407 | |
---|
408 | void addFacesToSymmetricComplex(SymmetricComplex &c, std::set<int> const &indices, ZMatrix const &facetCandidates, int dimension, Integer multiplicity) |
---|
409 | { |
---|
410 | ZMatrix const &rays=c.getVertices(); |
---|
411 | list<SymmetricComplex::Cone> clist; |
---|
412 | { |
---|
413 | SymmetricComplex::Cone temp(indices,dimension,multiplicity,true,c); |
---|
414 | // temp.dimension=cone.dimension(); |
---|
415 | // temp.multiplicity=cone.getMultiplicity(); |
---|
416 | clist.push_back(temp); |
---|
417 | } |
---|
418 | |
---|
419 | // int linealityDim=cone.dimensionOfLinealitySpace(); |
---|
420 | |
---|
421 | while(!clist.empty()) |
---|
422 | { |
---|
423 | SymmetricComplex::Cone &theCone=clist.front(); |
---|
424 | |
---|
425 | if(!c.contains(theCone)) |
---|
426 | { |
---|
427 | |
---|
428 | c.insert(theCone); |
---|
429 | // log0 fprintf(Stderr,"ADDING\n"); |
---|
430 | list<SymmetricComplex::Cone> facets=computeFacets(theCone,rays,facetCandidates,c/*,linealityDim*/); |
---|
431 | clist.splice(clist.end(),facets); |
---|
432 | } |
---|
433 | clist.pop_front(); |
---|
434 | } |
---|
435 | |
---|
436 | } |
---|
437 | |
---|
438 | #if 0 |
---|
439 | /** |
---|
440 | Produce strings that express the vectors in terms of rays of the fan modulo the lineality space. Symmetry is ignored?? |
---|
441 | */ |
---|
442 | vector<string> PolyhedralFan::renamingStrings(IntegerVectorList const &theVectors, IntegerVectorList const &originalRays, IntegerVectorList const &linealitySpace, SymmetryGroup *sym)const |
---|
443 | { |
---|
444 | vector<string> ret; |
---|
445 | for(IntegerVectorList::const_iterator i=theVectors.begin();i!=theVectors.end();i++) |
---|
446 | { |
---|
447 | for(PolyhedralConeList::const_iterator j=cones.begin();j!=cones.end();j++) |
---|
448 | { |
---|
449 | if(j->contains(*i)) |
---|
450 | { |
---|
451 | vector<int> relevantIndices; |
---|
452 | IntegerVectorList relevantRays=linealitySpace; |
---|
453 | int K=0; |
---|
454 | for(IntegerVectorList::const_iterator k=originalRays.begin();k!=originalRays.end();k++,K++) |
---|
455 | if(j->contains(*k)) |
---|
456 | { |
---|
457 | relevantIndices.push_back(K); |
---|
458 | relevantRays.push_back(*k); |
---|
459 | } |
---|
460 | |
---|
461 | FieldMatrix LFA(Q,relevantRays.size(),n); |
---|
462 | int J=0; |
---|
463 | for(IntegerVectorList::const_iterator j=relevantRays.begin();j!=relevantRays.end();j++,J++) |
---|
464 | LFA[J]=integerVectorToFieldVector(*j,Q); |
---|
465 | FieldVector LFB=concatenation(integerVectorToFieldVector(*i,Q),FieldVector(Q,relevantRays.size())); |
---|
466 | LFA=LFA.transposed(); |
---|
467 | FieldVector LFX=LFA.solver().canonicalize(LFB); |
---|
468 | stringstream s; |
---|
469 | if(LFX.subvector(0,n).isZero()) |
---|
470 | { |
---|
471 | s<<"Was:"; |
---|
472 | FieldVector S=LFX.subvector(n+linealitySpace.size(),LFX.size()); |
---|
473 | for(int k=0;k<S.size();k++) |
---|
474 | if(!S[k].isZero()) |
---|
475 | s<<"+"<<S[k].toString()<<"*["<<relevantIndices[k]<<"] "; |
---|
476 | } |
---|
477 | ret.push_back(s.str()); |
---|
478 | break; |
---|
479 | } |
---|
480 | } |
---|
481 | } |
---|
482 | return ret; |
---|
483 | } |
---|
484 | #endif |
---|
485 | |
---|
486 | SymmetricComplex PolyhedralFan::toSymmetricComplex()const |
---|
487 | { |
---|
488 | |
---|
489 | ZMatrix rays=getRaysInPrintingOrder(); |
---|
490 | |
---|
491 | ZMatrix generatorsOfLinealitySpace=cones.empty()?ZMatrix::identity(getAmbientDimension()):cones.begin()->generatorsOfLinealitySpace(); |
---|
492 | SymmetricComplex symCom(rays,generatorsOfLinealitySpace,symmetries); |
---|
493 | |
---|
494 | for(PolyhedralConeList::const_iterator i=cones.begin();i!=cones.end();i++) |
---|
495 | { |
---|
496 | { |
---|
497 | static int t; |
---|
498 | // log1 fprintf(Stderr,"Adding faces of cone %i\n",t++); |
---|
499 | } |
---|
500 | // log2 fprintf(Stderr,"Dim: %i\n",i->dimension()); |
---|
501 | |
---|
502 | addFacesToSymmetricComplex(symCom,*i,i->getFacets(),generatorsOfLinealitySpace); |
---|
503 | } |
---|
504 | |
---|
505 | // log1 cerr<<"Remapping"; |
---|
506 | symCom.remap(); |
---|
507 | // log1 cerr<<"Done remapping"; |
---|
508 | |
---|
509 | return symCom; |
---|
510 | } |
---|
511 | |
---|
512 | std::string PolyhedralFan::toString(int flags)const |
---|
513 | //void PolyhedralFan::printWithIndices(class Printer *p, bool printMultiplicities, SymmetryGroup *sym, bool group, bool ignoreCones, bool xml, bool tPlaneSort, vector<string> const *comments)const |
---|
514 | { |
---|
515 | stringstream ret; |
---|
516 | |
---|
517 | for(PolyhedralConeList::const_iterator i=cones.begin();i!=cones.end();i++) |
---|
518 | { |
---|
519 | ret<<"Cone\n"<<endl; |
---|
520 | ret<<*i; |
---|
521 | } return ret.str(); |
---|
522 | #if 0 |
---|
523 | PolymakeFile polymakeFile; |
---|
524 | polymakeFile.create("NONAME","PolyhedralFan","PolyhedralFan",flags&FPF_xml); |
---|
525 | |
---|
526 | // if(!sym)sym=&symm; |
---|
527 | |
---|
528 | if(cones.empty()) |
---|
529 | { |
---|
530 | // p->printString("Polyhedral fan is empty. Printing not supported.\n"); |
---|
531 | ret<<"Polyhedral fan is empty. Printing not supported.\n"; |
---|
532 | return ret.str(); |
---|
533 | } |
---|
534 | |
---|
535 | int h=cones.begin()->dimensionOfLinealitySpace(); |
---|
536 | |
---|
537 | // log1 fprintf(Stderr,"Computing rays.\n"); |
---|
538 | ZMatrix rays=getRaysInPrintingOrder(); |
---|
539 | |
---|
540 | SymmetricComplex symCom(rays,cones.begin()->generatorsOfLinealitySpace(),symmetries); |
---|
541 | |
---|
542 | polymakeFile.writeCardinalProperty("AMBIENT_DIM",n); |
---|
543 | polymakeFile.writeCardinalProperty("DIM",getMaxDimension()); |
---|
544 | polymakeFile.writeCardinalProperty("LINEALITY_DIM",h); |
---|
545 | polymakeFile.writeMatrixProperty("RAYS",rays,true,comments); |
---|
546 | polymakeFile.writeCardinalProperty("N_RAYS",rays.size()); |
---|
547 | IntegerVectorList linealitySpaceGenerators=highestDimensionalCone().linealitySpace().dualCone().getEquations(); |
---|
548 | polymakeFile.writeMatrixProperty("LINEALITY_SPACE",rowsToIntegerMatrix(linealitySpaceGenerators,n)); |
---|
549 | polymakeFile.writeMatrixProperty("ORTH_LINEALITY_SPACE",rowsToIntegerMatrix(highestDimensionalCone().linealitySpace().getEquations(),n)); |
---|
550 | |
---|
551 | if(flags & FPF_primitiveRays) |
---|
552 | { |
---|
553 | ZMatrix primitiveRays; |
---|
554 | for(IntegerVectorList::const_iterator i=rays.begin();i!=rays.end();i++) |
---|
555 | for(PolyhedralConeList::const_iterator j=cones.begin();j!=cones.end();j++) |
---|
556 | if(j->contains(*i)&&(j->dimensionOfLinealitySpace()+1==j->dimension())) |
---|
557 | primitiveRays.push_back(j->semiGroupGeneratorOfRay()); |
---|
558 | |
---|
559 | polymakeFile.writeMatrixProperty("PRIMITIVE_RAYS",rowsToIntegerMatrix(primitiveRays,n)); |
---|
560 | } |
---|
561 | |
---|
562 | |
---|
563 | ZMatrix generatorsOfLinealitySpace=cones.begin()->generatorsOfLinealitySpace(); |
---|
564 | |
---|
565 | log1 fprintf(Stderr,"Building symmetric complex.\n"); |
---|
566 | for(PolyhedralConeList::const_iterator i=cones.begin();i!=cones.end();i++) |
---|
567 | { |
---|
568 | { |
---|
569 | static int t; |
---|
570 | // log1 fprintf(Stderr,"Adding faces of cone %i\n",t++); |
---|
571 | } |
---|
572 | // log2 fprintf(Stderr,"Dim: %i\n",i->dimension()); |
---|
573 | |
---|
574 | addFacesToSymmetricComplex(symCom,*i,i->getHalfSpaces(),generatorsOfLinealitySpace); |
---|
575 | } |
---|
576 | |
---|
577 | // log1 cerr<<"Remapping"; |
---|
578 | symCom.remap(); |
---|
579 | // log1 cerr<<"Done remapping"; |
---|
580 | |
---|
581 | |
---|
582 | PolyhedralFan f=*this; |
---|
583 | |
---|
584 | // log1 fprintf(Stderr,"Computing f-vector.\n"); |
---|
585 | ZVector fvector=symCom.fvector(); |
---|
586 | polymakeFile.writeCardinalVectorProperty("F_VECTOR",fvector); |
---|
587 | // log1 fprintf(Stderr,"Done computing f-vector.\n"); |
---|
588 | |
---|
589 | if(flags&FPF_boundedInfo) |
---|
590 | { |
---|
591 | // log1 fprintf(Stderr,"Computing bounded f-vector.\n"); |
---|
592 | ZVector fvectorBounded=symCom.fvector(true); |
---|
593 | polymakeFile.writeCardinalVectorProperty("F_VECTOR_BOUNDED",fvectorBounded); |
---|
594 | // log1 fprintf(Stderr,"Done computing bounded f-vector.\n"); |
---|
595 | } |
---|
596 | { |
---|
597 | Integer euler; |
---|
598 | int mul=-1; |
---|
599 | for(int i=0;i<fvector.size();i++,mul*=-1)euler+=Integer(mul)*fvector[i]; |
---|
600 | polymakeFile.writeCardinalProperty("MY_EULER",euler); |
---|
601 | } |
---|
602 | |
---|
603 | // log1 fprintf(Stderr,"Checking if complex is simplicial and pure.\n"); |
---|
604 | polymakeFile.writeCardinalProperty("SIMPLICIAL",symCom.isSimplicial()); |
---|
605 | polymakeFile.writeCardinalProperty("PURE",symCom.isPure()); |
---|
606 | // log1 fprintf(Stderr,"Done checking.\n"); |
---|
607 | |
---|
608 | |
---|
609 | if(flags&FPF_conesCompressed) |
---|
610 | { |
---|
611 | // log1 fprintf(Stderr,"Producing list of cones up to symmetry.\n"); |
---|
612 | polymakeFile.writeStringProperty("CONES_ORBITS",symCom.toString(symCom.getMinDim(),symCom.getMaxDim(),false,flags&FPF_group,0,true,flags&FPF_tPlaneSort)); |
---|
613 | // log1 fprintf(Stderr,"Done producing list of cones up to symmetry.\n"); |
---|
614 | // log1 fprintf(Stderr,"Producing list of maximal cones up to symmetry.\n"); |
---|
615 | stringstream multiplicities; |
---|
616 | polymakeFile.writeStringProperty("MAXIMAL_CONES_ORBITS",symCom.toString(symCom.getMinDim(),symCom.getMaxDim(),true,flags&FPF_group, &multiplicities,true,flags&FPF_tPlaneSort)); |
---|
617 | if(flags&FPF_multiplicities)polymakeFile.writeStringProperty("MULTIPLICITIES_ORBITS",multiplicities.str()); |
---|
618 | // log1 fprintf(Stderr,"Done producing list of maximal cones up to symmetry.\n"); |
---|
619 | } |
---|
620 | |
---|
621 | if(flags&FPF_conesExpanded) |
---|
622 | { |
---|
623 | if(flags&FPF_cones) |
---|
624 | { |
---|
625 | // log1 fprintf(Stderr,"Producing list of cones.\n"); |
---|
626 | polymakeFile.writeStringProperty("CONES",symCom.toString(symCom.getMinDim(),symCom.getMaxDim(),false,flags&FPF_group,0,false,flags&FPF_tPlaneSort)); |
---|
627 | // log1 fprintf(Stderr,"Done producing list of cones.\n"); |
---|
628 | } |
---|
629 | if(flags&FPF_maximalCones) |
---|
630 | { |
---|
631 | // log1 fprintf(Stderr,"Producing list of maximal cones.\n"); |
---|
632 | stringstream multiplicities; |
---|
633 | polymakeFile.writeStringProperty("MAXIMAL_CONES",symCom.toString(symCom.getMinDim(),symCom.getMaxDim(),true,flags&FPF_group, &multiplicities,false,flags&FPF_tPlaneSort)); |
---|
634 | if(flags&FPF_multiplicities)polymakeFile.writeStringProperty("MULTIPLICITIES",multiplicities.str()); |
---|
635 | // log1 fprintf(Stderr,"Done producing list of maximal cones.\n"); |
---|
636 | } |
---|
637 | } |
---|
638 | #endif |
---|
639 | #if 0 |
---|
640 | if(flags&FPF_values) |
---|
641 | { |
---|
642 | { |
---|
643 | ZMatrix values; |
---|
644 | for(int i=0;i<linealitySpaceGenerators.getHeight();i++) |
---|
645 | { |
---|
646 | ZVector v(1); |
---|
647 | v[0]=evaluatePiecewiseLinearFunction(linealitySpaceGenerators[i]); |
---|
648 | values.appendRow(v); |
---|
649 | } |
---|
650 | polymakeFile.writeMatrixProperty("LINEALITY_VALUES",rowsToIntegerMatrix(values,1)); |
---|
651 | } |
---|
652 | { |
---|
653 | ZMatrix values; |
---|
654 | for(IntegerVectorList::const_iterator i=rays.begin();i!=rays.end();i++) |
---|
655 | { |
---|
656 | ZVector v(1); |
---|
657 | v[0]=evaluatePiecewiseLinearFunction(*i); |
---|
658 | values.push_back(v); |
---|
659 | } |
---|
660 | polymakeFile.writeMatrixProperty("RAY_VALUES",rowsToIntegerMatrix(values,1)); |
---|
661 | } |
---|
662 | } |
---|
663 | #endif |
---|
664 | |
---|
665 | |
---|
666 | // log1 fprintf(Stderr,"Producing final string for output.\n"); |
---|
667 | /* stringstream s; |
---|
668 | polymakeFile.writeStream(s); |
---|
669 | string S=s.str(); |
---|
670 | // log1 fprintf(Stderr,"Printing string.\n"); |
---|
671 | p->printString(S.c_str()); |
---|
672 | */// log1 fprintf(Stderr,"Done printing string.\n"); |
---|
673 | } |
---|
674 | |
---|
675 | #if 0 |
---|
676 | PolyhedralFan PolyhedralFan::readFan(string const &filename, bool onlyMaximal, IntegerVector *w, set<int> const *coneIndices, SymmetryGroup const *sym, bool readCompressedIfNotSym) |
---|
677 | { |
---|
678 | PolymakeFile inFile; |
---|
679 | inFile.open(filename.c_str()); |
---|
680 | |
---|
681 | int n=inFile.readCardinalProperty("AMBIENT_DIM"); |
---|
682 | int nRays=inFile.readCardinalProperty("N_RAYS"); |
---|
683 | IntegerMatrix rays=inFile.readMatrixProperty("RAYS",nRays,n); |
---|
684 | int linealityDim=inFile.readCardinalProperty("LINEALITY_DIM"); |
---|
685 | IntegerMatrix linealitySpace=inFile.readMatrixProperty("LINEALITY_SPACE",linealityDim,n); |
---|
686 | |
---|
687 | |
---|
688 | const char *sectionName=0; |
---|
689 | const char *sectionNameMultiplicities=0; |
---|
690 | if(sym || readCompressedIfNotSym) |
---|
691 | { |
---|
692 | sectionName=(onlyMaximal)?"MAXIMAL_CONES_ORBITS":"CONES_ORBITS"; |
---|
693 | sectionNameMultiplicities=(onlyMaximal)?"MULTIPLICITIES_ORBITS":"DUMMY123"; |
---|
694 | } |
---|
695 | else |
---|
696 | { sectionName=(onlyMaximal)?"MAXIMAL_CONES":"CONES"; |
---|
697 | sectionNameMultiplicities=(onlyMaximal)?"MULTIPLICITIES":"DUMMY123"; |
---|
698 | } |
---|
699 | |
---|
700 | |
---|
701 | IntegerVector w2(n); |
---|
702 | if(w==0)w=&w2; |
---|
703 | |
---|
704 | SymmetryGroup sym2(n); |
---|
705 | if(sym==0)sym=&sym2; |
---|
706 | |
---|
707 | vector<list<int> > cones=inFile.readMatrixIncidenceProperty(sectionName); |
---|
708 | IntegerVectorList r; |
---|
709 | |
---|
710 | bool hasMultiplicities=inFile.hasProperty(sectionNameMultiplicities); |
---|
711 | IntegerMatrix multiplicities(0,0); |
---|
712 | if(hasMultiplicities)multiplicities=inFile.readMatrixProperty(sectionNameMultiplicities,cones.size(),1); |
---|
713 | |
---|
714 | |
---|
715 | PolyhedralFan ret(n); |
---|
716 | |
---|
717 | log2 cerr<< "Number of orbits to expand "<<cones.size()<<endl; |
---|
718 | for(int i=0;i<cones.size();i++) |
---|
719 | if(coneIndices==0 || coneIndices->count(i)) |
---|
720 | { |
---|
721 | log2 cerr<<"Expanding symmetries of cone"<<endl; |
---|
722 | { |
---|
723 | IntegerVectorList coneRays; |
---|
724 | for(list<int>::const_iterator j=cones[i].begin();j!=cones[i].end();j++) |
---|
725 | coneRays.push_back((rays[*j])); |
---|
726 | PolyhedralCone C=PolyhedralCone::givenByRays(coneRays,linealitySpace.getRows(),n); |
---|
727 | if(hasMultiplicities)C.setMultiplicity(multiplicities[i][0]); |
---|
728 | for(SymmetryGroup::ElementContainer::const_iterator perm=sym->elements.begin();perm!=sym->elements.end();perm++) |
---|
729 | { |
---|
730 | if(C.contains(SymmetryGroup::composeInverse(*perm,*w))) |
---|
731 | { |
---|
732 | PolyhedralCone C2=C.permuted(*perm); |
---|
733 | C2.canonicalize(); |
---|
734 | ret.insert(C2); |
---|
735 | } |
---|
736 | } |
---|
737 | } |
---|
738 | } |
---|
739 | return ret; |
---|
740 | } |
---|
741 | #endif |
---|
742 | |
---|
743 | #if 0 |
---|
744 | IncidenceList PolyhedralFan::getIncidenceList(SymmetryGroup *sym)const //fan must be pure |
---|
745 | { |
---|
746 | IncidenceList ret; |
---|
747 | SymmetryGroup symm(n); |
---|
748 | if(!sym)sym=&symm; |
---|
749 | assert(!cones.empty()); |
---|
750 | int h=cones.begin()->dimensionOfLinealitySpace(); |
---|
751 | IntegerVectorList rays=getRaysInPrintingOrder(sym); |
---|
752 | PolyhedralFan f=*this; |
---|
753 | |
---|
754 | while(f.getMaxDimension()!=h) |
---|
755 | { |
---|
756 | IntegerVectorList l; |
---|
757 | PolyhedralFan done(n); |
---|
758 | IntegerVector rayIncidenceCounter(rays.size()); |
---|
759 | int faceIndex =0; |
---|
760 | for(PolyhedralConeList::const_iterator i=f.cones.begin();i!=f.cones.end();i++) |
---|
761 | { |
---|
762 | if(!done.contains(*i)) |
---|
763 | { |
---|
764 | for(SymmetryGroup::ElementContainer::const_iterator k=sym->elements.begin();k!=sym->elements.end();k++) |
---|
765 | { |
---|
766 | PolyhedralCone cone=i->permuted(*k); |
---|
767 | if(!done.contains(cone)) |
---|
768 | { |
---|
769 | int rayIndex=0; |
---|
770 | IntegerVector indices(0); |
---|
771 | for(IntegerVectorList::const_iterator j=rays.begin();j!=rays.end();j++) |
---|
772 | { |
---|
773 | if(cone.contains(*j)) |
---|
774 | { |
---|
775 | indices.grow(indices.size()+1); |
---|
776 | indices[indices.size()-1]=rayIndex; |
---|
777 | rayIncidenceCounter[rayIndex]++; |
---|
778 | } |
---|
779 | rayIndex++; |
---|
780 | } |
---|
781 | l.push_back(indices); |
---|
782 | faceIndex++; |
---|
783 | done.insert(cone); |
---|
784 | } |
---|
785 | } |
---|
786 | } |
---|
787 | } |
---|
788 | ret[f.getMaxDimension()]=l; |
---|
789 | f=f.facetComplex(); |
---|
790 | } |
---|
791 | return ret; |
---|
792 | } |
---|
793 | #endif |
---|
794 | |
---|
795 | void PolyhedralFan::makePure() |
---|
796 | { |
---|
797 | if(getMaxDimension()!=getMinDimension())removeAllLowerDimensional(); |
---|
798 | } |
---|
799 | |
---|
800 | bool PolyhedralFan::contains(ZCone const &c)const |
---|
801 | { |
---|
802 | return cones.count(c); |
---|
803 | } |
---|
804 | |
---|
805 | |
---|
806 | #if 0 |
---|
807 | PolyhedralCone PolyhedralFan::coneContaining(ZVector const &v)const |
---|
808 | { |
---|
809 | for(PolyhedralConeList::const_iterator i=cones.begin();i!=cones.end();i++) |
---|
810 | if(i->contains(v))return i->faceContaining(v); |
---|
811 | debug<<"Vector "<<v<<" not contained in support of fan\n"; |
---|
812 | assert(0); |
---|
813 | } |
---|
814 | #endif |
---|
815 | |
---|
816 | PolyhedralFan::coneIterator PolyhedralFan::conesBegin()const |
---|
817 | { |
---|
818 | return cones.begin(); |
---|
819 | } |
---|
820 | |
---|
821 | |
---|
822 | PolyhedralFan::coneIterator PolyhedralFan::conesEnd()const |
---|
823 | { |
---|
824 | return cones.end(); |
---|
825 | } |
---|
826 | |
---|
827 | |
---|
828 | |
---|
829 | PolyhedralFan PolyhedralFan::link(ZVector const &w, SymmetryGroup *sym)const |
---|
830 | { |
---|
831 | SymmetryGroup symL(n); |
---|
832 | if(!sym)sym=&symL; |
---|
833 | |
---|
834 | PolyhedralFan ret(n); |
---|
835 | |
---|
836 | for(PolyhedralConeList::const_iterator i=cones.begin();i!=cones.end();i++) |
---|
837 | { |
---|
838 | for(SymmetryGroup::ElementContainer::const_iterator perm=sym->elements.begin();perm!=sym->elements.end();perm++) |
---|
839 | { |
---|
840 | ZVector w2=perm->applyInverse(w); |
---|
841 | if(i->contains(w2)) |
---|
842 | { |
---|
843 | ret.insert(i->link(w2)); |
---|
844 | } |
---|
845 | } |
---|
846 | } |
---|
847 | return ret; |
---|
848 | } |
---|
849 | |
---|
850 | PolyhedralFan PolyhedralFan::link(ZVector const &w)const |
---|
851 | { |
---|
852 | PolyhedralFan ret(n); |
---|
853 | |
---|
854 | for(PolyhedralConeList::const_iterator i=cones.begin();i!=cones.end();i++) |
---|
855 | { |
---|
856 | if(i->contains(w)) |
---|
857 | { |
---|
858 | ret.insert(i->link(w)); |
---|
859 | } |
---|
860 | } |
---|
861 | return ret; |
---|
862 | } |
---|
863 | |
---|
864 | |
---|
865 | int PolyhedralFan::size()const |
---|
866 | { |
---|
867 | return cones.size(); |
---|
868 | } |
---|
869 | |
---|
870 | int PolyhedralFan::dimensionOfLinealitySpace()const |
---|
871 | { |
---|
872 | assert(cones.size());//slow! |
---|
873 | return cones.begin()->dimensionOfLinealitySpace(); |
---|
874 | } |
---|
875 | |
---|
876 | |
---|
877 | |
---|
878 | |
---|
879 | void PolyhedralFan::removeNonMaximal() |
---|
880 | { |
---|
881 | for(PolyhedralConeList::iterator i=cones.begin();i!=cones.end();) |
---|
882 | { |
---|
883 | ZVector w=i->getRelativeInteriorPoint(); |
---|
884 | bool containedInOther=false; |
---|
885 | for(PolyhedralConeList::iterator j=cones.begin();j!=cones.end();j++) |
---|
886 | if(j!=i) |
---|
887 | { |
---|
888 | if(j->contains(w)){containedInOther=true;break;} |
---|
889 | } |
---|
890 | if(containedInOther) |
---|
891 | { |
---|
892 | PolyhedralConeList::iterator k=i; |
---|
893 | i++; |
---|
894 | cones.erase(k); |
---|
895 | } |
---|
896 | else i++; |
---|
897 | } |
---|
898 | } |
---|
899 | |
---|
900 | |
---|
901 | } |
---|