1 /*
2  * gfanlib_zfan.cpp
3  *
4  *  Created on: Nov 17, 2010
5  *      Author: anders
6  */
7 
8 #include "gfanlib_zfan.h"
9 #include "gfanlib_polymakefile.h"
10 
11 using namespace std;
12 
13 namespace gfan
14 {
numberOf(std::vector<std::vector<IntVector>> T,int dimension)15   static int numberOf(std::vector<std::vector<IntVector> > T, int dimension)
16   {
17     assert(dimension>=0);
18     if(dimension>=(int)T.size())return 0;
19     return T[dimension].size();
20   }
table(bool orbit,bool maximal) const21   std::vector<std::vector<IntVector> > &ZFan::table(bool orbit, bool maximal)const
22   {
23     if(orbit)
24       {
25         if(maximal)return maximalConeOrbits;
26         return coneOrbits;
27       }
28     if(maximal)return maximalCones;
29     return cones;
30   }
numberOfConesOfDimension(int d,bool orbit,bool maximal) const31   int ZFan::numberOfConesOfDimension(int d, bool orbit, bool maximal)const
32   {
33     this->ensureComplex();
34     return numberOf(table(orbit,maximal),d);
35   }
getCone(int dimension,int index,bool orbit,bool maximal) const36   ZCone ZFan::getCone(int dimension, int index, bool orbit, bool maximal)const
37   {
38     IntVector indices=getConeIndices(dimension,index,orbit,maximal);
39     ZCone ret=this->complex->makeZCone(indices);
40     if(maximal)ret.setMultiplicity(((orbit)?multiplicitiesOrbits:multiplicities)[dimension][index]);
41     return ret;
42   }
getConeIndices(int dimension,int index,bool orbit,bool maximal) const43   IntVector ZFan::getConeIndices(int dimension, int index, bool orbit, bool maximal)const
44   {
45     assert(index>=0);
46     assert(index<numberOfConesOfDimension(dimension,orbit,maximal));
47     return table(orbit,maximal)[dimension][index];
48   }
ensureConeCollection() const49   void ZFan::ensureConeCollection()const
50   {
51     if(!coneCollection)
52       {
53         assert(0);
54       }
55   }
ensureComplex() const56   void ZFan::ensureComplex()const
57   {
58     if(!complex)
59       {
60         assert(coneCollection);
61         complex = new SymmetricComplex(coneCollection->toSymmetricComplex());
62         complex->buildConeLists(false,false,&cones);
63         complex->buildConeLists(true,false,&maximalCones,&multiplicities);
64         complex->buildConeLists(false,true,&coneOrbits);
65         complex->buildConeLists(true,true,&maximalConeOrbits,&multiplicitiesOrbits);
66       }
67   }
killComplex() const68   void ZFan::killComplex()const
69   {
70     if(complex)
71       {
72         delete complex;
73         complex=0;
74       }
75   }
76 
ZFan(std::istream & f)77   ZFan::ZFan(std::istream &f):
78     coneCollection(0),
79     complex(0)
80   {
81 //    PolyhedralFan PolyhedralFan::readFan(string const &filename, bool onlyMaximal, IntegerVector *w, set<int> const *coneIndices, SymmetryGroup const *sym, bool readCompressedIfNotSym)
82     PolymakeFile inFile;
83     //assert(0);
84      inFile.open(f);
85 
86     int n=inFile.readCardinalProperty("AMBIENT_DIM").toInt();
87     int nRays=inFile.readCardinalProperty("N_RAYS").toInt();
88     ZMatrix rays=inFile.readMatrixProperty("RAYS",nRays,n);
89     int linealityDim=inFile.readCardinalProperty("LINEALITY_DIM").toInt();
90     ZMatrix linealitySpace=inFile.readMatrixProperty("LINEALITY_SPACE",linealityDim,n);
91 
92     SymmetryGroup sym(n);
93     bool readingSymmetricComplex=false;
94     if(inFile.hasProperty("SYMMETRY_GENERATORS"))
95       {
96         sym.computeClosure(ZToIntMatrix(inFile.readMatrixProperty("SYMMETRY_GENERATORS",-1,n)));
97         readingSymmetricComplex=true;
98       }
99 
100 
101     const char *sectionName=0;
102     const char *sectionNameMultiplicities=0;
103     if(readingSymmetricComplex)
104       {
105         if(inFile.hasProperty("MAXIMAL_CONES_ORBITS"))
106           {
107             sectionName="MAXIMAL_CONES_ORBITS";
108             sectionNameMultiplicities="MULTIPLICITIES_ORBITS";
109           }
110         else
111           {
112             sectionName="CONES_ORBITS";
113           }
114       }
115     else
116       {
117         if(inFile.hasProperty("MAXIMAL_CONES"))
118           {
119             sectionName="MAXIMAL_CONES";
120             sectionNameMultiplicities="MULTIPLICITIES";
121           }
122         else
123           {
124             sectionName="CONES";
125           }
126       }
127 
128     /*    if(sym || readCompressedIfNotSym)
129       {
130         sectionName=(onlyMaximal)?"MAXIMAL_CONES_ORBITS":"CONES_ORBITS";
131         sectionNameMultiplicities=(onlyMaximal)?"MULTIPLICITIES_ORBITS":"DUMMY123";
132       }
133     else
134 */
135     /*{
136         sectionName="MAXIMAL_CONES";//(onlyMaximal)?"MAXIMAL_CONES":"CONES";
137         sectionNameMultiplicities="MULTIPLICITIES";//(onlyMaximal)?"MULTIPLICITIES":"DUMMY123";
138       }
139 */
140 //    ZVector w2(n);
141 //    if(w==0)w=&w2;
142 
143  //       SymmetryGroup sym2(n);
144  //       if(sym==0)sym=&sym2;
145 
146 /*  sectionName=0;
147   if(inFile.hasProperty("MAXIMAL_CONES"))
148     sectionName="MAXIMAL_CONES";
149   else
150     {  if(inFile.hasProperty("CONES"))
151       sectionName="CONES";
152     else
153       assert(0);
154     }*/
155 
156   vector<list<int> > cones=inFile.readMatrixIncidenceProperty(sectionName);
157 //        IntegerVectorList r;
158 
159         bool hasMultiplicities=inFile.hasProperty(sectionNameMultiplicities);
160         ZMatrix multiplicities(0,0);
161         if(hasMultiplicities)multiplicities=inFile.readMatrixProperty(sectionNameMultiplicities,cones.size(),1);
162 
163         ZFan ret(sym);
164 
165 //        gfan_log2 cerr<< "Number of orbits to expand "<<cones.size()<<endl;
166         for(unsigned i=0;i<cones.size();i++)
167         //  if(coneIndices==0 || coneIndices->count(i))
168             {
169 //              gfan_log2 cerr<<"Expanding symmetries of cone"<<endl;
170               {
171                 ZMatrix coneRays(0,n);
172                 for(list<int>::const_iterator j=cones[i].begin();j!=cones[i].end();j++)
173                   coneRays.appendRow((rays[*j]));
174                 ZCone C=ZCone::givenByRays(coneRays,linealitySpace);
175                 if(hasMultiplicities)C.setMultiplicity(multiplicities[i][0]);
176 //                for(SymmetryGroup::ElementContainer::const_iterator perm=sym->elements.begin();perm!=sym->elements.end();perm++)
177                   {
178 //                    if(C.contains(perm.applyInverse(*w)))
179 //                      {
180  //                       PolyhedralCone C2=C.permuted(*perm);
181 //                        C2.canonicalize();
182 //                        ret.insert(C2);
183  //                     }
184                     ret.insert(C);
185                   }
186               }
187             }
188 //        return ret;
189         *this=ret;
190   }
191 
~ZFan()192   ZFan::~ZFan()
193   {
194     if(coneCollection)
195       {
196         delete coneCollection;
197         coneCollection=0;
198       }
199     if(complex)
200       {
201         delete complex;
202         complex=0;
203       }
204   }
ZFan(ZFan const & f)205   ZFan::ZFan(ZFan const& f):
206     coneCollection(0),
207     complex(0),
208     cones(f.table(0,0)),
209     maximalCones(f.table(0,1)),
210     coneOrbits(f.table(1,0)),
211     maximalConeOrbits(f.table(1,1))
212   {
213     if(f.coneCollection)
214       {
215         coneCollection=new PolyhedralFan(*f.coneCollection);
216       }
217     if(f.complex)
218       {
219         complex=new SymmetricComplex(*f.complex);
220       }
221   }
operator =(ZFan const & f)222   ZFan& ZFan::operator=(ZFan const &f)
223   {
224     if(this!=&f)
225       {
226         if(complex)
227           {
228             delete complex;
229             complex=0;
230           }
231         if(coneCollection)
232           {
233             delete coneCollection;
234             coneCollection=0;
235           }
236         if(f.coneCollection)
237           {
238             coneCollection=new PolyhedralFan(*f.coneCollection);
239           }
240         if(f.complex)
241           {
242             complex=new SymmetricComplex(*f.complex);
243           }
244       }
245     return *this;
246   }
ZFan(int ambientDimension)247   ZFan::ZFan(int ambientDimension):
248     complex(0)
249   {
250     coneCollection=new PolyhedralFan(ambientDimension);
251   }
ZFan(SymmetryGroup const & sym_)252   ZFan::ZFan(SymmetryGroup const &sym_):
253     complex(0)
254   {
255     coneCollection=new PolyhedralFan(sym_);
256   }
fullFan(int n)257   ZFan ZFan::fullFan(int n)
258   {
259     ZFan ret(n);
260     ret.insert(ZCone(ZMatrix(0,n),ZMatrix(0,n)));
261     return ret;
262   }
fullFan(SymmetryGroup const & sym_)263   ZFan ZFan::fullFan(SymmetryGroup const &sym_)
264   {
265     ZFan ret(sym_);
266     ret.insert(ZCone(ZMatrix(0,sym_.sizeOfBaseSet()),ZMatrix(0,sym_.sizeOfBaseSet())));
267     return ret;
268   }
getAmbientDimension() const269   int ZFan::getAmbientDimension()const
270   {
271     if(complex)
272       return complex->getAmbientDimension();
273     if(coneCollection)
274       return coneCollection->getAmbientDimension();
275     assert(0);
276     return 0;
277   }
getCodimension() const278   int ZFan::getCodimension()const
279   {
280     if(complex)
281       return complex->getAmbientDimension()-complex->getMaxDim();
282     if(coneCollection)
283     {
284     	if(coneCollection->isEmpty())
285     		return -1;
286     	else
287     		return coneCollection->getAmbientDimension()-coneCollection->getMaxDimension();
288     }
289     assert(0);
290     return 0;
291   }
getDimension() const292   int ZFan::getDimension()const
293   {
294     if(complex)
295       return complex->getMaxDim();
296     if(coneCollection)
297     {
298     	if(coneCollection->isEmpty())
299     		return -1;
300     	else
301     		return coneCollection->getMaxDimension();
302     }
303     assert(0);
304     return 0;
305   }
getLinealityDimension() const306   int ZFan::getLinealityDimension()const
307   {
308     if(complex)
309       return complex->getLinDim();
310     if(coneCollection)
311     {
312     	if(coneCollection->isEmpty())
313     		return getAmbientDimension();
314     	else
315     		return coneCollection->dimensionOfLinealitySpace();
316     }
317     assert(0);
318     return 0;
319   }
getFVector() const320   ZVector ZFan::getFVector()const
321   {
322     ensureComplex();
323     return complex->fvector();
324   }
isSimplicial() const325   bool ZFan::isSimplicial()const
326   {
327     ensureComplex();
328     return complex->isSimplicial();
329   }
isPure() const330   bool ZFan::isPure()const
331   {
332     ensureComplex();
333     return complex->isPure();
334   }
insert(ZCone const & c)335   void ZFan::insert(ZCone const &c)
336   {
337     ensureConeCollection();
338     killComplex();
339     coneCollection->insert(c);
340   }
remove(ZCone const & c)341   void ZFan::remove(ZCone const &c)
342   {
343     ensureConeCollection();
344     killComplex();
345     coneCollection->remove(c);
346   }
347 
348 /*  ZFan::ZFan(int ambientDimension):
349     theFan(ambientDimension)
350   {
351 
352   }*/
353 /*
354 ZFan::ZFan(SymmetryGroup const &sym):
355   theFan(sym)
356 {
357 }
358 */
359 
toString(int flags) const360 std::string ZFan::toString(int flags)const
361 {
362   ensureComplex();
363 
364   //  std::string s=complex->toString(flags);
365 //  killComplex();
366 //  return s;//complex->getMinDim(),complex->getMaxDim(),0,0);
367   return complex->toString(flags);//complex->getMinDim(),complex->getMaxDim(),0,0);
368 //  return "NEEDTOFIXTHIS";
369 
370   //return theFan.toString();
371 }
372 
373 /*int ZFan::getAmbientDimension()const
374 {
375   return theFan.getAmbientDimension();
376 }
377 
378 
379 void ZFan::insert(ZCone const &c)
380 {
381   theFan.insert(c);
382 }
383 */
384 /*
385 void ZFan::remove(ZCone const &c)
386 {
387   theFan.remove(c);
388 }
389 */
390 /*
391 ZFan::coneIterator ZFan::conesBegin()const
392 {
393   return theFan.conesBegin();
394 }
395 
396 ZFan::coneIterator ZFan::conesEnd()const
397 {
398   return theFan.conesEnd();
399 }
400 */
401   //  static PolyhedralFan readFan(string const &filename, bool onlyMaximal=true, IntegerVector *w=0, set<int> const *conesIndice=0, SymmetryGroup const *sym=0, bool readCompressedIfNotSym=false);
402 
403 }
404