Annotation of OpenXM_contrib2/asir2000/engine/Fgfs.c, Revision 1.9
1.9 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Fgfs.c,v 1.8 2002/11/01 05:43:35 noro Exp $ */
1.1 noro 2:
3: #include "ca.h"
4:
1.3 noro 5: void cont_pp_mv_sf(VL vl,VL rvl,P p,P *c,P *pp);
6: void gcdsf_main(VL vl,P *pa,int m,P *r);
7: void ugcdsf(P *pa,int m,P *r);
8: void head_monomial(V v,P p,P *coef,P *term);
1.4 noro 9: void sqfrsfmain(VL vl,P f,DCP *dcp);
10: void pthrootsf(P f,Q m,P *r);
11: void partial_sqfrsf(VL vl,V v,P f,P *r,DCP *dcp);
12: void gcdsf(VL vl,P *pa,int k,P *r);
1.5 noro 13: void mfctrsfmain(VL vl, P f, DCP *dcp);
1.7 noro 14: void next_evaluation_point(int *mev,int n);
15: void estimatelc_sf(VL vl,VL rvl,P c,DCP dc,P *lcp);
16: void mfctrsf_hensel(VL vl,VL rvl,P f,P pp0,P u0,P v0,P lcu,P lcv,P *up);
17: void substvp_sf(VL vl,VL rvl,P f,int *mev,P *r);
18: void shift_sf(VL vl, VL rvl, P f, int *mev, int sgn, P *r);
19: void adjust_coef_sf(VL vl,VL rvl,P lcu,P u0,P *r);
1.8 noro 20: void extended_gcd_modyk(P u0,P v0,V x,V y,int dy,P *cu,P *cv);
1.7 noro 21: void poly_to_gfsn_poly(VL vl,P f,V v,P *r);
22: void gfsn_poly_to_poly(VL vl,P f,V v,P *r);
1.8 noro 23: void poly_to_gfsn_poly_main(P f,V v,P *r);
24: void gfsn_poly_to_poly_main(P f,V v,P *r);
25: void gfsn_univariate_to_sfbm(P f,int dy,BM *r);
26: void sfbm_to_gfsn_univariate(BM f,V x,V y,P *r);
1.4 noro 27:
28: void lex_lc(P f,P *c)
29: {
30: if ( !f || NUM(f) )
31: *c = f;
32: else
33: lex_lc(COEF(DC(f)),c);
34: }
35:
36: DCP append_dc(DCP dc,DCP dct)
37: {
38: DCP dcs;
39:
40: if ( !dc )
41: return dct;
42: else {
43: for ( dcs = dc; NEXT(dcs); dcs = NEXT(dcs) );
44: NEXT (dcs) = dct;
45: return dc;
46: }
47: }
48:
49: void sqfrsf(VL vl, P f, DCP *dcp)
50: {
51: DCP dc,dct;
52: Obj obj;
53: P t,s,c;
54: VL tvl,nvl;
55:
56: simp_ff((Obj)f,&obj); f = (P)obj;
57: lex_lc(f,&c); divsp(vl,f,c,&t); f = t;
58: monomialfctr(vl,f,&t,&dc); f = t;
59: clctv(vl,f,&tvl); vl = tvl;
60: if ( !vl )
61: ;
62: else if ( !NEXT(vl) ) {
63: sfusqfr(f,&dct);
64: dc = append_dc(dc,NEXT(dct));
65: } else {
66: t = f;
67: for ( tvl = vl; tvl; tvl = NEXT(tvl) ) {
68: reordvar(vl,tvl->v,&nvl);
69: cont_pp_mv_sf(vl,NEXT(nvl),t,&c,&s); t = s;
70: sqfrsf(vl,c,&dct);
71: dc = append_dc(dc,NEXT(dct));
72: }
73: sqfrsfmain(vl,t,&dct);
74: dc = append_dc(dc,dct);
75: }
76: NEWDC(dct); DEG(dct) = ONE; COEF(dct) = (P)c; NEXT(dct) = dc;
77: *dcp = dct;
78: }
79:
80: void sqfrsfmain(VL vl,P f,DCP *dcp)
81: {
82: VL tvl;
83: DCP dc,dct,dcs;
84: P t,s;
85: Q m,m1;
86: V v;
87:
88: clctv(vl,f,&tvl); vl = tvl;
89: dc = 0;
90: t = f;
91: for ( tvl = vl; tvl; tvl = NEXT(tvl) ) {
92: v = tvl->v;
93: partial_sqfrsf(vl,v,t,&s,&dct); t = s;
94: dc = append_dc(dc,dct);
95: }
96: if ( !NUM(t) ) {
97: STOQ(characteristic_sf(),m);
98: pthrootsf(t,m,&s);
99: sqfrsfmain(vl,s,&dct);
100: for ( dcs = dct; dcs; dcs = NEXT(dcs) ) {
101: mulq(DEG(dcs),m,&m1); DEG(dcs) = m1;
102: }
103: dc = append_dc(dc,dct);
104: }
105: *dcp = dc;
106: }
107:
108: void pthrootsf(P f,Q m,P *r)
109: {
110: DCP dc,dc0,dct;
111: N qn,rn;
112:
113: if ( NUM(f) )
114: pthrootgfs(f,r);
115: else {
116: dc = DC(f);
117: dc0 = 0;
118: for ( dc0 = 0; dc; dc = NEXT(dc) ) {
119: NEXTDC(dc0,dct);
120: pthrootsf(COEF(dc),m,&COEF(dct));
121: if ( DEG(dc) ) {
122: divn(NM(DEG(dc)),NM(m),&qn,&rn);
123: if ( rn )
124: error("pthrootsf : cannot happen");
125: NTOQ(qn,1,DEG(dct));
126: } else
127: DEG(dct) = 0;
128: }
129: NEXT(dct) = 0;
130: MKP(VR(f),dc0,*r);
131: }
132: }
133:
134: void partial_sqfrsf(VL vl,V v,P f,P *r,DCP *dcp)
135: {
136: P ps[2];
137: DCP dc0,dc;
138: int m;
139: P t,flat,flat1,g,df,q;
140:
141: diffp(vl,f,v,&df);
142: if ( !df ) {
143: *dcp = 0;
144: *r = f;
145: return;
146: }
147: ps[0] = f; ps[1] = df;
148: gcdsf(vl,ps,2,&g);
149: divsp(vl,f,g,&flat);
150: m = 0;
151: t = f;
152: dc0 = 0;
153: while ( !NUM(flat) ) {
154: while ( divtp(vl,t,flat,&q) ) {
155: t = q; m++;
156: }
157: ps[0] = t; ps[1] = flat;
158: gcdsf(vl,ps,2,&flat1);
159: divsp(vl,flat,flat1,&g);
160: flat = flat1;
161: NEXTDC(dc0,dc);
162: COEF(dc) = g;
163: STOQ(m,DEG(dc));
164: }
165: NEXT(dc) = 0;
166: *dcp = dc0;
167: *r = t;
168: }
1.1 noro 169:
170: void gcdsf(VL vl,P *pa,int k,P *r)
171: {
1.3 noro 172: P *ps,*pl,*pm;
173: P **cp;
1.1 noro 174: int *cn;
175: DCP *ml;
176: Obj obj;
177: int i,j,l,m;
178: P mg,mgsf,t;
179: VL avl,nvl,tvl,svl;
180:
181: ps = (P *)ALLOCA(k*sizeof(P));
182: for ( i = 0, m = 0; i < k; i++ ) {
183: simp_ff((Obj)pa[i],&obj);
184: if ( obj )
1.3 noro 185: ps[m++] = (P)obj;
1.1 noro 186: }
187: if ( !m ) {
188: *r = 0;
189: return;
190: }
191: if ( m == 1 ) {
1.3 noro 192: *r = ps[0];
1.1 noro 193: return;
194: }
195: pl = (P *)ALLOCA(m*sizeof(P));
196: ml = (DCP *)ALLOCA(m*sizeof(DCP));
197: for ( i = 0; i < m; i++ )
198: monomialfctr(vl,ps[i],&pl[i],&ml[i]);
1.3 noro 199: gcdmonomial(vl,ml,m,&mg); simp_ff((Obj)mg,&obj); mgsf = (P)obj;
1.1 noro 200: for ( i = 0, nvl = vl, avl = 0; nvl && i < m; i++ ) {
201: clctv(vl,pl[i],&tvl);
202: intersectv(nvl,tvl,&svl); nvl = svl;
203: mergev(vl,avl,tvl,&svl); avl = svl;
204: }
205: if ( !nvl ) {
206: *r = mgsf;
207: return;
208: }
209: if ( !NEXT(avl) ) {
210: ugcdsf(pl,m,&t);
211: mulp(vl,mgsf,t,r);
212: return;
213: }
214: for ( tvl = nvl, i = 0; tvl; tvl = NEXT(tvl), i++ );
215: for ( tvl = avl, j = 0; tvl; tvl = NEXT(tvl), j++ );
216: if ( i == j ) {
217: /* all the pl[i]'s have the same variables */
218: gcdsf_main(avl,pl,m,&t);
219: } else {
220: cp = (P **)ALLOCA(m*sizeof(P *));
221: cn = (int *)ALLOCA(m*sizeof(int));
222: for ( i = 0; i < m; i++ ) {
223: cp[i] = (P *)ALLOCA(lengthp(pl[i])*sizeof(P));
224: cn[i] = pcoef(vl,nvl,pl[i],cp[i]);
225: }
226: for ( i = j = 0; i < m; i++ )
227: j += cn[i];
228: pm = (P *)ALLOCA(j*sizeof(P));
229: for ( i = l = 0; i < m; i++ )
230: for ( j = 0; j < cn[i]; j++ )
231: pm[l++] = cp[i][j];
232: gcdsf(vl,pm,l,&t);
233: }
234: mulp(vl,mgsf,t,r);
235: }
236:
237: /* univariate gcd */
238:
239: void ugcdsf(P *pa,int m,P *r)
240: {
1.3 noro 241: P *ps;
1.1 noro 242: int i;
243: UM w1,w2,w3,w;
244: int d;
245: V v;
246:
247: if ( m == 1 ) {
248: *r = pa[0];
249: return;
250: }
1.3 noro 251: for ( i = 0; i < m; i++ )
252: if ( NUM(pa[i]) ) {
253: itogfs(1,r);
254: return;
255: }
1.1 noro 256: ps = (P *)ALLOCA(m*sizeof(P));
257: sort_by_deg(m,pa,ps);
1.3 noro 258: v = VR(ps[m-1]);
259: d = getdeg(v,ps[m-1]);
1.1 noro 260: w1 = W_UMALLOC(d);
261: w2 = W_UMALLOC(d);
262: w3 = W_UMALLOC(d);
263: ptosfum(ps[0],w1);
264: for ( i = 1; i < m; i++ ) {
265: ptosfum(ps[i],w2);
266: gcdsfum(w1,w2,w3);
267: w = w1; w1 = w3; w3 = w;
268: if ( !DEG(w1) ) {
1.3 noro 269: itogfs(1,r);
1.1 noro 270: return;
271: }
272: }
273: sfumtop(v,w1,r);
274: }
275:
1.4 noro 276: /* deg(HT(p),v), where p is considered as distributed poly over F[v] */
277: int gethdeg(VL vl,V v,P p)
278: {
279: DCP dc;
280: Q dmax;
281: P cmax;
282:
283: if ( !p )
284: return -1;
285: else if ( NUM(p) )
286: return 0;
287: else if ( VR(p) != v )
288: /* HT(p) = HT(lc(p))*x^D */
289: return gethdeg(vl,v,COEF(DC(p)));
290: else {
291: /* VR(p) = v */
292: dc = DC(p); dmax = DEG(dc); cmax = COEF(dc);
293: for ( dc = NEXT(dc); dc; dc = NEXT(dc) )
294: if ( compp(vl,COEF(dc),cmax) > 0 ) {
295: dmax = DEG(dc); cmax = COEF(dc);
296: }
297: return QTOS(dmax);
298: }
299: }
1.1 noro 300:
301: /* all the pa[i]'s have the same variables (=vl) */
302:
303: void gcdsf_main(VL vl,P *pa,int m,P *r)
304: {
1.3 noro 305: int nv,i,i0,imin,d,d0,d1,d2,dmin,index;
306: V v,v0,vmin;
1.2 noro 307: VL tvl,nvl,rvl,nvl0,rvl0;
1.3 noro 308: P *pc, *ps, *ph,*lps;
309: P x,t,cont,hg,g,hm,mod,s;
310: P hge,ge,ce,he,u,cof1e,mode,mod1,adj,cof1,coadj,q;
311: GFS sf;
1.2 noro 312:
1.1 noro 313: for ( nv = 0, tvl = vl; tvl; tvl = NEXT(tvl), nv++);
314: if ( nv == 1 ) {
315: ugcdsf(pa,m,r);
316: return;
317: }
1.4 noro 318: /* find v s.t. min(deg(pa[i],v)+gethdeg(pa[i],v)) is minimal */
1.1 noro 319: tvl = vl;
320: do {
321: v = tvl->v;
322: i = 0;
323: do {
1.4 noro 324: d = getdeg(v,pa[i])+gethdeg(vl,v,pa[i]);
1.1 noro 325: if ( i == 0 || (d < d0) ) {
326: d0 = d; i0 = i; v0 = v;
327: }
328: } while ( ++i < m );
329: if ( tvl == vl || (d0 < dmin) ) {
330: dmin = d0; imin = i0; vmin = v0;
331: }
332: } while ( tvl = NEXT(tvl) );
333:
334: /* reorder variables so that vmin is the last variable */
335: for ( nvl0 = 0, rvl0 = 0, tvl = vl; tvl; tvl = NEXT(tvl) )
336: if ( tvl->v != vmin ) {
337: NEXTVL(nvl0,nvl); nvl->v = tvl->v;
338: NEXTVL(rvl0,rvl); rvl->v = tvl->v;
339: }
340: /* rvl = remaining variables */
1.3 noro 341: NEXT(rvl) = 0; rvl = rvl0;
1.1 noro 342: /* nvl = ...,vmin */
1.3 noro 343: NEXTVL(nvl0,nvl); nvl->v = vmin; NEXT(nvl) = 0; nvl = nvl0;
1.2 noro 344: MKV(vmin,x);
1.1 noro 345:
346: /* for content and primitive part */
347: pc = (P *)ALLOCA(m*sizeof(P));
348: ps = (P *)ALLOCA(m*sizeof(P));
349: ph = (P *)ALLOCA(m*sizeof(P));
350: /* separate the contents */
351: for ( i = 0; i < m; i++ ) {
1.3 noro 352: reorderp(nvl,vl,pa[i],&t);
1.1 noro 353: cont_pp_mv_sf(nvl,rvl,t,&pc[i],&ps[i]);
354: head_monomial(vmin,ps[i],&ph[i],&t);
355: }
356: ugcdsf(pc,m,&cont);
357: ugcdsf(ph,m,&hg);
358:
359: /* for hg*pp (used in check phase) */
360: lps = (P *)ALLOCA(m*sizeof(P));
361: for ( i = 0; i < m; i++ )
362: mulp(nvl,hg,ps[i],&lps[i]);
363:
364: while ( 1 ) {
365: g = 0;
1.3 noro 366: cof1 = 0;
1.1 noro 367: hm = 0;
1.3 noro 368: itogfs(1,&mod);
1.1 noro 369: index = 0;
1.3 noro 370: for ( index = 0; getdeg(vmin,mod) <= d+1; index++ ) {
1.1 noro 371: /* evaluation pt */
1.3 noro 372: indextogfs(index,&s);
1.1 noro 373: substp(nvl,hg,vmin,s,&hge);
374: if ( !hge )
375: continue;
376: for ( i = 0; i < m; i++ )
377: substp(nvl,ps[i],vmin,s,&ph[i]);
378: /* ge = GCD(ps[0]|x=s,...,ps[m-1]|x=s) */
379: gcdsf(nvl,ph,m,&ge);
380: head_monomial(vmin,ge,&ce,&he);
1.3 noro 381: if ( NUM(he) ) {
1.1 noro 382: *r = cont;
383: return;
384: }
1.3 noro 385: divgfs((GFS)hge,(GFS)ce,&sf); t = (P)sf;
386: mulp(nvl,t,ge,&u); ge = u;
1.1 noro 387: divsp(nvl,ph[imin],ge,&t); mulp(nvl,hge,t,&cof1e);
1.2 noro 388: /* hm=0 : reset; he==hm : lucky */
1.3 noro 389: if ( !hm || !compp(nvl,he,hm) ) {
1.2 noro 390: substp(nvl,mod,vmin,s,&mode); divsp(nvl,mod,mode,&mod1);
391: /* adj = mod/(mod|x=s)*(ge-g|x=s) */
392: substp(nvl,g,vmin,s,&t);
393: subp(nvl,ge,t,&u); mulp(nvl,mod1,u,&adj);
394: /* coadj = mod/(mod|vmin=s)*(cof1e-cof1e|vmin=s) */
395: substp(nvl,cof1,vmin,s,&t);
1.3 noro 396: subp(nvl,cof1e,t,&u); mulp(nvl,mod1,u,&coadj);
1.2 noro 397: if ( !adj ) {
398: /* adj == gcd ? */
399: for ( i = 0; i < m; i++ )
1.3 noro 400: if ( !divtp(nvl,lps[i],g,&t) )
1.2 noro 401: break;
402: if ( i == m ) {
1.3 noro 403: cont_pp_mv_sf(nvl,rvl,g,&t,&u);
1.2 noro 404: mulp(nvl,cont,u,&t);
1.3 noro 405: reorderp(vl,nvl,t,r);
1.2 noro 406: return;
407: }
408: } else if ( !coadj ) {
1.3 noro 409: /* ps[imin]/coadj == gcd ? */
410: if ( divtp(nvl,lps[imin],cof1,&q) ) {
1.2 noro 411: for ( i = 0; i < m; i++ )
412: if ( !divtp(nvl,lps[i],q,&t) )
413: break;
414: if ( i == m ) {
415: cont_pp_mv_sf(nvl,rvl,q,&t,&u);
416: mulp(nvl,cont,u,&t);
1.3 noro 417: reorderp(vl,nvl,t,r);
1.2 noro 418: return;
419: }
420: }
421: }
422: addp(nvl,g,adj,&t); g = t;
423: addp(nvl,cof1,coadj,&t); cof1 = t;
424: subp(nvl,x,s,&t); mulp(nvl,mod,t,&u); mod = u;
425: hm = he;
426: } else {
427: d1 = homdeg(hm); d2 = homdeg(he);
428: if ( d1 < d2 ) /* we use current hm */
429: continue;
430: else if ( d1 > d2 ) {
431: /* use he */
432: g = ge;
433: cof1 = cof1e;
434: hm = he;
435: subp(nvl,x,s,&mod);
436: } else {
437: /* d1==d2, but hm!=he => both are unlucky */
438: g = 0;
439: cof1 = 0;
1.3 noro 440: itogfs(1,&mod);
1.2 noro 441: }
1.1 noro 442: }
443: }
444: }
445: }
446:
447: void head_monomial(V v,P p,P *coef,P *term)
448: {
449: P t,s,u;
450: DCP dc;
451: GFS one;
1.3 noro 452: VL vl;
1.1 noro 453:
1.3 noro 454: itogfs(1,&one);
455: t = (P)one;
1.1 noro 456: while ( 1 ) {
457: if ( NUM(p) || VR(p) == v ) {
458: *coef = p;
459: *term = t;
460: return;
461: } else {
1.3 noro 462: NEWDC(dc);
463: COEF(dc) = (P)one; DEG(dc) = DEG(DC(p));
1.1 noro 464: MKP(VR(p),dc,s);
465: mulp(vl,t,s,&u); t = u;
466: p = COEF(DC(p));
467: }
468: }
469: }
470:
471: void cont_pp_mv_sf(VL vl,VL rvl,P p,P *c,P *pp)
472: {
473: DP dp;
474: MP t;
475: int i,m;
476: P *ps;
477:
478: ptod(vl,rvl,p,&dp);
479: for ( t = BDY(dp), m = 0; t; t = NEXT(t), m++ );
480: ps = (P *)ALLOCA(m*sizeof(P));
1.3 noro 481: for ( t = BDY(dp), i = 0; t; t = NEXT(t), i++ )
1.1 noro 482: ps[i] = C(t);
483: ugcdsf(ps,m,c);
1.3 noro 484: divsp(vl,p,*c,pp);
1.5 noro 485: }
486:
487: void mfctrsf(VL vl, P f, DCP *dcp)
488: {
489: DCP dc0,dc,dct,dcs,dcr;
490: Obj obj;
491:
492: simp_ff((Obj)f,&obj); f = (P)obj;
493: sqfrsf(vl,f,&dct);
494: dc = dc0 = dct; dct = NEXT(dct); NEXT(dc) = 0;
495: for ( ; dct; dct = NEXT(dct) ) {
496: mfctrsfmain(vl,COEF(dct),&dcs);
497: for ( dcr = dcs; dcr; dcr = NEXT(dcr) )
498: DEG(dcr) = DEG(dct);
499: for ( ; NEXT(dc); dc = NEXT(dc) );
500: NEXT(dc) = dcs;
501: }
502: *dcp = dc0;
503: }
504:
505: /* f : sqfr, non const */
506:
507: void mfctrsfmain(VL vl, P f, DCP *dcp)
508: {
1.6 noro 509: VL tvl,nvl,rvl;
1.7 noro 510: DCP dc,dc0,dc1,dc2,dct,lcfdc,dcs;
511: int imin,inext,i,j,n,k,np;
1.5 noro 512: int *da;
513: V vx,vy;
514: V *va;
1.7 noro 515: P *l,*tl;
1.5 noro 516: P gcd,g,df,dfmin;
517: P pa[2];
1.6 noro 518: P g0,pp0,spp0,c,c0,x,y,u,v,lcf,lcu,lcv,u0,v0,t,s;
1.7 noro 519: P ype,yme;
1.6 noro 520: GFS ev,evy;
521: P *fp0;
522: int *mev,*win;
1.5 noro 523:
524: clctv(vl,f,&tvl); vl = tvl;
525: if ( !vl )
526: error("mfctrsfmain : cannot happen");
527: if ( !NEXT(vl) ) {
528: /* univariate */
529: ufctrsf(f,&dc);
530: /* remove lc */
531: *dcp = NEXT(dc);
532: return;
533: }
534: for ( n = 0, tvl = vl; tvl; tvl = NEXT(tvl), n++ );
535: va = (V *)ALLOCA(n*sizeof(int));
536: da = (int *)ALLOCA(n*sizeof(int));
537: /* find v s.t. diff(f,v) is nonzero and deg(f,v) is minimal */
538: imin = -1;
539: for ( i = 0, tvl = vl; i < n; tvl = NEXT(tvl), i++ ) {
540: va[i] = tvl->v;
541: da[i] = getdeg(va[i],f);
542: diffp(vl,f,va[i],&df);
543: if ( !df )
544: continue;
545: if ( imin < 0 || da[i] < da[imin] ) {
546: dfmin = df;
547: imin = i;
548: }
549: }
550: /* find v1 neq v s.t. deg(f,v) is minimal */
551: inext = -1;
552: for ( i = 0; i < n; i++ ) {
553: if ( i == imin )
554: continue;
555: if ( inext < 0 || da[i] < da[inext] )
556: inext = i;
557: }
558: pa[0] = f;
559: pa[1] = dfmin;
560: gcdsf_main(vl,pa,2,&gcd);
561: if ( !NUM(gcd) ) {
562: /* f = gcd * f/gcd */
563: mfctrsfmain(vl,gcd,&dc1);
564: divsp(vl,f,gcd,&g);
565: mfctrsfmain(vl,g,&dc2);
566: for ( dct = dc1; NEXT(dct); dct = NEXT(dct) );
567: NEXT(dct) = dc2;
568: *dcp = dc1;
569: return;
570: }
571: /* create vl s.t. vl[0] = va[imin], vl[1] = va[inext] */
572: nvl = 0;
573: NEXTVL(nvl,tvl); tvl->v = va[imin];
574: NEXTVL(nvl,tvl); tvl->v = va[inext];
575: for ( i = 0; i < n; i++ ) {
576: if ( i == imin || i == inext )
577: continue;
578: NEXTVL(nvl,tvl); tvl->v = va[i];
579: }
580: NEXT(tvl) = 0;
581:
582: reorderp(nvl,vl,f,&g);
583: vx = nvl->v;
584: vy = NEXT(nvl)->v;
1.6 noro 585: MKV(vx,x);
586: MKV(vy,y);
587: /* remaining variables */
588: rvl = NEXT(NEXT(nvl));
589: if ( !rvl ) {
1.5 noro 590: /* bivariate */
591: sfbfctr(g,vx,vy,getdeg(vx,g),&dc1);
592: for ( dc0 = 0; dc1; dc1 = NEXT(dc1) ) {
593: NEXTDC(dc0,dc);
594: DEG(dc) = ONE;
595: reorderp(vl,nvl,COEF(dc1),&COEF(dc));
596: }
597: NEXT(dc) = 0;
598: *dcp = dc0;
599: return;
600: }
1.6 noro 601: /* n >= 3; nvl = (vx,vy,X) */
602: /* find good evaluation pt for X */
603: mev = (int *)CALLOC(n-2,sizeof(int));
604: while ( 1 ) {
1.7 noro 605: substvp_sf(nvl,rvl,g,mev,&g0);
1.6 noro 606: pa[0] = g0;
607: diffp(nvl,g0,vx,&pa[1]);
608: if ( pa[1] ) {
609: gcdsf(nvl,pa,2,&gcd);
610: /* XXX maybe we have to accept the case where gcd is a poly of y */
611: if ( NUM(gcd) )
612: break;
613: }
1.7 noro 614: /* XXX if generated indices exceed q of GF(q) => error in indextogfs */
615: next_evaluation_point(mev,n-2);
1.6 noro 616: }
617: /* g0 = g(x,y,mev) */
618: /* separate content; g0 may have the content wrt x */
619: cont_pp_sfp(nvl,g0,&c0,&pp0);
620:
1.7 noro 621: /* factorize pp0; pp0 = pp0(x,y+evy) = prod dc */
622: sfbfctr_shift(pp0,vx,vy,getdeg(vx,pp0),&evy,&spp0,&dc); pp0 = spp0;
1.6 noro 623:
624: if ( !NEXT(dc) ) {
625: /* f is irreducible */
626: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0;
627: *dcp = dc;
628: return;
629: }
1.7 noro 630: /* ype = y+evy, yme = y-evy */
631: addp(nvl,y,(P)evy,&ype); subp(nvl,y,(P)evy,&yme);
632:
1.6 noro 633: /* shift c0; c0 <- c0(y+evy) */
1.7 noro 634: substp(nvl,c0,vy,ype,&s); c0 = s;
635:
636: /* shift f; f <- f(y+evy) */
637: substp(nvl,f,vy,ype,&s); f = s;
638:
639: /* now f(x,0,mev) = c0 * prod dc */
1.6 noro 640:
641: /* factorize lc_x(f) */
642: lcf = COEF(DC(f));
1.7 noro 643: mfctrsf(nvl,lcf,&dct);
644: /* skip the first element (= a number) */
645: dct = NEXT(dct);
646:
647: /* shift lcfdc; c <- c(X+mev) */
648: for ( lcfdc = 0; dct; dct = NEXT(dct) ) {
649: NEXTDC(lcfdc,dcs);
650: DEG(dcs) = DEG(dct);
651: shift_sf(nvl,rvl,COEF(dct),mev,1,&COEF(dcs));
652: }
653: NEXT(dcs) = 0;
1.6 noro 654:
655: /* np = number of bivariate factors */
656: for ( np = 0, dct = dc; dct; dct = NEXT(dct), np++ );
657: fp0 = (P *)ALLOCA((np+1)*sizeof(P));
658: for ( i = 0, dct = dc; i < np; dct = NEXT(dct), i++ )
659: fp0[i] = COEF(dct);
660: fp0[np] = 0;
1.7 noro 661: l = tl = (P *)ALLOCA((np+1)*sizeof(P));
1.6 noro 662: win = W_ALLOC(np+1);
1.7 noro 663:
664: /* f <- f(X+mev) */
665: shift_sf(nvl,rvl,f,mev,1,&s); f = s;
666:
1.6 noro 667: for ( k = 1, win[0] = 1, --np; ; ) {
668: itogfs(1,&u0);
669: /* u0 = product of selected factors */
670: for ( i = 0; i < k; i++ ) {
671: mulp(nvl,u0,fp0[win[i]],&t); u0 = t;
672: }
673: /* we have to consider the content */
1.7 noro 674: /* g0 = c0*u0*v0 */
675: mulp(nvl,LC(u0),c0,&c); estimatelc_sf(nvl,rvl,c,lcfdc,&lcu);
1.6 noro 676: divsp(nvl,pp0,u0,&v0);
1.7 noro 677: mulp(nvl,LC(v0),c0,&c); estimatelc_sf(nvl,rvl,c,lcfdc,&lcv);
678: mfctrsf_hensel(nvl,rvl,f,pp0,u0,v0,lcu,lcv,&u);
679: if ( u ) {
680: /* save the factor */
681: reorderp(vl,nvl,u,&t);
682: /* x -> x-mev, y -> y-evy */
683: shift_sf(vl,rvl,t,mev,-1,&s); substp(vl,s,vy,yme,tl++);
684:
685: /* update f,pp0 */
686: divsp(nvl,f,u,&t); f = t;
687: divsp(nvl,pp0,u0,&t); pp0 = t;
688: /* update win, fp0 */
689: for ( i = 0; i < k-1; i++ )
690: for ( j = win[i]+1; j < win[i+1]; j++ )
691: fp0[j-i-1] = fp0[j];
692: for ( j = win[k-1]+1; j <= np; j++ )
693: fp0[j-k] = fp0[j];
694: if ( ( np -= k ) < k ) break;
695: if ( np-win[0]+1 < k )
696: if ( ++k <= np ) {
697: for ( i = 0; i < k; i++ )
698: win[i] = i + 1;
699: continue;
700: } else
701: break;
702: else
703: for ( i = 1; i < k; i++ )
704: win[i] = win[0] + i;
705: } else {
706: if ( ncombi(1,np,k,win) == 0 )
707: if ( k == np ) break;
708: else
709: for ( i = 0, ++k; i < k; i++ )
710: win[i] = i + 1;
711: }
712: reorderp(vl,nvl,f,&t);
713: /* x -> x-mev, y -> y-evy */
714: shift_sf(vl,rvl,t,mev,-1,&s); substp(vl,s,vy,yme,tl++);
715: *tl = 0;
716:
717: for ( dc0 = 0, i = 0; l[i]; i++ ) {
718: NEXTDC(dc0,dc); DEG(dc) = ONE; COEF(dc) = l[i];
719: }
720: NEXT(dc) = 0; *dcp = dc0;
1.6 noro 721: }
722: }
723:
1.7 noro 724: void next_evaluation_point(int *e,int n)
725: {
726: int i,t,j;
727:
728: for ( i = n-1; i >= 0; i-- )
729: if ( e[i] ) break;
730: if ( i < 0 ) e[n-1] = 1;
731: else if ( i == 0 ) {
732: t = e[0]; e[0] = 0; e[n-1] = t+1;
733: } else {
734: e[i-1]++; t = e[i];
735: for ( j = i; j < n-1; j++ )
736: e[j] = 0;
737: e[n-1] = t-1;
738: }
739: }
740:
741: /*
742: * dc : f1^E1*...*fk^Ek
743: * find e1,...,ek s.t. fi(0)^ei | c
744: * and return f1^e1*...*fk^ek
745: * vl = (vx,vy,rvl)
746: */
747:
748: void estimatelc_sf(VL vl,VL rvl,P c,DCP dc,P *lcp)
749: {
750: DCP dct;
751: P r,c1,c2,t,s,f;
752: int i,d;
753: Q q;
754:
755: for ( dct = dc, r = (P)ONE; dct; dct = NEXT(dct) ) {
756: if ( NUM(COEF(dct)) )
757: continue;
758: /* constant part */
759: substvp_sf(vl,rvl,COEF(dct),0,&f);
760: d = QTOS(DEG(dct));
761: for ( i = 0, c1 = c; i < d; i++ )
762: if ( !divtp(vl,c1,f,&c2) )
763: break;
764: else
765: c1 = c2;
766: if ( i ) {
767: STOQ(i,q);
768: pwrp(vl,COEF(dct),q,&s); mulp(vl,r,s,&t); r = t;
769: }
770: }
771: *lcp = r;
772: }
773:
774: void substvp_sf(VL vl,VL rvl,P f,int *mev,P *r)
775: {
776: int i;
777: VL tvl;
778: P g,t;
779: GFS ev;
780:
781: for ( g = f, i = 0, tvl = rvl; tvl; tvl = NEXT(tvl), i++ ) {
782: if ( !mev )
783: ev = 0;
784: else
785: indextogfs(mev[i],&ev);
786: substp(vl,g,tvl->v,(P)ev,&t); g = t;
787: }
788: *r = g;
789: }
790:
791: /*
792: * f <- f(X+sgn*mev)
793: */
794:
795: void shift_sf(VL vl, VL rvl, P f, int *mev, int sgn, P *r)
796: {
797: VL tvl;
798: int i;
799: P x,g,t,s;
800: GFS ev;
801:
802: for ( g = f, tvl = rvl, i = 0; tvl; tvl = NEXT(tvl), i++ ) {
803: if ( !mev[i] )
804: continue;
805: indextogfs(mev[i],&ev);
806: MKV(tvl->v,x);
807: if ( sgn > 0 )
808: addp(vl,x,(P)ev,&t);
809: else
810: subp(vl,x,(P)ev,&t);
811: substp(vl,g,tvl->v,t,&s); g = s;
812: }
813: *r = g;
814: }
815:
816: /*
817: * pp(f(0)) = u0*v0
818: */
819:
820: void mfctrsf_hensel(VL vl,VL rvl,P f,P pp0,P u0,P v0,P lcu,P lcv,P *up)
821: {
822: VL tvl,onevl;
823: P t,s,w,u,v,ff,si,wu,wv,fj,cont;
824: UM ydy;
825: V vx,vy;
826: int dy,n,i,dbd,nv,j;
827: int *md;
828: P *uh,*vh;
829: P x,du0,dv0,m,q,r;
830: P *cu,*cv;
831: GFSN inv;
832:
833: /* adjust coeffs */
834: /* u0 = am x^m+ ... -> lcu*x^m + a(m-1)*(lcu(0)/am)*x^(m-1)+... */
835: /* v0 = bm x^l+ ... -> lcv*x^l + b(l-1)*(lcv(0)/bl)*x^(l-1)+... */
836: adjust_coef_sf(vl,rvl,lcu,u0,&u);
837: adjust_coef_sf(vl,rvl,lcv,v0,&v);
838: vx = vl->v; vy = NEXT(vl)->v;
839: n = getdeg(vx,f);
840: dy = getdeg(vy,f)+1;
841: MKV(vx,x);
842: cu = (P *)ALLOCA((n+1)*sizeof(P));
843: cv = (P *)ALLOCA((n+1)*sizeof(P));
844:
845: /* ydy = y^dy */
1.9 ! noro 846: ydy = C_UMALLOC(dy); DEG(ydy) = dy; COEF(ydy)[dy] = 1;
1.7 noro 847: setmod_gfsn(ydy);
848:
849: /* (R[y]/(y^dy))[x,X] */
850: poly_to_gfsn_poly(vl,f,vy,&t); ff = t;
851: poly_to_gfsn_poly(vl,u,vy,&t); u = t;
852: poly_to_gfsn_poly(vl,v,vy,&t); v = t;
853: substvp_sf(vl,rvl,u,0,&u0);
854: substvp_sf(vl,rvl,v,0,&v0);
855:
856: /* compute a(x,y), b(x,y) s.t. a*u0+b*v0 = 1 mod y^dy */
1.8 noro 857: extended_gcd_modyk(u0,v0,vx,vy,dy,&cu[0],&cv[0]);
1.7 noro 858:
859: /* du0 = LC(u0)^(-1)*u0 mod y^dy */
860: /* dv0 = LC(v0)^(-1)*v0 mod y^dy */
861: invgfsn((GFSN)LC(u0),&inv); mulp(vl,u0,(P)inv,&du0);
862: invgfsn((GFSN)LC(v0),&inv); mulp(vl,v0,(P)inv,&dv0);
863:
864: /* cu[i]*u0+cv[i]*v0 = x^i mod y^dy */
865: for ( i = 1; i <= n; i++ ) {
866: mulp(vl,x,cu[i-1],&m); divsrp(vl,m,dv0,&q,&cu[i]);
867: mulp(vl,x,cv[i-1],&m); divsrp(vl,m,du0,&q,&cv[i]);
868: }
869: dbd = dbound(vx,f)+1;
870:
871: /* extract homogeneous parts */
872: W_CALLOC(dbd,P,uh); W_CALLOC(dbd,P,vh);
873: for ( i = 0; i <= dbd; i++ ) {
874: exthpc(vl,vx,u,i,&uh[i]); exthpc(vl,vx,v,i,&vh[i]);
875: }
876:
877: /* register degrees in each variables */
878: for ( nv = 0, tvl = rvl; tvl; tvl = NEXT(tvl), nv++ );
879: md = (int *)ALLOCA(nv*sizeof(int));
880: for ( i = 0, tvl = rvl; i < nv; tvl = NEXT(tvl), i++ )
881: md[i] = getdeg(tvl->v,ff);
882:
883: /* XXX for removing content of factor wrt vx */
884: NEWVL(onevl); onevl->v = vx; NEXT(onevl) = 0;
885:
886: for ( j = 1; j <= dbd; j++ ) {
887: for ( i = 0, tvl = rvl; i < nv; tvl = NEXT(tvl), i++ )
888: if ( getdeg(tvl->v,u)+getdeg(tvl->v,v) > md[i] ) {
889: *up = 0;
890: return;
891: }
892: for ( i = 0, t = 0; i <= j; i++ ) {
893: mulp(vl,uh[i],vh[j-i],&s); addp(vl,s,t,&w); t = w;
894: }
895: /* s = degree j part of (f-uv) */
896: exthpc(vl,vx,ff,j,&fj); subp(vl,fj,t,&s);
897: for ( i = 0, wu = 0, wv = 0; i <= n; i++ ) {
898: if ( s )
899: si = 0;
900: else if ( VR(s) == vx )
901: coefp(s,i,&si);
902: else if ( i == 0 )
903: si = s;
904: else
905: si = 0;
906: if ( si ) {
907: mulp(vl,si,cu[i],&m); addp(vl,wu,m,&t); wu = t;
908: mulp(vl,si,cv[i],&m); addp(vl,wv,m,&t); wv = t;
909: }
910: }
911: if ( !wu ) {
912: gfsn_poly_to_poly(vl,u,vy,&t); u = t;
913: if ( divtp(vl,f,u,&q) ) {
914: cont_pp_mv_sf(vl,onevl,u,&cont,up);
915: return;
916: }
917: }
918: if ( !wv ) {
919: gfsn_poly_to_poly(vl,v,vy,&t); v = t;
920: if ( divtp(vl,f,u,&q) ) {
921: cont_pp_mv_sf(vl,onevl,q,&cont,up);
922: return;
923: }
924: }
925: addp(vl,u,wu,&t); u = t;
926: addp(vl,uh[j],wu,&t); uh[j] = t;
927: addp(vl,v,wv,&t); v = t;
928: addp(vl,vh[j],wv,&t); vh[j] = t;
929: }
930: }
931:
932: void adjust_coef_sf(VL vl,VL rvl,P lcu,P u0,P *r)
933: {
934: P lcu0,cu;
935: DCP dc0,dcu,dc;
936:
937: substvp_sf(vl,rvl,lcu,0,&lcu0);
938: divsp(vl,lcu0,LC(u0),&cu);
939: for ( dc0 = 0, dcu = DC(u0); dcu; dcu = NEXT(dcu) ) {
940: if ( !dc0 ) {
941: NEXTDC(dc0,dc);
942: COEF(dc) = lcu;
943: } else {
944: NEXTDC(dc0,dc);
945: mulp(vl,cu,COEF(dcu),&COEF(dc));
946: }
947: DEG(dc) = DEG(dcu);
948: }
949: NEXT(dc) = 0;
950: MKP(VR(u0),dc0,*r);
951: }
952:
1.8 noro 953: void extended_gcd_modyk(P u0,P v0,V x,V y,int dy,P *cu,P *cv)
1.6 noro 954: {
1.8 noro 955: BM g,h,a,b;
956:
957: gfsn_univariate_to_sfbm(u0,dy,&g);
958: gfsn_univariate_to_sfbm(v0,dy,&h);
959: sfexgcd_by_hensel(g,h,dy,&a,&b);
960: sfbm_to_gfsn_univariate(a,x,y,cu);
961: sfbm_to_gfsn_univariate(b,x,y,cv);
962: }
963:
964: /* (F[y])[x] -> F[x][y] */
965:
966: void gfsn_univariate_to_sfbm(P f,int dy,BM *r)
967: {
968: int dx,d,i;
969: BM b;
970: UM cy;
971: DCP dc;
972:
973: dx = getdeg(VR(f),f);
974: b = BMALLOC(dx,dy);
975: DEG(b) = dy;
976: for ( dc = DC(f); dc; dc = NEXT(dc) ) {
977: /* d : degree in x, cy : poly in y */
978: d = QTOS(DEG(dc));
979: cy = BDY((GFSN)COEF(dc));
980: for ( i = DEG(cy); i >= 0; i-- )
981: COEF(COEF(b)[i])[d] = COEF(cy)[i];
982: }
1.9 ! noro 983: for ( i = 0; i <= dy; i++ )
! 984: degum(COEF(b)[i],dx);
1.8 noro 985: *r = b;
986: }
987:
988: void sfbm_to_gfsn_univariate(BM f,V x,V y,P *r)
989: {
990: P g;
991: VL vl;
992:
993: sfbmtop(f,x,y,&g);
994: NEWVL(vl); vl->v = x;
995: NEWVL(NEXT(vl)); NEXT(vl)->v = y;
996: NEXT(NEXT(vl)) = 0;
997: poly_to_gfsn_poly(vl,g,y,r);
1.6 noro 998: }
999:
1.7 noro 1000: void poly_to_gfsn_poly(VL vl,P f,V v,P *r)
1.6 noro 1001: {
1.8 noro 1002: VL tvl,nvl0,nvl;
1003: P g;
1004:
1005: /* (x,y,...,v,...) -> (x,y,...,v) */
1006: for ( nvl0 = 0, tvl = vl; tvl; tvl = NEXT(tvl) ) {
1007: if ( tvl->v != v ) {
1008: NEXTVL(nvl0,nvl);
1009: nvl->v = tvl->v;
1010: }
1011: }
1012: NEXTVL(nvl0,nvl);
1013: nvl->v = v;
1014: NEXT(nvl) = 0;
1015: reorderp(nvl0,vl,f,&g);
1016: poly_to_gfsn_poly_main(g,v,r);
1017: }
1018:
1019: void poly_to_gfsn_poly_main(P f,V v,P *r)
1020: {
1021: int d;
1022: UM u;
1023: GFSN g;
1024: DCP dc,dct,dc0;
1025:
1.9 ! noro 1026: if ( !f )
1.8 noro 1027: *r = f;
1.9 ! noro 1028: else if ( NUM(f) || VR(f) == v ) {
1.8 noro 1029: d = getdeg(v,f);
1030: u = UMALLOC(d);
1031: ptosfum(f,u);
1032: MKGFSN(u,g);
1033: *r = (P)g;
1034: } else {
1035: for ( dc0 = 0, dct = DC(f); dct; dct = NEXT(dct) ) {
1036: NEXTDC(dc0,dc);
1037: DEG(dc) = DEG(dct);
1038: poly_to_gfsn_poly_main(COEF(dct),v,&COEF(dc));
1039: }
1040: NEXT(dc) = 0;
1041: MKP(VR(f),dc0,*r);
1042: }
1.6 noro 1043: }
1044:
1.7 noro 1045: void gfsn_poly_to_poly(VL vl,P f,V v,P *r)
1.6 noro 1046: {
1.8 noro 1047: VL tvl,nvl0,nvl;
1048: P g;
1049:
1050: gfsn_poly_to_poly_main(f,v,&g);
1051: /* (x,y,...,v,...) -> (x,y,...,v) */
1052: for ( nvl0 = 0, tvl = vl; tvl; tvl = NEXT(tvl) ) {
1053: if ( tvl->v != v ) {
1054: NEXTVL(nvl0,nvl);
1055: nvl->v = tvl->v;
1056: }
1057: }
1058: NEXTVL(nvl0,nvl);
1059: nvl->v = v;
1060: NEXT(nvl) = 0;
1061: reorderp(vl,nvl0,g,r);
1062: }
1063:
1064: void gfsn_poly_to_poly_main(P f,V v,P *r)
1065: {
1066: DCP dc,dc0,dct;
1067:
1068: if ( !f )
1069: *r = f;
1070: else if ( NUM(f) ) {
1071: if ( NID((Num)f) == N_GFSN )
1072: sfumtop(v,BDY((GFSN)f),r);
1073: else
1074: *r = f;
1075: } else {
1076: for ( dc0 = 0, dct = DC(f); dct; dct = NEXT(dct) ) {
1077: NEXTDC(dc0,dc);
1078: DEG(dc) = DEG(dct);
1079: gfsn_poly_to_poly_main(COEF(dct),v,&COEF(dc));
1080: }
1081: NEXT(dc) = 0;
1082: MKP(VR(f),dc0,*r);
1083: }
1.1 noro 1084: }
1.9 ! noro 1085:
! 1086: void printsfum(UM f)
! 1087: {
! 1088: int i;
! 1089:
! 1090: for ( i = DEG(f); i >= 0; i-- ) {
! 1091: printf("+(");
! 1092: printf("%d",IFTOF(COEF(f)[i]));
! 1093: printf(")*y^%d",i);
! 1094: }
! 1095: }
! 1096:
! 1097: void printsfbm(BM f)
! 1098: {
! 1099: int i;
! 1100:
! 1101: for ( i = DEG(f); i >= 0; i-- ) {
! 1102: printf("+(");
! 1103: printsfum(COEF(f)[i]);
! 1104: printf(")*y^%d",i);
! 1105: }
! 1106: }
! 1107:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>