source: git/Singular/dyn_modules/gfanlib/gfan.cc @ 922890

spielwiese
Last change on this file since 922890 was 922890, checked in by Hans Schoenemann <hannes@…>, 9 years ago
moved stairc.h to kernel/combinatorics/
  • Property mode set to 100644
File size: 181.1 KB
Line 
1/*
2Compute the Groebner fan of an ideal
3Author: monerjan
4Date: 2009/11/03 06:57:32
5*/
6
7#include <kernel/mod2.h>
8
9#if HAVE_GFANLIB
10
11#include <misc/options.h>
12#include <kernel/GBEngine/kstd1.h>
13#include <kernel/GBEngine/kutil.h>
14#include <kernel/polys.h>
15#include <kernel/ideals.h>
16#include <kernel/spectrum/kmatrix.h>
17#include <kernel/spectrum/GMPrat.h>
18#include <misc/intvec.h>
19
20//#include "ring.h"        //apparently not needed
21#include <Singular/lists.h>
22#include <polys/prCopy.h>
23#include <kernel/combinatorics/stairc.h>
24#include <fstream>        //read-write cones to files
25#include <string>
26#include <sstream>
27#include <stdlib.h>
28#include <assert.h>
29#include <gfanlib/gfanlib.h>
30
31/*DO NOT REMOVE THIS*/
32#ifndef GMPRATIONAL
33#define GMPRATIONAL
34#endif
35
36#ifdef HAVE_CDD_SETOPER_H
37#include <cdd/setoper.h>
38#include <cdd/cdd.h>
39#include <cdd/cddmp.h>
40#else
41#include <setoper.h>
42#include <cdd.h>
43#include <cddmp.h>
44#endif
45
46#ifndef gfan_DEBUG
47// #define gfan_DEBUG
48#ifndef gfan_DEBUGLEVEL
49#define gfan_DEBUGLEVEL 1
50#endif
51#endif
52
53//NOTE Defining this will slow things down!
54//Only good for very coarse profiling
55// #define gfanp
56#ifdef gfanp
57  #include <sys/time.h>
58  #include <iostream>
59#endif
60
61//NOTE DO NOT REMOVE THIS
62#ifndef SHALLOW
63  #define SHALLOW
64#endif
65
66#ifndef USE_ZFAN
67  #define USE_ZFAN
68#endif
69
70#include <gfan.h>
71using namespace std;
72
73#define ivIsStrictlyPositive iv64isStrictlyPositive
74
75/**
76*\brief Class facet
77*        Implements the facet structure as a linked list
78*
79*/
80
81/** \brief The default constructor for facets
82*/
83
84gfan::ZMatrix* intmat2ZMatrix(const intvec* iMat)
85{
86  int d=iMat->rows();
87  int n=iMat->cols();
88  gfan::ZMatrix* ret = new gfan::ZMatrix(d,n);
89  for(int i=0;i<d;i++)
90    for(int j=0;j<n;j++)
91      (*ret)[i][j]=IMATELEM(*iMat, i+1, j+1);
92  return ret;
93}
94
95facet::facet()
96{
97        // Pointer to next facet.  */
98        /* Defaults to NULL. This way there is no need to check explicitly */
99        this->fNormal=NULL;
100        this->interiorPoint=NULL;
101        this->UCN=0;
102        this->codim2Ptr=NULL;
103        this->codim=1;                //default for (codim-1)-facets
104        this->numCodim2Facets=0;
105        this->numRays=0;
106        this->flipGB=NULL;
107        this->next=NULL;
108        this->prev=NULL;
109        this->flipRing=NULL;        //the ring on the other side
110        this->isFlippable=FALSE;
111}
112
113/** \brief Constructor for facets of codim == 2
114* Note that as of now the code of the constructors is only for facets and codim2-faces. One
115* could easily change that by renaming numCodim2Facets to numCodimNminusOneFacets or similar
116*/
117facet::facet(const int &n)
118{
119        this->fNormal=NULL;
120        this->interiorPoint=NULL;
121        this->UCN=0;
122        this->codim2Ptr=NULL;
123        if(n==2)
124        {
125                this->codim=n;
126        }//NOTE Handle exception here!
127        this->numCodim2Facets=0;
128        this->numRays=0;
129        this->flipGB=NULL;
130        this->next=NULL;
131        this->prev=NULL;
132        this->flipRing=NULL;
133        this->isFlippable=FALSE;
134}
135
136/** \brief The copy constructor
137* By default only copies the fNormal, f2Normals and UCN
138*/
139facet::facet(const facet& f)
140{
141        this->fNormal=iv64Copy(f.fNormal);
142        this->UCN=f.UCN;
143        this->isFlippable=f.isFlippable;
144        //Needed for flip2
145        //NOTE ONLY REFERENCE
146        this->interiorPoint=iv64Copy(f.interiorPoint);//only referencing is prolly not the best thing to do in a copy constructor
147        facet* f2Copy;
148        f2Copy=f.codim2Ptr;
149        facet* f2Act;
150        f2Act=this->codim2Ptr;
151        while(f2Copy!=NULL)
152        {
153                if(f2Act==NULL
154#ifndef SING_NDEBUG
155  #if SIZEOF_LONG==8
156                                 || f2Act==(facet*)0xfefefefefefefefe
157  #elif SIZEOF_LONG==4
158                                 || f2Act==(facet*)0xfefefefe
159  #endif
160#endif
161                  )
162                {
163                        f2Act=new facet(2);
164                        this->codim2Ptr=f2Act;
165                }
166                else
167                {
168                        facet* marker;
169                        marker = f2Act;
170                        f2Act->next = new facet(2);
171                        f2Act = f2Act->next;
172                        f2Act->prev = marker;
173                }
174                int64vec *f2Normal;
175                f2Normal = f2Copy->getFacetNormal();
176//                 f2Act->setFacetNormal(f2Copy->getFacetNormal());
177                f2Act->setFacetNormal(f2Normal);
178                delete f2Normal;
179                f2Act->setUCN(f2Copy->getUCN());
180                f2Copy = f2Copy->next;
181        }
182}
183
184/** \brief Shallow copy constructor for facets
185* We only need the interior point for equality testing
186*/
187facet* facet::shallowCopy(const facet& f)
188{
189        facet *res = new facet();
190        res->fNormal=(int64vec * const)f.fNormal;
191        res->UCN=f.UCN;
192        res->isFlippable=f.isFlippable;
193        res->interiorPoint=(int64vec * const)f.interiorPoint;
194        res->codim2Ptr=(facet * const)f.codim2Ptr;
195        res->prev=NULL;
196        res->next=NULL;
197        res->flipGB=NULL;
198        res->flipRing=NULL;
199        return res;
200}
201
202void facet::shallowDelete()
203{
204#ifndef SING_NDEBUG
205//         printf("shallowdel@UCN %i\n", this->getUCN());
206#endif
207        this->fNormal=NULL;
208//         this->UCN=0;
209        this->interiorPoint=NULL;
210        this->codim2Ptr=NULL;
211        this->prev=NULL;
212        this->next=NULL;
213        this->flipGB=NULL;
214        this->flipRing=NULL;
215        assert(this->fNormal==NULL);
216//         delete(this);
217}
218
219/** The default destructor */
220facet::~facet()
221{
222#ifndef SING_NDEBUG
223//         printf("~facet@UCN %i\n",this->getUCN());
224#endif
225        if(this->fNormal!=NULL)
226                delete this->fNormal;
227        if(this->interiorPoint!=NULL)
228                delete this->interiorPoint;
229        /* Cleanup the codim2-structure */
230//         if(this->codim==2)
231//         {
232//                 facet *codim2Ptr;
233//                 codim2Ptr = this->codim2Ptr;
234//                 while(codim2Ptr!=NULL)
235//                 {
236//                         if(codim2Ptr->fNormal!=NULL)
237//                         {
238//                                 delete codim2Ptr->fNormal;//NOTE Do not want this anymore since the rays are now in gcone!
239//                                 codim2Ptr = codim2Ptr->next;
240//                         }
241//                 }
242//         }
243        //The rays are stored in the cone!
244        if(this->flipGB!=NULL)
245                idDelete((ideal *)&this->flipGB);
246//         if(this->flipRing!=NULL && this->flipRing->idroot!=(idhdl)0xfbfbfbfbfbfbfbfb)
247//                 rDelete(this->flipRing); //See vol II/134
248//         this->flipRing=NULL;
249        this->prev=NULL;
250        this->next=NULL;
251}
252
253inline const int64vec *facet::getRef2FacetNormal() const
254{
255        return(this->fNormal);
256}
257
258/** Equality check for facets based on unique interior points*/
259static bool areEqual2(facet* f, facet *g)
260{
261#ifdef gfanp
262        gcone::numberOfFacetChecks++;
263        timeval start, end;
264        gettimeofday(&start, 0);
265#endif
266        bool res = TRUE;
267        const int64vec *fIntP = f->getRef2InteriorPoint();
268        const int64vec *gIntP = g->getRef2InteriorPoint();
269        for(int ii=0;ii<pVariables;ii++)
270        {
271                if( (*fIntP)[ii] != (*gIntP)[ii] )
272                {
273                        res=FALSE;
274                        break;
275                }
276        }
277//         if( fIntP->compare(gIntP)!=0) res=FALSE;
278#ifdef gfanp
279        gettimeofday(&end, 0);
280        gcone::t_areEqual += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
281#endif
282        return res;
283}
284
285/** \brief Comparison of facets
286 * called from enqueueNewFacets
287* The facet normals are primitve vectors since we call gcone::normalize() on all cones.
288* Hence it should suffice to check whether facet normal f equals minus facet normal s.
289* If so we check the extremal rays
290*
291* BEWARE: It would be better to use const int64vec* but that will lead to call something like
292* int foo=((int64vec*)f2Normal)->compare((int64vec*)s2Normal) resulting in much higher memory usage
293*/
294static bool areEqual(facet *f, facet *s)
295{
296#ifdef gfanp
297        gcone::numberOfFacetChecks++;
298        timeval start, end;
299        gettimeofday(&start, 0);
300#endif
301        bool res = TRUE;
302        int notParallelCtr=0;
303        int ctr=0;
304        const int64vec* fNormal; //No new since iv64Copy and therefore getFacetNormal return a new
305        const int64vec* sNormal;
306        fNormal = f->getRef2FacetNormal();
307        sNormal = s->getRef2FacetNormal();
308#include <misc/intvec.h>
309        //Do not need parallelity. Too time consuming
310//          if(!isParallel(*fNormal,*sNormal))
311//         if(fNormal->compare(ivNeg(sNormal))!=0)//This results in a Mandelbug
312 //                 notParallelCtr++;
313//         else//parallelity, so we check the codim2-facets
314        int64vec *fNRef=const_cast<int64vec*>(fNormal);
315        int64vec *sNRef=const_cast<int64vec*>(sNormal);
316        if(isParallel(*fNRef,*sNRef))
317//         if(fNormal->compare((sNormal))!=0)//Behold! Teh definitive Mandelbug
318        {
319                facet* f2Act;
320                facet* s2Act;
321                f2Act = f->codim2Ptr;
322                ctr=0;
323                while(f2Act!=NULL)
324                {
325                        const int64vec* f2Normal;
326                        f2Normal = f2Act->getRef2FacetNormal();
327//                         int64vec *f2Ref=const_cast<int64vec*>(f2Normal);
328                        s2Act = s->codim2Ptr;
329                        while(s2Act!=NULL)
330                        {
331                                const int64vec* s2Normal;
332                                s2Normal = s2Act->getRef2FacetNormal();
333//                                 bool foo=areEqual(f2Normal,s2Normal);
334//                                 int64vec *s2Ref=const_cast<int64vec*>(s2Normal);
335                                int foo=f2Normal->compare(s2Normal);
336                                if(foo==0)
337                                        ctr++;
338                                s2Act = s2Act->next;
339                        }
340                        f2Act = f2Act->next;
341                }
342        }
343        if(ctr==f->numCodim2Facets)
344                res=TRUE;
345        else
346        {
347#ifdef gfanp
348                gcone::parallelButNotEqual++;
349#endif
350                res=FALSE;
351        }
352#ifdef gfanp
353        gettimeofday(&end, 0);
354        gcone::t_areEqual += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
355#endif
356        return res;
357}
358
359/** Stores the facet normal \param int64vec*/
360inline void facet::setFacetNormal(int64vec *iv)
361{
362        if(this->fNormal!=NULL)
363                delete this->fNormal;
364        this->fNormal = iv64Copy(iv);
365}
366
367/** Hopefully returns the facet normal
368* Mind: iv64Copy returns a new int64vec, so use this in the following way:
369* int64vec *iv;
370* iv = this->getFacetNormal();
371* [...]
372* delete(iv);
373*/
374inline int64vec *facet::getFacetNormal() const
375{
376        return iv64Copy(this->fNormal);
377}
378
379/** Method to print the facet normal*/
380inline void facet::printNormal() const
381{
382        fNormal->show();
383}
384
385/** Store the flipped GB*/
386inline void facet::setFlipGB(ideal I)
387{
388        this->flipGB=idCopy(I);
389}
390
391/** Returns a pointer to the flipped GB
392Seems not be used
393Anyhow idCopy would make sense here.
394*/
395inline ideal facet::getFlipGB()
396{
397        return this->flipGB;
398}
399
400/** Print the flipped GB*/
401inline void facet::printFlipGB()
402{
403#ifndef SING_NDEBUG
404        idShow(this->flipGB);
405#endif
406}
407
408/** Set the UCN */
409inline void facet::setUCN(int n)
410{
411        this->UCN=n;
412}
413
414/** \brief Get the UCN
415* Returns the UCN iff this != NULL, else -1
416*/
417inline int facet::getUCN()
418{
419#ifndef SING_NDEBUG
420  #if SIZEOF_LONG==8
421        if((this!=NULL && this!=(facet * const)0xfbfbfbfbfbfbfbfb))
422  #elif SIZEOF_LONG==4
423        if((this!=NULL && this!=(facet * const)0xfbfbfbfb))
424  #endif
425#endif
426#ifdef SING_NDEBUG
427        if(this!=NULL)
428#endif
429                return this->UCN;
430        else
431                return -1;
432}
433
434/** Store an interior point of the facet */
435inline void facet::setInteriorPoint(int64vec *iv)
436{
437        if(this->interiorPoint!=NULL)
438                delete this->interiorPoint;
439        this->interiorPoint = iv64Copy(iv);
440}
441
442/** Returns a pointer to this->interiorPoint
443* MIND: iv64Copy returns a new int64vec
444* @see facet::getFacetNormal
445*/
446inline int64vec *facet::getInteriorPoint()
447{
448        return iv64Copy(this->interiorPoint);
449}
450
451inline const int64vec *facet::getRef2InteriorPoint()
452{
453        return (this->interiorPoint);
454}
455
456/** \brief Debugging function
457* prints the facet normal an all (codim-2)-facets that belong to it
458*/
459volatile void facet::fDebugPrint()
460{
461  #ifndef SING_NDEBUG
462        facet *codim2Act;
463        codim2Act = this->codim2Ptr;
464        int64vec *fNormal;
465        fNormal = this->getFacetNormal();
466        printf("=======================\n");
467        printf("Facet normal = (");fNormal->show(1,1);printf(")\n");
468        printf("-----------------------\n");
469        printf("Codim2 facets:\n");
470        while(codim2Act!=NULL)
471        {
472                int64vec *f2Normal;
473                f2Normal = codim2Act->getFacetNormal();
474                printf("(");f2Normal->show(1,0);printf(")\n");
475                codim2Act = codim2Act->next;
476                delete f2Normal;
477        }
478        printf("=======================\n");
479         delete fNormal;
480  #endif
481}
482
483
484/**
485*\brief Implements the cone structure
486*
487* A cone is represented by a linked list of facet normals
488* @see facet
489*/
490
491
492/** \brief Default constructor.
493 *
494 * Initialises this->next=NULL and this->facetPtr=NULL
495 */
496gcone::gcone()
497{
498        this->next=NULL;
499        this->prev=NULL;
500        this->facetPtr=NULL;        //maybe this->facetPtr = new facet();
501        this->baseRing=currRing;
502        this->counter++;
503        this->UCN=this->counter;
504        this->numFacets=0;
505        this->ivIntPt=NULL;
506        this->gcRays=NULL;
507}
508
509/** \brief Constructor with ring and ideal
510 *
511 * This constructor takes the root ring and the root ideal as parameters and stores
512 * them in the private members gcone::rootRing and gcone::inputIdeal
513 * This constructor is only called once in the computation of the Gröbner fan,
514 * namely for the very first cone. Therefore pred is set to 1.
515 * Might set it to this->UCN though...
516 * Since knowledge of the root ring is only needed when using reverse search,
517 * this constructor is not needed when using the "second" method
518*/
519gcone::gcone(ring r, ideal I)
520{
521        this->next=NULL;
522        this->prev=NULL;
523        this->facetPtr=NULL;
524        this->inputIdeal=I;
525        this->baseRing=currRing;
526        this->counter++;
527        this->UCN=this->counter;
528        this->pred=1;
529        this->numFacets=0;
530        this->ivIntPt=NULL;
531        this->gcRays=NULL;
532}
533
534/** \brief Copy constructor
535 *
536 * Copies a cone, sets this->gcBasis to the flipped GB
537 * Call this only after a successful call to gcone::flip which sets facet::flipGB
538*/
539gcone::gcone(const gcone& gc, const facet &f)
540{
541        this->next=NULL;
542//         this->prev=(gcone *)&gc; //comment in to get a tree
543        this->prev=NULL;
544        this->numVars=gc.numVars;
545        this->counter++;
546        this->UCN=this->counter;
547        this->pred=gc.UCN;
548        this->facetPtr=NULL;
549           this->gcBasis=idrCopyR(f.flipGB, f.flipRing);
550//          this->inputIdeal=idCopy(this->gcBasis);
551        this->baseRing=rCopy(f.flipRing);
552        this->numFacets=0;
553        this->ivIntPt=NULL;
554        this->gcRays=NULL;
555}
556
557/** \brief Default destructor
558*/
559gcone::~gcone()
560{
561#ifndef SING_NDEBUG
562  #if SIZEOF_LONG==8
563        if( ( this->gcBasis!=(ideal)(0xfbfbfbfbfbfbfbfb) ) && (this->gcBasis!=NULL) )
564                idDelete((ideal*)&this->gcBasis);
565  #elif SIZEOF_LONG!=8
566        if(this->gcBasis!=(ideal)0xfbfbfbfb)
567                idDelete((ideal *)&this->gcBasis);
568  #endif
569#else
570        if(this->gcBasis!=NULL)
571                idDelete((ideal *)&this->gcBasis);
572#endif
573//         idDelete((ideal *)&this->gcBasis);
574//         if(this->inputIdeal!=NULL)
575//                 idDelete((ideal *)&this->inputIdeal);
576//         if (this->rootRing!=NULL && this->rootRing!=(ip_sring *)0xfefefefefefefefe)
577//                 rDelete(this->rootRing);
578        if(this->UCN!=1 && this->baseRing!=NULL)
579                rDelete(this->baseRing);
580        facet *fAct;
581        facet *fDel;
582        /*Delete the facet structure*/
583        fAct=this->facetPtr;
584        fDel=fAct;
585        while(fAct!=NULL)
586        {
587                fDel=fAct;
588                fAct=fAct->next;
589                delete fDel;
590        }
591        this->counter--;
592        //should be deleted in noRevS
593//         dd_FreeMatrix(this->ddFacets);
594        //dd_FreeMatrix(this->ddFacets);
595        for(int ii=0;ii<this->numRays;ii++)
596                delete(gcRays[ii]);
597        omFree(gcRays);
598}
599
600/** Returns the number of cones existing at the time*/
601inline int gcone::getCounter()
602{
603        return this->counter;
604}
605
606/** \brief Set the interior point of a cone */
607inline void gcone::setIntPoint(int64vec *iv)
608{
609        if(this->ivIntPt!=NULL)
610                delete this->ivIntPt;
611        this->ivIntPt=iv64Copy(iv);
612}
613
614/** \brief Returns either a physical copy the interior point of a cone or just a reference to it.*/
615inline int64vec *gcone::getIntPoint(bool shallow)
616{
617        if(shallow==TRUE)
618                return this->ivIntPt;
619        else
620                return iv64Copy(this->ivIntPt);
621}
622
623/** \brief Print the interior point */
624inline void gcone::showIntPoint()
625{
626        ivIntPt->show();
627}
628
629/** \brief Print facets
630 * This is mainly for debugging purposes. Usually called from within gdb
631 */
632volatile void gcone::showFacets(const short codim)
633{
634  #ifndef SING_NDEBUG
635        facet *f=this->facetPtr;
636        facet *f2=NULL;
637        if(codim==2)
638                f2=this->facetPtr->codim2Ptr;
639        while(f!=NULL)
640        {
641                int64vec *iv;
642                iv = f->getFacetNormal();
643                printf("(");iv->show(1,0);
644                if(f->isFlippable==FALSE)
645                        printf(")* ");
646                else
647                        printf(") ");
648                delete iv;
649                if(codim==2)
650                {
651                        f2=f->codim2Ptr;
652                        while(f2!=NULL)
653                        {
654                                printf("[");f2->getFacetNormal()->show(1,0);printf("]");
655                                f2 = f2->next;
656                        }
657                        printf("\n");
658                }
659                f=f->next;
660        }
661        printf("\n");
662  #endif
663}
664
665/** For debugging purposes only */
666static volatile void showSLA(facet &f)
667{
668  #ifndef SING_NDEBUG
669        facet *fAct;
670        fAct = &f;
671        if(fAct!=NULL)
672        {
673                facet *codim2Act;
674                codim2Act = fAct->codim2Ptr;
675
676                printf("\n");
677                while(fAct!=NULL)
678                {
679                        int64vec *fNormal;
680                        fNormal=fAct->getFacetNormal();
681                        printf("(");fNormal->show(1,0);
682                        if(fAct->isFlippable==TRUE)
683                                printf(") ");
684                        else
685                                printf(")* ");
686                        delete fNormal;
687                        codim2Act = fAct->codim2Ptr;
688                        printf(" Codim2: ");
689                        while(codim2Act!=NULL)
690                        {
691                                int64vec *f2Normal;
692                                f2Normal = codim2Act->getFacetNormal();
693                                printf("(");f2Normal->show(1,0);printf(") ");
694                                delete f2Normal;
695                                codim2Act = codim2Act->next;
696                        }
697                        printf("UCN = %i\n",fAct->getUCN());
698                        fAct = fAct->next;
699                }
700        }
701  #endif
702}
703
704static void idDebugPrint(const ideal &I)
705{
706  #ifndef SING_NDEBUG
707        int numElts=IDELEMS(I);
708        printf("Ideal with %i generators\n", numElts);
709        printf("Leading terms: ");
710        for (int ii=0;ii<numElts;ii++)
711        {
712                pWrite0(pHead(I->m[ii]));
713                printf(",");
714        }
715        printf("\n");
716  #endif
717}
718
719static void invPrint(const ideal &I)
720{
721//         int numElts=IDELEMS(I);
722//         cout << "inv = ";
723//         for(int ii=0;ii<numElts;ii++);
724//         {
725//                 pWrite0(pHead(I->m[ii]));
726//                 cout << ",";
727//         }
728//         cout << endl;
729}
730
731static bool isMonomial(const ideal &I)
732{
733        bool res = TRUE;
734        for(int ii=0;ii<IDELEMS(I);ii++)
735        {
736                if(pLength((poly)I->m[ii])>1)
737                {
738                        res = FALSE;
739                        break;
740                }
741        }
742        return res;
743}
744
745/** \brief Set gcone::numFacets */
746inline void gcone::setNumFacets()
747{
748}
749
750/** \brief Get gcone::numFacets */
751inline int gcone::getNumFacets()
752{
753        return this->numFacets;
754}
755
756inline int gcone::getUCN()
757{
758        if( this!=NULL)// && ( this!=(gcone * const)0xfbfbfbfbfbfbfbfb && this!=(gcone * const)0xfbfbfbfb ) )
759                return this->UCN;
760        else
761                return -1;
762}
763
764inline int gcone::getPredUCN()
765{
766        return this->pred;
767}
768/** Returns a copy of the this->baseRing */
769inline ring gcone::getBaseRing()
770{
771        return rCopy(this->baseRing);
772}
773
774inline void gcone::setBaseRing(ring r)
775{
776        this->baseRing=rCopy(r);
777}
778
779inline ring gcone::getRef2BaseRing()
780{
781        return this->baseRing;
782}
783
784/** \brief Compute the normals of the cone
785 *
786 * This method computes a representation of the cone in terms of facet normals. It takes an ideal
787 * as its input. Redundancies are automatically removed using cddlib's dd_MatrixCanonicalize.
788 * Other methods for redundancy checkings might be implemented later. See Anders' diss p.44.
789 * Note that in order to use cddlib a 0-th column has to be added to the matrix since cddlib expects
790 * each row to represent an inequality of type const+x1+...+xn <= 0. While computing the normals we come across
791 * the set \f$ \partial\mathcal{G} \f$ which we might store for later use. C.f p71 of journal
792 * As a result of this procedure the pointer facetPtr points to the first facet of the cone.
793 *
794 * Optionally, if the parameter bool compIntPoint is set to TRUE the method will also compute
795 * an interior point of the cone.
796                 */
797void gcone::getConeNormals(const ideal &I, bool compIntPoint)
798{
799#ifdef gfanp
800        timeval start, end;
801        gettimeofday(&start, 0);
802#endif
803        poly aktpoly;
804        int rows;                         // will contain the dimensions of the ineq matrix - deprecated by
805        dd_rowrange ddrows;
806        dd_colrange ddcols;
807        dd_rowset ddredrows;                // # of redundant rows in ddineq
808        dd_rowset ddlinset;                // the opposite
809        dd_rowindex ddnewpos=NULL;                  // all to make dd_Canonicalize happy
810        dd_NumberType ddnumb=dd_Integer; //Number type
811        dd_ErrorType dderr=dd_NoError;
812        //Compute the # inequalities i.e. rows of the matrix
813        rows=0; //Initialization
814        for (int ii=0;ii<IDELEMS(I);ii++)
815        {
816//                 aktpoly=(poly)I->m[ii];
817//                 rows=rows+pLength(aktpoly)-1;
818                rows=rows+pLength((poly)I->m[ii])-1;
819        }
820
821        dd_rowrange aktmatrixrow=0;        // needed to store the diffs of the expvects in the rows of ddineq
822        ddrows=rows;
823        ddcols=this->numVars;
824        dd_MatrixPtr ddineq;                 //Matrix to store the inequalities
825        ddineq=dd_CreateMatrix(ddrows,ddcols+1); //The first col has to be 0 since cddlib checks for additive consts there
826
827        // We loop through each g\in GB and compute the resulting inequalities
828        for (int i=0; i<IDELEMS(I); i++)
829        {
830                aktpoly=(poly)I->m[i];                //get aktpoly as i-th component of I
831                //simpler version of storing expvect diffs
832                int *leadexpv=(int*)omAlloc(((this->numVars)+1)*sizeof(int));
833                pGetExpV(aktpoly,leadexpv);
834                poly pNextTerm=aktpoly;
835                while(pNext(pNextTerm)/*pNext(aktpoly)*/!=NULL)
836                {
837                        pNextTerm/*aktpoly*/=pNext(pNextTerm);
838                        int *tailexpv=(int*)omAlloc(((this->numVars)+1)*sizeof(int));
839                        pGetExpV(pNextTerm,tailexpv);
840                        for(int kk=1;kk<=this->numVars;kk++)
841                        {
842                                dd_set_si(ddineq->matrix[(dd_rowrange)aktmatrixrow][kk],leadexpv[kk]-tailexpv[kk]);
843                        }
844                        aktmatrixrow += 1;
845                        omFree(tailexpv);
846                }
847                omFree(leadexpv);
848        } //for
849#if true
850        /*Let's make the preprocessing here. This could already be done in the above for-loop,
851        * but for a start it is more convenient here.
852        * We check the necessary condition of FJT p.18
853        * Quote: [...] every non-zero spoly should have at least one of its terms in inv(G)
854        */
855//         ideal initialForm=idInit(IDELEMS(I),1);
856//         int64vec *gamma=new int64vec(this->numVars);
857        int falseGammaCounter=0;
858        int *redRowsArray=NULL;
859        int num_alloc=0;
860        int num_elts=0;
861        for(int ii=0;ii<ddineq->rowsize;ii++)
862        {
863                ideal initialForm=idInit(IDELEMS(I),I->rank);
864                //read row ii into gamma
865//                 int64 tmp;
866                int64vec *gamma=new int64vec(this->numVars);
867                for(int jj=1;jj<=this->numVars;jj++)
868                {
869                        int64 tmp;
870                        tmp=(int64)mpq_get_d(ddineq->matrix[ii][jj]);
871                        (*gamma)[jj-1]=(int64)tmp;
872                }
873                computeInv((ideal&)I,initialForm,*gamma);
874                delete gamma;
875                //Create leading ideal
876                ideal L=idInit(IDELEMS(initialForm),1);
877                for(int jj=0;jj<IDELEMS(initialForm);jj++)
878                {
879                        poly p=pHead(initialForm->m[jj]);
880                        L->m[jj]=pCopy(/*pHead(initialForm->m[jj]))*/p);
881                        pDelete(&p);
882                }
883
884                LObject *P = new sLObject();//TODO What's the difference between sLObject and LObject?
885                memset(P,0,sizeof(LObject));
886
887                for(int jj=0;jj<=IDELEMS(initialForm)-2;jj++)
888                {
889                        bool isMaybeFacet=FALSE;
890                        P->p1=initialForm->m[jj];        //build spolys of initialForm in_v
891
892                        for(int kk=jj+1;kk<=IDELEMS(initialForm)-1;kk++)
893                        {
894                                P->p2=initialForm->m[kk];
895                                ksCreateSpoly(P);
896                                if(P->p!=NULL)        //spoly non zero=?
897                                {
898                                        poly p;//NOTE Don't use pInit here. Evil memleak will follow
899                                        poly q;
900                                        poly pDel,qDel;
901                                        p=pCopy(P->p);
902                                        q=pHead(p);        //Monomial q
903                                        pDelete(&q);
904                                        pDel=p; qDel=q;
905                                        isMaybeFacet=FALSE;
906                                        //TODO: Suffices to check LTs here
907                                        while(p!=NULL)
908                                        {
909                                                q=pHead(p);
910                                                for(int ll=0;ll<IDELEMS(L);ll++)
911                                                {
912                                                        if(pLmEqual(L->m[ll],q) || pDivisibleBy(L->m[ll],q))
913                                                        {
914                                                                isMaybeFacet=TRUE;
915                                                                 break;//for
916                                                        }
917                                                }
918                                                pDelete(&q);
919                                                if(isMaybeFacet==TRUE)
920                                                {
921                                                        break;//while(p!=NULL)
922                                                }
923                                                p=pNext(p);
924                                        }//while
925//                                         pDelete(&p);//NOTE Better to use pDel and qDel. Commenting in this line will not work!
926                                        if(q!=NULL) pDelete(&q);
927                                        pDelete(&pDel);
928                                        pDelete(&qDel);
929                                        if(isMaybeFacet==FALSE)
930                                        {
931                                                dd_set_si(ddineq->matrix[ii][0],1);
932//                                                 if(num_alloc==0)
933//                                                         num_alloc += 1;
934//                                                 else
935//                                                         num_alloc += 1;
936                                                if(num_alloc==num_elts)        num_alloc==0 ? num_alloc=1 : num_alloc*=2;
937
938                                                void *tmp = realloc(redRowsArray,(num_alloc*sizeof(int)));
939                                                if(!tmp)
940                                                {
941                                                        WerrorS("Woah dude! Couldn't realloc memory\n");
942                                                        exit(-1);
943                                                }
944                                                redRowsArray = (int*)tmp;
945                                                redRowsArray[num_elts]=ii;
946                                                num_elts++;
947                                                //break;//for(int kk, since we have found one that is not in L
948                                                goto _start;        //mea culpa, mea culpa, mea maxima culpa
949                                        }
950                                }//if(P->p!=NULL)
951                                pDelete(&(P->p));
952                        }//for k
953                }//for jj
954                _start:;
955                idDelete(&L);
956                delete P;
957                idDelete(&initialForm);
958        }//for(ii<ddineq-rowsize
959//         delete gamma;
960        int offset=0;//needed for correction of redRowsArray[ii]
961#ifndef SING_NDEBUG
962        printf("Removed %i of %i in preprocessing step\n",num_elts,ddineq->rowsize);
963#endif
964        for( int ii=0;ii<num_elts;ii++ )
965        {
966                dd_MatrixRowRemove(&ddineq,redRowsArray[ii]+1-offset);//cddlib sucks at enumeration
967                offset++;
968        }
969        free(redRowsArray);//NOTE May crash on some machines.
970        /*And now for the strictly positive rows
971        * Doesn't gain significant speedup
972        */
973        /*int *posRowsArray=NULL;
974        num_alloc=0;
975        num_elts=0;
976        for(int ii=0;ii<ddineq->rowsize;ii++)
977        {
978                int64vec *ivPos = new int64vec(this->numVars);
979                for(int jj=0;jj<this->numVars;jj++)
980                        (*ivPos)[jj]=(int)mpq_get_d(ddineq->matrix[ii][jj+1]);
981                bool isStrictlyPos=FALSE;
982                int posCtr=0;
983                for(int jj=0;jj<this->numVars;jj++)
984                {
985                        int64vec *ivCanonical = new int64vec(this->numVars);
986                        jj==0 ? (*ivCanonical)[ivPos->length()-1]=1 : (*ivCanonical)[jj-1]=1;
987                        if(dotProduct(*ivCanonical,*ivPos)!=0)
988                        {
989                                if ((*ivPos)[jj]>=0)
990                                {
991                                        posCtr++;
992                                }
993                        }
994                        delete ivCanonical;
995                }
996                if(posCtr==ivPos->length())
997                        isStrictlyPos=TRUE;
998                if(isStrictlyPos==TRUE)
999                {
1000                        if(num_alloc==0)
1001                                num_alloc += 1;
1002                        else
1003                                num_alloc += 1;
1004                        void *tmp = realloc(posRowsArray,(num_alloc*sizeof(int)));
1005                        if(!tmp)
1006                        {
1007                                WerrorS("Woah dude! Couldn't realloc memory\n");
1008                                exit(-1);
1009                        }
1010                        posRowsArray = (int*)tmp;
1011                        posRowsArray[num_elts]=ii;
1012                        num_elts++;
1013                }
1014                delete ivPos;
1015        }
1016        offset=0;
1017        for(int ii=0;ii<num_elts;ii++)
1018        {
1019                dd_MatrixRowRemove(&ddineq,posRowsArray[ii]+1-offset);
1020                offset++;
1021        }
1022        free(posRowsArray);*/
1023#endif
1024
1025        dd_MatrixCanonicalize(&ddineq, &ddlinset, &ddredrows, &ddnewpos, &dderr);
1026        ddrows = ddineq->rowsize;        //Size of the matrix with redundancies removed
1027        ddcols = ddineq->colsize;
1028
1029        this->ddFacets = dd_CopyMatrix(ddineq);
1030
1031        /*Write the normals into class facet*/
1032        facet *fAct;         //pointer to active facet
1033        int numNonFlip=0;
1034        for (int kk = 0; kk<ddrows; kk++)
1035        {
1036                int64 ggT=1;//NOTE Why does (int)mpq_get_d(ddineq->matrix[kk][1]) not work?
1037                int64vec *load = new int64vec(this->numVars);//int64vec to store a single facet normal that will then be stored via setFacetNormal
1038                for (int jj = 1; jj <ddcols; jj++)
1039                {
1040                        int64 val;
1041                        val = (int64)mpq_get_d(ddineq->matrix[kk][jj]);
1042                        (*load)[jj-1] = val;        //store typecasted entry at pos jj-1 of load
1043                        ggT = int64gcd(ggT,/*(int64&)foo*/val);
1044                }//for (int jj = 1; jj <ddcols; jj++)
1045                if(ggT>1)
1046                {
1047                        for(int ll=0;ll<this->numVars;ll++)
1048                                (*load)[ll] /= ggT;//make primitive vector
1049                }
1050                /*Quick'n'dirty hack for flippability. Executed only if gcone::hasHomInput==FALSE
1051                * Otherwise every facet intersects the positive orthant
1052                */
1053                if(gcone::hasHomInput==FALSE)
1054                {
1055                        //TODO: No dP needed
1056                        bool isFlip=FALSE;
1057                        for(int jj = 0; jj<load->length(); jj++)
1058                        {
1059//                                 int64vec *ivCanonical = new int64vec(load->length());
1060//                                 (*ivCanonical)[jj]=1;
1061//                                 if (dotProduct(*load,*ivCanonical)<0)
1062//                                 {
1063//                                         isFlip=TRUE;
1064//                                         break;        //URGHS
1065//                                 }
1066//                                 delete ivCanonical;
1067                                if((*load)[jj]<0)
1068                                {
1069                                        isFlip=TRUE;
1070                                        break;
1071                                }
1072                        }/*End of check for flippability*/
1073//                         if(iv64isStrictlyPositive(load))
1074//                                 isFlip=TRUE;
1075                        if(isFlip==FALSE)
1076                        {
1077                                this->numFacets++;
1078                                numNonFlip++;
1079                                if(this->numFacets==1)
1080                                {
1081                                        facet *fRoot = new facet();
1082                                        this->facetPtr = fRoot;
1083                                        fAct = fRoot;
1084                                }
1085                                else
1086                                {
1087                                        fAct->next = new facet();
1088                                        fAct = fAct->next;
1089                                }
1090                                fAct->isFlippable=FALSE;
1091                                fAct->setFacetNormal(load);
1092                                fAct->setUCN(this->getUCN());
1093#ifndef SING_NDEBUG
1094                                printf("Marking facet (");load->show(1,0);printf(") as non flippable\n");
1095#endif
1096                        }
1097                        else
1098                        {
1099                                this->numFacets++;
1100                                if(this->numFacets==1)
1101                                {
1102                                        facet *fRoot = new facet();
1103                                        this->facetPtr = fRoot;
1104                                        fAct = fRoot;
1105                                }
1106                                else
1107                                {
1108                                        fAct->next = new facet();
1109                                        fAct = fAct->next;
1110                                }
1111                                fAct->isFlippable=TRUE;
1112                                fAct->setFacetNormal(load);
1113                                fAct->setUCN(this->getUCN());
1114                        }
1115                }//hasHomInput==FALSE
1116                else        //Every facet is flippable
1117                {        /*Now load should be full and we can call setFacetNormal*/
1118                        this->numFacets++;
1119                        if(this->numFacets==1)
1120                        {
1121                                facet *fRoot = new facet();
1122                                this->facetPtr = fRoot;
1123                                fAct = fRoot;
1124                        }
1125                        else
1126                        {
1127                                fAct->next = new facet();
1128                                fAct = fAct->next;
1129                        }
1130                        fAct->isFlippable=TRUE;
1131                        fAct->setFacetNormal(load);
1132                        fAct->setUCN(this->getUCN());
1133                }//if (isFlippable==FALSE)
1134                delete load;
1135        }//for (int kk = 0; kk<ddrows; kk++)
1136
1137        //In cases like I=<x-1,y-1> there are only non-flippable facets...
1138        if(numNonFlip==this->numFacets)
1139        {
1140                WerrorS ("Only non-flippable facets. Terminating...\n");
1141//                 exit(-1);//Bit harsh maybe...
1142        }
1143
1144        /*
1145        Now we should have a linked list containing the facet normals of those facets that are
1146        -irredundant
1147        -flipable
1148        Adressing is done via *facetPtr
1149        */
1150        if (compIntPoint==TRUE)
1151        {
1152                int64vec *iv = new int64vec(this->numVars);
1153                dd_MatrixPtr posRestr=dd_CreateMatrix(this->numVars,this->numVars+1);
1154                int jj=1;
1155                for (int ii=0;ii<=this->numVars;ii++)
1156                {
1157                        dd_set_si(posRestr->matrix[ii][jj],1);
1158                        jj++;
1159                }
1160                dd_MatrixAppendTo(&ddineq,posRestr);
1161                interiorPoint(ddineq, *iv);        //NOTE ddineq contains non-flippable facets
1162                this->setIntPoint(iv);        //stores the interior point in gcone::ivIntPt
1163                delete iv;
1164                dd_FreeMatrix(posRestr);
1165        }
1166        //Clean up but don't delete the return value!
1167        //dd_FreeMatrix(ddineq);
1168        set_free(ddredrows);//check
1169        set_free(ddlinset);//check
1170        //free(ddnewpos);//<-- NOTE Here the crash occurs omAlloc issue?
1171#ifdef gfanp
1172        gettimeofday(&end, 0);
1173        time_getConeNormals += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
1174#endif
1175
1176}//gcone::getConeNormals(ideal I)
1177
1178/** \brief Compute the (codim-2)-facets of a given cone
1179 * This method is used during noRevS
1180 * Additionally we check whether the codim2-facet normal is strictly positive. Otherwise
1181 * the facet is marked as non-flippable.
1182 */
1183void gcone::getCodim2Normals(const gcone &gc)
1184{
1185#ifdef gfanp
1186        timeval start, end;
1187          gettimeofday(&start, 0);
1188#endif
1189        //this->facetPtr->codim2Ptr = new facet(2);        //instantiate a (codim-2)-facet
1190        facet *fAct;
1191        fAct = this->facetPtr;
1192        facet *codim2Act;
1193        //codim2Act = this->facetPtr->codim2Ptr;
1194        dd_MatrixPtr ddineq;//,P,ddakt;
1195        dd_ErrorType err;
1196        //ddineq = facets2Matrix(gc);        //get a matrix representation of the cone
1197        ddineq = dd_CopyMatrix(gc.ddFacets);
1198        /*Now set appropriate linearity*/
1199        for (int ii=0; ii<this->numFacets; ii++)
1200        {
1201                dd_rowset impl_linset, redset;
1202                dd_rowindex newpos;
1203                dd_MatrixPtr ddakt;
1204                  ddakt = dd_CopyMatrix(ddineq);
1205//                 ddakt->representation=dd_Inequality;        //Not using this makes it faster. But why does the quick check below still work?
1206//                 ddakt->representation=dd_Generator;
1207                set_addelem(ddakt->linset,ii+1);/*Now set appropriate linearity*/
1208#ifdef gfanp
1209                timeval t_ddMC_start, t_ddMC_end;
1210                gettimeofday(&t_ddMC_start,0);
1211#endif
1212                 //dd_MatrixCanonicalize(&ddakt, &impl_linset, &redset, &newpos, &err);
1213                dd_PolyhedraPtr ddpolyh;
1214                ddpolyh=dd_DDMatrix2Poly(ddakt, &err);
1215//                 ddpolyh=dd_DDMatrix2Poly2(ddakt, dd_MaxCutoff, &err);
1216                dd_MatrixPtr P;
1217                P=dd_CopyGenerators(ddpolyh);
1218                dd_FreePolyhedra(ddpolyh);
1219                //TODO Call for one cone , normalize - check equalities - plus lineality -done
1220#ifdef gfanp
1221                gettimeofday(&t_ddMC_end,0);
1222                t_ddMC += (t_ddMC_end.tv_sec - t_ddMC_start.tv_sec + 1e-6*(t_ddMC_end.tv_usec - t_ddMC_start.tv_usec));
1223#endif
1224                /* We loop through each row of P normalize it by making all
1225                * entries integer ones and add the resulting vector to the
1226                * int matrix facet::codim2Facets */
1227                for (int jj=1;jj<=/*ddakt*/P->rowsize;jj++)
1228                {
1229                        fAct->numCodim2Facets++;
1230                        if(fAct->numCodim2Facets==1)
1231                        {
1232                                fAct->codim2Ptr = new facet(2);
1233                                codim2Act = fAct->codim2Ptr;
1234                        }
1235                        else
1236                        {
1237                                codim2Act->next = new facet(2);
1238                                codim2Act = codim2Act->next;
1239                        }
1240                        int64vec *n = new int64vec(this->numVars);
1241#ifdef gfanp
1242                        timeval t_mI_start, t_mI_end;
1243                        gettimeofday(&t_mI_start,0);
1244#endif
1245                        makeInt(P,jj,*n);
1246                        /*for(int kk=0;kk<this->numVars;kk++)
1247                        {
1248                                int foo;
1249                                foo = (int)mpq_get_d(ddakt->matrix[ii][kk+1]);
1250                                (*n)[kk]=foo;
1251                        }*/
1252#ifdef gfanp
1253                        gettimeofday(&t_mI_end,0);
1254                        t_mI += (t_mI_end.tv_sec - t_mI_start.tv_sec + 1e-6*(t_mI_end.tv_usec - t_mI_start.tv_usec));
1255#endif
1256                        codim2Act->setFacetNormal(n);
1257                        delete n;
1258                }
1259                /*We check whether the facet spanned by the codim-2 facets
1260                * intersects with the positive orthant. Otherwise we define this
1261                * facet to be non-flippable. Works since we set the appropriate
1262                * linearity for ddakt above.
1263                */
1264                //TODO It might be faster to compute jus the implied equations instead of a relative interior point
1265//                 int64vec *iv_intPoint = new int64vec(this->numVars);
1266//                 dd_MatrixPtr shiftMatrix;
1267//                 dd_MatrixPtr intPointMatrix;
1268//                 shiftMatrix = dd_CreateMatrix(this->numVars,this->numVars+1);
1269//                 for(int kk=0;kk<this->numVars;kk++)
1270//                 {
1271//                         dd_set_si(shiftMatrix->matrix[kk][0],1);
1272//                         dd_set_si(shiftMatrix->matrix[kk][kk+1],1);
1273//                 }
1274//                 intPointMatrix=dd_MatrixAppend(ddakt,shiftMatrix);
1275// #ifdef gfanp
1276//                 timeval t_iP_start, t_iP_end;
1277//                 gettimeofday(&t_iP_start, 0);
1278// #endif
1279//                 interiorPoint(intPointMatrix,*iv_intPoint);
1280// //                 dd_rowset impl_linste,lbasis;
1281// //                 dd_LPSolutionPtr lps=NULL;
1282// //                 dd_ErrorType err;
1283// //                 dd_FindRelativeInterior(intPointMatrix, &impl_linset, &lbasis, &lps, &err);
1284// #ifdef gfanp
1285//                 gettimeofday(&t_iP_end, 0);
1286//                 t_iP += (t_iP_end.tv_sec - t_iP_start.tv_sec + 1e-6*(t_iP_end.tv_usec - t_iP_start.tv_usec));
1287// #endif
1288//                 for(int ll=0;ll<this->numVars;ll++)
1289//                 {
1290//                         if( (*iv_intPoint)[ll] < 0 )
1291//                         {
1292//                                 fAct->isFlippable=FALSE;
1293//                                 break;
1294//                         }
1295//                 }
1296                /*End of check*/
1297                /*This test should be way less time consuming*/
1298#ifdef gfanp
1299                timeval t_iP_start, t_iP_end;
1300                gettimeofday(&t_iP_start, 0);
1301#endif
1302                bool containsStrictlyPosRay=TRUE;
1303                for(int ii=0;ii<ddakt->rowsize;ii++)
1304                {
1305                        containsStrictlyPosRay=TRUE;
1306                        for(int jj=1;jj<this->numVars;jj++)
1307                        {
1308                                if(ddakt->matrix[ii][jj]<=0)
1309                                {
1310                                        containsStrictlyPosRay=FALSE;
1311                                        break;
1312                                }
1313                        }
1314                        if(containsStrictlyPosRay==TRUE)
1315                                break;
1316                }
1317                if(containsStrictlyPosRay==FALSE)
1318                //TODO Not sufficient. Intersect with pos orthant for pos int
1319                        fAct->isFlippable=FALSE;
1320#ifdef gfanp
1321                gettimeofday(&t_iP_end, 0);
1322                t_iP += (t_iP_end.tv_sec - t_iP_start.tv_sec + 1e-6*(t_iP_end.tv_usec - t_iP_start.tv_usec));
1323#endif
1324                /**/
1325                fAct = fAct->next;
1326                dd_FreeMatrix(ddakt);
1327                dd_FreeMatrix(P);
1328        }//for
1329        dd_FreeMatrix(ddineq);
1330#ifdef gfanp
1331        gettimeofday(&end, 0);
1332        time_getCodim2Normals += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
1333#endif
1334}
1335
1336/** Really extremal rays this time ;)
1337* Extremal rays are unique modulo the homogeneity space.
1338* Therefore we dd_MatrixAppend gc->ddFacets and gcone::dd_LinealitySpace
1339* into ddineq. Next we compute the extremal rays of the so given subspace.
1340* Figuring out whether a ray belongs to a given facet(normal) is done by
1341* checking whether the inner product of the ray with the normal is zero.
1342* We use ivAdd here which returns a new int64vec. Therefore we need to avoid
1343* a memory leak which would be cause by the line
1344* iv=ivAdd(iv,b)
1345* So we keep pointer tmp to iv and delete(tmp), so there should not occur a
1346* memleak
1347* TODO normalization
1348*/
1349void gcone::getExtremalRays(const gcone &gc)
1350{
1351#ifdef gfanp
1352        timeval start, end;
1353        gettimeofday(&start, 0);
1354        timeval poly_start, poly_end;
1355        gettimeofday(&poly_start,0);
1356#endif
1357        //Add lineality space - dd_LinealitySpace
1358        dd_MatrixPtr ddineq;
1359        dd_ErrorType err;
1360        ddineq = (dd_LinealitySpace->rowsize>0) ? dd_AppendMatrix(gc.ddFacets,gcone::dd_LinealitySpace) : dd_CopyMatrix(gc.ddFacets);
1361        /* In case the input is non-homogeneous we add constrains for the positive orthant.
1362        * This is justified by the fact that for non-homog ideals we only consider the
1363        * restricted fan. This way we can be sure to find strictly positive interior points.
1364        * This in turn makes life easy when checking for flippability!
1365        * Drawback: Makes the LP larger so probably slows down computations a wee bit.
1366        */
1367        dd_MatrixPtr ddPosRestr;
1368        if(hasHomInput==FALSE)
1369        {
1370                dd_MatrixPtr tmp;
1371                ddPosRestr=dd_CreateMatrix(this->numVars,this->numVars+1);
1372                for(int ii=0;ii<this->numVars;ii++)
1373                        dd_set_si(ddPosRestr->matrix[ii][ii+1],1);
1374                dd_MatrixAppendTo(&ddineq,ddPosRestr);
1375                assert(ddineq);
1376                dd_FreeMatrix(ddPosRestr);
1377        }
1378        dd_PolyhedraPtr ddPolyh;
1379        ddPolyh = dd_DDMatrix2Poly(ddineq, &err);
1380        dd_MatrixPtr P;
1381        P=dd_CopyGenerators(ddPolyh);//Here we actually compute the rays!
1382        dd_FreePolyhedra(ddPolyh);
1383        dd_FreeMatrix(ddineq);
1384#ifdef gfanp
1385        gettimeofday(&poly_end,0);
1386        t_ddPolyh += (poly_end.tv_sec - poly_start.tv_sec + 1e-6*(poly_end.tv_usec - poly_start.tv_usec));
1387#endif
1388        /* Compute interior point on the fly*/
1389        int64vec *ivIntPointOfCone = new int64vec(this->numVars);
1390        int64vec *foo = new int64vec(this->numVars);
1391        for(int ii=0;ii<P->rowsize;ii++)
1392        {
1393                int64vec *tmp = ivIntPointOfCone;
1394                makeInt(P,ii+1,*foo);
1395                ivIntPointOfCone = iv64Add(ivIntPointOfCone,foo);
1396                delete tmp;
1397        }
1398        delete foo;
1399        int64 ggT=(*ivIntPointOfCone)[0];
1400        for (int ii=0;ii<(this->numVars);ii++)
1401        {
1402                if( (*ivIntPointOfCone)[ii]>INT_MAX )
1403                        WarnS("Interior point exceeds INT_MAX!\n");
1404                //Compute intgcd
1405                ggT=int64gcd(ggT,(*ivIntPointOfCone)[ii]);
1406        }
1407
1408        //Divide out a common gcd > 1
1409        if(ggT>1)
1410        {
1411                for(int ii=0;ii<this->numVars;ii++)
1412                {
1413                        (*ivIntPointOfCone)[ii] /= ggT;
1414                        if( (*ivIntPointOfCone)[ii]>INT_MAX ) WarnS("Interior point still exceeds INT_MAX after GCD!\n");
1415                }
1416        }
1417
1418        /*For homogeneous input (like Det3,3,5) the int points may be negative. So add a suitable multiple of (1,_,1)*/
1419        if(hasHomInput==TRUE && iv64isStrictlyPositive(ivIntPointOfCone)==FALSE)
1420        {
1421                int64vec *ivOne = new int64vec(this->numVars);
1422                int maxNegEntry=0;
1423                for(int ii=0;ii<this->numVars;ii++)
1424                {
1425//                         (*ivOne)[ii]=1;
1426                        if ((*ivIntPointOfCone)[ii]<maxNegEntry) maxNegEntry=(*ivIntPointOfCone)[ii];
1427                }
1428                maxNegEntry *= -1;
1429                maxNegEntry++;//To be on the safe side
1430                for(int ii=0;ii<this->numVars;ii++)
1431                        (*ivOne)[ii]=maxNegEntry;
1432                int64vec *tmp=ivIntPointOfCone;
1433                ivIntPointOfCone=iv64Add(ivIntPointOfCone,ivOne);
1434                delete(tmp);
1435//                 while( !iv64isStrictlyPositive(ivIntPointOfCone) )
1436//                 {
1437//                         int64vec *tmp = ivIntPointOfCone;
1438//                         for(int jj=0;jj<this->numVars;jj++)
1439//                                 (*ivOne)[jj] = (*ivOne)[jj] << 1; //times 2
1440//                         ivIntPointOfCone = ivAdd(ivIntPointOfCone,ivOne);
1441//                         delete tmp;
1442//                 }
1443                delete ivOne;
1444                int64 ggT=(*ivIntPointOfCone)[0];
1445                for(int ii=0;ii<this->numVars;ii++)
1446                        ggT=int64gcd( ggT, (*ivIntPointOfCone)[ii]);
1447                if(ggT>1)
1448                {
1449                        for(int jj=0;jj<this->numVars;jj++)
1450                                (*ivIntPointOfCone)[jj] /= ggT;
1451                }
1452        }
1453//         assert(iv64isStrictlyPositive(ivIntPointOfCone));
1454
1455        this->setIntPoint(ivIntPointOfCone);
1456        delete(ivIntPointOfCone);
1457        /* end of interior point computation*/
1458
1459        //Loop through the rows of P and check whether fNormal*row[i]=0 => row[i] belongs to fNormal
1460        int rows=P->rowsize;
1461        facet *fAct=gc.facetPtr;
1462        //Construct an array to hold the extremal rays of the cone
1463        this->gcRays = (int64vec**)omAlloc0(sizeof(int64vec*)*P->rowsize);
1464        for(int ii=0;ii<P->rowsize;ii++)
1465        {
1466                int64vec *rowvec = new int64vec(this->numVars);
1467                makeInt(P,ii+1,*rowvec);//get an integer entry instead of rational, rowvec is primitve
1468                this->gcRays[ii] = iv64Copy(rowvec);
1469                delete rowvec;
1470        }
1471        this->numRays=P->rowsize;
1472        //Check which rays belong to which facet
1473        while(fAct!=NULL)
1474        {
1475                const int64vec *fNormal;// = new int64vec(this->numVars);
1476                fNormal = fAct->getRef2FacetNormal();//->getFacetNormal();
1477                int64vec *ivIntPointOfFacet = new int64vec(this->numVars);
1478                for(int ii=0;ii<rows;ii++)
1479                {
1480                         if(dotProduct(*fNormal,this->gcRays[ii])==0)
1481                        {
1482                                int64vec *tmp = ivIntPointOfFacet;//Prevent memleak
1483                                fAct->numCodim2Facets++;
1484                                facet *codim2Act;
1485                                if(fAct->numCodim2Facets==1)
1486                                {
1487                                        fAct->codim2Ptr = new facet(2);
1488                                        codim2Act = fAct->codim2Ptr;
1489                                }
1490                                else
1491                                {
1492                                        codim2Act->next = new facet(2);
1493                                        codim2Act = codim2Act->next;
1494                                }
1495                                //codim2Act->setFacetNormal(rowvec);
1496                                //Rather just let codim2Act point to the corresponding int64vec of gcRays
1497                                codim2Act->fNormal=this->gcRays[ii];
1498                                fAct->numRays++;
1499                                //Memleak avoided via tmp
1500                                ivIntPointOfFacet=iv64Add(ivIntPointOfFacet,this->gcRays[ii]);
1501                                //Now tmp still points to the OLD address of ivIntPointOfFacet
1502                                delete(tmp);
1503
1504                        }
1505                }//For non-homog input ivIntPointOfFacet should already be >0 here
1506//                 if(!hasHomInput) {assert(iv64isStrictlyPositive(ivIntPointOfFacet));}
1507                //if we have no strictly pos ray but the input is homogeneous
1508                //then add a suitable multiple of (1,...,1)
1509                if( !iv64isStrictlyPositive(ivIntPointOfFacet) && hasHomInput==TRUE)
1510                {
1511                        int64vec *ivOne = new int64vec(this->numVars);
1512                        for(int ii=0;ii<this->numVars;ii++)
1513                                (*ivOne)[ii]=1;
1514                        while( !iv64isStrictlyPositive(ivIntPointOfFacet) )
1515                        {
1516                                int64vec *tmp = ivIntPointOfFacet;
1517                                for(int jj=0;jj<this->numVars;jj++)
1518                                {
1519                                        (*ivOne)[jj] = (*ivOne)[jj] << 1; //times 2
1520                                }
1521                                ivIntPointOfFacet = iv64Add(ivIntPointOfFacet/*diff*/,ivOne);
1522                                delete tmp;
1523                        }
1524                        delete ivOne;
1525                }
1526                int64 ggT=(*ivIntPointOfFacet)[0];
1527                for(int ii=0;ii<this->numVars;ii++)
1528                        ggT=int64gcd(ggT,(*ivIntPointOfFacet)[ii]);
1529                if(ggT>1)
1530                {
1531                        for(int ii=0;ii<this->numVars;ii++)
1532                                (*ivIntPointOfFacet)[ii] /= ggT;
1533                }
1534                fAct->setInteriorPoint(ivIntPointOfFacet);
1535
1536                delete(ivIntPointOfFacet);
1537                //Now (if we have at least 3 variables) do a bubblesort on the rays
1538                /*if(this->numVars>2)
1539                {
1540                        facet *A[fAct->numRays-1];
1541                        facet *f2Act=fAct->codim2Ptr;
1542                        for(unsigned ii=0;ii<fAct->numRays;ii++)
1543                        {
1544                                A[ii]=f2Act;
1545                                f2Act=f2Act->next;
1546                        }
1547                        bool exchanged=FALSE;
1548                        unsigned n=fAct->numRays-1;
1549                        do
1550                        {
1551                                exchanged=FALSE;//n=fAct->numRays-1;
1552                                for(unsigned ii=0;ii<=n-1;ii++)
1553                                {
1554                                        if((A[ii]->fNormal)->compare((A[ii+1]->fNormal))==1)
1555                                        {
1556                                                //Swap rays
1557                                                cout << "Swapping ";
1558                                                A[ii]->fNormal->show(1,0); cout << " with "; A[ii+1]->fNormal->show(1,0); cout << endl;
1559                                                A[ii]->next=A[ii+1]->next;
1560                                                if(ii>0)
1561                                                        A[ii-1]->next=A[ii+1];
1562                                                A[ii+1]->next=A[ii];
1563                                                if(ii==0)
1564                                                        fAct->codim2Ptr=A[ii+1];
1565                                                //end swap
1566                                                facet *tmp=A[ii];//swap in list
1567                                                A[ii+1]=A[ii];
1568                                                A[ii]=tmp;
1569//                                                 tmp=NULL;
1570                                        }
1571                                }
1572                                n--;
1573                        }while(exchanged==TRUE && n>=0);
1574                }*///if pVariables>2
1575//                 delete fNormal;
1576                fAct = fAct->next;
1577        }//end of facet checking
1578        dd_FreeMatrix(P);
1579        //Now all extremal rays should be set w.r.t their respective fNormal
1580        //TODO Not sufficient -> vol2 II/125&127
1581        //NOTE Sufficient according to cddlibs doc. These ARE rays
1582        //What the hell... let's just take interior points
1583        if(gcone::hasHomInput==FALSE)
1584        {
1585                fAct=gc.facetPtr;
1586                while(fAct!=NULL)
1587                {
1588//                         bool containsStrictlyPosRay=FALSE;
1589//                         facet *codim2Act;
1590//                         codim2Act = fAct->codim2Ptr;
1591//                         while(codim2Act!=NULL)
1592//                         {
1593//                                 int64vec *rayvec;
1594//                                 rayvec = codim2Act->getFacetNormal();//Mind this is no normal but a ray!
1595//                                 //int negCtr=0;
1596//                                 if(iv64isStrictlyPositive(rayvec))
1597//                                 {
1598//                                         containsStrictlyPosRay=TRUE;
1599//                                         delete(rayvec);
1600//                                         break;
1601//                                 }
1602//                                 delete(rayvec);
1603//                                 codim2Act = codim2Act->next;
1604//                         }
1605//                         if(containsStrictlyPosRay==FALSE)
1606//                                 fAct->isFlippable=FALSE;
1607                        if(!iv64isStrictlyPositive(fAct->interiorPoint))
1608                                fAct->isFlippable=FALSE;
1609                        fAct = fAct->next;
1610                }
1611        }//hasHomInput?
1612#ifdef gfanp
1613        gettimeofday(&end, 0);
1614        t_getExtremalRays += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
1615#endif
1616}
1617
1618/** Order the spanning rays in a lex way hopefully using qsort()*/
1619void gcone::orderRays()
1620{
1621//   qsort(gcRays,sizeof(int64vec),int64vec::compare);
1622}
1623
1624inline bool gcone::iv64isStrictlyPositive(const int64vec * iv64)
1625{
1626        bool res=TRUE;
1627        for(int ii=0;ii<iv64->length();ii++)
1628        {
1629                if((*iv64)[ii]<=0)
1630                {
1631                        res=FALSE;
1632                        break;
1633                }
1634        }
1635        return res;
1636}
1637
1638/** \brief Compute the Groebner Basis on the other side of a shared facet
1639 *
1640 * Implements algorithm 4.3.2 from Anders' thesis.
1641 * As shown there it is not necessary to compute an interior point. The knowledge of the facet normal
1642 * suffices. A term \f$ x^\gamma \f$ of \f$ g \f$ is in \f$  in_\omega(g) \f$ iff \f$ \gamma - leadexp(g)\f$
1643 * is parallel to \f$ leadexp(g) \f$
1644 * Parallelity is checked using basic linear algebra. See gcone::isParallel.
1645 * Other possibilities include computing the rank of the matrix consisting of the vectors in question and
1646 * computing an interior point of the facet and taking all terms having the same weight with respect
1647 * to this interior point.
1648 *\param ideal, facet
1649 * Input: a marked,reduced Groebner basis and a facet
1650 */
1651inline void gcone::flip(ideal gb, facet *f)                //Compute "the other side"
1652{
1653#ifdef gfanp
1654        timeval start, end;
1655        gettimeofday(&start, 0);
1656#endif
1657        int64vec *fNormal;// = new int64vec(this->numVars);        //facet normal, check for parallelity
1658        fNormal = f->getFacetNormal();        //read this->fNormal;
1659#ifndef SING_NDEBUG
1660//         std::cout << "running gcone::flip" << std::endl;
1661        printf("flipping UCN %i over facet",this->getUCN());
1662        fNormal->show(1,0);
1663        printf(") with UCN %i\n",f->getUCN() );
1664#endif
1665        if(this->getUCN() != f->getUCN())
1666        {
1667                WerrorS("Uh oh... Trying to flip over facet with incompatible UCN");
1668                exit(-1);
1669        }
1670        /*1st step: Compute the initial ideal*/
1671        /*poly initialFormElement[IDELEMS(gb)];*/        //array of #polys in GB to store initial form
1672        ideal initialForm=idInit(IDELEMS(gb),this->gcBasis->rank);
1673
1674        computeInv(gb,initialForm,*fNormal);
1675
1676#ifndef SING_NDEBUG
1677/*        cout << "Initial ideal is: " << endl;
1678        idShow(initialForm);
1679        //f->printFlipGB();*/
1680//         cout << "===" << endl;
1681#endif
1682        /*2nd step: lift initial ideal to a GB of the neighbouring cone using minus alpha as weight*/
1683        /*Substep 2.1
1684        compute $G_{-\alpha}(in_v(I))
1685        see journal p. 66
1686        NOTE Check for different rings. Prolly it will not always be necessary to add a weight, if the
1687        srcRing already has a weighted ordering
1688        */
1689        ring srcRing=currRing;
1690        ring tmpRing;
1691
1692        if(  /*(*/ srcRing->order[0]!=ringorder_a /*)*/ )
1693        {
1694                int64vec *iv;// = new int64vec(this->numVars);
1695                iv = ivNeg(fNormal);//ivNeg uses iv64Copy -> new
1696//                 tmpRing=rCopyAndAddWeight(srcRing,ivNeg(fNormal));
1697                tmpRing=rCopyAndAddWeight(srcRing,iv);
1698                delete iv;
1699        }
1700        else
1701        {
1702                tmpRing=rCopy0(srcRing);
1703                int length=fNormal->length();
1704                int *A=(int *)omAlloc0(length*sizeof(int));
1705                for(int jj=0;jj<length;jj++)
1706                {
1707                        A[jj]=-(*fNormal)[jj];
1708                }
1709                omFree(tmpRing->wvhdl[0]);
1710                tmpRing->wvhdl[0]=(int*)A;
1711                tmpRing->block1[0]=length;
1712                rComplete(tmpRing);
1713                //omFree(A);
1714        }
1715          delete fNormal;
1716        rChangeCurrRing(tmpRing);
1717
1718        ideal ina;
1719        ina=idrCopyR(initialForm,srcRing);
1720        idDelete(&initialForm);
1721        ideal H;
1722//          H=kStd(ina,NULL,isHomog,NULL);        //we know it is homogeneous
1723#ifdef gfanp
1724        timeval t_kStd_start, t_kStd_end;
1725        gettimeofday(&t_kStd_start,0);
1726#endif
1727        if(gcone::hasHomInput==TRUE)
1728                H=kStd(ina,NULL,isHomog,NULL/*,gcone::hilbertFunction*/);
1729        else
1730                H=kStd(ina,NULL,isNotHomog,NULL);        //This is \mathcal(G)_{>_-\alpha}(in_v(I))
1731#ifdef gfanp
1732        gettimeofday(&t_kStd_end, 0);
1733        t_kStd += (t_kStd_end.tv_sec - t_kStd_start.tv_sec + 1e-6*(t_kStd_end.tv_usec - t_kStd_start.tv_usec));
1734#endif
1735        idSkipZeroes(H);
1736        idDelete(&ina);
1737
1738        /*Substep 2.2
1739        do the lifting and mark according to H
1740        */
1741        rChangeCurrRing(srcRing);
1742        ideal srcRing_H;
1743        ideal srcRing_HH;
1744        srcRing_H=idrCopyR(H,tmpRing);
1745        //H is needed further below, so don't idDelete here
1746         srcRing_HH=ffG(srcRing_H,this->gcBasis);
1747         idDelete(&srcRing_H);
1748
1749        /*Substep 2.2.1
1750         * Mark according to G_-\alpha
1751         * Here we have a minimal basis srcRing_HH. In order to turn this basis into a reduced basis
1752         * we have to compute an interior point of C(srcRing_HH). For this we need to know the cone
1753         * represented by srcRing_HH MARKED ACCORDING TO G_{-\alpha}
1754         * Thus we check whether the leading monomials of srcRing_HH and srcRing_H coincide. If not we
1755         * compute the difference accordingly
1756        */
1757#ifdef gfanp
1758        timeval t_markings_start, t_markings_end;
1759        gettimeofday(&t_markings_start, 0);
1760#endif
1761        bool markingsAreCorrect=FALSE;
1762        dd_MatrixPtr intPointMatrix;
1763        int iPMatrixRows=0;
1764        dd_rowrange aktrow=0;
1765        for (int ii=0;ii<IDELEMS(srcRing_HH);ii++)
1766        {
1767                poly aktpoly=(poly)srcRing_HH->m[ii];//This is a pointer, so don't pDelete
1768                iPMatrixRows = iPMatrixRows+pLength(aktpoly);
1769        }
1770        /* additionally one row for the standard-simplex and another for a row that becomes 0 during
1771         * construction of the differences
1772         */
1773        intPointMatrix = dd_CreateMatrix(iPMatrixRows+2,this->numVars+1);
1774        intPointMatrix->numbtype=dd_Integer;        //NOTE: DO NOT REMOVE OR CHANGE TO dd_Rational
1775
1776        for (int ii=0;ii<IDELEMS(srcRing_HH);ii++)
1777        {
1778                markingsAreCorrect=FALSE;        //crucial to initialise here
1779                poly aktpoly=srcRing_HH->m[ii]; //Only a pointer, so don't pDelete
1780                /*Comparison of leading monomials is done via exponent vectors*/
1781                for (int jj=0;jj<IDELEMS(H);jj++)
1782                {
1783                        int *src_ExpV = (int *)omAlloc((this->numVars+1)*sizeof(int));
1784                        int *dst_ExpV = (int *)omAlloc((this->numVars+1)*sizeof(int));
1785                        pGetExpV(aktpoly,src_ExpV);
1786                        rChangeCurrRing(tmpRing);        //this ring change is crucial!
1787                        poly p=pCopy(H->m[ii]);
1788                        pGetExpV(p/*pCopy(H->m[ii])*/,dst_ExpV);
1789                        pDelete(&p);
1790                        rChangeCurrRing(srcRing);
1791                        bool expVAreEqual=TRUE;
1792                        for (int kk=1;kk<=this->numVars;kk++)
1793                        {
1794#ifndef SING_NDEBUG
1795//                                 cout << src_ExpV[kk] << "," << dst_ExpV[kk] << endl;
1796#endif
1797                                if (src_ExpV[kk]!=dst_ExpV[kk])
1798                                {
1799                                        expVAreEqual=FALSE;
1800                                }
1801                        }
1802                        if (expVAreEqual==TRUE)
1803                        {
1804                                markingsAreCorrect=TRUE; //everything is fine
1805#ifndef SING_NDEBUG
1806//                                 cout << "correct markings" << endl;
1807#endif
1808                        }//if (pHead(aktpoly)==pHead(H->m[jj])
1809                        omFree(src_ExpV);
1810                        omFree(dst_ExpV);
1811                }//for (int jj=0;jj<IDELEMS(H);jj++)
1812
1813                int *leadExpV=(int *)omAlloc((this->numVars+1)*sizeof(int));
1814                if (markingsAreCorrect==TRUE)
1815                {
1816                        pGetExpV(aktpoly,leadExpV);
1817                }
1818                else
1819                {
1820                        rChangeCurrRing(tmpRing);
1821                        pGetExpV(pHead(H->m[ii]),leadExpV); //We use H->m[ii] as leading monomial
1822                        rChangeCurrRing(srcRing);
1823                }
1824                /*compute differences of the expvects*/
1825                while (pNext(aktpoly)!=NULL)
1826                {
1827                        int *v=(int *)omAlloc((this->numVars+1)*sizeof(int));
1828                        /*The following if-else-block makes sure the first term (i.e. the wrongly marked term)
1829                        is not omitted when computing the differences*/
1830                        if(markingsAreCorrect==TRUE)
1831                        {
1832                                aktpoly=pNext(aktpoly);
1833                                pGetExpV(aktpoly,v);
1834                        }
1835                        else
1836                        {
1837                                pGetExpV(pHead(aktpoly),v);
1838                                markingsAreCorrect=TRUE;
1839                        }
1840                        int ctr=0;
1841                        for (int jj=0;jj<this->numVars;jj++)
1842                        {
1843                                /*Store into ddMatrix*/
1844                                if(leadExpV[jj+1]-v[jj+1])
1845                                        ctr++;
1846                                dd_set_si(intPointMatrix->matrix[aktrow][jj+1],leadExpV[jj+1]-v[jj+1]);
1847                        }
1848                        /*It ought to be more sensible to avoid 0-rows in the first place*/
1849//                         if(ctr==this->numVars)//We have a 0-row
1850//                                 dd_MatrixRowRemove(&intPointMatrix,aktrow);
1851//                         else
1852                                aktrow +=1;
1853                        omFree(v);
1854                }
1855                omFree(leadExpV);
1856        }//for (int ii=0;ii<IDELEMS(srcRing_HH);ii++)
1857#ifdef gfanp
1858        gettimeofday(&t_markings_end, 0);
1859        t_markings += (t_markings_end.tv_sec - t_markings_start.tv_sec + 1e-6*(t_markings_end.tv_usec - t_markings_start.tv_usec));
1860#endif
1861        /*Now it is safe to idDelete(H)*/
1862        idDelete(&H);
1863        /*Preprocessing goes here since otherwise we would delete the constraint
1864        * for the standard simplex.
1865        */
1866        preprocessInequalities(intPointMatrix);
1867        /*Now we add the constraint for the standard simplex*/
1868//         dd_set_si(intPointMatrix->matrix[aktrow][0],-1);
1869//         for (int jj=1;jj<=this->numVars;jj++)
1870//         {
1871//                 dd_set_si(intPointMatrix->matrix[aktrow][jj],1);
1872//         }
1873        //Let's make sure we compute interior points from the positive orthant
1874//         dd_MatrixPtr posRestr=dd_CreateMatrix(this->numVars,this->numVars+1);
1875//
1876//         int jj=1;
1877//         for (int ii=0;ii<this->numVars;ii++)
1878//         {
1879//                 dd_set_si(posRestr->matrix[ii][jj],1);
1880//                 jj++;
1881//         }
1882        /*We create a matrix containing the standard simplex
1883        * and constraints to assure a strictly positive point
1884        * is computed */
1885        dd_MatrixPtr posRestr = dd_CreateMatrix(this->numVars+1, this->numVars+1);
1886        for(int ii=0;ii<posRestr->rowsize;ii++)
1887        {
1888                if(ii==0)
1889                {
1890                        dd_set_si(posRestr->matrix[ii][0],-1);
1891                        for(int jj=1;jj<=this->numVars;jj++)
1892                                dd_set_si(posRestr->matrix[ii][jj],1);
1893                }
1894                else
1895                {
1896                        /** Set all variables to \geq 1/10. YMMV but this choice is pretty equal*/
1897                        dd_set_si2(posRestr->matrix[ii][0],-1,2);
1898                        dd_set_si(posRestr->matrix[ii][ii],1);
1899                }
1900        }
1901        dd_MatrixAppendTo(&intPointMatrix,posRestr);
1902        dd_FreeMatrix(posRestr);
1903
1904        int64vec *iv_weight = new int64vec(this->numVars);
1905#ifdef gfanp
1906        timeval t_dd_start, t_dd_end;
1907        gettimeofday(&t_dd_start, 0);
1908#endif
1909        dd_ErrorType err;
1910        dd_rowset implLin, redrows;
1911        dd_rowindex newpos;
1912
1913        //NOTE Here we should remove interiorPoint and instead
1914        // create and ordering like (a(omega),a(fNormal),dp)
1915//         if(this->ivIntPt==NULL)
1916                interiorPoint(intPointMatrix, *iv_weight);        //iv_weight now contains the interior point
1917//         else
1918//                 iv_weight=this->getIntPoint();
1919        dd_FreeMatrix(intPointMatrix);
1920        /*Crude attempt for interior point */
1921        /*dd_PolyhedraPtr ddpolyh;
1922        dd_ErrorType err;
1923        dd_rowset impl_linset,redset;
1924        dd_rowindex newpos;
1925        dd_MatrixCanonicalize(&intPointMatrix, &impl_linset, &redset, &newpos, &err);
1926        ddpolyh=dd_DDMatrix2Poly(intPointMatrix, &err);
1927        dd_MatrixPtr P;
1928        P=dd_CopyGenerators(ddpolyh);
1929        dd_FreePolyhedra(ddpolyh);
1930        for(int ii=0;ii<P->rowsize;ii++)
1931        {
1932                 int64vec *iv_row=new int64vec(this->numVars);
1933                makeInt(P,ii+1,*iv_row);
1934                iv_weight =ivAdd(iv_weight, iv_row);
1935                delete iv_row;
1936        }
1937        dd_FreeMatrix(P);
1938        dd_FreeMatrix(intPointMatrix);*/
1939#ifdef gfanp
1940        gettimeofday(&t_dd_end, 0);
1941        t_dd += (t_dd_end.tv_sec - t_dd_start.tv_sec + 1e-6*(t_dd_end.tv_usec - t_dd_start.tv_usec));
1942#endif
1943
1944        /*Step 3
1945        * turn the minimal basis into a reduced one */
1946        // NOTE May assume that at this point srcRing already has 3 blocks of orderins, starting with a
1947        // Thus:
1948        //ring dstRing=rCopyAndChangeWeight(srcRing,iv_weight);
1949        ring dstRing=rCopy0(tmpRing);
1950        int length=iv_weight->length();
1951        int *A=(int *)omAlloc0(length*sizeof(int));
1952        for(int jj=0;jj<length;jj++)
1953        {
1954                A[jj]=(*iv_weight)[jj];
1955        }
1956        dstRing->wvhdl[0]=(int*)A;
1957        rComplete(dstRing);
1958        rChangeCurrRing(dstRing);
1959         rDelete(tmpRing);
1960        delete iv_weight;
1961
1962        ideal dstRing_I;
1963        dstRing_I=idrCopyR(srcRing_HH,srcRing);
1964        idDelete(&srcRing_HH); //Hmm.... causes trouble - no more
1965        //dstRing_I=idrCopyR(inputIdeal,srcRing);
1966        BITSET save=test;
1967        test|=Sy_bit(OPT_REDSB);
1968        test|=Sy_bit(OPT_REDTAIL);
1969#ifndef SING_NDEBUG
1970//         test|=Sy_bit(6);        //OPT_DEBUG
1971#endif
1972        ideal tmpI;
1973        //NOTE Any of the two variants of tmpI={idrCopy(),dstRing_I} does the trick
1974        //tmpI = idrCopyR(this->inputIdeal,this->baseRing);
1975        tmpI = dstRing_I;
1976#ifdef gfanp
1977        gettimeofday(&t_kStd_start,0);
1978#endif
1979        if(gcone::hasHomInput==TRUE)
1980                dstRing_I=kStd(tmpI,NULL,isHomog,NULL/*,gcone::hilbertFunction*/);
1981        else
1982                dstRing_I=kStd(tmpI,NULL,isNotHomog,NULL);
1983#ifdef gfanp
1984        gettimeofday(&t_kStd_end, 0);
1985        t_kStd += (t_kStd_end.tv_sec - t_kStd_start.tv_sec + 1e-6*(t_kStd_end.tv_usec - t_kStd_start.tv_usec));
1986#endif
1987        idDelete(&tmpI);
1988        idNorm(dstRing_I);
1989//         kInterRed(dstRing_I);
1990        idSkipZeroes(dstRing_I);
1991        test=save;
1992        /*End of step 3 - reduction*/
1993
1994        f->setFlipGB(dstRing_I);//store the flipped GB
1995//         idDelete(&dstRing_I);
1996        f->flipRing=rCopy(dstRing);        //store the ring on the other side
1997#ifndef SING_NDEBUG
1998         printf("Flipped GB is UCN %i:\n",counter+1);
1999         idDebugPrint(dstRing_I);
2000        printf("\n");
2001#endif
2002        idDelete(&dstRing_I);
2003        rChangeCurrRing(srcRing);        //return to the ring we started the computation of flipGB in
2004         rDelete(dstRing);
2005#ifdef gfanp
2006        gettimeofday(&end, 0);
2007        time_flip += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
2008#endif
2009}//void flip(ideal gb, facet *f)
2010
2011/** \brief A slightly different approach to flipping
2012* Here we use the fact that in_v(in_u(I))=in_(u+eps*v)(I). Therefore, we do no longer
2013* need to compute an interior point and run BBA on the minimal basis but we can rather
2014* use the ordering (a(omega),a(fNormal),dp)
2015* The second parameter facet *f must not be const since we need to store f->flipGB
2016* Problem: Assume we start in a cone with ordering (dp,C). Then \f$ in_\omega(I) \f$
2017* will be from a ring with (a(),dp,C) and our resulting cone from (a(),a(),dp,C). Hence a way
2018* must be found to circumvent the sequence of a()'s growing to a ridiculous size.
2019* Therefore: We use (a(),a(),dp,C) for the computation of the reduced basis. But then we
2020* do have an interior point of the cone by adding the extremal rays. So we replace
2021* the latter cone by a cone with (a(sum_of_rays),dp,C).
2022* Con: It's incredibly ugly
2023* Pro: No messing around with readConeFromFile()
2024* Is there a way to construct a vector from \f$ \omega \f$ and the facet normal?
2025*/
2026inline void gcone::flip2(const ideal &gb, facet *f)
2027{
2028#ifdef gfanp
2029        timeval start, end;
2030        gettimeofday(&start, 0);
2031#endif
2032        const int64vec *fNormal;
2033        fNormal = f->getRef2FacetNormal();/*->getFacetNormal();*/        //read this->fNormal;
2034#ifndef SING_NDEBUG
2035//         printf("flipping UCN %i over facet(",this->getUCN());
2036//         fNormal->show(1,0);
2037//         printf(") with UCN %i\n",f->getUCN());
2038#endif
2039        if(this->getUCN() != f->getUCN())
2040        {        printf("%i vs %i\n",this->getUCN(), f->getUCN() );
2041                WerrorS("Uh oh... Trying to flip over facet with incompatible UCN");
2042                exit(-1);
2043        }
2044        /*1st step: Compute the initial ideal*/
2045        ideal initialForm=idInit(IDELEMS(gb),this->gcBasis->rank);
2046        computeInv( gb, initialForm, *fNormal );
2047        ring srcRing=currRing;
2048        ring tmpRing;
2049
2050        const int64vec *intPointOfFacet;
2051        intPointOfFacet=f->getInteriorPoint();
2052        //Now we need two blocks of ringorder_a!
2053        //May assume the same situation as in flip() here
2054        if( (srcRing->order[0]!=ringorder_a/*64*/) && (srcRing->order[1]!=ringorder_a/*64*/) )
2055        {
2056                int64vec *iv = new int64vec(this->numVars);//init with 1s, since we do not need a 2nd block here but later
2057//                 int64vec *iv_foo = new int64vec(this->numVars,1);//placeholder
2058                int64vec *ivw = ivNeg(const_cast<int64vec*>(fNormal));
2059                tmpRing=rCopyAndAddWeight2(srcRing,ivw/*intPointOfFacet*/,iv);
2060                delete iv;delete ivw;
2061//                 delete iv_foo;
2062        }
2063        else
2064        {
2065                int64vec *iv=new int64vec(this->numVars);
2066                int64vec *ivw=ivNeg(const_cast<int64vec*>(fNormal));
2067                tmpRing=rCopyAndAddWeight2(srcRing,ivw,iv);
2068                delete iv; delete ivw;
2069                /*tmpRing=rCopy0(srcRing);
2070                int length=fNormal->length();
2071                int *A1=(int *)omAlloc0(length*sizeof(int));
2072                int *A2=(int *)omAlloc0(length*sizeof(int));
2073                for(int jj=0;jj<length;jj++)
2074                {
2075                        A1[jj] = -(*fNormal)[jj];
2076                        A2[jj] = 1;//-(*fNormal)[jj];//NOTE Do we need this here? This is only the facet ideal
2077                }
2078                omFree(tmpRing->wvhdl[0]);
2079                if(tmpRing->wvhdl[1]!=NULL)
2080                        omFree(tmpRing->wvhdl[1]);
2081                tmpRing->wvhdl[0]=(int*)A1;
2082                tmpRing->block1[0]=length;
2083                tmpRing->wvhdl[1]=(int*)A2;
2084                tmpRing->block1[1]=length;
2085                rComplete(tmpRing);*/
2086        }
2087//         delete fNormal; //NOTE Do not delete when using getRef2FacetNormal();
2088        rChangeCurrRing(tmpRing);
2089        //Now currRing should have (a(),a(),dp,C)
2090        ideal ina;
2091        ina=idrCopyR(initialForm,srcRing);
2092        idDelete(&initialForm);
2093        ideal H;
2094#ifdef gfanp
2095        timeval t_kStd_start, t_kStd_end;
2096        gettimeofday(&t_kStd_start,0);
2097#endif
2098        BITSET save=test;
2099        test|=Sy_bit(OPT_REDSB);
2100        test|=Sy_bit(OPT_REDTAIL);
2101//         if(gcone::hasHomInput==TRUE)
2102                H=kStd(ina,NULL,testHomog/*isHomog*/,NULL/*,gcone::hilbertFunction*/);
2103//         else
2104//                 H=kStd(ina,NULL,isNotHomog,NULL);        //This is \mathcal(G)_{>_-\alpha}(in_v(I))
2105        test=save;
2106#ifdef gfanp
2107        gettimeofday(&t_kStd_end, 0);
2108        t_kStd += (t_kStd_end.tv_sec - t_kStd_start.tv_sec + 1e-6*(t_kStd_end.tv_usec - t_kStd_start.tv_usec));
2109#endif
2110        idSkipZeroes(H);
2111        idDelete(&ina);
2112
2113        rChangeCurrRing(srcRing);
2114        ideal srcRing_H;
2115        ideal srcRing_HH;
2116        srcRing_H=idrCopyR(H,tmpRing);
2117        //H is needed further below, so don't idDelete here
2118        srcRing_HH=ffG(srcRing_H,this->gcBasis);
2119        idDelete(&srcRing_H);
2120        //Now BBA(srcRing_HH) with (a(),a(),dp)
2121        /* Evil modification of currRing */
2122        ring dstRing=rCopy0(tmpRing);
2123        int length=this->numVars;
2124        int *A1=(int *)omAlloc0(length*sizeof(int));
2125        int *A2=(int *)omAlloc0(length*sizeof(int));
2126        const int64vec *ivw=f->getRef2FacetNormal();
2127        for(int jj=0;jj<length;jj++)
2128        {
2129                A1[jj] = (*intPointOfFacet)[jj];
2130                A2[jj] = -(*ivw)[jj];//TODO Or minus (*ivw)[ii] ??? NOTE minus
2131        }
2132        omFree(dstRing->wvhdl[0]);
2133        if(dstRing->wvhdl[1]!=NULL)
2134                omFree(dstRing->wvhdl[1]);
2135        dstRing->wvhdl[0]=(int*)A1;
2136        dstRing->block1[0]=length;
2137        dstRing->wvhdl[1]=(int*)A2;
2138        dstRing->block1[1]=length;
2139        rComplete(dstRing);
2140        rChangeCurrRing(dstRing);
2141        ideal dstRing_I;
2142        dstRing_I=idrCopyR(srcRing_HH,srcRing);
2143        idDelete(&srcRing_HH); //Hmm.... causes trouble - no more
2144        save=test;
2145        test|=Sy_bit(OPT_REDSB);
2146        test|=Sy_bit(OPT_REDTAIL);
2147        ideal tmpI;
2148        tmpI = dstRing_I;
2149#ifdef gfanp
2150//         timeval t_kStd_start, t_kStd_end;
2151        gettimeofday(&t_kStd_start,0);
2152#endif
2153//         if(gcone::hasHomInput==TRUE)
2154//                 dstRing_I=kStd(tmpI,NULL,isHomog,NULL/*,gcone::hilbertFunction*/);
2155//         else
2156                dstRing_I=kStd(tmpI,NULL,testHomog,NULL);
2157#ifdef gfanp
2158        gettimeofday(&t_kStd_end, 0);
2159        t_kStd += (t_kStd_end.tv_sec - t_kStd_start.tv_sec + 1e-6*(t_kStd_end.tv_usec - t_kStd_start.tv_usec));
2160#endif
2161        idDelete(&tmpI);
2162        idNorm(dstRing_I);
2163        idSkipZeroes(dstRing_I);
2164        test=save;
2165        /*End of step 3 - reduction*/
2166
2167        f->setFlipGB(dstRing_I);
2168        f->flipRing=rCopy(dstRing);
2169        rDelete(tmpRing);
2170        rDelete(dstRing);
2171        //Now we should have dstRing with (a(),a(),dp,C)
2172        //This must be replaced with (a(),dp,C) BEFORE gcTmp is actually added to the list
2173        //of cones in noRevS
2174        rChangeCurrRing(srcRing);
2175#ifdef gfanp
2176        gettimeofday(&end, 0);
2177        time_flip2 += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
2178#endif
2179}//flip2
2180
2181/** \brief Compute initial ideal
2182 * Compute the initial ideal in_v(G) wrt a (possible) facet normal
2183 * used in gcone::getFacetNormal in order to preprocess possible facet normals
2184 * and in gcone::flip for obvious reasons.
2185*/
2186/*inline*/ void gcone::computeInv(const ideal &gb, ideal &initialForm, const int64vec &fNormal)
2187{
2188#ifdef gfanp
2189        timeval start, end;
2190        gettimeofday(&start, 0);
2191#endif
2192        for (int ii=0;ii<IDELEMS(gb);ii++)
2193        {
2194                poly initialFormElement;
2195                poly aktpoly = (poly)gb->m[ii];//Ptr, so don't pDelete(aktpoly)
2196                int *leadExpV=(int *)omAlloc((this->numVars+1)*sizeof(int));
2197                pGetExpV(aktpoly,leadExpV);        //find the leading exponent in leadExpV[1],...,leadExpV[n], use pNext(p)
2198                initialFormElement=pHead(aktpoly);
2199//                 int *v=(int *)omAlloc((this->numVars+1)*sizeof(int));
2200                while(pNext(aktpoly)!=NULL)        /*loop trough terms and check for parallelity*/
2201                {
2202                        int64vec *check = new int64vec(this->numVars);
2203                        aktpoly=pNext(aktpoly);        //next term
2204                        int *v=(int *)omAlloc((this->numVars+1)*sizeof(int));
2205                        pGetExpV(aktpoly,v);
2206                        /* Convert (int)v into (int64vec)check */
2207//                         bool notPar=FALSE;
2208                        for (int jj=0;jj<this->numVars;jj++)
2209                        {
2210                                (*check)[jj]=v[jj+1]-leadExpV[jj+1];
2211//                                 register int64 foo=(fNormal)[jj];
2212//                                 if( ( (*check)[jj]  == /*fNormal[jj]*/foo )
2213//                                  || ( (/*fNormal[jj]*/foo!=0) && ( ( (*check)[jj] % /*fNormal[jj]*/foo ) !=0 ) ) )
2214//                                 {
2215//                                         notPar=TRUE;
2216//                                         break;
2217//                                 }
2218                        }
2219                        omFree(v);
2220                        if (isParallel(*check,fNormal))//Found a parallel vector. Add it
2221//                          if(notPar==FALSE)
2222                        {
2223                                initialFormElement = pAdd((initialFormElement),(poly)pHead(aktpoly));//pAdd = p_Add_q destroys args
2224                        }
2225                        delete check;
2226                }//while
2227//                 omFree(v);
2228#ifndef SING_NDEBUG
2229//                  cout << "Initial Form=";
2230//                  pWrite(initialFormElement[ii]);
2231//                  cout << "---" << endl;
2232#endif
2233                /*Now initialFormElement must be added to (ideal)initialForm */
2234                initialForm->m[ii]=pCopy(initialFormElement);
2235                pDelete(&initialFormElement);
2236                omFree(leadExpV);
2237        }//for
2238#ifdef gfanp
2239        gettimeofday(&end, 0);
2240        time_computeInv += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
2241#endif
2242}
2243
2244/** \brief Compute the remainder of a polynomial by a given ideal
2245 *
2246 * Compute \f$ f^{\mathcal{G}} \f$
2247 * Algorithm is taken from Cox, Little, O'Shea, IVA 2nd Ed. p 62
2248 * However, since we are only interested in the remainder, there is no need to
2249 * compute the factors \f$ a_i \f$
2250 */
2251//NOTE: Should be replaced by kNF or kNF2
2252//NOTE: Done
2253//NOTE: removed with r12286
2254
2255/** \brief Compute \f$ f-f^{\mathcal{G}} \f$
2256*/
2257//NOTE: use kNF or kNF2 instead of restOfDivision
2258inline ideal gcone::ffG(const ideal &H, const ideal &G)
2259{
2260        int size=IDELEMS(H);
2261        ideal res=idInit(size,1);
2262        for (int ii=0;ii<size;ii++)
2263        {
2264//                 poly temp1;//=pInit();
2265//                 poly temp2;//=pInit();
2266                poly temp3;//=pInit();//polys to temporarily store values for pSub
2267//                 res->m[ii]=pCopy(kNF(G, NULL,H->m[ii],0,0));
2268//                  temp1=pCopy(H->m[ii]);//TRY
2269//                 temp2=pCopy(res->m[ii]);
2270                //NOTE if gfanHeuristic=0 (sic!) this results in dPolyErrors - mon from wrong ring
2271//                  temp2=pCopy(kNF(G, NULL,H->m[ii],0,0));//TRY
2272//                  temp3=pSub(temp1, temp2);//TRY
2273                temp3=pSub(pCopy(H->m[ii]),pCopy(kNF(G,NULL,H->m[ii],0,0)));//NOTRY
2274                res->m[ii]=pCopy(temp3);
2275                //res->m[ii]=pSub(temp1,temp2); //buggy
2276                //cout << "res->m["<<ii<<"]=";pWrite(res->m[ii]);
2277//                 pDelete(&temp1);//TRY
2278//                 pDelete(&temp2);
2279                pDelete(&temp3);
2280        }
2281        return res;
2282}
2283
2284/** \brief Preprocessing of inequlities
2285* Do some preprocessing on the matrix of inequalities
2286* 1) Replace several constraints on the pos. orthants by just one for each orthant
2287* 2) Remove duplicates of inequalities
2288* 3) Remove inequalities that arise as sums of other inequalities
2289*/
2290void gcone::preprocessInequalities(dd_MatrixPtr &ddineq)
2291{
2292/*        int *posRowsArray=NULL;
2293        int num_alloc=0;
2294        int num_elts=0;
2295         int offset=0;*/
2296        //Remove zeroes (and strictly pos rows?)
2297        for(int ii=0;ii<ddineq->rowsize;ii++)
2298        {
2299                int64vec *iv = new int64vec(this->numVars);
2300                int64vec *ivNull = new int64vec(this->numVars);//Needed for intvec64::compare(*int64vec)
2301                int posCtr=0;
2302                for(int jj=0;jj<this->numVars;jj++)
2303                {
2304                        (*iv)[jj]=(int)mpq_get_d(ddineq->matrix[ii][jj+1]);
2305                        if((*iv)[jj]>0)//check for strictly pos rows
2306                                posCtr++;
2307                        //Behold! This will delete the row for the standard simplex!
2308                }
2309//                 if( (iv->compare(0)==0) || (posCtr==iv->length()) )
2310                if( (posCtr==iv->length()) || (iv->compare(ivNull)==0) )
2311                {
2312                        dd_MatrixRowRemove(&ddineq,ii+1);
2313                        ii--;//Yes. This is on purpose
2314                }
2315                delete iv;
2316                delete ivNull;
2317        }
2318        //Remove duplicates of rows
2319//         posRowsArray=NULL;
2320//         num_alloc=0;
2321//         num_elts=0;
2322//         offset=0;
2323//         int num_newRows = ddineq->rowsize;
2324//         for(int ii=0;ii<ddineq->rowsize-1;ii++)
2325//         for(int ii=0;ii<num_newRows-1;ii++)
2326//         {
2327//                 int64vec *iv = new int64vec(this->numVars);//1st vector to check against
2328//                 for(int jj=0;jj<this->numVars;jj++)
2329//                         (*iv)[jj]=(int)mpq_get_d(ddineq->matrix[ii][jj+1]);
2330//                 for(int jj=ii+1;jj</*ddineq->rowsize*/num_newRows;jj++)
2331//                 {
2332//                         int64vec *ivCheck = new int64vec(this->numVars);//Checked against iv
2333//                         for(int kk=0;kk<this->numVars;kk++)
2334//                                 (*ivCheck)[kk]=(int)mpq_get_d(ddineq->matrix[jj][kk+1]);
2335//                         if (iv->compare(ivCheck)==0)
2336//                         {
2337// //                                 cout << "=" << endl;
2338// //                                 num_alloc++;
2339// //                                 void *tmp=realloc(posRowsArray,(num_alloc*sizeof(int)));
2340// //                                 if(!tmp)
2341// //                                 {
2342// //                                         WerrorS("Woah dude! Couldn't realloc memory\n");
2343// //                                         exit(-1);
2344// //                                 }
2345// //                                 posRowsArray = (int*)tmp;
2346// //                                 posRowsArray[num_elts]=jj;
2347// //                                 num_elts++;
2348//                                 dd_MatrixRowRemove(&ddineq,jj+1);
2349//                                 num_newRows = ddineq->rowsize;
2350//                         }
2351//                         delete ivCheck;
2352//                 }
2353//                 delete iv;
2354//         }
2355//         for(int ii=0;ii<num_elts;ii++)
2356//         {
2357//                 dd_MatrixRowRemove(&ddineq,posRowsArray[ii]+1-offset);
2358//                 offset++;
2359//         }
2360//         free(posRowsArray);
2361        //Apply Thm 2.1 of JOTA Vol 53 No 1 April 1987*/
2362}//preprocessInequalities
2363
2364/** \brief Compute a Groebner Basis
2365 *
2366 * Computes the Groebner basis and stores the result in gcone::gcBasis
2367 *\param ideal
2368 *\return void
2369 */
2370inline void gcone::getGB(const ideal &inputIdeal)
2371{
2372        BITSET save=test;
2373        test|=Sy_bit(OPT_REDSB);
2374        test|=Sy_bit(OPT_REDTAIL);
2375        ideal gb;
2376        gb=kStd(inputIdeal,NULL,testHomog,NULL);
2377        idNorm(gb);
2378        idSkipZeroes(gb);
2379        this->gcBasis=gb;        //write the GB into gcBasis
2380        test=save;
2381}//void getGB
2382
2383/** \brief Compute the negative of a given int64vec
2384        */
2385static int64vec* ivNeg(/*const*/int64vec *iv)
2386{        //Hm, switching to int64vec const int64vec does no longer work
2387        int64vec *res;// = new int64vec(iv->length());
2388        res=iv64Copy(iv);
2389        *res *= (int)-1;
2390        return res;
2391}
2392
2393
2394/** \brief Compute the dot product of two intvecs
2395*
2396*/
2397static int dotProduct(const int64vec &iva, const int64vec &ivb)
2398{
2399        int res=0;
2400        for (int i=0;i<pVariables;i++)
2401        {
2402// #ifndef SING_NDEBUG
2403//         (const_cast<int64vec*>(&iva))->show(1,0); (const_cast<int64vec*>(&ivb))->show(1,0);
2404// #endif
2405                res = res+(iva[i]*ivb[i]);
2406        }
2407        return res;
2408}
2409/** \brief Check whether two intvecs are parallel
2410 *
2411 * \f$ \alpha\parallel\beta\Leftrightarrow\langle\alpha,\beta\rangle^2=\langle\alpha,\alpha\rangle\langle\beta,\beta\rangle \f$
2412 */
2413static bool isParallel(const int64vec &a,const int64vec &b)
2414{
2415        bool res;
2416        int lhs=dotProduct(a,b)*dotProduct(a,b);
2417        int rhs=dotProduct(a,a)*dotProduct(b,b);
2418        return res = (lhs==rhs)?TRUE:FALSE;
2419}
2420
2421/** \brief Compute an interior point of a given cone
2422 * Result will be written into int64vec iv.
2423 * Any rational point is automatically converted into an integer.
2424 */
2425void gcone::interiorPoint( dd_MatrixPtr &M, int64vec &iv) //no const &M here since we want to remove redundant rows
2426{
2427        dd_LPPtr lp,lpInt;
2428        dd_ErrorType err=dd_NoError;
2429        dd_LPSolverType solver=dd_DualSimplex;
2430        dd_LPSolutionPtr lpSol=NULL;
2431//         dd_rowset ddlinset,ddredrows;        //needed for dd_FindRelativeInterior
2432//          dd_rowindex ddnewpos;
2433        dd_NumberType numb;
2434        //M->representation=dd_Inequality;
2435
2436        //NOTE: Make this n-dimensional!
2437        //dd_set_si(M->rowvec[0],1);dd_set_si(M->rowvec[1],1);dd_set_si(M->rowvec[2],1);
2438
2439        /*NOTE: Leave the following line commented out!
2440        * Otherwise it will slow down computations a great deal
2441        * */
2442//         dd_MatrixCanonicalizeLinearity(&M, &ddlinset, &ddnewpos, &err);
2443        //if (err!=dd_NoError){cout << "Error during dd_MatrixCanonicalize" << endl;}
2444        dd_MatrixPtr posRestr=dd_CreateMatrix(this->numVars,this->numVars+1);
2445        int jj=1;
2446        for (int ii=0;ii<this->numVars;ii++)
2447        {
2448                dd_set_si(posRestr->matrix[ii][jj],1);
2449                jj++;
2450        }
2451        dd_MatrixAppendTo(&M,posRestr);
2452        dd_FreeMatrix(posRestr);
2453        lp=dd_Matrix2LP(M, &err);
2454        if (err!=dd_NoError){WerrorS("Error during dd_Matrix2LP in gcone::interiorPoint");}
2455        if (lp==NULL){WerrorS("LP is NULL");}
2456#ifndef SING_NDEBUG
2457//                        dd_WriteLP(stdout,lp);
2458#endif
2459
2460        lpInt=dd_MakeLPforInteriorFinding(lp);
2461        if (err!=dd_NoError){WerrorS("Error during dd_MakeLPForInteriorFinding in gcone::interiorPoint");}
2462#ifndef SING_NDEBUG
2463//                         dd_WriteLP(stdout,lpInt);
2464#endif
2465//         dd_FindRelativeInterior(M,&ddlinset,&ddredrows,&lpSol,&err);
2466        if (err!=dd_NoError)
2467        {
2468                WerrorS("Error during dd_FindRelativeInterior in gcone::interiorPoint");
2469                dd_WriteErrorMessages(stdout, err);
2470        }
2471        dd_LPSolve(lpInt,solver,&err);        //This will not result in a point from the relative interior
2472//         if (err!=dd_NoError){WerrorS("Error during dd_LPSolve");}
2473        lpSol=dd_CopyLPSolution(lpInt);
2474//         if (err!=dd_NoError){WerrorS("Error during dd_CopyLPSolution");}
2475#ifndef SING_NDEBUG
2476        printf("Interior point: ");
2477        for (int ii=1; ii<(lpSol->d)-1;ii++)
2478        {
2479                dd_WriteNumber(stdout,lpSol->sol[ii]);
2480        }
2481        printf("\n");
2482#endif
2483        //NOTE The following strongly resembles parts of makeInt.
2484        //Maybe merge sometimes
2485        mpz_t kgV; mpz_init(kgV);
2486        mpz_set_str(kgV,"1",10);
2487        mpz_t den; mpz_init(den);
2488        mpz_t tmp; mpz_init(tmp);
2489        mpq_get_den(tmp,lpSol->sol[1]);
2490        for (int ii=1;ii<(lpSol->d)-1;ii++)
2491        {
2492                mpq_get_den(den,lpSol->sol[ii+1]);
2493                mpz_lcm(kgV,tmp,den);
2494                mpz_set(tmp, kgV);
2495        }
2496        mpq_t qkgV;
2497        mpq_init(qkgV);
2498        mpq_set_z(qkgV,kgV);
2499        for (int ii=1;ii<(lpSol->d)-1;ii++)
2500        {
2501                mpq_t product;
2502                mpq_init(product);
2503                mpq_mul(product,qkgV,lpSol->sol[ii]);
2504                iv[ii-1]=(int)mpz_get_d(mpq_numref(product));
2505                mpq_clear(product);
2506        }
2507#ifndef SING_NDEBUG
2508//                         iv.show();
2509//                         cout << endl;
2510#endif
2511        mpq_clear(qkgV);
2512        mpz_clear(tmp);
2513        mpz_clear(den);
2514        mpz_clear(kgV);
2515
2516        dd_FreeLPSolution(lpSol);
2517        dd_FreeLPData(lpInt);
2518        dd_FreeLPData(lp);
2519//         set_free(ddlinset);
2520//         set_free(ddredrows);
2521
2522}//void interiorPoint(dd_MatrixPtr const &M)
2523
2524/** Computes an interior point of a cone by taking two interior points a,b from two different facets
2525* and then computing b+(a-b)/2
2526* Of course this only works for flippable facets
2527* Two cases may occur:
2528* 1st: There are only two facets who share the only strictly positive ray
2529* 2nd: There are at least two facets which have a distinct positive ray
2530* In the former case we use linear algebra to determine an interior point,
2531* in the latter case we simply add up the two rays
2532*
2533* Way too bad! The case may occur that the cone is spanned by three rays, of which only two are strictly
2534* positive => these lie in a plane and thus their sum is not from relative interior.
2535* So let's just sum up all rays, find one strictly positive and shift the point along that ray
2536*
2537* Used by noRevS
2538*NOTE no longer used nor maintained. MM Mar 9, 2010
2539*/
2540// void gcone::interiorPoint2()
2541// {//idPrint(this->gcBasis);
2542// #ifndef SING_NDEBUG
2543//         if(this->ivIntPt!=NULL)
2544//                 WarnS("Interior point already exists - ovrewriting!");
2545// #endif
2546//         facet *f1 = this->facetPtr;
2547//         facet *f2 = NULL;
2548//         int64vec *intF1=NULL;
2549//         while(f1!=NULL)
2550//         {
2551//                 if(f1->isFlippable)
2552//                 {
2553//                         facet *f1Ray = f1->codim2Ptr;
2554//                         while(f1Ray!=NULL)
2555//                         {
2556//                                 const int64vec *check=f1Ray->getRef2FacetNormal();
2557//                                 if(iv64isStrictlyPositive(check))
2558//                                 {
2559//                                         intF1=iv64Copy(check);
2560//                                         break;
2561//                                 }
2562//                                 f1Ray=f1Ray->next;
2563//                         }
2564//                 }
2565//                 if(intF1!=NULL)
2566//                         break;
2567//                 f1=f1->next;
2568//         }
2569//         if(f1!=NULL && f1->next!=NULL)//Choose another facet, different from f1
2570//                 f2=f1->next;
2571//         else
2572//                 f2=this->facetPtr;
2573//         if(intF1==NULL && hasHomInput==TRUE)
2574//         {
2575//                 intF1 = new int64vec(this->numVars);
2576//                 for(int ii=0;ii<this->numVars;ii++)
2577//                         (*intF1)[ii]=1;
2578//         }
2579//         assert(f1); assert(f2);
2580//         int64vec *intF2=f2->getInteriorPoint();
2581//         mpq_t *qPosRay = new mpq_t[this->numVars];//The positive ray from above
2582//         mpq_t *qIntPt = new mpq_t[this->numVars];//starting vector a+((b-a)/2)
2583//         mpq_t *qPosIntPt = new mpq_t[this->numVars];//This should be >0 eventually
2584//         for(int ii=0;ii<this->numVars;ii++)
2585//         {
2586//                 mpq_init(qPosRay[ii]);
2587//                 mpq_init(qIntPt[ii]);
2588//                 mpq_init(qPosIntPt[ii]);
2589//         }
2590//         //Compute a+((b-a)/2) && Convert intF1 to mpq
2591//         for(int ii=0;ii<this->numVars;ii++)
2592//         {
2593//                 mpq_t a,b;
2594//                 mpq_init(a); mpq_init(b);
2595//                 mpq_set_si(a,(*intF1)[ii],1);
2596//                 mpq_set_si(b,(*intF2)[ii],1);
2597//                 mpq_t diff;
2598//                 mpq_init(diff);
2599//                 mpq_sub(diff,b,a);        //diff=b-a
2600//                 mpq_t quot;
2601//                 mpq_init(quot);
2602//                 mpq_div_2exp(quot,diff,1);        //quot=diff/2=(b-a)/2
2603//                 mpq_clear(diff);
2604//                 //Don't be clever and reuse diff here
2605//                 mpq_t sum; mpq_init(sum);
2606//                 mpq_add(sum,b,quot);        //sum=b+quot=a+(b-a)/2
2607//                 mpq_set(qIntPt[ii],sum);
2608//                 mpq_clear(sum);
2609//                 mpq_clear(quot);
2610//                 mpq_clear(a); mpq_clear(b);
2611//                 //Now for intF1
2612//                 mpq_set_si(qPosRay[ii],(*intF1)[ii],1);
2613//         }
2614//         //Now add: qPosIntPt=qPosRay+qIntPt until qPosIntPt >0
2615//         while(TRUE)
2616//         {
2617//                 bool success=FALSE;
2618//                 int posCtr=0;
2619//                 for(int ii=0;ii<this->numVars;ii++)
2620//                 {
2621//                         mpq_t sum; mpq_init(sum);
2622//                         mpq_add(sum,qPosRay[ii],qIntPt[ii]);
2623//                         mpq_set(qPosIntPt[ii],sum);
2624//                         mpq_clear(sum);
2625//                         if(mpq_sgn(qPosIntPt[ii])==1)
2626//                                 posCtr++;
2627//                 }
2628//                 if(posCtr==this->numVars)//qPosIntPt > 0
2629//                         break;
2630//                 else
2631//                 {
2632//                         mpq_t qTwo; mpq_init(qTwo);
2633//                         mpq_set_ui(qTwo,2,1);
2634//                         for(int jj=0;jj<this->numVars;jj++)
2635//                         {
2636//                                 mpq_t tmp; mpq_init(tmp);
2637//                                 mpq_mul(tmp,qPosRay[jj],qTwo);
2638//                                 mpq_set( qPosRay[jj], tmp);
2639//                                 mpq_clear(tmp);
2640//                         }
2641//                         mpq_clear(qTwo);
2642//                 }
2643//         }//while
2644//         //Now qPosIntPt ought to be >0, so convert back to int :D
2645//         /*Compute lcm of the denominators*/
2646//         mpz_t *denom = new mpz_t[this->numVars];
2647//         mpz_t tmp,kgV;
2648//         mpz_init(tmp); mpz_init(kgV);
2649//         for (int ii=0;ii<this->numVars;ii++)
2650//         {
2651//                 mpz_t z;
2652//                 mpz_init(z);
2653//                 mpq_get_den(z,qPosIntPt[ii]);
2654//                 mpz_init(denom[ii]);
2655//                 mpz_set( denom[ii], z);
2656//                 mpz_clear(z);
2657//         }
2658//
2659//         mpz_set(tmp,denom[0]);
2660//         for (int ii=0;ii<this->numVars;ii++)
2661//         {
2662//                 mpz_lcm(kgV,tmp,denom[ii]);
2663//                 mpz_set(tmp,kgV);
2664//         }
2665//         mpz_clear(tmp);
2666//         /*Multiply the nominators by kgV*/
2667//         mpq_t qkgV,res;
2668//         mpq_init(qkgV);
2669//         mpq_canonicalize(qkgV);
2670//         mpq_init(res);
2671//         mpq_canonicalize(res);
2672//
2673//         mpq_set_num(qkgV,kgV);
2674//         int64vec *n=new int64vec(this->numVars);
2675//         for (int ii=0;ii<this->numVars;ii++)
2676//         {
2677//                 mpq_canonicalize(qPosIntPt[ii]);
2678//                 mpq_mul(res,qkgV,qPosIntPt[ii]);
2679//                 (*n)[ii]=(int)mpz_get_d(mpq_numref(res));
2680//         }
2681//         this->setIntPoint(n);
2682//         delete n;
2683//         delete [] qPosIntPt;
2684//         delete [] denom;
2685//         delete [] qPosRay;
2686//         delete [] qIntPt;
2687//         mpz_clear(kgV);
2688//         mpq_clear(qkgV); mpq_clear(res);
2689// }
2690
2691/** \brief Copy a ring and add a weighted ordering in first place
2692 *
2693 */
2694ring gcone::rCopyAndAddWeight(const ring &r, int64vec *ivw)
2695{
2696        ring res=rCopy0(r);
2697        int jj;
2698
2699        omFree(res->order);
2700        res->order =(int *)omAlloc0(4*sizeof(int/*64*/));
2701        omFree(res->block0);
2702        res->block0=(int *)omAlloc0(4*sizeof(int/*64*/));
2703        omFree(res->block1);
2704        res->block1=(int *)omAlloc0(4*sizeof(int/*64*/));
2705        omfree(res->wvhdl);
2706        res->wvhdl =(int **)omAlloc0(4*sizeof(int/*64*/**));
2707
2708        res->order[0]=ringorder_a/*64*/;
2709        res->block0[0]=1;
2710        res->block1[0]=res->N;
2711        res->order[1]=ringorder_dp;        //basically useless, since that should never be used
2712        res->block0[1]=1;
2713        res->block1[1]=res->N;
2714        res->order[2]=ringorder_C;
2715
2716        int length=ivw->length();
2717        int/*64*/ *A=(int/*64*/ *)omAlloc0(length*sizeof(int/*64*/));
2718        for (jj=0;jj<length;jj++)
2719        {
2720                A[jj]=(*ivw)[jj];
2721                if((*ivw)[jj]>=INT_MAX) WarnS("A[jj] exceeds INT_MAX in gcone::rCopyAndAddWeight!\n");
2722        }
2723        res->wvhdl[0]=(int *)A;
2724        res->block1[0]=length;
2725
2726        rComplete(res);
2727        return res;
2728}//rCopyAndAdd
2729
2730ring gcone::rCopyAndAddWeight2(const ring &r,const int64vec *ivw, const int64vec *fNormal)
2731{
2732        ring res=rCopy0(r);
2733
2734        omFree(res->order);
2735        res->order =(int *)omAlloc0(5*sizeof(int/*64*/));
2736        omFree(res->block0);
2737        res->block0=(int *)omAlloc0(5*sizeof(int/*64*/));
2738        omFree(res->block1);
2739        res->block1=(int *)omAlloc0(5*sizeof(int/*64*/));
2740        omfree(res->wvhdl);
2741        res->wvhdl =(int **)omAlloc0(5*sizeof(int/*64*/**));
2742
2743        res->order[0]=ringorder_a/*64*/;
2744        res->block0[0]=1;
2745        res->block1[0]=res->N;
2746        res->order[1]=ringorder_a/*64*/;
2747        res->block0[1]=1;
2748        res->block1[1]=res->N;
2749
2750        res->order[2]=ringorder_dp;
2751        res->block0[2]=1;
2752        res->block1[2]=res->N;
2753
2754        res->order[3]=ringorder_C;
2755
2756        int length=ivw->length();
2757        int/*64*/ *A1=(int/*64*/ *)omAlloc0(length*sizeof(int/*64*/));
2758        int/*64*/ *A2=(int/*64*/ *)omAlloc0(length*sizeof(int/*64*/));
2759        for (int jj=0;jj<length;jj++)
2760        {
2761                A1[jj]=(*ivw)[jj];
2762                A2[jj]=-(*fNormal)[jj];
2763                if((*ivw)[jj]>=INT_MAX || (*fNormal)[jj]>=INT_MAX) WarnS("A[jj] exceeds INT_MAX in gcone::rCopyAndAddWeight2!\n");
2764        }
2765        res->wvhdl[0]=(int *)A1;
2766        res->block1[0]=length;
2767        res->wvhdl[1]=(int *)A2;
2768        res->block1[1]=length;
2769        rComplete(res);
2770        return res;
2771}
2772
2773//NOTE not needed anywhere
2774// ring rCopyAndChangeWeight(ring const &r, int64vec *ivw)
2775// {
2776//         ring res=rCopy0(currRing);
2777//         rComplete(res);
2778//         rSetWeightVec(res,(int64*)ivw);
2779//         //rChangeCurrRing(rnew);
2780//         return res;
2781// }
2782
2783/** \brief Checks whether a given facet is a search facet
2784 * Determines whether a given facet of a cone is the search facet of a neighbouring cone
2785 * This is done in the following way:
2786 * We loop through all facets of the cone and find the "smallest" facet, i.e. the unique facet
2787 * that is first crossed during the generic walk.
2788 * We then check whether the fNormal of this facet is parallel to the fNormal of our testfacet.
2789 * If this is the case, then our facet is indeed a search facet and TRUE is retuned.
2790*/
2791//removed with r12286
2792
2793/** \brief Check for equality of two intvecs
2794 */
2795static bool ivAreEqual(const int64vec &a, const int64vec &b)
2796{
2797        bool res=TRUE;
2798        for(int ii=0;ii<pVariables;ii++)
2799        {
2800                if(a[ii]!=b[ii])
2801                {
2802                        res=FALSE;
2803                        break;
2804                }
2805        }
2806        return res;
2807}
2808
2809/** \brief The reverse search algorithm
2810 */
2811//removed with r12286
2812/** \brief Compute the lineality/homogeneity space
2813* It is the kernel of the inequality matrix Ax=0
2814* As a result gcone::dd_LinealitySpace is set
2815*/
2816dd_MatrixPtr gcone::computeLinealitySpace()
2817{
2818        dd_MatrixPtr res;
2819        dd_MatrixPtr ddineq;
2820        ddineq=dd_CopyMatrix(this->ddFacets);
2821        //Add a row of 0s in 0th place
2822        dd_MatrixPtr ddAppendRowOfZeroes=dd_CreateMatrix(1,this->numVars+1);
2823        dd_MatrixPtr ddFoo=dd_AppendMatrix(ddAppendRowOfZeroes,ddineq);
2824        dd_FreeMatrix(ddAppendRowOfZeroes);
2825        dd_FreeMatrix(ddineq);
2826        ddineq=dd_CopyMatrix(ddFoo);
2827        dd_FreeMatrix(ddFoo);
2828        //Cohen starts here
2829        int dimKer=0;//Cohen calls this r
2830        int m=ddineq->rowsize;//Rows
2831        int n=ddineq->colsize;//Cols
2832        int c[m+1];
2833        int d[n+1];
2834        for(int ii=0;ii<m;ii++)
2835                c[ii]=0;
2836        for(int ii=0;ii<n;ii++)
2837                d[ii]=0;
2838
2839        for(int k=1;k<n;k++)
2840        {
2841                //Let's find a j s.t. m[j][k]!=0 && c[j]=0
2842                int condCtr=0;//Check each row for zeroness
2843                for(int j=1;j<m;j++)
2844                {
2845                        if(mpq_sgn(ddineq->matrix[j][k])!=0 && c[j]==0)
2846                        {
2847                                mpq_t quot; mpq_init(quot);
2848                                mpq_t one; mpq_init(one); mpq_set_str(one,"-1",10);
2849                                mpq_t ratd; mpq_init(ratd);
2850                                if((int)mpq_get_d(ddineq->matrix[j][k])!=0)
2851                                        mpq_div(quot,one,ddineq->matrix[j][k]);
2852                                mpq_set(ratd,quot);
2853                                mpq_canonicalize(ratd);
2854
2855                                mpq_set_str(ddineq->matrix[j][k],"-1",10);
2856                                for(int ss=k+1;ss<n;ss++)
2857                                {
2858                                        mpq_t prod; mpq_init(prod);
2859                                        mpq_mul(prod, ratd, ddineq->matrix[j][ss]);
2860                                        mpq_set(ddineq->matrix[j][ss],prod);
2861                                        mpq_canonicalize(ddineq->matrix[j][ss]);
2862                                        mpq_clear(prod);
2863                                }
2864                                for(int ii=1;ii<m;ii++)
2865                                {
2866                                        if(ii!=j)
2867                                        {
2868                                                mpq_set(ratd,ddineq->matrix[ii][k]);
2869                                                mpq_set_str(ddineq->matrix[ii][k],"0",10);
2870                                                for(int ss=k+1;ss<n;ss++)
2871                                                {
2872                                                        mpq_t prod; mpq_init(prod);
2873                                                        mpq_mul(prod, ratd, ddineq->matrix[j][ss]);
2874                                                        mpq_t sum; mpq_init(sum);
2875                                                        mpq_add(sum, ddineq->matrix[ii][ss], prod);
2876                                                        mpq_set(ddineq->matrix[ii][ss],        sum);
2877                                                        mpq_canonicalize(ddineq->matrix[ii][ss]);
2878                                                        mpq_clear(prod);
2879                                                        mpq_clear(sum);
2880                                                }
2881                                        }
2882                                }
2883                                c[j]=k;
2884                                d[k]=j;
2885                                mpq_clear(quot); mpq_clear(ratd); mpq_clear(one);
2886                        }
2887                        else
2888                                condCtr++;
2889                }
2890                if(condCtr==m-1)        //Why -1 ???
2891                {
2892                        dimKer++;
2893                        d[k]=0;
2894//                         break;//goto _4;
2895                }//else{
2896                /*Eliminate*/
2897        }//for(k
2898        /*Output kernel, i.e. set gcone::dd_LinealitySpace here*/
2899//         gcone::dd_LinealitySpace = dd_CreateMatrix(dimKer,this->numVars+1);
2900        res = dd_CreateMatrix(dimKer,this->numVars+1);
2901        int row=-1;
2902        for(int kk=1;kk<n;kk++)
2903        {
2904                if(d[kk]==0)
2905                {
2906                        row++;
2907                        for(int ii=1;ii<n;ii++)
2908                        {
2909                                if(d[ii]>0)
2910                                        mpq_set(res->matrix[row][ii],ddineq->matrix[d[ii]][kk]);
2911                                else if(ii==kk)
2912                                        mpq_set_str(res->matrix[row][ii],"1",10);
2913                                mpq_canonicalize(res->matrix[row][ii]);
2914                        }
2915                }
2916        }
2917        dd_FreeMatrix(ddineq);
2918        return(res);
2919        //Better safe than sorry:
2920        //NOTE dd_LinealitySpace contains RATIONAL ENTRIES
2921        //Therefore if you need integer ones: CALL gcone::makeInt(...) method
2922}
2923
2924
2925/** \brief The new method of Markwig and Jensen
2926 * Compute gcBasis and facets for the arbitrary starting cone. Store \f$(codim-1)\f$-facets as normals.
2927 * In order to represent a facet uniquely compute also the \f$(codim-2)\f$-facets and norm by the gcd of the components.
2928 * Keep a list of facets as a linked list containing an int64vec and an integer matrix.
2929 * Since a \f$(codim-1)\f$-facet belongs to exactly two full dimensional cones, we remove a facet from the list as
2930 * soon as we compute this facet again. Comparison of facets is done by...
2931 */
2932void gcone::noRevS(gcone &gcRoot, bool usingIntPoint)
2933{
2934        facet *SearchListRoot = new facet(); //The list containing ALL facets we come across
2935        facet *SearchListAct;
2936        SearchListAct = NULL;
2937        //SearchListAct = SearchListRoot;
2938        gcone *gcAct;
2939        gcAct = &gcRoot;
2940        gcone *gcPtr;        //Pointer to end of linked list of cones
2941        gcPtr = &gcRoot;
2942        gcone *gcNext;        //Pointer to next cone to be visited
2943        gcNext = &gcRoot;
2944        gcone *gcHead;
2945        gcHead = &gcRoot;
2946        facet *fAct;
2947        fAct = gcAct->facetPtr;
2948        ring rAct;
2949        rAct = currRing;
2950        int UCNcounter=gcAct->getUCN();
2951#ifndef SING_NDEBUG
2952        printf("NoRevs\n");
2953        printf("Facets are:\n");
2954        gcAct->showFacets();
2955#endif
2956        /*Compute lineality space here
2957        * Afterwards static dd_MatrixPtr gcone::dd_LinealitySpace is set*/
2958        if(dd_LinealitySpace==NULL)
2959                dd_LinealitySpace = gcAct->computeLinealitySpace();
2960        /*End of lineality space computation*/
2961        //gcAct->getCodim2Normals(*gcAct);
2962        if(fAct->codim2Ptr==NULL)
2963                gcAct->getExtremalRays(*gcAct);
2964        /* Make a copy of the facet list of first cone
2965           Since the operations getCodim2Normals and normalize affect the facets
2966           we must not memcpy them before these ops! */
2967        /*facet *codim2Act; codim2Act = NULL;
2968        facet *sl2Root;
2969        facet *sl2Act;*/
2970        for(int ii=0;ii<this->numFacets;ii++)
2971        {
2972                //only copy flippable facets! NOTE: We do not need flipRing or any such information.
2973                if(fAct->isFlippable==TRUE)
2974                {
2975                        /*Using shallow copy here*/
2976#ifdef SHALLOW
2977                        if( ii==0 || (ii>0 && SearchListAct==NULL) ) //1st facet may be non-flippable
2978                        {
2979                                if(SearchListRoot!=NULL) delete(SearchListRoot);
2980                                SearchListRoot = fAct->shallowCopy(*fAct);
2981                                SearchListAct = SearchListRoot;        //SearchListRoot is already 'new'ed at beginning of method!
2982                        }
2983                        else
2984                        {
2985                                SearchListAct->next = fAct->shallowCopy(*fAct);
2986                                SearchListAct = SearchListAct->next;
2987                        }
2988                        fAct=fAct->next;
2989#else
2990                        /*End of shallow copy*/
2991                        int64vec *fNormal;
2992                        fNormal = fAct->getFacetNormal();
2993                        if( ii==0 || (ii>0 && SearchListAct==NULL) ) //1st facet may be non-flippable
2994                        {
2995                                SearchListAct = SearchListRoot;        //SearchListRoot is already 'new'ed at beginning of method!
2996                        }
2997                        else
2998                        {
2999                                SearchListAct->next = new facet();
3000                                SearchListAct = SearchListAct->next;
3001                        }
3002                        SearchListAct->setFacetNormal(fNormal);
3003                        SearchListAct->setUCN(this->getUCN());
3004                        SearchListAct->numCodim2Facets=fAct->numCodim2Facets;
3005                        SearchListAct->isFlippable=TRUE;
3006                        //Copy int point as well
3007                        int64vec *ivIntPt;
3008                        ivIntPt = fAct->getInteriorPoint();
3009                        SearchListAct->setInteriorPoint(ivIntPt);
3010                        delete(ivIntPt);
3011                        //Copy codim2-facets
3012                        facet *codim2Act;
3013                        codim2Act = NULL;
3014                        facet *sl2Root;
3015                        facet *sl2Act;
3016                        codim2Act=fAct->codim2Ptr;
3017                        SearchListAct->codim2Ptr = new facet(2);
3018                        sl2Root = SearchListAct->codim2Ptr;
3019                        sl2Act = sl2Root;
3020                        for(int jj=0;jj<fAct->numCodim2Facets;jj++)
3021//                         for(int jj=0;jj<fAct->numRays-1;jj++)
3022                        {
3023                                int64vec *f2Normal;
3024                                f2Normal = codim2Act->getFacetNormal();
3025                                if(jj==0)
3026                                {
3027                                        sl2Act = sl2Root;
3028                                        sl2Act->setFacetNormal(f2Normal);
3029                                }
3030                                else
3031                                {
3032                                        sl2Act->next = new facet(2);
3033                                        sl2Act = sl2Act->next;
3034                                        sl2Act->setFacetNormal(f2Normal);
3035                                }
3036                                delete f2Normal;
3037                                codim2Act = codim2Act->next;
3038                        }
3039                        fAct = fAct->next;
3040                        delete fNormal;
3041#endif
3042                }//if(fAct->isFlippable==TRUE)
3043                else {fAct = fAct->next;}
3044        }//End of copying facets into SLA
3045
3046        SearchListAct = SearchListRoot;        //Set to beginning of list
3047        /*Make SearchList doubly linked*/
3048#ifndef SING_NDEBUG
3049  #if SIZEOF_LONG==8
3050        while(SearchListAct!=(facet*)0xfefefefefefefefe && SearchListAct!=NULL)
3051        {
3052                if(SearchListAct->next!=(facet*)0xfefefefefefefefe && SearchListAct->next!=NULL){
3053  #elif SIZEOF_LONG!=8
3054        while(SearchListAct!=(facet*)0xfefefefe)
3055        {
3056                if(SearchListAct->next!=(facet*)0xfefefefe){
3057  #endif
3058#else
3059        while(SearchListAct!=NULL)
3060        {
3061                if(SearchListAct->next!=NULL){
3062#endif
3063                        SearchListAct->next->prev = SearchListAct;
3064                }
3065                SearchListAct = SearchListAct->next;
3066        }
3067        SearchListAct = SearchListRoot;        //Set to beginning of List
3068
3069//         fAct = gcAct->facetPtr;//???
3070        gcAct->writeConeToFile(*gcAct);
3071        /*End of initialisation*/
3072
3073        fAct = SearchListAct;
3074        /*2nd step
3075          Choose a facet from SearchList, flip it and forget the previous cone
3076          We always choose the first facet from SearchList as facet to be flipped
3077        */
3078        while( (SearchListAct!=NULL))//&& counter<490)
3079        {//NOTE See to it that the cone is only changed after ALL facets have been flipped!
3080                fAct = SearchListAct;
3081                 while(fAct!=NULL)
3082//                 while( (fAct->getUCN() == fAct->next->getUCN()) )
3083                {        //Since SLA should only contain flippables there should be no need to check for that
3084                        gcAct->flip2(gcAct->gcBasis,fAct);
3085                        //NOTE rCopy needed?
3086                        ring rTmp=rCopy(fAct->flipRing);
3087                        rComplete(rTmp);
3088                        rChangeCurrRing(rTmp);
3089                        gcone *gcTmp = new gcone(*gcAct,*fAct);//copy constructor!
3090                        /* Now gcTmp->gcBasis and gcTmp->baseRing are set from fAct->flipGB and fAct->flipRing.
3091                         * Since we come at most once across a given facet from gcAct->facetPtr we can delete them.
3092                         * NOTE: Can this cause trouble with the destructor?
3093                         * Answer: Yes it bloody well does!
3094                         * However I'd like to delete this data here, since if we have a cone with many many facets it
3095                         * should pay to get rid of the flipGB as soon as possible.
3096                         * Destructor must be equipped with necessary checks.
3097                        */
3098                         idDelete((ideal *)&fAct->flipGB);
3099                        rDelete(fAct->flipRing);
3100                        gcTmp->getConeNormals(gcTmp->gcBasis/*, FALSE*/);
3101//                         gcTmp->getCodim2Normals(*gcTmp);
3102                        gcTmp->getExtremalRays(*gcTmp);
3103                        //NOTE Order rays lex here
3104                        gcTmp->orderRays();
3105//                         //NOTE If flip2 is used we need to get an interior point of gcTmp
3106//                         // and replace gcTmp->baseRing with an appropriate ring with only
3107//                         // one weight
3108//                         gcTmp->interiorPoint2();
3109                        gcTmp->replaceDouble_ringorder_a_ByASingleOne();
3110                        //gcTmp->ddFacets should not be needed anymore, so
3111                        dd_FreeMatrix(gcTmp->ddFacets);
3112#ifndef SING_NDEBUG
3113//                         gcTmp->showFacets(1);
3114#endif
3115                        /*add facets to SLA here*/
3116#ifdef SHALLOW
3117  #ifndef SING_NDEBUG
3118                        printf("fActUCN before enq2: %i\n",fAct->getUCN());
3119  #endif
3120                        facet *tmp;
3121                        tmp=gcTmp->enqueue2(SearchListRoot);
3122  #ifndef SING_NDEBUG
3123                        printf("\nheadUCN=%i\n",tmp->getUCN());
3124                        printf("fActUCN after enq2: %i\n",fAct->getUCN());
3125  #endif
3126                        SearchListRoot=tmp;
3127                        //SearchListRoot=gcTmp->enqueue2/*NewFacets*/(SearchListRoot);
3128#else
3129                        SearchListRoot=gcTmp->enqueueNewFacets(SearchListRoot);
3130#endif //ifdef SHALLOW
3131//                         gcTmp->writeConeToFile(*gcTmp);
3132                         if(gfanHeuristic==1)
3133                         {
3134                                gcTmp->writeConeToFile(*gcTmp);
3135                                 idDelete((ideal*)&gcTmp->gcBasis);//Whonder why?
3136                                rDelete(gcTmp->baseRing);
3137                         }
3138#ifndef SING_NDEBUG
3139                        if(SearchListRoot!=NULL)
3140                                showSLA(*SearchListRoot);
3141#endif
3142                        rChangeCurrRing(gcAct->baseRing);
3143                        rDelete(rTmp);
3144                        //doubly linked for easier removal
3145                        gcTmp->prev = gcPtr;
3146                        gcPtr->next=gcTmp;
3147                        gcPtr=gcPtr->next;
3148                        //Cleverly disguised exit condition follows
3149                        if(fAct->getUCN() == fAct->next->getUCN())
3150                        {
3151                                printf("Switching UCN from %i to %i\n",fAct->getUCN(),fAct->next->getUCN());
3152                                fAct=fAct->next;
3153                        }
3154                        else
3155                        {
3156                                //rDelete(gcAct->baseRing);
3157//                                 printf("break\n");
3158                                break;
3159                        }
3160//                         fAct=fAct->next;
3161                }//while( ( (fAct->next!=NULL) && (fAct->getUCN()==fAct->next->getUCN() ) ) );
3162                //Search for cone with smallest UCN
3163#ifndef SING_NDEBUG
3164  #if SIZEOF_LONG==8        //64 bit
3165                while(gcNext!=(gcone * const)0xfbfbfbfbfbfbfbfb && SearchListRoot!=NULL)
3166  #elif SIZEOF_LONG == 4
3167                while(gcNext!=(gcone * const)0xfbfbfbfb && SearchListRoot!=NULL)
3168  #endif
3169#endif
3170#ifdef SING_NDEBUG
3171                while(gcNext!=NULL && SearchListRoot!=NULL)
3172#endif
3173                {
3174                        if( gcNext->getUCN() == SearchListRoot->getUCN() )
3175                        {
3176                                if(gfanHeuristic==0)
3177                                {
3178                                        gcAct = gcNext;
3179                                        //Seems better to not to use rCopy here
3180//                                         rAct=rCopy(gcAct->baseRing);
3181                                        rAct=gcAct->baseRing;
3182                                        rComplete(rAct);
3183                                        rChangeCurrRing(rAct);
3184                                        break;
3185                                }
3186                                else if(gfanHeuristic==1)
3187                                {
3188                                        gcone *gcDel;
3189                                        gcDel = gcAct;
3190                                        gcAct = gcNext;
3191                                        //Read st00f from file &
3192                                        //implant the GB into gcAct
3193                                        readConeFromFile(gcAct->getUCN(), gcAct);
3194                                        //Kill the baseRing but ONLY if it is not the ring the computation started in!
3195//                                         if(gcDel->getUCN()!=1)//WTF!? This causes the Mandelbug in gcone::areEqual(facet, facet)
3196//                                                 rDelete(gcDel->baseRing);
3197//                                         rAct=rCopy(gcAct->baseRing);
3198                                        /*The ring change occurs in readConeFromFile, so as to
3199                                        assure that gcAct->gcBasis belongs to the right ring*/
3200                                        rAct=gcAct->baseRing;
3201                                        rComplete(rAct);
3202                                        rChangeCurrRing(rAct);
3203                                        break;
3204                                }
3205                        }
3206                        else if(gcNext->getUCN() < SearchListRoot->getUCN() )
3207                        {
3208                                idDelete( (ideal*)&gcNext->gcBasis );
3209//                                 rDelete(gcNext->baseRing);//TODO Why does this crash?
3210                        }
3211                        /*else
3212                        {
3213                                if(gfanHeuristic==1)
3214                                {
3215                                        gcone *gcDel;
3216                                        gcDel = gcNext;
3217                                        if(gcDel->getUCN()!=1)
3218                                                rDelete(gcDel->baseRing);
3219                                }
3220                        }*/
3221                         gcNext = gcNext->next;
3222                }
3223                UCNcounter++;
3224                SearchListAct = SearchListRoot;
3225        }
3226        printf("\nFound %i cones - terminating\n", counter);
3227}//void noRevS(gcone &gc)
3228
3229
3230/** \brief Make a set of rational vectors into integers
3231 *
3232 * We compute the lcm of the denominators and multiply with this to get integer values.
3233 * If the gcd of the nominators > 1 we divide by the gcd => primitive vector.
3234 * Expects a new int64vec as 3rd parameter
3235 * \param dd_MatrixPtr,int64vec
3236 */
3237void gcone::makeInt(const dd_MatrixPtr &M, const int line, int64vec &n)
3238{
3239        mpz_t *denom = new mpz_t[this->numVars];
3240        for(int ii=0;ii<this->numVars;ii++)
3241        {
3242                mpz_init_set_str(denom[ii],"0",10);
3243        }
3244
3245        mpz_t kgV,tmp;
3246        mpz_init(kgV);
3247        mpz_init(tmp);
3248
3249        for (int ii=0;ii<(M->colsize)-1;ii++)
3250        {
3251                mpz_t z;
3252                mpz_init(z);
3253                mpq_get_den(z,M->matrix[line-1][ii+1]);
3254                mpz_set( denom[ii], z);
3255                mpz_clear(z);
3256        }
3257
3258        /*Compute lcm of the denominators*/
3259        mpz_set(tmp,denom[0]);
3260        for (int ii=0;ii<(M->colsize)-1;ii++)
3261        {
3262                mpz_lcm(kgV,tmp,denom[ii]);
3263                mpz_set(tmp,kgV);
3264        }
3265        mpz_clear(tmp);
3266        /*Multiply the nominators by kgV*/
3267        mpq_t qkgV,res;
3268        mpq_init(qkgV);
3269        mpq_set_str(qkgV,"1",10);
3270        mpq_canonicalize(qkgV);
3271
3272        mpq_init(res);
3273        mpq_set_str(res,"1",10);
3274        mpq_canonicalize(res);
3275
3276        mpq_set_num(qkgV,kgV);
3277
3278//        mpq_canonicalize(qkgV);
3279//         int ggT=1;
3280        for (int ii=0;ii<(M->colsize)-1;ii++)
3281        {
3282                mpq_mul(res,qkgV,M->matrix[line-1][ii+1]);
3283                n[ii]=(int64)mpz_get_d(mpq_numref(res));
3284//                 ggT=int64gcd(ggT,n[ii]);
3285        }
3286        int64 ggT=n[0];
3287        for(int ii=0;ii<this->numVars;ii++)
3288                ggT=int64gcd(ggT,n[ii]);
3289        //Normalisation
3290        if(ggT>1)
3291        {
3292                for(int ii=0;ii<this->numVars;ii++)
3293                        n[ii] /= ggT;
3294        }
3295        delete [] denom;
3296        mpz_clear(kgV);
3297        mpq_clear(qkgV); mpq_clear(res);
3298
3299}
3300/**
3301 * For all codim-2-facets we compute the gcd of the components of the facet normal and
3302 * divide it out. Thus we get a normalized representation of each
3303 * (codim-2)-facet normal, i.e. a primitive vector
3304 * Actually we now also normalize the facet normals.
3305 */
3306// void gcone::normalize()
3307// {
3308//         int *ggT = new int;
3309//                 *ggT=1;
3310//         facet *fAct;
3311//         facet *codim2Act;
3312//         fAct = this->facetPtr;
3313//         codim2Act = fAct->codim2Ptr;
3314//         while(fAct!=NULL)
3315//         {
3316//                 int64vec *fNormal;
3317//                 fNormal = fAct->getFacetNormal();
3318//                 int *ggT = new int;
3319//                 *ggT=1;
3320//                 for(int ii=0;ii<this->numVars;ii++)
3321//                 {
3322//                         *ggT=intgcd((*ggT),(*fNormal)[ii]);
3323//                 }
3324//                 if(*ggT>1)//We only need to do this if the ggT is non-trivial
3325//                 {
3326// //                         int64vec *fCopy = fAct->getFacetNormal();
3327//                         for(int ii=0;ii<this->numVars;ii++)
3328//                                 (*fNormal)[ii] = ((*fNormal)[ii])/(*ggT);
3329//                         fAct->setFacetNormal(fNormal);
3330//                 }
3331//                 delete fNormal;
3332//                 delete ggT;
3333//                 /*And now for the codim2*/
3334//                 while(codim2Act!=NULL)
3335//                 {
3336//                         int64vec *n;
3337//                         n=codim2Act->getFacetNormal();
3338//                         int *ggT=new int;
3339//                         *ggT=1;
3340//                         for(int ii=0;ii<this->numVars;ii++)
3341//                         {
3342//                                 *ggT = intgcd((*ggT),(*n)[ii]);
3343//                         }
3344//                         if(*ggT>1)
3345//                         {
3346//                                 for(int ii=0;ii<this->numVars;ii++)
3347//                                 {
3348//                                         (*n)[ii] = ((*n)[ii])/(*ggT);
3349//                                 }
3350//                                 codim2Act->setFacetNormal(n);
3351//                         }
3352//                         codim2Act = codim2Act->next;
3353//                         delete n;
3354//                         delete ggT;
3355//                 }
3356//                 fAct = fAct->next;
3357//         }
3358// }
3359
3360/** \brief Enqueue new facets into the searchlist
3361 * The searchlist (SLA for short) is implemented as a FIFO
3362 * Checks are done as follows:
3363 * 1) Check facet fAct against all facets in SLA for parallelity. If it is not parallel to any one add it.
3364 * 2) If it is parallel compare the codim2 facets. If they coincide the facets are equal and we delete the
3365 *        facet from SLA and do not add fAct.
3366 * It may very well happen, that SLA=\f$ \emptyset \f$ but we do not have checked all fActs yet. In this case we
3367 * can be sure, that none of the facets that are still there already were in SLA once, so we just dump them into SLA.
3368 * Takes ptr to search list root
3369 * Returns a pointer to new first element of Searchlist
3370 */
3371facet * gcone::enqueueNewFacets(facet *f)
3372{
3373#ifdef gfanp
3374        timeval start, end;
3375        gettimeofday(&start, 0);
3376#endif
3377        facet *slHead;
3378        slHead = f;
3379        facet *slAct;        //called with f=SearchListRoot
3380        slAct = f;
3381        facet *slEnd;        //Pointer to end of SLA
3382        slEnd = f;
3383//         facet *slEndStatic;        //marks the end before a new facet is added
3384        facet *fAct;
3385        fAct = this->facetPtr;
3386        facet *codim2Act;
3387        codim2Act = this->facetPtr->codim2Ptr;
3388        facet *sl2Act;
3389        sl2Act = f->codim2Ptr;
3390        /** Pointer to a facet that will be deleted */
3391        volatile facet *deleteMarker;
3392        deleteMarker = NULL;
3393
3394        /** \brief  Flag to mark a facet that might be added
3395         * The following case may occur:
3396         * A facet fAct is found to be parallel but not equal to the current facet slAct from SLA.
3397         * This does however not mean that there does not exist a facet behind the current slAct that is actually equal
3398         * to fAct. In this case we set the boolean flag maybe to TRUE. If we encounter an equality lateron, it is reset to
3399         * FALSE and the according slAct is deleted.
3400         * If slAct->next==NULL AND maybe==TRUE we know, that fAct must be added
3401         */
3402
3403        /**A facet was removed, lengthOfSearchlist-- thus we must not rely on
3404         * if(notParallelCtr==lengthOfSearchList) but rather
3405         * if( (notParallelCtr==lengthOfSearchList && removalOccured==FALSE)
3406         */
3407        volatile bool removalOccured=FALSE;
3408        while(slEnd->next!=NULL)
3409        {
3410                slEnd=slEnd->next;
3411        }
3412        /*1st step: compare facetNormals*/
3413        while(fAct!=NULL)
3414        {
3415                if(fAct->isFlippable==TRUE)
3416                {
3417                        int64vec *fNormal=NULL;
3418                        fNormal=fAct->getFacetNormal();
3419                        slAct = slHead;
3420                        /*If slAct==NULL and fAct!=NULL
3421                        we just copy all remaining facets into SLA*/
3422                        if(slAct==NULL)
3423                        {
3424                                facet *fCopy;
3425                                fCopy = fAct;
3426                                while(fCopy!=NULL)
3427                                {
3428                                        if(fCopy->isFlippable==TRUE)//We must assure fCopy is also flippable
3429                                        {
3430                                                if(slAct==NULL)
3431                                                {
3432                                                        slAct = new facet(*fCopy/*,TRUE*/);//copy constructor
3433                                                        slHead = slAct;
3434                                                }
3435                                                else
3436                                                {
3437                                                        slAct->next = new facet(*fCopy/*,TRUE*/);
3438                                                        slAct = slAct->next;
3439                                                }
3440                                        }
3441                                        fCopy = fCopy->next;
3442                                }
3443                                break;//Where does this lead to?
3444                        }
3445                        /*End of dumping into SLA*/
3446                        while(slAct!=NULL)
3447                        {
3448                                int64vec *slNormal=NULL;
3449                                removalOccured=FALSE;
3450                                slNormal = slAct->getFacetNormal();
3451#ifndef SING_NDEBUG
3452                                printf("Checking facet (");fNormal->show(1,1);printf(") against (");slNormal->show(1,1);printf(")\n");
3453#endif
3454//                                 if( (areEqual(fAct,slAct) && (!areEqual2(fAct,slAct)) ))
3455//                                         exit(-1);
3456                                 if(areEqual2(fAct,slAct))
3457                                {
3458                                        deleteMarker = slAct;
3459                                        if(slAct==slHead)
3460                                        {
3461                                                slHead = slAct->next;
3462                                                if(slHead!=NULL)
3463                                                        slHead->prev = NULL;
3464                                        }
3465                                        else if (slAct==slEnd)
3466                                        {
3467                                                slEnd=slEnd->prev;
3468                                                slEnd->next = NULL;
3469                                        }
3470                                        else
3471                                        {
3472                                                slAct->prev->next = slAct->next;
3473                                                slAct->next->prev = slAct->prev;
3474                                        }
3475                                        removalOccured=TRUE;
3476                                        gcone::lengthOfSearchList--;
3477                                        if(deleteMarker!=NULL)
3478                                        {
3479//                                                 delete deleteMarker;
3480//                                                 deleteMarker=NULL;
3481                                        }
3482#ifndef SING_NDEBUG
3483                                        printf("Removing (");fNormal->show(1,1);printf(") from list\n");
3484#endif
3485                                        delete slNormal;
3486                                        break;//leave the while loop, since we found fAct=slAct thus delete slAct and do not add fAct
3487                                }
3488                                slAct = slAct->next;
3489                                /* NOTE The following lines must not be here but rather called inside the if-block above.
3490                                Otherwise results get crappy. Unfortunately there are two slAct=slAct->next calls now,
3491                                (not nice!) but since they are in seperate branches of the if-statement there should not
3492                                be a way it gets called twice thus ommiting one facet:
3493                                slAct = slAct->next;*/
3494                                if(deleteMarker!=NULL)
3495                                {
3496//                                          delete deleteMarker;
3497//                                          deleteMarker=NULL;
3498                                }
3499                                delete slNormal;
3500                                                //if slAct was marked as to be deleted, delete it here!
3501                        }//while(slAct!=NULL)
3502                        if(removalOccured==FALSE)
3503                        {
3504#ifndef SING_NDEBUG
3505//                                 cout << "Adding facet (";fNormal->show(1,0);cout << ") to SLA " << endl;
3506#endif
3507                                //Add fAct to SLA
3508                                facet *marker;
3509                                marker = slEnd;
3510                                facet *f2Act;
3511                                f2Act = fAct->codim2Ptr;
3512
3513                                slEnd->next = new facet();
3514                                slEnd = slEnd->next;//Update slEnd
3515                                facet *slEndCodim2Root;
3516                                facet *slEndCodim2Act;
3517                                slEndCodim2Root = slEnd->codim2Ptr;
3518                                slEndCodim2Act = slEndCodim2Root;
3519
3520                                slEnd->setUCN(this->getUCN());
3521                                slEnd->isFlippable = TRUE;
3522                                slEnd->setFacetNormal(fNormal);
3523                                //NOTE Add interior point here
3524                                //This is ugly but needed for flip2
3525                                //Better: have slEnd->interiorPoint point to fAct->interiorPoint
3526                                //NOTE Only reference -> c.f. copy constructor
3527                                //slEnd->setInteriorPoint(fAct->getInteriorPoint());
3528                                slEnd->interiorPoint = fAct->interiorPoint;
3529                                slEnd->prev = marker;
3530                                //Copy codim2-facets
3531                                //int64vec *f2Normal=new int64vec(this->numVars);
3532                                while(f2Act!=NULL)
3533                                {
3534                                        int64vec *f2Normal;
3535                                        f2Normal=f2Act->getFacetNormal();
3536                                        if(slEndCodim2Root==NULL)
3537                                        {
3538                                                slEndCodim2Root = new facet(2);
3539                                                slEnd->codim2Ptr = slEndCodim2Root;
3540                                                slEndCodim2Root->setFacetNormal(f2Normal);
3541                                                slEndCodim2Act = slEndCodim2Root;
3542                                        }
3543                                        else
3544                                        {
3545                                                slEndCodim2Act->next = new facet(2);
3546                                                slEndCodim2Act = slEndCodim2Act->next;
3547                                                slEndCodim2Act->setFacetNormal(f2Normal);
3548                                        }
3549                                        f2Act = f2Act->next;
3550                                        delete f2Normal;
3551                                }
3552                                gcone::lengthOfSearchList++;
3553                        }//if( (notParallelCtr==lengthOfSearchList && removalOccured==FALSE) ||
3554                        fAct = fAct->next;
3555                         delete fNormal;
3556//                          delete slNormal;
3557                }//if(fAct->isFlippable==TRUE)
3558                else
3559                {
3560                        fAct = fAct->next;
3561                }
3562                if(gcone::maxSize<gcone::lengthOfSearchList)
3563                        gcone::maxSize= gcone::lengthOfSearchList;
3564        }//while(fAct!=NULL)
3565#ifdef gfanp
3566        gettimeofday(&end, 0);
3567        time_enqueue += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
3568#endif
3569        return slHead;
3570}//addC2N
3571
3572/** Enqueuing using shallow copies*/
3573facet * gcone::enqueue2(facet *f)
3574{
3575        assert(f!=NULL);
3576#ifdef gfanp
3577        timeval start, end;
3578        gettimeofday(&start, 0);
3579#endif
3580        facet *slHead;
3581        slHead = f;
3582        facet *slAct;        //called with f=SearchListRoot
3583        slAct = f;
3584        static facet *slEnd;        //Pointer to end of SLA
3585        if(slEnd==NULL)
3586                slEnd = f;
3587
3588        facet *fAct;
3589        fAct = this->facetPtr;//New facets to compare
3590        facet *codim2Act;
3591        codim2Act = this->facetPtr->codim2Ptr;
3592        volatile bool removalOccured=FALSE;
3593        while(slEnd->next!=NULL)
3594        {
3595                slEnd=slEnd->next;
3596        }
3597        while(fAct!=NULL)
3598        {
3599                if(fAct->isFlippable)
3600                {
3601                        facet *fDeleteMarker=NULL;
3602                        slAct = slHead;
3603                        if(slAct==NULL)
3604                        {
3605                                printf("Zero length SLA\n");
3606                                facet *fCopy;
3607                                fCopy = fAct;
3608                                while(fCopy!=NULL)
3609                                {
3610                                        if(fCopy->isFlippable==TRUE)//We must assure fCopy is also flippable
3611                                        {
3612                                                if(slAct==NULL)
3613                                                {
3614                                                        slAct = slAct->shallowCopy(*fCopy);//shallow copy constructor
3615                                                        slHead = slAct;
3616                                                }
3617                                                else
3618                                                {
3619                                                        slAct->next = slAct->shallowCopy(*fCopy);
3620                                                        slAct = slAct->next;
3621                                                }
3622                                        }
3623                                        fCopy = fCopy->next;
3624                                }
3625                                break;        //WTF?
3626                        }
3627                        /*Comparison starts here*/
3628                        while(slAct!=NULL)
3629                        {
3630                                removalOccured=FALSE;
3631#ifndef SING_NDEBUG
3632        printf("Checking facet (");fAct->fNormal->show(1,1);printf(") against (");slAct->fNormal->show(1,1);printf(")\n");
3633#endif
3634                                if(areEqual2(fAct,slAct))
3635                                {
3636                                        fDeleteMarker=slAct;
3637                                        if(slAct==slHead)
3638                                        {
3639//                                                 fDeleteMarker=slHead;
3640//                                                 printf("headUCN@enq=%i\n",slHead->getUCN());
3641                                                slHead = slAct->next;
3642//                                                 printf("headUCN@enq=%i\n",slHead->getUCN());
3643                                                if(slHead!=NULL)
3644                                                {
3645                                                        slHead->prev = NULL;
3646                                                }
3647                                                fDeleteMarker->shallowDelete();
3648                                                 //delete fDeleteMarker;//NOTE this messes up fAct in noRevS!
3649//                                                 printf("headUCN@enq=%i\n",slHead->getUCN());
3650                                        }
3651                                        else if (slAct==slEnd)
3652                                        {
3653                                                slEnd=slEnd->prev;
3654                                                slEnd->next = NULL;
3655                                                fDeleteMarker->shallowDelete();
3656                                                delete(fDeleteMarker);
3657                                        }
3658                                        else
3659                                        {
3660                                                slAct->prev->next = slAct->next;
3661                                                slAct->next->prev = slAct->prev;
3662                                                fDeleteMarker->shallowDelete();
3663                                                delete(fDeleteMarker);
3664                                        }
3665                                        removalOccured=TRUE;
3666                                        gcone::lengthOfSearchList--;
3667#ifndef SING_NDEBUG
3668printf("Removing (");fAct->fNormal->show(1,1);printf(") from list\n");
3669#endif
3670                                        break;//leave the while loop, since we found fAct=slAct thus delete slAct and do not add fAct
3671                                }
3672                                slAct = slAct->next;
3673                        }//while(slAct!=NULL)
3674                        if(removalOccured==FALSE)
3675                        {
3676                                facet *marker=slEnd;
3677                                slEnd->next = fAct->shallowCopy(*fAct);
3678                                slEnd = slEnd->next;
3679                                slEnd->prev=marker;
3680                                gcone::lengthOfSearchList++;
3681                        }
3682                        fAct = fAct->next;
3683//                         if(fDeleteMarker!=NULL)
3684//                         {
3685//                                 fDeleteMarker->shallowDelete();
3686//                                 delete(fDeleteMarker);
3687//                                 fDeleteMarker=NULL;
3688//                         }
3689                }
3690                else
3691                        fAct = fAct->next;
3692        }//while(fAct!=NULL)
3693
3694#ifdef gfanp
3695        gettimeofday(&end, 0);
3696        time_enqueue += (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec));
3697#endif
3698//         printf("headUCN@enq=%i\n",slHead->getUCN());
3699        return slHead;
3700}
3701
3702/**
3703* During flip2 every gc->baseRing gets two ringorder_a
3704* To avoid having an awful lot of those in the end we endow
3705* gc->baseRing by a suitable ring with (a,dp,C) and copy all
3706* necessary stuff over
3707* But first we will try to just do an inplace exchange and copying only the
3708* gc->gcBasis
3709*/
3710void gcone::replaceDouble_ringorder_a_ByASingleOne()
3711{
3712        ring srcRing=currRing;
3713        ring replacementRing=rCopy0((ring)this->baseRing);
3714        /*We assume we have (a(),a(),dp) here*/
3715        omFree(replacementRing->order);
3716        replacementRing->order =(int *)omAlloc0(4*sizeof(int/*64*/));
3717        omFree(replacementRing->block0);
3718        replacementRing->block0=(int *)omAlloc0(4*sizeof(int/*64*/));
3719        omFree(replacementRing->block1);
3720        replacementRing->block1=(int *)omAlloc0(4*sizeof(int/*64*/));
3721        omfree(replacementRing->wvhdl);
3722        replacementRing->wvhdl =(int **)omAlloc0(4*sizeof(int/*64*/**));
3723
3724        replacementRing->order[0]=ringorder_a/*64*/;
3725        replacementRing->block0[0]=1;
3726        replacementRing->block1[0]=replacementRing->N;
3727
3728        replacementRing->order[1]=ringorder_dp;
3729        replacementRing->block0[1]=1;
3730        replacementRing->block1[1]=replacementRing->N;
3731
3732        replacementRing->order[2]=ringorder_C;
3733
3734        int64vec *ivw = this->getIntPoint(TRUE);//returns a reference
3735//         assert(this->ivIntPt);
3736        int length=ivw->length();
3737        int/*64*/ *A=(int/*64*/ *)omAlloc0(length*sizeof(int/*64*/));
3738        for (int jj=0;jj<length;jj++)
3739        {
3740                A[jj]=(*ivw)[jj];
3741                if((*ivw)[jj]>=INT_MAX) WarnS("A[jj] exceeds INT_MAX in gcone::replaceDouble_ringorder_a_ByASingleOne!\n");
3742        }
3743        //delete ivw; //Not needed if this->getIntPoint(TRUE)
3744        replacementRing->wvhdl[0]=(int *)A;
3745        replacementRing->block1[0]=length;
3746        /*Finish*/
3747        rComplete(replacementRing);
3748        rChangeCurrRing(replacementRing);
3749        ideal temporaryGroebnerBasis=idrCopyR(this->gcBasis,this->baseRing);
3750        rDelete(this->baseRing);
3751        this->baseRing=rCopy(replacementRing);
3752        this->gcBasis=idCopy(temporaryGroebnerBasis);
3753        /*And back to where we came from*/
3754        rChangeCurrRing(srcRing);
3755        idDelete( (ideal*)&temporaryGroebnerBasis );
3756        rDelete(replacementRing);
3757}
3758
3759/** \brief Compute the gcd of two ints
3760 */
3761static int64 int64gcd(const int64 &a, const int64 &b)
3762{
3763        int64 r, p=a, q=b;
3764        if(p < 0)
3765                p = -p;
3766        if(q < 0)
3767                q = -q;
3768        while(q != 0)
3769        {
3770                r = p % q;
3771                p = q;
3772                q = r;
3773        }
3774        return p;
3775}
3776
3777static int intgcd(const int &a, const int &b)
3778{
3779        int r, p=a, q=b;
3780        if(p < 0)
3781                p = -p;
3782        if(q < 0)
3783                q = -q;
3784        while(q != 0)
3785        {
3786                r = p % q;
3787                p = q;
3788                q = r;
3789        }
3790        return p;
3791}
3792
3793/** \brief Construct a dd_MatrixPtr from a cone's list of facets
3794 * NO LONGER USED
3795 */
3796// inline dd_MatrixPtr gcone::facets2Matrix(const gcone &gc)
3797// {
3798//         facet *fAct;
3799//         fAct = gc.facetPtr;
3800//
3801//         dd_MatrixPtr M;
3802//         dd_rowrange ddrows;
3803//         dd_colrange ddcols;
3804//         ddcols=(this->numVars)+1;
3805//         ddrows=this->numFacets;
3806//         dd_NumberType numb = dd_Integer;
3807//         M=dd_CreateMatrix(ddrows,ddcols);
3808//
3809//         int jj=0;
3810//
3811//         while(fAct!=NULL)
3812//         {
3813//                 int64vec *comp;
3814//                 comp = fAct->getFacetNormal();
3815//                 for(int ii=0;ii<this->numVars;ii++)
3816//                 {
3817//                         dd_set_si(M->matrix[jj][ii+1],(*comp)[ii]);
3818//                 }
3819//                 jj++;
3820//                 delete comp;
3821//                 fAct=fAct->next;
3822//         }
3823//         return M;
3824// }
3825
3826/** \brief Write information about a cone into a file on disk
3827 *
3828 * This methods writes the information needed for the "second" method into a file.
3829 * The file's is divided in sections containing information on
3830 * 1) the ring
3831 * 2) the cone's Groebner Basis
3832 * 3) the cone's facets
3833 * Each line contains exactly one date
3834 * Each section starts with its name in CAPITALS
3835 */
3836void gcone::writeConeToFile(const gcone &gc, bool usingIntPoints)
3837{
3838        int UCN=gc.UCN;
3839        stringstream ss;
3840        ss << UCN;
3841        string UCNstr = ss.str();
3842
3843         string prefix="/tmp/Singular/cone";
3844//         string prefix="./";        //crude hack -> run tests in separate directories
3845        string suffix=".sg";
3846        string filename;
3847        filename.append(prefix);
3848        filename.append(UCNstr);
3849        filename.append(suffix);
3850
3851//         int thisPID = getpid();
3852//         ss << thisPID;
3853//         string strPID = ss.str();
3854//         string prefix="./";
3855
3856        ofstream gcOutputFile(filename.c_str());
3857        assert(gcOutputFile);
3858        facet *fAct;
3859        fAct = gc.facetPtr;
3860        facet *f2Act;
3861        f2Act=fAct->codim2Ptr;
3862
3863        char *ringString = rString(gc.baseRing);
3864
3865        if (!gcOutputFile)
3866        {
3867                WerrorS("Error opening file for writing in writeConeToFile\n");
3868        }
3869        else
3870        {
3871                gcOutputFile << "UCN" << endl;
3872                gcOutputFile << this->UCN << endl;
3873                gcOutputFile << "RING" << endl;
3874                gcOutputFile << ringString << endl;
3875                gcOutputFile << "GCBASISLENGTH" << endl;
3876                gcOutputFile << IDELEMS(gc.gcBasis) << endl;
3877                //Write this->gcBasis into file
3878                gcOutputFile << "GCBASIS" << endl;
3879                for (int ii=0;ii<IDELEMS(gc.gcBasis);ii++)
3880                {
3881                        gcOutputFile << p_String((poly)gc.gcBasis->m[ii],gc.baseRing) << endl;
3882                }
3883
3884                gcOutputFile << "FACETS" << endl;
3885
3886                while(fAct!=NULL)
3887                {
3888                        const int64vec *iv=fAct->getRef2FacetNormal();
3889//                         iv=fAct->getRef2FacetNormal();//->getFacetNormal();
3890                        f2Act=fAct->codim2Ptr;
3891                        for (int ii=0;ii<iv->length();ii++)
3892                        {
3893//                                 if (ii<iv->length()-1)
3894//                                         gcOutputFile << (*iv)[ii] << ",";
3895//                                 else
3896//                                         gcOutputFile << (*iv)[ii] << " ";
3897                                gcOutputFile << (*iv)[ii];
3898                                (ii<iv->length()-1) ? gcOutputFile << "," : gcOutputFile << " ";
3899                        }
3900                        //delete iv;
3901                        while(f2Act!=NULL)
3902                        {
3903                                const int64vec *iv2;
3904                                iv2=f2Act->getRef2FacetNormal();
3905                                for(int jj=0;jj<iv2->length();jj++)
3906                                {
3907//                                         if (jj<iv2->length()-1)
3908//                                                 gcOutputFile << (*iv2)[jj] << ",";
3909//                                         else
3910//                                                 gcOutputFile << (*iv2)[jj] << " ";
3911                                        gcOutputFile << (*iv2)[jj];
3912                                        (jj<iv2->length()-1) ? gcOutputFile << "," : gcOutputFile << " ";
3913                                }
3914                                f2Act = f2Act->next;
3915                        }
3916                        gcOutputFile << endl;
3917                        fAct=fAct->next;
3918                }
3919        gcOutputFile.close();
3920        }
3921        delete [] ringString;
3922
3923}//writeConeToFile(gcone const &gc)
3924
3925/** \brief Reads a cone from a file identified by its number
3926* ||depending on whether flip or flip2 is used, switch the flag flipFlag
3927* ||defaults to 0 => flip
3928* ||1 => flip2
3929*/
3930void gcone::readConeFromFile(int UCN, gcone *gc)
3931{
3932        //int UCN=gc.UCN;
3933        stringstream ss;
3934        ss << UCN;
3935        string UCNstr = ss.str();
3936        int gcBasisLength=0;
3937        size_t found;        //used for find_first_(not)_of
3938
3939        string prefix="/tmp/Singular/cone";
3940        string suffix=".sg";
3941        string filename;
3942        filename.append(prefix);
3943        filename.append(UCNstr);
3944        filename.append(suffix);
3945
3946        ifstream gcInputFile(filename.c_str(), ifstream::in);
3947
3948        ring saveRing=currRing;
3949        //Comment the following if you uncomment the if(line=="RING") part below
3950//          rChangeCurrRing(gc->baseRing);
3951
3952        while( !gcInputFile.eof() )
3953        {
3954                string line;
3955                getline(gcInputFile,line);
3956                if(line=="RING")
3957                {
3958                        getline(gcInputFile,line);
3959                        found = line.find("a(");
3960                        line.erase(0,found+2);
3961                        string strweight;
3962                        strweight=line.substr(0,line.find_first_of(")"));
3963
3964                        int64vec *iv=new int64vec(this->numVars);//
3965                        for(int ii=0;ii<this->numVars;ii++)
3966                        {
3967                                string weight;
3968                                weight=line.substr(0,line.find_first_of(",)"));
3969                                char *w=new char[weight.size()+1];
3970                                strcpy(w,weight.c_str());
3971                                (*iv)[ii]=atol(w/*weight.c_str()*/);//Better to long. Weight bound in Singular:2147483647
3972                                delete[] w;
3973                                line.erase(0,line.find_first_of(",)")+1);
3974                        }
3975                        found = line.find("a(");
3976
3977                        ring newRing;
3978                        if(currRing->order[0]!=ringorder_a/*64*/)
3979                        {
3980                                        newRing=rCopyAndAddWeight(currRing,iv);
3981                        }
3982                        else
3983                        {
3984                                        newRing=rCopy0(currRing);
3985                                        int length=this->numVars;
3986                                        int *A=(int *)omAlloc0(length*sizeof(int));
3987                                        for(int jj=0;jj<length;jj++)
3988                                        {
3989                                                A[jj]=(*iv)[jj];
3990                                        }
3991                                        omFree(newRing->wvhdl[0]);
3992                                        newRing->wvhdl[0]=(int*)A;
3993                                        newRing->block1[0]=length;
3994                        }
3995                        delete iv;
3996                         rComplete(newRing);
3997                        gc->baseRing=rCopy(newRing);
3998                        rDelete(newRing);
3999                        rComplete(gc->baseRing);
4000                        if(currRing!=gc->baseRing)
4001                                rChangeCurrRing(gc->baseRing);
4002                }
4003
4004                if(line=="GCBASISLENGTH")
4005                {
4006                        string strGcBasisLength;
4007                        getline(gcInputFile, line);
4008                        strGcBasisLength = line;
4009                        char *s=new char[strGcBasisLength.size()+1];
4010                        strcpy(s,strGcBasisLength.c_str());
4011                        int size=atoi(s/*strGcBasisLength.c_str()*/);
4012                        delete[] s;
4013                        gcBasisLength=size;
4014                        gc->gcBasis=idInit(size,1);
4015                }
4016                if(line=="GCBASIS")
4017                {
4018                        for(int jj=0;jj<gcBasisLength;jj++)
4019                        {
4020                                getline(gcInputFile,line);
4021                                //magically convert strings into polynomials
4022                                //polys.cc:p_Read
4023                                //check until first occurance of + or -
4024                                //data or c_str
4025//                                 poly strPoly;//=pInit();//Ought to be inside the while loop, but that will eat your memory
4026                                poly resPoly=pInit();        //The poly to be read in
4027                                while(!line.empty())
4028                                {
4029                                        poly strPoly;//=pInit();
4030
4031                                        string strMonom, strCoeff, strCoeffNom, strCoeffDenom;
4032                                        bool hasCoeffInQ = FALSE;        //does the polynomial have rational coeff?
4033                                        bool hasNegCoeff = FALSE;        //or a negative one?
4034                                        found = line.find_first_of("+-");        //get the first monomial
4035                                        string tmp;
4036                                        tmp=line[found];
4037//                                         if(found!=0 && (tmp.compare("-")==0) )
4038//                                                 hasNegCoeff = TRUE;        //We have a coeff < 0
4039                                        if(found==0)
4040                                        {
4041                                                if(tmp.compare("-")==0)
4042                                                        hasNegCoeff = TRUE;
4043                                                line.erase(0,1);        //remove leading + or -
4044                                                found = line.find_first_of("+-");        //adjust found
4045                                        }
4046                                        strMonom = line.substr(0,found);
4047                                        line.erase(0,found);
4048                                        number nCoeff=nInit(1);
4049                                        number nCoeffNom=nInit(1);
4050                                        number nCoeffDenom=nInit(1);
4051                                        found = strMonom.find_first_of("/");
4052                                        if(found!=string::npos)        //i.e. "/" exists in strMonom
4053                                        {
4054                                                hasCoeffInQ = TRUE;
4055                                                strCoeffNom=strMonom.substr(0,found);
4056                                                strCoeffDenom=strMonom.substr(found+1,strMonom.find_first_not_of("1234567890",found+1));
4057                                                strMonom.erase(0,found);
4058                                                strMonom.erase(0,strMonom.find_first_not_of("1234567890/"));
4059                                                char *Nom=new char[strCoeffNom.size()+1];
4060                                                char *Denom=new char[strCoeffDenom.size()+1];
4061                                                strcpy(Nom,strCoeffNom.c_str());
4062                                                strcpy(Denom,strCoeffDenom.c_str());
4063                                                nRead(Nom/*strCoeffNom.c_str()*/, &nCoeffNom);
4064                                                nRead(Denom/*strCoeffDenom.c_str()*/, &nCoeffDenom);
4065                                                delete[] Nom;
4066                                                delete[] Denom;
4067                                        }
4068                                        else
4069                                        {
4070                                                found = strMonom.find_first_not_of("1234567890");
4071                                                strCoeff = strMonom.substr(0,found);
4072                                                if(!strCoeff.empty())
4073                                                {
4074                                                        char *coeff = new char[strCoeff.size()+1];
4075                                                        strcpy(coeff, strCoeff.c_str());
4076                                                         nRead(coeff/*strCoeff.c_str()*/,&nCoeff);
4077                                                        delete[] coeff;
4078                                                }
4079                                        }
4080                                        char* monom = new char[strMonom.size()+1];
4081                                        strcpy(monom, strMonom.c_str());
4082                                        p_Read(monom,strPoly,currRing);        //strPoly:=monom
4083                                        delete[] monom;
4084                                        switch (hasCoeffInQ)
4085                                        {
4086                                                case TRUE:
4087                                                        if(hasNegCoeff)
4088                                                                nCoeffNom=nInpNeg(nCoeffNom);
4089                                                        pSetCoeff(strPoly, nDiv(nCoeffNom, nCoeffDenom));
4090                                                        break;
4091                                                case FALSE:
4092                                                        if(hasNegCoeff)
4093                                                                nCoeff=nInpNeg(nCoeff);
4094                                                        if(!nIsOne(nCoeff))
4095                                                        {
4096                                                                pSetCoeff(strPoly, nCoeff );
4097                                                        }
4098                                                        break;
4099                                        }
4100                                                //pSetCoeff(strPoly, (number) intCoeff);//Why is this set to zero instead of 1???
4101                                        if(pIsConstantComp(resPoly))
4102                                        {
4103                                                resPoly=pCopy(strPoly);
4104                                                pDelete(&strPoly);
4105                                        }
4106                                        else
4107                                        {
4108//                                                 poly tmp=pAdd(pCopy(resPoly),strPoly);//foo is destroyed
4109//                                                 pDelete(&resPoly);
4110//                                                 resPoly=tmp;
4111//                                                 pDelete(&tmp);
4112                                                resPoly=pAdd(resPoly,strPoly);//pAdd = p_Add_q, destroys args
4113                                        }
4114                                        /*if(nCoeff!=NULL)
4115                                                nDelete(&nCoeff);*/ //NOTE This may cause a crash on certain examples...
4116                                        nDelete(&nCoeffNom);
4117                                        nDelete(&nCoeffDenom);
4118                                }//while(!line.empty())
4119                                gc->gcBasis->m[jj]=pCopy(resPoly);
4120                                pDelete(&resPoly);        //reset
4121                        }
4122//                         break;
4123                }//if(line=="GCBASIS")
4124                if(line=="FACETS")
4125                {
4126                        facet *fAct=gc->facetPtr;
4127                        while(fAct!=NULL)
4128                        {
4129                                getline(gcInputFile,line);
4130                                found = line.find("\t");
4131                                string normalString=line.substr(0,found);
4132                                int64vec *fN = new int64vec(this->numVars);
4133                                for(int ii=0;ii<this->numVars;ii++)
4134                                {
4135                                        string component;
4136                                        found = normalString.find(",");
4137                                        component=normalString.substr(0,found);
4138                                        char *sComp = new char[component.size()+1];
4139                                        strcpy(sComp,component.c_str());
4140                                        (*fN)[ii]=atol(sComp/*component.c_str()*/);
4141                                        delete[] sComp;
4142                                        normalString.erase(0,found+1);
4143                                }
4144                                /*Only the following line needs to be commented out if you decide not to delete fNormals*/
4145//                                 fAct->setFacetNormal(fN);
4146                                delete(fN);
4147                                fAct = fAct->next;        //Booh, this is ugly
4148                        }
4149                        break; //NOTE Must always be in the last if-block!
4150                }
4151        }//while(!gcInputFile.eof())
4152        gcInputFile.close();
4153        rChangeCurrRing(saveRing);
4154}
4155
4156
4157/** \brief Sort the rays of a facet lexicographically
4158*/
4159// void gcone::sortRays(gcone *gc)
4160// {
4161//         facet *fAct;
4162//         fAct = this->facetPtr->codim2Ptr;
4163//         while(fAct->next!=NULL)
4164//         {
4165//                 if(fAct->fNormal->compare(fAct->fNormal->next)==-1
4166//         }
4167// }
4168
4169/** \brief Gather the output
4170* List of lists
4171* If heuristic==1 readConeFromFile() is called once more on every cone. This may slow down the computation but it also
4172* allows us to rDelete(gcDel->baseRing) and the such in gcone::noRevS.
4173*\param *gc Pointer to gcone, preferably gcRoot ;-)
4174*\param n the number of cones as determined by gcRoot->getCounter()
4175*
4176*/
4177lists lprepareResult(gcone *gc, const int n)
4178{
4179        gcone *gcAct;
4180        gcAct = gc;
4181        facet *fAct;
4182        fAct = gc->facetPtr;
4183
4184        lists res=(lists)omAllocBin(slists_bin);
4185        res->Init(n);        //initialize to store n cones
4186        for(int ii=0;ii<n;ii++)
4187        {
4188                if(gfanHeuristic==1)// && gcAct->getUCN()>1)
4189                {
4190                        gcAct->readConeFromFile(gcAct->getUCN(),gcAct);
4191//                         rChangeCurrRing(gcAct->getBaseRing());//NOTE memleak?
4192                }
4193                rChangeCurrRing(gcAct->getRef2BaseRing());
4194                res->m[ii].rtyp=LIST_CMD;
4195                lists l=(lists)omAllocBin(slists_bin);
4196                l->Init(6);
4197                l->m[0].rtyp=INT_CMD;
4198                l->m[0].data=(void*)(long)gcAct->getUCN();
4199                l->m[1].rtyp=IDEAL_CMD;
4200                /*The following is necessary for leaves in the tree of cones
4201                * Since we don't use them in the computation and gcBasis is
4202                * set to (poly)NULL in noRevS we need to get this back here.
4203                */
4204//                 if(gcAct->gcBasis->m[0]==(poly)NULL)
4205//                 if(gfanHeuristic==1 && gcAct->getUCN()>1)
4206//                         gcAct->readConeFromFile(gcAct->getUCN(),gcAct);
4207//                 ring saveRing=currRing;
4208//                 ring tmpRing=gcAct->getBaseRing;
4209//                 rChangeCurrRing(tmpRing);
4210//                 l->m[1].data=(void*)idrCopyR_NoSort(gcAct->gcBasis,gcAct->getBaseRing());
4211//                 l->m[1].data=(void*)idrCopyR(gcAct->gcBasis,gcAct->getBaseRing());//NOTE memleak?
4212                l->m[1].data=(void*)idrCopyR(gcAct->gcBasis,gcAct->getRef2BaseRing());
4213//                 rChangeCurrRing(saveRing);
4214
4215                l->m[2].rtyp=INTVEC_CMD;
4216                int64vec iv=(gcAct->f2M(gcAct,gcAct->facetPtr));//NOTE memleak?
4217                l->m[2].data=(void*)iv64Copy(&iv);
4218
4219                l->m[3].rtyp=LIST_CMD;
4220                        lists lCodim2List = (lists)omAllocBin(slists_bin);
4221                        lCodim2List->Init(gcAct->numFacets);
4222                        fAct = gcAct->facetPtr;//fAct->codim2Ptr;
4223                        int jj=0;
4224                        while(fAct!=NULL && jj<gcAct->numFacets)
4225                        {
4226                                lCodim2List->m[jj].rtyp=INTVEC_CMD;
4227                                int64vec ivC2=(gcAct->f2M(gcAct,fAct,2));
4228                                lCodim2List->m[jj].data=(void*)iv64Copy(&ivC2);
4229                                jj++;
4230                                fAct = fAct->next;
4231                        }
4232                l->m[3].data=(void*)lCodim2List;
4233                l->m[4].rtyp=INTVEC_CMD/*RING_CMD*/;
4234                l->m[4].data=(void*)(gcAct->getIntPoint/*BaseRing*/());
4235                l->m[5].rtyp=INT_CMD;
4236                l->m[5].data=(void*)(long)gcAct->getPredUCN();
4237                res->m[ii].data=(void*)l;
4238                gcAct = gcAct->next;
4239        }
4240        return res;
4241}
4242
4243/** Convert gc->gcRays into an intvec in order to be used with bbcone stuff*/
4244intvec *gcRays2Intmat(gcone *gc)
4245{
4246  int r = gc->numRays;
4247  int c = gc->numVars;  //Spalten
4248  intvec *res = new intvec(r,c,(int)0);
4249
4250  int offset=0;
4251  for(int ii=0;ii<gc->numRays;ii++)
4252  {
4253    int64vec *ivTmp=iv64Copy(gc->gcRays[ii]);
4254    for(int jj=0;jj<pVariables;jj++)
4255      (*res)[offset+jj]=(int)(*ivTmp)[jj];
4256    offset += pVariables;
4257    delete ivTmp;
4258  }
4259  return res;
4260}
4261
4262/** \brief Put stuff in gfanlib's datatype gfan::ZFan
4263*/
4264void prepareGfanLib(gcone *gc, gfan::ZFan *fan)
4265{
4266  using namespace gfan;
4267  int ambientDimension = gc->numVars;
4268  gcone *gcAct;
4269  gcAct = gc;
4270
4271  //Iterate over all cones and adjoin to PolyhedralFan
4272   while(gcAct!=NULL)
4273  {
4274    intvec *rays=gcRays2Intmat(gcAct);
4275    ZMatrix zm = *intmat2ZMatrix(rays);
4276    delete rays;
4277    ZCone *zc = new ZCone();
4278    *zc = ZCone::givenByRays(zm, gfan::ZMatrix(0, zm.getWidth()));
4279//     delete &zm;
4280    zc->canonicalize();//As per Anders' hint
4281    fan->insert(*zc);
4282//     delete zc;
4283    gcAct=gcAct->next;
4284  }
4285}
4286
4287/** \brief Write facets of a cone into a matrix
4288* Takes a pointer to a facet as 2nd arg
4289* f should always point to gc->facetPtr
4290* param n is used to determine whether it operates in codim 1 or 2
4291* We have to cast the int64vecs to int64vec due to issues with list structure
4292*/
4293inline int64vec gcone::f2M(gcone *gc, facet *f, int n)
4294{
4295        facet *fAct;
4296        int64vec *res;//=new int64vec(this->numVars);
4297//         int codim=n;
4298//         int bound;
4299//         if(f==gc->facetPtr)
4300        if(n==1)
4301        {
4302                int64vec *m1Res=new int64vec(gc->numFacets,gc->numVars,0);
4303                res = iv64Copy(m1Res);
4304                fAct = gc->facetPtr;
4305                delete m1Res;
4306//                 bound = gc->numFacets*(this->numVars);
4307        }
4308        else
4309        {
4310                fAct = f->codim2Ptr;
4311                int64vec *m2Res = new int64vec(f->numCodim2Facets,gc->numVars,0);
4312                res = iv64Copy(m2Res);
4313                delete m2Res;
4314//                 bound = fAct->numCodim2Facets*(this->numVars);
4315
4316        }
4317        int ii=0;
4318        while(fAct!=NULL )//&& ii < bound )
4319        {
4320                const int64vec *fNormal;
4321                fNormal = fAct->getRef2FacetNormal();//->getFacetNormal();
4322                for(int jj=0;jj<this->numVars;jj++)
4323                {
4324                        (*res)[ii]=(int)(*fNormal)[jj];//This is ugly and prone to overflow
4325                        ii++;
4326                }
4327                fAct = fAct->next;
4328        }
4329        return *res;
4330}
4331
4332int gcone::counter=0;
4333int gfanHeuristic;
4334int gcone::lengthOfSearchList;
4335int gcone::maxSize;
4336dd_MatrixPtr gcone::dd_LinealitySpace;
4337int64vec *gcone::hilbertFunction;
4338#ifdef gfanp
4339// int gcone::lengthOfSearchList=0;
4340float gcone::time_getConeNormals;
4341float gcone::time_getCodim2Normals;
4342float gcone::t_getExtremalRays;
4343float gcone::t_ddPolyh;
4344float gcone::time_flip;
4345float gcone::time_flip2;
4346float gcone::t_areEqual;
4347float gcone::t_markings;
4348float gcone::t_dd;
4349float gcone::t_kStd=0;
4350float gcone::time_enqueue;
4351float gcone::time_computeInv;
4352float gcone::t_ddMC;
4353float gcone::t_mI;
4354float gcone::t_iP;
4355float gcone::t_isParallel;
4356unsigned gcone::parallelButNotEqual=0;
4357unsigned gcone::numberOfFacetChecks=0;
4358#endif
4359int gcone::numVars;
4360bool gcone::hasHomInput=FALSE;
4361int64vec *gcone::ivZeroVector;
4362// ideal gfan(ideal inputIdeal, int h)
4363/** Main routine
4364 * The first and second parameter are mandatory. The third (and maybe fourth) parameter is for Janko :)
4365 */
4366#ifndef USE_ZFAN
4367lists grfan(ideal inputIdeal, int h, bool singleCone=FALSE)
4368#else
4369gfan::ZFan* grfan(ideal inputIdeal, int h, bool singleCone=FALSE)
4370#endif
4371{
4372        lists lResList; //this is the object we return
4373        gfan::ZFan *zResFan = new gfan::ZFan(pVariables);
4374
4375        if(rHasGlobalOrdering(currRing))
4376        {
4377//                 int numvar = pVariables;
4378                gfanHeuristic = h;
4379
4380                enum searchMethod {
4381                        reverseSearch,
4382                        noRevS
4383                };
4384
4385                searchMethod method;
4386                method = noRevS;
4387
4388                ring inputRing=currRing;        // The ring the user entered
4389//                 ring rootRing;                        // The ring associated to the target ordering
4390
4391                dd_set_global_constants();
4392                if(method==noRevS)
4393                {
4394                        gcone *gcRoot = new gcone(currRing,inputIdeal);
4395                        gcone *gcAct;
4396                        gcAct = gcRoot;
4397                        gcone::numVars=pVariables;
4398                        //gcAct->numVars=pVariables;//NOTE is now static
4399                        gcAct->getGB(inputIdeal);
4400                        /*Check whether input is homogeneous
4401                        if TRUE each facet intersects the positive orthant, so we don't need the
4402                        flippability test in getConeNormals & getExtremalRays
4403                        */
4404                        if(idHomIdeal(gcAct->gcBasis,NULL))//disabled for tests
4405                        {
4406                                gcone::hasHomInput=TRUE;
4407//                                 gcone::hilbertFunction=hHstdSeries(inputIdeal,NULL,NULL,NULL,currRing);
4408                        }
4409                        else
4410                        {
4411                                gcone::ivZeroVector = new int64vec(pVariables);
4412                                for(int ii=0;ii<pVariables;ii++)
4413                                        (*gcone::ivZeroVector)[ii]=0;
4414                        }
4415
4416                        if(isMonomial(gcAct->gcBasis))
4417                        {//FIXME
4418                                WerrorS("Monomial input - terminating");
4419                                dd_free_global_constants();
4420                                //This is filthy
4421                                goto pointOfNoReturn;
4422                        }
4423                        gcAct->getConeNormals(gcAct->gcBasis);
4424                        gcone::dd_LinealitySpace = gcAct->computeLinealitySpace();
4425                        gcAct->getExtremalRays(*gcAct);
4426                        if(singleCone==FALSE)//Is Janko here?
4427                        {//Compute the whole fan
4428                          gcAct->noRevS(*gcAct);        //Here we go!
4429                        }
4430                        //Switch back to the ring the computation was started in
4431                        rChangeCurrRing(inputRing);
4432                        //res=gcAct->gcBasis;
4433                        //Below is a workaround, since gcAct->gcBasis gets deleted in noRevS
4434#ifndef USE_ZFAN
4435                        lResList=lprepareResult(gcRoot,gcRoot->getCounter());
4436#else
4437                        prepareGfanLib(gcRoot,zResFan);
4438#endif
4439                        /*Cleanup*/
4440                        gcone *gcDel;
4441                        gcDel = gcRoot;
4442                        gcAct = gcRoot;
4443                        while(gcAct!=NULL)
4444                        {
4445                                gcDel = gcAct;
4446                                gcAct = gcAct->next;
4447//                                 delete gcDel;
4448                        }
4449                }//method==noRevS
4450                dd_FreeMatrix(gcone::dd_LinealitySpace);
4451                dd_free_global_constants();
4452        }//rHasGlobalOrdering
4453        else
4454        {
4455                //Simply return an empty list
4456                WerrorS("Ring has non-global ordering.\nThis function requires your current ring to be endowed with a global ordering.\n Now terminating!");
4457//                 gcone *gcRoot=new gcone();
4458//                 gcone *gcPtr = gcRoot;
4459//                 for(int ii=0;ii<10000;ii++)
4460//                 {
4461//                         gcPtr->setBaseRing(currRing);
4462//                         facet *fPtr=gcPtr->facetPtr=new facet();
4463//                         for(int jj=0;jj<5;jj++)
4464//                         {
4465//                                 int64vec *iv=new int64vec(pVariables);
4466//                                 fPtr->setFacetNormal(iv);
4467//                                 delete(iv);
4468//                                 fPtr->next=new facet();
4469//                                 fPtr=fPtr->next;
4470//                         }
4471//                         gcPtr->next=new gcone();
4472//                         gcPtr->next->prev=gcPtr;
4473//                         gcPtr=gcPtr->next;
4474//                 }
4475//                 gcPtr=gcRoot;
4476//                 while(gcPtr!=NULL)
4477//                 {
4478//                         gcPtr=gcPtr->next;
4479// //                         delete(gcPtr->prev);
4480//                 }
4481                goto pointOfNoReturn;
4482        }
4483        /*Return result*/
4484#ifdef gfanp
4485        cout << endl << "t_getConeNormals:" << gcone::time_getConeNormals << endl;
4486        /*cout << "t_getCodim2Normals:" << gcone::time_getCodim2Normals << endl;
4487        cout << "  t_ddMC:" << gcone::t_ddMC << endl;
4488        cout << "  t_mI:" << gcone::t_mI << endl;
4489        cout << "  t_iP:" << gcone::t_iP << endl;*/
4490        cout << "t_getExtremalRays:" << gcone::t_getExtremalRays << endl;
4491        cout << "  t_ddPolyh:" << gcone::t_ddPolyh << endl;
4492        cout << "t_Flip:" << gcone::time_flip << endl;
4493        cout << "  t_markings:" << gcone::t_markings << endl;
4494        cout << "  t_dd:" << gcone::t_dd << endl;
4495        cout << "  t_kStd:" << gcone::t_kStd << endl;
4496        cout << "t_Flip2:" << gcone::time_flip2 << endl;
4497        cout << "  t_dd:" << gcone::t_dd << endl;
4498        cout << "  t_kStd:" << gcone::t_kStd << endl;
4499        cout << "t_computeInv:" << gcone::time_computeInv << endl;
4500        cout << "t_enqueue:" << gcone::time_enqueue << endl;
4501        cout << "  t_areEqual:" <<gcone::t_areEqual << endl;
4502        cout << "t_isParallel:" <<gcone::t_isParallel << endl;
4503        cout << endl;
4504        cout << "Checked " << gcone::numberOfFacetChecks << " Facets" << endl;
4505        cout << " out of which there were " << gcone::parallelButNotEqual << " parallel but not equal." << endl;
4506#endif
4507        printf("Maximum length of list of facets: %i", gcone::maxSize);
4508pointOfNoReturn:
4509#ifndef USE_ZFAN
4510        return lResList;
4511#else
4512        return zResFan;
4513#endif
4514}
4515
4516/** Compute a single Gröbner cone by specifying an ideal and a weight vector.
4517 * NOTE: We do NOT check whether the vector is from the relative interior of the cone.
4518 * That is upon the user to assure.
4519 */
4520// lists grcone_by_intvec(ideal inputIdeal)
4521// {
4522//   if( (rRingOrder_t)currRing->order[0] == ringorder_wp)
4523//   {
4524//     lists lResList;
4525//     lResList=grfan(inputIdeal, 0, TRUE);
4526//   }
4527//   else
4528//     WerrorS("Need wp ordering");
4529// }
4530
4531#endif
Note: See TracBrowser for help on using the repository browser.