[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.24

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

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