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