[BACK]Return to Hgfs.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / engine

Annotation of OpenXM_contrib2/asir2000/engine/Hgfs.c, Revision 1.18

1.18    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Hgfs.c,v 1.17 2001/09/03 07:01:06 noro Exp $ */
1.1       noro        2:
                      3: #include "ca.h"
                      4:
1.18    ! noro        5: void lnfsf(int n,UM p0,UM p1,struct p_pair *list,UM np0,UM np1);
1.1       noro        6:
1.18    ! noro        7: int comp_dum(DUM a,DUM b)
1.12      noro        8: {
                      9:        if ( DEG(a->f) > DEG(b->f) )
                     10:                return -1;
                     11:        else if ( DEG(a->f) < DEG(b->f) )
                     12:                return 1;
                     13:        else
                     14:                return 0;
                     15: }
                     16:
1.18    ! noro       17: void fctrsf(P p,DCP *dcp)
1.1       noro       18: {
                     19:        int n,i,j,k;
                     20:        DCP dc,dc0;
                     21:        P lc;
                     22:        UM mp;
                     23:        UM *tl;
1.18    ! noro       24:        Obj obj;
1.1       noro       25:        struct oDUM *udc,*udc1;
                     26:
1.18    ! noro       27:        simp_ff((Obj)p,&obj); p = (P)obj;
1.1       noro       28:        if ( !p ) {
                     29:                *dcp = 0; return;
                     30:        }
                     31:        mp = W_UMALLOC(UDEG(p));
                     32:        ptosfum(p,mp);
                     33:        if ( (n = DEG(mp)) < 0 ) {
                     34:                *dcp = 0; return;
                     35:        } else if ( n == 0 ) {
                     36:                NEWDC(dc); COEF(dc) = p; DEG(dc) = ONE;
                     37:                NEXT(dc) = 0; *dcp = dc;
                     38:                return;
                     39:        }
                     40:        lc = COEF(DC(p));
                     41:        if ( !_isonesf(COEF(mp)[n]) ) {
                     42:                monicsfum(mp);
                     43:        }
                     44:
                     45:        W_CALLOC(n+1,struct oDUM,udc);
                     46:        gensqfrsfum(mp,udc);
                     47:
                     48:        tl = (UM *)ALLOCA((n+1)*sizeof(UM));
                     49:        W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
                     50:
                     51:        for ( i = 0,j = 0; udc[i].f; i++ )
                     52:                if ( DEG(udc[i].f) == 1 ) {
                     53:                        udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
                     54:                } else {
                     55:                        bzero((char *)tl,(n+1)*sizeof(UM));
                     56:                        czsfum(udc[i].f,tl);
                     57:                        for ( k = 0; tl[k]; k++, j++ ) {
                     58:                                udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
                     59:                        }
                     60:                }
                     61:        udc = udc1;
1.12      noro       62:        for ( i = 0; udc[i].f; i++ );
                     63:        qsort(udc,i,sizeof(struct oDUM),
                     64:                (int (*)(const void *,const void *))comp_dum);
                     65:
1.1       noro       66:        NEWDC(dc0); COEF(dc0) = lc; DEG(dc0) = ONE; dc = dc0;
                     67:        for ( n = 0; udc[n].f; n++ ) {
                     68:                NEWDC(NEXT(dc)); dc = NEXT(dc);
                     69:                STOQ(udc[n].n,DEG(dc)); sfumtop(VR(p),udc[n].f,&COEF(dc));
                     70:        }
                     71:        NEXT(dc) = 0; *dcp = dc0;
                     72: }
                     73:
1.18    ! noro       74: void gensqfrsfum(UM p,struct oDUM *dc)
1.1       noro       75: {
                     76:        int n,i,j,d,mod;
                     77:        UM t,s,g,f,f1,b;
                     78:
                     79:        if ( (n = DEG(p)) == 1 ) {
                     80:                dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
                     81:                return;
                     82:        }
                     83:        t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
                     84:        f = W_UMALLOC(n); f1 = W_UMALLOC(n); b = W_UMALLOC(n);
                     85:        diffsfum(p,t); cpyum(p,s); gcdsfum(t,s,g);
                     86:        if ( !DEG(g) ) {
                     87:                dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
                     88:                return;
                     89:        }
                     90:        cpyum(p,b); cpyum(p,t); divsfum(t,g,f);
                     91:        for ( i = 0, d = 0; DEG(f); i++ ) {
                     92:                while ( 1 ) {
                     93:                        cpyum(b,t);
                     94:                        if ( divsfum(t,f,s) >= 0 )
                     95:                                break;
                     96:                        else {
                     97:                                cpyum(s,b); d++;
                     98:                        }
                     99:                }
                    100:                cpyum(b,t); cpyum(f,s); gcdsfum(t,s,f1);
                    101:                divsfum(f,f1,s); cpyum(f1,f);
                    102:                dc[i].f = UMALLOC(DEG(s)); cpyum(s,dc[i].f); dc[i].n = d;
                    103:        }
                    104:        mod = characteristic_sf();
                    105:        if ( DEG(b) > 0 ) {
                    106:                d = 1;
                    107:                while ( 1 ) {
                    108:                        cpyum(b,t);
                    109:                        for ( j = DEG(t); j >= 0; j-- )
                    110:                                if ( COEF(t)[j] && (j % mod) )
                    111:                                        break;
                    112:                        if ( j >= 0 )
                    113:                                break;
                    114:                        else {
                    115:                                DEG(s) = DEG(t)/mod;
                    116:                                for ( j = 0; j <= DEG(t); j++ )
                    117:                                        COEF(s)[j] = COEF(t)[j*mod];
                    118:                                cpyum(s,b); d *= mod;
                    119:                        }
                    120:                }
                    121:                gensqfrsfum(b,dc+i);
                    122:                for ( j = i; dc[j].f; j++ )
                    123:                        dc[j].n *= d;
                    124:        }
                    125: }
                    126:
1.18    ! noro      127: void randsfum(int d,UM p)
1.1       noro      128: {
                    129:        int i;
                    130:
1.2       noro      131:        for ( i = 0; i < d; i++ )
1.1       noro      132:                COEF(p)[i] = _randomsf();
1.2       noro      133:        for ( i = d-1; i >= 0 && !COEF(p)[i]; i-- );
                    134:        p->d = i;
1.1       noro      135: }
                    136:
1.18    ! noro      137: void pwrmodsfum(UM p,int e,UM f,UM pr)
1.1       noro      138: {
                    139:        UM wt,ws,q;
                    140:
                    141:        if ( e == 0 ) {
                    142:                DEG(pr) = 0; COEF(pr)[0] = _onesf();
                    143:        } else if ( DEG(p) < 0 )
                    144:                DEG(pr) = -1;
                    145:        else if ( e == 1 ) {
                    146:                q = W_UMALLOC(DEG(p)); cpyum(p,pr);
                    147:                DEG(pr) = divsfum(pr,f,q);
                    148:        } else if ( DEG(p) == 0 ) {
                    149:                DEG(pr) = 0; COEF(pr)[0] = _pwrsf(COEF(p)[0],e);
                    150:        } else {
                    151:                wt = W_UMALLOC(2*DEG(f)); ws = W_UMALLOC(2*DEG(f));
                    152:                q = W_UMALLOC(2*DEG(f));
                    153:                pwrmodsfum(p,e/2,f,wt);
                    154:                if ( !(e%2) )  {
                    155:                        mulsfum(wt,wt,pr); DEG(pr) = divsfum(pr,f,q);
                    156:                } else {
                    157:                        mulsfum(wt,wt,ws);
                    158:                        DEG(ws) = divsfum(ws,f,q);
                    159:                        mulsfum(ws,p,pr);
                    160:                        DEG(pr) = divsfum(pr,f,q);
                    161:                }
                    162:        }
                    163: }
                    164:
