1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | // $Id: fglmhom.cc,v 1.13 1999-07-15 13:37:33 wichmann Exp $ |
---|
3 | |
---|
4 | /**************************************** |
---|
5 | * Computer Algebra System SINGULAR * |
---|
6 | ****************************************/ |
---|
7 | /* |
---|
8 | * ABSTRACT - The FGLM-Algorithm extended for homogeneous ideals. |
---|
9 | * Calculates via the hilbert-function a groebner basis. |
---|
10 | */ |
---|
11 | |
---|
12 | |
---|
13 | #include "mod2.h" |
---|
14 | #ifdef HAVE_FGLM_HOM |
---|
15 | #ifndef NOSTREAMIO |
---|
16 | #include <iostream.h> |
---|
17 | #endif |
---|
18 | #include "tok.h" |
---|
19 | #include "structs.h" |
---|
20 | #include "subexpr.h" |
---|
21 | #include "polys.h" |
---|
22 | #include "ideals.h" |
---|
23 | #include "ring.h" |
---|
24 | #include "ipid.h" |
---|
25 | #include "ipshell.h" |
---|
26 | #include "febase.h" |
---|
27 | #include "maps.h" |
---|
28 | #include "mmemory.h" |
---|
29 | #include "fglm.h" |
---|
30 | #include "fglmvec.h" |
---|
31 | #include "fglmgauss.h" |
---|
32 | #include "intvec.h" |
---|
33 | #include "kstd1.h" |
---|
34 | #include "stairc.h" // -> hHStdSeries, hFirstSeries usw. |
---|
35 | #include <templates/ftmpl_list.h> |
---|
36 | |
---|
37 | // obachman: Got rid off those "redefiende messages by includeing fglm.h |
---|
38 | #include "fglm.h" |
---|
39 | #if 0 |
---|
40 | #define PROT(msg) if (BTEST1(OPT_PROT)) Print(msg) |
---|
41 | #define STICKYPROT(msg) if (BTEST1(OPT_PROT)) Print(msg) |
---|
42 | #define PROT2(msg,arg) if (BTEST1(OPT_PROT)) Print(msg,arg) |
---|
43 | #define STICKYPROT2(msg,arg) if (BTEST1(OPT_PROT)) Print(msg,arg) |
---|
44 | #define fglmASSERT(ignore1,ignore2) |
---|
45 | #endif |
---|
46 | |
---|
47 | struct doublepoly |
---|
48 | { |
---|
49 | poly sm; |
---|
50 | poly dm; |
---|
51 | }; |
---|
52 | |
---|
53 | class homogElem |
---|
54 | { |
---|
55 | public: |
---|
56 | doublepoly mon; |
---|
57 | fglmVector v; |
---|
58 | fglmVector dv; |
---|
59 | int basis; |
---|
60 | int destbasis; |
---|
61 | BOOLEAN inDest; |
---|
62 | homogElem() : v(), dv(), basis(0), destbasis(0), inDest(FALSE) {} |
---|
63 | homogElem( poly m, int b, BOOLEAN ind ) : |
---|
64 | basis(b), inDest(ind) |
---|
65 | { |
---|
66 | mon.dm= m; |
---|
67 | mon.sm= NULL; |
---|
68 | } |
---|
69 | #ifndef HAVE_EXPLICIT_CONSTR |
---|
70 | void initialize( poly m, int b, BOOLEAN ind ) |
---|
71 | { |
---|
72 | basis = b; |
---|
73 | inDest = ind; |
---|
74 | mon.dm = m; |
---|
75 | mon.sm = NULL; |
---|
76 | } |
---|
77 | void initialize( const homogElem h ) |
---|
78 | { |
---|
79 | basis = h.basis; |
---|
80 | inDest = h.inDest; |
---|
81 | mon.dm = h.mon.dm; |
---|
82 | mon.sm = h.mon.sm; |
---|
83 | } |
---|
84 | #endif |
---|
85 | }; |
---|
86 | |
---|
87 | struct homogData |
---|
88 | { |
---|
89 | ideal sourceIdeal; |
---|
90 | doublepoly * sourceHeads; |
---|
91 | int numSourceHeads; |
---|
92 | ideal destIdeal; |
---|
93 | int numDestPolys; |
---|
94 | homogElem * monlist; |
---|
95 | int monlistmax; |
---|
96 | int monlistblock; |
---|
97 | int numMonoms; |
---|
98 | int basisSize; |
---|
99 | int overall; // nur zum testen. |
---|
100 | int numberofdestbasismonoms; |
---|
101 | // homogData() : sourceHeads(NULL), numSourceHeads(0), monlist(NULL), |
---|
102 | // numMonoms(0), basisSize(0) {} |
---|
103 | }; |
---|
104 | |
---|
105 | int |
---|
106 | hfglmNextdegree( intvec * source, ideal current, int & deg ) |
---|
107 | { |
---|
108 | int numelems; |
---|
109 | intvec * newhilb = hHstdSeries( current, NULL, currQuotient ); |
---|
110 | |
---|
111 | loop |
---|
112 | { |
---|
113 | if ( deg < newhilb->length() ) |
---|
114 | { |
---|
115 | if ( deg < source->length() ) |
---|
116 | numelems= (*newhilb)[deg]-(*source)[deg]; |
---|
117 | else |
---|
118 | numelems= (*newhilb)[deg]; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | if (deg < source->length()) |
---|
123 | numelems= -(*source)[deg]; |
---|
124 | else |
---|
125 | { |
---|
126 | deg= 0; |
---|
127 | return 0; |
---|
128 | } |
---|
129 | } |
---|
130 | if (numelems != 0) |
---|
131 | return numelems; |
---|
132 | deg++; |
---|
133 | } |
---|
134 | delete newhilb; |
---|
135 | } |
---|
136 | |
---|
137 | void |
---|
138 | generateMonoms( poly m, int var, int deg, homogData * dat ) |
---|
139 | { |
---|
140 | if ( var == pVariables ) { |
---|
141 | BOOLEAN inSource = FALSE; |
---|
142 | BOOLEAN inDest = FALSE; |
---|
143 | poly mon = pCopy( m ); |
---|
144 | pSetExp( mon, var, deg ); |
---|
145 | pSetm( mon ); |
---|
146 | ++dat->overall; |
---|
147 | int i; |
---|
148 | for ( i= dat->numSourceHeads - 1; (i >= 0) && (inSource==FALSE); i-- ) { |
---|
149 | if ( pDivisibleBy( dat->sourceHeads[i].dm, mon ) ) { |
---|
150 | inSource= TRUE; |
---|
151 | } |
---|
152 | } |
---|
153 | for ( i= dat->numDestPolys - 1; (i >= 0) && (inDest==FALSE); i-- ) { |
---|
154 | if ( pDivisibleBy( (dat->destIdeal->m)[i], mon ) ) { |
---|
155 | inDest= TRUE; |
---|
156 | } |
---|
157 | } |
---|
158 | if ( (!inSource) || (!inDest) ) { |
---|
159 | int basis = 0; |
---|
160 | if ( !inSource ) |
---|
161 | basis= ++(dat->basisSize); |
---|
162 | if ( !inDest ) |
---|
163 | ++dat->numberofdestbasismonoms; |
---|
164 | if ( dat->numMonoms == dat->monlistmax ) { |
---|
165 | int k; |
---|
166 | #ifdef HAVE_EXPLICIT_CONSTR |
---|
167 | // Expand array using Singulars ReAlloc function |
---|
168 | dat->monlist= |
---|
169 | (homogElem * )ReAlloc( dat->monlist, |
---|
170 | (dat->monlistmax)*sizeof( homogElem ), |
---|
171 | (dat->monlistmax+dat->monlistblock) * sizeof( homogElem ) ); |
---|
172 | for ( k= dat->monlistmax; k < (dat->monlistmax+dat->monlistblock); k++ ) |
---|
173 | dat->monlist[k].homogElem(); |
---|
174 | #else |
---|
175 | // Expand array by generating new one and copying |
---|
176 | int newsize = dat->monlistmax + dat->monlistblock; |
---|
177 | homogElem * tempelem = new homogElem[ newsize ]; |
---|
178 | // Copy old elements |
---|
179 | for ( k= dat->monlistmax - 1; k >= 0; k-- ) |
---|
180 | tempelem[k].initialize( dat->monlist[k] ); |
---|
181 | delete [] homogElem; |
---|
182 | homogElem = tempelem; |
---|
183 | #endif |
---|
184 | dat->monlistmax+= dat->monlistblock; |
---|
185 | } |
---|
186 | #ifdef HAVE_EXPLICIT_CONSTR |
---|
187 | dat->monlist[dat->numMonoms]= homogElem( mon, basis, inDest ); |
---|
188 | #else |
---|
189 | dat->monlist[dat->numMonoms].initialize( mon, basis, inDest ); |
---|
190 | #endif |
---|
191 | dat->numMonoms++; |
---|
192 | if ( inSource && ! inDest ) PROT( "\\" ); |
---|
193 | if ( ! inSource && inDest ) PROT( "/" ); |
---|
194 | if ( ! inSource && ! inDest ) PROT( "." ); |
---|
195 | } |
---|
196 | else { |
---|
197 | pDelete( & mon ); |
---|
198 | } |
---|
199 | return; |
---|
200 | } |
---|
201 | else { |
---|
202 | poly newm = pCopy( m ); |
---|
203 | while ( deg >= 0 ) { |
---|
204 | generateMonoms( newm, var+1, deg, dat ); |
---|
205 | pIncrExp( newm, var ); |
---|
206 | pSetm( newm ); |
---|
207 | deg--; |
---|
208 | } |
---|
209 | pDelete( & newm ); |
---|
210 | } |
---|
211 | return; |
---|
212 | } |
---|
213 | |
---|
214 | void |
---|
215 | mapMonoms( ring oldRing, homogData & dat ) |
---|
216 | { |
---|
217 | int * vperm = (int *)Alloc( (currRing->N + 1)*sizeof(int) ); |
---|
218 | maFindPerm( oldRing->names, oldRing->N, NULL, 0, currRing->names, currRing->N, NULL, 0, vperm, NULL ); |
---|
219 | nSetMap( oldRing->ch, oldRing->parameter, oldRing->P, oldRing->minpoly ); |
---|
220 | int s; |
---|
221 | for ( s= dat.numMonoms - 1; s >= 0; s-- ) { |
---|
222 | // dat.monlist[s].mon.sm= pPermPoly( dat.monlist[s].mon.dm, vperm, currRing->N, NULL, 0 ); |
---|
223 | // obachman: changed the folowing to reflect the new calling interface of |
---|
224 | // pPermPoly -- Tim please check whether this is correct! |
---|
225 | dat.monlist[s].mon.sm= pPermPoly( dat.monlist[s].mon.dm, vperm, oldRing, NULL, 0 ); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | void |
---|
230 | getVectorRep( homogData & dat ) |
---|
231 | { |
---|
232 | // Calculate the NormalForms |
---|
233 | int s; |
---|
234 | for ( s= 0; s < dat.numMonoms; s++ ) { |
---|
235 | if ( dat.monlist[s].inDest == FALSE ) { |
---|
236 | fglmVector v; |
---|
237 | if ( dat.monlist[s].basis == 0 ) { |
---|
238 | v= fglmVector( dat.basisSize ); |
---|
239 | // now the monom is in L(source) |
---|
240 | PROT( "(" ); |
---|
241 | poly nf = kNF( dat.sourceIdeal, NULL, dat.monlist[s].mon.sm ); |
---|
242 | PROT( ")" ); |
---|
243 | poly temp = nf; |
---|
244 | while (temp != NULL ) { |
---|
245 | int t; |
---|
246 | for ( t= dat.numMonoms - 1; t >= 0; t-- ) { |
---|
247 | if ( dat.monlist[t].basis > 0 ) { |
---|
248 | if ( pEqual( dat.monlist[t].mon.sm, temp ) ) { |
---|
249 | number coeff= nCopy( pGetCoeff( temp ) ); |
---|
250 | v.setelem( dat.monlist[t].basis, coeff ); |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|
254 | pIter(temp); |
---|
255 | } |
---|
256 | pDelete( & nf ); |
---|
257 | } |
---|
258 | else { |
---|
259 | PROT( "." ); |
---|
260 | v= fglmVector( dat.basisSize, dat.monlist[s].basis ); |
---|
261 | } |
---|
262 | dat.monlist[s].v= v; |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | void |
---|
268 | remapVectors( ring oldring, homogData & dat ) |
---|
269 | { |
---|
270 | nSetMap( oldring->ch, oldring->parameter, oldring->P, oldring->minpoly ); |
---|
271 | int s; |
---|
272 | for ( s= dat.numMonoms - 1; s >= 0; s-- ) { |
---|
273 | if ( dat.monlist[s].inDest == FALSE ) { |
---|
274 | int k; |
---|
275 | fglmVector newv( dat.basisSize ); |
---|
276 | for ( k= dat.basisSize; k > 0; k-- ){ |
---|
277 | number newnum= nMap( dat.monlist[s].v.getelem( k ) ); |
---|
278 | newv.setelem( k, newnum ); |
---|
279 | } |
---|
280 | dat.monlist[s].dv= newv; |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | void |
---|
286 | gaussreduce( homogData & dat, int maxnum, int BS ) |
---|
287 | { |
---|
288 | int s; |
---|
289 | int found= 0; |
---|
290 | |
---|
291 | int destbasisSize = 0; |
---|
292 | gaussReducer gauss( dat.basisSize ); |
---|
293 | |
---|
294 | for ( s= 0; (s < dat.numMonoms) && (found < maxnum); s++ ) { |
---|
295 | if ( dat.monlist[s].inDest == FALSE ) { |
---|
296 | if ( gauss.reduce( dat.monlist[s].dv ) == FALSE ) { |
---|
297 | destbasisSize++; |
---|
298 | dat.monlist[s].destbasis= destbasisSize; |
---|
299 | gauss.store(); |
---|
300 | PROT( "." ); |
---|
301 | } |
---|
302 | else { |
---|
303 | fglmVector p= gauss.getDependence(); |
---|
304 | poly result = pCopy( dat.monlist[s].mon.dm ); |
---|
305 | pSetCoeff( result, nCopy( p.getconstelem( p.size() ) ) ); |
---|
306 | int l = 0; |
---|
307 | int k; |
---|
308 | for ( k= 1; k < p.size(); k++ ) { |
---|
309 | if ( ! p.elemIsZero( k ) ) { |
---|
310 | while ( dat.monlist[l].destbasis != k ) |
---|
311 | l++; |
---|
312 | poly temp = pCopy( dat.monlist[l].mon.dm ); |
---|
313 | pSetCoeff( temp, nCopy( p.getconstelem( k ) ) ); |
---|
314 | result= pAdd( result, temp ); |
---|
315 | } |
---|
316 | } |
---|
317 | if ( ! nGreaterZero( pGetCoeff( result ) ) ) result= pNeg( result ); |
---|
318 | // PROT2( "(%s)", pString( result ) ); |
---|
319 | PROT( "+" ); |
---|
320 | found++; |
---|
321 | (dat.destIdeal->m)[dat.numDestPolys]= result; |
---|
322 | dat.numDestPolys++; |
---|
323 | if ( IDELEMS(dat.destIdeal) == dat.numDestPolys ) { |
---|
324 | pEnlargeSet( & dat.destIdeal->m, IDELEMS( dat.destIdeal ), BS ); |
---|
325 | IDELEMS( dat.destIdeal )+= BS; |
---|
326 | } |
---|
327 | |
---|
328 | } |
---|
329 | |
---|
330 | } |
---|
331 | } |
---|
332 | PROT2( "(%i", s ); |
---|
333 | PROT2( "/%i)", dat.numberofdestbasismonoms ); |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | BOOLEAN |
---|
338 | fglmhomog( idhdl sourceRingHdl, ideal sourceIdeal, idhdl destRingHdl, ideal & destIdeal ) |
---|
339 | { |
---|
340 | #define groebnerBS 16 |
---|
341 | int numGBelems; |
---|
342 | int deg = 0; |
---|
343 | |
---|
344 | homogData dat; |
---|
345 | |
---|
346 | // get the hilbert series and the leading monomials of the sourceIdeal: |
---|
347 | rSetHdl( sourceRingHdl, TRUE ); |
---|
348 | ring sourceRing = currRing; |
---|
349 | |
---|
350 | intvec * hilb = hHstdSeries( sourceIdeal, NULL, currQuotient ); |
---|
351 | int s; |
---|
352 | dat.sourceIdeal= sourceIdeal; |
---|
353 | dat.sourceHeads= (doublepoly *)Alloc( IDELEMS( sourceIdeal ) * sizeof( doublepoly ) ); |
---|
354 | for ( s= IDELEMS( sourceIdeal ) - 1; s >= 0; s-- ) { |
---|
355 | dat.sourceHeads[s].sm= pHead( (sourceIdeal->m)[s] ); |
---|
356 | } |
---|
357 | dat.numSourceHeads= IDELEMS( sourceIdeal ); |
---|
358 | rSetHdl( destRingHdl, TRUE ); |
---|
359 | ring destRing = currRing; |
---|
360 | |
---|
361 | // Map the sourceHeads to the destRing |
---|
362 | int * vperm = (int *)Alloc( (sourceRing->N + 1)*sizeof(int) ); |
---|
363 | maFindPerm( sourceRing->names, sourceRing->N, NULL, 0, currRing->names, |
---|
364 | currRing->N, NULL, 0, vperm, NULL, currRing->ch); |
---|
365 | nSetMap( sourceRing->ch, sourceRing->parameter, sourceRing->P, sourceRing->minpoly ); |
---|
366 | for ( s= IDELEMS( sourceIdeal ) - 1; s >= 0; s-- ) { |
---|
367 | dat.sourceHeads[s].dm= pPermPoly( dat.sourceHeads[s].sm, vperm, sourceRing, NULL, 0 ); |
---|
368 | } |
---|
369 | |
---|
370 | dat.destIdeal= idInit( groebnerBS, 1 ); |
---|
371 | dat.numDestPolys= 0; |
---|
372 | |
---|
373 | while ( (numGBelems= hfglmNextdegree( hilb, dat.destIdeal, deg )) != 0 ) { |
---|
374 | int num = 0; // the number of monoms of degree deg |
---|
375 | PROT2( "deg= %i ", deg ); |
---|
376 | PROT2( "num= %i\ngen>", numGBelems ); |
---|
377 | dat.monlistblock= 512; |
---|
378 | dat.monlistmax= dat.monlistblock; |
---|
379 | #ifdef HAVE_EXPLICIT_CONSTR |
---|
380 | dat.monlist= (homogElem *)Alloc( dat.monlistmax*sizeof( homogElem ) ); |
---|
381 | int j; |
---|
382 | for ( j= dat.monlistmax - 1; j >= 0; j-- ) dat.monlist[j].homogElem(); |
---|
383 | #else |
---|
384 | dat.monlist = new homogElem[ dat.monlistmax ]; |
---|
385 | #endif |
---|
386 | dat.numMonoms= 0; |
---|
387 | dat.basisSize= 0; |
---|
388 | dat.overall= 0; |
---|
389 | dat.numberofdestbasismonoms= 0; |
---|
390 | |
---|
391 | poly start= pOne(); |
---|
392 | generateMonoms( start, 1, deg, &dat ); |
---|
393 | pDelete( & start ); |
---|
394 | |
---|
395 | PROT2( "(%i/", dat.basisSize ); |
---|
396 | PROT2( "%i)\nvec>", dat.overall ); |
---|
397 | // switch to sourceRing and map monoms |
---|
398 | rSetHdl( sourceRingHdl, TRUE ); |
---|
399 | mapMonoms( destRing, dat ); |
---|
400 | getVectorRep( dat ); |
---|
401 | |
---|
402 | // switch to destination Ring and remap the vectors |
---|
403 | rSetHdl( destRingHdl, TRUE ); |
---|
404 | remapVectors( sourceRing, dat ); |
---|
405 | |
---|
406 | PROT( "<\nred>" ); |
---|
407 | // now do gaussian reduction |
---|
408 | gaussreduce( dat, numGBelems, groebnerBS ); |
---|
409 | |
---|
410 | #ifdef HAVE_EXPLICIT_CONSTR |
---|
411 | Free( (ADDRESS)dat.monlist, dat.monlistmax*sizeof( homogElem ) ); |
---|
412 | #else |
---|
413 | delete [] dat.monlist; |
---|
414 | #endif |
---|
415 | PROT( "<\n" ); |
---|
416 | } |
---|
417 | PROT( "\n" ); |
---|
418 | destIdeal= dat.destIdeal; |
---|
419 | idSkipZeroes( destIdeal ); |
---|
420 | return TRUE; |
---|
421 | } |
---|
422 | |
---|
423 | ideal |
---|
424 | fglmhomProc(leftv first, leftv second) |
---|
425 | { |
---|
426 | idhdl dest= currRingHdl; |
---|
427 | ideal result; |
---|
428 | // in den durch das erste Argument angegeben Ring schalten: |
---|
429 | rSetHdl( (idhdl)first->data, TRUE ); |
---|
430 | idhdl ih= currRing->idroot->get( second->name, myynest ); |
---|
431 | ASSERT( ih!=NULL, "Error: Can't find ideal in ring"); |
---|
432 | rSetHdl( dest, TRUE ); |
---|
433 | |
---|
434 | ideal i= IDIDEAL(ih); |
---|
435 | fglmhomog( (idhdl)first->data, i, dest, result ); |
---|
436 | |
---|
437 | return( result ); |
---|
438 | } |
---|
439 | |
---|
440 | #endif |
---|
441 | |
---|
442 | // Questions: |
---|
443 | // Muss ich einen intvec freigeben? |
---|
444 | // ---------------------------------------------------------------------------- |
---|
445 | // Local Variables: *** |
---|
446 | // compile-command: "make Singular" *** |
---|
447 | // page-delimiter: "^\\(\\|//!\\)" *** |
---|
448 | // fold-internal-margins: nil *** |
---|
449 | // End: *** |
---|