Annotation of OpenXM_contrib2/asir2000/engine/Mgfs.c, Revision 1.8
1.8 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Mgfs.c,v 1.7 2001/06/27 04:07:57 noro Exp $ */
1.1 noro 2:
3: #include "ca.h"
4:
1.5 noro 5: extern int up_kara_mag, current_gfs_q1;
6: extern int *current_gfs_plus1;
7:
1.1 noro 8: void mulssfum(UM,int,UM);
1.5 noro 9: void kmulsfummain(UM,UM,UM);
10:
11: inline int _ADDSF(a,b)
12: int a,b;
13: {
14: if ( !a )
15: return b;
16: else if ( !b )
17: return a;
18:
19: a = IFTOF(a); b = IFTOF(b);
20: if ( a > b ) {
21: /* tab[a]+tab[b] = tab[b](tab[a-b]+1) */
22: a = current_gfs_plus1[a-b];
23: if ( a < 0 )
24: return 0;
25: else {
26: a += b;
27: if ( a >= current_gfs_q1 )
28: a -= current_gfs_q1;
29: return FTOIF(a);
30: }
31: } else {
32: /* tab[a]+tab[b] = tab[a](tab[b-a]+1) */
33: b = current_gfs_plus1[b-a];
34: if ( b < 0 )
35: return 0;
36: else {
37: b += a;
38: if ( b >= current_gfs_q1 )
39: b -= current_gfs_q1;
40: return FTOIF(b);
41: }
42: }
43: }
44:
45: inline int _MULSF(a,b)
46: int a,b;
47: {
48: if ( !a || !b )
49: return 0;
50: else {
51: a = IFTOF(a) + IFTOF(b);
52: if ( a >= current_gfs_q1 )
53: a -= current_gfs_q1;
54: return FTOIF(a);
55: }
56: }
1.1 noro 57:
58: void addsfum(p1,p2,pr)
59: UM p1,p2,pr;
60: {
61: int *c1,*c2,*cr,i,dmax,dmin;
62:
63: if ( DEG(p1) == -1 ) {
64: cpyum(p2,pr);
65: return;
66: }
67: if ( DEG(p2) == -1 ) {
68: cpyum(p1,pr);
69: return;
70: }
71: if ( DEG(p1) >= DEG(p2) ) {
72: c1 = COEF(p1); c2 = COEF(p2); dmax = DEG(p1); dmin = DEG(p2);
73: } else {
74: c1 = COEF(p2); c2 = COEF(p1); dmax = DEG(p2); dmin = DEG(p1);
75: }
76: for ( i = 0, cr = COEF(pr); i <= dmin; i++ )
1.5 noro 77: cr[i] = _ADDSF(c1[i],c2[i]);
1.1 noro 78: for ( ; i <= dmax; i++ )
79: cr[i] = c1[i];
80: if ( dmax == dmin )
81: degum(pr,dmax);
82: else
83: DEG(pr) = dmax;
84: }
85:
86: void subsfum(p1,p2,pr)
87: UM p1,p2,pr;
88: {
89: int *c1,*c2,*cr,i;
90: int dmax,dmin;
91:
92: if ( DEG(p1) == -1 ) {
93: for ( i = DEG(pr) = DEG(p2), c2 = COEF(p2), cr = COEF(pr);
94: i >= 0; i-- )
95: cr[i] = _chsgnsf(c2[i]);
96: return;
97: }
98: if ( DEG(p2) == -1 ) {
99: cpyum(p1,pr);
100: return;
101: }
102: c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
103: if ( DEG(p1) >= DEG(p2) ) {
104: dmax = DEG(p1); dmin = DEG(p2);
105: for ( i = 0; i <= dmin; i++ )
106: cr[i] = _subsf(c1[i],c2[i]);
107: for ( ; i <= dmax; i++ )
108: cr[i] = c1[i];
109: } else {
110: dmax = DEG(p2); dmin = DEG(p1);
111: for ( i = 0; i <= dmin; i++ )
112: cr[i] = _subsf(c1[i],c2[i]);
113: for ( ; i <= dmax; i++ )
114: cr[i] = _chsgnsf(c2[i]);
115: }
116: if ( dmax == dmin )
117: degum(pr,dmax);
118: else
119: DEG(pr) = dmax;
120: }
121:
122: void gcdsfum(p1,p2,pr)
123: UM p1,p2,pr;
124: {
125: int inv;
126: UM t1,t2,q,tum;
127: int drem;
128:
129: if ( DEG(p1) < 0 )
130: cpyum(p2,pr);
131: else if ( DEG(p2) < 0 )
132: cpyum(p1,pr);
133: else {
134: if ( DEG(p1) >= DEG(p2) ) {
135: t1 = p1; t2 = p2;
136: } else {
137: t1 = p2; t2 = p1;
138: }
139: q = W_UMALLOC(DEG(t1));
140: while ( ( drem = divsfum(t1,t2,q) ) >= 0 ) {
141: tum = t1; t1 = t2; t2 = tum; DEG(t2) = drem;
142: }
143: inv = _invsf(COEF(t2)[DEG(t2)]);
144: mulssfum(t2,inv,pr);
145: }
146: }
147: void mulsfum(p1,p2,pr)
148: UM p1,p2,pr;
149: {
150: int *pc1,*pcr;
151: int *c1,*c2,*cr;
152: int mul;
153: int i,j,d1,d2;
154:
155: if ( ( (d1 = DEG(p1)) < 0) || ( (d2 = DEG(p2)) < 0 ) ) {
156: DEG(pr) = -1;
157: return;
158: }
159: c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
160: bzero((char *)cr,(int)((d1+d2+1)*sizeof(int)));
161: for ( i = 0; i <= d2; i++, cr++ )
162: if ( mul = *c2++ )
163: for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ )
1.5 noro 164: if ( *pc1 )
165: *pcr = _ADDSF(_MULSF(*pc1,mul),*pcr);
1.1 noro 166: DEG(pr) = d1 + d2;
167: }
168:
169: void mulssfum(p,n,pr)
170: int n;
171: UM p,pr;
172: {
173: int *sp,*dp;
174: int i;
175:
176: for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i;
177: i >= 0; i--, dp--, sp-- )
1.5 noro 178: *dp = _MULSF(*sp,n);
1.1 noro 179: }
180:
1.5 noro 181: void kmulsfum(n1,n2,nr)
182: UM n1,n2,nr;
183: {
184: UM n,t,s,m,carry;
185: int d,d1,d2,len,i,l;
186: unsigned int *r,*r0;
187:
188: if ( !n1 || !n2 ) {
189: nr->d = -1; return;
190: }
191: d1 = DEG(n1)+1; d2 = DEG(n2)+1;
192: if ( (d1 < up_kara_mag) || (d2 < up_kara_mag) ) {
193: mulsfum(n1,n2,nr); return;
194: }
195: if ( d1 < d2 ) {
196: n = n1; n1 = n2; n2 = n;
197: d = d1; d1 = d2; d2 = d;
198: }
199: if ( d2 > (d1+1)/2 ) {
200: kmulsfummain(n1,n2,nr); return;
201: }
202: d = (d1/d2)+((d1%d2)!=0);
203: len = (d+1)*d2;
204: r0 = (unsigned int *)ALLOCA(len*sizeof(int));
205: bzero((char *)r0,len*sizeof(int));
206: m = W_UMALLOC(d2+1);
207: carry = W_UMALLOC(d2+1);
208: t = W_UMALLOC(d1+d2+1);
209: s = W_UMALLOC(d1+d2+1);
210: for ( DEG(carry) = -1, i = 0, r = r0; i < d; i++, r += d2 ) {
211: extractum(n1,i*d2,d2,m);
212: if ( m ) {
213: kmulsfum(m,n2,t);
214: addsfum(t,carry,s);
215: c_copyum(s,d2,r);
216: extractum(s,d2,d2,carry);
217: } else {
218: c_copyum(carry,d2,r);
219: carry = 0;
220: }
221: }
222: c_copyum(carry,d2,r);
223: for ( l = len - 1; !r0[l]; l-- );
224: l++;
225: DEG(nr) = l-1;
226: bcopy((char *)r0,(char *)COEF(nr),l*sizeof(int));
227: }
228:
229: void kmulsfummain(n1,n2,nr)
230: UM n1,n2,nr;
231: {
232: int d1,d2,h,len;
233: UM n1lo,n1hi,n2lo,n2hi,hi,lo,mid1,mid2,mid,s1,s2,t1,t2;
234:
235: d1 = DEG(n1)+1; d2 = DEG(n2)+1; h = (d1+1)/2;
236: n1lo = W_UMALLOC(d1+1); n1hi = W_UMALLOC(d1+1);
237: n2lo = W_UMALLOC(d2+1); n2hi = W_UMALLOC(d2+1);
238: lo = W_UMALLOC(d1+d2+1); hi = W_UMALLOC(d1+d2+1);
239: mid1 = W_UMALLOC(d1+d2+1); mid2 = W_UMALLOC(d1+d2+1);
240: mid = W_UMALLOC(d1+d2+1);
241: s1 = W_UMALLOC(d1+d2+1); s2 = W_UMALLOC(d1+d2+1);
242: extractum(n1,0,h,n1lo); extractum(n1,h,d1-h,n1hi);
243: extractum(n2,0,h,n2lo); extractum(n2,h,d2-h,n2hi);
244: kmulsfum(n1hi,n2hi,hi); kmulsfum(n1lo,n2lo,lo);
245: len = DEG(hi)+1+2*h; t1 = W_UMALLOC(len-1); DEG(t1) = len-1;
246: bzero((char *)COEF(t1),len*sizeof(int));
247: if ( lo )
248: bcopy((char *)COEF(lo),(char *)COEF(t1),(DEG(lo)+1)*sizeof(int));
249: if ( hi )
250: bcopy((char *)COEF(hi),(char *)(COEF(t1)+2*h),(DEG(hi)+1)*sizeof(int));
251:
252: addsfum(hi,lo,mid1);
253: subsfum(n1hi,n1lo,s1); subsfum(n2lo,n2hi,s2);
254: kmulsfum(s1,s2,mid2); addsfum(mid1,mid2,mid);
255: if ( mid ) {
256: len = DEG(mid)+1+h; t2 = W_UMALLOC(len-1); DEG(t2) = len-1;
257: bzero((char *)COEF(t2),len*sizeof(int));
258: bcopy((char *)COEF(mid),(char *)(COEF(t2)+h),(DEG(mid)+1)*sizeof(int));
259: addsfum(t1,t2,nr);
260: } else
261: copyum(t1,nr);
262: }
263:
1.1 noro 264: int divsfum(p1,p2,pq)
265: UM p1,p2,pq;
266: {
267: int *pc1,*pct;
268: int *c1,*c2,*ct;
269: int inv,hd,tmp;
270: int i,j, d1,d2,dd;
271:
272: if ( (d1 = DEG(p1)) < (d2 = DEG(p2)) ) {
273: DEG(pq) = -1;
274: return d1;
275: }
276: c1 = COEF(p1); c2 = COEF(p2); dd = d1-d2;
277: if ( ( hd = c2[d2] ) != _onesf() ) {
278: inv = _invsf(hd);
279: for ( pc1 = c2 + d2; pc1 >= c2; pc1-- )
1.5 noro 280: *pc1 = _MULSF(*pc1,inv);
1.1 noro 281: } else
282: inv = _onesf();
283: for ( i = dd, ct = c1+d1; i >= 0; i-- )
284: if ( tmp = *ct-- ) {
285: tmp = _chsgnsf(tmp);
286: for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- )
1.5 noro 287: *pct = _ADDSF(_MULSF(*pc1,tmp),*pct);
1.1 noro 288: }
289: if ( inv != _onesf() ) {
290: for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ )
1.5 noro 291: *pc1 = _MULSF(*pc1,inv);
1.1 noro 292: for ( pc1 = c2, pct = c2+d2; pc1 <= pct; pc1++ )
1.5 noro 293: *pc1 = _MULSF(*pc1,hd);
1.1 noro 294: }
295: for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- );
296: for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- )
297: *pct-- = *pc1--;
298: return i;
299: }
300:
301: void diffsfum(f,fd)
302: UM f,fd;
303: {
304: int *dp,*sp;
305: int i;
306:
307: for ( i = DEG(f), dp = COEF(fd)+i-1, sp = COEF(f)+i;
308: i >= 1; i--, dp--, sp-- ) {
1.5 noro 309: *dp = _MULSF(*sp,_itosf(i));
1.1 noro 310: }
311: degum(fd,DEG(f) - 1);
312: }
313:
314: void monicsfum(f)
315: UM f;
316: {
317: int *sp;
318: int i,inv;
319:
320: i = DEG(f); sp = COEF(f)+i;
321: inv = _invsf(*sp);
322: for ( ; i >= 0; i--, sp-- )
1.5 noro 323: *sp = _MULSF(*sp,inv);
1.2 noro 324: }
325:
1.3 noro 326: void addsfarray(int,int *,int *);
327: void mulsfarray_trunc(int,int *,int *,int *);
1.2 noro 328:
1.4 noro 329: /* f1 = f1->c[0]+f1->c[1]*y+..., f2 = f2->c[0]+f2->c[1]*y+... mod y^n */
330:
1.8 ! noro 331: void mulsfbm(f1,f2,fr)
1.4 noro 332: BM f1,f2,fr;
333: {
334: UM mul,t,s;
1.8 ! noro 335: int i,j,h,d1,d2,dy;
1.4 noro 336:
1.8 ! noro 337: dy = MIN(DEG(f1),DEG(f2));
1.4 noro 338:
1.8 ! noro 339: d1 = degbm(f1);
! 340: d2 = degbm(f2);
1.4 noro 341: t = W_UMALLOC(d1+d2);
342: s = W_UMALLOC(d1+d2);
343:
1.8 ! noro 344: for ( i = 0; i <= dy; i++ ) {
1.4 noro 345: mul = COEF(f2)[i];
346: if ( DEG(mul) >= 0 )
1.8 ! noro 347: for ( j = 0; i+j <= dy; j++ ) {
1.4 noro 348: if ( COEF(f1)[j] ) {
1.7 noro 349: kmulsfum(COEF(f1)[j],mul,t);
1.4 noro 350: addsfum(t,COEF(fr)[i+j],s);
351: cpyum(s,COEF(fr)[i+j]);
352: }
353: }
354: }
1.8 ! noro 355: DEG(fr) = dy;
1.4 noro 356: }
357:
1.8 ! noro 358: int degbm(f)
1.6 noro 359: BM f;
360: {
1.8 ! noro 361: int d,i,dy;
1.6 noro 362:
1.8 ! noro 363: dy = DEG(f);
1.6 noro 364: d = DEG(COEF(f)[0]);
1.8 ! noro 365: for ( i = 1; i <= dy; i++ )
1.6 noro 366: d = MAX(DEG(COEF(f)[i]),d);
367: return d;
368: }
369:
1.4 noro 370: /* g += f */
371:
1.8 ! noro 372: void addtosfbm(f,g)
1.4 noro 373: BM f,g;
374: {
1.8 ! noro 375: int i,d1,d2,dy;
1.4 noro 376: UM t;
377:
1.8 ! noro 378: dy = DEG(g);
! 379: if ( DEG(f) > dy )
! 380: error("addtosfbm : invalid input");
! 381: dy = MIN(DEG(f),dy);
! 382: d1 = degbm(f);
! 383: d2 = degbm(g);
1.4 noro 384: t = W_UMALLOC(MAX(d1,d2));
1.8 ! noro 385: for ( i = 0; i <= dy; i++ ) {
1.4 noro 386: addsfum(COEF(f)[i],COEF(g)[i],t);
387: cpyum(t,COEF(g)[i]);
388: }
389: }
390:
1.2 noro 391: void eucsfum(f1,f2,a,b)
392: UM f1,f2,a,b;
393: {
394: UM g1,g2,a1,a2,a3,wm,q,tum;
395: int d,dr;
396:
397: /* XXX */
398: d = DEG(f1) + DEG(f2) + 10;
399: g1 = W_UMALLOC(d); g2 = W_UMALLOC(d); a1 = W_UMALLOC(d);
400: a2 = W_UMALLOC(d); a3 = W_UMALLOC(d); wm = W_UMALLOC(d);
401: q = W_UMALLOC(d);
1.3 noro 402: DEG(a1) = 0; COEF(a1)[0] = _onesf(); DEG(a2) = -1;
1.2 noro 403: cpyum(f1,g1); cpyum(f2,g2);
404: while ( 1 ) {
405: dr = divsfum(g1,g2,q); tum = g1; g1 = g2; g2 = tum;
406: if ( ( DEG(g2) = dr ) == -1 )
407: break;
408: mulsfum(a2,q,wm); subsfum(a1,wm,a3); dr = divsfum(a3,f2,q);
409: tum = a1; a1 = a2; a2 = a3; a3 = tum; DEG(a3) = dr;
410: }
1.3 noro 411: if ( COEF(g1)[0] != _onesf() )
1.2 noro 412: mulssfum(a2,_invsf(COEF(g1)[0]),a);
413: else
414: cpyum(a2,a);
415: mulsfum(a,f1,wm);
416: if ( DEG(wm) >= 0 )
417: COEF(wm)[0] = _subsf(COEF(wm)[0],_onesf());
418: else {
419: DEG(wm) = 0; COEF(wm)[0] = _chsgnsf(_onesf());
420: }
421: divsfum(wm,f2,q); mulssfum(q,_chsgnsf(_onesf()),b);
1.3 noro 422: }
423:
424: void shiftsfum(UM,int,UM);
425:
426: void shiftsflum(n,f,ev)
427: int n;
428: LUM f;
429: int ev;
430: {
431: int d,i,j;
432: UM w,w1;
433: int *coef;
434: int **c;
435:
436: d = f->d;
437: c = f->c;
438: w = W_UMALLOC(n);
439: w1 = W_UMALLOC(n);
440: for ( i = 0; i <= d; i++ ) {
441: coef = c[i];
442: for ( j = n-1; j >= 0 && coef[j] == 0; j-- );
443: if ( j <= 0 )
444: continue;
445: DEG(w) = j; bcopy(coef,COEF(w),(j+1)*sizeof(int));
446: shiftsfum(w,ev,w1);
447: bcopy(COEF(w1),coef,(j+1)*sizeof(int));
448: }
449: }
450:
451: /* f(x) -> g(x) = f(x+a) */
452:
453: void shiftsfum(f,a,g)
454: UM f;
455: int a;
456: UM g;
457: {
458: int n,i;
459: UM pwr,xa,w,t;
460: int *c;
461:
462: n = DEG(f);
463: if ( n <= 0 )
464: cpyum(f,g);
465: else {
466: c = COEF(f);
467:
468: /* g = 0 */
469: DEG(g) = -1;
470:
471: /* pwr = 1 */
472: pwr = W_UMALLOC(n); DEG(pwr) = 0; COEF(pwr)[0] = _onesf();
473:
474: /* xa = x+a */
475: xa = W_UMALLOC(n); DEG(xa) = 1;
476: COEF(xa)[0] = a; COEF(xa)[1] = _onesf();
477:
478: w = W_UMALLOC(n);
479: t = W_UMALLOC(n);
480:
481: /* g += pwr*c[0] */
482: for ( i = 0; i <= n; i++ ) {
483: mulssfum(pwr,c[i],w); addsfum(g,w,t); cpyum(t,g);
484: if ( i < n ) {
485: mulsfum(pwr,xa,t); cpyum(t,pwr);
486: }
487: }
488: }
489: }
490:
1.4 noro 491: /* f(y) -> f(y+a) */
492:
1.8 ! noro 493: void shiftsfbm(f,a)
1.4 noro 494: BM f;
495: int a;
496: {
1.8 ! noro 497: int i,j,d,dy;
1.4 noro 498: UM pwr,ya,w,t,s;
499: UM *c;
500:
1.8 ! noro 501: dy = DEG(f);
! 502: c = COEF(f);
! 503: d = degbm(f);
! 504:
! 505: w = W_UMALLOC(d);
! 506: t = W_UMALLOC(d);
! 507: s = W_UMALLOC(dy);
! 508:
! 509: /* pwr = 1 */
! 510: pwr = W_UMALLOC(dy); DEG(pwr) = 0; COEF(pwr)[0] = _onesf();
! 511:
! 512: /* ya = y+a */
! 513: ya = W_UMALLOC(1); DEG(ya) = 1;
! 514: COEF(ya)[0] = a; COEF(ya)[1] = _onesf();
! 515:
! 516: for ( i = 0; i <= dy; i++ ) {
! 517: /* c[i] does not change */
! 518: for ( j = 0; j < i; j++ ) {
! 519: mulssfum(c[i],COEF(pwr)[j],w);
! 520: addsfum(w,c[j],t); cpyum(t,c[j]);
! 521: }
! 522: if ( i < dy ) {
! 523: mulsfum(pwr,ya,s); cpyum(s,pwr);
1.4 noro 524: }
525: }
526: }
527:
1.8 ! noro 528: void clearbm(n,f)
! 529: int n;
1.4 noro 530: BM f;
531: {
1.8 ! noro 532: int i,dy;
1.4 noro 533: UM *c;
534:
1.8 ! noro 535: dy = DEG(f);
! 536: for ( c = COEF(f), i = 0; i <= dy; i++ )
1.4 noro 537: clearum(c[i],n);
1.1 noro 538: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>