1.18    ! noro      165: void spwrsfum(UM m,UM f,N e,UM r)
1.1       noro      166: {
                    167:        UM t,s,q;
                    168:        N e1;
                    169:        int a;
                    170:
                    171:        if ( !e ) {
                    172:                DEG(r) = 0; COEF(r)[0] = _onesf();
                    173:        } else if ( UNIN(e) )
                    174:                cpyum(f,r);
                    175:        else {
                    176:                a = divin(e,2,&e1);
1.2       noro      177:                t = W_UMALLOC(2*DEG(m)); spwrsfum(m,f,e1,t);
1.1       noro      178:                s = W_UMALLOC(2*DEG(m)); q = W_UMALLOC(2*DEG(m));
                    179:                mulsfum(t,t,s); DEG(s) = divsfum(s,m,q);
                    180:                if ( a ) {
                    181:                        mulsfum(s,f,t); DEG(t) = divsfum(t,m,q); cpyum(t,r);
                    182:         } else
                    183:                        cpyum(s,r);
                    184:        }
                    185: }
                    186:
1.18    ! noro      187: void tracemodsfum(UM m,UM f,int e,UM r)
1.2       noro      188: {
                    189:        UM t,s,q,u;
                    190:        int i;
                    191:
                    192:        q = W_UMALLOC(2*DEG(m)+DEG(f)); /* XXX */
                    193:        t = W_UMALLOC(2*DEG(m));
                    194:        s = W_UMALLOC(2*DEG(m));
                    195:        u = W_UMALLOC(2*DEG(m));
                    196:        DEG(f) = divsfum(f,m,q);
                    197:        cpyum(f,s);
                    198:        cpyum(f,t);
                    199:        for ( i = 1; i < e; i++ ) {
                    200:                mulsfum(t,t,u);
                    201:                DEG(u) = divsfum(u,m,q); cpyum(u,t);
                    202:                addsfum(t,s,u); cpyum(u,s);
                    203:        }
                    204:        cpyum(s,r);
                    205: }
                    206:
1.18    ! noro      207: void make_qmatsf(UM p,UM *tab,int ***mp)
1.1       noro      208: {
                    209:        int n,i,j;
                    210:        int *c;
                    211:        UM q,r;
                    212:        int **mat;
                    213:        int one;
                    214:
                    215:        n = DEG(p);
                    216:        *mp = mat = almat(n,n);
                    217:        for ( j = 0; j < n; j++ ) {
                    218:                r = W_UMALLOC(DEG(tab[j])); q = W_UMALLOC(DEG(tab[j]));
                    219:                cpyum(tab[j],r); DEG(r) = divsfum(r,p,q);
                    220:                for ( i = 0, c = COEF(r); i <= DEG(r); i++ )
                    221:                        mat[i][j] = c[i];
                    222:        }
                    223:        one = _onesf();
                    224:        for ( i = 0; i < n; i++ )
                    225:                mat[i][i] = _subsf(mat[i][i],one);
                    226: }
                    227:
1.18    ! noro      228: void nullsf(int **mat,int n,int *ind)
1.1       noro      229: {
                    230:        int i,j,l,s,h,inv;
                    231:        int *t,*u;
                    232:
                    233:        bzero((char *)ind,n*sizeof(int));
                    234:        ind[0] = 0;
                    235:        for ( i = j = 0; j < n; i++, j++ ) {
                    236:                for ( ; j < n; j++ ) {
                    237:                        for ( l = i; l < n; l++ )
                    238:                                if ( mat[l][j] )
                    239:                                        break;
                    240:                        if ( l < n ) {
                    241:                                t = mat[i]; mat[i] = mat[l]; mat[l] = t; break;
                    242:                        } else
                    243:                                ind[j] = 1;
                    244:                }
                    245:                if ( j == n )
                    246:                        break;
                    247:                inv = _invsf(mat[i][j]);
                    248:                for ( s = j, t = mat[i]; s < n; s++ )
                    249:                        t[s] = _mulsf(t[s],inv);
                    250:                for ( l = 0; l < n; l++ ) {
                    251:                        if ( l == i )
                    252:                                continue;
                    253:                        u = mat[l]; h = _chsgnsf(u[j]);
                    254:                        for ( s = j; s < n; s++ )
                    255:                                u[s] = _addsf(_mulsf(h,t[s]),u[s]);
                    256:                }
                    257:        }
                    258: }
                    259:
1.18    ! noro      260: void null_to_solsf(int **mat,int *ind,int n,UM *r)
1.1       noro      261: {
                    262:        int i,j,k,l;
                    263:        int *c;
                    264:        UM w;
                    265:
                    266:        for ( i = 0, l = 0; i < n; i++ ) {
                    267:                if ( !ind[i] )
                    268:                        continue;
                    269:                w = UMALLOC(n);
                    270:                for ( j = k = 0, c = COEF(w); j < n; j++ )
                    271:                        if ( ind[j] )
                    272:                                c[j] = 0;
                    273:                        else
                    274:                                c[j] = mat[k++][i];
                    275:                c[i] = _chsgnsf(_onesf());
                    276:                for ( j = n; j >= 0; j-- )
                    277:                        if ( c[j] )
                    278:                                break;
                    279:                DEG(w) = j;
                    280:                r[l++] = w;
                    281:        }
                    282: }
                    283: /*
                    284: make_qmatsf(p,tab,mp)
                    285: nullsf(mat,n,ind)
                    286: null_to_solsf(ind,n,r)
                    287: */
                    288:
1.18    ! noro      289: void czsfum(UM f,UM *r)
1.1       noro      290: {
                    291:        int i,j;
                    292:        int d,n,ord;
                    293:        UM s,t,u,v,w,g,x,m,q;
                    294:        UM *base;
                    295:
                    296:        n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM));
                    297:        bzero((char *)base,n*sizeof(UM));
                    298:
                    299:        w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
                    300:
                    301:        base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = _onesf();
                    302:
                    303:        t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = _onesf();
                    304:
                    305:        ord = field_order_sf();
                    306:        pwrmodsfum(t,ord,f,w);
                    307:        base[1] = W_UMALLOC(DEG(w));
                    308:        cpyum(w,base[1]);
                    309:
                    310:        for ( i = 2; i < n; i++ ) {
                    311:                mulsfum(base[i-1],base[1],m);
                    312:                DEG(m) = divsfum(m,f,q);
                    313:                base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
                    314:        }
                    315:
                    316:        v = W_UMALLOC(n); cpyum(f,v);
                    317:        DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = _onesf();
                    318:        x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = _onesf();
                    319:        t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
                    320:
                    321:        for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
                    322:                for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
                    323:                        if ( COEF(w)[i] ) {
                    324:                                mulssfum(base[i],COEF(w)[i],s);
                    325:                                addsfum(s,t,u); cpyum(u,t);
                    326:                        }
                    327:                cpyum(t,w); cpyum(v,s); subsfum(w,x,t);
                    328:                gcdsfum(s,t,g);
                    329:                if ( DEG(g) >= 1 ) {
                    330:                        berlekampsf(g,d,base,r+j); j += DEG(g)/d;
                    331:                        divsfum(v,g,q); cpyum(q,v);
                    332:                        DEG(w) = divsfum(w,v,q);
                    333:                        for ( i = 0; i < DEG(v); i++ )
                    334:                                DEG(base[i]) = divsfum(base[i],v,q);
                    335:                }
                    336:        }
                    337:        if ( DEG(v) ) {
                    338:                r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
                    339:        }
                    340:        r[j] = 0;
                    341: }
                    342:
