1 | #include "mod2.h" |
---|
2 | #include <omalloc.h> |
---|
3 | #include "p_polys.h" |
---|
4 | #include "prCopy.h" |
---|
5 | #include "ideals.h" |
---|
6 | #include "ring.h" |
---|
7 | #include "febase.h" |
---|
8 | #include "structs.h" |
---|
9 | #include "polys.h" |
---|
10 | #include "stdlib.h" |
---|
11 | |
---|
12 | |
---|
13 | #include "kutil.h" |
---|
14 | #include "kInline.cc" |
---|
15 | #include "kstd1.h" |
---|
16 | #include "kbuckets.h" |
---|
17 | |
---|
18 | #define FULLREDUCTIONS |
---|
19 | #define HEAD_BIN |
---|
20 | #define HOMOGENEOUS_EXAMPLE |
---|
21 | //#define QUICK_SPOLY_TEST |
---|
22 | //#define DIAGONAL_GOING |
---|
23 | //#define RANDOM_WALK |
---|
24 | struct int_pair_node{ |
---|
25 | int_pair_node* next; |
---|
26 | int a; |
---|
27 | int b; |
---|
28 | }; |
---|
29 | #ifdef RANDOM_WALK |
---|
30 | int my_rand(int n){ |
---|
31 | //RANDOM integer beetween 0,..,n-1 |
---|
32 | int erg=(double(rand())/RAND_MAX) *n; |
---|
33 | return ((erg>=n)?erg-1:erg); |
---|
34 | |
---|
35 | } |
---|
36 | #endif |
---|
37 | enum calc_state |
---|
38 | { |
---|
39 | UNCALCULATED, |
---|
40 | HASTREP, |
---|
41 | UNIMPORTANT |
---|
42 | }; |
---|
43 | struct calc_dat |
---|
44 | { |
---|
45 | int* rep; |
---|
46 | char** states; |
---|
47 | ideal S; |
---|
48 | ring r; |
---|
49 | int* lengths; |
---|
50 | long* short_Exps; |
---|
51 | kStrategy strat; |
---|
52 | int** deg; |
---|
53 | int* T_deg; |
---|
54 | int* misses; |
---|
55 | int_pair_node* soon_free; |
---|
56 | #ifdef HEAD_BIN |
---|
57 | struct omBin_s* HeadBin; |
---|
58 | #endif |
---|
59 | int max_misses; |
---|
60 | int found_i; |
---|
61 | int found_j; |
---|
62 | int continue_i; |
---|
63 | int continue_j; |
---|
64 | int n; |
---|
65 | int skipped_i; |
---|
66 | int normal_forms; |
---|
67 | int skipped_pairs; |
---|
68 | int current_degree; |
---|
69 | int misses_counter; |
---|
70 | int misses_series; |
---|
71 | }; |
---|
72 | bool find_next_pair(calc_dat* c); |
---|
73 | void shorten_tails(calc_dat* c, poly monom); |
---|
74 | void replace_pair(int & i, int & j, calc_dat* c); |
---|
75 | void initial_data(calc_dat* c); |
---|
76 | void add_to_basis(poly h, calc_dat* c); |
---|
77 | void do_this_spoly_stuff(int i,int j,calc_dat* c); |
---|
78 | ideal t_rep_gb(ring r,ideal arg_I); |
---|
79 | bool has_t_rep(const int & arg_i, const int & arg_j, calc_dat* state); |
---|
80 | int* make_connections(int from, poly bound, calc_dat* c); |
---|
81 | int* make_connections(int from, int to, poly bound, calc_dat* c); |
---|
82 | void now_t_rep(int arg_i, int arg_j, calc_dat* c); |
---|
83 | int pLcmDeg(poly a, poly b); |
---|
84 | int simple_posInS (kStrategy strat, poly p,int len); |
---|
85 | |
---|
86 | bool find_next_pair(calc_dat* c) |
---|
87 | { |
---|
88 | int start_i,start_j,i,j; |
---|
89 | start_i=0; |
---|
90 | start_j=-1; |
---|
91 | #ifndef HOMOGENEOUS_EXAMPLE |
---|
92 | if (c->misses_series>80){ |
---|
93 | c->current_degree++; |
---|
94 | c->misses_series=0; |
---|
95 | } |
---|
96 | #endif |
---|
97 | start_i=c->continue_i; |
---|
98 | start_j=c->continue_j; |
---|
99 | #ifndef DIAGONAL_GOING |
---|
100 | for (int i=start_i;i<c->n;i++){ |
---|
101 | if (c->T_deg[i]>c->current_degree) |
---|
102 | { |
---|
103 | c->skipped_pairs++; |
---|
104 | continue; |
---|
105 | } |
---|
106 | for(int j=(i==start_i)?start_j+1:0;j<i;j++){ |
---|
107 | // printf("searching at %d,%d",i,j); |
---|
108 | if (c->misses_counter>=2) { |
---|
109 | c->skipped_pairs++; |
---|
110 | break; |
---|
111 | } |
---|
112 | if (c->states[i][j]==UNCALCULATED){ |
---|
113 | if(c->deg[i][j]<=c->current_degree) |
---|
114 | { |
---|
115 | c->continue_i=c->found_i=i; |
---|
116 | c->continue_j=c->found_j=j; |
---|
117 | return true; |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | ++(c->skipped_pairs); |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | c->misses_counter=0; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | if (!((start_i==1) &&(start_j==0))){ |
---|
130 | c->continue_i=1; |
---|
131 | c->continue_j=0; |
---|
132 | return find_next_pair(c); |
---|
133 | } |
---|
134 | |
---|
135 | #else |
---|
136 | int z=0; |
---|
137 | i=start_i; |
---|
138 | j=start_j; |
---|
139 | z=start_i+start_j; |
---|
140 | while(z<=(2*c->n-3)){ |
---|
141 | |
---|
142 | while(i>j){ |
---|
143 | if(i>=c->n) { |
---|
144 | if (c->skipped_i<0) c->skipped_i=i; |
---|
145 | } else{ |
---|
146 | if(c->states[i][j]==UNCALCULATED){ |
---|
147 | if (c->deg[i][j]<=c->current_degree) |
---|
148 | { |
---|
149 | c->continue_i=c->found_i=i; |
---|
150 | c->continue_j=c->found_j=j; |
---|
151 | return true; |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | ++(c->skipped_pairs); |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | --i; |
---|
160 | ++j; |
---|
161 | } |
---|
162 | ++z; |
---|
163 | i=z; |
---|
164 | j=0; |
---|
165 | } |
---|
166 | #endif |
---|
167 | if (c->skipped_pairs>0){ |
---|
168 | ++(c->current_degree); |
---|
169 | c->skipped_pairs=0; |
---|
170 | c->continue_i=0; |
---|
171 | c->continue_j=0; |
---|
172 | return find_next_pair(c); |
---|
173 | } |
---|
174 | return false; |
---|
175 | } |
---|
176 | void move_forward_in_S(int old_pos, int new_pos,kStrategy strat){ |
---|
177 | poly p=strat->S[old_pos]; |
---|
178 | int ecart=strat->ecartS[old_pos]; |
---|
179 | long sev=strat->sevS[old_pos]; |
---|
180 | int s_2_r=strat->S_2_R[old_pos]; |
---|
181 | int length=strat->lenS[old_pos]; |
---|
182 | int i; |
---|
183 | for (i=old_pos; i>new_pos; i--) |
---|
184 | { |
---|
185 | strat->S[i] = strat->S[i-1]; |
---|
186 | strat->ecartS[i] = strat->ecartS[i-1]; |
---|
187 | strat->sevS[i] = strat->sevS[i-1]; |
---|
188 | strat->S_2_R[i] = strat->S_2_R[i-1]; |
---|
189 | } |
---|
190 | if (strat->lenS!=NULL) |
---|
191 | for (i=old_pos; i>new_pos; i--) |
---|
192 | strat->lenS[i] = strat->lenS[i-1]; |
---|
193 | |
---|
194 | strat->S[new_pos]=p; |
---|
195 | strat->ecartS[new_pos]=ecart; |
---|
196 | strat->sevS[new_pos]=sev; |
---|
197 | strat->S_2_R[new_pos]=s_2_r; |
---|
198 | strat->lenS[new_pos]=length; |
---|
199 | } |
---|
200 | void replace_pair(int & i, int & j, calc_dat* c) |
---|
201 | { |
---|
202 | c->soon_free=NULL; |
---|
203 | int curr_deg; |
---|
204 | poly lm=pOne(); |
---|
205 | |
---|
206 | pLcm(c->S->m[i], c->S->m[j], lm); |
---|
207 | pSetm(lm); |
---|
208 | int deciding_deg= pFDeg(lm); |
---|
209 | int* i_con =make_connections(i,j,lm,c); |
---|
210 | int z=0; |
---|
211 | |
---|
212 | for (int n=0;((n<c->n) && (i_con[n]>=0));n++){ |
---|
213 | if (i_con[n]==j){ |
---|
214 | // curr_deg=pFDeg(lm); |
---|
215 | // for(int z1=0;((z1<c->n) && (i_con[z1]>=0));z1++) |
---|
216 | // for (int z2=z1+1;((z2<c->n)&&(i_con[z2]>=0));z2++) |
---|
217 | // { |
---|
218 | // pLcm(c->S->m[i_con[z1]], c->S->m[i_con[z2]], lm); |
---|
219 | // pSetm(lm); |
---|
220 | // if (pFDeg(lm)==curr_deg) |
---|
221 | // now_t_rep(i_con[z1],i_con[z2],c); |
---|
222 | // } |
---|
223 | now_t_rep(i,j,c); |
---|
224 | omfree(i_con); |
---|
225 | p_Delete(&lm,c->r); |
---|
226 | return; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | int* j_con =make_connections(j,lm,c); |
---|
231 | i= i_con[0]; |
---|
232 | j=j_con[0]; |
---|
233 | if(c->n>1){ |
---|
234 | if (i_con[1]>=0) |
---|
235 | i=i_con[1]; |
---|
236 | else { |
---|
237 | if (j_con[1]>=0) |
---|
238 | j=j_con[1]; |
---|
239 | } |
---|
240 | } |
---|
241 | pLcm(c->S->m[i], c->S->m[j], lm); |
---|
242 | pSetm(lm); |
---|
243 | poly short_s; |
---|
244 | curr_deg=pFDeg(lm); |
---|
245 | int_pair_node* last=NULL; |
---|
246 | |
---|
247 | for (int n=0;((n<c->n) && (j_con[n]>=0));n++){ |
---|
248 | for (int m=0;((m<c->n) && (i_con[m]>=0));m++){ |
---|
249 | pLcm(c->S->m[i_con[m]], c->S->m[j_con[n]], lm); |
---|
250 | pSetm(lm); |
---|
251 | if (pFDeg(lm)>=deciding_deg) |
---|
252 | { |
---|
253 | int_pair_node* h= (int_pair_node*)omalloc(sizeof(int_pair_node)); |
---|
254 | if (last!=NULL) |
---|
255 | last->next=h; |
---|
256 | else |
---|
257 | c->soon_free=h; |
---|
258 | h->next=NULL; |
---|
259 | h->a=i_con[m]; |
---|
260 | h->b=j_con[n]; |
---|
261 | last=h; |
---|
262 | } |
---|
263 | // if ((comp_deg<curr_deg) |
---|
264 | // || |
---|
265 | // ((comp_deg==curr_deg) && |
---|
266 | short_s=ksCreateShortSpoly(c->S->m[i_con[m]],c->S->m[j_con[n]],c->r); |
---|
267 | if (short_s==NULL) { |
---|
268 | i=i_con[m]; |
---|
269 | j=j_con[n]; |
---|
270 | now_t_rep(i_con[m],j_con[n],c); |
---|
271 | p_Delete(&lm,c->r); |
---|
272 | omfree(i_con); |
---|
273 | omfree(j_con); |
---|
274 | |
---|
275 | return; |
---|
276 | |
---|
277 | } |
---|
278 | #ifdef QUICK_SPOLY_TEST |
---|
279 | |
---|
280 | for (int dz=0;dz<=c->n;dz++){ |
---|
281 | if (dz==c->n) { |
---|
282 | //have found not head reducing pair |
---|
283 | i=i_con[m]; |
---|
284 | j=j_con[n]; |
---|
285 | p_Delete(&short_s,c->r); |
---|
286 | p_Delete(&lm,c->r); |
---|
287 | omfree(i_con); |
---|
288 | omfree(j_con); |
---|
289 | |
---|
290 | return; |
---|
291 | } |
---|
292 | if (p_LmDivisibleBy(c->S->m[dz],short_s,c->r)) break; |
---|
293 | } |
---|
294 | #endif |
---|
295 | int comp_deg(pFDeg(short_s)); |
---|
296 | p_Delete(&short_s,c->r); |
---|
297 | if ((comp_deg<curr_deg) |
---|
298 | || |
---|
299 | ((comp_deg==curr_deg) && |
---|
300 | (c->misses[i]+c->misses[j] |
---|
301 | <= |
---|
302 | c->misses[i_con[m]]+c->misses[j_con[n]]))) |
---|
303 | // if ((comp_deg<curr_deg) |
---|
304 | // || |
---|
305 | // ((comp_deg==curr_deg) && |
---|
306 | // (c->lengths[i]+c->lengths[j] |
---|
307 | // <= |
---|
308 | // c->lengths[i_con[m]]+c->lengths[j_con[n]]))) |
---|
309 | |
---|
310 | { |
---|
311 | curr_deg=comp_deg; |
---|
312 | i=i_con[m]; |
---|
313 | j=j_con[n]; |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | p_Delete(&lm,c->r); |
---|
318 | omfree(i_con); |
---|
319 | omfree(j_con); |
---|
320 | return; |
---|
321 | } |
---|
322 | int* make_connections(int from, poly bound, calc_dat* c) |
---|
323 | { |
---|
324 | ideal I=c->S; |
---|
325 | int s=pFDeg(bound); |
---|
326 | int* cans=(int*) omalloc(c->n*sizeof(int)); |
---|
327 | int* connected=(int*) omalloc(c->n*sizeof(int)); |
---|
328 | int cans_length=0; |
---|
329 | connected[0]=from; |
---|
330 | int connected_length=1; |
---|
331 | long neg_bounds_short= ~p_GetShortExpVector(bound,c->r); |
---|
332 | for (int i=0;i<c->n;i++){ |
---|
333 | if (c->T_deg[i]>s) continue; |
---|
334 | if (i!=from){ |
---|
335 | if(p_LmShortDivisibleBy(I->m[i],c->short_Exps[i],bound,neg_bounds_short,c->r)){ |
---|
336 | cans[cans_length]=i; |
---|
337 | cans_length++; |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | int not_yet_found=cans_length; |
---|
342 | int con_checked=0; |
---|
343 | int pos; |
---|
344 | while((not_yet_found>0) && (con_checked<connected_length)){ |
---|
345 | pos=connected[con_checked]; |
---|
346 | for(int i=0;i<cans_length;i++){ |
---|
347 | if (cans[i]<0) continue; |
---|
348 | if (has_t_rep(pos,cans[i],c)){ |
---|
349 | |
---|
350 | connected[connected_length]=cans[i]; |
---|
351 | connected_length++; |
---|
352 | cans[i]=-1; |
---|
353 | --not_yet_found; |
---|
354 | |
---|
355 | } |
---|
356 | } |
---|
357 | con_checked++; |
---|
358 | } |
---|
359 | if (connected_length<c->n){ |
---|
360 | connected[connected_length]=-1; |
---|
361 | } |
---|
362 | omfree(cans); |
---|
363 | return connected; |
---|
364 | } |
---|
365 | int* make_connections(int from, int to, poly bound, calc_dat* c) |
---|
366 | { |
---|
367 | ideal I=c->S; |
---|
368 | int s=pFDeg(bound); |
---|
369 | int* cans=(int*) omalloc(c->n*sizeof(int)); |
---|
370 | int* connected=(int*) omalloc(c->n*sizeof(int)); |
---|
371 | cans[0]=to; |
---|
372 | int cans_length=1; |
---|
373 | connected[0]=from; |
---|
374 | int last_cans_pos=-1; |
---|
375 | int connected_length=1; |
---|
376 | long neg_bounds_short= ~p_GetShortExpVector(bound,c->r); |
---|
377 | // for (int i=0;i<c->n;i++){ |
---|
378 | // if (c->T_deg[i]>s) continue; |
---|
379 | // if (i!=from){ |
---|
380 | // if(p_LmShortDivisibleBy(I->m[i],c->short_Exps[i],bound,neg_bounds_short,c->r)){ |
---|
381 | // cans[cans_length]=i; |
---|
382 | // cans_length++; |
---|
383 | // } |
---|
384 | // } |
---|
385 | // } |
---|
386 | int not_yet_found=cans_length; |
---|
387 | int con_checked=0; |
---|
388 | int pos; |
---|
389 | bool can_find_more=true; |
---|
390 | while(((not_yet_found>0) && (con_checked<connected_length))||can_find_more){ |
---|
391 | if ((con_checked<connected_length)&& (not_yet_found>0)){ |
---|
392 | pos=connected[con_checked]; |
---|
393 | for(int i=0;i<cans_length;i++){ |
---|
394 | if (cans[i]<0) continue; |
---|
395 | if (has_t_rep(pos,cans[i],c)){ |
---|
396 | |
---|
397 | connected[connected_length]=cans[i]; |
---|
398 | connected_length++; |
---|
399 | cans[i]=-1; |
---|
400 | --not_yet_found; |
---|
401 | |
---|
402 | if (connected[connected_length-1]==to){ |
---|
403 | if (connected_length<c->n){ |
---|
404 | connected[connected_length]=-1; |
---|
405 | } |
---|
406 | |
---|
407 | omfree(cans); |
---|
408 | return connected; |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | con_checked++; |
---|
413 | } else { |
---|
414 | |
---|
415 | for(last_cans_pos++;last_cans_pos<=c->n;last_cans_pos++){ |
---|
416 | if (last_cans_pos==c->n){ |
---|
417 | if (connected_length<c->n){ |
---|
418 | connected[connected_length]=-1; |
---|
419 | } |
---|
420 | omfree(cans); |
---|
421 | return connected; |
---|
422 | } |
---|
423 | if ((last_cans_pos==from)||(last_cans_pos==to)) |
---|
424 | continue; |
---|
425 | if(p_LmShortDivisibleBy(I->m[last_cans_pos],c->short_Exps[last_cans_pos],bound,neg_bounds_short,c->r)){ |
---|
426 | cans[cans_length]=last_cans_pos; |
---|
427 | cans_length++; |
---|
428 | break; |
---|
429 | } |
---|
430 | } |
---|
431 | not_yet_found++; |
---|
432 | for (int i=0;i<con_checked;i++){ |
---|
433 | if (has_t_rep(connected[i],last_cans_pos,c)){ |
---|
434 | |
---|
435 | connected[connected_length]=last_cans_pos; |
---|
436 | connected_length++; |
---|
437 | cans[cans_length-1]=-1; |
---|
438 | |
---|
439 | --not_yet_found; |
---|
440 | if (connected[connected_length-1]==to){ |
---|
441 | if (connected_length<c->n){ |
---|
442 | connected[connected_length]=-1; |
---|
443 | } |
---|
444 | |
---|
445 | omfree(cans); |
---|
446 | return connected; |
---|
447 | } |
---|
448 | break; |
---|
449 | } |
---|
450 | |
---|
451 | } |
---|
452 | } |
---|
453 | } |
---|
454 | if (connected_length<c->n){ |
---|
455 | connected[connected_length]=-1; |
---|
456 | } |
---|
457 | |
---|
458 | omfree(cans); |
---|
459 | return connected; |
---|
460 | } |
---|
461 | #ifdef HEAD_BIN |
---|
462 | static inline poly p_MoveHead(poly p, omBin b) |
---|
463 | { |
---|
464 | poly np; |
---|
465 | omTypeAllocBin(poly, np, b); |
---|
466 | memmove(np, p, b->sizeW*sizeof(long)); |
---|
467 | omFreeBinAddr(p); |
---|
468 | return np; |
---|
469 | } |
---|
470 | #endif |
---|
471 | void initial_data(calc_dat* c){ |
---|
472 | void* h; |
---|
473 | int i,j; |
---|
474 | c->misses_series=0; |
---|
475 | c->soon_free=NULL; |
---|
476 | c->misses_counter=0; |
---|
477 | c->max_misses=0; |
---|
478 | c->normal_forms=0; |
---|
479 | c->current_degree=1; |
---|
480 | c->skipped_pairs=0; |
---|
481 | int n=c->S->idelems(); |
---|
482 | c->n=n; |
---|
483 | c->T_deg=(int*) omalloc(n*sizeof(int)); |
---|
484 | c->continue_i=0; |
---|
485 | c->continue_j=0; |
---|
486 | c->skipped_i=-1; |
---|
487 | #ifdef HEAD_BIN |
---|
488 | c->HeadBin=omGetSpecBin(POLYSIZE + (currRing->ExpL_Size)*sizeof(long)); |
---|
489 | #endif |
---|
490 | /* omUnGetSpecBin(&(c->HeadBin)); */ |
---|
491 | h=omalloc(n*sizeof(char*)); |
---|
492 | if (h!=NULL) |
---|
493 | c->states=(char**) h; |
---|
494 | else |
---|
495 | exit(1); |
---|
496 | c->misses=(int*) omalloc(n*sizeof(int)); |
---|
497 | c->deg=(int **) omalloc(n*sizeof(int*)); |
---|
498 | h=omalloc(n*sizeof(int)); |
---|
499 | if (h!=NULL) |
---|
500 | c->lengths=(int*) h; |
---|
501 | else |
---|
502 | exit(1); |
---|
503 | |
---|
504 | h=omalloc(n*sizeof(int)); |
---|
505 | if (h!=NULL) |
---|
506 | c->rep=(int*) h; |
---|
507 | else |
---|
508 | exit(1); |
---|
509 | c->short_Exps=(long*) omalloc(n*sizeof(long)); |
---|
510 | for (i=0;i<n;i++) |
---|
511 | { |
---|
512 | #ifdef HEAD_BIN |
---|
513 | c->S->m[i]=p_MoveHead(c->S->m[i],c->HeadBin); |
---|
514 | #endif |
---|
515 | c->T_deg[i]=pFDeg(c->S->m[i]); |
---|
516 | c->lengths[i]=pLength(c->S->m[i]); |
---|
517 | c->misses[i]=0; |
---|
518 | h=omalloc(i*sizeof(char)); |
---|
519 | if (h!=NULL){ |
---|
520 | c->states[i]=(char*) h; |
---|
521 | } else { |
---|
522 | exit(1); |
---|
523 | } |
---|
524 | c->deg[i]=(int*) omalloc(i*sizeof(int)); |
---|
525 | for (j=0;j<i;j++){ |
---|
526 | //check product criterion |
---|
527 | if (pHasNotCF(c->S->m[i],c->S->m[j])){ |
---|
528 | c->states[i][j]=HASTREP; |
---|
529 | } else { |
---|
530 | c->states[i][j]=UNCALCULATED; |
---|
531 | } |
---|
532 | c->deg[i][j]=pLcmDeg(c->S->m[i],c->S->m[j]); |
---|
533 | } |
---|
534 | if ((c->lengths[i]==1) && (c->lengths[j]==1)) |
---|
535 | c->states[i][j]=HASTREP; |
---|
536 | c->rep[i]=i; |
---|
537 | c->short_Exps[i]=p_GetShortExpVector(c->S->m[i],c->r); |
---|
538 | |
---|
539 | } |
---|
540 | |
---|
541 | |
---|
542 | c->strat=new skStrategy; |
---|
543 | c->strat->syzComp = 0; |
---|
544 | initBuchMoraCrit(c->strat); |
---|
545 | initBuchMoraPos(c->strat); |
---|
546 | c->strat->initEcart = initEcartBBA; |
---|
547 | c->strat->enterS = enterSBba; |
---|
548 | c->strat->sl = -1; |
---|
549 | /* initS(c->S,NULL,c->strat); */ |
---|
550 | /* intS start: */ |
---|
551 | i=((i+IDELEMS(c->S)+15)/16)*16; |
---|
552 | c->strat->ecartS=(intset)omAlloc(i*sizeof(int)); /*initec(i);*/ |
---|
553 | c->strat->sevS=(unsigned long*)omAlloc0(i*sizeof(unsigned long)); |
---|
554 | /*initsevS(i);*/ |
---|
555 | c->strat->S_2_R=(int*)omAlloc0(i*sizeof(int));/*initS_2_R(i);*/ |
---|
556 | c->strat->fromQ=NULL; |
---|
557 | c->strat->Shdl=idInit(i,1); |
---|
558 | c->strat->S=c->strat->Shdl->m; |
---|
559 | c->strat->lenS=(int*)omAlloc0(i*sizeof(int)); |
---|
560 | for (i=0; i<IDELEMS(c->S); i++) |
---|
561 | { |
---|
562 | int pos; |
---|
563 | assume (c->S->m[i]!=NULL); |
---|
564 | LObject h; |
---|
565 | h.p = c->S->m[i]; |
---|
566 | h.pNorm(); |
---|
567 | c->strat->initEcart(&h); |
---|
568 | assume(c->lengths[i]==pLength(h.p)); |
---|
569 | if (c->strat->sl==-1) pos=0; |
---|
570 | else pos = simple_posInS(c->strat,h.p,c->lengths[i]); |
---|
571 | h.sev = pGetShortExpVector(h.p); |
---|
572 | c->strat->enterS(h,pos,c->strat); |
---|
573 | c->strat->lenS[pos]=c->lengths[i]; |
---|
574 | } |
---|
575 | //c->strat->lenS=(int*)omAlloc0(IDELEMS(c->strat->Shdl)*sizeof(int)); |
---|
576 | //for (i=c->strat->sl;i>=0;i--) |
---|
577 | //{ |
---|
578 | // pNorm(c->strat->S[i]); |
---|
579 | // c->strat->lenS[i]=pLength(c->strat->S[i]); |
---|
580 | //} |
---|
581 | /* initS end */ |
---|
582 | } |
---|
583 | int simple_posInS (kStrategy strat, poly p,int len) |
---|
584 | { |
---|
585 | if(strat->sl==-1) return 0; |
---|
586 | polyset set=strat->S; |
---|
587 | intset setL=strat->lenS; |
---|
588 | int length=strat->sl; |
---|
589 | int i; |
---|
590 | int an = 0; |
---|
591 | int en= length; |
---|
592 | |
---|
593 | if ((len>setL[length]) |
---|
594 | || ((len==setL[length]) && (pLmCmp(set[length],p)== -1))) |
---|
595 | return length+1; |
---|
596 | |
---|
597 | loop |
---|
598 | { |
---|
599 | if (an >= en-1) |
---|
600 | { |
---|
601 | if ((len<setL[an]) |
---|
602 | || ((len==setL[length]) && (pLmCmp(set[an],p) == 1))) return an; |
---|
603 | return en; |
---|
604 | } |
---|
605 | i=(an+en) / 2; |
---|
606 | if ((len<setL[i]) |
---|
607 | || ((len==setL[i]) && (pLmCmp(set[i],p) == 1))) en=i; |
---|
608 | //else if ((len>setL[i]) |
---|
609 | //|| ((len==setL[i]) && (pLmCmp(set[i],p) == -1))) an=i; |
---|
610 | else an=i; |
---|
611 | } |
---|
612 | } |
---|
613 | /*2 |
---|
614 | *if the leading term of p |
---|
615 | *divides the leading term of some S[i] it will be canceled |
---|
616 | */ |
---|
617 | static inline void clearS (poly p, unsigned long p_sev,int l, int* at, int* k, |
---|
618 | kStrategy strat) |
---|
619 | { |
---|
620 | assume(p_sev == pGetShortExpVector(p)); |
---|
621 | if (!pLmShortDivisibleBy(p,p_sev, strat->S[*at], ~ strat->sevS[*at])) return; |
---|
622 | if (l>=strat->lenS[*at]) return; |
---|
623 | PrintS("!");mflush(); |
---|
624 | //pDelete(&strat->S[*at]); |
---|
625 | deleteInS((*at),strat); |
---|
626 | (*at)--; |
---|
627 | (*k)--; |
---|
628 | } |
---|
629 | void add_to_basis(poly h, int i_pos, int j_pos,calc_dat* c) |
---|
630 | { |
---|
631 | BOOLEAN R_found=FALSE; |
---|
632 | void* hp; |
---|
633 | PrintS("s"); |
---|
634 | ++(c->n); |
---|
635 | ++(c->S->ncols); |
---|
636 | int i,j; |
---|
637 | i=c->n-1; |
---|
638 | c->T_deg=(int*) omrealloc(c->T_deg,c->n*sizeof(int)); |
---|
639 | c->T_deg[i]=pFDeg(h); |
---|
640 | hp=omrealloc(c->rep, c->n *sizeof(int)); |
---|
641 | if (hp!=NULL){ |
---|
642 | c->rep=(int*) hp; |
---|
643 | } else { |
---|
644 | exit(1); |
---|
645 | } |
---|
646 | c->short_Exps=(long *) omrealloc(c->short_Exps ,c->n*sizeof(long)); |
---|
647 | |
---|
648 | hp=omrealloc(c->lengths, c->n *sizeof(int)); |
---|
649 | if (hp!=NULL){ |
---|
650 | c->lengths=(int*) hp; |
---|
651 | } else { |
---|
652 | exit(1); |
---|
653 | } |
---|
654 | c->misses=(int*) omrealloc(c->misses,c->n*sizeof(int)); |
---|
655 | c->misses[i]=0; |
---|
656 | c->lengths[i]=pLength(h); |
---|
657 | hp=omrealloc(c->states, c->n * sizeof(char*)); |
---|
658 | if (hp!=NULL){ |
---|
659 | c->states=(char**) hp; |
---|
660 | } else { |
---|
661 | exit(1); |
---|
662 | } |
---|
663 | c->deg=(int**) omrealloc(c->deg, c->n * sizeof(int*)); |
---|
664 | c->rep[i]=i; |
---|
665 | hp=omalloc(i*sizeof(char)); |
---|
666 | if (hp!=NULL){ |
---|
667 | c->states[i]=(char*) hp; |
---|
668 | } else { |
---|
669 | exit(1); |
---|
670 | } |
---|
671 | c->deg[i]=(int*) omalloc(i*sizeof(int)); |
---|
672 | hp=omrealloc(c->S->m,c->n*sizeof(poly)); |
---|
673 | if (hp!=NULL){ |
---|
674 | c->S->m=(poly*) hp; |
---|
675 | } else { |
---|
676 | exit(1); |
---|
677 | } |
---|
678 | c->S->m[i]=h; |
---|
679 | c->short_Exps[i]=p_GetShortExpVector(h,c->r); |
---|
680 | for (j=0;j<i;j++){ |
---|
681 | c->deg[i][j]=pLcmDeg(c->S->m[i],c->S->m[j]); |
---|
682 | if (c->rep[j]==j){ |
---|
683 | //check product criterion |
---|
684 | |
---|
685 | c->states[i][j]=UNCALCULATED; |
---|
686 | |
---|
687 | |
---|
688 | //lies I[i] under I[j] ? |
---|
689 | if(p_LmShortDivisibleBy(c->S->m[i],c->short_Exps[i],c->S->m[j],~(c->short_Exps[j]),c->r)){ |
---|
690 | c->rep[j]=i; |
---|
691 | PrintS("R"); R_found=TRUE; |
---|
692 | |
---|
693 | c->misses[i_pos]--; |
---|
694 | c->misses[j_pos]--; |
---|
695 | for(int z=0;z<j;z++){ |
---|
696 | if (c->states[j][z]==UNCALCULATED){ |
---|
697 | c->states[j][z]=UNIMPORTANT; |
---|
698 | } |
---|
699 | } |
---|
700 | for(int z=j+1;z<i;z++){ |
---|
701 | if (c->states[z][j]==UNCALCULATED){ |
---|
702 | c->states[z][j]=UNIMPORTANT; |
---|
703 | } |
---|
704 | } |
---|
705 | } |
---|
706 | } |
---|
707 | else { |
---|
708 | c->states[i][j]=UNIMPORTANT; |
---|
709 | } |
---|
710 | if ((c->lengths[i]==1) && (c->lengths[j]==1)) |
---|
711 | c->states[i][j]=HASTREP; |
---|
712 | if (pHasNotCF(c->S->m[i],c->S->m[j])) |
---|
713 | c->states[i][j]=HASTREP; |
---|
714 | |
---|
715 | } |
---|
716 | if (c->skipped_i>0){ |
---|
717 | c->continue_i=c->skipped_i; |
---|
718 | c->continue_j=0; |
---|
719 | c->skipped_i=-1; |
---|
720 | } |
---|
721 | |
---|
722 | //i=posInS(c->strat,c->strat->sl,h,0 /*ecart*/); |
---|
723 | i=simple_posInS(c->strat,h,c->lengths[c->n-1]); |
---|
724 | |
---|
725 | LObject P; memset(&P,0,sizeof(P)); |
---|
726 | P.tailRing=c->r; |
---|
727 | P.p=h; /*p_Copy(h,c->r);*/ |
---|
728 | P.FDeg=pFDeg(P.p,c->r); |
---|
729 | if (!rField_is_Zp(c->r)) pCleardenom(P.p); |
---|
730 | enterT(P,c->strat,-1); |
---|
731 | c->strat->enterS(P,i,c->strat); |
---|
732 | c->strat->lenS[i]=/*pLength(c->strat->S[i]);*/ c->lengths[c->n-1]; |
---|
733 | pNorm(c->strat->S[i]); |
---|
734 | if (0 /*R_found && (i<c->strat->sl)*/) |
---|
735 | { |
---|
736 | i++; |
---|
737 | unsigned long h_sev = pGetShortExpVector(h); |
---|
738 | loop |
---|
739 | { |
---|
740 | if (i>c->strat->sl) break; |
---|
741 | clearS(h,h_sev,c->lengths[c->n-1], &i,&(c->strat->sl),c->strat); |
---|
742 | i++; |
---|
743 | } |
---|
744 | } |
---|
745 | if (c->lengths[c->n-1]==1) |
---|
746 | shorten_tails(c,c->S->m[c->n-1]); |
---|
747 | //you should really update c->lengths, c->strat->lenS, and the oder of polys in strat if you sort after lengths |
---|
748 | |
---|
749 | //for(i=c->strat->sl; i>0;i--) |
---|
750 | // if(c->strat->lenS[i]<c->strat->lenS[i-1]) printf("fehler bei %d\n",i); |
---|
751 | } |
---|
752 | #if 0 |
---|
753 | static poly redNF (poly h,kStrategy strat) |
---|
754 | { |
---|
755 | int j = 0; |
---|
756 | int z = 3; |
---|
757 | unsigned long not_sev; |
---|
758 | |
---|
759 | if (0 > strat->sl) |
---|
760 | { |
---|
761 | return h; |
---|
762 | } |
---|
763 | not_sev = ~ pGetShortExpVector(h); |
---|
764 | loop |
---|
765 | { |
---|
766 | if (pLmShortDivisibleBy(strat->S[j], strat->sevS[j], h, not_sev)) |
---|
767 | { |
---|
768 | //if (strat->interpt) test_int_std(strat->kIdeal); |
---|
769 | /*- compute the s-polynomial -*/ |
---|
770 | #ifdef KDEBUG |
---|
771 | if (TEST_OPT_DEBUG) |
---|
772 | { |
---|
773 | PrintS("red:"); |
---|
774 | wrp(h); |
---|
775 | PrintS(" with "); |
---|
776 | wrp(strat->S[j]); |
---|
777 | } |
---|
778 | #endif |
---|
779 | h = ksOldSpolyRed(strat->S[j],h,strat->kNoether); |
---|
780 | #ifdef KDEBUG |
---|
781 | if (TEST_OPT_DEBUG) |
---|
782 | { |
---|
783 | PrintS("\nto:"); |
---|
784 | wrp(h); |
---|
785 | PrintLn(); |
---|
786 | } |
---|
787 | #endif |
---|
788 | if (h == NULL) return NULL; |
---|
789 | z++; |
---|
790 | if (z>=10) |
---|
791 | { |
---|
792 | z=0; |
---|
793 | pNormalize(h); |
---|
794 | } |
---|
795 | /*- try to reduce the s-polynomial -*/ |
---|
796 | j = 0; |
---|
797 | not_sev = ~ pGetShortExpVector(h); |
---|
798 | } |
---|
799 | else |
---|
800 | { |
---|
801 | if (j >= strat->sl) return h; |
---|
802 | j++; |
---|
803 | } |
---|
804 | } |
---|
805 | } |
---|
806 | #else |
---|
807 | static poly redNF (poly h,kStrategy strat) |
---|
808 | { |
---|
809 | if (h==NULL) return NULL; |
---|
810 | int j; |
---|
811 | |
---|
812 | if (0 > strat->sl) |
---|
813 | { |
---|
814 | return h; |
---|
815 | } |
---|
816 | LObject P(h); |
---|
817 | P.SetShortExpVector(); |
---|
818 | P.bucket = kBucketCreate(currRing); |
---|
819 | kBucketInit(P.bucket,P.p,pLength(P.p)); |
---|
820 | //int max_pos=simple_posInS(strat,P.p); |
---|
821 | loop |
---|
822 | { |
---|
823 | j=kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P); |
---|
824 | if (j>=0) |
---|
825 | { |
---|
826 | nNormalize(pGetCoeff(P.p)); |
---|
827 | #ifdef KDEBUG |
---|
828 | if (TEST_OPT_DEBUG) |
---|
829 | { |
---|
830 | PrintS("red:"); |
---|
831 | wrp(h); |
---|
832 | PrintS(" with "); |
---|
833 | wrp(strat->S[j]); |
---|
834 | } |
---|
835 | #endif |
---|
836 | number coef=kBucketPolyRed(P.bucket,strat->S[j], |
---|
837 | strat->lenS[j]/*pLength(strat->S[j])*/, |
---|
838 | strat->kNoether); |
---|
839 | nDelete(&coef); |
---|
840 | h = kBucketGetLm(P.bucket); |
---|
841 | if (h==NULL) return NULL; |
---|
842 | P.p=h; |
---|
843 | P.t_p=NULL; |
---|
844 | P.SetShortExpVector(); |
---|
845 | #ifdef KDEBUG |
---|
846 | if (TEST_OPT_DEBUG) |
---|
847 | { |
---|
848 | PrintS("\nto:"); |
---|
849 | wrp(h); |
---|
850 | PrintLn(); |
---|
851 | } |
---|
852 | #endif |
---|
853 | } |
---|
854 | else |
---|
855 | { |
---|
856 | P.p=kBucketClear(P.bucket); |
---|
857 | kBucketDestroy(&P.bucket); |
---|
858 | pNormalize(P.p); |
---|
859 | return P.p; |
---|
860 | } |
---|
861 | } |
---|
862 | } |
---|
863 | #endif |
---|
864 | |
---|
865 | void do_this_spoly_stuff(int i,int j,calc_dat* c){ |
---|
866 | poly f=c->S->m[i]; |
---|
867 | poly g=c->S->m[j]; |
---|
868 | poly h=ksOldCreateSpoly(f, g, NULL, c->r); |
---|
869 | poly hr=NULL; |
---|
870 | #ifdef FULLREDUCTIONS |
---|
871 | // if (h!=NULL) hr=kNF2(c->S,NULL,h,c->strat, 0); |
---|
872 | if (h!=NULL) |
---|
873 | { |
---|
874 | hr=redNF(h,c->strat); |
---|
875 | if (hr!=NULL) |
---|
876 | hr = redtailBba(hr,c->strat->sl,c->strat); |
---|
877 | } |
---|
878 | #else |
---|
879 | //if (h!=NULL) hr=kNF2(c->S,NULL,h,c->strat, 1); |
---|
880 | if (h!=NULL) |
---|
881 | hr=redNF(h,c->strat); |
---|
882 | #endif |
---|
883 | c->normal_forms++; |
---|
884 | if (hr==NULL) |
---|
885 | { |
---|
886 | PrintS("-"); |
---|
887 | c->misses_counter++; |
---|
888 | c->misses[i]++; |
---|
889 | c->misses[j]++; |
---|
890 | c->misses_series++; |
---|
891 | } else { |
---|
892 | c->misses_series=0; |
---|
893 | #ifdef HEAD_BIN |
---|
894 | hr=p_MoveHead(hr,c->HeadBin); |
---|
895 | #endif |
---|
896 | add_to_basis(hr, i,j,c); |
---|
897 | } |
---|
898 | } |
---|
899 | |
---|
900 | ideal t_rep_gb(ring r,ideal arg_I){ |
---|
901 | ideal I_temp=kInterRed(arg_I); |
---|
902 | ideal I=idCompactify(I_temp); |
---|
903 | idDelete(&I_temp); |
---|
904 | calc_dat* c=(calc_dat*) omalloc(sizeof(calc_dat)); |
---|
905 | c->r=currRing; |
---|
906 | c->S=I; |
---|
907 | initial_data(c); |
---|
908 | while (find_next_pair(c)){ |
---|
909 | int i,j; |
---|
910 | int z; |
---|
911 | i=c->found_i; |
---|
912 | j=c->found_j; |
---|
913 | replace_pair(i,j,c); |
---|
914 | if (!(c->states[max(i,j)][min(i,j)]==HASTREP)){ |
---|
915 | do_this_spoly_stuff(i,j,c); |
---|
916 | } |
---|
917 | //else |
---|
918 | // PrintS("f"); |
---|
919 | |
---|
920 | now_t_rep(i,j,c); |
---|
921 | now_t_rep(c->found_i,c->found_j,c); |
---|
922 | #ifdef RANDOM_WALK |
---|
923 | c->continue_i=my_rand(c->n-1)+1; |
---|
924 | c->continue_j=my_rand(c->continue_i); |
---|
925 | #endif |
---|
926 | int_pair_node* h=c->soon_free; |
---|
927 | while(h!=NULL) |
---|
928 | { |
---|
929 | int_pair_node* s=h; |
---|
930 | now_t_rep(h->a,h->b,c); |
---|
931 | |
---|
932 | h=h->next; |
---|
933 | omfree(s); |
---|
934 | } |
---|
935 | |
---|
936 | |
---|
937 | } |
---|
938 | omfree(c->rep); |
---|
939 | for(int z=0;z<c->n;z++){ |
---|
940 | omfree(c->states[z]); |
---|
941 | } |
---|
942 | omfree(c->states); |
---|
943 | omfree(c->lengths); |
---|
944 | printf("calculated %d NFs\n",c->normal_forms); |
---|
945 | omfree(c); |
---|
946 | return(I); |
---|
947 | } |
---|
948 | void now_t_rep(int arg_i, int arg_j, calc_dat* c){ |
---|
949 | int i,j; |
---|
950 | if (arg_i==arg_j){ |
---|
951 | return; |
---|
952 | } |
---|
953 | if (arg_i>arg_j){ |
---|
954 | i=arg_j; |
---|
955 | j=arg_i; |
---|
956 | } else { |
---|
957 | i=arg_i; |
---|
958 | j=arg_j; |
---|
959 | } |
---|
960 | c->states[j][i]=HASTREP; |
---|
961 | } |
---|
962 | bool has_t_rep(const int & arg_i, const int & arg_j, calc_dat* state){ |
---|
963 | |
---|
964 | if (arg_i==arg_j) |
---|
965 | { |
---|
966 | return (true); |
---|
967 | } |
---|
968 | if (arg_i>arg_j) |
---|
969 | { |
---|
970 | return (state->states[arg_i][arg_j]==HASTREP); |
---|
971 | } else |
---|
972 | { |
---|
973 | return (state->states[arg_j][arg_i]==HASTREP); |
---|
974 | } |
---|
975 | } |
---|
976 | int pLcmDeg(poly a, poly b) |
---|
977 | { |
---|
978 | int i; |
---|
979 | int n=0; |
---|
980 | for (i=pVariables; i; i--) |
---|
981 | { |
---|
982 | n+=max( pGetExp(a,i), pGetExp(b,i)); |
---|
983 | } |
---|
984 | return n; |
---|
985 | |
---|
986 | } |
---|
987 | int pMinDeg3(poly f){ |
---|
988 | if (f==NULL){ |
---|
989 | return(-1); |
---|
990 | |
---|
991 | } |
---|
992 | |
---|
993 | poly h=f->next; |
---|
994 | int n=pFDeg(h); |
---|
995 | int i=0; |
---|
996 | while((i<2)){ |
---|
997 | if (h==NULL) |
---|
998 | return(n); |
---|
999 | h=h->next; |
---|
1000 | if (h!=NULL){ |
---|
1001 | n=min(n,pFDeg(h)); |
---|
1002 | } |
---|
1003 | i++; |
---|
1004 | } |
---|
1005 | |
---|
1006 | return n; |
---|
1007 | } |
---|
1008 | |
---|
1009 | |
---|
1010 | void shorten_tails(calc_dat* c, poly monom){ |
---|
1011 | |
---|
1012 | for(int i=0;i<c->n;i++){ |
---|
1013 | //enter tail |
---|
1014 | if (c->rep[i]!=i) continue; |
---|
1015 | if (c->S->m[i]==NULL) continue; |
---|
1016 | poly tail=c->S->m[i]->next; |
---|
1017 | poly prev=c->S->m[i]; |
---|
1018 | bool did_something=false; |
---|
1019 | while((tail!=NULL)&& (pLmCmp(tail, monom)>=0)){ |
---|
1020 | if (p_LmDivisibleBy(monom,tail,c->r)){ |
---|
1021 | |
---|
1022 | did_something=true; |
---|
1023 | prev->next=tail->next; |
---|
1024 | tail->next=NULL; |
---|
1025 | p_Delete(& tail,c->r); |
---|
1026 | tail=prev; |
---|
1027 | //PrintS("Shortened"); |
---|
1028 | c->lengths[i]--; |
---|
1029 | |
---|
1030 | } |
---|
1031 | |
---|
1032 | prev=tail; |
---|
1033 | tail=tail->next; |
---|
1034 | } |
---|
1035 | if (did_something){ |
---|
1036 | int new_pos=simple_posInS(c->strat,c->S->m[i],c->lengths[i]); |
---|
1037 | int old_pos=-1; |
---|
1038 | //assume new_pos<old_pos |
---|
1039 | for (int z=new_pos;z<=c->strat->sl;z++) |
---|
1040 | { |
---|
1041 | if (c->strat->S[z]==c->S->m[i]) |
---|
1042 | { |
---|
1043 | old_pos=z; |
---|
1044 | break; |
---|
1045 | } |
---|
1046 | } |
---|
1047 | assume(old_pos>=0); |
---|
1048 | assume(pLength(c->strat->S[old_pos])==c->lengths[i]); |
---|
1049 | c->strat->lenS[old_pos]=c->lengths[i]; |
---|
1050 | |
---|
1051 | move_forward_in_S(old_pos,new_pos,c->strat); |
---|
1052 | if (c->lengths[i]==1){ |
---|
1053 | int j; |
---|
1054 | for ( j=0;j<i;j++){ |
---|
1055 | if (c->lengths[j]==1) |
---|
1056 | c->states[i][j]=HASTREP; |
---|
1057 | } |
---|
1058 | for ( j=i+1;j<c->n;j++){ |
---|
1059 | if (c->lengths[j]==1) |
---|
1060 | c->states[j][i]=HASTREP; |
---|
1061 | } |
---|
1062 | } |
---|
1063 | } |
---|
1064 | } |
---|
1065 | } |
---|