Annotation of OpenXM_contrib2/asir2000/builtin/dp-supp.c, Revision 1.12
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.12 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/dp-supp.c,v 1.11 2000/12/13 05:37:29 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;
1.11 noro 725: MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,s,p2,&t);
1.7 noro 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:
1.10 noro 734: struct oEGT eg_red_mod;
735:
1.7 noro 736: void _dp_red_mod_destructive(p1,p2,mod,rp)
737: DP p1,p2;
738: int mod;
739: DP *rp;
740: {
741: int i,n;
742: DL d1,d2,d;
743: MP m;
744: DP t,s;
745: int c,c1;
1.10 noro 746: struct oEGT t0,t1;
1.7 noro 747:
748: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
749: _NEWDL(d,n); d->td = d1->td - d2->td;
750: for ( i = 0; i < n; i++ )
751: d->d[i] = d1->d[i]-d2->d[i];
752: c = invm(ITOS(BDY(p2)->c),mod); c1 = dmar(c,ITOS(BDY(p1)->c),0,mod);
753: _NEWMP(m); m->dl = d; m->c = STOI(mod-c1); NEXT(m) = 0;
754: _MKDP(n,m,s); s->sugar = d->td;
755: _mulmd_dup(mod,s,p2,&t); _free_dp(s);
1.10 noro 756: /* get_eg(&t0); */
1.7 noro 757: _addmd_destructive(mod,p1,t,rp);
1.10 noro 758: /* get_eg(&t1); add_eg(&eg_red_mod,&t0,&t1); */
1.7 noro 759: }
760:
761: /*
762: * normal form computation
763: *
764: */
1.5 noro 765:
766: void dp_true_nf(b,g,ps,full,rp,dnp)
767: NODE b;
768: DP g;
769: DP *ps;
770: int full;
771: DP *rp;
772: P *dnp;
773: {
774: DP u,p,d,s,t,dmy;
775: NODE l;
776: MP m,mr;
777: int i,n;
778: int *wb;
779: int sugar,psugar;
780: P dn,tdn,tdn1;
781:
782: dn = (P)ONE;
783: if ( !g ) {
784: *rp = 0; *dnp = dn; return;
785: }
786: for ( n = 0, l = b; l; l = NEXT(l), n++ );
787: wb = (int *)ALLOCA(n*sizeof(int));
788: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
789: wb[i] = QTOS((Q)BDY(l));
790: sugar = g->sugar;
791: for ( d = 0; g; ) {
792: for ( u = 0, i = 0; i < n; i++ ) {
793: if ( dp_redble(g,p = ps[wb[i]]) ) {
794: dp_red(d,g,p,&t,&u,&tdn,&dmy);
795: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
796: sugar = MAX(sugar,psugar);
797: if ( !u ) {
798: if ( d )
799: d->sugar = sugar;
800: *rp = d; *dnp = dn; return;
801: } else {
802: d = t;
803: mulp(CO,dn,tdn,&tdn1); dn = tdn1;
804: }
805: break;
806: }
807: }
808: if ( u )
809: g = u;
810: else if ( !full ) {
811: if ( g ) {
812: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
813: }
814: *rp = g; *dnp = dn; return;
815: } else {
816: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
817: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
818: addd(CO,d,t,&s); d = s;
819: dp_rest(g,&t); g = t;
820: }
821: }
822: if ( d )
823: d->sugar = sugar;
824: *rp = d; *dnp = dn;
825: }
826:
827: void dp_nf_ptozp(b,g,ps,full,multiple,rp)
828: NODE b;
829: DP g;
830: DP *ps;
831: int full,multiple;
832: DP *rp;
833: {
834: DP u,p,d,s,t,dmy1;
835: P dmy;
836: NODE l;
837: MP m,mr;
838: int i,n;
839: int *wb;
840: int hmag;
841: int sugar,psugar;
1.12 ! noro 842: int fcoef;
1.5 noro 843:
844: if ( !g ) {
845: *rp = 0; return;
846: }
847: for ( n = 0, l = b; l; l = NEXT(l), n++ );
848: wb = (int *)ALLOCA(n*sizeof(int));
849: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
850: wb[i] = QTOS((Q)BDY(l));
1.12 ! noro 851:
! 852: /* check whether polys have coeffs in finite fields */
! 853: fcoef = 0;
! 854: for ( i = 0; i < n; i++ )
! 855: if ( has_fcoef(ps[wb[i]]) ) {
! 856: fcoef = 1;
! 857: break;
! 858: }
! 859: if ( has_fcoef(g) )
! 860: fcoef = 1;
! 861:
! 862: if ( !fcoef )
! 863: hmag = multiple*HMAG(g);
1.5 noro 864: sugar = g->sugar;
1.12 ! noro 865:
1.5 noro 866: for ( d = 0; g; ) {
867: for ( u = 0, i = 0; i < n; i++ ) {
868: if ( dp_redble(g,p = ps[wb[i]]) ) {
869: dp_red(d,g,p,&t,&u,&dmy,&dmy1);
870: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
871: sugar = MAX(sugar,psugar);
872: if ( !u ) {
873: if ( d )
874: d->sugar = sugar;
875: *rp = d; return;
876: }
877: d = t;
878: break;
879: }
880: }
881: if ( u ) {
882: g = u;
883: if ( d ) {
1.12 ! noro 884: if ( !fcoef && multiple && HMAG(d) > hmag ) {
1.5 noro 885: dp_ptozp2(d,g,&t,&u); d = t; g = u;
886: hmag = multiple*HMAG(d);
887: }
888: } else {
1.12 ! noro 889: if ( !fcoef && multiple && HMAG(g) > hmag ) {
1.5 noro 890: dp_ptozp(g,&t); g = t;
891: hmag = multiple*HMAG(g);
892: }
893: }
894: }
895: else if ( !full ) {
896: if ( g ) {
897: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
898: }
899: *rp = g; return;
900: } else {
901: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
902: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
903: addd(CO,d,t,&s); d = s;
904: dp_rest(g,&t); g = t;
905:
906: }
907: }
908: if ( d )
909: d->sugar = sugar;
910: *rp = d;
911: }
912:
913: void dp_nf_mod(b,g,ps,mod,full,rp)
914: NODE b;
915: DP g;
916: DP *ps;
917: int mod,full;
918: DP *rp;
919: {
920: DP u,p,d,s,t;
921: P dmy;
922: NODE l;
923: MP m,mr;
924: int sugar,psugar;
925:
926: if ( !g ) {
927: *rp = 0; return;
928: }
929: sugar = g->sugar;
930: for ( d = 0; g; ) {
931: for ( u = 0, l = b; l; l = NEXT(l) ) {
932: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
933: dp_red_mod(d,g,p,mod,&t,&u,&dmy);
934: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
935: sugar = MAX(sugar,psugar);
936: if ( !u ) {
937: if ( d )
938: d->sugar = sugar;
939: *rp = d; return;
940: }
941: d = t;
942: break;
943: }
944: }
945: if ( u )
946: g = u;
947: else if ( !full ) {
948: if ( g ) {
949: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
950: }
951: *rp = g; return;
952: } else {
953: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
954: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
955: addmd(CO,mod,d,t,&s); d = s;
956: dp_rest(g,&t); g = t;
957: }
958: }
959: if ( d )
960: d->sugar = sugar;
961: *rp = d;
962: }
963:
964: void dp_true_nf_mod(b,g,ps,mod,full,rp,dnp)
965: NODE b;
966: DP g;
967: DP *ps;
968: int mod,full;
969: DP *rp;
970: P *dnp;
971: {
972: DP u,p,d,s,t;
973: NODE l;
974: MP m,mr;
975: int i,n;
976: int *wb;
977: int sugar,psugar;
978: P dn,tdn,tdn1;
979:
980: dn = (P)ONEM;
981: if ( !g ) {
982: *rp = 0; *dnp = dn; return;
983: }
984: for ( n = 0, l = b; l; l = NEXT(l), n++ );
985: wb = (int *)ALLOCA(n*sizeof(int));
986: for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
987: wb[i] = QTOS((Q)BDY(l));
988: sugar = g->sugar;
989: for ( d = 0; g; ) {
990: for ( u = 0, i = 0; i < n; i++ ) {
991: if ( dp_redble(g,p = ps[wb[i]]) ) {
992: dp_red_mod(d,g,p,mod,&t,&u,&tdn);
993: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
994: sugar = MAX(sugar,psugar);
995: if ( !u ) {
996: if ( d )
997: d->sugar = sugar;
998: *rp = d; *dnp = dn; return;
999: } else {
1000: d = t;
1001: mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
1002: }
1003: break;
1004: }
1005: }
1006: if ( u )
1007: g = u;
1008: else if ( !full ) {
1009: if ( g ) {
1010: MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
1011: }
1012: *rp = g; *dnp = dn; return;
1013: } else {
1014: m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
1015: NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
1016: addmd(CO,mod,d,t,&s); d = s;
1017: dp_rest(g,&t); g = t;
1018: }
1019: }
1020: if ( d )
1021: d->sugar = sugar;
1022: *rp = d; *dnp = dn;
1023: }
1024:
1.7 noro 1025: void _dp_nf_mod_destructive(b,g,ps,mod,full,rp)
1026: NODE b;
1027: DP g;
1028: DP *ps;
1029: int mod,full;
1030: DP *rp;
1.5 noro 1031: {
1.7 noro 1032: DP u,p,d,s,t;
1033: NODE l;
1034: MP m,mr,mrd;
1035: int sugar,psugar,n,h_reducible,i;
1.5 noro 1036:
1.7 noro 1037: if ( !g ) {
1038: *rp = 0; return;
1.5 noro 1039: }
1.7 noro 1040: sugar = g->sugar;
1041: n = g->nv;
1042: for ( d = 0; g; ) {
1043: for ( h_reducible = 0, l = b; l; l = NEXT(l) ) {
1044: if ( dp_redble(g,p = ps[(int)BDY(l)]) ) {
1045: h_reducible = 1;
1046: psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
1047: _dp_red_mod_destructive(g,p,mod,&u); g = u;
1048: sugar = MAX(sugar,psugar);
1049: if ( !g ) {
1050: if ( d )
1051: d->sugar = sugar;
1052: _dptodp(d,rp); _free_dp(d); return;
1053: }
1054: break;
1055: }
1056: }
1057: if ( !h_reducible ) {
1058: /* head term is not reducible */
1059: if ( !full ) {
1060: if ( g )
1061: g->sugar = sugar;
1062: _dptodp(g,rp); _free_dp(g); return;
1063: } else {
1064: m = BDY(g);
1065: if ( NEXT(m) ) {
1066: BDY(g) = NEXT(m); NEXT(m) = 0;
1067: } else {
1068: _FREEDP(g); g = 0;
1069: }
1070: if ( d ) {
1071: for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
1072: NEXT(mrd) = m;
1073: } else {
1074: _MKDP(n,m,d);
1075: }
1076: }
1077: }
1.5 noro 1078: }
1.7 noro 1079: if ( d )
1080: d->sugar = sugar;
1081: _dptodp(d,rp); _free_dp(d);
1.5 noro 1082: }
1083:
1.7 noro 1084: void dp_lnf_mod(p1,p2,g,mod,r1p,r2p)
1085: DP p1,p2;
1086: NODE g;
1087: int mod;
1088: DP *r1p,*r2p;
1.5 noro 1089: {
1.7 noro 1090: DP r1,r2,b1,b2,t,s;
1091: P c;
1092: MQ c1,c2;
1093: NODE l,b;
1094: int n;
1095:
1096: if ( !p1 ) {
1097: *r1p = p1; *r2p = p2; return;
1098: }
1099: n = p1->nv;
1100: for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
1101: if ( !r1 ) {
1102: *r1p = r1; *r2p = r2; return;
1103: }
1104: b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
1105: if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
1106: b2 = (DP)BDY(NEXT(b));
1107: invmq(mod,(MQ)BDY(b1)->c,&c1);
1108: mulmq(mod,c1,(MQ)BDY(r1)->c,&c2); chsgnmp(mod,(P)c2,&c);
1109: mulmdc(CO,mod,b1,c,&t); addmd(CO,mod,r1,t,&s); r1 = s;
1110: mulmdc(CO,mod,b2,c,&t); addmd(CO,mod,r2,t,&s); r2 = s;
1111: }
1112: }
1113: *r1p = r1; *r2p = r2;
1.5 noro 1114: }
1115:
1.7 noro 1116: void dp_nf_tab_mod(p,tab,mod,rp)
1117: DP p;
1118: LIST *tab;
1119: int mod;
1120: DP *rp;
1.5 noro 1121: {
1.7 noro 1122: DP s,t,u;
1123: MP m;
1124: DL h;
1125: int i,n;
1126:
1127: if ( !p ) {
1128: *rp = p; return;
1129: }
1130: n = p->nv;
1131: for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
1132: h = m->dl;
1133: while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
1134: i++;
1135: mulmdc(CO,mod,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
1136: addmd(CO,mod,s,t,&u); s = u;
1137: }
1138: *rp = s;
1.5 noro 1139: }
1140:
1.7 noro 1141: /*
1142: * setting flags
1143: *
1144: */
1145:
1146: int create_order_spec(obj,spec)
1147: Obj obj;
1148: struct order_spec *spec;
1.5 noro 1149: {
1.7 noro 1150: int i,j,n,s,row,col;
1151: struct order_pair *l;
1152: NODE node,t,tn;
1153: MAT m;
1154: pointer **b;
1155: int **w;
1.5 noro 1156:
1.7 noro 1157: if ( !obj || NUM(obj) ) {
1158: spec->id = 0; spec->obj = obj;
1159: spec->ord.simple = QTOS((Q)obj);
1160: return 1;
1161: } else if ( OID(obj) == O_LIST ) {
1162: node = BDY((LIST)obj);
1163: for ( n = 0, t = node; t; t = NEXT(t), n++ );
1164: l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
1165: for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
1166: tn = BDY((LIST)BDY(t)); l[i].order = QTOS((Q)BDY(tn));
1167: tn = NEXT(tn); l[i].length = QTOS((Q)BDY(tn));
1168: s += l[i].length;
1169: }
1170: spec->id = 1; spec->obj = obj;
1171: spec->ord.block.order_pair = l;
1172: spec->ord.block.length = n; spec->nv = s;
1173: return 1;
1174: } else if ( OID(obj) == O_MAT ) {
1175: m = (MAT)obj; row = m->row; col = m->col; b = BDY(m);
1176: w = almat(row,col);
1177: for ( i = 0; i < row; i++ )
1178: for ( j = 0; j < col; j++ )
1179: w[i][j] = QTOS((Q)b[i][j]);
1180: spec->id = 2; spec->obj = obj;
1181: spec->nv = col; spec->ord.matrix.row = row;
1182: spec->ord.matrix.matrix = w;
1183: return 1;
1184: } else
1.5 noro 1185: return 0;
1186: }
1187:
1.7 noro 1188: /*
1189: * converters
1190: *
1191: */
1192:
1193: void dp_homo(p,rp)
1194: DP p;
1195: DP *rp;
1.5 noro 1196: {
1.7 noro 1197: MP m,mr,mr0;
1198: int i,n,nv,td;
1199: DL dl,dlh;
1.5 noro 1200:
1.7 noro 1201: if ( !p )
1202: *rp = 0;
1203: else {
1204: n = p->nv; nv = n + 1;
1205: m = BDY(p); td = sugard(m);
1206: for ( mr0 = 0; m; m = NEXT(m) ) {
1207: NEXTMP(mr0,mr); mr->c = m->c;
1208: dl = m->dl;
1209: mr->dl = dlh = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
1210: dlh->td = td;
1211: for ( i = 0; i < n; i++ )
1212: dlh->d[i] = dl->d[i];
1213: dlh->d[n] = td - dl->td;
1214: }
1215: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.5 noro 1216: }
1217: }
1218:
1.7 noro 1219: void dp_dehomo(p,rp)
1220: DP p;
1.5 noro 1221: DP *rp;
1222: {
1.7 noro 1223: MP m,mr,mr0;
1224: int i,n,nv;
1225: DL dl,dlh;
1.5 noro 1226:
1.7 noro 1227: if ( !p )
1228: *rp = 0;
1229: else {
1230: n = p->nv; nv = n - 1;
1231: m = BDY(p);
1232: for ( mr0 = 0; m; m = NEXT(m) ) {
1233: NEXTMP(mr0,mr); mr->c = m->c;
1234: dlh = m->dl;
1235: mr->dl = dl = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
1236: dl->td = dlh->td - dlh->d[nv];
1237: for ( i = 0; i < nv; i++ )
1238: dl->d[i] = dlh->d[i];
1239: }
1240: NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
1241: }
1.5 noro 1242: }
1243:
1.7 noro 1244: void dp_mod(p,mod,subst,rp)
1245: DP p;
1246: int mod;
1247: NODE subst;
1.5 noro 1248: DP *rp;
1249: {
1.7 noro 1250: MP m,mr,mr0;
1251: P t,s,s1;
1252: V v;
1253: NODE tn;
1.5 noro 1254:
1.7 noro 1255: if ( !p )
1256: *rp = 0;
1257: else {
1258: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
1259: for ( tn = subst, s = m->c; tn; tn = NEXT(tn) ) {
1260: v = VR((P)BDY(tn)); tn = NEXT(tn);
1261: substp(CO,s,v,(P)BDY(tn),&s1); s = s1;
1262: }
1263: ptomp(mod,s,&t);
1264: if ( t ) {
1265: NEXTMP(mr0,mr); mr->c = t; mr->dl = m->dl;
1266: }
1267: }
1268: if ( mr0 ) {
1269: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1270: } else
1271: *rp = 0;
1272: }
1.5 noro 1273: }
1274:
1.7 noro 1275: void dp_rat(p,rp)
1276: DP p;
1277: DP *rp;
1.5 noro 1278: {
1.7 noro 1279: MP m,mr,mr0;
1.5 noro 1280:
1.7 noro 1281: if ( !p )
1282: *rp = 0;
1283: else {
1284: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
1285: NEXTMP(mr0,mr); mptop(m->c,&mr->c); mr->dl = m->dl;
1.5 noro 1286: }
1.7 noro 1287: if ( mr0 ) {
1288: NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1289: } else
1290: *rp = 0;
1.5 noro 1291: }
1292: }
1293:
1294:
1.7 noro 1295: void homogenize_order(old,n,new)
1296: struct order_spec *old,*new;
1297: int n;
1.5 noro 1298: {
1.7 noro 1299: struct order_pair *l;
1300: int length,nv,row,i,j;
1301: int **newm,**oldm;
1.5 noro 1302:
1.7 noro 1303: switch ( old->id ) {
1304: case 0:
1305: switch ( old->ord.simple ) {
1306: case 0:
1307: new->id = 0; new->ord.simple = 0; break;
1308: case 1:
1309: l = (struct order_pair *)
1310: MALLOC_ATOMIC(2*sizeof(struct order_pair));
1311: l[0].length = n; l[0].order = 1;
1312: l[1].length = 1; l[1].order = 2;
1313: new->id = 1;
1314: new->ord.block.order_pair = l;
1315: new->ord.block.length = 2; new->nv = n+1;
1316: break;
1317: case 2:
1318: new->id = 0; new->ord.simple = 1; break;
1319: case 3: case 4: case 5:
1320: new->id = 0; new->ord.simple = old->ord.simple+3;
1321: dp_nelim = n-1; break;
1322: case 6: case 7: case 8: case 9:
1323: new->id = 0; new->ord.simple = old->ord.simple; break;
1324: default:
1325: error("homogenize_order : invalid input");
1326: }
1327: break;
1328: case 1:
1329: length = old->ord.block.length;
1330: l = (struct order_pair *)
1331: MALLOC_ATOMIC((length+1)*sizeof(struct order_pair));
1332: bcopy((char *)old->ord.block.order_pair,(char *)l,length*sizeof(struct order_pair));
1333: l[length].order = 2; l[length].length = 1;
1334: new->id = 1; new->nv = n+1;
1335: new->ord.block.order_pair = l;
1336: new->ord.block.length = length+1;
1337: break;
1338: case 2:
1339: nv = old->nv; row = old->ord.matrix.row;
1340: oldm = old->ord.matrix.matrix; newm = almat(row+1,nv+1);
1341: for ( i = 0; i <= nv; i++ )
1342: newm[0][i] = 1;
1343: for ( i = 0; i < row; i++ ) {
1344: for ( j = 0; j < nv; j++ )
1345: newm[i+1][j] = oldm[i][j];
1346: newm[i+1][j] = 0;
1347: }
1348: new->id = 2; new->nv = nv+1;
1349: new->ord.matrix.row = row+1; new->ord.matrix.matrix = newm;
1350: break;
1351: default:
1352: error("homogenize_order : invalid input");
1.5 noro 1353: }
1.7 noro 1354: }
1355:
1356: void qltozl(w,n,dvr)
1357: Q *w,*dvr;
1358: int n;
1359: {
1360: N nm,dn;
1361: N g,l1,l2,l3;
1362: Q c,d;
1363: int i;
1364: struct oVECT v;
1.5 noro 1365:
1366: for ( i = 0; i < n; i++ )
1.7 noro 1367: if ( w[i] && !INT(w[i]) )
1368: break;
1369: if ( i == n ) {
1370: v.id = O_VECT; v.len = n; v.body = (pointer *)w;
1371: igcdv(&v,dvr); return;
1372: }
1373: c = w[0]; nm = NM(c); dn = INT(c) ? ONEN : DN(c);
1374: for ( i = 1; i < n; i++ ) {
1375: c = w[i]; l1 = INT(c) ? ONEN : DN(c);
1376: gcdn(nm,NM(c),&g); nm = g;
1377: gcdn(dn,l1,&l2); muln(dn,l1,&l3); divsn(l3,l2,&dn);
1.5 noro 1378: }
1.7 noro 1379: if ( UNIN(dn) )
1380: NTOQ(nm,1,d);
1381: else
1382: NDTOQ(nm,dn,1,d);
1383: *dvr = d;
1384: }
1.5 noro 1385:
1.7 noro 1386: int comp_nm(a,b)
1387: Q *a,*b;
1388: {
1389: return cmpn((*a)?NM(*a):0,(*b)?NM(*b):0);
1390: }
1391:
1392: void sortbynm(w,n)
1393: Q *w;
1394: int n;
1395: {
1396: qsort(w,n,sizeof(Q),(int (*)(const void *,const void *))comp_nm);
1397: }
1.5 noro 1398:
1399:
1.7 noro 1400: /*
1401: * simple operations
1402: *
1403: */
1.5 noro 1404:
1.7 noro 1405: int dp_redble(p1,p2)
1406: DP p1,p2;
1407: {
1408: int i,n;
1409: DL d1,d2;
1.5 noro 1410:
1.7 noro 1411: d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
1412: if ( d1->td < d2->td )
1413: return 0;
1414: else {
1415: for ( i = 0, n = p1->nv; i < n; i++ )
1416: if ( d1->d[i] < d2->d[i] )
1417: return 0;
1418: return 1;
1.5 noro 1419: }
1420: }
1421:
1.7 noro 1422: void dp_subd(p1,p2,rp)
1.5 noro 1423: DP p1,p2;
1424: DP *rp;
1425: {
1.7 noro 1426: int i,n;
1.5 noro 1427: DL d1,d2,d;
1428: MP m;
1.7 noro 1429: DP s;
1.5 noro 1430:
1431: n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
1.7 noro 1432: NEWDL(d,n); d->td = d1->td - d2->td;
1.5 noro 1433: for ( i = 0; i < n; i++ )
1.7 noro 1434: d->d[i] = d1->d[i]-d2->d[i];
1435: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
1436: MKDP(n,m,s); s->sugar = d->td;
1437: *rp = s;
1438: }
1439:
1440: void dltod(d,n,rp)
1441: DL d;
1442: int n;
1443: DP *rp;
1444: {
1445: MP m;
1446: DP s;
1447:
1448: NEWMP(m); m->dl = d; m->c = (P)ONE; NEXT(m) = 0;
1449: MKDP(n,m,s); s->sugar = d->td;
1450: *rp = s;
1.5 noro 1451: }
1452:
1453: void dp_hm(p,rp)
1454: DP p;
1455: DP *rp;
1456: {
1457: MP m,mr;
1458:
1459: if ( !p )
1460: *rp = 0;
1461: else {
1462: m = BDY(p);
1463: NEWMP(mr); mr->dl = m->dl; mr->c = m->c; NEXT(mr) = 0;
1464: MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td; /* XXX */
1465: }
1466: }
1467:
1468: void dp_rest(p,rp)
1469: DP p,*rp;
1470: {
1471: MP m;
1472:
1473: m = BDY(p);
1474: if ( !NEXT(m) )
1475: *rp = 0;
1476: else {
1477: MKDP(p->nv,NEXT(m),*rp);
1478: if ( *rp )
1479: (*rp)->sugar = p->sugar;
1480: }
1481: }
1482:
1483: DL lcm_of_DL(nv,dl1,dl2,dl)
1484: int nv;
1485: DL dl1,dl2;
1486: register DL dl;
1487: {
1488: register int n, *d1, *d2, *d, td;
1489:
1490: if ( !dl ) NEWDL(dl,nv);
1491: d = dl->d, d1 = dl1->d, d2 = dl2->d;
1492: for ( td = 0, n = nv; --n >= 0; d1++, d2++, d++ )
1493: td += (*d = *d1 > *d2 ? *d1 : *d2 );
1494: dl->td = td;
1495: return dl;
1496: }
1497:
1498: int dl_equal(nv,dl1,dl2)
1499: int nv;
1500: DL dl1, dl2;
1501: {
1502: register int *d1, *d2, n;
1503:
1504: if ( dl1->td != dl2->td ) return 0;
1505: for ( d1 = dl1->d, d2 = dl2->d, n = nv; --n >= 0; d1++, d2++ )
1506: if ( *d1 != *d2 ) return 0;
1507: return 1;
1508: }
1509:
1510: int dp_nt(p)
1511: DP p;
1512: {
1513: int i;
1514: MP m;
1515:
1516: if ( !p )
1517: return 0;
1518: else {
1519: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
1520: return i;
1521: }
1522: }
1523:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>