source: git/kernel/gfan.cc @ 1534d9

spielwiese
Last change on this file since 1534d9 was 18765e, checked in by Martin Monerjan, 15 years ago
more stuff into class {facet,gcone} git-svn-id: file:///usr/local/Singular/svn/trunk@11504 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2Compute the Groebner fan of an ideal
3Author: $Author: monerjan $
4Date: $Date: 2009-03-02 14:44:49 $
5Header: $Header: /exports/cvsroot-2/cvsroot/kernel/gfan.cc,v 1.17 2009-03-02 14:44:49 monerjan Exp $
6Id: $Id: gfan.cc,v 1.17 2009-03-02 14:44: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 "/users/urmel/alggeom/monerjan/cddlib/include/setoper.h" //Support for cddlib. Dirty hack
19#include "/users/urmel/alggeom/monerjan/cddlib/include/cdd.h"
20
21#ifndef gfan_DEBUG
22#define gfan_DEBUG
23#endif
24
25class facet
26{
27        private:
28                intvec fNormal;         //inner normal, describing the facet uniquely
29        public:
30                facet();                //constructor
31                bool isFlippable;       //flippable facet? Want to have cone->isflippable.facet[i]
32                bool isIncoming;        //Is the facet incoming or outgoing?
33};
34
35/*class gcone
36finally this should become s.th. like gconelib.{h,cc} to provide an API
37*/
38class gcone
39{
40        private:
41                int numFacets;          //#of facets of the cone
42
43        public:
44                gcone(int);             //constructor with dimension
45                poly gcMarkedTerm;      //marked terms of the cone's Gröbner basis
46                ideal gcBasis;          //GB of the cone
47                gcone *next;            //Pointer to *previous* cone in search tree
48       
49                void flip();            //Compute "the other side"
50                void remRedFacets();    //Remove redundant facets of the cone
51               
52
53};//class gcone
54
55ideal getGB(ideal inputIdeal)
56{
57        #ifdef gfan_DEBUG
58        printf("Computing a groebner basis...\n");
59        #endif
60
61        ideal gb;
62        gb=kStd(inputIdeal,NULL,testHomog,NULL); //Possible to call without testHomog/isHomog?
63        idSkipZeroes(gb); //Get rid of zero entries
64
65        return gb;
66}
67
68/****** getWallIneq computes the inequalities ***/
69/*INPUT_TYPE: ideal                             */
70/*RETURN_TYPE: matrix                           */
71/************************************************/
72void getWallIneq(ideal I)
73{
74        #ifdef gfan_DEBUG
75        printf("*** Computing Inequalities... ***\n");
76        #endif
77
78        //All variables go here - except ineq matrix and *v, see below
79        int lengthGB=IDELEMS(I);        // # of polys in the groebner basis
80        int pCompCount;                 // # of terms in a poly
81        poly aktpoly;
82        int numvar = pVariables;        // # of variables in a polynomial (or ring?)
83        int leadexp[numvar];            // dirty hack of exp.vects
84        int aktexp[numvar];
85        int cols,rows;                  // will contain the dimensions of the ineq matrix - deprecated by
86        dd_rowrange ddrows;
87        dd_colrange ddcols;
88        dd_rowset ddredrows;            // # of redundant rows in ddineq
89        dd_rowset ddlinset;             // the opposite
90        dd_rowindex ddnewpos;           // all to make dd_Canonicalize happy
91        dd_NumberType ddnumb=dd_Real;   //Number type
92        dd_ErrorType dderr=dd_NoError;  //
93        // End of var declaration
94
95        printf("The Groebner basis has %i elements\n",lengthGB);
96        printf("The current ring has %i variables\n",numvar);
97        cols = numvar;
98
99        //Compute the # inequalities i.e. rows of the matrix
100        rows=0; //Initialization
101        for (int ii=0;ii<IDELEMS(I);ii++)
102        {
103                aktpoly=(poly)I->m[ii];
104                rows=rows+pLength(aktpoly)-1;
105        }
106        printf("rows=%i\n",rows);
107        printf("Will create a %i x %i - matrix to store inequalities\n",rows,cols);
108
109        dd_rowrange aktmatrixrow=0;     // needed to store the diffs of the expvects in the rows of ddineq
110        dd_set_global_constants();
111        ddrows=rows;
112        ddcols=cols;
113        dd_MatrixPtr ddineq;            //Matrix to store the inequalities
114        ddineq=dd_CreateMatrix(ddrows,ddcols+1); //The first col has to be 0 since cddlib checks for additive consts there
115
116        // We loop through each g\in GB and compute the resulting inequalities
117        for (int i=0; i<IDELEMS(I); i++)
118        {
119                aktpoly=(poly)I->m[i];          //get aktpoly as i-th component of I
120                pCompCount=pLength(aktpoly);    //How many terms does aktpoly consist of?
121                printf("Poly No. %i has %i components\n",i,pCompCount);
122
123                int *v=(int *)omAlloc((numvar+1)*sizeof(int));
124                pGetExpV(aktpoly,v);    //find the exp.vect in v[1],...,v[n], use pNext(p)
125               
126                //Store leadexp for aktpoly
127                for (int kk=0;kk<numvar;kk++)
128                {
129                        leadexp[kk]=v[kk+1];
130                        //printf("Leadexpcomp=%i\n",leadexp[kk]);
131                        //Since we need to know the difference of leadexp with the other expvects we do nothing here
132                        //but compute the diff below
133                }
134
135               
136                while (pNext(aktpoly)!=NULL) //move to next term until NULL
137                {
138                        aktpoly=pNext(aktpoly);
139                        pSetm(aktpoly);         //doesn't seem to help anything
140                        pGetExpV(aktpoly,v);
141                        for (int kk=0;kk<numvar;kk++)
142                        {
143//The ordering somehow gets lost here but this is acceptable, since we are only interested in the inequalities
144                                aktexp[kk]=v[kk+1];
145                                //printf("aktexpcomp=%i\n",aktexp[kk]);
146                                //ineq[aktmatrixrow][kk]=leadexp[kk]-aktexp[kk];        //dito
147                                dd_set_si(ddineq->matrix[(dd_rowrange)aktmatrixrow][kk+1],leadexp[kk]-aktexp[kk]); //because of the 1st col being const 0
148                        }
149                        aktmatrixrow=aktmatrixrow+1;
150                }//while
151
152        } //for
153
154        //Maybe add another row to contain the constraints of the standard simplex?
155
156        #ifdef gfan_DEBUG
157        printf("The inequality matrix is:\n");
158        dd_WriteMatrix(stdout, ddineq);
159        #endif
160
161        // The inequalities are now stored in ddineq
162        // Next we check for superflous rows
163        ddredrows = dd_RedundantRows(ddineq, &dderr);
164        if (dderr!=dd_NoError)                  // did an error occur?
165        {
166                 dd_WriteErrorMessages(stderr,dderr);   //if so tell us
167        } else
168        {
169                printf("Redundant rows: ");
170                set_fwrite(stdout, ddredrows);          //otherwise print the redundant rows
171        }//if dd_Error
172
173        //Remove reduntant rows here!
174        dd_MatrixCanonicalize(&ddineq, &ddlinset, &ddredrows, &ddnewpos, &dderr);
175        #ifdef gfan_DEBUG
176        printf("Having removed redundant rows, the inequalities now read:\n");
177        dd_WriteMatrix(stdout,ddineq);
178        #endif
179
180        dd_PolyhedraPtr ddpolyh;
181        dd_MatrixPtr G;
182        ddpolyh=dd_DDMatrix2Poly(ddineq, &dderr);
183        G=dd_CopyGenerators(ddpolyh);
184        printf("\nSpanning vectors = rows:\n");
185        dd_WriteMatrix(stdout, G);
186
187        //ddineq->representation=dd_Inequality;         //We want our LP to be Ax>=0
188
189        //Clean up but don't delete the return value! (Whatever it will turn out to be)
190        dd_FreeMatrix(ddineq);
191        //set_free(ddrows);
192        //set_free(ddcols);
193        set_free(ddredrows);
194        //set_free(ddnumb);
195        //set_free(dderr);
196        free(ddnewpos);
197        //set_free(ddlinset);
198        dd_free_global_constants();
199
200        //res=(ideal)aktpoly;
201        //return res;
202}
203
204ideal gfan(ideal inputIdeal)
205{
206        #ifdef gfan_DEBUG
207        printf("Now in subroutine gfan\n");
208        #endif
209        ideal res;
210        matrix ineq; //Matrix containing the boundary inequalities
211
212        res=getGB(inputIdeal);
213        getWallIneq(res);
214        return res;
215}
216#endif
Note: See TracBrowser for help on using the repository browser.