source: git/kernel/gfan.cc @ 9f9b142

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