source: git/gfanlib/gfanlib_zfan.cpp @ 6aca64

spielwiese
Last change on this file since 6aca64 was e4ffc4, checked in by Yue <ren@…>, 8 years ago
fix: crash when asking for certain dimensions in an empty fan
  • Property mode set to 100644
File size: 9.8 KB
Line 
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
11using namespace std;
12
13namespace gfan
14{
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  }
21  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  }
31  int ZFan::numberOfConesOfDimension(int d, bool orbit, bool maximal)const
32  {
33    this->ensureComplex();
34    return numberOf(table(orbit,maximal),d);
35  }
36  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  }
43  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  }
49  void ZFan::ensureConeCollection()const
50  {
51    if(!coneCollection)
52      {
53        assert(0);
54      }
55  }
56  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  }
68  void ZFan::killComplex()const
69  {
70    if(complex)
71      {
72        delete complex;
73        complex=0;
74      }
75  }
76
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//        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//              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
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  }
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  }
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  }
247  ZFan::ZFan(int ambientDimension):
248    complex(0)
249  {
250    coneCollection=new PolyhedralFan(ambientDimension);
251  }
252  ZFan::ZFan(SymmetryGroup const &sym_):
253    complex(0)
254  {
255    coneCollection=new PolyhedralFan(sym_);
256  }
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  }
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  }
269  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  }
278  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      return coneCollection->getAmbientDimension()-coneCollection->getMaxDimension();
287    }
288    assert(0);
289    return 0;
290  }
291  int ZFan::getDimension()const
292  {
293    if(complex)
294      return complex->getMaxDim();
295    if(coneCollection)
296    {
297      if(coneCollection->isEmpty())
298        return -1;
299      return coneCollection->getMaxDimension();
300    }
301    assert(0);
302    return 0;
303  }
304  int ZFan::getLinealityDimension()const
305  {
306    if(complex)
307      return complex->getLinDim();
308    if(coneCollection)
309      return coneCollection->dimensionOfLinealitySpace();
310    assert(0);
311    return 0;
312  }
313  ZVector ZFan::getFVector()const
314  {
315    ensureComplex();
316    return complex->fvector();
317  }
318  bool ZFan::isSimplicial()const
319  {
320    ensureComplex();
321    return complex->isSimplicial();
322  }
323  bool ZFan::isPure()const
324  {
325    ensureComplex();
326    return complex->isPure();
327  }
328  void ZFan::insert(ZCone const &c)
329  {
330    ensureConeCollection();
331    killComplex();
332    coneCollection->insert(c);
333  }
334  void ZFan::remove(ZCone const &c)
335  {
336    ensureConeCollection();
337    killComplex();
338    coneCollection->remove(c);
339  }
340
341/*  ZFan::ZFan(int ambientDimension):
342    theFan(ambientDimension)
343  {
344
345  }*/
346/*
347ZFan::ZFan(SymmetryGroup const &sym):
348  theFan(sym)
349{
350}
351*/
352
353std::string ZFan::toString(int flags)const
354{
355  ensureComplex();
356
357  //  std::string s=complex->toString(flags);
358//  killComplex();
359//  return s;//complex->getMinDim(),complex->getMaxDim(),0,0);
360  return complex->toString(flags);//complex->getMinDim(),complex->getMaxDim(),0,0);
361//  return "NEEDTOFIXTHIS";
362
363  //return theFan.toString();
364}
365
366/*int ZFan::getAmbientDimension()const
367{
368  return theFan.getAmbientDimension();
369}
370
371
372void ZFan::insert(ZCone const &c)
373{
374  theFan.insert(c);
375}
376*/
377/*
378void ZFan::remove(ZCone const &c)
379{
380  theFan.remove(c);
381}
382*/
383/*
384ZFan::coneIterator ZFan::conesBegin()const
385{
386  return theFan.conesBegin();
387}
388
389ZFan::coneIterator ZFan::conesEnd()const
390{
391  return theFan.conesEnd();
392}
393*/
394  //  static PolyhedralFan readFan(string const &filename, bool onlyMaximal=true, IntegerVector *w=0, set<int> const *conesIndice=0, SymmetryGroup const *sym=0, bool readCompressedIfNotSym=false);
395
396}
Note: See TracBrowser for help on using the repository browser.