1.18    ! noro      343: int berlekampsf(UM p,int df,UM *tab,UM *r)
1.1       noro      344: {
                    345:        int n,i,j,k,nf,d,nr;
                    346:        int **mat;
                    347:        int *ind;
                    348:        UM mp,w,q,gcd,w1,w2;
                    349:        UM *u;
                    350:        int *root;
                    351:
                    352:        n = DEG(p);
                    353:        ind = ALLOCA(n*sizeof(int));
                    354:        make_qmatsf(p,tab,&mat);
                    355:        nullsf(mat,n,ind);
                    356:        for ( i = 0, d = 0; i < n; i++ )
                    357:                if ( ind[i] )
                    358:                        d++;
                    359:        if ( d == 1 ) {
                    360:                r[0] = UMALLOC(n); cpyum(p,r[0]); return 1;
                    361:        }
                    362:        u = ALLOCA(d*sizeof(UM *));
                    363:        r[0] = UMALLOC(n); cpyum(p,r[0]);
                    364:        null_to_solsf(mat,ind,n,u);
                    365:        root = ALLOCA(d*sizeof(int));
                    366:        w = W_UMALLOC(n); mp = W_UMALLOC(d);
                    367:        w1 = W_UMALLOC(n); w2 = W_UMALLOC(n);
                    368:        for ( i = 1, nf = 1; i < d; i++ ) {
                    369:                minipolysf(u[i],p,mp);
                    370:                nr = find_rootsf(mp,root);
                    371:                for ( j = 0; j < nf; j++ ) {
                    372:                        if ( DEG(r[j]) == df )
                    373:                                continue;
                    374:                        for ( k = 0; k < nr; k++ ) {
                    375:                                cpyum(u[i],w1); cpyum(r[j],w2);
                    376:                                COEF(w1)[0] = _chsgnsf(root[k]);
                    377:                                gcdsfum(w1,w2,w);
                    378:                                if ( DEG(w) > 0 && DEG(w) < DEG(r[j]) ) {
                    379:                                        gcd = UMALLOC(DEG(w));
                    380:                                        q = UMALLOC(DEG(r[j])-DEG(w));
                    381:                                        cpyum(w,gcd); divsfum(r[j],w,q);
                    382:                                        r[j] = q; r[nf++] = gcd;
                    383:                                }
                    384:                                if ( nf == d )
                    385:                                        return d;
                    386:                        }
                    387:                }
                    388:        }
1.18    ! noro      389:        /* NOT REACHED */
        !           390:        error("berlekampsf : cannot happen");
        !           391:        return 0;
1.1       noro      392: }
                    393:
1.18    ! noro      394: void minipolysf(UM f,UM p,UM mp)
1.1       noro      395: {
                    396:        struct p_pair *list,*l,*l1,*lprev;
                    397:        int n,d;
                    398:        UM u,p0,p1,np0,np1,q,w;
                    399:
                    400:        list = (struct p_pair *)MALLOC(sizeof(struct p_pair));
                    401:        list->p0 = u = W_UMALLOC(0); DEG(u) = 0; COEF(u)[0] = _onesf();
                    402:        list->p1 = W_UMALLOC(0); cpyum(list->p0,list->p1);
                    403:        list->next = 0;
                    404:        n = DEG(p); w = UMALLOC(2*n);
                    405:        p0 = UMALLOC(2*n); cpyum(list->p0,p0);
                    406:        p1 = UMALLOC(2*n); cpyum(list->p1,p1);
                    407:        q = W_UMALLOC(2*n);
                    408:        while ( 1 ) {
                    409:                COEF(p0)[DEG(p0)] = 0; DEG(p0)++; COEF(p0)[DEG(p0)] = _onesf();
                    410:                mulsfum(f,p1,w); DEG(w) = divsfum(w,p,q); cpyum(w,p1);
                    411:                np0 = UMALLOC(n); np1 = UMALLOC(n);
                    412:                lnfsf(n,p0,p1,list,np0,np1);
                    413:                if ( DEG(np1) < 0 ) {
                    414:                        cpyum(np0,mp); return;
                    415:                } else {
                    416:                        l1 = (struct p_pair *)MALLOC(sizeof(struct p_pair));
                    417:                        l1->p0 = np0; l1->p1 = np1;
                    418:                        for ( l = list, lprev = 0, d = DEG(np1);
                    419:                                l && (DEG(l->p1) > d); lprev = l, l = l->next );
                    420:                        if ( lprev ) {
                    421:                                lprev->next = l1; l1->next = l;
                    422:                        } else {
                    423:                                l1->next = list; list = l1;
                    424:                        }
                    425:                }
                    426:        }
                    427: }
                    428:
1.18    ! noro      429: void lnfsf(int n,UM p0,UM p1,struct p_pair *list,UM np0,UM np1)
1.1       noro      430: {
1.18    ! noro      431:        int h,d1;
1.1       noro      432:        UM t0,t1,s0,s1;
                    433:        struct p_pair *l;
                    434:
                    435:        cpyum(p0,np0); cpyum(p1,np1);
                    436:        t0 = W_UMALLOC(n); t1 = W_UMALLOC(n);
                    437:        s0 = W_UMALLOC(n); s1 = W_UMALLOC(n);
                    438:        for ( l = list; l; l = l->next ) {
                    439:                d1 = DEG(np1);
                    440:                if ( d1 == DEG(l->p1) ) {
                    441:                        h = _divsf(COEF(np1)[d1],_chsgnsf(COEF(l->p1)[d1]));
                    442:                        mulssfum(l->p0,h,t0); addsfum(np0,t0,s0); cpyum(s0,np0);
                    443:                        mulssfum(l->p1,h,t1); addsfum(np1,t1,s1); cpyum(s1,np1);
                    444:                }
                    445:        }
                    446: }
                    447:
1.18    ! noro      448: int find_rootsf(UM p,int *root)
1.1       noro      449: {
                    450:        UM *r;
1.18    ! noro      451:        int i,n;
1.1       noro      452:
                    453:        n = DEG(p);
                    454:        r = ALLOCA((DEG(p))*sizeof(UM));
                    455:        canzassf(p,1,r);
                    456:        for ( i = 0; i < n; i++ )
                    457:                root[i] = _chsgnsf(COEF(r[i])[0]);
                    458:        return n;
                    459: }
                    460:
1.18    ! noro      461: void canzassf(UM f,int d,UM *r)
1.1       noro      462: {
                    463:        UM t,s,u,w,g,o;
                    464:        N n1,n2,n3,n4,n5;
                    465:        UM *b;
1.18    ! noro      466:        int n,q,ed;
1.1       noro      467:
                    468:        if ( DEG(f) == d ) {
                    469:                r[0] = UMALLOC(d); cpyum(f,r[0]);
                    470:                return;
                    471:        } else {
                    472:                n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM));
                    473:                bzero((char *)b,n*sizeof(UM));
                    474:
                    475:                t = W_UMALLOC(2*d);
                    476:                s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
                    477:                w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
                    478:                o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = _onesf();
                    479:                q = field_order_sf();
1.2       noro      480:                if ( q % 2 ) {
                    481:                        STON(q,n1); pwrn(n1,d,&n2); subn(n2,ONEN,&n3);
                    482:                        STON(2,n4); divsn(n3,n4,&n5);
                    483:                } else
                    484:                        ed = d*extdeg_sf();
1.1       noro      485:                while ( 1 ) {
1.2       noro      486:                        randsfum(2*d,t);
                    487:                        if ( q % 2 ) {
                    488:                                spwrsfum(f,t,n5,s); subsfum(s,o,u);
                    489:                        } else
                    490:                                tracemodsfum(f,t,ed,u);
                    491:                        cpyum(f,w);
                    492:                        gcdsfum(w,u,g);
1.1       noro      493:                        if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
                    494:                                canzassf(g,d,r);
                    495:                                cpyum(f,w); divsfum(w,g,s);
                    496:                                canzassf(s,d,r+DEG(g)/d);
                    497:                                return;
                    498:                        }
                    499:                }
                    500:        }
                    501: }
                    502:
1.3       noro      503: /* Hensel related functions */
                    504:
                    505: int sfberle(VL,P,int,GFS *,DCP *);
                    506: void sfgcdgen(P,ML,ML *);
1.5       noro      507: void sfhenmain2(BM,UM,UM,int,BM *);
                    508: void ptosfbm(int,P,BM);
1.3       noro      509:
                    510: /* f = f(x,y) */
                    511:
