source: git/kernel/gfan.cc @ 854405

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