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