source: git/kernel/gfan.cc @ fe919c

spielwiese
Last change on this file since fe919c was fe919c, checked in by Martin Monerjan, 15 years ago
started doxygenization git-svn-id: file:///usr/local/Singular/svn/trunk@11587 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.3 KB
Line 
1/*
2Compute the Groebner fan of an ideal
3$Author: monerjan $
4$Date: 2009-03-24 21:31:49 $
5$Header: /exports/cvsroot-2/cvsroot/kernel/gfan.cc,v 1.20 2009-03-24 21:31:49 monerjan Exp $
6$Id: gfan.cc,v 1.20 2009-03-24 21:31:49 monerjan Exp $
7*/
8
9#include "mod2.h"
10
11#ifdef HAVE_GFAN
12
13#include "kstd1.h"
14#include "intvec.h"
15#include "polys.h"
16#include "ideals.h"
17#include "kmatrix.h"
18#include "iostream.h"   //deprecated
19
20//Hacks for different working places
21#define HOME
22
23#ifdef UNI
24#include "/users/urmel/alggeom/monerjan/cddlib/include/setoper.h" //Support for cddlib. Dirty hack
25#include "/users/urmel/alggeom/monerjan/cddlib/include/cdd.h"
26#endif
27
28#ifdef HOME
29#include "/home/momo/studium/diplomarbeit/cddlib/include/setoper.h"
30#include "/home/momo/studium/diplomarbeit/cddlib/include/cdd.h"
31#endif
32
33#ifdef ITWM
34#include "/u/slg/monerjan/cddlib/include/setoper.h"
35#include "/u/slg/monerjan/cddlib/include/cdd.h"
36#endif
37
38#ifndef gfan_DEBUG
39#define gfan_DEBUG
40#endif
41/**
42*\brief Class facet
43*       Implements the facet structure
44*
45*/
46class facet
47{
48        private:
49                /** inner normal, describing the facet uniquely  */
50                intvec fNormal;         
51        public:
52                /** The default constructor. Do I need a constructor of type facet(intvec)? */
53                facet()                 
54                {
55                        //fNormal = FN;
56                        // Pointer to next facet.  */
57                        /* Defaults to NULL. This way there is no need to check explicitly */
58                        this->next=NULL; 
59                }
60               
61                /** The default destructor */
62                ~facet(){;}
63               
64                void setFacetNormal(intvec iv){
65                        fNormal = iv;
66                        return;
67                }
68               
69                bool isFlippable;       //flippable facet? Want to have cone->isflippable.facet[i]
70                bool isIncoming;        //Is the facet incoming or outgoing?
71                facet *next;            //Pointer to next facet
72};
73
74/**
75*\brief Class gcone
76*       Implements the cone structure
77*/
78/*class gcone
79finally this should become s.th. like gconelib.{h,cc} to provide an API
80*/
81class gcone
82{
83        private:
84                int numFacets;          //#of facets of the cone
85
86        public:
87                gcone(int);             //constructor with dimension
88                ~gcone();               //destructor
89                poly gcMarkedTerm;      //marked terms of the cone's Groebner basis
90                ideal gcBasis;          //GB of the cone
91                gcone *next;            //Pointer to *previous* cone in search tree
92       
93                void flip();            //Compute "the other side"
94                void remRedFacets();    //Remove redundant facets of the cone NOT NEEDED since this is already done by cddlib while compunting the normals
95               
96                ideal GenGrbWlk(ideal, ideal);  //Implementation of the Generic Groebner Walk. Needed for a) Computing the sink and b) finding search facets
97               
98
99};//class gcone
100
101ideal getGB(ideal inputIdeal)
102{
103        #ifdef gfan_DEBUG
104        cout << "Computing a groebner basis..." << endl;
105        #endif
106
107        ideal gb;
108        gb=kStd(inputIdeal,NULL,testHomog,NULL); //Possible to call without testHomog/isHomog?
109        idSkipZeroes(gb); //Get rid of zero entries
110
111        return gb;
112}
113
114/**
115*\brief Compute the representation of a cone
116*
117*       Detailed description goes here
118*
119*\param An ideal
120*
121*\return A pointer to a facet
122*/
123/****** getConeNormals computes the inequalities ***/
124/*INPUT_TYPE: ideal                             */
125/*RETURN_TYPE: pointer to first facet           */
126/************************************************/
127facet getConeNormals(ideal I)
128{
129        #ifdef gfan_DEBUG
130        cout << "*** Computing Inequalities... ***" << endl;
131        #endif
132
133        //All variables go here - except ineq matrix and *v, see below
134        int lengthGB=IDELEMS(I);        // # of polys in the groebner basis
135        int pCompCount;                 // # of terms in a poly
136        poly aktpoly;
137        int numvar = pVariables;        // # of variables in a polynomial (or ring?)
138        int leadexp[numvar];            // dirty hack of exp.vects
139        int aktexp[numvar];
140        int cols,rows;                  // will contain the dimensions of the ineq matrix - deprecated by
141        dd_rowrange ddrows;
142        dd_colrange ddcols;
143        dd_rowset ddredrows;            // # of redundant rows in ddineq
144        dd_rowset ddlinset;             // the opposite
145        dd_rowindex ddnewpos;           // all to make dd_Canonicalize happy
146        dd_NumberType ddnumb=dd_Real;   //Number type
147        dd_ErrorType dderr=dd_NoError;  //
148        // End of var declaration
149
150        cout << "The Groebner basis has " << lengthGB << " elements" << endl;
151        cout << "The current ring has " << numvar << " variables" << endl;
152        cols = numvar;
153
154        //Compute the # inequalities i.e. rows of the matrix
155        rows=0; //Initialization
156        for (int ii=0;ii<IDELEMS(I);ii++)
157        {
158                aktpoly=(poly)I->m[ii];
159                rows=rows+pLength(aktpoly)-1;
160        }
161        cout << "rows=" << rows << endl;
162        cout << "Will create a " << rows << " x " << cols << " matrix to store inequalities" << endl;
163
164        dd_rowrange aktmatrixrow=0;     // needed to store the diffs of the expvects in the rows of ddineq
165        dd_set_global_constants();
166        ddrows=rows;
167        ddcols=cols;
168        dd_MatrixPtr ddineq;            //Matrix to store the inequalities
169        ddineq=dd_CreateMatrix(ddrows,ddcols+1); //The first col has to be 0 since cddlib checks for additive consts there
170
171        // We loop through each g\in GB and compute the resulting inequalities
172        for (int i=0; i<IDELEMS(I); i++)
173        {
174                aktpoly=(poly)I->m[i];          //get aktpoly as i-th component of I
175                pCompCount=pLength(aktpoly);    //How many terms does aktpoly consist of?
176                cout << "Poly No. " << i << " has " << pCompCount << " components" << endl;
177
178                int *v=(int *)omAlloc((numvar+1)*sizeof(int));
179                pGetExpV(aktpoly,v);    //find the exp.vect in v[1],...,v[n], use pNext(p)
180               
181                //Store leadexp for aktpoly
182                for (int kk=0;kk<numvar;kk++)
183                {
184                        leadexp[kk]=v[kk+1];
185                        //printf("Leadexpcomp=%i\n",leadexp[kk]);
186                        //Since we need to know the difference of leadexp with the other expvects we do nothing here
187                        //but compute the diff below
188                }
189
190               
191                while (pNext(aktpoly)!=NULL) //move to next term until NULL
192                {
193                        aktpoly=pNext(aktpoly);
194                        pSetm(aktpoly);         //doesn't seem to help anything
195                        pGetExpV(aktpoly,v);
196                        for (int kk=0;kk<numvar;kk++)
197                        {
198//The ordering somehow gets lost here but this is acceptable, since we are only interested in the inequalities
199                                aktexp[kk]=v[kk+1];
200                                //printf("aktexpcomp=%i\n",aktexp[kk]);
201                                //ineq[aktmatrixrow][kk]=leadexp[kk]-aktexp[kk];        //dito
202                                dd_set_si(ddineq->matrix[(dd_rowrange)aktmatrixrow][kk+1],leadexp[kk]-aktexp[kk]); //because of the 1st col being const 0
203                        }
204                        aktmatrixrow=aktmatrixrow+1;
205                }//while
206
207        } //for
208
209        //Maybe add another row to contain the constraints of the standard simplex?
210
211        #ifdef gfan_DEBUG
212        cout << "The inequality matrix is" << endl;
213        dd_WriteMatrix(stdout, ddineq);
214        #endif
215
216        // The inequalities are now stored in ddineq
217        // Next we check for superflous rows
218        ddredrows = dd_RedundantRows(ddineq, &dderr);
219        if (dderr!=dd_NoError)                  // did an error occur?
220        {
221                 dd_WriteErrorMessages(stderr,dderr);   //if so tell us
222        } else
223        {
224                cout << "Redundant rows: ";
225                set_fwrite(stdout, ddredrows);          //otherwise print the redundant rows
226        }//if dd_Error
227
228        //Remove reduntant rows here!
229        dd_MatrixCanonicalize(&ddineq, &ddlinset, &ddredrows, &ddnewpos, &dderr);
230        ddrows = ddineq->rowsize;       //Size of the matrix with redundancies removed
231        ddcols = ddineq->colsize;
232        #ifdef gfan_DEBUG
233        cout << "Having removed redundancies, the normals now read:" << endl;
234        dd_WriteMatrix(stdout,ddineq);
235        cout << "Rows = " << ddrows << endl;
236        cout << "Cols = " << ddcols << endl;
237        #endif
238
239        /*dd_PolyhedraPtr ddpolyh;
240        dd_MatrixPtr G;
241        ddpolyh=dd_DDMatrix2Poly(ddineq, &dderr);
242        G=dd_CopyGenerators(ddpolyh);
243        printf("\nSpanning vectors = rows:\n");
244        dd_WriteMatrix(stdout, G);
245        */
246       
247       
248        /*Write the normals into class facet
249                How do I get the #rows in ddineq? \exists s.th. like dd_NumRows? dd_get_si(ddineq->matrix[i][j]) ?
250        Strange enough: facet *f=new facet(intvec(2,3)) does work for the constructor facet(intvec FN){fNormal = FN;}
251        */
252        #ifdef gfan_DEBUG
253        cout << "Creating list of normals" << endl;
254        #endif
255        /*The pointer *fRoot should be the return value of this function*/
256        facet *fRoot = new facet();             //instantiate new facet with intvec with numvar rows, one column and initial values all 0
257        //facet *fRoot;
258        facet *fAct;                    //instantiate pointer to active facet
259        fAct = (facet*)&fRoot; 
260        //fAct = fRoot;                 //Let fAct point to fRoot
261        for (int kk = 0; kk<ddrows; kk++)
262        {
263                intvec load;    //intvec to sto
264                for (int jj = 1; jj <ddcols; jj++)
265                {
266                        double *foo;
267                        foo = (double*)ddineq->matrix[kk][jj];  //get entry from actual position
268#ifdef gfan_DEBUG
269                        std::cout << "fAct is " << *foo << " at " << fAct << endl;
270#endif
271                        /*next two lines commented out. How to store values into intvec? */
272                        //load[jj] = (int)*foo;                 //store typecasted entry at pos jj of load
273                        //fAct->setFacetNormal(load);
274                        if (jj<ddcols)                          //Is this facet NOT the last facet? Writing while instead of if is a really bad idea :)
275                        {
276                                fAct->next = new facet();       //If so: instantiate new facet. Otherwise this->next=NULL due to the constructor
277                                fAct = fAct->next;              //scary :)
278                        }
279                }
280        }
281        /*
282        Now we should have a concatenated list containing the facet normals which we can adress via *f
283        */
284       
285        //ddineq->representation=dd_Inequality;         //We want our LP to be Ax>=0
286        //Clean up but don't delete the return value! (Whatever it will turn out to be)
287        dd_FreeMatrix(ddineq);
288        //set_free(ddrows);
289        //set_free(ddcols);
290        set_free(ddredrows);
291        //set_free(ddnumb);
292        //set_free(dderr);
293        free(ddnewpos);
294        //set_free(ddlinset);
295        dd_free_global_constants();
296
297
298        return *fRoot;
299}
300
301ideal gfan(ideal inputIdeal)
302{
303        #ifdef gfan_DEBUG
304        cout << "Now in subroutine gfan" << endl;
305        #endif
306        ideal res;
307        matrix ineq; //Matrix containing the boundary inequalities
308        /*
309        1. Select target order
310        2. Compute GB of inputIdeal wrt target order -> newRing, setCurrRing etc...
311        3. getConeNormals
312        */
313        res=getGB(inputIdeal);
314        getConeNormals(res);
315        return res;
316}
317#endif
Note: See TracBrowser for help on using the repository browser.