source: git/Singular/LIB/nfmodstd.lib @ 257093d

spielwiese
Last change on this file since 257093d was 257093d, checked in by Andreas Steenpass <steenpass@…>, 9 years ago
add: include benchmark problems in nfmodstd.lib
  • Property mode set to 100644
File size: 16.4 KB
Line 
1////////////////////////////////////////////////////////////////////////////////
2version="version nfmodstd.lib 4.0.1.0 Sep_2014 ";  // $Id$
3category="Commutative Algebra";
4info="
5
6LIBRARY:   nfmodstd.lib  Groebner bases of ideals in polynomial rings
7                           over algebraic number fields
8AUTHORS:   D.K. Boku       boku@mathematik.uni-kl.de
9@*         W. Decker       decker@mathematik.uni-kl.de
10@*         C. Fieker       fieker@mathematik.uni-kl.de
11
12OVERVIEW:
13  A library for computing the Groebner basis of an ideal in the polynomial
14  ring over an algebraic number field Q(t) using the modular methods, where t is
15  algebraic over the field of rational numbers Q. For the case Q(t) = Q, the procedure
16  is inspired by Arnold [1]. This idea is then extended
17  to the case t not in Q using factorization as follows:
18  Let f be the minimal polynomial of t.
19  For I, I' ideals in Q(t)[X], Q[X,t]/<f> respectively, we map I to I' via the map sending
20  t to t + <f>.
21  We first choose a prime p such that f has at least two factors in characteristic p and
22  add each factor f_i to I' to obtain the ideal J'_i = I' + <f_i>.
23  We then compute a standard basis G'_i of J'_i for each i and combine the G'_i to G_p
24  (a standard basis of I'_p) using chinese remaindering for polynomials. The procedure is
25  repeated for many primes p, where we compute the G_p in parallel until the
26  number of primes is sufficiently large to recover the correct standard basis G' of I'.
27  Finally, by mapping G' back to Q(t)[X], a standard basis G of I is obtained.
28
29REFERENCES:
30  [1] E. A. Arnold: Modular algorithms for computing Groebner bases.
31      J. Symb. Comp. 35, 403-419 (2003).
32
33PROCEDURES:
34  chinrempoly(l,m);       chinese remaindering for polynomials
35  nfmodStd(I);          standard basis of I over algebraic number field using modular methods
36";
37
38LIB "modstd.lib";
39
40////////////////////////////////////////////////////////////////////////////////
41
42static proc testPrime(int p, ideal I)
43{
44    /*
45     * test whether a prime p divides the denominator(s)
46     * and leading coefficients of generating set of ideal
47     */
48    int i,j;
49    poly f;
50    number num;
51    bigint d1,d2,d3;
52    for(i = 1; i <= size(I); i++)
53    {
54        f = cleardenom(I[i]);
55        if(f == 0)
56        {
57            return(0);
58        }
59        num = leadcoef(I[i])/leadcoef(f);
60        d1 = bigint(numerator(num));
61        d2 = bigint(denominator(num));
62        if( (d1 mod p) == 0)
63        {
64            return(0);
65        }
66        if((d2 mod p) == 0)
67        {
68            return(0);
69        }
70        for(j = size(f); j > 0; j--)
71        {
72            d3 = bigint(leadcoef(f[j]));
73            if( (d3 mod p) == 0)
74            {
75                return(0);
76            }
77        }
78    }
79    return(1);
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/* return 1 if the number of factors are in the required bound , 0 else */
84
85static proc minpolyTask(poly f,int p)
86{
87    /*
88     * bound for irreducible factor(s) of (f mod p)
89     * see testfact()
90     */
91    int nr,k,ur;
92    ur=deg(f);
93    list L=factmodp(f,p);
94    if(degtest(L[2])==1)
95    {
96        // now each factor is squarefree
97        if(ur<=3)
98        {
99            return(1);
100        }
101        else
102        {
103            nr = testfact(ur);
104            k=ncols(L[1]);
105            if(nr < k && k < (ur-nr))// set bound for k
106            {
107                return(1);
108            }
109        }
110    }
111    return(0);
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/* return 1 if both testPrime(p,J) and minpolyTask(f,p) is true, 0 else */
116
117static proc PrimeTestTask(int p, list L)
118{
119    /* L=list(I), I=J,f; J ideal , f minpoly */
120    int sz,nr,dg;
121    ideal J=L[1];
122    sz=ncols(J);
123    poly f=J[sz];
124    dg=deg(f);
125    if(!testPrime(p,J) or !minpolyTask(f,p))
126    {
127        return(0);
128    }
129    return(1);
130}
131
132////////////////////////////////////////////////////////////////////////////////
133/* compute factors of f mod p with multiplicity */
134
135static proc factmodp(poly f, int p)
136{
137    def R=basering;
138    list l=ringlist(R);
139    l[1]=p;
140    def S=ring(l);
141    setring S;
142    list L=factorize(imap(R,f),2);
143    ideal J=L[1];
144    intvec v=L[2];
145    list scx=J,v;
146    setring R;
147    return(imap(S,scx));
148    kill S;
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/* set a bound for number of factors w.r.t degree nr*/
153
154static proc testfact(int nr)
155{
156    // nr must be greater than 3
157    int i;
158    if(nr>3 and nr<=5)
159    {
160        i=1;
161    }
162    if(nr>5 and nr<=10)
163    {
164        i=2;
165    }
166    if(nr>10 and nr<=15)
167    {
168        i=3;
169    }
170    if(nr>15 and nr<=20)
171    {
172        i=4;
173    }
174    if(nr>20 and nr<=25)
175    {
176        i=5;
177    }
178    if(nr>25 and nr<=30)
179    {
180        i=6;
181    }
182    if(nr>30)
183    {
184        i=10;
185    }
186    return(i);
187}
188
189///////////////////////////////////////////////////////////////////////////////
190// return 1 if v[i]>1 , 0 else
191
192static proc degtest(intvec v)
193{
194    for(int j=1;j<=nrows(v);j++)
195    {
196        if(v[j]>1)
197        {
198            return(0);
199        }
200    }
201    return(1);
202}
203
204////////////////////////////////////////////////////////////////////////////////
205
206static proc chinRm(list m, list ll, list lk,list l1,int uz)
207{
208    poly ff,c;
209
210    for(int i=1;i<=uz;i++)
211    {
212        c = division(l1[i]*ll[i],m[i])[2][1];
213        ff = ff + c*lk[i];
214    }
215    return(ff);
216}
217
218////////////////////////////////////////////////////////////////////////////////
219
220proc chinrempoly(list l,list m)
221"USAGE:  chinrempoly(l, m); l list, m list
222RETURN:  a polynomial (resp. ideal) which is congruent to l[i] modulo m[i] for all i
223NOTE: The procedure applies chinese remaindering to the first argument w.r.t. the
224      moduli given in the second. The elements in the first list must be of same type
225      which can be polynomial or ideal. The moduli must be of type polynomial. Elements
226      in the second list must be distinct and co-prime.
227SEE ALSO: chinrem
228EXAMPLE: example chinrempoly; shows an example
229"
230{
231    int i,j,sz,uz;
232    uz = size(l);
233    sz = ncols(ideal(l[1]));
234    poly f=1;
235    for(i=1;i<=uz;i++)
236    {
237        f=f*m[i];
238    }
239
240    ideal I,J;
241    list l1,ll,lk,l2;
242    poly c,ff;
243    for(j=1;j<=uz;j++)
244    {
245        lk[j]=f/m[j];
246        ll[j]=extgcd(lk[j],m[j])[2];
247    }
248
249    for(i=1;i<=sz;i++)
250    {
251        for(j=1;j<=uz;j++)
252        {
253            I = l[j];
254            l1[j] = I[i];
255        }
256        J[i] = chinRm(m,ll,lk,l1,uz);
257    }
258    return(J);
259}
260example
261{ "EXAMPLE:"; echo = 2;
262    ring rr=97,x,dp;
263    poly f=x^7-7*x + 3;
264    ideal J=factorize(f,1);
265    J;
266    list m=J[1..ncols(J)];
267    list l= x^2+2*x+3, x^2+5, x^2+7;
268    ideal I=chinrempoly(l,m);
269    I;
270    ring s=0,x,dp;
271    list m= x^2+2*x+3, x^3+5, x^4+x^3+7;
272    list l=x^3 + 2, x^4 + 7, x^5 + 11;
273    ideal I=chinrempoly(l,m);
274    I;
275    int p=prime(536546513);
276    ring r = p, (x,y,a), (dp(2),dp(1));
277    poly minpolynomial = a^2+1;
278    ideal kf=factorize(minpolynomial,1);//return factors without multiplicity
279    kf;
280    ideal k=(a+1)*x2+y, 3x-ay+ a+2;
281    option(redSB);
282    ideal k1=k,kf[1];
283    ideal k2 =k,kf[2];
284    k1=std(k1);
285    k2=std(k2);
286    list l=k1,k2;
287    list m=kf[1..ncols(kf)];
288    ideal I=chinrempoly(l,m);
289    I=simplify(I,2);
290    I;
291}
292
293////////////////////////////////////////////////////////////////////////////////
294
295static proc check_leadmonom_and_size(list L)
296{
297    /*
298     * compare the size of ideals in the list and
299     * check the corresponding leading monomials
300     * size(L)>=2
301     */
302    ideal J=L[1];
303    int i=size(L);
304    int sc=ncols(J);
305    int j,k;
306    poly g=leadmonom(J[1]);
307    for(j=1;j<=i;j++)
308    {
309        if(ncols(L[j])!=sc)
310        {
311            return(0);
312        }
313    }
314    for(k=2;k<=i;k++)
315    {
316        for(j=1;j<=sc;j++)
317        {
318            if(leadmonom(J[j])!=leadmonom(L[k][j]))
319            {
320                return(0);
321            }
322        }
323    }
324    return(1);
325}
326
327////////////////////////////////////////////////////////////////////////////////
328
329static proc LiftPolyCRT(ideal I)
330{
331    /*
332     * compute std for each factor and combine this result
333     * to modulo minpoly via CRT for poly over char p>0
334     */
335    int u,in,j;
336    list LL,Lk;
337    ideal J,K,II;
338    poly f;
339    u=ncols(I);
340    J=I[1..u-1];
341    f=I[u];
342    K=factorize(f,1);
343    in=ncols(K);
344    for(j=1;j<=in;j++)
345    {
346        LL[j]=K[j];
347        ideal I(j)=J,K[j];
348        I(j)=std(I(j));
349        if(size(I(j))==1)
350        {
351            Lk[j]=I(j);
352        }
353        else
354        {
355            I(j)[1]=0;
356            I(j)=simplify(I(j), 2);
357            Lk[j]=I(j);
358        }
359    }
360    if(check_leadmonom_and_size(Lk))
361    {
362        // apply CRT for polynomials
363        II =chinrempoly(Lk,LL),f;
364    }
365    else
366    {
367        II=0;
368    }
369    return(II);
370}
371
372////////////////////////////////////////////////////////////////////////////////
373
374static proc PtestStd(string command, list args, ideal result, int p)
375{
376    /*
377     * let G be std of I which is not yet known whether it is the correct
378     *  standard basis or not. So this procedure does the first test
379     */
380    def br = basering;
381    list lbr = ringlist(br);
382    if (typeof(lbr[1]) == "int")
383    {
384        lbr[1] = p;
385    }
386    else
387    {
388        lbr[1][1] = p;
389    }
390    def rp = ring(lbr);
391    setring(rp);
392    ideal Ip = fetch(br, args)[1];
393    ideal Gp = fetch(br, result);
394    attrib(Gp, "isSB", 1);
395    int i;
396    for (i = ncols(Ip); i > 0; i--)
397    {
398        if (reduce(Ip[i], Gp, 1) != 0)
399        {
400            setring(br);
401            return(0);
402        }
403    }
404    Ip = LiftPolyCRT(Ip);
405    attrib(Ip,"isSB",1);
406    for (i = ncols(Gp); i > 0; i--)
407    {
408        if (reduce(Gp[i], Ip, 1) != 0)
409        {
410            setring(br);
411            return(0);
412        }
413    }
414    setring(br);
415    return(1);
416}
417
418////////////////////////////////////////////////////////////////////////////////
419
420static proc cleardenomIdeal(ideal I)
421{
422    int t=ncols(I);
423    if(size(I)==0)
424    {
425        return(I);
426    }
427    else
428    {
429        for(int i=1;i<=t;i++)
430        {
431            I[i]=cleardenom(I[i]);
432        }
433    }
434    return(I);
435}
436
437////////////////////////////////////////////////////////////////////////////////
438
439static proc modStdparallelized(ideal I)
440{
441    // apply modular.lib
442    /* save options */
443    intvec opt = option(get);
444    option(redSB);
445    I = modular("Nfmodstd::LiftPolyCRT", list(I), PrimeTestTask, Modstd::deleteUnluckyPrimes_std,
446              PtestStd, Modstd::finalTest_std,536870909);
447    attrib(I, "isSB", 1);
448    option(set,opt);
449    return(I);
450}
451
452////////////////////////////////////////////////////////////////////////////////
453/* main procedure */
454proc nfmodStd(ideal I, list #)
455"USAGE:  nfmodStd(I, #); I ideal, # optional parameters
456RETURN:  standard basis of I over algebraic number field
457NOTE: The procedure passes to @ref{modStd} if the ground field has no
458      parameter. In this case, the optional parameters # (if given)
459      are directly passed to @ref{modStd}.
460SEE ALSO: modStd
461EXAMPLE: example nfmodStd; shows an example
462"
463{
464    list L=#;
465    def Rbs=basering;
466    poly f;
467    ideal J;
468    int n=nvars(Rbs);
469    if(size(I)==0)
470    {
471        return(ideal(0));
472    }
473    if(npars(Rbs)==0)
474    {
475        J=modStd(I,L);//if algebraic number is in Q
476        if(size(#)>0)
477        {
478            return(cleardenomIdeal(J));
479        }
480        return(J);
481    }
482    list rl=ringlist(Rbs);
483    f=rl[1][4][1];
484    rl[2][n+1]=rl[1][2][1];
485    rl[1]=rl[1][1];
486    rl[3][size(rl[3])+1]=rl[3][size(rl[3])];
487    rl[3][size(rl[3])-1]=list("dp",1);
488    def S=ring(rl);
489    setring S;
490    poly f=imap(Rbs,f);
491    ideal I=imap(Rbs,I);
492    I = simplify(I,2);// eraze the zero generatos
493    ideal J;
494    if(f==0)
495    {
496        ERROR("minpoly must be non-zero");
497    }
498    I=I,f;
499    J=modStdparallelized(I);
500    setring Rbs;
501    J=imap(S,J);
502    J=simplify(J,2);
503    if(size(#)>0)
504    {
505        return(cleardenomIdeal(J));
506    }
507    return(J);
508}
509example
510{ "EXAMPLE:"; echo = 2;
511    ring r1 =(0,a),(x,y),dp;
512    minpoly =a^2+1;
513    ideal k=(a/2+1)*x^2+2/3y, 3*x-a*y+ a/7+2;
514    ideal I=nfmodStd(k);
515    I;
516    ring r2 =(0,a),(x,y,z),dp;
517    minpoly =a^3 +2;
518    ideal k=(a^2+a/2)*x^2+(a^2 -2/3*a)*yz, (3*a^2+1)*zx-(a+4/7)*y+ a+2/5;
519    ideal IJ=nfmodStd(k);
520    IJ;
521    ring r3=0,(x,y),dp;// ring without parameter
522    ideal I = x2 + y, xy - 7y + 2x;
523    I=nfmodStd(I);
524    I;
525}
526
527//////////////////////////////////////////////////////////////////////////////
528
529/*
530Benchmark Problems from
531
532Boku, Decker, Fieker, Steenpass: Groebner Bases over Algebraic Number
533Fields.
534
535// 1
536ring R = (0,a), (x,y,z), dp;
537minpoly = (a^2+1);
538poly f1 = (a+8)*x^2*y^2+5*x*y^3+(-a+3)*x^3*z
539          +x^2*y*z;
540poly f2 = x^5+2*y^3*z^2+13*y^2*z^3+5*y*z^4;
541poly f3 = 8*x^3+(a+12)*y^3+x*z^2+3;
542poly f4 = (-a+7)*x^2*y^4+y^3*z^3+18*y^3*z^2;
543ideal I1 = f1,f2,f3,f4;
544
545// 2
546ring R = (0,a), (x,y,z), dp;
547minpoly = (a^5+a^2+2);
548poly f1 = 2*x*y^4*z^2+(a-1)*x^2*y^3*z
549          +(2*a)*x*y*z^2+7*y^3+(7*a+1);
550poly f2 = 2*x^2*y^4*z+(a)*x^2*y*z^2-x*y^2*z^2
551          +(2*a+3)*x^2*y*z-12*x+(12*a)*y;
552poly f3 = (2*a)*y^5*z+x^2*y^2*z-x*y^3*z
553          +(-a)*x*y^3+y^4+2*y^2*z;
554poly f4 = (3*a)*x*y^4*z^3+(a+1)*x^2*y^2*z
555          -x*y^3*z+4*y^3*z^2+(3*a)*x*y*z^3
556          +4*z^2-x+(a)*y;
557ideal I2 = f1,f2,f3,f4;
558
559// 3a
560ring R = (0,a), (v,w,x,y,z), dp;
561minpoly = (a^7-7*a+3);
562poly f1 = (a)*v+(a-1)*w+x+(a+2)*y+z;
563poly f2 = v*w+(a-1)*w*x+(a+2)*v*y+x*y+(a)*y*z;
564poly f3 = (a)*v*w*x+(a+5)*w*x*y+(a)*v*w*z
565          +(a+2)*v*y*z+(a)*x*y*z;
566poly f4 = (a-11)*v*w*x*y+(a+5)*v*w*x*z
567          +(a)*v*w*y*z+(a)*v*x*y*z
568          +(a)*w*x*y*z;
569poly f5 = (a+3)*v*w*x*y*z+(a+23);
570ideal I3a = f1,f2,f3,f4,f5;
571
572// 3b
573ring R = (0,a), (u,v,w,x,y,z), dp;
574minpoly = (a^7-7*a+3);
575poly f1 = (a)*u+(a+2)*v+w+x+y+z;
576poly f2 = u*v+v*w+w*x+x*y+(a+3)*u*z+y*z;
577poly f3 = u*v*w+v*w*x+(a+1)*w*x*y+u*v*z+u*y*z
578          +x*y*z;
579poly f4 = (a-1)*u*v*w*x+v*w*x*y+u*v*w*z
580          +u*v*y*z+u*x*y*z+w*x*y*z;
581poly f5 = u*v*w*x*y+(a+1)*u*v*w*x*z+u*v*w*y*z
582          +u*v*x*y*z+u*w*x*y*z+v*w*x*y*z;
583poly f6 = u*v*w*x*y*z+(-a+2);
584ideal I3b = f1,f2,f3,f4,f5,f6;
585
586// 4
587ring R = (0,a), (w,x,y,z), dp;
588minpoly = (a^6+a^5+a^4+a^3+a^2+a+1);
589poly f1 = (a+5)*w^3*x^2*y+(a-3)*w^2*x^3*y
590          +(a+7)*w*x^2*y^2;
591poly f2 = (a)*w^5+(a+3)*w*x^2*y^2
592          +(a^2+11)*x^2*y^2*z;
593poly f3 = (a+7)*w^3+12*x^3+4*w*x*y+(a)*z^3;
594poly f4 = 3*w^3+(a-4)*x^3+x*y^2;
595ideal I4 = f1,f2,f3,f4;
596
597// 5
598ring R = (0,a), (w,x,y,z), dp;
599minpoly = (a^12-5*a^11+24*a^10-115*a^9+551*a^8
600          -2640*a^7+12649*a^6-2640*a^5+551*a^4
601          -115*a^3+24*a^2-5*a+1);
602poly f1 = (2*a+3)*w*x^4*y^2+(a+1)*w^2*x^3*y*z
603          +2*w*x*y^2*z^3+(7*a-1)*x^3*z^4;
604poly f2 = 2*w^2*x^4*y+w^2*x*y^2*z^2
605          +(-a)*w*x^2*y^2*z^2
606          +(a+11)*w^2*x*y*z^3-12*w*z^6
607          +12*x*z^6;
608poly f3 = 2*x^5*y+w^2*x^2*y*z-w*x^3*y*z
609          -w*x^3*z^2+(a)*x^4*z^2+2*x^2*y*z^3;
610poly f4 = 3*w*x^4*y^3+w^2*x^2*y*z^3
611          -w*x^3*y*z^3+(a+4)*x^3*y^2*z^3
612          +3*w*x*y^3*z^3+(4*a)*y^2*z^6-w*z^7
613          +x*z^7;
614ideal I5 = f1,f2,f3,f4;
615
616// 6
617ring R = (0,a), (u,v,w,x,y,z), dp;
618minpoly = (a^2+5*a+1);
619poly f1 = u+v+w+x+y+z+(a);
620poly f2 = u*v+v*w+w*x+x*y+y*z+(a)*u+(a)*z;
621poly f3 = u*v*w+v*w*x+w*x*y+x*y*z+(a)*u*v
622          +(a)*u*z+(a)*y*z;
623poly f4 = u*v*w*x+v*w*x*y+w*x*y*z+(a)*u*v*w
624          +(a)*u*v*z+(a)*u*y*z+(a)*x*y*z;
625poly f5 = u*v*w*x*y+v*w*x*y*z+(a)*u*v*w*x
626          +(a)*u*v*w*z+(a)*u*v*y*z+(a)*u*x*y*z
627          +(a)*w*x*y*z;
628poly f6 = u*v*w*x*y*z+(a)*u*v*w*x*y
629          +(a)*u*v*w*x*z+(a)*u*v*w*y*z
630          +(a)*u*v*x*y*z+(a)*u*w*x*y*z
631          +(a)*v*w*x*y*z;
632poly f7 = (a)*u*v*w*x*y*z-1;
633ideal I6 = f1,f2,f3,f4,f5,f6,f7;
634
635// 7
636ring R = (0,a), (w,x,y,z), dp;
637minpoly = (a^8-16*a^7+19*a^6-a^5-5*a^4+13*a^3
638          -9*a^2+13*a+17);
639poly f1 = (-a^2-1)*x^2*y+2*w*x*z-2*w
640          +(a^2+1)*y;
641poly f2 = (a^3-a-3)*w^3*y+4*w*x^2*y+4*w^2*x*z
642          +2*x^3*z+(a)*w^2-10*x^2+4*w*y-10*x*z
643          +(2*a^2+a);
644poly f3 = (a^2+a+11)*x*y*z+w*z^2-w-2*y;
645poly f4 = -w*y^3+4*x*y^2*z+4*w*y*z^2+2*x*z^3
646          +(2*a^3+a^2)*w*y+4*y^2-10*x*z-10*z^2
647          +(3*a^2+5);
648ideal I7 = f1,f2,f3,f4;
649
650// 8
651ring R = (0,a), (t,u,v,w,x,y,z), dp;
652minpoly = (a^7+10*a^5+5*a^3+10*a+1);
653poly f1 = v*x+w*y-x*z-w-y;
654poly f2 = v*w-u*x+x*y-w*z+v+x+z;
655poly f3 = t*w-w^2+x^2-t;
656poly f4 = (-a)*v^2-u*y+y^2-v*z-z^2+u;
657poly f5 = t*v+v*w+(-a^2-a-5)*x*y-t*z+w*z+v+x+z
658          +(a+1);
659poly f6 = t*u+u*w+(-a-11)*v*x-t*y+w*y-x*z-t-u
660          +w+y;
661poly f7 = w^2*y^3-w*x*y^3+x^2*y^3+w^2*y^2*z
662          -w*x*y^2*z+x^2*y^2*z+w^2*y*z^2
663          -w*x*y*z^2+x^2*y*z^2+w^2*z^3-w*x*z^3
664          +x^2*z^3;
665poly f8 = t^2*u^3+t^2*u^2*v+t^2*u*v^2+t^2*v^3
666          -t*u^3*x-t*u^2*v*x-t*u*v^2*x-t*v^3*x
667          +u^3*x^2+u^2*v*x^2+u*v^2*x^2
668          +v^3*x^2;
669ideal I8 = f1,f2,f3,f4,f5,f6,f7,f8;
670*/
Note: See TracBrowser for help on using the repository browser.