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

Annotation of OpenXM_contrib2/asir2000/engine/Mgfs.c, Revision 1.3

1.3     ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Mgfs.c,v 1.2 2001/06/22 08:51:12 noro Exp $ */
1.1       noro        2:
                      3: #include "ca.h"
                      4:
                      5: void mulssfum(UM,int,UM);
                      6:
                      7: void addsfum(p1,p2,pr)
                      8: UM p1,p2,pr;
                      9: {
                     10:        int *c1,*c2,*cr,i,dmax,dmin;
                     11:
                     12:        if ( DEG(p1) == -1 ) {
                     13:                cpyum(p2,pr);
                     14:                return;
                     15:        }
                     16:        if ( DEG(p2) == -1 ) {
                     17:                cpyum(p1,pr);
                     18:                return;
                     19:        }
                     20:        if ( DEG(p1) >= DEG(p2) ) {
                     21:                c1 = COEF(p1); c2 = COEF(p2); dmax = DEG(p1); dmin = DEG(p2);
                     22:        } else {
                     23:                c1 = COEF(p2); c2 = COEF(p1); dmax = DEG(p2); dmin = DEG(p1);
                     24:        }
                     25:        for ( i = 0, cr = COEF(pr); i <= dmin; i++ )
                     26:                cr[i] = _addsf(c1[i],c2[i]);
                     27:        for ( ; i <= dmax; i++ )
                     28:                cr[i] = c1[i];
                     29:        if ( dmax == dmin )
                     30:                degum(pr,dmax);
                     31:        else
                     32:                DEG(pr) = dmax;
                     33: }
                     34:
                     35: void subsfum(p1,p2,pr)
                     36: UM p1,p2,pr;
                     37: {
                     38:        int *c1,*c2,*cr,i;
                     39:        int dmax,dmin;
                     40:
                     41:        if ( DEG(p1) == -1 ) {
                     42:                for ( i = DEG(pr) = DEG(p2), c2 = COEF(p2), cr = COEF(pr);
                     43:                          i >= 0; i-- )
                     44:                        cr[i] = _chsgnsf(c2[i]);
                     45:                return;
                     46:        }
                     47:        if ( DEG(p2) == -1 ) {
                     48:                cpyum(p1,pr);
                     49:                return;
                     50:        }
                     51:        c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
                     52:        if ( DEG(p1) >= DEG(p2) ) {
                     53:                dmax = DEG(p1); dmin = DEG(p2);
                     54:                for ( i = 0; i <= dmin; i++ )
                     55:                        cr[i] = _subsf(c1[i],c2[i]);
                     56:                for ( ; i <= dmax; i++ )
                     57:                        cr[i] = c1[i];
                     58:        } else {
                     59:                dmax = DEG(p2); dmin = DEG(p1);
                     60:                for ( i = 0; i <= dmin; i++ )
                     61:                        cr[i] = _subsf(c1[i],c2[i]);
                     62:                for ( ; i <= dmax; i++ )
                     63:                        cr[i] = _chsgnsf(c2[i]);
                     64:        }
                     65:        if ( dmax == dmin )
                     66:                degum(pr,dmax);
                     67:        else
                     68:                DEG(pr) = dmax;
                     69: }
                     70:
                     71: void gcdsfum(p1,p2,pr)
                     72: UM p1,p2,pr;
                     73: {
                     74:        int inv;
                     75:        UM t1,t2,q,tum;
                     76:        int drem;
                     77:
                     78:        if ( DEG(p1) < 0 )
                     79:                cpyum(p2,pr);
                     80:        else if ( DEG(p2) < 0 )
                     81:                cpyum(p1,pr);
                     82:        else {
                     83:                if ( DEG(p1) >= DEG(p2) ) {
                     84:                        t1 = p1; t2 = p2;
                     85:                } else {
                     86:                        t1 = p2; t2 = p1;
                     87:                }
                     88:                q = W_UMALLOC(DEG(t1));
                     89:                while ( ( drem = divsfum(t1,t2,q) ) >= 0 ) {
                     90:                        tum = t1; t1 = t2; t2 = tum; DEG(t2) = drem;
                     91:                }
                     92:                inv = _invsf(COEF(t2)[DEG(t2)]);
                     93:                mulssfum(t2,inv,pr);
                     94:        }
                     95: }
                     96: void mulsfum(p1,p2,pr)
                     97: UM p1,p2,pr;
                     98: {
                     99:        int *pc1,*pcr;
                    100:        int *c1,*c2,*cr;
                    101:        int mul;
                    102:        int i,j,d1,d2;
                    103:
                    104:        if ( ( (d1 = DEG(p1)) < 0) || ( (d2 = DEG(p2)) < 0 ) ) {
                    105:                DEG(pr) = -1;
                    106:                return;
                    107:        }
                    108:        c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
                    109:        bzero((char *)cr,(int)((d1+d2+1)*sizeof(int)));
                    110:        for ( i = 0; i <= d2; i++, cr++ )
                    111:                if ( mul = *c2++ )
                    112:                        for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ )
                    113:                                *pcr = _addsf(_mulsf(*pc1,mul),*pcr);
                    114:        DEG(pr) = d1 + d2;
                    115: }
                    116:
                    117: void mulssfum(p,n,pr)
                    118: int n;
                    119: UM p,pr;
                    120: {
                    121:        int *sp,*dp;
                    122:        int i;
                    123:
                    124:        for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i;
                    125:                  i >= 0; i--, dp--, sp-- )
                    126:                *dp = _mulsf(*sp,n);
                    127: }
                    128:
                    129: int divsfum(p1,p2,pq)
                    130: UM p1,p2,pq;
                    131: {
                    132:        int *pc1,*pct;
                    133:        int *c1,*c2,*ct;
                    134:        int inv,hd,tmp;
                    135:        int i,j, d1,d2,dd;
                    136:
                    137:        if ( (d1 = DEG(p1)) < (d2 = DEG(p2)) ) {
                    138:                DEG(pq) = -1;
                    139:                return d1;
                    140:        }
                    141:        c1 = COEF(p1); c2 = COEF(p2); dd = d1-d2;
                    142:        if ( ( hd = c2[d2] ) != _onesf() ) {
                    143:                inv = _invsf(hd);
                    144:                for ( pc1 = c2 + d2; pc1 >= c2; pc1-- )
                    145:                        *pc1 = _mulsf(*pc1,inv);
                    146:        } else
                    147:                inv = _onesf();
                    148:        for ( i = dd, ct = c1+d1; i >= 0; i-- )
                    149:                if ( tmp = *ct-- ) {
                    150:                        tmp = _chsgnsf(tmp);
                    151:                        for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- )
                    152:                                *pct = _addsf(_mulsf(*pc1,tmp),*pct);
                    153:                }
                    154:        if ( inv != _onesf() ) {
                    155:                for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ )
                    156:                        *pc1 = _mulsf(*pc1,inv);
                    157:                for ( pc1 = c2, pct = c2+d2; pc1 <= pct; pc1++ )
                    158:                        *pc1 = _mulsf(*pc1,hd);
                    159:        }
                    160:        for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- );
                    161:        for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- )
                    162:                *pct-- = *pc1--;
                    163:        return i;
                    164: }
                    165:
                    166: void diffsfum(f,fd)
                    167: UM f,fd;
                    168: {
                    169:        int *dp,*sp;
                    170:        int i;
                    171:
                    172:        for ( i = DEG(f), dp = COEF(fd)+i-1, sp = COEF(f)+i;
                    173:                i >= 1; i--, dp--, sp-- ) {
                    174:                *dp = _mulsf(*sp,_itosf(i));
                    175:        }
                    176:        degum(fd,DEG(f) - 1);
                    177: }
                    178:
                    179: void monicsfum(f)
                    180: UM f;
                    181: {
                    182:        int *sp;
                    183:        int i,inv;
                    184:
                    185:        i = DEG(f); sp = COEF(f)+i;
                    186:        inv = _invsf(*sp);
                    187:        for ( ; i >= 0; i--, sp-- )
                    188:                *sp = _mulsf(*sp,inv);
1.2       noro      189: }
                    190:
1.3     ! noro      191: void addsfarray(int,int *,int *);
        !           192: void mulsfarray_trunc(int,int *,int *,int *);
1.2       noro      193:
                    194: void mulsflum(n,f1,f2,fr)
                    195: int n;
                    196: LUM f1,f2,fr;
                    197: {
                    198:        int max;
                    199:        int i,j,**p1,**p2,*px;
                    200:        int *w,*w1,*w2;
                    201:
                    202:        p1 = (int **)COEF(f1); p2 = (int **)COEF(f2);
                    203:        w = W_ALLOC(2*(n+1)); w1 = W_ALLOC(DEG(f1)); w2 = W_ALLOC(DEG(f2));
                    204:        for ( i = DEG(f1); i >= 0; i-- ) {
                    205:                for ( j = n - 1, px = p1[i]; ( j >= 0 ) && ( px[j] == 0 ); j-- );
                    206:                w1[i] = ( j == -1 ? 0 : 1 );
                    207:        }
                    208:        for ( i = DEG(f2); i >= 0; i-- ) {
                    209:                for ( j = n - 1, px = p2[i]; ( j >= 0 ) && ( px[j] == 0 ); j-- );
                    210:                w2[i] = ( j == -1 ? 0 : 1 );
                    211:        }
                    212:        for ( j = DEG(fr) = DEG(f1) + DEG(f2); j >= 0; j-- ) {
                    213:                for ( i = n - 1, px = COEF(fr)[j]; i >= 0; i-- )
                    214:                        px[i] = 0;
                    215:                for ( max = MIN(DEG(f1),j), i = MAX(0,j-DEG(f2)); i <= max; i++ )
                    216:                        if ( w1[i] != 0 && w2[j - i] != 0 ) {
1.3     ! noro      217:                                mulsfarray_trunc(n,p1[i],p2[j - i],w); addsfarray(n,w,px);
1.2       noro      218:                        }
                    219:        }
                    220: }
                    221:
