Annotation of OpenXM_contrib2/asir2000/lib/sp, Revision 1.12
1.7 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.8 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.7 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.12 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/lib/sp,v 1.11 2003/02/07 09:25:58 noro Exp $
1.7 noro 49: */
1.1 noro 50: /*
51: sp : functions related to algebraic number fields
52:
53: Revision History:
54:
1.9 noro 55: 2001/10/12 noro if USE_PARI_FACTOR is nonzero, pari factor is called
1.6 noro 56: 2000/03/10 noro fixed several bugs around gathering algebraic numbers
1.3 noro 57: 1999/08/24 noro modified for 1999 release version
1.1 noro 58: */
59:
60: #include "defs.h"
61:
62: extern ASCENT,GCDTIME,UFTIME,RESTIME,SQTIME,PRINT$
63: extern Ord$
1.9 noro 64: extern USE_PARI_FACTOR$
1.11 noro 65:
66: /* gen_sp can handle non-monic poly */
67:
68: def gen_sp(P)
69: {
70: P = ptozp(P);
71: V = var(P);
72: D = deg(P,V);
73: LC = coef(P,D,V);
74: F = LC^(D-1)*subst(P,V,V/LC);
75: /* F must be monic */
76: L = sp(F);
77: return cons(map(subst,car(L),V,LC*V),cdr(L));
78: }
1.1 noro 79:
80: def sp(P)
81: {
82: RESTIME=UFTIME=GCDTIME=SQTIME=0;
83: L = flatmf(fctr(P)); X = var(P);
84: AL = []; ADL = [];
85: while ( 1 ) {
86: L = sort_by_deg(L);
87: for ( T = L, H = []; T != []; H = cons(car(T),H), T = cdr(T) )
88: if ( deg(car(T),X) > 1 )
89: break;
90: if ( T == [] ) {
91: if ( dp_gr_print() ) {
92: print(["GCDTIME = ",GCDTIME]);
93: print(["UFTIME = ",UFTIME]);
94: print(["RESTIME = ",RESTIME]);
95: }
96: return [L,ADL];
97: } else {
98: A = newalg(car(T));
99: R = pdiva(car(T),X-A);
100: AL = cons(A,AL);
101: ADL = cons([A,defpoly(A)],ADL);
102: L = aflist(append(H,append([X-A,R],cdr(T))),AL);
103: }
104: }
1.6 noro 105: }
106:
107: /*
108: Input:
109: F=F(x,a1,...,an)
110: DL = [[an,dn(an,...,a1)],...,[a2,d2(a2,a1)],[a1,d1(a1)]]
111: 'ai' denotes a root of di(t).
112: Output:
113: irreducible factorization of F over Q(a1,...,an)
114: [[F1(x,a1,...,an),e1],...,[Fk(x,a1,...,an),ek]]
115: 'ej' denotes the multiplicity of Fj.
116: */
117:
118: def af_noalg(F,DL)
119: {
120: DL = reverse(DL);
121: N = length(DL);
122: Tab = newvect(N);
123: /* Tab = [[a1,r1],...]; ri is a root of di(t,r(i-1),...,r1). */
124: AL = [];
125: for ( I = 0; I < N; I++ ) {
126: T = DL[I];
127: for ( J = 0, DP = T[1]; J < I; J++ )
128: DP = subst(DP,Tab[J][0],Tab[J][1]);
129: B = newalg(DP);
130: Tab[I] = [T[0],B];
131: F = subst(F,T[0],B);
132: AL = cons(B,AL);
133: }
134: FL = af(F,AL);
135: for ( T = FL, R = []; T != []; T = cdr(T) )
136: R = cons([conv_noalg(T[0][0],Tab),T[0][1]],R);
137: return reverse(R);
138: }
139:
140: /*
141: Input:
142: F=F(x) univariate polynomial over the rationals
143: Output:
144: [FL,DL]
145: DL = [[an,dn(an,...,a1)],...,[a2,d2(a2,a1)],[a1,d1(a1)]]
146: 'ai' denotes a root of di(t).
147: FL = [F1,F2,...]
148: irreducible factors of F over Q(a1,...,an)
149: */
150:
151: def sp_noalg(F)
152: {
153: L = sp(F);
154: FL = map(algptorat,L[0]);
155: for ( T = L[1], DL = []; T != []; T = cdr(T) )
156: DL = cons([algtorat(T[0][0]),T[0][1]],DL);
157: return [FL,reverse(DL)];
158: }
159:
160: def conv_noalg(F,Tab)
161: {
162: N = size(Tab)[0];
163: F = algptorat(F);
164: for ( I = N-1; I >= 0; I-- )
165: F = subst(F,algtorat(Tab[I][1]),Tab[I][0]);
166: return F;
1.1 noro 167: }
168:
169: def aflist(L,AL)
170: {
171: for ( DC = []; L != []; L = cdr(L) ) {
172: T = af_sp(car(L),AL,1);
173: DC = append(DC,T);
174: }
175: return DC;
176: }
177:
178: def sort_by_deg(F)
179: {
180: for ( T = F, S = []; T != []; T = cdr(T) )
181: if ( type(car(T)) != NUM )
182: S = cons(car(T),S);
183: N = length(S); W = newvect(N);
184: for ( I = 0; I < N; I++ )
185: W[I] = S[I];
186: V = var(W[0]);
187: for ( I = 0; I < N; I++ ) {
188: for ( J = I + 1, J0 = I; J < N; J++ )
189: if ( deg(W[J0],V) > deg(W[J],V) )
190: J0 = J;
191: if ( J0 != I ) {
192: T = W[I]; W[I] = W[J0]; W[J0] = T;
193: }
194: }
195: if ( ASCENT )
196: for ( I = N-1, S = []; I >= 0; I-- )
197: S = cons(W[I],S);
198: else
199: for ( I = 0, S = []; I < N; I++ )
200: S = cons(W[I],S);
201: return S;
202: }
203:
204: def flatmf(L) {
205: for ( S = []; L != []; L = cdr(L) )
206: if ( type(F=car(car(L))) != NUM )
207: S = append(S,[F]);
208: return S;
209: }
210:
211: def af(P,AL)
212: {
213: RESTIME=UFTIME=GCDTIME=SQTIME=0;
1.2 noro 214: S = reverse(asq(P));
1.1 noro 215: for ( L = []; S != []; S = cdr(S) ) {
216: FM = car(S); F = FM[0]; M = FM[1];
217: G = af_sp(F,AL,1);
218: for ( ; G != []; G = cdr(G) )
219: L = cons([car(G),M],L);
220: }
221: if ( dp_gr_print() )
222: print(["GCDTIME = ",GCDTIME,"UFTIME = ",UFTIME,"RESTIME = ",RESTIME,"SQTIME=",SQTIME]);
223: return L;
224: }
225:
226: def af_sp(P,AL,HINT)
227: {
228: if ( !P || type(P) == NUM )
229: return [P];
230: P1 = simpcoef(simpalg(P));
231: return af_spmain(P1,AL,1,HINT,P1,[]);
232: }
233:
234: def af_spmain(P,AL,INIT,HINT,PP,SHIFT)
235: {
236: if ( !P || type(P) == NUM )
237: return [P];
238: P = simpcoef(simpalg(P));
239: if ( DEG(P) == 1 )
240: return [simpalg(P)];
241: if ( AL == [] ) {
242: TTT = time()[0];
243: F = flatmf(ufctrhint_heuristic(P,HINT,PP,SHIFT));
244: UFTIME+=time()[0]-TTT;
245: return F;
246: }
247: A0 = car(AL); P0 = defpoly(A0);
248: V = var(P); V0 = var(P0);
249: P = simpcoef(P);
250: TTT = time()[0];
251: N = simpcoef(sp_norm(A0,V,subst(P,V,V-INIT*A0),AL));
252: RESTIME+=time()[0]-TTT;
253: TTT = time()[0];
1.2 noro 254: DCSQ = sortfs(asq(N));
1.1 noro 255: SQTIME+=time()[0]-TTT;
256: for ( G = P, A = V+INIT*A0, DCR = []; DCSQ != []; DCSQ = cdr(DCSQ) ) {
257: C = TT(DCSQ); D = TS(DCSQ);
258: if ( !var(C) )
259: continue;
260: if ( D == 1 )
261: DCT = af_spmain(C,cdr(AL),1,HINT*deg(P0,V0),PP,cons([A0,INIT],SHIFT));
262: else
263: DCT = af_spmain(C,cdr(AL),1,1,C,[]);
264: for ( ; DCT != []; DCT = cdr(DCT) ) {
265: if ( !var(car(DCT)) )
266: continue;
267: if ( length(DCSQ) == 1 && length(DCT) == 1 )
268: U = simpcoef(G);
269: else {
270: S = subst(car(DCT),V,A);
271: if ( pra(G,S,AL) )
1.2 noro 272: U = cr_gcda(S,G);
1.1 noro 273: else
274: U = S;
275: }
276: if ( var(U) == V ) {
277: G = pdiva(G,U);
278: if ( D == 1 )
279: DCR = cons(simpcoef(U),DCR);
280: else {
281: T = af_spmain(U,AL,sp_next(INIT),HINT,PP,SHIFT);
282: DCR = append(DCR,T);
283: }
284: }
285: }
286: }
287: return DCR;
288: }
289:
290: def sp_next(I)
291: {
292: if ( I > 0 )
293: return -I;
294: else
295: return -I+1;
296: }
297:
298: extern USE_RES;
299:
300: def sp_norm(A,V,P,AL)
301: {
302: P = simpcoef(simpalg(P));
303: if (USE_RES)
304: return sp_norm_res(A,V,P,AL);
305: else
306: return sp_norm_ch(A,V,P,AL);
307: }
308:
309: def sp_norm_ch(A,V,P,AL)
310: {
311: Len = length(AL);
312: P0 = defpoly(A); V0 = var(P0);
313: PR = algptorat(P);
314: if ( nmono(P0) == 2 )
315: R = res(V0,PR,P0);
316: else if ( Len == 1 || Len == 3 )
317: R = res_ch1(V0,V,PR,P0);
318: else if ( Len == 2 ) {
319: P1 = defpoly(AL[1]);
320: R = norm_ch1(V0,V,PR,P0,P1);
321: } else
322: R = res(V0,PR,P0);
323: return rattoalgp(R,cdr(AL));
324: }
325:
326: def sp_norm_res(A,V,P,AL)
327: {
328: Len = length(AL);
329: P0 = defpoly(A); V0 = var(P0);
330: PR = algptorat(P);
331: R = res(V0,PR,P0);
332: return rattoalgp(R,cdr(AL));
333: }
334:
335: def simpalg(P) {
336: if ( !P )
337: return 0;
338: else if ( type(P) == NUM )
339: return ntype(P) <= 1 ? P : simpalgn(P);
340: else if ( type(P) == POLY )
341: return simpalgp(P);
342: else if ( type(P) == RAT )
343: return simpalg(nm(P))/simpalg(dn(P));
344: }
345:
346: def simpalgp(P) {
347: for ( V = var(P), I = deg(P,V), T = 0; I >= 0; I-- )
348: if ( C = coef(P,I) )
349: T += simpalg(C)*V^I;
350: return T;
351: }
352:
353: def simpalgn(A) {
354: if ( ntype(A) <= 1 )
355: return A;
356: else if ( type(R=algtorat(A)) == POLY )
357: return simpalgb(A);
358: else
359: return simpalgb(
360: invalgp(simpalgb(rattoalg(dn(R))))
361: *simpalgb(rattoalg(nm(R)))
362: );
363: }
364:
365: def simpalgb(P) {
366: if ( ntype(P) <= 1 )
367: return P;
368: else {
369: A0 = getalg(P);
370: Used = [];
371: while ( A0 != [] ) {
372: S = algtorat(P);
373: for ( A = A0; A != []; A = cdr(A) )
374: S = srem(S,defpoly(car(A)));
375: P = rattoalg(S);
376: Used = append(Used,[car(A0)]);
377: A0 = setminus(getalg(P),Used);
378: }
379: return P;
380: }
381: }
382:
383: def setminus(A,B) {
384: for ( T = reverse(A), R = []; T != []; T = cdr(T) ) {
385: for ( S = B, M = car(T); S != []; S = cdr(S) )
386: if ( car(S) == M )
387: break;
388: if ( S == [] )
389: R = cons(M,R);
390: }
391: return R;
392: }
393:
394: def getalgp(P) {
395: if ( type(P) <= 1 )
396: return getalg(P);
397: else {
398: for ( V = var(P), I = deg(P,V), T = []; I >= 0; I-- )
399: if ( C = coef(P,I) )
1.2 noro 400: T = union_sort(T,getalgp(C));
1.1 noro 401: return T;
402: }
403: }
404:
1.2 noro 405: def getalgtreep(P) {
406: if ( type(P) <= 1 )
407: return getalgtree(P);
408: else {
409: for ( V = var(P), I = deg(P,V), T = []; I >= 0; I-- )
410: if ( C = coef(P,I) )
411: T = union_sort(T,getalgtreep(C));
412: return T;
413: }
1.1 noro 414: }
415:
1.2 noro 416: /* C = union of A and B; A and B is sorted. C should also be sorted. */
417:
418: def union_sort(A,B)
1.1 noro 419: {
420: if ( A == [] )
1.2 noro 421: return B;
422: else if ( B == [] )
1.1 noro 423: return A;
1.2 noro 424: else {
425: A0 = car(A);
426: B0 = car(B);
427: if ( A0 == B0 )
428: return cons(A0,union_sort(cdr(A),cdr(B)));
429: else if ( A0 > B0 )
430: return cons(A0,union_sort(cdr(A),B));
431: else
432: return cons(B0,union_sort(A,cdr(B)));
433: }
1.1 noro 434: }
435:
436: def invalgp(A)
437: {
438: if ( ntype(A) <= 1 )
439: return 1/A;
440: P0 = defpoly(mainalg(A)); P = algtorat(A);
441: V = var(P0); G1 = P0;
442: G2 = DEG(P)>=DEG(P0)?srem(P,P0):P;
443: for ( H = 1, X = 1, U1 = 0, U2 = 1; deg(G2,V); ) {
444: D = DEG(G1)-DEG(G2); T = LCOEF(G2)^(D+1);
445: L = sqr(G1*T,G2); Q = car(L); R = car(cdr(L));
446: S = U1*T-U2*Q;
447: M = H^D; M1 = M*X;
448: G1 = G2; G2 = sdiv(R,M1);
449: U1 = U2; U2 = sdiv(S,M1);
450: X = LCOEF(G1); H = sdiv(X^D*H,M);
451: }
452: C = invalgp(rattoalg(srem(P*U2,P0)));
453: return C*rattoalg(U2);
454: }
455:
456: def algptorat(P) {
457: if ( type(P) <= 1 )
458: return algtorat(P);
459: else {
460: for ( V = var(P), I = deg(P,V), T = 0; I >= 0; I-- )
461: if ( C = coef(P,I) )
462: T += algptorat(C)*V^I;
463: return T;
464: }
465: }
466:
467: def rattoalgp(P,M) {
468: for ( T = M, S = P; T != []; T = cdr(T) )
469: S = subst(S,algtorat(FIRST(T)),FIRST(T));
470: return S;
471: }
472: def sortfs(L)
473: {
474: #define Factor(a) car(a)
475: #define Mult(a) car(cdr(a))
476: if ( type(TT(L)) == NUM )
477: L = cdr(L);
478: for ( N = 0, T = L; T != []; T = cdr(T), N++ );
479: P = newvect(N); P1 = newvect(N);
480: for ( I = 0, T = L, R = []; T != []; T = cdr(T) )
481: if ( Mult(car(T)) == 1 ) {
482: R = cons(car(T),R); N--;
483: } else {
484: P[I] = car(T); I++;
485: }
486: for ( J = 0, V = var(Factor(P[0])); J < N; J++ ) {
487: for ( K0 = K = J, D = deg(Factor(P[J]),V); K < N; K++ )
488: if ( deg(Factor(P[K]),V) < D ) {
489: K0 = K;
490: D = deg(Factor(P[K]),V);
491: }
492: P1[J] = P[K0];
493: if ( J != K0 )
494: P[K0] = P[J];
495: }
496: for ( I = N - 1; I >= 0; I-- )
497: R = cons(P1[I],R);
498: return R;
499: }
500:
501: def pdiva(P1,P2)
502: {
1.2 noro 503: A = union_sort(getalgp(P1),getalgp(P2));
1.1 noro 504: P1 = algptorat(P1); P2 = algptorat(P2);
505: return simpalg(rattoalgp(sdiv(P1*LCOEF(P2)^(DEG(P1)-DEG(P2)+1),P2),A));
506: }
507:
508: def pqra(P1,P2)
509: {
510: if ( type(P2) != POLY )
511: return [P1,0];
512: else if ( (type(P1) != POLY) || (deg(P1,var(P1)) < deg(P2,var(P1))) )
513: return [0,P1];
514: else {
1.2 noro 515: A = union_sort(getalgp(P1),getalgp(P2));
1.1 noro 516: P1 = algptorat(P1); P2 = algptorat(P2);
517: L = sqr(P1*LCOEF(P2)^(DEG(P1)-DEG(P2)+1),P2);
518: return [simpalg(rattoalgp(L[0],A)),simpalg(rattoalgp(L[1],A))];
519: }
520: }
521:
522: def pra(P1,P2,AL)
523: {
524: if ( type(P2) != POLY )
525: return 0;
526: else if ( (type(P1) != POLY) || (deg(P1,var(P1)) < deg(P2,var(P1))) )
527: return P1;
528: else {
529: F1 = algptorat(P1); F2 = algptorat(P2); ML = map(defpoly,AL);
530: B = append(reverse(ML),[F2]);
531: V0 = var(P1);
532: V = cons(V0,map(algtorat,AL));
533: G = srem_by_nf(F1,B,V,2);
534: return simpalg(rattoalgp(G[0]/G[1],AL));
535: }
536: }
537:
538: def sort_alg(VL)
539: {
540: N = length(VL); W = newvect(N,VL);
541: for ( I = 0; I < N; I++ ) {
542: for ( M = I, J = I + 1; J < N; J++ )
543: if ( W[J] > W[M] )
544: M = J;
545: if ( I != M ) {
546: T = W[I]; W[I] = W[M]; W[M] = T;
547: }
548: }
549: for ( I = N-1, L = []; I >= 0; I-- )
550: L = cons(W[I],L);
551: return L;
552: }
553:
1.2 noro 554: def asq(P)
1.1 noro 555: {
556: P = simpalg(P);
557: if ( type(P) == NUM )
558: return [[1,1]];
559: else if ( getalgp(P) == [] )
560: return sqfr(P);
561: else {
562: V = var(P); N = DEG(P); A = newvect(N+1); B = newvect(N+1);
563: for ( I = 0, F = P; ;I++ ) {
564: if ( type(F) == NUM )
565: break;
566: F1 = diff(F,V);
1.2 noro 567: GCD = cr_gcda(F,F1);
1.1 noro 568: FLAT = pdiva(F,GCD);
569: if ( type(GCD) == NUM ) {
570: A[I] = F; B[I] = 1;
571: break;
572: }
573: for ( J = 1, F = GCD; ; J++ ) {
574: L = pqra(F,FLAT); Q = L[0]; R = L[1];
575: if ( R )
576: break;
577: else
578: F = Q;
579: }
580: A[I] = FLAT; B[I] = J;
581: }
582: for ( I = 0, J = 0, L = []; A[I]; I++ ) {
583: J += B[I];
584: if ( A[I+1] )
585: C = pdiva(A[I],A[I+1]);
586: else
587: C = A[I];
588: L = cons([C,J],L);
589: }
590: return L;
591: }
592: }
593:
594: def ufctrhint1(P,HINT)
595: {
596: if ( deg(P,var(P)) == 168 ) {
597: SQ = sqfr(P);
598: if ( length(SQ) == 2 && SQ[1][1] == 1 )
599: return [[1,1],[P,1]];
600: else
601: return ufctrhint(P,HINT);
602: } else
603: return ufctrhint(P,HINT);
604: }
605:
606: def simpcoef(P) {
607: return rattoalgp(ptozp(algptorat(P)),getalgp(P));
608: }
609:
610: def ufctrhint_heuristic(P,HINT,PP,SHIFT) {
1.9 noro 611: if ( USE_PARI_FACTOR )
612: return pari_ufctr(P);
613: else
614: return asir_ufctrhint_heuristic(P,HINT,PP,SHIFT);
615: }
616:
617: def pari_ufctr(P) {
618: F = pari(factor,P);
619: S = size(F);
620: for ( I = S[0]-1, R = []; I >= 0; I-- )
621: R = cons(vtol(F[I]),R);
622: return cons([1,1],R);
623: }
624:
625: def asir_ufctrhint_heuristic(P,HINT,PP,SHIFT) {
1.1 noro 626: V = var(P); D = deg(P,V);
627: if ( D == HINT )
628: return [[P,1]];
629: for ( S = 0, L = SHIFT, AL = [], K = 1; L != []; L = cdr(L) ) {
630: A = car(L)[0]; S += A*car(L)[1]; AL = cons(A,AL);
631: K *= deg(defpoly(A),algtorat(A));
632: }
633: PPP = simpcoef(simpalg(subst(PP,V,V-S)));
634: for ( T = P-coef(P,D)*V^D, G = D; T; T -= coef(T,DT)*V^DT )
635: G = igcd(G,DT=deg(T,V));
636: if ( G == 1 ) {
637: if ( K*deg(PPP,V) != deg(P,V) )
1.2 noro 638: PPP = cr_gcda(PPP,P);
1.1 noro 639: return ufctrhint2(P,HINT,PPP,AL);
640: } else {
641: for ( S = 0, T = P; T; T -= coef(T,DT)*V^DT ) {
642: DT = deg(T,V);
643: S += coef(T,DT)*V^(DT/G);
644: }
645: L = fctr(S);
646: for ( DC = [car(L)], L = cdr(L); L != []; L = cdr(L) ) {
647: H = subst(car(car(L)),V,V^G);
1.2 noro 648: HH = cr_gcda(PPP,H);
1.1 noro 649: T = ufctrhint2(H,HINT,HH,AL);
650: DC = append(DC,T);
651: }
652: return DC;
653: }
654: }
655:
656: def ufctrhint2(P,HINT,PP,AL)
657: {
658: if ( deg(P,var(P)) == HINT )
659: return [[P,1]];
660: if ( AL == [] )
661: return ufctrhint(P,HINT);
662: L = resfctr(algptorat(PP),map(defpoly,AL),map(algtorat,AL),P);
663: for ( T = reverse(L[1]), DL = []; T != []; T = cdr(T) )
664: DL = cons(deg(car(car(T)),a_),DL);
665: return resfmain(P,L[2],L[0],DL);
666: }
667:
668: def res_det(V,P1,P2)
669: {
670: D1 = deg(P1,V); D2 = deg(P2,V);
671: M = newmat(D1+D2,D1+D2);
672: for ( J = 0; J <= D2; J++ )
673: M[0][J] = coef(P2,D2-J,V);
674: for ( I = 1; I < D1; I++ )
675: for ( J = 0; J <= D2; J++ )
676: M[I][I+J] = M[0][J];
677: for ( J = 0; J <= D1; J++ )
678: M[D1][J] = coef(P1,D1-J,V);
679: for ( I = 1; I < D2; I++ )
680: for ( J = 0; J <= D1; J++ )
681: M[D1+I][I+J] = M[D1][J];
682: return det(M);
683: }
684:
685: def norm_ch1(V0,VM,P,P0,PR) {
686: D = deg(P,V0); D0 = deg(P0,V0); DM = deg(P,VM); N = DM*D0;
687: X = newvect(N+1); V = newvect(N+1); U = newvect(N+1);
688: Min = -idiv(N,2);
689: C = coef(P,D,V0);
690: for ( I = J = 0; I <= N; J++ ) {
691: if ( PRINT )
692: print([J,N]);
693: T=J+Min;
694: if ( subst(C,VM,T) ) {
695: U[I] = srem(res(V0,subst(P,VM,T),P0),PR);
696: X[I++] = T;
697: }
698: }
699: for ( I = 1, M = 1, S = V[0] = U[0]; I <= N; I++ ) {
700: for ( J = 0, T = U[I]; J < I; J++ )
701: T = sdiv(T-V[J],X[I]-X[J]);
702: V[I] = T;
703: M *= (VM-X[I-1]);
704: S += T*M;
705: }
706: return S;
707: }
708:
709: def norm_ch2(V0,VM,P,P0,PR) {
710: D = deg(P,V0); D0 = deg(P0,V0); DM = deg(P,VM); N = DM*D0;
711: X = newvect(N+1); V = newvect(N+1); U = newvect(N+1);
712: Min = -idiv(N,2);
713: C = coef(P,D,V0);
714: for ( I = J = 0; I <= N; J++ ) {
715: T=J+Min;
716: if ( subst(C,VM,T) ) {
717: U[I] = srem(res_det(V0,subst(P,VM,T),P0),PR);
718: X[I++] = T;
719: }
720: }
721: for ( I = 1, M = 1, S = V[0] = U[0]; I <= N; I++ ) {
722: for ( J = 0, T = U[I]; J < I; J++ )
723: T = sdiv(T-V[J],X[I]-X[J]);
724: V[I] = T;
725: M *= (VM-X[I-1]);
726: S += T*M;
727: }
728: return S;
729: }
730:
731: def res_ch1(V0,VM,P,P0) {
732: D = deg(P,V0); D0 = deg(P0,V0); N = deg(P,VM)*D0+deg(P0,VM)*D;
733: X = newvect(N+1); V = newvect(N+1); U = newvect(N+1);
734: Min = -idiv(N,2);
735: C = coef(P,D,V0); C0 = coef(P0,D0,V0);
736: for ( I = J = 0; I <= N; J++ ) {
737: if ( PRINT )
738: print([J,N]);
739: T=J+Min;
740: if ( subst(C,VM,T) && subst(C0,VM,T) ) {
741: U[I] = res(V0,subst(P,VM,T),subst(P0,VM,T));
742: X[I++] = T;
743: }
744: }
745: for ( I = 1, M = 1, S = V[0] = U[0]; I <= N; I++ ) {
746: for ( J = 0, T = U[I]; J < I; J++ )
747: T = sdiv(T-V[J],X[I]-X[J]);
748: V[I] = T;
749: M *= (VM-X[I-1]);
750: S += T*M;
751: }
752: return S;
753: }
754:
755: def res_ch(V0,VM,P,P0) {
756: D = deg(P,V0); D0 = deg(P0,V0); N = deg(P,VM)*D0+deg(P0,VM)*D;
757: X = newvect(N+1); V = newvect(N+1); U = newvect(N+1);
758: Min = -idiv(N,2);
759: C = coef(P,D,V0); C0 = coef(P0,D0,V0);
760: for ( I = J = 0; I <= N; J++ ) {
761: T=J+Min;
762: if ( subst(C,VM,T) && subst(C0,VM,T) ) {
763: U[I] = res_det(V0,subst(P,VM,T),subst(P0,VM,T));
764: X[I++] = T;
765: }
766: }
767: for ( I = 1, M = 1, S = V[0] = U[0]; I <= N; I++ ) {
768: for ( J = 0, T = U[I]; J < I; J++ )
769: T = sdiv(T-V[J],X[I]-X[J]);
770: V[I] = T;
771: M *= (VM-X[I-1]);
772: S += T*M;
773: }
774: return S;
775: }
776:
777: def norm_ch2_lag(V,VM,P,P0,PR) {
778: D0 = deg(P0,V); DM = deg(P,VM); N = DM*D0;
779: Min = -idiv(N,2);
780: for ( A = 1, I = 0; I <= N; I++ )
781: A *= (VM-I-Min);
782: for ( I = 0, S = 0; I <= N; I++ ) {
783: R = res_det(V,subst(P,VM,I+Min),P0);
784: R = srem(R,PR);
785: T = sdiv(A,VM-I-Min);
786: S += R*T/subst(T,VM,I+Min);
787: }
788: return S;
789: }
790:
791: def norm_ch_lag(V,VM,P,P0) {
792: D0 = deg(P0,V); DM = deg(P,VM); N = DM*D0;
793: Min = -idiv(N,2);
794: for ( A = 1, I = 0; I <= N; I++ )
795: A *= (VM-I-Min);
796: for ( I = 0, S = 0; I <= N; I++ ) {
797: R = res_det(V,subst(P,VM,I+Min),P0);
798: T = sdiv(A,VM-I-Min);
799: S += R*T/subst(T,VM,I+Min);
800: }
801: return S;
802: }
803:
1.2 noro 804: def cr_gcda(P1,P2)
1.1 noro 805: {
1.12 ! noro 806: if ( !var(P1) )
! 807: return P2;
! 808: else if ( !var(P2) )
! 809: return P1;
! 810: V = var(P1);
1.2 noro 811: EXT = union_sort(getalgtreep(P1),getalgtreep(P2));
812: if ( EXT == [] )
1.1 noro 813: return gcd(P1,P2);
814: NEXT = length(EXT);
815: if ( deg(P1,V) < deg(P2,V) ) {
816: T = P1; P1 = P2; P2 = T;
817: }
818: G1 = ptozp(algptorat(P1)); G2 = ptozp(algptorat(P2));
819: for ( ML = VL = [], T = reverse(EXT); T != []; T = cdr(T) ) {
820: ML = cons(defpoly(car(T)),ML);
821: VL = cons(algptorat(car(T)),VL);
822: }
823: DL = [coef(G1,deg(G1,V),V),coef(G2,deg(G2,V),V)];
824: for ( T = EXT; T != []; T = cdr(T) ) {
825: DL = cons(discr(sp_minipoly(car(T),EXT)),DL);
826: C = LCOEF(defpoly(car(T)));
827: if ( C != 1 && C != -1 )
828: DL = cons(C,DL);
829: }
830: TIME = time()[0];
831: for ( D = deg(P1,V)+1, I = 0; ; I++ ) {
832: MOD = lprime(I);
833: for ( J = 0; J < length(DL); J++ )
834: if ( !(DL[J] % MOD) )
835: break;
836: if ( J != length(DL) )
837: continue;
838: Ord = 2; NOSUGAR = 1;
839: T = ag_mod(G1 % MOD,G2 % MOD,ML,VL,MOD);
840: if ( dp_gr_print() )
841: print(".");
842: if ( !T )
843: continue;
844: T = (T*inv(coef(T,deg(T,V),V),MOD))%MOD;
845: if ( deg(T,V) > D )
846: continue;
847: else if ( deg(T,V) < D ) {
848: IMAGE = T; M = MOD; D = deg(T,V);
849: } else {
850: L = cr(IMAGE,M,T,MOD); IMAGE = L[0]; M = L[1];
851: }
852: F = intptoratp(IMAGE,M,calcb(M));
853: if ( F != [] ) {
854: F = ptozp(F);
855: DIV = rattoalgp(F,EXT);
856: if ( type(DIV) == 1 )
857: return 1;
858: /*
859: if ( srem_simp(G1,F,V,ML) )
860: continue;
861: if ( srem_simp(G2,F,V,ML) )
862: continue;
863: */
864: if ( srem_by_nf(G1,reverse(cons(F,ML)),cons(V,VL),2)[0] )
865: continue;
866: if ( srem_by_nf(G2,reverse(cons(F,ML)),cons(V,VL),2)[0] )
867: continue;
868: TIME = time()[0]-TIME;
869: if ( dp_gr_print() )
870: print([TIME]);
871: GCDTIME += TIME;
872: return DIV;
873: }
874: }
875: }
876:
877: def srem_simp(F1,F2,V,D)
878: {
879: D2 = deg(F2,V); C = coef(F2,D2);
880: while ( (D1 = deg(F1,V)) >= D2 ) {
881: F1 -= coef(F1,D1)/C*V^(D1-D2)*F2;
882: F1 = simp_by_dp(F1,D);
883: }
884: return F1;
885: }
886:
887: def member(E,L)
888: {
889: for ( ; L != []; L = cdr(L) )
890: if ( E == car(L) )
891: return 1;
892: return 0;
893: }
894:
895: def discr(P) {
896: V = var(P);
897: return res(V,P,diff(P,V));
898: }
899:
900: def sp_minipoly(A,EXT)
901: {
902: while ( car(EXT) != A )
903: EXT = cdr(EXT);
904: for ( M = x-A; EXT != []; EXT = cdr(EXT) )
905: M = sp_norm(car(EXT),x,M,EXT);
906: F = sqfr(M);
907: return F[1][0];
908: }
909:
910: def cr(F1,M1,F2,M2)
911: {
912: K = inv(M1 % M2,M2);
913: M3 = M1*M2;
914: F3 = (F1 + (F2-(F1%M2))*K*M1) % M3;
915: return [F3,M3];
916: }
917:
918: #define ABS(a) ((a)>=0?(a):(-a))
919:
920: #if 0
921: def calcb(M) {
922: setprec(800);
923: return pari(floor,eval((M/2)^(1/2)));
924: }
925: #endif
926:
927: def calcb(M) {
928: N = 2*M;
929: T = sp_sqrt(N);
930: if ( T^2 <= N && N < (T+1)^2 )
931: return idiv(T,2);
932: else
933: error("afo");
934: }
935:
936: def sp_sqrt(A) {
937: for ( J = 0, T = A; T >= 2^27; J++ ) {
938: T = idiv(T,2^27)+1;
939: }
940: for ( I = 0; T >= 2; I++ ) {
941: S = idiv(T,2);
942: if ( T = S+S )
943: T = S;
944: else
945: T = S+1;
946: }
947: X = (2^27)^idiv(J,2)*2^idiv(I,2);
948: while ( 1 ) {
949: if ( (Y=X^2) < A )
950: X += X;
951: else if ( Y == A )
952: return X;
953: else
954: break;
955: }
956: while ( 1 )
957: if ( (Y = X^2) <= A )
958: return X;
959: else
960: X = idiv(A + Y,2*X);
961: }
962:
963: def intptoratp(P,M,B) {
964: if ( type(P) == 1 ) {
965: L = inttorat(P,M,B);
966: if ( L == 0 )
967: return [];
968: else
969: return L[0]/L[1];
970: } else {
971: V = var(P);
972: S = 0;
973: while ( P ) {
974: D = deg(P,V);
975: C = coef(P,D,V);
976: T = intptoratp(C,M,B);
977: if ( T == [] )
978: return [];
979: S += T*V^D;
980: P -= C*V^D;
981: }
982: return S;
983: }
984: }
985:
986: def ltoalg(L) {
987: F = L[0]; V = reverse(L[1]);
988: N = length(V)-1;
989: for ( I = 0, G = F; I < N; I++ ) {
990: D = car(G);
991: A = newalg(D); V = var(D);
992: for ( G = reverse(cdr(G)), T = []; G != []; G = cdr(G) )
993: T = cons(subst(car(G),V,A),T);
994: G = T;
995: }
996: return G;
997: }
998:
999: /*
1000: def ag_mod(F1,F2,D,MOD)
1001: {
1002: if ( length(D) == 1 )
1003: return ag_mod_single(F1,F2,D,MOD);
1004: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1005: if ( D1 < D2 ) {
1006: T = F1; F1 = F2; F2 = T;
1007: T = D1; D1 = D2; D2 = T;
1008: }
1009: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1010: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1011: for ( T = reverse(D), S = []; T != []; T = cdr(T) ) {
1012: U = car(T);
1013: S = cons((inv(LCOEF(U),MOD)*U) % MOD,S);
1014: }
1015: D = S;
1016: while ( 1 ) {
1017: F = srem_simp_mod(F1,F2,V,D,MOD);
1018: if ( !F )
1019: return F2;
1020: if ( !deg(F,V) )
1021: return 1;
1022: C = LCOEF(F);
1023: INV = inverse_by_gr_mod(C,D,MOD);
1024: if ( !INV )
1025: return 0;
1026: F = simp_by_dp_mod(F*INV,D,MOD);
1027: F = (inv(LCOEF(F),MOD)*F) % MOD;
1028: F1 = F2; F2 = F;
1029: }
1030: }
1031: */
1032:
1033: def ag_mod(F1,F2,D,VL,MOD)
1034: {
1035: if ( length(D) == 1 )
1036: return ag_mod_single6(F1,F2,D,MOD);
1037: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1038: if ( D1 < D2 ) {
1039: T = F1; F1 = F2; F2 = T;
1040: T = D1; D1 = D2; D2 = T;
1041: }
1042: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1043: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1044: for ( T = reverse(D), S = []; T != []; T = cdr(T) ) {
1045: U = car(T);
1046: S = cons((inv(LCOEF(U),MOD)*U) % MOD,S);
1047: }
1048: D = S;
1049: VL = cons(V,VL); B = append([F1,F2],D); N = length(VL);
1050: while ( 1 ) {
1051: FLAGS = dp_gr_flags(); dp_gr_flags(["Reverse",1,"NoSugar",1]);
1052: G = dp_gr_mod_main(B,VL,0,MOD,Ord);
1053: dp_gr_flags(FLAGS);
1054: if ( length(G) == 1 )
1055: return 1;
1056: if ( length(G) == N ) {
1057: for ( T = G; T != []; T = cdr(T) )
1058: if ( member(V,vars(car(T))) )
1059: return car(T);
1060: }
1061: }
1062: }
1063:
1064: def srem_simp_mod(F1,F2,V,D,MOD)
1065: {
1066: D2 = deg(F2,V); C = coef(F2,D2);
1067: while ( (D1 = deg(F1,V)) >= D2 ) {
1068: F1 -= coef(F1,D1)/C*V^(D1-D2)*F2;
1069: F1 = simp_by_dp_mod(F1,D,MOD);
1070: }
1071: return F1;
1072: }
1073:
1074: def ag_mod_single(F1,F2,D,MOD)
1075: {
1076: TD = TI = TM = 0;
1077: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1078: if ( D1 < D2 ) {
1079: T = F1; F1 = F2; F2 = T;
1080: T = D1; D1 = D2; D2 = T;
1081: }
1082: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1083: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1084: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1085: FLAGS = dp_gr_flags(); dp_gr_flags(["Reverse",1,"NoSugar",1]);
1086: G = dp_gr_mod_main([F1,F2,D],[V,var(D)],0,MOD,2);
1087: dp_gr_flags(FLAGS);
1088: if ( length(G) == 1 )
1089: return 1;
1090: if ( length(G) != 2 )
1091: return 0;
1092: if ( vars(G[0]) == [var(D)] )
1093: return G[1];
1094: else
1095: return G[0];
1096: }
1097:
1098: def ag_mod_single2(F1,F2,D,MOD)
1099: {
1100: TD = TI = TM = 0;
1101: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1102: if ( D1 < D2 ) {
1103: T = F1; F1 = F2; F2 = T;
1104: T = D1; D1 = D2; D2 = T;
1105: }
1106: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1107: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1108: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1109: while ( 1 ) {
1110: T0 = time()[0];
1111: F = srem((srem(F1,F2) % MOD),D) % MOD;
1112: TD += time()[0] - T0;
1113: if ( !F ) {
1114: if ( dp_gr_print() )
1115: print(["TD",TD,"TM",TM,"TI",TI]);
1116: return F2;
1117: }
1118: if ( !deg(F,V) ) {
1119: if ( dp_gr_print() )
1120: print(["TD",TD,"TM",TM,"TI",TI]);
1121: return 1;
1122: }
1123: C = LCOEF(F);
1124: T0 = time()[0];
1125: INV = inva_mod(D,MOD,C);
1126: TI += time()[0] - T0;
1127: if ( !INV )
1128: return 0;
1129: T0 = time()[0];
1130: F = remc_mod((INV*F) % MOD,D,MOD);
1131: TM += time()[0] - T0;
1132: F1 = F2; F2 = F;
1133: }
1134: }
1135:
1136: def ag_mod_single3(F1,F2,D,MOD)
1137: {
1138: TD = TI = TM = 0;
1139: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1140: if ( D1 < D2 ) {
1141: T = F1; F1 = F2; F2 = T;
1142: T = D1; D1 = D2; D2 = T;
1143: }
1144: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1145: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1146: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1147: while ( 1 ) {
1148: if ( !D2 )
1149: return 1;
1150: while ( D1 >= D2 ) {
1151: F = srem((coef(F2,D2,V)*F1-coef(F1,D1,V)*F2*V^(D1-D2))%MOD,D)%MOD;
1152: F1 = F; D1 = deg(F1,V);
1153: }
1154: if ( !F1 ) {
1155: INV = inva_mod(D,MOD,coef(F2,D2,V));
1156: if ( dp_gr_print() )
1157: print(".");
1158: return srem((INV*F2) % MOD,D)%MOD;
1159: } else {
1160: T = F1; F1 = F2; F2 = T;
1161: T = D1; D1 = D2; D2 = T;
1162: }
1163: }
1164: }
1165:
1166: def ag_mod_single4(F1,F2,D,MOD)
1167: {
1168: if ( !F1 )
1169: return F2;
1170: if ( !F2 )
1171: return F1;
1172: TD = TI = TM = TR = 0;
1173: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1174: if ( D1 < D2 ) {
1175: T = F1; F1 = F2; F2 = T;
1176: T = D1; D1 = D2; D2 = T;
1177: }
1178: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1179: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1180: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1181: while ( 1 ) {
1182: T0 = time()[0]; R = srem(F1,F2); TR += time()[0] - T0;
1183: T0 = time()[0]; F = srem(R % MOD,D) % MOD; TD += time()[0] - T0;
1184: if ( !F ) {
1185: if ( dp_gr_print() )
1186: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1187: return F2;
1188: }
1189: if ( !deg(F,V) ) {
1190: if ( dp_gr_print() )
1191: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1192: return 1;
1193: }
1194: C = LCOEF(F);
1195: T0 = time()[0]; INV = inva_mod(D,MOD,C); TI += time()[0] - T0;
1196: if ( !INV )
1197: return 0;
1198: T0 = time()[0]; F = srem((INV*F) % MOD,D) % MOD; TM += time()[0] - T0;
1199: F1 = F2; F2 = F;
1200: }
1201: }
1202:
1203: def ag_mod_single5(F1,F2,D,MOD)
1204: {
1205: TD = TI = TM = TR = 0;
1206: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1207: if ( D1 < D2 ) {
1208: T = F1; F1 = F2; F2 = T;
1209: T = D1; D1 = D2; D2 = T;
1210: }
1211: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1212: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1213: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1214: while ( 1 ) {
1215: T0 = time()[0];
1216: D1 = deg(F1,V); D2 = deg(F2,V); F = F1;
1217: while ( D1 >= D2 ) {
1218: R = (F-coef(F,D1,V)*F2*V^(D1-D2))%MOD;
1219: D1 = deg(R,V); HC = coef(R,D1,V);
1220: F = (R - HC*V^D1) + (srem(HC,D)%MOD)*V^D1;
1221: }
1222: TR += time()[0] - T0;
1223: T0 = time()[0]; F = srem(R % MOD,D) % MOD; TD += time()[0] - T0;
1224: if ( !F ) {
1225: if ( dp_gr_print() )
1226: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1227: return F2;
1228: }
1229: if ( !deg(F,V) ) {
1230: if ( dp_gr_print() )
1231: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1232: return 1;
1233: }
1234: C = LCOEF(F);
1235: T0 = time()[0]; INV = inva_mod(D,MOD,C); TI += time()[0] - T0;
1236: if ( !INV )
1237: return 0;
1238: T0 = time()[0]; F = srem((INV*F) % MOD,D) % MOD; TM += time()[0] - T0;
1239: F1 = F2; F2 = F;
1240: }
1241: }
1242:
1243: def ag_mod_single6(F1,F2,D,MOD)
1244: {
1245: TD = TI = TM = TR = 0;
1246: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1247: if ( D1 < D2 ) {
1248: T = F1; F1 = F2; F2 = T;
1249: T = D1; D1 = D2; D2 = T;
1250: }
1251: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1252: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1253: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1254: while ( 1 ) {
1255: T0 = time()[0];
1256: D1 = deg(F1,V); D2 = deg(F2,V); F = F1;
1257: while ( D1 >= D2 ) {
1258: R = (F-coef(F,D1,V)*F2*V^(D1-D2))%MOD;
1259: D1 = deg(R,V); HC = coef(R,D1,V);
1260: /* F = (R - HC*V^D1) + (srem_mod(HC,D,MOD))*V^D1; */
1261: F = remc_mod(R,D,MOD);
1262: }
1263: TR += time()[0] - T0;
1264: T0 = time()[0]; F = remc_mod(R%MOD,D,MOD); TD += time()[0] - T0;
1265: if ( !F ) {
1266: if ( dp_gr_print() )
1267: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1268: return F2;
1269: }
1270: if ( !deg(F,V) ) {
1271: if ( dp_gr_print() )
1272: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1273: return 1;
1274: }
1275: C = LCOEF(F);
1276: T0 = time()[0]; INV = inva_mod(D,MOD,C); TI += time()[0] - T0;
1277: if ( !INV )
1278: return 0;
1279: T0 = time()[0]; F = remc_mod((INV*F)%MOD,D,MOD); TM += time()[0] - T0;
1280: F1 = F2; F2 = F;
1281: }
1282: }
1283:
1284: def inverse_by_gr_mod(C,D,MOD)
1285: {
1286: Ord = 2;
1287: dp_gr_flags(["NoSugar",1]);
1288: G = dp_gr_mod_main(cons(x*C-1,D),cons(x,vars(D)),0,MOD,Ord);
1289: dp_gr_flags(["NoSugar",0]);
1290: if ( length(G) == 1 )
1291: return 1;
1292: else if ( length(G) == length(D)+1 ) {
1293: for ( T = G; T != []; T = cdr(T) )
1294: if ( member(x,vars(car(G))) )
1295: break;
1296: T = car(G);
1297: if ( type(coef(T,1,x)) != NUM )
1298: return 0;
1299: else
1300: return coef(T,0,x);
1301: } else
1302: return 0;
1303: }
1304:
1305: def simp_by_dp(F,D)
1306: {
1307: for ( T = D; T != []; T = cdr(T) )
1308: F = srem(F,car(T));
1309: return F;
1310: }
1311:
1312: def simp_by_dp_mod(F,D,MOD)
1313: {
1314: F %= MOD;
1315: for ( T = D; T != []; T = cdr(T) )
1316: F = srem(F,car(T)) % MOD;
1317: return F;
1318: }
1319:
1320: def remc_mod(P,D,M)
1321: {
1322: V = var(P);
1323: if ( !V || V == var(D) )
1324: return srem_mod(P,D,M);
1325: for ( I = deg(P,V), S = 0; I >= 0; I-- )
1326: if ( C = coef(P,I,V) )
1327: S += srem_mod(C,D,M)*V^I;
1328: return S;
1329: }
1330:
1331: def rem_mod(C,D,M)
1332: {
1333: V = var(D);
1334: D2 = deg(D,V);
1335: while ( (D1 = deg(C,V)) >= D2 ) {
1336: C -= (D*V^(D1-D2)*coef(C,D1,V))%M;
1337: C %= M;
1338: }
1339: return C;
1340: }
1341:
1342: def resfctr(F,L,V,N)
1343: {
1344: N = ptozp(N);
1345: V0 = var(N);
1346: DN = diff(N,V0);
1.10 noro 1347: LC = coef(N,deg(N,V0),V0);
1348: LCD = coef(DN,deg(DN,V0),V0);
1.1 noro 1349: for ( I = 0, J = 2, Len = deg(N,V0)+1; I < 5; J++ ) {
1350: M = prime(J);
1.10 noro 1351: if ( !(LC%M) || !(LCD%M))
1352: continue;
1.1 noro 1353: G = gcd(N,DN,M);
1354: if ( !deg(G,V0) ) {
1355: I++;
1356: T = nfctr_mod(N,M);
1357: if ( T < Len ) {
1358: Len = T; M0 = M;
1359: }
1360: }
1361: }
1362: S = spm(L,V,M0);
1363: T = resfctr_mod(F,S,M0);
1364: return [T,S,M0];
1365: }
1366:
1367: def resfctr_mod(F,L,M)
1368: {
1369: for ( T = L, R = []; T != []; T = cdr(T) ) {
1370: U = car(T); MP = U[0]; W = U[1];
1371: for ( A = W, B = F; A != []; A = cdr(cdr(A)) )
1372: B = sremm(subst(B,A[0],A[1]),MP,M);
1373: C = res(var(MP),B,MP) % M;
1374: R = cons(flatten(cdr(modfctr(C,M))),R);
1375: }
1376: return R;
1377: }
1378:
1379: def flatten(L)
1380: {
1381: for ( T = L, R = []; T != []; T = cdr(T) )
1382: R = cons(car(car(T)),R);
1383: return R;
1384: }
1385:
1386: def spm(L,V,M)
1387: {
1388: if ( length(V) == 1 ) {
1389: U = modfctr(car(L),M);
1390: for ( T = cdr(U), R = []; T != []; T = cdr(T) ) {
1391: S = car(T);
1392: R = cons([subst(S[0],var(S[0]),a_),[var(S[0]),a_]],R);
1393: }
1394: return R;
1395: }
1396: L1 = spm(cdr(L),cdr(V),M);
1397: F0 = car(L); V0 = car(V); VR = cdr(V);
1398: for ( T = L1, R = []; T != []; T = cdr(T) ) {
1399: S = car(T);
1400: F1 = subst(F0,S[1]);
1401: U = fctr_mod(F1,V0,S[0],M);
1402: VS = var(S[0]);
1403: for ( W = U; W != []; W = cdr(W) ) {
1404: A = car(car(W));
1405: if ( deg(A,V0) == 1 ) {
1406: A = monic_mod(A,V0,S[0],M);
1407: R = cons([S[0],append([V0,-coef(A,0,V0)],S[1])],R);
1408: } else {
1409: B = pe_mod(A,S[0],M);
1410: MP = B[0]; VMP = var(MP); NV = B[1];
1411: for ( C = S[1], D = []; C != []; C = cdr(cdr(C)) ) {
1412: G = subst(sremm(subst(C[1],VS,NV[1]),MP,M),VMP,VS);
1413: D = append([C[0],G],D);
1414: }
1415: R = cons([subst(MP,VMP,VS),
1416: append([B[2][0],subst(B[2][1],VMP,VS)],D)],R);
1417: }
1418: }
1419: }
1420: return R;
1421: }
1422:
1423: def pe_mod(F,G,M)
1424: {
1425: V = var(G); W = car(setminus(vars(F),[V]));
1426: NG = deg(G,V); NF = deg(F,W); N = NG*NF;
1427: X = prim;
1428: while ( 1 ) {
1429: D = mrandompoly(N,X,M);
1430: if ( irred_check(D,M) )
1431: break;
1432: }
1433: L = fctr_mod(G,V,D,M);
1434: for ( T = L; T != []; T = cdr(T) ) {
1435: U = car(car(T));
1436: if ( deg(U,V) == 1 )
1437: break;
1438: }
1439: U = monic_mod(U,V,D,M); RV = -coef(U,0,V);
1440: L = fctr_mod(sremm(subst(F,V,RV),D,M),W,D,M);
1441: for ( T = L; T != []; T = cdr(T) ) {
1442: U = car(car(T));
1443: if ( deg(U,W) == 1 )
1444: break;
1445: }
1446: U = monic_mod(U,W,D,M); RW = -coef(U,0,W);
1447: return [D,[V,RV],[W,RW]];
1448: }
1449:
1450: def fctr_mod(F,V,D,M)
1451: {
1452: if ( V != x ) {
1453: F = subst(F,V,x); V0 = V; V = x;
1454: } else
1455: V0 = x;
1456: F = monic_mod(F,V,D,M);
1457: L = sqfr_mod(F,V,D,M);
1458: for ( R = [], T = L; T != []; T = cdr(T) ) {
1459: S = car(T); A = S[0]; E = S[1];
1460: B = ddd_mod(A,V,D,M);
1461: R = append(append_mult(B,E),R);
1462: }
1463: if ( V0 != x ) {
1464: for ( R = reverse(R), T = []; R != []; R = cdr(R) )
1465: T = cons([subst(car(R)[0],x,V0),car(R)[1]],T);
1466: R = T;
1467: }
1468: return R;
1469: }
1470:
1471: def append_mult(L,E)
1472: {
1473: for ( T = L, R = []; T != []; T = cdr(T) )
1474: R = cons([car(T),E],R);
1475: return R;
1476: }
1477:
1478: def sqfr_mod(F,V,D,M)
1479: {
1480: setmod(M);
1481: F = sremm(F,D,M);
1482: F1 = sremm(diff(F,V),D,M);
1483: F1 = sremm(F1*inva_mod(D,M,LCOEF(F1)),D,M);
1484: if ( F1 ) {
1485: F2 = ag_mod_single4(F,F1,[D],M);
1486: FLAT = sremm(sdivm(F,F2,M,V),D,M);
1487: I = 0; L = [];
1488: while ( deg(FLAT,V) ) {
1489: while ( 1 ) {
1490: QR = sqrm(F,FLAT,M,V);
1491: if ( !sremm(QR[1],D,M) ) {
1492: F = sremm(QR[0],D,M); I++;
1493: } else
1494: break;
1495: }
1496: if ( !deg(F,V) )
1497: FLAT1 = 1;
1498: else
1499: FLAT1 = ag_mod_single4(F,FLAT,[D],M);
1500: G = sremm(sdivm(FLAT,FLAT1,M,V),D,M);
1501: FLAT = FLAT1;
1502: L = cons([G,I],L);
1503: }
1504: }
1505: if ( deg(F,V) ) {
1506: T = sqfr_mod(pthroot_p_mod(F,V,D,M),V,D,M);
1507: for ( R = []; T != []; T = cdr(T) ) {
1508: H = car(T); R = cons([H[0],M*H[1]],R);
1509: }
1510: } else
1511: R = [];
1512: return append(L,R);
1513: }
1514:
1515: def pthroot_p_mod(F,V,D,M)
1516: {
1517: for ( T = F, R = 0; T; ) {
1518: D1 = deg(T,V); C = coef(T,D1,V); T -= C*V^D1;
1519: R += pthroot_n_mod(C,D,M)*V^idiv(D1,M);
1520: }
1521: return R;
1522: }
1523:
1524: def pthroot_n_mod(C,D,M)
1525: {
1526: pwr_n_mod(C,D,M,deg(D,var(D))-1);
1527: }
1528:
1529: def pwr_n_mod(C,D,M,N)
1530: {
1531: if ( N == 0 )
1532: return 1;
1533: else if ( N == 1 )
1534: return C;
1535: else {
1536: QR = iqr(N,2);
1537: T = pwr_n_mod(C,D,M,QR[0]);
1538: S = sremm(T^2,D,M);
1539: if ( QR[1] )
1540: return sremm(S*C,D,M);
1541: else
1542: return S;
1543: }
1544: }
1545:
1546: def pwr_p_mod(P,A,V,D,M,N)
1547: {
1548: if ( N == 0 )
1549: return 1;
1550: else if ( N == 1 )
1551: return P;
1552: else {
1553: QR = iqr(N,2);
1554: T = pwr_p_mod(P,A,V,D,M,QR[0]);
1555: S = sremm(sremm(sremm(T^2,D,M),A,M,V),D,M);
1556: if ( QR[1] )
1557: return sremm(sremm(sremm(S*P,D,M),A,M,V),D,M);
1558: else
1559: return S;
1560: }
1561: }
1562:
1563: def qmat_mod(F,V,D,M)
1564: {
1565: R = tab_mod(F,V,D,M);
1566: Q = newmat(N,N);
1567: for ( J = 0; J < N; J++ )
1568: for ( I = 0, T = R[J]; I < N; I++ ) {
1569: Q[I][J] = coef(T,I);
1570: }
1571: for ( I = 0; I < N; I++ )
1572: Q[I][I] = (Q[I][I]+(M-1))%M;
1573: return Q;
1574: }
1575:
1576: def tab_mod(F,V,D,M)
1577: {
1578: MD = M^deg(D,var(D));
1579: N = deg(F,V);
1580: F = sremm(F*inva_mod(D,M,coef(F,N,V)),D,M);
1581: R = newvect(N); R[0] = 1;
1582: R[1] = pwr_mod(V,F,V,D,M,MD);
1583: for ( I = 2; I < N; I++ )
1584: R[I] = sremm(sremm(R[1]*R[I-1],F,M),D,M);
1585: return R;
1586: }
1587:
1588: def ddd_mod(F,V,D,M)
1589: {
1590: if ( deg(F,V) == 1 )
1591: return [F];
1592: TAB = tab_mod(F,V,D,M);
1593: for ( I = 1, W = V, L = []; 2*I <= deg(F,V); I++ ) {
1594: for ( T = 0, K = 0; K <= deg(W,V); K++ )
1595: if ( C = coef(W,K,V) )
1596: T = sremm(T+TAB[K]*C,D,M);
1597: W = T;
1598: GCD = ag_mod_single4(F,monic_mod(W-V,V,D,M),[D],M);
1599: if ( deg(GCD,V) ) {
1600: L = append(berlekamp(GCD,V,I,TAB,D,M),L);
1601: F = sremm(sdivm(F,GCD,M,V),D,M);
1602: W = sremm(sremm(W,F,M,V),D,M);
1603: }
1604: }
1605: if ( deg(F,V) )
1606: return cons(F,L);
1607: else
1608: return L;
1609: }
1610:
1611: def monic_mod(F,V,D,M) {
1612: if ( !F || !deg(F,V) )
1613: return F;
1614: return sremm(F*inva_mod(D,M,coef(F,deg(F,V),V)),D,M);
1615: }
1616:
1617: def berlekamp(F,V,E,TAB,D,M)
1618: {
1619: N = deg(F,V);
1620: Q = newmat(N,N);
1621: for ( J = 0; J < N; J++ ) {
1622: T = sremm(sremm(TAB[J],F,M,V),D,M);
1623: for ( I = 0; I < N; I++ ) {
1624: Q[I][J] = coef(T,I);
1625: }
1626: }
1627: for ( I = 0; I < N; I++ )
1628: Q[I][I] = (Q[I][I]+(M-1))%M;
1629: L = nullspace(Q,D,M); MT = L[0]; IND = L[1];
1630: NF0 = N/E;
1631: PS = null_to_poly(MT,IND,V,M);
1632: R = newvect(NF0); R[0] = monic_mod(F,V,D,M);
1633: for ( I = 1, NF = 1; NF < NF0 && I < NF0; I++ ) {
1634: PSI = PS[I];
1635: MP = minipoly_mod(PSI,F,V,D,M);
1636: ROOT = find_root(MP,V,D,M); NR = length(ROOT);
1637: for ( J = 0; J < NF; J++ ) {
1638: if ( deg(R[J],V) == E )
1639: continue;
1640: for ( K = 0; K < NR; K++ ) {
1641: GCD = ag_mod_single4(R[J],PSI-ROOT[K],[D],M);
1642: if ( deg(GCD,V) > 0 && deg(GCD,V) < deg(R[J],V) ) {
1643: Q = sremm(sdivm(R[J],GCD,M,V),D,M);
1644: R[J] = Q; R[NF++] = GCD;
1645: }
1646: }
1647: }
1648: }
1649: return vtol(R);
1650: }
1651:
1652: def null_to_poly(MT,IND,V,M)
1653: {
1654: N = size(MT)[0];
1655: for ( I = 0, J = 0; I < N; I++ )
1656: if ( IND[I] )
1657: J++;
1658: R = newvect(J);
1659: for ( I = 0, L = 0; I < N; I++ ) {
1660: if ( !IND[I] )
1661: continue;
1662: for ( J = K = 0, T = 0; J < N; J++ )
1663: if ( !IND[J] )
1664: T += MT[K++][I]*V^J;
1665: else if ( J == I )
1666: T += (M-1)*V^I;
1667: R[L++] = T;
1668: }
1669: return R;
1670: }
1671:
1672: def minipoly_mod(P,F,V,D,M)
1673: {
1674: L = [[1,1]]; P0 = P1 = 1;
1675: while ( 1 ) {
1676: P0 *= V;
1677: P1 = sremm(sremm(P*P1,F,M,V),D,M);
1678: L1 = lnf_mod(P0,P1,L,V,D,M); NP0 = L1[0]; NP1 = L1[1];
1679: if ( !NP1 )
1680: return NP0;
1681: else
1682: L = lnf_insert([NP0,NP1],L,V);
1683: }
1684: }
1685:
1686: def lnf_mod(P0,P1,L,V,D,M)
1687: {
1688: NP0 = P0; NP1 = P1;
1689: for ( T = L; T != []; T = cdr(T) ) {
1690: Q = car(T);
1691: D1 = deg(NP1,V);
1692: if ( D1 == deg(Q[1],V) ) {
1693: C = coef(Q[1],D1,V);
1694: INV = inva_mod(D,M,M-C); H = sremm(coef(NP1,D1,V)*INV,D,M);
1695: NP0 = sremm(NP0+Q[0]*H,D,M);
1696: NP1 = sremm(NP1+Q[1]*H,D,M);
1697: }
1698: }
1699: return [NP0,NP1];
1700: }
1701:
1702: def lnf_insert(P,L,V)
1703: {
1704: if ( L == [] )
1705: return [P];
1706: else {
1707: P0 = car(L);
1708: if ( deg(P0[1],V) > deg(P[1],V) )
1709: return cons(P0,lnf_insert(P,cdr(L),V));
1710: else
1711: return cons(P,L);
1712: }
1713: }
1714:
1715: def find_root(P,V,D,M)
1716: {
1717: L = c_z(P,V,1,D,M);
1718: for ( T = L, U = []; T != []; T = cdr(T) ) {
1719: S = monic_mod(car(T),V,D,M); U = cons(-coef(S,0,V),U);
1720: }
1721: return U;
1722: }
1723:
1724: def c_z(F,V,E,D,M)
1725: {
1726: N = deg(F,V);
1727: if ( N == E )
1728: return [F];
1729: Q = M^deg(D,var(D));
1730: K = idiv(N,E);
1731: L = [F];
1732: while ( 1 ) {
1733: W = mrandomgfpoly(2*E,V,D,M);
1734: if ( M == 2 ) {
1735: W = monic_mod(tr_mod(W,F,V,D,M,N-1),V,D,M);
1736: } else {
1737: /* W = monic_mod(pwr_p_mod(W,F,V,D,M,idiv(Q^E-1,2))-1,V,D,M); */
1738: /* T = pwr_p_mod(W,F,V,D,M,idiv(Q^E-1,2)); */
1739: T = pwr_mod(W,F,V,D,M,idiv(Q^E-1,2));
1740: W = monic_mod(T-1,V,D,M);
1741: }
1742: if ( !W )
1743: continue;
1744: G = ag_mod_single4(F,W,[D],M);
1745: if ( deg(G,V) && deg(G,V) < N ) {
1746: L1 = c_z(G,V,E,D,M);
1747: L2 = c_z(sremm(sdivm(F,G,M,V),D,M),V,E,D,M);
1748: return append(L1,L2);
1749: }
1750: }
1751: }
1752:
1753: def tr_mod(P,F,V,D,M,N)
1754: {
1755: for ( I = 1, S = P, W = P; I <= N; I++ ) {
1756: W = sremm(sremm(W^2,F,M,V),D,M);
1757: S = sremm(S+W,D,M);
1758: }
1759: return S;
1760: }
1761:
1762: def mrandomgfpoly(N,V,D,M)
1763: {
1764: W = var(D); ND = deg(D,W);
1765: for ( I = N-2, S = V^(N-1); I >= 0; I-- )
1766: S += randompoly(ND,W,M)*V^I;
1767: return S;
1768: }
1769:
1770: def randompoly(N,V,M)
1771: {
1772: for ( I = 0, S = 0; I < N; I++ )
1773: S += (random()%M)*V^I;
1774: return S;
1775: }
1776:
1777: def mrandompoly(N,V,M)
1778: {
1779: for ( I = N-1, S = V^N; I >=0; I-- )
1780: S += (random()%M)*V^I;
1781: return S;
1782: }
1783:
1784: def srem_by_nf(P,B,V,O) {
1785: dp_ord(O); DP = dp_ptod(P,V);
1786: N = length(B); DB = newvect(N);
1787: for ( I = N-1, IL = []; I >= 0; I-- ) {
1788: DB[I] = dp_ptod(B[I],V);
1789: IL = cons(I,IL);
1790: }
1791: L = dp_true_nf(IL,DP,DB,1);
1792: return [dp_dtop(L[0],V),L[1]];
1793: }
1794: end$
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>