1.18    ! noro      512: void sfhensel(int count,P f,V x,GFS *evp,P *sfp,ML *listp)
1.3       noro      513: {
1.18    ! noro      514:        int i;
        !           515:        int fn;
1.4       noro      516:        ML rlist;
1.5       noro      517:        BM fl;
1.3       noro      518:        VL vl,nvl;
                    519:        V y;
1.18    ! noro      520:        int dx,dy;
1.3       noro      521:        GFS ev;
1.18    ! noro      522:        P f1,t,c,sf;
1.3       noro      523:        DCP dc;
1.18    ! noro      524:        UM q,fm,hm;
1.4       noro      525:        UM *gm;
1.12      noro      526:        struct oEGT tmp0,tmp1,eg_hensel,eg_hensel_t;
1.3       noro      527:
                    528:        clctv(CO,f,&vl);
                    529:        if ( vl->v != x ) {
                    530:                reordvar(vl,x,&nvl); reorderp(nvl,vl,f,&f1);
                    531:                vl = nvl; f = f1;
                    532:        }
                    533:        y = vl->next->v;
                    534:        dx = getdeg(x,f);
                    535:        dy = getdeg(y,f);
                    536:        if ( dx == 1 ) {
1.4       noro      537:                *listp = rlist = MLALLOC(1); rlist->n = 1; rlist->c[0] = 0;
1.3       noro      538:                return;
                    539:        }
                    540:        fn = sfberle(vl,f,count,&ev,&dc);
                    541:        if ( fn <= 1 ) {
                    542:                /* fn == 0 => short of evaluation points */
1.4       noro      543:                *listp = rlist = MLALLOC(1); rlist->n = fn; rlist->c[0] = 0;
1.3       noro      544:                return;
                    545:        }
                    546:        /* pass the the leading coeff. to the first element */
                    547:        c = dc->c; dc = NEXT(dc);
                    548:        mulp(vl,dc->c,c,&t); dc->c = t;
1.4       noro      549:
                    550:        /* convert mod y-a factors into UM */
                    551:        gm = (UM *)ALLOCA(fn*sizeof(UM));
1.3       noro      552:        for ( i = 0; i < fn; i++, dc = NEXT(dc) ) {
1.4       noro      553:                gm[i] = W_UMALLOC(UDEG(dc->c));
                    554:                ptosfum(dc->c,gm[i]);
1.3       noro      555:        }
1.4       noro      556:
                    557:        /* f(x,y) -> f(x,y+ev) */
1.14      noro      558:        fl = BMALLOC(dx,dy);
                    559:        ptosfbm(dy,f,fl);
1.15      noro      560:        if ( ev ) shiftsfbm(fl,FTOIF(CONT(ev)));
1.4       noro      561:
1.8       noro      562:        /* sf = f(x+ev) */
1.14      noro      563:        sfbmtop(fl,x,y,&sf);
1.8       noro      564:
1.4       noro      565:        /* fm = fl mod y */
                    566:        fm = W_UMALLOC(dx);
1.5       noro      567:        cpyum(COEF(fl)[0],fm);
1.4       noro      568:        hm = W_UMALLOC(dx);
                    569:
                    570:        q = W_UMALLOC(dx);
1.14      noro      571:        rlist = MLALLOC(fn); rlist->n = fn; rlist->bound = dy;
1.12      noro      572:        fprintf(asir_out,"%d candidates\n",fn);
                    573:        init_eg(&eg_hensel);
1.7       noro      574:        for ( i = 0; i < fn-1; i++ ) {
1.12      noro      575:                fprintf(asir_out,"deg(fm) = %d, deg(gm[%d]) = %d\n",
                    576:                        DEG(fm),i,DEG(gm[i]));
                    577:                init_eg(&eg_hensel_t);
                    578:                get_eg(&tmp0);
1.4       noro      579:                /* fl = gm[i]*hm mod y */
                    580:                divsfum(fm,gm[i],hm);
1.14      noro      581:                /* fl is replaced by the cofactor of gk mod y^dy */
1.4       noro      582:                /* rlist->c[i] = gk */
1.14      noro      583:                sfhenmain2(fl,gm[i],hm,dy,(BM *)&rlist->c[i]);
1.4       noro      584:                cpyum(hm,fm);
1.12      noro      585:                get_eg(&tmp1); add_eg(&eg_hensel_t,&tmp0,&tmp1);
                    586:                add_eg(&eg_hensel,&tmp0,&tmp1);
                    587:                print_eg("Hensel",&eg_hensel_t);
                    588:                fprintf(asir_out,"\n");
1.4       noro      589:        }
1.12      noro      590:        print_eg("Hensel total",&eg_hensel);
                    591:        fprintf(asir_out,"\n");
1.7       noro      592:        /* finally, fl must be the lift of gm[fn-1] */
1.4       noro      593:        rlist->c[i] = fl;
                    594:
1.8       noro      595: #if 0
1.4       noro      596:        /* y -> y-a */
                    597:        mev = _chsgnsf(FTOIF(CONT(ev)));
                    598:        for ( i = 0; i < fn; i++ )
1.14      noro      599:                shiftsfbm((BM)(rlist->c[i]),mev);
1.8       noro      600: #endif
                    601:        *evp = ev;
                    602:        *sfp = sf;
1.4       noro      603:        *listp = rlist;
1.3       noro      604: }
                    605:
                    606: /* main variable of f = x */
                    607:
1.18    ! noro      608: int sfberle(VL vl,P f,int count,GFS *ev,DCP *dcp)
1.3       noro      609: {
                    610:        UM wf,wf1,wf2,wfs,gcd;
1.18    ! noro      611:        int fn,n;
1.3       noro      612:        GFS m,fm;
                    613:        DCP dc,dct,dc0;
                    614:        VL nvl;
                    615:        V x,y;
1.18    ! noro      616:        P lc,lc0,f0;
        !           617:        Obj obj;
1.3       noro      618:        int j,q1,index,i;
                    619:
                    620:        clctv(vl,f,&nvl); vl = nvl;
                    621:        x = vl->v; y = vl->next->v;
1.18    ! noro      622:        simp_ff((Obj)f,&obj); f = (P)obj;
1.3       noro      623:        n = QTOS(DEG(DC(f)));
                    624:        wf = W_UMALLOC(n); wf1 = W_UMALLOC(n); wf2 = W_UMALLOC(n);
                    625:        wfs = W_UMALLOC(n); gcd = W_UMALLOC(n);
                    626:        q1 = field_order_sf()-1;
                    627:        lc = DC(f)->c;
                    628:        for ( j = 0, fn = n + 1, index = 0;
1.4       noro      629:                index < q1 && j < count && fn > 1; index++ ) {
1.3       noro      630:                MKGFS(index,m);
                    631:                substp(vl,lc,y,(P)m,&lc0);
                    632:                if ( lc0 ) {
                    633:                        substp(vl,f,y,(P)m,&f0);
1.4       noro      634:                        ptosfum(f0,wf); cpyum(wf,wf1);
                    635:                        diffsfum(wf1,wf2); gcdsfum(wf1,wf2,gcd);
1.3       noro      636:                        if ( DEG(gcd) == 0 ) {
                    637:                                fctrsf(f0,&dc);
                    638:                                for ( dct = NEXT(dc), i = 0; dct; dct = NEXT(dct), i++ );
                    639:                                if ( i < fn ) {
                    640:                                        dc0 = dc; fn = i; fm = m;
                    641:                                }
                    642:                                j++;
                    643:                        }
                    644:                }
                    645:        }
                    646:        if ( index == q1 )
                    647:                return 0;
                    648:        else if ( fn == 1 )
                    649:                return 1;
                    650:        else {
                    651:                *dcp = dc0;
                    652:                *ev = fm;
                    653:                return fn;
                    654:        }
                    655: }
                    656:
1.18    ! noro      657: void sfgcdgen(P f,ML blist,ML *clistp)
1.3       noro      658: {
                    659:        int i;
                    660:        int n,d,np;
                    661:        UM wf,wm,wx,wy,wu,wv,wa,wb,wg,q,tum;
                    662:        UM *in,*out;
                    663:        ML clist;
                    664:
                    665:        n = UDEG(f); np = blist->n;
                    666:        d = 2*n;
                    667:        q = W_UMALLOC(d); wf = W_UMALLOC(d);
                    668:        wm = W_UMALLOC(d); wx = W_UMALLOC(d);
                    669:        wy = W_UMALLOC(d); wu = W_UMALLOC(d);
                    670:        wv = W_UMALLOC(d); wg = W_UMALLOC(d);
                    671:        wa = W_UMALLOC(d); wb = W_UMALLOC(d);
                    672:        ptosfum(f,wf); DEG(wg) = 0; COEF(wg)[0] = _onesf();
                    673:        *clistp = clist = MLALLOC(np); clist->n = np;
                    674:        for ( i = 0, in = (UM *)blist->c, out = (UM *)clist->c; i < np; i++ ) {
                    675:                divsfum(wf,in[i],q); tum = wf; wf = q; q = tum;
                    676:                cpyum(wf,wx); cpyum(in[i],wy);
                    677:                eucsfum(wx,wy,wa,wb); mulsfum(wa,wg,wm);
                    678:                DEG(wm) = divsfum(wm,in[i],q); out[i] = UMALLOC(DEG(wm));
                    679:                cpyum(wm,out[i]); mulsfum(q,wf,wu);
                    680:                mulsfum(wg,wb,wv); addsfum(wu,wv,wg);
                    681:        }
                    682: }
                    683:
1.14      noro      684: /* f = g0*h0 mod y -> f = gk*hk mod y^(dy+1), f is replaced by hk */
1.3       noro      685:
1.18    ! noro      686: void sfhenmain2(BM f,UM g0,UM h0,int dy,BM *gp)
1.4       noro      687: {
1.18    ! noro      688:        int i,k;
        !           689:        int dx;
        !           690:        UM wt,wa,wb,q,w1,w2,wh1,wg1,ws;
1.4       noro      691:        UM wc,wd,we,wz;
1.5       noro      692:        BM wb0,wb1;
1.14      noro      693:        int dg,dh;
1.5       noro      694:        BM fk,gk,hk;
1.4       noro      695:
1.14      noro      696:        if ( DEG(f) < dy )
                    697:                error("sfhenmain2 : invalid input");
                    698:
                    699:        dx = degbm(f);
                    700:        dg = DEG(g0);
                    701:        dh = DEG(h0);
                    702:
                    703:        W_BMALLOC(dx,dy,wb0); W_BMALLOC(dx,dy,wb1);
                    704:        wt = W_UMALLOC(dx); ws = W_UMALLOC(dx); q = W_UMALLOC(2*dx);
                    705:        wg1 = W_UMALLOC(2*dx); wh1 = W_UMALLOC(2*dx);
1.5       noro      706:
1.4       noro      707:        /* fk = gk*hk mod y^k */
1.14      noro      708:        W_BMALLOC(dx,dy,fk);
1.8       noro      709:        cpyum(COEF(f)[0],COEF(fk)[0]);
1.14      noro      710:        gk = BMALLOC(dg,dy);
1.8       noro      711:        cpyum(g0,COEF(gk)[0]);
1.14      noro      712:        W_BMALLOC(dh,dy,hk);
1.5       noro      713:        cpyum(h0,COEF(hk)[0]);
1.4       noro      714:
1.14      noro      715:        wc = W_UMALLOC(2*dx); wd = W_UMALLOC(2*dx);
                    716:        we = W_UMALLOC(2*dx); wz = W_UMALLOC(2*dx);
1.4       noro      717:
                    718:        /* compute wa,wb s.t. wa*g0+wb*h0 = 1 mod y */
1.14      noro      719:        w1 = W_UMALLOC(dg); cpyum(g0,w1);
                    720:        w2 = W_UMALLOC(dh); cpyum(h0,w2);
                    721:        wa = W_UMALLOC(2*dx); wb = W_UMALLOC(2*dx);  /* XXX */
1.4       noro      722:        eucsfum(w1,w2,wa,wb);
                    723:
1.14      noro      724:        fprintf(stderr,"dy=%d\n",dy);
                    725:        for ( k = 1; k <= dy; k++ ) {
1.4       noro      726:                fprintf(stderr,".");
                    727:
                    728:                /* at this point, f = gk*hk mod y^k */
                    729:
                    730:                /* clear wt */
1.14      noro      731:                clearum(wt,dx);
1.4       noro      732:
                    733:                /* wt = (f-gk*hk)/y^k */
1.5       noro      734:                subsfum(COEF(f)[k],COEF(fk)[k],wt);
1.4       noro      735:
                    736:                /* compute wf1,wg1 s.t. wh1*g0+wg1*h0 = wt */
                    737:                mulsfum(wa,wt,wh1); DEG(wh1) = divsfum(wh1,h0,q);
                    738:                mulsfum(wh1,g0,wc); subsfum(wt,wc,wd); DEG(wd) = divsfum(wd,h0,wg1);
                    739:
                    740:                /* check */
                    741: #if 0
                    742:                if ( DEG(wd) >= 0 || DEG(wg1) > ng )
1.5       noro      743:                        error("henmain2 : cannot happen(adj)");
1.4       noro      744:
                    745:                mulsfum(wg1,h0,wc); mulsfum(wh1,g0,wd); addsfum(wc,wd,we);
                    746:                subsfum(we,wt,wz);
                    747:                if ( DEG(wz) >= 0 )
                    748:                        error("henmain2 : cannot happen");
                    749: #endif
                    750:
1.14      noro      751:                /* fk += ((wg1*hk+wh1*gk)*y^k+wg1*wh1*y^(2*k) mod y^(dy+1) */
1.4       noro      752:                /* wb0 = wh1*y^k */
1.14      noro      753:                clearbm(dx,wb0);
1.5       noro      754:                cpyum(wh1,COEF(wb0)[k]);
                    755:
1.14      noro      756:                /* wb1 = gk*wb0 mod y^(dy+1) */
                    757:                clearbm(dx,wb1);
                    758:                mulsfbm(gk,wb0,wb1);
1.4       noro      759:                /* fk += wb1 */
1.14      noro      760:                addtosfbm(wb1,fk);
1.4       noro      761:
                    762:                /* wb0 = wg1*y^k */
1.14      noro      763:                clearbm(dx,wb0);
1.5       noro      764:                cpyum(wg1,COEF(wb0)[k]);
                    765:
1.14      noro      766:                /* wb1 = hk*wb0 mod y^(dy+1) */
                    767:                clearbm(dx,wb1);
                    768:                mulsfbm(hk,wb0,wb1);
1.4       noro      769:                /* fk += wb1 */
1.14      noro      770:                addtosfbm(wb1,fk);
1.4       noro      771:
1.14      noro      772:                /* fk += wg1*wh1*y^(2*k) mod y^(dy+1) */
                    773:                if ( 2*k <= dy ) {
1.5       noro      774:                        mulsfum(wg1,wh1,wt); addsfum(COEF(fk)[2*k],wt,ws);
                    775:                        cpyum(ws,COEF(fk)[2*k]);
1.4       noro      776:                }
                    777:
                    778:                /* gk += wg1*y^k, hk += wh1*y^k */
1.5       noro      779:                cpyum(wg1,COEF(gk)[k]);
                    780:                cpyum(wh1,COEF(hk)[k]);
1.4       noro      781:        }
                    782:        fprintf(stderr,"\n");
                    783:        *gp = gk;
1.14      noro      784:        DEG(f) = dy;
                    785:        for ( i = 0; i <= dy; i++ )
1.5       noro      786:                cpyum(COEF(hk)[i],COEF(f)[i]);
1.3       noro      787: }
                    788:
1.5       noro      789: /* fl->c[i] = coef_y(f,i) */
                    790:
