source: git/kernel/gfan.cc @ d51339

spielwiese
Last change on this file since d51339 was d59666c, checked in by Martin Monerjan, 15 years ago
dd_MatrixCanonicalize - remove redundant rows from ddineq git-svn-id: file:///usr/local/Singular/svn/trunk@11402 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2Compute the Groebner fan of an ideal
3Author: $Author: monerjan $
4Date: $Date: 2009-02-16 16:16:21 $
5Header: $Header: /exports/cvsroot-2/cvsroot/kernel/gfan.cc,v 1.13 2009-02-16 16:16:21 monerjan Exp $
6Id: $Id: gfan.cc,v 1.13 2009-02-16 16:16:21 monerjan Exp $
7*/
8
9#include "mod2.h"
10
11//A hack that hopefully will make compiler happy. Workaround only
12//do the same in extra.cc and remove it befor committing!
13#ifndef HAVE_GFAN
14#define HAVE_GFAN
15#endif
16
17#ifdef HAVE_GFAN
18
19#include "kstd1.h"
20#include "intvec.h"
21#include "polys.h"
22#include "ideals.h"
23#include "kmatrix.h"
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
27#ifndef gfan_DEBUG
28#define gfan_DEBUG
29#endif
30
31ideal getGB(ideal inputIdeal)
32{
33        #ifdef gfan_DEBUG
34        printf("Now in getGB\n");
35        #endif
36
37        ideal gb;
38        gb=kStd(inputIdeal,NULL,testHomog,NULL); //Possible to call without testHomog/isHomog?
39        idSkipZeroes(gb); //Get rid of zero entries
40
41        return gb;
42}
43
44/****** getWallIneq computes the inequalities ***/
45/*INPUT_TYPE: ideal                             */
46/*RETURN_TYPE: matrix                           */
47/************************************************/
48void getWallIneq(ideal I)
49{
50        #ifdef gfan_DEBUG
51        printf("*** Computing Inequalities... ***\n");
52        #endif
53
54        //All variables go here - except ineq matrix and *v, see below
55        int lengthGB=IDELEMS(I);        // # of polys in the groebner basis
56        int pCompCount;                 // # of terms in a poly
57        poly aktpoly;
58        int numvar = pVariables;        // # of variables in a polynomial (or ring?)
59        int leadexp[numvar];            // dirty hack of exp.vects
60        int aktexp[numvar];
61        int cols,rows;                  // will contain the dimensions of the ineq matrix - deprecated by
62        dd_rowrange ddrows;
63        dd_colrange ddcols;
64        dd_rowset ddredrows;            // # of redundant rows in ddineq
65        dd_rowset ddlinset;             // the opposite
66        dd_rowindex ddnewpos;           // all to make dd_Canonicalize happy
67        dd_NumberType ddnumb=dd_Real;   //Number type
68        dd_ErrorType dderr=dd_NoError;  //
69        // End of var declaration
70
71        printf("The Groebner basis has %i elements\n",lengthGB);
72        printf("The current ring has %i variables\n",numvar);
73        cols = numvar;
74
75        //Compute the # inequalities i.e. rows of the matrix
76        rows=0; //Initialization
77        for (int ii=0;ii<IDELEMS(I);ii++)
78        {
79                aktpoly=(poly)I->m[ii];
80                rows=rows+pLength(aktpoly)-1;
81        }
82        printf("rows=%i\n",rows);
83        printf("Will create a %i x %i - matrix to store inequalities\n",rows,cols);
84
85        dd_rowrange aktmatrixrow=0;     // needed to store the diffs of the expvects in the rows of ddineq
86        dd_set_global_constants();
87        ddrows=rows;
88        ddcols=cols;
89        dd_MatrixPtr ddineq;            //Matrix to store the inequalities
90        ddineq=dd_CreateMatrix(ddrows,ddcols+1); //The first col has to be 0 since cddlib checks for additive consts there
91
92        // We loop through each g\in GB and compute the resulting inequalities
93        for (int i=0; i<IDELEMS(I); i++)
94        {
95                aktpoly=(poly)I->m[i];          //get aktpoly as i-th component of I
96                pCompCount=pLength(aktpoly);    //How many terms does aktpoly consist of?
97                printf("Poly No. %i has %i components\n",i,pCompCount);
98
99                int *v=(int *)omAlloc((numvar+1)*sizeof(int));
100                pGetExpV(aktpoly,v);    //find the exp.vect in v[1],...,v[n], use pNext(p)
101               
102                //Store leadexp for aktpoly
103                for (int kk=0;kk<numvar;kk++)
104                {
105                        leadexp[kk]=v[kk+1];
106                        //printf("Leadexpcomp=%i\n",leadexp[kk]);
107                        //Since we need to know the difference of leadexp with the other expvects we do nothing here
108                        //but compute the diff below
109                }
110
111               
112                while (pNext(aktpoly)!=NULL) //move to next term until NULL
113                {
114                        aktpoly=pNext(aktpoly);
115                        pSetm(aktpoly);         //doesn't seem to help anything
116                        pGetExpV(aktpoly,v);
117                        for (int kk=0;kk<numvar;kk++)
118                        {
119//The ordering somehow gets lost here but this is acceptable, since we are only interested in the inequalities
120                                aktexp[kk]=v[kk+1];
121                                //printf("aktexpcomp=%i\n",aktexp[kk]);
122                                //ineq[aktmatrixrow][kk]=leadexp[kk]-aktexp[kk];        //dito
123                                dd_set_si(ddineq->matrix[(dd_rowrange)aktmatrixrow][kk+1],leadexp[kk]-aktexp[kk]); //because of the 1st col being const 0
124                        }
125                        aktmatrixrow=aktmatrixrow+1;
126                }//while
127
128        } //for
129
130        //Maybe add another row to contain the constraints of the standard simplex?
131
132        #ifdef gfan_DEBUG
133        printf("The inequality matrix is:\n");
134        dd_WriteMatrix(stdout, ddineq);
135        #endif
136
137        // The inequalities are now stored in ddineq
138        // Next we check for superflous rows
139        ddredrows = dd_RedundantRows(ddineq, &dderr);
140        if (dderr!=dd_NoError)                  // did an error occur?
141        {
142                 dd_WriteErrorMessages(stderr,dderr);   //if so tell us
143        } else
144        {
145                printf("Redundant rows: ");
146                set_fwrite(stdout, ddredrows);          //otherwise print the redundant rows
147        }//if dd_Error
148
149        //Remove reduntant rows here!
150        dd_MatrixCanonicalize(&ddineq, &ddlinset, &ddredrows, &ddnewpos, &dderr);
151        #ifdef gfan_DEBUG
152        printf("Having removed redundant rows, the inequalities now read:\n");
153        dd_WriteMatrix(stdout,ddineq);
154        #endif
155
156        //ddineq->representation=dd_Inequality;         //We want our LP to be Ax>=0
157
158        //Clean up but don't delete the return value! (Whatever it will turn out to be)
159        dd_FreeMatrix(ddineq);
160        //set_free(ddrows);
161        //set_free(ddcols);
162        set_free(ddredrows);
163        //set_free(ddnumb);
164        //set_free(dderr);
165        free(ddnewpos);
166        //set_free(ddlinset);
167        dd_free_global_constants();
168
169        //res=(ideal)aktpoly;
170        //return res;
171}
172
173ideal gfan(ideal inputIdeal)
174{
175        #ifdef gfan_DEBUG
176        printf("Now in subroutine gfan\n");
177        #endif
178        ideal res;
179        matrix ineq; //Matrix containing the boundary inequalities
180
181        res=getGB(inputIdeal);
182        getWallIneq(res);
183        return res;
184}
185#endif
Note: See TracBrowser for help on using the repository browser.