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