Annotation of OpenXM_contrib2/asir2000/builtin/dp-supp.c, Revision 1.9
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.9 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/dp-supp.c,v 1.8 2000/12/08 06:43:09 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "base.h"
52: #include "parse.h"
53: #include "ox.h"
54:
1.5 noro 55: #define HMAG(p) (p_mag(BDY(p)->c))
56:
1.1 noro 57: extern int (*cmpdl)();
1.5 noro 58: extern double pz_t_e,pz_t_d,pz_t_d1,pz_t_c;
59: extern int dp_nelim,dp_fcoeffs;
1.7 noro 60: extern int NoGCD;
61: extern int GenTrace;
62: extern NODE TraceList;
63:
64: /*
65: * content reduction
66: *
67: */
68:
69: void dp_ptozp(p,rp)
70: DP p,*rp;
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:
97: void dp_ptozp2(p0,p1,hp,rp)
98: DP p0,p1;
99: DP *hp,*rp;
100: {
101: DP t,s,h,r;
102: MP m,mr,mr0,m0;
103:
104: addd(CO,p0,p1,&t); dp_ptozp(t,&s);
105: if ( !p0 ) {
106: h = 0; r = s;
107: } else if ( !p1 ) {
108: h = s; r = 0;
109: } else {
110: for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
111: m = NEXT(m), m0 = NEXT(m0) ) {
112: NEXTMP(mr0,mr); mr->c = m->c; mr->dl = m->dl;
113: }
114: NEXT(mr) = 0; MKDP(p0->nv,mr0,h); MKDP(p0->nv,m,r);
115: }
116: if ( h )
117: h->sugar = p0->sugar;
118: if ( r )
119: r->sugar = p1->sugar;
120: *hp = h; *rp = r;
121: }
1.1 noro 122:
123: void dp_idiv(p,c,rp)
124: DP p;
125: Q c;
126: DP *rp;
127: {
128: Q t;
129: N nm,q;
130: int sgn,s;
131: MP mr0,m,mr;
132:
133: if ( !p )
134: *rp = 0;
135: else if ( MUNIQ((Q)c) )
136: *rp = p;
137: else if ( MUNIQ((Q)c) )
138: chsgnd(p,rp);
139: else {
140: nm = NM(c); sgn = SGN(c);
141: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
142: NEXTMP(mr0,mr);
143:
144: divsn(NM((Q)(m->c)),nm,&q);
145: s = sgn*SGN((Q)(m->c));
146: NTOQ(q,s,t);
147: mr->c = (P)t;
148: mr->dl = m->dl;
149: }
150: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp);
151: if ( *rp )
152: (*rp)->sugar = p->sugar;
153: }
154: }
155:
156: void dp_mbase(hlist,mbase)
157: NODE hlist;
158: NODE *mbase;
159: {
160: DL *dl;
161: DL d;
162: int i,j,n,nvar,td;
163:
164: n = length(hlist); nvar = ((DP)BDY(hlist))->nv;
165: dl = (DL *)MALLOC(n*sizeof(DL));
166: for ( i = 0; i < n; i++, hlist = NEXT(hlist) )
167: dl[i] = BDY((DP)BDY(hlist))->dl;
168: NEWDL(d,nvar); *mbase = 0;
169: while ( 1 ) {
170: insert_to_node(d,mbase,nvar);
171: for ( i = nvar-1; i >= 0; ) {
172: d->d[i]++; d->td++;
173: for ( j = 0; j < n; j++ ) {
174: if ( _dl_redble(dl[j],d,nvar) )
175: break;
176: }
177: if ( j < n ) {
178: for ( j = nvar-1; j >= i; j-- )
179: d->d[j] = 0;
180: for ( j = 0, td = 0; j < i; j++ )
181: td += d->d[j];
182: d->td = td;
183: i--;
184: } else
185: break;
186: }
187: if ( i < 0 )
188: break;
189: }
190: }
191:
192: int _dl_redble(d1,d2,nvar)
193: DL d1,d2;
194: int nvar;
195: {
196: int i;
197:
198: if ( d1->td > d2->td )
199: return 0;
200: for ( i = 0; i < nvar; i++ )
201: if ( d1->d[i] > d2->d[i] )
202: break;
203: if ( i < nvar )
204: return 0;
205: else
206: return 1;
207: }
208:
209: void insert_to_node(d,n,nvar)
210: DL d;
211: NODE *n;
212: int nvar;
213: {
214: DL d1;
215: MP m;
216: DP dp;
217: NODE n0,n1,n2;
218:
219: NEWDL(d1,nvar); d1->td = d->td;
220: bcopy((char *)d->d,(char *)d1->d,nvar*sizeof(int));
221: NEWMP(m); m->dl = d1; m->c = (P)ONE; NEXT(m) = 0;
222: MKDP(nvar,m,dp); dp->sugar = d->td;
223: if ( !(*n) ) {
224: MKNODE(n1,dp,0); *n = n1;
225: } else {
226: for ( n1 = *n, n0 = 0; n1; n0 = n1, n1 = NEXT(n1) )
227: if ( (*cmpdl)(nvar,d,BDY((DP)BDY(n1))->dl) > 0 ) {
228: MKNODE(n2,dp,n1);
229: if ( !n0 )
230: *n = n2;
231: else
232: NEXT(n0) = n2;
233: break;
234: }
235: if ( !n1 ) {
236: MKNODE(n2,dp,0); NEXT(n0) = n2;
237: }
238: }
239: }
240:
241: void dp_vtod(c,p,rp)
242: Q *c;
243: DP p;
244: DP *rp;
245: {
246: MP mr0,m,mr;
247: int i;
248:
249: if ( !p )
250: *rp = 0;
251: else {
252: for ( mr0 = 0, m = BDY(p), i = 0; m; m = NEXT(m), i++ ) {
253: NEXTMP(mr0,mr); mr->c = (P)c[i]; mr->dl = m->dl;
254: }
255: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp);
256: (*rp)->sugar = p->sugar;
257: }
258: }
259:
1.8 noro 260: extern int mpi_mag;
261: extern int PCoeffs;
262:
263: void dp_ptozp_d(p,rp)
1.1 noro 264: DP p,*rp;
265: {
266: int i,j,k,l,n,nsep;
267: MP m;
268: NODE tn,n0,n1,n2,n3;
269: struct oVECT v;
270: VECT c,cs;
271: VECT qi,ri;
272: LIST *qr;
273: int s,id;
274: Obj dmy;
275: Q d0,d1,gcd,a,u,u1;
276: Q *q,*r;
277: STRING iqr_v;
278: pointer *b;
279: N qn,gn;
280: double get_rtime();
281: int blen;
1.8 noro 282: NODE dist;
283: int ndist;
1.1 noro 284: double t0;
285: double t_e,t_d,t_d1,t_c;
1.8 noro 286: extern int DP_NFStat;
287: extern LIST Dist;
1.1 noro 288:
289: if ( !p )
290: *rp = 0;
291: else {
1.8 noro 292: if ( PCoeffs ) {
293: dp_ptozp(p,rp); return;
294: }
1.9 ! noro 295: if ( !Dist || p_mag(BDY(p)->c) <= mpi_mag ) {
1.8 noro 296: dist = 0; ndist = 0;
297: if ( DP_NFStat ) fprintf(asir_out,"L");
298: } else {
299: dist = BDY(Dist); ndist = length(dist);
300: if ( DP_NFStat ) fprintf(asir_out,"D");
301: }
1.1 noro 302: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
303: nsep = ndist + 1;
304: if ( n <= nsep ) {
305: dp_ptozp(p,rp); return;
306: }
307: t0 = get_rtime();
308: dp_dtov(p,&c);
309: igcdv_estimate(c,&d0);
310: t_e = get_rtime()-t0;
311: t0 = get_rtime();
312: dp_dtov(p,&c);
313: sepvect(c,nsep,&cs);
314: MKSTR(iqr_v,"iqr");
315: qr = (LIST *)CALLOC(nsep,sizeof(LIST));
316: q = (Q *)CALLOC(n,sizeof(Q));
317: r = (Q *)CALLOC(n,sizeof(Q));
318: for ( i = 0, tn = dist, b = BDY(cs); i < ndist; i++, tn = NEXT(tn) ) {
319: MKNODE(n3,d0,0); MKNODE(n2,b[i],n3);
320: MKNODE(n1,iqr_v,n2); MKNODE(n0,BDY(tn),n1);
321: Pox_rpc(n0,&dmy);
322: }
323: iqrv(b[i],d0,&qr[i]);
324: dp_dtov(p,&c);
325: for ( i = 0, tn = dist; i < ndist; i++, tn = NEXT(tn) ) {
326: Pox_pop_local(tn,&qr[i]);
327: if ( OID(qr[i]) == O_ERR ) {
328: printexpr(CO,(Obj)qr[i]);
329: error("dp_ptozp_d : aborted");
330: }
331: }
332: t_d = get_rtime()-t0;
333: t_d1 = t_d/n;
334: t0 = get_rtime();
335: for ( i = j = 0; i < nsep; i++ ) {
336: tn = BDY(qr[i]); qi = (VECT)BDY(tn); ri = (VECT)BDY(NEXT(tn));
337: for ( k = 0, l = qi->len; k < l; k++, j++ ) {
338: q[j] = (Q)BDY(qi)[k]; r[j] = (Q)BDY(ri)[k];
339: }
340: }
341: v.id = O_VECT; v.len = n; v.body = (pointer *)r; igcdv(&v,&d1);
342: if ( d1 ) {
343: gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
344: divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
345: for ( i = 0; i < n; i++ ) {
346: mulq(a,q[i],&u);
347: if ( r[i] ) {
348: divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
349: addq(u,u1,&q[i]);
350: } else
351: q[i] = u;
352: }
353: } else
354: gcd = d0;
355: dp_vtod(q,p,rp);
356: t_c = get_rtime()-t0;
357: blen=p_mag((P)gcd);
358: pz_t_e += t_e; pz_t_d += t_d; pz_t_d1 += t_d1; pz_t_c += t_c;
359: if ( 0 )
360: fprintf(stderr,"(%d,%d)",p_mag((P)d0)-blen,blen);
361: }
362: }
363:
1.8 noro 364: void dp_ptozp2_d(p0,p1,hp,rp)
1.1 noro 365: DP p0,p1;
366: DP *hp,*rp;
367: {
368: DP t,s,h,r;
369: MP m,mr,mr0,m0;
370:
1.8 noro 371: addd(CO,p0,p1,&t); dp_ptozp_d(t,&s);
1.1 noro 372: if ( !p0 ) {
373: h = 0; r = s;
374: } else if ( !p1 ) {
375: h = s; r = 0;
376: } else {
377: for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
378: m = NEXT(m), m0 = NEXT(m0) ) {
379: NEXTMP(mr0,mr); mr->c = m->c; mr->dl = m->dl;
380: }
381: NEXT(mr) = 0; MKDP(p0->nv,mr0,h); MKDP(p0->nv,m,r);
382: }
383: if ( h )
384: h->sugar = p0->sugar;
385: if ( r )
386: r->sugar = p1->sugar;
387: *hp = h; *rp = r;
1.5 noro 388: }
389:
1.7 noro 390: void dp_prim(p,rp)
391: DP p,*rp;
1.5 noro 392: {
1.7 noro 393: P t,g;
394: DP p1;
395: MP m,mr,mr0;
396: int i,n;
397: P *w;
398: Q *c;
399: Q dvr;
1.5 noro 400:
1.7 noro 401: if ( !p )
402: *rp = 0;
403: else if ( dp_fcoeffs )
404: *rp = p;
405: else if ( NoGCD )
406: dp_ptozp(p,rp);
407: else {
408: dp_ptozp(p,&p1); p = p1;
409: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
410: if ( n == 1 ) {
411: m = BDY(p);
412: NEWMP(mr); mr->dl = m->dl; mr->c = (P)ONE; NEXT(mr) = 0;
413: MKDP(p->nv,mr,*rp); (*rp)->sugar = p->sugar;
414: return;
415: }
416: w = (P *)ALLOCA(n*sizeof(P));
417: c = (Q *)ALLOCA(n*sizeof(Q));
418: for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
419: if ( NUM(m->c) ) {
420: c[i] = (Q)m->c; w[i] = (P)ONE;
421: } else
422: ptozp(m->c,1,&c[i],&w[i]);
423: qltozl(c,n,&dvr); heu_nezgcdnpz(CO,w,n,&t); mulp(CO,t,(P)dvr,&g);
424: if ( NUM(g) )
425: *rp = p;
426: else {
427: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
428: NEXTMP(mr0,mr); divsp(CO,m->c,g,&mr->c); mr->dl = m->dl;
429: }
430: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.5 noro 431: }
1.7 noro 432: }
1.5 noro 433: }
434:
435: void heu_nezgcdnpz(vl,pl,m,pr)
436: VL vl;
437: P *pl,*pr;
438: int m;
439: {
440: int i,r;
441: P gcd,t,s1,s2,u;
442: Q rq;
443:
444: while ( 1 ) {
445: for ( i = 0, s1 = 0; i < m; i++ ) {
446: r = random(); UTOQ(r,rq);
447: mulp(vl,pl[i],(P)rq,&t); addp(vl,s1,t,&u); s1 = u;
448: }
449: for ( i = 0, s2 = 0; i < m; i++ ) {
450: r = random(); UTOQ(r,rq);
451: mulp(vl,pl[i],(P)rq,&t); addp(vl,s2,t,&u); s2 = u;
452: }
453: ezgcdp(vl,s1,s2,&gcd);
454: for ( i = 0; i < m; i++ ) {
455: if ( !divtpz(vl,pl[i],gcd,&t) )
456: break;
457: }
458: if ( i == m )
459: break;
460: }
461: *pr = gcd;
462: }
463:
464: void dp_prim_mod(p,mod,rp)
465: int mod;
466: DP p,*rp;
467: {
468: P t,g;
469: MP m,mr,mr0;
470:
471: if ( !p )
472: *rp = 0;
473: else if ( NoGCD )
474: *rp = p;
475: else {
476: for ( m = BDY(p), g = m->c, m = NEXT(m); m; m = NEXT(m) ) {
477: gcdprsmp(CO,mod,g,m->c,&t); g = t;
478: }
479: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
480: NEXTMP(mr0,mr); divsmp(CO,mod,m->c,g,&mr->c); mr->dl = m->dl;
481: }
482: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
483: }
484: }
485:
1.7 noro 486: void dp_cont(p,rp)
1.5 noro 487: DP p;
1.7 noro 488: Q *rp;
1.5 noro 489: {
1.7 noro 490: VECT v;
1.5 noro 491:
1.7 noro 492: dp_dtov(p,&v); igcdv(v,rp);
1.5 noro 493: }
494:
1.7 noro 495: void dp_dtov(dp,rp)
496: DP dp;
497: VECT *rp;
1.5 noro 498: {
1.7 noro 499: MP m,t;
500: int i,n;
501: VECT v;
502: pointer *p;
1.5 noro 503:
1.7 noro 504: m = BDY(dp);
505: for ( t = m, n = 0; t; t = NEXT(t), n++ );
506: MKVECT(v,n);
507: for ( i = 0, p = BDY(v), t = m; i < n; t = NEXT(t), i++ )
508: p[i] = (pointer)(t->c);
509: *rp = v;
1.5 noro 510: }
511:
1.7 noro 512: /*
513: * s-poly computation
514: *
515: */
1.5 noro 516:
1.7 noro 517: void dp_sp(p1,p2,rp)
518: DP p1,p2;
1.5 noro 519: DP *rp;
520: {
1.7 noro 521: int i,n,td;
522: int *w;
523: DL d1,d2,d;
524: MP m;
525: DP t,s1,s2,u;
526: Q c,c1,c2;
527: N gn,tn;
1.5 noro 528:
1.7 noro 529: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
530: w = (int *)ALLOCA(n*sizeof(int));
531: for ( i = 0, td = 0; i < n; i++ ) {
532: w[i] = MAX(d1->d[i],d2->d[i]); td += w[i];
1.5 noro 533: }
1.7 noro 534:
535: NEWDL(d,n); d->td = td - d1->td;
536: for ( i = 0; i < n; i++ )
537: d->d[i] = w[i] - d1->d[i];
538: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
539: if ( INT(c1) && INT(c2) ) {
540: gcdn(NM(c1),NM(c2),&gn);
541: if ( !UNIN(gn) ) {
542: divsn(NM(c1),gn,&tn); NTOQ(tn,SGN(c1),c); c1 = c;
543: divsn(NM(c2),gn,&tn); NTOQ(tn,SGN(c2),c); c2 = c;
1.5 noro 544: }
545: }
1.7 noro 546:
547: NEWMP(m); m->dl = d; m->c = (P)c2; NEXT(m) = 0;
548: MKDP(n,m,s1); s1->sugar = d->td; muld(CO,s1,p1,&t);
549:
550: NEWDL(d,n); d->td = td - d2->td;
551: for ( i = 0; i < n; i++ )
552: d->d[i] = w[i] - d2->d[i];
553: NEWMP(m); m->dl = d; m->c = (P)c1; NEXT(m) = 0;
554: MKDP(n,m,s2); s2->sugar = d->td; muld(CO,s2,p2,&u);
555:
556: subd(CO,t,u,rp);
557: if ( GenTrace ) {
558: LIST hist;
559: NODE node;
560:
561: node = mknode(4,ONE,0,s1,ONE);
562: MKLIST(hist,node);
563: MKNODE(TraceList,hist,0);
564:
565: node = mknode(4,ONE,0,0,ONE);
566: chsgnd(s2,(DP *)&ARG2(node));
567: MKLIST(hist,node);
568: MKNODE(node,hist,TraceList); TraceList = node;
569: }
570: }
571:
572: void dp_sp_mod(p1,p2,mod,rp)
573: DP p1,p2;
574: int mod;
575: DP *rp;
576: {
577: int i,n,td;
578: int *w;
579: DL d1,d2,d;
580: MP m;
581: DP t,s,u;
582:
583: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
584: w = (int *)ALLOCA(n*sizeof(int));
585: for ( i = 0, td = 0; i < n; i++ ) {
586: w[i] = MAX(d1->d[i],d2->d[i]); td += w[i];
587: }
588: NEWDL(d,n); d->td = td - d1->td;
589: for ( i = 0; i < n; i++ )
590: d->d[i] = w[i] - d1->d[i];
591: NEWMP(m); m->dl = d; m->c = (P)BDY(p2)->c; NEXT(m) = 0;
592: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p1,s,&t);
593: NEWDL(d,n); d->td = td - d2->td;
594: for ( i = 0; i < n; i++ )
595: d->d[i] = w[i] - d2->d[i];
596: NEWMP(m); m->dl = d; m->c = (P)BDY(p1)->c; NEXT(m) = 0;
597: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p2,s,&u);
598: submd(CO,mod,t,u,rp);
599: }
600:
601: void _dp_sp_mod_dup(p1,p2,mod,rp)
602: DP p1,p2;
603: int mod;
604: DP *rp;
605: {
606: int i,n,td;
607: int *w;
608: DL d1,d2,d;
609: MP m;
610: DP t,s,u;
611:
612: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
613: w = (int *)ALLOCA(n*sizeof(int));
614: for ( i = 0, td = 0; i < n; i++ ) {
615: w[i] = MAX(d1->d[i],d2->d[i]); td += w[i];
616: }
617: _NEWDL(d,n); d->td = td - d1->td;
618: for ( i = 0; i < n; i++ )
619: d->d[i] = w[i] - d1->d[i];
620: _NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
621: _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p1,&t); _free_dp(s);
622: _NEWDL(d,n); d->td = td - d2->td;
623: for ( i = 0; i < n; i++ )
624: d->d[i] = w[i] - d2->d[i];
625: _NEWMP(m); m->dl = d; m->c = STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
626: _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p2,&u); _free_dp(s);
627: _addmd_destructive(mod,t,u,rp);
628: }
629:
630: void _dp_sp_mod(p1,p2,mod,rp)
631: DP p1,p2;
632: int mod;
633: DP *rp;
634: {
635: int i,n,td;
636: int *w;
637: DL d1,d2,d;
638: MP m;
639: DP t,s,u;
640:
641: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
642: w = (int *)ALLOCA(n*sizeof(int));
643: for ( i = 0, td = 0; i < n; i++ ) {
644: w[i] = MAX(d1->d[i],d2->d[i]); td += w[i];
645: }
646: NEWDL(d,n); d->td = td - d1->td;
647: for ( i = 0; i < n; i++ )
648: d->d[i] = w[i] - d1->d[i];
649: NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
650: MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p1,&t);
651: NEWDL(d,n); d->td = td - d2->td;
652: for ( i = 0; i < n; i++ )
653: d->d[i] = w[i] - d2->d[i];
654: NEWMP(m); m->dl = d; m->c = STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
655: MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p2,&u);
656: addmd_destructive(mod,t,u,rp);
657: }
658:
659: /*
660: * m-reduction
661: *
662: */
663:
664: void dp_red(p0,p1,p2,head,rest,dnp,multp)
665: DP p0,p1,p2;
666: DP *head,*rest;
667: P *dnp;
668: DP *multp;
669: {
670: int i,n;
671: DL d1,d2,d;
672: MP m;
673: DP t,s,r,h;
674: Q c,c1,c2;
675: N gn,tn;
676: P g,a;
677:
678: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
679: NEWDL(d,n); d->td = d1->td - d2->td;
680: for ( i = 0; i < n; i++ )
681: d->d[i] = d1->d[i]-d2->d[i];
682: c1 = (Q)BDY(p1)->c; c2 = (Q)BDY(p2)->c;
683: if ( dp_fcoeffs ) {
684: /* do nothing */
685: } else 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: } else {
692: ezgcdpz(CO,(P)c1,(P)c2,&g);
693: divsp(CO,(P)c1,g,&a); c1 = (Q)a; divsp(CO,(P)c2,g,&a); c2 = (Q)a;
694: }
695: NEWMP(m); m->dl = d; chsgnp((P)c1,&m->c); NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
696: *multp = s;
697: muld(CO,s,p2,&t); muldc(CO,p1,(P)c2,&s); addd(CO,s,t,&r);
698: muldc(CO,p0,(P)c2,&h);
699: *head = h; *rest = r; *dnp = (P)c2;
700: }
701:
702: void dp_red_mod(p0,p1,p2,mod,head,rest,dnp)
703: DP p0,p1,p2;
704: int mod;
705: DP *head,*rest;
706: P *dnp;
707: {
708: int i,n;
709: DL d1,d2,d;
710: MP m;
711: DP t,s,r,h;
712: P c1,c2,g,u;
713:
714: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
715: NEWDL(d,n); d->td = d1->td - d2->td;
716: for ( i = 0; i < n; i++ )
717: d->d[i] = d1->d[i]-d2->d[i];
718: c1 = (P)BDY(p1)->c; c2 = (P)BDY(p2)->c;
719: gcdprsmp(CO,mod,c1,c2,&g);
720: divsmp(CO,mod,c1,g,&u); c1 = u; divsmp(CO,mod,c2,g,&u); c2 = u;
721: if ( NUM(c2) ) {
722: divsmp(CO,mod,c1,c2,&u); c1 = u; c2 = (P)ONEM;
723: }
724: NEWMP(m); m->dl = d; chsgnmp(mod,(P)c1,&m->c); NEXT(m) = 0;
725: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p2,s,&t);
726: if ( NUM(c2) ) {
727: addmd(CO,mod,p1,t,&r); h = p0;
728: } else {
729: mulmdc(CO,mod,p1,c2,&s); addmd(CO,mod,s,t,&r); mulmdc(CO,mod,p0,c2,&h);
730: }
731: *head = h; *rest = r; *dnp = c2;
732: }
733:
734: void _dp_red_mod_destructive(p1,p2,mod,rp)
735: DP p1,p2;
736: int mod;
737: DP *rp;
738: {
739: int i,n;
740: DL d1,d2,d;
741: MP m;
742: DP t,s;
743: int c,c1;
744:
745: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
746: _NEWDL(d,n); d->td = d1->td - d2->td;
747: for ( i = 0; i < n; i++ )
748: d->d[i] = d1->d[i]-d2->d[i];
749: c = invm(ITOS(BDY(p2)->c),mod); c1 = dmar(c,ITOS(BDY(p1)->c),0,mod);
750: _NEWMP(m); m->dl = d; m->c = STOI(mod-c1); NEXT(m) = 0;
751: _MKDP(n,m,s); s->sugar = d->td;
752: _mulmd_dup(mod,s,p2,&t); _free_dp(s);
753: _addmd_destructive(mod,p1,t,rp);
754: }
755:
756: /*
757: * normal form computation
758: *
759: */
1.5 noro 760:
761: void dp_true_nf(b,g,ps,full,rp,dnp)
762: NODE b;
763: DP g;
764: DP *ps;
765: int full;
766: DP *rp;
767: P *dnp;
768: {
769: DP u,p,d,s,t,dmy;
770: NODE l;
771: MP m,mr;
772: int i,n;
773: int *wb;
774: int sugar,psugar;
775: P dn,tdn,tdn1;
776:
777: dn = (P)ONE;
778: if ( !g ) {
779: *rp = 0; *dnp = dn; return;
780: }
781: for ( n = 0, l = b; l; l = NEXT(l), n++ );
782: wb = (int *)ALLOCA(n*sizeof(int));
783: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
784: wb[i] = QTOS((Q)BDY(l));
785: sugar = g->sugar;
786: for ( d = 0; g; ) {
787: for ( u = 0, i = 0; i < n; i++ ) {
788: if ( dp_redble(g,p = ps[wb[i]]) ) {
789: dp_red(d,g,p,&t,&u,&tdn,&dmy);
790: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
791: sugar = MAX(sugar,psugar);
792: if ( !u ) {
793: if ( d )
794: d->sugar = sugar;
795: *rp = d; *dnp = dn; return;
796: } else {
797: d = t;
798: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
799: }
800: break;
801: }
802: }
803: if ( u )
804: g = u;
805: else if ( !full ) {
806: if ( g ) {
807: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
808: }
809: *rp = g; *dnp = dn; return;
810: } else {
811: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
812: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
813: addd(CO,d,t,&s); d = s;
814: dp_rest(g,&t); g = t;
815: }
816: }
817: if ( d )
818: d->sugar = sugar;
819: *rp = d; *dnp = dn;
820: }
821:
822: void dp_nf_ptozp(b,g,ps,full,multiple,rp)
823: NODE b;
824: DP g;
825: DP *ps;
826: int full,multiple;
827: DP *rp;
828: {
829: DP u,p,d,s,t,dmy1;
830: P dmy;
831: NODE l;
832: MP m,mr;
833: int i,n;
834: int *wb;
835: int hmag;
836: int sugar,psugar;
837:
838: if ( !g ) {
839: *rp = 0; return;
840: }
841: for ( n = 0, l = b; l; l = NEXT(l), n++ );
842: wb = (int *)ALLOCA(n*sizeof(int));
843: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
844: wb[i] = QTOS((Q)BDY(l));
845: hmag = multiple*HMAG(g);
846: sugar = g->sugar;
847: for ( d = 0; g; ) {
848: for ( u = 0, i = 0; i < n; i++ ) {
849: if ( dp_redble(g,p = ps[wb[i]]) ) {
850: dp_red(d,g,p,&t,&u,&dmy,&dmy1);
851: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
852: sugar = MAX(sugar,psugar);
853: if ( !u ) {
854: if ( d )
855: d->sugar = sugar;
856: *rp = d; return;
857: }
858: d = t;
859: break;
860: }
861: }
862: if ( u ) {
863: g = u;
864: if ( d ) {
1.7 noro 865: if ( multiple && HMAG(d) > hmag ) {
1.5 noro 866: dp_ptozp2(d,g,&t,&u); d = t; g = u;
867: hmag = multiple*HMAG(d);
868: }
869: } else {
1.7 noro 870: if ( multiple && HMAG(g) > hmag ) {
1.5 noro 871: dp_ptozp(g,&t); g = t;
872: hmag = multiple*HMAG(g);
873: }
874: }
875: }
876: else if ( !full ) {
877: if ( g ) {
878: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
879: }
880: *rp = g; return;
881: } else {
882: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
883: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
884: addd(CO,d,t,&s); d = s;
885: dp_rest(g,&t); g = t;
886:
887: }
888: }
889: if ( d )
890: d->sugar = sugar;
891: *rp = d;
892: }
893:
894: void dp_nf_mod(b,g,ps,mod,full,rp)
895: NODE b;
896: DP g;
897: DP *ps;
898: int mod,full;
899: DP *rp;
900: {
901: DP u,p,d,s,t;
902: P dmy;
903: NODE l;
904: MP m,mr;
905: int sugar,psugar;
906:
907: if ( !g ) {
908: *rp = 0; return;
909: }
910: sugar = g->sugar;
911: for ( d = 0; g; ) {
912: for ( u = 0, l = b; l; l = NEXT(l) ) {
913: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
914: dp_red_mod(d,g,p,mod,&t,&u,&dmy);
915: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
916: sugar = MAX(sugar,psugar);
917: if ( !u ) {
918: if ( d )
919: d->sugar = sugar;
920: *rp = d; return;
921: }
922: d = t;
923: break;
924: }
925: }
926: if ( u )
927: g = u;
928: else if ( !full ) {
929: if ( g ) {
930: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
931: }
932: *rp = g; return;
933: } else {
934: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
935: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
936: addmd(CO,mod,d,t,&s); d = s;
937: dp_rest(g,&t); g = t;
938: }
939: }
940: if ( d )
941: d->sugar = sugar;
942: *rp = d;
943: }
944:
945: void dp_true_nf_mod(b,g,ps,mod,full,rp,dnp)
946: NODE b;
947: DP g;
948: DP *ps;
949: int mod,full;
950: DP *rp;
951: P *dnp;
952: {
953: DP u,p,d,s,t;
954: NODE l;
955: MP m,mr;
956: int i,n;
957: int *wb;
958: int sugar,psugar;
959: P dn,tdn,tdn1;
960:
961: dn = (P)ONEM;
962: if ( !g ) {
963: *rp = 0; *dnp = dn; return;
964: }
965: for ( n = 0, l = b; l; l = NEXT(l), n++ );
966: wb = (int *)ALLOCA(n*sizeof(int));
967: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
968: wb[i] = QTOS((Q)BDY(l));
969: sugar = g->sugar;
970: for ( d = 0; g; ) {
971: for ( u = 0, i = 0; i < n; i++ ) {
972: if ( dp_redble(g,p = ps[wb[i]]) ) {
973: dp_red_mod(d,g,p,mod,&t,&u,&tdn);
974: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
975: sugar = MAX(sugar,psugar);
976: if ( !u ) {
977: if ( d )
978: d->sugar = sugar;
979: *rp = d; *dnp = dn; return;
980: } else {
981: d = t;
982: mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
983: }
984: break;
985: }
986: }
987: if ( u )
988: g = u;
989: else if ( !full ) {
990: if ( g ) {
991: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
992: }
993: *rp = g; *dnp = dn; return;
994: } else {
995: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
996: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
997: addmd(CO,mod,d,t,&s); d = s;
998: dp_rest(g,&t); g = t;
999: }
1000: }
1001: if ( d )
1002: d->sugar = sugar;
1003: *rp = d; *dnp = dn;
1004: }
1005:
1.7 noro 1006: void _dp_nf_mod_destructive(b,g,ps,mod,full,rp)
1007: NODE b;
1008: DP g;
1009: DP *ps;
1010: int mod,full;
1011: DP *rp;
1.5 noro 1012: {
1.7 noro 1013: DP u,p,d,s,t;
1014: NODE l;
1015: MP m,mr,mrd;
1016: int sugar,psugar,n,h_reducible,i;
1.5 noro 1017:
1.7 noro 1018: if ( !g ) {
1019: *rp = 0; return;
1.5 noro 1020: }
1.7 noro 1021: sugar = g->sugar;
1022: n = g->nv;
1023: for ( d = 0; g; ) {
1024: for ( h_reducible = 0, l = b; l; l = NEXT(l) ) {
1025: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
1026: h_reducible = 1;
1027: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1028: _dp_red_mod_destructive(g,p,mod,&u); g = u;
1029: sugar = MAX(sugar,psugar);
1030: if ( !g ) {
1031: if ( d )
1032: d->sugar = sugar;
1033: _dptodp(d,rp); _free_dp(d); return;
1034: }
1035: break;
1036: }
1037: }
1038: if ( !h_reducible ) {
1039: /* head term is not reducible */
1040: if ( !full ) {
1041: if ( g )
1042: g->sugar = sugar;
1043: _dptodp(g,rp); _free_dp(g); return;
1044: } else {
1045: m = BDY(g);
1046: if ( NEXT(m) ) {
1047: BDY(g) = NEXT(m); NEXT(m) = 0;
1048: } else {
1049: _FREEDP(g); g = 0;
1050: }
1051: if ( d ) {
1052: for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
1053: NEXT(mrd) = m;
1054: } else {
1055: _MKDP(n,m,d);
1056: }
1057: }
1058: }
1.5 noro 1059: }
1.7 noro 1060: if ( d )
1061: d->sugar = sugar;
1062: _dptodp(d,rp); _free_dp(d);
1.5 noro 1063: }
1064:
1.7 noro 1065: void dp_lnf_mod(p1,p2,g,mod,r1p,r2p)
1066: DP p1,p2;
1067: NODE g;
1068: int mod;
1069: DP *r1p,*r2p;
1.5 noro 1070: {
1.7 noro 1071: DP r1,r2,b1,b2,t,s;
1072: P c;
1073: MQ c1,c2;
1074: NODE l,b;
1075: int n;
1076:
1077: if ( !p1 ) {
1078: *r1p = p1; *r2p = p2; return;
1079: }
1080: n = p1->nv;
1081: for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
1082: if ( !r1 ) {
1083: *r1p = r1; *r2p = r2; return;
1084: }
1085: b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
1086: if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
1087: b2 = (DP)BDY(NEXT(b));
1088: invmq(mod,(MQ)BDY(b1)->c,&c1);
1089: mulmq(mod,c1,(MQ)BDY(r1)->c,&c2); chsgnmp(mod,(P)c2,&c);
1090: mulmdc(CO,mod,b1,c,&t); addmd(CO,mod,r1,t,&s); r1 = s;
1091: mulmdc(CO,mod,b2,c,&t); addmd(CO,mod,r2,t,&s); r2 = s;
1092: }
1093: }
1094: *r1p = r1; *r2p = r2;
1.5 noro 1095: }
1096:
1.7 noro 1097: void dp_nf_tab_mod(p,tab,mod,rp)
1098: DP p;
1099: LIST *tab;
1100: int mod;
1101: DP *rp;
1.5 noro 1102: {
1.7 noro 1103: DP s,t,u;
1104: MP m;
1105: DL h;
1106: int i,n;
1107:
1108: if ( !p ) {
1109: *rp = p; return;
1110: }
1111: n = p->nv;
1112: for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
1113: h = m->dl;
1114: while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
1115: i++;
1116: mulmdc(CO,mod,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
1117: addmd(CO,mod,s,t,&u); s = u;
1118: }
1119: *rp = s;
1.5 noro 1120: }
1121:
1.7 noro 1122: /*
1123: * setting flags
1124: *
1125: */
1126:
1127: int create_order_spec(obj,spec)
1128: Obj obj;
1129: struct order_spec *spec;
1.5 noro 1130: {
1.7 noro 1131: int i,j,n,s,row,col;
1132: struct order_pair *l;
1133: NODE node,t,tn;
1134: MAT m;
1135: pointer **b;
1136: int **w;
1.5 noro 1137:
1.7 noro 1138: if ( !obj || NUM(obj) ) {
1139: spec->id = 0; spec->obj = obj;
1140: spec->ord.simple = QTOS((Q)obj);
1141: return 1;
1142: } else if ( OID(obj) == O_LIST ) {
1143: node = BDY((LIST)obj);
1144: for ( n = 0, t = node; t; t = NEXT(t), n++ );
1145: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
1146: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
1147: tn = BDY((LIST)BDY(t)); l[i].order = QTOS((Q)BDY(tn));
1148: tn = NEXT(tn); l[i].length = QTOS((Q)BDY(tn));
1149: s += l[i].length;
1150: }
1151: spec->id = 1; spec->obj = obj;
1152: spec->ord.block.order_pair = l;
1153: spec->ord.block.length = n; spec->nv = s;
1154: return 1;
1155: } else if ( OID(obj) == O_MAT ) {
1156: m = (MAT)obj; row = m->row; col = m->col; b = BDY(m);
1157: w = almat(row,col);
1158: for ( i = 0; i < row; i++ )
1159: for ( j = 0; j < col; j++ )
1160: w[i][j] = QTOS((Q)b[i][j]);
1161: spec->id = 2; spec->obj = obj;
1162: spec->nv = col; spec->ord.matrix.row = row;
1163: spec->ord.matrix.matrix = w;
1164: return 1;
1165: } else
1.5 noro 1166: return 0;
1167: }
1168:
1.7 noro 1169: /*
1170: * converters
1171: *
1172: */
1173:
1174: void dp_homo(p,rp)
1175: DP p;
1176: DP *rp;
1.5 noro 1177: {
1.7 noro 1178: MP m,mr,mr0;
1179: int i,n,nv,td;
1180: DL dl,dlh;
1.5 noro 1181:
1.7 noro 1182: if ( !p )
1183: *rp = 0;
1184: else {
1185: n = p->nv; nv = n + 1;
1186: m = BDY(p); td = sugard(m);
1187: for ( mr0 = 0; m; m = NEXT(m) ) {
1188: NEXTMP(mr0,mr); mr->c = m->c;
1189: dl = m->dl;
1190: mr->dl = dlh = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
1191: dlh->td = td;
1192: for ( i = 0; i < n; i++ )
1193: dlh->d[i] = dl->d[i];
1194: dlh->d[n] = td - dl->td;
1195: }
1196: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.5 noro 1197: }
1198: }
1199:
1.7 noro 1200: void dp_dehomo(p,rp)
1201: DP p;
1.5 noro 1202: DP *rp;
1203: {
1.7 noro 1204: MP m,mr,mr0;
1205: int i,n,nv;
1206: DL dl,dlh;
1.5 noro 1207:
1.7 noro 1208: if ( !p )
1209: *rp = 0;
1210: else {
1211: n = p->nv; nv = n - 1;
1212: m = BDY(p);
1213: for ( mr0 = 0; m; m = NEXT(m) ) {
1214: NEXTMP(mr0,mr); mr->c = m->c;
1215: dlh = m->dl;
1216: mr->dl = dl = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
1217: dl->td = dlh->td - dlh->d[nv];
1218: for ( i = 0; i < nv; i++ )
1219: dl->d[i] = dlh->d[i];
1220: }
1221: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
1222: }
1.5 noro 1223: }
1224:
1.7 noro 1225: void dp_mod(p,mod,subst,rp)
1226: DP p;
1227: int mod;
1228: NODE subst;
1.5 noro 1229: DP *rp;
1230: {
1.7 noro 1231: MP m,mr,mr0;
1232: P t,s,s1;
1233: V v;
1234: NODE tn;
1.5 noro 1235:
1.7 noro 1236: if ( !p )
1237: *rp = 0;
1238: else {
1239: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
1240: for ( tn = subst, s = m->c; tn; tn = NEXT(tn) ) {
1241: v = VR((P)BDY(tn)); tn = NEXT(tn);
1242: substp(CO,s,v,(P)BDY(tn),&s1); s = s1;
1243: }
1244: ptomp(mod,s,&t);
1245: if ( t ) {
1246: NEXTMP(mr0,mr); mr->c = t; mr->dl = m->dl;
1247: }
1248: }
1249: if ( mr0 ) {
1250: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1251: } else
1252: *rp = 0;
1253: }
1.5 noro 1254: }
1255:
1.7 noro 1256: void dp_rat(p,rp)
1257: DP p;
1258: DP *rp;
1.5 noro 1259: {
1.7 noro 1260: MP m,mr,mr0;
1.5 noro 1261:
1.7 noro 1262: if ( !p )
1263: *rp = 0;
1264: else {
1265: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
1266: NEXTMP(mr0,mr); mptop(m->c,&mr->c); mr->dl = m->dl;
1.5 noro 1267: }
1.7 noro 1268: if ( mr0 ) {
1269: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1270: } else
1271: *rp = 0;
1.5 noro 1272: }
1273: }
1274:
1275:
1.7 noro 1276: void homogenize_order(old,n,new)
1277: struct order_spec *old,*new;
1278: int n;
1.5 noro 1279: {
1.7 noro 1280: struct order_pair *l;
1281: int length,nv,row,i,j;
1282: int **newm,**oldm;
1.5 noro 1283:
1.7 noro 1284: switch ( old->id ) {
1285: case 0:
1286: switch ( old->ord.simple ) {
1287: case 0:
1288: new->id = 0; new->ord.simple = 0; break;
1289: case 1:
1290: l = (struct order_pair *)
1291: MALLOC_ATOMIC(2*sizeof(struct order_pair));
1292: l[0].length = n; l[0].order = 1;
1293: l[1].length = 1; l[1].order = 2;
1294: new->id = 1;
1295: new->ord.block.order_pair = l;
1296: new->ord.block.length = 2; new->nv = n+1;
1297: break;
1298: case 2:
1299: new->id = 0; new->ord.simple = 1; break;
1300: case 3: case 4: case 5:
1301: new->id = 0; new->ord.simple = old->ord.simple+3;
1302: dp_nelim = n-1; break;
1303: case 6: case 7: case 8: case 9:
1304: new->id = 0; new->ord.simple = old->ord.simple; break;
1305: default:
1306: error("homogenize_order : invalid input");
1307: }
1308: break;
1309: case 1:
1310: length = old->ord.block.length;
1311: l = (struct order_pair *)
1312: MALLOC_ATOMIC((length+1)*sizeof(struct order_pair));
1313: bcopy((char *)old->ord.block.order_pair,(char *)l,length*sizeof(struct order_pair));
1314: l[length].order = 2; l[length].length = 1;
1315: new->id = 1; new->nv = n+1;
1316: new->ord.block.order_pair = l;
1317: new->ord.block.length = length+1;
1318: break;
1319: case 2:
1320: nv = old->nv; row = old->ord.matrix.row;
1321: oldm = old->ord.matrix.matrix; newm = almat(row+1,nv+1);
1322: for ( i = 0; i <= nv; i++ )
1323: newm[0][i] = 1;
1324: for ( i = 0; i < row; i++ ) {
1325: for ( j = 0; j < nv; j++ )
1326: newm[i+1][j] = oldm[i][j];
1327: newm[i+1][j] = 0;
1328: }
1329: new->id = 2; new->nv = nv+1;
1330: new->ord.matrix.row = row+1; new->ord.matrix.matrix = newm;
1331: break;
1332: default:
1333: error("homogenize_order : invalid input");
1.5 noro 1334: }
1.7 noro 1335: }
1336:
1337: void qltozl(w,n,dvr)
1338: Q *w,*dvr;
1339: int n;
1340: {
1341: N nm,dn;
1342: N g,l1,l2,l3;
1343: Q c,d;
1344: int i;
1345: struct oVECT v;
1.5 noro 1346:
1347: for ( i = 0; i < n; i++ )
1.7 noro 1348: if ( w[i] && !INT(w[i]) )
1349: break;
1350: if ( i == n ) {
1351: v.id = O_VECT; v.len = n; v.body = (pointer *)w;
1352: igcdv(&v,dvr); return;
1353: }
1354: c = w[0]; nm = NM(c); dn = INT(c) ? ONEN : DN(c);
1355: for ( i = 1; i < n; i++ ) {
1356: c = w[i]; l1 = INT(c) ? ONEN : DN(c);
1357: gcdn(nm,NM(c),&g); nm = g;
1358: gcdn(dn,l1,&l2); muln(dn,l1,&l3); divsn(l3,l2,&dn);
1.5 noro 1359: }
1.7 noro 1360: if ( UNIN(dn) )
1361: NTOQ(nm,1,d);
1362: else
1363: NDTOQ(nm,dn,1,d);
1364: *dvr = d;
1365: }
1.5 noro 1366:
1.7 noro 1367: int comp_nm(a,b)
1368: Q *a,*b;
1369: {
1370: return cmpn((*a)?NM(*a):0,(*b)?NM(*b):0);
1371: }
1372:
1373: void sortbynm(w,n)
1374: Q *w;
1375: int n;
1376: {
1377: qsort(w,n,sizeof(Q),(int (*)(const void *,const void *))comp_nm);
1378: }
1.5 noro 1379:
1380:
1.7 noro 1381: /*
1382: * simple operations
1383: *
1384: */
1.5 noro 1385:
1.7 noro 1386: int dp_redble(p1,p2)
1387: DP p1,p2;
1388: {
1389: int i,n;
1390: DL d1,d2;
1.5 noro 1391:
1.7 noro 1392: d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
1393: if ( d1->td < d2->td )
1394: return 0;
1395: else {
1396: for ( i = 0, n = p1->nv; i < n; i++ )
1397: if ( d1->d[i] < d2->d[i] )
1398: return 0;
1399: return 1;
1.5 noro 1400: }
1401: }
1402:
1.7 noro 1403: void dp_subd(p1,p2,rp)
1.5 noro 1404: DP p1,p2;
1405: DP *rp;
1406: {
1.7 noro 1407: int i,n;
1.5 noro 1408: DL d1,d2,d;
1409: MP m;
1.7 noro 1410: DP s;
1.5 noro 1411:
1412: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
1.7 noro 1413: NEWDL(d,n); d->td = d1->td - d2->td;
1.5 noro 1414: for ( i = 0; i < n; i++ )
1.7 noro 1415: d->d[i] = d1->d[i]-d2->d[i];
1416: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
1417: MKDP(n,m,s); s->sugar = d->td;
1418: *rp = s;
1419: }
1420:
1421: void dltod(d,n,rp)
1422: DL d;
1423: int n;
1424: DP *rp;
1425: {
1426: MP m;
1427: DP s;
1428:
1429: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
1430: MKDP(n,m,s); s->sugar = d->td;
1431: *rp = s;
1.5 noro 1432: }
1433:
1434: void dp_hm(p,rp)
1435: DP p;
1436: DP *rp;
1437: {
1438: MP m,mr;
1439:
1440: if ( !p )
1441: *rp = 0;
1442: else {
1443: m = BDY(p);
1444: NEWMP(mr); mr->dl = m->dl; mr->c = m->c; NEXT(mr) = 0;
1445: MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td; /* XXX */
1446: }
1447: }
1448:
1449: void dp_rest(p,rp)
1450: DP p,*rp;
1451: {
1452: MP m;
1453:
1454: m = BDY(p);
1455: if ( !NEXT(m) )
1456: *rp = 0;
1457: else {
1458: MKDP(p->nv,NEXT(m),*rp);
1459: if ( *rp )
1460: (*rp)->sugar = p->sugar;
1461: }
1462: }
1463:
1464: DL lcm_of_DL(nv,dl1,dl2,dl)
1465: int nv;
1466: DL dl1,dl2;
1467: register DL dl;
1468: {
1469: register int n, *d1, *d2, *d, td;
1470:
1471: if ( !dl ) NEWDL(dl,nv);
1472: d = dl->d, d1 = dl1->d, d2 = dl2->d;
1473: for ( td = 0, n = nv; --n >= 0; d1++, d2++, d++ )
1474: td += (*d = *d1 > *d2 ? *d1 : *d2 );
1475: dl->td = td;
1476: return dl;
1477: }
1478:
1479: int dl_equal(nv,dl1,dl2)
1480: int nv;
1481: DL dl1, dl2;
1482: {
1483: register int *d1, *d2, n;
1484:
1485: if ( dl1->td != dl2->td ) return 0;
1486: for ( d1 = dl1->d, d2 = dl2->d, n = nv; --n >= 0; d1++, d2++ )
1487: if ( *d1 != *d2 ) return 0;
1488: return 1;
1489: }
1490:
1491: int dp_nt(p)
1492: DP p;
1493: {
1494: int i;
1495: MP m;
1496:
1497: if ( !p )
1498: return 0;
1499: else {
1500: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
1501: return i;
1502: }
1503: }
1504:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>