1.18    ! noro      791: void ptosfbm(int dy,P f,BM fl)
1.5       noro      792: {
                    793:        DCP dc;
1.14      noro      794:        int d,i,dx;
1.5       noro      795:        UM t;
                    796:
1.14      noro      797:        dx = QTOS(DEG(DC(f)));
                    798:        if ( DEG(fl) < dy )
                    799:                error("ptosfbm : invalid input");
                    800:        DEG(fl) = dy;
                    801:        clearbm(dx,fl);
                    802:        t = UMALLOC(dy);
1.5       noro      803:        for ( dc = DC(f); dc; dc = NEXT(dc) ) {
                    804:                d = QTOS(DEG(dc));
                    805:                ptosfum(COEF(dc),t);
                    806:                for ( i = 0; i <= DEG(t); i++ )
                    807:                        COEF(COEF(fl)[i])[d] = COEF(t)[i];
                    808:        }
1.14      noro      809:        for ( i = 0; i <= dy; i++ )
                    810:                degum(COEF(fl)[i],dx);
1.5       noro      811: }
                    812:
                    813: /* x : main variable */
                    814:
1.18    ! noro      815: void sfbmtop(BM f,V x,V y,P *fp)
1.5       noro      816: {
                    817:        UM *c;
1.14      noro      818:        int i,j,d,a,dy;
1.8       noro      819:        GFS b;
                    820:        DCP dc0,dc,dct;
                    821:
1.14      noro      822:        dy = DEG(f);
1.8       noro      823:        c = COEF(f);
1.14      noro      824:        d = degbm(f);
1.8       noro      825:
                    826:        dc0 = 0;
                    827:        for ( i = 0; i <= d; i++ ) {
                    828:                dc = 0;
1.14      noro      829:                for ( j = 0; j <= dy; j++ ) {
1.8       noro      830:                        if ( DEG(c[j]) >= i && (a = COEF(c[j])[i]) ) {
                    831:                                NEWDC(dct);
                    832:                                STOQ(j,DEG(dct));
                    833:                                MKGFS(IFTOF(a),b);
                    834:                                COEF(dct) = (P)b;
                    835:                                NEXT(dct) = dc;
                    836:                                dc = dct;
                    837:                        }
                    838:                }
                    839:                if ( dc ) {
                    840:                        NEWDC(dct);
                    841:                        STOQ(i,DEG(dct));
                    842:                        MKP(y,dc,COEF(dct));
                    843:                        NEXT(dct) = dc0;
                    844:                        dc0 = dct;
                    845:                }
                    846:        }
                    847:        if ( dc0 )
                    848:                MKP(x,dc0,*fp);
                    849:        else
                    850:                *fp = 0;
                    851: }
                    852:
1.18    ! noro      853: void sfsqfr(P f,DCP *dcp)
1.14      noro      854: {
1.18    ! noro      855:        Obj obj;
1.14      noro      856:        DCP dc;
                    857:        VL vl;
                    858:
1.18    ! noro      859:        simp_ff((Obj)f,&obj); f = (P)obj;
1.14      noro      860:        clctv(CO,f,&vl);
                    861:        if ( !vl ) {
                    862:                /* f is a const */
                    863:                NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0; *dcp = dc;
                    864:        } else if ( !NEXT(vl) )
                    865:                sfusqfr(f,dcp);
                    866:        else if ( !NEXT(NEXT(vl)) )
1.18    ! noro      867:                sfbsqfr(f,vl->v,NEXT(vl)->v,dcp);
1.14      noro      868:        else
                    869:                error("sfsqfr : not implemented yet");
                    870: }
                    871:
1.18    ! noro      872: void sfusqfr(P f,DCP *dcp)
1.14      noro      873: {
                    874:        DCP dc,dct;
                    875:        struct oDUM *udc;
                    876:        V x;
                    877:        P lc;
                    878:        int n,i;
                    879:        UM mf;
                    880:
                    881:        x = VR(f);
                    882:        n = getdeg(x,f);
                    883:        mf = W_UMALLOC(n);
                    884:        ptosfum(f,mf);
                    885:        lc = COEF(DC(f));
                    886:        if ( !_isonesf(COEF(mf)[n]) ) {
                    887:                monicsfum(mf);
                    888:        }
                    889:        W_CALLOC(n+1,struct oDUM,udc);
                    890:        gensqfrsfum(mf,udc);
                    891:        for ( i = 0, dc = 0; udc[i].f; i++ ) {
                    892:                NEWDC(dct); STOQ(udc[i].n,DEG(dct));
                    893:                sfumtop(x,udc[i].f,&COEF(dct));
                    894:                NEXT(dct) = dc; dc = dct;
                    895:        }
                    896:        NEWDC(dct); DEG(dct) = ONE; COEF(dct) = (P)lc; NEXT(dct) = dc;
                    897:        *dcp = dct;
                    898: }
                    899:
1.18    ! noro      900: void sfbsqfr(P f,V x,V y,DCP *dcp)
1.14      noro      901: {
                    902:        P t,rf,cx,cy;
                    903:        VL vl,rvl;
                    904:        DCP dcx,dcy;
                    905:        struct oVL vl0,vl1;
                    906:
                    907:        /* vl = [x,y] */
                    908:        vl0.v = x; vl0.next = &vl1; vl1.v = y; vl1.next = 0; vl = &vl0;
                    909:        /* cy(y) = cont(f,x), f /= cx */
                    910:        cont_pp_sfp(vl,f,&cy,&t); f = t;
                    911:        /* rvl = [y,x] */
                    912:        reordvar(vl,y,&rvl); reorderp(rvl,vl,f,&rf);
                    913:        /* cx(x) = cont(rf,y), Rf /= cy */
                    914:        cont_pp_sfp(rvl,rf,&cx,&t); rf = t;
                    915:        reorderp(vl,rvl,rf,&f);
                    916:
                    917:        /* f -> cx*cy*f */
                    918:        sfusqfr(cx,&dcx);
                    919:        sfusqfr(cy,&dcy);
                    920: }
                    921:
1.8       noro      922: void sfdtest(P,ML,V,V,DCP *);
                    923:
1.18    ! noro      924: void sfbfctr(P f,V x,V y,DCP *dcp)
1.8       noro      925: {
                    926:        ML list;
                    927:        P sf;
1.9       noro      928:        GFS ev;
                    929:        DCP dc,dct;
                    930:        BM fl;
1.14      noro      931:        int dx,dy;
1.8       noro      932:
                    933:        /* sf(x) = f(x+ev) = list->c[0]*list->c[1]*... */
1.9       noro      934:        sfhensel(5,f,x,&ev,&sf,&list);
1.13      noro      935:        if ( list->n == 0 )
                    936:                error("sfbfctr : short of evaluation points");
                    937:        else if ( list->n == 1 ) {
                    938:                /* f is irreducible */
                    939:                NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0;
                    940:                *dcp = dc;
                    941:                return;
                    942:        }
1.9       noro      943:        sfdtest(sf,list,x,y,&dc);
1.15      noro      944:        if ( ev ) {
                    945:                dx = getdeg(x,sf);
                    946:                dy = getdeg(y,sf);
                    947:                W_BMALLOC(dx,dy,fl);
                    948:                for ( dct = dc; dct; dct = NEXT(dct) ) {
                    949:                        ptosfbm(dy,COEF(dct),fl);
                    950:                        shiftsfbm(fl,_chsgnsf(FTOIF(CONT(ev))));
                    951:                        sfbmtop(fl,x,y,&COEF(dct));
                    952:                }
1.9       noro      953:        }
                    954:        *dcp = dc;
1.8       noro      955: }
                    956:
