Annotation of OpenXM_contrib2/asir2000/engine/Fgfs.c, Revision 1.11
1.11 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Fgfs.c,v 1.10 2002/11/22 07:32:10 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);
1.10 noro 8: void head_monomial(VL vl,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]);
1.10 noro 354: head_monomial(nvl,vmin,ps[i],&ph[i],&t);
1.1 noro 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);
1.10 noro 380: head_monomial(nvl,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:
1.10 noro 447: void head_monomial(VL vl,V v,P p,P *coef,P *term)
1.1 noro 448: {
449: P t,s,u;
450: DCP dc;
451: GFS one;
452:
1.3 noro 453: itogfs(1,&one);
454: t = (P)one;
1.1 noro 455: while ( 1 ) {
456: if ( NUM(p) || VR(p) == v ) {
457: *coef = p;
458: *term = t;
459: return;
460: } else {
1.3 noro 461: NEWDC(dc);
462: COEF(dc) = (P)one; DEG(dc) = DEG(DC(p));
1.1 noro 463: MKP(VR(p),dc,s);
464: mulp(vl,t,s,&u); t = u;
465: p = COEF(DC(p));
466: }
467: }
468: }
469:
470: void cont_pp_mv_sf(VL vl,VL rvl,P p,P *c,P *pp)
471: {
472: DP dp;
473: MP t;
474: int i,m;
475: P *ps;
476:
477: ptod(vl,rvl,p,&dp);
478: for ( t = BDY(dp), m = 0; t; t = NEXT(t), m++ );
479: ps = (P *)ALLOCA(m*sizeof(P));
1.3 noro 480: for ( t = BDY(dp), i = 0; t; t = NEXT(t), i++ )
1.1 noro 481: ps[i] = C(t);
1.10 noro 482: gcdsf(vl,ps,m,c);
1.3 noro 483: divsp(vl,p,*c,pp);
1.5 noro 484: }
485:
486: void mfctrsf(VL vl, P f, DCP *dcp)
487: {
488: DCP dc0,dc,dct,dcs,dcr;
489: Obj obj;
490:
491: simp_ff((Obj)f,&obj); f = (P)obj;
492: sqfrsf(vl,f,&dct);
493: dc = dc0 = dct; dct = NEXT(dct); NEXT(dc) = 0;
494: for ( ; dct; dct = NEXT(dct) ) {
495: mfctrsfmain(vl,COEF(dct),&dcs);
496: for ( dcr = dcs; dcr; dcr = NEXT(dcr) )
497: DEG(dcr) = DEG(dct);
498: for ( ; NEXT(dc); dc = NEXT(dc) );
499: NEXT(dc) = dcs;
500: }
501: *dcp = dc0;
502: }
503:
504: /* f : sqfr, non const */
505:
506: void mfctrsfmain(VL vl, P f, DCP *dcp)
507: {
1.6 noro 508: VL tvl,nvl,rvl;
1.7 noro 509: DCP dc,dc0,dc1,dc2,dct,lcfdc,dcs;
510: int imin,inext,i,j,n,k,np;
1.5 noro 511: int *da;
512: V vx,vy;
513: V *va;
1.7 noro 514: P *l,*tl;
1.5 noro 515: P gcd,g,df,dfmin;
516: P pa[2];
1.10 noro 517: P f0,pp0,spp0,c,c0,x,y,u,v,lcf,lcu,lcv,u0,v0,t,s;
1.7 noro 518: P ype,yme;
1.6 noro 519: GFS ev,evy;
520: P *fp0;
521: int *mev,*win;
1.5 noro 522:
523: clctv(vl,f,&tvl); vl = tvl;
524: if ( !vl )
525: error("mfctrsfmain : cannot happen");
526: if ( !NEXT(vl) ) {
527: /* univariate */
528: ufctrsf(f,&dc);
529: /* remove lc */
530: *dcp = NEXT(dc);
531: return;
532: }
533: for ( n = 0, tvl = vl; tvl; tvl = NEXT(tvl), n++ );
534: va = (V *)ALLOCA(n*sizeof(int));
535: da = (int *)ALLOCA(n*sizeof(int));
536: /* find v s.t. diff(f,v) is nonzero and deg(f,v) is minimal */
537: imin = -1;
538: for ( i = 0, tvl = vl; i < n; tvl = NEXT(tvl), i++ ) {
539: va[i] = tvl->v;
540: da[i] = getdeg(va[i],f);
541: diffp(vl,f,va[i],&df);
542: if ( !df )
543: continue;
544: if ( imin < 0 || da[i] < da[imin] ) {
545: dfmin = df;
546: imin = i;
547: }
548: }
549: /* find v1 neq v s.t. deg(f,v) is minimal */
550: inext = -1;
551: for ( i = 0; i < n; i++ ) {
552: if ( i == imin )
553: continue;
554: if ( inext < 0 || da[i] < da[inext] )
555: inext = i;
556: }
557: pa[0] = f;
558: pa[1] = dfmin;
1.11 ! noro 559: gcdsf(vl,pa,2,&gcd);
1.5 noro 560: if ( !NUM(gcd) ) {
561: /* f = gcd * f/gcd */
562: mfctrsfmain(vl,gcd,&dc1);
563: divsp(vl,f,gcd,&g);
564: mfctrsfmain(vl,g,&dc2);
565: for ( dct = dc1; NEXT(dct); dct = NEXT(dct) );
566: NEXT(dct) = dc2;
567: *dcp = dc1;
568: return;
569: }
570: /* create vl s.t. vl[0] = va[imin], vl[1] = va[inext] */
571: nvl = 0;
572: NEXTVL(nvl,tvl); tvl->v = va[imin];
573: NEXTVL(nvl,tvl); tvl->v = va[inext];
574: for ( i = 0; i < n; i++ ) {
575: if ( i == imin || i == inext )
576: continue;
577: NEXTVL(nvl,tvl); tvl->v = va[i];
578: }
579: NEXT(tvl) = 0;
580:
1.10 noro 581: reorderp(nvl,vl,f,&g); f = g;
1.5 noro 582: vx = nvl->v;
583: vy = NEXT(nvl)->v;
1.6 noro 584: MKV(vx,x);
585: MKV(vy,y);
586: /* remaining variables */
587: rvl = NEXT(NEXT(nvl));
588: if ( !rvl ) {
1.5 noro 589: /* bivariate */
1.10 noro 590: sfbfctr(f,vx,vy,getdeg(vx,f),&dc1);
1.5 noro 591: for ( dc0 = 0; dc1; dc1 = NEXT(dc1) ) {
592: NEXTDC(dc0,dc);
593: DEG(dc) = ONE;
594: reorderp(vl,nvl,COEF(dc1),&COEF(dc));
595: }
596: NEXT(dc) = 0;
597: *dcp = dc0;
598: return;
599: }
1.6 noro 600: /* n >= 3; nvl = (vx,vy,X) */
601: /* find good evaluation pt for X */
602: mev = (int *)CALLOC(n-2,sizeof(int));
603: while ( 1 ) {
1.10 noro 604: /* lcf(mev)=0 => invalid */
605: substvp_sf(nvl,rvl,COEF(DC(f)),mev,&t);
606: if ( t ) {
607: substvp_sf(nvl,rvl,f,mev,&f0);
608: pa[0] = f0;
609: diffp(nvl,f0,vx,&pa[1]);
610: if ( pa[1] ) {
611: gcdsf(nvl,pa,2,&gcd);
1.6 noro 612: /* XXX maybe we have to accept the case where gcd is a poly of y */
1.10 noro 613: if ( NUM(gcd) )
614: break;
615: }
1.6 noro 616: }
1.7 noro 617: /* XXX if generated indices exceed q of GF(q) => error in indextogfs */
618: next_evaluation_point(mev,n-2);
1.6 noro 619: }
1.10 noro 620: /* f0 = f(x,y,mev) */
621: /* separate content; f0 may have the content wrt x */
622: cont_pp_sfp(nvl,f0,&c0,&pp0);
1.6 noro 623:
1.7 noro 624: /* factorize pp0; pp0 = pp0(x,y+evy) = prod dc */
625: sfbfctr_shift(pp0,vx,vy,getdeg(vx,pp0),&evy,&spp0,&dc); pp0 = spp0;
1.6 noro 626:
627: if ( !NEXT(dc) ) {
628: /* f is irreducible */
629: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0;
630: *dcp = dc;
631: return;
632: }
1.7 noro 633: /* ype = y+evy, yme = y-evy */
634: addp(nvl,y,(P)evy,&ype); subp(nvl,y,(P)evy,&yme);
635:
1.6 noro 636: /* shift c0; c0 <- c0(y+evy) */
1.7 noro 637: substp(nvl,c0,vy,ype,&s); c0 = s;
638:
639: /* shift f; f <- f(y+evy) */
640: substp(nvl,f,vy,ype,&s); f = s;
641:
642: /* now f(x,0,mev) = c0 * prod dc */
1.6 noro 643:
644: /* factorize lc_x(f) */
645: lcf = COEF(DC(f));
1.7 noro 646: mfctrsf(nvl,lcf,&dct);
647: /* skip the first element (= a number) */
648: dct = NEXT(dct);
649:
650: /* shift lcfdc; c <- c(X+mev) */
651: for ( lcfdc = 0; dct; dct = NEXT(dct) ) {
652: NEXTDC(lcfdc,dcs);
653: DEG(dcs) = DEG(dct);
654: shift_sf(nvl,rvl,COEF(dct),mev,1,&COEF(dcs));
655: }
656: NEXT(dcs) = 0;
1.6 noro 657:
658: /* np = number of bivariate factors */
659: for ( np = 0, dct = dc; dct; dct = NEXT(dct), np++ );
660: fp0 = (P *)ALLOCA((np+1)*sizeof(P));
661: for ( i = 0, dct = dc; i < np; dct = NEXT(dct), i++ )
662: fp0[i] = COEF(dct);
663: fp0[np] = 0;
1.7 noro 664: l = tl = (P *)ALLOCA((np+1)*sizeof(P));
1.6 noro 665: win = W_ALLOC(np+1);
1.7 noro 666:
667: /* f <- f(X+mev) */
668: shift_sf(nvl,rvl,f,mev,1,&s); f = s;
669:
1.6 noro 670: for ( k = 1, win[0] = 1, --np; ; ) {
671: itogfs(1,&u0);
672: /* u0 = product of selected factors */
673: for ( i = 0; i < k; i++ ) {
674: mulp(nvl,u0,fp0[win[i]],&t); u0 = t;
675: }
676: /* we have to consider the content */
1.10 noro 677: /* f0 = c0*u0*v0 */
1.7 noro 678: mulp(nvl,LC(u0),c0,&c); estimatelc_sf(nvl,rvl,c,lcfdc,&lcu);
1.6 noro 679: divsp(nvl,pp0,u0,&v0);
1.7 noro 680: mulp(nvl,LC(v0),c0,&c); estimatelc_sf(nvl,rvl,c,lcfdc,&lcv);
681: mfctrsf_hensel(nvl,rvl,f,pp0,u0,v0,lcu,lcv,&u);
682: if ( u ) {
683: /* save the factor */
684: reorderp(vl,nvl,u,&t);
685: /* x -> x-mev, y -> y-evy */
686: shift_sf(vl,rvl,t,mev,-1,&s); substp(vl,s,vy,yme,tl++);
687:
688: /* update f,pp0 */
689: divsp(nvl,f,u,&t); f = t;
690: divsp(nvl,pp0,u0,&t); pp0 = t;
691: /* update win, fp0 */
692: for ( i = 0; i < k-1; i++ )
693: for ( j = win[i]+1; j < win[i+1]; j++ )
694: fp0[j-i-1] = fp0[j];
695: for ( j = win[k-1]+1; j <= np; j++ )
696: fp0[j-k] = fp0[j];
697: if ( ( np -= k ) < k ) break;
698: if ( np-win[0]+1 < k )
699: if ( ++k <= np ) {
700: for ( i = 0; i < k; i++ )
701: win[i] = i + 1;
702: continue;
703: } else
704: break;
705: else
706: for ( i = 1; i < k; i++ )
707: win[i] = win[0] + i;
708: } else {
709: if ( ncombi(1,np,k,win) == 0 )
710: if ( k == np ) break;
711: else
712: for ( i = 0, ++k; i < k; i++ )
713: win[i] = i + 1;
714: }
1.6 noro 715: }
1.10 noro 716: reorderp(vl,nvl,f,&t);
717: /* x -> x-mev, y -> y-evy */
718: shift_sf(vl,rvl,t,mev,-1,&s); substp(vl,s,vy,yme,tl++);
719: *tl = 0;
720: for ( dc0 = 0, i = 0; l[i]; i++ ) {
721: NEXTDC(dc0,dc); DEG(dc) = ONE; COEF(dc) = l[i];
722: }
723: NEXT(dc) = 0; *dcp = dc0;
1.6 noro 724: }
725:
1.7 noro 726: void next_evaluation_point(int *e,int n)
727: {
728: int i,t,j;
729:
730: for ( i = n-1; i >= 0; i-- )
731: if ( e[i] ) break;
732: if ( i < 0 ) e[n-1] = 1;
733: else if ( i == 0 ) {
734: t = e[0]; e[0] = 0; e[n-1] = t+1;
735: } else {
736: e[i-1]++; t = e[i];
737: for ( j = i; j < n-1; j++ )
738: e[j] = 0;
739: e[n-1] = t-1;
740: }
741: }
742:
743: /*
744: * dc : f1^E1*...*fk^Ek
745: * find e1,...,ek s.t. fi(0)^ei | c
746: * and return f1^e1*...*fk^ek
747: * vl = (vx,vy,rvl)
748: */
749:
750: void estimatelc_sf(VL vl,VL rvl,P c,DCP dc,P *lcp)
751: {
752: DCP dct;
753: P r,c1,c2,t,s,f;
754: int i,d;
755: Q q;
756:
757: for ( dct = dc, r = (P)ONE; dct; dct = NEXT(dct) ) {
758: if ( NUM(COEF(dct)) )
759: continue;
760: /* constant part */
761: substvp_sf(vl,rvl,COEF(dct),0,&f);
762: d = QTOS(DEG(dct));
763: for ( i = 0, c1 = c; i < d; i++ )
764: if ( !divtp(vl,c1,f,&c2) )
765: break;
766: else
767: c1 = c2;
768: if ( i ) {
769: STOQ(i,q);
770: pwrp(vl,COEF(dct),q,&s); mulp(vl,r,s,&t); r = t;
771: }
772: }
773: *lcp = r;
774: }
775:
776: void substvp_sf(VL vl,VL rvl,P f,int *mev,P *r)
777: {
778: int i;
779: VL tvl;
780: P g,t;
781: GFS ev;
782:
783: for ( g = f, i = 0, tvl = rvl; tvl; tvl = NEXT(tvl), i++ ) {
784: if ( !mev )
785: ev = 0;
786: else
787: indextogfs(mev[i],&ev);
788: substp(vl,g,tvl->v,(P)ev,&t); g = t;
789: }
790: *r = g;
791: }
792:
793: /*
794: * f <- f(X+sgn*mev)
795: */
796:
797: void shift_sf(VL vl, VL rvl, P f, int *mev, int sgn, P *r)
798: {
799: VL tvl;
800: int i;
801: P x,g,t,s;
802: GFS ev;
803:
804: for ( g = f, tvl = rvl, i = 0; tvl; tvl = NEXT(tvl), i++ ) {
805: if ( !mev[i] )
806: continue;
807: indextogfs(mev[i],&ev);
808: MKV(tvl->v,x);
809: if ( sgn > 0 )
810: addp(vl,x,(P)ev,&t);
811: else
812: subp(vl,x,(P)ev,&t);
813: substp(vl,g,tvl->v,t,&s); g = s;
814: }
815: *r = g;
816: }
817:
818: /*
819: * pp(f(0)) = u0*v0
820: */
821:
822: void mfctrsf_hensel(VL vl,VL rvl,P f,P pp0,P u0,P v0,P lcu,P lcv,P *up)
823: {
824: VL tvl,onevl;
825: P t,s,w,u,v,ff,si,wu,wv,fj,cont;
826: UM ydy;
827: V vx,vy;
828: int dy,n,i,dbd,nv,j;
829: int *md;
830: P *uh,*vh;
831: P x,du0,dv0,m,q,r;
832: P *cu,*cv;
833: GFSN inv;
834:
835: /* adjust coeffs */
836: /* u0 = am x^m+ ... -> lcu*x^m + a(m-1)*(lcu(0)/am)*x^(m-1)+... */
837: /* v0 = bm x^l+ ... -> lcv*x^l + b(l-1)*(lcv(0)/bl)*x^(l-1)+... */
1.10 noro 838: /* f -> lcu*lcv*x^(m+l)+... */
1.7 noro 839: adjust_coef_sf(vl,rvl,lcu,u0,&u);
840: adjust_coef_sf(vl,rvl,lcv,v0,&v);
1.10 noro 841: mulp(vl,lcu,lcv,&t); divsp(vl,t,LC(f),&m); mulp(vl,m,f,&t); f = t;
842:
1.7 noro 843: vx = vl->v; vy = NEXT(vl)->v;
844: n = getdeg(vx,f);
845: dy = getdeg(vy,f)+1;
846: MKV(vx,x);
847: cu = (P *)ALLOCA((n+1)*sizeof(P));
848: cv = (P *)ALLOCA((n+1)*sizeof(P));
849:
850: /* ydy = y^dy */
1.10 noro 851: ydy = C_UMALLOC(dy); DEG(ydy) = dy; COEF(ydy)[dy] = _onesf();
1.7 noro 852: setmod_gfsn(ydy);
853:
854: /* (R[y]/(y^dy))[x,X] */
1.10 noro 855: poly_to_gfsn_poly(vl,f,vy,&ff);
1.7 noro 856: poly_to_gfsn_poly(vl,u,vy,&t); u = t;
857: poly_to_gfsn_poly(vl,v,vy,&t); v = t;
858: substvp_sf(vl,rvl,u,0,&u0);
859: substvp_sf(vl,rvl,v,0,&v0);
860:
861: /* compute a(x,y), b(x,y) s.t. a*u0+b*v0 = 1 mod y^dy */
1.8 noro 862: extended_gcd_modyk(u0,v0,vx,vy,dy,&cu[0],&cv[0]);
1.7 noro 863:
864: /* dv0 = LC(v0)^(-1)*v0 mod y^dy */
865: invgfsn((GFSN)LC(v0),&inv); mulp(vl,v0,(P)inv,&dv0);
866:
867: /* cu[i]*u0+cv[i]*v0 = x^i mod y^dy */
1.10 noro 868: /* (x*cu[i])*u0+(x*cv[i])*v0 = x^(i+1) */
869: /* x*cu[i] = q*dv0+r => cu[i+1] = r */
870: /* cv[i+1]*v0 = x*cv[i]*v0+q*u0*dv0 = (x*cv[i]+q*u0*inv)*v0 */
1.7 noro 871: for ( i = 1; i <= n; i++ ) {
872: mulp(vl,x,cu[i-1],&m); divsrp(vl,m,dv0,&q,&cu[i]);
1.10 noro 873: mulp(vl,x,cv[i-1],&m); mulp(vl,q,(P)inv,&t);
874: mulp(vl,t,u0,&s);
875: addp(vl,m,s,&cv[i]);
876: }
877:
878: #if 0
879: /* XXX : check */
880: for ( i = 0; i <= n; i++ ) {
881: mulp(vl,cu[i],u0,&m); mulp(vl,cv[i],v0,&s);
882: addp(vl,m,s,&w);
883: printexpr(vl,w);
884: fprintf(asir_out,"\n");
1.7 noro 885: }
1.10 noro 886: #endif
887:
1.7 noro 888: dbd = dbound(vx,f)+1;
889:
890: /* extract homogeneous parts */
891: W_CALLOC(dbd,P,uh); W_CALLOC(dbd,P,vh);
892: for ( i = 0; i <= dbd; i++ ) {
893: exthpc(vl,vx,u,i,&uh[i]); exthpc(vl,vx,v,i,&vh[i]);
894: }
895:
896: /* register degrees in each variables */
897: for ( nv = 0, tvl = rvl; tvl; tvl = NEXT(tvl), nv++ );
898: md = (int *)ALLOCA(nv*sizeof(int));
899: for ( i = 0, tvl = rvl; i < nv; tvl = NEXT(tvl), i++ )
1.10 noro 900: md[i] = getdeg(tvl->v,f);
1.7 noro 901:
902: /* XXX for removing content of factor wrt vx */
903: NEWVL(onevl); onevl->v = vx; NEXT(onevl) = 0;
904:
905: for ( j = 1; j <= dbd; j++ ) {
906: for ( i = 0, tvl = rvl; i < nv; tvl = NEXT(tvl), i++ )
907: if ( getdeg(tvl->v,u)+getdeg(tvl->v,v) > md[i] ) {
908: *up = 0;
909: return;
910: }
911: for ( i = 0, t = 0; i <= j; i++ ) {
912: mulp(vl,uh[i],vh[j-i],&s); addp(vl,s,t,&w); t = w;
913: }
1.10 noro 914:
1.7 noro 915: /* s = degree j part of (f-uv) */
916: exthpc(vl,vx,ff,j,&fj); subp(vl,fj,t,&s);
917: for ( i = 0, wu = 0, wv = 0; i <= n; i++ ) {
1.10 noro 918: if ( !s )
1.7 noro 919: si = 0;
920: else if ( VR(s) == vx )
921: coefp(s,i,&si);
922: else if ( i == 0 )
923: si = s;
924: else
925: si = 0;
926: if ( si ) {
1.10 noro 927: mulp(vl,si,cv[i],&m); addp(vl,wu,m,&t); wu = t;
928: mulp(vl,si,cu[i],&m); addp(vl,wv,m,&t); wv = t;
1.7 noro 929: }
930: }
931: if ( !wu ) {
1.10 noro 932: gfsn_poly_to_poly(vl,u,vy,&t);
933: if ( divtp(vl,f,t,&q) ) {
934: cont_pp_mv_sf(vl,onevl,t,&cont,up);
1.7 noro 935: return;
936: }
937: }
938: if ( !wv ) {
1.10 noro 939: gfsn_poly_to_poly(vl,v,vy,&t);
940: if ( divtp(vl,f,t,&q) ) {
1.7 noro 941: cont_pp_mv_sf(vl,onevl,q,&cont,up);
942: return;
943: }
944: }
945: addp(vl,u,wu,&t); u = t;
946: addp(vl,uh[j],wu,&t); uh[j] = t;
947: addp(vl,v,wv,&t); v = t;
948: addp(vl,vh[j],wv,&t); vh[j] = t;
949: }
950: }
951:
952: void adjust_coef_sf(VL vl,VL rvl,P lcu,P u0,P *r)
953: {
954: P lcu0,cu;
955: DCP dc0,dcu,dc;
956:
957: substvp_sf(vl,rvl,lcu,0,&lcu0);
958: divsp(vl,lcu0,LC(u0),&cu);
959: for ( dc0 = 0, dcu = DC(u0); dcu; dcu = NEXT(dcu) ) {
960: if ( !dc0 ) {
961: NEXTDC(dc0,dc);
962: COEF(dc) = lcu;
963: } else {
964: NEXTDC(dc0,dc);
965: mulp(vl,cu,COEF(dcu),&COEF(dc));
966: }
967: DEG(dc) = DEG(dcu);
968: }
969: NEXT(dc) = 0;
970: MKP(VR(u0),dc0,*r);
971: }
972:
1.8 noro 973: void extended_gcd_modyk(P u0,P v0,V x,V y,int dy,P *cu,P *cv)
1.6 noro 974: {
1.8 noro 975: BM g,h,a,b;
976:
977: gfsn_univariate_to_sfbm(u0,dy,&g);
978: gfsn_univariate_to_sfbm(v0,dy,&h);
979: sfexgcd_by_hensel(g,h,dy,&a,&b);
980: sfbm_to_gfsn_univariate(a,x,y,cu);
981: sfbm_to_gfsn_univariate(b,x,y,cv);
982: }
983:
984: /* (F[y])[x] -> F[x][y] */
985:
986: void gfsn_univariate_to_sfbm(P f,int dy,BM *r)
987: {
988: int dx,d,i;
989: BM b;
990: UM cy;
991: DCP dc;
992:
993: dx = getdeg(VR(f),f);
994: b = BMALLOC(dx,dy);
995: DEG(b) = dy;
996: for ( dc = DC(f); dc; dc = NEXT(dc) ) {
997: /* d : degree in x, cy : poly in y */
998: d = QTOS(DEG(dc));
999: cy = BDY((GFSN)COEF(dc));
1000: for ( i = DEG(cy); i >= 0; i-- )
1001: COEF(COEF(b)[i])[d] = COEF(cy)[i];
1002: }
1.9 noro 1003: for ( i = 0; i <= dy; i++ )
1004: degum(COEF(b)[i],dx);
1.8 noro 1005: *r = b;
1006: }
1007:
1008: void sfbm_to_gfsn_univariate(BM f,V x,V y,P *r)
1009: {
1010: P g;
1011: VL vl;
1012:
1013: sfbmtop(f,x,y,&g);
1014: NEWVL(vl); vl->v = x;
1015: NEWVL(NEXT(vl)); NEXT(vl)->v = y;
1016: NEXT(NEXT(vl)) = 0;
1017: poly_to_gfsn_poly(vl,g,y,r);
1.6 noro 1018: }
1019:
1.7 noro 1020: void poly_to_gfsn_poly(VL vl,P f,V v,P *r)
1.6 noro 1021: {
1.8 noro 1022: VL tvl,nvl0,nvl;
1023: P g;
1024:
1025: /* (x,y,...,v,...) -> (x,y,...,v) */
1026: for ( nvl0 = 0, tvl = vl; tvl; tvl = NEXT(tvl) ) {
1027: if ( tvl->v != v ) {
1028: NEXTVL(nvl0,nvl);
1029: nvl->v = tvl->v;
1030: }
1031: }
1032: NEXTVL(nvl0,nvl);
1033: nvl->v = v;
1034: NEXT(nvl) = 0;
1035: reorderp(nvl0,vl,f,&g);
1036: poly_to_gfsn_poly_main(g,v,r);
1037: }
1038:
1039: void poly_to_gfsn_poly_main(P f,V v,P *r)
1040: {
1041: int d;
1042: UM u;
1043: GFSN g;
1044: DCP dc,dct,dc0;
1045:
1.9 noro 1046: if ( !f )
1.8 noro 1047: *r = f;
1.9 noro 1048: else if ( NUM(f) || VR(f) == v ) {
1.8 noro 1049: d = getdeg(v,f);
1050: u = UMALLOC(d);
1051: ptosfum(f,u);
1052: MKGFSN(u,g);
1053: *r = (P)g;
1054: } else {
1055: for ( dc0 = 0, dct = DC(f); dct; dct = NEXT(dct) ) {
1056: NEXTDC(dc0,dc);
1057: DEG(dc) = DEG(dct);
1058: poly_to_gfsn_poly_main(COEF(dct),v,&COEF(dc));
1059: }
1060: NEXT(dc) = 0;
1061: MKP(VR(f),dc0,*r);
1062: }
1.6 noro 1063: }
1064:
1.7 noro 1065: void gfsn_poly_to_poly(VL vl,P f,V v,P *r)
1.6 noro 1066: {
1.8 noro 1067: VL tvl,nvl0,nvl;
1068: P g;
1069:
1070: gfsn_poly_to_poly_main(f,v,&g);
1071: /* (x,y,...,v,...) -> (x,y,...,v) */
1072: for ( nvl0 = 0, tvl = vl; tvl; tvl = NEXT(tvl) ) {
1073: if ( tvl->v != v ) {
1074: NEXTVL(nvl0,nvl);
1075: nvl->v = tvl->v;
1076: }
1077: }
1078: NEXTVL(nvl0,nvl);
1079: nvl->v = v;
1080: NEXT(nvl) = 0;
1081: reorderp(vl,nvl0,g,r);
1082: }
1083:
1084: void gfsn_poly_to_poly_main(P f,V v,P *r)
1085: {
1086: DCP dc,dc0,dct;
1087:
1088: if ( !f )
1089: *r = f;
1090: else if ( NUM(f) ) {
1091: if ( NID((Num)f) == N_GFSN )
1092: sfumtop(v,BDY((GFSN)f),r);
1093: else
1094: *r = f;
1095: } else {
1096: for ( dc0 = 0, dct = DC(f); dct; dct = NEXT(dct) ) {
1097: NEXTDC(dc0,dc);
1098: DEG(dc) = DEG(dct);
1099: gfsn_poly_to_poly_main(COEF(dct),v,&COEF(dc));
1100: }
1101: NEXT(dc) = 0;
1102: MKP(VR(f),dc0,*r);
1103: }
1.1 noro 1104: }
1.9 noro 1105:
1106: void printsfum(UM f)
1107: {
1108: int i;
1109:
1110: for ( i = DEG(f); i >= 0; i-- ) {
1111: printf("+(");
1112: printf("%d",IFTOF(COEF(f)[i]));
1113: printf(")*y^%d",i);
1114: }
1115: }
1116:
1117: void printsfbm(BM f)
1118: {
1119: int i;
1120:
1121: for ( i = DEG(f); i >= 0; i-- ) {
1122: printf("+(");
1123: printsfum(COEF(f)[i]);
1124: printf(")*y^%d",i);
1125: }
1126: }
1127:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>