1 | |
---|
2 | |
---|
3 | #include "kernel/mod2.h" |
---|
4 | #include "omalloc/omalloc.h" |
---|
5 | |
---|
6 | #include "coeffs/numbers.h" |
---|
7 | |
---|
8 | #include "polys/monomials/ring.h" |
---|
9 | #include "polys/monomials/p_polys.h" |
---|
10 | #include "polys/kbuckets.h" |
---|
11 | |
---|
12 | #include "kernel/ideals.h" |
---|
13 | #include "kernel/polys.h" |
---|
14 | #include "kernel/GBEngine/kutil.h" |
---|
15 | #include "kernel/GBEngine/janet.h" |
---|
16 | |
---|
17 | |
---|
18 | #include <stdio.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <string.h> |
---|
21 | #include <stdarg.h> |
---|
22 | #include <time.h> |
---|
23 | |
---|
24 | #if (defined(__CYGWIN__)) |
---|
25 | #include <ctype.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | |
---|
29 | //------GLOBALS------- |
---|
30 | static int /*m_s,v_s,vectorized,VarN1,*/offset; |
---|
31 | static jList *T,*Q; |
---|
32 | static TreeM *G; |
---|
33 | // static Poly *phD; |
---|
34 | static NodeM *FreeNodes; |
---|
35 | static int degree_compatible; |
---|
36 | static int (*ListGreatMove)(jList *,jList *,poly); |
---|
37 | static int Mask[8]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1}; |
---|
38 | |
---|
39 | //#define DebugPrint |
---|
40 | |
---|
41 | //#define pow_(x) pTotaldegree((x)) |
---|
42 | //#define pow_(x) p_Deg((x,currRing)) |
---|
43 | pFDegProc jDeg; |
---|
44 | #define pow_(x) jDeg((x),currRing) |
---|
45 | |
---|
46 | #if 0 |
---|
47 | void Debug() |
---|
48 | { |
---|
49 | LCI it=T->root; |
---|
50 | |
---|
51 | PrintS("T==================================\n"); |
---|
52 | while (it) |
---|
53 | { |
---|
54 | pWrite(it->info->root); |
---|
55 | it=it->next; |
---|
56 | } |
---|
57 | |
---|
58 | it=Q->root; |
---|
59 | |
---|
60 | PrintS("Q==================================\n"); |
---|
61 | while (it) |
---|
62 | { |
---|
63 | if (it->info->root) pWrite(it->info->root); |
---|
64 | else |
---|
65 | { |
---|
66 | Print("%d.........",it->info->prolonged); |
---|
67 | pWrite(it->info->history); |
---|
68 | } |
---|
69 | it=it->next; |
---|
70 | } |
---|
71 | PrintS("===================================\n"); |
---|
72 | } |
---|
73 | #endif |
---|
74 | |
---|
75 | int ReducePolyLead(Poly *x,Poly *y) |
---|
76 | { |
---|
77 | if (!x->root || !y->root) |
---|
78 | return 0; |
---|
79 | |
---|
80 | /* poly b1=pMDivide(x->root,y->root); |
---|
81 | |
---|
82 | number gcd=n_Gcd(pGetCoeff(x->root),pGetCoeff(y->root),currRing->cf); |
---|
83 | |
---|
84 | number a1=nDiv(pGetCoeff(y->root),gcd); |
---|
85 | pGetCoeff(b1)=nDiv(pGetCoeff(x->root),gcd); |
---|
86 | |
---|
87 | x->root=pMult_nn(x->root,a1); |
---|
88 | nDelete(&a1); |
---|
89 | |
---|
90 | x->root=pMinus_mm_Mult_qq(x->root,b1,y->root); |
---|
91 | |
---|
92 | pDelete(&b1); |
---|
93 | */ |
---|
94 | #if 1 |
---|
95 | if (x->root_b==NULL) |
---|
96 | { |
---|
97 | if (x->root_l<=0) x->root_l=pLength(x->root); |
---|
98 | x->root_b=kBucketCreate(currRing); |
---|
99 | kBucketInit(x->root_b,x->root,x->root_l); |
---|
100 | } |
---|
101 | number coef; |
---|
102 | if (y->root_l<=0) y->root_l=pLength(y->root); |
---|
103 | coef=kBucketPolyRed(x->root_b,y->root,y->root_l,NULL); |
---|
104 | nDelete(&coef); |
---|
105 | x->root=kBucketGetLm(x->root_b); |
---|
106 | if (x->root==NULL) |
---|
107 | { |
---|
108 | kBucketDestroy(&x->root_b); |
---|
109 | x->root_b=NULL; |
---|
110 | x->root_l=0; |
---|
111 | } |
---|
112 | #else |
---|
113 | x->root=ksOldSpolyRed(y->root,x->root,NULL); |
---|
114 | #endif |
---|
115 | // if (x->root) p_Content(x->root,currRing); |
---|
116 | // if (x->root) pSimpleContent(x->root,5); |
---|
117 | |
---|
118 | return 1; |
---|
119 | } |
---|
120 | |
---|
121 | int ReducePoly(Poly *x,poly from,Poly *y) |
---|
122 | { |
---|
123 | if (!x->root || !y->root) |
---|
124 | return 0; |
---|
125 | |
---|
126 | /* poly b1=pMDivide(from,y->root); |
---|
127 | |
---|
128 | number gcd=n_Gcd(pGetCoeff(from),pGetCoeff(y->root),currRing->cf); |
---|
129 | |
---|
130 | number a1=nDiv(pGetCoeff(y->root),gcd); |
---|
131 | pGetCoeff(b1)=nDiv(pGetCoeff(from),gcd); |
---|
132 | |
---|
133 | x->root=pMult_nn(x->root,a1); |
---|
134 | nDelete(&a1);*/ |
---|
135 | |
---|
136 | // x->root=pMinus_mm_Mult_qq(x->root,b1,y->root); |
---|
137 | // pDelete(&b1); |
---|
138 | |
---|
139 | ksOldSpolyTail(y->root,x->root,from,NULL,currRing); |
---|
140 | y->root_l=0; |
---|
141 | |
---|
142 | return 1; |
---|
143 | } |
---|
144 | |
---|
145 | void PNF(Poly *p, TreeM *F) |
---|
146 | { |
---|
147 | if (p->root==NULL) return; |
---|
148 | |
---|
149 | Poly *f; |
---|
150 | BOOLEAN done=FALSE; |
---|
151 | poly temp=p->root; |
---|
152 | |
---|
153 | // if (TEST_OPT_PROT) { PrintS("r"); mflush(); } |
---|
154 | int count=0; |
---|
155 | poly pp=p->root; |
---|
156 | int old_size=nSize(pGetCoeff(pp)); |
---|
157 | p->root_l=0; |
---|
158 | while(temp->next) |
---|
159 | { |
---|
160 | f=is_div_(F,temp->next); |
---|
161 | if (f) |
---|
162 | { |
---|
163 | if (ReducePoly(p,temp,f)) //temp->next |
---|
164 | { |
---|
165 | count++; |
---|
166 | //if (TEST_OPT_PROT) { PrintS("-"); mflush(); } |
---|
167 | if ((f!=NULL) |
---|
168 | && (count>20) |
---|
169 | && (nSize(pGetCoeff(pp))>old_size) |
---|
170 | ) |
---|
171 | { |
---|
172 | //pSimpleContent(pp,2); |
---|
173 | p_Content(pp,currRing); |
---|
174 | count=0; |
---|
175 | // old_size=nSize(pGetCoeff(pp)); |
---|
176 | } |
---|
177 | } |
---|
178 | done=TRUE; |
---|
179 | } |
---|
180 | else |
---|
181 | temp=temp->next; |
---|
182 | } |
---|
183 | |
---|
184 | if (done) p_Content(p->root,currRing); |
---|
185 | //if (done) pSimpleContent(p->root,-1); |
---|
186 | pTest(p->root); |
---|
187 | } |
---|
188 | |
---|
189 | void NFL(Poly *p, TreeM *F) |
---|
190 | { |
---|
191 | Poly *f; |
---|
192 | // int g1,f1,gg; |
---|
193 | |
---|
194 | if ((f=is_div_(F,p->lead))==NULL) return; |
---|
195 | |
---|
196 | int pX=pow_(p->lead); |
---|
197 | int phX=pow_(p->history); |
---|
198 | |
---|
199 | if (pX!=phX) |
---|
200 | { |
---|
201 | int phF=pow_(f->history); |
---|
202 | if (pX >= (phX+phF)) |
---|
203 | { |
---|
204 | pDelete(&p->root); |
---|
205 | //p->root=NULL; |
---|
206 | return; |
---|
207 | } |
---|
208 | |
---|
209 | /* poly p2=pInit(); |
---|
210 | pLcm(p->history,f->history,p2); |
---|
211 | pSetm(p2); |
---|
212 | |
---|
213 | if (pLmCmp(p->root,p2) > 0) |
---|
214 | { |
---|
215 | pLmDelete(&p2); |
---|
216 | pDelete(&p->root); |
---|
217 | //p->root=NULL; |
---|
218 | return; |
---|
219 | } |
---|
220 | |
---|
221 | pLmDelete(&p2); |
---|
222 | */ |
---|
223 | /* for(int i=0, gg=0 ; i<currRing->N;i++) |
---|
224 | if ((g1=pGetExp(p->history,i+1)) > (f1=pGetExp(f->history,i+1))) |
---|
225 | gg+=g1; |
---|
226 | else gg+=f1; |
---|
227 | |
---|
228 | if (pX > gg) |
---|
229 | { |
---|
230 | pDelete(&p->root); |
---|
231 | //x->root=NULL; |
---|
232 | return; |
---|
233 | } |
---|
234 | */ |
---|
235 | int pF=pow_(f->lead); |
---|
236 | |
---|
237 | if ((pX == pF) && (pF == phF)) |
---|
238 | { |
---|
239 | pLmFree(&f->history); |
---|
240 | f->history=p_Copy_noCheck(p->history,currRing); /* cf of p->history is NULL */ |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | //if (TEST_OPT_PROT) { PrintS("R"); mflush(); } |
---|
245 | int /*old_size, */count; |
---|
246 | count=0; |
---|
247 | while(f && p->root) |
---|
248 | { |
---|
249 | // PrintS("R"); |
---|
250 | // if (TEST_OPT_PROT) { PrintS("R"); mflush(); } |
---|
251 | #if 0 |
---|
252 | old_size=nSize(pGetCoeff(p->root)); |
---|
253 | #endif |
---|
254 | if (ReducePolyLead(p,f) == 0) break; |
---|
255 | if (p->root!=NULL) |
---|
256 | { |
---|
257 | count++; |
---|
258 | #if 0 |
---|
259 | if ((count>4) && (3<nSize(pGetCoeff(p->root))) |
---|
260 | && (nSize(pGetCoeff(p->root))>old_size)) |
---|
261 | { |
---|
262 | pSimpleContent(p->root,old_size); |
---|
263 | count=0; |
---|
264 | } |
---|
265 | #else |
---|
266 | if (count>50) |
---|
267 | { |
---|
268 | kBucketClear(p->root_b,&p->root,&p->root_l); |
---|
269 | p_SimpleContent(p->root,2,currRing); |
---|
270 | kBucketInit(p->root_b,p->root,p->root_l); |
---|
271 | count=0; |
---|
272 | //PrintS("."); |
---|
273 | } |
---|
274 | #endif |
---|
275 | f=is_div_(F,p->root); |
---|
276 | } |
---|
277 | } |
---|
278 | #if 1 |
---|
279 | if (p->root_b!=NULL) |
---|
280 | { |
---|
281 | kBucketClear(p->root_b,&p->root,&p->root_l); |
---|
282 | kBucketDestroy(&p->root_b); |
---|
283 | p->root_b=NULL; |
---|
284 | } |
---|
285 | #endif |
---|
286 | |
---|
287 | if (!p->root) |
---|
288 | return; |
---|
289 | |
---|
290 | InitHistory(p); |
---|
291 | InitProl(p); |
---|
292 | InitLead(p); |
---|
293 | p->changed=1; |
---|
294 | |
---|
295 | p_Content(p->root,currRing); |
---|
296 | //pSimpleContent(p->root,-1); |
---|
297 | pTest(p->root); |
---|
298 | } |
---|
299 | |
---|
300 | int ValidatePoly(Poly *x, TreeM */*F*/) |
---|
301 | { |
---|
302 | Poly /*f,*/*g; |
---|
303 | // int g1,f1; |
---|
304 | |
---|
305 | if (x->root) return 1; |
---|
306 | |
---|
307 | g=is_present(T,x->history); //it's a prolongation - do we have a parent ? |
---|
308 | |
---|
309 | if (!g) return 0; //if not - kill him ! |
---|
310 | |
---|
311 | poly lmX=pMDivide(x->lead,g->root); |
---|
312 | pSetCoeff0(lmX,nInit(1)); |
---|
313 | |
---|
314 | /* if ((f=is_div_(F,lmX)) != NULL) |
---|
315 | { |
---|
316 | int pX=pow_(lmX); |
---|
317 | int phX=pow_(x->history); |
---|
318 | |
---|
319 | if (pX!=phX) |
---|
320 | { |
---|
321 | int phF=pow_(f->history); |
---|
322 | if (pX >= (phX+phF)) |
---|
323 | { |
---|
324 | pLmDelete(&lmX); |
---|
325 | //x->root=NULL; |
---|
326 | return 0; |
---|
327 | } |
---|
328 | |
---|
329 | for(int i=0, gg=0 ; i<currRing->N;i++) |
---|
330 | if ((g1=pGetExp(x->history,i+1)) > (f1=pGetExp(f->history,i+1))) |
---|
331 | gg+=g1; |
---|
332 | else |
---|
333 | gg+=f1; |
---|
334 | |
---|
335 | if (pX > gg) |
---|
336 | { |
---|
337 | pLmDelete(&lmX); |
---|
338 | return 0; |
---|
339 | } |
---|
340 | int pF=pow_(f->root); |
---|
341 | |
---|
342 | if ((pX == pF) && (pF == phF)) |
---|
343 | f->history=x->history; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | pLmDelete(&lmX); |
---|
348 | |
---|
349 | */ |
---|
350 | x->root=pCopy(g->root); |
---|
351 | x->root_l=g->root_l; |
---|
352 | |
---|
353 | x->root=pMult(x->root,lmX); |
---|
354 | |
---|
355 | pTest(x->root); |
---|
356 | |
---|
357 | x->prolonged=-1; |
---|
358 | |
---|
359 | return 1; |
---|
360 | } |
---|
361 | |
---|
362 | Poly *NewPoly(poly p) |
---|
363 | { |
---|
364 | Poly *beg=(Poly *)GCM(sizeof(Poly)); |
---|
365 | |
---|
366 | beg->root=p;//(p == NULL ? pInit() : p); |
---|
367 | beg->root_b=NULL; |
---|
368 | beg->root_l=0; |
---|
369 | beg->history=NULL;//pInit(); |
---|
370 | beg->lead=NULL; |
---|
371 | beg->mult=(char *)GCMA(sizeof(char)*2*offset); |
---|
372 | |
---|
373 | for (int i=0; i < currRing->N; i++) |
---|
374 | { |
---|
375 | ClearMult(beg,i); |
---|
376 | ClearProl(beg,i); |
---|
377 | }; |
---|
378 | |
---|
379 | beg->prolonged=-1; |
---|
380 | |
---|
381 | return beg; |
---|
382 | } |
---|
383 | |
---|
384 | void DestroyPoly(Poly *x) |
---|
385 | { |
---|
386 | pDelete(&x->root); |
---|
387 | pLmFree(&x->history); |
---|
388 | if (x->lead) pDelete(&x->lead); |
---|
389 | GCF(x->mult); |
---|
390 | GCF(x); |
---|
391 | } |
---|
392 | |
---|
393 | void ControlProlong(Poly *x) |
---|
394 | { |
---|
395 | for (int i = 0; i< offset; i++) |
---|
396 | { |
---|
397 | (x->mult+offset)[i]&=~((x->mult)[i]); |
---|
398 | // if (!GetMult(x,i) && !GetProl(x,i)) |
---|
399 | // ProlVar(x,i); |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | void InitHistory(Poly *p) |
---|
404 | { |
---|
405 | if (p->history) pLmFree(&p->history); |
---|
406 | p->history=pLmInit(p->root); |
---|
407 | p->changed=0; |
---|
408 | } |
---|
409 | |
---|
410 | void InitLead(Poly *p) |
---|
411 | { |
---|
412 | if (p->lead) pLmDelete(&p->lead); |
---|
413 | p->lead=pLmInit(p->root); |
---|
414 | p->prolonged=-1; |
---|
415 | } |
---|
416 | |
---|
417 | void InitProl(Poly *p) |
---|
418 | { |
---|
419 | memset(p->mult+offset,0,sizeof(char)*offset); |
---|
420 | } |
---|
421 | |
---|
422 | int GetMult(Poly *x,int i) |
---|
423 | { |
---|
424 | return x->mult[i/8] & Mask[i%8]; |
---|
425 | } |
---|
426 | |
---|
427 | void SetMult(Poly *x,int i) |
---|
428 | { |
---|
429 | x->mult[i/8] |= Mask[i%8]; |
---|
430 | } |
---|
431 | |
---|
432 | void ClearMult(Poly *x,int i) |
---|
433 | { |
---|
434 | x->mult[i/8] &= ~Mask[i%8]; |
---|
435 | } |
---|
436 | |
---|
437 | int GetProl(Poly *x, int i) |
---|
438 | { |
---|
439 | return (x->mult+offset)[i/8] & Mask[i%8]; |
---|
440 | } |
---|
441 | |
---|
442 | void SetProl(Poly *x, int i) |
---|
443 | { |
---|
444 | (x->mult+offset)[i/8] |= Mask[i%8]; |
---|
445 | } |
---|
446 | |
---|
447 | void ClearProl(Poly *x, int i) |
---|
448 | { |
---|
449 | (x->mult+offset)[i/8] &= ~Mask[i%8]; |
---|
450 | } |
---|
451 | |
---|
452 | int LengthCompare(poly p1,poly p2) |
---|
453 | { |
---|
454 | do |
---|
455 | { |
---|
456 | if (p1 == NULL) return 1; |
---|
457 | if (p2 == NULL) return 0; |
---|
458 | pIter(p1); |
---|
459 | pIter(p2); |
---|
460 | }while(p1 && p2); |
---|
461 | return 1; |
---|
462 | } |
---|
463 | |
---|
464 | int ProlCompare(Poly *item1, Poly *item2) |
---|
465 | { |
---|
466 | switch(pLmCmp(item1->lead,item2->lead)) |
---|
467 | { |
---|
468 | case -1: |
---|
469 | return 1; |
---|
470 | |
---|
471 | case 1: |
---|
472 | return 0; |
---|
473 | |
---|
474 | default: |
---|
475 | if ((item1->root_l<=0)||(item2->root_l<=0)) |
---|
476 | return LengthCompare(item1->root,item2->root); |
---|
477 | return item1->root_l<=item2->root_l; |
---|
478 | } |
---|
479 | } |
---|
480 | |
---|
481 | void ProlVar(Poly *temp,int i) |
---|
482 | { |
---|
483 | Poly *Pr; |
---|
484 | |
---|
485 | if (!GetProl(temp,i) && !GetMult(temp,i)) |
---|
486 | { |
---|
487 | Pr=NewPoly(); |
---|
488 | SetProl(temp,i); |
---|
489 | |
---|
490 | Pr->prolonged=i; |
---|
491 | Pr->history=pLmInit(temp->history); |
---|
492 | Pr->lead=pLmInit(temp->lead); |
---|
493 | pIncrExp(Pr->lead,i+1); |
---|
494 | pSetm(Pr->lead); |
---|
495 | InitProl(temp); |
---|
496 | |
---|
497 | Pr->changed=0; |
---|
498 | // pTest(Pr->root); |
---|
499 | InsertInCount(Q,Pr); |
---|
500 | } |
---|
501 | } |
---|
502 | |
---|
503 | void DestroyListNode(ListNode *x) |
---|
504 | { |
---|
505 | DestroyPoly(x->info); |
---|
506 | GCF(x); |
---|
507 | } |
---|
508 | |
---|
509 | ListNode* CreateListNode(Poly *x) |
---|
510 | { |
---|
511 | ListNode* ret=(ListNode *)GCM(sizeof(ListNode)); |
---|
512 | ret->info=x; |
---|
513 | ret->next=NULL; |
---|
514 | return ret; |
---|
515 | } |
---|
516 | |
---|
517 | |
---|
518 | Poly *FindMinList(jList *L) |
---|
519 | { |
---|
520 | LI min=&(L->root); |
---|
521 | LI l; |
---|
522 | LCI xl; |
---|
523 | Poly *x; |
---|
524 | |
---|
525 | if (degree_compatible) |
---|
526 | { |
---|
527 | while ((*min) && ((*min)->info->root == NULL)) |
---|
528 | min=&((*min)->next); |
---|
529 | } |
---|
530 | |
---|
531 | if (!(*min)) return NULL; |
---|
532 | |
---|
533 | l=&((*min)->next); |
---|
534 | |
---|
535 | while (*l) |
---|
536 | { |
---|
537 | if ((*l)->info->root != NULL) |
---|
538 | { |
---|
539 | if (ProlCompare((*l)->info,(*min)->info)) |
---|
540 | min=l; |
---|
541 | } |
---|
542 | |
---|
543 | l=&((*l)->next); |
---|
544 | } |
---|
545 | x=(*min)->info; |
---|
546 | xl=*min; |
---|
547 | *min=(*min)->next; |
---|
548 | GCF(xl); |
---|
549 | |
---|
550 | return x; |
---|
551 | } |
---|
552 | |
---|
553 | void InsertInList(jList *x,Poly *y) |
---|
554 | { |
---|
555 | ListNode *ins; |
---|
556 | LI ix=&(x->root); |
---|
557 | |
---|
558 | while (*ix) |
---|
559 | { |
---|
560 | if (pLmCmp(y->lead,(*ix)->info->lead) == -1) |
---|
561 | ix=(ListNode **)&((*ix)->next); |
---|
562 | else |
---|
563 | break; |
---|
564 | } |
---|
565 | |
---|
566 | ins=CreateListNode(y); |
---|
567 | ins->next=(ListNode *)(*ix); |
---|
568 | *ix=ins; |
---|
569 | return; |
---|
570 | } |
---|
571 | |
---|
572 | void InsertInCount(jList *x,Poly *y) |
---|
573 | { |
---|
574 | ListNode *ins; |
---|
575 | LI ix=&(x->root); |
---|
576 | |
---|
577 | ins=CreateListNode(y); |
---|
578 | ins->next=(ListNode *)(*ix); |
---|
579 | *ix=ins; |
---|
580 | return; |
---|
581 | } |
---|
582 | |
---|
583 | int ListGreatMoveOrder(jList *A,jList *B,poly x) |
---|
584 | { |
---|
585 | LCI y=A->root; |
---|
586 | |
---|
587 | if (!y || pLmCmp(y->info->lead,x) < 0) return 0; |
---|
588 | |
---|
589 | while(y && pLmCmp(y->info->lead,x) >= 0) |
---|
590 | { |
---|
591 | InsertInCount(B,y->info); |
---|
592 | A->root=y->next; |
---|
593 | GCF(y); |
---|
594 | y=A->root; |
---|
595 | } |
---|
596 | |
---|
597 | return 1; |
---|
598 | } |
---|
599 | |
---|
600 | int ListGreatMoveDegree(jList *A,jList *B,poly x) |
---|
601 | { |
---|
602 | LCI y=A->root; |
---|
603 | int pow_x=pow_(x); |
---|
604 | |
---|
605 | if (!y || pow_(y->info->lead) <= pow_x) return 0; |
---|
606 | |
---|
607 | while(y && pow_(y->info->lead) > pow_x) |
---|
608 | { |
---|
609 | InsertInCount(B,y->info); |
---|
610 | A->root=y->next; |
---|
611 | GCF(y); |
---|
612 | y=A->root; |
---|
613 | } |
---|
614 | |
---|
615 | return 1; |
---|
616 | } |
---|
617 | |
---|
618 | int CountList(jList *Q) |
---|
619 | { |
---|
620 | int i=0; |
---|
621 | LCI y=Q->root; |
---|
622 | |
---|
623 | while(y) |
---|
624 | { |
---|
625 | i++; |
---|
626 | y=y->next; |
---|
627 | } |
---|
628 | |
---|
629 | return i; |
---|
630 | } |
---|
631 | |
---|
632 | void NFListQ() |
---|
633 | { |
---|
634 | LCI ll; |
---|
635 | int p,p1; |
---|
636 | LI l; |
---|
637 | |
---|
638 | do |
---|
639 | { |
---|
640 | if (!Q->root) break; |
---|
641 | |
---|
642 | ll=Q->root; |
---|
643 | |
---|
644 | p=pow_(Q->root->info->lead); |
---|
645 | |
---|
646 | while (ll) |
---|
647 | { |
---|
648 | int ploc=pow_(ll->info->lead); |
---|
649 | if (ploc < p) p=ploc; |
---|
650 | ll=ll->next; |
---|
651 | } |
---|
652 | |
---|
653 | p1=1; |
---|
654 | |
---|
655 | l=&(Q->root); |
---|
656 | |
---|
657 | while (*l) |
---|
658 | { |
---|
659 | // PrintS("*"); |
---|
660 | int ploc=pow_((*l)->info->lead); |
---|
661 | |
---|
662 | if (ploc == p) |
---|
663 | { |
---|
664 | if (!ValidatePoly((*l)->info,G)) |
---|
665 | { |
---|
666 | ll=(*l); |
---|
667 | *l=(*l)->next; |
---|
668 | DestroyListNode(ll); |
---|
669 | continue; |
---|
670 | }; |
---|
671 | |
---|
672 | (*l)->info->changed=0; |
---|
673 | // PrintS("!"); |
---|
674 | NFL((*l)->info,G); |
---|
675 | // PrintS("$"); |
---|
676 | if (!(*l)->info->root) |
---|
677 | { |
---|
678 | ll=(*l); |
---|
679 | *l=(*l)->next; |
---|
680 | DestroyListNode(ll); |
---|
681 | continue; |
---|
682 | }; |
---|
683 | p1=0; |
---|
684 | } |
---|
685 | |
---|
686 | l=&((*l)->next); |
---|
687 | } |
---|
688 | }while(p1); |
---|
689 | // PrintLn(); |
---|
690 | } |
---|
691 | |
---|
692 | |
---|
693 | void ForEachPNF(jList *x,int i) |
---|
694 | { |
---|
695 | LCI y=x->root; |
---|
696 | |
---|
697 | while(y) |
---|
698 | { |
---|
699 | if (pow_(y->info->root) == i) PNF(y->info,G); |
---|
700 | y=y->next; |
---|
701 | } |
---|
702 | } |
---|
703 | |
---|
704 | void ForEachControlProlong(jList *x) |
---|
705 | { |
---|
706 | LCI y=x->root; |
---|
707 | |
---|
708 | while(y) |
---|
709 | { |
---|
710 | ControlProlong(y->info); |
---|
711 | y=y->next; |
---|
712 | } |
---|
713 | } |
---|
714 | |
---|
715 | void DestroyList(jList *x) |
---|
716 | { |
---|
717 | LCI y=x->root,z; |
---|
718 | |
---|
719 | while(y) |
---|
720 | { |
---|
721 | z=y->next; |
---|
722 | DestroyPoly(y->info); |
---|
723 | GCF(y); |
---|
724 | y=z; |
---|
725 | } |
---|
726 | |
---|
727 | GCF(x); |
---|
728 | } |
---|
729 | |
---|
730 | Poly* is_present(jList *F,poly x) |
---|
731 | { |
---|
732 | LCI iF=F->root; |
---|
733 | while(iF) |
---|
734 | if (pLmCmp(iF->info->root,x) == 0) |
---|
735 | return iF->info; |
---|
736 | else iF=iF->next; |
---|
737 | |
---|
738 | return NULL; |
---|
739 | } |
---|
740 | |
---|
741 | int GB_length() |
---|
742 | { |
---|
743 | LCI iT=T->root; |
---|
744 | int l=0; |
---|
745 | |
---|
746 | while(iT) |
---|
747 | { |
---|
748 | if (pow_(iT->info->lead) == pow_(iT->info->history)) |
---|
749 | ++l; |
---|
750 | iT=iT->next; |
---|
751 | } |
---|
752 | |
---|
753 | return l; |
---|
754 | } |
---|
755 | |
---|
756 | static Poly *temp_l; |
---|
757 | |
---|
758 | NodeM* create() |
---|
759 | { |
---|
760 | NodeM *y; |
---|
761 | |
---|
762 | if (FreeNodes == NULL) |
---|
763 | { |
---|
764 | y=(NodeM *)GCM(sizeof(NodeM)); |
---|
765 | } |
---|
766 | else |
---|
767 | { |
---|
768 | y=FreeNodes; |
---|
769 | FreeNodes=FreeNodes->left; |
---|
770 | } |
---|
771 | |
---|
772 | y->left=y->right=NULL; |
---|
773 | y->ended=NULL; |
---|
774 | return y; |
---|
775 | } |
---|
776 | |
---|
777 | void DestroyFreeNodes() |
---|
778 | { |
---|
779 | NodeM *y; |
---|
780 | |
---|
781 | while((y=FreeNodes)!=NULL) |
---|
782 | { |
---|
783 | FreeNodes=FreeNodes->left; |
---|
784 | GCF(y); |
---|
785 | } |
---|
786 | } |
---|
787 | |
---|
788 | #if 0 |
---|
789 | static void go_right(NodeM *current,poly_function disp) |
---|
790 | { |
---|
791 | if (current) |
---|
792 | { |
---|
793 | go_right(current->left,disp); |
---|
794 | if (current->ended) disp(current->ended); |
---|
795 | go_right(current->right,disp); |
---|
796 | } |
---|
797 | } |
---|
798 | |
---|
799 | void ForEach(TreeM *t,poly_function disp) |
---|
800 | { |
---|
801 | go_right(t->root,disp); |
---|
802 | } |
---|
803 | #endif |
---|
804 | |
---|
805 | void DestroyTree(NodeM *G) |
---|
806 | { |
---|
807 | if (G) |
---|
808 | { |
---|
809 | DestroyTree(G->left); |
---|
810 | DestroyTree(G->right); |
---|
811 | G->left=FreeNodes; |
---|
812 | FreeNodes=G; |
---|
813 | } |
---|
814 | } |
---|
815 | |
---|
816 | void Define(TreeM **G) |
---|
817 | { |
---|
818 | *G=(TreeM *)GCM(sizeof(TreeM)); |
---|
819 | (*G)->root=create(); |
---|
820 | } |
---|
821 | |
---|
822 | int sp_div(poly m1,poly m2,int from) |
---|
823 | { |
---|
824 | |
---|
825 | if (pow_(m2) == 0 && pow_(m1)) return 0; |
---|
826 | |
---|
827 | for(int k=from; k < currRing->N; k++) |
---|
828 | if (pGetExp(m1,k+1) < pGetExp(m2,k+1)) return 0; |
---|
829 | |
---|
830 | return 1; |
---|
831 | } |
---|
832 | |
---|
833 | void div_l(poly item, NodeM *x,int from) |
---|
834 | { |
---|
835 | if (x && !temp_l) |
---|
836 | { |
---|
837 | div_l(item,x->left,from); |
---|
838 | if ((x->ended) && sp_div(item,x->ended->root,from)) |
---|
839 | { |
---|
840 | temp_l=x->ended; |
---|
841 | return; |
---|
842 | }; |
---|
843 | div_l(item,x->right,from); |
---|
844 | } |
---|
845 | } |
---|
846 | |
---|
847 | Poly* is_div_upper(poly item, NodeM *x,int from) |
---|
848 | { |
---|
849 | temp_l=NULL; |
---|
850 | div_l(item,x,from); |
---|
851 | return temp_l; |
---|
852 | } |
---|
853 | |
---|
854 | Poly* is_div_(TreeM *tree, poly item) |
---|
855 | { |
---|
856 | int power_tmp,i,i_con=currRing->N-1; |
---|
857 | NodeM *curr=tree->root; |
---|
858 | |
---|
859 | if (!curr) return NULL; |
---|
860 | if (pow_(item) == 0) return NULL; |
---|
861 | |
---|
862 | for ( ; i_con>=0 && !pGetExp(item,i_con+1) ; i_con--) |
---|
863 | ; |
---|
864 | |
---|
865 | for (i=0; i <= i_con ; i++) |
---|
866 | { |
---|
867 | power_tmp=pGetExp(item,i+1); |
---|
868 | |
---|
869 | while (power_tmp) |
---|
870 | { |
---|
871 | if (curr->ended) return curr->ended; |
---|
872 | |
---|
873 | if (!curr->left) |
---|
874 | { |
---|
875 | if (curr->right) |
---|
876 | return is_div_upper(item,curr->right,i); //?????? |
---|
877 | return NULL; |
---|
878 | } |
---|
879 | |
---|
880 | curr=curr->left; |
---|
881 | power_tmp--; |
---|
882 | } |
---|
883 | |
---|
884 | if (curr->ended) return curr->ended; |
---|
885 | |
---|
886 | if (!curr->right) return NULL; |
---|
887 | |
---|
888 | curr=curr->right; |
---|
889 | } |
---|
890 | |
---|
891 | if (curr->ended) return curr->ended; |
---|
892 | else return NULL; |
---|
893 | } |
---|
894 | |
---|
895 | static void ClearMultiplicative(NodeM *xx,int i) |
---|
896 | { |
---|
897 | if (!xx) return; |
---|
898 | |
---|
899 | while (xx->left) |
---|
900 | { |
---|
901 | ClearMultiplicative(xx->right, i); |
---|
902 | xx = xx->left; |
---|
903 | } |
---|
904 | if ((xx->ended) && (GetMult(xx->ended,i))) |
---|
905 | { |
---|
906 | ClearMult(xx->ended,i); |
---|
907 | ProlVar(xx->ended,i); |
---|
908 | } |
---|
909 | else |
---|
910 | ClearMultiplicative(xx->right,i); |
---|
911 | } |
---|
912 | //====================================================== |
---|
913 | void insert_(TreeM **tree, Poly *item) |
---|
914 | { |
---|
915 | int power_tmp,i,i_con=currRing->N-1; |
---|
916 | NodeM *curr=(*tree)->root; |
---|
917 | |
---|
918 | for ( ; (i_con>=0) && !pGetExp(item->root,i_con+1) ; i_con--) |
---|
919 | SetMult(item,i_con); |
---|
920 | |
---|
921 | for (i = 0; i<= i_con; i++) |
---|
922 | //<= |
---|
923 | { |
---|
924 | power_tmp=pGetExp(item->root,i+1); |
---|
925 | |
---|
926 | ClearMult(item,i); |
---|
927 | |
---|
928 | while (power_tmp) |
---|
929 | { |
---|
930 | if (!curr->left) |
---|
931 | { |
---|
932 | SetMult(item,i); |
---|
933 | ClearMultiplicative(curr->right,i); |
---|
934 | curr->left=create(); |
---|
935 | }; |
---|
936 | curr=curr->left; |
---|
937 | power_tmp--; |
---|
938 | }; |
---|
939 | |
---|
940 | if (i<i_con) |
---|
941 | { |
---|
942 | if (!curr->left) SetMult(item,i); |
---|
943 | if (!curr->right) curr->right=create(); |
---|
944 | curr=curr->right; |
---|
945 | |
---|
946 | ProlVar(item,i); |
---|
947 | } |
---|
948 | } |
---|
949 | |
---|
950 | curr->ended=item; |
---|
951 | } |
---|
952 | |
---|
953 | void Initialization(char *Ord) |
---|
954 | { |
---|
955 | offset=(currRing->N % 8 == 0) ? (currRing->N/8)*8 : (currRing->N/8+1)*8; |
---|
956 | if (strstr(Ord,"dp\0") || strstr(Ord,"Dp\0")) |
---|
957 | { |
---|
958 | degree_compatible=1; |
---|
959 | jDeg=p_Deg; |
---|
960 | ListGreatMove=ListGreatMoveDegree; |
---|
961 | } |
---|
962 | else |
---|
963 | { |
---|
964 | degree_compatible=0; |
---|
965 | jDeg=p_Totaldegree; |
---|
966 | ListGreatMove=ListGreatMoveOrder; |
---|
967 | } |
---|
968 | |
---|
969 | Define(&G); |
---|
970 | } |
---|
971 | |
---|
972 | static Poly *h/*,*f*/; |
---|
973 | |
---|
974 | #if 0 |
---|
975 | void insert_in_G(Poly *x) |
---|
976 | { |
---|
977 | insert_(&G,x); |
---|
978 | } |
---|
979 | #endif |
---|
980 | |
---|
981 | void T2G(); |
---|
982 | |
---|
983 | #if 0 |
---|
984 | void Q2TG() |
---|
985 | { |
---|
986 | LCI t; |
---|
987 | Poly *x; |
---|
988 | |
---|
989 | while (Q->root) |
---|
990 | { |
---|
991 | t=Q->root; |
---|
992 | x=t->info; |
---|
993 | insert_(&G,x); |
---|
994 | InsertInList(T,x); |
---|
995 | Q->root=t->next; |
---|
996 | GCF(t); |
---|
997 | } |
---|
998 | } |
---|
999 | #endif |
---|
1000 | |
---|
1001 | int ComputeBasis(jList *_lT,jList *_lQ) |
---|
1002 | { |
---|
1003 | // int gb_l,i,ret_value=1; |
---|
1004 | |
---|
1005 | T=_lT; Q=_lQ; |
---|
1006 | |
---|
1007 | // Debug(); |
---|
1008 | |
---|
1009 | while((h=FindMinList(Q))!=NULL) |
---|
1010 | { |
---|
1011 | // PrintS("New element\n"); |
---|
1012 | // Debug(); |
---|
1013 | |
---|
1014 | if (!degree_compatible) |
---|
1015 | { |
---|
1016 | if (!ValidatePoly(h,G)) |
---|
1017 | { |
---|
1018 | DestroyPoly(h); |
---|
1019 | continue; |
---|
1020 | } |
---|
1021 | |
---|
1022 | h->changed=0; |
---|
1023 | |
---|
1024 | NFL(h,G); |
---|
1025 | |
---|
1026 | if (!h->root) |
---|
1027 | { |
---|
1028 | DestroyPoly(h); |
---|
1029 | continue; |
---|
1030 | } |
---|
1031 | } |
---|
1032 | |
---|
1033 | if (h->root) |
---|
1034 | { |
---|
1035 | if (pIsConstant(h->root)) |
---|
1036 | { |
---|
1037 | WarnS("Constant in basis\n"); |
---|
1038 | return 0; |
---|
1039 | } |
---|
1040 | |
---|
1041 | if (h->changed && ListGreatMove(T,Q,h->root)) |
---|
1042 | { |
---|
1043 | // PrintS("<-\n"); |
---|
1044 | DestroyTree(G->root); |
---|
1045 | G->root=create(); |
---|
1046 | T2G(); |
---|
1047 | } |
---|
1048 | } |
---|
1049 | |
---|
1050 | // PrintS("PNF\n"); |
---|
1051 | PNF(h,G); |
---|
1052 | // Print("{%d}\n",pow_(h->root)); |
---|
1053 | insert_(&G,h); |
---|
1054 | InsertInList(T,h); |
---|
1055 | |
---|
1056 | // PrintS("For each PNF\n"); |
---|
1057 | if (degree_compatible) |
---|
1058 | ForEachPNF(T,pow_(h->root)); |
---|
1059 | |
---|
1060 | // PrintS("Control of prolongations\n"); |
---|
1061 | if (h->changed) |
---|
1062 | ForEachControlProlong(T); |
---|
1063 | else |
---|
1064 | ControlProlong(h); |
---|
1065 | |
---|
1066 | // Debug(); |
---|
1067 | |
---|
1068 | // PrintS("NFListQ\n"); |
---|
1069 | if (degree_compatible) |
---|
1070 | NFListQ(); |
---|
1071 | //Debug(); |
---|
1072 | } |
---|
1073 | |
---|
1074 | // gb_l=GB_length(); |
---|
1075 | |
---|
1076 | Print("Length of Janet basis: %d\n",CountList(T)); |
---|
1077 | // Print("Length of Groebner basis: %d\n",gb_l); |
---|
1078 | |
---|
1079 | DestroyTree(G->root); |
---|
1080 | GCF(G); |
---|
1081 | DestroyFreeNodes(); |
---|
1082 | |
---|
1083 | return 1; |
---|
1084 | } |
---|
1085 | |
---|
1086 | void T2G() |
---|
1087 | { |
---|
1088 | LCI i=T->root; |
---|
1089 | while (i) |
---|
1090 | { |
---|
1091 | insert_(&G,i->info); |
---|
1092 | i=i->next; |
---|
1093 | } |
---|
1094 | } |
---|