Annotation of OpenXM_contrib2/asir2000/builtin/dp-supp.c, Revision 1.53
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.53 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/dp-supp.c,v 1.52 2009/01/05 02:14:00 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: }
402: }
403:
1.20 noro 404: void dp_ptozp2_d(DP p0,DP p1,DP *hp,DP *rp)
1.1 noro 405: {
406: DP t,s,h,r;
407: MP m,mr,mr0,m0;
408:
1.8 noro 409: addd(CO,p0,p1,&t); dp_ptozp_d(t,&s);
1.1 noro 410: if ( !p0 ) {
411: h = 0; r = s;
412: } else if ( !p1 ) {
413: h = s; r = 0;
414: } else {
415: for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
416: m = NEXT(m), m0 = NEXT(m0) ) {
417: NEXTMP(mr0,mr); mr->c = m->c; mr->dl = m->dl;
418: }
419: NEXT(mr) = 0; MKDP(p0->nv,mr0,h); MKDP(p0->nv,m,r);
420: }
421: if ( h )
422: h->sugar = p0->sugar;
423: if ( r )
424: r->sugar = p1->sugar;
425: *hp = h; *rp = r;
1.5 noro 426: }
427:
1.22 noro 428: int have_sf_coef(P p)
429: {
430: DCP dc;
431:
432: if ( !p )
433: return 0;
434: else if ( NUM(p) )
435: return NID((Num)p) == N_GFS ? 1 : 0;
436: else {
437: for ( dc = DC(p); dc; dc = NEXT(dc) )
438: if ( have_sf_coef(COEF(dc)) )
439: return 1;
440: return 0;
441: }
442: }
443:
1.25 noro 444: void head_coef(P p,Num *c)
445: {
446: if ( !p )
447: *c = 0;
448: else if ( NUM(p) )
449: *c = (Num)p;
450: else
451: head_coef(COEF(DC(p)),c);
452: }
453:
454: void dp_monic_sf(DP p,DP *rp)
455: {
456: Num c;
457:
458: if ( !p )
459: *rp = 0;
460: else {
461: head_coef(BDY(p)->c,&c);
462: divsdc(CO,p,(P)c,rp);
463: }
464: }
465:
1.20 noro 466: void dp_prim(DP p,DP *rp)
1.5 noro 467: {
1.7 noro 468: P t,g;
469: DP p1;
470: MP m,mr,mr0;
471: int i,n;
472: P *w;
473: Q *c;
474: Q dvr;
1.46 noro 475: NODE tn;
1.5 noro 476:
1.7 noro 477: if ( !p )
478: *rp = 0;
1.23 noro 479: else if ( dp_fcoeffs == N_GFS ) {
480: for ( m = BDY(p); m; m = NEXT(m) )
1.22 noro 481: if ( OID(m->c) == O_N ) {
482: /* GCD of coeffs = 1 */
1.25 noro 483: dp_monic_sf(p,rp);
1.22 noro 484: return;
1.23 noro 485: } else break;
486: /* compute GCD over the finite fieid */
487: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
488: w = (P *)ALLOCA(n*sizeof(P));
489: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ )
490: w[i] = m->c;
491: gcdsf(CO,w,n,&g);
492: if ( NUM(g) )
1.25 noro 493: dp_monic_sf(p,rp);
1.23 noro 494: else {
495: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
496: NEXTMP(mr0,mr); divsp(CO,m->c,g,&mr->c); mr->dl = m->dl;
1.22 noro 497: }
1.25 noro 498: NEXT(mr) = 0; MKDP(p->nv,mr0,p1); p1->sugar = p->sugar;
499: dp_monic_sf(p1,rp);
1.22 noro 500: }
1.23 noro 501: return;
502: } else if ( dp_fcoeffs )
1.7 noro 503: *rp = p;
1.23 noro 504: else if ( NoGCD )
1.7 noro 505: dp_ptozp(p,rp);
506: else {
507: dp_ptozp(p,&p1); p = p1;
508: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
509: if ( n == 1 ) {
510: m = BDY(p);
511: NEWMP(mr); mr->dl = m->dl; mr->c = (P)ONE; NEXT(mr) = 0;
512: MKDP(p->nv,mr,*rp); (*rp)->sugar = p->sugar;
513: return;
514: }
515: w = (P *)ALLOCA(n*sizeof(P));
516: c = (Q *)ALLOCA(n*sizeof(Q));
517: for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
518: if ( NUM(m->c) ) {
519: c[i] = (Q)m->c; w[i] = (P)ONE;
520: } else
521: ptozp(m->c,1,&c[i],&w[i]);
522: qltozl(c,n,&dvr); heu_nezgcdnpz(CO,w,n,&t); mulp(CO,t,(P)dvr,&g);
523: if ( NUM(g) )
524: *rp = p;
525: else {
526: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
527: NEXTMP(mr0,mr); divsp(CO,m->c,g,&mr->c); mr->dl = m->dl;
528: }
529: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.46 noro 530: add_denomlist(g);
1.5 noro 531: }
1.7 noro 532: }
1.5 noro 533: }
534:
1.20 noro 535: void heu_nezgcdnpz(VL vl,P *pl,int m,P *pr)
1.5 noro 536: {
537: int i,r;
538: P gcd,t,s1,s2,u;
539: Q rq;
1.40 noro 540: DCP dc;
541: extern int DP_Print;
542:
1.5 noro 543: while ( 1 ) {
544: for ( i = 0, s1 = 0; i < m; i++ ) {
545: r = random(); UTOQ(r,rq);
546: mulp(vl,pl[i],(P)rq,&t); addp(vl,s1,t,&u); s1 = u;
547: }
548: for ( i = 0, s2 = 0; i < m; i++ ) {
549: r = random(); UTOQ(r,rq);
550: mulp(vl,pl[i],(P)rq,&t); addp(vl,s2,t,&u); s2 = u;
551: }
552: ezgcdp(vl,s1,s2,&gcd);
1.40 noro 553: if ( DP_Print > 2 )
554: { fprintf(asir_out,"(%d)",nmonop(gcd)); fflush(asir_out); }
1.5 noro 555: for ( i = 0; i < m; i++ ) {
556: if ( !divtpz(vl,pl[i],gcd,&t) )
557: break;
558: }
559: if ( i == m )
560: break;
561: }
562: *pr = gcd;
563: }
564:
1.20 noro 565: void dp_prim_mod(DP p,int mod,DP *rp)
1.5 noro 566: {
567: P t,g;
568: MP m,mr,mr0;
569:
570: if ( !p )
571: *rp = 0;
572: else if ( NoGCD )
573: *rp = p;
574: else {
575: for ( m = BDY(p), g = m->c, m = NEXT(m); m; m = NEXT(m) ) {
576: gcdprsmp(CO,mod,g,m->c,&t); g = t;
577: }
578: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
579: NEXTMP(mr0,mr); divsmp(CO,mod,m->c,g,&mr->c); mr->dl = m->dl;
580: }
581: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
582: }
583: }
584:
1.20 noro 585: void dp_cont(DP p,Q *rp)
1.5 noro 586: {
1.7 noro 587: VECT v;
1.5 noro 588:
1.7 noro 589: dp_dtov(p,&v); igcdv(v,rp);
1.5 noro 590: }
591:
1.20 noro 592: void dp_dtov(DP dp,VECT *rp)
1.5 noro 593: {
1.7 noro 594: MP m,t;
595: int i,n;
596: VECT v;
597: pointer *p;
1.5 noro 598:
1.7 noro 599: m = BDY(dp);
600: for ( t = m, n = 0; t; t = NEXT(t), n++ );
601: MKVECT(v,n);
602: for ( i = 0, p = BDY(v), t = m; i < n; t = NEXT(t), i++ )
603: p[i] = (pointer)(t->c);
604: *rp = v;
1.5 noro 605: }
606:
1.7 noro 607: /*
608: * s-poly computation
609: *
610: */
1.5 noro 611:
1.20 noro 612: void dp_sp(DP p1,DP p2,DP *rp)
1.5 noro 613: {
1.7 noro 614: int i,n,td;
615: int *w;
616: DL d1,d2,d;
617: MP m;
618: DP t,s1,s2,u;
619: Q c,c1,c2;
620: N gn,tn;
1.5 noro 621:
1.7 noro 622: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
623: w = (int *)ALLOCA(n*sizeof(int));
624: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 625: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.5 noro 626: }
1.7 noro 627:
628: NEWDL(d,n); d->td = td - d1->td;
629: for ( i = 0; i < n; i++ )
630: d->d[i] = w[i] - d1->d[i];
631: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
632: if ( INT(c1) && INT(c2) ) {
633: gcdn(NM(c1),NM(c2),&gn);
634: if ( !UNIN(gn) ) {
635: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
636: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
1.5 noro 637: }
638: }
1.7 noro 639:
640: NEWMP(m); m->dl = d; m->c = (P)c2; NEXT(m) = 0;
641: MKDP(n,m,s1); s1->sugar = d->td; muld(CO,s1,p1,&t);
642:
643: NEWDL(d,n); d->td = td - d2->td;
644: for ( i = 0; i < n; i++ )
645: d->d[i] = w[i] - d2->d[i];
646: NEWMP(m); m->dl = d; m->c = (P)c1; NEXT(m) = 0;
647: MKDP(n,m,s2); s2->sugar = d->td; muld(CO,s2,p2,&u);
648:
649: subd(CO,t,u,rp);
1.14 noro 650: if ( GenTrace ) {
651: LIST hist;
652: NODE node;
653:
654: node = mknode(4,ONE,0,s1,ONE);
655: MKLIST(hist,node);
656: MKNODE(TraceList,hist,0);
657:
658: node = mknode(4,ONE,0,0,ONE);
659: chsgnd(s2,(DP *)&ARG2(node));
660: MKLIST(hist,node);
661: MKNODE(node,hist,TraceList); TraceList = node;
662: }
663: }
664:
1.20 noro 665: void _dp_sp_dup(DP p1,DP p2,DP *rp)
1.14 noro 666: {
667: int i,n,td;
668: int *w;
669: DL d1,d2,d;
670: MP m;
671: DP t,s1,s2,u;
672: Q c,c1,c2;
673: N gn,tn;
674:
675: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
676: w = (int *)ALLOCA(n*sizeof(int));
677: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 678: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.14 noro 679: }
680:
681: _NEWDL(d,n); d->td = td - d1->td;
682: for ( i = 0; i < n; i++ )
683: d->d[i] = w[i] - d1->d[i];
684: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
685: if ( INT(c1) && INT(c2) ) {
686: gcdn(NM(c1),NM(c2),&gn);
687: if ( !UNIN(gn) ) {
688: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
689: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
690: }
691: }
692:
693: _NEWMP(m); m->dl = d; m->c = (P)c2; NEXT(m) = 0;
694: _MKDP(n,m,s1); s1->sugar = d->td; _muld_dup(CO,s1,p1,&t); _free_dp(s1);
695:
696: _NEWDL(d,n); d->td = td - d2->td;
697: for ( i = 0; i < n; i++ )
698: d->d[i] = w[i] - d2->d[i];
699: _NEWMP(m); m->dl = d; chsgnp((P)c1,&m->c); NEXT(m) = 0;
700: _MKDP(n,m,s2); s2->sugar = d->td; _muld_dup(CO,s2,p2,&u); _free_dp(s2);
701:
702: _addd_destructive(CO,t,u,rp);
1.7 noro 703: if ( GenTrace ) {
704: LIST hist;
705: NODE node;
706:
707: node = mknode(4,ONE,0,s1,ONE);
708: MKLIST(hist,node);
709: MKNODE(TraceList,hist,0);
710:
711: node = mknode(4,ONE,0,0,ONE);
712: chsgnd(s2,(DP *)&ARG2(node));
713: MKLIST(hist,node);
714: MKNODE(node,hist,TraceList); TraceList = node;
715: }
716: }
717:
1.20 noro 718: void dp_sp_mod(DP p1,DP p2,int mod,DP *rp)
1.7 noro 719: {
720: int i,n,td;
721: int *w;
722: DL d1,d2,d;
723: MP m;
724: DP t,s,u;
725:
726: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
727: w = (int *)ALLOCA(n*sizeof(int));
728: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 729: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 730: }
1.18 noro 731: NEWDL_NOINIT(d,n); d->td = td - d1->td;
1.7 noro 732: for ( i = 0; i < n; i++ )
733: d->d[i] = w[i] - d1->d[i];
734: NEWMP(m); m->dl = d; m->c = (P)BDY(p2)->c; NEXT(m) = 0;
735: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p1,s,&t);
1.18 noro 736: NEWDL_NOINIT(d,n); d->td = td - d2->td;
1.7 noro 737: for ( i = 0; i < n; i++ )
738: d->d[i] = w[i] - d2->d[i];
739: NEWMP(m); m->dl = d; m->c = (P)BDY(p1)->c; NEXT(m) = 0;
740: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p2,s,&u);
741: submd(CO,mod,t,u,rp);
742: }
743:
1.20 noro 744: void _dp_sp_mod_dup(DP p1,DP p2,int mod,DP *rp)
1.7 noro 745: {
746: int i,n,td;
747: int *w;
748: DL d1,d2,d;
749: MP m;
750: DP t,s,u;
751:
752: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
753: w = (int *)ALLOCA(n*sizeof(int));
754: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 755: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 756: }
757: _NEWDL(d,n); d->td = td - d1->td;
758: for ( i = 0; i < n; i++ )
759: d->d[i] = w[i] - d1->d[i];
760: _NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
761: _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p1,&t); _free_dp(s);
762: _NEWDL(d,n); d->td = td - d2->td;
763: for ( i = 0; i < n; i++ )
764: d->d[i] = w[i] - d2->d[i];
765: _NEWMP(m); m->dl = d; m->c = STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
766: _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p2,&u); _free_dp(s);
767: _addmd_destructive(mod,t,u,rp);
768: }
769:
1.20 noro 770: void _dp_sp_mod(DP p1,DP p2,int mod,DP *rp)
1.7 noro 771: {
772: int i,n,td;
773: int *w;
774: DL d1,d2,d;
775: MP m;
776: DP t,s,u;
777:
778: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
779: w = (int *)ALLOCA(n*sizeof(int));
780: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 781: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 782: }
783: NEWDL(d,n); d->td = td - d1->td;
784: for ( i = 0; i < n; i++ )
785: d->d[i] = w[i] - d1->d[i];
786: NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
787: MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p1,&t);
788: NEWDL(d,n); d->td = td - d2->td;
789: for ( i = 0; i < n; i++ )
790: d->d[i] = w[i] - d2->d[i];
791: NEWMP(m); m->dl = d; m->c = STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
792: MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p2,&u);
793: addmd_destructive(mod,t,u,rp);
794: }
795:
796: /*
797: * m-reduction
1.13 noro 798: * do content reduction over Z or Q(x,...)
799: * do nothing over finite fields
1.7 noro 800: *
801: */
802:
1.20 noro 803: void dp_red(DP p0,DP p1,DP p2,DP *head,DP *rest,P *dnp,DP *multp)
1.7 noro 804: {
805: int i,n;
806: DL d1,d2,d;
807: MP m;
808: DP t,s,r,h;
809: Q c,c1,c2;
810: N gn,tn;
811: P g,a;
1.23 noro 812: P p[2];
1.7 noro 813:
814: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
815: NEWDL(d,n); d->td = d1->td - d2->td;
816: for ( i = 0; i < n; i++ )
817: d->d[i] = d1->d[i]-d2->d[i];
818: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
1.23 noro 819: if ( dp_fcoeffs == N_GFS ) {
820: p[0] = (P)c1; p[1] = (P)c2;
821: gcdsf(CO,p,2,&g);
822: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
823: } else if ( dp_fcoeffs ) {
1.7 noro 824: /* do nothing */
825: } else if ( INT(c1) && INT(c2) ) {
826: gcdn(NM(c1),NM(c2),&gn);
827: if ( !UNIN(gn) ) {
828: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
829: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
830: }
831: } else {
832: ezgcdpz(CO,(P)c1,(P)c2,&g);
833: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
1.46 noro 834: add_denomlist(g);
1.7 noro 835: }
836: NEWMP(m); m->dl = d; chsgnp((P)c1,&m->c); NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
837: *multp = s;
838: muld(CO,s,p2,&t); muldc(CO,p1,(P)c2,&s); addd(CO,s,t,&r);
839: muldc(CO,p0,(P)c2,&h);
840: *head = h; *rest = r; *dnp = (P)c2;
841: }
842:
1.41 noro 843: /*
844: * m-reduction by a marked poly
845: * do content reduction over Z or Q(x,...)
846: * do nothing over finite fields
847: *
848: */
849:
850:
851: void dp_red_marked(DP p0,DP p1,DP p2,DP hp2,DP *head,DP *rest,P *dnp,DP *multp)
852: {
853: int i,n;
854: DL d1,d2,d;
855: MP m;
856: DP t,s,r,h;
857: Q c,c1,c2;
858: N gn,tn;
859: P g,a;
860: P p[2];
861:
862: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(hp2)->dl;
863: NEWDL(d,n); d->td = d1->td - d2->td;
864: for ( i = 0; i < n; i++ )
865: d->d[i] = d1->d[i]-d2->d[i];
866: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(hp2)->c;
867: if ( dp_fcoeffs == N_GFS ) {
868: p[0] = (P)c1; p[1] = (P)c2;
869: gcdsf(CO,p,2,&g);
870: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
871: } else if ( dp_fcoeffs ) {
872: /* do nothing */
873: } else if ( INT(c1) && INT(c2) ) {
874: gcdn(NM(c1),NM(c2),&gn);
875: if ( !UNIN(gn) ) {
876: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
877: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
878: }
879: } else {
880: ezgcdpz(CO,(P)c1,(P)c2,&g);
881: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
882: }
1.47 noro 883: NEWMP(m); m->dl = d; m->c = (P)c1; NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
1.41 noro 884: *multp = s;
1.47 noro 885: muld(CO,s,p2,&t); muldc(CO,p1,(P)c2,&s); subd(CO,s,t,&r);
1.41 noro 886: muldc(CO,p0,(P)c2,&h);
887: *head = h; *rest = r; *dnp = (P)c2;
888: }
889:
1.44 noro 890: void dp_red_marked_mod(DP p0,DP p1,DP p2,DP hp2,int mod,DP *head,DP *rest,P *dnp)
891: {
892: int i,n;
893: DL d1,d2,d;
894: MP m;
895: DP t,s,r,h;
896: P c1,c2,g,u;
897:
898: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(hp2)->dl;
899: NEWDL(d,n); d->td = d1->td - d2->td;
900: for ( i = 0; i < n; i++ )
901: d->d[i] = d1->d[i]-d2->d[i];
902: c1 = (P)BDY(p1)->c; c2 = (P)BDY(hp2)->c;
903: gcdprsmp(CO,mod,c1,c2,&g);
904: divsmp(CO,mod,c1,g,&u); c1 = u; divsmp(CO,mod,c2,g,&u); c2 = u;
905: if ( NUM(c2) ) {
906: divsmp(CO,mod,c1,c2,&u); c1 = u; c2 = (P)ONEM;
907: }
908: NEWMP(m); m->dl = d; chsgnmp(mod,(P)c1,&m->c); NEXT(m) = 0;
909: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,s,p2,&t);
910: if ( NUM(c2) ) {
911: addmd(CO,mod,p1,t,&r); h = p0;
912: } else {
913: mulmdc(CO,mod,p1,c2,&s); addmd(CO,mod,s,t,&r); mulmdc(CO,mod,p0,c2,&h);
914: }
915: *head = h; *rest = r; *dnp = c2;
916: }
917:
1.13 noro 918: /* m-reduction over a field */
919:
1.20 noro 920: void dp_red_f(DP p1,DP p2,DP *rest)
1.13 noro 921: {
922: int i,n;
923: DL d1,d2,d;
924: MP m;
1.20 noro 925: DP t,s;
1.13 noro 926: Obj a,b;
927:
928: n = p1->nv;
929: d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
930:
931: NEWDL(d,n); d->td = d1->td - d2->td;
932: for ( i = 0; i < n; i++ )
933: d->d[i] = d1->d[i]-d2->d[i];
934:
935: NEWMP(m); m->dl = d;
936: divr(CO,(Obj)BDY(p1)->c,(Obj)BDY(p2)->c,&a); chsgnr(a,&b);
937: C(m) = (P)b;
938: NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
939:
940: muld(CO,s,p2,&t); addd(CO,p1,t,rest);
941: }
942:
1.20 noro 943: void dp_red_mod(DP p0,DP p1,DP p2,int mod,DP *head,DP *rest,P *dnp)
1.7 noro 944: {
945: int i,n;
946: DL d1,d2,d;
947: MP m;
948: DP t,s,r,h;
949: P c1,c2,g,u;
950:
951: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
952: NEWDL(d,n); d->td = d1->td - d2->td;
953: for ( i = 0; i < n; i++ )
954: d->d[i] = d1->d[i]-d2->d[i];
955: c1 = (P)BDY(p1)->c; c2 = (P)BDY(p2)->c;
956: gcdprsmp(CO,mod,c1,c2,&g);
957: divsmp(CO,mod,c1,g,&u); c1 = u; divsmp(CO,mod,c2,g,&u); c2 = u;
958: if ( NUM(c2) ) {
959: divsmp(CO,mod,c1,c2,&u); c1 = u; c2 = (P)ONEM;
960: }
961: NEWMP(m); m->dl = d; chsgnmp(mod,(P)c1,&m->c); NEXT(m) = 0;
1.11 noro 962: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,s,p2,&t);
1.7 noro 963: if ( NUM(c2) ) {
964: addmd(CO,mod,p1,t,&r); h = p0;
965: } else {
966: mulmdc(CO,mod,p1,c2,&s); addmd(CO,mod,s,t,&r); mulmdc(CO,mod,p0,c2,&h);
967: }
968: *head = h; *rest = r; *dnp = c2;
969: }
970:
1.10 noro 971: struct oEGT eg_red_mod;
972:
1.20 noro 973: void _dp_red_mod_destructive(DP p1,DP p2,int mod,DP *rp)
1.7 noro 974: {
975: int i,n;
976: DL d1,d2,d;
977: MP m;
978: DP t,s;
1.16 noro 979: int c,c1,c2;
980: extern int do_weyl;
1.7 noro 981:
982: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
983: _NEWDL(d,n); d->td = d1->td - d2->td;
984: for ( i = 0; i < n; i++ )
985: d->d[i] = d1->d[i]-d2->d[i];
1.16 noro 986: c = invm(ITOS(BDY(p2)->c),mod);
987: c2 = ITOS(BDY(p1)->c);
988: DMAR(c,c2,0,mod,c1);
1.7 noro 989: _NEWMP(m); m->dl = d; m->c = STOI(mod-c1); NEXT(m) = 0;
1.16 noro 990: #if 0
1.7 noro 991: _MKDP(n,m,s); s->sugar = d->td;
992: _mulmd_dup(mod,s,p2,&t); _free_dp(s);
1.16 noro 993: #else
994: if ( do_weyl ) {
1.19 noro 995: _MKDP(n,m,s); s->sugar = d->td;
996: _mulmd_dup(mod,s,p2,&t); _free_dp(s);
1.16 noro 997: } else {
998: _mulmdm_dup(mod,p2,m,&t); _FREEMP(m);
999: }
1000: #endif
1.10 noro 1001: /* get_eg(&t0); */
1.7 noro 1002: _addmd_destructive(mod,p1,t,rp);
1.10 noro 1003: /* get_eg(&t1); add_eg(&eg_red_mod,&t0,&t1); */
1.7 noro 1004: }
1005:
1006: /*
1007: * normal form computation
1008: *
1009: */
1.5 noro 1010:
1.20 noro 1011: void dp_true_nf(NODE b,DP g,DP *ps,int full,DP *rp,P *dnp)
1.5 noro 1012: {
1013: DP u,p,d,s,t,dmy;
1014: NODE l;
1015: MP m,mr;
1016: int i,n;
1017: int *wb;
1018: int sugar,psugar;
1019: P dn,tdn,tdn1;
1020:
1021: dn = (P)ONE;
1022: if ( !g ) {
1023: *rp = 0; *dnp = dn; return;
1024: }
1025: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1026: wb = (int *)ALLOCA(n*sizeof(int));
1027: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1028: wb[i] = QTOS((Q)BDY(l));
1029: sugar = g->sugar;
1030: for ( d = 0; g; ) {
1031: for ( u = 0, i = 0; i < n; i++ ) {
1032: if ( dp_redble(g,p = ps[wb[i]]) ) {
1033: dp_red(d,g,p,&t,&u,&tdn,&dmy);
1034: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1035: sugar = MAX(sugar,psugar);
1036: if ( !u ) {
1037: if ( d )
1038: d->sugar = sugar;
1039: *rp = d; *dnp = dn; return;
1040: } else {
1041: d = t;
1042: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
1043: }
1044: break;
1045: }
1046: }
1047: if ( u )
1048: g = u;
1049: else if ( !full ) {
1050: if ( g ) {
1051: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1052: }
1053: *rp = g; *dnp = dn; return;
1054: } else {
1055: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1056: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1057: addd(CO,d,t,&s); d = s;
1058: dp_rest(g,&t); g = t;
1059: }
1060: }
1061: if ( d )
1062: d->sugar = sugar;
1063: *rp = d; *dnp = dn;
1064: }
1065:
1.43 noro 1066: void dp_removecont2(DP p1,DP p2,DP *r1p,DP *r2p,Q *contp)
1067: {
1068: struct oVECT v;
1069: int i,n1,n2,n;
1070: MP m,m0,t;
1071: Q *w;
1072: Q h;
1073:
1074: if ( p1 ) {
1075: for ( i = 0, m = BDY(p1); m; m = NEXT(m), i++ );
1076: n1 = i;
1077: } else
1078: n1 = 0;
1079: if ( p2 ) {
1080: for ( i = 0, m = BDY(p2); m; m = NEXT(m), i++ );
1081: n2 = i;
1082: } else
1083: n2 = 0;
1084: n = n1+n2;
1085: if ( !n ) {
1086: *r1p = 0; *r2p = 0; *contp = ONE; return;
1087: }
1088: w = (Q *)ALLOCA(n*sizeof(Q));
1089: v.len = n;
1090: v.body = (pointer *)w;
1091: i = 0;
1092: if ( p1 )
1093: for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = (Q)m->c;
1094: if ( p2 )
1095: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = (Q)m->c;
1096: h = w[0]; removecont_array((P *)w,n,1); divq(h,w[0],contp);
1097: i = 0;
1098: if ( p1 ) {
1099: for ( m0 = 0, t = BDY(p1); i < n1; i++, t = NEXT(t) ) {
1100: NEXTMP(m0,m); m->c = (P)w[i]; m->dl = t->dl;
1101: }
1102: NEXT(m) = 0;
1103: MKDP(p1->nv,m0,*r1p); (*r1p)->sugar = p1->sugar;
1104: } else
1105: *r1p = 0;
1106: if ( p2 ) {
1107: for ( m0 = 0, t = BDY(p2); i < n; i++, t = NEXT(t) ) {
1108: NEXTMP(m0,m); m->c = (P)w[i]; m->dl = t->dl;
1109: }
1110: NEXT(m) = 0;
1111: MKDP(p2->nv,m0,*r2p); (*r2p)->sugar = p2->sugar;
1112: } else
1113: *r2p = 0;
1114: }
1115:
1.41 noro 1116: /* true nf by a marked GB */
1117:
1.43 noro 1118: void dp_true_nf_marked(NODE b,DP g,DP *ps,DP *hps,DP *rp,P *nmp,P *dnp)
1.41 noro 1119: {
1120: DP u,p,d,s,t,dmy,hp;
1121: NODE l;
1122: MP m,mr;
1.43 noro 1123: int i,n,hmag;
1.41 noro 1124: int *wb;
1.43 noro 1125: int sugar,psugar,multiple;
1126: P nm,tnm1,dn,tdn,tdn1;
1127: Q cont;
1.41 noro 1128:
1.43 noro 1129: multiple = 0;
1130: hmag = multiple*HMAG(g);
1131: nm = (P)ONE;
1.41 noro 1132: dn = (P)ONE;
1133: if ( !g ) {
1134: *rp = 0; *dnp = dn; return;
1135: }
1136: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1137: wb = (int *)ALLOCA(n*sizeof(int));
1138: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1139: wb[i] = QTOS((Q)BDY(l));
1140: sugar = g->sugar;
1141: for ( d = 0; g; ) {
1142: for ( u = 0, i = 0; i < n; i++ ) {
1143: if ( dp_redble(g,hp = hps[wb[i]]) ) {
1144: p = ps[wb[i]];
1145: dp_red_marked(d,g,p,hp,&t,&u,&tdn,&dmy);
1146: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1147: sugar = MAX(sugar,psugar);
1148: if ( !u ) {
1.43 noro 1149: goto last;
1.41 noro 1150: } else {
1151: d = t;
1152: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
1153: }
1154: break;
1155: }
1156: }
1.43 noro 1157: if ( u ) {
1.41 noro 1158: g = u;
1.43 noro 1159: if ( multiple && ((d && HMAG(d)>hmag) || (HMAG(g)>hmag)) ) {
1160: dp_removecont2(d,g,&t,&u,&cont); d = t; g = u;
1161: mulp(CO,nm,(P)cont,&tnm1); nm = tnm1;
1162: if ( d )
1163: hmag = multiple*HMAG(d);
1164: else
1165: hmag = multiple*HMAG(g);
1166: }
1167: } else {
1.41 noro 1168: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1169: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1170: addd(CO,d,t,&s); d = s;
1171: dp_rest(g,&t); g = t;
1172: }
1173: }
1.43 noro 1174: last:
1175: if ( d ) {
1176: dp_removecont2(d,0,&t,&u,&cont); d = t;
1177: mulp(CO,nm,(P)cont,&tnm1); nm = tnm1;
1.41 noro 1178: d->sugar = sugar;
1.43 noro 1179: }
1180: *rp = d; *nmp = nm; *dnp = dn;
1.41 noro 1181: }
1182:
1.44 noro 1183: void dp_true_nf_marked_mod(NODE b,DP g,DP *ps,DP *hps,int mod,DP *rp,P *dnp)
1184: {
1185: DP hp,u,p,d,s,t;
1186: NODE l;
1187: MP m,mr;
1188: int i,n;
1189: int *wb;
1190: int sugar,psugar;
1191: P dn,tdn,tdn1;
1192:
1193: dn = (P)ONEM;
1194: if ( !g ) {
1195: *rp = 0; *dnp = dn; return;
1196: }
1197: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1198: wb = (int *)ALLOCA(n*sizeof(int));
1199: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1200: wb[i] = QTOS((Q)BDY(l));
1201: sugar = g->sugar;
1202: for ( d = 0; g; ) {
1203: for ( u = 0, i = 0; i < n; i++ ) {
1204: if ( dp_redble(g,hp = hps[wb[i]]) ) {
1205: p = ps[wb[i]];
1206: dp_red_marked_mod(d,g,p,hp,mod,&t,&u,&tdn);
1207: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1208: sugar = MAX(sugar,psugar);
1209: if ( !u ) {
1210: if ( d )
1211: d->sugar = sugar;
1212: *rp = d; *dnp = dn; return;
1213: } else {
1214: d = t;
1215: mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
1216: }
1217: break;
1218: }
1219: }
1220: if ( u )
1221: g = u;
1222: else {
1223: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1224: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1225: addmd(CO,mod,d,t,&s); d = s;
1226: dp_rest(g,&t); g = t;
1227: }
1228: }
1229: if ( d )
1230: d->sugar = sugar;
1231: *rp = d; *dnp = dn;
1232: }
1233:
1.47 noro 1234: /* true nf by a marked GB and collect quotients */
1235:
1236: DP *dp_true_nf_and_quotient_marked (NODE b,DP g,DP *ps,DP *hps,DP *rp,P *dnp)
1237: {
1238: DP u,p,d,s,t,dmy,hp,mult;
1239: DP *q;
1240: NODE l;
1241: MP m,mr;
1242: int i,n,j;
1243: int *wb;
1244: int sugar,psugar,multiple;
1245: P nm,tnm1,dn,tdn,tdn1;
1246: Q cont;
1247:
1248: dn = (P)ONE;
1249: if ( !g ) {
1250: *rp = 0; *dnp = dn; return;
1251: }
1252: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1253: wb = (int *)ALLOCA(n*sizeof(int));
1254: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1255: wb[i] = QTOS((Q)BDY(l));
1256: q = (DP *)MALLOC(n*sizeof(DP));
1257: for ( i = 0; i < n; i++ ) q[i] = 0;
1258: sugar = g->sugar;
1259: for ( d = 0; g; ) {
1260: for ( u = 0, i = 0; i < n; i++ ) {
1261: if ( dp_redble(g,hp = hps[wb[i]]) ) {
1262: p = ps[wb[i]];
1263: dp_red_marked(d,g,p,hp,&t,&u,&tdn,&mult);
1264: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1265: sugar = MAX(sugar,psugar);
1266: for ( j = 0; j < n; j++ ) {
1267: muldc(CO,q[j],(P)tdn,&dmy); q[j] = dmy;
1268: }
1269: addd(CO,q[wb[i]],mult,&dmy); q[wb[i]] = dmy;
1270: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
1271: d = t;
1272: if ( !u ) goto last;
1273: break;
1274: }
1275: }
1276: if ( u ) {
1277: g = u;
1278: } else {
1279: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1280: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1281: addd(CO,d,t,&s); d = s;
1282: dp_rest(g,&t); g = t;
1283: }
1284: }
1285: last:
1286: if ( d ) d->sugar = sugar;
1287: *rp = d; *dnp = dn;
1288: return q;
1289: }
1290:
1.13 noro 1291: /* nf computation over Z */
1292:
1.20 noro 1293: void dp_nf_z(NODE b,DP g,DP *ps,int full,int multiple,DP *rp)
1.5 noro 1294: {
1295: DP u,p,d,s,t,dmy1;
1296: P dmy;
1297: NODE l;
1298: MP m,mr;
1299: int i,n;
1300: int *wb;
1301: int hmag;
1302: int sugar,psugar;
1303:
1304: if ( !g ) {
1305: *rp = 0; return;
1306: }
1307: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1308: wb = (int *)ALLOCA(n*sizeof(int));
1309: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1310: wb[i] = QTOS((Q)BDY(l));
1.12 noro 1311:
1.13 noro 1312: hmag = multiple*HMAG(g);
1.5 noro 1313: sugar = g->sugar;
1.12 noro 1314:
1.5 noro 1315: for ( d = 0; g; ) {
1316: for ( u = 0, i = 0; i < n; i++ ) {
1317: if ( dp_redble(g,p = ps[wb[i]]) ) {
1318: dp_red(d,g,p,&t,&u,&dmy,&dmy1);
1319: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1320: sugar = MAX(sugar,psugar);
1321: if ( !u ) {
1322: if ( d )
1323: d->sugar = sugar;
1324: *rp = d; return;
1325: }
1326: d = t;
1327: break;
1328: }
1329: }
1330: if ( u ) {
1331: g = u;
1332: if ( d ) {
1.13 noro 1333: if ( multiple && HMAG(d) > hmag ) {
1.5 noro 1334: dp_ptozp2(d,g,&t,&u); d = t; g = u;
1335: hmag = multiple*HMAG(d);
1336: }
1337: } else {
1.13 noro 1338: if ( multiple && HMAG(g) > hmag ) {
1.5 noro 1339: dp_ptozp(g,&t); g = t;
1340: hmag = multiple*HMAG(g);
1341: }
1342: }
1343: }
1344: else if ( !full ) {
1345: if ( g ) {
1346: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1347: }
1348: *rp = g; return;
1349: } else {
1350: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1351: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1352: addd(CO,d,t,&s); d = s;
1353: dp_rest(g,&t); g = t;
1354:
1355: }
1356: }
1357: if ( d )
1358: d->sugar = sugar;
1359: *rp = d;
1360: }
1361:
1.13 noro 1362: /* nf computation over a field */
1363:
1.20 noro 1364: void dp_nf_f(NODE b,DP g,DP *ps,int full,DP *rp)
1.13 noro 1365: {
1366: DP u,p,d,s,t;
1367: NODE l;
1368: MP m,mr;
1369: int i,n;
1370: int *wb;
1371: int sugar,psugar;
1372:
1373: if ( !g ) {
1374: *rp = 0; return;
1375: }
1376: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1377: wb = (int *)ALLOCA(n*sizeof(int));
1378: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1379: wb[i] = QTOS((Q)BDY(l));
1380:
1381: sugar = g->sugar;
1382: for ( d = 0; g; ) {
1383: for ( u = 0, i = 0; i < n; i++ ) {
1384: if ( dp_redble(g,p = ps[wb[i]]) ) {
1385: dp_red_f(g,p,&u);
1386: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1387: sugar = MAX(sugar,psugar);
1388: if ( !u ) {
1389: if ( d )
1390: d->sugar = sugar;
1391: *rp = d; return;
1392: }
1393: break;
1394: }
1395: }
1396: if ( u )
1397: g = u;
1398: else if ( !full ) {
1399: if ( g ) {
1400: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1401: }
1402: *rp = g; return;
1403: } else {
1404: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1405: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1406: addd(CO,d,t,&s); d = s;
1407: dp_rest(g,&t); g = t;
1408: }
1409: }
1410: if ( d )
1411: d->sugar = sugar;
1412: *rp = d;
1413: }
1414:
1415: /* nf computation over GF(mod) (only for internal use) */
1416:
1.20 noro 1417: void dp_nf_mod(NODE b,DP g,DP *ps,int mod,int full,DP *rp)
1.5 noro 1418: {
1419: DP u,p,d,s,t;
1420: P dmy;
1421: NODE l;
1422: MP m,mr;
1423: int sugar,psugar;
1424:
1425: if ( !g ) {
1426: *rp = 0; return;
1427: }
1428: sugar = g->sugar;
1429: for ( d = 0; g; ) {
1430: for ( u = 0, l = b; l; l = NEXT(l) ) {
1431: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
1432: dp_red_mod(d,g,p,mod,&t,&u,&dmy);
1433: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1434: sugar = MAX(sugar,psugar);
1435: if ( !u ) {
1436: if ( d )
1437: d->sugar = sugar;
1438: *rp = d; return;
1439: }
1440: d = t;
1441: break;
1442: }
1443: }
1444: if ( u )
1445: g = u;
1446: else if ( !full ) {
1447: if ( g ) {
1448: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1449: }
1450: *rp = g; return;
1451: } else {
1452: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1453: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1454: addmd(CO,mod,d,t,&s); d = s;
1455: dp_rest(g,&t); g = t;
1456: }
1457: }
1458: if ( d )
1459: d->sugar = sugar;
1460: *rp = d;
1461: }
1462:
1.20 noro 1463: void dp_true_nf_mod(NODE b,DP g,DP *ps,int mod,int full,DP *rp,P *dnp)
1.5 noro 1464: {
1465: DP u,p,d,s,t;
1466: NODE l;
1467: MP m,mr;
1468: int i,n;
1469: int *wb;
1470: int sugar,psugar;
1471: P dn,tdn,tdn1;
1472:
1473: dn = (P)ONEM;
1474: if ( !g ) {
1475: *rp = 0; *dnp = dn; return;
1476: }
1477: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1478: wb = (int *)ALLOCA(n*sizeof(int));
1479: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1480: wb[i] = QTOS((Q)BDY(l));
1481: sugar = g->sugar;
1482: for ( d = 0; g; ) {
1483: for ( u = 0, i = 0; i < n; i++ ) {
1484: if ( dp_redble(g,p = ps[wb[i]]) ) {
1485: dp_red_mod(d,g,p,mod,&t,&u,&tdn);
1486: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1487: sugar = MAX(sugar,psugar);
1488: if ( !u ) {
1489: if ( d )
1490: d->sugar = sugar;
1491: *rp = d; *dnp = dn; return;
1492: } else {
1493: d = t;
1494: mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
1495: }
1496: break;
1497: }
1498: }
1499: if ( u )
1500: g = u;
1501: else if ( !full ) {
1502: if ( g ) {
1503: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1504: }
1505: *rp = g; *dnp = dn; return;
1506: } else {
1507: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1508: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1509: addmd(CO,mod,d,t,&s); d = s;
1510: dp_rest(g,&t); g = t;
1511: }
1512: }
1513: if ( d )
1514: d->sugar = sugar;
1515: *rp = d; *dnp = dn;
1516: }
1517:
1.20 noro 1518: void _dp_nf_mod_destructive(NODE b,DP g,DP *ps,int mod,int full,DP *rp)
1.5 noro 1519: {
1.20 noro 1520: DP u,p,d;
1.7 noro 1521: NODE l;
1.20 noro 1522: MP m,mrd;
1523: int sugar,psugar,n,h_reducible;
1.5 noro 1524:
1.7 noro 1525: if ( !g ) {
1526: *rp = 0; return;
1.5 noro 1527: }
1.7 noro 1528: sugar = g->sugar;
1529: n = g->nv;
1530: for ( d = 0; g; ) {
1531: for ( h_reducible = 0, l = b; l; l = NEXT(l) ) {
1532: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
1533: h_reducible = 1;
1534: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1535: _dp_red_mod_destructive(g,p,mod,&u); g = u;
1536: sugar = MAX(sugar,psugar);
1537: if ( !g ) {
1538: if ( d )
1539: d->sugar = sugar;
1540: _dptodp(d,rp); _free_dp(d); return;
1541: }
1542: break;
1543: }
1544: }
1545: if ( !h_reducible ) {
1546: /* head term is not reducible */
1547: if ( !full ) {
1548: if ( g )
1549: g->sugar = sugar;
1550: _dptodp(g,rp); _free_dp(g); return;
1551: } else {
1552: m = BDY(g);
1553: if ( NEXT(m) ) {
1554: BDY(g) = NEXT(m); NEXT(m) = 0;
1555: } else {
1556: _FREEDP(g); g = 0;
1557: }
1558: if ( d ) {
1559: for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
1560: NEXT(mrd) = m;
1561: } else {
1562: _MKDP(n,m,d);
1563: }
1564: }
1565: }
1.5 noro 1566: }
1.7 noro 1567: if ( d )
1568: d->sugar = sugar;
1569: _dptodp(d,rp); _free_dp(d);
1.5 noro 1570: }
1.13 noro 1571:
1572: /* reduction by linear base over a field */
1573:
1.20 noro 1574: void dp_lnf_f(DP p1,DP p2,NODE g,DP *r1p,DP *r2p)
1.13 noro 1575: {
1576: DP r1,r2,b1,b2,t,s;
1577: Obj c,c1,c2;
1578: NODE l,b;
1579: int n;
1580:
1581: if ( !p1 ) {
1582: *r1p = p1; *r2p = p2; return;
1583: }
1584: n = p1->nv;
1585: for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
1586: if ( !r1 ) {
1587: *r1p = r1; *r2p = r2; return;
1588: }
1589: b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
1590: if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
1591: b2 = (DP)BDY(NEXT(b));
1592: divr(CO,(Obj)ONE,(Obj)BDY(b1)->c,&c1);
1593: mulr(CO,c1,(Obj)BDY(r1)->c,&c2); chsgnr(c2,&c);
1594: muldc(CO,b1,(P)c,&t); addd(CO,r1,t,&s); r1 = s;
1595: muldc(CO,b2,(P)c,&t); addd(CO,r2,t,&s); r2 = s;
1596: }
1597: }
1598: *r1p = r1; *r2p = r2;
1599: }
1600:
1601: /* reduction by linear base over GF(mod) */
1.5 noro 1602:
1.20 noro 1603: void dp_lnf_mod(DP p1,DP p2,NODE g,int mod,DP *r1p,DP *r2p)
1.5 noro 1604: {
1.7 noro 1605: DP r1,r2,b1,b2,t,s;
1606: P c;
1607: MQ c1,c2;
1608: NODE l,b;
1609: int n;
1610:
1611: if ( !p1 ) {
1612: *r1p = p1; *r2p = p2; return;
1613: }
1614: n = p1->nv;
1615: for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
1616: if ( !r1 ) {
1617: *r1p = r1; *r2p = r2; return;
1618: }
1619: b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
1620: if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
1621: b2 = (DP)BDY(NEXT(b));
1622: invmq(mod,(MQ)BDY(b1)->c,&c1);
1623: mulmq(mod,c1,(MQ)BDY(r1)->c,&c2); chsgnmp(mod,(P)c2,&c);
1624: mulmdc(CO,mod,b1,c,&t); addmd(CO,mod,r1,t,&s); r1 = s;
1625: mulmdc(CO,mod,b2,c,&t); addmd(CO,mod,r2,t,&s); r2 = s;
1626: }
1627: }
1628: *r1p = r1; *r2p = r2;
1.5 noro 1629: }
1630:
1.20 noro 1631: void dp_nf_tab_mod(DP p,LIST *tab,int mod,DP *rp)
1.5 noro 1632: {
1.7 noro 1633: DP s,t,u;
1634: MP m;
1635: DL h;
1636: int i,n;
1637:
1638: if ( !p ) {
1639: *rp = p; return;
1640: }
1641: n = p->nv;
1642: for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
1643: h = m->dl;
1644: while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
1645: i++;
1646: mulmdc(CO,mod,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
1647: addmd(CO,mod,s,t,&u); s = u;
1.24 noro 1648: }
1649: *rp = s;
1650: }
1651:
1652: void dp_nf_tab_f(DP p,LIST *tab,DP *rp)
1653: {
1654: DP s,t,u;
1655: MP m;
1656: DL h;
1657: int i,n;
1658:
1659: if ( !p ) {
1660: *rp = p; return;
1661: }
1662: n = p->nv;
1663: for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
1664: h = m->dl;
1665: while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
1666: i++;
1667: muldc(CO,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
1668: addd(CO,s,t,&u); s = u;
1.7 noro 1669: }
1670: *rp = s;
1.5 noro 1671: }
1672:
1.7 noro 1673: /*
1674: * setting flags
1.30 noro 1675: * call create_order_spec with vl=0 to set old type order.
1.7 noro 1676: *
1677: */
1678:
1.27 noro 1679: int create_order_spec(VL vl,Obj obj,struct order_spec **specp)
1.5 noro 1680: {
1.37 noro 1681: int i,j,n,s,row,col,ret;
1.27 noro 1682: struct order_spec *spec;
1.7 noro 1683: struct order_pair *l;
1684: NODE node,t,tn;
1685: MAT m;
1.49 noro 1686: VECT v;
1687: pointer **b,*bv;
1.7 noro 1688: int **w;
1.5 noro 1689:
1.37 noro 1690: if ( vl && obj && OID(obj) == O_LIST ) {
1691: ret = create_composite_order_spec(vl,(LIST)obj,specp);
1692: if ( show_orderspec )
1693: print_composite_order_spec(*specp);
1694: return ret;
1695: }
1.27 noro 1696:
1697: *specp = spec = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1.7 noro 1698: if ( !obj || NUM(obj) ) {
1699: spec->id = 0; spec->obj = obj;
1700: spec->ord.simple = QTOS((Q)obj);
1701: return 1;
1702: } else if ( OID(obj) == O_LIST ) {
1703: node = BDY((LIST)obj);
1.53 ! noro 1704: if ( !BDY(node) || NUM(BDY(node)) ) {
! 1705: if ( length(node) < 2 )
! 1706: error("create_order_spec : invalid argument");
! 1707: create_order_spec(0,(Obj)BDY(NEXT(node)),&spec);
! 1708: spec->id += 256; spec->obj = obj;
! 1709: spec->ispot = (BDY(node)!=0);
! 1710: *specp = spec;
! 1711: return 1;
! 1712: }
! 1713:
1.7 noro 1714: for ( n = 0, t = node; t; t = NEXT(t), n++ );
1715: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
1716: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
1717: tn = BDY((LIST)BDY(t)); l[i].order = QTOS((Q)BDY(tn));
1718: tn = NEXT(tn); l[i].length = QTOS((Q)BDY(tn));
1719: s += l[i].length;
1720: }
1721: spec->id = 1; spec->obj = obj;
1722: spec->ord.block.order_pair = l;
1723: spec->ord.block.length = n; spec->nv = s;
1724: return 1;
1725: } else if ( OID(obj) == O_MAT ) {
1726: m = (MAT)obj; row = m->row; col = m->col; b = BDY(m);
1727: w = almat(row,col);
1728: for ( i = 0; i < row; i++ )
1729: for ( j = 0; j < col; j++ )
1730: w[i][j] = QTOS((Q)b[i][j]);
1731: spec->id = 2; spec->obj = obj;
1732: spec->nv = col; spec->ord.matrix.row = row;
1733: spec->ord.matrix.matrix = w;
1734: return 1;
1735: } else
1.5 noro 1736: return 0;
1737: }
1738:
1.28 noro 1739: void print_composite_order_spec(struct order_spec *spec)
1740: {
1741: int nv,n,len,i,j,k,start;
1742: struct weight_or_block *worb;
1743:
1744: nv = spec->nv;
1745: n = spec->ord.composite.length;
1746: worb = spec->ord.composite.w_or_b;
1747: for ( i = 0; i < n; i++, worb++ ) {
1748: len = worb->length;
1749: printf("[ ");
1750: switch ( worb->type ) {
1751: case IS_DENSE_WEIGHT:
1752: for ( j = 0; j < len; j++ )
1753: printf("%d ",worb->body.dense_weight[j]);
1754: for ( ; j < nv; j++ )
1755: printf("0 ");
1756: break;
1757: case IS_SPARSE_WEIGHT:
1758: for ( j = 0, k = 0; j < nv; j++ )
1759: if ( j == worb->body.sparse_weight[k].pos )
1760: printf("%d ",worb->body.sparse_weight[k++].value);
1761: else
1762: printf("0 ");
1763: break;
1764: case IS_BLOCK:
1765: start = worb->body.block.start;
1766: for ( j = 0; j < start; j++ ) printf("0 ");
1767: switch ( worb->body.block.order ) {
1768: case 0:
1769: for ( k = 0; k < len; k++, j++ ) printf("R ");
1770: break;
1771: case 1:
1772: for ( k = 0; k < len; k++, j++ ) printf("G ");
1773: break;
1774: case 2:
1775: for ( k = 0; k < len; k++, j++ ) printf("L ");
1776: break;
1777: }
1778: for ( ; j < nv; j++ ) printf("0 ");
1779: break;
1780: }
1781: printf("]\n");
1782: }
1.38 noro 1783: }
1784:
1785: struct order_spec *append_block(struct order_spec *spec,
1786: int nv,int nalg,int ord)
1787: {
1788: MAT m,mat;
1789: int i,j,row,col,n;
1790: Q **b,**wp;
1791: int **w;
1792: NODE t,s,s0;
1793: struct order_pair *l,*l0;
1794: int n0,nv0;
1795: LIST list0,list1,list;
1796: Q oq,nq;
1797: struct order_spec *r;
1798:
1799: r = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1800: switch ( spec->id ) {
1801: case 0:
1802: STOQ(spec->ord.simple,oq); STOQ(nv,nq);
1803: t = mknode(2,oq,nq); MKLIST(list0,t);
1804: STOQ(ord,oq); STOQ(nalg,nq);
1805: t = mknode(2,oq,nq); MKLIST(list1,t);
1806: t = mknode(2,list0,list1); MKLIST(list,t);
1807: l = (struct order_pair *)MALLOC_ATOMIC(2*sizeof(struct order_pair));
1808: l[0].order = spec->ord.simple; l[0].length = nv;
1809: l[1].order = ord; l[1].length = nalg;
1810: r->id = 1; r->obj = (Obj)list;
1811: r->ord.block.order_pair = l;
1812: r->ord.block.length = 2;
1813: r->nv = nv+nalg;
1814: break;
1815: case 1:
1816: if ( spec->nv != nv )
1817: error("append_block : number of variables mismatch");
1818: l0 = spec->ord.block.order_pair;
1819: n0 = spec->ord.block.length;
1820: nv0 = spec->nv;
1821: list0 = (LIST)spec->obj;
1822: n = n0+1;
1823: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
1824: for ( i = 0; i < n0; i++ )
1825: l[i] = l0[i];
1826: l[i].order = ord; l[i].length = nalg;
1827: for ( t = BDY(list0), s0 = 0; t; t = NEXT(t) ) {
1828: NEXTNODE(s0,s); BDY(s) = BDY(t);
1829: }
1830: STOQ(ord,oq); STOQ(nalg,nq);
1831: t = mknode(2,oq,nq); MKLIST(list,t);
1832: NEXTNODE(s0,s); BDY(s) = (pointer)list; NEXT(s) = 0;
1833: MKLIST(list,s0);
1834: r->id = 1; r->obj = (Obj)list;
1835: r->ord.block.order_pair = l;
1836: r->ord.block.length = n;
1837: r->nv = nv+nalg;
1838: break;
1839: case 2:
1840: if ( spec->nv != nv )
1841: error("append_block : number of variables mismatch");
1842: m = (MAT)spec->obj;
1843: row = m->row; col = m->col; b = (Q **)BDY(m);
1844: w = almat(row+nalg,col+nalg);
1845: MKMAT(mat,row+nalg,col+nalg); wp = (Q **)BDY(mat);
1846: for ( i = 0; i < row; i++ )
1847: for ( j = 0; j < col; j++ ) {
1848: w[i][j] = QTOS(b[i][j]);
1849: wp[i][j] = b[i][j];
1850: }
1851: for ( i = 0; i < nalg; i++ ) {
1852: w[i+row][i+col] = 1;
1853: wp[i+row][i+col] = ONE;
1854: }
1855: r->id = 2; r->obj = (Obj)mat;
1856: r->nv = col+nalg; r->ord.matrix.row = row+nalg;
1857: r->ord.matrix.matrix = w;
1858: break;
1859: case 3:
1860: default:
1861: /* XXX */
1862: error("append_block : not implemented yet");
1863: }
1864: return r;
1.28 noro 1865: }
1866:
1.37 noro 1867: int comp_sw(struct sparse_weight *a, struct sparse_weight *b)
1868: {
1869: if ( a->pos > b->pos ) return 1;
1870: else if ( a->pos < b->pos ) return -1;
1871: else return 0;
1872: }
1873:
1.27 noro 1874: /* order = [w_or_b, w_or_b, ... ] */
1875: /* w_or_b = w or b */
1876: /* w = [1,2,...] or [x,1,y,2,...] */
1877: /* b = [@lex,x,y,...,z] etc */
1878:
1879: int create_composite_order_spec(VL vl,LIST order,struct order_spec **specp)
1880: {
1881: NODE wb,t,p;
1882: struct order_spec *spec;
1883: VL tvl;
1.29 noro 1884: int n,i,j,k,l,start,end,len,w;
1.27 noro 1885: int *dw;
1886: struct sparse_weight *sw;
1887: struct weight_or_block *w_or_b;
1888: Obj a0;
1889: NODE a;
1.29 noro 1890: V v,sv,ev;
1891: SYMBOL sym;
1892: int *top;
1.27 noro 1893:
1894: /* l = number of vars in vl */
1895: for ( l = 0, tvl = vl; tvl; tvl = NEXT(tvl), l++ );
1896: /* n = number of primitives in order */
1897: wb = BDY(order);
1898: n = length(wb);
1899: *specp = spec = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1900: spec->id = 3;
1901: spec->obj = (Obj)order;
1902: spec->nv = l;
1903: spec->ord.composite.length = n;
1.28 noro 1904: w_or_b = spec->ord.composite.w_or_b = (struct weight_or_block *)
1.29 noro 1905: MALLOC(sizeof(struct weight_or_block)*(n+1));
1906:
1907: /* top : register the top variable in each w_or_b specification */
1908: top = (int *)ALLOCA(l*sizeof(int));
1909: for ( i = 0; i < l; i++ ) top[i] = 0;
1910:
1.28 noro 1911: for ( t = wb, i = 0; t; t = NEXT(t), i++ ) {
1.30 noro 1912: if ( !BDY(t) || OID((Obj)BDY(t)) != O_LIST )
1913: error("a list of lists must be specified for the key \"order\"");
1.28 noro 1914: a = BDY((LIST)BDY(t));
1.27 noro 1915: len = length(a);
1916: a0 = (Obj)BDY(a);
1917: if ( !a0 || OID(a0) == O_N ) {
1.28 noro 1918: /* a is a dense weight vector */
1.27 noro 1919: dw = (int *)MALLOC(sizeof(int)*len);
1.30 noro 1920: for ( j = 0, p = a; j < len; p = NEXT(p), j++ ) {
1921: if ( !INT((Q)BDY(p)) )
1922: error("a dense weight vector must be specified as a list of integers");
1.27 noro 1923: dw[j] = QTOS((Q)BDY(p));
1.30 noro 1924: }
1.27 noro 1925: w_or_b[i].type = IS_DENSE_WEIGHT;
1926: w_or_b[i].length = len;
1927: w_or_b[i].body.dense_weight = dw;
1.29 noro 1928:
1929: /* find the top */
1930: for ( k = 0; k < len && !dw[k]; k++ );
1931: if ( k < len ) top[k] = 1;
1932:
1.27 noro 1933: } else if ( OID(a0) == O_P ) {
1.28 noro 1934: /* a is a sparse weight vector */
1935: len >>= 1;
1.27 noro 1936: sw = (struct sparse_weight *)
1937: MALLOC(sizeof(struct sparse_weight)*len);
1938: for ( j = 0, p = a; j < len; j++ ) {
1.30 noro 1939: if ( !BDY(p) || OID((P)BDY(p)) != O_P )
1940: error("a sparse weight vector must be specified as [var1,weight1,...]");
1.28 noro 1941: v = VR((P)BDY(p)); p = NEXT(p);
1.27 noro 1942: for ( tvl = vl, k = 0; tvl && tvl->v != v;
1943: k++, tvl = NEXT(tvl) );
1944: if ( !tvl )
1.30 noro 1945: error("invalid variable name in a sparse weight vector");
1.27 noro 1946: sw[j].pos = k;
1.30 noro 1947: if ( !INT((Q)BDY(p)) )
1948: error("a sparse weight vector must be specified as [var1,weight1,...]");
1.28 noro 1949: sw[j].value = QTOS((Q)BDY(p)); p = NEXT(p);
1.27 noro 1950: }
1.37 noro 1951: qsort(sw,len,sizeof(struct sparse_weight),
1952: (int (*)(const void *,const void *))comp_sw);
1.27 noro 1953: w_or_b[i].type = IS_SPARSE_WEIGHT;
1954: w_or_b[i].length = len;
1955: w_or_b[i].body.sparse_weight = sw;
1.29 noro 1956:
1957: /* find the top */
1958: for ( k = 0; k < len && !sw[k].value; k++ );
1959: if ( k < len ) top[sw[k].pos] = 1;
1960: } else if ( OID(a0) == O_RANGE ) {
1961: /* [range(v1,v2),w] */
1962: sv = VR((P)(((RANGE)a0)->start));
1963: ev = VR((P)(((RANGE)a0)->end));
1964: for ( tvl = vl, start = 0; tvl && tvl->v != sv; start++, tvl = NEXT(tvl) );
1965: if ( !tvl )
1966: error("invalid range");
1967: for ( end = start; tvl && tvl->v != ev; end++, tvl = NEXT(tvl) );
1968: if ( !tvl )
1969: error("invalid range");
1970: len = end-start+1;
1971: sw = (struct sparse_weight *)
1972: MALLOC(sizeof(struct sparse_weight)*len);
1973: w = QTOS((Q)BDY(NEXT(a)));
1974: for ( tvl = vl, k = 0; k < start; k++, tvl = NEXT(tvl) );
1975: for ( j = 0 ; k <= end; k++, tvl = NEXT(tvl), j++ ) {
1976: sw[j].pos = k;
1977: sw[j].value = w;
1978: }
1979: w_or_b[i].type = IS_SPARSE_WEIGHT;
1980: w_or_b[i].length = len;
1981: w_or_b[i].body.sparse_weight = sw;
1982:
1983: /* register the top */
1984: if ( w ) top[start] = 1;
1.28 noro 1985: } else if ( OID(a0) == O_SYMBOL ) {
1986: /* a is a block */
1.29 noro 1987: sym = (SYMBOL)a0; a = NEXT(a); len--;
1988: if ( OID((Obj)BDY(a)) == O_RANGE ) {
1989: sv = VR((P)(((RANGE)BDY(a))->start));
1990: ev = VR((P)(((RANGE)BDY(a))->end));
1991: for ( tvl = vl, start = 0; tvl && tvl->v != sv; start++, tvl = NEXT(tvl) );
1992: if ( !tvl )
1993: error("invalid range");
1994: for ( end = start; tvl && tvl->v != ev; end++, tvl = NEXT(tvl) );
1995: if ( !tvl )
1996: error("invalid range");
1997: len = end-start+1;
1998: } else {
1999: for ( start = 0, tvl = vl; tvl->v != VR((P)BDY(a));
1.28 noro 2000: tvl = NEXT(tvl), start++ );
1.29 noro 2001: for ( p = NEXT(a), tvl = NEXT(tvl); p;
1.30 noro 2002: p = NEXT(p), tvl = NEXT(tvl) ) {
2003: if ( !BDY(p) || OID((P)BDY(p)) != O_P )
2004: error("a block must be specified as [ordsymbol,var1,var2,...]");
1.29 noro 2005: if ( tvl->v != VR((P)BDY(p)) ) break;
1.30 noro 2006: }
1.29 noro 2007: if ( p )
1.30 noro 2008: error("a block must be contiguous in the variable list");
1.29 noro 2009: }
1.28 noro 2010: w_or_b[i].type = IS_BLOCK;
2011: w_or_b[i].length = len;
2012: w_or_b[i].body.block.start = start;
2013: if ( !strcmp(sym->name,"@grlex") )
2014: w_or_b[i].body.block.order = 0;
2015: else if ( !strcmp(sym->name,"@glex") )
2016: w_or_b[i].body.block.order = 1;
2017: else if ( !strcmp(sym->name,"@lex") )
2018: w_or_b[i].body.block.order = 2;
2019: else
1.29 noro 2020: error("invalid ordername");
2021: /* register the tops */
2022: for ( j = 0, k = start; j < len; j++, k++ )
2023: top[k] = 1;
1.28 noro 2024: }
1.29 noro 2025: }
2026: for ( k = 0; k < l && top[k]; k++ );
2027: if ( k < l ) {
2028: /* incomplete order specification; add @grlex */
2029: w_or_b[n].type = IS_BLOCK;
2030: w_or_b[n].length = l;
2031: w_or_b[n].body.block.start = 0;
2032: w_or_b[n].body.block.order = 0;
2033: spec->ord.composite.length = n+1;
1.27 noro 2034: }
2035: }
2036:
1.35 noro 2037: /* module order spec */
2038:
2039: void create_modorder_spec(int id,LIST shift,struct modorder_spec **s)
2040: {
2041: struct modorder_spec *spec;
2042: NODE n,t;
2043: LIST list;
2044: int *ds;
2045: int i,l;
2046: Q q;
2047:
2048: *s = spec = (struct modorder_spec *)MALLOC(sizeof(struct modorder_spec));
2049: spec->id = id;
2050: if ( shift ) {
2051: n = BDY(shift);
2052: spec->len = l = length(n);
2053: spec->degree_shift = ds = (int *)MALLOC_ATOMIC(l*sizeof(int));
2054: for ( t = n, i = 0; t; t = NEXT(t), i++ )
2055: ds[i] = QTOS((Q)BDY(t));
2056: } else {
2057: spec->len = 0;
2058: spec->degree_shift = 0;
2059: }
2060: STOQ(id,q);
2061: n = mknode(2,q,shift);
2062: MKLIST(list,n);
2063: spec->obj = (Obj)list;
2064: }
2065:
1.7 noro 2066: /*
2067: * converters
2068: *
2069: */
2070:
1.20 noro 2071: void dp_homo(DP p,DP *rp)
1.5 noro 2072: {
1.7 noro 2073: MP m,mr,mr0;
2074: int i,n,nv,td;
2075: DL dl,dlh;
1.5 noro 2076:
1.7 noro 2077: if ( !p )
2078: *rp = 0;
2079: else {
2080: n = p->nv; nv = n + 1;
2081: m = BDY(p); td = sugard(m);
2082: for ( mr0 = 0; m; m = NEXT(m) ) {
2083: NEXTMP(mr0,mr); mr->c = m->c;
2084: dl = m->dl;
2085: mr->dl = dlh = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
2086: dlh->td = td;
2087: for ( i = 0; i < n; i++ )
2088: dlh->d[i] = dl->d[i];
2089: dlh->d[n] = td - dl->td;
2090: }
2091: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.5 noro 2092: }
2093: }
2094:
1.20 noro 2095: void dp_dehomo(DP p,DP *rp)
1.5 noro 2096: {
1.7 noro 2097: MP m,mr,mr0;
2098: int i,n,nv;
2099: DL dl,dlh;
1.5 noro 2100:
1.7 noro 2101: if ( !p )
2102: *rp = 0;
2103: else {
2104: n = p->nv; nv = n - 1;
2105: m = BDY(p);
2106: for ( mr0 = 0; m; m = NEXT(m) ) {
2107: NEXTMP(mr0,mr); mr->c = m->c;
2108: dlh = m->dl;
2109: mr->dl = dl = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
2110: dl->td = dlh->td - dlh->d[nv];
2111: for ( i = 0; i < nv; i++ )
2112: dl->d[i] = dlh->d[i];
2113: }
2114: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
2115: }
1.5 noro 2116: }
2117:
1.20 noro 2118: void dp_mod(DP p,int mod,NODE subst,DP *rp)
1.5 noro 2119: {
1.7 noro 2120: MP m,mr,mr0;
2121: P t,s,s1;
2122: V v;
2123: NODE tn;
1.5 noro 2124:
1.7 noro 2125: if ( !p )
2126: *rp = 0;
2127: else {
2128: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
2129: for ( tn = subst, s = m->c; tn; tn = NEXT(tn) ) {
2130: v = VR((P)BDY(tn)); tn = NEXT(tn);
2131: substp(CO,s,v,(P)BDY(tn),&s1); s = s1;
2132: }
2133: ptomp(mod,s,&t);
2134: if ( t ) {
2135: NEXTMP(mr0,mr); mr->c = t; mr->dl = m->dl;
2136: }
2137: }
2138: if ( mr0 ) {
2139: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
2140: } else
2141: *rp = 0;
2142: }
1.5 noro 2143: }
2144:
1.20 noro 2145: void dp_rat(DP p,DP *rp)
1.5 noro 2146: {
1.7 noro 2147: MP m,mr,mr0;
1.5 noro 2148:
1.7 noro 2149: if ( !p )
2150: *rp = 0;
2151: else {
2152: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
2153: NEXTMP(mr0,mr); mptop(m->c,&mr->c); mr->dl = m->dl;
1.5 noro 2154: }
1.7 noro 2155: if ( mr0 ) {
2156: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
2157: } else
2158: *rp = 0;
1.5 noro 2159: }
2160: }
2161:
2162:
1.27 noro 2163: void homogenize_order(struct order_spec *old,int n,struct order_spec **newp)
1.5 noro 2164: {
1.7 noro 2165: struct order_pair *l;
2166: int length,nv,row,i,j;
2167: int **newm,**oldm;
1.27 noro 2168: struct order_spec *new;
1.31 noro 2169: int onv,nnv,nlen,olen,owlen;
2170: struct weight_or_block *owb,*nwb;
1.5 noro 2171:
1.27 noro 2172: *newp = new = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1.7 noro 2173: switch ( old->id ) {
2174: case 0:
2175: switch ( old->ord.simple ) {
2176: case 0:
2177: new->id = 0; new->ord.simple = 0; break;
2178: case 1:
2179: l = (struct order_pair *)
2180: MALLOC_ATOMIC(2*sizeof(struct order_pair));
2181: l[0].length = n; l[0].order = 1;
2182: l[1].length = 1; l[1].order = 2;
2183: new->id = 1;
2184: new->ord.block.order_pair = l;
2185: new->ord.block.length = 2; new->nv = n+1;
2186: break;
2187: case 2:
2188: new->id = 0; new->ord.simple = 1; break;
2189: case 3: case 4: case 5:
2190: new->id = 0; new->ord.simple = old->ord.simple+3;
2191: dp_nelim = n-1; break;
2192: case 6: case 7: case 8: case 9:
2193: new->id = 0; new->ord.simple = old->ord.simple; break;
2194: default:
2195: error("homogenize_order : invalid input");
2196: }
2197: break;
1.50 noro 2198: case 1: case 257:
1.7 noro 2199: length = old->ord.block.length;
2200: l = (struct order_pair *)
2201: MALLOC_ATOMIC((length+1)*sizeof(struct order_pair));
2202: bcopy((char *)old->ord.block.order_pair,(char *)l,length*sizeof(struct order_pair));
2203: l[length].order = 2; l[length].length = 1;
1.50 noro 2204: new->id = old->id; new->nv = n+1;
1.7 noro 2205: new->ord.block.order_pair = l;
2206: new->ord.block.length = length+1;
1.51 noro 2207: new->ispot = old->ispot;
1.7 noro 2208: break;
1.50 noro 2209: case 2: case 258:
1.7 noro 2210: nv = old->nv; row = old->ord.matrix.row;
2211: oldm = old->ord.matrix.matrix; newm = almat(row+1,nv+1);
2212: for ( i = 0; i <= nv; i++ )
2213: newm[0][i] = 1;
2214: for ( i = 0; i < row; i++ ) {
2215: for ( j = 0; j < nv; j++ )
2216: newm[i+1][j] = oldm[i][j];
2217: newm[i+1][j] = 0;
2218: }
1.50 noro 2219: new->id = old->id; new->nv = nv+1;
1.7 noro 2220: new->ord.matrix.row = row+1; new->ord.matrix.matrix = newm;
1.51 noro 2221: new->ispot = old->ispot;
1.31 noro 2222: break;
1.50 noro 2223: case 3: case 259:
1.31 noro 2224: onv = old->nv;
2225: nnv = onv+1;
2226: olen = old->ord.composite.length;
2227: nlen = olen+1;
2228: owb = old->ord.composite.w_or_b;
2229: nwb = (struct weight_or_block *)
2230: MALLOC(nlen*sizeof(struct weight_or_block));
2231: for ( i = 0; i < olen; i++ ) {
2232: nwb[i].type = owb[i].type;
2233: switch ( owb[i].type ) {
2234: case IS_DENSE_WEIGHT:
2235: owlen = owb[i].length;
2236: nwb[i].length = owlen+1;
2237: nwb[i].body.dense_weight = (int *)MALLOC((owlen+1)*sizeof(int));
2238: for ( j = 0; j < owlen; j++ )
2239: nwb[i].body.dense_weight[j] = owb[i].body.dense_weight[j];
2240: nwb[i].body.dense_weight[owlen] = 0;
2241: break;
2242: case IS_SPARSE_WEIGHT:
2243: nwb[i].length = owb[i].length;
2244: nwb[i].body.sparse_weight = owb[i].body.sparse_weight;
2245: break;
2246: case IS_BLOCK:
2247: nwb[i].length = owb[i].length;
2248: nwb[i].body.block = owb[i].body.block;
2249: break;
2250: }
2251: }
2252: nwb[i].type = IS_SPARSE_WEIGHT;
2253: nwb[i].body.sparse_weight =
2254: (struct sparse_weight *)MALLOC(sizeof(struct sparse_weight));
2255: nwb[i].body.sparse_weight[0].pos = onv;
2256: nwb[i].body.sparse_weight[0].value = 1;
1.50 noro 2257: new->id = old->id;
1.31 noro 2258: new->nv = nnv;
2259: new->ord.composite.length = nlen;
2260: new->ord.composite.w_or_b = nwb;
1.51 noro 2261: new->ispot = old->ispot;
1.31 noro 2262: print_composite_order_spec(new);
1.7 noro 2263: break;
1.50 noro 2264: case 256: /* simple module order */
2265: switch ( old->ord.simple ) {
2266: case 0:
2267: new->id = 256; new->ord.simple = 0; break;
2268: case 1:
2269: l = (struct order_pair *)
2270: MALLOC_ATOMIC(2*sizeof(struct order_pair));
2271: l[0].length = n; l[0].order = old->ord.simple;
2272: l[1].length = 1; l[1].order = 2;
2273: new->id = 257;
2274: new->ord.block.order_pair = l;
2275: new->ord.block.length = 2; new->nv = n+1;
2276: break;
2277: case 2:
2278: new->id = 256; new->ord.simple = 1; break;
2279: default:
2280: error("homogenize_order : invalid input");
2281: }
1.51 noro 2282: new->ispot = old->ispot;
1.50 noro 2283: break;
1.7 noro 2284: default:
2285: error("homogenize_order : invalid input");
1.5 noro 2286: }
1.7 noro 2287: }
2288:
1.20 noro 2289: void qltozl(Q *w,int n,Q *dvr)
1.7 noro 2290: {
2291: N nm,dn;
2292: N g,l1,l2,l3;
2293: Q c,d;
2294: int i;
2295: struct oVECT v;
1.5 noro 2296:
2297: for ( i = 0; i < n; i++ )
1.7 noro 2298: if ( w[i] && !INT(w[i]) )
2299: break;
2300: if ( i == n ) {
2301: v.id = O_VECT; v.len = n; v.body = (pointer *)w;
2302: igcdv(&v,dvr); return;
2303: }
2304: c = w[0]; nm = NM(c); dn = INT(c) ? ONEN : DN(c);
2305: for ( i = 1; i < n; i++ ) {
2306: c = w[i]; l1 = INT(c) ? ONEN : DN(c);
2307: gcdn(nm,NM(c),&g); nm = g;
2308: gcdn(dn,l1,&l2); muln(dn,l1,&l3); divsn(l3,l2,&dn);
1.5 noro 2309: }
1.7 noro 2310: if ( UNIN(dn) )
2311: NTOQ(nm,1,d);
2312: else
2313: NDTOQ(nm,dn,1,d);
2314: *dvr = d;
2315: }
1.5 noro 2316:
1.20 noro 2317: int comp_nm(Q *a,Q *b)
1.7 noro 2318: {
2319: return cmpn((*a)?NM(*a):0,(*b)?NM(*b):0);
2320: }
2321:
1.20 noro 2322: void sortbynm(Q *w,int n)
1.7 noro 2323: {
2324: qsort(w,n,sizeof(Q),(int (*)(const void *,const void *))comp_nm);
2325: }
1.5 noro 2326:
2327:
1.7 noro 2328: /*
2329: * simple operations
2330: *
2331: */
1.5 noro 2332:
1.20 noro 2333: int dp_redble(DP p1,DP p2)
1.7 noro 2334: {
2335: int i,n;
2336: DL d1,d2;
1.5 noro 2337:
1.7 noro 2338: d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
2339: if ( d1->td < d2->td )
2340: return 0;
2341: else {
2342: for ( i = 0, n = p1->nv; i < n; i++ )
2343: if ( d1->d[i] < d2->d[i] )
2344: return 0;
2345: return 1;
1.5 noro 2346: }
2347: }
2348:
1.20 noro 2349: void dp_subd(DP p1,DP p2,DP *rp)
1.5 noro 2350: {
1.7 noro 2351: int i,n;
1.5 noro 2352: DL d1,d2,d;
2353: MP m;
1.7 noro 2354: DP s;
1.5 noro 2355:
2356: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
1.7 noro 2357: NEWDL(d,n); d->td = d1->td - d2->td;
1.5 noro 2358: for ( i = 0; i < n; i++ )
1.7 noro 2359: d->d[i] = d1->d[i]-d2->d[i];
2360: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
2361: MKDP(n,m,s); s->sugar = d->td;
2362: *rp = s;
2363: }
2364:
1.20 noro 2365: void dltod(DL d,int n,DP *rp)
1.7 noro 2366: {
2367: MP m;
2368: DP s;
2369:
2370: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
2371: MKDP(n,m,s); s->sugar = d->td;
2372: *rp = s;
1.5 noro 2373: }
2374:
1.20 noro 2375: void dp_hm(DP p,DP *rp)
1.5 noro 2376: {
2377: MP m,mr;
2378:
2379: if ( !p )
2380: *rp = 0;
2381: else {
2382: m = BDY(p);
2383: NEWMP(mr); mr->dl = m->dl; mr->c = m->c; NEXT(mr) = 0;
2384: MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td; /* XXX */
2385: }
2386: }
2387:
1.35 noro 2388: void dp_ht(DP p,DP *rp)
2389: {
2390: MP m,mr;
2391:
2392: if ( !p )
2393: *rp = 0;
2394: else {
2395: m = BDY(p);
2396: NEWMP(mr); mr->dl = m->dl; mr->c = (P)ONE; NEXT(mr) = 0;
2397: MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td; /* XXX */
2398: }
2399: }
2400:
1.20 noro 2401: void dp_rest(DP p,DP *rp)
1.5 noro 2402: {
2403: MP m;
2404:
2405: m = BDY(p);
2406: if ( !NEXT(m) )
2407: *rp = 0;
2408: else {
2409: MKDP(p->nv,NEXT(m),*rp);
2410: if ( *rp )
2411: (*rp)->sugar = p->sugar;
2412: }
2413: }
2414:
1.20 noro 2415: DL lcm_of_DL(int nv,DL dl1,DL dl2,DL dl)
1.5 noro 2416: {
1.21 noro 2417: register int i, *d1, *d2, *d, td;
1.5 noro 2418:
2419: if ( !dl ) NEWDL(dl,nv);
2420: d = dl->d, d1 = dl1->d, d2 = dl2->d;
1.21 noro 2421: for ( td = 0, i = 0; i < nv; d1++, d2++, d++, i++ ) {
2422: *d = *d1 > *d2 ? *d1 : *d2;
2423: td += MUL_WEIGHT(*d,i);
2424: }
1.5 noro 2425: dl->td = td;
2426: return dl;
2427: }
2428:
1.20 noro 2429: int dl_equal(int nv,DL dl1,DL dl2)
1.5 noro 2430: {
2431: register int *d1, *d2, n;
2432:
2433: if ( dl1->td != dl2->td ) return 0;
2434: for ( d1 = dl1->d, d2 = dl2->d, n = nv; --n >= 0; d1++, d2++ )
2435: if ( *d1 != *d2 ) return 0;
2436: return 1;
2437: }
2438:
1.20 noro 2439: int dp_nt(DP p)
1.5 noro 2440: {
2441: int i;
2442: MP m;
2443:
2444: if ( !p )
2445: return 0;
2446: else {
2447: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
2448: return i;
2449: }
2450: }
2451:
1.20 noro 2452: int dp_homogeneous(DP p)
1.15 noro 2453: {
2454: MP m;
2455: int d;
2456:
2457: if ( !p )
2458: return 1;
2459: else {
2460: m = BDY(p);
2461: d = m->dl->td;
2462: m = NEXT(m);
2463: for ( ; m; m = NEXT(m) ) {
2464: if ( m->dl->td != d )
2465: return 0;
2466: }
2467: return 1;
2468: }
1.16 noro 2469: }
2470:
1.20 noro 2471: void _print_mp(int nv,MP m)
1.16 noro 2472: {
2473: int i;
2474:
1.17 noro 2475: if ( !m )
1.16 noro 2476: return;
2477: for ( ; m; m = NEXT(m) ) {
2478: fprintf(stderr,"%d<",ITOS(C(m)));
2479: for ( i = 0; i < nv; i++ ) {
2480: fprintf(stderr,"%d",m->dl->d[i]);
2481: if ( i != nv-1 )
2482: fprintf(stderr," ");
2483: }
2484: fprintf(stderr,">",C(m));
2485: }
2486: fprintf(stderr,"\n");
1.15 noro 2487: }
1.26 noro 2488:
2489: static int cmp_mp_nvar;
2490:
2491: int comp_mp(MP *a,MP *b)
2492: {
2493: return -(*cmpdl)(cmp_mp_nvar,(*a)->dl,(*b)->dl);
2494: }
2495:
2496: void dp_sort(DP p,DP *rp)
2497: {
2498: MP t,mp,mp0;
2499: int i,n;
2500: DP r;
2501: MP *w;
2502:
2503: if ( !p ) {
2504: *rp = 0;
2505: return;
2506: }
2507: for ( t = BDY(p), n = 0; t; t = NEXT(t), n++ );
2508: w = (MP *)ALLOCA(n*sizeof(MP));
2509: for ( t = BDY(p), i = 0; i < n; t = NEXT(t), i++ )
2510: w[i] = t;
2511: cmp_mp_nvar = NV(p);
2512: qsort(w,n,sizeof(MP),(int (*)(const void *,const void *))comp_mp);
2513: mp0 = 0;
2514: for ( i = n-1; i >= 0; i-- ) {
2515: NEWMP(mp); mp->dl = w[i]->dl; C(mp) = C(w[i]);
2516: NEXT(mp) = mp0; mp0 = mp;
2517: }
2518: MKDP(p->nv,mp0,r);
2519: r->sugar = p->sugar;
2520: *rp = r;
2521: }
2522:
1.32 noro 2523: DP extract_initial_term_from_dp(DP p,int *weight,int n);
2524: LIST extract_initial_term(LIST f,int *weight,int n);
2525:
2526: DP extract_initial_term_from_dp(DP p,int *weight,int n)
2527: {
1.34 noro 2528: int w,t,i,top;
1.32 noro 2529: MP m,r0,r;
2530: DP dp;
2531:
2532: if ( !p ) return 0;
1.34 noro 2533: top = 1;
1.32 noro 2534: for ( m = BDY(p); m; m = NEXT(m) ) {
2535: for ( i = 0, t = 0; i < n; i++ )
2536: t += weight[i]*m->dl->d[i];
1.34 noro 2537: if ( top || t > w ) {
1.32 noro 2538: r0 = 0;
2539: w = t;
1.34 noro 2540: top = 0;
1.32 noro 2541: }
2542: if ( t == w ) {
2543: NEXTMP(r0,r);
2544: r->dl = m->dl;
2545: r->c = m->c;
2546: }
2547: }
2548: NEXT(r) = 0;
2549: MKDP(p->nv,r0,dp);
2550: return dp;
2551: }
2552:
2553: LIST extract_initial_term(LIST f,int *weight,int n)
2554: {
2555: NODE nd,r0,r;
2556: Obj p;
2557: LIST l;
2558:
2559: nd = BDY(f);
2560: for ( r0 = 0; nd; nd = NEXT(nd) ) {
2561: NEXTNODE(r0,r);
2562: p = (Obj)BDY(nd);
2563: BDY(r) = (pointer)extract_initial_term_from_dp((DP)p,weight,n);
2564: }
2565: if ( r0 ) NEXT(r) = 0;
2566: MKLIST(l,r0);
2567: return l;
2568: }
2569:
2570: LIST dp_initial_term(LIST f,struct order_spec *ord)
2571: {
2572: int n,l,i;
2573: struct weight_or_block *worb;
2574: int *weight;
2575:
2576: switch ( ord->id ) {
2577: case 2: /* matrix order */
2578: /* extract the first row */
2579: n = ord->nv;
2580: weight = ord->ord.matrix.matrix[0];
2581: return extract_initial_term(f,weight,n);
2582: case 3: /* composite order */
2583: /* the first w_or_b */
2584: worb = ord->ord.composite.w_or_b;
2585: switch ( worb->type ) {
2586: case IS_DENSE_WEIGHT:
2587: n = worb->length;
2588: weight = worb->body.dense_weight;
2589: return extract_initial_term(f,weight,n);
2590: case IS_SPARSE_WEIGHT:
2591: n = ord->nv;
2592: weight = (int *)ALLOCA(n*sizeof(int));
1.33 noro 2593: for ( i = 0; i < n; i++ ) weight[i] = 0;
1.32 noro 2594: l = worb->length;
2595: for ( i = 0; i < l; i++ )
2596: weight[worb->body.sparse_weight[i].pos]
2597: = worb->body.sparse_weight[i].value;
2598: return extract_initial_term(f,weight,n);
2599: default:
2600: error("dp_initial_term : unsupported order");
2601: }
2602: default:
2603: error("dp_initial_term : unsupported order");
2604: }
2605: }
2606:
2607: int highest_order_dp(DP p,int *weight,int n);
2608: LIST highest_order(LIST f,int *weight,int n);
2609:
2610: int highest_order_dp(DP p,int *weight,int n)
2611: {
1.34 noro 2612: int w,t,i,top;
1.32 noro 2613: MP m;
2614:
2615: if ( !p ) return -1;
1.34 noro 2616: top = 1;
1.32 noro 2617: for ( m = BDY(p); m; m = NEXT(m) ) {
2618: for ( i = 0, t = 0; i < n; i++ )
2619: t += weight[i]*m->dl->d[i];
1.34 noro 2620: if ( top || t > w ) {
1.32 noro 2621: w = t;
1.34 noro 2622: top = 0;
2623: }
1.32 noro 2624: }
2625: return w;
2626: }
2627:
2628: LIST highest_order(LIST f,int *weight,int n)
2629: {
2630: int h;
2631: NODE nd,r0,r;
2632: Obj p;
2633: LIST l;
2634: Q q;
2635:
2636: nd = BDY(f);
2637: for ( r0 = 0; nd; nd = NEXT(nd) ) {
2638: NEXTNODE(r0,r);
2639: p = (Obj)BDY(nd);
2640: h = highest_order_dp((DP)p,weight,n);
2641: STOQ(h,q);
2642: BDY(r) = (pointer)q;
2643: }
2644: if ( r0 ) NEXT(r) = 0;
2645: MKLIST(l,r0);
2646: return l;
2647: }
2648:
2649: LIST dp_order(LIST f,struct order_spec *ord)
2650: {
2651: int n,l,i;
2652: struct weight_or_block *worb;
2653: int *weight;
2654:
2655: switch ( ord->id ) {
2656: case 2: /* matrix order */
2657: /* extract the first row */
2658: n = ord->nv;
2659: weight = ord->ord.matrix.matrix[0];
2660: return highest_order(f,weight,n);
2661: case 3: /* composite order */
2662: /* the first w_or_b */
2663: worb = ord->ord.composite.w_or_b;
2664: switch ( worb->type ) {
2665: case IS_DENSE_WEIGHT:
2666: n = worb->length;
2667: weight = worb->body.dense_weight;
2668: return highest_order(f,weight,n);
2669: case IS_SPARSE_WEIGHT:
2670: n = ord->nv;
2671: weight = (int *)ALLOCA(n*sizeof(int));
1.33 noro 2672: for ( i = 0; i < n; i++ ) weight[i] = 0;
1.32 noro 2673: l = worb->length;
2674: for ( i = 0; i < l; i++ )
2675: weight[worb->body.sparse_weight[i].pos]
2676: = worb->body.sparse_weight[i].value;
2677: return highest_order(f,weight,n);
2678: default:
2679: error("dp_initial_term : unsupported order");
2680: }
2681: default:
2682: error("dp_initial_term : unsupported order");
1.35 noro 2683: }
2684: }
2685:
2686: int dpv_ht(DPV p,DP *h)
2687: {
2688: int len,max,maxi,i,t;
2689: DP *e;
2690: MP m,mr;
2691:
2692: len = p->len;
2693: e = p->body;
2694: max = -1;
2695: maxi = -1;
2696: for ( i = 0; i < len; i++ )
2697: if ( e[i] && (t = BDY(e[i])->dl->td) > max ) {
2698: max = t;
2699: maxi = i;
2700: }
2701: if ( max < 0 ) {
2702: *h = 0;
2703: return -1;
2704: } else {
2705: m = BDY(e[maxi]);
2706: NEWMP(mr); mr->dl = m->dl; mr->c = (P)ONE; NEXT(mr) = 0;
2707: MKDP(e[maxi]->nv,mr,*h); (*h)->sugar = mr->dl->td; /* XXX */
2708: return maxi;
1.32 noro 2709: }
2710: }
1.42 noro 2711:
2712: /* return 1 if 0 <_w1 v && v <_w2 0 */
2713:
2714: int in_c12(int n,int *v,int row1,int **w1,int row2, int **w2)
2715: {
2716: int t1,t2;
2717:
2718: t1 = compare_zero(n,v,row1,w1);
2719: t2 = compare_zero(n,v,row2,w2);
2720: if ( t1 > 0 && t2 < 0 ) return 1;
2721: else return 0;
2722: }
2723:
2724: /* 0 < u => 1, 0 > u => -1 */
2725:
2726: int compare_zero(int n,int *u,int row,int **w)
2727: {
2728: int i,j,t;
2729: int *wi;
2730:
2731: for ( i = 0; i < row; i++ ) {
2732: wi = w[i];
2733: for ( j = 0, t = 0; j < n; j++ ) t += u[j]*wi[j];
2734: if ( t > 0 ) return 1;
2735: else if ( t < 0 ) return -1;
2736: }
2737: return 0;
2738: }
2739:
2740: /* functions for generic groebner walk */
2741: /* u=0 means u=-infty */
2742:
2743: int compare_facet_preorder(int n,int *u,int *v,
2744: int row1,int **w1,int row2,int **w2)
2745: {
2746: int i,j,s,t,tu,tv;
2747: int *w2i,*uv;
2748:
2749: if ( !u ) return 1;
2750: uv = W_ALLOC(n);
2751: for ( i = 0; i < row2; i++ ) {
2752: w2i = w2[i];
2753: for ( j = 0, tu = tv = 0; j < n; j++ )
2754: if ( s = w2i[j] ) {
2755: tu += s*u[j]; tv += s*v[j];
2756: }
2757: for ( j = 0; j < n; j++ ) uv[j] = u[j]*tv-v[j]*tu;
2758: t = compare_zero(n,uv,row1,w1);
2759: if ( t > 0 ) return 1;
2760: else if ( t < 0 ) return 0;
2761: }
2762: return 1;
2763: }
2764:
1.48 noro 2765: Q inner_product_with_small_vector(VECT w,int *v)
2766: {
2767: int n,i;
2768: Q q,s,t,u;
2769:
2770: n = w->len;
2771: s = 0;
2772: for ( i = 0; i < n; i++ ) {
2773: STOQ(v[i],q); mulq((Q)w->body[i],q,&t); addq(t,s,&u); s = u;
2774: }
2775: return s;
2776: }
2777:
2778: Q compute_last_t(NODE g,NODE gh,Q t,VECT w1,VECT w2,NODE *homo,VECT *wp)
2779: {
2780: int n,i;
2781: int *wt;
2782: Q last,d1,d2,dn,nm,s,t1;
2783: VECT wd,wt1,wt2,w;
2784: NODE tg,tgh;
2785: MP f;
2786: int *h;
2787: NODE r0,r;
2788: MP m0,m;
2789: DP d;
2790:
2791: n = w1->len;
2792: wt = W_ALLOC(n);
2793: last = ONE;
2794: /* t1 = 1-t */
2795: for ( tg = g, tgh = gh; tg; tg = NEXT(tg), tgh = NEXT(tgh ) ) {
2796: f = BDY((DP)BDY(tg));
2797: h = BDY((DP)BDY(tgh))->dl->d;
2798: for ( ; f; f = NEXT(f) ) {
2799: for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
2800: for ( i = 0; i < n && !wt[i]; i++ );
2801: if ( i == n ) continue;
2802: d1 = inner_product_with_small_vector(w1,wt);
2803: d2 = inner_product_with_small_vector(w2,wt);
2804: nm = d1; subq(d1,d2,&dn);
2805: /* if d1=d2 then nothing happens */
2806: if ( !dn ) continue;
2807: /* s satisfies ds = 0*/
2808: divq(nm,dn,&s);
2809:
2810: if ( cmpq(s,t) > 0 && cmpq(s,last) < 0 )
2811: last = s;
2812: else if ( !cmpq(s,t) ) {
2813: if ( cmpq(d2,0) < 0 ) {
2814: last = t;
2815: break;
2816: }
2817: }
2818: }
2819: }
2820: if ( !last ) {
2821: dn = ONE; nm = 0;
2822: } else {
2823: NTOQ(NM(last),1,nm);
2824: if ( INT(last) ) dn = ONE;
2825: else {
2826: NTOQ(DN(last),1,dn);
2827: }
2828: }
2829: /* (1-n/d)*w1+n/d*w2 -> w=(d-n)*w1+n*w2 */
2830: subq(dn,nm,&t1); mulvect(CO,(Obj)w1,(Obj)t1,(Obj *)&wt1);
2831: mulvect(CO,(Obj)w2,(Obj)nm,(Obj *)&wt2); addvect(CO,wt1,wt2,&w);
2832:
2833: r0 = 0;
2834: for ( tg = g, tgh = gh; tg; tg = NEXT(tg), tgh = NEXT(tgh ) ) {
2835: f = BDY((DP)BDY(tg));
2836: h = BDY((DP)BDY(tgh))->dl->d;
2837: for ( m0 = 0; f; f = NEXT(f) ) {
2838: for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
2839: for ( i = 0; i < n && !wt[i]; i++ );
2840: if ( !inner_product_with_small_vector(w,wt) ) {
2841: NEXTMP(m0,m); m->c = f->c; m->dl = f->dl;
2842: }
2843: }
2844: NEXT(m) = 0;
2845: MKDP(((DP)BDY(tg))->nv,m0,d); d->sugar = ((DP)BDY(tg))->sugar;
2846: NEXTNODE(r0,r); BDY(r) = (pointer)d;
2847: }
2848: NEXT(r) = 0;
2849: *homo = r0;
2850: *wp = w;
2851: return last;
2852: }
2853:
1.42 noro 2854: /* return 0 if last_w = infty */
2855:
2856: NODE compute_last_w(NODE g,NODE gh,int n,int **w,
2857: int row1,int **w1,int row2,int **w2)
2858: {
2859: DP d;
2860: MP f,m0,m;
2861: int *wt,*v,*h;
2862: NODE t,s,n0,tn,n1,r0,r;
2863: int i;
2864:
2865: wt = W_ALLOC(n);
2866: n0 = 0;
2867: for ( t = g, s = gh; t; t = NEXT(t), s = NEXT(s) ) {
2868: f = BDY((DP)BDY(t));
2869: h = BDY((DP)BDY(s))->dl->d;
2870: for ( ; f; f = NEXT(f) ) {
2871: for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
2872: for ( i = 0; i < n && !wt[i]; i++ );
2873: if ( i == n ) continue;
2874:
2875: if ( in_c12(n,wt,row1,w1,row2,w2) &&
2876: compare_facet_preorder(n,*w,wt,row1,w1,row2,w2) ) {
2877: v = (int *)MALLOC_ATOMIC(n*sizeof(int));
2878: for ( i = 0; i < n; i++ ) v[i] = wt[i];
2879: MKNODE(n1,v,n0); n0 = n1;
2880: }
2881: }
2882: }
2883: if ( !n0 ) return 0;
2884: for ( t = n0; t; t = NEXT(t) ) {
2885: v = (int *)BDY(t);
2886: for ( s = n0; s; s = NEXT(s) )
2887: if ( !compare_facet_preorder(n,v,(int *)BDY(s),row1,w1,row2,w2) )
2888: break;
2889: if ( !s ) {
2890: *w = v;
2891: break;
2892: }
2893: }
2894: if ( !t )
2895: error("compute_last_w : cannot happen");
2896: r0 = 0;
2897: for ( t = g, s = gh; t; t = NEXT(t), s = NEXT(s) ) {
2898: f = BDY((DP)BDY(t));
2899: h = BDY((DP)BDY(s))->dl->d;
2900: for ( m0 = 0; f; f = NEXT(f) ) {
2901: for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
2902: for ( i = 0; i < n && !wt[i]; i++ );
2903: if ( i == n ||
2904: (compare_facet_preorder(n,wt,*w,row1,w1,row2,w2)
2905: && compare_facet_preorder(n,*w,wt,row1,w1,row2,w2)) ) {
2906: NEXTMP(m0,m); m->c = f->c; m->dl = f->dl;
2907: }
2908: }
1.43 noro 2909: NEXT(m) = 0;
1.42 noro 2910: MKDP(((DP)BDY(t))->nv,m0,d); d->sugar = ((DP)BDY(t))->sugar;
2911: NEXTNODE(r0,r); BDY(r) = (pointer)d;
2912: }
2913: NEXT(r) = 0;
2914: return r0;
2915: }
1.44 noro 2916:
2917: /* compute a sufficient set of d(f)=u-v */
2918:
2919: NODE compute_essential_df(DP *g,DP *gh,int ng)
2920: {
1.45 noro 2921: int nv,i,j,k,t,lj;
2922: NODE r,r1,ri,rt,r0;
2923: MP m;
2924: MP *mj;
2925: DL di,hj,dl,dlt;
2926: int *d,*dt;
2927: LIST l;
1.44 noro 2928: Q q;
1.45 noro 2929:
2930: nv = g[0]->nv;
2931: r = 0;
2932: for ( j = 0; j < ng; j++ ) {
2933: for ( m = BDY(g[j]), lj = 0; m; m = NEXT(m), lj++ );
2934: mj = (MP *)ALLOCA(lj*sizeof(MP));
2935: for ( m = BDY(g[j]), k = 0; m; m = NEXT(m), k++ )
2936: mj[k] = m;
2937: for ( i = 0; i < lj; i++ ) {
2938: for ( di = mj[i]->dl, k = i+1; k < lj; k++ )
2939: if ( _dl_redble(di,mj[k]->dl,nv) ) break;
2940: if ( k < lj ) mj[i] = 0;
2941: }
2942: hj = BDY(gh[j])->dl;
2943: _NEWDL(dl,nv); d = dl->d;
2944: r0 = r;
2945: for ( i = 0; i < lj; i++ ) {
2946: if ( mj[i] && !dl_equal(nv,di=mj[i]->dl,hj) ) {
2947: for ( k = 0, t = 0; k < nv; k++ ) {
2948: d[k] = hj->d[k]-di->d[k];
2949: t += d[k];
2950: }
2951: dl->td = t;
2952: #if 1
2953: for ( rt = r0; rt; rt = NEXT(rt) ) {
2954: dlt = (DL)BDY(rt);
2955: if ( dlt->td != dl->td ) continue;
2956: for ( dt = dlt->d, k = 0; k < nv; k++ )
2957: if ( d[k] != dt[k] ) break;
2958: if ( k == nv ) break;
2959: }
2960: #else
2961: rt = 0;
2962: #endif
2963: if ( !rt ) {
2964: MKNODE(r1,dl,r); r = r1;
2965: _NEWDL(dl,nv); d = dl->d;
2966: }
1.44 noro 2967: }
2968: }
2969: }
1.45 noro 2970: for ( rt = r; rt; rt = NEXT(rt) ) {
2971: dl = (DL)BDY(rt); d = dl->d;
2972: ri = 0;
2973: for ( k = nv-1; k >= 0; k-- ) {
2974: STOQ(d[k],q);
2975: MKNODE(r1,q,ri); ri = r1;
1.44 noro 2976: }
1.45 noro 2977: MKNODE(r1,0,ri); MKLIST(l,r1);
2978: BDY(rt) = (pointer)l;
1.44 noro 2979: }
2980: return r;
2981: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>