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