source: git/gfanlib/gfanlib_zfan.cpp @ c987db

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