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