Annotation of OpenXM_contrib2/asir2000/engine/Hgfs.c, Revision 1.6
1.6 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Hgfs.c,v 1.5 2001/06/25 04:11:42 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:
1.3 noro 531: /* Hensel related functions */
532:
533: int sfberle(VL,P,int,GFS *,DCP *);
534: void sfgcdgen(P,ML,ML *);
535: void sfhenmain(LUM,ML,ML,ML *);
536: void ptosflum(int,P,LUM);
1.5 noro 537: void sfhenmain2(BM,UM,UM,int,BM *);
538: void ptosfbm(int,P,BM);
1.3 noro 539:
540: /* f = f(x,y) */
541:
542: void sfhensel(count,f,x,listp)
543: int count;
544: P f;
545: V x;
546: ML *listp;
547: {
548: int i,j;
549: int fn,n,bound;
1.4 noro 550: ML rlist;
1.5 noro 551: BM fl;
1.3 noro 552: VL vl,nvl;
553: V y;
1.4 noro 554: int dx,dy,mev;
1.3 noro 555: GFS ev;
1.4 noro 556: P f1,t,yev,c;
1.3 noro 557: DCP dc;
1.4 noro 558: UM w,w1,q,fm,hm;
559: UM *gm;
1.3 noro 560:
561: clctv(CO,f,&vl);
562: if ( vl->v != x ) {
563: reordvar(vl,x,&nvl); reorderp(nvl,vl,f,&f1);
564: vl = nvl; f = f1;
565: }
566: y = vl->next->v;
567: dx = getdeg(x,f);
568: dy = getdeg(y,f);
569: if ( dx == 1 ) {
1.4 noro 570: *listp = rlist = MLALLOC(1); rlist->n = 1; rlist->c[0] = 0;
1.3 noro 571: return;
572: }
573: fn = sfberle(vl,f,count,&ev,&dc);
574: if ( fn <= 1 ) {
575: /* fn == 0 => short of evaluation points */
1.4 noro 576: *listp = rlist = MLALLOC(1); rlist->n = fn; rlist->c[0] = 0;
1.3 noro 577: return;
578: }
579: /* pass the the leading coeff. to the first element */
580: c = dc->c; dc = NEXT(dc);
581: mulp(vl,dc->c,c,&t); dc->c = t;
1.4 noro 582:
583: /* convert mod y-a factors into UM */
584: gm = (UM *)ALLOCA(fn*sizeof(UM));
1.3 noro 585: for ( i = 0; i < fn; i++, dc = NEXT(dc) ) {
1.4 noro 586: gm[i] = W_UMALLOC(UDEG(dc->c));
587: ptosfum(dc->c,gm[i]);
1.3 noro 588: }
1.4 noro 589:
590: bound = dy+1;
591: /* f(x,y) -> f(x,y+ev) */
1.5 noro 592: fl = BMALLOC(dx,bound);
593: ptosfbm(bound,f,fl);
594: shiftsfbm(bound,fl,FTOIF(CONT(ev)));
1.4 noro 595:
596: /* fm = fl mod y */
597: fm = W_UMALLOC(dx);
1.5 noro 598: cpyum(COEF(fl)[0],fm);
1.4 noro 599: hm = W_UMALLOC(dx);
600:
601: q = W_UMALLOC(dx);
602: rlist = MLALLOC(fn); rlist->n = fn; rlist->bound = bound;
1.6 ! noro 603: for ( i = fn-1; i >= 1; i-- ) {
1.4 noro 604: fprintf(stderr,"%d\n",i);
605: /* fl = gm[i]*hm mod y */
606: divsfum(fm,gm[i],hm);
607: /* fl is replaced by the cofactor of gk mod y^bound */
608: /* rlist->c[i] = gk */
1.5 noro 609: sfhenmain2(fl,gm[i],hm,bound,(BM *)&rlist->c[i]);
1.4 noro 610: cpyum(hm,fm);
611: }
1.6 ! noro 612: /* finally, fl must be the lift of gm[0] */
1.4 noro 613: rlist->c[i] = fl;
614:
615: /* y -> y-a */
616: w = W_UMALLOC(bound);
617: w1 = W_UMALLOC(bound);
618: mev = _chsgnsf(FTOIF(CONT(ev)));
619: for ( i = 0; i < fn; i++ )
1.5 noro 620: shiftsfbm(bound,(BM)(rlist->c[i]),mev);
1.4 noro 621: *listp = rlist;
1.3 noro 622: }
623:
624: /* main variable of f = x */
625:
626: int sfberle(vl,f,count,ev,dcp)
627: VL vl;
628: P f;
629: int count;
630: GFS *ev;
631: DCP *dcp;
632: {
633: UM wf,wf1,wf2,wfs,gcd;
634: ML flist;
635: int fn,fn1,n;
636: GFS m,fm;
637: DCP dc,dct,dc0;
638: VL nvl;
639: V x,y;
640: P g,lc,lc0,f0;
641: int j,q1,index,i;
642:
643: clctv(vl,f,&nvl); vl = nvl;
644: x = vl->v; y = vl->next->v;
645: simp_ff(f,&g); g = f;
646: n = QTOS(DEG(DC(f)));
647: wf = W_UMALLOC(n); wf1 = W_UMALLOC(n); wf2 = W_UMALLOC(n);
648: wfs = W_UMALLOC(n); gcd = W_UMALLOC(n);
649: q1 = field_order_sf()-1;
650: lc = DC(f)->c;
651: for ( j = 0, fn = n + 1, index = 0;
1.4 noro 652: index < q1 && j < count && fn > 1; index++ ) {
1.3 noro 653: MKGFS(index,m);
654: substp(vl,lc,y,(P)m,&lc0);
655: if ( lc0 ) {
656: substp(vl,f,y,(P)m,&f0);
1.4 noro 657: ptosfum(f0,wf); cpyum(wf,wf1);
658: diffsfum(wf1,wf2); gcdsfum(wf1,wf2,gcd);
1.3 noro 659: if ( DEG(gcd) == 0 ) {
660: fctrsf(f0,&dc);
661: for ( dct = NEXT(dc), i = 0; dct; dct = NEXT(dct), i++ );
662: if ( i < fn ) {
663: dc0 = dc; fn = i; fm = m;
664: }
665: j++;
666: }
667: }
668: }
669: if ( index == q1 )
670: return 0;
671: else if ( fn == 1 )
672: return 1;
673: else {
674: *dcp = dc0;
675: *ev = fm;
676: return fn;
677: }
678: }
679:
680: void sfgcdgen(f,blist,clistp)
681: P f;
682: ML blist,*clistp;
683: {
684: int i;
685: int n,d,np;
686: UM wf,wm,wx,wy,wu,wv,wa,wb,wg,q,tum;
687: UM *in,*out;
688: ML clist;
689:
690: n = UDEG(f); np = blist->n;
691: d = 2*n;
692: q = W_UMALLOC(d); wf = W_UMALLOC(d);
693: wm = W_UMALLOC(d); wx = W_UMALLOC(d);
694: wy = W_UMALLOC(d); wu = W_UMALLOC(d);
695: wv = W_UMALLOC(d); wg = W_UMALLOC(d);
696: wa = W_UMALLOC(d); wb = W_UMALLOC(d);
697: ptosfum(f,wf); DEG(wg) = 0; COEF(wg)[0] = _onesf();
698: *clistp = clist = MLALLOC(np); clist->n = np;
699: for ( i = 0, in = (UM *)blist->c, out = (UM *)clist->c; i < np; i++ ) {
700: divsfum(wf,in[i],q); tum = wf; wf = q; q = tum;
701: cpyum(wf,wx); cpyum(in[i],wy);
702: eucsfum(wx,wy,wa,wb); mulsfum(wa,wg,wm);
703: DEG(wm) = divsfum(wm,in[i],q); out[i] = UMALLOC(DEG(wm));
704: cpyum(wm,out[i]); mulsfum(q,wf,wu);
705: mulsfum(wg,wb,wv); addsfum(wu,wv,wg);
706: }
707: }
708:
709: /*
710: sfhenmain(fl,bqlist,cqlist,listp)
711: */
712:
713: void sfhenmain(f,bqlist,cqlist,listp)
714: LUM f;
715: ML bqlist,cqlist,*listp;
716: {
717: int i,j,k;
718: int *px,*py;
719: int **pp,**pp1;
720: int n,np,bound,dr,tmp;
721: UM wt,wq0,wq,wr,wm,wm0,wa,q;
722: LUM wb0,wb1,tlum;
723: UM *b,*c;
724: LUM *l;
725: ML list;
726:
727: n = DEG(f); np = bqlist->n; bound = bqlist->bound;
728: *listp = list = MLALLOC(n);
729: list->n = np; list->bound = bound;
730: W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
731: wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
732: wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
733: wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
734: b = (UM *)bqlist->c; c = (UM *)cqlist->c; l = (LUM *)list->c;
735: for ( i = 0; i < np; i++ ) {
736: l[i] = LUMALLOC(DEG(b[i]),bound);
737: for ( j = DEG(b[i]), pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
738: pp[j][0] = px[j];
739: }
740: for ( i = 1; i < bound; i++ ) {
1.4 noro 741: fprintf(stderr,".");
742: /* at this point, f = l[0]*l[1]*...*l[np-1] mod y^i */
1.3 noro 743: mulsflum(i+1,l[0],l[1],wb0);
744: for ( j = 2; j < np; j++ ) {
745: mulsflum(i+1,l[j],wb0,wb1);
746: tlum = wb0; wb0 = wb1; wb1 = tlum;
747: }
1.5 noro 748: #if 0
1.4 noro 749: /* check */
750: for ( j = 0, pp = COEF(f), pp1 = COEF(wb0); j <= n; j++ )
751: for ( k = 0; k < i; k++ )
752: if ( pp[j][k] != pp1[j][k] )
753: error("henmain : cannot happen");
754: #endif
1.3 noro 755: for ( j = n, px = COEF(wt); j >= 0; j-- )
756: px[j] = 0;
757: for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- )
758: COEF(wt)[j] = _subsf(pp[j][i],pp1[j][i]);
759: degum(wt,n);
760: for ( j = n, px = COEF(wq0); j >= 0; j-- )
761: px[j] = 0;
762: for ( j = 1; j < np; j++ ) {
763: mulsfum(wt,c[j],wm); dr = divsfum(wm,b[j],q);
764: for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
765: px[k] = _addsf(px[k],py[k]);
766: for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
767: pp[k][i] = px[k];
768: }
769: degum(wq0,n); mulsfum(wq0,b[0],wm);
770: mulsfum(wt,c[0],wm0); addsfum(wm,wm0,wa);
771: for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
772: pp[j][i] = px[j];
773: for ( j = n, px = COEF(wq0); j >= 0; j-- )
774: px[j] = 0;
775: }
1.4 noro 776: fprintf(stderr,"\n");
777: }
778:
779: /* f = g0*h0 mod y -> f = gk*hk mod y^bound, f is replaced by hk */
780:
781: void sfhenmain2(f,g0,h0,bound,gp)
1.5 noro 782: BM f;
1.4 noro 783: UM g0,h0;
784: int bound;
1.5 noro 785: BM *gp;
1.4 noro 786: {
787: int i,j,k,l;
788: int *px,*py;
789: int **pp,**pp1;
790: int n,np,dr,tmp;
1.5 noro 791: UM wt,wa,wb,wq,wm,q,w1,w2,wh1,wg1,ws;
1.4 noro 792: UM wc,wd,we,wz;
1.5 noro 793: BM wb0,wb1;
1.4 noro 794: int ng,nh;
1.5 noro 795: BM fk,gk,hk;
1.4 noro 796:
797: n = f->d;
798: ng = g0->d;
799: nh = h0->d;
800:
1.5 noro 801: W_BMALLOC(n,bound,wb0); W_BMALLOC(n,bound,wb1);
802: wt = W_UMALLOC(n); ws = W_UMALLOC(n);
803: wq = W_UMALLOC(n); q = W_UMALLOC(2*n);
1.4 noro 804: wg1 = W_UMALLOC(2*n); wh1 = W_UMALLOC(2*n);
1.5 noro 805:
1.4 noro 806: /* fk = gk*hk mod y^k */
1.5 noro 807: W_BMALLOC(n,bound,fk); cpyum(COEF(f)[0],COEF(fk)[0]);
808: gk = BMALLOC(ng,bound); cpyum(g0,COEF(gk)[0]);
809: W_BMALLOC(nh,bound,hk);
810: cpyum(h0,COEF(hk)[0]);
1.4 noro 811:
1.5 noro 812: wc = W_UMALLOC(2*n); wd = W_UMALLOC(2*n);
813: we = W_UMALLOC(2*n); wz = W_UMALLOC(2*n);
1.4 noro 814:
815: /* compute wa,wb s.t. wa*g0+wb*h0 = 1 mod y */
816: w1 = W_UMALLOC(ng); cpyum(g0,w1);
817: w2 = W_UMALLOC(nh); cpyum(h0,w2);
818: wa = W_UMALLOC(2*n); wb = W_UMALLOC(2*n); /* XXX */
819: eucsfum(w1,w2,wa,wb);
820:
821: #if 0
822: mulsfum(wa,g0,wc); mulsfum(wb,h0,wd); addsfum(wc,wd,we);
823: if ( DEG(we) != 0 || COEF(we)[0] != _onesf() )
1.5 noro 824: error("henmain2 : cannot happen(euc)");
1.4 noro 825: #endif
826:
827: fprintf(stderr,"bound=%d\n",bound);
828: for ( k = 1; k < bound; k++ ) {
829: fprintf(stderr,".");
830:
831: /* at this point, f = gk*hk mod y^k */
832: #if 0
1.5 noro 833: for ( j = 0; j < k; j++ )
834: if ( !isequalum(COEF(f)[j],COEF(fk)[j]) )
835: error("henmain2 : cannot happen(history)");
1.4 noro 836: #endif
837:
838: /* clear wt */
839: bzero(COEF(wt),(n+1)*sizeof(int));
840:
841: /* wt = (f-gk*hk)/y^k */
1.5 noro 842: subsfum(COEF(f)[k],COEF(fk)[k],wt);
1.4 noro 843:
844: /* clear wq */
845: bzero(COEF(wq),(n+1)*sizeof(int));
846:
847: /* compute wf1,wg1 s.t. wh1*g0+wg1*h0 = wt */
848: mulsfum(wa,wt,wh1); DEG(wh1) = divsfum(wh1,h0,q);
849: mulsfum(wh1,g0,wc); subsfum(wt,wc,wd); DEG(wd) = divsfum(wd,h0,wg1);
850:
851: /* check */
852: #if 0
853: if ( DEG(wd) >= 0 || DEG(wg1) > ng )
1.5 noro 854: error("henmain2 : cannot happen(adj)");
1.4 noro 855:
856: mulsfum(wg1,h0,wc); mulsfum(wh1,g0,wd); addsfum(wc,wd,we);
857: subsfum(we,wt,wz);
858: if ( DEG(wz) >= 0 )
859: error("henmain2 : cannot happen");
860: #endif
861:
862: /* fk += ((wg1*hk+wh1*gk)*y^k+wg1*wh1*y^(2*k) mod y^bound */
863: /* wb0 = wh1*y^k */
1.5 noro 864: clearsfbm(bound,n,wb0);
865: DEG(wb0) = bound;
866: cpyum(wh1,COEF(wb0)[k]);
867:
1.4 noro 868: /* wb1 = gk*wb0 mod y^bound */
1.5 noro 869: clearsfbm(bound,n,wb1);
870: mulsfbm(bound,gk,wb0,wb1);
1.4 noro 871: /* fk += wb1 */
1.5 noro 872: addtosfbm(bound,wb1,fk);
1.4 noro 873:
874: /* wb0 = wg1*y^k */
1.5 noro 875: clearsfbm(bound,n,wb0);
876: DEG(wb0) = bound;
877: cpyum(wg1,COEF(wb0)[k]);
878:
1.4 noro 879: /* wb1 = hk*wb0 mod y^bound */
1.5 noro 880: clearsfbm(bound,n,wb1);
881: mulsfbm(bound,hk,wb0,wb1);
1.4 noro 882: /* fk += wb1 */
1.5 noro 883: addtosfbm(bound,wb1,fk);
1.4 noro 884:
885: /* fk += wg1*wh1*y^(2*k) mod y^bound */
886: if ( 2*k < bound ) {
1.5 noro 887: mulsfum(wg1,wh1,wt); addsfum(COEF(fk)[2*k],wt,ws);
888: cpyum(ws,COEF(fk)[2*k]);
1.4 noro 889: }
890:
891: /* gk += wg1*y^k, hk += wh1*y^k */
1.5 noro 892: cpyum(wg1,COEF(gk)[k]);
893: cpyum(wh1,COEF(hk)[k]);
1.4 noro 894: }
895: fprintf(stderr,"\n");
896: *gp = gk;
1.5 noro 897: DEG(f) = bound;
898: for ( i = 0; i < bound; i++ )
899: cpyum(COEF(hk)[i],COEF(f)[i]);
1.3 noro 900: }
901:
902: void ptosflum(bound,f,fl)
903: int bound;
904: P f;
905: LUM fl;
906: {
907: DCP dc;
908: int **pp;
909: int d;
910: UM t;
911:
912: t = UMALLOC(bound);
913: for ( dc = DC(f), pp = COEF(fl); dc; dc = NEXT(dc) ) {
914: d = QTOS(DEG(dc));
915: ptosfum(COEF(dc),t);
916: bcopy(t->c,pp[d],(t->d+1)*sizeof(int));
917: }
1.4 noro 918: }
919:
1.5 noro 920: /* fl->c[i] = coef_y(f,i) */
921:
922: void ptosfbm(bound,f,fl)
923: int bound;
924: P f;
925: BM fl;
926: {
927: DCP dc;
928: int d,i,n;
929: UM t;
930:
931: DEG(fl) = bound;
932: t = UMALLOC(bound);
933: for ( dc = DC(f); dc; dc = NEXT(dc) ) {
934: d = QTOS(DEG(dc));
935: ptosfum(COEF(dc),t);
936: for ( i = 0; i <= DEG(t); i++ )
937: COEF(COEF(fl)[i])[d] = COEF(t)[i];
938: }
939: n = UDEG(f);
940: for ( i = 0; i <= n; i++ )
941: degum(COEF(fl)[i],n);
942: }
943:
1.4 noro 944: /* x : main variable */
945:
946: void sflumtop(bound,fl,x,y,fp)
947: int bound;
948: LUM fl;
949: V x,y;
950: P *fp;
951: {
952: int i,j,n;
953: int **c;
954: UM w;
955: int *coef;
956: DCP dc,dct;
957:
958: n = fl->d;
959: c = fl->c;
960: w = W_UMALLOC(bound);
961: for ( i = 0, dc = 0; i <= n; i++ ) {
962: coef = c[i];
963: for ( j = bound-1; j >= 0 && coef[j] == 0; j-- );
964: if ( j < 0 )
965: continue;
966: DEG(w) = j; bcopy(coef,COEF(w),(j+1)*sizeof(int));
967: NEWDC(dct); STOQ(i,DEG(dct));
968: sfumtop(y,w,&COEF(dct)); NEXT(dct) = dc; dc = dct;
969: }
970: MKP(x,dc,*fp);
1.5 noro 971: }
972:
973: /* x : main variable */
974:
975: void sfbmtop(vl,bound,f,x,y,fp)
976: VL vl;
977: int bound;
978: BM f;
979: V x,y;
980: P *fp;
981: {
982: UM *c;
983: P yv,r,s,t,u,yvd;
984: Q d;
985: int i;
986:
987: c = f->c;
988: MKV(y,yv);
989: r = 0;
990: for ( i = 0; i < bound; i++ ) {
991: STOQ(i,d); sfumtop(x,c[i],&t);
992: pwrp(vl,yv,d,&yvd);
993: mulp(vl,t,yvd,&s);
994: addp(vl,r,s,&u); r = u;
995: }
996: *fp = r;
1.3 noro 997: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>