Annotation of OpenXM_contrib2/asir2000/lib/gr, Revision 1.7
1.5 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.6 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.5 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.7 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/lib/gr,v 1.6 2000/08/22 05:04:22 noro Exp $
1.5 noro 49: */
1.1 noro 50: extern INIT_COUNT,ITOR_FAIL$
51: extern REMOTE_MATRIX,REMOTE_NF,REMOTE_VARS$
52:
53: #define MAX(a,b) ((a)>(b)?(a):(b))
54: #define HigherDim 0
55: #define ZeroDim 1
56: #define MiniPoly 2
57:
58: /* toplevel functions for Groebner basis computation */
59:
60: def gr(B,V,O)
61: {
62: G = dp_gr_main(B,V,0,1,O);
63: return G;
64: }
65:
66: def hgr(B,V,O)
67: {
68: G = dp_gr_main(B,V,1,1,O);
69: return G;
70: }
71:
72: def gr_mod(B,V,O,M)
73: {
74: G = dp_gr_mod_main(B,V,0,M,O);
75: return G;
76: }
77:
78: def hgr_mod(B,V,O,M)
79: {
80: G = dp_gr_mod_main(B,V,1,M,O);
81: return G;
82: }
83:
84: /* toplevel functions for change-of-ordering */
85:
86: def lex_hensel(B,V,O,W,H)
87: {
88: G = dp_gr_main(B,V,H,1,O);
89: return tolex(G,V,O,W);
90: }
91:
92: def lex_hensel_gsl(B,V,O,W,H)
93: {
94: G = dp_gr_main(B,V,H,1,O);
95: return tolex_gsl(G,V,O,W);
96: }
97:
98: def gr_minipoly(B,V,O,P,V0,H)
99: {
100: G = dp_gr_main(B,V,H,1,O);
101: return minipoly(G,V,O,P,V0);
102: }
103:
104: def lex_tl(B,V,O,W,H)
105: {
106: G = dp_gr_main(B,V,H,1,O);
107: return tolex_tl(G,V,O,W,H);
108: }
109:
110: def tolex_tl(G0,V,O,W,H)
111: {
112: N = length(V); HM = hmlist(G0,V,O); ZD = zero_dim(HM,V,O);
113: for ( I = 0; ; I++ ) {
114: M = lprime(I);
115: if ( !valid_modulus(HM,M) )
116: continue;
117: if ( ZD ) {
118: if ( G3 = dp_gr_main(G0,W,H,-M,3) )
119: for ( J = 0; ; J++ )
120: if ( G2 = dp_gr_main(G3,W,0,-lprime(J),2) )
121: return G2;
122: } else if ( G2 = dp_gr_main(G0,W,H,-M,2) )
123: return G2;
124: }
125: }
126:
127: def tolex(G0,V,O,W)
128: {
129: TM = TE = TNF = 0;
130: N = length(V); HM = hmlist(G0,V,O); ZD = zero_dim(HM,V,O);
131: if ( !ZD )
132: error("tolex : ideal is not zero-dimensional!");
133: MB = dp_mbase(map(dp_ptod,HM,V));
134: for ( J = 0; ; J++ ) {
135: M = lprime(J);
136: if ( !valid_modulus(HM,M) )
137: continue;
138: T0 = time()[0]; GM = tolexm(G0,V,O,W,M); TM += time()[0] - T0;
139: dp_ord(2);
140: DL = map(dp_etov,map(dp_ht,map(dp_ptod,GM,W)));
141: D = newvect(N); TL = [];
142: do
143: TL = cons(dp_dtop(dp_vtoe(D),W),TL);
144: while ( nextm(D,DL,N) );
145: L = npos_check(DL); NPOSV = L[0]; DIM = L[1];
146: T0 = time()[0]; NF = gennf(G0,TL,V,O,W[N-1],1)[0];
147: TNF += time()[0] - T0;
148: T0 = time()[0];
149: R = tolex_main(V,O,NF,GM,M,MB);
150: TE += time()[0] - T0;
151: if ( R ) {
152: if ( dp_gr_print() )
153: print("mod="+rtostr(TM)+",nf="+rtostr(TNF)+",eq="+rtostr(TE));
154: return R;
155: }
156: }
157: }
158:
159: def tolex_gsl(G0,V,O,W)
160: {
161: TM = TE = TNF = 0;
162: N = length(V); HM = hmlist(G0,V,O); ZD = zero_dim(HM,V,O);
163: MB = dp_mbase(map(dp_ptod,HM,V));
164: if ( !ZD )
165: error("tolex_gsl : ideal is not zero-dimensional!");
166: for ( J = 0; ; J++ ) {
167: M = lprime(J);
168: if ( !valid_modulus(HM,M) )
169: continue;
170: T0 = time()[0]; GM = tolexm(G0,V,O,W,M); TM += time()[0] - T0;
171: dp_ord(2);
172: DL = map(dp_etov,map(dp_ht,map(dp_ptod,GM,W)));
173: D = newvect(N); TL = [];
174: do
175: TL = cons(dp_dtop(dp_vtoe(D),W),TL);
176: while ( nextm(D,DL,N) );
177: L = npos_check(DL); NPOSV = L[0]; DIM = L[1];
178: if ( NPOSV >= 0 ) {
179: V0 = W[NPOSV];
180: T0 = time()[0]; NFL = gennf(G0,TL,V,O,V0,1);
181: TNF += time()[0] - T0;
182: T0 = time()[0];
183: R = tolex_gsl_main(G0,V,O,W,NFL,NPOSV,GM,M,MB);
184: TE += time()[0] - T0;
185: } else {
186: T0 = time()[0]; NF = gennf(G0,TL,V,O,W[N-1],1)[0];
187: TNF += time()[0] - T0;
188: T0 = time()[0];
189: R = tolex_main(V,O,NF,GM,M,MB);
190: TE += time()[0] - T0;
191: }
192: if ( R ) {
193: if ( dp_gr_print() )
194: print("mod="+rtostr(TM)+",nf="+rtostr(TNF)+",eq="+rtostr(TE));
195: return R;
196: }
197: }
198: }
199:
200: def termstomat(NF,TERMS,MB,MOD)
201: {
202: DN = NF[1];
203: NF = NF[0];
204: N = length(MB);
205: M = length(TERMS);
206: MAT = newmat(N,M);
207: W = newvect(N);
208: Len = length(NF);
209: for ( I = 0; I < M; I++ ) {
210: T = TERMS[I];
211: for ( K = 0; K < Len; K++ )
212: if ( T == NF[K][1] )
213: break;
214: dptov(NF[K][0],W,MB);
215: for ( J = 0; J < N; J++ )
216: MAT[J][I] = W[J];
217: }
218: return [henleq_prep(MAT,MOD),DN];
219: }
220:
221: def tolex_gsl_main(G0,V,O,W,NFL,NPOSV,GM,M,MB)
222: {
223: NF = NFL[0]; PS = NFL[1]; GI = NFL[2];
224: V0 = W[NPOSV]; N = length(W);
225: DIM = length(MB);
226: DV = newvect(DIM);
227: TERMS = gather_terms(GM,W,M,NPOSV);
228: Len = length(TERMS);
229: dp_ord(O); RHS = termstomat(NF,map(dp_ptod,TERMS,V),MB,M);
230: for ( T = GM; T != []; T = cdr(T) )
231: if ( vars(car(T)) == [V0] )
232: break;
233: dp_ord(0); NHT = nf_tab_gsl(dp_ptod(V0^deg(car(T),V0),V),NF);
234: dptov(NHT[0],DV,MB);
235: B = hen_ttob_gsl([DV,NHT[1]],RHS,TERMS,M);
236: if ( !B )
237: return 0;
238: for ( I = 0, U = B[1]*V0^deg(car(T),V0); I < Len; I++ )
239: U += B[0][I]*TERMS[I];
240: DN0 = diff(U,V0);
241: dp_ord(O); DN0NF = nf_tab_gsl(dp_ptod(DN0,V),NF);
242: SL = [[V0,U,DN0]];
243: for ( I = N-1, LCM = 1; I >= 0; I-- ) {
244: if ( I == NPOSV )
245: continue;
246: V1 = W[I];
247: dp_ord(O); L = nf(GI,DN0NF[0]*dp_ptod(-LCM*V1,V),DN0NF[1],PS);
248: L = remove_cont(L);
249: dptov(L[0],DV,MB);
250: dp_ord(O); B = hen_ttob_gsl([DV,L[1]],RHS,TERMS,M);
251: if ( !B )
252: return 0;
253: for ( K = 0, R = 0; K < Len; K++ )
254: R += B[0][K]*TERMS[K];
255: LCM *= B[1];
256: SL = cons(cons(V1,[R,LCM]),SL);
1.7 ! noro 257: if ( dp_gr_print() )
! 258: print(["DN",B[1]]);
1.1 noro 259: }
260: return SL;
261: }
262:
263: def hen_ttob_gsl(LHS,RHS,TERMS,M)
264: {
265: LDN = LHS[1]; RDN = RHS[1]; LCM = ilcm(LDN,RDN);
266: L1 = idiv(LCM,LDN); R1 = idiv(LCM,RDN);
267: T0 = time()[0];
268: S = henleq_gsl(RHS[0],LHS[0]*L1,M);
1.7 ! noro 269: if ( dp_gr_print() )
! 270: print(["henleq_gsl",time()[0]-T0]);
1.1 noro 271: N = length(TERMS);
272: return [S[0],S[1]*R1];
273: }
274:
275: def gather_terms(GM,W,M,NPOSV)
276: {
277: N = length(W); V0 = W[NPOSV];
278: for ( T = GM; T != []; T = cdr(T) ) {
279: if ( vars(car(T)) == [V0] )
280: break;
281: }
282: U = car(T); DU = diff(U,V0);
283: R = tpoly(cdr(p_terms(U,W,2)));
284: for ( I = 0; I < N; I++ ) {
285: if ( I == NPOSV )
286: continue;
287: V1 = W[I];
288: for ( T = GM; T != []; T = cdr(T) )
289: if ( member(V1,vars(car(T))) )
290: break;
291: P = car(T);
292: R += tpoly(p_terms(srem(DU*coef(P,0,V1),U,M),W,2));
293: }
294: return p_terms(R,W,2);
295: }
296:
297: def tpoly(L)
298: {
299: for ( R = 0; L != []; L = cdr(L) )
300: R += car(L);
301: return R;
302: }
303:
304: def dptov(P,W,MB)
305: {
306: N = size(W)[0];
307: for ( I = 0; I < N; I++ )
308: W[I] = 0;
309: for ( I = 0, S = MB; P; P = dp_rest(P) ) {
310: HM = dp_hm(P); C = dp_hc(HM); T = dp_ht(HM);
311: for ( ; T != car(S); S = cdr(S), I++ );
312: W[I] = C;
313: I++; S = cdr(S);
314: }
315: }
316:
317: def tolex_main(V,O,NF,GM,M,MB)
318: {
319: DIM = length(MB);
320: DV = newvect(DIM);
321: for ( T = GM, SL = [], LCM = 1; T != []; T = cdr(T) ) {
322: S = p_terms(car(T),V,2);
323: dp_ord(O); RHS = termstomat(NF,map(dp_ptod,cdr(S),V),MB,M);
324: dp_ord(0); NHT = nf_tab_gsl(dp_ptod(LCM*car(S),V),NF);
325: dptov(NHT[0],DV,MB);
326: dp_ord(O); B = hen_ttob_gsl([DV,NHT[1]],RHS,cdr(S),M);
327: if ( !B )
328: return 0;
329: Len = length(S);
330: LCM *= B[1];
331: for ( U = LCM*car(S), I = 1; I < Len; I++ )
332: U += B[0][I-1]*S[I];
333: R = ptozp(U);
334: SL = cons(R,SL);
1.7 ! noro 335: if ( dp_gr_print() )
! 336: print(["DN",B[1]]);
1.1 noro 337: }
338: return SL;
339: }
340:
341: def reduce_dn(L)
342: {
343: NM = L[0]; DN = L[1]; V = vars(NM);
344: T = remove_cont([dp_ptod(NM,V),DN]);
345: return [dp_dtop(T[0],V),T[1]];
346: }
347:
348: /* a function for computation of minimal polynomial */
349:
350: def minipoly(G0,V,O,P,V0)
351: {
352: if ( !zero_dim(hmlist(G0,V,O),V,O) )
353: error("tolex : ideal is not zero-dimensional!");
354:
355: G1 = cons(V0-P,G0);
356: O1 = [[0,1],[O,length(V)]];
357: V1 = cons(V0,V);
358: W = append(V,[V0]);
359:
360: N = length(V1);
361: dp_ord(O1);
362: HM = hmlist(G1,V1,O1);
363: MB = dp_mbase(map(dp_ptod,HM,V1));
364: dp_ord(O);
365:
366: for ( J = 0; ; J++ ) {
367: M = lprime(J);
368: if ( !valid_modulus(HM,M) )
369: continue;
370: MP = minipolym(G0,V,O,P,V0,M);
371: for ( D = deg(MP,V0), TL = [], J = 0; J <= D; J++ )
372: TL = cons(V0^J,TL);
373: NF = gennf(G1,TL,V1,O1,V0,1)[0];
374: R = tolex_main(V1,O1,NF,[MP],M,MB);
375: return R[0];
376: }
377: }
378:
379: /* subroutines */
380:
381: def gennf(G,TL,V,O,V0,FLAG)
382: {
383: N = length(V); Len = length(G); dp_ord(O); PS = newvect(Len);
384: for ( I = 0, T = G, HL = []; T != []; T = cdr(T), I++ ) {
385: PS[I] = dp_ptod(car(T),V); HL = cons(dp_ht(PS[I]),HL);
386: }
387: for ( I = 0, DTL = []; TL != []; TL = cdr(TL) )
388: DTL = cons(dp_ptod(car(TL),V),DTL);
389: for ( I = Len - 1, GI = []; I >= 0; I-- )
390: GI = cons(I,GI);
391: T = car(DTL); DTL = cdr(DTL);
392: H = [nf(GI,T,T,PS)];
393:
394: USE_TAB = (FLAG != 0);
395: if ( USE_TAB ) {
396: T0 = time()[0];
397: MB = dp_mbase(HL); DIM = length(MB);
398: U = dp_ptod(V0,V);
399: UTAB = newvect(DIM);
400: for ( I = 0; I < DIM; I++ ) {
401: UTAB[I] = [MB[I],remove_cont(dp_true_nf(GI,U*MB[I],PS,1))];
402: if ( dp_gr_print() )
403: print(".",2);
404: }
1.7 ! noro 405: if ( dp_gr_print() )
! 406: print("");
1.1 noro 407: TTAB = time()[0]-T0;
408: }
409:
410: T0 = time()[0];
411: for ( LCM = 1; DTL != []; ) {
412: if ( dp_gr_print() )
413: print(".",2);
414: T = car(DTL); DTL = cdr(DTL);
415: if ( L = search_redble(T,H) ) {
416: DD = dp_subd(T,L[1]);
417: if ( USE_TAB && (DD == U) ) {
418: NF = nf_tab(L[0],UTAB);
419: NF = [NF[0],dp_hc(L[1])*NF[1]*T];
420: } else
421: NF = nf(GI,L[0]*dp_subd(T,L[1]),dp_hc(L[1])*T,PS);
422: } else
423: NF = nf(GI,T,T,PS);
424: NF = remove_cont(NF);
425: H = cons(NF,H);
426: LCM = ilcm(LCM,dp_hc(NF[1]));
427: }
428: TNF = time()[0]-T0;
429: if ( dp_gr_print() )
430: print("gennf(TAB="+rtostr(TTAB)+" NF="+rtostr(TNF)+")");
431: return [[map(adj_dn,H,LCM),LCM],PS,GI];
432: }
433:
434: def adj_dn(P,D)
435: {
436: return [(idiv(D,dp_hc(P[1])))*P[0],dp_ht(P[1])];
437: }
438:
439: def hen_ttob(T,NF,LHS,V,MOD)
440: {
441: if ( length(T) == 1 )
442: return car(T);
443: T0 = time()[0]; M = etom(leq_nf(T,NF,LHS,V)); TE = time()[0] - T0;
444: T0 = time()[0]; U = henleq(M,MOD); TH = time()[0] - T0;
445: if ( dp_gr_print() ) {
446: print("(etom="+rtostr(TE)+" hen="+rtostr(TH)+")");
447: }
448: return U ? vtop(T,U,LHS) : 0;
449: }
450:
451: def vtop(S,L,GSL)
452: {
453: U = L[0]; H = L[1];
454: if ( GSL ) {
455: for ( A = 0, I = 0; S != []; S = cdr(S), I++ )
456: A += U[I]*car(S);
457: return [A,H];
458: } else {
459: for ( A = H*car(S), S = cdr(S), I = 0; S != []; S = cdr(S), I++ )
460: A += U[I]*car(S);
461: return ptozp(A);
462: }
463: }
464:
465: def leq_nf(TL,NF,LHS,V)
466: {
467: TLen = length(NF);
468: T = newvect(TLen); M = newvect(TLen);
469: for ( I = 0; I < TLen; I++ ) {
470: T[I] = dp_ht(NF[I][1]);
471: M[I] = dp_hc(NF[I][1]);
472: }
473: Len = length(TL); INDEX = newvect(Len); COEF = newvect(Len);
474: for ( L = TL, J = 0; L != []; L = cdr(L), J++ ) {
475: D = dp_ptod(car(L),V);
476: for ( I = 0; I < TLen; I++ )
477: if ( D == T[I] )
478: break;
479: INDEX[J] = I; COEF[J] = strtov("u"+rtostr(J));
480: }
481: if ( !LHS ) {
482: COEF[0] = 1; NM = 0; DN = 1;
483: } else {
484: NM = LHS[0]; DN = LHS[1];
485: }
486: for ( J = 0, S = -NM; J < Len; J++ ) {
487: DNJ = M[INDEX[J]];
488: GCD = igcd(DN,DNJ); CS = DNJ/GCD; CJ = DN/GCD;
489: S = CS*S + CJ*NF[INDEX[J]][0]*COEF[J];
490: DN *= CS;
491: }
492: for ( D = S, E = []; D; D = dp_rest(D) )
493: E = cons(dp_hc(D),E);
494: BOUND = LHS ? 0 : 1;
495: for ( I = Len - 1, W = []; I >= BOUND; I-- )
496: W = cons(COEF[I],W);
497: return [E,W];
498: }
499:
500: def nf_tab(F,TAB)
501: {
502: for ( NM = 0, DN = 1, I = 0; F; F = dp_rest(F) ) {
503: T = dp_ht(F);
504: for ( ; TAB[I][0] != T; I++);
505: NF = TAB[I][1]; N = NF[0]; D = NF[1];
506: G = igcd(DN,D); DN1 = idiv(DN,G); D1 = idiv(D,G);
507: NM = D1*NM + DN1*dp_hc(F)*N; DN *= D1;
508: }
509: return [NM,DN];
510: }
511:
512: def nf_tab_gsl(A,NF)
513: {
514: DN = NF[1];
515: NF = NF[0];
516: TLen = length(NF);
517: for ( R = 0; A; A = dp_rest(A) ) {
518: HM = dp_hm(A); C = dp_hc(HM); T = dp_ht(HM);
519: for ( I = 0; I < TLen; I++ )
520: if ( NF[I][1] == T )
521: break;
522: R += C*NF[I][0];
523: }
524: return remove_cont([R,DN]);
525: }
526:
527: def redble(D1,D2,N)
528: {
529: for ( I = 0; I < N; I++ )
530: if ( D1[I] > D2[I] )
531: break;
532: return I == N ? 1 : 0;
533: }
534:
535: def tolexm(G,V,O,W,M)
536: {
537: N = length(V); Len = length(G);
538: dp_ord(O); setmod(M); PS = newvect(Len);
539: for ( I = 0, T = G; T != []; T = cdr(T), I++ )
540: PS[I] = dp_mod(dp_ptod(car(T),V),M,[]);
541: for ( I = Len-1, HL = []; I >= 0; I-- )
542: HL = cons(dp_ht(PS[I]),HL);
543: G2 = tolexm_main(PS,HL,V,W,M,ZeroDim);
544: L = map(dp_dtop,G2,V);
545: return L;
546: }
547:
548: def tolexm_main(PS,HL,V,W,M,FLAG)
549: {
550: N = length(W); D = newvect(N); Len = size(PS)[0];
551: for ( I = Len - 1, GI = []; I >= 0; I-- )
552: GI = cons(I,GI);
553: MB = dp_mbase(HL); DIM = length(MB);
554: U = dp_mod(dp_ptod(W[N-1],V),M,[]);
555: UTAB = newvect(DIM);
556: for ( I = 0; I < DIM; I++ ) {
557: if ( dp_gr_print() )
558: print(".",2);
559: UTAB[I] = [MB[I],dp_nf_mod(GI,U*dp_mod(MB[I],M,[]),PS,1,M)];
560: }
1.7 ! noro 561: if ( dp_gr_print() )
! 562: print("");
1.1 noro 563: T = dp_mod(dp_ptod(dp_dtop(dp_vtoe(D),W),V),M,[]);
564: H = G = [[T,T]];
565: DL = []; G2 = [];
566: TNF = 0;
567: while ( 1 ) {
568: if ( dp_gr_print() )
569: print(".",2);
570: S = nextm(D,DL,N);
571: if ( !S )
572: break;
573: T = dp_mod(dp_ptod(dp_dtop(dp_vtoe(D),W),V),M,[]);
574: T0 = time()[0];
575: if ( L = search_redble(T,H) ) {
576: DD = dp_mod(dp_subd(T,L[1]),M,[]);
577: if ( DD == U )
578: NT = dp_nf_tab_mod(L[0],UTAB,M);
579: else
580: NT = dp_nf_mod(GI,L[0]*DD,PS,1,M);
581: } else
582: NT = dp_nf_mod(GI,T,PS,1,M);
583: TNF += time()[0] - T0;
584: H = cons([NT,T],H);
585: T0 = time()[0];
586: L = dp_lnf_mod([NT,T],G,M); N1 = L[0]; N2 = L[1];
587: TLNF += time()[0] - T0;
588: if ( !N1 ) {
589: G2 = cons(N2,G2);
590: if ( FLAG == MiniPoly )
591: break;
592: D1 = newvect(N);
593: for ( I = 0; I < N; I++ )
594: D1[I] = D[I];
595: DL = cons(D1,DL);
596: } else
597: G = insert(G,L);
598: }
599: if ( dp_gr_print() )
600: print("tolexm(nfm="+rtostr(TNF)+" lnfm="+rtostr(TLNF)+")");
601: return G2;
602: }
603:
604: def minipolym(G,V,O,P,V0,M)
605: {
606: N = length(V); Len = length(G);
607: dp_ord(O); setmod(M); PS = newvect(Len);
608: for ( I = 0, T = G; T != []; T = cdr(T), I++ )
609: PS[I] = dp_mod(dp_ptod(car(T),V),M,[]);
610: for ( I = Len-1, HL = []; I >= 0; I-- )
611: HL = cons(dp_ht(PS[I]),HL);
612: for ( I = Len - 1, GI = []; I >= 0; I-- )
613: GI = cons(I,GI);
614: MB = dp_mbase(HL); DIM = length(MB); UT = newvect(DIM);
615: U = dp_mod(dp_ptod(P,V),M,[]);
616: for ( I = 0; I < DIM; I++ )
617: UT[I] = [MB[I],dp_nf_mod(GI,U*dp_mod(MB[I],M,[]),PS,1,M)];
618: T = dp_mod(<<0>>,M,[]); TT = dp_mod(dp_ptod(1,V),M,[]);
619: G = H = [[TT,T]]; TNF = TLNF = 0;
620: for ( I = 1; ; I++ ) {
621: T = dp_mod(<<I>>,M,[]);
622: T0 = time()[0]; NT = dp_nf_tab_mod(H[0][0],UT,M); TNF += time()[0] - T0;
623: H = cons([NT,T],H);
624: T0 = time()[0]; L = dp_lnf_mod([NT,T],G,M); TLNF += time()[0] - T0;
625: if ( !L[0] ) {
626: if ( dp_gr_print() ) print(["nfm",TNF,"lnfm",TLNF]);
627: return dp_dtop(L[1],[V0]);
628: } else
629: G = insert(G,L);
630: }
631: }
632:
633: def nextm(D,DL,N)
634: {
635: for ( I = N-1; I >= 0; ) {
636: D[I]++;
637: for ( T = DL; T != []; T = cdr(T) )
638: if ( car(T) == D )
639: return 1;
640: else if ( redble(car(T),D,N) )
641: break;
642: if ( T != [] ) {
643: for ( J = N-1; J >= I; J-- )
644: D[J] = 0;
645: I--;
646: } else
647: break;
648: }
649: if ( I < 0 )
650: return 0;
651: else
652: return 1;
653: }
654:
655: def search_redble(T,G)
656: {
657: for ( ; G != []; G = cdr(G) )
658: if ( dp_redble(T,car(G)[1]) )
659: return car(G);
660: return 0;
661: }
662:
663: def insert(G,A)
664: {
665: if ( G == [] )
666: return [A];
667: else if ( dp_ht(car(A)) > dp_ht(car(car(G))) )
668: return cons(A,G);
669: else
670: return cons(car(G),insert(cdr(G),A));
671: }
672:
673: #if 0
674: def etom(L) {
675: E = L[0]; W = L[1];
676: LE = length(E); LW = length(W);
677: M = newmat(LE,LW+1);
678: for(J=0;J<LE;J++) {
679: for ( T = E[J]; T && (type(T) == 2); )
680: for ( V = var(T), I = 0; I < LW; I++ )
681: if ( V == W[I] ) {
682: M[J][I] = coef(T,1,V);
683: T = coef(T,0,V);
684: }
685: M[J][LW] = T;
686: }
687: return M;
688: }
689: #endif
690:
691: def etom(L) {
692: E = L[0]; W = L[1];
693: LE = length(E); LW = length(W);
694: M = newmat(LE,LW+1);
695: for(J=0;J<LE;J++) {
696: for ( I = 0, T = E[J]; I < LW; I++ ) {
697: M[J][I] = coef(T,1,W[I]); T = coef(T,0,W[I]);
698: }
699: M[J][LW] = T;
700: }
701: return M;
702: }
703:
704: def calcb_old(M) {
705: N = 2*M;
706: T = gr_sqrt(N);
707: if ( T^2 <= N && N < (T+1)^2 )
708: return idiv(T,2);
709: else
710: error("afo");
711: }
712:
713: def calcb_special(PK,P,K) { /* PK = P^K */
714: N = 2*PK;
715: T = sqrt_special(N,2,P,K);
716: if ( T^2 <= N && N < (T+1)^2 )
717: return idiv(T,2);
718: else
719: error("afo");
720: }
721:
722: def sqrt_special(A,C,M,K) { /* A = C*M^K */
723: L = idiv(K,2); B = M^L;
724: if ( K % 2 )
725: C *= M;
726: D = 2^K; X = idiv((gr_sqrt(C*D^2)+1)*B,D)+1;
727: while ( 1 )
728: if ( (Y = X^2) <= A )
729: return X;
730: else
731: X = idiv(A + Y,2*X);
732: }
733:
734: def gr_sqrt(A) {
735: for ( J = 0, T = A; T >= 2^27; J++ ) {
736: T = idiv(T,2^27)+1;
737: }
738: for ( I = 0; T >= 2; I++ ) {
739: S = idiv(T,2);
740: if ( T = S+S )
741: T = S;
742: else
743: T = S+1;
744: }
745: X = (2^27)^idiv(J,2)*2^idiv(I,2);
746: while ( 1 ) {
747: if ( (Y=X^2) < A )
748: X += X;
749: else if ( Y == A )
750: return X;
751: else
752: break;
753: }
754: while ( 1 )
755: if ( (Y = X^2) <= A )
756: return X;
757: else
758: X = idiv(A + Y,2*X);
759: }
760:
761: #define ABS(a) ((a)>=0?(a):(-a))
762:
763: def inttorat_asir(C,M,B)
764: {
765: if ( M < 0 )
766: M = -M;
767: C %= M;
768: if ( C < 0 )
769: C += M;
770: U1 = 0; U2 = M; V1 = 1; V2 = C;
771: while ( V2 >= B ) {
772: L = iqr(U2,V2); Q = L[0]; R2 = L[1];
773: R1 = U1 - Q*V1;
774: U1 = V1; U2 = V2;
775: V1 = R1; V2 = R2;
776: }
777: if ( ABS(V1) >= B )
778: return 0;
779: else
780: if ( V1 < 0 )
781: return [-V2,-V1];
782: else
783: return [V2,V1];
784: }
785:
786: def intvtoratv(V,M,B) {
787: if ( !B )
788: B = 1;
789: N = size(V)[0];
790: W = newvect(N);
791: if ( ITOR_FAIL >= 0 ) {
792: if ( V[ITOR_FAIL] ) {
793: T = inttorat(V[ITOR_FAIL],M,B);
794: if ( !T ) {
795: if ( dp_gr_print() ) {
796: print("F",2);
797: }
798: return 0;
799: }
800: }
801: }
802: for ( I = 0, DN = 1; I < N; I++ )
803: if ( V[I] ) {
804: T = inttorat((V[I]*DN) % M,M,B);
805: if ( !T ) {
806: ITOR_FAIL = I;
807: if ( dp_gr_print() ) {
808: #if 0
809: print("intvtoratv : failed at I = ",0); print(ITOR_FAIL);
810: #endif
811: print("F",2);
812: }
813: return 0;
814: } else {
815: for( J = 0; J < I; J++ )
816: W[J] *= T[1];
817: W[I] = T[0]; DN *= T[1];
818: }
819: }
820: return [W,DN];
821: }
822:
823: def nf(B,G,M,PS)
824: {
825: for ( D = 0; G; ) {
826: for ( U = 0, L = B; L != []; L = cdr(L) ) {
827: if ( dp_redble(G,R=PS[car(L)]) > 0 ) {
828: GCD = igcd(dp_hc(G),dp_hc(R));
829: CG = idiv(dp_hc(R),GCD); CR = idiv(dp_hc(G),GCD);
830: U = CG*G-dp_subd(G,R)*CR*R;
831: if ( !U )
832: return [D,M];
833: D *= CG; M *= CG;
834: break;
835: }
836: }
837: if ( U )
838: G = U;
839: else {
840: D += dp_hm(G); G = dp_rest(G);
841: }
842: }
843: return [D,M];
844: }
845:
846: def remove_cont(L)
847: {
848: if ( type(L[1]) == 1 ) {
849: T = remove_cont([L[0],L[1]*<<0>>]);
850: return [T[0],dp_hc(T[1])];
851: } else if ( !L[0] )
852: return [0,dp_ptozp(L[1])];
853: else if ( !L[1] )
854: return [dp_ptozp(L[0]),0];
855: else {
856: A0 = dp_ptozp(L[0]); A1 = dp_ptozp(L[1]);
857: C0 = idiv(dp_hc(L[0]),dp_hc(A0)); C1 = idiv(dp_hc(L[1]),dp_hc(A1));
858: GCD = igcd(C0,C1); M0 = idiv(C0,GCD); M1 = idiv(C1,GCD);
859: return [M0*A0,M1*A1];
860: }
861: }
862:
863: def union(A,B)
864: {
865: for ( T = B; T != []; T = cdr(T) )
866: A = union1(A,car(T));
867: return A;
868: }
869:
870: def union1(A,E)
871: {
872: if ( A == [] )
873: return [E];
874: else if ( car(A) == E )
875: return A;
876: else
877: return cons(car(A),union1(cdr(A),E));
878: }
879:
880: def setminus(A,B) {
881: for ( T = reverse(A), R = []; T != []; T = cdr(T) ) {
882: for ( S = B, M = car(T); S != []; S = cdr(S) )
883: if ( car(S) == M )
884: break;
885: if ( S == [] )
886: R = cons(M,R);
887: }
888: return R;
889: }
890:
891: def member(A,L) {
892: for ( ; L != []; L = cdr(L) )
893: if ( A == car(L) )
894: return 1;
895: return 0;
896: }
897:
898: /* several functions for computation of normal forms etc. */
899:
900: def p_nf(P,B,V,O) {
901: dp_ord(O); DP = dp_ptod(P,V);
902: N = length(B); DB = newvect(N);
903: for ( I = N-1, IL = []; I >= 0; I-- ) {
904: DB[I] = dp_ptod(B[I],V);
905: IL = cons(I,IL);
906: }
907: return dp_dtop(dp_nf(IL,DP,DB,1),V);
908: }
909:
910: def p_true_nf(P,B,V,O) {
911: dp_ord(O); DP = dp_ptod(P,V);
912: N = length(B); DB = newvect(N);
913: for ( I = N-1, IL = []; I >= 0; I-- ) {
914: DB[I] = dp_ptod(B[I],V);
915: IL = cons(I,IL);
916: }
917: L = dp_true_nf(IL,DP,DB,1);
918: return [dp_dtop(L[0],V),L[1]];
919: }
920:
921: def p_terms(D,V,O)
922: {
923: dp_ord(O);
924: for ( L = [], T = dp_ptod(D,V); T; T = dp_rest(T) )
925: L = cons(dp_dtop(dp_ht(T),V),L);
926: return reverse(L);
927: }
928:
929: def dp_terms(D,V)
930: {
931: for ( L = [], T = D; T; T = dp_rest(T) )
932: L = cons(dp_dtop(dp_ht(T),V),L);
933: return reverse(L);
934: }
935:
936: def gb_comp(A,B)
937: {
938: for ( T = A; T != []; T = cdr(T) ) {
939: for ( S = B, M = car(T), N = -M; S != []; S = cdr(S) )
940: if ( car(S) == M || car(S) == N )
941: break;
942: if ( S == [] )
943: break;
944: }
945: return T == [] ? 1 : 0;
946: }
947:
948: def zero_dim(G,V,O) {
949: dp_ord(O);
950: HL = map(dp_dtop,map(dp_ht,map(dp_ptod,G,V)),V);
951: for ( L = []; HL != []; HL = cdr(HL) )
952: if ( length(vars(car(HL))) == 1 )
953: L = cons(car(HL),L);
954: return length(vars(L)) == length(V) ? 1 : 0;
955: }
956:
957: def hmlist(G,V,O) {
958: dp_ord(O);
959: return map(dp_dtop,map(dp_hm,map(dp_ptod,G,V)),V);
960: }
961:
962: def valid_modulus(HL,M) {
963: V = vars(HL);
964: for ( T = HL; T != []; T = cdr(T) )
965: if ( !dp_mod(dp_ptod(car(T),V),M,[]) )
966: break;
967: return T == [] ? 1 : 0;
968: }
969:
970: def npos_check(DL) {
971: N = size(car(DL))[0];
972: if ( length(DL) != N )
973: return [-1,0];
974: D = newvect(N);
975: for ( I = 0; I < N; I++ ) {
976: for ( J = 0; J < N; J++ )
977: D[J] = 0;
978: D[I] = 1;
979: for ( T = DL; T != []; T = cdr(T) )
980: if ( D == car(T) )
981: break;
982: if ( T != [] )
983: DL = setminus(DL,[car(T)]);
984: }
985: if ( length(DL) != 1 )
986: return [-1,0];
987: U = car(DL);
988: for ( I = 0, J = 0, I0 = -1; I < N; I++ )
989: if ( U[I] ) {
990: I0 = I; J++;
991: }
992: if ( J != 1 )
993: return [-1,0];
994: else
995: return [I0,U[I0]];
996: }
997:
998: def mult_mat(L,TAB,MB)
999: {
1000: A = L[0]; DN0 = L[1];
1001: for ( NM = 0, DN = 1, I = 0; A; A = dp_rest(A) ) {
1002: H = dp_ht(A);
1003: for ( ; MB[I] != H; I++ );
1004: NM1 = TAB[I][0]; DN1 = TAB[I][1]; I++;
1005: GCD = igcd(DN,DN1); C = DN1/GCD; C1 = DN/GCD;
1006: NM = C*NM + C1*dp_hc(A)*NM1;
1007: DN *= C;
1008: }
1009: Z=remove_cont([NM,DN*DN0]);
1010: return Z;
1011: }
1012:
1013: def sepm(MAT)
1014: {
1015: S = size(MAT); N = S[0]; M = S[1]-1;
1016: A = newmat(N,M); B = newvect(N);
1017: for ( I = 0; I < N; I++ )
1018: for ( J = 0, T1 = MAT[I], T2 = A[I]; J < M; J++ )
1019: T2[J] = T1[J];
1020: for ( I = 0; I < N; I++ )
1021: B[I] = MAT[I][M];
1022: return [A,B];
1023: }
1024:
1025: def henleq(M,MOD)
1026: {
1027: SIZE = size(M); ROW = SIZE[0]; COL = SIZE[1];
1028: W = newvect(COL);
1029: L = sepm(M); A = L[0]; B = L[1];
1030: COUNT = INIT_COUNT?INIT_COUNT:idiv(max_mag(M),54);
1031: if ( !COUNT )
1032: COUNT = 1;
1033:
1034: TINV = TC = TR = TS = TM = TDIV = 0;
1035:
1036: T0 = time()[0];
1037: L = geninvm_swap(A,MOD); INV = L[0]; INDEX = L[1];
1038: TS += time()[0] - T0;
1039:
1040: COL1 = COL - 1;
1041: AA = newmat(COL1,COL1); BB = newvect(COL1);
1042: for ( I = 0; I < COL1; I++ ) {
1043: for ( J = 0, T = AA[I], S = A[INDEX[I]]; J < COL1; J++ )
1044: T[J] = S[J];
1045: BB[I] = B[INDEX[I]];
1046: }
1047: if ( COL1 != ROW ) {
1048: RESTA = newmat(ROW-COL1,COL1); RESTB = newvect(ROW-COL1);
1049: for ( ; I < ROW; I++ ) {
1050: for ( J = 0, T = RESTA[I-COL1], S = A[INDEX[I]]; J < COL1; J++ )
1051: T[J] = S[J];
1052: RESTB[I-COL1] = B[INDEX[I]];
1053: }
1054: } else
1055: RESTA = RESTB = 0;
1056:
1057: MOD2 = idiv(MOD,2);
1058: for ( I = 0, C = BB, X = 0, PK = 1, CCC = 0, ITOR_FAIL = -1; ;
1059: I++, PK *= MOD ) {
1060: if ( COUNT == CCC ) {
1061: CCC = 0;
1062: T0 = time()[0];
1063: ND = intvtoratv(X,PK,ishift(calcb_special(PK,MOD,I),32));
1064: TR += time()[0]-T0;
1065: if ( ND ) {
1066: T0 = time()[0];
1067: F = ND[0]; LCM = ND[1]; T = AA*F+LCM*BB;
1068: TM += time()[0]-T0;
1069: if ( zerovector(T) ) {
1070: T0 = time()[0]; T = RESTA*F+LCM*RESTB; TM += time()[0]-T0;
1071: if ( zerovector(T) ) {
1072: #if 0
1073: if ( dp_gr_print() ) print(["init",TS,"pinv",TINV,"c",TC,"div",TDIV,"rat",TR,"mul",TM]);
1074: #endif
1075: if ( dp_gr_print() ) print("end",2);
1076: return [F,LCM];
1077: } else
1078: return 0;
1079: }
1080: } else {
1081: #if 0
1082: if ( dp_gr_print() ) print(I);
1083: #endif
1084: }
1085: } else {
1086: #if 0
1087: if ( dp_gr_print() ) print([I,TINV,TC,TDIV]);
1088: #endif
1089: if ( dp_gr_print() ) print(".",2);
1090: CCC++;
1091: }
1092: T0 = time()[0];
1093: XT = sremainder(INV*sremainder(-C,MOD),MOD);
1094: XT = map(adj_sgn,XT,MOD,MOD2);
1095: TINV += time()[0] - T0;
1096: X += XT*PK;
1097: T0 = time()[0];
1098: C += mul_mat_vect_int(AA,XT);
1099: TC += time()[0] - T0;
1100: T0 = time()[0]; C = map(idiv,C,MOD); TDIV += time()[0] - T0;
1101: }
1102: }
1103:
1104: def henleq_prep(A,MOD)
1105: {
1106: SIZE = size(A); ROW = SIZE[0]; COL = SIZE[1];
1107: L = geninvm_swap(A,MOD); INV = L[0]; INDEX = L[1];
1108: AA = newmat(COL,COL);
1109: for ( I = 0; I < COL; I++ )
1110: for ( J = 0, T = AA[I], S = A[INDEX[I]]; J < COL; J++ )
1111: T[J] = S[J];
1112: if ( COL != ROW ) {
1113: RESTA = newmat(ROW-COL,COL);
1114: for ( ; I < ROW; I++ )
1115: for ( J = 0, T = RESTA[I-COL], S = A[INDEX[I]]; J < COL; J++ )
1116: T[J] = S[J];
1117: } else
1118: RESTA = 0;
1119: return [[A,AA,RESTA],L];
1120: }
1121:
1122: def henleq_gsl(L,B,MOD)
1123: {
1124: AL = L[0]; INVL = L[1];
1125: A = AL[0]; AA = AL[1]; RESTA = AL[2];
1126: INV = INVL[0]; INDEX = INVL[1];
1127: SIZE = size(A); ROW = SIZE[0]; COL = SIZE[1];
1128: BB = newvect(COL);
1129: for ( I = 0; I < COL; I++ )
1130: BB[I] = B[INDEX[I]];
1131: if ( COL != ROW ) {
1132: RESTB = newvect(ROW-COL);
1133: for ( ; I < ROW; I++ )
1134: RESTB[I-COL] = B[INDEX[I]];
1135: } else
1136: RESTB = 0;
1137:
1138: COUNT = INIT_COUNT?INIT_COUNT:idiv(MAX(max_mag(A),max_mag_vect(B)),54);
1139: if ( !COUNT )
1140: COUNT = 1;
1141: MOD2 = idiv(MOD,2);
1.3 noro 1142: X = newvect(size(AA)[0]);
1143: for ( I = 0, C = BB, PK = 1, CCC = 0, ITOR_FAIL = -1; ;
1.1 noro 1144: I++, PK *= MOD ) {
1145: if ( zerovector(C) )
1146: if ( zerovector(RESTA*X+RESTB) ) {
1147: if ( dp_gr_print() ) print("end",0);
1148: return [X,1];
1149: } else
1150: return 0;
1151: else if ( COUNT == CCC ) {
1152: CCC = 0;
1153: ND = intvtoratv(X,PK,ishift(calcb_special(PK,MOD,I),32));
1154: if ( ND ) {
1155: F = ND[0]; LCM = ND[1]; T = AA*F+LCM*BB;
1156: if ( zerovector(T) ) {
1157: T = RESTA*F+LCM*RESTB;
1158: if ( zerovector(T) ) {
1159: if ( dp_gr_print() ) print("end",0);
1160: return [F,LCM];
1161: } else
1162: return 0;
1163: }
1164: } else {
1165: }
1166: } else {
1167: if ( dp_gr_print() ) print(".",2);
1168: CCC++;
1169: }
1170: XT = sremainder(INV*sremainder(-C,MOD),MOD);
1171: XT = map(adj_sgn,XT,MOD,MOD2);
1172: X += XT*PK;
1173: C += mul_mat_vect_int(AA,XT);
1174: C = map(idiv,C,MOD);
1175: }
1176: }
1177:
1178: def adj_sgn(A,M,M2)
1179: {
1180: return A > M2 ? A-M : A;
1181: }
1182:
1183: def zerovector(C)
1184: {
1185: if ( !C )
1186: return 1;
1187: for ( I = size(C)[0]-1; I >= 0 && !C[I]; I-- );
1188: if ( I < 0 )
1189: return 1;
1190: else
1191: return 0;
1192: }
1193:
1194: def solvem(INV,COMP,V,MOD)
1195: {
1196: T = COMP*V;
1197: N = size(T)[0];
1198: for ( I = 0; I < N; I++ )
1199: if ( T[I] % MOD )
1200: return 0;
1201: return modvect(INV*V,MOD);
1202: }
1203:
1204: def modmat(A,MOD)
1205: {
1206: if ( !A )
1207: return 0;
1208: S = size(A); N = S[0]; M = S[1];
1209: MAT = newmat(N,M);
1210: for ( I = 0, NZ = 0; I < N; I++ )
1211: for ( J = 0, T1 = A[I], T2 = MAT[I]; J < M; J++ ) {
1212: T2[J] = T1[J] % MOD;
1213: NZ = NZ || T2[J];
1214: }
1215: return NZ?MAT:0;
1216: }
1217:
1218: def modvect(A,MOD)
1219: {
1220: if ( !A )
1221: return 0;
1222: N = size(A)[0];
1223: VECT = newvect(N);
1224: for ( I = 0, NZ = 0; I < N; I++ ) {
1225: VECT[I] = A[I] % MOD;
1226: NZ = NZ || VECT[I];
1227: }
1228: return NZ?VECT:0;
1229: }
1230:
1231: def qrmat(A,MOD)
1232: {
1233: if ( !A )
1234: return [0,0];
1235: S = size(A); N = S[0]; M = S[1];
1236: Q = newmat(N,M); R = newmat(N,M);
1237: for ( I = 0, NZQ = 0, NZR = 0; I < N; I++ )
1238: for ( J = 0, TA = A[I], TQ = Q[I], TR = R[I]; J < M; J++ ) {
1239: L = iqr(TA[J],MOD); TQ[J] = L[0]; TR[J] = L[1];
1240: NZQ = NZQ || TQ[J]; NZR = NZR || TR[J];
1241: }
1242: return [NZQ?Q:0,NZR?R:0];
1243: }
1244:
1245: def qrvect(A,MOD)
1246: {
1247: if ( !A )
1248: return [0,0];
1249: N = size(A)[0];
1250: Q = newvect(N); R = newvect(N);
1251: for ( I = 0, NZQ = 0, NZR = 0; I < N; I++ ) {
1252: L = iqr(A[I],MOD); Q[I] = L[0]; R[I] = L[1];
1253: NZQ = NZQ || Q[I]; NZR = NZR || R[I];
1254: }
1255: return [NZQ?Q:0,NZR?R:0];
1256: }
1257:
1258: def max_mag(M)
1259: {
1260: R = size(M)[0];
1261: U = 1;
1262: for ( I = 0; I < R; I++ ) {
1263: A = max_mag_vect(M[I]);
1264: U = MAX(A,U);
1265: }
1266: return U;
1267: }
1268:
1269: def max_mag_vect(V)
1270: {
1271: R = size(V)[0];
1272: U = 1;
1273: for ( I = 0; I < R; I++ ) {
1274: A = dp_mag(V[I]*<<0>>);
1275: U = MAX(A,U);
1276: }
1277: return U;
1278: }
1279:
1280: def gsl_check(B,V,S)
1281: {
1282: N = length(V);
1283: U = S[N-1]; M = U[1]; D = U[2];
1284: W = setminus(V,[var(M)]);
1285: H = uc(); VH = append(W,[H]);
1286: for ( T = B; T != []; T = cdr(T) ) {
1287: A = car(T);
1288: AH = dp_dtop(dp_homo(dp_ptod(A,W)),VH);
1289: for ( I = 0, Z = S; I < N-1; I++, Z = cdr(Z) ) {
1290: L = car(Z); AH = ptozp(subst(AH,L[0],L[1]/L[2]));
1291: }
1292: AH = ptozp(subst(AH,H,D));
1293: R = srem(AH,M);
1294: if ( dp_gr_print() )
1295: if ( !R )
1296: print([A,"ok"]);
1297: else
1298: print([A,"bad"]);
1299: if ( R )
1300: break;
1301: }
1302: return T == [] ? 1 : 0;
1303: }
1304:
1305: def vs_dim(G,V,O)
1306: {
1307: HM = hmlist(G,V,O); ZD = zero_dim(HM,V,O);
1308: if ( ZD ) {
1309: MB = dp_mbase(map(dp_ptod,HM,V));
1310: return length(MB);
1311: } else
1312: error("vs_dim : ideal is not zero-dimensional!");
1313: }
1314:
1.2 noro 1315: def dgr(G,V,O)
1.1 noro 1316: {
1.2 noro 1317: P = getopt(proc);
1318: if ( type(P) == -1 )
1319: return gr(G,V,O);
1.1 noro 1320: P0 = P[0]; P1 = P[1]; P = [P0,P1];
1.2 noro 1321: map(ox_reset,P);
1322: ox_cmo_rpc(P0,"dp_gr_main",G,V,0,1,O);
1323: ox_cmo_rpc(P1,"dp_gr_main",G,V,1,1,O);
1324: map(ox_push_cmd,P,262); /* 262 = OX_popCMO */
1325: F = ox_select(P);
1326: R = ox_get(F[0]);
1327: if ( F[0] == P0 ) {
1328: Win = "nonhomo";
1329: Lose = P1;
1330: } else {
1331: Win = "nhomo";
1332: Lose = P0;
1333: }
1334: ox_reset(Lose);
1335: return [Win,R];
1.1 noro 1336: }
1337:
1338: /* functions for rpc */
1339:
1340: def register_matrix(M)
1341: {
1342: REMOTE_MATRIX = M; return 0;
1343: }
1344:
1345: def register_nfv(L)
1346: {
1347: REMOTE_NF = L[0]; REMOTE_VARS = L[1]; return 0;
1348: }
1349:
1350: def r_ttob(T,M)
1351: {
1352: return hen_ttob(T,REMOTE_NF,0,REMOTE_VARS,M);
1353: }
1354:
1355: def r_ttob_gsl(L,M)
1356: {
1357: return cons(L[2],hen_ttob(L[0],REMOTE_NF,L[1],REMOTE_VARS,M));
1358: }
1359:
1360: def get_matrix()
1361: {
1362: REMOTE_MATRIX;
1.4 noro 1363: }
1364:
1365: extern NFArray$
1366:
1367: /*
1368: * HL = [[c,i,m,d],...]
1369: * if c != 0
1370: * g = 0
1371: * g = (c*g + m*gi)/d
1372: * ...
1373: * finally compare g with NF
1374: * if g == NF then NFArray[NFIndex] = g
1375: *
1376: * if c = 0 then HL consists of single history [0,i,0,0],
1377: * which means that dehomogenization of NFArray[i] should be
1378: * eqall to NF.
1379: */
1380:
1381: def check_trace(NF,NFIndex,HL)
1382: {
1383: if ( !car(HL)[0] ) {
1384: /* dehomogenization */
1385: DH = dp_dehomo(NFArray[car(HL)[1]]);
1386: if ( NF == DH ) {
1387: realloc_NFArray(NFIndex);
1388: NFArray[NFIndex] = NF;
1389: return 0;
1390: } else
1391: error("check_trace(dehomo)");
1392: }
1393:
1394: for ( G = 0, T = HL; T != []; T = cdr(T) ) {
1395: H = car(T);
1396:
1397: Coeff = H[0];
1398: Index = H[1];
1399: Monomial = H[2];
1400: Denominator = H[3];
1401:
1402: Reducer = NFArray[Index];
1403: G = (Coeff*G+Monomial*Reducer)/Denominator;
1404: }
1405: if ( NF == G ) {
1406: realloc_NFArray(NFIndex);
1407: NFArray[NFIndex] = NF;
1408: return 0;
1409: } else
1410: error("check_trace");
1411: }
1412:
1413: /*
1414: * realloc NFArray so that it can hold * an element as NFArray[Ind].
1415: */
1416:
1417: def realloc_NFArray(Ind)
1418: {
1419: if ( Ind == size(NFArray)[0] ) {
1420: New = newvect(Ind + 100);
1421: for ( I = 0; I < Ind; I++ )
1422: New[I] = NFArray[I];
1423: NFArray = New;
1424: }
1425: }
1426:
1427: /*
1428: * create NFArray and initialize it by List.
1429: */
1430:
1431: def register_input(List)
1432: {
1433: Len = length(List);
1434: NFArray = newvect(Len+100,List);
1.1 noro 1435: }
1436: end$
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>