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