source: git/kernel/gfan.cc @ 76e501

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