1.14      noro      957: /* f = f(x,y) = list->c[0]*list->c[1]*... mod y^(list->bound+1) */
1.8       noro      958:
1.18    ! noro      959: void sfdtest(P f,ML list,V x,V y,DCP *dcp)
1.8       noro      960: {
1.14      noro      961:        int np,dx,dy;
1.8       noro      962:        int i,j,k;
                    963:        int *win;
                    964:        P g,lcg,factor,cofactor,lcyx;
1.18    ! noro      965:        P csum;
1.8       noro      966:        DCP dcf,dcf0,dc;
                    967:        BM *c;
                    968:        BM lcy;
1.9       noro      969:        UM lcg0;
1.8       noro      970:        ML wlist;
                    971:        struct oVL vl1,vl0;
                    972:        VL vl;
                    973:        int z;
                    974:
                    975:        /* vl = [x,y] */
                    976:        vl0.v = x; vl0.next = &vl1; vl1.v = y; vl1.next = 0; vl = &vl0;
                    977:
1.9       noro      978:        /* setup various structures and arrays */
1.14      noro      979:        dx = getdeg(x,f);
                    980:        dy = getdeg(y,f);
                    981:        np = list->n;
                    982:        win = W_ALLOC(np+1);
                    983:        wlist = W_MLALLOC(np);
                    984:        wlist->n = list->n;
                    985:        wlist->bound = dy;
1.8       noro      986:        c = (BM *)COEF(wlist);
                    987:        bcopy((char *)COEF(list),(char *)c,(int)(sizeof(BM)*np));
                    988:
1.14      noro      989:        lcg0 = W_UMALLOC(2*dy);
1.9       noro      990:
1.8       noro      991:        /* initialize g by f */
1.9       noro      992:        g = f;
                    993:
                    994:        /* initialize lcg */
                    995:        mulp(vl,g,COEF(DC(g)),&lcg);
                    996:
                    997:        /* initialize lcg0 */
                    998:        const_term(lcg,lcg0);
                    999:
                   1000:        /* initialize csum = lcg(1) */
                   1001:        sfcsump(vl,lcg,&csum);
1.8       noro     1002:
                   1003:        /* initialize lcy by LC(f) */
1.14      noro     1004:        W_BMALLOC(0,dy,lcy);
1.9       noro     1005:        NEWDC(dc); COEF(dc) = COEF(DC(g)); DEG(dc) = 0;
1.8       noro     1006:        NEWP(lcyx); VR(lcyx) = x; DC(lcyx) = dc;
1.14      noro     1007:        ptosfbm(dy,lcyx,lcy);
1.8       noro     1008:
                   1009:        fprintf(stderr,"np = %d\n",np);
                   1010:        for ( g = f, k = 1, dcf = dcf0 = 0, win[0] = 1, --np, z = 0; ; z++ ) {
                   1011:                if ( !(z % 1000) ) fprintf(stderr,".");
1.9       noro     1012:                if ( sfdtestmain(vl,lcg,lcg0,lcy,csum,wlist,k,win,&factor,&cofactor) ) {
1.8       noro     1013:                        NEXTDC(dcf0,dcf); DEG(dcf) = ONE; COEF(dcf) = factor;
                   1014:                        g = cofactor;
                   1015:
                   1016:                        /* update lcg */
                   1017:                        mulp(vl,g,COEF(DC(g)),&lcg);
                   1018:
1.9       noro     1019:                        /* update lcg0 */
                   1020:                        const_term(lcg,lcg0);
                   1021:
                   1022:                        /* update csum */
                   1023:                        sfcsump(vl,lcg,&csum);
                   1024:
1.8       noro     1025:                        /* update lcy */
1.14      noro     1026:                        clearbm(0,lcy);
                   1027:                        COEF(dc) = COEF(DC(g));
                   1028:                        ptosfbm(dy,lcyx,lcy);
1.8       noro     1029:
                   1030:                        for ( i = 0; i < k - 1; i++ )
                   1031:                                for ( j = win[i] + 1; j < win[i + 1]; j++ )
                   1032:                                        c[j-i-1] = c[j];
                   1033:                        for ( j = win[k-1] + 1; j <= np; j++ )
                   1034:                                        c[j-k] = c[j];
                   1035:                        if ( ( np -= k ) < k )
                   1036:                                break;
                   1037:                        if ( np - win[0] + 1 < k )
                   1038:                                if ( ++k > np )
                   1039:                                        break;
                   1040:                                else
                   1041:                                        for ( i = 0; i < k; i++ )
                   1042:                                                win[i] = i + 1;
                   1043:                        else
                   1044:                                for ( i = 1; i < k; i++ )
                   1045:                                        win[i] = win[0] + i;
                   1046:                } else if ( !ncombi(1,np,k,win) )
                   1047:                        if ( k == np )
                   1048:                                break;
                   1049:                        else
                   1050:                                for ( i = 0, ++k; i < k; i++ )
                   1051:                                        win[i] = i + 1;
                   1052:        }
                   1053:        fprintf(stderr,"\n");
                   1054:        NEXTDC(dcf0,dcf); COEF(dcf) = g;
                   1055:        DEG(dcf) = ONE; NEXT(dcf) = 0; *dcp = dcf0;
                   1056: }
                   1057:
1.9       noro     1058: /* lcy = LC(g), lcg = lcy*g, lcg0 = const part of lcg */
1.18    ! noro     1059: int sfdtestmain(VL vl,P lcg,UM lcg0,BM lcy,P csum,ML list,
        !          1060:        int k,int *in,P *fp,P *cofp)
1.8       noro     1061: {
1.14      noro     1062:        P fmul,csumg,q,cont;
1.8       noro     1063:        V x,y;
                   1064:
                   1065:        x = vl->v;
                   1066:        y = vl->next->v;
1.9       noro     1067:        if (!sfctest(lcg0,lcy,list,k,in))
1.8       noro     1068:                return 0;
                   1069:        mulsfbmarray(UDEG(lcg),lcy,list,k,in,x,y,&fmul);
                   1070:        if ( csum ) {
                   1071:                sfcsump(vl,fmul,&csumg);
                   1072:                if ( csumg ) {
                   1073:                        if ( !divtp(vl,csum,csumg,&q) )
                   1074:                                return 0;
                   1075:                }
                   1076:        }
1.11      noro     1077:        if ( divtp_by_sfbm(vl,lcg,fmul,&q) ) {
1.14      noro     1078:                cont_pp_sfp(vl,fmul,&cont,fp);
                   1079:                cont_pp_sfp(vl,q,&cont,cofp);
1.8       noro     1080:                return 1;
                   1081:        } else
                   1082:                return 0;
                   1083: }
                   1084:
1.18    ! noro     1085: void const_term(P f,UM c)
1.9       noro     1086: {
                   1087:        DCP dc;
                   1088:
                   1089:        for ( dc = DC(f); dc && DEG(dc); dc = NEXT(dc) );
                   1090:        if ( dc )
                   1091:                ptosfum(COEF(dc),c);
                   1092:        else
                   1093:                DEG(c) = -1;
                   1094: }
                   1095:
1.18    ! noro     1096: void const_term_sfbm(BM f,UM c)
1.9       noro     1097: {
1.14      noro     1098:        int i,dy;
1.9       noro     1099:
1.14      noro     1100:        dy = DEG(f);
                   1101:        for ( i = 0; i <= dy; i++ )
1.9       noro     1102:                if ( DEG(COEF(f)[i]) >= 0 )
                   1103:                        COEF(c)[i] = COEF(COEF(f)[i])[0];
                   1104:                else
                   1105:                        COEF(c)[i] = 0;
1.14      noro     1106:        degum(c,dy);
1.9       noro     1107: }
                   1108:
                   1109: /* lcy*(product of const part) | lcg0 ? */
                   1110:
1.18    ! noro     1111: int sfctest(UM lcg0,BM lcy,ML list,int k,int *in)
1.8       noro     1112: {
1.14      noro     1113:        int dy,i,dr;
1.9       noro     1114:        UM t,s,u,w;
                   1115:        BM *l;
                   1116:
1.14      noro     1117:        dy = list->bound;
                   1118:        t = W_UMALLOC(2*dy);
                   1119:        s = W_UMALLOC(2*dy);
                   1120:        u = W_UMALLOC(2*dy);
                   1121:        const_term_sfbm(lcy,t);
1.9       noro     1122:        if ( DEG(t) < 0 )
                   1123:                return 1;
1.8       noro     1124:
1.9       noro     1125:        l = (BM *)list->c;
                   1126:        for ( i = 0; i < k; i++ ) {
1.14      noro     1127:                const_term_sfbm(l[in[i]],s);
1.9       noro     1128:                mulsfum(t,s,u);
1.14      noro     1129:                if ( DEG(u) > dy )
                   1130:                        degum(u,dy);
1.9       noro     1131:                w = t; t = u; u = w;
                   1132:        }
                   1133:        cpyum(lcg0,s);
                   1134:        dr = divsfum(s,t,u);
                   1135:        if ( dr >= 0 )
                   1136:                return 0;
                   1137:        else
1.8       noro     1138:                return 1;
                   1139: }
                   1140:
                   1141: /* main var of f is x */
                   1142:
