Annotation of OpenXM_contrib2/asir2000/builtin/dp-supp.c, Revision 1.31
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.31 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/dp-supp.c,v 1.30 2004/03/05 02:26:52 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:
65: /*
66: * content reduction
67: *
68: */
69:
1.20 noro 70: void dp_ptozp(DP p,DP *rp)
1.7 noro 71: {
72: MP m,mr,mr0;
73: int i,n;
74: Q *w;
75: Q dvr;
76: P t;
77:
78: if ( !p )
79: *rp = 0;
80: else {
81: for ( m =BDY(p), n = 0; m; m = NEXT(m), n++ );
82: w = (Q *)ALLOCA(n*sizeof(Q));
83: for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
84: if ( NUM(m->c) )
85: w[i] = (Q)m->c;
86: else
87: ptozp(m->c,1,&w[i],&t);
88: sortbynm(w,n);
89: qltozl(w,n,&dvr);
90: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
91: NEXTMP(mr0,mr); divsp(CO,m->c,(P)dvr,&mr->c); mr->dl = m->dl;
92: }
93: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
94: }
95: }
96:
1.20 noro 97: void dp_ptozp2(DP p0,DP p1,DP *hp,DP *rp)
1.7 noro 98: {
99: DP t,s,h,r;
100: MP m,mr,mr0,m0;
101:
102: addd(CO,p0,p1,&t); dp_ptozp(t,&s);
103: if ( !p0 ) {
104: h = 0; r = s;
105: } else if ( !p1 ) {
106: h = s; r = 0;
107: } else {
108: for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
109: m = NEXT(m), m0 = NEXT(m0) ) {
110: NEXTMP(mr0,mr); mr->c = m->c; mr->dl = m->dl;
111: }
112: NEXT(mr) = 0; MKDP(p0->nv,mr0,h); MKDP(p0->nv,m,r);
113: }
114: if ( h )
115: h->sugar = p0->sugar;
116: if ( r )
117: r->sugar = p1->sugar;
118: *hp = h; *rp = r;
119: }
1.1 noro 120:
1.20 noro 121: void dp_idiv(DP p,Q c,DP *rp)
1.1 noro 122: {
123: Q t;
124: N nm,q;
125: int sgn,s;
126: MP mr0,m,mr;
127:
128: if ( !p )
129: *rp = 0;
130: else if ( MUNIQ((Q)c) )
131: *rp = p;
132: else if ( MUNIQ((Q)c) )
133: chsgnd(p,rp);
134: else {
135: nm = NM(c); sgn = SGN(c);
136: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
137: NEXTMP(mr0,mr);
138:
139: divsn(NM((Q)(m->c)),nm,&q);
140: s = sgn*SGN((Q)(m->c));
141: NTOQ(q,s,t);
142: mr->c = (P)t;
143: mr->dl = m->dl;
144: }
145: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp);
146: if ( *rp )
147: (*rp)->sugar = p->sugar;
148: }
149: }
150:
1.20 noro 151: void dp_mbase(NODE hlist,NODE *mbase)
1.1 noro 152: {
153: DL *dl;
154: DL d;
155: int i,j,n,nvar,td;
156:
157: n = length(hlist); nvar = ((DP)BDY(hlist))->nv;
158: dl = (DL *)MALLOC(n*sizeof(DL));
159: for ( i = 0; i < n; i++, hlist = NEXT(hlist) )
160: dl[i] = BDY((DP)BDY(hlist))->dl;
161: NEWDL(d,nvar); *mbase = 0;
162: while ( 1 ) {
163: insert_to_node(d,mbase,nvar);
164: for ( i = nvar-1; i >= 0; ) {
1.21 noro 165: d->d[i]++;
166: d->td += MUL_WEIGHT(1,i);
1.1 noro 167: for ( j = 0; j < n; j++ ) {
168: if ( _dl_redble(dl[j],d,nvar) )
169: break;
170: }
171: if ( j < n ) {
172: for ( j = nvar-1; j >= i; j-- )
173: d->d[j] = 0;
174: for ( j = 0, td = 0; j < i; j++ )
1.21 noro 175: td += MUL_WEIGHT(d->d[j],j);
1.1 noro 176: d->td = td;
177: i--;
178: } else
179: break;
180: }
181: if ( i < 0 )
182: break;
183: }
184: }
185:
1.20 noro 186: int _dl_redble(DL d1,DL d2,int nvar)
1.1 noro 187: {
188: int i;
189:
190: if ( d1->td > d2->td )
191: return 0;
192: for ( i = 0; i < nvar; i++ )
193: if ( d1->d[i] > d2->d[i] )
194: break;
195: if ( i < nvar )
196: return 0;
197: else
198: return 1;
199: }
200:
1.20 noro 201: void insert_to_node(DL d,NODE *n,int nvar)
1.1 noro 202: {
203: DL d1;
204: MP m;
205: DP dp;
206: NODE n0,n1,n2;
207:
208: NEWDL(d1,nvar); d1->td = d->td;
209: bcopy((char *)d->d,(char *)d1->d,nvar*sizeof(int));
210: NEWMP(m); m->dl = d1; m->c = (P)ONE; NEXT(m) = 0;
211: MKDP(nvar,m,dp); dp->sugar = d->td;
212: if ( !(*n) ) {
213: MKNODE(n1,dp,0); *n = n1;
214: } else {
215: for ( n1 = *n, n0 = 0; n1; n0 = n1, n1 = NEXT(n1) )
216: if ( (*cmpdl)(nvar,d,BDY((DP)BDY(n1))->dl) > 0 ) {
217: MKNODE(n2,dp,n1);
218: if ( !n0 )
219: *n = n2;
220: else
221: NEXT(n0) = n2;
222: break;
223: }
224: if ( !n1 ) {
225: MKNODE(n2,dp,0); NEXT(n0) = n2;
226: }
227: }
228: }
229:
1.20 noro 230: void dp_vtod(Q *c,DP p,DP *rp)
1.1 noro 231: {
232: MP mr0,m,mr;
233: int i;
234:
235: if ( !p )
236: *rp = 0;
237: else {
238: for ( mr0 = 0, m = BDY(p), i = 0; m; m = NEXT(m), i++ ) {
239: NEXTMP(mr0,mr); mr->c = (P)c[i]; mr->dl = m->dl;
240: }
241: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp);
242: (*rp)->sugar = p->sugar;
243: }
244: }
245:
1.8 noro 246: extern int mpi_mag;
247: extern int PCoeffs;
248:
1.20 noro 249: void dp_ptozp_d(DP p,DP *rp)
1.1 noro 250: {
251: int i,j,k,l,n,nsep;
252: MP m;
253: NODE tn,n0,n1,n2,n3;
254: struct oVECT v;
255: VECT c,cs;
256: VECT qi,ri;
257: LIST *qr;
258: Obj dmy;
259: Q d0,d1,gcd,a,u,u1;
260: Q *q,*r;
261: STRING iqr_v;
262: pointer *b;
263: N qn,gn;
264: double get_rtime();
265: int blen;
1.8 noro 266: NODE dist;
267: int ndist;
1.1 noro 268: double t0;
269: double t_e,t_d,t_d1,t_c;
1.8 noro 270: extern int DP_NFStat;
271: extern LIST Dist;
1.20 noro 272: void Pox_rpc();
273: void Pox_pop_local();
1.1 noro 274:
275: if ( !p )
276: *rp = 0;
277: else {
1.8 noro 278: if ( PCoeffs ) {
279: dp_ptozp(p,rp); return;
280: }
1.9 noro 281: if ( !Dist || p_mag(BDY(p)->c) <= mpi_mag ) {
1.8 noro 282: dist = 0; ndist = 0;
283: if ( DP_NFStat ) fprintf(asir_out,"L");
284: } else {
285: dist = BDY(Dist); ndist = length(dist);
286: if ( DP_NFStat ) fprintf(asir_out,"D");
287: }
1.1 noro 288: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
289: nsep = ndist + 1;
290: if ( n <= nsep ) {
291: dp_ptozp(p,rp); return;
292: }
293: t0 = get_rtime();
294: dp_dtov(p,&c);
295: igcdv_estimate(c,&d0);
296: t_e = get_rtime()-t0;
297: t0 = get_rtime();
298: dp_dtov(p,&c);
299: sepvect(c,nsep,&cs);
300: MKSTR(iqr_v,"iqr");
301: qr = (LIST *)CALLOC(nsep,sizeof(LIST));
302: q = (Q *)CALLOC(n,sizeof(Q));
303: r = (Q *)CALLOC(n,sizeof(Q));
304: for ( i = 0, tn = dist, b = BDY(cs); i < ndist; i++, tn = NEXT(tn) ) {
305: MKNODE(n3,d0,0); MKNODE(n2,b[i],n3);
306: MKNODE(n1,iqr_v,n2); MKNODE(n0,BDY(tn),n1);
307: Pox_rpc(n0,&dmy);
308: }
309: iqrv(b[i],d0,&qr[i]);
310: dp_dtov(p,&c);
311: for ( i = 0, tn = dist; i < ndist; i++, tn = NEXT(tn) ) {
312: Pox_pop_local(tn,&qr[i]);
313: if ( OID(qr[i]) == O_ERR ) {
314: printexpr(CO,(Obj)qr[i]);
315: error("dp_ptozp_d : aborted");
316: }
317: }
318: t_d = get_rtime()-t0;
319: t_d1 = t_d/n;
320: t0 = get_rtime();
321: for ( i = j = 0; i < nsep; i++ ) {
322: tn = BDY(qr[i]); qi = (VECT)BDY(tn); ri = (VECT)BDY(NEXT(tn));
323: for ( k = 0, l = qi->len; k < l; k++, j++ ) {
324: q[j] = (Q)BDY(qi)[k]; r[j] = (Q)BDY(ri)[k];
325: }
326: }
327: v.id = O_VECT; v.len = n; v.body = (pointer *)r; igcdv(&v,&d1);
328: if ( d1 ) {
329: gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
330: divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
331: for ( i = 0; i < n; i++ ) {
332: mulq(a,q[i],&u);
333: if ( r[i] ) {
334: divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
335: addq(u,u1,&q[i]);
336: } else
337: q[i] = u;
338: }
339: } else
340: gcd = d0;
341: dp_vtod(q,p,rp);
342: t_c = get_rtime()-t0;
343: blen=p_mag((P)gcd);
344: pz_t_e += t_e; pz_t_d += t_d; pz_t_d1 += t_d1; pz_t_c += t_c;
345: if ( 0 )
346: fprintf(stderr,"(%d,%d)",p_mag((P)d0)-blen,blen);
347: }
348: }
349:
1.20 noro 350: void dp_ptozp2_d(DP p0,DP p1,DP *hp,DP *rp)
1.1 noro 351: {
352: DP t,s,h,r;
353: MP m,mr,mr0,m0;
354:
1.8 noro 355: addd(CO,p0,p1,&t); dp_ptozp_d(t,&s);
1.1 noro 356: if ( !p0 ) {
357: h = 0; r = s;
358: } else if ( !p1 ) {
359: h = s; r = 0;
360: } else {
361: for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
362: m = NEXT(m), m0 = NEXT(m0) ) {
363: NEXTMP(mr0,mr); mr->c = m->c; mr->dl = m->dl;
364: }
365: NEXT(mr) = 0; MKDP(p0->nv,mr0,h); MKDP(p0->nv,m,r);
366: }
367: if ( h )
368: h->sugar = p0->sugar;
369: if ( r )
370: r->sugar = p1->sugar;
371: *hp = h; *rp = r;
1.5 noro 372: }
373:
1.22 noro 374: int have_sf_coef(P p)
375: {
376: DCP dc;
377:
378: if ( !p )
379: return 0;
380: else if ( NUM(p) )
381: return NID((Num)p) == N_GFS ? 1 : 0;
382: else {
383: for ( dc = DC(p); dc; dc = NEXT(dc) )
384: if ( have_sf_coef(COEF(dc)) )
385: return 1;
386: return 0;
387: }
388: }
389:
1.25 noro 390: void head_coef(P p,Num *c)
391: {
392: if ( !p )
393: *c = 0;
394: else if ( NUM(p) )
395: *c = (Num)p;
396: else
397: head_coef(COEF(DC(p)),c);
398: }
399:
400: void dp_monic_sf(DP p,DP *rp)
401: {
402: Num c;
403:
404: if ( !p )
405: *rp = 0;
406: else {
407: head_coef(BDY(p)->c,&c);
408: divsdc(CO,p,(P)c,rp);
409: }
410: }
411:
1.20 noro 412: void dp_prim(DP p,DP *rp)
1.5 noro 413: {
1.7 noro 414: P t,g;
415: DP p1;
416: MP m,mr,mr0;
417: int i,n;
418: P *w;
419: Q *c;
420: Q dvr;
1.5 noro 421:
1.7 noro 422: if ( !p )
423: *rp = 0;
1.23 noro 424: else if ( dp_fcoeffs == N_GFS ) {
425: for ( m = BDY(p); m; m = NEXT(m) )
1.22 noro 426: if ( OID(m->c) == O_N ) {
427: /* GCD of coeffs = 1 */
1.25 noro 428: dp_monic_sf(p,rp);
1.22 noro 429: return;
1.23 noro 430: } else break;
431: /* compute GCD over the finite fieid */
432: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
433: w = (P *)ALLOCA(n*sizeof(P));
434: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ )
435: w[i] = m->c;
436: gcdsf(CO,w,n,&g);
437: if ( NUM(g) )
1.25 noro 438: dp_monic_sf(p,rp);
1.23 noro 439: else {
440: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
441: NEXTMP(mr0,mr); divsp(CO,m->c,g,&mr->c); mr->dl = m->dl;
1.22 noro 442: }
1.25 noro 443: NEXT(mr) = 0; MKDP(p->nv,mr0,p1); p1->sugar = p->sugar;
444: dp_monic_sf(p1,rp);
1.22 noro 445: }
1.23 noro 446: return;
447: } else if ( dp_fcoeffs )
1.7 noro 448: *rp = p;
1.23 noro 449: else if ( NoGCD )
1.7 noro 450: dp_ptozp(p,rp);
451: else {
452: dp_ptozp(p,&p1); p = p1;
453: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
454: if ( n == 1 ) {
455: m = BDY(p);
456: NEWMP(mr); mr->dl = m->dl; mr->c = (P)ONE; NEXT(mr) = 0;
457: MKDP(p->nv,mr,*rp); (*rp)->sugar = p->sugar;
458: return;
459: }
460: w = (P *)ALLOCA(n*sizeof(P));
461: c = (Q *)ALLOCA(n*sizeof(Q));
462: for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
463: if ( NUM(m->c) ) {
464: c[i] = (Q)m->c; w[i] = (P)ONE;
465: } else
466: ptozp(m->c,1,&c[i],&w[i]);
467: qltozl(c,n,&dvr); heu_nezgcdnpz(CO,w,n,&t); mulp(CO,t,(P)dvr,&g);
468: if ( NUM(g) )
469: *rp = p;
470: else {
471: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
472: NEXTMP(mr0,mr); divsp(CO,m->c,g,&mr->c); mr->dl = m->dl;
473: }
474: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.5 noro 475: }
1.7 noro 476: }
1.5 noro 477: }
478:
1.20 noro 479: void heu_nezgcdnpz(VL vl,P *pl,int m,P *pr)
1.5 noro 480: {
481: int i,r;
482: P gcd,t,s1,s2,u;
483: Q rq;
484:
485: while ( 1 ) {
486: for ( i = 0, s1 = 0; i < m; i++ ) {
487: r = random(); UTOQ(r,rq);
488: mulp(vl,pl[i],(P)rq,&t); addp(vl,s1,t,&u); s1 = u;
489: }
490: for ( i = 0, s2 = 0; i < m; i++ ) {
491: r = random(); UTOQ(r,rq);
492: mulp(vl,pl[i],(P)rq,&t); addp(vl,s2,t,&u); s2 = u;
493: }
494: ezgcdp(vl,s1,s2,&gcd);
495: for ( i = 0; i < m; i++ ) {
496: if ( !divtpz(vl,pl[i],gcd,&t) )
497: break;
498: }
499: if ( i == m )
500: break;
501: }
502: *pr = gcd;
503: }
504:
1.20 noro 505: void dp_prim_mod(DP p,int mod,DP *rp)
1.5 noro 506: {
507: P t,g;
508: MP m,mr,mr0;
509:
510: if ( !p )
511: *rp = 0;
512: else if ( NoGCD )
513: *rp = p;
514: else {
515: for ( m = BDY(p), g = m->c, m = NEXT(m); m; m = NEXT(m) ) {
516: gcdprsmp(CO,mod,g,m->c,&t); g = t;
517: }
518: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
519: NEXTMP(mr0,mr); divsmp(CO,mod,m->c,g,&mr->c); mr->dl = m->dl;
520: }
521: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
522: }
523: }
524:
1.20 noro 525: void dp_cont(DP p,Q *rp)
1.5 noro 526: {
1.7 noro 527: VECT v;
1.5 noro 528:
1.7 noro 529: dp_dtov(p,&v); igcdv(v,rp);
1.5 noro 530: }
531:
1.20 noro 532: void dp_dtov(DP dp,VECT *rp)
1.5 noro 533: {
1.7 noro 534: MP m,t;
535: int i,n;
536: VECT v;
537: pointer *p;
1.5 noro 538:
1.7 noro 539: m = BDY(dp);
540: for ( t = m, n = 0; t; t = NEXT(t), n++ );
541: MKVECT(v,n);
542: for ( i = 0, p = BDY(v), t = m; i < n; t = NEXT(t), i++ )
543: p[i] = (pointer)(t->c);
544: *rp = v;
1.5 noro 545: }
546:
1.7 noro 547: /*
548: * s-poly computation
549: *
550: */
1.5 noro 551:
1.20 noro 552: void dp_sp(DP p1,DP p2,DP *rp)
1.5 noro 553: {
1.7 noro 554: int i,n,td;
555: int *w;
556: DL d1,d2,d;
557: MP m;
558: DP t,s1,s2,u;
559: Q c,c1,c2;
560: N gn,tn;
1.5 noro 561:
1.7 noro 562: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
563: w = (int *)ALLOCA(n*sizeof(int));
564: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 565: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.5 noro 566: }
1.7 noro 567:
568: NEWDL(d,n); d->td = td - d1->td;
569: for ( i = 0; i < n; i++ )
570: d->d[i] = w[i] - d1->d[i];
571: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
572: if ( INT(c1) && INT(c2) ) {
573: gcdn(NM(c1),NM(c2),&gn);
574: if ( !UNIN(gn) ) {
575: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
576: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
1.5 noro 577: }
578: }
1.7 noro 579:
580: NEWMP(m); m->dl = d; m->c = (P)c2; NEXT(m) = 0;
581: MKDP(n,m,s1); s1->sugar = d->td; muld(CO,s1,p1,&t);
582:
583: NEWDL(d,n); d->td = td - d2->td;
584: for ( i = 0; i < n; i++ )
585: d->d[i] = w[i] - d2->d[i];
586: NEWMP(m); m->dl = d; m->c = (P)c1; NEXT(m) = 0;
587: MKDP(n,m,s2); s2->sugar = d->td; muld(CO,s2,p2,&u);
588:
589: subd(CO,t,u,rp);
1.14 noro 590: if ( GenTrace ) {
591: LIST hist;
592: NODE node;
593:
594: node = mknode(4,ONE,0,s1,ONE);
595: MKLIST(hist,node);
596: MKNODE(TraceList,hist,0);
597:
598: node = mknode(4,ONE,0,0,ONE);
599: chsgnd(s2,(DP *)&ARG2(node));
600: MKLIST(hist,node);
601: MKNODE(node,hist,TraceList); TraceList = node;
602: }
603: }
604:
1.20 noro 605: void _dp_sp_dup(DP p1,DP p2,DP *rp)
1.14 noro 606: {
607: int i,n,td;
608: int *w;
609: DL d1,d2,d;
610: MP m;
611: DP t,s1,s2,u;
612: Q c,c1,c2;
613: N gn,tn;
614:
615: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
616: w = (int *)ALLOCA(n*sizeof(int));
617: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 618: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.14 noro 619: }
620:
621: _NEWDL(d,n); d->td = td - d1->td;
622: for ( i = 0; i < n; i++ )
623: d->d[i] = w[i] - d1->d[i];
624: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
625: if ( INT(c1) && INT(c2) ) {
626: gcdn(NM(c1),NM(c2),&gn);
627: if ( !UNIN(gn) ) {
628: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
629: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
630: }
631: }
632:
633: _NEWMP(m); m->dl = d; m->c = (P)c2; NEXT(m) = 0;
634: _MKDP(n,m,s1); s1->sugar = d->td; _muld_dup(CO,s1,p1,&t); _free_dp(s1);
635:
636: _NEWDL(d,n); d->td = td - d2->td;
637: for ( i = 0; i < n; i++ )
638: d->d[i] = w[i] - d2->d[i];
639: _NEWMP(m); m->dl = d; chsgnp((P)c1,&m->c); NEXT(m) = 0;
640: _MKDP(n,m,s2); s2->sugar = d->td; _muld_dup(CO,s2,p2,&u); _free_dp(s2);
641:
642: _addd_destructive(CO,t,u,rp);
1.7 noro 643: if ( GenTrace ) {
644: LIST hist;
645: NODE node;
646:
647: node = mknode(4,ONE,0,s1,ONE);
648: MKLIST(hist,node);
649: MKNODE(TraceList,hist,0);
650:
651: node = mknode(4,ONE,0,0,ONE);
652: chsgnd(s2,(DP *)&ARG2(node));
653: MKLIST(hist,node);
654: MKNODE(node,hist,TraceList); TraceList = node;
655: }
656: }
657:
1.20 noro 658: void dp_sp_mod(DP p1,DP p2,int mod,DP *rp)
1.7 noro 659: {
660: int i,n,td;
661: int *w;
662: DL d1,d2,d;
663: MP m;
664: DP t,s,u;
665:
666: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
667: w = (int *)ALLOCA(n*sizeof(int));
668: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 669: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 670: }
1.18 noro 671: NEWDL_NOINIT(d,n); d->td = td - d1->td;
1.7 noro 672: for ( i = 0; i < n; i++ )
673: d->d[i] = w[i] - d1->d[i];
674: NEWMP(m); m->dl = d; m->c = (P)BDY(p2)->c; NEXT(m) = 0;
675: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p1,s,&t);
1.18 noro 676: NEWDL_NOINIT(d,n); d->td = td - d2->td;
1.7 noro 677: for ( i = 0; i < n; i++ )
678: d->d[i] = w[i] - d2->d[i];
679: NEWMP(m); m->dl = d; m->c = (P)BDY(p1)->c; NEXT(m) = 0;
680: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p2,s,&u);
681: submd(CO,mod,t,u,rp);
682: }
683:
1.20 noro 684: void _dp_sp_mod_dup(DP p1,DP p2,int mod,DP *rp)
1.7 noro 685: {
686: int i,n,td;
687: int *w;
688: DL d1,d2,d;
689: MP m;
690: DP t,s,u;
691:
692: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
693: w = (int *)ALLOCA(n*sizeof(int));
694: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 695: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 696: }
697: _NEWDL(d,n); d->td = td - d1->td;
698: for ( i = 0; i < n; i++ )
699: d->d[i] = w[i] - d1->d[i];
700: _NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
701: _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p1,&t); _free_dp(s);
702: _NEWDL(d,n); d->td = td - d2->td;
703: for ( i = 0; i < n; i++ )
704: d->d[i] = w[i] - d2->d[i];
705: _NEWMP(m); m->dl = d; m->c = STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
706: _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p2,&u); _free_dp(s);
707: _addmd_destructive(mod,t,u,rp);
708: }
709:
1.20 noro 710: void _dp_sp_mod(DP p1,DP p2,int mod,DP *rp)
1.7 noro 711: {
712: int i,n,td;
713: int *w;
714: DL d1,d2,d;
715: MP m;
716: DP t,s,u;
717:
718: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
719: w = (int *)ALLOCA(n*sizeof(int));
720: for ( i = 0, td = 0; i < n; i++ ) {
1.21 noro 721: w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
1.7 noro 722: }
723: NEWDL(d,n); d->td = td - d1->td;
724: for ( i = 0; i < n; i++ )
725: d->d[i] = w[i] - d1->d[i];
726: NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
727: MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p1,&t);
728: NEWDL(d,n); d->td = td - d2->td;
729: for ( i = 0; i < n; i++ )
730: d->d[i] = w[i] - d2->d[i];
731: NEWMP(m); m->dl = d; m->c = STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
732: MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p2,&u);
733: addmd_destructive(mod,t,u,rp);
734: }
735:
736: /*
737: * m-reduction
1.13 noro 738: * do content reduction over Z or Q(x,...)
739: * do nothing over finite fields
1.7 noro 740: *
741: */
742:
1.20 noro 743: void dp_red(DP p0,DP p1,DP p2,DP *head,DP *rest,P *dnp,DP *multp)
1.7 noro 744: {
745: int i,n;
746: DL d1,d2,d;
747: MP m;
748: DP t,s,r,h;
749: Q c,c1,c2;
750: N gn,tn;
751: P g,a;
1.23 noro 752: P p[2];
1.7 noro 753:
754: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
755: NEWDL(d,n); d->td = d1->td - d2->td;
756: for ( i = 0; i < n; i++ )
757: d->d[i] = d1->d[i]-d2->d[i];
758: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
1.23 noro 759: if ( dp_fcoeffs == N_GFS ) {
760: p[0] = (P)c1; p[1] = (P)c2;
761: gcdsf(CO,p,2,&g);
762: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
763: } else if ( dp_fcoeffs ) {
1.7 noro 764: /* do nothing */
765: } else if ( INT(c1) && INT(c2) ) {
766: gcdn(NM(c1),NM(c2),&gn);
767: if ( !UNIN(gn) ) {
768: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
769: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
770: }
771: } else {
772: ezgcdpz(CO,(P)c1,(P)c2,&g);
773: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
774: }
775: NEWMP(m); m->dl = d; chsgnp((P)c1,&m->c); NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
776: *multp = s;
777: muld(CO,s,p2,&t); muldc(CO,p1,(P)c2,&s); addd(CO,s,t,&r);
778: muldc(CO,p0,(P)c2,&h);
779: *head = h; *rest = r; *dnp = (P)c2;
780: }
781:
1.13 noro 782: /* m-reduction over a field */
783:
1.20 noro 784: void dp_red_f(DP p1,DP p2,DP *rest)
1.13 noro 785: {
786: int i,n;
787: DL d1,d2,d;
788: MP m;
1.20 noro 789: DP t,s;
1.13 noro 790: Obj a,b;
791:
792: n = p1->nv;
793: d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
794:
795: NEWDL(d,n); d->td = d1->td - d2->td;
796: for ( i = 0; i < n; i++ )
797: d->d[i] = d1->d[i]-d2->d[i];
798:
799: NEWMP(m); m->dl = d;
800: divr(CO,(Obj)BDY(p1)->c,(Obj)BDY(p2)->c,&a); chsgnr(a,&b);
801: C(m) = (P)b;
802: NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
803:
804: muld(CO,s,p2,&t); addd(CO,p1,t,rest);
805: }
806:
1.20 noro 807: void dp_red_mod(DP p0,DP p1,DP p2,int mod,DP *head,DP *rest,P *dnp)
1.7 noro 808: {
809: int i,n;
810: DL d1,d2,d;
811: MP m;
812: DP t,s,r,h;
813: P c1,c2,g,u;
814:
815: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
816: NEWDL(d,n); d->td = d1->td - d2->td;
817: for ( i = 0; i < n; i++ )
818: d->d[i] = d1->d[i]-d2->d[i];
819: c1 = (P)BDY(p1)->c; c2 = (P)BDY(p2)->c;
820: gcdprsmp(CO,mod,c1,c2,&g);
821: divsmp(CO,mod,c1,g,&u); c1 = u; divsmp(CO,mod,c2,g,&u); c2 = u;
822: if ( NUM(c2) ) {
823: divsmp(CO,mod,c1,c2,&u); c1 = u; c2 = (P)ONEM;
824: }
825: NEWMP(m); m->dl = d; chsgnmp(mod,(P)c1,&m->c); NEXT(m) = 0;
1.11 noro 826: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,s,p2,&t);
1.7 noro 827: if ( NUM(c2) ) {
828: addmd(CO,mod,p1,t,&r); h = p0;
829: } else {
830: mulmdc(CO,mod,p1,c2,&s); addmd(CO,mod,s,t,&r); mulmdc(CO,mod,p0,c2,&h);
831: }
832: *head = h; *rest = r; *dnp = c2;
833: }
834:
1.10 noro 835: struct oEGT eg_red_mod;
836:
1.20 noro 837: void _dp_red_mod_destructive(DP p1,DP p2,int mod,DP *rp)
1.7 noro 838: {
839: int i,n;
840: DL d1,d2,d;
841: MP m;
842: DP t,s;
1.16 noro 843: int c,c1,c2;
844: extern int do_weyl;
1.7 noro 845:
846: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
847: _NEWDL(d,n); d->td = d1->td - d2->td;
848: for ( i = 0; i < n; i++ )
849: d->d[i] = d1->d[i]-d2->d[i];
1.16 noro 850: c = invm(ITOS(BDY(p2)->c),mod);
851: c2 = ITOS(BDY(p1)->c);
852: DMAR(c,c2,0,mod,c1);
1.7 noro 853: _NEWMP(m); m->dl = d; m->c = STOI(mod-c1); NEXT(m) = 0;
1.16 noro 854: #if 0
1.7 noro 855: _MKDP(n,m,s); s->sugar = d->td;
856: _mulmd_dup(mod,s,p2,&t); _free_dp(s);
1.16 noro 857: #else
858: if ( do_weyl ) {
1.19 noro 859: _MKDP(n,m,s); s->sugar = d->td;
860: _mulmd_dup(mod,s,p2,&t); _free_dp(s);
1.16 noro 861: } else {
862: _mulmdm_dup(mod,p2,m,&t); _FREEMP(m);
863: }
864: #endif
1.10 noro 865: /* get_eg(&t0); */
1.7 noro 866: _addmd_destructive(mod,p1,t,rp);
1.10 noro 867: /* get_eg(&t1); add_eg(&eg_red_mod,&t0,&t1); */
1.7 noro 868: }
869:
870: /*
871: * normal form computation
872: *
873: */
1.5 noro 874:
1.20 noro 875: void dp_true_nf(NODE b,DP g,DP *ps,int full,DP *rp,P *dnp)
1.5 noro 876: {
877: DP u,p,d,s,t,dmy;
878: NODE l;
879: MP m,mr;
880: int i,n;
881: int *wb;
882: int sugar,psugar;
883: P dn,tdn,tdn1;
884:
885: dn = (P)ONE;
886: if ( !g ) {
887: *rp = 0; *dnp = dn; return;
888: }
889: for ( n = 0, l = b; l; l = NEXT(l), n++ );
890: wb = (int *)ALLOCA(n*sizeof(int));
891: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
892: wb[i] = QTOS((Q)BDY(l));
893: sugar = g->sugar;
894: for ( d = 0; g; ) {
895: for ( u = 0, i = 0; i < n; i++ ) {
896: if ( dp_redble(g,p = ps[wb[i]]) ) {
897: dp_red(d,g,p,&t,&u,&tdn,&dmy);
898: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
899: sugar = MAX(sugar,psugar);
900: if ( !u ) {
901: if ( d )
902: d->sugar = sugar;
903: *rp = d; *dnp = dn; return;
904: } else {
905: d = t;
906: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
907: }
908: break;
909: }
910: }
911: if ( u )
912: g = u;
913: else if ( !full ) {
914: if ( g ) {
915: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
916: }
917: *rp = g; *dnp = dn; return;
918: } else {
919: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
920: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
921: addd(CO,d,t,&s); d = s;
922: dp_rest(g,&t); g = t;
923: }
924: }
925: if ( d )
926: d->sugar = sugar;
927: *rp = d; *dnp = dn;
928: }
929:
1.13 noro 930: /* nf computation over Z */
931:
1.20 noro 932: void dp_nf_z(NODE b,DP g,DP *ps,int full,int multiple,DP *rp)
1.5 noro 933: {
934: DP u,p,d,s,t,dmy1;
935: P dmy;
936: NODE l;
937: MP m,mr;
938: int i,n;
939: int *wb;
940: int hmag;
941: int sugar,psugar;
942:
943: if ( !g ) {
944: *rp = 0; return;
945: }
946: for ( n = 0, l = b; l; l = NEXT(l), n++ );
947: wb = (int *)ALLOCA(n*sizeof(int));
948: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
949: wb[i] = QTOS((Q)BDY(l));
1.12 noro 950:
1.13 noro 951: hmag = multiple*HMAG(g);
1.5 noro 952: sugar = g->sugar;
1.12 noro 953:
1.5 noro 954: for ( d = 0; g; ) {
955: for ( u = 0, i = 0; i < n; i++ ) {
956: if ( dp_redble(g,p = ps[wb[i]]) ) {
957: dp_red(d,g,p,&t,&u,&dmy,&dmy1);
958: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
959: sugar = MAX(sugar,psugar);
960: if ( !u ) {
961: if ( d )
962: d->sugar = sugar;
963: *rp = d; return;
964: }
965: d = t;
966: break;
967: }
968: }
969: if ( u ) {
970: g = u;
971: if ( d ) {
1.13 noro 972: if ( multiple && HMAG(d) > hmag ) {
1.5 noro 973: dp_ptozp2(d,g,&t,&u); d = t; g = u;
974: hmag = multiple*HMAG(d);
975: }
976: } else {
1.13 noro 977: if ( multiple && HMAG(g) > hmag ) {
1.5 noro 978: dp_ptozp(g,&t); g = t;
979: hmag = multiple*HMAG(g);
980: }
981: }
982: }
983: else if ( !full ) {
984: if ( g ) {
985: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
986: }
987: *rp = g; return;
988: } else {
989: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
990: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
991: addd(CO,d,t,&s); d = s;
992: dp_rest(g,&t); g = t;
993:
994: }
995: }
996: if ( d )
997: d->sugar = sugar;
998: *rp = d;
999: }
1000:
1.13 noro 1001: /* nf computation over a field */
1002:
1.20 noro 1003: void dp_nf_f(NODE b,DP g,DP *ps,int full,DP *rp)
1.13 noro 1004: {
1005: DP u,p,d,s,t;
1006: NODE l;
1007: MP m,mr;
1008: int i,n;
1009: int *wb;
1010: int sugar,psugar;
1011:
1012: if ( !g ) {
1013: *rp = 0; return;
1014: }
1015: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1016: wb = (int *)ALLOCA(n*sizeof(int));
1017: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1018: wb[i] = QTOS((Q)BDY(l));
1019:
1020: sugar = g->sugar;
1021: for ( d = 0; g; ) {
1022: for ( u = 0, i = 0; i < n; i++ ) {
1023: if ( dp_redble(g,p = ps[wb[i]]) ) {
1024: dp_red_f(g,p,&u);
1025: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1026: sugar = MAX(sugar,psugar);
1027: if ( !u ) {
1028: if ( d )
1029: d->sugar = sugar;
1030: *rp = d; return;
1031: }
1032: break;
1033: }
1034: }
1035: if ( u )
1036: g = u;
1037: else if ( !full ) {
1038: if ( g ) {
1039: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1040: }
1041: *rp = g; return;
1042: } else {
1043: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1044: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1045: addd(CO,d,t,&s); d = s;
1046: dp_rest(g,&t); g = t;
1047: }
1048: }
1049: if ( d )
1050: d->sugar = sugar;
1051: *rp = d;
1052: }
1053:
1054: /* nf computation over GF(mod) (only for internal use) */
1055:
1.20 noro 1056: void dp_nf_mod(NODE b,DP g,DP *ps,int mod,int full,DP *rp)
1.5 noro 1057: {
1058: DP u,p,d,s,t;
1059: P dmy;
1060: NODE l;
1061: MP m,mr;
1062: int sugar,psugar;
1063:
1064: if ( !g ) {
1065: *rp = 0; return;
1066: }
1067: sugar = g->sugar;
1068: for ( d = 0; g; ) {
1069: for ( u = 0, l = b; l; l = NEXT(l) ) {
1070: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
1071: dp_red_mod(d,g,p,mod,&t,&u,&dmy);
1072: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1073: sugar = MAX(sugar,psugar);
1074: if ( !u ) {
1075: if ( d )
1076: d->sugar = sugar;
1077: *rp = d; return;
1078: }
1079: d = t;
1080: break;
1081: }
1082: }
1083: if ( u )
1084: g = u;
1085: else if ( !full ) {
1086: if ( g ) {
1087: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1088: }
1089: *rp = g; return;
1090: } else {
1091: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1092: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1093: addmd(CO,mod,d,t,&s); d = s;
1094: dp_rest(g,&t); g = t;
1095: }
1096: }
1097: if ( d )
1098: d->sugar = sugar;
1099: *rp = d;
1100: }
1101:
1.20 noro 1102: void dp_true_nf_mod(NODE b,DP g,DP *ps,int mod,int full,DP *rp,P *dnp)
1.5 noro 1103: {
1104: DP u,p,d,s,t;
1105: NODE l;
1106: MP m,mr;
1107: int i,n;
1108: int *wb;
1109: int sugar,psugar;
1110: P dn,tdn,tdn1;
1111:
1112: dn = (P)ONEM;
1113: if ( !g ) {
1114: *rp = 0; *dnp = dn; return;
1115: }
1116: for ( n = 0, l = b; l; l = NEXT(l), n++ );
1117: wb = (int *)ALLOCA(n*sizeof(int));
1118: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1119: wb[i] = QTOS((Q)BDY(l));
1120: sugar = g->sugar;
1121: for ( d = 0; g; ) {
1122: for ( u = 0, i = 0; i < n; i++ ) {
1123: if ( dp_redble(g,p = ps[wb[i]]) ) {
1124: dp_red_mod(d,g,p,mod,&t,&u,&tdn);
1125: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1126: sugar = MAX(sugar,psugar);
1127: if ( !u ) {
1128: if ( d )
1129: d->sugar = sugar;
1130: *rp = d; *dnp = dn; return;
1131: } else {
1132: d = t;
1133: mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
1134: }
1135: break;
1136: }
1137: }
1138: if ( u )
1139: g = u;
1140: else if ( !full ) {
1141: if ( g ) {
1142: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1143: }
1144: *rp = g; *dnp = dn; return;
1145: } else {
1146: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1147: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1148: addmd(CO,mod,d,t,&s); d = s;
1149: dp_rest(g,&t); g = t;
1150: }
1151: }
1152: if ( d )
1153: d->sugar = sugar;
1154: *rp = d; *dnp = dn;
1155: }
1156:
1.20 noro 1157: void _dp_nf_mod_destructive(NODE b,DP g,DP *ps,int mod,int full,DP *rp)
1.5 noro 1158: {
1.20 noro 1159: DP u,p,d;
1.7 noro 1160: NODE l;
1.20 noro 1161: MP m,mrd;
1162: int sugar,psugar,n,h_reducible;
1.5 noro 1163:
1.7 noro 1164: if ( !g ) {
1165: *rp = 0; return;
1.5 noro 1166: }
1.7 noro 1167: sugar = g->sugar;
1168: n = g->nv;
1169: for ( d = 0; g; ) {
1170: for ( h_reducible = 0, l = b; l; l = NEXT(l) ) {
1171: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
1172: h_reducible = 1;
1173: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1174: _dp_red_mod_destructive(g,p,mod,&u); g = u;
1175: sugar = MAX(sugar,psugar);
1176: if ( !g ) {
1177: if ( d )
1178: d->sugar = sugar;
1179: _dptodp(d,rp); _free_dp(d); return;
1180: }
1181: break;
1182: }
1183: }
1184: if ( !h_reducible ) {
1185: /* head term is not reducible */
1186: if ( !full ) {
1187: if ( g )
1188: g->sugar = sugar;
1189: _dptodp(g,rp); _free_dp(g); return;
1190: } else {
1191: m = BDY(g);
1192: if ( NEXT(m) ) {
1193: BDY(g) = NEXT(m); NEXT(m) = 0;
1194: } else {
1195: _FREEDP(g); g = 0;
1196: }
1197: if ( d ) {
1198: for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
1199: NEXT(mrd) = m;
1200: } else {
1201: _MKDP(n,m,d);
1202: }
1203: }
1204: }
1.5 noro 1205: }
1.7 noro 1206: if ( d )
1207: d->sugar = sugar;
1208: _dptodp(d,rp); _free_dp(d);
1.5 noro 1209: }
1.13 noro 1210:
1211: /* reduction by linear base over a field */
1212:
1.20 noro 1213: void dp_lnf_f(DP p1,DP p2,NODE g,DP *r1p,DP *r2p)
1.13 noro 1214: {
1215: DP r1,r2,b1,b2,t,s;
1216: Obj c,c1,c2;
1217: NODE l,b;
1218: int n;
1219:
1220: if ( !p1 ) {
1221: *r1p = p1; *r2p = p2; return;
1222: }
1223: n = p1->nv;
1224: for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
1225: if ( !r1 ) {
1226: *r1p = r1; *r2p = r2; return;
1227: }
1228: b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
1229: if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
1230: b2 = (DP)BDY(NEXT(b));
1231: divr(CO,(Obj)ONE,(Obj)BDY(b1)->c,&c1);
1232: mulr(CO,c1,(Obj)BDY(r1)->c,&c2); chsgnr(c2,&c);
1233: muldc(CO,b1,(P)c,&t); addd(CO,r1,t,&s); r1 = s;
1234: muldc(CO,b2,(P)c,&t); addd(CO,r2,t,&s); r2 = s;
1235: }
1236: }
1237: *r1p = r1; *r2p = r2;
1238: }
1239:
1240: /* reduction by linear base over GF(mod) */
1.5 noro 1241:
1.20 noro 1242: void dp_lnf_mod(DP p1,DP p2,NODE g,int mod,DP *r1p,DP *r2p)
1.5 noro 1243: {
1.7 noro 1244: DP r1,r2,b1,b2,t,s;
1245: P c;
1246: MQ c1,c2;
1247: NODE l,b;
1248: int n;
1249:
1250: if ( !p1 ) {
1251: *r1p = p1; *r2p = p2; return;
1252: }
1253: n = p1->nv;
1254: for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
1255: if ( !r1 ) {
1256: *r1p = r1; *r2p = r2; return;
1257: }
1258: b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
1259: if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
1260: b2 = (DP)BDY(NEXT(b));
1261: invmq(mod,(MQ)BDY(b1)->c,&c1);
1262: mulmq(mod,c1,(MQ)BDY(r1)->c,&c2); chsgnmp(mod,(P)c2,&c);
1263: mulmdc(CO,mod,b1,c,&t); addmd(CO,mod,r1,t,&s); r1 = s;
1264: mulmdc(CO,mod,b2,c,&t); addmd(CO,mod,r2,t,&s); r2 = s;
1265: }
1266: }
1267: *r1p = r1; *r2p = r2;
1.5 noro 1268: }
1269:
1.20 noro 1270: void dp_nf_tab_mod(DP p,LIST *tab,int mod,DP *rp)
1.5 noro 1271: {
1.7 noro 1272: DP s,t,u;
1273: MP m;
1274: DL h;
1275: int i,n;
1276:
1277: if ( !p ) {
1278: *rp = p; return;
1279: }
1280: n = p->nv;
1281: for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
1282: h = m->dl;
1283: while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
1284: i++;
1285: mulmdc(CO,mod,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
1286: addmd(CO,mod,s,t,&u); s = u;
1.24 noro 1287: }
1288: *rp = s;
1289: }
1290:
1291: void dp_nf_tab_f(DP p,LIST *tab,DP *rp)
1292: {
1293: DP s,t,u;
1294: MP m;
1295: DL h;
1296: int i,n;
1297:
1298: if ( !p ) {
1299: *rp = p; return;
1300: }
1301: n = p->nv;
1302: for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
1303: h = m->dl;
1304: while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
1305: i++;
1306: muldc(CO,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
1307: addd(CO,s,t,&u); s = u;
1.7 noro 1308: }
1309: *rp = s;
1.5 noro 1310: }
1311:
1.7 noro 1312: /*
1313: * setting flags
1.30 noro 1314: * call create_order_spec with vl=0 to set old type order.
1.7 noro 1315: *
1316: */
1317:
1.27 noro 1318: int create_order_spec(VL vl,Obj obj,struct order_spec **specp)
1.5 noro 1319: {
1.7 noro 1320: int i,j,n,s,row,col;
1.27 noro 1321: struct order_spec *spec;
1.7 noro 1322: struct order_pair *l;
1323: NODE node,t,tn;
1324: MAT m;
1325: pointer **b;
1326: int **w;
1.5 noro 1327:
1.27 noro 1328: if ( vl && obj && OID(obj) == O_LIST )
1329: return create_composite_order_spec(vl,(LIST)obj,specp);
1330:
1331: *specp = spec = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1.7 noro 1332: if ( !obj || NUM(obj) ) {
1333: spec->id = 0; spec->obj = obj;
1334: spec->ord.simple = QTOS((Q)obj);
1335: return 1;
1336: } else if ( OID(obj) == O_LIST ) {
1337: node = BDY((LIST)obj);
1338: for ( n = 0, t = node; t; t = NEXT(t), n++ );
1339: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
1340: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
1341: tn = BDY((LIST)BDY(t)); l[i].order = QTOS((Q)BDY(tn));
1342: tn = NEXT(tn); l[i].length = QTOS((Q)BDY(tn));
1343: s += l[i].length;
1344: }
1345: spec->id = 1; spec->obj = obj;
1346: spec->ord.block.order_pair = l;
1347: spec->ord.block.length = n; spec->nv = s;
1348: return 1;
1349: } else if ( OID(obj) == O_MAT ) {
1350: m = (MAT)obj; row = m->row; col = m->col; b = BDY(m);
1351: w = almat(row,col);
1352: for ( i = 0; i < row; i++ )
1353: for ( j = 0; j < col; j++ )
1354: w[i][j] = QTOS((Q)b[i][j]);
1355: spec->id = 2; spec->obj = obj;
1356: spec->nv = col; spec->ord.matrix.row = row;
1357: spec->ord.matrix.matrix = w;
1358: return 1;
1359: } else
1.5 noro 1360: return 0;
1361: }
1362:
1.28 noro 1363: void print_composite_order_spec(struct order_spec *spec)
1364: {
1365: int nv,n,len,i,j,k,start;
1366: struct weight_or_block *worb;
1367:
1368: nv = spec->nv;
1369: n = spec->ord.composite.length;
1370: worb = spec->ord.composite.w_or_b;
1371: for ( i = 0; i < n; i++, worb++ ) {
1372: len = worb->length;
1373: printf("[ ");
1374: switch ( worb->type ) {
1375: case IS_DENSE_WEIGHT:
1376: for ( j = 0; j < len; j++ )
1377: printf("%d ",worb->body.dense_weight[j]);
1378: for ( ; j < nv; j++ )
1379: printf("0 ");
1380: break;
1381: case IS_SPARSE_WEIGHT:
1382: for ( j = 0, k = 0; j < nv; j++ )
1383: if ( j == worb->body.sparse_weight[k].pos )
1384: printf("%d ",worb->body.sparse_weight[k++].value);
1385: else
1386: printf("0 ");
1387: break;
1388: case IS_BLOCK:
1389: start = worb->body.block.start;
1390: for ( j = 0; j < start; j++ ) printf("0 ");
1391: switch ( worb->body.block.order ) {
1392: case 0:
1393: for ( k = 0; k < len; k++, j++ ) printf("R ");
1394: break;
1395: case 1:
1396: for ( k = 0; k < len; k++, j++ ) printf("G ");
1397: break;
1398: case 2:
1399: for ( k = 0; k < len; k++, j++ ) printf("L ");
1400: break;
1401: }
1402: for ( ; j < nv; j++ ) printf("0 ");
1403: break;
1404: }
1405: printf("]\n");
1406: }
1407: }
1408:
1.27 noro 1409: /* order = [w_or_b, w_or_b, ... ] */
1410: /* w_or_b = w or b */
1411: /* w = [1,2,...] or [x,1,y,2,...] */
1412: /* b = [@lex,x,y,...,z] etc */
1413:
1414: int create_composite_order_spec(VL vl,LIST order,struct order_spec **specp)
1415: {
1416: NODE wb,t,p;
1417: struct order_spec *spec;
1418: VL tvl;
1.29 noro 1419: int n,i,j,k,l,start,end,len,w;
1.27 noro 1420: int *dw;
1421: struct sparse_weight *sw;
1422: struct weight_or_block *w_or_b;
1423: Obj a0;
1424: NODE a;
1.29 noro 1425: V v,sv,ev;
1426: SYMBOL sym;
1427: int *top;
1.27 noro 1428:
1429: /* l = number of vars in vl */
1430: for ( l = 0, tvl = vl; tvl; tvl = NEXT(tvl), l++ );
1431: /* n = number of primitives in order */
1432: wb = BDY(order);
1433: n = length(wb);
1434: *specp = spec = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1435: spec->id = 3;
1436: spec->obj = (Obj)order;
1437: spec->nv = l;
1438: spec->ord.composite.length = n;
1.28 noro 1439: w_or_b = spec->ord.composite.w_or_b = (struct weight_or_block *)
1.29 noro 1440: MALLOC(sizeof(struct weight_or_block)*(n+1));
1441:
1442: /* top : register the top variable in each w_or_b specification */
1443: top = (int *)ALLOCA(l*sizeof(int));
1444: for ( i = 0; i < l; i++ ) top[i] = 0;
1445:
1.28 noro 1446: for ( t = wb, i = 0; t; t = NEXT(t), i++ ) {
1.30 noro 1447: if ( !BDY(t) || OID((Obj)BDY(t)) != O_LIST )
1448: error("a list of lists must be specified for the key \"order\"");
1.28 noro 1449: a = BDY((LIST)BDY(t));
1.27 noro 1450: len = length(a);
1451: a0 = (Obj)BDY(a);
1452: if ( !a0 || OID(a0) == O_N ) {
1.28 noro 1453: /* a is a dense weight vector */
1.27 noro 1454: dw = (int *)MALLOC(sizeof(int)*len);
1.30 noro 1455: for ( j = 0, p = a; j < len; p = NEXT(p), j++ ) {
1456: if ( !INT((Q)BDY(p)) )
1457: error("a dense weight vector must be specified as a list of integers");
1.27 noro 1458: dw[j] = QTOS((Q)BDY(p));
1.30 noro 1459: }
1.27 noro 1460: w_or_b[i].type = IS_DENSE_WEIGHT;
1461: w_or_b[i].length = len;
1462: w_or_b[i].body.dense_weight = dw;
1.29 noro 1463:
1464: /* find the top */
1465: for ( k = 0; k < len && !dw[k]; k++ );
1466: if ( k < len ) top[k] = 1;
1467:
1.27 noro 1468: } else if ( OID(a0) == O_P ) {
1.28 noro 1469: /* a is a sparse weight vector */
1470: len >>= 1;
1.27 noro 1471: sw = (struct sparse_weight *)
1472: MALLOC(sizeof(struct sparse_weight)*len);
1473: for ( j = 0, p = a; j < len; j++ ) {
1.30 noro 1474: if ( !BDY(p) || OID((P)BDY(p)) != O_P )
1475: error("a sparse weight vector must be specified as [var1,weight1,...]");
1.28 noro 1476: v = VR((P)BDY(p)); p = NEXT(p);
1.27 noro 1477: for ( tvl = vl, k = 0; tvl && tvl->v != v;
1478: k++, tvl = NEXT(tvl) );
1479: if ( !tvl )
1.30 noro 1480: error("invalid variable name in a sparse weight vector");
1.27 noro 1481: sw[j].pos = k;
1.30 noro 1482: if ( !INT((Q)BDY(p)) )
1483: error("a sparse weight vector must be specified as [var1,weight1,...]");
1.28 noro 1484: sw[j].value = QTOS((Q)BDY(p)); p = NEXT(p);
1.27 noro 1485: }
1486: w_or_b[i].type = IS_SPARSE_WEIGHT;
1487: w_or_b[i].length = len;
1488: w_or_b[i].body.sparse_weight = sw;
1.29 noro 1489:
1490: /* find the top */
1491: for ( k = 0; k < len && !sw[k].value; k++ );
1492: if ( k < len ) top[sw[k].pos] = 1;
1493: } else if ( OID(a0) == O_RANGE ) {
1494: /* [range(v1,v2),w] */
1495: sv = VR((P)(((RANGE)a0)->start));
1496: ev = VR((P)(((RANGE)a0)->end));
1497: for ( tvl = vl, start = 0; tvl && tvl->v != sv; start++, tvl = NEXT(tvl) );
1498: if ( !tvl )
1499: error("invalid range");
1500: for ( end = start; tvl && tvl->v != ev; end++, tvl = NEXT(tvl) );
1501: if ( !tvl )
1502: error("invalid range");
1503: len = end-start+1;
1504: sw = (struct sparse_weight *)
1505: MALLOC(sizeof(struct sparse_weight)*len);
1506: w = QTOS((Q)BDY(NEXT(a)));
1507: for ( tvl = vl, k = 0; k < start; k++, tvl = NEXT(tvl) );
1508: for ( j = 0 ; k <= end; k++, tvl = NEXT(tvl), j++ ) {
1509: sw[j].pos = k;
1510: sw[j].value = w;
1511: }
1512: w_or_b[i].type = IS_SPARSE_WEIGHT;
1513: w_or_b[i].length = len;
1514: w_or_b[i].body.sparse_weight = sw;
1515:
1516: /* register the top */
1517: if ( w ) top[start] = 1;
1.28 noro 1518: } else if ( OID(a0) == O_SYMBOL ) {
1519: /* a is a block */
1.29 noro 1520: sym = (SYMBOL)a0; a = NEXT(a); len--;
1521: if ( OID((Obj)BDY(a)) == O_RANGE ) {
1522: sv = VR((P)(((RANGE)BDY(a))->start));
1523: ev = VR((P)(((RANGE)BDY(a))->end));
1524: for ( tvl = vl, start = 0; tvl && tvl->v != sv; start++, tvl = NEXT(tvl) );
1525: if ( !tvl )
1526: error("invalid range");
1527: for ( end = start; tvl && tvl->v != ev; end++, tvl = NEXT(tvl) );
1528: if ( !tvl )
1529: error("invalid range");
1530: len = end-start+1;
1531: } else {
1532: for ( start = 0, tvl = vl; tvl->v != VR((P)BDY(a));
1.28 noro 1533: tvl = NEXT(tvl), start++ );
1.29 noro 1534: for ( p = NEXT(a), tvl = NEXT(tvl); p;
1.30 noro 1535: p = NEXT(p), tvl = NEXT(tvl) ) {
1536: if ( !BDY(p) || OID((P)BDY(p)) != O_P )
1537: error("a block must be specified as [ordsymbol,var1,var2,...]");
1.29 noro 1538: if ( tvl->v != VR((P)BDY(p)) ) break;
1.30 noro 1539: }
1.29 noro 1540: if ( p )
1.30 noro 1541: error("a block must be contiguous in the variable list");
1.29 noro 1542: }
1.28 noro 1543: w_or_b[i].type = IS_BLOCK;
1544: w_or_b[i].length = len;
1545: w_or_b[i].body.block.start = start;
1546: if ( !strcmp(sym->name,"@grlex") )
1547: w_or_b[i].body.block.order = 0;
1548: else if ( !strcmp(sym->name,"@glex") )
1549: w_or_b[i].body.block.order = 1;
1550: else if ( !strcmp(sym->name,"@lex") )
1551: w_or_b[i].body.block.order = 2;
1552: else
1.29 noro 1553: error("invalid ordername");
1554: /* register the tops */
1555: for ( j = 0, k = start; j < len; j++, k++ )
1556: top[k] = 1;
1.28 noro 1557: }
1.29 noro 1558: }
1559: for ( k = 0; k < l && top[k]; k++ );
1560: if ( k < l ) {
1561: /* incomplete order specification; add @grlex */
1562: w_or_b[n].type = IS_BLOCK;
1563: w_or_b[n].length = l;
1564: w_or_b[n].body.block.start = 0;
1565: w_or_b[n].body.block.order = 0;
1566: spec->ord.composite.length = n+1;
1.27 noro 1567: }
1.28 noro 1568: if ( 1 ) print_composite_order_spec(spec);
1.27 noro 1569: }
1570:
1.7 noro 1571: /*
1572: * converters
1573: *
1574: */
1575:
1.20 noro 1576: void dp_homo(DP p,DP *rp)
1.5 noro 1577: {
1.7 noro 1578: MP m,mr,mr0;
1579: int i,n,nv,td;
1580: DL dl,dlh;
1.5 noro 1581:
1.7 noro 1582: if ( !p )
1583: *rp = 0;
1584: else {
1585: n = p->nv; nv = n + 1;
1586: m = BDY(p); td = sugard(m);
1587: for ( mr0 = 0; m; m = NEXT(m) ) {
1588: NEXTMP(mr0,mr); mr->c = m->c;
1589: dl = m->dl;
1590: mr->dl = dlh = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
1591: dlh->td = td;
1592: for ( i = 0; i < n; i++ )
1593: dlh->d[i] = dl->d[i];
1594: dlh->d[n] = td - dl->td;
1595: }
1596: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.5 noro 1597: }
1598: }
1599:
1.20 noro 1600: void dp_dehomo(DP p,DP *rp)
1.5 noro 1601: {
1.7 noro 1602: MP m,mr,mr0;
1603: int i,n,nv;
1604: DL dl,dlh;
1.5 noro 1605:
1.7 noro 1606: if ( !p )
1607: *rp = 0;
1608: else {
1609: n = p->nv; nv = n - 1;
1610: m = BDY(p);
1611: for ( mr0 = 0; m; m = NEXT(m) ) {
1612: NEXTMP(mr0,mr); mr->c = m->c;
1613: dlh = m->dl;
1614: mr->dl = dl = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
1615: dl->td = dlh->td - dlh->d[nv];
1616: for ( i = 0; i < nv; i++ )
1617: dl->d[i] = dlh->d[i];
1618: }
1619: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
1620: }
1.5 noro 1621: }
1622:
1.20 noro 1623: void dp_mod(DP p,int mod,NODE subst,DP *rp)
1.5 noro 1624: {
1.7 noro 1625: MP m,mr,mr0;
1626: P t,s,s1;
1627: V v;
1628: NODE tn;
1.5 noro 1629:
1.7 noro 1630: if ( !p )
1631: *rp = 0;
1632: else {
1633: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
1634: for ( tn = subst, s = m->c; tn; tn = NEXT(tn) ) {
1635: v = VR((P)BDY(tn)); tn = NEXT(tn);
1636: substp(CO,s,v,(P)BDY(tn),&s1); s = s1;
1637: }
1638: ptomp(mod,s,&t);
1639: if ( t ) {
1640: NEXTMP(mr0,mr); mr->c = t; mr->dl = m->dl;
1641: }
1642: }
1643: if ( mr0 ) {
1644: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1645: } else
1646: *rp = 0;
1647: }
1.5 noro 1648: }
1649:
1.20 noro 1650: void dp_rat(DP p,DP *rp)
1.5 noro 1651: {
1.7 noro 1652: MP m,mr,mr0;
1.5 noro 1653:
1.7 noro 1654: if ( !p )
1655: *rp = 0;
1656: else {
1657: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
1658: NEXTMP(mr0,mr); mptop(m->c,&mr->c); mr->dl = m->dl;
1.5 noro 1659: }
1.7 noro 1660: if ( mr0 ) {
1661: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1662: } else
1663: *rp = 0;
1.5 noro 1664: }
1665: }
1666:
1667:
1.27 noro 1668: void homogenize_order(struct order_spec *old,int n,struct order_spec **newp)
1.5 noro 1669: {
1.7 noro 1670: struct order_pair *l;
1671: int length,nv,row,i,j;
1672: int **newm,**oldm;
1.27 noro 1673: struct order_spec *new;
1.31 ! noro 1674: int onv,nnv,nlen,olen,owlen;
! 1675: struct weight_or_block *owb,*nwb;
1.5 noro 1676:
1.27 noro 1677: *newp = new = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1.7 noro 1678: switch ( old->id ) {
1679: case 0:
1680: switch ( old->ord.simple ) {
1681: case 0:
1682: new->id = 0; new->ord.simple = 0; break;
1683: case 1:
1684: l = (struct order_pair *)
1685: MALLOC_ATOMIC(2*sizeof(struct order_pair));
1686: l[0].length = n; l[0].order = 1;
1687: l[1].length = 1; l[1].order = 2;
1688: new->id = 1;
1689: new->ord.block.order_pair = l;
1690: new->ord.block.length = 2; new->nv = n+1;
1691: break;
1692: case 2:
1693: new->id = 0; new->ord.simple = 1; break;
1694: case 3: case 4: case 5:
1695: new->id = 0; new->ord.simple = old->ord.simple+3;
1696: dp_nelim = n-1; break;
1697: case 6: case 7: case 8: case 9:
1698: new->id = 0; new->ord.simple = old->ord.simple; break;
1699: default:
1700: error("homogenize_order : invalid input");
1701: }
1702: break;
1703: case 1:
1704: length = old->ord.block.length;
1705: l = (struct order_pair *)
1706: MALLOC_ATOMIC((length+1)*sizeof(struct order_pair));
1707: bcopy((char *)old->ord.block.order_pair,(char *)l,length*sizeof(struct order_pair));
1708: l[length].order = 2; l[length].length = 1;
1709: new->id = 1; new->nv = n+1;
1710: new->ord.block.order_pair = l;
1711: new->ord.block.length = length+1;
1712: break;
1713: case 2:
1714: nv = old->nv; row = old->ord.matrix.row;
1715: oldm = old->ord.matrix.matrix; newm = almat(row+1,nv+1);
1716: for ( i = 0; i <= nv; i++ )
1717: newm[0][i] = 1;
1718: for ( i = 0; i < row; i++ ) {
1719: for ( j = 0; j < nv; j++ )
1720: newm[i+1][j] = oldm[i][j];
1721: newm[i+1][j] = 0;
1722: }
1723: new->id = 2; new->nv = nv+1;
1724: new->ord.matrix.row = row+1; new->ord.matrix.matrix = newm;
1.31 ! noro 1725: break;
! 1726: case 3:
! 1727: onv = old->nv;
! 1728: nnv = onv+1;
! 1729: olen = old->ord.composite.length;
! 1730: nlen = olen+1;
! 1731: owb = old->ord.composite.w_or_b;
! 1732: nwb = (struct weight_or_block *)
! 1733: MALLOC(nlen*sizeof(struct weight_or_block));
! 1734: for ( i = 0; i < olen; i++ ) {
! 1735: nwb[i].type = owb[i].type;
! 1736: switch ( owb[i].type ) {
! 1737: case IS_DENSE_WEIGHT:
! 1738: owlen = owb[i].length;
! 1739: nwb[i].length = owlen+1;
! 1740: nwb[i].body.dense_weight = (int *)MALLOC((owlen+1)*sizeof(int));
! 1741: for ( j = 0; j < owlen; j++ )
! 1742: nwb[i].body.dense_weight[j] = owb[i].body.dense_weight[j];
! 1743: nwb[i].body.dense_weight[owlen] = 0;
! 1744: break;
! 1745: case IS_SPARSE_WEIGHT:
! 1746: nwb[i].length = owb[i].length;
! 1747: nwb[i].body.sparse_weight = owb[i].body.sparse_weight;
! 1748: break;
! 1749: case IS_BLOCK:
! 1750: nwb[i].length = owb[i].length;
! 1751: nwb[i].body.block = owb[i].body.block;
! 1752: break;
! 1753: }
! 1754: }
! 1755: nwb[i].type = IS_SPARSE_WEIGHT;
! 1756: nwb[i].body.sparse_weight =
! 1757: (struct sparse_weight *)MALLOC(sizeof(struct sparse_weight));
! 1758: nwb[i].body.sparse_weight[0].pos = onv;
! 1759: nwb[i].body.sparse_weight[0].value = 1;
! 1760: new->id = 3;
! 1761: new->nv = nnv;
! 1762: new->ord.composite.length = nlen;
! 1763: new->ord.composite.w_or_b = nwb;
! 1764: print_composite_order_spec(new);
1.7 noro 1765: break;
1766: default:
1767: error("homogenize_order : invalid input");
1.5 noro 1768: }
1.7 noro 1769: }
1770:
1.20 noro 1771: void qltozl(Q *w,int n,Q *dvr)
1.7 noro 1772: {
1773: N nm,dn;
1774: N g,l1,l2,l3;
1775: Q c,d;
1776: int i;
1777: struct oVECT v;
1.5 noro 1778:
1779: for ( i = 0; i < n; i++ )
1.7 noro 1780: if ( w[i] && !INT(w[i]) )
1781: break;
1782: if ( i == n ) {
1783: v.id = O_VECT; v.len = n; v.body = (pointer *)w;
1784: igcdv(&v,dvr); return;
1785: }
1786: c = w[0]; nm = NM(c); dn = INT(c) ? ONEN : DN(c);
1787: for ( i = 1; i < n; i++ ) {
1788: c = w[i]; l1 = INT(c) ? ONEN : DN(c);
1789: gcdn(nm,NM(c),&g); nm = g;
1790: gcdn(dn,l1,&l2); muln(dn,l1,&l3); divsn(l3,l2,&dn);
1.5 noro 1791: }
1.7 noro 1792: if ( UNIN(dn) )
1793: NTOQ(nm,1,d);
1794: else
1795: NDTOQ(nm,dn,1,d);
1796: *dvr = d;
1797: }
1.5 noro 1798:
1.20 noro 1799: int comp_nm(Q *a,Q *b)
1.7 noro 1800: {
1801: return cmpn((*a)?NM(*a):0,(*b)?NM(*b):0);
1802: }
1803:
1.20 noro 1804: void sortbynm(Q *w,int n)
1.7 noro 1805: {
1806: qsort(w,n,sizeof(Q),(int (*)(const void *,const void *))comp_nm);
1807: }
1.5 noro 1808:
1809:
1.7 noro 1810: /*
1811: * simple operations
1812: *
1813: */
1.5 noro 1814:
1.20 noro 1815: int dp_redble(DP p1,DP p2)
1.7 noro 1816: {
1817: int i,n;
1818: DL d1,d2;
1.5 noro 1819:
1.7 noro 1820: d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
1821: if ( d1->td < d2->td )
1822: return 0;
1823: else {
1824: for ( i = 0, n = p1->nv; i < n; i++ )
1825: if ( d1->d[i] < d2->d[i] )
1826: return 0;
1827: return 1;
1.5 noro 1828: }
1829: }
1830:
1.20 noro 1831: void dp_subd(DP p1,DP p2,DP *rp)
1.5 noro 1832: {
1.7 noro 1833: int i,n;
1.5 noro 1834: DL d1,d2,d;
1835: MP m;
1.7 noro 1836: DP s;
1.5 noro 1837:
1838: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
1.7 noro 1839: NEWDL(d,n); d->td = d1->td - d2->td;
1.5 noro 1840: for ( i = 0; i < n; i++ )
1.7 noro 1841: d->d[i] = d1->d[i]-d2->d[i];
1842: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
1843: MKDP(n,m,s); s->sugar = d->td;
1844: *rp = s;
1845: }
1846:
1.20 noro 1847: void dltod(DL d,int n,DP *rp)
1.7 noro 1848: {
1849: MP m;
1850: DP s;
1851:
1852: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
1853: MKDP(n,m,s); s->sugar = d->td;
1854: *rp = s;
1.5 noro 1855: }
1856:
1.20 noro 1857: void dp_hm(DP p,DP *rp)
1.5 noro 1858: {
1859: MP m,mr;
1860:
1861: if ( !p )
1862: *rp = 0;
1863: else {
1864: m = BDY(p);
1865: NEWMP(mr); mr->dl = m->dl; mr->c = m->c; NEXT(mr) = 0;
1866: MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td; /* XXX */
1867: }
1868: }
1869:
1.20 noro 1870: void dp_rest(DP p,DP *rp)
1.5 noro 1871: {
1872: MP m;
1873:
1874: m = BDY(p);
1875: if ( !NEXT(m) )
1876: *rp = 0;
1877: else {
1878: MKDP(p->nv,NEXT(m),*rp);
1879: if ( *rp )
1880: (*rp)->sugar = p->sugar;
1881: }
1882: }
1883:
1.20 noro 1884: DL lcm_of_DL(int nv,DL dl1,DL dl2,DL dl)
1.5 noro 1885: {
1.21 noro 1886: register int i, *d1, *d2, *d, td;
1.5 noro 1887:
1888: if ( !dl ) NEWDL(dl,nv);
1889: d = dl->d, d1 = dl1->d, d2 = dl2->d;
1.21 noro 1890: for ( td = 0, i = 0; i < nv; d1++, d2++, d++, i++ ) {
1891: *d = *d1 > *d2 ? *d1 : *d2;
1892: td += MUL_WEIGHT(*d,i);
1893: }
1.5 noro 1894: dl->td = td;
1895: return dl;
1896: }
1897:
1.20 noro 1898: int dl_equal(int nv,DL dl1,DL dl2)
1.5 noro 1899: {
1900: register int *d1, *d2, n;
1901:
1902: if ( dl1->td != dl2->td ) return 0;
1903: for ( d1 = dl1->d, d2 = dl2->d, n = nv; --n >= 0; d1++, d2++ )
1904: if ( *d1 != *d2 ) return 0;
1905: return 1;
1906: }
1907:
1.20 noro 1908: int dp_nt(DP p)
1.5 noro 1909: {
1910: int i;
1911: MP m;
1912:
1913: if ( !p )
1914: return 0;
1915: else {
1916: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
1917: return i;
1918: }
1919: }
1920:
1.20 noro 1921: int dp_homogeneous(DP p)
1.15 noro 1922: {
1923: MP m;
1924: int d;
1925:
1926: if ( !p )
1927: return 1;
1928: else {
1929: m = BDY(p);
1930: d = m->dl->td;
1931: m = NEXT(m);
1932: for ( ; m; m = NEXT(m) ) {
1933: if ( m->dl->td != d )
1934: return 0;
1935: }
1936: return 1;
1937: }
1.16 noro 1938: }
1939:
1.20 noro 1940: void _print_mp(int nv,MP m)
1.16 noro 1941: {
1942: int i;
1943:
1.17 noro 1944: if ( !m )
1.16 noro 1945: return;
1946: for ( ; m; m = NEXT(m) ) {
1947: fprintf(stderr,"%d<",ITOS(C(m)));
1948: for ( i = 0; i < nv; i++ ) {
1949: fprintf(stderr,"%d",m->dl->d[i]);
1950: if ( i != nv-1 )
1951: fprintf(stderr," ");
1952: }
1953: fprintf(stderr,">",C(m));
1954: }
1955: fprintf(stderr,"\n");
1.15 noro 1956: }
1.26 noro 1957:
1958: static int cmp_mp_nvar;
1959:
1960: int comp_mp(MP *a,MP *b)
1961: {
1962: return -(*cmpdl)(cmp_mp_nvar,(*a)->dl,(*b)->dl);
1963: }
1964:
1965: void dp_sort(DP p,DP *rp)
1966: {
1967: MP t,mp,mp0;
1968: int i,n;
1969: DP r;
1970: MP *w;
1971:
1972: if ( !p ) {
1973: *rp = 0;
1974: return;
1975: }
1976: for ( t = BDY(p), n = 0; t; t = NEXT(t), n++ );
1977: w = (MP *)ALLOCA(n*sizeof(MP));
1978: for ( t = BDY(p), i = 0; i < n; t = NEXT(t), i++ )
1979: w[i] = t;
1980: cmp_mp_nvar = NV(p);
1981: qsort(w,n,sizeof(MP),(int (*)(const void *,const void *))comp_mp);
1982: mp0 = 0;
1983: for ( i = n-1; i >= 0; i-- ) {
1984: NEWMP(mp); mp->dl = w[i]->dl; C(mp) = C(w[i]);
1985: NEXT(mp) = mp0; mp0 = mp;
1986: }
1987: MKDP(p->nv,mp0,r);
1988: r->sugar = p->sugar;
1989: *rp = r;
1990: }
1991:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>