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