Annotation of OpenXM_contrib2/asir2000/engine/Hgfs.c, Revision 1.2
1.2 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Hgfs.c,v 1.1 2001/06/20 09:32:13 noro Exp $ */
1.1 noro 2:
3: #include "ca.h"
4:
5: struct p_pair {
6: UM p0;
7: UM p1;
8: struct p_pair *next;
9: };
10:
11: void canzassf(UM,int,UM *);
12: void lnfsf(int,UM,UM,struct p_pair *,UM,UM);
13: void minipolysf(UM,UM,UM);
14: void czsfum(UM,UM *);
15: void gensqfrsfum(UM,DUM);
16:
17: void fctrsf(p,dcp)
18: P p;
19: DCP *dcp;
20: {
21: int n,i,j,k;
22: DCP dc,dc0;
23: P lc;
24: P zp;
25: UM mp;
26: UM *tl;
27: struct oDUM *udc,*udc1;
28:
29: simp_ff(p,&zp); p = zp;
30: if ( !p ) {
31: *dcp = 0; return;
32: }
33: mp = W_UMALLOC(UDEG(p));
34: ptosfum(p,mp);
35: if ( (n = DEG(mp)) < 0 ) {
36: *dcp = 0; return;
37: } else if ( n == 0 ) {
38: NEWDC(dc); COEF(dc) = p; DEG(dc) = ONE;
39: NEXT(dc) = 0; *dcp = dc;
40: return;
41: }
42: lc = COEF(DC(p));
43: if ( !_isonesf(COEF(mp)[n]) ) {
44: monicsfum(mp);
45: }
46:
47: W_CALLOC(n+1,struct oDUM,udc);
48: gensqfrsfum(mp,udc);
49:
50: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
51: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
52:
53: for ( i = 0,j = 0; udc[i].f; i++ )
54: if ( DEG(udc[i].f) == 1 ) {
55: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
56: } else {
57: bzero((char *)tl,(n+1)*sizeof(UM));
58: czsfum(udc[i].f,tl);
59: for ( k = 0; tl[k]; k++, j++ ) {
60: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
61: }
62: }
63: udc = udc1;
64: NEWDC(dc0); COEF(dc0) = lc; DEG(dc0) = ONE; dc = dc0;
65: for ( n = 0; udc[n].f; n++ ) {
66: NEWDC(NEXT(dc)); dc = NEXT(dc);
67: STOQ(udc[n].n,DEG(dc)); sfumtop(VR(p),udc[n].f,&COEF(dc));
68: }
69: NEXT(dc) = 0; *dcp = dc0;
70: }
71:
72: void gensqfrsfum(p,dc)
73: UM p;
74: struct oDUM *dc;
75: {
76: int n,i,j,d,mod;
77: UM t,s,g,f,f1,b;
78:
79: if ( (n = DEG(p)) == 1 ) {
80: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
81: return;
82: }
83: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
84: f = W_UMALLOC(n); f1 = W_UMALLOC(n); b = W_UMALLOC(n);
85: diffsfum(p,t); cpyum(p,s); gcdsfum(t,s,g);
86: if ( !DEG(g) ) {
87: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
88: return;
89: }
90: cpyum(p,b); cpyum(p,t); divsfum(t,g,f);
91: for ( i = 0, d = 0; DEG(f); i++ ) {
92: while ( 1 ) {
93: cpyum(b,t);
94: if ( divsfum(t,f,s) >= 0 )
95: break;
96: else {
97: cpyum(s,b); d++;
98: }
99: }
100: cpyum(b,t); cpyum(f,s); gcdsfum(t,s,f1);
101: divsfum(f,f1,s); cpyum(f1,f);
102: dc[i].f = UMALLOC(DEG(s)); cpyum(s,dc[i].f); dc[i].n = d;
103: }
104: mod = characteristic_sf();
105: if ( DEG(b) > 0 ) {
106: d = 1;
107: while ( 1 ) {
108: cpyum(b,t);
109: for ( j = DEG(t); j >= 0; j-- )
110: if ( COEF(t)[j] && (j % mod) )
111: break;
112: if ( j >= 0 )
113: break;
114: else {
115: DEG(s) = DEG(t)/mod;
116: for ( j = 0; j <= DEG(t); j++ )
117: COEF(s)[j] = COEF(t)[j*mod];
118: cpyum(s,b); d *= mod;
119: }
120: }
121: gensqfrsfum(b,dc+i);
122: for ( j = i; dc[j].f; j++ )
123: dc[j].n *= d;
124: }
125: }
126:
127: void randsfum(d,p)
128: int d;
129: UM p;
130: {
131: int i;
132:
1.2 ! noro 133: for ( i = 0; i < d; i++ )
1.1 noro 134: COEF(p)[i] = _randomsf();
1.2 ! noro 135: for ( i = d-1; i >= 0 && !COEF(p)[i]; i-- );
! 136: p->d = i;
1.1 noro 137: }
138:
139: void pwrmodsfum(p,e,f,pr)
140: int e;
141: UM p,f,pr;
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.2 ! noro 169: void spwrsfum(m,f,e,r)
1.1 noro 170: UM f,m,r;
171: N e;
172: {
173: UM t,s,q;
174: N e1;
175: int a;
176:
177: if ( !e ) {
178: DEG(r) = 0; COEF(r)[0] = _onesf();
179: } else if ( UNIN(e) )
180: cpyum(f,r);
181: else {
182: a = divin(e,2,&e1);
1.2 ! noro 183: t = W_UMALLOC(2*DEG(m)); spwrsfum(m,f,e1,t);
1.1 noro 184: s = W_UMALLOC(2*DEG(m)); q = W_UMALLOC(2*DEG(m));
185: mulsfum(t,t,s); DEG(s) = divsfum(s,m,q);
186: if ( a ) {
187: mulsfum(s,f,t); DEG(t) = divsfum(t,m,q); cpyum(t,r);
188: } else
189: cpyum(s,r);
190: }
191: }
192:
1.2 ! noro 193: void tracemodsfum(m,f,e,r)
! 194: UM f,m,r;
! 195: int e;
! 196: {
! 197: UM t,s,q,u;
! 198: int i;
! 199:
! 200: q = W_UMALLOC(2*DEG(m)+DEG(f)); /* XXX */
! 201: t = W_UMALLOC(2*DEG(m));
! 202: s = W_UMALLOC(2*DEG(m));
! 203: u = W_UMALLOC(2*DEG(m));
! 204: DEG(f) = divsfum(f,m,q);
! 205: cpyum(f,s);
! 206: cpyum(f,t);
! 207: for ( i = 1; i < e; i++ ) {
! 208: mulsfum(t,t,u);
! 209: DEG(u) = divsfum(u,m,q); cpyum(u,t);
! 210: addsfum(t,s,u); cpyum(u,s);
! 211: }
! 212: cpyum(s,r);
! 213: }
! 214:
1.1 noro 215: void make_qmatsf(p,tab,mp)
216: UM p;
217: UM *tab;
218: int ***mp;
219: {
220: int n,i,j;
221: int *c;
222: UM q,r;
223: int **mat;
224: int one;
225:
226: n = DEG(p);
227: *mp = mat = almat(n,n);
228: for ( j = 0; j < n; j++ ) {
229: r = W_UMALLOC(DEG(tab[j])); q = W_UMALLOC(DEG(tab[j]));
230: cpyum(tab[j],r); DEG(r) = divsfum(r,p,q);
231: for ( i = 0, c = COEF(r); i <= DEG(r); i++ )
232: mat[i][j] = c[i];
233: }
234: one = _onesf();
235: for ( i = 0; i < n; i++ )
236: mat[i][i] = _subsf(mat[i][i],one);
237: }
238:
239: void nullsf(mat,n,ind)
240: int **mat;
241: int *ind;
242: int n;
243: {
244: int i,j,l,s,h,inv;
245: int *t,*u;
246:
247: bzero((char *)ind,n*sizeof(int));
248: ind[0] = 0;
249: for ( i = j = 0; j < n; i++, j++ ) {
250: for ( ; j < n; j++ ) {
251: for ( l = i; l < n; l++ )
252: if ( mat[l][j] )
253: break;
254: if ( l < n ) {
255: t = mat[i]; mat[i] = mat[l]; mat[l] = t; break;
256: } else
257: ind[j] = 1;
258: }
259: if ( j == n )
260: break;
261: inv = _invsf(mat[i][j]);
262: for ( s = j, t = mat[i]; s < n; s++ )
263: t[s] = _mulsf(t[s],inv);
264: for ( l = 0; l < n; l++ ) {
265: if ( l == i )
266: continue;
267: u = mat[l]; h = _chsgnsf(u[j]);
268: for ( s = j; s < n; s++ )
269: u[s] = _addsf(_mulsf(h,t[s]),u[s]);
270: }
271: }
272: }
273:
274: void null_to_solsf(mat,ind,n,r)
275: int **mat;
276: int *ind;
277: int n;
278: UM *r;
279: {
280: int i,j,k,l;
281: int *c;
282: UM w;
283:
284: for ( i = 0, l = 0; i < n; i++ ) {
285: if ( !ind[i] )
286: continue;
287: w = UMALLOC(n);
288: for ( j = k = 0, c = COEF(w); j < n; j++ )
289: if ( ind[j] )
290: c[j] = 0;
291: else
292: c[j] = mat[k++][i];
293: c[i] = _chsgnsf(_onesf());
294: for ( j = n; j >= 0; j-- )
295: if ( c[j] )
296: break;
297: DEG(w) = j;
298: r[l++] = w;
299: }
300: }
301: /*
302: make_qmatsf(p,tab,mp)
303: nullsf(mat,n,ind)
304: null_to_solsf(ind,n,r)
305: */
306:
307: void czsfum(f,r)
308: UM f,*r;
309: {
310: int i,j;
311: int d,n,ord;
312: UM s,t,u,v,w,g,x,m,q;
313: UM *base;
314:
315: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM));
316: bzero((char *)base,n*sizeof(UM));
317:
318: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
319:
320: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = _onesf();
321:
322: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = _onesf();
323:
324: ord = field_order_sf();
325: pwrmodsfum(t,ord,f,w);
326: base[1] = W_UMALLOC(DEG(w));
327: cpyum(w,base[1]);
328:
329: for ( i = 2; i < n; i++ ) {
330: mulsfum(base[i-1],base[1],m);
331: DEG(m) = divsfum(m,f,q);
332: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
333: }
334:
335: v = W_UMALLOC(n); cpyum(f,v);
336: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = _onesf();
337: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = _onesf();
338: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
339:
340: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
341: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
342: if ( COEF(w)[i] ) {
343: mulssfum(base[i],COEF(w)[i],s);
344: addsfum(s,t,u); cpyum(u,t);
345: }
346: cpyum(t,w); cpyum(v,s); subsfum(w,x,t);
347: gcdsfum(s,t,g);
348: if ( DEG(g) >= 1 ) {
349: berlekampsf(g,d,base,r+j); j += DEG(g)/d;
350: divsfum(v,g,q); cpyum(q,v);
351: DEG(w) = divsfum(w,v,q);
352: for ( i = 0; i < DEG(v); i++ )
353: DEG(base[i]) = divsfum(base[i],v,q);
354: }
355: }
356: if ( DEG(v) ) {
357: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
358: }
359: r[j] = 0;
360: }
361:
362: int berlekampsf(p,df,tab,r)
363: UM p;
364: int df;
365: UM *tab,*r;
366: {
367: int n,i,j,k,nf,d,nr;
368: int **mat;
369: int *ind;
370: UM mp,w,q,gcd,w1,w2;
371: UM *u;
372: int *root;
373:
374: n = DEG(p);
375: ind = ALLOCA(n*sizeof(int));
376: make_qmatsf(p,tab,&mat);
377: nullsf(mat,n,ind);
378: for ( i = 0, d = 0; i < n; i++ )
379: if ( ind[i] )
380: d++;
381: if ( d == 1 ) {
382: r[0] = UMALLOC(n); cpyum(p,r[0]); return 1;
383: }
384: u = ALLOCA(d*sizeof(UM *));
385: r[0] = UMALLOC(n); cpyum(p,r[0]);
386: null_to_solsf(mat,ind,n,u);
387: root = ALLOCA(d*sizeof(int));
388: w = W_UMALLOC(n); mp = W_UMALLOC(d);
389: w1 = W_UMALLOC(n); w2 = W_UMALLOC(n);
390: for ( i = 1, nf = 1; i < d; i++ ) {
391: minipolysf(u[i],p,mp);
392: nr = find_rootsf(mp,root);
393: for ( j = 0; j < nf; j++ ) {
394: if ( DEG(r[j]) == df )
395: continue;
396: for ( k = 0; k < nr; k++ ) {
397: cpyum(u[i],w1); cpyum(r[j],w2);
398: COEF(w1)[0] = _chsgnsf(root[k]);
399: gcdsfum(w1,w2,w);
400: if ( DEG(w) > 0 && DEG(w) < DEG(r[j]) ) {
401: gcd = UMALLOC(DEG(w));
402: q = UMALLOC(DEG(r[j])-DEG(w));
403: cpyum(w,gcd); divsfum(r[j],w,q);
404: r[j] = q; r[nf++] = gcd;
405: }
406: if ( nf == d )
407: return d;
408: }
409: }
410: }
411: }
412:
413: void minipolysf(f,p,mp)
414: UM f,p,mp;
415: {
416: struct p_pair *list,*l,*l1,*lprev;
417: int n,d;
418: UM u,p0,p1,np0,np1,q,w;
419:
420: list = (struct p_pair *)MALLOC(sizeof(struct p_pair));
421: list->p0 = u = W_UMALLOC(0); DEG(u) = 0; COEF(u)[0] = _onesf();
422: list->p1 = W_UMALLOC(0); cpyum(list->p0,list->p1);
423: list->next = 0;
424: n = DEG(p); w = UMALLOC(2*n);
425: p0 = UMALLOC(2*n); cpyum(list->p0,p0);
426: p1 = UMALLOC(2*n); cpyum(list->p1,p1);
427: q = W_UMALLOC(2*n);
428: while ( 1 ) {
429: COEF(p0)[DEG(p0)] = 0; DEG(p0)++; COEF(p0)[DEG(p0)] = _onesf();
430: mulsfum(f,p1,w); DEG(w) = divsfum(w,p,q); cpyum(w,p1);
431: np0 = UMALLOC(n); np1 = UMALLOC(n);
432: lnfsf(n,p0,p1,list,np0,np1);
433: if ( DEG(np1) < 0 ) {
434: cpyum(np0,mp); return;
435: } else {
436: l1 = (struct p_pair *)MALLOC(sizeof(struct p_pair));
437: l1->p0 = np0; l1->p1 = np1;
438: for ( l = list, lprev = 0, d = DEG(np1);
439: l && (DEG(l->p1) > d); lprev = l, l = l->next );
440: if ( lprev ) {
441: lprev->next = l1; l1->next = l;
442: } else {
443: l1->next = list; list = l1;
444: }
445: }
446: }
447: }
448:
449: void lnfsf(n,p0,p1,list,np0,np1)
450: int n;
451: UM p0,p1;
452: struct p_pair *list;
453: UM np0,np1;
454: {
455: int inv,h,d1;
456: UM t0,t1,s0,s1;
457: struct p_pair *l;
458:
459: cpyum(p0,np0); cpyum(p1,np1);
460: t0 = W_UMALLOC(n); t1 = W_UMALLOC(n);
461: s0 = W_UMALLOC(n); s1 = W_UMALLOC(n);
462: for ( l = list; l; l = l->next ) {
463: d1 = DEG(np1);
464: if ( d1 == DEG(l->p1) ) {
465: h = _divsf(COEF(np1)[d1],_chsgnsf(COEF(l->p1)[d1]));
466: mulssfum(l->p0,h,t0); addsfum(np0,t0,s0); cpyum(s0,np0);
467: mulssfum(l->p1,h,t1); addsfum(np1,t1,s1); cpyum(s1,np1);
468: }
469: }
470: }
471:
472: int find_rootsf(p,root)
473: UM p;
474: int *root;
475: {
476: UM *r;
477: int i,j,n;
478:
479: n = DEG(p);
480: r = ALLOCA((DEG(p))*sizeof(UM));
481: canzassf(p,1,r);
482: for ( i = 0; i < n; i++ )
483: root[i] = _chsgnsf(COEF(r[i])[0]);
484: return n;
485: }
486:
487: void canzassf(f,d,r)
488: UM f,*r;
489: int d;
490: {
491: UM t,s,u,w,g,o;
492: N n1,n2,n3,n4,n5;
493: UM *b;
1.2 ! noro 494: int n,m,i,q,ed;
1.1 noro 495:
496: if ( DEG(f) == d ) {
497: r[0] = UMALLOC(d); cpyum(f,r[0]);
498: return;
499: } else {
500: n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM));
501: bzero((char *)b,n*sizeof(UM));
502:
503: t = W_UMALLOC(2*d);
504: s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
505: w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
506: o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = _onesf();
507: q = field_order_sf();
1.2 ! noro 508: if ( q % 2 ) {
! 509: STON(q,n1); pwrn(n1,d,&n2); subn(n2,ONEN,&n3);
! 510: STON(2,n4); divsn(n3,n4,&n5);
! 511: } else
! 512: ed = d*extdeg_sf();
1.1 noro 513: while ( 1 ) {
1.2 ! noro 514: randsfum(2*d,t);
! 515: if ( q % 2 ) {
! 516: spwrsfum(f,t,n5,s); subsfum(s,o,u);
! 517: } else
! 518: tracemodsfum(f,t,ed,u);
! 519: cpyum(f,w);
! 520: gcdsfum(w,u,g);
1.1 noro 521: if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
522: canzassf(g,d,r);
523: cpyum(f,w); divsfum(w,g,s);
524: canzassf(s,d,r+DEG(g)/d);
525: return;
526: }
527: }
528: }
529: }
530:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>