source: git/kernel/f5gb.cc @ f42fe73

spielwiese
Last change on this file since f42fe73 was f42fe73, checked in by Christian Eder, 14 years ago
bug fix: tempPoly could be zero during reduciton process in procedure findReducers() git-svn-id: file:///usr/local/Singular/svn/trunk@11865 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 61.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: f5gb interface
6*/
7
8#include "mod2.h"
9#ifdef HAVE_F5
10#include <unistd.h>
11#include "structs.h"
12#include "kutil.h"
13#include "omalloc.h"
14#include "polys.h"
15#include "p_polys.h"
16#include "p_Procs.h"
17#include "ideals.h"
18#include "febase.h"
19#include "kstd1.h"
20#include "khstd.h"
21#include "kbuckets.h"
22#include "weight.h"
23#include "intvec.h"
24#include "pInline1.h"
25#include "f5gb.h"
26#include "f5data.h"
27#include "f5lists.h"
28#include "timer.h"
29int reductionsToZero    =   0;
30int reductionTime       =   0;
31int spolsTime           =   0;
32int highestDegree       =   0;
33/*
34====================================================================
35sorting ideals by decreasing total degree "left" and "right" are the
36pointer of the first and last polynomial in the considered ideal
37====================================================================
38*/
39void qsortDegree(poly* left, poly* right) {
40    poly* ptr1 = left;
41    poly* ptr2 = right;
42    poly p1,p2;
43    p2 = *(left + (right - left >> 1));
44    do {
45            while(pTotaldegree(*ptr1, currRing) < pTotaldegree(p2, currRing)) {
46                    ptr1++;
47            } 
48            while(pTotaldegree(*ptr2, currRing) > pTotaldegree(p2,currRing)) {
49                    ptr2--;
50            }
51            if(ptr1 > ptr2) {
52                    break;
53            }
54            p1    = *ptr1;
55            *ptr1 = *ptr2;
56            *ptr2 = p1;
57    } while(++ptr1 <= --ptr2);
58
59    if(left < ptr2) {
60            qsortDegree(left,ptr2);
61    }
62    if(ptr1 < right) {
63            qsortDegree(ptr1,right);
64    }
65}
66
67/*!
68 * ======================================================================
69 * builds the sum of the entries of the exponent vectors, i.e. the degree
70 * of the corresponding monomial
71 * ======================================================================
72*/
73long sumVector(int* v, int k) {
74    int i;
75    long sum =  0;
76    for(i=1; i<=k; i++) {
77        Print("%d\n",v[i]);
78        Print("%ld\n",sum);
79        sum =   sum + v[i];
80    }
81    return sum;
82}
83
84/*!
85==========================================================================
86compares monomials, i.e. divisibility tests for criterion 1 and criterion 2
87==========================================================================
88*/
89bool compareMonomials(int* m1, int** m2, int numberOfRules, int k) {
90    int i,j;
91    long sumM1  =   sumVector(m1,k);
92    //int k   =   sizeof(m1) / sizeof(int);
93    for(i=0; i<numberOfRules; i++) {
94        if(sumVector(m2[i],k) <= sumM1) {
95            for(j=1; j<=k; j++) {
96                if(m1[j]>m2[i][j]) {
97                    return true;
98                }
99            }   
100        }
101    }
102    return false;
103}
104
105/*
106==================================================
107computes incrementally gbs of subsets of the input
108gb{f_m} -> gb{f_m,f_(m-1)} -> gb{f_m,...,f_1}
109==================================================
110*/
111LList* F5inc(int i, poly f_i, LList* gPrev, ideal gbPrev, poly ONE, LTagList* lTag, RList* rules, RTagList* rTag) {
112    //Print("in f5inc\n");           
113    //pWrite(rules->getFirst()->getRuleTerm());
114    int j;
115    //Print("%p\n",gPrev->getFirst());
116    //pWrite(gPrev->getFirst()->getPoly());
117    poly tempNF =   kNF(gbPrev,currQuotient,f_i);
118    f_i         =   tempNF;
119    //gPrev->insert(ONE,i,f_i);
120    gPrev->insert(ONE,gPrev->getLength()+1,f_i);
121    // tag the first element in gPrev of the current index for findReductor()
122    lTag->setFirstCurrentIdx(gPrev->getLast());
123    //Print("1st gPrev: ");
124    //pWrite(gPrev->getFirst()->getPoly());
125    //Print("2nd gPrev: ");
126    //pWrite(gPrev->getFirst()->getNext()->getPoly());
127    //pWrite(gPrev->getFirst()->getNext()->getPoly());   
128    CList* critPairs        =   new CList();
129    CNode* critPairsMinDeg  =   new CNode();   
130    // computation of critical pairs with checking of criterion 1 and criterion 2 and saving them
131    // in the list critPairs
132    criticalPair(gPrev, critPairs, lTag, rTag, rules);
133    static LList* sPolyList        =   new LList();
134    //sPolyList->print();
135    // labeled polynomials which have passed reduction() and have to be added to list gPrev
136    static LList* completed        =   new LList();
137    // the reduced labeled polynomials which are returned from subalgorithm reduction()
138    static LList* reducedLPolys     =   new LList();
139    // while there are critical pairs to be further checked and deleted/computed
140    while(NULL != critPairs->getFirst()) { 
141        // critPairs->getMinDeg() deletes the first elements of minimal degree from
142        // critPairs, thus the while loop is not infinite.
143        critPairsMinDeg =   critPairs->getMinDeg();
144        // adds all to be reduced S-polynomials in the list sPolyList and adds
145        // the corresponding rules to the list rules
146        // NOTE: inside there is a second check of criterion 2 if new rules are
147        // added
148        //int timer4  =   initTimer();
149        //startTimer();
150        //critPairs->print();
151        computeSPols(critPairsMinDeg,rTag,rules,sPolyList);
152        //timer4  =   getTimer();
153        //Print("SPOLS TIMER: %d\n",timer4);
154        //spolsTime  =   spolsTime  +   timer4;
155        // DEBUG STUFF FOR SPOLYLIST
156        LNode* temp     =   sPolyList->getFirst();
157        //while(NULL != temp && NULL != temp->getLPoly()) {
158            //Print("Spolylist element: ");
159            //pWrite(temp->getPoly());
160            //temp    =   temp->getNext();
161        //}
162        // reduction process of new S-polynomials and also adds new critical pairs to critPairs
163        //int timer3  =   initTimer();
164        //startTimer();
165        //sPolyList->print();
166        //reduction(sPolyList, critPairs, gPrev, rules, lTag, rTag, gbPrev);
167        newReduction(sPolyList, critPairs, gPrev, rules, lTag, rTag, gbPrev);
168        //timer3      =  getTimer();
169        //reductionTime = reductionTime + timer3;
170        //Print("REDUCTION TIMER: %d\n",timer3);
171        // DEBUG STUFF FOR GPREV
172        //temp    =   gPrev->getFirst();
173        //int number  =   1;
174        //Print("\n\n");
175        //while(NULL != temp) {
176        //    Print("%d.  ",number);
177        //    pWrite(temp->getPoly());
178        //    temp    =   temp->getNext();
179        //    number++;
180        //    Print("\n");
181        //}
182        //sleep(5);
183   
184    }
185    //Print("REDUCTION DONE\n");
186    //Print("%p\n",rules->getFirst());
187    //Print("%p\n",rTag->getFirst());
188    //if(rules->getFirst() != rTag->getFirst()) {
189        //Print("+++++++++++++++++++++++++++++++++++++NEW RULES+++++++++++++++++++++++++++++++++++++\n");
190        //rTag->insert(rules->getFirst());
191    //}
192    //else {
193        //Print("+++++++++++++++++++++++++++++++++++NO NEW RULES++++++++++++++++++++++++++++++++++++\n");
194    //}
195    lTag->insert(lTag->getFirstCurrentIdx());
196    //Print("INDEX: %d\n",tempTag->getIndex());
197    //pWrite(tempTag->getPoly());
198    //Print("COMPLETED FIRST IN F5INC: \n");
199    //Print("1st gPrev: ");
200    //pWrite(gPrev->getFirst()->getPoly());
201    //Print("2nd gPrev: ");
202    //pWrite(gPrev->getFirst()->getNext()->getPoly());
203    //Print("3rd gPrev: ");
204    //pWrite(gPrev->getFirst()->getNext()->getNext()->getPoly());
205    //delete sPolyList;
206    //critPairs->print();
207    delete critPairs;
208    //Print("IN F5INC\n");
209    /*
210    Print("\n\n\nRULES: \n");
211        RNode* tempR    =   rules->getFirst();
212        Print("%p\n",tempR);
213        int t   = 1;
214        while(NULL != tempR) {
215            Print("ADDRESS OF %d RNODE: %p\n",t,tempR);
216            Print("ADDRESS OF RULE: %p\n",tempR->getRule());
217            pWrite(tempR->getRuleTerm());
218            Print("ADDRESS OF TERM: %p\n",tempR->getRuleTerm());
219            Print("%d\n\n",tempR->getRuleIndex());
220            tempR   =   tempR->getNext();
221            t++;
222        }
223    */
224    //gPrev->print();
225    //Print("COMPLETE REDUCTION TIME UNTIL NOW: %d\n",reductionTime);
226    //Print("COMPLETE SPOLS TIME UNTIL NOW:     %d\n",spolsTime);
227    return gPrev;
228}
229
230
231
232/*
233================================================================
234computes a list of critical pairs for the next reduction process
235first element in gPrev is always the newest element which must
236build critical pairs with all other elements in gPrev
237================================================================
238*/
239inline void criticalPair(LList* gPrev, CList* critPairs, LTagList* lTag, RTagList* rTag, RList* rules) {
240    //Print("IN CRITPAIRS\n");
241    // initialization for usage in pLcm()
242    number nOne         =   nInit(1);
243    LNode* newElement   =   gPrev->getLast();
244    LNode* temp         =   gPrev->getFirst();
245    poly u1             =   pOne();
246    poly u2             =   pOne();
247    poly lcm            =   pOne();
248    poly t              =   pHead(newElement->getPoly());
249    Rule* testedRule    =   NULL;
250    if(NULL != rules->getFirst()) {
251        testedRule  =   rules->getFirst()->getRule();
252    }
253    // computation of critical pairs
254    while( gPrev->getLast() != temp) {
255        pLcm(newElement->getPoly(), temp->getPoly(), lcm);
256        pSetCoeff(lcm,nOne);
257        // computing factors u2 for new labels
258        u1 = pDivide(lcm,t);
259        if(NULL == u1) {
260            break;
261        }
262        pSetCoeff(u1,nOne);
263        u2 = pDivide(lcm,pHead(temp->getPoly()));
264        pSetCoeff(u2,nOne);
265        // testing both new labels by the F5 Criterion
266        if(!criterion2(gPrev->getFirst()->getIndex(), u2, temp, rules, rTag)
267           && !criterion2(gPrev->getFirst()->getIndex(), u1, newElement, rules, rTag) 
268           && !criterion1(gPrev,u1,newElement,lTag) && !criterion1(gPrev,u2,temp,lTag)) {
269            // if they pass the test, add them to CList critPairs, having the LPoly with greater
270            // label as first element in the CPair
271            if(newElement->getIndex() == temp->getIndex() && 
272            -1 == pLmCmp(ppMult_qq(u1, newElement->getTerm()),ppMult_qq(u2, temp->getTerm()))) {
273                CPair* cp   =   new CPair(pDeg(ppMult_qq(u2,pHead(temp->getPoly()))), u2, 
274                                temp->getLPoly(), u1, newElement->getLPoly(), testedRule);                   
275                critPairs->insert(cp);
276            }
277            else {
278                CPair* cp   =   new CPair(pDeg(ppMult_qq(u2,pHead(temp->getPoly()))), u1, 
279                                newElement->getLPoly(), u2, temp->getLPoly(), testedRule);                   
280                critPairs->insert(cp);
281            }
282        }
283        else {
284        }
285        temp    =   temp->getNext();
286    }
287}
288
289
290
291
292
293
294
295/*
296========================================
297Criterion 1, i.e. Faugere's F5 Criterion
298========================================
299*/
300inline bool criterion1(LList* gPrev, poly t, LNode* l, LTagList* lTag) {
301    // starts at the first element in gPrev with index = (index of l)-1, these tags are saved in lTag
302        int idx =   l->getIndex();
303    int i;
304    if(idx == 1) {
305        //Print("HIER\n");
306        return false;
307    }
308    else {
309        LNode* testNode =   gPrev->getFirst();
310        // save the monom t1*label_term(l) as it is tested various times in the following
311        poly u1 = ppMult_qq(t,l->getTerm());
312        //Print("------------------------------IN CRITERION 1-----------------------------------------\n");
313        //Print("TESTED ELEMENT: ");
314        //pWrite(l->getPoly());
315        //pWrite(l->getTerm());
316        //pWrite(ppMult_qq(t,l->getTerm()));
317        //Print("%d\n\n",l->getIndex());
318        while(testNode->getIndex() < idx) { // && NULL != testNode->getLPoly()) {
319            //pWrite(testNode->getPoly());
320            //Print("%d\n",testNode->getIndex());
321            if(pLmDivisibleByNoComp(testNode->getPoly(),u1)) {
322                //Print("Criterion 1 NOT passed!\n");
323                return true;
324            }
325            //pWrite(testNode->getNext()->getPoly());
326            testNode    =   testNode->getNext();
327        }
328        /*
329        ideal testId    =   idInit(idx-1,1);
330        for(i=0;i<idx-1;i++) {
331            testId->m[i]  =   pHead(testNode->getPoly());
332            testNode        =   testNode->getNext();
333        }
334        poly temp   =   kNF(testId,currQuotient,u1);
335        //pWrite(temp);
336        for(i=0;i<IDELEMS(testId);i++) {
337            testId->m[i]    =   NULL;
338        }
339        idDelete(&testId);
340        if(NULL == temp) {
341            //if(l->getIndex() != gPrev->getFirst()->getIndex()) {
342            //    Print("----------------------------Criterion1 not passed----------------------------------\n");
343            //}
344            return true;
345        }
346        */
347        return false;
348    }
349}
350
351
352
353/*
354=====================================
355Criterion 2, i.e. Rewritten Criterion
356=====================================
357*/
358inline bool criterion2(int idx, poly t, LNode* l, RList* rules, RTagList* rTag) {
359    //Print("------------------------------IN CRITERION 2/1-----------------------------------------\n");
360    /* 
361    Print("RULES: \n");
362        RNode* tempR    =   rules->getFirst();
363        Print("%p\n",tempR);
364        int i   = 1;
365        while(NULL != tempR) {
366            Print("ADDRESS OF %d RNODE: %p\n",i,tempR);
367            Print("ADDRESS OF RULE: %p\n",tempR->getRule());
368            pWrite(tempR->getRuleTerm());
369            Print("ADDRESS OF TERM: %p\n",tempR->getRuleTerm());
370            Print("%d\n\n",tempR->getRuleIndex());
371            tempR   =   tempR->getNext();
372            i++;
373        }
374        //Print("TESTED ELEMENT: ");
375        //pWrite(l->getPoly());
376        //pWrite(l->getTerm());
377        //pWrite(ppMult_qq(t,l->getTerm()));
378        //Print("%d\n\n",l->getIndex());
379      */
380// start at the previously added element to gPrev, as all other elements will have the same index for sure
381    if(idx > l->getIndex()) {
382        return false;
383    }
384   
385    RNode* testNode; // =   new RNode();
386   
387
388    if(NULL == rTag->getFirst()) {
389        if(NULL != rules->getFirst()) {
390            testNode    =   rules->getFirst();
391        }
392        else {
393            return false;
394        }
395    }
396    else {
397
398        if(l->getIndex() > rTag->getFirst()->getRuleIndex()) {
399            testNode    =   rules->getFirst();
400        }
401        else {
402       //Print("HIER\n");
403            //Print("DEBUG\n");
404        //Print("L INDEX: %d\n",l->getIndex());
405            /*-------------------------------------
406             * TODO: WHEN INTERREDUCING THE GB THE
407             *       INDEX OF THE PREVIOUS ELEMENTS
408             *       GETS HIGHER!
409             *-----------------------------------*/
410            //testNode    =   rules->getFirst();
411            testNode    =   rTag->get(l->getIndex());
412            if(NULL == testNode) {
413                testNode    =   rules->getFirst();
414            }
415            //Print("TESTNODE ADDRESS: %p\n",testNode);
416        }
417    }
418    //testNode    =   rules->getFirst();
419        // save the monom t1*label_term(l) as it is tested various times in the following
420    poly u1 = ppMult_qq(t,l->getTerm());
421    // first element added to rTag was NULL, check for this
422    //Print("%p\n",testNode->getRule());
423    // NOTE: testNode is possibly NULL as rTag->get() returns NULL for elements of index <=1!
424    //Print("TESTNODE: %p\n",testNode);
425    //pWrite(testNode->getRuleTerm());
426    if(NULL != testNode ) {   
427        //pWrite(testNode->getRuleTerm());
428    }
429    if(NULL != testNode) {
430        if(testNode->getRule() == l->getRule()) {
431            //Print("%p\n%p\n",testNode->getRule(),l->getRule());
432            //Print("EQUAL\n");
433        }
434        else {
435            //Print("NOT EQUAL\n");
436        }
437    }
438    while(NULL != testNode && testNode->getRule() != l->getRule() 
439          && l->getIndex() == testNode->getRuleIndex()) {
440        //Print("%p\n",testNode);
441        //pWrite(testNode->getRuleTerm());
442        //pWrite(t);
443        //pWrite(l->getTerm());
444        //pWrite(u1);
445        //Print("%d\n",testNode->getRuleIndex());
446        if(pLmDivisibleByNoComp(testNode->getRuleTerm(),u1)) {
447            //Print("-----------------Criterion 2 NOT passed!-----------------------------------\n");
448            //Print("INDEX: %d\n",l->getIndex());
449            pDelete(&u1);
450    //Print("------------------------------IN CRITERION 2/1-----------------------------------------\n\n");
451            return true;
452        }
453                testNode    =   testNode->getNext();
454    }
455    //delete testNode;
456    pDelete(&u1);
457    //Print("------------------------------IN CRITERION 2/1-----------------------------------------\n\n");
458    return false;
459}
460
461
462
463/*
464=================================================================================================================
465Criterion 2, i.e. Rewritten Criterion, for its second call in computeSPols(), with added lastRuleTested parameter
466=================================================================================================================
467*/
468inline bool criterion2(poly t, LPoly* l, RList* rules, Rule* testedRule) {
469    //Print("------------------------------IN CRITERION 2/2-----------------------------------------\n");
470    //Print("LAST RULE TESTED: %p",testedRule);
471    /*
472    Print("RULES: \n");
473        RNode* tempR    =   rules->getFirst();
474        while(NULL != tempR) {
475            pWrite(tempR->getRuleTerm());
476            Print("%d\n\n",tempR->getRuleIndex());
477            tempR   =   tempR->getNext();
478        }
479        //Print("TESTED ELEMENT: ");
480        //pWrite(l->getPoly());
481        //pWrite(l->getTerm());
482        //pWrite(ppMult_qq(t,l->getTerm()));
483        //Print("%d\n\n",l->getIndex());
484    */
485// start at the previously added element to gPrev, as all other elements will have the same index for sure
486        RNode* testNode =   rules->getFirst();
487    // save the monom t1*label_term(l) as it is tested various times in the following
488    poly u1 = ppMult_qq(t,l->getTerm());
489        // first element added to rTag was NULL, check for this
490        while(NULL != testNode && testNode->getRule() != testedRule) {
491        //pWrite(testNode->getRuleTerm());
492        if(pLmDivisibleByNoComp(testNode->getRuleTerm(),u1)) {
493            //Print("--------------------------Criterion 2 NOT passed!------------------------------\n");
494            //Print("INDEX: %d\n",l->getIndex());
495            pDelete(&u1);
496    //Print("------------------------------IN CRITERION 2/2-----------------------------------------\n\n");
497            return true;
498        }
499                testNode    =   testNode->getNext();
500    }
501    pDelete(&u1);
502    //Print("------------------------------IN CRITERION 2/2-----------------------------------------\n\n");
503    return false;
504}
505
506
507
508/*
509==================================
510Computation of S-Polynomials in F5
511==================================
512*/
513void computeSPols(CNode* first, RTagList* rTag, RList* rules, LList* sPolyList) { 
514    CNode* temp         =   first;
515    poly sp     =   pInit();
516    number sign =   nInit(-1);   
517    //Print("###############################IN SPOLS##############################\n");
518    //first->print();
519
520    while(NULL != temp) {
521        //Print("JA\n");
522        // only if a new rule was added since the last test in subalgorithm criticalPair()
523        //if(rules->getFirst() != rTag->getFirst()) {
524        if(!criterion2(temp->getT1(),temp->getAdLp1(),rules,temp->getTestedRule())) {
525                // the second component is tested only when it has the actual index, otherwise there is
526                // no new rule to test since the last test in subalgorithm criticalPair()
527                if(highestDegree < pDeg(ppMult_qq(temp->getT1(),temp->getLp1Poly()))) { 
528                    highestDegree   = pDeg(ppMult_qq(temp->getT1(),temp->getLp1Poly()));
529                    //pWrite(pHead(ppMult_qq(temp->getT1(),temp->getLp1Poly())));
530                }   
531                if(temp->getLp2Index() == temp->getLp1Index()) {
532                    if(!criterion2(temp->getT2(),temp->getAdLp2(),rules,temp->getTestedRule())) {
533                        // computation of S-polynomial
534                        //poly p1 =   temp->getLp1Poly();
535                        //poly p2 =   temp->getLp2Poly();
536                        //pIter(p1);
537                        //pIter(p2);
538                        //sp  =   pAdd(ppMult_qq(temp->getT1(),p1),pMult_nn(ppMult_qq(temp->getT2(),p2),sign)); 
539                        sp      =   ksOldSpolyRedNew(ppMult_qq(temp->getT1(),temp->getLp1Poly()),
540                                         ppMult_qq(temp->getT2(),temp->getLp2Poly()));
541                        //Print("BEGIN SPOLY1\n====================\n");
542                        pNorm(sp);
543                        //pWrite(sp);
544                        //Print("END SPOLY1\n====================\n");
545                        if(NULL == sp) {
546                            // as rules consist only of pointers we need to save the labeled
547                            // S-polynomial also of a zero S-polynomial
548                            //zeroList->insert(temp->getAdT1(),temp->getLp1Index(),&sp);
549                            // origin of rule can be set NULL as the labeled polynomial
550                            // will never be used again as it is zero => no problems with
551                            // further criterion2() tests and termination conditions
552                            //Print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ZERO REDUCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
553                                                        reductionsToZero++;
554                        //Print("IN SPOLS 1\n");
555                            //Rule* rNew  =  new Rule(temp->getLp1Index(),ppMult_qq(temp->getT1(),temp->getLp1Term()));
556                            //rules->insertOrdered(rNew);
557                            rules->insert(temp->getLp1Index(),ppMult_qq(temp->getT1(),temp->getLp1Term()));
558                            //Print("RULE ADDED: \n");
559                            //pWrite(rules->getFirst()->getRuleTerm());
560                            //rules->print();
561                            // as sp = NULL, delete it
562                            pDelete(&sp);
563                            //Print("HIER\n");
564                        }
565                        else {
566                        //Print("IN SPOLS 2\n");
567                            //Rule* rNew  =  new Rule(temp->getLp1Index(),ppMult_qq(temp->getT1(),temp->getLp1Term()));
568                            //rules->insertOrdered(rNew);
569                            rules->insert(temp->getLp1Index(),ppMult_qq(temp->getT1(),temp->getLp1Term()));
570                            //Print("RULE ADDED: \n");
571                            //pWrite(rules->getFirst()->getRuleTerm()); 
572                            //rules->print();
573                            sPolyList->insertByLabel(ppMult_qq(temp->getT1(),temp->getLp1Term()),temp->getLp1Index(),sp,rules->getFirst()->getRule());
574                            //sPolyList->insertByLabel(ppMult_qq(temp->getT1(),temp->getLp1Term()),temp->getLp1Index(),sp,rNew);
575                        }
576                        // data is saved in sPolyList or zero => delete sp
577                    }
578                }
579                else { // temp->getLp2Index() < temp->getLp1Index()
580                    // computation of S-polynomial
581                        //poly p1 =   temp->getLp1Poly();
582                        //poly p2 =   temp->getLp2Poly();
583                        //pIter(p1);
584                        //pIter(p2);
585                        //sp  =   pAdd(ppMult_qq(temp->getT1(),p1),pMult_nn(ppMult_qq(temp->getT2(),p2),sign)); 
586                    sp      =   ksOldSpolyRedNew(ppMult_qq(temp->getT1(),temp->getLp1Poly()),
587                                     ppMult_qq(temp->getT2(),temp->getLp2Poly()));
588                    //Print("BEGIN SPOLY2\n====================\n");
589                    pNorm(sp);
590                    //pWrite(sp);
591                    //Print("END SPOLY2\n====================\n");
592                    if(NULL == sp) {
593                        // zeroList->insert(temp->getAdT1(),temp->getLp1Index(),&sp);
594                        // origin of rule can be set NULL as the labeled polynomial
595                        // will never be used again as it is zero => no problems with
596                        // further criterion2() tests and termination conditions
597                            //Print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ZERO REDUCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
598                        reductionsToZero++;
599                        //Print("IN SPOLS 3\n");
600                        rules->insert(temp->getLp1Index(),ppMult_qq(temp->getT1(),temp->getLp1Term()));
601                        //Print("RULE ADDED: \n");
602                        //pWrite(rules->getFirst()->getRuleTerm());
603                        //rules->print();
604                        // as sp = NULL, delete it
605                        pDelete(&sp);
606                    }
607                    else {
608                        //Print("IN SPOLS 4\n");
609                       
610                        //////////////////////////////////////////////////////////
611                        //////////////////////////////////////////////////////////
612                        // TODO: Rules inserted ordered by their label monomial!//
613                        //////////////////////////////////////////////////////////
614                        //////////////////////////////////////////////////////////
615
616                        //Rule* rNew      =   new Rule(temp->getLp1Index(),ppMult_qq(temp->getT1(),temp->getLp1Term()));
617                        //RNode* rNodeNew =   new RNode(rNew);
618                        //rules->insertOrdered(rNew);
619                        rules->insert(temp->getLp1Index(),ppMult_qq(temp->getT1(),temp->getLp1Term()));
620                        //Print("RULE ADDED: \n");
621                        //pWrite(rules->getFirst()->getRuleTerm());
622                        //rules->print();
623                        //Print("%d\n",rules->getFirst()->getRuleIndex());
624                        //Print("%p\n",sPolyList->getFirst());
625                        sPolyList->insertByLabel(ppMult_qq(temp->getT1(),temp->getLp1Term()),temp->getLp1Index(),sp,rules->getFirst()->getRule());
626                        //sPolyList->insertByLabel(ppMult_qq(temp->getT1(),temp->getLp1Term()),temp->getLp1Index(),sp,rNew);
627                    }
628                }
629            }
630        //}
631        //Print("%p\n",temp);
632        temp    =   temp->getNext();
633        //Print("%p\n",temp);
634        //Print("%p\n",temp->getData());
635        //pWrite(temp->getLp1Poly());
636    }
637    // these critical pairs can be deleted now as they are either useless for further computations or
638    // already saved as an S-polynomial to be reduced in the following
639    delete first;   
640}
641
642
643
644/*
645========================================================================
646reduction including subalgorithm topReduction() using Faugere's criteria
647========================================================================
648*/
649void reduction(LList* sPolyList, CList* critPairs, LList* gPrev, RList* rules, LTagList* lTag, RTagList* rTag, 
650                 ideal gbPrev) { 
651    //Print("##########################################In REDUCTION!########################################\n");
652    // check if sPolyList has any elements
653    // NOTE: due to initialization sPolyList always has a default NULL element
654    LNode* temp = sPolyList->getFirst();
655    while(NULL != temp) {
656        // temp is the first element in the sPolyList which should be reduced
657        // due to earlier sorting this is the element of minimal degree AND
658        // minimal label
659        // delete the above first element from sPolyList, temp will be either reduced to
660        // zero or added to gPrev, but never come back to sPolyList
661        //pWrite(sPolyList->getFirst()->getPoly());
662        //Print("LIST OF SPOLYNOMIALS!\n");
663        //sPolyList->print();
664        sPolyList->setFirst(temp->getNext());
665        poly tempNF = kNF(gbPrev,currQuotient,temp->getPoly());
666        if(NULL != tempNF) {
667            pNorm(tempNF);
668            temp->setPoly(tempNF);
669            // try further reductions of temp with polynomials in gPrev
670            // with label index = current label index: this is done such that there
671            // is no label corruption during the reduction process
672            //Print("lower label reduction:  ");
673            //pWrite(tempNF);
674            topReduction(temp,sPolyList,gPrev,critPairs,rules,lTag,rTag,gbPrev);
675       
676        }
677        else {
678            reductionsToZero++;
679            delete temp;
680        }
681        //if(NULL != temp->getPoly()) {
682        //    criticalPair(gPrev,critPairs,lTag,rTag,rules);
683        //}
684        temp =   sPolyList->getFirst();
685    }
686    //sPolyList->print();
687    //delete sPolyList;
688}   
689
690/*
691========================================================================
692reduction including subalgorithm topReduction() using Faugere's criteria
693========================================================================
694*/
695void newReduction(LList* sPolyList, CList* critPairs, LList* gPrev, RList* rules, LTagList* lTag, RTagList* rTag, 
696                 ideal gbPrev) { 
697    //Print("##########################################In REDUCTION!########################################\n");
698    // check if sPolyList has any elements
699    // NOTE: due to initialization sPolyList always has a default NULL element
700    //Print("--1--\n");
701    LNode* temp = sPolyList->getFirst();
702    while(NULL != temp) {
703        // temp is the first element in the sPolyList which should be reduced
704        // due to earlier sorting this is the element of minimal degree AND
705        // minimal label
706        // delete the above first element from sPolyList, temp will be either reduced to
707        // zero or added to gPrev, but never come back to sPolyList
708        //Print("LIST OF SPOLYNOMIALS!\n");
709        //sPolyList->print();
710        //pWrite(sPolyList->getFirst()->getPoly());
711        sPolyList->setFirst(temp->getNext());
712        //pWrite(temp->getPoly());
713        //poly tempNF = kNF(gbPrev,currQuotient,temp->getPoly());
714        //Print("!!!\n");
715        //if(NULL != tempNF) {
716            //pNorm(tempNF);
717            //temp->setPoly(tempNF);
718            //Print("lower label reduction:  ");
719            //pWrite(tempNF);
720            // try further reductions of temp with polynomials in gPrev
721            // with label index = current label index: this is done such that there
722            // is no label corruption during the reduction process
723            findReducers(temp,sPolyList,gbPrev,gPrev,critPairs,rules,lTag,rTag); 
724        //}
725        //else {
726        //    reductionsToZero++;
727        //    delete temp;
728        //}
729        //if(NULL != temp->getPoly()) {
730        //    criticalPair(gPrev,critPairs,lTag,rTag,rules);
731        //}
732        //Print("HIER AUCH ?\n");
733        //Print("--2--\n");
734        //sPolyList->print();
735        //critPairs->print();
736        temp =   sPolyList->getFirst();
737        //Print("%p\n",temp);
738    }
739    //sPolyList->print();
740    //delete sPolyList;
741    //Print("REDUCTION FERTIG\n");
742}     
743
744
745/*!
746 * ================================================================================
747 * searches for reducers of temp similar to the symbolic preprocessing of F4  and
748 * divides them into a "good" and "bad" part:
749 *
750 * the "good" ones are the reducers which do not corrupt the label of temp, with
751 * these the normal form of temp is computed
752 *
753 * the "bad" ones are the reducers which corrupt the label of temp, they are tested
754 * later on for possible new rules and S-polynomials to be added to the algorithm
755 * ================================================================================
756*/
757void findReducers(LNode* l, LList* sPolyList, ideal gbPrev, LList* gPrev, CList* critPairs, RList* rules, LTagList* lTag, RTagList* rTag) {
758    int canonicalize    =   0;
759    //int timerRed        =   0;
760    number sign         =   nInit(-1);
761    LList* good         =   new LList();
762    LList* bad          =   new LList();
763    LList* monomials    =   new LList(l->getLPoly());
764    poly u              =   pOne();
765    number nOne         =   nInit(1);
766    LNode* tempRed      =   lTag->getFirstCurrentIdx();
767    LNode* tempMon      =   monomials->getFirst();
768    poly tempPoly       =   pInit();
769    poly redPoly        =   NULL;
770    int idx             =   l->getIndex();
771    //Print("IN FIND REDUCERS:  ");
772    //pWrite(l->getPoly());
773    tempPoly    =   pCopy(l->getPoly());
774    //tempMon->setPoly(tempPoly);
775    //while(NULL != tempMon) {
776        // iteration over all monomials in tempMon
777        kBucket* bucket  =   kBucketCreate();
778        kBucketInit(bucket,tempPoly,0);
779        tempPoly    =   kBucketGetLm(bucket);
780        //Print("\n\n\nTO BE REDUCED:  ");
781        //pWrite(l->getPoly());
782        //pWrite(tempPoly);
783        while(NULL != tempPoly) {
784            // iteration of all elements in gPrev of the current index
785            tempRed =   gPrev->getFirst();
786            while(NULL != tempRed) {
787                //Print("TEMPREDPOLY:  ");
788                //pWrite(tempRed->getPoly());
789                if(pLmDivisibleByNoComp(tempRed->getPoly(),tempPoly)) {
790                    u   =   pDivideM(pHead(tempPoly),pHead(tempRed->getPoly()));
791                    //Print("U:  ");
792                    //pWrite(u);
793                    if(tempRed->getIndex() != idx) {
794                            // passing criterion1 ?
795                            if(!criterion1(gPrev,u,tempRed,lTag)) {
796                                    poly tempRedPoly    =   tempRed->getPoly();
797                                    //Print("REDUCER: ");
798                                    //pWrite(ppMult_qq(u,tempRedPoly));
799                                    pIter(tempRedPoly);
800                                    int lTempRedPoly    =   pLength(tempRedPoly);
801                                    kBucketExtractLm(bucket);
802                                    kBucket_Minus_m_Mult_p(bucket,u,tempRedPoly,&lTempRedPoly);
803                                    canonicalize++;
804                                    if(!(canonicalize % 50)) {
805                                        kBucketCanonicalize(bucket);
806                                    }
807                                    tempPoly    =   kBucketGetLm(bucket);
808                                    //Print("TEMPPOLY:  ");
809                                    //pWrite(tempPoly);
810                                    if(NULL != tempPoly) {
811                                        tempRed     =   gPrev->getFirst();
812                                        continue;
813                                    }
814                                    else {
815                                        break;
816                                    }
817                                    //tempRedPoly =   ppMult_qq(u,tempRedPoly);
818                                    //pMult_nn(tempRedPoly,sign);
819                                    //pAdd(tempPoly,tempRedPoly);
820                                    //good->insert(pOne(),1,ppMult_qq(u,tempRed->getPoly()),NULL);
821                                    //break;
822                             }   
823                   
824                    }
825                    else {
826                        if(pLmCmp(ppMult_qq(u,tempRed->getTerm()),l->getTerm()) != 0) {
827                            // passing criterion2 ?
828                            if(!criterion2(gPrev->getFirst()->getIndex(), u,tempRed,rules,rTag)) {
829                                // passing criterion1 ?
830                                if(!criterion1(gPrev,u,tempRed,lTag)) {
831                                    if(pLmCmp(ppMult_qq(u,tempRed->getTerm()),l->getTerm()) == 1) {
832                                        if(NULL == redPoly) {
833                                            bad->insert(tempRed->getLPoly());
834                                            //poly tempRedPoly    =   tempRed->getPoly();
835                                            //break;
836                                        }
837                                        /*
838                                        // DEBUGGING STUFF
839                                        //
840                                        //Print("REDUCER: ");
841                                        //pWrite(ppMult_qq(u,tempRedPoly));
842                                        rules->insert(l->getIndex(),ppMult_qq(u,tempRed->getTerm()));
843                                        l->setRule(rules->getFirst()->getRule());
844                                        poly tempRedPoly    =   tempRed->getPoly();
845                                        pIter(tempRedPoly);
846                                        int lTempRedPoly    =   pLength(tempRedPoly);
847                                        //Print("HEAD MONOMIAL KBUCKET: ");
848                                        //pWrite(kBucketGetLm(bucket));
849                                        kBucketExtractLm(bucket);
850                                        kBucket_Minus_m_Mult_p(bucket,u,tempRedPoly,&lTempRedPoly);
851                                        canonicalize++;
852                                        if(!(canonicalize % 50)) {
853                                            kBucketCanonicalize(bucket);
854                                        }
855                                        //Print("HEAD MONOMIAL KBUCKET: ");
856                                        //pWrite(kBucketGetLm(bucket));
857                                        tempPoly    =   kBucketGetLm(bucket);
858                                        //Print("TEMPPOLY:  ");
859                                        //pWrite(tempPoly);
860                                        if(NULL != tempPoly) {
861                                            tempRed     =   gPrev->getFirst();
862                                            continue;
863                                        }
864                                        else {
865                                            break;
866                                        }
867                                        */
868                                    }
869                                    else {
870                                        poly tempRedPoly    =   tempRed->getPoly();
871                                        //Print("REDUCER: ");
872                                        //pWrite(ppMult_qq(u,tempRedPoly));
873                                        pIter(tempRedPoly);
874                                        int lTempRedPoly    =   pLength(tempRedPoly);
875                                        //Print("HEAD MONOMIAL KBUCKET: ");
876                                        //pWrite(kBucketGetLm(bucket));
877                                        kBucketExtractLm(bucket);
878                                        kBucket_Minus_m_Mult_p(bucket,u,tempRedPoly,&lTempRedPoly);
879                                        canonicalize++;
880                                        if(!(canonicalize % 50)) {
881                                            kBucketCanonicalize(bucket);
882                                        }
883                                        //Print("HEAD MONOMIAL KBUCKET: ");
884                                        //pWrite(kBucketGetLm(bucket));
885                                        tempPoly    =   kBucketGetLm(bucket);
886                                        //Print("TEMPPOLY:  ");
887                                        //pWrite(tempPoly);
888                                        if(NULL != tempPoly) {
889                                            tempRed     =   gPrev->getFirst();
890                                            continue;
891                                        }
892                                        else {
893                                            break;
894                                        }
895                                    }
896                                }   
897                            }
898                        }
899                    }
900                   
901                }
902                tempRed =   tempRed->getNext();
903            }
904            if(NULL != tempPoly) {
905                if(NULL == redPoly) {
906                    redPoly =   kBucketExtractLm(bucket);
907                }
908                else {
909                    redPoly     =   p_Merge_q(redPoly,kBucketExtractLm(bucket),currRing);
910                }
911                tempPoly    =   kBucketGetLm(bucket);
912            }
913        }
914        if(NULL == redPoly) {
915            reductionsToZero++;
916            //pDelete(&redPoly);
917        }
918        else {
919            //Print("\nELEMENT ADDED TO GPREV: ");
920            //pWrite(redPoly);
921            pNorm(redPoly);
922            //Print("%d\n",canonicalize);
923            l->setPoly(redPoly);
924            gPrev->insert(l->getLPoly());
925            criticalPair(gPrev,critPairs,lTag,rTag,rules);
926        }
927   
928    // if there are "bad" reducers than try to compute new S-polynomials and rules
929   
930    if(NULL != bad->getFirst()) {
931        //Print("BAD STUFF LIST:\n");
932        //bad->print();
933        LNode* tempBad  =   bad->getFirst();
934        //pWrite(l->getPoly());
935        while(NULL != tempBad) {
936            if(pDivisibleBy(tempBad->getPoly(),l->getPoly())) {
937                //Print("BAD STUFF\n");
938                //pWrite(l->getPoly());
939                //pWrite(tempBad->getPoly());
940                u   =   pDivide(pHead(l->getPoly()),pHead(tempBad->getPoly()));
941                //Print("MULTIPLIER:  ");
942                //pWrite(u);
943                pSetCoeff(u,nOne);
944                if(pLmCmp(ppMult_qq(u,tempBad->getTerm()),l->getTerm()) != 0) {
945                    // passing criterion2 ?
946                    if(!criterion2(gPrev->getFirst()->getIndex(), u,tempBad,rules,rTag)) {
947                        // passing criterion1 ?
948                        if(!criterion1(gPrev,u,tempBad,lTag)) {
949                            //Print("HIERHIERHIERHIERHIERHIER\n");
950                            rules->insert(tempBad->getIndex(),ppMult_qq(u,tempBad->getTerm()));
951                            //gPrev->print();
952                            //pWrite(l->getPoly());
953                            poly temp   =   ksOldSpolyRedNew(ppMult_qq(u,tempBad->getPoly()),l->getPoly());
954                            //pWrite(l->getPoly());
955                            //Print("%p\n",temp);
956                            //gPrev->print();
957                            if(NULL != temp) {
958                                pNorm(temp);
959                                LNode* tempBadNew   =   new LNode(ppMult_qq(u,tempBad->getTerm()),tempBad->getIndex(),temp,rules->getFirst()->getRule());
960                                //pWrite(temp);
961                                //pWrite(tempBadNew->getPoly());
962                                //pWrite(tempBadNew->getTerm());
963                                //pWrite(pHead(tempBadNew->getPoly()));
964                                //Print("%p\n",tempBadNew->getPoly());
965                                //tempRed->getLPoly()->setRule(rules->getFirst()->getRule());
966                                tempBadNew->setDel(1);
967                           
968                                sPolyList->insertByLabel(tempBadNew);
969                                //Print("BAD SPOLYLIST: \n");
970                                //sPolyList->print();
971                            }
972                        }
973                    }
974                }
975            }
976        //Print("HIER\n");
977            tempBad =   tempBad->getNext();
978            //Print("%p\n",tempBad);
979        }
980       // Print("-------------------BAD STUFF LIST-----------------------------\n");
981    }
982        //Print("HIER AUCH\n");
983        //Print("SPOLYLIST IN BAD: \n");
984        //sPolyList->print();
985    //Print("END FIND REDUCERS\n");
986}
987
988/*
989=======================================================================================
990merging 2 polynomials p & q without requiring that all monomials of p & q are different
991if there are equal monomials in p & q only one of these monomials (always that of p!)
992is taken into account
993=======================================================================================
994
995poly p_MergeEq_q(poly p, poly q, const ring r) {
996  assume(p != NULL && q != NULL);
997  p_Test(p, r);
998  p_Test(q, r);
999#if PDEBUG > 0
1000  int l = pLength(p) + pLength(q);
1001#endif
1002
1003  spolyrec rp;
1004  poly a = &rp;
1005  DECLARE_LENGTH(const unsigned long length = r->CmpL_Size);
1006  DECLARE_ORDSGN(const long* ordsgn = r->ordsgn);
1007
1008  Top:     // compare p and q w.r.t. monomial ordering
1009  p_MemCmp(p->exp, q->exp, length, ordsgn, goto Equal, goto Greater , goto Smaller);
1010
1011  Equal:
1012  a =   pNext(a)    =   p;
1013  pIter(p);
1014  pIter(q);
1015  if(NULL == p) {
1016      if(NULL == q) {
1017          goto Finish;
1018      }
1019      else {
1020          pNext(a)  =   q;
1021          goto Finish;
1022      }
1023  }
1024  goto Top;
1025
1026  Greater:
1027  a = pNext(a) = p;
1028  pIter(p);
1029  if (p==NULL) { pNext(a) = q; goto Finish;}
1030  goto Top;
1031
1032  Smaller:
1033  a = pNext(a) = q;
1034  pIter(q);
1035  if (q==NULL) { pNext(a) = p; goto Finish;}
1036  goto Top;
1037
1038  Finish:
1039
1040  p_Test(pNext(&rp), r);
1041#if PDEBUG > 0
1042  pAssume1(l - pLength(pNext(&rp)) == 0);
1043#endif
1044  return pNext(&rp);
1045}
1046*/
1047
1048/*
1049=====================================================================================
1050top reduction in F5, i.e. reduction of a given S-polynomial by labeled polynomials of
1051the same index whereas the labels are taken into account
1052=====================================================================================
1053*/
1054void topReduction(LNode* l, LList* sPolyList, LList* gPrev, CList* critPairs,  RList* rules, LTagList* lTag, RTagList* rTag, ideal gbPrev) {
1055    //Print("##########################################In topREDUCTION!########################################\n");
1056    // try l as long as there are reductors found by findReductor()
1057    LNode* gPrevRedCheck    =   lTag->getFirstCurrentIdx();
1058    LNode* tempRed;
1059    poly pOne               =   pOne();
1060    do {
1061        //int timer5  =   initTimer();
1062        //startTimer();
1063        //Print("TOP REDUCTION:  ");
1064        //pWrite(l->getPoly());
1065        tempRed  =   findReductor(l,sPolyList,gPrevRedCheck,gPrev,rules,lTag,rTag);
1066        //timer5  =   getTimer();
1067        //reductionTime   =   reductionTime   +   timer5;
1068        // if a reductor for l is found and saved in tempRed
1069        if(NULL != tempRed) {
1070            // if label of reductor is greater than the label of l we have to built a new element
1071            // and add it to sPolyList
1072           
1073            if(pLmCmp(tempRed->getTerm(),l->getTerm()) == 1) {
1074                // needed sinc pSub destroys the arguments!
1075                //poly temp_poly_l    =   pInit();
1076                //temp_poly_l         =   pCopy(l->getPoly());
1077                //Print("VORHER: ");
1078                //pWrite(tempRed->getPoly());
1079                //poly temp           =   pMinus_mm_Mult_qq(tempRed->getPoly(),pOne,l->getPoly());
1080                poly temp   =   ksOldSpolyRedNew(l->getPoly(),tempRed->getPoly());
1081                rules->insert(tempRed->getIndex(),tempRed->getTerm());
1082                //Print("NACHHER: ");
1083                //pWrite(tempRed->getPoly());
1084                //Print("TEMP: ");
1085                //pWrite(temp);
1086                if(NULL != temp) {
1087                    pNorm(temp);
1088                    //tempRed->setPoly(temp);
1089                    //tempRed->setDel(1);
1090                    //rules->insert(tempRed->getIndex(),tempRed->getTerm());
1091                    LNode* tempRedNew   =   new LNode(tempRed->getTerm(),tempRed->getIndex(),temp,rules->getFirst()->getRule());
1092                    //tempRed->getLPoly()->setRule(rules->getFirst()->getRule());
1093                    tempRedNew->setDel(1);
1094                    sPolyList->insertByLabel(tempRedNew);
1095                }
1096                else {
1097                    pDelete(&temp);
1098                    reductionsToZero++;
1099                    //delete tempRed;
1100                }
1101            }
1102           
1103            // label of reductor is smaller than the label of l, subtract reductor from l and delete the
1104            // gPrevRedCheck pointer added to l during findReductor() as the head term of l changes
1105            // after subtraction
1106            else {
1107               
1108                //poly temp_poly_l    =   pInit();
1109                //temp_poly_l         =   pCopy(l->getPoly());
1110                //poly temp   =   pMinus_mm_Mult_qq(tempRed->getPoly(),pOne,l->getPoly());
1111                //Print("REDUCER: ");
1112                //pWrite(tempRed->getPoly());
1113                //pWrite(tempRed->getTerm());
1114                poly temp   =   ksOldSpolyRedNew(l->getPoly(),tempRed->getPoly());
1115                //Print("REDUCED ELEMENT:  ");
1116                if(NULL != temp) {
1117                    pNorm(temp);
1118                    //pWrite(temp);
1119                    poly tempNF =   kNF(gbPrev,currQuotient,temp); 
1120                    pNorm(tempNF);
1121                    if(NULL == tempNF) {
1122                        reductionsToZero++;
1123                        pDelete(&tempNF);
1124                        l->setPoly(NULL);
1125                        break;
1126                    }
1127                    l->setPoly(tempNF);
1128                   
1129                    gPrevRedCheck   =   lTag->getFirstCurrentIdx();
1130                }
1131                else {
1132                    reductionsToZero++;
1133                    pDelete(&temp);
1134                    l->setPoly(NULL);
1135                    break;
1136                }
1137            }   
1138        }
1139        else {
1140            if(NULL != l->getPoly()) {
1141                pNorm(l->getPoly());
1142                //Print("ELEMENT ADDED TO GPREV: ");
1143                //pWrite(l->getPoly());
1144                gPrev->insert(l->getLPoly());
1145                //Print("TEMP RED == 0  ");
1146                //pWrite(l->getPoly());
1147                //pWrite(l->getTerm());
1148                //rules->print();
1149                criticalPair(gPrev,critPairs,lTag,rTag,rules);
1150                //Print("LIST OF CRITICAL PAIRS:    \n");
1151                //critPairs->print();
1152            }
1153            break;
1154        }
1155    } while(1);
1156}
1157
1158
1159/*
1160=====================================================================
1161subalgorithm to find a possible reductor for the labeled polynomial l
1162=====================================================================
1163*/
1164LNode* findReductor(LNode* l, LList* sPolyList, LNode* gPrevRedCheck, LList* gPrev, RList* rules, LTagList* lTag,RTagList* rTag) {
1165    // allociation of memory for the possible reductor
1166    //Print("LPOLY:  ");
1167    //pWrite(l->getPoly());
1168    poly u      =   pOne();
1169    poly red;
1170    number nOne =   nInit(1);
1171    LNode* temp;
1172    // head term of actual element such that we do not have to call pHead at each new reductor test
1173    poly t      =   pHead(l->getPoly());
1174    // if l was already checked use the information in gPrevRedCheck such
1175    // that we can start searching for new reducers from this point and
1176    // not from the first element of gPrev with the current index
1177    temp    =   gPrevRedCheck;
1178    // search for reductors until we are at the end of gPrev resp. at the
1179    // end of the elements of the current index
1180    while(NULL != temp && temp->getIndex() == l->getIndex()) {
1181        // does the head of the element of gPrev divides the head of
1182        // the to be reduced element?
1183        if(pLmDivisibleByNoComp(pHead(temp->getPoly()),t)) {
1184            // get all the information needed for the following tests
1185            // of the criteria
1186            u   =   pDivide(t,pHead(temp->getPoly()));
1187            pSetCoeff(u,nOne);
1188            red =   ppMult_qq(u,temp->getPoly());
1189            pNorm(red);
1190            // check if both have the same label
1191            if(pLmCmp(ppMult_qq(u,temp->getTerm()),l->getTerm()) != 0) {
1192                // passing criterion2 ?
1193                if(!criterion2(gPrev->getFirst()->getIndex(), u,temp,rules,rTag)) {
1194                    // passing criterion1 ?
1195                    if(!criterion1(gPrev,u,temp,lTag)) {
1196                            gPrevRedCheck   =   temp->getNext();
1197                            LNode* redNode  =   new LNode(ppMult_qq(u,temp->getTerm()),temp->getIndex(),red,NULL,NULL);
1198                            return redNode;
1199                    }
1200                }
1201            }
1202            if(pLmCmp(ppMult_qq(u,temp->getTerm()),l->getTerm()) == 1) {
1203                // passing criterion2 ?
1204                if(!criterion2(gPrev->getFirst()->getIndex(), u,temp,rules,rTag)) {
1205                    // passing criterion1 ?
1206                    if(!criterion1(gPrev,u,temp,lTag)) {
1207                        poly tempSpoly  =   ksOldSpolyRedNew(red,l->getPoly());
1208                        rules->insert(temp->getIndex(),ppMult_qq(u,temp->getTerm()));
1209                        gPrevRedCheck   =   temp->getNext();
1210                        if(NULL != tempSpoly) {
1211                            pNorm(tempSpoly);
1212                            sPolyList->insertByLabel(ppMult_qq(u,temp->getTerm()),temp->getIndex(),tempSpoly,rules->getFirst()->getRule());
1213                    //Print("NEW ONE: ");
1214                    //pWrite(tempSpoly);
1215                    //Print("HIER\n");
1216                            //sPolyList->print();
1217                            //sleep(5);
1218                        }
1219                    }
1220                }
1221            }
1222        }
1223        //Print("AUCH HIER\n");
1224        temp = temp->getNext();
1225    }
1226   
1227//    delete temp;
1228 return NULL;
1229}
1230
1231
1232
1233/*
1234==========================================================================
1235MAIN:computes a gb of the ideal i in the ring r with our F5 implementation
1236==========================================================================
1237*/
1238ideal F5main(ideal id, ring r) {
1239    int timer   =   initTimer();
1240    startTimer();
1241    int i,j,k,l;
1242    int gbLength;
1243    // 1 polynomial for defining initial labels & further tests
1244    poly ONE = pOne();
1245    poly pOne   =   pOne();
1246    number nOne =   nInit(1);
1247    // tag the first element of index i-1 for criterion 1
1248    //Print("LTAG BEGINNING: %p\n",lTag);
1249   
1250    // DEBUGGING STUFF START
1251    //Print("NUMBER: %d\n",r->N);
1252    /*
1253    int* ev = new int[r->N +1];
1254    for(i=0;i<IDELEMS(id);i++) {
1255        pGetExpV(id->m[i],ev);
1256        //ev2  =   pGetExp(id->m[i],1);
1257        pWrite(id->m[i]);
1258        Print("EXP1: %d\n",ev[1]);
1259        Print("EXP2: %d\n",ev[2]);
1260        Print("EXP3: %d\n\n",ev[3]);
1261        Print("SUM: %ld\n\n\n",sumVector(ev,r->N));
1262    }
1263    delete ev;
1264    */
1265    /*DEBUGGING STUFF END */
1266   
1267    // first element in rTag is first element of rules which is NULL RNode,
1268    // this must be done due to possible later improvements
1269    RList* rules    =   new RList();
1270    //Print("RULES FIRST: %p\n",rules->getFirst());
1271    //Print("RULES FIRST DATA: %p\n",rules->getFirst()->getRule());
1272    RTagList* rTag  =   new RTagList(rules->getFirst());
1273    i = 1;
1274    /*for(j=0; j<IDELEMS(id); j++) {
1275        if(NULL != id->m[j]) {
1276            if(pComparePolys(id->m[j],ONE)) {
1277                Print("One Polynomial in Input => Computations stopped\n");
1278                ideal idNew = idInit(1,1);
1279                idNew->m[0] = ONE;
1280                return(idNew);
1281            }   
1282        }
1283    }*/ 
1284    ideal idNew     =   kInterRed(id); 
1285    id              =   idNew;
1286    //qsortDegree(&id->m[0],&id->m[IDELEMS(id)-1]);
1287    //idShow(id);
1288    LList* gPrev    =   new LList(ONE, i, id->m[0]);
1289    //idShow(id);
1290    //Print("%p\n",id->m[0]);
1291    //pWrite(id->m[0]);
1292    //Print("%p\n",gPrev->getFirst()->getPoly());
1293    //pWrite(gPrev->getFirst()->getPoly());
1294
1295    LTagList* lTag  =   new LTagList(gPrev->getFirst());
1296    //lTag->insert(gPrev->getFirst());
1297    lTag->setFirstCurrentIdx(gPrev->getFirst());
1298    // computing the groebner basis of the elements of index < actual index
1299    gbLength    =   gPrev->getLength();
1300    //Print("Laenge der bisherigen Groebner Basis: %d\n",gPrev->getLength());
1301    ideal gbPrev    =   idInit(gbLength,1);
1302    // initializing the groebner basis of elements of index < actual index
1303    gbPrev->m[0]    =   gPrev->getFirst()->getPoly();
1304    //idShow(gbPrev);
1305    //idShow(currQuotient);
1306    for(i=2; i<=IDELEMS(id); i++) {
1307        LNode* gPrevTag =   gPrev->getLast();
1308        //Print("Last POlynomial in GPREV: ");
1309        //Print("%p\n",gPrevTag);   
1310        //pWrite(gPrevTag->getPoly());
1311        gPrev   =   F5inc(i, id->m[i-1], gPrev, gbPrev, ONE, lTag, rules, rTag);
1312        //Print("%d\n",gPrev->count(gPrevTag->getNext()));
1313        //Print("%d\n",gPrev->getLength());
1314        //Print("____________________________________ITERATION STEP DONE________________________________________\n");
1315       
1316        // DEBUGGING STUFF
1317        LNode* temp    =   gPrev->getFirst();
1318   
1319
1320        /////////////////////////////////////////////////////////////////////////////////
1321        //                                                                             //
1322        // one needs to choose one of the following 3 implementations of the algorithm //
1323        // F5,F5R or F5C                                                               //
1324        //                                                                             //
1325        /////////////////////////////////////////////////////////////////////////////////                                                                           
1326       
1327       
1328        //   
1329        // remove this comment to get "F5"
1330        //
1331         
1332        if(gPrev->getLength() > gbLength) {
1333            if(i < IDELEMS(id)) {
1334                ideal gbAdd =   idInit(gPrev->getLength()-gbLength,1);
1335                LNode*  temp =   gPrevTag;
1336                int counter =   0;
1337                for(j=0;j<=gPrev->getLength()-gbLength-1;j++) {
1338                    temp        =   temp->getNext();
1339                    if(0 == temp->getDel()) {
1340                        counter++;
1341                        gbAdd->m[j] =   temp->getPoly();
1342                    }
1343                }
1344                    gbPrev          =   idAdd(gbPrev,gbAdd);
1345            }
1346            else {
1347                ideal gbAdd =   idInit(gPrev->getLength()-gbLength,1);
1348                LNode*  temp =   gPrevTag;
1349                for(j=0;j<=gPrev->getLength()-gbLength-1;j++) {
1350                    temp        =   temp->getNext();
1351                    gbAdd->m[j] =   temp->getPoly();
1352                }
1353                gbPrev          =   idAdd(gbPrev,gbAdd);
1354            }
1355            if(i == IDELEMS(id)) {
1356                ideal tempId        =   kInterRed(gbPrev);
1357                gbPrev              =   tempId;
1358            }
1359        }
1360        gbLength    =   gPrev->getLength();
1361       
1362       
1363
1364        //
1365        // remove this comment to get "F5R"
1366        //
1367        /*
1368        if(gPrev->getLength() > gbLength) {
1369            if(i < IDELEMS(id)) {
1370                ideal gbAdd =   idInit(gPrev->getLength()-gbLength,1);
1371                LNode*  temp =   gPrevTag;
1372                int counter =   0;
1373                for(j=0;j<=gPrev->getLength()-gbLength-1;j++) {
1374                    temp        =   temp->getNext();
1375                    if(0 == temp->getDel()) {
1376                        counter++;
1377                        gbAdd->m[j] =   temp->getPoly();
1378                    }
1379                }
1380                    gbPrev          =   idAdd(gbPrev,gbAdd);
1381            }
1382            else {
1383                ideal gbAdd =   idInit(gPrev->getLength()-gbLength,1);
1384                LNode*  temp =   gPrevTag;
1385                for(j=0;j<=gPrev->getLength()-gbLength-1;j++) {
1386                    temp        =   temp->getNext();
1387                    gbAdd->m[j] =   temp->getPoly();
1388                }
1389                gbPrev          =   idAdd(gbPrev,gbAdd);
1390            }
1391            // interreduction stuff
1392            // comment this out if you want F5 instead of F5R
1393            //if(i<IDELEMS(id)) {
1394                ideal tempId    =   kInterRed(gbPrev);
1395                gbPrev          =   tempId;
1396            //}
1397        }
1398        gbLength    =   gPrev->getLength();
1399        */
1400       
1401
1402        //
1403        // Remove this comment to get "F5C"
1404        // computing new groebner basis gbPrev
1405        //
1406        /*
1407        if(gPrev->getLength() > gbLength) {
1408            if(i < IDELEMS(id)) {
1409                ideal gbAdd =   idInit(gPrev->getLength()-gbLength,1);
1410                LNode*  temp =   gPrevTag;
1411                for(j=0;j<=gPrev->getLength()-gbLength-1;j++) {
1412                    temp        =   temp->getNext();
1413                        gbAdd->m[j] =   temp->getPoly();
1414                }
1415                    gbPrev          =   idAdd(gbPrev,gbAdd);
1416            }
1417            else {
1418                ideal gbAdd =   idInit(gPrev->getLength()-gbLength,1);
1419                LNode*  temp =   gPrevTag;
1420                for(j=0;j<=gPrev->getLength()-gbLength-1;j++) {
1421                    temp        =   temp->getNext();
1422                    gbAdd->m[j] =   temp->getPoly();
1423                }
1424                gbPrev          =   idAdd(gbPrev,gbAdd);
1425            }
1426            //if(i<IDELEMS(id)) {
1427                ideal tempId    =   kInterRed(gbPrev);
1428                Print("HERE\n");
1429                gbPrev          =   tempId;
1430                delete gPrev;
1431                delete rules;
1432                gPrev    =   new LList(pOne,1,gbPrev->m[0]);
1433                gPrev->insert(pOne,1,gbPrev->m[1]);
1434                rules    =   new RList();
1435                rTag     =   new RTagList(rules->getFirst());
1436                for(k=2; k<IDELEMS(gbPrev); k++) {
1437                    gPrev->insert(pOne,k+1,gbPrev->m[k]);
1438                    for(l=0; l<k; l++) {
1439                    }
1440                    rTag->insert(rules->getFirst());
1441                }
1442            //}
1443            gbLength    =   gPrev->getLength();
1444        } 
1445       */   
1446
1447
1448    }
1449    //Print("\n\nADDING TIME IN REDUCTION: %d\n\n",reductionTime);
1450    Print("\n\nNumber of zero-reductions:  %d\n",reductionsToZero);
1451    timer   =   getTimer();
1452    Print("Highest Degree during computations: %d\n",highestDegree);
1453    Print("Time for computations: %d/1000 seconds\n",timer);
1454    Print("Number of elements in gb: %d\n",IDELEMS(gbPrev));
1455    //LNode* temp    =   gPrev->getFirst();
1456    //while(NULL != temp) {
1457    //    pWrite(temp->getPoly());
1458    //    temp    =   temp->getNext();
1459    // }
1460    //gPrev->print();
1461    //delete lTag;
1462    delete rTag;
1463    delete gPrev;
1464    reductionsToZero    =   0;
1465    highestDegree       =   0;
1466    reductionTime       =   0;
1467    spolsTime           =   0;
1468    return(gbPrev);
1469
1470
1471}
1472
1473#endif
Note: See TracBrowser for help on using the repository browser.