source: git/kernel/gfan.cc @ c888ad

spielwiese
Last change on this file since c888ad was c888ad, checked in by Martin Monerjan, 14 years ago
Rudimentary garbage collection Documentation Bugfix in noRevS if( ii==0 || (ii>0 && SearchListAct==NULL) ) for the case that the first facet of a cone is non-flippable git-svn-id: file:///usr/local/Singular/svn/trunk@12005 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 70.9 KB
Line 
1/*
2Compute the Groebner fan of an ideal
3$Author: monerjan $
4$Date: 2009-07-24 08:59:45 $
5$Header: /exports/cvsroot-2/cvsroot/kernel/gfan.cc,v 1.82 2009-07-24 08:59:45 monerjan Exp $
6$Id: gfan.cc,v 1.82 2009-07-24 08:59:45 monerjan Exp $
7*/
8
9#include "mod2.h"
10
11#ifdef HAVE_GFAN
12
13#include "kstd1.h"
14#include "kutil.h"
15#include "intvec.h"
16#include "polys.h"
17#include "ideals.h"
18#include "kmatrix.h"
19#include "fast_maps.h"  //Mapping of ideals
20#include "maps.h"
21#include "ring.h"
22#include "prCopy.h"
23#include <iostream>
24#include <bitset>
25#include <fstream>      //read-write cones to files
26#include <gmp.h>
27#include <string>
28#include <sstream>
29//#include <gmpxx.h>
30
31/*DO NOT REMOVE THIS*/
32#ifndef GMPRATIONAL
33#define GMPRATIONAL
34#endif
35
36//Hacks for different working places
37#define ITWM
38
39#ifdef UNI
40#include "/users/urmel/alggeom/monerjan/cddlib/include/setoper.h" //Support for cddlib. Dirty hack
41#include "/users/urmel/alggeom/monerjan/cddlib/include/cdd.h"
42#endif
43
44#ifdef HOME
45#include "/home/momo/studium/diplomarbeit/cddlib/include/setoper.h"
46#include "/home/momo/studium/diplomarbeit/cddlib/include/cdd.h"
47#endif
48
49#ifdef ITWM
50#include "/u/slg/monerjan/cddlib/include/setoper.h"
51#include "/u/slg/monerjan/cddlib/include/cdd.h"
52#include "/u/slg/monerjan/cddlib/include/cddmp.h"
53#endif
54
55#ifndef gfan_DEBUG
56#define gfan_DEBUG
57#endif
58
59//#include gcone.h
60using namespace std;
61
62/**
63*\brief Class facet
64*       Implements the facet structure as a linked list
65*
66*/
67class facet
68{
69        private:
70                /** \brief Inner normal of the facet, describing it uniquely up to isomorphism */
71                intvec *fNormal;
72               
73                /** \brief An interior point of the facet*/
74                intvec *interiorPoint;
75               
76                /** \brief Universal Cone Number
77                * The number of the cone the facet belongs to, Set in getConeNormals()
78                */
79                int UCN;
80               
81                /** \brief The codim of the facet
82                */
83                int codim;
84               
85                /** \brief The Groebner basis on the other side of a shared facet
86                 *
87                 * In order not to have to compute the flipped GB twice we store the basis we already get
88                 * when identifying search facets. Thus in the next step of the reverse search we can
89                 * just copy the old cone and update the facet and the gcBasis.
90                 * facet::flibGB is set via facet::setFlipGB() and printed via facet::printFlipGB
91                 */
92                ideal flipGB;           //The Groebner Basis on the other side, computed via gcone::flip               
93                       
94        public: 
95                /** \brief Boolean value to indicate whether a facet is flippable or not
96                * This is also used to mark facets that nominally are flippable but which do
97                * not intersect with the positive orthant. This check is done in gcone::getCodim2Normals
98                */     
99                bool isFlippable;       //**flippable facet? */
100                bool isIncoming;        //Is the facet incoming or outgoing in the reverse search?
101                facet *next;            //Pointer to next facet
102                facet *prev;            //Pointer to predecessor. Needed for the SearchList in noRevS
103                facet *codim2Ptr;       //Pointer to (codim-2)-facet. Bit of recursion here ;-)
104                int numCodim2Facets;    //#of (codim-2)-facets of this facet. Set in getCodim2Normals()
105                ring flipRing;          //the ring on the other side of the facet
106                                               
107                /** The default constructor. Do I need a constructor of type facet(intvec)? */
108                facet()                 
109                {
110                        // Pointer to next facet.  */
111                        /* Defaults to NULL. This way there is no need to check explicitly */
112                        this->fNormal=NULL;
113                        this->interiorPoint=NULL;               
114                        this->UCN=0;
115                        this->codim2Ptr=NULL;
116                        this->codim=1;          //default for (codim-1)-facets
117                        this->numCodim2Facets=0;
118                        this->flipGB=NULL;
119                        this->isIncoming=FALSE;
120                        this->next=NULL;
121                        this->prev=NULL;               
122                        this->flipRing=NULL;
123                        this->isFlippable=FALSE;
124                }
125               
126                /** \brief Constructor for facets of codim >= 2
127                */
128                facet(int const &n)
129                {
130                        this->fNormal=NULL;
131                        this->interiorPoint=NULL;                       
132                        this->UCN=0;
133                        this->codim2Ptr=NULL;
134                        if(n>1)
135                        {
136                                this->codim=n;
137                        }//NOTE Handle exception here!                 
138                        this->numCodim2Facets=0;
139                        this->flipGB=NULL;
140                        this->isIncoming=FALSE;
141                        this->next=NULL;
142                        this->prev=NULL;
143                        this->flipRing=NULL;
144                        this->isFlippable=FALSE;
145                }
146               
147                /** The default destructor */
148                ~facet(){;}
149               
150                /** \brief Comparison of facets*/
151                bool areEqual(facet &f, facet &g)
152                {
153                        bool res = TRUE;
154                        intvec *fNormal = new intvec(pVariables);
155                        intvec *gNormal = new intvec(pVariables);
156                        fNormal = f.getFacetNormal();
157                        gNormal = g.getFacetNormal();
158                        if((fNormal == gNormal))//||(gcone::isParallel(fNormal,gNormal)))
159                        {
160                                if(f.numCodim2Facets==g.numCodim2Facets)
161                                {
162                                        facet *f2Act;
163                                        facet *g2Act;
164                                        f2Act = f.codim2Ptr;
165                                        g2Act = g.codim2Ptr;
166                                        intvec *f2N = new intvec(pVariables);
167                                        intvec *g2N = new intvec(pVariables);
168                                        while(f2Act->next!=NULL && g2Act->next!=NULL)
169                                        {
170                                                for(int ii=0;ii<f2N->length();ii++)
171                                                {
172                                                        if(f2Act->getFacetNormal() != g2Act->getFacetNormal())
173                                                        {
174                                                                res=FALSE;                                                             
175                                                        }
176                                                        if (res==FALSE)
177                                                                break;
178                                                }
179                                                if(res==FALSE)
180                                                        break;
181                                               
182                                                f2Act = f2Act->next;
183                                                g2Act = g2Act->next;
184                                        }//while
185                                }//if           
186                        }//if
187                        else
188                        {
189                                res = FALSE;
190                        }
191                        return res;
192                }
193               
194                /** Stores the facet normal \param intvec*/
195                void setFacetNormal(intvec *iv)
196                {
197                        this->fNormal = ivCopy(iv);                     
198                }
199               
200                /** Hopefully returns the facet normal */
201                intvec *getFacetNormal()
202                {                               
203                        return this->fNormal;
204                }
205               
206                /** Method to print the facet normal*/
207                void printNormal()
208                {
209                        fNormal->show();
210                }
211               
212                /** Store the flipped GB*/
213                void setFlipGB(ideal I)
214                {
215                        this->flipGB=idCopy(I);
216                }
217               
218                /** Return the flipped GB*/
219                ideal getFlipGB()
220                {
221                        return this->flipGB;
222                }
223               
224                /** Print the flipped GB*/
225                void printFlipGB()
226                {
227                        idShow(this->flipGB);
228                }
229               
230                /** Set the UCN */
231                void setUCN(int n)
232                {
233                        this->UCN=n;
234                }
235               
236                /** \brief Get the UCN
237                * Returns the UCN iff this != NULL, else -1
238                */
239                int getUCN()
240                {
241                        if(this!=NULL)
242                                return this->UCN;
243                        else
244                                return -1;
245                }
246               
247                /** Store an interior point of the facet */
248                void setInteriorPoint(intvec *iv)
249                {
250                        this->interiorPoint = ivCopy(iv);
251                }
252               
253                intvec *getInteriorPoint()
254                {
255                        return this->interiorPoint;
256                }       
257               
258                /** \brief Debugging function
259                * prints the facet normal an all (codim-2)-facets that belong to it
260                */
261                void fDebugPrint()
262                {                       
263                        facet *codim2Act;                       
264                        codim2Act = this->codim2Ptr;
265                        intvec *fNormal;
266                        fNormal = this->getFacetNormal();
267                        intvec *f2Normal;
268                        cout << "=======================" << endl;
269                        cout << "Facet normal = (";
270                        for(int ii=0;ii<pVariables;ii++)
271                        {
272                                cout << (*fNormal)[ii] << ",";
273                        }
274                        if(this->isFlippable==TRUE)
275                                cout << ")" << endl;
276                        else
277                                cout << ")*" << endl;   //This case should never happen in SLA!
278                        cout << "-----------------------" << endl;
279                        cout << "Codim2 facets:" << endl;
280                        while(codim2Act!=NULL)
281                        {
282                                f2Normal = codim2Act->getFacetNormal();
283                                cout << "(";
284                                for(int jj=0;jj<pVariables;jj++)
285                                {
286                                        cout << (*f2Normal)[jj] << ",";
287                                }
288                                cout << ")" << endl;
289                                codim2Act = codim2Act->next;
290                        }
291                        cout << "=======================" << endl;
292                }
293               
294                /*bool isFlippable(intvec &load)
295                {
296                        bool res=TRUE;                 
297                        int jj;
298                        for (int jj = 0; jj<load.length(); jj++)
299                        {
300                                intvec *ivCanonical = new intvec(load.length());
301                                (*ivCanonical)[jj]=1;                           
302                                if (ivMult(&load,ivCanonical)<0)
303                                {
304                                        res=FALSE;
305                                        break;
306                                }
307                        }
308                        return res;
309                       
310                        /*while (dotProduct(load,ivCanonical)>=0)
311                        {
312                                if (jj!=this->numVars)
313                                {
314                                        intvec *ivCanonical = new intvec(this->numVars);
315                                        (*ivCanonical)[jj]=1;                   
316                                        res=TRUE;
317                                        jj += 1;
318                                }
319                        }
320                        if (jj==this->numVars)
321                        {                       
322                                delete ivCanonical;
323                                return FALSE;
324                        }
325                        else
326                        {
327                                delete ivCanonical;
328                                return TRUE;
329                }*/                                             
330                //}//bool isFlippable(facet &f)
331               
332               
333                friend class gcone;     //Bad style
334};
335
336/**
337*\brief Implements the cone structure
338*
339* A cone is represented by a linked list of facet normals
340* @see facet
341*/
342/*class gcone
343finally this should become s.th. like gconelib.{h,cc} to provide an API
344*/
345class gcone
346{
347        private:               
348                ring rootRing;          //good to know this -> generic walk
349                ideal inputIdeal;       //the original
350                ring baseRing;          //the basering of the cone             
351                /* TODO in order to save memory use pointers to rootRing and inputIdeal instead */
352                intvec *ivIntPt;        //an interior point of the cone
353                int UCN;                //unique number of the cone
354                static int counter;
355               
356        public: 
357                /** \brief Pointer to the first facet */
358                facet *facetPtr;        //Will hold the adress of the first facet; set by gcone::getConeNormals
359               
360                /** # of variables in the ring */
361                int numVars;            //#of variables in the ring
362               
363                /** # of facets of the cone
364                * This value is set by gcone::getConeNormals
365                */
366                int numFacets;          //#of facets of the cone
367               
368                /**
369                * At least as a workaround we store the irredundant facets of a matrix here.
370                * Otherwise, since we throw away non-flippable facets, facets2Matrix will not
371                * yield all the necessary information
372                */
373                dd_MatrixPtr ddFacets;  //Matrix to store irredundant facets of the cone
374               
375                /** Contains the Groebner basis of the cone. Is set by gcone::getGB(ideal I)*/
376                ideal gcBasis;          //GB of the cone, set by gcone::getGB();
377                gcone *next;            //Pointer to *previous* cone in search tree     
378               
379                /** \brief Default constructor.
380                *
381                * Initialises this->next=NULL and this->facetPtr=NULL
382                */
383                gcone()
384                {
385                        this->next=NULL;
386                        this->facetPtr=NULL;    //maybe this->facetPtr = new facet();                   
387                        this->baseRing=currRing;
388                        this->counter++;
389                        this->UCN=this->counter;                       
390                        this->numFacets=0;
391                        this->ivIntPt=NULL;
392                }
393               
394                /** \brief Constructor with ring and ideal
395                *
396                * This constructor takes the root ring and the root ideal as parameters and stores
397                * them in the private members gcone::rootRing and gcone::inputIdeal
398                * Since knowledge of the root ring is only needed when using reverse search,
399                * this constructor is not needed when using the "second" method
400                */
401                gcone(ring r, ideal I)
402                {
403                        this->next=NULL;
404                        this->facetPtr=NULL;                   
405                        this->rootRing=r;
406                        this->inputIdeal=I;
407                        this->baseRing=currRing;
408                        this->counter++;
409                        this->UCN=this->counter;                       
410                        this->numFacets=0;
411                        this->ivIntPt=NULL;
412                }
413               
414                /** \brief Copy constructor
415                *
416                * Copies a cone, sets this->gcBasis to the flipped GB
417                * Call this only after a successful call to gcone::flip which sets facet::flipGB
418                */             
419                gcone(const gcone& gc, const facet &f)
420                {
421                        this->next=NULL;
422                        this->numVars=gc.numVars;                                               
423                        this->counter++;
424                        this->UCN=this->counter;                       
425                        this->facetPtr=NULL;
426                        this->gcBasis=idCopy(f.flipGB);
427                        this->inputIdeal=idCopy(this->gcBasis);
428                        this->baseRing=rCopy(f.flipRing);
429                        this->numFacets=0;
430                        this->ivIntPt=NULL;
431                        //rComplete(this->baseRing);
432                        //rChangeCurrRing(this->baseRing);
433                }
434               
435                /** \brief Default destructor
436                */
437                ~gcone()
438                {
439                        //NOTE SAVE THE FACET STRUCTURE!!!
440                }                       
441               
442                /** \brief Set the interior point of a cone */
443                void setIntPoint(intvec *iv)
444                {
445                        this->ivIntPt=ivCopy(iv);
446                }
447               
448                /** \brief Return the interior point */
449                intvec *getIntPoint()
450                {
451                        return this->ivIntPt;
452                }
453               
454                /** \brief Print the interior point */
455                void showIntPoint()
456                {
457                        ivIntPt->show();
458                }
459               
460                /** \brief Print facets
461                * This is mainly for debugging purposes. Usually called from within gdb
462                */
463                void showFacets(short codim=1)
464                {
465                        facet *f;
466                        switch(codim)
467                        {
468                                case 1:
469                                        f = this->facetPtr;
470                                        break;
471                                case 2:
472                                        f = this->facetPtr->codim2Ptr;
473                                        break;
474                        }                       
475                       
476                        intvec *iv;                     
477                        while(f!=NULL)
478                        {
479                                iv = f->getFacetNormal();
480                                cout << "(";
481                                iv->show(1,0);                         
482                                if(f->isFlippable==FALSE)
483                                        cout << ")* ";
484                                else
485                                        cout << ") ";
486                                f=f->next;
487                        }
488                        cout << endl;
489                }
490               
491                /** For debugging purposes only */
492                void showSLA(facet &f)
493                {
494                        facet *fAct;
495                        fAct = &f;
496                        facet *codim2Act;
497                        codim2Act = fAct->codim2Ptr;
498                        intvec *fNormal;// = new intvec(this->numVars);
499                        intvec *f2Normal;
500                        cout << endl;
501                        while(fAct!=NULL)
502                        {
503                                fNormal=fAct->getFacetNormal();
504                                cout << "(";
505                                fNormal->show(1,0);
506                                if(fAct->isFlippable==TRUE)
507                                        cout << ") ";
508                                else
509                                        cout << ")* ";
510                                codim2Act = fAct->codim2Ptr;
511                                cout << " Codim2: ";
512                                while(codim2Act!=NULL)
513                                {
514                                        f2Normal = codim2Act->getFacetNormal();
515                                        cout << "(";
516                                        f2Normal->show(1,0);
517                                        cout << ") ";
518                                        codim2Act = codim2Act->next;
519                                }
520                                cout << "UCN = " << fAct->getUCN() << endl;                             
521                                fAct = fAct->next;
522                        }
523                }
524               
525                /** \brief Set gcone::numFacets */
526                void setNumFacets()
527                {
528                }
529               
530                /** \brief Get gcone::numFacets */
531                int getNumFacets()
532                {
533                        return this->numFacets;
534                }
535               
536                int getUCN()
537                {
538                        if(this!=NULL)
539                                return this->UCN;
540                        else
541                                return -1;
542                }
543               
544                /** \brief Compute the normals of the cone
545                *
546                * This method computes a representation of the cone in terms of facet normals. It takes an ideal
547                * as its input. Redundancies are automatically removed using cddlib's dd_MatrixCanonicalize.
548                * Other methods for redundancy checkings might be implemented later. See Anders' diss p.44.
549                * Note that in order to use cddlib a 0-th column has to be added to the matrix since cddlib expects
550                * each row to represent an inequality of type const+x1+...+xn <= 0. While computing the normals we come across
551                * the set \f$ \partial\mathcal{G} \f$ which we might store for later use. C.f p71 of journal
552                * As a result of this procedure the pointer facetPtr points to the first facet of the cone.
553                *
554                * Optionally, if the parameter bool compIntPoint is set to TRUE the method will also compute
555                * an interior point of the cone.
556                */
557                void getConeNormals(ideal const &I, bool compIntPoint=FALSE)
558                {
559#ifdef gfan_DEBUG
560                        std::cout << "*** Computing Inequalities... ***" << std::endl;
561#endif         
562                        //All variables go here - except ineq matrix and *v, see below
563                        int lengthGB=IDELEMS(I);        // # of polys in the groebner basis
564                        int pCompCount;                 // # of terms in a poly
565                        poly aktpoly;
566                        int numvar = pVariables;        // # of variables in a polynomial (or ring?)
567                        int leadexp[numvar];            // dirty hack of exp.vects
568                        int aktexp[numvar];
569                        int cols,rows;                  // will contain the dimensions of the ineq matrix - deprecated by
570                        dd_rowrange ddrows;
571                        dd_colrange ddcols;
572                        dd_rowset ddredrows;            // # of redundant rows in ddineq
573                        dd_rowset ddlinset;             // the opposite
574                        dd_rowindex ddnewpos;             // all to make dd_Canonicalize happy
575                        dd_NumberType ddnumb=dd_Integer; //Number type
576                        dd_ErrorType dderr=dd_NoError;  //
577                        // End of var declaration
578#ifdef gfan_DEBUG
579//                      cout << "The Groebner basis has " << lengthGB << " elements" << endl;
580//                      cout << "The current ring has " << numvar << " variables" << endl;
581#endif
582                        cols = numvar;
583               
584                        //Compute the # inequalities i.e. rows of the matrix
585                        rows=0; //Initialization
586                        for (int ii=0;ii<IDELEMS(I);ii++)
587                        {
588                                aktpoly=(poly)I->m[ii];
589                                rows=rows+pLength(aktpoly)-1;
590                        }
591#ifdef gfan_DEBUG
592//                      cout << "rows=" << rows << endl;
593//                      cout << "Will create a " << rows << " x " << cols << " matrix to store inequalities" << endl;
594#endif
595                        dd_rowrange aktmatrixrow=0;     // needed to store the diffs of the expvects in the rows of ddineq
596                        dd_set_global_constants();
597                        ddrows=rows;
598                        ddcols=cols;
599                        dd_MatrixPtr ddineq;            //Matrix to store the inequalities                     
600                        ddineq=dd_CreateMatrix(ddrows,ddcols+1); //The first col has to be 0 since cddlib checks for additive consts there
601               
602                        // We loop through each g\in GB and compute the resulting inequalities
603                        for (int i=0; i<IDELEMS(I); i++)
604                        {
605                                aktpoly=(poly)I->m[i];          //get aktpoly as i-th component of I
606                                pCompCount=pLength(aktpoly);    //How many terms does aktpoly consist of?
607#ifdef gfan_DEBUG
608//                              cout << "Poly No. " << i << " has " << pCompCount << " components" << endl;
609#endif
610               
611                                int *v=(int *)omAlloc((numvar+1)*sizeof(int));
612                                pGetExpV(aktpoly,v);    //find the exp.vect in v[1],...,v[n], use pNext(p)
613                               
614                                //Store leadexp for aktpoly
615                                for (int kk=0;kk<numvar;kk++)
616                                {
617                                        leadexp[kk]=v[kk+1];
618                                        //Since we need to know the difference of leadexp with the other expvects we do nothing here
619                                        //but compute the diff below
620                                }
621               
622                               
623                                while (pNext(aktpoly)!=NULL) //move to next term until NULL
624                                {
625                                        aktpoly=pNext(aktpoly);
626                                        pSetm(aktpoly);         //doesn't seem to help anything
627                                        pGetExpV(aktpoly,v);
628                                        for (int kk=0;kk<numvar;kk++)
629                                        {
630                                                aktexp[kk]=v[kk+1];
631                                                //ineq[aktmatrixrow][kk]=leadexp[kk]-aktexp[kk];        //dito
632                                                dd_set_si(ddineq->matrix[(dd_rowrange)aktmatrixrow][kk+1],leadexp[kk]-aktexp[kk]); //because of the 1st col being const 0
633                                        }
634                                        aktmatrixrow=aktmatrixrow+1;
635                                }//while
636               
637                        } //for
638               
639                        //Maybe add another row to contain the constraints of the standard simplex?
640
641#ifdef gfan_DEBUG
642//                      cout << "The inequality matrix is" << endl;
643//                      dd_WriteMatrix(stdout, ddineq);
644#endif
645
646                        // The inequalities are now stored in ddineq
647                        // Next we check for superflous rows
648                        ddredrows = dd_RedundantRows(ddineq, &dderr);
649                        if (dderr!=dd_NoError)                  // did an error occur?
650                        {
651                                dd_WriteErrorMessages(stderr,dderr);    //if so tell us
652                        } 
653#ifdef gfan_DEBUG
654                        else
655                        {
656                                cout << "Redundant rows: ";
657                                set_fwrite(stdout, ddredrows);          //otherwise print the redundant rows
658                        }//if dd_Error
659#endif
660                        //Remove reduntant rows here!
661                        dd_MatrixCanonicalize(&ddineq, &ddlinset, &ddredrows, &ddnewpos, &dderr);
662                        ddrows = ddineq->rowsize;       //Size of the matrix with redundancies removed
663                        ddcols = ddineq->colsize;
664                       
665                        //ddCreateMatrix(ddrows,ddcols+1);
666                        ddFacets = dd_CopyMatrix(ddineq);
667#ifdef gfan_DEBUG
668//                      cout << "Having removed redundancies, the normals now read:" << endl;
669//                      dd_WriteMatrix(stdout,ddineq);
670//                      cout << "Rows = " << ddrows << endl;
671//                      cout << "Cols = " << ddcols << endl;
672#endif
673                       
674                        /*Write the normals into class facet*/
675#ifdef gfan_DEBUG
676//                      cout << "Creating list of normals" << endl;
677#endif
678                        /*The pointer *fRoot should be the return value of this function*/
679                        //facet *fRoot = new facet();   //instantiate new facet
680                        //fRoot->setUCN(this->getUCN());
681                        //this->facetPtr = fRoot;               //set variable facetPtr of class gcone to first facet
682                        facet *fAct;                    //pointer to active facet
683                        //fAct = fRoot;                 //Seems to do the trick. fRoot and fAct have to point to the same adress!
684                        //std::cout << "fRoot = " << fRoot << ", fAct = " << fAct << endl;
685                        int numNonFlip=0;
686                        for (int kk = 0; kk<ddrows; kk++)
687                        {
688                                intvec *load = new intvec(this->numVars);       //intvec to store a single facet normal that will then be stored via setFacetNormal
689                                for (int jj = 1; jj <ddcols; jj++)
690                                {
691                                        double foo;
692                                        foo = mpq_get_d(ddineq->matrix[kk][jj]);
693#ifdef gfan_DEBUG
694//                                      std::cout << "fAct is " << foo << " at " << fAct << std::endl;
695#endif
696                                        (*load)[jj-1] = (int)foo;               //store typecasted entry at pos jj-1 of load           
697                                }//for (int jj = 1; jj <ddcols; jj++)
698                               
699                                /*Quick'n'dirty hack for flippability*/ 
700                                bool isFlip=FALSE;
701                                                               
702                                for (int jj = 0; jj<load->length(); jj++)
703                                {
704                                        intvec *ivCanonical = new intvec(load->length());
705                                        (*ivCanonical)[jj]=1;
706//                                      cout << "dotProd=" << dotProduct(*load,*ivCanonical) << endl;
707                                        if (dotProduct(*load,*ivCanonical)<0)   
708                                        //if (ivMult(load,ivCanonical)<0)
709                                        {
710                                                isFlip=TRUE;
711                                                break;  //URGHS
712                                        }
713                                }       
714                                if (isFlip==FALSE)
715                                {
716                                        this->numFacets++;
717                                        numNonFlip++;
718                                        if(this->numFacets==1)
719                                        {
720                                                facet *fRoot = new facet();
721                                                this->facetPtr = fRoot;
722                                                fAct = fRoot;
723                                               
724                                        }
725                                        else
726                                        {
727                                                fAct->next = new facet();
728                                                fAct = fAct->next;
729                                        }
730                                        fAct->isFlippable=FALSE;
731                                        fAct->setFacetNormal(load);
732                                        fAct->setUCN(this->getUCN());
733#ifdef gfan_DEBUG
734                                        cout << "Marking facet (";
735                                        load->show(1,0);
736                                        cout << ") as non flippable" << endl;                           
737#endif
738                                }
739                                else
740                                {       /*Now load should be full and we can call setFacetNormal*/                                     
741                                        this->numFacets++;
742                                        if(this->numFacets==1)
743                                        {
744                                                facet *fRoot = new facet();
745                                                this->facetPtr = fRoot;
746                                                fAct = fRoot;
747                                        }
748                                        else
749                                        {
750                                                fAct->next = new facet();
751                                                fAct = fAct->next;
752                                        }
753                                        fAct->isFlippable=TRUE;
754                                        fAct->setFacetNormal(load);
755                                        fAct->setUCN(this->getUCN());                                   
756                                }//if (isFlippable==FALSE)
757                                //delete load;                         
758                        }//for (int kk = 0; kk<ddrows; kk++)
759                       
760                        //In cases like I=<x-1,y-1> there are only non-flippable facets...
761                        if(numNonFlip==this->numFacets)
762                        {                                       
763                                cerr << "Only non-flippable facets. Terminating..." << endl;
764                        }
765                       
766                        /*
767                        Now we should have a linked list containing the facet normals of those facets that are
768                        -irredundant
769                        -flipable
770                        Adressing is done via *facetPtr
771                        */
772                       
773                        if (compIntPoint==TRUE)
774                        {
775                                intvec *iv = new intvec(this->numVars);
776                                interiorPoint(ddineq, *iv);     //NOTE ddineq contains non-flippable facets
777                                this->setIntPoint(iv);  //stores the interior point in gcone::ivIntPt
778                                //delete iv;
779                        }
780                       
781                        //Compute the number of facets
782                        // wrong because ddineq->rowsize contains only irredundant facets. There can still be
783                        // non-flippable ones!
784                        //this->numFacets=ddineq->rowsize;
785                       
786                        //Clean up but don't delete the return value! (Whatever it will turn out to be)                 
787                        //dd_FreeMatrix(ddineq);
788                        //set_free(ddredrows);
789                        //free(ddnewpos);
790                        //set_free(ddlinset);
791                        //NOTE Commented out. Solved the bug that after facet2Matrix there were facets lost
792                        //THIS SUCKS
793                        //dd_free_global_constants();
794
795                }//method getConeNormals(ideal I)       
796               
797                /** \brief Compute the (codim-2)-facets of a given cone
798                * This method is used during noRevS
799                */
800                void getCodim2Normals(gcone const &gc)
801                {
802                        //this->facetPtr->codim2Ptr = new facet(2);     //instantiate a (codim-2)-facet
803                        facet *fAct;
804                        fAct = this->facetPtr;         
805                        facet *codim2Act;
806                        //codim2Act = this->facetPtr->codim2Ptr;
807                       
808                        dd_set_global_constants();
809                       
810                        dd_MatrixPtr ddineq,P,ddakt;
811                        dd_rowset impl_linset, redset;
812                        dd_ErrorType err;
813                        dd_rowindex newpos;             
814
815                        //ddineq = facets2Matrix(gc);   //get a matrix representation of the cone
816                        ddineq = dd_CopyMatrix(gc.ddFacets);
817                               
818                        /*Now set appropriate linearity*/
819                        dd_PolyhedraPtr ddpolyh;
820                        for (int ii=0; ii<this->numFacets; ii++)                       
821                        {                               
822                                ddakt = dd_CopyMatrix(ddineq);
823                                ddakt->representation=dd_Inequality;
824                                set_addelem(ddakt->linset,ii+1);                               
825                                dd_MatrixCanonicalize(&ddakt, &impl_linset, &redset, &newpos, &err);                   
826                                       
827#ifdef gfan_DEBUG
828//                              cout << "Codim2 matrix"<<endl;
829//                              dd_WriteMatrix(stdout,ddakt);
830#endif
831                                ddpolyh=dd_DDMatrix2Poly(ddakt, &err);
832                                P=dd_CopyGenerators(ddpolyh);                           
833#ifdef gfan_DEBUG
834//                              cout << "Codim2 facet:" << endl;
835//                                      dd_WriteMatrix(stdout,P);
836//                              cout << endl;
837#endif
838                                       
839                                /* We loop through each row of P
840                                * normalize it by making all entries integer ones
841                                * and add the resulting vector to the int matrix facet::codim2Facets
842                                */
843                                for (int jj=1;jj<=P->rowsize;jj++)
844                                {                                       
845                                        fAct->numCodim2Facets++;
846                                        if(fAct->numCodim2Facets==1)                                   
847                                        {                                               
848                                                fAct->codim2Ptr = new facet(2);                                         
849                                                codim2Act = fAct->codim2Ptr;
850                                        }
851                                        else
852                                        {
853                                                codim2Act->next = new facet(2);
854                                                codim2Act = codim2Act->next;
855                                        }
856                                        intvec *n = new intvec(this->numVars);
857                                        makeInt(P,jj,*n);
858                                        codim2Act->setFacetNormal(n);
859                                        delete n;                                       
860                                }               
861                                /*We check whether the facet spanned by the codim-2 facets
862                                * intersects with the positive orthant. Otherwise we define this
863                                * facet to be non-flippable
864                                */
865                                intvec *iv_intPoint = new intvec(this->numVars);
866                                dd_MatrixPtr shiftMatrix;
867                                dd_MatrixPtr intPointMatrix;
868                                shiftMatrix = dd_CreateMatrix(this->numVars,this->numVars+1);
869                                for(int kk=0;kk<this->numVars;kk++)
870                                {
871                                        dd_set_si(shiftMatrix->matrix[kk][0],1);
872                                        dd_set_si(shiftMatrix->matrix[kk][kk+1],1);
873                                }
874                                intPointMatrix=dd_MatrixAppend(ddakt,shiftMatrix);
875                                //dd_WriteMatrix(stdout,intPointMatrix);
876                                interiorPoint(intPointMatrix,*iv_intPoint);
877                                for(int ll=0;ll<this->numVars;ll++)
878                                {
879                                        if( (*iv_intPoint)[ll] < 0 )
880                                        {
881                                                fAct->isFlippable=FALSE;
882                                                break;
883                                        }
884                                }
885                                /*End of check*/                                       
886                                fAct = fAct->next;     
887                                dd_FreeMatrix(ddakt);
888                                dd_FreePolyhedra(ddpolyh);
889                        }//while
890                }
891               
892                /** \brief Compute the Groebner Basis on the other side of a shared facet
893                *
894                * Implements algorithm 4.3.2 from Anders' thesis.
895                * As shown there it is not necessary to compute an interior point. The knowledge of the facet normal
896                * suffices. A term \f$ x^\gamma \f$ of \f$ g \f$ is in \f$  in_\omega(g) \f$ iff \f$ \gamma - leadexp(g)\f$
897                * is parallel to \f$ leadexp(g) \f$
898                * Parallelity is checked using basic linear algebra. See gcone::isParallel.
899                * Other possibilities include computing the rank of the matrix consisting of the vectors in question and
900                * computing an interior point of the facet and taking all terms having the same weight with respect
901                * to this interior point.
902                *\param ideal, facet
903                * Input: a marked,reduced Groebner basis and a facet
904                */
905                void flip(ideal gb, facet *f)           //Compute "the other side"
906                {                       
907                        intvec *fNormal = new intvec(this->numVars);    //facet normal, check for parallelity                   
908                        fNormal = f->getFacetNormal();  //read this->fNormal;
909#ifdef gfan_DEBUG
910                        //std::cout << "===" << std::endl;
911                        std::cout << "running gcone::flip" << std::endl;
912                        std::cout << "flipping UCN " << this->getUCN() << endl;
913                        for(int ii=0;ii<IDELEMS(gb);ii++)
914                        {
915                                pWrite((poly)gb->m[ii]);
916                        }
917                        cout << "over facet (";
918                        fNormal->show(1,0);
919                        cout << ") with UCN " << f->getUCN();
920                        std::cout << std::endl;
921#endif                         
922                        /*1st step: Compute the initial ideal*/
923                        poly initialFormElement[IDELEMS(gb)];   //array of #polys in GB to store initial form
924                        ideal initialForm=idInit(IDELEMS(gb),this->gcBasis->rank);
925                        poly aktpoly;
926                        intvec *check = new intvec(this->numVars);      //array to store the difference of LE and v
927                       
928                        for (int ii=0;ii<IDELEMS(gb);ii++)
929                        {
930                                aktpoly = (poly)gb->m[ii];                                                             
931                                int *v=(int *)omAlloc((this->numVars+1)*sizeof(int));
932                                int *leadExpV=(int *)omAlloc((this->numVars+1)*sizeof(int));
933                                pGetExpV(aktpoly,leadExpV);     //find the leading exponent in leadExpV[1],...,leadExpV[n], use pNext(p)
934                                initialFormElement[ii]=pHead(aktpoly);
935                               
936                                while(pNext(aktpoly)!=NULL)     /*loop trough terms and check for parallelity*/
937                                {
938                                        aktpoly=pNext(aktpoly); //next term
939                                        pSetm(aktpoly);
940                                        pGetExpV(aktpoly,v);           
941                                        /* Convert (int)v into (intvec)check */                 
942                                        for (int jj=0;jj<this->numVars;jj++)
943                                        {
944                                                //cout << "v["<<jj+1<<"]="<<v[jj+1]<<endl;
945                                                //cout << "leadExpV["<<jj+1<<"]="<<leadExpV[jj+1]<<endl;
946                                                (*check)[jj]=v[jj+1]-leadExpV[jj+1];
947                                        }
948#ifdef gfan_DEBUG
949//                                      cout << "check=";                       
950//                                      check->show();
951//                                      cout << endl;
952#endif
953                                        //TODO why not *check, *fNormal????
954                                        if (isParallel(*check,*fNormal)) //pass *check when
955                                        {
956//                                              cout << "Parallel vector found, adding to initialFormElement" << endl;                 
957                                                initialFormElement[ii] = pAdd(pCopy(initialFormElement[ii]),(poly)pHead(aktpoly));
958                                        }                                               
959                                }//while
960#ifdef gfan_DEBUG
961                                cout << "Initial Form=";                               
962                                pWrite(initialFormElement[ii]);
963                                cout << "---" << endl;
964#endif
965                                /*Now initialFormElement must be added to (ideal)initialForm */
966                                initialForm->m[ii]=initialFormElement[ii];
967                        }//for                 
968#ifdef gfan_DEBUG
969/*                      cout << "Initial ideal is: " << endl;
970                        idShow(initialForm);
971                        //f->printFlipGB();*/
972//                      cout << "===" << endl;
973#endif
974                        //delete check;
975                       
976                        /*2nd step: lift initial ideal to a GB of the neighbouring cone using minus alpha as weight*/
977                        /*Substep 2.1
978                        compute $G_{-\alpha}(in_v(I))
979                        see journal p. 66
980                        NOTE Check for different rings. Prolly it will not always be necessary to add a weight, if the
981                        srcRing already has a weighted ordering
982                        */
983                        ring srcRing=currRing;
984                        ring tmpRing;
985                       
986                        if( (srcRing->order[0]!=ringorder_a))
987                        {
988                                tmpRing=rCopyAndAddWeight(srcRing,ivNeg(fNormal));
989                        }
990                        else
991                        {
992                                tmpRing=rCopy0(srcRing);
993                                int length=fNormal->length();
994                                int *A=(int *)omAlloc0(length*sizeof(int));
995                                for(int jj=0;jj<length;jj++)
996                                {
997                                        A[jj]=-(*fNormal)[jj];
998                                }
999                                omFree(tmpRing->wvhdl[0]);
1000                                tmpRing->wvhdl[0]=(int*)A;
1001                                tmpRing->block1[0]=length;
1002                                rComplete(tmpRing);     
1003                        }
1004                        rChangeCurrRing(tmpRing);
1005                       
1006                        //rWrite(currRing); cout << endl;
1007                       
1008                        ideal ina;                     
1009                        ina=idrCopyR(initialForm,srcRing);                     
1010#ifdef gfan_DEBUG
1011//                      cout << "ina=";
1012//                      idShow(ina); cout << endl;
1013#endif
1014                        ideal H;
1015                        //H=kStd(ina,NULL,isHomog,NULL);        //we know it is homogeneous
1016                        H=kStd(ina,NULL,testHomog,NULL);
1017                        idSkipZeroes(H);
1018#ifdef gfan_DEBUG
1019//                      cout << "H="; idShow(H); cout << endl;
1020#endif
1021                        /*Substep 2.2
1022                        do the lifting and mark according to H
1023                        */
1024                        rChangeCurrRing(srcRing);
1025                        ideal srcRing_H;
1026                        ideal srcRing_HH;                       
1027                        srcRing_H=idrCopyR(H,tmpRing);
1028#ifdef gfan_DEBUG
1029//                      cout << "srcRing_H = ";
1030//                      idShow(srcRing_H); cout << endl;
1031#endif
1032                        srcRing_HH=ffG(srcRing_H,this->gcBasis);               
1033#ifdef gfan_DEBUG
1034//                      cout << "srcRing_HH = ";
1035//                      idShow(srcRing_HH); cout << endl;
1036#endif
1037                        /*Substep 2.2.1
1038                        Mark according to G_-\alpha
1039                        Here we have a minimal basis srcRing_HH. In order to turn this basis into a reduced basis
1040                        we have to compute an interior point of C(srcRing_HH). For this we need to know the cone
1041                        represented by srcRing_HH MARKED ACCORDING TO G_{-\alpha}
1042                        Thus we check whether the leading monomials of srcRing_HH and srcRing_H coincide. If not we
1043                        compute the difference accordingly
1044                        */
1045                        dd_set_global_constants();
1046                        bool markingsAreCorrect=FALSE;
1047                        dd_MatrixPtr intPointMatrix;
1048                        int iPMatrixRows=0;
1049                        dd_rowrange aktrow=0;                   
1050                        for (int ii=0;ii<IDELEMS(srcRing_HH);ii++)
1051                        {
1052                                poly aktpoly=(poly)srcRing_HH->m[ii];
1053                                iPMatrixRows = iPMatrixRows+pLength(aktpoly)-1;
1054                        }
1055                        /* additionally one row for the standard-simplex and another for a row that becomes 0 during
1056                        construction of the differences
1057                        */
1058                        intPointMatrix = dd_CreateMatrix(iPMatrixRows+10,this->numVars+1);
1059                        intPointMatrix->numbtype=dd_Integer;    //NOTE: DO NOT REMOVE OR CHANGE TO dd_Rational
1060                       
1061                        for (int ii=0;ii<IDELEMS(srcRing_HH);ii++)
1062                        {
1063                                markingsAreCorrect=FALSE;       //crucial to initialise here
1064                                poly aktpoly=srcRing_HH->m[ii];
1065                                /*Comparison of leading monomials is done via exponent vectors*/
1066                                for (int jj=0;jj<IDELEMS(H);jj++)
1067                                {
1068                                        int *src_ExpV = (int *)omAlloc((this->numVars+1)*sizeof(int));
1069                                        int *dst_ExpV = (int *)omAlloc((this->numVars+1)*sizeof(int));
1070                                        pGetExpV(aktpoly,src_ExpV);
1071                                        rChangeCurrRing(tmpRing);       //this ring change is crucial!
1072                                        pGetExpV(pCopy(H->m[ii]),dst_ExpV);
1073                                        rChangeCurrRing(srcRing);
1074                                        bool expVAreEqual=TRUE;
1075                                        for (int kk=1;kk<=this->numVars;kk++)
1076                                        {
1077#ifdef gfan_DEBUG
1078                                                //cout << src_ExpV[kk] << "," << dst_ExpV[kk] << endl;
1079#endif
1080                                                if (src_ExpV[kk]!=dst_ExpV[kk])
1081                                                {
1082                                                        expVAreEqual=FALSE;
1083                                                }
1084                                        }                                       
1085                                        //if (*src_ExpV == *dst_ExpV)
1086                                        if (expVAreEqual==TRUE)
1087                                        {
1088                                                markingsAreCorrect=TRUE; //everything is fine
1089#ifdef gfan_DEBUG
1090//                                              cout << "correct markings" << endl;
1091#endif
1092                                        }//if (pHead(aktpoly)==pHead(H->m[jj])
1093                                        delete src_ExpV;
1094                                        delete dst_ExpV;
1095                                }//for (int jj=0;jj<IDELEMS(H);jj++)
1096                               
1097                                int *v=(int *)omAlloc((this->numVars+1)*sizeof(int));
1098                                int *leadExpV=(int *)omAlloc((this->numVars+1)*sizeof(int));
1099                                if (markingsAreCorrect==TRUE)
1100                                {
1101                                        pGetExpV(aktpoly,leadExpV);
1102                                }
1103                                else
1104                                {
1105                                        rChangeCurrRing(tmpRing);
1106                                        pGetExpV(pHead(H->m[ii]),leadExpV); //We use H->m[ii] as leading monomial
1107                                        rChangeCurrRing(srcRing);
1108                                }
1109                                /*compute differences of the expvects*/                         
1110                                while (pNext(aktpoly)!=NULL)
1111                                {
1112                                        /*The following if-else-block makes sure the first term (i.e. the wrongly marked term)
1113                                        is not omitted when computing the differences*/
1114                                        if(markingsAreCorrect==TRUE)
1115                                        {
1116                                                aktpoly=pNext(aktpoly);
1117                                                pGetExpV(aktpoly,v);
1118                                        }
1119                                        else
1120                                        {
1121                                                pGetExpV(pHead(aktpoly),v);
1122                                                markingsAreCorrect=TRUE;
1123                                        }
1124
1125                                        for (int jj=0;jj<this->numVars;jj++)
1126                                        {                               
1127                                                /*Store into ddMatrix*/                                                         
1128                                                dd_set_si(intPointMatrix->matrix[aktrow][jj+1],leadExpV[jj+1]-v[jj+1]);
1129                                        }
1130                                        aktrow +=1;
1131                                }
1132                                delete v;
1133                                delete leadExpV;
1134                        }//for (int ii=0;ii<IDELEMS(srcRing_HH);ii++)
1135                        /*Now we add the constraint for the standard simplex*/
1136                        /*NOTE:Might actually work without the standard simplex*/
1137                        dd_set_si(intPointMatrix->matrix[aktrow][0],-1);
1138                        for (int jj=1;jj<=this->numVars;jj++)
1139                        {
1140                                dd_set_si(intPointMatrix->matrix[aktrow][jj],1);
1141                        }
1142#ifdef gfan_DEBUG
1143//                      dd_WriteMatrix(stdout,intPointMatrix);
1144#endif
1145                        intvec *iv_weight = new intvec(this->numVars);
1146                        interiorPoint(intPointMatrix, *iv_weight);      //iv_weight now contains the interior point
1147                        dd_FreeMatrix(intPointMatrix);
1148                        dd_free_global_constants();
1149                       
1150                        /*Step 3
1151                        turn the minimal basis into a reduced one
1152                        */
1153                        //ring dstRing=rCopyAndAddWeight(tmpRing,iv_weight);   
1154                       
1155                        // NOTE May assume that at this point srcRing already has 3 blocks of orderins, starting with a
1156                        // Thus:
1157                        //ring dstRing=rCopyAndChangeWeight(srcRing,iv_weight);
1158                        //cout << "PLING" << endl;
1159                        /*ring dstRing=rCopy0(srcRing);
1160                        for (int ii=0;ii<this->numVars;ii++)
1161                        {                               
1162                                dstRing->wvhdl[0][ii]=(*iv_weight)[ii];                         
1163                        }*/
1164                        ring dstRing=rCopy0(tmpRing);
1165                        int length=iv_weight->length();
1166                        int *A=(int *)omAlloc0(length*sizeof(int));
1167                        for(int jj=0;jj<length;jj++)
1168                        {
1169                                A[jj]=(*iv_weight)[jj];
1170                        }
1171                        dstRing->wvhdl[0]=(int*)A;
1172                        rComplete(dstRing);                                     
1173                        rChangeCurrRing(dstRing);
1174                       
1175//#ifdef gfan_DEBUG
1176                        rWrite(dstRing); cout << endl;
1177//#endif
1178                        ideal dstRing_I;                       
1179                        dstRing_I=idrCopyR(srcRing_HH,srcRing);
1180                        //dstRing_I=idrCopyR(inputIdeal,srcRing);
1181                        //validOpts<1>=TRUE;
1182#ifdef gfan_DEBUG
1183                        //idShow(dstRing_I);
1184#endif                 
1185                        BITSET save=test;
1186                        test|=Sy_bit(OPT_REDSB);
1187                        test|=Sy_bit(OPT_REDTAIL);
1188#ifdef gfan_DEBUG
1189                        test|=Sy_bit(6);        //OPT_DEBUG
1190#endif
1191                        ideal tmpI;
1192                        //NOTE Any of the two variants of tmpI={idrCopy(),dstRing_I} does the trick
1193                        //tmpI = idrCopyR(this->inputIdeal,this->baseRing);
1194                        tmpI = dstRing_I;
1195                        dstRing_I=kStd(tmpI,NULL,testHomog,NULL);                       
1196                        //kInterRed(dstRing_I);
1197                        idSkipZeroes(dstRing_I);
1198                        test=save;
1199                        /*End of step 3 - reduction*/
1200                       
1201                        f->setFlipGB(dstRing_I);//store the flipped GB
1202                        f->flipRing=rCopy(dstRing);     //store the ring on the other side
1203//#ifdef gfan_DEBUG
1204                        cout << "Flipped GB is UCN " << counter+1 << ":" << endl;
1205                        f->printFlipGB();
1206                        cout << endl;
1207//#endif                       
1208                        rChangeCurrRing(srcRing);       //return to the ring we started the computation of flipGB in
1209                }//void flip(ideal gb, facet *f)
1210                               
1211                /** \brief Compute the remainder of a polynomial by a given ideal
1212                *
1213                * Compute \f$ f^{\mathcal{G}} \f$
1214                * Algorithm is taken from Cox, Little, O'Shea, IVA 2nd Ed. p 62
1215                * However, since we are only interested in the remainder, there is no need to
1216                * compute the factors \f$ a_i \f$
1217                */
1218                //NOTE: Should be replaced by kNF or kNF2
1219                poly restOfDiv(poly const &f, ideal const &I)
1220                {
1221//                      cout << "Entering restOfDiv" << endl;
1222                        poly p=f;
1223                        //pWrite(p);                   
1224                        poly r=NULL;    //The zero polynomial
1225                        int ii;
1226                        bool divOccured;
1227                       
1228                        while (p!=NULL)
1229                        {
1230                                ii=1;
1231                                divOccured=FALSE;
1232                               
1233                                while( (ii<=IDELEMS(I) && (divOccured==FALSE) ))
1234                                {                                       
1235                                        if (pDivisibleBy(I->m[ii-1],p)) //does LM(I->m[ii]) divide LM(p) ?
1236                                        {                                               
1237                                                poly step1,step2,step3;
1238                                                //cout << "dividing "; pWrite(pHead(p));cout << "by ";pWrite(pHead(I->m[ii-1])); cout << endl;
1239                                                step1 = pDivideM(pHead(p),pHead(I->m[ii-1]));
1240                                                //cout << "LT(p)/LT(f_i)="; pWrite(step1); cout << endl;                               
1241                                                step2 = ppMult_qq(step1, I->m[ii-1]);                                           
1242                                                step3 = pSub(pCopy(p), step2);
1243                                                //p=pSub(p,pMult( pDivide(pHead(p),pHead(I->m[ii])), I->m[ii]));                       
1244                                                //pSetm(p);
1245                                                pSort(step3); //must be here, otherwise strange behaviour with many +o+o+o+o+ terms
1246                                                p=step3;
1247                                                //pWrite(p);                                           
1248                                                divOccured=TRUE;
1249                                        }
1250                                        else
1251                                        {
1252                                                ii += 1;
1253                                        }//if (pLmDivisibleBy(I->m[ii],p,currRing))
1254                                }//while( (ii<IDELEMS(I) && (divOccured==FALSE) ))
1255                                if (divOccured==FALSE)
1256                                {
1257                                        //cout << "TICK 5" << endl;
1258                                        r=pAdd(pCopy(r),pHead(p));
1259                                        pSetm(r);
1260                                        pSort(r);
1261                                        //cout << "r="; pWrite(r); cout << endl;
1262                                       
1263                                        if (pLength(p)!=1)
1264                                        {
1265                                                p=pSub(pCopy(p),pHead(p));      //Here it may occur that p=0 instead of p=NULL
1266                                        }
1267                                        else
1268                                        {
1269                                                p=NULL; //Hack to correct this situation                                               
1270                                        }                                       
1271                                        //cout << "p="; pWrite(p);
1272                                }//if (divOccured==FALSE)
1273                        }//while (p!=0)
1274                        return r;
1275                }//poly restOfDiv(poly const &f, ideal const &I)
1276               
1277                /** \brief Compute \f$ f-f^{\mathcal{G}} \f$
1278                */
1279                //NOTE: use kNF or kNF2 instead of restOfDivision
1280                ideal ffG(ideal const &H, ideal const &G)
1281                {
1282//                      cout << "Entering ffG" << endl;
1283                        int size=IDELEMS(H);
1284                        ideal res=idInit(size,1);
1285                        poly temp1, temp2, temp3;       //polys to temporarily store values for pSub
1286                        for (int ii=0;ii<size;ii++)
1287                        {
1288                                res->m[ii]=restOfDiv(H->m[ii],G);
1289                                //res->m[ii]=kNF(G, NULL,H->m[ii]);
1290                                temp1=H->m[ii];
1291                                temp2=res->m[ii];                               
1292                                temp3=pSub(temp1, temp2);
1293                                res->m[ii]=temp3;
1294                                //res->m[ii]=pSub(temp1,temp2); //buggy
1295                                //pSort(res->m[ii]);
1296                                //pSetm(res->m[ii]);
1297                                //cout << "res->m["<<ii<<"]=";pWrite(res->m[ii]);                                               
1298                        }                       
1299                        return res;
1300                }
1301               
1302                /** \brief Compute a Groebner Basis
1303                *
1304                * Computes the Groebner basis and stores the result in gcone::gcBasis
1305                *\param ideal
1306                *\return void
1307                */
1308                void getGB(ideal const &inputIdeal)             
1309                {
1310                        BITSET save=test;
1311                        test|=Sy_bit(OPT_REDSB);
1312                        test|=Sy_bit(OPT_REDTAIL);
1313                        ideal gb;
1314                        gb=kStd(inputIdeal,NULL,testHomog,NULL);
1315                        idSkipZeroes(gb);
1316                        this->gcBasis=gb;       //write the GB into gcBasis
1317                        test=save;
1318                }//void getGB
1319               
1320                /** \brief The Generic Groebner Walk due to FJLT
1321                * Needed for computing the search facet
1322                */
1323                ideal GenGrbWlk(ideal, ideal)
1324                {
1325                }//GGW
1326               
1327                /** \brief Compute the negative of a given intvec
1328                */             
1329                intvec *ivNeg(const intvec *iv)
1330                {
1331                        intvec *res = new intvec(iv->length());
1332                        res=ivCopy(iv);
1333                        *res *= (int)-1;
1334                        //for(int ii=0;ii<this->numVars;ii++)
1335                        //{                     
1336                                //(res)[ii] = (*res[ii])*(int)(-1);
1337                        //}
1338                        //res->show(1,0);                       
1339                        return res;
1340                }
1341
1342
1343                /** \brief Compute the dot product of two intvecs
1344                *
1345                */
1346                int dotProduct(intvec const &iva, intvec const &ivb)                           
1347                {
1348                        //intvec iva=a;
1349                        //intvec ivb=b;
1350                        int res=0;
1351                        for (int i=0;i<this->numVars;i++)
1352                        {
1353                                res = res+(iva[i]*ivb[i]);
1354                        }
1355                        return res;
1356                }//int dotProduct
1357
1358                /** \brief Check whether two intvecs are parallel
1359                *
1360                * \f$ \alpha\parallel\beta\Leftrightarrow\langle\alpha,\beta\rangle^2=\langle\alpha,\alpha\rangle\langle\beta,\beta\rangle \f$
1361                */
1362                bool isParallel(intvec const &a, intvec const &b)
1363                {                       
1364                        int lhs,rhs;
1365                        lhs=dotProduct(a,b)*dotProduct(a,b);
1366                        rhs=dotProduct(a,a)*dotProduct(b,b);
1367                        //cout << "LHS="<<lhs<<", RHS="<<rhs<<endl;
1368                        if (lhs==rhs)
1369                        {
1370                                return TRUE;
1371                        }
1372                        else
1373                        {
1374                                return FALSE;
1375                        }
1376                }//bool isParallel
1377               
1378                /** \brief Compute an interior point of a given cone
1379                * Result will be written into intvec iv.
1380                * Any rational point is automatically converted into an integer.
1381                */
1382                void interiorPoint(dd_MatrixPtr const &M, intvec &iv) //no const &M here since we want to remove redundant rows
1383                {
1384                        dd_LPPtr lp,lpInt;
1385                        dd_ErrorType err=dd_NoError;
1386                        dd_LPSolverType solver=dd_DualSimplex;
1387                        dd_LPSolutionPtr lpSol=NULL;
1388                        dd_rowset ddlinset,ddredrows;   //needed for dd_FindRelativeInterior
1389                        dd_rowindex ddnewpos;
1390                        dd_NumberType numb;     
1391                        //M->representation=dd_Inequality;
1392                        //M->objective-dd_LPMin;  //Not sure whether this is needed
1393                       
1394                        //NOTE: Make this n-dimensional!
1395                        //dd_set_si(M->rowvec[0],1);dd_set_si(M->rowvec[1],1);dd_set_si(M->rowvec[2],1);
1396                                                                       
1397                        //dd_MatrixCanonicalize(&M, &ddlinset, &ddredrows, &ddnewpos, &err);
1398                        //if (err!=dd_NoError){cout << "Error during dd_MatrixCanonicalize" << endl;}
1399                        //cout << "Tick 2" << endl;
1400                        //dd_WriteMatrix(stdout,M);
1401                       
1402                        lp=dd_Matrix2LP(M, &err);
1403                        if (err!=dd_NoError){cout << "Error during dd_Matrix2LP in gcone::interiorPoint" << endl;}                     
1404                        if (lp==NULL){cout << "LP is NULL" << endl;}
1405#ifdef gfan_DEBUG
1406//                      dd_WriteLP(stdout,lp);
1407#endif 
1408                                       
1409                        lpInt=dd_MakeLPforInteriorFinding(lp);
1410                        if (err!=dd_NoError){cout << "Error during dd_MakeLPForInteriorFinding in gcone::interiorPoint" << endl;}
1411#ifdef gfan_DEBUG
1412//                      dd_WriteLP(stdout,lpInt);
1413#endif                 
1414
1415                        dd_FindRelativeInterior(M,&ddlinset,&ddredrows,&lpSol,&err);
1416                        if (err!=dd_NoError)
1417                        {
1418                                cout << "Error during dd_FindRelativeInterior in gcone::interiorPoint" << endl;
1419                                dd_WriteErrorMessages(stdout, err);
1420                        }
1421                       
1422                        //dd_LPSolve(lpInt,solver,&err);        //This will not result in a point from the relative interior
1423                        if (err!=dd_NoError){cout << "Error during dd_LPSolve" << endl;}
1424                                                                       
1425                        //lpSol=dd_CopyLPSolution(lpInt);
1426                        if (err!=dd_NoError){cout << "Error during dd_CopyLPSolution" << endl;}                 
1427#ifdef gfan_DEBUG
1428                        cout << "Interior point: ";
1429                        for (int ii=1; ii<(lpSol->d)-1;ii++)
1430                        {
1431                                dd_WriteNumber(stdout,lpSol->sol[ii]);
1432                        }
1433                        cout << endl;
1434#endif
1435                       
1436                        //NOTE The following strongly resembles parts of makeInt.
1437                        //Maybe merge sometimes
1438                        mpz_t kgV; mpz_init(kgV);
1439                        mpz_set_str(kgV,"1",10);
1440                        mpz_t den; mpz_init(den);
1441                        mpz_t tmp; mpz_init(tmp);
1442                        mpq_get_den(tmp,lpSol->sol[1]);
1443                        for (int ii=1;ii<(lpSol->d)-1;ii++)
1444                        {
1445                                mpq_get_den(den,lpSol->sol[ii+1]);
1446                                mpz_lcm(kgV,tmp,den);
1447                                mpz_set(tmp, kgV);
1448                        }
1449                        mpq_t qkgV;
1450                        mpq_init(qkgV);
1451                        mpq_set_z(qkgV,kgV);
1452                        for (int ii=1;ii<(lpSol->d)-1;ii++)
1453                        {
1454                                mpq_t product;
1455                                mpq_init(product);
1456                                mpq_mul(product,qkgV,lpSol->sol[ii]);
1457                                iv[ii-1]=(int)mpz_get_d(mpq_numref(product));
1458                                mpq_clear(product);
1459                        }
1460#ifdef gfan_DEBUG
1461//                      iv.show();
1462//                      cout << endl;
1463#endif
1464                        mpq_clear(qkgV);
1465                        mpz_clear(tmp);
1466                        mpz_clear(den);
1467                        mpz_clear(kgV);                 
1468                       
1469                        dd_FreeLPSolution(lpSol);
1470                        dd_FreeLPData(lpInt);
1471                        dd_FreeLPData(lp);
1472                        set_free(ddlinset);
1473                        set_free(ddredrows);                   
1474                       
1475                }//void interiorPoint(dd_MatrixPtr const &M)
1476               
1477                /** \brief Copy a ring and add a weighted ordering in first place
1478                *
1479                */
1480                ring rCopyAndAddWeight(ring const &r, intvec const *ivw)                               
1481                {
1482                        ring res=rCopy0(r);
1483                        int jj;
1484                       
1485                        omFree(res->order);
1486                        res->order =(int *)omAlloc0(4*sizeof(int));
1487                        omFree(res->block0);
1488                        res->block0=(int *)omAlloc0(4*sizeof(int));
1489                        omFree(res->block1);
1490                        res->block1=(int *)omAlloc0(4*sizeof(int));
1491                        omfree(res->wvhdl);
1492                        res->wvhdl =(int **)omAlloc0(4*sizeof(int**));
1493                       
1494                        res->order[0]=ringorder_a;
1495                        res->block0[0]=1;
1496                        res->block1[0]=res->N;;
1497                        res->order[1]=ringorder_dp;     //basically useless, since that should never be used                   
1498                        res->block0[1]=1;
1499                        res->block1[1]=res->N;;
1500                        res->order[2]=ringorder_C;
1501
1502                        int length=ivw->length();
1503                        int *A=(int *)omAlloc0(length*sizeof(int));
1504                        for (jj=0;jj<length;jj++)
1505                        {                               
1506                                A[jj]=(*ivw)[jj];                               
1507                        }                       
1508                        res->wvhdl[0]=(int *)A;
1509                        res->block1[0]=length;
1510                       
1511                        rComplete(res);
1512                        return res;
1513                }//rCopyAndAdd
1514               
1515                ring rCopyAndChangeWeight(ring const &r, intvec *ivw)
1516                {
1517                        ring res=rCopy0(currRing);
1518                        rComplete(res);
1519                        rSetWeightVec(res,(int64*)ivw);
1520                        //rChangeCurrRing(rnew);
1521                        return res;
1522                }
1523               
1524                /** \brief Checks whether a given facet is a search facet
1525                * Determines whether a given facet of a cone is the search facet of a neighbouring cone
1526                * This is done in the following way:
1527                * We loop through all facets of the cone and find the "smallest" facet, i.e. the unique facet
1528                * that is first crossed during the generic walk.
1529                * We then check whether the fNormal of this facet is parallel to the fNormal of our testfacet.
1530                * If this is the case, then our facet is indeed a search facet and TRUE is retuned.
1531                */
1532                bool isSearchFacet(gcone &gcTmp, facet *testfacet)
1533                {                               
1534                        ring actRing=currRing;
1535                        facet *facetPtr=(facet*)gcTmp.facetPtr;                 
1536                        facet *fMin=new facet(*facetPtr);       //Pointer to the "minimal" facet
1537                        //facet *fMin = new facet(tmpcone.facetPtr);
1538                        //fMin=tmpcone.facetPtr;                //Initialise to first facet of tmpcone
1539                        facet *fAct;    //Ptr to alpha_i
1540                        facet *fCmp;    //Ptr to alpha_j
1541                        fAct = fMin;
1542                        fCmp = fMin->next;                             
1543                       
1544                        rChangeCurrRing(this->rootRing);        //because we compare the monomials in the rootring                     
1545                        poly p=pInit();
1546                        poly q=pInit();
1547                        intvec *alpha_i = new intvec(this->numVars);                   
1548                        intvec *alpha_j = new intvec(this->numVars);
1549                        intvec *sigma = new intvec(this->numVars);
1550                        sigma=gcTmp.getIntPoint();
1551                       
1552                        int *u=(int *)omAlloc((this->numVars+1)*sizeof(int));
1553                        int *v=(int *)omAlloc((this->numVars+1)*sizeof(int));
1554                        u[0]=0; v[0]=0;
1555                        int weight1,weight2;
1556                        while(fAct->next->next!=NULL)   //NOTE this is ugly. Can it be done without fCmp?
1557                        {
1558                                /* Get alpha_i and alpha_{i+1} */
1559                                alpha_i=fAct->getFacetNormal();
1560                                alpha_j=fCmp->getFacetNormal();
1561#ifdef gfan_DEBUG
1562                                alpha_i->show(); 
1563                                alpha_j->show();
1564#endif
1565                                /*Compute the dot product of sigma and alpha_{i,j}*/
1566                                weight1=dotProduct(sigma,alpha_i);
1567                                weight2=dotProduct(sigma,alpha_j);
1568#ifdef gfan_DEBUG
1569                                cout << "weight1=" << weight1 << " " << "weight2=" << weight2 << endl;
1570#endif
1571                                /*Adjust alpha_i and alpha_i+1 accordingly*/
1572                                for(int ii=1;ii<=this->numVars;ii++)
1573                                {                                       
1574                                        u[ii]=weight1*(*alpha_i)[ii-1];
1575                                        v[ii]=weight2*(*alpha_j)[ii-1];
1576                                }                               
1577                               
1578                                /*Now p_weight and q_weight need to be compared as exponent vectors*/
1579                                pSetCoeff0(p,nInit(1));
1580                                pSetCoeff0(q,nInit(1));
1581                                pSetExpV(p,u); 
1582                                pSetm(p);                       
1583                                pSetExpV(q,v); 
1584                                pSetm(q);
1585#ifdef gfan_DEBUG                               
1586                                pWrite(p);pWrite(q);
1587#endif 
1588                                /*We want to check whether x^p < x^q
1589                                => want to check for return value 1 */
1590                                if (pLmCmp(p,q)==1)     //i.e. x^q is smaller
1591                                {
1592                                        fMin=fCmp;
1593                                        fAct=fMin;
1594                                        fCmp=fCmp->next;
1595                                }
1596                                else
1597                                {
1598                                        //fAct=fAct->next;
1599                                        if(fCmp->next!=NULL)
1600                                        {
1601                                                fCmp=fCmp->next;
1602                                        }
1603                                        else
1604                                        {
1605                                                fAct=fAct->next;
1606                                        }
1607                                }
1608                                //fAct=fAct->next;
1609                        }//while(fAct.facetPtr->next!=NULL)
1610                        delete alpha_i,alpha_j,sigma;
1611                       
1612                        /*If testfacet was minimal then fMin should still point there */
1613                       
1614                        //if(fMin->getFacetNormal()==ivNeg(testfacet.getFacetNormal()))                 
1615#ifdef gfan_DEBUG
1616                        cout << "Checking for parallelity" << endl <<" fMin is";
1617                        fMin->printNormal();
1618                        cout << "testfacet is ";
1619                        testfacet->printNormal();
1620                        cout << endl;
1621#endif
1622                        if (fMin==gcTmp.facetPtr)                       
1623                        //if(areEqual(fMin->getFacetNormal(),ivNeg(testfacet.getFacetNormal())))
1624                        //if (isParallel(fMin->getFacetNormal(),testfacet->getFacetNormal()))
1625                        {                               
1626                                cout << "Parallel" << endl;
1627                                rChangeCurrRing(actRing);
1628                                //delete alpha_min, test;
1629                                return TRUE;
1630                        }
1631                        else 
1632                        {
1633                                cout << "Not parallel" << endl;
1634                                rChangeCurrRing(actRing);
1635                                //delete alpha_min, test;
1636                                return FALSE;
1637                        }
1638                }//bool isSearchFacet
1639               
1640                /** \brief Check for equality of two intvecs
1641                */
1642                bool areEqual(intvec const &a, intvec const &b)
1643                {
1644                        bool res=TRUE;
1645                        for(int ii=0;ii<this->numVars;ii++)
1646                        {
1647                                if(a[ii]!=b[ii])
1648                                {
1649                                        res=FALSE;
1650                                        break;
1651                                }
1652                        }
1653                        return res;
1654                }
1655               
1656                /** \brief The reverse search algorithm
1657                */
1658                void reverseSearch(gcone *gcAct) //no const possible here since we call gcAct->flip
1659                {
1660                        facet *fAct=new facet();
1661                        fAct = gcAct->facetPtr;                 
1662                       
1663                        while(fAct!=NULL) 
1664                        {
1665                                cout << "======================"<< endl;
1666                                gcAct->flip(gcAct->gcBasis,gcAct->facetPtr);
1667                                gcone *gcTmp = new gcone(*gcAct);
1668                                //idShow(gcTmp->gcBasis);
1669                                gcTmp->getConeNormals(gcTmp->gcBasis, TRUE);
1670#ifdef gfan_DEBUG
1671                                facet *f = new facet();
1672                                f=gcTmp->facetPtr;
1673                                while(f!=NULL)
1674                                {
1675                                        f->printNormal();
1676                                        f=f->next;                                     
1677                                }
1678#endif
1679                                gcTmp->showIntPoint();
1680                                /*recursive part goes gere*/
1681                                if (isSearchFacet(*gcTmp,(facet*)gcAct->facetPtr))
1682                                {
1683                                        gcAct->next=gcTmp;
1684                                        cout << "PING"<< endl;
1685                                        reverseSearch(gcTmp);
1686                                }
1687                                else
1688                                {
1689                                        delete gcTmp;
1690                                        /*NOTE remove fAct from linked list. It's no longer needed*/
1691                                }
1692                                /*recursion ends*/
1693                                fAct = fAct->next;             
1694                        }//while(fAct->next!=NULL)
1695                }//reverseSearch
1696               
1697                /** \brief The new method of Markwig and Jensen
1698                * Compute gcBasis and facets for the arbitrary starting cone. Store \f$(codim-1)\f$-facets as normals.
1699                * In order to represent a facet uniquely compute also the \f$(codim-2)\f$-facets and norm by the gcd of the components.
1700                * Keep a list of facets as a linked list containing an intvec and an integer matrix.
1701                * Since a \f$(codim-1)\f$-facet belongs to exactly two full dimensional cones, we remove a facet from the list as
1702                * soon as we compute this facet again. Comparison of facets is done by...
1703                */
1704                void noRevS(gcone &gcRoot, bool usingIntPoint=FALSE)
1705                {       
1706                        facet *SearchListRoot = new facet(); //The list containing ALL facets we come across
1707                        facet *SearchListAct;
1708                        SearchListAct = NULL;
1709                        //SearchListAct = SearchListRoot;
1710                       
1711                        gcone *gcAct;
1712                        gcAct = &gcRoot;
1713                        gcone *gcPtr;   //Pointer to end of linked list of cones
1714                        gcPtr = &gcRoot;
1715                        gcone *gcNext;  //Pointer to next cone to be visited
1716                        gcNext = &gcRoot;
1717                        gcone *gcHead;
1718                        gcHead = &gcRoot;
1719                       
1720                        facet *fAct;
1721                        fAct = gcAct->facetPtr;                 
1722                       
1723                        ring rAct;
1724                        rAct = currRing;
1725                                               
1726                        int UCNcounter=gcAct->getUCN();
1727#ifdef gfan_DEBUG
1728                        cout << "NoRevs" << endl;
1729                        cout << "Facets are:" << endl;
1730                        gcAct->showFacets();
1731#endif                 
1732                       
1733                        gcAct->getCodim2Normals(*gcAct);
1734                               
1735                        //Compute unique representation of codim-2-facets
1736                        gcAct->normalize();
1737                       
1738                        /*Make a copy of the facet list of first cone
1739                        Since the operations getCodim2Normals and normalize affect the facets
1740                        we must not memcpy them before these ops!
1741                        */
1742                        intvec *fNormal;// = new intvec(this->numVars);
1743                        intvec *f2Normal;// = new intvec(this->numVars);
1744                        facet *codim2Act; codim2Act = NULL;                     
1745                        facet *sl2Root; //sl2Root = new facet(2);
1746                        facet *sl2Act;  //sl2Act = sl2Root;
1747                       
1748                        for(int ii=0;ii<this->numFacets;ii++)
1749                        {
1750                                //only copy flippable facets!
1751                                if(fAct->isFlippable==TRUE)
1752                                {
1753                                        fNormal = fAct->getFacetNormal();
1754                                        if( ii==0 || (ii>0 && SearchListAct==NULL) ) //1st facet may be non-flippable
1755                                        {                                               
1756                                                SearchListAct = SearchListRoot;
1757                                                //memcpy(SearchListAct,fAct,sizeof(facet));                                     
1758                                        }
1759                                        else
1760                                        {                       
1761                                                SearchListAct->next = new facet();
1762                                                SearchListAct = SearchListAct->next;                                           
1763                                                //memcpy(SearchListAct,fAct,sizeof(facet));                             
1764                                        }
1765                                        SearchListAct->setFacetNormal(fNormal);
1766                                        SearchListAct->setUCN(this->getUCN());
1767                                        SearchListAct->numCodim2Facets=fAct->numCodim2Facets;
1768                                        SearchListAct->isFlippable=TRUE;
1769                                        //Copy codim2-facets                           
1770                                        codim2Act=fAct->codim2Ptr;
1771                                        SearchListAct->codim2Ptr = new facet(2);
1772                                        sl2Root = SearchListAct->codim2Ptr;
1773                                        sl2Act = sl2Root;
1774                                        //while(codim2Act!=NULL)
1775                                        for(int jj=0;jj<fAct->numCodim2Facets;jj++)
1776                                        {
1777                                                f2Normal = codim2Act->getFacetNormal();
1778                                                if(jj==0)
1779                                                {                                               
1780                                                        sl2Act = sl2Root;
1781                                                        sl2Act->setFacetNormal(f2Normal);
1782                                                }
1783                                                else
1784                                                {
1785                                                        sl2Act->next = new facet(2);
1786                                                        sl2Act = sl2Act->next;
1787                                                        sl2Act->setFacetNormal(f2Normal);
1788                                                }                                       
1789                                                codim2Act = codim2Act->next;
1790                                        }
1791                                        fAct = fAct->next;
1792                                }//if(fAct->isFlippable==TRUE)
1793                                else {fAct = fAct->next;}
1794                        }                               
1795                       
1796                        SearchListAct = SearchListRoot; //Set to beginning of list
1797                        /*Make SearchList doubly linked*/
1798                        while(SearchListAct!=NULL)
1799                        {
1800                                if(SearchListAct->next!=NULL)
1801                                {
1802                                        SearchListAct->next->prev = SearchListAct;                                     
1803                                }
1804                                SearchListAct = SearchListAct->next;
1805                        }
1806                        SearchListAct = SearchListRoot; //Set to beginning of List
1807                       
1808                        fAct = gcAct->facetPtr;
1809                        //NOTE Disabled until code works as expected
1810                        //gcAct->writeConeToFile(*gcAct);
1811                       
1812                        /*End of initialisation*/
1813                        fAct = SearchListAct;
1814                        /*2nd step
1815                        Choose a facet from fListPtr, flip it and forget the previous cone
1816                        We always choose the first facet from fListPtr as facet to be flipped
1817                        */                     
1818                        while(SearchListAct!=NULL)// && counter<limit)
1819                        {//NOTE See to it that the cone is only changed after ALL facets have been flipped!                             
1820                                fAct = SearchListAct;
1821                                //while( ( (fAct->next!=NULL) && (fAct->getUCN()==fAct->next->getUCN() ) ) )
1822                                //do
1823                                while(fAct!=NULL)
1824                                {       //Since SLA should only contain flippables there should be no need to check for that
1825                                        gcAct->flip(gcAct->gcBasis,fAct);
1826                                        ring rTmp=rCopy(fAct->flipRing);
1827                                        rComplete(rTmp);
1828                                        rChangeCurrRing(rTmp);
1829                                        gcone *gcTmp = new gcone::gcone(*gcAct,*fAct);
1830                                        gcTmp->getConeNormals(gcTmp->gcBasis, FALSE);                                   
1831                                        gcTmp->getCodim2Normals(*gcTmp);                                       
1832                                        gcTmp->normalize();
1833#ifdef gfan_DEBUG
1834                                        gcTmp->showFacets(1);
1835#endif
1836                                        //gcTmp->writeConeToFile(*gcTmp);
1837                                        /*add facets to SLA here*/
1838                                        SearchListRoot=gcTmp->enqueueNewFacets(*SearchListRoot);
1839#ifdef gfan_DEBUG
1840                                        if(SearchListRoot!=NULL)
1841                                                gcTmp->showSLA(*SearchListRoot);
1842#endif
1843                                        rChangeCurrRing(gcAct->baseRing);
1844                                        gcPtr->next=gcTmp;
1845                                        gcPtr=gcPtr->next;
1846                                        if(fAct->getUCN() == fAct->next->getUCN())
1847                                        {
1848                                                fAct=fAct->next;
1849                                        }
1850                                        else
1851                                                break;
1852                                }//while( ( (fAct->next!=NULL) && (fAct->getUCN()==fAct->next->getUCN() ) ) );
1853                                //Search for cone with smallest UCN
1854                                gcNext = gcHead;
1855                                while(gcNext!=NULL)
1856                                {
1857                                        //if( gcNext->getUCN() == UCNcounter+1 )
1858                                        if( gcNext->getUCN() == SearchListRoot->getUCN() )
1859                                        {
1860                                                gcAct = gcNext;
1861                                                rAct=rCopy(gcAct->baseRing);
1862                                                rComplete(rAct);
1863                                                rChangeCurrRing(rAct);                                         
1864                                                break;                                         
1865                                        }
1866                                        gcNext = gcNext->next;
1867                                }
1868                                UCNcounter++;
1869                                //SearchListAct = SearchListAct->next;
1870                                //SearchListAct = fAct->next;
1871                                SearchListAct = SearchListRoot;
1872                        }
1873                        cout << endl << "Found " << counter << " cones - terminating" << endl;
1874               
1875                        //NOTE Hm, comment in and get a crash for free...
1876                        //dd_free_global_constants();                           
1877                        //gc.writeConeToFile(gc);
1878                       
1879                       
1880                        //fAct = fListPtr;
1881                        //gcone *gcTmp = new gcone(gc); //copy
1882                        //gcTmp->flip(gcTmp->gcBasis,fAct);
1883                        //NOTE: gcTmp may be deleted, gcRoot from main proc should probably not!
1884                       
1885                }//void noRevS(gcone &gc)       
1886               
1887               
1888                /** \brief Make a set of rational vectors into integers
1889                *
1890                * We compute the lcm of the denominators and multiply with this to get integer values.         
1891                * \param dd_MatrixPtr,intvec
1892                */
1893                void makeInt(dd_MatrixPtr const &M, int const line, intvec &n)
1894                {                       
1895                        mpz_t denom[this->numVars];
1896                        for(int ii=0;ii<this->numVars;ii++)
1897                        {
1898                                mpz_init_set_str(denom[ii],"0",10);
1899                        }
1900               
1901                        mpz_t kgV,tmp;
1902                        mpz_init(kgV);
1903                        mpz_init(tmp);
1904                       
1905                        for (int ii=0;ii<(M->colsize)-1;ii++)
1906                        {
1907                                mpz_t z;
1908                                mpz_init(z);
1909                                mpq_get_den(z,M->matrix[line-1][ii+1]);
1910                                mpz_set( denom[ii], z);
1911                                mpz_clear(z);                           
1912                        }
1913                       
1914                        //Check whether denom is all ones, in which case we will divide out the gcd of the nominators
1915//                      mpz_t checksum; mpz_t rop;
1916//                      mpz_init(checksum);
1917//                      mpz_init(rop);
1918//                      bool divideOutGcd=FALSE;
1919//                      for(int ii=0;ii<this->numVars;ii++)
1920//                      {
1921//                              mpz_add(rop, checksum, denom[ii]);
1922//                              mpz_set(checksum, rop);
1923//                      }
1924//                      if( (int)mpz_get_ui(checksum)==this->numVars)
1925//                      {
1926//                              divideOutGcd=TRUE;
1927//                      }
1928                       
1929                        /*Compute lcm of the denominators*/
1930                        mpz_set(tmp,denom[0]);
1931                        for (int ii=0;ii<(M->colsize)-1;ii++)
1932                        {
1933                                mpz_lcm(kgV,tmp,denom[ii]);
1934                                mpz_set(tmp,kgV);                               
1935                        }
1936                       
1937                        /*Multiply the nominators by kgV*/
1938                        mpq_t qkgV,res;
1939                        mpq_init(qkgV);
1940                        mpq_set_str(qkgV,"1",10);
1941                        mpq_canonicalize(qkgV);
1942                       
1943                        mpq_init(res);
1944                        mpq_set_str(res,"1",10);
1945                        mpq_canonicalize(res);
1946                       
1947                        mpq_set_num(qkgV,kgV);
1948                       
1949//                      mpq_canonicalize(qkgV);
1950                        for (int ii=0;ii<(M->colsize)-1;ii++)
1951                        {
1952                                mpq_mul(res,qkgV,M->matrix[line-1][ii+1]);
1953                                n[ii]=(int)mpz_get_d(mpq_numref(res));
1954                        }
1955                        //mpz_clear(denom[this->numVars]);
1956                        mpz_clear(kgV);
1957                        mpq_clear(qkgV); mpq_clear(res);                       
1958                       
1959                }
1960                /**
1961                 * For all codim-2-facets we compute the gcd of the components of the facet normal and
1962                 * divide it out. Thus we get a normalized representation of each
1963                 * (codim-2)-facet normal, i.e. a primitive vector
1964                */
1965                void normalize()
1966                {
1967                        int ggT=1;
1968                        facet *fAct;
1969                        facet *codim2Act;
1970                        fAct = this->facetPtr;
1971                        codim2Act = fAct->codim2Ptr;
1972                        intvec *n = new intvec(this->numVars);
1973                       
1974                        //while(codim2Act->next!=NULL)
1975                        while(fAct!=NULL)
1976                        {
1977                                while(codim2Act!=NULL)
1978                                {                               
1979                                        n=codim2Act->getFacetNormal();
1980                                        for(int ii=0;ii<this->numVars;ii++)
1981                                        {
1982                                                ggT = intgcd(ggT,(*n)[ii]);
1983                                        }
1984                                        for(int ii=0;ii<this->numVars;ii++)
1985                                        {
1986                                                (*n)[ii] = ((*n)[ii])/ggT;
1987                                        }
1988                                        codim2Act->setFacetNormal(n);
1989                                        codim2Act = codim2Act->next;                           
1990                                }
1991                                fAct = fAct->next;
1992                        }
1993               
1994                        //delete n;
1995                        /*codim2Act = this->facetPtr->codim2Ptr;        //reset to start of linked list                 
1996                        while(codim2Act!=NULL)
1997                        {                               
1998                                n=codim2Act->getFacetNormal();
1999                                intvec *new_n = new intvec(this->numVars);
2000                                for(int ii=0;ii<this->numVars;ii++)
2001                                {
2002                                        (*new_n)[ii] = (*n)[ii]*ggT;
2003                                }
2004                                codim2Act->setFacetNormal(new_n);
2005                                codim2Act = codim2Act->next;
2006                                //delete n;
2007                                //delete new_n;
2008                        }       */             
2009                }
2010                /** \brief Enqueue new facets into the searchlist
2011                * The searchlist (SLA for short) is implemented as a FIFO
2012                * Checks are done as follows:
2013                * 1) Check facet fAct against all facets in SLA for parallelity. If it is not parallel to any one add it.
2014                * 2) If it is parallel compare the codim2 facets. If they coincide the facets are equal and we delete the
2015                *       facet from SLA and do not add fAct.
2016                * It may very well happen, that SLA=\f$ \emptyset \f$ but we do not have checked all fActs yet. In this case we
2017                * can be sure, that none of the facets that are still there already were in SLA once, so we just dump them into SLA.
2018                * Takes ptr to search list root
2019                * Returns a pointer to new first element of Searchlist
2020                */
2021                //void enqueueNewFacets(facet &f)
2022                facet * enqueueNewFacets(facet &f)
2023                {
2024                        facet *slHead;
2025                        slHead = &f;
2026                        facet *slAct;   //called with f=SearchListRoot
2027                        slAct = &f;
2028                        facet *slEnd;   //Pointer to end of SLA
2029                        slEnd = &f;
2030                        facet *slEndStatic;     //marks the end before a new facet is added             
2031                        facet *fAct;
2032                        fAct = this->facetPtr;
2033                        facet *codim2Act;
2034                        codim2Act = this->facetPtr->codim2Ptr;
2035                        facet *sl2Act;
2036                        sl2Act = f.codim2Ptr;
2037                        /** Pointer to a facet that will be deleted */
2038                        facet *deleteMarker;
2039                        deleteMarker = NULL;
2040                       
2041                        /** Flag to indicate a facet that should be added to SLA*/
2042                        bool doNotAdd=FALSE;
2043                        /**A facet was removed, lengthOfSearchlist-- thus we must not rely on
2044                        * if(notParallelCtr==lengthOfSearchList) but rather
2045                        * if( (notParallelCtr==lengthOfSearchList && removalOccured==FALSE)
2046                        */
2047                        bool removalOccured=FALSE;
2048                        int ctr=0;      //encountered qualities in SLA
2049                        int notParallelCtr=0;
2050                        int lengthOfSearchList=1;
2051                        while(slEnd->next!=NULL)
2052                        {
2053                                slEnd=slEnd->next;
2054                                lengthOfSearchList++;
2055                        }
2056                        slEndStatic = slEnd;
2057                        /*1st step: compare facetNormals*/
2058                        intvec *fNormal=NULL; //= new intvec(this->numVars);
2059                        intvec *f2Normal=NULL; //= new intvec(this->numVars);
2060                        intvec *slNormal=NULL; //= new intvec(this->numVars);
2061                        intvec *sl2Normal=NULL; //= new intvec(this->numVars);
2062                       
2063                        while(fAct!=NULL)
2064                        {                               
2065                                if(fAct->isFlippable==TRUE)
2066                                {
2067                                        doNotAdd=TRUE;
2068                                        fNormal=fAct->getFacetNormal();
2069                                        slAct = slHead;
2070                                        notParallelCtr=0;
2071                                        /*If slAct==NULL and fAct!=NULL
2072                                        we just copy all remaining facets into SLA*/
2073                                        if(slAct==NULL)
2074                                        {
2075                                                facet *fCopy;
2076                                                fCopy = fAct;
2077                                                while(fCopy!=NULL)
2078                                                {
2079                                                        if(slAct==NULL)
2080                                                        {
2081                                                                slAct = new facet();
2082                                                                slHead = slAct;                                                         
2083                                                        }
2084                                                        else
2085                                                        {
2086                                                                slAct->next = new facet();
2087                                                                slAct = slAct->next;
2088                                                        }                                                       
2089                                                        slAct->setFacetNormal(fAct->getFacetNormal());
2090                                                        slAct->setUCN(fAct->getUCN());
2091                                                        slAct->isFlippable=fAct->isFlippable;
2092                                                        facet *f2Copy;
2093                                                        f2Copy = fCopy->codim2Ptr;
2094                                                        sl2Act = slAct->codim2Ptr;
2095                                                        while(f2Copy!=NULL)
2096                                                        {
2097                                                                if(sl2Act==NULL)
2098                                                                {
2099                                                                        sl2Act = new facet(2);
2100                                                                        slAct->codim2Ptr = sl2Act;                                     
2101                                                                }
2102                                                                else
2103                                                                {
2104                                                                        facet *marker;
2105                                                                        marker = sl2Act;
2106                                                                        sl2Act->next = new facet(2);
2107                                                                        sl2Act = sl2Act->next;
2108                                                                        sl2Act->prev = marker;
2109                                                                }
2110                                                                sl2Act->setFacetNormal(f2Copy->getFacetNormal());
2111                                                                sl2Act->setUCN(f2Copy->getUCN());
2112                                                                f2Copy = f2Copy->next;
2113                                                        }
2114                                                        fCopy = fCopy->next;
2115                                                }
2116                                                break;
2117                                        }
2118                                        /*End of */
2119                                        while(slAct!=NULL)
2120                                        //while(slAct!=slEndStatic->next)
2121                                        {
2122                                                removalOccured=FALSE;
2123                                                slNormal = slAct->getFacetNormal();
2124#ifdef gfan_DEBUG
2125                                                cout << "Checking facet (";
2126                                                fNormal->show(1,1);
2127                                                cout << ") against (";
2128                                                slNormal->show(1,1);
2129                                                cout << ")" << endl;
2130#endif
2131                                                if(!isParallel(fNormal, slNormal))
2132                                                {
2133                                                        notParallelCtr++;
2134                                                        slAct = slAct->next;
2135                                                }
2136                                                else
2137                                                {
2138                                                        codim2Act = fAct->codim2Ptr;
2139                                                        ctr=0;
2140                                                        while(codim2Act!=NULL)
2141                                                        {
2142                                                                f2Normal = codim2Act->getFacetNormal();
2143                                                                //sl2Act = f.codim2Ptr;
2144                                                                sl2Act = slAct->codim2Ptr;
2145                                                                while(sl2Act!=NULL)
2146                                                                {
2147                                                                        sl2Normal = sl2Act->getFacetNormal();
2148                                                                        if(areEqual(f2Normal, sl2Normal))
2149                                                                                        ctr++;
2150                                                                        sl2Act = sl2Act->next;
2151                                                                }//while(sl2Act!=NULL)
2152                                                                codim2Act = codim2Act->next;
2153                                                        }//while(codim2Act!=NULL)
2154                                                        if(ctr==fAct->numCodim2Facets)  //facets ARE equal
2155                                                        {
2156#ifdef gfan_DEBUG
2157                                                                cout << "Removing facet (";
2158                                                                slNormal->show(1,0);
2159                                                                cout << ") from SLA:" << endl;
2160                                                                fAct->fDebugPrint();
2161                                                                slAct->fDebugPrint();
2162#endif
2163                                                                removalOccured=TRUE;
2164                                                                slAct->isFlippable=FALSE;
2165                                                                doNotAdd=TRUE;                                                         
2166                                                                if(slAct==slHead)       //We want to delete the first element of SearchList
2167                                                                {
2168                                                                        deleteMarker = slHead;                         
2169                                                                        slHead = slAct->next;                                           
2170                                                                        if(slHead!=NULL)
2171                                                                                slHead->prev = NULL;                                   
2172                                                                        //set a bool flag to mark slAct as to be deleted
2173                                                                }//NOTE find a way to delete without affecting slAct = slAct->next
2174                                                                else if(slAct==slEndStatic)
2175                                                                        {
2176                                                                                deleteMarker = slAct;                                   
2177                                                                                if(slEndStatic->next==NULL)
2178                                                                                {                                                       
2179                                                                                        slEndStatic = slEndStatic->prev;
2180                                                                                        slEndStatic->next = NULL;
2181                                                                                        slEnd = slEndStatic;
2182                                                                                }
2183                                                                                else    //we already added a facet after slEndStatic
2184                                                                                {
2185                                                                                        slEndStatic->prev->next = slEndStatic->next;
2186                                                                                        slEndStatic = slEndStatic->prev;
2187                                                                                        //slEnd = slEndStatic;
2188                                                                                }
2189                                                                        }                                                               
2190                                                                else
2191                                                                {
2192                                                                        deleteMarker = slAct;
2193                                                                        slAct->prev->next = slAct->next;
2194                                                                        slAct->next->prev = slAct->prev;
2195                                                                }
2196                                                               
2197                                                                //update lengthOfSearchList                                     
2198                                                                lengthOfSearchList--;
2199                                                                break;
2200                                                        }//if(ctr==fAct->numCodim2Facets)
2201                                                        else    //facets are NOT equal
2202                                                        {
2203                                                                doNotAdd=FALSE;
2204                                                                //break;
2205                                                        }
2206                                                        slAct = slAct->next;
2207                                                        delete deleteMarker;
2208                                                }//if(!isParallel(fNormal, slNormal))
2209                                                /* NOTE The following lines must not be here but rather called inside the if-block above.
2210                                                Otherwise results get crappy. Unfortunately there are two slAct=slAct->next calls now,
2211                                                (not nice!) but since they are in seperate branches of the if-statement there should not
2212                                                be a way it gets called twice thus ommiting one facet:
2213                                                slAct = slAct->next;                                           
2214                                                delete deleteMarker;*/
2215                                                deleteMarker=NULL;
2216                                                //if slAct was marked as to be deleted, delete it here!
2217                                        }//while(slAct!=NULL)                                                                   
2218                                        if( (notParallelCtr==lengthOfSearchList && removalOccured==FALSE) || (doNotAdd==FALSE) )
2219                                        //if( (notParallelCtr==lengthOfSearchList ) || doNotAdd==FALSE )
2220                                        {
2221#ifdef gfan_DEBUG
2222                                                cout << "Adding facet (";
2223                                                fNormal->show(1,0);
2224                                                cout << ") to SLA " << endl;
2225#endif
2226                                                //Add fAct to SLA
2227                                                facet *marker;
2228                                                marker = slEnd;
2229                                                facet *f2Act;
2230                                                f2Act = fAct->codim2Ptr;
2231                                               
2232                                                slEnd->next = new facet();
2233                                                slEnd = slEnd->next;
2234                                                facet *slEndCodim2Root;
2235                                                facet *slEndCodim2Act;
2236                                                slEndCodim2Root = slEnd->codim2Ptr;
2237                                                slEndCodim2Act = slEndCodim2Root;
2238                                                               
2239                                                slEnd->setUCN(this->getUCN());
2240                                                slEnd->isFlippable = TRUE;
2241                                                slEnd->setFacetNormal(fNormal);
2242                                                slEnd->prev = marker;
2243                                                //Copy codim2-facets
2244                                                intvec *f2Normal;// = new intvec(this->numVars);
2245                                                while(f2Act!=NULL)
2246                                                {
2247                                                        f2Normal=f2Act->getFacetNormal();
2248                                                        if(slEndCodim2Root==NULL)
2249                                                        {
2250                                                                slEndCodim2Root = new facet(2);
2251                                                                slEnd->codim2Ptr = slEndCodim2Root;                     
2252                                                                slEndCodim2Root->setFacetNormal(f2Normal);
2253                                                                slEndCodim2Act = slEndCodim2Root;
2254                                                        }
2255                                                        else                                   
2256                                                        {
2257                                                                slEndCodim2Act->next = new facet(2);
2258                                                                slEndCodim2Act = slEndCodim2Act->next;
2259                                                                slEndCodim2Act->setFacetNormal(f2Normal);
2260                                                        }
2261                                                        f2Act = f2Act->next;
2262                                                }
2263                                                lengthOfSearchList++;
2264                                                //delete f2Normal;                                             
2265                                        }//if( (notParallelCtr==lengthOfSearchList && removalOccured==FALSE) ||
2266                                        //remove facets marked as non-flip
2267//                                      slAct=slHead;
2268//                                      while(slAct!=NULL)
2269//                                      {
2270//                                              if(slAct->isFlippable==FALSE)
2271//                                              {
2272//                                                      if(slAct==slHead)       //We want to delete the first element of SearchList
2273//                                                      {                                                               
2274//                                                              slHead = slAct->next;                                   
2275//                                                              if(slHead!=NULL)
2276//                                                                      slHead->prev = NULL;
2277//                                              //set a bool flag to mark slAct as to be deleted
2278//                                                      }
2279//                                                      else if(slAct==slEndStatic)
2280//                                                      {
2281//                                                              if(slEndStatic->next==NULL)
2282//                                                              {
2283//                                                                      slEndStatic = slEndStatic->prev;
2284//                                                                      slEndStatic->next = NULL;
2285//                                                              }
2286//                                                              else    //we already added a facet after slEndStatic
2287//                                                              {
2288//                                                                      slEndStatic->prev->next = slEndStatic->next;
2289//                                                                      slEndStatic = slEndStatic->prev;
2290//                                                      //slEnd = slEndStatic;
2291//                                                              }
2292//                                                      }
2293//                                                      else
2294//                                                      {
2295//                                                              slAct->prev->next = slAct->next;
2296//                                                      }
2297//                                      //update lengthOfSearchList                                     
2298//                                                      lengthOfSearchList--;
2299//                                              }
2300//                                              slAct=slAct->next;
2301//                                      }
2302                                        fAct = fAct->next;
2303                                }//if(fAct->isFlippable==TRUE)
2304                                else
2305                                {
2306                                        fAct = fAct->next;
2307                                }
2308                        }//while(fAct!=NULL)                                           
2309                        return slHead;
2310//                      delete sl2Normal;
2311//                      delete slNormal;
2312//                      delete f2Normal;
2313//                      delete fNormal;
2314                }//addC2N
2315               
2316                /** \brief Compute the gcd of two ints
2317                */
2318                int intgcd(int a, int b)
2319                {
2320                        int r, p=a, q=b;
2321                        if(p < 0)
2322                                p = -p;
2323                        if(q < 0)
2324                                q = -q;
2325                        while(q != 0)
2326                        {
2327                                r = p % q;
2328                                p = q;
2329                                q = r;
2330                        }
2331                        return p;
2332                }               
2333               
2334                /** \brief Construct a dd_MatrixPtr from a cone's list of facets
2335                *
2336                */
2337                dd_MatrixPtr facets2Matrix(gcone const &gc)
2338                {
2339                        facet *fAct;
2340                        fAct = gc.facetPtr;
2341       
2342                        dd_MatrixPtr M;
2343                        dd_rowrange ddrows;
2344                        dd_colrange ddcols;
2345                        ddcols=(this->numVars)+1;
2346                        ddrows=this->numFacets;
2347                        dd_NumberType numb = dd_Integer;
2348                        M=dd_CreateMatrix(ddrows,ddcols);                       
2349                       
2350                        int jj=0;
2351                        //while(fAct->next!=NULL)
2352                        while(fAct!=NULL)
2353                        {
2354                                intvec *comp;
2355                                comp = fAct->getFacetNormal();
2356                                for(int ii=0;ii<this->numVars;ii++)
2357                                {
2358                                        dd_set_si(M->matrix[jj][ii+1],(*comp)[ii]);
2359                                }
2360                                jj++;
2361                                fAct=fAct->next;                               
2362                        }                       
2363                       
2364                        return M;
2365                }
2366               
2367                /** \brief Write information about a cone into a file on disk
2368                *
2369                * This methods writes the information needed for the "second" method into a file.
2370                * The file's is divided in sections containing information on
2371                * 1) the ring
2372                * 2) the cone's Groebner Basis
2373                * 3) the cone's facets
2374                * Each line contains exactly one date
2375                * Each section starts with its name in CAPITALS
2376                */
2377                void writeConeToFile(gcone const &gc, bool usingIntPoints=FALSE)
2378                {
2379                        int UCN=gc.UCN;
2380                        stringstream ss;
2381                        ss << UCN;
2382                        string UCNstr = ss.str();               
2383                       
2384                        char prefix[]="/tmp/cone";
2385                        char *UCNstring;
2386                        strcpy(UCNstring,UCNstr.c_str());
2387                        char suffix[]=".sg";
2388                        char *filename=strcat(prefix,UCNstring);
2389                        strcat(filename,suffix);
2390                                       
2391                        ofstream gcOutputFile(filename);
2392                        facet *fAct = new facet(); //NOTE Why new?
2393                        fAct = gc.facetPtr;                     
2394                       
2395                        char *ringString = rString(currRing);
2396                       
2397                        if (!gcOutputFile)
2398                        {
2399                                cout << "Error opening file for writing in writeConeToFile" << endl;
2400                        }
2401                        else
2402                        {       gcOutputFile << "UCN" << endl;
2403                                gcOutputFile << this->UCN << endl;
2404                                gcOutputFile << "RING" << endl; 
2405                                gcOutputFile << ringString << endl;                     
2406                                //Write this->gcBasis into file
2407                                gcOutputFile << "GCBASIS" << endl;                             
2408                                for (int ii=0;ii<IDELEMS(gc.gcBasis);ii++)
2409                                {                                       
2410                                        gcOutputFile << p_String((poly)gc.gcBasis->m[ii],gc.baseRing) << endl;
2411                                }                               
2412                               
2413                                gcOutputFile << "FACETS" << endl;                                                               
2414                                //while(fAct->next!=NULL)
2415                                while(fAct!=NULL)
2416                                {       
2417                                        intvec *iv = new intvec(gc.numVars);
2418                                        iv=fAct->getFacetNormal();
2419                                        for (int ii=0;ii<iv->length();ii++)
2420                                        {
2421                                                if (ii<iv->length()-1)
2422                                                {
2423                                                        gcOutputFile << (*iv)[ii] << ",";
2424                                                }
2425                                                else
2426                                                {
2427                                                        gcOutputFile << (*iv)[ii] << endl;
2428                                                }
2429                                        }
2430                                        fAct=fAct->next;
2431                                        //delete iv; iv=NULL;
2432                                }                               
2433                               
2434                                gcOutputFile.close();
2435                                //delete fAct; fAct=NULL;
2436                        }
2437                       
2438                }//writeConeToFile(gcone const &gc)
2439               
2440                /** \brief Reads a cone from a file identified by its number
2441                */
2442                void readConeFromFile(int gcNum)
2443                {
2444                }
2445               
2446        friend class facet;
2447};//class gcone
2448
2449int gcone::counter=0;
2450
2451ideal gfan(ideal inputIdeal)
2452{
2453        int numvar = pVariables; 
2454       
2455        enum searchMethod {
2456                reverseSearch,
2457                noRevS
2458        };
2459       
2460        searchMethod method;
2461        method = noRevS;
2462//      method = reverseSearch;
2463       
2464#ifdef gfan_DEBUG
2465        cout << "Now in subroutine gfan" << endl;
2466#endif
2467        ring inputRing=currRing;        // The ring the user entered
2468        ring rootRing;                  // The ring associated to the target ordering
2469        ideal res;     
2470        facet *fRoot;
2471       
2472        if (method==reverseSearch)
2473        {
2474       
2475        /* Construct a new ring which will serve as our root*/
2476        rootRing=rCopy0(currRing);
2477        rootRing->order[0]=ringorder_lp;
2478       
2479        rComplete(rootRing);
2480        rChangeCurrRing(rootRing);
2481       
2482        /* Fetch the inputIdeal into our rootRing */
2483        map theMap=(map)idMaxIdeal(1);  //evil hack!
2484        theMap->preimage=NULL;  //neccessary?
2485        ideal rootIdeal;
2486        rootIdeal=fast_map(inputIdeal,inputRing,(ideal)theMap, currRing);
2487#ifdef gfan_DEBUG
2488        cout << "Root ideal is " << endl;
2489        idShow(rootIdeal);
2490        cout << "The root ring is " << endl;
2491        rWrite(rootRing);
2492        cout << endl;
2493#endif 
2494       
2495        //gcone *gcRoot = new gcone();  //Instantiate the sink
2496        gcone *gcRoot = new gcone(rootRing,rootIdeal);
2497        gcone *gcAct;
2498        gcAct = gcRoot;
2499        gcAct->numVars=pVariables;
2500        gcAct->getGB(rootIdeal);        //sets gcone::gcBasis
2501        idShow(gcAct->gcBasis);
2502        gcAct->getConeNormals(gcAct->gcBasis);  //hopefully compute the normals
2503        //gcAct->flip(gcAct->gcBasis,gcAct->facetPtr); 
2504        /*Now it is time to compute the search facets, respectively start the reverse search.
2505        But since we are in the root all facets should be search facets. IS THIS TRUE?
2506        NOTE: Check for flippability is not very sophisticated
2507        */     
2508        //gcAct->reverseSearch(gcAct); 
2509        rChangeCurrRing(rootRing);
2510        res=gcRoot->gcBasis;   
2511        }//if method==reverSearch
2512       
2513        if(method==noRevS)
2514        {
2515                gcone *gcRoot = new gcone(currRing,inputIdeal);
2516                gcone *gcAct;
2517                gcAct = gcRoot;
2518                gcAct->numVars=pVariables;
2519                gcAct->getGB(inputIdeal);
2520                cout << "GB of input ideal is:" << endl;
2521                idShow(gcAct->gcBasis);
2522                cout << endl;
2523                gcAct->getConeNormals(gcAct->gcBasis);         
2524                gcAct->noRevS(*gcAct);         
2525                res=gcAct->gcBasis;
2526        }
2527        /*As of now extra.cc expects gfan to return type ideal. Probably this will change in near future.
2528        The return type will then be of type LIST_CMD
2529        Assume gfan has finished, thus we have enumerated all the cones
2530        Create an array of size of #cones. Let each entry in the array contain a pointer to the respective
2531        Groebner Basis and merge this somehow with LIST_CMD
2532        => Count the cones!
2533        */
2534        //rChangeCurrRing(rootRing);
2535        //res=gcAct->gcBasis;
2536        //res=gcRoot->gcBasis; 
2537        return res;
2538        //return GBlist;
2539}
2540/*
2541Since gfan.cc is #included from extra.cc there must not be a int main(){} here
2542*/
2543#endif
Note: See TracBrowser for help on using the repository browser.