1.18    ! noro     1143: void mulsfbmarray(int dx,BM lcy,ML list,int k,int *in,V x,V y,P *g)
1.8       noro     1144: {
1.14      noro     1145:        int dy,i;
1.18    ! noro     1146:        BM wb0,wb1,t;
1.8       noro     1147:        BM *l;
                   1148:
1.14      noro     1149:        dy = list->bound;
                   1150:        W_BMALLOC(dx,dy,wb0); W_BMALLOC(dx,dy,wb1);
1.8       noro     1151:        l = (BM *)list->c;
1.14      noro     1152:        clearbm(dx,wb0);
                   1153:        mulsfbm(lcy,l[in[0]],wb0);
1.8       noro     1154:        for ( i = 1; i < k; i++ ) {
1.14      noro     1155:                clearbm(dx,wb1);
                   1156:                mulsfbm(l[in[i]],wb0,wb1);
1.8       noro     1157:                t = wb0; wb0 = wb1; wb1 = t;
                   1158:        }
1.14      noro     1159:        sfbmtop(wb0,x,y,g);
1.8       noro     1160: }
                   1161:
1.18    ! noro     1162: void sfcsump(VL vl,P f,P *s)
1.8       noro     1163: {
                   1164:        P t,u;
                   1165:        DCP dc;
                   1166:
                   1167:        for ( dc = DC(f), t = 0; dc; dc = NEXT(dc) ) {
                   1168:                addp(vl,COEF(dc),t,&u); t = u;
                   1169:        }
                   1170:        *s = t;
                   1171: }
                   1172:
                   1173: /* *fp = primitive part of f w.r.t. x */
                   1174:
1.18    ! noro     1175: void cont_pp_sfp(VL vl,P f,P *cp,P *fp)
1.8       noro     1176: {
                   1177:        V x,y;
                   1178:        int d;
                   1179:        UM t,s,gcd;
                   1180:        DCP dc;
1.14      noro     1181:        GFS g;
1.5       noro     1182:
1.8       noro     1183:        x = vl->v;
                   1184:        y = vl->next->v;
                   1185:        d = getdeg(y,f);
1.14      noro     1186:        if ( d == 0 ) {
                   1187:                MKGFS(0,g);
                   1188:                *cp = (P)g;
1.8       noro     1189:                *fp = f;  /* XXX */
1.14      noro     1190:        } else {
1.8       noro     1191:                t = W_UMALLOC(2*d);
                   1192:                s = W_UMALLOC(2*d);
                   1193:                gcd = W_UMALLOC(2*d);
                   1194:                dc = DC(f);
                   1195:                ptosfum(COEF(dc),gcd);
                   1196:                for ( dc = NEXT(dc); dc; dc = NEXT(dc) ) {
                   1197:                        ptosfum(COEF(dc),t);
                   1198:                        gcdsfum(gcd,t,s);
                   1199:                        cpyum(s,gcd);
                   1200:                }
1.14      noro     1201:                sfumtop(y,gcd,cp);
                   1202:                divsp(vl,f,*cp,fp);
1.5       noro     1203:        }
1.11      noro     1204: }
                   1205:
1.18    ! noro     1206: int divtp_by_sfbm(VL vl,P f,P g,P *qp)
1.11      noro     1207: {
                   1208:        V x,y;
                   1209:        int fx,fy,gx,gy;
                   1210:        BM fl,gl,ql;
                   1211:        UM *cf,*cg,*cq;
                   1212:        UM hg,q,t,s;
                   1213:        int i,j,dr;
                   1214:
                   1215:        x = vl->v; y = vl->next->v;
                   1216:        fx = getdeg(x,f); fy = getdeg(y,f);
                   1217:        gx = getdeg(x,g); gy = getdeg(y,g);
                   1218:
                   1219:        if ( fx < gx || fy < gy )
                   1220:                return 0;
1.14      noro     1221:        W_BMALLOC(fx,fy,fl); ptosfbm(fy,f,fl); cf = COEF(fl);
                   1222:        W_BMALLOC(gx,gy,gl); ptosfbm(gy,g,gl); cg = COEF(gl);
                   1223:        W_BMALLOC(fx-gx,fy-gy,ql); cq = COEF(ql);
1.11      noro     1224:
                   1225:        hg = cg[gy];
                   1226:        q = W_UMALLOC(fx); t = W_UMALLOC(fx); s = W_UMALLOC(fx);
                   1227:
                   1228:        for ( i = fy; i >= gy; i-- ) {
                   1229:                if ( DEG(cf[i]) < 0 )
                   1230:                        continue;
                   1231:                dr = divsfum(cf[i],hg,q);
                   1232:                if ( dr >= 0 )
                   1233:                        return 0;
                   1234:                if ( DEG(q) > fx-gx )
                   1235:                        return 0;
                   1236:                cpyum(q,cq[i-gy]);
                   1237:                for ( j = 0; j <= gy; j++ ) {
                   1238:                        mulsfum(cg[j],q,t);
                   1239:                        subsfum(cf[j+i-gy],t,s);
                   1240:                        cpyum(s,cf[j+i-gy]);
                   1241:                }
                   1242:        }
                   1243:        for ( j = gy-1; j >= 0 && DEG(cf[j]) < 0; j-- );
                   1244:        if ( j >= 0 )
                   1245:                return 0;
1.14      noro     1246:        sfbmtop(ql,x,y,qp);
1.18    ! noro     1247:        return 1;
1.16      noro     1248: }
                   1249:
                   1250: /* XXX generate an irreducible poly of degree n */
                   1251:
1.17      noro     1252: extern int current_gfs_q1;
                   1253:
1.18    ! noro     1254: void generate_defpoly_sfum(int n,UM *dp)
1.16      noro     1255: {
1.17      noro     1256:        UM r,dr,t,g;
                   1257:        UM *f;
                   1258:        int *c,*w;
                   1259:        int max,i,j;
                   1260:
                   1261:        *dp = r = UMALLOC(n);
                   1262:        DEG(r) = n;
                   1263:        c = COEF(r);
                   1264:        c[n] = _onesf();
                   1265:        max = current_gfs_q1;
                   1266:        w = (int *)ALLOCA(n*sizeof(int));
                   1267:        bzero(w,n*sizeof(int));
                   1268:
                   1269:        dr = W_UMALLOC(n); t = W_UMALLOC(n); g = W_UMALLOC(n);
                   1270:        f = (UM *)ALLOCA((n+1)*sizeof(UM));
                   1271:        while ( 1 ) {
                   1272:                for ( i = 0; i < n && w[i] == max; i++ );
                   1273:                if ( i == n ) {
                   1274:                        /* XXX cannot happen */
                   1275:                        error("generate_defpoly_sfum : cannot happen");
                   1276:                }
                   1277:                for ( j = 0; j < i; j++ )
                   1278:                        w[j] = 0;
                   1279:                w[i]++;
                   1280:                for ( i = 0; i < n; i++ )
                   1281:                        c[i] = w[i]?FTOIF(w[i]-1):0;
                   1282:                if ( !c[0] )
                   1283:                        continue;
                   1284:                diffsfum(r,dr); cpyum(r,t); gcdsfum(t,dr,g);
                   1285:                if ( DEG(g) > 0 )
                   1286:                        continue;
                   1287:
                   1288:                czsfum(r,f);
                   1289:                for ( i = 0; f[i]; i++ );
                   1290:                if ( i == 1 )
                   1291:                        return;
                   1292:        }
1.3       noro     1293: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>