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