Annotation of OpenXM_contrib2/asir2000/builtin/dp-supp.c, Revision 1.60
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.60 ! fujimoto 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/dp-supp.c,v 1.59 2013/11/05 02:55:03 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "base.h"
1.16 noro 52: #include "inline.h"
1.1 noro 53: #include "parse.h"
54: #include "ox.h"
55:
1.5 noro 56: #define HMAG(p) (p_mag(BDY(p)->c))
57:
1.1 noro 58: extern int (*cmpdl)();
1.5 noro 59: extern double pz_t_e,pz_t_d,pz_t_d1,pz_t_c;
60: extern int dp_nelim,dp_fcoeffs;
1.7 noro 61: extern int NoGCD;
62: extern int GenTrace;
63: extern NODE TraceList;
64:
1.37 noro 65: int show_orderspec;
66:
67: void print_composite_order_spec(struct order_spec *spec);
68:
1.7 noro 69: /*
70: * content reduction
71: *
72: */
73:
1.46 noro 74: static NODE RatDenomList;
75:
76: void init_denomlist()
77: {
78: RatDenomList = 0;
79: }
80:
81: void add_denomlist(P f)
82: {
83: NODE n;
84:
85: if ( OID(f)==O_P ) {
86: MKNODE(n,f,RatDenomList); RatDenomList = n;
87: }
88: }
89:
90: LIST get_denomlist()
91: {
92: LIST l;
93:
94: MKLIST(l,RatDenomList); RatDenomList = 0;
95: return l;
96: }
97:
1.20 noro 98: void dp_ptozp(DP p,DP *rp)
1.7 noro 99: {
100: MP m,mr,mr0;
101: int i,n;
102: Q *w;
103: Q dvr;
104: P t;
105:
106: if ( !p )
107: *rp = 0;
108: else {
109: for ( m =BDY(p), n = 0; m; m = NEXT(m), n++ );
110: w = (Q *)ALLOCA(n*sizeof(Q));
111: for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
112: if ( NUM(m->c) )
113: w[i] = (Q)m->c;
114: else
115: ptozp(m->c,1,&w[i],&t);
116: sortbynm(w,n);
117: qltozl(w,n,&dvr);
118: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
119: NEXTMP(mr0,mr); divsp(CO,m->c,(P)dvr,&mr->c); mr->dl = m->dl;
120: }
121: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
122: }
123: }
124:
1.20 noro 125: void dp_ptozp2(DP p0,DP p1,DP *hp,DP *rp)
1.7 noro 126: {
127: DP t,s,h,r;
128: MP m,mr,mr0,m0;
129:
130: addd(CO,p0,p1,&t); dp_ptozp(t,&s);
131: if ( !p0 ) {
132: h = 0; r = s;
133: } else if ( !p1 ) {
134: h = s; r = 0;
135: } else {
136: for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
137: m = NEXT(m), m0 = NEXT(m0) ) {
138: NEXTMP(mr0,mr); mr->c = m->c; mr->dl = m->dl;
139: }
140: NEXT(mr) = 0; MKDP(p0->nv,mr0,h); MKDP(p0->nv,m,r);
141: }
142: if ( h )
143: h->sugar = p0->sugar;
144: if ( r )
145: r->sugar = p1->sugar;
146: *hp = h; *rp = r;
1.39 ohara 147: }
148:
149: void dp_ptozp3(DP p,Q *dvr,DP *rp)
150: {
151: MP m,mr,mr0;
152: int i,n;
153: Q *w;
154: P t;
155:
156: if ( !p ) {
157: *rp = 0; *dvr = 0;
158: }else {
159: for ( m =BDY(p), n = 0; m; m = NEXT(m), n++ );
160: w = (Q *)ALLOCA(n*sizeof(Q));
161: for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
162: if ( NUM(m->c) )
163: w[i] = (Q)m->c;
164: else
165: ptozp(m->c,1,&w[i],&t);
166: sortbynm(w,n);
167: qltozl(w,n,dvr);
168: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
169: NEXTMP(mr0,mr); divsp(CO,m->c,(P)(*dvr),&mr->c); mr->dl = m->dl;
170: }
171: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
172: }
1.7 noro 173: }
1.1 noro 174:
1.20 noro 175: void dp_idiv(DP p,Q c,DP *rp)
1.1 noro 176: {
177: Q t;
178: N nm,q;
179: int sgn,s;
180: MP mr0,m,mr;
181:
182: if ( !p )
183: *rp = 0;
184: else if ( MUNIQ((Q)c) )
185: *rp = p;
186: else if ( MUNIQ((Q)c) )
187: chsgnd(p,rp);
188: else {
189: nm = NM(c); sgn = SGN(c);
190: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
191: NEXTMP(mr0,mr);
192:
193: divsn(NM((Q)(m->c)),nm,&q);
194: s = sgn*SGN((Q)(m->c));
195: NTOQ(q,s,t);
196: mr->c = (P)t;
197: mr->dl = m->dl;
198: }
199: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp);
200: if ( *rp )
201: (*rp)->sugar = p->sugar;
202: }
203: }
204:
1.20 noro 205: void dp_mbase(NODE hlist,NODE *mbase)
1.1 noro 206: {
207: DL *dl;
208: DL d;
209: int i,j,n,nvar,td;
210:
211: n = length(hlist); nvar = ((DP)BDY(hlist))->nv;
212: dl = (DL *)MALLOC(n*sizeof(DL));
213: for ( i = 0; i < n; i++, hlist = NEXT(hlist) )
214: dl[i] = BDY((DP)BDY(hlist))->dl;
215: NEWDL(d,nvar); *mbase = 0;
216: while ( 1 ) {
217: insert_to_node(d,mbase,nvar);
218: for ( i = nvar-1; i >= 0; ) {
1.21 noro 219: d->d[i]++;
220: d->td += MUL_WEIGHT(1,i);
1.1 noro 221: for ( j = 0; j < n; j++ ) {
222: if ( _dl_redble(dl[j],d,nvar) )
223: break;
224: }
225: if ( j < n ) {
226: for ( j = nvar-1; j >= i; j-- )
227: d->d[j] = 0;
228: for ( j = 0, td = 0; j < i; j++ )
1.21 noro 229: td += MUL_WEIGHT(d->d[j],j);
1.1 noro 230: d->td = td;
231: i--;
232: } else
233: break;
234: }
235: if ( i < 0 )
236: break;
237: }
238: }
239:
1.20 noro 240: int _dl_redble(DL d1,DL d2,int nvar)
1.1 noro 241: {
242: int i;
243:
244: if ( d1->td > d2->td )
245: return 0;
246: for ( i = 0; i < nvar; i++ )
247: if ( d1->d[i] > d2->d[i] )
248: break;
249: if ( i < nvar )
250: return 0;
251: else
252: return 1;
253: }
254:
1.20 noro 255: void insert_to_node(DL d,NODE *n,int nvar)
1.1 noro 256: {
257: DL d1;
258: MP m;
259: DP dp;
260: NODE n0,n1,n2;
261:
262: NEWDL(d1,nvar); d1->td = d->td;
263: bcopy((char *)d->d,(char *)d1->d,nvar*sizeof(int));
264: NEWMP(m); m->dl = d1; m->c = (P)ONE; NEXT(m) = 0;
265: MKDP(nvar,m,dp); dp->sugar = d->td;
266: if ( !(*n) ) {
267: MKNODE(n1,dp,0); *n = n1;
268: } else {
269: for ( n1 = *n, n0 = 0; n1; n0 = n1, n1 = NEXT(n1) )
270: if ( (*cmpdl)(nvar,d,BDY((DP)BDY(n1))->dl) > 0 ) {
271: MKNODE(n2,dp,n1);
272: if ( !n0 )
273: *n = n2;
274: else
275: NEXT(n0) = n2;
276: break;
277: }
278: if ( !n1 ) {
279: MKNODE(n2,dp,0); NEXT(n0) = n2;
280: }
281: }
282: }
283:
1.20 noro 284: void dp_vtod(Q *c,DP p,DP *rp)
1.1 noro 285: {
286: MP mr0,m,mr;
287: int i;
288:
289: if ( !p )
290: *rp = 0;
291: else {
292: for ( mr0 = 0, m = BDY(p), i = 0; m; m = NEXT(m), i++ ) {
293: NEXTMP(mr0,mr); mr->c = (P)c[i]; mr->dl = m->dl;
294: }
295: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp);
296: (*rp)->sugar = p->sugar;
297: }
298: }
299:
1.8 noro 300: extern int mpi_mag;
301: extern int PCoeffs;
302:
1.20 noro 303: void dp_ptozp_d(DP p,DP *rp)
1.1 noro 304: {
305: int i,j,k,l,n,nsep;
306: MP m;
307: NODE tn,n0,n1,n2,n3;
308: struct oVECT v;
309: VECT c,cs;
310: VECT qi,ri;
311: LIST *qr;
312: Obj dmy;
313: Q d0,d1,gcd,a,u,u1;
314: Q *q,*r;
315: STRING iqr_v;
316: pointer *b;
317: N qn,gn;
318: double get_rtime();
319: int blen;
1.8 noro 320: NODE dist;
321: int ndist;
1.1 noro 322: double t0;
323: double t_e,t_d,t_d1,t_c;
1.8 noro 324: extern int DP_NFStat;
325: extern LIST Dist;
1.20 noro 326: void Pox_rpc();
327: void Pox_pop_local();
1.1 noro 328:
329: if ( !p )
330: *rp = 0;
331: else {
1.8 noro 332: if ( PCoeffs ) {
333: dp_ptozp(p,rp); return;
334: }
1.9 noro 335: if ( !Dist || p_mag(BDY(p)->c) <= mpi_mag ) {
1.8 noro 336: dist = 0; ndist = 0;
337: if ( DP_NFStat ) fprintf(asir_out,"L");
338: } else {
339: dist = BDY(Dist); ndist = length(dist);
340: if ( DP_NFStat ) fprintf(asir_out,"D");
341: }
1.1 noro 342: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
343: nsep = ndist + 1;
344: if ( n <= nsep ) {
345: dp_ptozp(p,rp); return;
346: }
347: t0 = get_rtime();
348: dp_dtov(p,&c);
349: igcdv_estimate(c,&d0);
350: t_e = get_rtime()-t0;
351: t0 = get_rtime();
352: dp_dtov(p,&c);
353: sepvect(c,nsep,&cs);
354: MKSTR(iqr_v,"iqr");
355: qr = (LIST *)CALLOC(nsep,sizeof(LIST));
356: q = (Q *)CALLOC(n,sizeof(Q));
357: r = (Q *)CALLOC(n,sizeof(Q));
358: for ( i = 0, tn = dist, b = BDY(cs); i < ndist; i++, tn = NEXT(tn) ) {
359: MKNODE(n3,d0,0); MKNODE(n2,b[i],n3);
360: MKNODE(n1,iqr_v,n2); MKNODE(n0,BDY(tn),n1);
361: Pox_rpc(n0,&dmy);
362: }
363: iqrv(b[i],d0,&qr[i]);
364: dp_dtov(p,&c);
365: for ( i = 0, tn = dist; i < ndist; i++, tn = NEXT(tn) ) {
366: Pox_pop_local(tn,&qr[i]);
367: if ( OID(qr[i]) == O_ERR ) {
368: printexpr(CO,(Obj)qr[i]);
369: error("dp_ptozp_d : aborted");
370: }
371: }
372: t_d = get_rtime()-t0;
373: t_d1 = t_d/n;
374: t0 = get_rtime();
375: for ( i = j = 0; i < nsep; i++ ) {
376: tn = BDY(qr[i]); qi = (VECT)BDY(tn); ri = (VECT)BDY(NEXT(tn));
377: for ( k = 0, l = qi->len; k < l; k++, j++ ) {
378: q[j] = (Q)BDY(qi)[k]; r[j] = (Q)BDY(ri)[k];
379: }
380: }
381: v.id = O_VECT; v.len = n; v.body = (pointer *)r; igcdv(&v,&d1);
382: if ( d1 ) {
383: gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
384: divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
385: for ( i = 0; i < n; i++ ) {
386: mulq(a,q[i],&u);
387: if ( r[i] ) {
388: divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
389: addq(u,u1,&q[i]);
390: } else
391: q[i] = u;
392: }
393: } else
394: gcd = d0;
395: dp_vtod(q,p,rp);
396: t_c = get_rtime()-t0;
397: blen=p_mag((P)gcd);
398: pz_t_e += t_e; pz_t_d += t_d; pz_t_d1 += t_d1; pz_t_c += t_c;
399: if ( 0 )
400: fprintf(stderr,"(%d,%d)",p_mag((P)d0)-blen,blen);
401: }
1.60 ! fujimoto 402: #if defined(__MINGW32__) || defined(__MINGW64__)
! 403: fflush(stderr);
! 404: #endif
1.1 noro 405: }
406:
1.20 noro 407: void dp_ptozp2_d(DP p0,DP p1,DP *hp,DP *rp)
1.1 noro 408: {
409: DP t,s,h,r;
410: MP m,mr,mr0,m0;
411:
1.8 noro 412: addd(CO,p0,p1,&t); dp_ptozp_d(t,&s);
1.1 noro 413: if ( !p0 ) {
414: h = 0; r = s;
415: } else if ( !p1 ) {
416: h = s; r = 0;
417: } else {
418: for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
419: m = NEXT(m), m0 = NEXT(m0) ) {
420: NEXTMP(mr0,mr); mr->c = m->c; mr->dl = m->dl;
421: }
422: NEXT(mr) = 0; MKDP(p0->nv,mr0,h); MKDP(p0->nv,m,r);
423: }
424: if ( h )
425: h->sugar = p0->sugar;
426: if ( r )
427: r->sugar = p1->sugar;
428: *hp = h; *rp = r;
1.5 noro 429: }
430:
1.22 noro 431: int have_sf_coef(P p)
432: {
433: DCP dc;
434:
435: if ( !p )
436: return 0;
437: else if ( NUM(p) )
438: return NID((Num)p) == N_GFS ? 1 : 0;
439: else {
440: for ( dc = DC(p); dc; dc = NEXT(dc) )
441: if ( have_sf_coef(COEF(dc)) )
442: return 1;
443: return 0;
444: }
445: }
446:
1.25 noro 447: void head_coef(P p,Num *c)
448: {
449: if ( !p )
450: *c = 0;
451: else if ( NUM(p) )
452: *c = (Num)p;
453: else
454: head_coef(COEF(DC(p)),c);
455: }
456:
457: void dp_monic_sf(DP p,DP *rp)
458: {
459: Num c;
460:
461: if ( !p )
462: *rp = 0;
463: else {
464: head_coef(BDY(p)->c,&c);
465: divsdc(CO,p,(P)c,rp);
466: }
467: }
468:
1.20 noro 469: void dp_prim(DP p,DP *rp)
1.5 noro 470: {
1.7 noro 471: P t,g;
472: DP p1;
473: MP m,mr,mr0;
474: int i,n;
475: P *w;
476: Q *c;
477: Q dvr;
1.46 noro 478: NODE tn;
1.5 noro 479:
1.7 noro 480: if ( !p )
481: *rp = 0;
1.23 noro 482: else if ( dp_fcoeffs == N_GFS ) {
483: for ( m = BDY(p); m; m = NEXT(m) )
1.22 noro 484: if ( OID(m->c) == O_N ) {
485: /* GCD of coeffs = 1 */
1.25 noro 486: dp_monic_sf(p,rp);
1.22 noro 487: return;
1.23 noro 488: } else break;
489: /* compute GCD over the finite fieid */
490: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
491: w = (P *)ALLOCA(n*sizeof(P));
492: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ )
493: w[i] = m->c;
494: gcdsf(CO,w,n,&g);
495: if ( NUM(g) )
1.25 noro 496: dp_monic_sf(p,rp);
1.23 noro 497: else {
498: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
499: NEXTMP(mr0,mr); divsp(CO,m->c,g,&mr->c); mr->dl = m->dl;
1.22 noro 500: }
1.25 noro 501: NEXT(mr) = 0; MKDP(p->nv,mr0,p1); p1->sugar = p->sugar;
502: dp_monic_sf(p1,rp);
1.22 noro 503: }
1.23 noro 504: return;
505: } else if ( dp_fcoeffs )
1.7 noro 506: *rp = p;
1.23 noro 507: else if ( NoGCD )
1.7 noro 508: dp_ptozp(p,rp);
509: else {
510: dp_ptozp(p,&p1); p = p1;
511: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
512: if ( n == 1 ) {
513: m = BDY(p);
514: NEWMP(mr); mr->dl = m->dl; mr->c = (P)ONE; NEXT(mr) = 0;
515: MKDP(p->nv,mr,*rp); (*rp)->sugar = p->sugar;
516: return;
517: }
518: w = (P *)ALLOCA(n*sizeof(P));
519: c = (Q *)ALLOCA(n*sizeof(Q));
520: for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
521: if ( NUM(m->c) ) {
522: c[i] = (Q)m->c; w[i] = (P)ONE;
523: } else
524: ptozp(m->c,1,&c[i],&w[i]);
525: qltozl(c,n,&dvr); heu_nezgcdnpz(CO,w,n,&t); mulp(CO,t,(P)dvr,&g);
526: if ( NUM(g) )
527: *rp = p;
528: else {
529: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
530: NEXTMP(mr0,mr); divsp(CO,m->c,g,&mr->c); mr->dl = m->dl;
531: }
532: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.46 noro 533: add_denomlist(g);
1.5 noro 534: }
1.7 noro 535: }
1.5 noro 536: }
537:
1.20 noro 538: void heu_nezgcdnpz(VL vl,P *pl,int m,P *pr)
1.5 noro 539: {
540: int i,r;
541: P gcd,t,s1,s2,u;
542: Q rq;
1.40 noro 543: DCP dc;
544: extern int DP_Print;
545:
1.5 noro 546: while ( 1 ) {
547: for ( i = 0, s1 = 0; i < m; i++ ) {
548: r = random(); UTOQ(r,rq);
549: mulp(vl,pl[i],(P)rq,&t); addp(vl,s1,t,&u); s1 = u;
550: }
551: for ( i = 0, s2 = 0; i < m; i++ ) {
552: r = random(); UTOQ(r,rq);
553: mulp(vl,pl[i],(P)rq,&t); addp(vl,s2,t,&u); s2 = u;
554: }
555: ezgcdp(vl,s1,s2,&gcd);
1.40 noro 556: if ( DP_Print > 2 )
557: { fprintf(asir_out,"(%d)",nmonop(gcd)); fflush(asir_out); }
1.5 noro 558: for ( i = 0; i < m; i++ ) {
559: if ( !divtpz(vl,pl[i],gcd,&t) )
560: break;
561: }
562: if ( i == m )
563: break;
564: }
565: *pr = gcd;
566: }
567:
1.20 noro 568: void dp_prim_mod(DP p,int mod,DP *rp)
1.5 noro 569: {
570: P t,g;
571: MP m,mr,mr0;
572:
573: if ( !p )
574: *rp = 0;
575: else if ( NoGCD )
576: *rp = p;
577: else {
578: for ( m = BDY(p), g = m->c, m = NEXT(m); m; m = NEXT(m) ) {
579: gcdprsmp(CO,mod,g,m->c,&t); g = t;
580: }
581: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
582: NEXTMP(mr0,mr); divsmp(CO,mod,m->c,g,&mr->c); mr->dl = m->dl;
583: }
584: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
585: }
586: }
587:
1.20 noro 588: void dp_cont(DP p,Q *rp)
1.5 noro 589: {
1.7 noro 590: VECT v;
1.5 noro 591:
1.7 noro 592: dp_dtov(p,&v); igcdv(v,rp);
1.5 noro 593: }
594:
1.20 noro 595: void dp_dtov(DP dp,VECT *rp)
1.5 noro 596: {
1.7 noro 597: MP m,t;
598: int i,n;
599: VECT v;
600: pointer *p;
1.5 noro 601:
1.7 noro 602: m = BDY(dp);
603: for ( t = m, n = 0; t; t = NEXT(t), n++ );
604: MKVECT(v,n);
605: for ( i = 0, p = BDY(v), t = m; i < n; t = NEXT(t), i++ )
606: p[i] = (pointer)(t->c);
607: *rp = v;
1.5 noro 608: }
609:
1.7 noro 610: /*
611: * s-poly computation
612: *
613: */
1.5 noro 614:
1.20 noro 615: void dp_sp(DP p1,DP p2,DP *rp)
1.5 noro 616: {
1.7 noro 617: int i,n,td;
618: int *w;
619: DL d1,d2,d;
620: MP m;
621: DP t,s1,s2,u;
622: Q c,c1,c2;
623: N gn,tn;
1.5 noro 624:
1.7 noro 625: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
626: w = (int *)ALLOCA(n*sizeof(int));
627: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 628: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.5 noro 629: }
1.7 noro 630:
631: NEWDL(d,n); d->td = td - d1->td;
632: for ( i = 0; i < n; i++ )
633: d->d[i] = w[i] - d1->d[i];
634: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
635: if ( INT(c1) && INT(c2) ) {
636: gcdn(NM(c1),NM(c2),&gn);
637: if ( !UNIN(gn) ) {
638: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
639: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
1.5 noro 640: }
641: }
1.7 noro 642:
643: NEWMP(m); m->dl = d; m->c = (P)c2; NEXT(m) = 0;
644: MKDP(n,m,s1); s1->sugar = d->td; muld(CO,s1,p1,&t);
645:
646: NEWDL(d,n); d->td = td - d2->td;
647: for ( i = 0; i < n; i++ )
648: d->d[i] = w[i] - d2->d[i];
649: NEWMP(m); m->dl = d; m->c = (P)c1; NEXT(m) = 0;
650: MKDP(n,m,s2); s2->sugar = d->td; muld(CO,s2,p2,&u);
651:
652: subd(CO,t,u,rp);
1.14 noro 653: if ( GenTrace ) {
654: LIST hist;
655: NODE node;
656:
1.58 noro 657: node = mknode(4,ONE,NULLP,s1,ONE);
1.14 noro 658: MKLIST(hist,node);
659: MKNODE(TraceList,hist,0);
660:
1.58 noro 661: node = mknode(4,ONE,NULLP,NULLP,ONE);
1.14 noro 662: chsgnd(s2,(DP *)&ARG2(node));
663: MKLIST(hist,node);
664: MKNODE(node,hist,TraceList); TraceList = node;
665: }
666: }
667:
1.20 noro 668: void _dp_sp_dup(DP p1,DP p2,DP *rp)
1.14 noro 669: {
670: int i,n,td;
671: int *w;
672: DL d1,d2,d;
673: MP m;
674: DP t,s1,s2,u;
675: Q c,c1,c2;
676: N gn,tn;
677:
678: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
679: w = (int *)ALLOCA(n*sizeof(int));
680: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 681: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.14 noro 682: }
683:
684: _NEWDL(d,n); d->td = td - d1->td;
685: for ( i = 0; i < n; i++ )
686: d->d[i] = w[i] - d1->d[i];
687: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
688: if ( INT(c1) && INT(c2) ) {
689: gcdn(NM(c1),NM(c2),&gn);
690: if ( !UNIN(gn) ) {
691: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
692: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
693: }
694: }
695:
696: _NEWMP(m); m->dl = d; m->c = (P)c2; NEXT(m) = 0;
697: _MKDP(n,m,s1); s1->sugar = d->td; _muld_dup(CO,s1,p1,&t); _free_dp(s1);
698:
699: _NEWDL(d,n); d->td = td - d2->td;
700: for ( i = 0; i < n; i++ )
701: d->d[i] = w[i] - d2->d[i];
702: _NEWMP(m); m->dl = d; chsgnp((P)c1,&m->c); NEXT(m) = 0;
703: _MKDP(n,m,s2); s2->sugar = d->td; _muld_dup(CO,s2,p2,&u); _free_dp(s2);
704:
705: _addd_destructive(CO,t,u,rp);
1.7 noro 706: if ( GenTrace ) {
707: LIST hist;
708: NODE node;
709:
1.58 noro 710: node = mknode(4,ONE,NULLP,s1,ONE);
1.7 noro 711: MKLIST(hist,node);
712: MKNODE(TraceList,hist,0);
713:
1.58 noro 714: node = mknode(4,ONE,NULLP,NULLP,ONE);
1.7 noro 715: chsgnd(s2,(DP *)&ARG2(node));
716: MKLIST(hist,node);
717: MKNODE(node,hist,TraceList); TraceList = node;
718: }
719: }
720:
1.20 noro 721: void dp_sp_mod(DP p1,DP p2,int mod,DP *rp)
1.7 noro 722: {
723: int i,n,td;
724: int *w;
725: DL d1,d2,d;
726: MP m;
727: DP t,s,u;
728:
729: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
730: w = (int *)ALLOCA(n*sizeof(int));
731: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 732: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 733: }
1.18 noro 734: NEWDL_NOINIT(d,n); d->td = td - d1->td;
1.7 noro 735: for ( i = 0; i < n; i++ )
736: d->d[i] = w[i] - d1->d[i];
737: NEWMP(m); m->dl = d; m->c = (P)BDY(p2)->c; NEXT(m) = 0;
738: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p1,s,&t);
1.18 noro 739: NEWDL_NOINIT(d,n); d->td = td - d2->td;
1.7 noro 740: for ( i = 0; i < n; i++ )
741: d->d[i] = w[i] - d2->d[i];
742: NEWMP(m); m->dl = d; m->c = (P)BDY(p1)->c; NEXT(m) = 0;
743: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p2,s,&u);
744: submd(CO,mod,t,u,rp);
745: }
746:
1.20 noro 747: void _dp_sp_mod_dup(DP p1,DP p2,int mod,DP *rp)
1.7 noro 748: {
749: int i,n,td;
750: int *w;
751: DL d1,d2,d;
752: MP m;
753: DP t,s,u;
754:
755: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
756: w = (int *)ALLOCA(n*sizeof(int));
757: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 758: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 759: }
760: _NEWDL(d,n); d->td = td - d1->td;
761: for ( i = 0; i < n; i++ )
762: d->d[i] = w[i] - d1->d[i];
763: _NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
764: _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p1,&t); _free_dp(s);
765: _NEWDL(d,n); d->td = td - d2->td;
766: for ( i = 0; i < n; i++ )
767: d->d[i] = w[i] - d2->d[i];
768: _NEWMP(m); m->dl = d; m->c = STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
769: _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p2,&u); _free_dp(s);
770: _addmd_destructive(mod,t,u,rp);
771: }
772:
1.20 noro 773: void _dp_sp_mod(DP p1,DP p2,int mod,DP *rp)
1.7 noro 774: {
775: int i,n,td;
776: int *w;
777: DL d1,d2,d;
778: MP m;
779: DP t,s,u;
780:
781: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
782: w = (int *)ALLOCA(n*sizeof(int));
783: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 784: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 785: }
786: NEWDL(d,n); d->td = td - d1->td;
787: for ( i = 0; i < n; i++ )
788: d->d[i] = w[i] - d1->d[i];
789: NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
790: MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p1,&t);
791: NEWDL(d,n); d->td = td - d2->td;
792: for ( i = 0; i < n; i++ )
793: d->d[i] = w[i] - d2->d[i];
794: NEWMP(m); m->dl = d; m->c = STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
795: MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p2,&u);
796: addmd_destructive(mod,t,u,rp);
797: }
798:
799: /*
800: * m-reduction
1.13 noro 801: * do content reduction over Z or Q(x,...)
802: * do nothing over finite fields
1.7 noro 803: *
804: */
805:
1.20 noro 806: void dp_red(DP p0,DP p1,DP p2,DP *head,DP *rest,P *dnp,DP *multp)
1.7 noro 807: {
808: int i,n;
809: DL d1,d2,d;
810: MP m;
811: DP t,s,r,h;
812: Q c,c1,c2;
813: N gn,tn;
814: P g,a;
1.23 noro 815: P p[2];
1.7 noro 816:
817: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
818: NEWDL(d,n); d->td = d1->td - d2->td;
819: for ( i = 0; i < n; i++ )
820: d->d[i] = d1->d[i]-d2->d[i];
821: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
1.23 noro 822: if ( dp_fcoeffs == N_GFS ) {
823: p[0] = (P)c1; p[1] = (P)c2;
824: gcdsf(CO,p,2,&g);
825: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
826: } else if ( dp_fcoeffs ) {
1.7 noro 827: /* do nothing */
828: } else if ( INT(c1) && INT(c2) ) {
829: gcdn(NM(c1),NM(c2),&gn);
830: if ( !UNIN(gn) ) {
831: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
832: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
833: }
834: } else {
835: ezgcdpz(CO,(P)c1,(P)c2,&g);
836: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
1.46 noro 837: add_denomlist(g);
1.7 noro 838: }
839: NEWMP(m); m->dl = d; chsgnp((P)c1,&m->c); NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
840: *multp = s;
841: muld(CO,s,p2,&t); muldc(CO,p1,(P)c2,&s); addd(CO,s,t,&r);
842: muldc(CO,p0,(P)c2,&h);
843: *head = h; *rest = r; *dnp = (P)c2;
844: }
845:
1.41 noro 846: /*
847: * m-reduction by a marked poly
848: * do content reduction over Z or Q(x,...)
849: * do nothing over finite fields
850: *
851: */
852:
853:
854: void dp_red_marked(DP p0,DP p1,DP p2,DP hp2,DP *head,DP *rest,P *dnp,DP *multp)
855: {
856: int i,n;
857: DL d1,d2,d;
858: MP m;
859: DP t,s,r,h;
860: Q c,c1,c2;
861: N gn,tn;
862: P g,a;
863: P p[2];
864:
865: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(hp2)->dl;
866: NEWDL(d,n); d->td = d1->td - d2->td;
867: for ( i = 0; i < n; i++ )
868: d->d[i] = d1->d[i]-d2->d[i];
869: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(hp2)->c;
870: if ( dp_fcoeffs == N_GFS ) {
871: p[0] = (P)c1; p[1] = (P)c2;
872: gcdsf(CO,p,2,&g);
873: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
874: } else if ( dp_fcoeffs ) {
875: /* do nothing */
876: } else if ( INT(c1) && INT(c2) ) {
877: gcdn(NM(c1),NM(c2),&gn);
878: if ( !UNIN(gn) ) {
879: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
880: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
881: }
882: } else {
883: ezgcdpz(CO,(P)c1,(P)c2,&g);
884: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
885: }
1.47 noro 886: NEWMP(m); m->dl = d; m->c = (P)c1; NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
1.41 noro 887: *multp = s;
1.47 noro 888: muld(CO,s,p2,&t); muldc(CO,p1,(P)c2,&s); subd(CO,s,t,&r);
1.41 noro 889: muldc(CO,p0,(P)c2,&h);
890: *head = h; *rest = r; *dnp = (P)c2;
891: }
892:
1.55 noro 893: void dp_red_marked_mod(DP p0,DP p1,DP p2,DP hp2,int mod,DP *head,DP *rest,P *dnp,DP *multp)
1.44 noro 894: {
895: int i,n;
896: DL d1,d2,d;
897: MP m;
898: DP t,s,r,h;
899: P c1,c2,g,u;
900:
901: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(hp2)->dl;
902: NEWDL(d,n); d->td = d1->td - d2->td;
903: for ( i = 0; i < n; i++ )
904: d->d[i] = d1->d[i]-d2->d[i];
905: c1 = (P)BDY(p1)->c; c2 = (P)BDY(hp2)->c;
906: gcdprsmp(CO,mod,c1,c2,&g);
907: divsmp(CO,mod,c1,g,&u); c1 = u; divsmp(CO,mod,c2,g,&u); c2 = u;
908: if ( NUM(c2) ) {
909: divsmp(CO,mod,c1,c2,&u); c1 = u; c2 = (P)ONEM;
910: }
1.55 noro 911: NEWMP(m); m->dl = d; m->c = (P)c1; NEXT(m) = 0;
912: MKDP(n,m,s); s->sugar = d->td;
913: *multp = s;
914: mulmd(CO,mod,s,p2,&t);
1.44 noro 915: if ( NUM(c2) ) {
1.55 noro 916: submd(CO,mod,p1,t,&r); h = p0;
1.44 noro 917: } else {
1.55 noro 918: mulmdc(CO,mod,p1,c2,&s); submd(CO,mod,s,t,&r); mulmdc(CO,mod,p0,c2,&h);
1.44 noro 919: }
920: *head = h; *rest = r; *dnp = c2;
921: }
922:
1.13 noro 923: /* m-reduction over a field */
924:
1.20 noro 925: void dp_red_f(DP p1,DP p2,DP *rest)
1.13 noro 926: {
927: int i,n;
928: DL d1,d2,d;
929: MP m;
1.20 noro 930: DP t,s;
1.13 noro 931: Obj a,b;
932:
933: n = p1->nv;
934: d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
935:
936: NEWDL(d,n); d->td = d1->td - d2->td;
937: for ( i = 0; i < n; i++ )
938: d->d[i] = d1->d[i]-d2->d[i];
939:
940: NEWMP(m); m->dl = d;
941: divr(CO,(Obj)BDY(p1)->c,(Obj)BDY(p2)->c,&a); chsgnr(a,&b);
942: C(m) = (P)b;
943: NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
944:
945: muld(CO,s,p2,&t); addd(CO,p1,t,rest);
946: }
947:
1.20 noro 948: void dp_red_mod(DP p0,DP p1,DP p2,int mod,DP *head,DP *rest,P *dnp)
1.7 noro 949: {
950: int i,n;
951: DL d1,d2,d;
952: MP m;
953: DP t,s,r,h;
954: P c1,c2,g,u;
955:
956: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
957: NEWDL(d,n); d->td = d1->td - d2->td;
958: for ( i = 0; i < n; i++ )
959: d->d[i] = d1->d[i]-d2->d[i];
960: c1 = (P)BDY(p1)->c; c2 = (P)BDY(p2)->c;
961: gcdprsmp(CO,mod,c1,c2,&g);
962: divsmp(CO,mod,c1,g,&u); c1 = u; divsmp(CO,mod,c2,g,&u); c2 = u;
963: if ( NUM(c2) ) {
964: divsmp(CO,mod,c1,c2,&u); c1 = u; c2 = (P)ONEM;
965: }
966: NEWMP(m); m->dl = d; chsgnmp(mod,(P)c1,&m->c); NEXT(m) = 0;
1.11 noro 967: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,s,p2,&t);
1.7 noro 968: if ( NUM(c2) ) {
969: addmd(CO,mod,p1,t,&r); h = p0;
970: } else {
971: mulmdc(CO,mod,p1,c2,&s); addmd(CO,mod,s,t,&r); mulmdc(CO,mod,p0,c2,&h);
972: }
973: *head = h; *rest = r; *dnp = c2;
974: }
975:
1.10 noro 976: struct oEGT eg_red_mod;
977:
1.20 noro 978: void _dp_red_mod_destructive(DP p1,DP p2,int mod,DP *rp)
1.7 noro 979: {
980: int i,n;
981: DL d1,d2,d;
982: MP m;
983: DP t,s;
1.16 noro 984: int c,c1,c2;
985: extern int do_weyl;
1.7 noro 986:
987: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
988: _NEWDL(d,n); d->td = d1->td - d2->td;
989: for ( i = 0; i < n; i++ )
990: d->d[i] = d1->d[i]-d2->d[i];
1.16 noro 991: c = invm(ITOS(BDY(p2)->c),mod);
992: c2 = ITOS(BDY(p1)->c);
993: DMAR(c,c2,0,mod,c1);
1.7 noro 994: _NEWMP(m); m->dl = d; m->c = STOI(mod-c1); NEXT(m) = 0;
1.16 noro 995: #if 0
1.7 noro 996: _MKDP(n,m,s); s->sugar = d->td;
997: _mulmd_dup(mod,s,p2,&t); _free_dp(s);
1.16 noro 998: #else
999: if ( do_weyl ) {
1.19 noro 1000: _MKDP(n,m,s); s->sugar = d->td;
1001: _mulmd_dup(mod,s,p2,&t); _free_dp(s);
1.16 noro 1002: } else {
1003: _mulmdm_dup(mod,p2,m,&t); _FREEMP(m);
1004: }
1005: #endif
1.10 noro 1006: /* get_eg(&t0); */
1.7 noro 1007: _addmd_destructive(mod,p1,t,rp);
1.10 noro 1008: /* get_eg(&t1); add_eg(&eg_red_mod,&t0,&t1); */
1.7 noro 1009: }
1010:
1011: /*
1012: * normal form computation
1013: *
1014: */
1.5 noro 1015:
1.20 noro 1016: void dp_true_nf(NODE b,DP g,DP *ps,int full,DP *rp,P *dnp)
1.5 noro 1017: {
1018: DP u,p,d,s,t,dmy;
1019: NODE l;
1020: MP m,mr;
1021: int i,n;
1022: int *wb;
1023: int sugar,psugar;
1024: P dn,tdn,tdn1;
1025:
1026: dn = (P)ONE;
1027: if ( !g ) {
1028: *rp = 0; *dnp = dn; return;
1029: }
1030: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1031: wb = (int *)ALLOCA(n*sizeof(int));
1032: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1033: wb[i] = QTOS((Q)BDY(l));
1034: sugar = g->sugar;
1035: for ( d = 0; g; ) {
1036: for ( u = 0, i = 0; i < n; i++ ) {
1037: if ( dp_redble(g,p = ps[wb[i]]) ) {
1038: dp_red(d,g,p,&t,&u,&tdn,&dmy);
1039: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1040: sugar = MAX(sugar,psugar);
1041: if ( !u ) {
1042: if ( d )
1043: d->sugar = sugar;
1044: *rp = d; *dnp = dn; return;
1045: } else {
1046: d = t;
1047: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
1048: }
1049: break;
1050: }
1051: }
1052: if ( u )
1053: g = u;
1054: else if ( !full ) {
1055: if ( g ) {
1056: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1057: }
1058: *rp = g; *dnp = dn; return;
1059: } else {
1060: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1061: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1062: addd(CO,d,t,&s); d = s;
1063: dp_rest(g,&t); g = t;
1064: }
1065: }
1066: if ( d )
1067: d->sugar = sugar;
1068: *rp = d; *dnp = dn;
1069: }
1070:
1.43 noro 1071: void dp_removecont2(DP p1,DP p2,DP *r1p,DP *r2p,Q *contp)
1072: {
1073: struct oVECT v;
1074: int i,n1,n2,n;
1075: MP m,m0,t;
1076: Q *w;
1077: Q h;
1078:
1079: if ( p1 ) {
1080: for ( i = 0, m = BDY(p1); m; m = NEXT(m), i++ );
1081: n1 = i;
1082: } else
1083: n1 = 0;
1084: if ( p2 ) {
1085: for ( i = 0, m = BDY(p2); m; m = NEXT(m), i++ );
1086: n2 = i;
1087: } else
1088: n2 = 0;
1089: n = n1+n2;
1090: if ( !n ) {
1091: *r1p = 0; *r2p = 0; *contp = ONE; return;
1092: }
1093: w = (Q *)ALLOCA(n*sizeof(Q));
1094: v.len = n;
1095: v.body = (pointer *)w;
1096: i = 0;
1097: if ( p1 )
1098: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = (Q)m->c;
1099: if ( p2 )
1100: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = (Q)m->c;
1101: h = w[0]; removecont_array((P *)w,n,1); divq(h,w[0],contp);
1102: i = 0;
1103: if ( p1 ) {
1104: for ( m0 = 0, t = BDY(p1); i < n1; i++, t = NEXT(t) ) {
1105: NEXTMP(m0,m); m->c = (P)w[i]; m->dl = t->dl;
1106: }
1107: NEXT(m) = 0;
1108: MKDP(p1->nv,m0,*r1p); (*r1p)->sugar = p1->sugar;
1109: } else
1110: *r1p = 0;
1111: if ( p2 ) {
1112: for ( m0 = 0, t = BDY(p2); i < n; i++, t = NEXT(t) ) {
1113: NEXTMP(m0,m); m->c = (P)w[i]; m->dl = t->dl;
1114: }
1115: NEXT(m) = 0;
1116: MKDP(p2->nv,m0,*r2p); (*r2p)->sugar = p2->sugar;
1117: } else
1118: *r2p = 0;
1119: }
1120:
1.41 noro 1121: /* true nf by a marked GB */
1122:
1.43 noro 1123: void dp_true_nf_marked(NODE b,DP g,DP *ps,DP *hps,DP *rp,P *nmp,P *dnp)
1.41 noro 1124: {
1125: DP u,p,d,s,t,dmy,hp;
1126: NODE l;
1127: MP m,mr;
1.43 noro 1128: int i,n,hmag;
1.41 noro 1129: int *wb;
1.43 noro 1130: int sugar,psugar,multiple;
1131: P nm,tnm1,dn,tdn,tdn1;
1132: Q cont;
1.41 noro 1133:
1.43 noro 1134: multiple = 0;
1135: hmag = multiple*HMAG(g);
1136: nm = (P)ONE;
1.41 noro 1137: dn = (P)ONE;
1138: if ( !g ) {
1139: *rp = 0; *dnp = dn; return;
1140: }
1141: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1142: wb = (int *)ALLOCA(n*sizeof(int));
1143: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1144: wb[i] = QTOS((Q)BDY(l));
1145: sugar = g->sugar;
1146: for ( d = 0; g; ) {
1147: for ( u = 0, i = 0; i < n; i++ ) {
1148: if ( dp_redble(g,hp = hps[wb[i]]) ) {
1149: p = ps[wb[i]];
1150: dp_red_marked(d,g,p,hp,&t,&u,&tdn,&dmy);
1151: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1152: sugar = MAX(sugar,psugar);
1153: if ( !u ) {
1.43 noro 1154: goto last;
1.41 noro 1155: } else {
1156: d = t;
1157: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
1158: }
1159: break;
1160: }
1161: }
1.43 noro 1162: if ( u ) {
1.41 noro 1163: g = u;
1.43 noro 1164: if ( multiple && ((d && HMAG(d)>hmag) || (HMAG(g)>hmag)) ) {
1165: dp_removecont2(d,g,&t,&u,&cont); d = t; g = u;
1166: mulp(CO,nm,(P)cont,&tnm1); nm = tnm1;
1167: if ( d )
1168: hmag = multiple*HMAG(d);
1169: else
1170: hmag = multiple*HMAG(g);
1171: }
1172: } else {
1.41 noro 1173: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1174: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1175: addd(CO,d,t,&s); d = s;
1176: dp_rest(g,&t); g = t;
1177: }
1178: }
1.43 noro 1179: last:
1180: if ( d ) {
1181: dp_removecont2(d,0,&t,&u,&cont); d = t;
1182: mulp(CO,nm,(P)cont,&tnm1); nm = tnm1;
1.41 noro 1183: d->sugar = sugar;
1.43 noro 1184: }
1185: *rp = d; *nmp = nm; *dnp = dn;
1.41 noro 1186: }
1187:
1.44 noro 1188: void dp_true_nf_marked_mod(NODE b,DP g,DP *ps,DP *hps,int mod,DP *rp,P *dnp)
1189: {
1.55 noro 1190: DP hp,u,p,d,s,t,dmy;
1.44 noro 1191: NODE l;
1192: MP m,mr;
1193: int i,n;
1194: int *wb;
1195: int sugar,psugar;
1196: P dn,tdn,tdn1;
1197:
1198: dn = (P)ONEM;
1199: if ( !g ) {
1200: *rp = 0; *dnp = dn; return;
1201: }
1202: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1203: wb = (int *)ALLOCA(n*sizeof(int));
1204: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1205: wb[i] = QTOS((Q)BDY(l));
1206: sugar = g->sugar;
1207: for ( d = 0; g; ) {
1208: for ( u = 0, i = 0; i < n; i++ ) {
1209: if ( dp_redble(g,hp = hps[wb[i]]) ) {
1210: p = ps[wb[i]];
1.55 noro 1211: dp_red_marked_mod(d,g,p,hp,mod,&t,&u,&tdn,&dmy);
1.44 noro 1212: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1213: sugar = MAX(sugar,psugar);
1214: if ( !u ) {
1215: if ( d )
1216: d->sugar = sugar;
1217: *rp = d; *dnp = dn; return;
1218: } else {
1219: d = t;
1220: mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
1221: }
1222: break;
1223: }
1224: }
1225: if ( u )
1226: g = u;
1227: else {
1228: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1229: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1230: addmd(CO,mod,d,t,&s); d = s;
1231: dp_rest(g,&t); g = t;
1232: }
1233: }
1234: if ( d )
1235: d->sugar = sugar;
1236: *rp = d; *dnp = dn;
1237: }
1238:
1.47 noro 1239: /* true nf by a marked GB and collect quotients */
1240:
1241: DP *dp_true_nf_and_quotient_marked (NODE b,DP g,DP *ps,DP *hps,DP *rp,P *dnp)
1242: {
1243: DP u,p,d,s,t,dmy,hp,mult;
1244: DP *q;
1245: NODE l;
1246: MP m,mr;
1247: int i,n,j;
1248: int *wb;
1249: int sugar,psugar,multiple;
1250: P nm,tnm1,dn,tdn,tdn1;
1251: Q cont;
1252:
1253: dn = (P)ONE;
1254: if ( !g ) {
1.59 noro 1255: *rp = 0; *dnp = dn; return 0;
1.47 noro 1256: }
1257: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1258: wb = (int *)ALLOCA(n*sizeof(int));
1259: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1260: wb[i] = QTOS((Q)BDY(l));
1261: q = (DP *)MALLOC(n*sizeof(DP));
1262: for ( i = 0; i < n; i++ ) q[i] = 0;
1263: sugar = g->sugar;
1264: for ( d = 0; g; ) {
1265: for ( u = 0, i = 0; i < n; i++ ) {
1266: if ( dp_redble(g,hp = hps[wb[i]]) ) {
1267: p = ps[wb[i]];
1268: dp_red_marked(d,g,p,hp,&t,&u,&tdn,&mult);
1269: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1270: sugar = MAX(sugar,psugar);
1271: for ( j = 0; j < n; j++ ) {
1272: muldc(CO,q[j],(P)tdn,&dmy); q[j] = dmy;
1273: }
1274: addd(CO,q[wb[i]],mult,&dmy); q[wb[i]] = dmy;
1275: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
1276: d = t;
1277: if ( !u ) goto last;
1278: break;
1279: }
1280: }
1281: if ( u ) {
1282: g = u;
1283: } else {
1284: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1285: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1286: addd(CO,d,t,&s); d = s;
1287: dp_rest(g,&t); g = t;
1288: }
1289: }
1290: last:
1291: if ( d ) d->sugar = sugar;
1292: *rp = d; *dnp = dn;
1293: return q;
1294: }
1295:
1.55 noro 1296: DP *dp_true_nf_and_quotient_marked_mod(NODE b,DP g,DP *ps,DP *hps,int mod,DP *rp,P *dnp)
1297: {
1298: DP u,p,d,s,t,dmy,hp,mult;
1299: DP *q;
1300: NODE l;
1301: MP m,mr;
1302: int i,n,j;
1303: int *wb;
1304: int sugar,psugar;
1305: P dn,tdn,tdn1;
1306:
1307: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1308: q = (DP *)MALLOC(n*sizeof(DP));
1309: for ( i = 0; i < n; i++ ) q[i] = 0;
1310: dn = (P)ONEM;
1311: if ( !g ) {
1.59 noro 1312: *rp = 0; *dnp = dn; return 0;
1.55 noro 1313: }
1314: wb = (int *)ALLOCA(n*sizeof(int));
1315: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1316: wb[i] = QTOS((Q)BDY(l));
1317: sugar = g->sugar;
1318: for ( d = 0; g; ) {
1319: for ( u = 0, i = 0; i < n; i++ ) {
1320: if ( dp_redble(g,hp = hps[wb[i]]) ) {
1321: p = ps[wb[i]];
1322: dp_red_marked_mod(d,g,p,hp,mod,&t,&u,&tdn,&mult);
1323: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1324: sugar = MAX(sugar,psugar);
1325: for ( j = 0; j < n; j++ ) {
1326: mulmdc(CO,mod,q[j],(P)tdn,&dmy); q[j] = dmy;
1327: }
1328: addmd(CO,mod,q[wb[i]],mult,&dmy); q[wb[i]] = dmy;
1329: mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
1330: d = t;
1331: if ( !u ) goto last;
1332: break;
1333: }
1334: }
1335: if ( u )
1336: g = u;
1337: else {
1338: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1339: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1340: addmd(CO,mod,d,t,&s); d = s;
1341: dp_rest(g,&t); g = t;
1342: }
1343: }
1344: last:
1345: if ( d )
1346: d->sugar = sugar;
1347: *rp = d; *dnp = dn;
1348: return q;
1349: }
1350:
1.13 noro 1351: /* nf computation over Z */
1352:
1.20 noro 1353: void dp_nf_z(NODE b,DP g,DP *ps,int full,int multiple,DP *rp)
1.5 noro 1354: {
1355: DP u,p,d,s,t,dmy1;
1356: P dmy;
1357: NODE l;
1358: MP m,mr;
1359: int i,n;
1360: int *wb;
1361: int hmag;
1362: int sugar,psugar;
1363:
1364: if ( !g ) {
1365: *rp = 0; return;
1366: }
1367: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1368: wb = (int *)ALLOCA(n*sizeof(int));
1369: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1370: wb[i] = QTOS((Q)BDY(l));
1.12 noro 1371:
1.13 noro 1372: hmag = multiple*HMAG(g);
1.5 noro 1373: sugar = g->sugar;
1.12 noro 1374:
1.5 noro 1375: for ( d = 0; g; ) {
1376: for ( u = 0, i = 0; i < n; i++ ) {
1377: if ( dp_redble(g,p = ps[wb[i]]) ) {
1378: dp_red(d,g,p,&t,&u,&dmy,&dmy1);
1379: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1380: sugar = MAX(sugar,psugar);
1381: if ( !u ) {
1382: if ( d )
1383: d->sugar = sugar;
1384: *rp = d; return;
1385: }
1386: d = t;
1387: break;
1388: }
1389: }
1390: if ( u ) {
1391: g = u;
1392: if ( d ) {
1.13 noro 1393: if ( multiple && HMAG(d) > hmag ) {
1.5 noro 1394: dp_ptozp2(d,g,&t,&u); d = t; g = u;
1395: hmag = multiple*HMAG(d);
1396: }
1397: } else {
1.13 noro 1398: if ( multiple && HMAG(g) > hmag ) {
1.5 noro 1399: dp_ptozp(g,&t); g = t;
1400: hmag = multiple*HMAG(g);
1401: }
1402: }
1403: }
1404: else if ( !full ) {
1405: if ( g ) {
1406: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1407: }
1408: *rp = g; return;
1409: } else {
1410: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1411: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1412: addd(CO,d,t,&s); d = s;
1413: dp_rest(g,&t); g = t;
1414:
1415: }
1416: }
1417: if ( d )
1418: d->sugar = sugar;
1419: *rp = d;
1420: }
1421:
1.13 noro 1422: /* nf computation over a field */
1423:
1.20 noro 1424: void dp_nf_f(NODE b,DP g,DP *ps,int full,DP *rp)
1.13 noro 1425: {
1426: DP u,p,d,s,t;
1427: NODE l;
1428: MP m,mr;
1429: int i,n;
1430: int *wb;
1431: int sugar,psugar;
1432:
1433: if ( !g ) {
1434: *rp = 0; return;
1435: }
1436: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1437: wb = (int *)ALLOCA(n*sizeof(int));
1438: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1439: wb[i] = QTOS((Q)BDY(l));
1440:
1441: sugar = g->sugar;
1442: for ( d = 0; g; ) {
1443: for ( u = 0, i = 0; i < n; i++ ) {
1444: if ( dp_redble(g,p = ps[wb[i]]) ) {
1445: dp_red_f(g,p,&u);
1446: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1447: sugar = MAX(sugar,psugar);
1448: if ( !u ) {
1449: if ( d )
1450: d->sugar = sugar;
1451: *rp = d; return;
1452: }
1453: break;
1454: }
1455: }
1456: if ( u )
1457: g = u;
1458: else if ( !full ) {
1459: if ( g ) {
1460: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1461: }
1462: *rp = g; return;
1463: } else {
1464: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1465: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1466: addd(CO,d,t,&s); d = s;
1467: dp_rest(g,&t); g = t;
1468: }
1469: }
1470: if ( d )
1471: d->sugar = sugar;
1472: *rp = d;
1473: }
1474:
1475: /* nf computation over GF(mod) (only for internal use) */
1476:
1.20 noro 1477: void dp_nf_mod(NODE b,DP g,DP *ps,int mod,int full,DP *rp)
1.5 noro 1478: {
1479: DP u,p,d,s,t;
1480: P dmy;
1481: NODE l;
1482: MP m,mr;
1483: int sugar,psugar;
1484:
1485: if ( !g ) {
1486: *rp = 0; return;
1487: }
1488: sugar = g->sugar;
1489: for ( d = 0; g; ) {
1490: for ( u = 0, l = b; l; l = NEXT(l) ) {
1491: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
1492: dp_red_mod(d,g,p,mod,&t,&u,&dmy);
1493: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1494: sugar = MAX(sugar,psugar);
1495: if ( !u ) {
1496: if ( d )
1497: d->sugar = sugar;
1498: *rp = d; return;
1499: }
1500: d = t;
1501: break;
1502: }
1503: }
1504: if ( u )
1505: g = u;
1506: else if ( !full ) {
1507: if ( g ) {
1508: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1509: }
1510: *rp = g; return;
1511: } else {
1512: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1513: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1514: addmd(CO,mod,d,t,&s); d = s;
1515: dp_rest(g,&t); g = t;
1516: }
1517: }
1518: if ( d )
1519: d->sugar = sugar;
1520: *rp = d;
1521: }
1522:
1.20 noro 1523: void dp_true_nf_mod(NODE b,DP g,DP *ps,int mod,int full,DP *rp,P *dnp)
1.5 noro 1524: {
1525: DP u,p,d,s,t;
1526: NODE l;
1527: MP m,mr;
1528: int i,n;
1529: int *wb;
1530: int sugar,psugar;
1531: P dn,tdn,tdn1;
1532:
1533: dn = (P)ONEM;
1534: if ( !g ) {
1535: *rp = 0; *dnp = dn; return;
1536: }
1537: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1538: wb = (int *)ALLOCA(n*sizeof(int));
1539: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1540: wb[i] = QTOS((Q)BDY(l));
1541: sugar = g->sugar;
1542: for ( d = 0; g; ) {
1543: for ( u = 0, i = 0; i < n; i++ ) {
1544: if ( dp_redble(g,p = ps[wb[i]]) ) {
1545: dp_red_mod(d,g,p,mod,&t,&u,&tdn);
1546: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1547: sugar = MAX(sugar,psugar);
1548: if ( !u ) {
1549: if ( d )
1550: d->sugar = sugar;
1551: *rp = d; *dnp = dn; return;
1552: } else {
1553: d = t;
1554: mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
1555: }
1556: break;
1557: }
1558: }
1559: if ( u )
1560: g = u;
1561: else if ( !full ) {
1562: if ( g ) {
1563: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1564: }
1565: *rp = g; *dnp = dn; return;
1566: } else {
1567: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1568: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1569: addmd(CO,mod,d,t,&s); d = s;
1570: dp_rest(g,&t); g = t;
1571: }
1572: }
1573: if ( d )
1574: d->sugar = sugar;
1575: *rp = d; *dnp = dn;
1576: }
1577:
1.20 noro 1578: void _dp_nf_mod_destructive(NODE b,DP g,DP *ps,int mod,int full,DP *rp)
1.5 noro 1579: {
1.20 noro 1580: DP u,p,d;
1.7 noro 1581: NODE l;
1.20 noro 1582: MP m,mrd;
1583: int sugar,psugar,n,h_reducible;
1.5 noro 1584:
1.7 noro 1585: if ( !g ) {
1586: *rp = 0; return;
1.5 noro 1587: }
1.7 noro 1588: sugar = g->sugar;
1589: n = g->nv;
1590: for ( d = 0; g; ) {
1591: for ( h_reducible = 0, l = b; l; l = NEXT(l) ) {
1592: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
1593: h_reducible = 1;
1594: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1595: _dp_red_mod_destructive(g,p,mod,&u); g = u;
1596: sugar = MAX(sugar,psugar);
1597: if ( !g ) {
1598: if ( d )
1599: d->sugar = sugar;
1600: _dptodp(d,rp); _free_dp(d); return;
1601: }
1602: break;
1603: }
1604: }
1605: if ( !h_reducible ) {
1606: /* head term is not reducible */
1607: if ( !full ) {
1608: if ( g )
1609: g->sugar = sugar;
1610: _dptodp(g,rp); _free_dp(g); return;
1611: } else {
1612: m = BDY(g);
1613: if ( NEXT(m) ) {
1614: BDY(g) = NEXT(m); NEXT(m) = 0;
1615: } else {
1616: _FREEDP(g); g = 0;
1617: }
1618: if ( d ) {
1619: for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
1620: NEXT(mrd) = m;
1621: } else {
1622: _MKDP(n,m,d);
1623: }
1624: }
1625: }
1.5 noro 1626: }
1.7 noro 1627: if ( d )
1628: d->sugar = sugar;
1629: _dptodp(d,rp); _free_dp(d);
1.5 noro 1630: }
1.13 noro 1631:
1632: /* reduction by linear base over a field */
1633:
1.20 noro 1634: void dp_lnf_f(DP p1,DP p2,NODE g,DP *r1p,DP *r2p)
1.13 noro 1635: {
1636: DP r1,r2,b1,b2,t,s;
1637: Obj c,c1,c2;
1638: NODE l,b;
1639: int n;
1640:
1641: if ( !p1 ) {
1642: *r1p = p1; *r2p = p2; return;
1643: }
1644: n = p1->nv;
1645: for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
1646: if ( !r1 ) {
1647: *r1p = r1; *r2p = r2; return;
1648: }
1649: b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
1650: if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
1651: b2 = (DP)BDY(NEXT(b));
1652: divr(CO,(Obj)ONE,(Obj)BDY(b1)->c,&c1);
1653: mulr(CO,c1,(Obj)BDY(r1)->c,&c2); chsgnr(c2,&c);
1654: muldc(CO,b1,(P)c,&t); addd(CO,r1,t,&s); r1 = s;
1655: muldc(CO,b2,(P)c,&t); addd(CO,r2,t,&s); r2 = s;
1656: }
1657: }
1658: *r1p = r1; *r2p = r2;
1659: }
1660:
1661: /* reduction by linear base over GF(mod) */
1.5 noro 1662:
1.20 noro 1663: void dp_lnf_mod(DP p1,DP p2,NODE g,int mod,DP *r1p,DP *r2p)
1.5 noro 1664: {
1.7 noro 1665: DP r1,r2,b1,b2,t,s;
1666: P c;
1667: MQ c1,c2;
1668: NODE l,b;
1669: int n;
1670:
1671: if ( !p1 ) {
1672: *r1p = p1; *r2p = p2; return;
1673: }
1674: n = p1->nv;
1675: for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
1676: if ( !r1 ) {
1677: *r1p = r1; *r2p = r2; return;
1678: }
1679: b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
1680: if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
1681: b2 = (DP)BDY(NEXT(b));
1682: invmq(mod,(MQ)BDY(b1)->c,&c1);
1683: mulmq(mod,c1,(MQ)BDY(r1)->c,&c2); chsgnmp(mod,(P)c2,&c);
1684: mulmdc(CO,mod,b1,c,&t); addmd(CO,mod,r1,t,&s); r1 = s;
1685: mulmdc(CO,mod,b2,c,&t); addmd(CO,mod,r2,t,&s); r2 = s;
1686: }
1687: }
1688: *r1p = r1; *r2p = r2;
1.5 noro 1689: }
1690:
1.20 noro 1691: void dp_nf_tab_mod(DP p,LIST *tab,int mod,DP *rp)
1.5 noro 1692: {
1.7 noro 1693: DP s,t,u;
1694: MP m;
1695: DL h;
1696: int i,n;
1697:
1698: if ( !p ) {
1699: *rp = p; return;
1700: }
1701: n = p->nv;
1702: for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
1703: h = m->dl;
1704: while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
1705: i++;
1706: mulmdc(CO,mod,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
1707: addmd(CO,mod,s,t,&u); s = u;
1.24 noro 1708: }
1709: *rp = s;
1710: }
1711:
1712: void dp_nf_tab_f(DP p,LIST *tab,DP *rp)
1713: {
1714: DP s,t,u;
1715: MP m;
1716: DL h;
1717: int i,n;
1718:
1719: if ( !p ) {
1720: *rp = p; return;
1721: }
1722: n = p->nv;
1723: for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
1724: h = m->dl;
1725: while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
1726: i++;
1727: muldc(CO,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
1728: addd(CO,s,t,&u); s = u;
1.7 noro 1729: }
1730: *rp = s;
1.5 noro 1731: }
1732:
1.7 noro 1733: /*
1734: * setting flags
1.30 noro 1735: * call create_order_spec with vl=0 to set old type order.
1.7 noro 1736: *
1737: */
1738:
1.27 noro 1739: int create_order_spec(VL vl,Obj obj,struct order_spec **specp)
1.5 noro 1740: {
1.37 noro 1741: int i,j,n,s,row,col,ret;
1.27 noro 1742: struct order_spec *spec;
1.7 noro 1743: struct order_pair *l;
1744: NODE node,t,tn;
1745: MAT m;
1.49 noro 1746: VECT v;
1747: pointer **b,*bv;
1.7 noro 1748: int **w;
1.5 noro 1749:
1.37 noro 1750: if ( vl && obj && OID(obj) == O_LIST ) {
1751: ret = create_composite_order_spec(vl,(LIST)obj,specp);
1752: if ( show_orderspec )
1753: print_composite_order_spec(*specp);
1754: return ret;
1755: }
1.27 noro 1756:
1757: *specp = spec = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1.7 noro 1758: if ( !obj || NUM(obj) ) {
1759: spec->id = 0; spec->obj = obj;
1760: spec->ord.simple = QTOS((Q)obj);
1761: return 1;
1762: } else if ( OID(obj) == O_LIST ) {
1763: node = BDY((LIST)obj);
1.53 noro 1764: if ( !BDY(node) || NUM(BDY(node)) ) {
1765: if ( length(node) < 2 )
1766: error("create_order_spec : invalid argument");
1767: create_order_spec(0,(Obj)BDY(NEXT(node)),&spec);
1768: spec->id += 256; spec->obj = obj;
1769: spec->ispot = (BDY(node)!=0);
1.54 noro 1770: if ( spec->ispot ) {
1771: n = QTOS((Q)BDY(node));
1772: if ( n < 0 )
1773: spec->pot_nelim = -n;
1774: else
1775: spec->pot_nelim = 0;
1776: }
1.53 noro 1777: *specp = spec;
1778: return 1;
1779: }
1780:
1.7 noro 1781: for ( n = 0, t = node; t; t = NEXT(t), n++ );
1782: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
1783: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
1784: tn = BDY((LIST)BDY(t)); l[i].order = QTOS((Q)BDY(tn));
1785: tn = NEXT(tn); l[i].length = QTOS((Q)BDY(tn));
1786: s += l[i].length;
1787: }
1788: spec->id = 1; spec->obj = obj;
1789: spec->ord.block.order_pair = l;
1790: spec->ord.block.length = n; spec->nv = s;
1791: return 1;
1792: } else if ( OID(obj) == O_MAT ) {
1793: m = (MAT)obj; row = m->row; col = m->col; b = BDY(m);
1794: w = almat(row,col);
1795: for ( i = 0; i < row; i++ )
1796: for ( j = 0; j < col; j++ )
1797: w[i][j] = QTOS((Q)b[i][j]);
1798: spec->id = 2; spec->obj = obj;
1799: spec->nv = col; spec->ord.matrix.row = row;
1800: spec->ord.matrix.matrix = w;
1801: return 1;
1802: } else
1.5 noro 1803: return 0;
1804: }
1805:
1.28 noro 1806: void print_composite_order_spec(struct order_spec *spec)
1807: {
1808: int nv,n,len,i,j,k,start;
1809: struct weight_or_block *worb;
1810:
1811: nv = spec->nv;
1812: n = spec->ord.composite.length;
1813: worb = spec->ord.composite.w_or_b;
1814: for ( i = 0; i < n; i++, worb++ ) {
1815: len = worb->length;
1816: printf("[ ");
1817: switch ( worb->type ) {
1818: case IS_DENSE_WEIGHT:
1819: for ( j = 0; j < len; j++ )
1820: printf("%d ",worb->body.dense_weight[j]);
1821: for ( ; j < nv; j++ )
1822: printf("0 ");
1823: break;
1824: case IS_SPARSE_WEIGHT:
1825: for ( j = 0, k = 0; j < nv; j++ )
1826: if ( j == worb->body.sparse_weight[k].pos )
1827: printf("%d ",worb->body.sparse_weight[k++].value);
1828: else
1829: printf("0 ");
1830: break;
1831: case IS_BLOCK:
1832: start = worb->body.block.start;
1833: for ( j = 0; j < start; j++ ) printf("0 ");
1834: switch ( worb->body.block.order ) {
1835: case 0:
1836: for ( k = 0; k < len; k++, j++ ) printf("R ");
1837: break;
1838: case 1:
1839: for ( k = 0; k < len; k++, j++ ) printf("G ");
1840: break;
1841: case 2:
1842: for ( k = 0; k < len; k++, j++ ) printf("L ");
1843: break;
1844: }
1845: for ( ; j < nv; j++ ) printf("0 ");
1846: break;
1847: }
1848: printf("]\n");
1849: }
1.38 noro 1850: }
1851:
1852: struct order_spec *append_block(struct order_spec *spec,
1853: int nv,int nalg,int ord)
1854: {
1855: MAT m,mat;
1856: int i,j,row,col,n;
1857: Q **b,**wp;
1858: int **w;
1859: NODE t,s,s0;
1860: struct order_pair *l,*l0;
1861: int n0,nv0;
1862: LIST list0,list1,list;
1863: Q oq,nq;
1864: struct order_spec *r;
1865:
1866: r = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1867: switch ( spec->id ) {
1868: case 0:
1869: STOQ(spec->ord.simple,oq); STOQ(nv,nq);
1870: t = mknode(2,oq,nq); MKLIST(list0,t);
1871: STOQ(ord,oq); STOQ(nalg,nq);
1872: t = mknode(2,oq,nq); MKLIST(list1,t);
1873: t = mknode(2,list0,list1); MKLIST(list,t);
1874: l = (struct order_pair *)MALLOC_ATOMIC(2*sizeof(struct order_pair));
1875: l[0].order = spec->ord.simple; l[0].length = nv;
1876: l[1].order = ord; l[1].length = nalg;
1877: r->id = 1; r->obj = (Obj)list;
1878: r->ord.block.order_pair = l;
1879: r->ord.block.length = 2;
1880: r->nv = nv+nalg;
1881: break;
1882: case 1:
1883: if ( spec->nv != nv )
1884: error("append_block : number of variables mismatch");
1885: l0 = spec->ord.block.order_pair;
1886: n0 = spec->ord.block.length;
1887: nv0 = spec->nv;
1888: list0 = (LIST)spec->obj;
1889: n = n0+1;
1890: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
1891: for ( i = 0; i < n0; i++ )
1892: l[i] = l0[i];
1893: l[i].order = ord; l[i].length = nalg;
1894: for ( t = BDY(list0), s0 = 0; t; t = NEXT(t) ) {
1895: NEXTNODE(s0,s); BDY(s) = BDY(t);
1896: }
1897: STOQ(ord,oq); STOQ(nalg,nq);
1898: t = mknode(2,oq,nq); MKLIST(list,t);
1899: NEXTNODE(s0,s); BDY(s) = (pointer)list; NEXT(s) = 0;
1900: MKLIST(list,s0);
1901: r->id = 1; r->obj = (Obj)list;
1902: r->ord.block.order_pair = l;
1903: r->ord.block.length = n;
1904: r->nv = nv+nalg;
1905: break;
1906: case 2:
1907: if ( spec->nv != nv )
1908: error("append_block : number of variables mismatch");
1909: m = (MAT)spec->obj;
1910: row = m->row; col = m->col; b = (Q **)BDY(m);
1911: w = almat(row+nalg,col+nalg);
1912: MKMAT(mat,row+nalg,col+nalg); wp = (Q **)BDY(mat);
1913: for ( i = 0; i < row; i++ )
1914: for ( j = 0; j < col; j++ ) {
1915: w[i][j] = QTOS(b[i][j]);
1916: wp[i][j] = b[i][j];
1917: }
1918: for ( i = 0; i < nalg; i++ ) {
1919: w[i+row][i+col] = 1;
1920: wp[i+row][i+col] = ONE;
1921: }
1922: r->id = 2; r->obj = (Obj)mat;
1923: r->nv = col+nalg; r->ord.matrix.row = row+nalg;
1924: r->ord.matrix.matrix = w;
1925: break;
1926: case 3:
1927: default:
1928: /* XXX */
1929: error("append_block : not implemented yet");
1930: }
1931: return r;
1.28 noro 1932: }
1933:
1.37 noro 1934: int comp_sw(struct sparse_weight *a, struct sparse_weight *b)
1935: {
1936: if ( a->pos > b->pos ) return 1;
1937: else if ( a->pos < b->pos ) return -1;
1938: else return 0;
1939: }
1940:
1.27 noro 1941: /* order = [w_or_b, w_or_b, ... ] */
1942: /* w_or_b = w or b */
1943: /* w = [1,2,...] or [x,1,y,2,...] */
1944: /* b = [@lex,x,y,...,z] etc */
1945:
1946: int create_composite_order_spec(VL vl,LIST order,struct order_spec **specp)
1947: {
1948: NODE wb,t,p;
1949: struct order_spec *spec;
1950: VL tvl;
1.29 noro 1951: int n,i,j,k,l,start,end,len,w;
1.27 noro 1952: int *dw;
1953: struct sparse_weight *sw;
1954: struct weight_or_block *w_or_b;
1955: Obj a0;
1956: NODE a;
1.29 noro 1957: V v,sv,ev;
1958: SYMBOL sym;
1959: int *top;
1.27 noro 1960:
1961: /* l = number of vars in vl */
1962: for ( l = 0, tvl = vl; tvl; tvl = NEXT(tvl), l++ );
1963: /* n = number of primitives in order */
1964: wb = BDY(order);
1965: n = length(wb);
1966: *specp = spec = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1967: spec->id = 3;
1968: spec->obj = (Obj)order;
1969: spec->nv = l;
1970: spec->ord.composite.length = n;
1.28 noro 1971: w_or_b = spec->ord.composite.w_or_b = (struct weight_or_block *)
1.29 noro 1972: MALLOC(sizeof(struct weight_or_block)*(n+1));
1973:
1974: /* top : register the top variable in each w_or_b specification */
1975: top = (int *)ALLOCA(l*sizeof(int));
1976: for ( i = 0; i < l; i++ ) top[i] = 0;
1977:
1.28 noro 1978: for ( t = wb, i = 0; t; t = NEXT(t), i++ ) {
1.30 noro 1979: if ( !BDY(t) || OID((Obj)BDY(t)) != O_LIST )
1980: error("a list of lists must be specified for the key \"order\"");
1.28 noro 1981: a = BDY((LIST)BDY(t));
1.27 noro 1982: len = length(a);
1983: a0 = (Obj)BDY(a);
1984: if ( !a0 || OID(a0) == O_N ) {
1.28 noro 1985: /* a is a dense weight vector */
1.27 noro 1986: dw = (int *)MALLOC(sizeof(int)*len);
1.30 noro 1987: for ( j = 0, p = a; j < len; p = NEXT(p), j++ ) {
1988: if ( !INT((Q)BDY(p)) )
1989: error("a dense weight vector must be specified as a list of integers");
1.27 noro 1990: dw[j] = QTOS((Q)BDY(p));
1.30 noro 1991: }
1.27 noro 1992: w_or_b[i].type = IS_DENSE_WEIGHT;
1993: w_or_b[i].length = len;
1994: w_or_b[i].body.dense_weight = dw;
1.29 noro 1995:
1996: /* find the top */
1997: for ( k = 0; k < len && !dw[k]; k++ );
1998: if ( k < len ) top[k] = 1;
1999:
1.27 noro 2000: } else if ( OID(a0) == O_P ) {
1.28 noro 2001: /* a is a sparse weight vector */
2002: len >>= 1;
1.27 noro 2003: sw = (struct sparse_weight *)
2004: MALLOC(sizeof(struct sparse_weight)*len);
2005: for ( j = 0, p = a; j < len; j++ ) {
1.30 noro 2006: if ( !BDY(p) || OID((P)BDY(p)) != O_P )
2007: error("a sparse weight vector must be specified as [var1,weight1,...]");
1.28 noro 2008: v = VR((P)BDY(p)); p = NEXT(p);
1.27 noro 2009: for ( tvl = vl, k = 0; tvl && tvl->v != v;
2010: k++, tvl = NEXT(tvl) );
2011: if ( !tvl )
1.30 noro 2012: error("invalid variable name in a sparse weight vector");
1.27 noro 2013: sw[j].pos = k;
1.30 noro 2014: if ( !INT((Q)BDY(p)) )
2015: error("a sparse weight vector must be specified as [var1,weight1,...]");
1.28 noro 2016: sw[j].value = QTOS((Q)BDY(p)); p = NEXT(p);
1.27 noro 2017: }
1.37 noro 2018: qsort(sw,len,sizeof(struct sparse_weight),
2019: (int (*)(const void *,const void *))comp_sw);
1.27 noro 2020: w_or_b[i].type = IS_SPARSE_WEIGHT;
2021: w_or_b[i].length = len;
2022: w_or_b[i].body.sparse_weight = sw;
1.29 noro 2023:
2024: /* find the top */
2025: for ( k = 0; k < len && !sw[k].value; k++ );
2026: if ( k < len ) top[sw[k].pos] = 1;
2027: } else if ( OID(a0) == O_RANGE ) {
2028: /* [range(v1,v2),w] */
2029: sv = VR((P)(((RANGE)a0)->start));
2030: ev = VR((P)(((RANGE)a0)->end));
2031: for ( tvl = vl, start = 0; tvl && tvl->v != sv; start++, tvl = NEXT(tvl) );
2032: if ( !tvl )
2033: error("invalid range");
2034: for ( end = start; tvl && tvl->v != ev; end++, tvl = NEXT(tvl) );
2035: if ( !tvl )
2036: error("invalid range");
2037: len = end-start+1;
2038: sw = (struct sparse_weight *)
2039: MALLOC(sizeof(struct sparse_weight)*len);
2040: w = QTOS((Q)BDY(NEXT(a)));
2041: for ( tvl = vl, k = 0; k < start; k++, tvl = NEXT(tvl) );
2042: for ( j = 0 ; k <= end; k++, tvl = NEXT(tvl), j++ ) {
2043: sw[j].pos = k;
2044: sw[j].value = w;
2045: }
2046: w_or_b[i].type = IS_SPARSE_WEIGHT;
2047: w_or_b[i].length = len;
2048: w_or_b[i].body.sparse_weight = sw;
2049:
2050: /* register the top */
2051: if ( w ) top[start] = 1;
1.28 noro 2052: } else if ( OID(a0) == O_SYMBOL ) {
2053: /* a is a block */
1.29 noro 2054: sym = (SYMBOL)a0; a = NEXT(a); len--;
2055: if ( OID((Obj)BDY(a)) == O_RANGE ) {
2056: sv = VR((P)(((RANGE)BDY(a))->start));
2057: ev = VR((P)(((RANGE)BDY(a))->end));
2058: for ( tvl = vl, start = 0; tvl && tvl->v != sv; start++, tvl = NEXT(tvl) );
2059: if ( !tvl )
2060: error("invalid range");
2061: for ( end = start; tvl && tvl->v != ev; end++, tvl = NEXT(tvl) );
2062: if ( !tvl )
2063: error("invalid range");
2064: len = end-start+1;
2065: } else {
2066: for ( start = 0, tvl = vl; tvl->v != VR((P)BDY(a));
1.28 noro 2067: tvl = NEXT(tvl), start++ );
1.29 noro 2068: for ( p = NEXT(a), tvl = NEXT(tvl); p;
1.30 noro 2069: p = NEXT(p), tvl = NEXT(tvl) ) {
2070: if ( !BDY(p) || OID((P)BDY(p)) != O_P )
2071: error("a block must be specified as [ordsymbol,var1,var2,...]");
1.29 noro 2072: if ( tvl->v != VR((P)BDY(p)) ) break;
1.30 noro 2073: }
1.29 noro 2074: if ( p )
1.30 noro 2075: error("a block must be contiguous in the variable list");
1.29 noro 2076: }
1.28 noro 2077: w_or_b[i].type = IS_BLOCK;
2078: w_or_b[i].length = len;
2079: w_or_b[i].body.block.start = start;
2080: if ( !strcmp(sym->name,"@grlex") )
2081: w_or_b[i].body.block.order = 0;
2082: else if ( !strcmp(sym->name,"@glex") )
2083: w_or_b[i].body.block.order = 1;
2084: else if ( !strcmp(sym->name,"@lex") )
2085: w_or_b[i].body.block.order = 2;
2086: else
1.29 noro 2087: error("invalid ordername");
2088: /* register the tops */
2089: for ( j = 0, k = start; j < len; j++, k++ )
2090: top[k] = 1;
1.28 noro 2091: }
1.29 noro 2092: }
2093: for ( k = 0; k < l && top[k]; k++ );
2094: if ( k < l ) {
2095: /* incomplete order specification; add @grlex */
2096: w_or_b[n].type = IS_BLOCK;
2097: w_or_b[n].length = l;
2098: w_or_b[n].body.block.start = 0;
2099: w_or_b[n].body.block.order = 0;
2100: spec->ord.composite.length = n+1;
1.27 noro 2101: }
2102: }
2103:
1.35 noro 2104: /* module order spec */
2105:
2106: void create_modorder_spec(int id,LIST shift,struct modorder_spec **s)
2107: {
2108: struct modorder_spec *spec;
2109: NODE n,t;
2110: LIST list;
2111: int *ds;
2112: int i,l;
2113: Q q;
2114:
2115: *s = spec = (struct modorder_spec *)MALLOC(sizeof(struct modorder_spec));
2116: spec->id = id;
2117: if ( shift ) {
2118: n = BDY(shift);
2119: spec->len = l = length(n);
2120: spec->degree_shift = ds = (int *)MALLOC_ATOMIC(l*sizeof(int));
2121: for ( t = n, i = 0; t; t = NEXT(t), i++ )
2122: ds[i] = QTOS((Q)BDY(t));
2123: } else {
2124: spec->len = 0;
2125: spec->degree_shift = 0;
2126: }
2127: STOQ(id,q);
2128: n = mknode(2,q,shift);
2129: MKLIST(list,n);
2130: spec->obj = (Obj)list;
2131: }
2132:
1.7 noro 2133: /*
2134: * converters
2135: *
2136: */
2137:
1.20 noro 2138: void dp_homo(DP p,DP *rp)
1.5 noro 2139: {
1.7 noro 2140: MP m,mr,mr0;
2141: int i,n,nv,td;
2142: DL dl,dlh;
1.5 noro 2143:
1.7 noro 2144: if ( !p )
2145: *rp = 0;
2146: else {
2147: n = p->nv; nv = n + 1;
2148: m = BDY(p); td = sugard(m);
2149: for ( mr0 = 0; m; m = NEXT(m) ) {
2150: NEXTMP(mr0,mr); mr->c = m->c;
2151: dl = m->dl;
2152: mr->dl = dlh = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
2153: dlh->td = td;
2154: for ( i = 0; i < n; i++ )
2155: dlh->d[i] = dl->d[i];
2156: dlh->d[n] = td - dl->td;
2157: }
2158: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.5 noro 2159: }
2160: }
2161:
1.20 noro 2162: void dp_dehomo(DP p,DP *rp)
1.5 noro 2163: {
1.7 noro 2164: MP m,mr,mr0;
2165: int i,n,nv;
2166: DL dl,dlh;
1.5 noro 2167:
1.7 noro 2168: if ( !p )
2169: *rp = 0;
2170: else {
2171: n = p->nv; nv = n - 1;
2172: m = BDY(p);
2173: for ( mr0 = 0; m; m = NEXT(m) ) {
2174: NEXTMP(mr0,mr); mr->c = m->c;
2175: dlh = m->dl;
2176: mr->dl = dl = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
2177: dl->td = dlh->td - dlh->d[nv];
2178: for ( i = 0; i < nv; i++ )
2179: dl->d[i] = dlh->d[i];
2180: }
2181: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
2182: }
1.5 noro 2183: }
2184:
1.20 noro 2185: void dp_mod(DP p,int mod,NODE subst,DP *rp)
1.5 noro 2186: {
1.7 noro 2187: MP m,mr,mr0;
2188: P t,s,s1;
2189: V v;
2190: NODE tn;
1.5 noro 2191:
1.7 noro 2192: if ( !p )
2193: *rp = 0;
2194: else {
2195: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
2196: for ( tn = subst, s = m->c; tn; tn = NEXT(tn) ) {
2197: v = VR((P)BDY(tn)); tn = NEXT(tn);
2198: substp(CO,s,v,(P)BDY(tn),&s1); s = s1;
2199: }
2200: ptomp(mod,s,&t);
2201: if ( t ) {
2202: NEXTMP(mr0,mr); mr->c = t; mr->dl = m->dl;
2203: }
2204: }
2205: if ( mr0 ) {
2206: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
2207: } else
2208: *rp = 0;
2209: }
1.5 noro 2210: }
2211:
1.20 noro 2212: void dp_rat(DP p,DP *rp)
1.5 noro 2213: {
1.7 noro 2214: MP m,mr,mr0;
1.5 noro 2215:
1.7 noro 2216: if ( !p )
2217: *rp = 0;
2218: else {
2219: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
2220: NEXTMP(mr0,mr); mptop(m->c,&mr->c); mr->dl = m->dl;
1.5 noro 2221: }
1.7 noro 2222: if ( mr0 ) {
2223: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
2224: } else
2225: *rp = 0;
1.5 noro 2226: }
2227: }
2228:
2229:
1.27 noro 2230: void homogenize_order(struct order_spec *old,int n,struct order_spec **newp)
1.5 noro 2231: {
1.7 noro 2232: struct order_pair *l;
2233: int length,nv,row,i,j;
2234: int **newm,**oldm;
1.27 noro 2235: struct order_spec *new;
1.31 noro 2236: int onv,nnv,nlen,olen,owlen;
2237: struct weight_or_block *owb,*nwb;
1.5 noro 2238:
1.27 noro 2239: *newp = new = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1.7 noro 2240: switch ( old->id ) {
2241: case 0:
2242: switch ( old->ord.simple ) {
2243: case 0:
2244: new->id = 0; new->ord.simple = 0; break;
2245: case 1:
2246: l = (struct order_pair *)
2247: MALLOC_ATOMIC(2*sizeof(struct order_pair));
2248: l[0].length = n; l[0].order = 1;
2249: l[1].length = 1; l[1].order = 2;
2250: new->id = 1;
2251: new->ord.block.order_pair = l;
2252: new->ord.block.length = 2; new->nv = n+1;
2253: break;
2254: case 2:
2255: new->id = 0; new->ord.simple = 1; break;
2256: case 3: case 4: case 5:
2257: new->id = 0; new->ord.simple = old->ord.simple+3;
2258: dp_nelim = n-1; break;
2259: case 6: case 7: case 8: case 9:
2260: new->id = 0; new->ord.simple = old->ord.simple; break;
2261: default:
2262: error("homogenize_order : invalid input");
2263: }
2264: break;
1.50 noro 2265: case 1: case 257:
1.7 noro 2266: length = old->ord.block.length;
2267: l = (struct order_pair *)
2268: MALLOC_ATOMIC((length+1)*sizeof(struct order_pair));
2269: bcopy((char *)old->ord.block.order_pair,(char *)l,length*sizeof(struct order_pair));
2270: l[length].order = 2; l[length].length = 1;
1.50 noro 2271: new->id = old->id; new->nv = n+1;
1.7 noro 2272: new->ord.block.order_pair = l;
2273: new->ord.block.length = length+1;
1.51 noro 2274: new->ispot = old->ispot;
1.7 noro 2275: break;
1.50 noro 2276: case 2: case 258:
1.7 noro 2277: nv = old->nv; row = old->ord.matrix.row;
2278: oldm = old->ord.matrix.matrix; newm = almat(row+1,nv+1);
2279: for ( i = 0; i <= nv; i++ )
2280: newm[0][i] = 1;
2281: for ( i = 0; i < row; i++ ) {
2282: for ( j = 0; j < nv; j++ )
2283: newm[i+1][j] = oldm[i][j];
2284: newm[i+1][j] = 0;
2285: }
1.50 noro 2286: new->id = old->id; new->nv = nv+1;
1.7 noro 2287: new->ord.matrix.row = row+1; new->ord.matrix.matrix = newm;
1.51 noro 2288: new->ispot = old->ispot;
1.31 noro 2289: break;
1.50 noro 2290: case 3: case 259:
1.31 noro 2291: onv = old->nv;
2292: nnv = onv+1;
2293: olen = old->ord.composite.length;
2294: nlen = olen+1;
2295: owb = old->ord.composite.w_or_b;
2296: nwb = (struct weight_or_block *)
2297: MALLOC(nlen*sizeof(struct weight_or_block));
2298: for ( i = 0; i < olen; i++ ) {
2299: nwb[i].type = owb[i].type;
2300: switch ( owb[i].type ) {
2301: case IS_DENSE_WEIGHT:
2302: owlen = owb[i].length;
2303: nwb[i].length = owlen+1;
2304: nwb[i].body.dense_weight = (int *)MALLOC((owlen+1)*sizeof(int));
2305: for ( j = 0; j < owlen; j++ )
2306: nwb[i].body.dense_weight[j] = owb[i].body.dense_weight[j];
2307: nwb[i].body.dense_weight[owlen] = 0;
2308: break;
2309: case IS_SPARSE_WEIGHT:
2310: nwb[i].length = owb[i].length;
2311: nwb[i].body.sparse_weight = owb[i].body.sparse_weight;
2312: break;
2313: case IS_BLOCK:
2314: nwb[i].length = owb[i].length;
2315: nwb[i].body.block = owb[i].body.block;
2316: break;
2317: }
2318: }
2319: nwb[i].type = IS_SPARSE_WEIGHT;
2320: nwb[i].body.sparse_weight =
2321: (struct sparse_weight *)MALLOC(sizeof(struct sparse_weight));
2322: nwb[i].body.sparse_weight[0].pos = onv;
2323: nwb[i].body.sparse_weight[0].value = 1;
1.50 noro 2324: new->id = old->id;
1.31 noro 2325: new->nv = nnv;
2326: new->ord.composite.length = nlen;
2327: new->ord.composite.w_or_b = nwb;
1.51 noro 2328: new->ispot = old->ispot;
1.31 noro 2329: print_composite_order_spec(new);
1.7 noro 2330: break;
1.50 noro 2331: case 256: /* simple module order */
2332: switch ( old->ord.simple ) {
2333: case 0:
2334: new->id = 256; new->ord.simple = 0; break;
2335: case 1:
2336: l = (struct order_pair *)
2337: MALLOC_ATOMIC(2*sizeof(struct order_pair));
2338: l[0].length = n; l[0].order = old->ord.simple;
2339: l[1].length = 1; l[1].order = 2;
2340: new->id = 257;
2341: new->ord.block.order_pair = l;
2342: new->ord.block.length = 2; new->nv = n+1;
2343: break;
2344: case 2:
2345: new->id = 256; new->ord.simple = 1; break;
2346: default:
2347: error("homogenize_order : invalid input");
2348: }
1.51 noro 2349: new->ispot = old->ispot;
1.50 noro 2350: break;
1.7 noro 2351: default:
2352: error("homogenize_order : invalid input");
1.5 noro 2353: }
1.7 noro 2354: }
2355:
1.20 noro 2356: void qltozl(Q *w,int n,Q *dvr)
1.7 noro 2357: {
2358: N nm,dn;
2359: N g,l1,l2,l3;
2360: Q c,d;
2361: int i;
2362: struct oVECT v;
1.5 noro 2363:
2364: for ( i = 0; i < n; i++ )
1.7 noro 2365: if ( w[i] && !INT(w[i]) )
2366: break;
2367: if ( i == n ) {
2368: v.id = O_VECT; v.len = n; v.body = (pointer *)w;
2369: igcdv(&v,dvr); return;
2370: }
1.56 noro 2371: for ( i = 0; !w[i]; i++ );
2372: c = w[i]; nm = NM(c); dn = INT(c) ? ONEN : DN(c);
2373: for ( i++; i < n; i++ ) {
2374: c = w[i];
2375: if ( !c ) continue;
2376: l1 = INT(c) ? ONEN : DN(c);
1.7 noro 2377: gcdn(nm,NM(c),&g); nm = g;
2378: gcdn(dn,l1,&l2); muln(dn,l1,&l3); divsn(l3,l2,&dn);
1.5 noro 2379: }
1.7 noro 2380: if ( UNIN(dn) )
2381: NTOQ(nm,1,d);
2382: else
2383: NDTOQ(nm,dn,1,d);
2384: *dvr = d;
2385: }
1.5 noro 2386:
1.20 noro 2387: int comp_nm(Q *a,Q *b)
1.7 noro 2388: {
2389: return cmpn((*a)?NM(*a):0,(*b)?NM(*b):0);
2390: }
2391:
1.20 noro 2392: void sortbynm(Q *w,int n)
1.7 noro 2393: {
2394: qsort(w,n,sizeof(Q),(int (*)(const void *,const void *))comp_nm);
2395: }
1.5 noro 2396:
2397:
1.7 noro 2398: /*
2399: * simple operations
2400: *
2401: */
1.5 noro 2402:
1.20 noro 2403: int dp_redble(DP p1,DP p2)
1.7 noro 2404: {
2405: int i,n;
2406: DL d1,d2;
1.5 noro 2407:
1.7 noro 2408: d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
2409: if ( d1->td < d2->td )
2410: return 0;
2411: else {
2412: for ( i = 0, n = p1->nv; i < n; i++ )
2413: if ( d1->d[i] < d2->d[i] )
2414: return 0;
2415: return 1;
1.5 noro 2416: }
2417: }
2418:
1.20 noro 2419: void dp_subd(DP p1,DP p2,DP *rp)
1.5 noro 2420: {
1.7 noro 2421: int i,n;
1.5 noro 2422: DL d1,d2,d;
2423: MP m;
1.7 noro 2424: DP s;
1.5 noro 2425:
2426: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
1.7 noro 2427: NEWDL(d,n); d->td = d1->td - d2->td;
1.5 noro 2428: for ( i = 0; i < n; i++ )
1.7 noro 2429: d->d[i] = d1->d[i]-d2->d[i];
2430: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
2431: MKDP(n,m,s); s->sugar = d->td;
2432: *rp = s;
2433: }
2434:
1.20 noro 2435: void dltod(DL d,int n,DP *rp)
1.7 noro 2436: {
2437: MP m;
2438: DP s;
2439:
2440: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
2441: MKDP(n,m,s); s->sugar = d->td;
2442: *rp = s;
1.5 noro 2443: }
2444:
1.20 noro 2445: void dp_hm(DP p,DP *rp)
1.5 noro 2446: {
2447: MP m,mr;
2448:
2449: if ( !p )
2450: *rp = 0;
2451: else {
2452: m = BDY(p);
2453: NEWMP(mr); mr->dl = m->dl; mr->c = m->c; NEXT(mr) = 0;
2454: MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td; /* XXX */
2455: }
2456: }
2457:
1.35 noro 2458: void dp_ht(DP p,DP *rp)
2459: {
2460: MP m,mr;
2461:
2462: if ( !p )
2463: *rp = 0;
2464: else {
2465: m = BDY(p);
2466: NEWMP(mr); mr->dl = m->dl; mr->c = (P)ONE; NEXT(mr) = 0;
2467: MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td; /* XXX */
2468: }
2469: }
2470:
1.20 noro 2471: void dp_rest(DP p,DP *rp)
1.5 noro 2472: {
2473: MP m;
2474:
2475: m = BDY(p);
2476: if ( !NEXT(m) )
2477: *rp = 0;
2478: else {
2479: MKDP(p->nv,NEXT(m),*rp);
2480: if ( *rp )
2481: (*rp)->sugar = p->sugar;
2482: }
2483: }
2484:
1.20 noro 2485: DL lcm_of_DL(int nv,DL dl1,DL dl2,DL dl)
1.5 noro 2486: {
1.21 noro 2487: register int i, *d1, *d2, *d, td;
1.5 noro 2488:
2489: if ( !dl ) NEWDL(dl,nv);
2490: d = dl->d, d1 = dl1->d, d2 = dl2->d;
1.21 noro 2491: for ( td = 0, i = 0; i < nv; d1++, d2++, d++, i++ ) {
2492: *d = *d1 > *d2 ? *d1 : *d2;
2493: td += MUL_WEIGHT(*d,i);
2494: }
1.5 noro 2495: dl->td = td;
2496: return dl;
2497: }
2498:
1.20 noro 2499: int dl_equal(int nv,DL dl1,DL dl2)
1.5 noro 2500: {
2501: register int *d1, *d2, n;
2502:
2503: if ( dl1->td != dl2->td ) return 0;
2504: for ( d1 = dl1->d, d2 = dl2->d, n = nv; --n >= 0; d1++, d2++ )
2505: if ( *d1 != *d2 ) return 0;
2506: return 1;
2507: }
2508:
1.20 noro 2509: int dp_nt(DP p)
1.5 noro 2510: {
2511: int i;
2512: MP m;
2513:
2514: if ( !p )
2515: return 0;
2516: else {
2517: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
2518: return i;
2519: }
2520: }
2521:
1.20 noro 2522: int dp_homogeneous(DP p)
1.15 noro 2523: {
2524: MP m;
2525: int d;
2526:
2527: if ( !p )
2528: return 1;
2529: else {
2530: m = BDY(p);
2531: d = m->dl->td;
2532: m = NEXT(m);
2533: for ( ; m; m = NEXT(m) ) {
2534: if ( m->dl->td != d )
2535: return 0;
2536: }
2537: return 1;
2538: }
1.16 noro 2539: }
2540:
1.20 noro 2541: void _print_mp(int nv,MP m)
1.16 noro 2542: {
2543: int i;
2544:
1.17 noro 2545: if ( !m )
1.16 noro 2546: return;
2547: for ( ; m; m = NEXT(m) ) {
2548: fprintf(stderr,"%d<",ITOS(C(m)));
2549: for ( i = 0; i < nv; i++ ) {
2550: fprintf(stderr,"%d",m->dl->d[i]);
2551: if ( i != nv-1 )
2552: fprintf(stderr," ");
2553: }
2554: fprintf(stderr,">",C(m));
2555: }
2556: fprintf(stderr,"\n");
1.60 ! fujimoto 2557: #if defined(__MINGW32__) || defined(__MINGW64__)
! 2558: fflush(stderr);
! 2559: #endif
1.15 noro 2560: }
1.26 noro 2561:
2562: static int cmp_mp_nvar;
2563:
2564: int comp_mp(MP *a,MP *b)
2565: {
2566: return -(*cmpdl)(cmp_mp_nvar,(*a)->dl,(*b)->dl);
2567: }
2568:
2569: void dp_sort(DP p,DP *rp)
2570: {
2571: MP t,mp,mp0;
2572: int i,n;
2573: DP r;
2574: MP *w;
2575:
2576: if ( !p ) {
2577: *rp = 0;
2578: return;
2579: }
2580: for ( t = BDY(p), n = 0; t; t = NEXT(t), n++ );
2581: w = (MP *)ALLOCA(n*sizeof(MP));
2582: for ( t = BDY(p), i = 0; i < n; t = NEXT(t), i++ )
2583: w[i] = t;
2584: cmp_mp_nvar = NV(p);
2585: qsort(w,n,sizeof(MP),(int (*)(const void *,const void *))comp_mp);
2586: mp0 = 0;
2587: for ( i = n-1; i >= 0; i-- ) {
2588: NEWMP(mp); mp->dl = w[i]->dl; C(mp) = C(w[i]);
2589: NEXT(mp) = mp0; mp0 = mp;
2590: }
2591: MKDP(p->nv,mp0,r);
2592: r->sugar = p->sugar;
2593: *rp = r;
2594: }
2595:
1.32 noro 2596: DP extract_initial_term_from_dp(DP p,int *weight,int n);
2597: LIST extract_initial_term(LIST f,int *weight,int n);
2598:
2599: DP extract_initial_term_from_dp(DP p,int *weight,int n)
2600: {
1.34 noro 2601: int w,t,i,top;
1.32 noro 2602: MP m,r0,r;
2603: DP dp;
2604:
2605: if ( !p ) return 0;
1.34 noro 2606: top = 1;
1.32 noro 2607: for ( m = BDY(p); m; m = NEXT(m) ) {
2608: for ( i = 0, t = 0; i < n; i++ )
2609: t += weight[i]*m->dl->d[i];
1.34 noro 2610: if ( top || t > w ) {
1.32 noro 2611: r0 = 0;
2612: w = t;
1.34 noro 2613: top = 0;
1.32 noro 2614: }
2615: if ( t == w ) {
2616: NEXTMP(r0,r);
2617: r->dl = m->dl;
2618: r->c = m->c;
2619: }
2620: }
2621: NEXT(r) = 0;
2622: MKDP(p->nv,r0,dp);
2623: return dp;
2624: }
2625:
2626: LIST extract_initial_term(LIST f,int *weight,int n)
2627: {
2628: NODE nd,r0,r;
2629: Obj p;
2630: LIST l;
2631:
2632: nd = BDY(f);
2633: for ( r0 = 0; nd; nd = NEXT(nd) ) {
2634: NEXTNODE(r0,r);
2635: p = (Obj)BDY(nd);
2636: BDY(r) = (pointer)extract_initial_term_from_dp((DP)p,weight,n);
2637: }
2638: if ( r0 ) NEXT(r) = 0;
2639: MKLIST(l,r0);
2640: return l;
2641: }
2642:
2643: LIST dp_initial_term(LIST f,struct order_spec *ord)
2644: {
2645: int n,l,i;
2646: struct weight_or_block *worb;
2647: int *weight;
2648:
2649: switch ( ord->id ) {
2650: case 2: /* matrix order */
2651: /* extract the first row */
2652: n = ord->nv;
2653: weight = ord->ord.matrix.matrix[0];
2654: return extract_initial_term(f,weight,n);
2655: case 3: /* composite order */
2656: /* the first w_or_b */
2657: worb = ord->ord.composite.w_or_b;
2658: switch ( worb->type ) {
2659: case IS_DENSE_WEIGHT:
2660: n = worb->length;
2661: weight = worb->body.dense_weight;
2662: return extract_initial_term(f,weight,n);
2663: case IS_SPARSE_WEIGHT:
2664: n = ord->nv;
2665: weight = (int *)ALLOCA(n*sizeof(int));
1.33 noro 2666: for ( i = 0; i < n; i++ ) weight[i] = 0;
1.32 noro 2667: l = worb->length;
2668: for ( i = 0; i < l; i++ )
2669: weight[worb->body.sparse_weight[i].pos]
2670: = worb->body.sparse_weight[i].value;
2671: return extract_initial_term(f,weight,n);
2672: default:
2673: error("dp_initial_term : unsupported order");
2674: }
2675: default:
2676: error("dp_initial_term : unsupported order");
2677: }
2678: }
2679:
2680: int highest_order_dp(DP p,int *weight,int n);
2681: LIST highest_order(LIST f,int *weight,int n);
2682:
2683: int highest_order_dp(DP p,int *weight,int n)
2684: {
1.34 noro 2685: int w,t,i,top;
1.32 noro 2686: MP m;
2687:
2688: if ( !p ) return -1;
1.34 noro 2689: top = 1;
1.32 noro 2690: for ( m = BDY(p); m; m = NEXT(m) ) {
2691: for ( i = 0, t = 0; i < n; i++ )
2692: t += weight[i]*m->dl->d[i];
1.34 noro 2693: if ( top || t > w ) {
1.32 noro 2694: w = t;
1.34 noro 2695: top = 0;
2696: }
1.32 noro 2697: }
2698: return w;
2699: }
2700:
2701: LIST highest_order(LIST f,int *weight,int n)
2702: {
2703: int h;
2704: NODE nd,r0,r;
2705: Obj p;
2706: LIST l;
2707: Q q;
2708:
2709: nd = BDY(f);
2710: for ( r0 = 0; nd; nd = NEXT(nd) ) {
2711: NEXTNODE(r0,r);
2712: p = (Obj)BDY(nd);
2713: h = highest_order_dp((DP)p,weight,n);
2714: STOQ(h,q);
2715: BDY(r) = (pointer)q;
2716: }
2717: if ( r0 ) NEXT(r) = 0;
2718: MKLIST(l,r0);
2719: return l;
2720: }
2721:
2722: LIST dp_order(LIST f,struct order_spec *ord)
2723: {
2724: int n,l,i;
2725: struct weight_or_block *worb;
2726: int *weight;
2727:
2728: switch ( ord->id ) {
2729: case 2: /* matrix order */
2730: /* extract the first row */
2731: n = ord->nv;
2732: weight = ord->ord.matrix.matrix[0];
2733: return highest_order(f,weight,n);
2734: case 3: /* composite order */
2735: /* the first w_or_b */
2736: worb = ord->ord.composite.w_or_b;
2737: switch ( worb->type ) {
2738: case IS_DENSE_WEIGHT:
2739: n = worb->length;
2740: weight = worb->body.dense_weight;
2741: return highest_order(f,weight,n);
2742: case IS_SPARSE_WEIGHT:
2743: n = ord->nv;
2744: weight = (int *)ALLOCA(n*sizeof(int));
1.33 noro 2745: for ( i = 0; i < n; i++ ) weight[i] = 0;
1.32 noro 2746: l = worb->length;
2747: for ( i = 0; i < l; i++ )
2748: weight[worb->body.sparse_weight[i].pos]
2749: = worb->body.sparse_weight[i].value;
2750: return highest_order(f,weight,n);
2751: default:
2752: error("dp_initial_term : unsupported order");
2753: }
2754: default:
2755: error("dp_initial_term : unsupported order");
1.35 noro 2756: }
2757: }
2758:
2759: int dpv_ht(DPV p,DP *h)
2760: {
2761: int len,max,maxi,i,t;
2762: DP *e;
2763: MP m,mr;
2764:
2765: len = p->len;
2766: e = p->body;
2767: max = -1;
2768: maxi = -1;
2769: for ( i = 0; i < len; i++ )
2770: if ( e[i] && (t = BDY(e[i])->dl->td) > max ) {
2771: max = t;
2772: maxi = i;
2773: }
2774: if ( max < 0 ) {
2775: *h = 0;
2776: return -1;
2777: } else {
2778: m = BDY(e[maxi]);
2779: NEWMP(mr); mr->dl = m->dl; mr->c = (P)ONE; NEXT(mr) = 0;
2780: MKDP(e[maxi]->nv,mr,*h); (*h)->sugar = mr->dl->td; /* XXX */
2781: return maxi;
1.32 noro 2782: }
2783: }
1.42 noro 2784:
2785: /* return 1 if 0 <_w1 v && v <_w2 0 */
2786:
2787: int in_c12(int n,int *v,int row1,int **w1,int row2, int **w2)
2788: {
2789: int t1,t2;
2790:
2791: t1 = compare_zero(n,v,row1,w1);
2792: t2 = compare_zero(n,v,row2,w2);
2793: if ( t1 > 0 && t2 < 0 ) return 1;
2794: else return 0;
2795: }
2796:
2797: /* 0 < u => 1, 0 > u => -1 */
2798:
2799: int compare_zero(int n,int *u,int row,int **w)
2800: {
2801: int i,j,t;
2802: int *wi;
2803:
2804: for ( i = 0; i < row; i++ ) {
2805: wi = w[i];
2806: for ( j = 0, t = 0; j < n; j++ ) t += u[j]*wi[j];
2807: if ( t > 0 ) return 1;
2808: else if ( t < 0 ) return -1;
2809: }
2810: return 0;
2811: }
2812:
2813: /* functions for generic groebner walk */
2814: /* u=0 means u=-infty */
2815:
2816: int compare_facet_preorder(int n,int *u,int *v,
2817: int row1,int **w1,int row2,int **w2)
2818: {
2819: int i,j,s,t,tu,tv;
2820: int *w2i,*uv;
2821:
2822: if ( !u ) return 1;
2823: uv = W_ALLOC(n);
2824: for ( i = 0; i < row2; i++ ) {
2825: w2i = w2[i];
2826: for ( j = 0, tu = tv = 0; j < n; j++ )
2827: if ( s = w2i[j] ) {
2828: tu += s*u[j]; tv += s*v[j];
2829: }
2830: for ( j = 0; j < n; j++ ) uv[j] = u[j]*tv-v[j]*tu;
2831: t = compare_zero(n,uv,row1,w1);
2832: if ( t > 0 ) return 1;
2833: else if ( t < 0 ) return 0;
2834: }
2835: return 1;
2836: }
2837:
1.48 noro 2838: Q inner_product_with_small_vector(VECT w,int *v)
2839: {
2840: int n,i;
2841: Q q,s,t,u;
2842:
2843: n = w->len;
2844: s = 0;
2845: for ( i = 0; i < n; i++ ) {
2846: STOQ(v[i],q); mulq((Q)w->body[i],q,&t); addq(t,s,&u); s = u;
2847: }
2848: return s;
2849: }
2850:
2851: Q compute_last_t(NODE g,NODE gh,Q t,VECT w1,VECT w2,NODE *homo,VECT *wp)
2852: {
2853: int n,i;
2854: int *wt;
2855: Q last,d1,d2,dn,nm,s,t1;
2856: VECT wd,wt1,wt2,w;
2857: NODE tg,tgh;
2858: MP f;
2859: int *h;
2860: NODE r0,r;
2861: MP m0,m;
2862: DP d;
2863:
2864: n = w1->len;
2865: wt = W_ALLOC(n);
2866: last = ONE;
2867: /* t1 = 1-t */
2868: for ( tg = g, tgh = gh; tg; tg = NEXT(tg), tgh = NEXT(tgh ) ) {
2869: f = BDY((DP)BDY(tg));
2870: h = BDY((DP)BDY(tgh))->dl->d;
2871: for ( ; f; f = NEXT(f) ) {
2872: for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
2873: for ( i = 0; i < n && !wt[i]; i++ );
2874: if ( i == n ) continue;
2875: d1 = inner_product_with_small_vector(w1,wt);
2876: d2 = inner_product_with_small_vector(w2,wt);
2877: nm = d1; subq(d1,d2,&dn);
2878: /* if d1=d2 then nothing happens */
2879: if ( !dn ) continue;
2880: /* s satisfies ds = 0*/
2881: divq(nm,dn,&s);
2882:
2883: if ( cmpq(s,t) > 0 && cmpq(s,last) < 0 )
2884: last = s;
2885: else if ( !cmpq(s,t) ) {
2886: if ( cmpq(d2,0) < 0 ) {
2887: last = t;
2888: break;
2889: }
2890: }
2891: }
2892: }
2893: if ( !last ) {
2894: dn = ONE; nm = 0;
2895: } else {
2896: NTOQ(NM(last),1,nm);
2897: if ( INT(last) ) dn = ONE;
2898: else {
2899: NTOQ(DN(last),1,dn);
2900: }
2901: }
2902: /* (1-n/d)*w1+n/d*w2 -> w=(d-n)*w1+n*w2 */
2903: subq(dn,nm,&t1); mulvect(CO,(Obj)w1,(Obj)t1,(Obj *)&wt1);
2904: mulvect(CO,(Obj)w2,(Obj)nm,(Obj *)&wt2); addvect(CO,wt1,wt2,&w);
2905:
2906: r0 = 0;
2907: for ( tg = g, tgh = gh; tg; tg = NEXT(tg), tgh = NEXT(tgh ) ) {
2908: f = BDY((DP)BDY(tg));
2909: h = BDY((DP)BDY(tgh))->dl->d;
2910: for ( m0 = 0; f; f = NEXT(f) ) {
2911: for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
2912: for ( i = 0; i < n && !wt[i]; i++ );
2913: if ( !inner_product_with_small_vector(w,wt) ) {
2914: NEXTMP(m0,m); m->c = f->c; m->dl = f->dl;
2915: }
2916: }
2917: NEXT(m) = 0;
2918: MKDP(((DP)BDY(tg))->nv,m0,d); d->sugar = ((DP)BDY(tg))->sugar;
2919: NEXTNODE(r0,r); BDY(r) = (pointer)d;
2920: }
2921: NEXT(r) = 0;
2922: *homo = r0;
2923: *wp = w;
2924: return last;
2925: }
2926:
1.42 noro 2927: /* return 0 if last_w = infty */
2928:
2929: NODE compute_last_w(NODE g,NODE gh,int n,int **w,
2930: int row1,int **w1,int row2,int **w2)
2931: {
2932: DP d;
2933: MP f,m0,m;
2934: int *wt,*v,*h;
2935: NODE t,s,n0,tn,n1,r0,r;
2936: int i;
2937:
2938: wt = W_ALLOC(n);
2939: n0 = 0;
2940: for ( t = g, s = gh; t; t = NEXT(t), s = NEXT(s) ) {
2941: f = BDY((DP)BDY(t));
2942: h = BDY((DP)BDY(s))->dl->d;
2943: for ( ; f; f = NEXT(f) ) {
2944: for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
2945: for ( i = 0; i < n && !wt[i]; i++ );
2946: if ( i == n ) continue;
2947:
2948: if ( in_c12(n,wt,row1,w1,row2,w2) &&
2949: compare_facet_preorder(n,*w,wt,row1,w1,row2,w2) ) {
2950: v = (int *)MALLOC_ATOMIC(n*sizeof(int));
2951: for ( i = 0; i < n; i++ ) v[i] = wt[i];
2952: MKNODE(n1,v,n0); n0 = n1;
2953: }
2954: }
2955: }
2956: if ( !n0 ) return 0;
2957: for ( t = n0; t; t = NEXT(t) ) {
2958: v = (int *)BDY(t);
2959: for ( s = n0; s; s = NEXT(s) )
2960: if ( !compare_facet_preorder(n,v,(int *)BDY(s),row1,w1,row2,w2) )
2961: break;
2962: if ( !s ) {
2963: *w = v;
2964: break;
2965: }
2966: }
2967: if ( !t )
2968: error("compute_last_w : cannot happen");
2969: r0 = 0;
2970: for ( t = g, s = gh; t; t = NEXT(t), s = NEXT(s) ) {
2971: f = BDY((DP)BDY(t));
2972: h = BDY((DP)BDY(s))->dl->d;
2973: for ( m0 = 0; f; f = NEXT(f) ) {
2974: for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
2975: for ( i = 0; i < n && !wt[i]; i++ );
2976: if ( i == n ||
2977: (compare_facet_preorder(n,wt,*w,row1,w1,row2,w2)
2978: && compare_facet_preorder(n,*w,wt,row1,w1,row2,w2)) ) {
2979: NEXTMP(m0,m); m->c = f->c; m->dl = f->dl;
2980: }
2981: }
1.43 noro 2982: NEXT(m) = 0;
1.42 noro 2983: MKDP(((DP)BDY(t))->nv,m0,d); d->sugar = ((DP)BDY(t))->sugar;
2984: NEXTNODE(r0,r); BDY(r) = (pointer)d;
2985: }
2986: NEXT(r) = 0;
2987: return r0;
2988: }
1.44 noro 2989:
2990: /* compute a sufficient set of d(f)=u-v */
2991:
2992: NODE compute_essential_df(DP *g,DP *gh,int ng)
2993: {
1.45 noro 2994: int nv,i,j,k,t,lj;
2995: NODE r,r1,ri,rt,r0;
2996: MP m;
2997: MP *mj;
2998: DL di,hj,dl,dlt;
2999: int *d,*dt;
3000: LIST l;
1.44 noro 3001: Q q;
1.45 noro 3002:
3003: nv = g[0]->nv;
3004: r = 0;
3005: for ( j = 0; j < ng; j++ ) {
3006: for ( m = BDY(g[j]), lj = 0; m; m = NEXT(m), lj++ );
3007: mj = (MP *)ALLOCA(lj*sizeof(MP));
3008: for ( m = BDY(g[j]), k = 0; m; m = NEXT(m), k++ )
3009: mj[k] = m;
3010: for ( i = 0; i < lj; i++ ) {
3011: for ( di = mj[i]->dl, k = i+1; k < lj; k++ )
3012: if ( _dl_redble(di,mj[k]->dl,nv) ) break;
3013: if ( k < lj ) mj[i] = 0;
3014: }
3015: hj = BDY(gh[j])->dl;
3016: _NEWDL(dl,nv); d = dl->d;
3017: r0 = r;
3018: for ( i = 0; i < lj; i++ ) {
3019: if ( mj[i] && !dl_equal(nv,di=mj[i]->dl,hj) ) {
3020: for ( k = 0, t = 0; k < nv; k++ ) {
3021: d[k] = hj->d[k]-di->d[k];
3022: t += d[k];
3023: }
3024: dl->td = t;
3025: #if 1
3026: for ( rt = r0; rt; rt = NEXT(rt) ) {
3027: dlt = (DL)BDY(rt);
3028: if ( dlt->td != dl->td ) continue;
3029: for ( dt = dlt->d, k = 0; k < nv; k++ )
3030: if ( d[k] != dt[k] ) break;
3031: if ( k == nv ) break;
3032: }
3033: #else
3034: rt = 0;
3035: #endif
3036: if ( !rt ) {
3037: MKNODE(r1,dl,r); r = r1;
3038: _NEWDL(dl,nv); d = dl->d;
3039: }
1.44 noro 3040: }
3041: }
3042: }
1.45 noro 3043: for ( rt = r; rt; rt = NEXT(rt) ) {
3044: dl = (DL)BDY(rt); d = dl->d;
3045: ri = 0;
3046: for ( k = nv-1; k >= 0; k-- ) {
3047: STOQ(d[k],q);
3048: MKNODE(r1,q,ri); ri = r1;
1.44 noro 3049: }
1.45 noro 3050: MKNODE(r1,0,ri); MKLIST(l,r1);
3051: BDY(rt) = (pointer)l;
1.44 noro 3052: }
3053: return r;
3054: }
1.57 noro 3055:
3056: int comp_bits_divisible(int *a,int *b,int n)
3057: {
3058: int bpi,i,wi,bi;
3059:
3060: bpi = (sizeof(int)/sizeof(char))*8;
3061: for ( i = 0; i < n; i++ ) {
3062: wi = i/bpi; bi = i%bpi;
3063: if ( !(a[wi]&(1<<bi)) && (b[wi]&(1<<bi)) ) return 0;
3064: }
3065: return 1;
3066: }
3067:
3068: int comp_bits_lex(int *a,int *b,int n)
3069: {
3070: int bpi,i,wi,ba,bb,bi;
3071:
3072: bpi = (sizeof(int)/sizeof(char))*8;
3073: for ( i = 0; i < n; i++ ) {
3074: wi = i/bpi; bi = i%bpi;
3075: ba = (a[wi]&(1<<bi))?1:0;
3076: bb = (b[wi]&(1<<bi))?1:0;
3077: if ( ba > bb ) return 1;
3078: else if ( ba < bb ) return -1;
3079: }
3080: return 0;
3081: }
3082:
3083: NODE mono_raddec(NODE ideal)
3084: {
3085: DP p;
3086: int nv,w,i,bpi,di,c,len;
3087: int *d,*s,*u,*new;
3088: NODE t,t1,v,r,rem,prev;
3089:
3090: if( !ideal ) return 0;
3091: p = (DP)BDY(ideal);
3092: nv = NV(p);
3093: bpi = (sizeof(int)/sizeof(char))*8;
3094: w = (nv+(bpi-1))/bpi;
3095: d = p->body->dl->d;
3096: if ( !NEXT(ideal) ) {
3097: for ( t = 0, i = nv-1; i >= 0; i-- ) {
3098: if ( d[i] ) {
3099: s = (int *)CALLOC(w,sizeof(int));
3100: s[i/bpi] |= 1<<(i%bpi);
3101: MKNODE(t1,s,t);
3102: t = t1;
3103: }
3104: }
3105: return t;
3106: }
3107: rem = mono_raddec(NEXT(ideal));
3108: r = 0;
3109: len = w*sizeof(int);
3110: u = (int *)CALLOC(w,sizeof(int));
3111: for ( i = nv-1; i >= 0; i-- ) {
3112: if ( d[i] ) {
3113: for ( t = rem; t; t = NEXT(t) ) {
3114: bcopy((char *)BDY(t),(char *)u,len);
3115: u[i/bpi] |= 1<<(i%bpi);
3116: for ( v = r; v; v = NEXT(v) ) {
3117: if ( comp_bits_divisible(u,(int *)BDY(v),nv) ) break;
3118: }
3119: if ( v ) continue;
3120: for ( v = r, prev = 0; v; v = NEXT(v) ) {
3121: if ( comp_bits_divisible((int *)BDY(v),u,nv) ) {
3122: if ( prev ) NEXT(prev) = NEXT(v);
3123: else r = NEXT(r);
3124: } else prev =v;
3125: }
3126: for ( v = r, prev = 0; v; prev = v, v = NEXT(v) ) {
3127: if ( comp_bits_lex(u,(int *)BDY(v),nv) < 0 ) break;
3128: }
3129: new = (int *)CALLOC(w,sizeof(int));
3130: bcopy((char *)u,(char *)new,len);
3131: MKNODE(t1,new,v);
3132: if ( prev ) NEXT(prev) = t1;
3133: else r = t1;
3134: }
3135: }
3136: }
3137: return r;
3138: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>