Annotation of OpenXM_contrib2/asir2000/engine/Hgfs.c, Revision 1.28
1.28 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Hgfs.c,v 1.27 2002/11/01 05:43:35 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.25 noro 19: void ufctrsf(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:
1.28 ! noro 509: int sfberle(V,V,P,int,GFS *,DCP *);
1.3 noro 510: void sfgcdgen(P,ML,ML *);
1.5 noro 511: void sfhenmain2(BM,UM,UM,int,BM *);
512: void ptosfbm(int,P,BM);
1.28 ! noro 513: void sfhensel(int count,P f,V x,V y,int degbound,GFS *evp,P *sfp,ML *listp);
1.3 noro 514:
515: /* f = f(x,y) */
516:
1.28 ! noro 517: void sfhensel(int count,P f,V x,V y,int degbound,GFS *evp,P *sfp,ML *listp)
1.3 noro 518: {
1.18 noro 519: int i;
520: int fn;
1.4 noro 521: ML rlist;
1.5 noro 522: BM fl;
1.3 noro 523: VL vl,nvl;
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: }
1.28 ! noro 537: if ( vl->next )
! 538: y = vl->next->v;
1.3 noro 539: dx = getdeg(x,f);
540: dy = getdeg(y,f);
541: if ( dx == 1 ) {
1.4 noro 542: *listp = rlist = MLALLOC(1); rlist->n = 1; rlist->c[0] = 0;
1.3 noro 543: return;
544: }
1.28 ! noro 545: fn = sfberle(x,y,f,count,&ev,&dc);
1.3 noro 546: if ( fn <= 1 ) {
547: /* fn == 0 => short of evaluation points */
1.4 noro 548: *listp = rlist = MLALLOC(1); rlist->n = fn; rlist->c[0] = 0;
1.3 noro 549: return;
550: }
1.21 noro 551: if ( degbound >= 0 ) {
552: /*
553: * reconstruct dc so that
554: * dc[1],... : factors satisfy degree bound
555: * dc[0] : product of others
556: */
557: c = dc->c; dc = NEXT(dc);
558: dc0 = 0;
559: fn = 0;
560: while ( dc ) {
561: if ( getdeg(x,COEF(dc)) <= degbound ) {
562: dct = NEXT(dc); NEXT(dc) = dc0; dc0 = dc; dc = dct;
563: fn++;
564: } else {
565: mulp(vl,COEF(dc),c,&t); c = t;
566: dc = NEXT(dc);
567: }
568: }
569: if ( OID(c) == O_P ) {
570: NEWDC(dc); COEF(dc) = c; DEG(dc) = ONE; NEXT(dc) = dc0;
571: fn++;
572: } else {
573: mulp(vl,dc0->c,c,&t); dc0->c = t; dc = dc0;
574: }
575: } else {
576: /* pass the the leading coeff. to the first element */
577: c = dc->c; dc = NEXT(dc);
578: mulp(vl,dc->c,c,&t); dc->c = t;
579: }
1.4 noro 580:
581: /* convert mod y-a factors into UM */
582: gm = (UM *)ALLOCA(fn*sizeof(UM));
1.3 noro 583: for ( i = 0; i < fn; i++, dc = NEXT(dc) ) {
1.4 noro 584: gm[i] = W_UMALLOC(UDEG(dc->c));
585: ptosfum(dc->c,gm[i]);
1.3 noro 586: }
1.4 noro 587:
1.19 noro 588: /* set bound */
1.20 noro 589: /* g | f, lc_y(g) = lc_y(f) => deg_y(g) <= deg_y(f) */
590: /* so, bound = dy is sufficient, but we use slightly large value */
1.19 noro 591: bound = dy+2;
592:
1.4 noro 593: /* f(x,y) -> f(x,y+ev) */
1.19 noro 594: fl = BMALLOC(dx,bound);
595: ptosfbm(bound,f,fl);
1.15 noro 596: if ( ev ) shiftsfbm(fl,FTOIF(CONT(ev)));
1.4 noro 597:
1.8 noro 598: /* sf = f(x+ev) */
1.14 noro 599: sfbmtop(fl,x,y,&sf);
1.8 noro 600:
1.4 noro 601: /* fm = fl mod y */
602: fm = W_UMALLOC(dx);
1.5 noro 603: cpyum(COEF(fl)[0],fm);
1.4 noro 604: hm = W_UMALLOC(dx);
605:
606: q = W_UMALLOC(dx);
1.19 noro 607: rlist = MLALLOC(fn); rlist->n = fn; rlist->bound = bound;
1.12 noro 608: fprintf(asir_out,"%d candidates\n",fn);
609: init_eg(&eg_hensel);
1.7 noro 610: for ( i = 0; i < fn-1; i++ ) {
1.12 noro 611: fprintf(asir_out,"deg(fm) = %d, deg(gm[%d]) = %d\n",
612: DEG(fm),i,DEG(gm[i]));
613: init_eg(&eg_hensel_t);
614: get_eg(&tmp0);
1.4 noro 615: /* fl = gm[i]*hm mod y */
616: divsfum(fm,gm[i],hm);
1.19 noro 617: /* fl is replaced by the cofactor of gk mod y^bound */
1.4 noro 618: /* rlist->c[i] = gk */
1.19 noro 619: sfhenmain2(fl,gm[i],hm,bound,(BM *)&rlist->c[i]);
1.4 noro 620: cpyum(hm,fm);
1.12 noro 621: get_eg(&tmp1); add_eg(&eg_hensel_t,&tmp0,&tmp1);
622: add_eg(&eg_hensel,&tmp0,&tmp1);
623: print_eg("Hensel",&eg_hensel_t);
624: fprintf(asir_out,"\n");
1.4 noro 625: }
1.12 noro 626: print_eg("Hensel total",&eg_hensel);
627: fprintf(asir_out,"\n");
1.7 noro 628: /* finally, fl must be the lift of gm[fn-1] */
1.4 noro 629: rlist->c[i] = fl;
630:
1.8 noro 631: #if 0
1.4 noro 632: /* y -> y-a */
633: mev = _chsgnsf(FTOIF(CONT(ev)));
634: for ( i = 0; i < fn; i++ )
1.14 noro 635: shiftsfbm((BM)(rlist->c[i]),mev);
1.8 noro 636: #endif
637: *evp = ev;
638: *sfp = sf;
1.4 noro 639: *listp = rlist;
1.3 noro 640: }
641:
642: /* main variable of f = x */
643:
1.28 ! noro 644: int sfberle(V x,V y,P f,int count,GFS *ev,DCP *dcp)
1.3 noro 645: {
646: UM wf,wf1,wf2,wfs,gcd;
1.18 noro 647: int fn,n;
1.3 noro 648: GFS m,fm;
649: DCP dc,dct,dc0;
1.28 ! noro 650: VL vl;
1.18 noro 651: P lc,lc0,f0;
652: Obj obj;
1.23 noro 653: int j,q,index,i;
1.3 noro 654:
1.28 ! noro 655: NEWVL(vl); vl->v = x;
! 656: NEWVL(NEXT(vl)); NEXT(vl)->v = y;
! 657: NEXT(NEXT(vl)) =0;
1.18 noro 658: simp_ff((Obj)f,&obj); f = (P)obj;
1.3 noro 659: n = QTOS(DEG(DC(f)));
660: wf = W_UMALLOC(n); wf1 = W_UMALLOC(n); wf2 = W_UMALLOC(n);
661: wfs = W_UMALLOC(n); gcd = W_UMALLOC(n);
1.23 noro 662: q = field_order_sf();
1.3 noro 663: lc = DC(f)->c;
664: for ( j = 0, fn = n + 1, index = 0;
1.23 noro 665: index < q && j < count && fn > 1; index++ ) {
666: indextogfs(index,&m);
1.3 noro 667: substp(vl,lc,y,(P)m,&lc0);
668: if ( lc0 ) {
669: substp(vl,f,y,(P)m,&f0);
1.4 noro 670: ptosfum(f0,wf); cpyum(wf,wf1);
671: diffsfum(wf1,wf2); gcdsfum(wf1,wf2,gcd);
1.3 noro 672: if ( DEG(gcd) == 0 ) {
1.25 noro 673: ufctrsf(f0,&dc);
1.3 noro 674: for ( dct = NEXT(dc), i = 0; dct; dct = NEXT(dct), i++ );
675: if ( i < fn ) {
676: dc0 = dc; fn = i; fm = m;
677: }
678: j++;
679: }
680: }
681: }
1.23 noro 682: if ( index == q )
1.3 noro 683: return 0;
684: else if ( fn == 1 )
685: return 1;
686: else {
687: *dcp = dc0;
688: *ev = fm;
689: return fn;
690: }
691: }
692:
1.18 noro 693: void sfgcdgen(P f,ML blist,ML *clistp)
1.3 noro 694: {
695: int i;
696: int n,d,np;
697: UM wf,wm,wx,wy,wu,wv,wa,wb,wg,q,tum;
698: UM *in,*out;
699: ML clist;
700:
701: n = UDEG(f); np = blist->n;
702: d = 2*n;
703: q = W_UMALLOC(d); wf = W_UMALLOC(d);
704: wm = W_UMALLOC(d); wx = W_UMALLOC(d);
705: wy = W_UMALLOC(d); wu = W_UMALLOC(d);
706: wv = W_UMALLOC(d); wg = W_UMALLOC(d);
707: wa = W_UMALLOC(d); wb = W_UMALLOC(d);
708: ptosfum(f,wf); DEG(wg) = 0; COEF(wg)[0] = _onesf();
709: *clistp = clist = MLALLOC(np); clist->n = np;
710: for ( i = 0, in = (UM *)blist->c, out = (UM *)clist->c; i < np; i++ ) {
711: divsfum(wf,in[i],q); tum = wf; wf = q; q = tum;
712: cpyum(wf,wx); cpyum(in[i],wy);
713: eucsfum(wx,wy,wa,wb); mulsfum(wa,wg,wm);
714: DEG(wm) = divsfum(wm,in[i],q); out[i] = UMALLOC(DEG(wm));
715: cpyum(wm,out[i]); mulsfum(q,wf,wu);
716: mulsfum(wg,wb,wv); addsfum(wu,wv,wg);
717: }
718: }
719:
1.14 noro 720: /* f = g0*h0 mod y -> f = gk*hk mod y^(dy+1), f is replaced by hk */
1.3 noro 721:
1.18 noro 722: void sfhenmain2(BM f,UM g0,UM h0,int dy,BM *gp)
1.4 noro 723: {
1.18 noro 724: int i,k;
725: int dx;
726: UM wt,wa,wb,q,w1,w2,wh1,wg1,ws;
1.4 noro 727: UM wc,wd,we,wz;
1.5 noro 728: BM wb0,wb1;
1.14 noro 729: int dg,dh;
1.5 noro 730: BM fk,gk,hk;
1.4 noro 731:
1.14 noro 732: if ( DEG(f) < dy )
733: error("sfhenmain2 : invalid input");
734:
735: dx = degbm(f);
736: dg = DEG(g0);
737: dh = DEG(h0);
738:
739: W_BMALLOC(dx,dy,wb0); W_BMALLOC(dx,dy,wb1);
740: wt = W_UMALLOC(dx); ws = W_UMALLOC(dx); q = W_UMALLOC(2*dx);
741: wg1 = W_UMALLOC(2*dx); wh1 = W_UMALLOC(2*dx);
1.5 noro 742:
1.4 noro 743: /* fk = gk*hk mod y^k */
1.14 noro 744: W_BMALLOC(dx,dy,fk);
1.8 noro 745: cpyum(COEF(f)[0],COEF(fk)[0]);
1.14 noro 746: gk = BMALLOC(dg,dy);
1.8 noro 747: cpyum(g0,COEF(gk)[0]);
1.14 noro 748: W_BMALLOC(dh,dy,hk);
1.5 noro 749: cpyum(h0,COEF(hk)[0]);
1.4 noro 750:
1.14 noro 751: wc = W_UMALLOC(2*dx); wd = W_UMALLOC(2*dx);
752: we = W_UMALLOC(2*dx); wz = W_UMALLOC(2*dx);
1.4 noro 753:
754: /* compute wa,wb s.t. wa*g0+wb*h0 = 1 mod y */
1.14 noro 755: w1 = W_UMALLOC(dg); cpyum(g0,w1);
756: w2 = W_UMALLOC(dh); cpyum(h0,w2);
757: wa = W_UMALLOC(2*dx); wb = W_UMALLOC(2*dx); /* XXX */
1.4 noro 758: eucsfum(w1,w2,wa,wb);
759:
1.14 noro 760: fprintf(stderr,"dy=%d\n",dy);
761: for ( k = 1; k <= dy; k++ ) {
1.4 noro 762: fprintf(stderr,".");
763:
764: /* at this point, f = gk*hk mod y^k */
765:
766: /* clear wt */
1.14 noro 767: clearum(wt,dx);
1.4 noro 768:
769: /* wt = (f-gk*hk)/y^k */
1.5 noro 770: subsfum(COEF(f)[k],COEF(fk)[k],wt);
1.4 noro 771:
772: /* compute wf1,wg1 s.t. wh1*g0+wg1*h0 = wt */
773: mulsfum(wa,wt,wh1); DEG(wh1) = divsfum(wh1,h0,q);
774: mulsfum(wh1,g0,wc); subsfum(wt,wc,wd); DEG(wd) = divsfum(wd,h0,wg1);
775:
776: /* check */
777: #if 0
778: if ( DEG(wd) >= 0 || DEG(wg1) > ng )
1.5 noro 779: error("henmain2 : cannot happen(adj)");
1.4 noro 780:
781: mulsfum(wg1,h0,wc); mulsfum(wh1,g0,wd); addsfum(wc,wd,we);
782: subsfum(we,wt,wz);
783: if ( DEG(wz) >= 0 )
784: error("henmain2 : cannot happen");
785: #endif
786:
1.14 noro 787: /* fk += ((wg1*hk+wh1*gk)*y^k+wg1*wh1*y^(2*k) mod y^(dy+1) */
1.4 noro 788: /* wb0 = wh1*y^k */
1.14 noro 789: clearbm(dx,wb0);
1.5 noro 790: cpyum(wh1,COEF(wb0)[k]);
791:
1.14 noro 792: /* wb1 = gk*wb0 mod y^(dy+1) */
793: clearbm(dx,wb1);
794: mulsfbm(gk,wb0,wb1);
1.4 noro 795: /* fk += wb1 */
1.14 noro 796: addtosfbm(wb1,fk);
1.4 noro 797:
798: /* wb0 = wg1*y^k */
1.14 noro 799: clearbm(dx,wb0);
1.5 noro 800: cpyum(wg1,COEF(wb0)[k]);
801:
1.14 noro 802: /* wb1 = hk*wb0 mod y^(dy+1) */
803: clearbm(dx,wb1);
804: mulsfbm(hk,wb0,wb1);
1.4 noro 805: /* fk += wb1 */
1.14 noro 806: addtosfbm(wb1,fk);
1.4 noro 807:
1.14 noro 808: /* fk += wg1*wh1*y^(2*k) mod y^(dy+1) */
809: if ( 2*k <= dy ) {
1.5 noro 810: mulsfum(wg1,wh1,wt); addsfum(COEF(fk)[2*k],wt,ws);
811: cpyum(ws,COEF(fk)[2*k]);
1.4 noro 812: }
813:
814: /* gk += wg1*y^k, hk += wh1*y^k */
1.5 noro 815: cpyum(wg1,COEF(gk)[k]);
816: cpyum(wh1,COEF(hk)[k]);
1.4 noro 817: }
818: fprintf(stderr,"\n");
819: *gp = gk;
1.14 noro 820: DEG(f) = dy;
821: for ( i = 0; i <= dy; i++ )
1.5 noro 822: cpyum(COEF(hk)[i],COEF(f)[i]);
1.27 noro 823: }
824:
825: /* a0*g+b0*h = 1 mod y -> a*g+b*h = 1 mod y^(dy+1) */
826:
827: void sfexgcd_by_hensel(BM g,BM h,int dy,BM *ap,BM *bp)
828: {
829: int i,k;
830: int dx;
831: UM wt,wa,wb,q,w1,w2,ws;
832: UM wc,wd,we,wz,wa1,wb1;
833: BM wz0,wz1;
834: int dg,dh;
835: BM a,b,c;
836:
837: dg = degbm(g);
838: dh = degbm(h);
839: dx = dg+dh;
840:
841: a = BMALLOC(dh,dy);
842: b = BMALLOC(dg,dy);
843: /* c holds a*g+b*h-1 */
844: c = BMALLOC(dg+dh,dy);
845:
846: W_BMALLOC(dx,dy,wz0); W_BMALLOC(dx,dy,wz1);
847:
848: wt = W_UMALLOC(dx); ws = W_UMALLOC(dx); q = W_UMALLOC(2*dx);
849: wa1 = W_UMALLOC(2*dx); wb1 = W_UMALLOC(2*dx);
850: wc = W_UMALLOC(2*dx); wd = W_UMALLOC(2*dx);
851: we = W_UMALLOC(2*dx); wz = W_UMALLOC(2*dx);
852:
853: /* compute wa,wb s.t. wa*g0+wb*h0 = 1 mod y */
854: w1 = W_UMALLOC(dg); cpyum(COEF(g)[0],w1);
855: w2 = W_UMALLOC(dh); cpyum(COEF(h)[0],w2);
856: wa = W_UMALLOC(2*dx); wb = W_UMALLOC(2*dx); /* XXX */
857: eucsfum(w1,w2,wa,wb);
858: cpyum(wa,COEF(a)[0]); cpyum(wb,COEF(b)[0]);
859:
860: /* initialize c to a*g+b*h-1 */
861: mulsfbm(a,g,c); mulsfbm(b,h,wz0); addtosfbm(wz0,c);
862: COEF(COEF(c)[0])[0] = 0;
863:
864: fprintf(stderr,"dy=%d\n",dy);
865: for ( k = 1; k <= dy; k++ ) {
866: fprintf(stderr,".");
867:
868: /* at this point, a*g+b*h = 1 mod y^k, c = a*g+b*h-1 */
869:
870: /* wt = -((a*g+b*h-1)/y^k) */
871: cpyum(COEF(c)[k],wt);
872: for ( i = DEG(wt); i >= 0; i-- )
873: COEF(wt)[i] = _chsgnsf(COEF(wt)[i]);
874:
875: /* compute wa1,wb1 s.t. wa1*g0+wb1*h0 = wt */
876: mulsfum(wa,wt,wa1); DEG(wa1) = divsfum(wa1,COEF(h)[0],q);
877: mulsfum(wa1,COEF(g)[0],wc); subsfum(wt,wc,wd);
878: DEG(wd) = divsfum(wd,COEF(h)[0],wb1);
879:
880: /* c += ((wa1*g+wb1*h)*y^k mod y^(dy+1) */
881: /* wz0 = wa1*y^k */
882: clearbm(dx,wz0);
883: cpyum(wa1,COEF(wz0)[k]);
884:
885: /* wz1 = wz0*g mod y^(dy+1) */
886: clearbm(dx,wz1);
887: mulsfbm(g,wz0,wz1);
888: /* c += wz1 */
889: addtosfbm(wz1,c);
890:
891: /* wz0 = wb1*y^k */
892: clearbm(dx,wz0);
893: cpyum(wb1,COEF(wz0)[k]);
894:
895: /* wz1 = wz0*h mod y^(dy+1) */
896: clearbm(dx,wz1);
897: mulsfbm(h,wz0,wz1);
898: /* c += wz1 */
899: addtosfbm(wz1,c);
900:
901: /* a += wa1*y^k, b += wb1*y^k */
902: cpyum(wa1,COEF(a)[k]);
903: cpyum(wb1,COEF(b)[k]);
904: }
905: fprintf(stderr,"\n");
906: DEG(a) = dy;
907: DEG(b) = dy;
908: *ap = a;
909: *bp = b;
1.3 noro 910: }
911:
1.5 noro 912: /* fl->c[i] = coef_y(f,i) */
913:
1.18 noro 914: void ptosfbm(int dy,P f,BM fl)
1.5 noro 915: {
916: DCP dc;
1.14 noro 917: int d,i,dx;
1.5 noro 918: UM t;
919:
1.14 noro 920: dx = QTOS(DEG(DC(f)));
921: if ( DEG(fl) < dy )
922: error("ptosfbm : invalid input");
923: DEG(fl) = dy;
924: clearbm(dx,fl);
925: t = UMALLOC(dy);
1.5 noro 926: for ( dc = DC(f); dc; dc = NEXT(dc) ) {
927: d = QTOS(DEG(dc));
928: ptosfum(COEF(dc),t);
929: for ( i = 0; i <= DEG(t); i++ )
930: COEF(COEF(fl)[i])[d] = COEF(t)[i];
931: }
1.14 noro 932: for ( i = 0; i <= dy; i++ )
933: degum(COEF(fl)[i],dx);
1.5 noro 934: }
935:
936: /* x : main variable */
937:
1.18 noro 938: void sfbmtop(BM f,V x,V y,P *fp)
1.5 noro 939: {
940: UM *c;
1.14 noro 941: int i,j,d,a,dy;
1.8 noro 942: GFS b;
943: DCP dc0,dc,dct;
944:
1.14 noro 945: dy = DEG(f);
1.8 noro 946: c = COEF(f);
1.14 noro 947: d = degbm(f);
1.8 noro 948:
949: dc0 = 0;
950: for ( i = 0; i <= d; i++ ) {
951: dc = 0;
1.14 noro 952: for ( j = 0; j <= dy; j++ ) {
1.8 noro 953: if ( DEG(c[j]) >= i && (a = COEF(c[j])[i]) ) {
954: NEWDC(dct);
955: STOQ(j,DEG(dct));
1.23 noro 956: iftogfs(a,&b);
1.8 noro 957: COEF(dct) = (P)b;
958: NEXT(dct) = dc;
959: dc = dct;
960: }
961: }
962: if ( dc ) {
963: NEWDC(dct);
964: STOQ(i,DEG(dct));
965: MKP(y,dc,COEF(dct));
966: NEXT(dct) = dc0;
967: dc0 = dct;
968: }
969: }
970: if ( dc0 )
971: MKP(x,dc0,*fp);
972: else
973: *fp = 0;
974: }
975:
1.18 noro 976: void sfsqfr(P f,DCP *dcp)
1.14 noro 977: {
1.18 noro 978: Obj obj;
1.14 noro 979: DCP dc;
980: VL vl;
981:
1.18 noro 982: simp_ff((Obj)f,&obj); f = (P)obj;
1.14 noro 983: clctv(CO,f,&vl);
984: if ( !vl ) {
985: /* f is a const */
986: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0; *dcp = dc;
987: } else if ( !NEXT(vl) )
988: sfusqfr(f,dcp);
989: else
1.24 noro 990: sqfrsf(f,dcp);
1.14 noro 991: }
992:
1.18 noro 993: void sfusqfr(P f,DCP *dcp)
1.14 noro 994: {
995: DCP dc,dct;
996: struct oDUM *udc;
997: V x;
998: P lc;
999: int n,i;
1000: UM mf;
1001:
1002: x = VR(f);
1003: n = getdeg(x,f);
1004: mf = W_UMALLOC(n);
1005: ptosfum(f,mf);
1006: lc = COEF(DC(f));
1007: if ( !_isonesf(COEF(mf)[n]) ) {
1008: monicsfum(mf);
1009: }
1010: W_CALLOC(n+1,struct oDUM,udc);
1011: gensqfrsfum(mf,udc);
1012: for ( i = 0, dc = 0; udc[i].f; i++ ) {
1013: NEWDC(dct); STOQ(udc[i].n,DEG(dct));
1014: sfumtop(x,udc[i].f,&COEF(dct));
1015: NEXT(dct) = dc; dc = dct;
1016: }
1017: NEWDC(dct); DEG(dct) = ONE; COEF(dct) = (P)lc; NEXT(dct) = dc;
1018: *dcp = dct;
1019: }
1020:
1.24 noro 1021: #if 0
1.21 noro 1022: void sfbsqfrmain(P f,V x,V y,DCP *dcp)
1023: {
1024: /* XXX*/
1025: }
1026:
1027: /* f is bivariate */
1028:
1.18 noro 1029: void sfbsqfr(P f,V x,V y,DCP *dcp)
1.14 noro 1030: {
1031: P t,rf,cx,cy;
1032: VL vl,rvl;
1.21 noro 1033: DCP dcx,dcy,dct,dc;
1.14 noro 1034: struct oVL vl0,vl1;
1035:
1.21 noro 1036: /* cy(y) = cont(f,x), f /= cy */
1.14 noro 1037: cont_pp_sfp(vl,f,&cy,&t); f = t;
1038: /* rvl = [y,x] */
1039: reordvar(vl,y,&rvl); reorderp(rvl,vl,f,&rf);
1040: /* cx(x) = cont(rf,y), Rf /= cy */
1041: cont_pp_sfp(rvl,rf,&cx,&t); rf = t;
1042: reorderp(vl,rvl,rf,&f);
1043:
1044: /* f -> cx*cy*f */
1.21 noro 1045: sfsqfr(cx,&dcx); dcx = NEXT(dcx);
1046: sfsqfr(cy,&dcy); dcy = NEXT(dcy);
1047: if ( dcx ) {
1048: for ( dct = dcx; NEXT(dct); dct = NEXT(dct) );
1049: NEXT(dct) = dcy;
1050: } else
1051: dcx = dcy;
1052: if ( OID(f) == O_N )
1053: *dcp = dcx;
1054: else {
1055: /* f must be bivariate */
1056: sfbsqfrmain(f,x,y,&dc);
1057: if ( dcx ) {
1058: for ( dct = dcx; NEXT(dct); dct = NEXT(dct) );
1059: NEXT(dct) = dc;
1060: } else
1061: dcx = dc;
1062: *dcp = dcx;
1063: }
1.14 noro 1064: }
1.24 noro 1065: #endif
1.14 noro 1066:
1.8 noro 1067: void sfdtest(P,ML,V,V,DCP *);
1068:
1.21 noro 1069: /* if degbound >= 0 find factor s.t. deg_x(factor) <= degbound */
1070:
1071: void sfbfctr(P f,V x,V y,int degbound,DCP *dcp)
1.8 noro 1072: {
1073: ML list;
1074: P sf;
1.9 noro 1075: GFS ev;
1076: DCP dc,dct;
1077: BM fl;
1.14 noro 1078: int dx,dy;
1.8 noro 1079:
1080: /* sf(x) = f(x+ev) = list->c[0]*list->c[1]*... */
1.28 ! noro 1081: sfhensel(5,f,x,y,degbound,&ev,&sf,&list);
1.13 noro 1082: if ( list->n == 0 )
1083: error("sfbfctr : short of evaluation points");
1084: else if ( list->n == 1 ) {
1085: /* f is irreducible */
1086: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0;
1087: *dcp = dc;
1088: return;
1089: }
1.9 noro 1090: sfdtest(sf,list,x,y,&dc);
1.15 noro 1091: if ( ev ) {
1092: dx = getdeg(x,sf);
1093: dy = getdeg(y,sf);
1094: W_BMALLOC(dx,dy,fl);
1095: for ( dct = dc; dct; dct = NEXT(dct) ) {
1096: ptosfbm(dy,COEF(dct),fl);
1097: shiftsfbm(fl,_chsgnsf(FTOIF(CONT(ev))));
1098: sfbmtop(fl,x,y,&COEF(dct));
1099: }
1.9 noro 1100: }
1101: *dcp = dc;
1.26 noro 1102: }
1103:
1104: /* returns shifted f, shifted factors and the eval pt */
1105:
1106: void sfbfctr_shift(P f,V x,V y,int degbound,GFS *evp,P *sfp,DCP *dcp)
1107: {
1108: ML list;
1109: P sf;
1110: GFS ev;
1111: DCP dc,dct;
1112: int dx,dy;
1113:
1114: /* sf(x) = f(x+ev) = list->c[0]*list->c[1]*... */
1.28 ! noro 1115: sfhensel(5,f,x,y,degbound,&ev,&sf,&list);
1.26 noro 1116: if ( list->n == 0 )
1117: error("sfbfctr_shift : short of evaluation points");
1118: else if ( list->n == 1 ) {
1119: /* f is irreducible */
1120: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0;
1121: *evp = 0;
1122: *sfp = f;
1123: *dcp = dc;
1124: } else {
1125: sfdtest(sf,list,x,y,dcp);
1126: *evp = ev;
1127: *sfp = sf;
1128: }
1.8 noro 1129: }
1130:
1.14 noro 1131: /* f = f(x,y) = list->c[0]*list->c[1]*... mod y^(list->bound+1) */
1.8 noro 1132:
1.18 noro 1133: void sfdtest(P f,ML list,V x,V y,DCP *dcp)
1.8 noro 1134: {
1.14 noro 1135: int np,dx,dy;
1.20 noro 1136: int i,j,k,bound;
1.8 noro 1137: int *win;
1138: P g,lcg,factor,cofactor,lcyx;
1.18 noro 1139: P csum;
1.8 noro 1140: DCP dcf,dcf0,dc;
1141: BM *c;
1142: BM lcy;
1.20 noro 1143: UM lcg0,lcy0,w;
1144: UM *d1c;
1.8 noro 1145: ML wlist;
1146: struct oVL vl1,vl0;
1147: VL vl;
1.19 noro 1148: int z,dt,dtok;
1.8 noro 1149:
1150: /* vl = [x,y] */
1151: vl0.v = x; vl0.next = &vl1; vl1.v = y; vl1.next = 0; vl = &vl0;
1152:
1.9 noro 1153: /* setup various structures and arrays */
1.14 noro 1154: dx = getdeg(x,f);
1155: dy = getdeg(y,f);
1156: np = list->n;
1157: win = W_ALLOC(np+1);
1158: wlist = W_MLALLOC(np);
1159: wlist->n = list->n;
1.20 noro 1160: bound = wlist->bound = list->bound;
1.8 noro 1161: c = (BM *)COEF(wlist);
1162: bcopy((char *)COEF(list),(char *)c,(int)(sizeof(BM)*np));
1163:
1.14 noro 1164: lcg0 = W_UMALLOC(2*dy);
1.9 noro 1165:
1.8 noro 1166: /* initialize g by f */
1.9 noro 1167: g = f;
1168:
1169: /* initialize lcg */
1170: mulp(vl,g,COEF(DC(g)),&lcg);
1171:
1172: /* initialize lcg0 */
1173: const_term(lcg,lcg0);
1174:
1175: /* initialize csum = lcg(1) */
1176: sfcsump(vl,lcg,&csum);
1.8 noro 1177:
1178: /* initialize lcy by LC(f) */
1.14 noro 1179: W_BMALLOC(0,dy,lcy);
1.9 noro 1180: NEWDC(dc); COEF(dc) = COEF(DC(g)); DEG(dc) = 0;
1.8 noro 1181: NEWP(lcyx); VR(lcyx) = x; DC(lcyx) = dc;
1.14 noro 1182: ptosfbm(dy,lcyx,lcy);
1.8 noro 1183:
1.20 noro 1184: /* initialize lcy0 by LC(f) */
1185: lcy0 = W_UMALLOC(bound);
1186: ptosfum(COEF(DC(g)),lcy0);
1187:
1188: /* ((d-1 coefs)*lcy0 */
1189: d1c = (UM *)W_ALLOC(np*sizeof(UM));
1190: w = W_UMALLOC(2*bound);
1191: for ( i = 1; i < np; i++ ) {
1192: extractcoefbm(c[i],degbm(c[i])-1,w);
1193: d1c[i] = W_UMALLOC(2*bound);
1194: mulsfum(w,lcy0,d1c[i]);
1195: /* d1c[i] = d1c[i] mod y^(bound+1) */
1196: if ( DEG(d1c[i]) > bound ) {
1197: for ( j = DEG(d1c[i]); j > bound; j-- )
1198: COEF(d1c[i])[j] = 0;
1199: degum(d1c[i],bound);
1200: }
1201: }
1202:
1.8 noro 1203: fprintf(stderr,"np = %d\n",np);
1.19 noro 1204: dtok = 0;
1.8 noro 1205: for ( g = f, k = 1, dcf = dcf0 = 0, win[0] = 1, --np, z = 0; ; z++ ) {
1206: if ( !(z % 1000) ) fprintf(stderr,".");
1.20 noro 1207: dt = sfdegtest(dy,bound,d1c,k,win);
1.19 noro 1208: if ( dt )
1209: dtok++;
1210: if ( dt && sfdtestmain(vl,lcg,lcg0,lcy,csum,wlist,
1211: k,win,&factor,&cofactor) ) {
1.8 noro 1212: NEXTDC(dcf0,dcf); DEG(dcf) = ONE; COEF(dcf) = factor;
1213: g = cofactor;
1214:
1215: /* update lcg */
1216: mulp(vl,g,COEF(DC(g)),&lcg);
1217:
1.9 noro 1218: /* update lcg0 */
1219: const_term(lcg,lcg0);
1220:
1221: /* update csum */
1222: sfcsump(vl,lcg,&csum);
1223:
1.19 noro 1224: /* update dy */
1225: dy = getdeg(y,g);
1226:
1.8 noro 1227: /* update lcy */
1.14 noro 1228: clearbm(0,lcy);
1229: COEF(dc) = COEF(DC(g));
1230: ptosfbm(dy,lcyx,lcy);
1.8 noro 1231:
1232: for ( i = 0; i < k - 1; i++ )
1233: for ( j = win[i] + 1; j < win[i + 1]; j++ )
1234: c[j-i-1] = c[j];
1235: for ( j = win[k-1] + 1; j <= np; j++ )
1236: c[j-k] = c[j];
1237: if ( ( np -= k ) < k )
1238: break;
1239: if ( np - win[0] + 1 < k )
1240: if ( ++k > np )
1241: break;
1242: else
1243: for ( i = 0; i < k; i++ )
1244: win[i] = i + 1;
1245: else
1246: for ( i = 1; i < k; i++ )
1247: win[i] = win[0] + i;
1.20 noro 1248:
1249:
1250: /* update lcy0 */
1251: ptosfum(COEF(DC(g)),lcy0);
1252:
1253: /* update d-1 coeffs */
1254: for ( i = 1; i <= np; i++ ) {
1255: extractcoefbm(c[i],degbm(c[i])-1,w);
1256: mulsfum(w,lcy0,d1c[i]);
1257: /* d1c[i] = d1c[1] mod y^(bound+1) */
1258: if ( DEG(d1c[i]) > bound ) {
1259: for ( j = DEG(d1c[i]); j > bound; j-- )
1260: COEF(d1c[i])[j] = 0;
1261: degum(d1c[i],bound);
1262: }
1263: }
1.8 noro 1264: } else if ( !ncombi(1,np,k,win) )
1265: if ( k == np )
1266: break;
1267: else
1268: for ( i = 0, ++k; i < k; i++ )
1269: win[i] = i + 1;
1270: }
1.19 noro 1271: fprintf(stderr,"total %d, omitted by degtest %d\n",z,z-dtok);
1.8 noro 1272: NEXTDC(dcf0,dcf); COEF(dcf) = g;
1273: DEG(dcf) = ONE; NEXT(dcf) = 0; *dcp = dcf0;
1274: }
1275:
1.20 noro 1276: void extractcoefbm(BM f,int dx,UM r)
1.19 noro 1277: {
1278: int j;
1279: UM fj;
1280:
1281: for ( j = DEG(f); j >= 0; j-- ) {
1282: fj = COEF(f)[j];
1.20 noro 1283: if ( fj && DEG(fj) >= dx ) {
1284: COEF(r)[j] = COEF(fj)[dx];
1.19 noro 1285: } else
1286: COEF(r)[j] = 0;
1287: }
1288: degum(r,DEG(f));
1289: }
1290:
1291: /* deg_y(prod mod y^(bound+1)) <= dy ? */
1292:
1.20 noro 1293: int sfdegtest(int dy,int bound,UM *d1c,int k,int *in)
1.19 noro 1294: {
1.20 noro 1295: int i,j;
1296: UM w,w1,wt;
1.19 noro 1297: BM t;
1298:
1299: w = W_UMALLOC(bound);
1300: w1 = W_UMALLOC(bound);
1301: clearum(w,bound);
1302: for ( i = 0; i < k; i++ ) {
1.20 noro 1303: addsfum(w,d1c[in[i]],w1); wt = w; w = w1; w1 = wt;
1.19 noro 1304: }
1.20 noro 1305: return DEG(w) <= dy ? 1 : 0;
1.19 noro 1306: }
1307:
1.9 noro 1308: /* lcy = LC(g), lcg = lcy*g, lcg0 = const part of lcg */
1.19 noro 1309:
1.18 noro 1310: int sfdtestmain(VL vl,P lcg,UM lcg0,BM lcy,P csum,ML list,
1311: int k,int *in,P *fp,P *cofp)
1.8 noro 1312: {
1.14 noro 1313: P fmul,csumg,q,cont;
1.8 noro 1314: V x,y;
1315:
1316: x = vl->v;
1317: y = vl->next->v;
1.9 noro 1318: if (!sfctest(lcg0,lcy,list,k,in))
1.8 noro 1319: return 0;
1320: mulsfbmarray(UDEG(lcg),lcy,list,k,in,x,y,&fmul);
1321: if ( csum ) {
1322: sfcsump(vl,fmul,&csumg);
1323: if ( csumg ) {
1324: if ( !divtp(vl,csum,csumg,&q) )
1325: return 0;
1326: }
1327: }
1.11 noro 1328: if ( divtp_by_sfbm(vl,lcg,fmul,&q) ) {
1.14 noro 1329: cont_pp_sfp(vl,fmul,&cont,fp);
1330: cont_pp_sfp(vl,q,&cont,cofp);
1.8 noro 1331: return 1;
1332: } else
1333: return 0;
1334: }
1335:
1.18 noro 1336: void const_term(P f,UM c)
1.9 noro 1337: {
1338: DCP dc;
1339:
1340: for ( dc = DC(f); dc && DEG(dc); dc = NEXT(dc) );
1341: if ( dc )
1342: ptosfum(COEF(dc),c);
1343: else
1344: DEG(c) = -1;
1345: }
1346:
1.18 noro 1347: void const_term_sfbm(BM f,UM c)
1.9 noro 1348: {
1.14 noro 1349: int i,dy;
1.9 noro 1350:
1.14 noro 1351: dy = DEG(f);
1352: for ( i = 0; i <= dy; i++ )
1.9 noro 1353: if ( DEG(COEF(f)[i]) >= 0 )
1354: COEF(c)[i] = COEF(COEF(f)[i])[0];
1355: else
1356: COEF(c)[i] = 0;
1.14 noro 1357: degum(c,dy);
1.9 noro 1358: }
1359:
1360: /* lcy*(product of const part) | lcg0 ? */
1361:
1.18 noro 1362: int sfctest(UM lcg0,BM lcy,ML list,int k,int *in)
1.8 noro 1363: {
1.14 noro 1364: int dy,i,dr;
1.9 noro 1365: UM t,s,u,w;
1366: BM *l;
1367:
1.14 noro 1368: dy = list->bound;
1369: t = W_UMALLOC(2*dy);
1370: s = W_UMALLOC(2*dy);
1371: u = W_UMALLOC(2*dy);
1372: const_term_sfbm(lcy,t);
1.9 noro 1373: if ( DEG(t) < 0 )
1374: return 1;
1.8 noro 1375:
1.9 noro 1376: l = (BM *)list->c;
1377: for ( i = 0; i < k; i++ ) {
1.14 noro 1378: const_term_sfbm(l[in[i]],s);
1.9 noro 1379: mulsfum(t,s,u);
1.14 noro 1380: if ( DEG(u) > dy )
1381: degum(u,dy);
1.9 noro 1382: w = t; t = u; u = w;
1383: }
1384: cpyum(lcg0,s);
1385: dr = divsfum(s,t,u);
1386: if ( dr >= 0 )
1387: return 0;
1388: else
1.8 noro 1389: return 1;
1390: }
1391:
1392: /* main var of f is x */
1393:
1.18 noro 1394: void mulsfbmarray(int dx,BM lcy,ML list,int k,int *in,V x,V y,P *g)
1.8 noro 1395: {
1.14 noro 1396: int dy,i;
1.18 noro 1397: BM wb0,wb1,t;
1.8 noro 1398: BM *l;
1399:
1.14 noro 1400: dy = list->bound;
1401: W_BMALLOC(dx,dy,wb0); W_BMALLOC(dx,dy,wb1);
1.8 noro 1402: l = (BM *)list->c;
1.14 noro 1403: clearbm(dx,wb0);
1404: mulsfbm(lcy,l[in[0]],wb0);
1.8 noro 1405: for ( i = 1; i < k; i++ ) {
1.14 noro 1406: clearbm(dx,wb1);
1407: mulsfbm(l[in[i]],wb0,wb1);
1.8 noro 1408: t = wb0; wb0 = wb1; wb1 = t;
1409: }
1.14 noro 1410: sfbmtop(wb0,x,y,g);
1.8 noro 1411: }
1412:
1.18 noro 1413: void sfcsump(VL vl,P f,P *s)
1.8 noro 1414: {
1415: P t,u;
1416: DCP dc;
1417:
1418: for ( dc = DC(f), t = 0; dc; dc = NEXT(dc) ) {
1419: addp(vl,COEF(dc),t,&u); t = u;
1420: }
1421: *s = t;
1422: }
1423:
1424: /* *fp = primitive part of f w.r.t. x */
1425:
1.18 noro 1426: void cont_pp_sfp(VL vl,P f,P *cp,P *fp)
1.8 noro 1427: {
1428: V x,y;
1429: int d;
1430: UM t,s,gcd;
1431: DCP dc;
1.14 noro 1432: GFS g;
1.5 noro 1433:
1.8 noro 1434: x = vl->v;
1435: y = vl->next->v;
1436: d = getdeg(y,f);
1.14 noro 1437: if ( d == 0 ) {
1.23 noro 1438: itogfs(1,&g);
1.14 noro 1439: *cp = (P)g;
1.8 noro 1440: *fp = f; /* XXX */
1.14 noro 1441: } else {
1.8 noro 1442: t = W_UMALLOC(2*d);
1443: s = W_UMALLOC(2*d);
1444: gcd = W_UMALLOC(2*d);
1445: dc = DC(f);
1446: ptosfum(COEF(dc),gcd);
1447: for ( dc = NEXT(dc); dc; dc = NEXT(dc) ) {
1448: ptosfum(COEF(dc),t);
1449: gcdsfum(gcd,t,s);
1450: cpyum(s,gcd);
1451: }
1.14 noro 1452: sfumtop(y,gcd,cp);
1453: divsp(vl,f,*cp,fp);
1.5 noro 1454: }
1.11 noro 1455: }
1456:
1.18 noro 1457: int divtp_by_sfbm(VL vl,P f,P g,P *qp)
1.11 noro 1458: {
1459: V x,y;
1460: int fx,fy,gx,gy;
1461: BM fl,gl,ql;
1462: UM *cf,*cg,*cq;
1463: UM hg,q,t,s;
1464: int i,j,dr;
1465:
1466: x = vl->v; y = vl->next->v;
1467: fx = getdeg(x,f); fy = getdeg(y,f);
1468: gx = getdeg(x,g); gy = getdeg(y,g);
1469:
1470: if ( fx < gx || fy < gy )
1471: return 0;
1.14 noro 1472: W_BMALLOC(fx,fy,fl); ptosfbm(fy,f,fl); cf = COEF(fl);
1473: W_BMALLOC(gx,gy,gl); ptosfbm(gy,g,gl); cg = COEF(gl);
1474: W_BMALLOC(fx-gx,fy-gy,ql); cq = COEF(ql);
1.11 noro 1475:
1476: hg = cg[gy];
1477: q = W_UMALLOC(fx); t = W_UMALLOC(fx); s = W_UMALLOC(fx);
1478:
1479: for ( i = fy; i >= gy; i-- ) {
1480: if ( DEG(cf[i]) < 0 )
1481: continue;
1482: dr = divsfum(cf[i],hg,q);
1483: if ( dr >= 0 )
1484: return 0;
1485: if ( DEG(q) > fx-gx )
1486: return 0;
1487: cpyum(q,cq[i-gy]);
1488: for ( j = 0; j <= gy; j++ ) {
1489: mulsfum(cg[j],q,t);
1490: subsfum(cf[j+i-gy],t,s);
1491: cpyum(s,cf[j+i-gy]);
1492: }
1493: }
1494: for ( j = gy-1; j >= 0 && DEG(cf[j]) < 0; j-- );
1495: if ( j >= 0 )
1496: return 0;
1.14 noro 1497: sfbmtop(ql,x,y,qp);
1.18 noro 1498: return 1;
1.16 noro 1499: }
1500:
1501: /* XXX generate an irreducible poly of degree n */
1502:
1.17 noro 1503: extern int current_gfs_q1;
1.22 noro 1504: extern int *current_gfs_ntoi;
1.17 noro 1505:
1.18 noro 1506: void generate_defpoly_sfum(int n,UM *dp)
1.16 noro 1507: {
1.17 noro 1508: UM r,dr,t,g;
1509: UM *f;
1510: int *c,*w;
1511: int max,i,j;
1512:
1513: *dp = r = UMALLOC(n);
1514: DEG(r) = n;
1515: c = COEF(r);
1516: c[n] = _onesf();
1517: max = current_gfs_q1;
1518: w = (int *)ALLOCA(n*sizeof(int));
1519: bzero(w,n*sizeof(int));
1520:
1521: dr = W_UMALLOC(n); t = W_UMALLOC(n); g = W_UMALLOC(n);
1522: f = (UM *)ALLOCA((n+1)*sizeof(UM));
1523: while ( 1 ) {
1524: for ( i = 0; i < n && w[i] == max; i++ );
1525: if ( i == n ) {
1526: /* XXX cannot happen */
1527: error("generate_defpoly_sfum : cannot happen");
1528: }
1529: for ( j = 0; j < i; j++ )
1530: w[j] = 0;
1531: w[i]++;
1.22 noro 1532: if ( !current_gfs_ntoi )
1533: for ( i = 0; i < n; i++ )
1534: c[i] = w[i]?FTOIF(w[i]):0;
1535: else
1536: for ( i = 0; i < n; i++ )
1537: c[i] = w[i]?FTOIF(w[i]-1):0;
1.17 noro 1538: if ( !c[0] )
1539: continue;
1540: diffsfum(r,dr); cpyum(r,t); gcdsfum(t,dr,g);
1541: if ( DEG(g) > 0 )
1542: continue;
1543:
1544: czsfum(r,f);
1545: for ( i = 0; f[i]; i++ );
1546: if ( i == 1 )
1547: return;
1548: }
1.3 noro 1549: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>