1.3     ! noro      222: void addsfarray(n,a1,a2)
1.2       noro      223: int n;
                    224: int *a1,*a2;
                    225: {
1.3     ! noro      226:        int i;
        !           227:
        !           228:        for ( i = 0; i < n; i++, a1++, a2++ )
        !           229:                if ( *a1 )
        !           230:                        *a2 = _addsf(*a1,*a2);
1.2       noro      231: }
                    232:
1.3     ! noro      233: void mulsfarray_trunc(n,a1,a2,r)
1.2       noro      234: int n;
                    235: int *a1,*a2,*r;
                    236: {
1.3     ! noro      237:        int mul,i,j;
        !           238:        int *c1,*c2,*cr;
        !           239:        int *pc1,*pc2,*pcr;
        !           240:
        !           241:        bzero(r,n*sizeof(int));
        !           242:        c1 = a1; c2 = a2; cr = r;
        !           243:        for ( i = 0; i < n; i++, cr++ ) {
        !           244:                if ( mul = *c2++ )
        !           245:                for ( j = 0, pc1 = c1, pcr = cr; j+i < n; j++, pc1++, pcr++ )
        !           246:                        if ( *pc1 )
        !           247:                                *pcr = _addsf(_mulsf(*pc1,mul),*pcr);
        !           248:        }
1.2       noro      249: }
                    250:
                    251: void eucsfum(f1,f2,a,b)
                    252: UM f1,f2,a,b;
                    253: {
                    254:        UM g1,g2,a1,a2,a3,wm,q,tum;
                    255:        int d,dr;
                    256:
                    257:        /* XXX */
                    258:        d = DEG(f1) + DEG(f2) + 10;
                    259:        g1 = W_UMALLOC(d); g2 = W_UMALLOC(d); a1 = W_UMALLOC(d);
                    260:        a2 = W_UMALLOC(d); a3 = W_UMALLOC(d); wm = W_UMALLOC(d);
                    261:        q = W_UMALLOC(d);
1.3     ! noro      262:        DEG(a1) = 0; COEF(a1)[0] = _onesf(); DEG(a2) = -1;
1.2       noro      263:        cpyum(f1,g1); cpyum(f2,g2);
                    264:        while ( 1 ) {
                    265:                dr = divsfum(g1,g2,q); tum = g1; g1 = g2; g2 = tum;
                    266:                if ( ( DEG(g2) = dr ) == -1 )
                    267:                        break;
                    268:                mulsfum(a2,q,wm); subsfum(a1,wm,a3); dr = divsfum(a3,f2,q);
                    269:                tum = a1; a1 = a2; a2 = a3; a3 = tum; DEG(a3) = dr;
                    270:        }
1.3     ! noro      271:        if ( COEF(g1)[0] != _onesf() )
1.2       noro      272:                mulssfum(a2,_invsf(COEF(g1)[0]),a);
                    273:        else
                    274:                cpyum(a2,a);
                    275:        mulsfum(a,f1,wm);
                    276:        if ( DEG(wm) >= 0 )
                    277:                COEF(wm)[0] = _subsf(COEF(wm)[0],_onesf());
                    278:        else {
                    279:                DEG(wm) = 0; COEF(wm)[0] = _chsgnsf(_onesf());
                    280:        }
                    281:        divsfum(wm,f2,q); mulssfum(q,_chsgnsf(_onesf()),b);
1.3     ! noro      282: }
        !           283:
        !           284: void shiftsfum(UM,int,UM);
        !           285:
        !           286: void shiftsflum(n,f,ev)
        !           287: int n;
        !           288: LUM f;
        !           289: int ev;
        !           290: {
        !           291:        int d,i,j;
        !           292:        UM w,w1;
        !           293:        int *coef;
        !           294:        int **c;
        !           295:
        !           296:        d = f->d;
        !           297:        c = f->c;
        !           298:        w = W_UMALLOC(n);
        !           299:        w1 = W_UMALLOC(n);
        !           300:        for ( i = 0; i <= d; i++ ) {
        !           301:                coef = c[i];
        !           302:                for ( j = n-1; j >= 0 && coef[j] == 0; j-- );
        !           303:                if ( j <= 0 )
        !           304:                        continue;
        !           305:                DEG(w) = j; bcopy(coef,COEF(w),(j+1)*sizeof(int));
        !           306:                shiftsfum(w,ev,w1);
        !           307:                bcopy(COEF(w1),coef,(j+1)*sizeof(int));
        !           308:        }
        !           309: }
        !           310:
        !           311: /* f(x) -> g(x) = f(x+a) */
        !           312:
        !           313: void shiftsfum(f,a,g)
        !           314: UM f;
        !           315: int a;
        !           316: UM g;
        !           317: {
        !           318:        int n,i;
        !           319:        UM pwr,xa,w,t;
        !           320:        int *c;
        !           321:
        !           322:        n = DEG(f);
        !           323:        if ( n <= 0 )
        !           324:                cpyum(f,g);
        !           325:        else {
        !           326:                c = COEF(f);
        !           327:
        !           328:                /* g = 0 */
        !           329:                DEG(g) = -1;
        !           330:
        !           331:                /* pwr = 1 */
        !           332:                pwr = W_UMALLOC(n); DEG(pwr) = 0; COEF(pwr)[0] = _onesf();
        !           333:
        !           334:                /* xa = x+a */
        !           335:                xa = W_UMALLOC(n); DEG(xa) = 1;
        !           336:                COEF(xa)[0] = a; COEF(xa)[1] = _onesf();
        !           337:
        !           338:                w = W_UMALLOC(n);
        !           339:                t = W_UMALLOC(n);
        !           340:
        !           341:                /* g += pwr*c[0] */
        !           342:                for ( i = 0; i <= n; i++ ) {
        !           343:                        mulssfum(pwr,c[i],w); addsfum(g,w,t); cpyum(t,g);
        !           344:                        if ( i < n ) {
        !           345:                                mulsfum(pwr,xa,t); cpyum(t,pwr);
        !           346:                        }
        !           347:                }
        !           348:        }
        !           349: }
        !           350:
        !           351: /* clear the body of f */
        !           352:
        !           353: void clearsflum(bound,n,f)
        !           354: int bound,n;
        !           355: LUM f;
        !           356: {
        !           357:        int **c;
        !           358:        int i;
        !           359:
        !           360:        DEG(f) = 0;
        !           361:        for ( c = COEF(f), i = 0; i <= n; i++ )
        !           362:                bzero(c[i],(bound+1)*sizeof(int));
        !           363: }
        !           364:
        !           365: /* f += g */
        !           366:
        !           367: void addtosflum(bound,g,f)
        !           368: int bound;
        !           369: LUM g,f;
        !           370: {
        !           371:        int dg,i,j;
        !           372:        int **cg,**cf;
        !           373:
        !           374:        dg = DEG(g);
        !           375:        cg = COEF(g);
        !           376:        cf = COEF(f);
        !           377:        for ( i = 0; i <= dg; i++ )
        !           378:                for ( j = 0; j <= bound; j++ )
        !           379:                        cf[i][j] = _addsf(cf[i][j],cg[i][j]);
1.1       noro      380: }

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