source: git/kernel/f5lists.cc @ d51339

fieker-DuValspielwiese
Last change on this file since d51339 was d51339, checked in by Christian Eder, 15 years ago
new written reduction process, still problems getting the right rules in criterion2() ->lastRuleTested shouldnt be part of the CPair as it is a characteristic of an LPoly git-svn-id: file:///usr/local/Singular/svn/trunk@11406 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 21.7 KB
Line 
1#include "mod2.h"
2
3#ifdef HAVE_F5
4#include "kutil.h"
5#include "structs.h"
6#include "omalloc.h"
7#include "polys.h"
8#include "p_polys.h"
9#include "ideals.h"
10#include "febase.h"
11#include "kstd1.h"
12#include "khstd.h"
13#include "kbuckets.h"
14#include "weight.h"
15#include "intvec.h"
16#include "pInline1.h"
17#include "f5gb.h"
18#include "f5data.h"
19#include "f5lists.h"
20
21/*
22====================================
23functions working on the class LNode
24====================================
25*/
26
27// generating new list elements (labeled / classical polynomial / LNode view)
28LNode::LNode() {
29    data                =   NULL;
30    next                =   NULL;
31    gPrevRedCheck       =   NULL;
32}
33 LNode::LNode(LPoly* lp) {
34    data                =   lp;
35    next                =   NULL;
36    gPrevRedCheck       =   NULL;
37}
38       
39LNode::LNode(LPoly* lp, LNode* l) {
40Print("HIER LNODE\n");
41    data                =   lp;
42    next                =   l;
43    gPrevRedCheck       =   NULL;
44}
45
46LNode::LNode(poly t, int i, poly p, Rule* r, LNode* gPCheck) {
47LPoly* lp           =   new LPoly(t,i,p,r);
48data                =   lp;
49next                =   NULL;
50gPrevRedCheck       =   gPCheck;
51}
52       
53LNode::LNode(poly t, int i, poly p, Rule* r, LNode* gPCheck, LNode* l) {
54    LPoly* lp           =   new LPoly(t,i,p,r);
55    data                =   lp;
56    next                =   l;
57    gPrevRedCheck       =   gPCheck;
58}
59
60 LNode::LNode(LNode* ln) {
61    data                =   ln->getLPoly();
62    next                =   ln->getNext();
63    gPrevRedCheck       =   NULL;
64}
65       
66LNode::~LNode() {
67    //delete next;
68    delete gPrevRedCheck;
69    delete data;   
70}
71       
72// insert new elements to the list always at the end (labeled / classical polynomial view)
73// needed for list gPrev
74LNode* LNode::insert(LPoly* lp) {
75    Print("INSERTION: \n");
76    Print("LAST GPREV: ");
77    pWrite(this->getPoly());
78LNode* newElement   =   new LNode(lp, NULL);
79    this->next          =   newElement;
80    return newElement;
81}
82       
83LNode* LNode::insert(poly t, int i, poly p, Rule* r) {
84    LNode* newElement   =   new LNode(t, i, p, r, NULL, NULL);
85    this->next          =   newElement;
86    return newElement;
87}
88
89// insert new elements to the list always in front (labeled / classical polynomial view)
90// needed for sPolyList
91LNode* LNode::insertSP(LPoly* lp) {
92    LNode* newElement   =   new LNode(lp, this);
93    return newElement;
94}
95       
96LNode* LNode::insertSP(poly t, int i, poly p, Rule* r) {
97    LNode* newElement   =   new LNode(t, i, p, r, NULL, this);
98    return newElement;
99}
100// insert new elemets to the list w.r.t. increasing labels
101// only used for the S-polys to be reduced (TopReduction building new S-polys with higher label)
102LNode* LNode::insertByLabel(poly t, int i, poly p, Rule* r) {
103    if(0 == pLmCmp(t,this->getTerm()) || -1 == pLmCmp(t,this->getTerm())) {
104        LNode* newElement   =   new LNode(t, i, p, r, this);
105        return newElement;
106    }
107    else {
108        LNode* temp = this;
109        while( NULL != temp->next->data ) {
110            if( 0 == pLmCmp(t,temp->next->getTerm()) || -1 == pLmCmp(t,temp->next->getTerm())) {
111                LNode* newElement   =   new LNode(t, i, p, r, temp->next);
112                temp->next          =   newElement;
113                return this;
114            }
115            else {
116                temp = temp->next;
117            }
118        }
119        LNode* newElement   =   new LNode(t, i, p, r, NULL);
120        temp->next          =   newElement;
121        return this;
122    }
123}
124
125// deletes the first elements of the list with the same degree
126// only used for the S-polys, which are already sorted by increasing degree by CList
127LNode*  LNode::deleteByDeg() {
128    return this;
129}
130
131// get next from current LNode
132LNode* LNode::getNext() {
133    return next;
134}
135
136// get the LPoly* out of LNode*
137LPoly* LNode::getLPoly() {
138    return data;
139}
140
141// get the data from the LPoly saved in LNode
142poly LNode::getPoly() {
143    return data->getPoly();
144}
145
146poly LNode::getTerm() {
147    return data->getTerm();
148}
149
150int LNode::getIndex() {
151    return data->getIndex();
152}
153
154Rule* LNode::getRule() {
155    return data->getRule();
156}
157
158LNode* LNode::getGPrevRedCheck() {
159    return gPrevRedCheck;
160}
161
162// set the data from the LPoly saved in LNode
163void LNode::setPoly(poly p) {
164    data->setPoly(p);
165}
166
167void LNode::setTerm(poly t) {
168    data->setTerm(t);
169}
170
171void LNode::setIndex(int i) {
172    data->setIndex(i);
173}
174
175void LNode::setGPrevRedCheck(LNode* l) {
176    gPrevRedCheck   =   l;
177}
178
179// test if for any list element the polynomial part of the data is equal to *p
180bool LNode::polyTest(poly* p) {
181    LNode* temp = new LNode(this);
182    while(NULL != temp) {
183        if(pComparePolys(temp->getPoly(),*p)) {
184            return 1;
185        }
186        temp = temp->next;
187    }
188    return 0;
189}
190
191LNode* LNode::getNext(LNode* l) {
192    return l->next;
193}
194
195// for debugging
196void LNode::print() {
197    LNode* temp = this;
198    Print("___________________List of S-polynomials______________________:\n");
199    while(NULL != temp->data) {
200        Print("Index: %d\n",temp->getIndex());
201        Print("Term: ");
202        pWrite(temp->getTerm());
203        Print("Poly: ");
204        pWrite(temp->getPoly());
205        Print("\n");
206        temp = temp->next;
207    }
208}
209
210
211/*
212====================================
213functions working on the class LList
214====================================
215*/
216
217LList::LList() {
218    first   =   new LNode();
219    last    =   first;
220    length  =   0;
221}
222
223LList::LList(LPoly* lp) {
224    first   =   new LNode(lp);
225    last    =   first;
226    length  =   1;
227}
228
229LList::LList(poly t,int i,poly p,Rule* r) {
230    first   =   new LNode(t,i,p,r);
231    last    =   first;
232    length  =   1;
233} 
234
235LList::~LList() {
236    delete first;
237}
238
239// insertion at the end of the list, needed for gPrev
240void LList::insert(LPoly* lp) {
241    last = last->insert(lp);
242    Print("NEW LAST GPREV: ");
243    pWrite(last->getPoly());
244    length++;
245    Print("LENGTH %d\n",length);
246}
247
248void LList::insert(poly t,int i, poly p, Rule* r) {
249    last = last->insert(t,i,p,r);
250    length++;
251    Print("LENGTH %d\n",length);
252}
253
254// insertion in front of the list, needed for sPolyList
255void LList::insertSP(LPoly* lp) {
256    first = first->insertSP(lp);
257    length++;
258    Print("LENGTH %d\n",length);
259}
260
261void LList::insertSP(poly t,int i, poly p, Rule* r) {
262    first = first->insertSP(t,i,p,r);
263    length++;
264    Print("LENGTH %d\n",length);
265}
266
267
268void LList::insertByLabel(poly t, int i, poly p, Rule* r) {
269    first = first->insertByLabel(t,i,p,r);
270    length++;
271    Print("LENGTH %d\n",length);
272}
273
274void LList::insertByLabel(LNode* l) {
275    first = first->insertByLabel(l->getTerm(),l->getIndex(),l->getPoly(),l->getRule());
276    length++;
277    Print("LENGTH %d\n",length);
278}
279
280void LList::deleteByDeg() {
281    first = first->deleteByDeg();
282}
283
284bool LList::polyTest(poly* p) {
285    return first->polyTest(p);
286}
287
288LNode* LList::getFirst() {
289    return first;
290}
291
292LNode* LList::getLast() {
293    return last;
294}
295
296int LList::getLength() {
297    return length;
298}
299
300void LList::setFirst(LNode* l) {
301    first       =   l;
302    length--;
303}
304
305void LList::print() {
306    first->print();
307}
308
309/*
310=======================================
311functions working on the class LTagNode
312=======================================
313*/
314LTagNode::LTagNode() {
315    data    =   NULL;
316    next    =   NULL;
317}
318
319LTagNode::LTagNode(LNode* l) {
320    data = l;
321    next = NULL;
322}
323       
324LTagNode::LTagNode(LNode* l, LTagNode* n) {
325    data = l;
326    next = n;
327}
328
329 LTagNode::~LTagNode() {
330    delete next;
331    delete data;   
332}
333       
334// declaration with first as parameter due to sorting of LTagList
335LTagNode* LTagNode::insert(LNode* l) {
336    LTagNode* newElement  = new LTagNode(l, this);
337    return newElement;
338}
339
340LNode* LTagNode::getLNode() {
341    return this->data;
342}
343
344LTagNode* LTagNode::getNext() {
345    return next;
346}
347
348// NOTE: We insert at the beginning of the list and length = i-1, where i is the actual index.
349//       Thus given actual index i and idx being the index of the LPoly under investigation
350//       the element on position length-idx is the right one
351LNode* LTagNode::get(int idx, int length) {
352    if(idx == 1) {
353        return NULL;
354    }
355    else {
356        int j;
357        LTagNode* temp = this; // last
358        for(j=1;j<=length-idx+1;j++) {
359            temp = temp->next;
360        }
361        return temp->data;
362    }
363}
364
365
366/*
367=======================================
368functions working on the class LTagList
369=======================================
370*/
371LTagList::LTagList() {
372    LTagNode* first =   new LTagNode();
373   
374    length          =   0;
375}
376
377LTagList::LTagList(LNode* l) {
378    LTagNode* first =   new LTagNode(l);
379    length          =   1;
380}
381
382// declaration with first as parameter in LTagNode due to sorting of LTagList
383void LTagList::insert(LNode* l) {
384    first   =   first->insert(l);
385    length++;
386}
387
388void LTagList::setFirstCurrentIdx(LNode* l) {
389    firstCurrentIdx =   l;
390}
391
392LNode* LTagList::get(int idx) {
393    return first->get(idx, length);
394}
395
396LNode* LTagList::getFirst() {
397    return first->getLNode();
398}
399
400LNode* LTagList::getFirstCurrentIdx() {
401    return firstCurrentIdx;
402}
403
404/*
405=====================================
406functions working on the class TopRed
407=====================================
408*/
409
410TopRed::TopRed() {
411    _completed  =   NULL;
412    _toDo       =   NULL;
413}
414
415TopRed::TopRed(LList* c, LList* t) {
416    _completed  =   c;
417    _toDo       =   t;
418}
419
420LList* TopRed::getCompleted() {
421    return _completed;
422}
423
424LList* TopRed::getToDo() {
425    return _toDo;
426}
427
428/*
429====================================
430functions working on the class CNode
431====================================
432*/
433
434CNode::CNode() {
435    data    =   NULL;   
436    next    =   NULL;   
437}
438
439CNode::CNode(CPair* c) {
440    data    =   c;   
441    next    =   NULL;   
442}
443
444CNode::CNode(CPair* c, CNode* n) {
445    data    =   c;   
446    next    =   n;   
447}
448
449CNode::~CNode() {
450    delete next;
451    delete data;
452}
453
454// insert sorts the critical pairs firstly by increasing total degree, secondly by increasing label
455// note: as all critical pairs have the same index here, the second sort is done on the terms of the labels
456// working only with linked, but not doubly linked lists due to memory usage we have to check the
457// insertion around the first element separately from the insertion around all other elements in the list
458CNode* CNode::insert(CPair* c, CNode* last) {
459    if(NULL == this->data) {
460        CNode* newElement   =   new CNode(c, this);
461        return newElement;
462    }
463    else {
464        poly u1 = ppMult_qq(c->getT1(),c->getLp1Term());
465        if( c->getDeg() < this->data->getDeg() ) { // lower degree than the first list element
466            CNode* newElement   =   new CNode(c, this);
467            return newElement;
468        }
469        if( c->getDeg() == this->data->getDeg() ) { // same degree than the first list element
470            if(1 != pLmCmp(u1,ppMult_qq(this->data->getT1(), this->data->getLp1Term()))) {
471                pWrite(u1);
472                Print("Multi-Term in CritPairs Sortierung altes Element: ");
473                pWrite(ppMult_qq(this->data->getT1(),this->data->getLp1Term()));
474                CNode* newElement   =   new CNode(c, this);
475                return newElement;
476            }
477            else {
478                Print("Insert Deg\n");
479                CNode* temp = this;
480                while(  NULL != temp->next->data ) {
481                    if(temp->next->data->getDeg() == c->getDeg() ) { 
482                        if(1 == pLmCmp(u1,ppMult_qq(temp->next->data->getT1(),temp->next->data->getLp1Term()))) {
483                            temp = temp->next;
484                        }
485                        else {
486                            CNode* newElement   =   new CNode(c, temp->next);
487                            temp->next          =   newElement;
488                            return this;
489                        } 
490                    }
491                    else {
492                        CNode* newElement   =   new CNode(c, temp->next);
493                        temp->next          =   newElement;
494                        return this;
495                    }
496                }
497                CNode* newElement   =   new CNode(c, last);
498                temp->next          =   newElement;
499                return this;
500            }
501        } // outer if-clause
502        if( c->getDeg() > this->data->getDeg() ) { // greater degree than the first list element
503            CNode* temp =   this;
504            while( NULL != temp->next->data ) {   
505                if( c->getDeg() < temp->next->data->getDeg() ) {
506                    CNode* newElement   =   new CNode(c, temp->next);
507                    temp->next          =   newElement;
508                    return this;
509                }
510                if( c->getDeg() == temp->next->data->getDeg() ) {
511                    if(1 != pLmCmp(u1,ppMult_qq(temp->next->data->getT1(),temp->next->data->getLp1Term()))) { 
512                        CNode* newElement   =   new CNode(c, temp->next);
513                        temp->next          =   newElement;
514                        return this;
515                    }
516                    else {
517                        temp = temp->next;
518                        while(  NULL != temp->next->data ) {
519                            if( temp->next->data->getDeg() == c->getDeg() ) { 
520                                if(1 == pLmCmp(u1,ppMult_qq(temp->next->data->getT1(),
521                                               temp->next->data->getLp1Term()))) {
522                                    temp = temp->next;
523                                }
524                                else {
525                                    CNode* newElement   =   new CNode(c, temp->next);
526                                    temp->next          =   newElement;
527                                    return this;
528                                } 
529                            }
530                            else {
531                                CNode* newElement   =   new CNode(c, temp->next);
532                                temp->next          =   newElement;
533                                return this;
534                            }
535                        }
536                        CNode* newElement   =   new CNode(c, last);
537                        temp->next          =   newElement;
538                        return this;
539                    }
540                }
541                if( c->getDeg() > temp->next->data->getDeg() ) {
542                    temp    =   temp->next;
543                }
544            }
545            CNode* newElement   =   new CNode(c, last);
546            temp->next          =   newElement;
547            return this;
548        }
549    }
550}
551
552// get the first elements from CList which by the above sorting have minimal degree
553CNode* CNode::getMinDeg() {
554    CNode* temp = this;
555    while( NULL != temp->data ) {
556        while(NULL != temp->next->data && temp->next->data->getDeg() == this->data->getDeg()) {
557            temp = temp->next;
558        }
559        CNode* returnCNode  =   temp->next;   
560        // every CList should end with a (NULL,NULL) element for a similar behaviour
561        // using termination conditions throughout the algorithm
562        temp->next          =   new CNode();
563        return returnCNode;
564    }
565    return NULL;
566}
567
568CPair* CNode::getData() {
569    return data;
570}
571
572CNode* CNode::getNext() {
573    return next;
574}
575
576LPoly* CNode::getAdLp1() {
577    return this->data->getAdLp1();
578}
579
580LPoly* CNode::getAdLp2() {
581    return this->data->getAdLp2();
582}
583
584poly CNode::getLp1Poly() {
585    return this->data->getLp1Poly();
586}
587
588poly CNode::getLp2Poly() {
589    return this->data->getLp2Poly();
590}
591
592poly CNode::getLp1Term() {
593    return this->data->getLp1Term();
594}
595
596poly CNode::getLp2Term() {
597    return this->data->getLp2Term();
598}
599
600int CNode::getLp1Index() {
601    return this->data->getLp1Index();
602}
603
604int CNode::getLp2Index() {
605    return this->data->getLp2Index();
606}
607
608poly CNode::getT1() {
609    return this->data->getT1();
610}
611
612poly* CNode::getAdT1() {
613    return this->data->getAdT1();
614}
615
616poly CNode::getT2() {
617    return this->data->getT2();
618}
619
620poly* CNode::getAdT2() {
621    return this->data->getAdT2();
622}
623
624Rule* CNode::getLastRuleTested() {
625    return this->data->getLastRuleTested();
626}
627
628// for debugging
629void CNode::print() {
630    CNode* temp = this;
631    Print("___________________List of critical pairs______________________:\n");
632    while(NULL != temp->data) {
633        Print("LP1 Index: %d\n",temp->getLp1Index());
634        Print("T1: ");
635        pWrite(temp->getT1());
636        Print("LP1 Term: ");
637        pWrite(temp->getLp1Term());
638        Print("LP1 Poly: ");
639        pWrite(temp->getLp1Poly());
640        Print("LP2 Index: %d\n",temp->getLp2Index());
641        Print("T2: ");
642        pWrite(temp->getT2());
643        Print("LP2 Term: ");
644        pWrite(temp->getLp2Term());
645        Print("LP2 Poly: ");
646        pWrite(temp->getLp2Poly());
647        Print("\n");
648        temp = temp->next;
649    }
650}
651
652void CNode::setLastRuleTested(Rule* r) {
653    data->setLastRuleTested(r);
654}
655
656/*
657====================================
658functions working on the class CList
659====================================
660*/
661// for initialization of CLists, last element alwas has data=NULL and next=NULL
662CList::CList() {
663    first   =   new CNode();
664    last    =   first;
665}
666
667CList::CList(CPair* c) {
668    first   =   new CNode(c);
669    last    =   first;
670}
671
672CList::~CList() {
673    delete first;
674}
675
676// insert sorts the critical pairs firstly by increasing total degree, secondly by increasing label
677// note: as all critical pairs have the same index here, the second sort is done on the terms of the labels
678void CList::insert(CPair* c) {
679    first = first->insert(c, last);
680}
681
682CNode* CList::getFirst() {
683    return first;
684}
685
686// get the first elements from CList which by the above sorting have minimal degree
687// returns the pointer on the first element of those
688CNode* CList::getMinDeg() {
689    CNode* temp     =   first;
690    first           =   first->getMinDeg();
691    return temp;
692}
693
694void CList::print() {
695    first->print();
696}
697
698/*
699====================================
700functions working on the class RNode
701====================================
702*/
703RNode::RNode() {
704    data    =   NULL;
705    next    =   NULL;
706}
707
708RNode::RNode(Rule* r) {
709    data    =   r;
710    next    =   NULL;
711}
712
713RNode::~RNode() {
714    delete  next;
715    delete  data;
716}
717
718RNode* RNode::insert(Rule* r) {
719    RNode* newElement   =   new RNode(r);
720    newElement->next    =   this;
721    return newElement;
722}
723
724RNode* RNode::insert(int i, poly t) {
725    Rule*   r           =   new Rule(i,t);
726    RNode* newElement   =   new RNode(r);
727    newElement->next    =   this;
728    return newElement;
729}
730
731RNode* RNode::getNext() {
732    return next;
733}   
734
735Rule* RNode::getRule() {
736    return data;
737}
738
739int RNode::getRuleIndex() {
740    return data->getIndex();
741}
742
743poly RNode::getRuleTerm() {
744    return data->getTerm();
745}
746
747/*
748====================================
749functions working on the class RList
750====================================
751*/
752RList::RList() {
753    first = new RNode();
754}
755
756RList::RList(Rule* r) {
757    first = new RNode(r);
758}
759
760void RList::insert(int i, poly t) {
761    first = first->insert(i,t);
762}
763
764void RList::insert(Rule* r) {
765    first = first->insert(r);
766}
767
768RNode* RList::getFirst() {
769    return first;
770}
771
772Rule* RList::getRule() {
773    return this->getRule();
774}
775
776/*
777=======================================
778functions working on the class RTagNode
779=======================================
780*/
781
782RTagNode::RTagNode() {
783    data = NULL;
784    next = NULL;
785}
786 
787RTagNode::RTagNode(RNode* r) {
788    data = r;
789    next = NULL;
790}
791       
792RTagNode::RTagNode(RNode* r, RTagNode* n) {
793    data = r;
794    next = n;
795}
796
797 RTagNode::~RTagNode() {
798    delete next;
799    delete data;   
800}
801       
802// declaration with first as parameter due to sorting of RTagList
803RTagNode* RTagNode::insert(RNode* r) {
804    Print("Hier1\n");
805    RTagNode* newElement  = new RTagNode(r, this);
806    Print("Hier2\n");
807    return newElement;
808}
809
810RNode* RTagNode::getRNode() {
811    return this->data;
812}
813
814// NOTE: We insert at the beginning of the list and length = i-1, where i is the actual index.
815//       Thus given actual index i and idx being the index of the LPoly under investigation
816//       the element on position length-idx+1 is the right one
817RNode* RTagNode::get(int idx, int length) {
818    if(idx==1 || idx==0) {
819        // NOTE: We set this NULL as putting it the last element in the list, i.e. the element having
820        //       RNode* = NULL would cost lots of iterations at each step of F5inc, with increasing
821        //       length of the list this should be prevented
822        return NULL;
823    }
824    else {
825        int j;
826        RTagNode* temp = this; 
827    Print("\n\nHIER IN GET IDX\n");
828    Print("FOR LOOP: %d\n",length-idx+1);   
829    for(j=1; j<=length-idx+1; j++) {
830            temp = temp->next;
831        }
832        return temp->data;
833    }
834}
835
836void RTagNode::set(RNode* r) {
837    this->data  =   r;
838}
839
840void RTagNode::print() {
841    RTagNode* temp  =   this;
842    Print("1. element: %d",getRNode()->getRule()->getIndex());
843    pWrite(getRNode()->getRule()->getTerm());
844    temp    =   temp->next;
845    int i   =   2;
846    while(NULL != temp->getRNode()) {
847        Print("%d. element: %d",i,getRNode()->getRule()->getIndex());
848        pWrite(getRNode()->getRule()->getTerm());
849        temp    =   temp->next;
850        i++;
851    }
852}
853/*
854=======================================
855functions working on the class LTagList
856=======================================
857*/
858
859RTagList::RTagList() {
860    RTagNode* first =   new RTagNode();
861    length          =   0;
862}
863
864RTagList::RTagList(RNode* r) {
865    RTagNode* first =   new RTagNode(r);
866    length          =   1;
867}
868
869// declaration with first as parameter in LTagNode due to sorting of LTagList
870void RTagList::insert(RNode* r) {
871    first = first->insert(r);
872    Print("LENGTH:%d\n",length);
873    length = length +1;
874    Print("LENGTH:%d\n",length);
875}
876
877RNode* RTagList::getFirst() {
878    return first->getRNode();
879}
880
881RNode* RTagList::get(int idx) {
882    return first->get(idx, length);
883}
884
885void RTagList::setFirst(RNode* r) {
886    first->set(r);
887}
888
889void RTagList::print() {
890    first->print();
891}
892
893int RTagList::getLength() {
894    return length;
895}
896#endif
Note: See TracBrowser for help on using the repository browser.