source: git/dyn_modules/callgfanlib/gfan.cc @ 58b407

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