source: git/kernel/f5gb.cc @ 8978fd

spielwiese
Last change on this file since 8978fd was 8978fd, checked in by Christian Eder, 15 years ago
criterion2() for spols() added git-svn-id: file:///usr/local/Singular/svn/trunk@11349 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: f5gb.cc,v 1.22 2009-02-04 19:27:11 ederc Exp $ */
5/*
6* ABSTRACT: f5gb interface
7*/
8#include "mod2.h"
9#ifdef HAVE_F5
10#include "kutil.h"
11#include "structs.h"
12#include "omalloc.h"
13#include "polys.h"
14#include "p_polys.h"
15#include "ideals.h"
16#include "febase.h"
17#include "kstd1.h"
18#include "khstd.h"
19#include "kbuckets.h"
20#include "weight.h"
21#include "intvec.h"
22#include "pInline1.h"
23#include "f5gb.h"
24#include "f5data.h"
25#include "f5lists.h"
26
27
28/*
29====================================================================
30sorting ideals by decreasing total degree "left" and "right" are the
31pointer of the first and last polynomial in the considered ideal
32====================================================================
33*/
34void qsortDegree(poly* left, poly* right) {
35    poly* ptr1 = left;
36    poly* ptr2 = right;
37    poly p1,p2;
38    p2 = *(left + (right - left >> 1));
39    do {
40            while(pTotaldegree(*ptr1, currRing) < pTotaldegree(p2, currRing)) {
41                    ptr1++;
42            } 
43            while(pTotaldegree(*ptr2, currRing) > pTotaldegree(p2,currRing)) {
44                    ptr2--;
45            }
46            if(ptr1 > ptr2) {
47                    break;
48            }
49            p1    = *ptr1;
50            *ptr1 = *ptr2;
51            *ptr2 = p1;
52    } while(++ptr1 <= --ptr2);
53
54    if(left < ptr2) {
55            qsortDegree(left,ptr2);
56    }
57    if(ptr1 < right) {
58            qsortDegree(ptr1,right);
59    }
60}
61
62
63/*
64==================================================
65computes incrementally gbs of subsets of the input
66gb{f_m} -> gb{f_m,f_(m-1)} -> gb{f_m,...,f_1}
67==================================================
68*/
69LList* F5inc(int* i, poly* f_i, LList* gPrev, poly* ONE) {
70    // tag the first element of index i-1 for criterion 1
71    LTagList* lTag  =   new LTagList(gPrev->getFirst());
72    // first element in rTag is NULL, this must be done due to possible later improvements
73    RTagList* rTag  =   new RTagList();
74    gPrev->insert(ONE,i,f_i);
75    CList* critPairs    =   new CList();
76    critPairs           =   criticalPair(gPrev, critPairs, lTag, rTag);
77   
78    return gPrev;
79}
80
81
82/*
83================================================================
84computes a list of critical pairs for the next reduction process
85first element in gPrev is always the newest element which must
86build critical pairs with all other elements in gPrev
87================================================================
88*/
89CList* criticalPair(LList* gPrev, CList* critPairs, LTagList* lTag, RTagList* rTag) {
90    // initialization for usage in pLcm()
91    number nOne         =   nInit(1);
92    LNode* first        =   gPrev->getFirst();
93    LNode* l            =   gPrev->getFirst()->getNext();
94    poly* u1            =   new poly(pInit());
95    poly* u2            =   new poly(pInit());
96    poly* lcm           =   new poly(pInit());
97    // computation of critical pairs
98    while( NULL != l) {
99        //pWrite( *(gPrev->getFirst()->getPoly()) );
100        //pWrite( *(l->getPoly()) );
101        pLcm(first->getPoly(), l->getPoly(), *lcm);
102        pSetCoeff(*lcm,nOne);
103        // computing factors u1 & u2 for new labels
104        *u1 = pDivide(*lcm,pHead(first->getPoly()));
105        pSetCoeff(*u1,nOne);
106        *u2 = pDivide(*lcm, pHead(l->getPoly()));
107        pSetCoeff(*u2,nOne);
108        pWrite(*u2);
109        // testing both new labels by the F5 Criterion
110        if(!criterion1(u1, first, lTag) && !criterion1(u2, l, lTag) && 
111           !criterion2(u1, first, rTag) && !criterion2(u2, l, rTag)) {
112            Print("Criteria passed\n");
113            // if they pass the test, add them to CList critPairs, having the LPoly with greater
114            // label as first element in the CPair
115            if(first->getIndex() == l->getIndex() && 
116            pMult(*u1, first->getTerm()) < pMult(*u2, l->getTerm())) {
117                CPair* cp   =   new CPair(pDeg(ppMult_qq(*u2,pHead(l->getPoly()))), *u2, 
118                                l->getLPoly(), *u1, first->getLPoly());                   
119                    critPairs->insert(cp);
120            }
121            else {
122                CPair* cp   =   new CPair(pDeg(ppMult_qq(*u2,pHead(l->getPoly()))), *u1, 
123                                first->getLPoly(), *u2, l->getLPoly());                   
124                    critPairs->insert(cp);
125            }
126        }
127        else {
128            Print("Criteria not passed\n");
129        }
130       
131        // for debugging
132        if(NULL != critPairs) {
133            critPairs->print(); 
134        }
135        l   =   l->getNext();
136    }
137    Print("ENDE CRITPAIRS\n");
138    return critPairs;
139}
140
141/*
142========================================
143Criterion 1, i.e. Faugere's F5 Criterion
144========================================
145*/
146bool criterion1(poly* t, LNode* l, LTagList* lTag) {
147    // starts at the first element in gPrev with index = (index of l)-1, these tags are saved in lTag
148    LNode* testNode =   lTag->get(l->getIndex());
149    // save the monom t1*label_term(l) as it is tested various times in the following
150    poly u1 = ppMult_qq(*t,l->getTerm());
151    while(NULL != testNode) {
152        if(pLmDivisibleByNoComp(pHead(testNode->getPoly()),u1)) {
153            return true;
154        }
155        testNode    =   testNode->getNext();
156    }
157    return false;
158}
159
160/*
161=====================================
162Criterion 2, i.e. Rewritten Criterion
163=====================================
164*/
165bool criterion2(poly* t, LNode* l, RTagList* rTag) {
166    // start at the previously added element to gPrev, as all other elements will have the same index for sure
167    RNode* testNode =   rTag->get(l->getIndex());
168    // save the monom t1*label_term(l) as it is tested various times in the following
169    poly u1 = ppMult_qq(*t,l->getTerm());
170    // first element added to rTag was NULL, check for this
171    Print("Hier1\n");
172    while(NULL != testNode && testNode->getRule()->getOrigin() != l->getLPoly()) {
173        Print("Hier2\n");
174        if(pLmDivisibleByNoComp(ppMult_qq(*t,l->getTerm()),testNode->getRuleTerm())) {
175            return true;
176        }
177        testNode    =   testNode->getNext();
178    }
179    return false;
180}
181
182/*
183==========================================================================================================
184Criterion 2, i.e. Rewritten Criterion, for its second call in sPols(), with added lastRuleTested parameter
185==========================================================================================================
186*/
187bool criterion2(poly* t, LNode* l, RTagList* rTag, Rule* lastRuleTested) {
188    // start at the previously added element to gPrev, as all other elements will have the same index for sure
189    RNode* testNode =   rTag->getFirst();
190    // save the monom t1*label_term(l) as it is tested various times in the following
191    poly u1 = ppMult_qq(*t,l->getTerm());
192    // first element added to rTag was NULL, check for this
193    Print("Hier1\n");
194    while(NULL != testNode && testNode->getRule()->getOrigin() != l->getLPoly()) {
195        Print("Hier2\n");
196        if(pLmDivisibleByNoComp(ppMult_qq(*t,l->getTerm()),testNode->getRuleTerm())) {
197            return true;
198        }
199        testNode    =   testNode->getNext();
200    }
201    return false;
202}
203
204/*
205==========================================================================
206MAIN:computes a gb of the ideal i in the ring r with our F5 implementation
207==========================================================================
208*/
209ideal F5main(ideal id, ring r) {
210    int i,j;
211    // 1 polynomial for defining initial labels & further tests
212    poly ONE = pOne();
213    // list of rules
214    RList* rules    =   new RList();
215    i = 1;
216    LList* gPrev    =   new LList( &ONE, &i, &id->m[0]);
217    poly* lcm = new poly;
218    // initialization for usage in pLcm()
219    *lcm = pOne();
220    pWrite(*lcm); 
221    // definition of one-polynomial as global constant ONE
222    //poly one = pInit();
223    //pSetCoeff(one, nInit(1));
224    //static poly ONE = one;
225   
226    for(j=0; j<IDELEMS(id); j++) {
227        if(NULL != id->m[j]) { 
228            if(pComparePolys(id->m[j],ONE)) {
229                Print("One Polynomial in Input => Computations stopped\n");
230                ideal idNew = idInit(1,1);
231                idNew->m[0] = ONE;
232                return(idNew);
233            }   
234        }
235    } 
236    pLcm( id->m[0], id->m[1], *lcm);
237    Print("LCM NEU\n");
238    //*lcm = pHead(*lcm);
239    //pWrite(*lcm);
240    Print("\n\n"); 
241    id = kInterRed(id,0); 
242    qsortDegree(&id->m[0],&id->m[IDELEMS(id)-1]);
243    idShow(id);
244    poly p = pHead(id->m[0]);
245    pWrite(p);
246    poly q = pHead(id->m[2]);
247    pWrite(q);
248    for(i=2; i<=IDELEMS(id); i++) {
249        gPrev   =   F5inc(&i, &id->m[i-1], gPrev, &ONE);
250        Print("JA\n");
251    } 
252    // only for debugging
253    //LNode* current;
254    //LList* g_curr = new LList(lp);
255    //}
256    //for(i=2; i<IDELEMS(id); i++) {
257        //g_curr = F5inc(&i,&id->m[i],g_prev);
258        //if(g_curr->polyTest(&ONE)) {
259        //    Print("One Polynomial in Input => Computations stopped\n");
260         //   ideal id_new = idInit(1,1);
261        //    id_new->m[0] = ONE;
262        //    return(id_new);               
263        //}
264    //}
265    return(id);
266
267
268}
269
270#endif
Note: See TracBrowser for help on using the repository browser.