Changeset 90adba8 in git
- Timestamp:
- Mar 30, 2009, 4:07:21 PM (14 years ago)
- Branches:
- (u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'f875bbaccd0831e36aaed09ff6adeb3eb45aeb94')
- Children:
- 35a0b054b8027b42f72fe226e78e0f61ee6d824a
- Parents:
- 53ec64bb9f72a646c04fa753e0f39d30c6a310f1
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/extra.cc
r53ec64 r90adba8 2 2 * Computer Algebra System SINGULAR * 3 3 *****************************************/ 4 /* $Id: extra.cc,v 1.29 7 2009-03-10 15:47:18 levandovExp $ */4 /* $Id: extra.cc,v 1.298 2009-03-30 14:07:21 monerjan Exp $ */ 5 5 /* 6 6 * ABSTRACT: general interface to internals of Singular ("system" command) … … 3131 3131 #ifdef HAVE_GFAN 3132 3132 /*======== GFAN ==============*/ 3133 /* 3134 WILL HAVE TO CHANGE RETURN TYPE TO LIST_CMD 3135 */ 3133 3136 if (strcmp(sys_cmd,"gfan")==0) 3134 3137 { … … 3141 3144 res->rtyp=IDEAL_CMD; 3142 3145 res->data=(ideal) gfan(I); 3146 //res->rtyp=LIST_CMD; 3147 //res->data= ??? 3143 3148 3144 3149 return FALSE; //Everything went fine -
kernel/gfan.cc
r53ec64 r90adba8 2 2 Compute the Groebner fan of an ideal 3 3 $Author: monerjan $ 4 $Date: 2009-03- 27 12:21:27$5 $Header: /exports/cvsroot-2/cvsroot/kernel/gfan.cc,v 1.2 3 2009-03-27 12:21:27monerjan Exp $6 $Id: gfan.cc,v 1.2 3 2009-03-27 12:21:27monerjan Exp $4 $Date: 2009-03-30 14:07:21 $ 5 $Header: /exports/cvsroot-2/cvsroot/kernel/gfan.cc,v 1.24 2009-03-30 14:07:21 monerjan Exp $ 6 $Id: gfan.cc,v 1.24 2009-03-30 14:07:21 monerjan Exp $ 7 7 */ 8 8 … … 16 16 #include "ideals.h" 17 17 #include "kmatrix.h" 18 #include "fast_maps.h" //Mapping of ideals 18 19 #include "iostream.h" //deprecated 19 20 … … 50 51 { 51 52 private: 52 /** inner normal, describing the facet uniquely */53 /** inner normal, describing the facet uniquely up to isomorphism */ 53 54 intvec *fNormal; 54 55 public: … … 82 83 83 84 /** 84 *\brief Class gcone 85 * Implements the cone structure 85 *\brief Implements the cone structure 86 * 87 * A cone is represented by a linked list of facet normals 88 * @see facet 86 89 */ 87 90 /*class gcone … … 94 97 95 98 public: 96 /** Default constructor. */ 99 /** \brief Default constructor. 100 * 101 * Initialises this->next=NULL and this->facetPtr=NULL 102 */ 97 103 gcone() 98 104 { … … 100 106 this->facetPtr=NULL; 101 107 } 102 gcone(int); //constructor with dimension103 108 ~gcone(); //destructor 104 109 /** Pointer to the first facet */ … … 107 112 ideal gcBasis; //GB of the cone 108 113 gcone *next; //Pointer to *previous* cone in search tree 109 void getConeNormals(); //Compute 114 /** \brief Compute the normals of the cone 115 * 116 * This method computes a representation of the cone in terms of facet normals. It takes an ideal 117 * as its input. Redundancies are automatically removed using cddlib's dd_MatrixCanonicalize. 118 * Other methods for redundancy checkings might be implemented later. See Anders' diss p.44. 119 * Note that in order to use cddlib a 0-th column has to be added to the matrix since cddlib expects 120 * each row to represent an inequality of type const+x1+...+xn <= 0 121 * As a result of this procedure the pointer facetPtr points to the first facet of the cone. 122 */ 123 void getConeNormals(ideal I) 124 { 125 #ifdef gfan_DEBUG 126 cout << "*** Computing Inequalities... ***" << endl; 127 #endif 128 //All variables go here - except ineq matrix and *v, see below 129 int lengthGB=IDELEMS(I); // # of polys in the groebner basis 130 int pCompCount; // # of terms in a poly 131 poly aktpoly; 132 int numvar = pVariables; // # of variables in a polynomial (or ring?) 133 int leadexp[numvar]; // dirty hack of exp.vects 134 int aktexp[numvar]; 135 int cols,rows; // will contain the dimensions of the ineq matrix - deprecated by 136 dd_rowrange ddrows; 137 dd_colrange ddcols; 138 dd_rowset ddredrows; // # of redundant rows in ddineq 139 dd_rowset ddlinset; // the opposite 140 dd_rowindex ddnewpos; // all to make dd_Canonicalize happy 141 dd_NumberType ddnumb=dd_Real; //Number type 142 dd_ErrorType dderr=dd_NoError; // 143 // End of var declaration 144 #ifdef gfan_DEBUG 145 cout << "The Groebner basis has " << lengthGB << " elements" << endl; 146 cout << "The current ring has " << numvar << " variables" << endl; 147 #endif 148 cols = numvar; 149 150 //Compute the # inequalities i.e. rows of the matrix 151 rows=0; //Initialization 152 for (int ii=0;ii<IDELEMS(I);ii++) 153 { 154 aktpoly=(poly)I->m[ii]; 155 rows=rows+pLength(aktpoly)-1; 156 } 157 #ifdef gfan_DEBUG 158 cout << "rows=" << rows << endl; 159 cout << "Will create a " << rows << " x " << cols << " matrix to store inequalities" << endl; 160 #endif 161 dd_rowrange aktmatrixrow=0; // needed to store the diffs of the expvects in the rows of ddineq 162 dd_set_global_constants(); 163 ddrows=rows; 164 ddcols=cols; 165 dd_MatrixPtr ddineq; //Matrix to store the inequalities 166 ddineq=dd_CreateMatrix(ddrows,ddcols+1); //The first col has to be 0 since cddlib checks for additive consts there 167 168 // We loop through each g\in GB and compute the resulting inequalities 169 for (int i=0; i<IDELEMS(I); i++) 170 { 171 aktpoly=(poly)I->m[i]; //get aktpoly as i-th component of I 172 pCompCount=pLength(aktpoly); //How many terms does aktpoly consist of? 173 cout << "Poly No. " << i << " has " << pCompCount << " components" << endl; 174 175 int *v=(int *)omAlloc((numvar+1)*sizeof(int)); 176 pGetExpV(aktpoly,v); //find the exp.vect in v[1],...,v[n], use pNext(p) 177 178 //Store leadexp for aktpoly 179 for (int kk=0;kk<numvar;kk++) 180 { 181 leadexp[kk]=v[kk+1]; 182 //Since we need to know the difference of leadexp with the other expvects we do nothing here 183 //but compute the diff below 184 } 185 186 187 while (pNext(aktpoly)!=NULL) //move to next term until NULL 188 { 189 aktpoly=pNext(aktpoly); 190 pSetm(aktpoly); //doesn't seem to help anything 191 pGetExpV(aktpoly,v); 192 for (int kk=0;kk<numvar;kk++) 193 { 194 aktexp[kk]=v[kk+1]; 195 //ineq[aktmatrixrow][kk]=leadexp[kk]-aktexp[kk]; //dito 196 dd_set_si(ddineq->matrix[(dd_rowrange)aktmatrixrow][kk+1],leadexp[kk]-aktexp[kk]); //because of the 1st col being const 0 197 } 198 aktmatrixrow=aktmatrixrow+1; 199 }//while 200 201 } //for 202 203 //Maybe add another row to contain the constraints of the standard simplex? 204 205 #ifdef gfan_DEBUG 206 cout << "The inequality matrix is" << endl; 207 dd_WriteMatrix(stdout, ddineq); 208 #endif 209 210 // The inequalities are now stored in ddineq 211 // Next we check for superflous rows 212 ddredrows = dd_RedundantRows(ddineq, &dderr); 213 if (dderr!=dd_NoError) // did an error occur? 214 { 215 dd_WriteErrorMessages(stderr,dderr); //if so tell us 216 } else 217 { 218 cout << "Redundant rows: "; 219 set_fwrite(stdout, ddredrows); //otherwise print the redundant rows 220 }//if dd_Error 221 222 //Remove reduntant rows here! 223 dd_MatrixCanonicalize(&ddineq, &ddlinset, &ddredrows, &ddnewpos, &dderr); 224 ddrows = ddineq->rowsize; //Size of the matrix with redundancies removed 225 ddcols = ddineq->colsize; 226 #ifdef gfan_DEBUG 227 cout << "Having removed redundancies, the normals now read:" << endl; 228 dd_WriteMatrix(stdout,ddineq); 229 cout << "Rows = " << ddrows << endl; 230 cout << "Cols = " << ddcols << endl; 231 #endif 232 /*Write the normals into class facet*/ 233 #ifdef gfan_DEBUG 234 cout << "Creating list of normals" << endl; 235 #endif 236 /*The pointer *fRoot should be the return value of this function*/ 237 facet *fRoot = new facet(); //instantiate new facet with intvec with numvar rows, one column and initial values all 0 238 facetPtr = fRoot; //set variable facetPtr of class gcone to first facet 239 facet *fAct; //instantiate pointer to active facet 240 fAct = fRoot; //This does not seem to do the trick. fRoot and fAct have to point to the same adress! 241 std::cout << "fRoot = " << fRoot << ", fAct = " << fAct << endl; 242 for (int kk = 0; kk<ddrows; kk++) 243 { 244 intvec *load = new intvec(numvar); //intvec to store a single facet normal that will then be stored via setFacetNormal 245 for (int jj = 1; jj <ddcols; jj++) 246 { 247 double *foo; 248 foo = (double*)ddineq->matrix[kk][jj]; //get entry from actual position 249 #ifdef gfan_DEBUG 250 std::cout << "fAct is " << *foo << " at " << fAct << std::endl; 251 #endif 252 (*load)[jj-1] = (int)*foo; //store typecasted entry at pos jj-1 of load 253 //check for flipability here 254 if (jj<ddcols) //Is this facet NOT the last facet? Writing while instead of if is a really bad idea :) 255 { 256 fAct->next = new facet(); //If so: instantiate new facet. Otherwise this->next=NULL due to the constructor 257 fAct = fAct->next; //scary :) 258 } 259 }//for jj<ddcols 260 /*Now load should be full and we can call setFacetNormal*/ 261 fAct->setFacetNormal(load); 262 fAct->printNormal(); 263 } 264 /* 265 Now we should have a linked list containing the facet normals of those facets that are 266 -irredundant 267 -flipable 268 Adressing is done via *facetPtr 269 */ 270 271 //Clean up but don't delete the return value! (Whatever it will turn out to be) 272 dd_FreeMatrix(ddineq); 273 set_free(ddredrows); 274 free(ddnewpos); 275 set_free(ddlinset); 276 dd_free_global_constants(); 277 278 }//method getConeNormals(ideal I) 279 110 280 void flip(); //Compute "the other side" 111 void remRedFacets(); //Remove redundant facets of the cone NOT NEEDED since this is already done by cddlib while compunting the normals 281 282 /** \brief Compute a Groebner Basis 283 * 284 * Computes the Groebner basis and stores the result in this->gcBasis 285 *\param ideal 286 *\return void 287 */ 288 void getGB(ideal inputIdeal) 289 { 290 ideal gb; 291 gb=kStd(inputIdeal,NULL,testHomog,NULL); 292 idSkipZeroes(gb); 293 this->gcBasis=gb; //write the GB into gcBasis 294 } 112 295 113 296 ideal GenGrbWlk(ideal, ideal); //Implementation of the Generic Groebner Walk. Needed for a) Computing the sink and b) finding search facets … … 116 299 };//class gcone 117 300 118 ideal getGB(ideal inputIdeal) 119 { 120 #ifdef gfan_DEBUG 121 cout << "Computing a groebner basis..." << endl; 122 #endif 123 124 ideal gb; 125 gb=kStd(inputIdeal,NULL,testHomog,NULL); //Possible to call without testHomog/isHomog? 126 idSkipZeroes(gb); //Get rid of zero entries 127 128 return gb; 129 } 130 131 /** 132 *\brief Compute the representation of a cone 133 * 134 * Detailed description goes here 135 * 136 *\param An ideal 137 * 138 *\return A pointer to a facet 139 */ 140 /****** getConeNormals computes the inequalities ***/ 141 /*INPUT_TYPE: ideal */ 142 /*RETURN_TYPE: pointer to first facet */ 143 /************************************************/ 301 /* 302 function getGB incorporated into class gcone with rev 1.24 303 */ 304 305 //DEPRECATED since rev 1.24 with existence of gcone::getConeNormals(ideal I); 306 //Kept for unknown reasons ;) 144 307 facet *getConeNormals(ideal I) 145 308 { 146 #ifdef gfan_DEBUG 147 cout << "*** Computing Inequalities... ***" << endl; 148 #endif 149 150 //All variables go here - except ineq matrix and *v, see below 151 int lengthGB=IDELEMS(I); // # of polys in the groebner basis 152 int pCompCount; // # of terms in a poly 153 poly aktpoly; 154 int numvar = pVariables; // # of variables in a polynomial (or ring?) 155 int leadexp[numvar]; // dirty hack of exp.vects 156 int aktexp[numvar]; 157 int cols,rows; // will contain the dimensions of the ineq matrix - deprecated by 158 dd_rowrange ddrows; 159 dd_colrange ddcols; 160 dd_rowset ddredrows; // # of redundant rows in ddineq 161 dd_rowset ddlinset; // the opposite 162 dd_rowindex ddnewpos; // all to make dd_Canonicalize happy 163 dd_NumberType ddnumb=dd_Real; //Number type 164 dd_ErrorType dderr=dd_NoError; // 165 // End of var declaration 166 167 cout << "The Groebner basis has " << lengthGB << " elements" << endl; 168 cout << "The current ring has " << numvar << " variables" << endl; 169 cols = numvar; 170 171 //Compute the # inequalities i.e. rows of the matrix 172 rows=0; //Initialization 173 for (int ii=0;ii<IDELEMS(I);ii++) 174 { 175 aktpoly=(poly)I->m[ii]; 176 rows=rows+pLength(aktpoly)-1; 177 } 178 cout << "rows=" << rows << endl; 179 cout << "Will create a " << rows << " x " << cols << " matrix to store inequalities" << endl; 180 181 dd_rowrange aktmatrixrow=0; // needed to store the diffs of the expvects in the rows of ddineq 182 dd_set_global_constants(); 183 ddrows=rows; 184 ddcols=cols; 185 dd_MatrixPtr ddineq; //Matrix to store the inequalities 186 ddineq=dd_CreateMatrix(ddrows,ddcols+1); //The first col has to be 0 since cddlib checks for additive consts there 187 188 // We loop through each g\in GB and compute the resulting inequalities 189 for (int i=0; i<IDELEMS(I); i++) 190 { 191 aktpoly=(poly)I->m[i]; //get aktpoly as i-th component of I 192 pCompCount=pLength(aktpoly); //How many terms does aktpoly consist of? 193 cout << "Poly No. " << i << " has " << pCompCount << " components" << endl; 194 195 int *v=(int *)omAlloc((numvar+1)*sizeof(int)); 196 pGetExpV(aktpoly,v); //find the exp.vect in v[1],...,v[n], use pNext(p) 197 198 //Store leadexp for aktpoly 199 for (int kk=0;kk<numvar;kk++) 200 { 201 leadexp[kk]=v[kk+1]; 202 //printf("Leadexpcomp=%i\n",leadexp[kk]); 203 //Since we need to know the difference of leadexp with the other expvects we do nothing here 204 //but compute the diff below 205 } 206 207 208 while (pNext(aktpoly)!=NULL) //move to next term until NULL 209 { 210 aktpoly=pNext(aktpoly); 211 pSetm(aktpoly); //doesn't seem to help anything 212 pGetExpV(aktpoly,v); 213 for (int kk=0;kk<numvar;kk++) 214 { 215 //The ordering somehow gets lost here but this is acceptable, since we are only interested in the inequalities 216 aktexp[kk]=v[kk+1]; 217 //printf("aktexpcomp=%i\n",aktexp[kk]); 218 //ineq[aktmatrixrow][kk]=leadexp[kk]-aktexp[kk]; //dito 219 dd_set_si(ddineq->matrix[(dd_rowrange)aktmatrixrow][kk+1],leadexp[kk]-aktexp[kk]); //because of the 1st col being const 0 220 } 221 aktmatrixrow=aktmatrixrow+1; 222 }//while 223 224 } //for 225 226 //Maybe add another row to contain the constraints of the standard simplex? 227 228 #ifdef gfan_DEBUG 229 cout << "The inequality matrix is" << endl; 230 dd_WriteMatrix(stdout, ddineq); 231 #endif 232 233 // The inequalities are now stored in ddineq 234 // Next we check for superflous rows 235 ddredrows = dd_RedundantRows(ddineq, &dderr); 236 if (dderr!=dd_NoError) // did an error occur? 237 { 238 dd_WriteErrorMessages(stderr,dderr); //if so tell us 239 } else 240 { 241 cout << "Redundant rows: "; 242 set_fwrite(stdout, ddredrows); //otherwise print the redundant rows 243 }//if dd_Error 244 245 //Remove reduntant rows here! 246 dd_MatrixCanonicalize(&ddineq, &ddlinset, &ddredrows, &ddnewpos, &dderr); 247 ddrows = ddineq->rowsize; //Size of the matrix with redundancies removed 248 ddcols = ddineq->colsize; 249 #ifdef gfan_DEBUG 250 cout << "Having removed redundancies, the normals now read:" << endl; 251 dd_WriteMatrix(stdout,ddineq); 252 cout << "Rows = " << ddrows << endl; 253 cout << "Cols = " << ddcols << endl; 254 #endif 255 256 /*dd_PolyhedraPtr ddpolyh; 257 dd_MatrixPtr G; 258 ddpolyh=dd_DDMatrix2Poly(ddineq, &dderr); 259 G=dd_CopyGenerators(ddpolyh); 260 printf("\nSpanning vectors = rows:\n"); 261 dd_WriteMatrix(stdout, G); 262 */ 263 264 265 /*Write the normals into class facet*/ 266 #ifdef gfan_DEBUG 267 cout << "Creating list of normals" << endl; 268 #endif 269 /*The pointer *fRoot should be the return value of this function*/ 270 facet *fRoot = new facet(); //instantiate new facet with intvec with numvar rows, one column and initial values all 0 271 facet *fAct; //instantiate pointer to active facet 272 fAct = fRoot; //This does not seem to do the trick. fRoot and fAct have to point to the same adress! 273 std::cout << "fRoot = " << fRoot << ", fAct = " << fAct << endl; 274 //fAct = fRoot; //Let fAct point to fRoot 275 for (int kk = 0; kk<ddrows; kk++) 276 { 277 intvec *load = new intvec(numvar); //intvec to store a single facet normal that will then be stored via setFacetNormal 278 for (int jj = 1; jj <ddcols; jj++) 279 { 280 double *foo; 281 foo = (double*)ddineq->matrix[kk][jj]; //get entry from actual position 282 #ifdef gfan_DEBUG 283 std::cout << "fAct is " << *foo << " at " << fAct << std::endl; 284 #endif 285 /*next two lines commented out. How to store values into intvec? */ 286 (*load)[jj-1] = (int)*foo; //store typecasted entry at pos jj-1 of load 287 //fAct->setFacetNormal(load); 288 //check for flipability here 289 if (jj<ddcols) //Is this facet NOT the last facet? Writing while instead of if is a really bad idea :) 290 { 291 fAct->next = new facet(); //If so: instantiate new facet. Otherwise this->next=NULL due to the constructor 292 fAct = fAct->next; //scary :) 293 } 294 }//for jj<ddcols 295 /*Now load should be full and we can call setFacetNormal*/ 296 fAct->setFacetNormal(load); 297 fAct->printNormal(); 298 } 299 /* 300 Now we should have a concatenated list containing the facet normals of those facets that are 301 -irredundant 302 -flipable 303 Adressing is done via *fRoot 304 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 305 */ 306 307 //ddineq->representation=dd_Inequality; //We want our LP to be Ax>=0 308 //Clean up but don't delete the return value! (Whatever it will turn out to be) 309 dd_FreeMatrix(ddineq); 310 set_free(ddredrows); 311 free(ddnewpos); 312 set_free(ddlinset); 313 dd_free_global_constants(); 314 315 return fRoot; 309 return NULL; 316 310 } 317 311 … … 323 317 cout << "Now in subroutine gfan" << endl; 324 318 #endif 325 ring rootRing; // The ring associated to the target ordering 326 rRingOrder_t t; 327 319 ring inputRing=currRing; // The ring the user entered 320 ring rootRing; // The ring associated to the target ordering 328 321 ideal res; 329 matrix ineq;//Matrix containing the boundary inequalities322 //matrix ineq; //Matrix containing the boundary inequalities 330 323 facet *fRoot; 331 332 333 rootRing=rCopy0(currRing);334 rootRing->order[0]=ringorder_dp;335 //t=rootRing->order[0];336 rootRing=rInit(0,2,rootRing->order);337 rootRing=rDefault(currRing->ch,numvar,currRing->names);338 rComplete(rootRing);339 rChangeCurrRing(rootRing);340 cout << "The current ring is " << endl;341 rWrite(rootRing);342 343 gcone *gcRoot = new gcone(); //Instantiate the sink344 gcone *gcAct;345 gcAct = gcRoot;346 347 324 348 325 /* … … 351 328 3. getConeNormals 352 329 */ 353 res=getGB(inputIdeal); 354 fRoot=getConeNormals(res); 355 cout << fRoot << endl; 330 331 332 /* Construct a new ring which will serve as our root 333 Does not yet work as expected. Will work fine with order dp,Dp but otherwise hangs in getGB 334 */ 335 rootRing=rCopy0(currRing); 336 rootRing->order[0]=ringorder_dp; 337 rComplete(rootRing); 338 rChangeCurrRing(rootRing); 339 ideal rootIdeal; 340 /* Fetch the inputIdeal into our rootRing */ 341 map m=(map)idInit(IDELEMS(inputIdeal),0); 342 rootIdeal=fast_map(inputIdeal,inputRing,(ideal)m, currRing); 343 #ifdef gfan_DEBUG 344 cout << "Root ideal is " << endl; 345 idPrint(rootIdeal); 346 cout << "The current ring is " << endl; 347 rWrite(rootRing); 348 cout << endl; 349 #endif 350 351 gcone *gcRoot = new gcone(); //Instantiate the sink 352 gcone *gcAct; 353 gcAct = gcRoot; 354 gcAct->getGB(inputIdeal); 355 gcAct->getConeNormals(gcAct->gcBasis); //hopefully compute the normals 356 /*Now it is time to compute the search facets, respectively start the reverse search. 357 But since we are in the root all facets should be search facets. 358 MIND: AS OF NOW, THE LIST OF FACETS IS NOT PURGED OF NON-FLIPPAPLE FACETS 359 */ 360 361 /*As of now extra.cc expects gfan to return type ideal. Probably this will change in near future. 362 The return type will then be of type LIST_CMD 363 Assume gfan has finished, thus we have enumerated all the cones 364 Create an array of size of #cones. Let each entry in the array contain a pointer to the respective 365 Groebner Basis and merge this somehow with LIST_CMD 366 => Count the cones! 367 */ 368 369 res=gcAct->gcBasis; 370 //cout << fRoot << endl; 356 371 return res; 372 //return GBlist; 357 373 } 358 374 /*
Note: See TracChangeset
for help on using the changeset viewer.