Annotation of OpenXM_contrib2/asir2000/lib/sp, Revision 1.13
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.13 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/lib/sp,v 1.12 2004/01/07 08:23:11 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.13 ! noro 806: if ( !P1 )
1.12 noro 807: return P2;
1.13 ! noro 808: if ( !P2 )
1.12 noro 809: return P1;
1.13 ! noro 810: if ( !var(P1) || !var(P2) )
! 811: return 1;
1.12 noro 812: V = var(P1);
1.2 noro 813: EXT = union_sort(getalgtreep(P1),getalgtreep(P2));
814: if ( EXT == [] )
1.1 noro 815: return gcd(P1,P2);
816: NEXT = length(EXT);
817: if ( deg(P1,V) < deg(P2,V) ) {
818: T = P1; P1 = P2; P2 = T;
819: }
820: G1 = ptozp(algptorat(P1)); G2 = ptozp(algptorat(P2));
821: for ( ML = VL = [], T = reverse(EXT); T != []; T = cdr(T) ) {
822: ML = cons(defpoly(car(T)),ML);
823: VL = cons(algptorat(car(T)),VL);
824: }
825: DL = [coef(G1,deg(G1,V),V),coef(G2,deg(G2,V),V)];
826: for ( T = EXT; T != []; T = cdr(T) ) {
827: DL = cons(discr(sp_minipoly(car(T),EXT)),DL);
828: C = LCOEF(defpoly(car(T)));
829: if ( C != 1 && C != -1 )
830: DL = cons(C,DL);
831: }
832: TIME = time()[0];
833: for ( D = deg(P1,V)+1, I = 0; ; I++ ) {
834: MOD = lprime(I);
835: for ( J = 0; J < length(DL); J++ )
836: if ( !(DL[J] % MOD) )
837: break;
838: if ( J != length(DL) )
839: continue;
840: Ord = 2; NOSUGAR = 1;
841: T = ag_mod(G1 % MOD,G2 % MOD,ML,VL,MOD);
842: if ( dp_gr_print() )
843: print(".");
844: if ( !T )
845: continue;
846: T = (T*inv(coef(T,deg(T,V),V),MOD))%MOD;
847: if ( deg(T,V) > D )
848: continue;
849: else if ( deg(T,V) < D ) {
850: IMAGE = T; M = MOD; D = deg(T,V);
851: } else {
852: L = cr(IMAGE,M,T,MOD); IMAGE = L[0]; M = L[1];
853: }
854: F = intptoratp(IMAGE,M,calcb(M));
855: if ( F != [] ) {
856: F = ptozp(F);
857: DIV = rattoalgp(F,EXT);
858: if ( type(DIV) == 1 )
859: return 1;
860: /*
861: if ( srem_simp(G1,F,V,ML) )
862: continue;
863: if ( srem_simp(G2,F,V,ML) )
864: continue;
865: */
866: if ( srem_by_nf(G1,reverse(cons(F,ML)),cons(V,VL),2)[0] )
867: continue;
868: if ( srem_by_nf(G2,reverse(cons(F,ML)),cons(V,VL),2)[0] )
869: continue;
870: TIME = time()[0]-TIME;
871: if ( dp_gr_print() )
872: print([TIME]);
873: GCDTIME += TIME;
874: return DIV;
875: }
876: }
877: }
878:
879: def srem_simp(F1,F2,V,D)
880: {
881: D2 = deg(F2,V); C = coef(F2,D2);
882: while ( (D1 = deg(F1,V)) >= D2 ) {
883: F1 -= coef(F1,D1)/C*V^(D1-D2)*F2;
884: F1 = simp_by_dp(F1,D);
885: }
886: return F1;
887: }
888:
889: def member(E,L)
890: {
891: for ( ; L != []; L = cdr(L) )
892: if ( E == car(L) )
893: return 1;
894: return 0;
895: }
896:
897: def discr(P) {
898: V = var(P);
899: return res(V,P,diff(P,V));
900: }
901:
902: def sp_minipoly(A,EXT)
903: {
904: while ( car(EXT) != A )
905: EXT = cdr(EXT);
906: for ( M = x-A; EXT != []; EXT = cdr(EXT) )
907: M = sp_norm(car(EXT),x,M,EXT);
908: F = sqfr(M);
909: return F[1][0];
910: }
911:
912: def cr(F1,M1,F2,M2)
913: {
914: K = inv(M1 % M2,M2);
915: M3 = M1*M2;
916: F3 = (F1 + (F2-(F1%M2))*K*M1) % M3;
917: return [F3,M3];
918: }
919:
920: #define ABS(a) ((a)>=0?(a):(-a))
921:
922: #if 0
923: def calcb(M) {
924: setprec(800);
925: return pari(floor,eval((M/2)^(1/2)));
926: }
927: #endif
928:
929: def calcb(M) {
930: N = 2*M;
931: T = sp_sqrt(N);
932: if ( T^2 <= N && N < (T+1)^2 )
933: return idiv(T,2);
934: else
935: error("afo");
936: }
937:
938: def sp_sqrt(A) {
939: for ( J = 0, T = A; T >= 2^27; J++ ) {
940: T = idiv(T,2^27)+1;
941: }
942: for ( I = 0; T >= 2; I++ ) {
943: S = idiv(T,2);
944: if ( T = S+S )
945: T = S;
946: else
947: T = S+1;
948: }
949: X = (2^27)^idiv(J,2)*2^idiv(I,2);
950: while ( 1 ) {
951: if ( (Y=X^2) < A )
952: X += X;
953: else if ( Y == A )
954: return X;
955: else
956: break;
957: }
958: while ( 1 )
959: if ( (Y = X^2) <= A )
960: return X;
961: else
962: X = idiv(A + Y,2*X);
963: }
964:
965: def intptoratp(P,M,B) {
966: if ( type(P) == 1 ) {
967: L = inttorat(P,M,B);
968: if ( L == 0 )
969: return [];
970: else
971: return L[0]/L[1];
972: } else {
973: V = var(P);
974: S = 0;
975: while ( P ) {
976: D = deg(P,V);
977: C = coef(P,D,V);
978: T = intptoratp(C,M,B);
979: if ( T == [] )
980: return [];
981: S += T*V^D;
982: P -= C*V^D;
983: }
984: return S;
985: }
986: }
987:
988: def ltoalg(L) {
989: F = L[0]; V = reverse(L[1]);
990: N = length(V)-1;
991: for ( I = 0, G = F; I < N; I++ ) {
992: D = car(G);
993: A = newalg(D); V = var(D);
994: for ( G = reverse(cdr(G)), T = []; G != []; G = cdr(G) )
995: T = cons(subst(car(G),V,A),T);
996: G = T;
997: }
998: return G;
999: }
1000:
1001: /*
1002: def ag_mod(F1,F2,D,MOD)
1003: {
1004: if ( length(D) == 1 )
1005: return ag_mod_single(F1,F2,D,MOD);
1006: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1007: if ( D1 < D2 ) {
1008: T = F1; F1 = F2; F2 = T;
1009: T = D1; D1 = D2; D2 = T;
1010: }
1011: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1012: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1013: for ( T = reverse(D), S = []; T != []; T = cdr(T) ) {
1014: U = car(T);
1015: S = cons((inv(LCOEF(U),MOD)*U) % MOD,S);
1016: }
1017: D = S;
1018: while ( 1 ) {
1019: F = srem_simp_mod(F1,F2,V,D,MOD);
1020: if ( !F )
1021: return F2;
1022: if ( !deg(F,V) )
1023: return 1;
1024: C = LCOEF(F);
1025: INV = inverse_by_gr_mod(C,D,MOD);
1026: if ( !INV )
1027: return 0;
1028: F = simp_by_dp_mod(F*INV,D,MOD);
1029: F = (inv(LCOEF(F),MOD)*F) % MOD;
1030: F1 = F2; F2 = F;
1031: }
1032: }
1033: */
1034:
1035: def ag_mod(F1,F2,D,VL,MOD)
1036: {
1037: if ( length(D) == 1 )
1038: return ag_mod_single6(F1,F2,D,MOD);
1039: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1040: if ( D1 < D2 ) {
1041: T = F1; F1 = F2; F2 = T;
1042: T = D1; D1 = D2; D2 = T;
1043: }
1044: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1045: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1046: for ( T = reverse(D), S = []; T != []; T = cdr(T) ) {
1047: U = car(T);
1048: S = cons((inv(LCOEF(U),MOD)*U) % MOD,S);
1049: }
1050: D = S;
1051: VL = cons(V,VL); B = append([F1,F2],D); N = length(VL);
1052: while ( 1 ) {
1053: FLAGS = dp_gr_flags(); dp_gr_flags(["Reverse",1,"NoSugar",1]);
1054: G = dp_gr_mod_main(B,VL,0,MOD,Ord);
1055: dp_gr_flags(FLAGS);
1056: if ( length(G) == 1 )
1057: return 1;
1058: if ( length(G) == N ) {
1059: for ( T = G; T != []; T = cdr(T) )
1060: if ( member(V,vars(car(T))) )
1061: return car(T);
1062: }
1063: }
1064: }
1065:
1066: def srem_simp_mod(F1,F2,V,D,MOD)
1067: {
1068: D2 = deg(F2,V); C = coef(F2,D2);
1069: while ( (D1 = deg(F1,V)) >= D2 ) {
1070: F1 -= coef(F1,D1)/C*V^(D1-D2)*F2;
1071: F1 = simp_by_dp_mod(F1,D,MOD);
1072: }
1073: return F1;
1074: }
1075:
1076: def ag_mod_single(F1,F2,D,MOD)
1077: {
1078: TD = TI = TM = 0;
1079: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1080: if ( D1 < D2 ) {
1081: T = F1; F1 = F2; F2 = T;
1082: T = D1; D1 = D2; D2 = T;
1083: }
1084: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1085: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1086: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1087: FLAGS = dp_gr_flags(); dp_gr_flags(["Reverse",1,"NoSugar",1]);
1088: G = dp_gr_mod_main([F1,F2,D],[V,var(D)],0,MOD,2);
1089: dp_gr_flags(FLAGS);
1090: if ( length(G) == 1 )
1091: return 1;
1092: if ( length(G) != 2 )
1093: return 0;
1094: if ( vars(G[0]) == [var(D)] )
1095: return G[1];
1096: else
1097: return G[0];
1098: }
1099:
1100: def ag_mod_single2(F1,F2,D,MOD)
1101: {
1102: TD = TI = TM = 0;
1103: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1104: if ( D1 < D2 ) {
1105: T = F1; F1 = F2; F2 = T;
1106: T = D1; D1 = D2; D2 = T;
1107: }
1108: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1109: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1110: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1111: while ( 1 ) {
1112: T0 = time()[0];
1113: F = srem((srem(F1,F2) % MOD),D) % MOD;
1114: TD += time()[0] - T0;
1115: if ( !F ) {
1116: if ( dp_gr_print() )
1117: print(["TD",TD,"TM",TM,"TI",TI]);
1118: return F2;
1119: }
1120: if ( !deg(F,V) ) {
1121: if ( dp_gr_print() )
1122: print(["TD",TD,"TM",TM,"TI",TI]);
1123: return 1;
1124: }
1125: C = LCOEF(F);
1126: T0 = time()[0];
1127: INV = inva_mod(D,MOD,C);
1128: TI += time()[0] - T0;
1129: if ( !INV )
1130: return 0;
1131: T0 = time()[0];
1132: F = remc_mod((INV*F) % MOD,D,MOD);
1133: TM += time()[0] - T0;
1134: F1 = F2; F2 = F;
1135: }
1136: }
1137:
1138: def ag_mod_single3(F1,F2,D,MOD)
1139: {
1140: TD = TI = TM = 0;
1141: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1142: if ( D1 < D2 ) {
1143: T = F1; F1 = F2; F2 = T;
1144: T = D1; D1 = D2; D2 = T;
1145: }
1146: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1147: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1148: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1149: while ( 1 ) {
1150: if ( !D2 )
1151: return 1;
1152: while ( D1 >= D2 ) {
1153: F = srem((coef(F2,D2,V)*F1-coef(F1,D1,V)*F2*V^(D1-D2))%MOD,D)%MOD;
1154: F1 = F; D1 = deg(F1,V);
1155: }
1156: if ( !F1 ) {
1157: INV = inva_mod(D,MOD,coef(F2,D2,V));
1158: if ( dp_gr_print() )
1159: print(".");
1160: return srem((INV*F2) % MOD,D)%MOD;
1161: } else {
1162: T = F1; F1 = F2; F2 = T;
1163: T = D1; D1 = D2; D2 = T;
1164: }
1165: }
1166: }
1167:
1168: def ag_mod_single4(F1,F2,D,MOD)
1169: {
1170: if ( !F1 )
1171: return F2;
1172: if ( !F2 )
1173: return F1;
1174: TD = TI = TM = TR = 0;
1175: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1176: if ( D1 < D2 ) {
1177: T = F1; F1 = F2; F2 = T;
1178: T = D1; D1 = D2; D2 = T;
1179: }
1180: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1181: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1182: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1183: while ( 1 ) {
1184: T0 = time()[0]; R = srem(F1,F2); TR += time()[0] - T0;
1185: T0 = time()[0]; F = srem(R % MOD,D) % MOD; TD += time()[0] - T0;
1186: if ( !F ) {
1187: if ( dp_gr_print() )
1188: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1189: return F2;
1190: }
1191: if ( !deg(F,V) ) {
1192: if ( dp_gr_print() )
1193: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1194: return 1;
1195: }
1196: C = LCOEF(F);
1197: T0 = time()[0]; INV = inva_mod(D,MOD,C); TI += time()[0] - T0;
1198: if ( !INV )
1199: return 0;
1200: T0 = time()[0]; F = srem((INV*F) % MOD,D) % MOD; TM += time()[0] - T0;
1201: F1 = F2; F2 = F;
1202: }
1203: }
1204:
1205: def ag_mod_single5(F1,F2,D,MOD)
1206: {
1207: TD = TI = TM = TR = 0;
1208: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1209: if ( D1 < D2 ) {
1210: T = F1; F1 = F2; F2 = T;
1211: T = D1; D1 = D2; D2 = T;
1212: }
1213: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1214: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1215: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1216: while ( 1 ) {
1217: T0 = time()[0];
1218: D1 = deg(F1,V); D2 = deg(F2,V); F = F1;
1219: while ( D1 >= D2 ) {
1220: R = (F-coef(F,D1,V)*F2*V^(D1-D2))%MOD;
1221: D1 = deg(R,V); HC = coef(R,D1,V);
1222: F = (R - HC*V^D1) + (srem(HC,D)%MOD)*V^D1;
1223: }
1224: TR += time()[0] - T0;
1225: T0 = time()[0]; F = srem(R % MOD,D) % MOD; TD += time()[0] - T0;
1226: if ( !F ) {
1227: if ( dp_gr_print() )
1228: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1229: return F2;
1230: }
1231: if ( !deg(F,V) ) {
1232: if ( dp_gr_print() )
1233: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1234: return 1;
1235: }
1236: C = LCOEF(F);
1237: T0 = time()[0]; INV = inva_mod(D,MOD,C); TI += time()[0] - T0;
1238: if ( !INV )
1239: return 0;
1240: T0 = time()[0]; F = srem((INV*F) % MOD,D) % MOD; TM += time()[0] - T0;
1241: F1 = F2; F2 = F;
1242: }
1243: }
1244:
1245: def ag_mod_single6(F1,F2,D,MOD)
1246: {
1247: TD = TI = TM = TR = 0;
1248: V = var(F1); D1 = deg(F1,V); D2 = deg(F2,V);
1249: if ( D1 < D2 ) {
1250: T = F1; F1 = F2; F2 = T;
1251: T = D1; D1 = D2; D2 = T;
1252: }
1253: F1 = (inv(LCOEF(F1),MOD)*F1) % MOD;
1254: F2 = (inv(LCOEF(F2),MOD)*F2) % MOD;
1255: D = (inv(LCOEF(car(D)),MOD)*car(D)) % MOD;
1256: while ( 1 ) {
1257: T0 = time()[0];
1258: D1 = deg(F1,V); D2 = deg(F2,V); F = F1;
1259: while ( D1 >= D2 ) {
1260: R = (F-coef(F,D1,V)*F2*V^(D1-D2))%MOD;
1261: D1 = deg(R,V); HC = coef(R,D1,V);
1262: /* F = (R - HC*V^D1) + (srem_mod(HC,D,MOD))*V^D1; */
1263: F = remc_mod(R,D,MOD);
1264: }
1265: TR += time()[0] - T0;
1266: T0 = time()[0]; F = remc_mod(R%MOD,D,MOD); TD += time()[0] - T0;
1267: if ( !F ) {
1268: if ( dp_gr_print() )
1269: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1270: return F2;
1271: }
1272: if ( !deg(F,V) ) {
1273: if ( dp_gr_print() )
1274: print(["TD",TD,"TM",TM,"TI",TI,"TR",TR]);
1275: return 1;
1276: }
1277: C = LCOEF(F);
1278: T0 = time()[0]; INV = inva_mod(D,MOD,C); TI += time()[0] - T0;
1279: if ( !INV )
1280: return 0;
1281: T0 = time()[0]; F = remc_mod((INV*F)%MOD,D,MOD); TM += time()[0] - T0;
1282: F1 = F2; F2 = F;
1283: }
1284: }
1285:
1286: def inverse_by_gr_mod(C,D,MOD)
1287: {
1288: Ord = 2;
1289: dp_gr_flags(["NoSugar",1]);
1290: G = dp_gr_mod_main(cons(x*C-1,D),cons(x,vars(D)),0,MOD,Ord);
1291: dp_gr_flags(["NoSugar",0]);
1292: if ( length(G) == 1 )
1293: return 1;
1294: else if ( length(G) == length(D)+1 ) {
1295: for ( T = G; T != []; T = cdr(T) )
1296: if ( member(x,vars(car(G))) )
1297: break;
1298: T = car(G);
1299: if ( type(coef(T,1,x)) != NUM )
1300: return 0;
1301: else
1302: return coef(T,0,x);
1303: } else
1304: return 0;
1305: }
1306:
1307: def simp_by_dp(F,D)
1308: {
1309: for ( T = D; T != []; T = cdr(T) )
1310: F = srem(F,car(T));
1311: return F;
1312: }
1313:
1314: def simp_by_dp_mod(F,D,MOD)
1315: {
1316: F %= MOD;
1317: for ( T = D; T != []; T = cdr(T) )
1318: F = srem(F,car(T)) % MOD;
1319: return F;
1320: }
1321:
1322: def remc_mod(P,D,M)
1323: {
1324: V = var(P);
1325: if ( !V || V == var(D) )
1326: return srem_mod(P,D,M);
1327: for ( I = deg(P,V), S = 0; I >= 0; I-- )
1328: if ( C = coef(P,I,V) )
1329: S += srem_mod(C,D,M)*V^I;
1330: return S;
1331: }
1332:
1333: def rem_mod(C,D,M)
1334: {
1335: V = var(D);
1336: D2 = deg(D,V);
1337: while ( (D1 = deg(C,V)) >= D2 ) {
1338: C -= (D*V^(D1-D2)*coef(C,D1,V))%M;
1339: C %= M;
1340: }
1341: return C;
1342: }
1343:
1344: def resfctr(F,L,V,N)
1345: {
1346: N = ptozp(N);
1347: V0 = var(N);
1348: DN = diff(N,V0);
1.10 noro 1349: LC = coef(N,deg(N,V0),V0);
1350: LCD = coef(DN,deg(DN,V0),V0);
1.1 noro 1351: for ( I = 0, J = 2, Len = deg(N,V0)+1; I < 5; J++ ) {
1352: M = prime(J);
1.10 noro 1353: if ( !(LC%M) || !(LCD%M))
1354: continue;
1.1 noro 1355: G = gcd(N,DN,M);
1356: if ( !deg(G,V0) ) {
1357: I++;
1358: T = nfctr_mod(N,M);
1359: if ( T < Len ) {
1360: Len = T; M0 = M;
1361: }
1362: }
1363: }
1364: S = spm(L,V,M0);
1365: T = resfctr_mod(F,S,M0);
1366: return [T,S,M0];
1367: }
1368:
1369: def resfctr_mod(F,L,M)
1370: {
1371: for ( T = L, R = []; T != []; T = cdr(T) ) {
1372: U = car(T); MP = U[0]; W = U[1];
1373: for ( A = W, B = F; A != []; A = cdr(cdr(A)) )
1374: B = sremm(subst(B,A[0],A[1]),MP,M);
1375: C = res(var(MP),B,MP) % M;
1376: R = cons(flatten(cdr(modfctr(C,M))),R);
1377: }
1378: return R;
1379: }
1380:
1381: def flatten(L)
1382: {
1383: for ( T = L, R = []; T != []; T = cdr(T) )
1384: R = cons(car(car(T)),R);
1385: return R;
1386: }
1387:
1388: def spm(L,V,M)
1389: {
1390: if ( length(V) == 1 ) {
1391: U = modfctr(car(L),M);
1392: for ( T = cdr(U), R = []; T != []; T = cdr(T) ) {
1393: S = car(T);
1394: R = cons([subst(S[0],var(S[0]),a_),[var(S[0]),a_]],R);
1395: }
1396: return R;
1397: }
1398: L1 = spm(cdr(L),cdr(V),M);
1399: F0 = car(L); V0 = car(V); VR = cdr(V);
1400: for ( T = L1, R = []; T != []; T = cdr(T) ) {
1401: S = car(T);
1402: F1 = subst(F0,S[1]);
1403: U = fctr_mod(F1,V0,S[0],M);
1404: VS = var(S[0]);
1405: for ( W = U; W != []; W = cdr(W) ) {
1406: A = car(car(W));
1407: if ( deg(A,V0) == 1 ) {
1408: A = monic_mod(A,V0,S[0],M);
1409: R = cons([S[0],append([V0,-coef(A,0,V0)],S[1])],R);
1410: } else {
1411: B = pe_mod(A,S[0],M);
1412: MP = B[0]; VMP = var(MP); NV = B[1];
1413: for ( C = S[1], D = []; C != []; C = cdr(cdr(C)) ) {
1414: G = subst(sremm(subst(C[1],VS,NV[1]),MP,M),VMP,VS);
1415: D = append([C[0],G],D);
1416: }
1417: R = cons([subst(MP,VMP,VS),
1418: append([B[2][0],subst(B[2][1],VMP,VS)],D)],R);
1419: }
1420: }
1421: }
1422: return R;
1423: }
1424:
1425: def pe_mod(F,G,M)
1426: {
1427: V = var(G); W = car(setminus(vars(F),[V]));
1428: NG = deg(G,V); NF = deg(F,W); N = NG*NF;
1429: X = prim;
1430: while ( 1 ) {
1431: D = mrandompoly(N,X,M);
1432: if ( irred_check(D,M) )
1433: break;
1434: }
1435: L = fctr_mod(G,V,D,M);
1436: for ( T = L; T != []; T = cdr(T) ) {
1437: U = car(car(T));
1438: if ( deg(U,V) == 1 )
1439: break;
1440: }
1441: U = monic_mod(U,V,D,M); RV = -coef(U,0,V);
1442: L = fctr_mod(sremm(subst(F,V,RV),D,M),W,D,M);
1443: for ( T = L; T != []; T = cdr(T) ) {
1444: U = car(car(T));
1445: if ( deg(U,W) == 1 )
1446: break;
1447: }
1448: U = monic_mod(U,W,D,M); RW = -coef(U,0,W);
1449: return [D,[V,RV],[W,RW]];
1450: }
1451:
1452: def fctr_mod(F,V,D,M)
1453: {
1454: if ( V != x ) {
1455: F = subst(F,V,x); V0 = V; V = x;
1456: } else
1457: V0 = x;
1458: F = monic_mod(F,V,D,M);
1459: L = sqfr_mod(F,V,D,M);
1460: for ( R = [], T = L; T != []; T = cdr(T) ) {
1461: S = car(T); A = S[0]; E = S[1];
1462: B = ddd_mod(A,V,D,M);
1463: R = append(append_mult(B,E),R);
1464: }
1465: if ( V0 != x ) {
1466: for ( R = reverse(R), T = []; R != []; R = cdr(R) )
1467: T = cons([subst(car(R)[0],x,V0),car(R)[1]],T);
1468: R = T;
1469: }
1470: return R;
1471: }
1472:
1473: def append_mult(L,E)
1474: {
1475: for ( T = L, R = []; T != []; T = cdr(T) )
1476: R = cons([car(T),E],R);
1477: return R;
1478: }
1479:
1480: def sqfr_mod(F,V,D,M)
1481: {
1482: setmod(M);
1483: F = sremm(F,D,M);
1484: F1 = sremm(diff(F,V),D,M);
1485: F1 = sremm(F1*inva_mod(D,M,LCOEF(F1)),D,M);
1486: if ( F1 ) {
1487: F2 = ag_mod_single4(F,F1,[D],M);
1488: FLAT = sremm(sdivm(F,F2,M,V),D,M);
1489: I = 0; L = [];
1490: while ( deg(FLAT,V) ) {
1491: while ( 1 ) {
1492: QR = sqrm(F,FLAT,M,V);
1493: if ( !sremm(QR[1],D,M) ) {
1494: F = sremm(QR[0],D,M); I++;
1495: } else
1496: break;
1497: }
1498: if ( !deg(F,V) )
1499: FLAT1 = 1;
1500: else
1501: FLAT1 = ag_mod_single4(F,FLAT,[D],M);
1502: G = sremm(sdivm(FLAT,FLAT1,M,V),D,M);
1503: FLAT = FLAT1;
1504: L = cons([G,I],L);
1505: }
1506: }
1507: if ( deg(F,V) ) {
1508: T = sqfr_mod(pthroot_p_mod(F,V,D,M),V,D,M);
1509: for ( R = []; T != []; T = cdr(T) ) {
1510: H = car(T); R = cons([H[0],M*H[1]],R);
1511: }
1512: } else
1513: R = [];
1514: return append(L,R);
1515: }
1516:
1517: def pthroot_p_mod(F,V,D,M)
1518: {
1519: for ( T = F, R = 0; T; ) {
1520: D1 = deg(T,V); C = coef(T,D1,V); T -= C*V^D1;
1521: R += pthroot_n_mod(C,D,M)*V^idiv(D1,M);
1522: }
1523: return R;
1524: }
1525:
1526: def pthroot_n_mod(C,D,M)
1527: {
1528: pwr_n_mod(C,D,M,deg(D,var(D))-1);
1529: }
1530:
1531: def pwr_n_mod(C,D,M,N)
1532: {
1533: if ( N == 0 )
1534: return 1;
1535: else if ( N == 1 )
1536: return C;
1537: else {
1538: QR = iqr(N,2);
1539: T = pwr_n_mod(C,D,M,QR[0]);
1540: S = sremm(T^2,D,M);
1541: if ( QR[1] )
1542: return sremm(S*C,D,M);
1543: else
1544: return S;
1545: }
1546: }
1547:
1548: def pwr_p_mod(P,A,V,D,M,N)
1549: {
1550: if ( N == 0 )
1551: return 1;
1552: else if ( N == 1 )
1553: return P;
1554: else {
1555: QR = iqr(N,2);
1556: T = pwr_p_mod(P,A,V,D,M,QR[0]);
1557: S = sremm(sremm(sremm(T^2,D,M),A,M,V),D,M);
1558: if ( QR[1] )
1559: return sremm(sremm(sremm(S*P,D,M),A,M,V),D,M);
1560: else
1561: return S;
1562: }
1563: }
1564:
1565: def qmat_mod(F,V,D,M)
1566: {
1567: R = tab_mod(F,V,D,M);
1568: Q = newmat(N,N);
1569: for ( J = 0; J < N; J++ )
1570: for ( I = 0, T = R[J]; I < N; I++ ) {
1571: Q[I][J] = coef(T,I);
1572: }
1573: for ( I = 0; I < N; I++ )
1574: Q[I][I] = (Q[I][I]+(M-1))%M;
1575: return Q;
1576: }
1577:
1578: def tab_mod(F,V,D,M)
1579: {
1580: MD = M^deg(D,var(D));
1581: N = deg(F,V);
1582: F = sremm(F*inva_mod(D,M,coef(F,N,V)),D,M);
1583: R = newvect(N); R[0] = 1;
1584: R[1] = pwr_mod(V,F,V,D,M,MD);
1585: for ( I = 2; I < N; I++ )
1586: R[I] = sremm(sremm(R[1]*R[I-1],F,M),D,M);
1587: return R;
1588: }
1589:
1590: def ddd_mod(F,V,D,M)
1591: {
1592: if ( deg(F,V) == 1 )
1593: return [F];
1594: TAB = tab_mod(F,V,D,M);
1595: for ( I = 1, W = V, L = []; 2*I <= deg(F,V); I++ ) {
1596: for ( T = 0, K = 0; K <= deg(W,V); K++ )
1597: if ( C = coef(W,K,V) )
1598: T = sremm(T+TAB[K]*C,D,M);
1599: W = T;
1600: GCD = ag_mod_single4(F,monic_mod(W-V,V,D,M),[D],M);
1601: if ( deg(GCD,V) ) {
1602: L = append(berlekamp(GCD,V,I,TAB,D,M),L);
1603: F = sremm(sdivm(F,GCD,M,V),D,M);
1604: W = sremm(sremm(W,F,M,V),D,M);
1605: }
1606: }
1607: if ( deg(F,V) )
1608: return cons(F,L);
1609: else
1610: return L;
1611: }
1612:
1613: def monic_mod(F,V,D,M) {
1614: if ( !F || !deg(F,V) )
1615: return F;
1616: return sremm(F*inva_mod(D,M,coef(F,deg(F,V),V)),D,M);
1617: }
1618:
1619: def berlekamp(F,V,E,TAB,D,M)
1620: {
1621: N = deg(F,V);
1622: Q = newmat(N,N);
1623: for ( J = 0; J < N; J++ ) {
1624: T = sremm(sremm(TAB[J],F,M,V),D,M);
1625: for ( I = 0; I < N; I++ ) {
1626: Q[I][J] = coef(T,I);
1627: }
1628: }
1629: for ( I = 0; I < N; I++ )
1630: Q[I][I] = (Q[I][I]+(M-1))%M;
1631: L = nullspace(Q,D,M); MT = L[0]; IND = L[1];
1632: NF0 = N/E;
1633: PS = null_to_poly(MT,IND,V,M);
1634: R = newvect(NF0); R[0] = monic_mod(F,V,D,M);
1635: for ( I = 1, NF = 1; NF < NF0 && I < NF0; I++ ) {
1636: PSI = PS[I];
1637: MP = minipoly_mod(PSI,F,V,D,M);
1638: ROOT = find_root(MP,V,D,M); NR = length(ROOT);
1639: for ( J = 0; J < NF; J++ ) {
1640: if ( deg(R[J],V) == E )
1641: continue;
1642: for ( K = 0; K < NR; K++ ) {
1643: GCD = ag_mod_single4(R[J],PSI-ROOT[K],[D],M);
1644: if ( deg(GCD,V) > 0 && deg(GCD,V) < deg(R[J],V) ) {
1645: Q = sremm(sdivm(R[J],GCD,M,V),D,M);
1646: R[J] = Q; R[NF++] = GCD;
1647: }
1648: }
1649: }
1650: }
1651: return vtol(R);
1652: }
1653:
1654: def null_to_poly(MT,IND,V,M)
1655: {
1656: N = size(MT)[0];
1657: for ( I = 0, J = 0; I < N; I++ )
1658: if ( IND[I] )
1659: J++;
1660: R = newvect(J);
1661: for ( I = 0, L = 0; I < N; I++ ) {
1662: if ( !IND[I] )
1663: continue;
1664: for ( J = K = 0, T = 0; J < N; J++ )
1665: if ( !IND[J] )
1666: T += MT[K++][I]*V^J;
1667: else if ( J == I )
1668: T += (M-1)*V^I;
1669: R[L++] = T;
1670: }
1671: return R;
1672: }
1673:
1674: def minipoly_mod(P,F,V,D,M)
1675: {
1676: L = [[1,1]]; P0 = P1 = 1;
1677: while ( 1 ) {
1678: P0 *= V;
1679: P1 = sremm(sremm(P*P1,F,M,V),D,M);
1680: L1 = lnf_mod(P0,P1,L,V,D,M); NP0 = L1[0]; NP1 = L1[1];
1681: if ( !NP1 )
1682: return NP0;
1683: else
1684: L = lnf_insert([NP0,NP1],L,V);
1685: }
1686: }
1687:
1688: def lnf_mod(P0,P1,L,V,D,M)
1689: {
1690: NP0 = P0; NP1 = P1;
1691: for ( T = L; T != []; T = cdr(T) ) {
1692: Q = car(T);
1693: D1 = deg(NP1,V);
1694: if ( D1 == deg(Q[1],V) ) {
1695: C = coef(Q[1],D1,V);
1696: INV = inva_mod(D,M,M-C); H = sremm(coef(NP1,D1,V)*INV,D,M);
1697: NP0 = sremm(NP0+Q[0]*H,D,M);
1698: NP1 = sremm(NP1+Q[1]*H,D,M);
1699: }
1700: }
1701: return [NP0,NP1];
1702: }
1703:
1704: def lnf_insert(P,L,V)
1705: {
1706: if ( L == [] )
1707: return [P];
1708: else {
1709: P0 = car(L);
1710: if ( deg(P0[1],V) > deg(P[1],V) )
1711: return cons(P0,lnf_insert(P,cdr(L),V));
1712: else
1713: return cons(P,L);
1714: }
1715: }
1716:
1717: def find_root(P,V,D,M)
1718: {
1719: L = c_z(P,V,1,D,M);
1720: for ( T = L, U = []; T != []; T = cdr(T) ) {
1721: S = monic_mod(car(T),V,D,M); U = cons(-coef(S,0,V),U);
1722: }
1723: return U;
1724: }
1725:
1726: def c_z(F,V,E,D,M)
1727: {
1728: N = deg(F,V);
1729: if ( N == E )
1730: return [F];
1731: Q = M^deg(D,var(D));
1732: K = idiv(N,E);
1733: L = [F];
1734: while ( 1 ) {
1735: W = mrandomgfpoly(2*E,V,D,M);
1736: if ( M == 2 ) {
1737: W = monic_mod(tr_mod(W,F,V,D,M,N-1),V,D,M);
1738: } else {
1739: /* W = monic_mod(pwr_p_mod(W,F,V,D,M,idiv(Q^E-1,2))-1,V,D,M); */
1740: /* T = pwr_p_mod(W,F,V,D,M,idiv(Q^E-1,2)); */
1741: T = pwr_mod(W,F,V,D,M,idiv(Q^E-1,2));
1742: W = monic_mod(T-1,V,D,M);
1743: }
1744: if ( !W )
1745: continue;
1746: G = ag_mod_single4(F,W,[D],M);
1747: if ( deg(G,V) && deg(G,V) < N ) {
1748: L1 = c_z(G,V,E,D,M);
1749: L2 = c_z(sremm(sdivm(F,G,M,V),D,M),V,E,D,M);
1750: return append(L1,L2);
1751: }
1752: }
1753: }
1754:
1755: def tr_mod(P,F,V,D,M,N)
1756: {
1757: for ( I = 1, S = P, W = P; I <= N; I++ ) {
1758: W = sremm(sremm(W^2,F,M,V),D,M);
1759: S = sremm(S+W,D,M);
1760: }
1761: return S;
1762: }
1763:
1764: def mrandomgfpoly(N,V,D,M)
1765: {
1766: W = var(D); ND = deg(D,W);
1767: for ( I = N-2, S = V^(N-1); I >= 0; I-- )
1768: S += randompoly(ND,W,M)*V^I;
1769: return S;
1770: }
1771:
1772: def randompoly(N,V,M)
1773: {
1774: for ( I = 0, S = 0; I < N; I++ )
1775: S += (random()%M)*V^I;
1776: return S;
1777: }
1778:
1779: def mrandompoly(N,V,M)
1780: {
1781: for ( I = N-1, S = V^N; I >=0; I-- )
1782: S += (random()%M)*V^I;
1783: return S;
1784: }
1785:
1786: def srem_by_nf(P,B,V,O) {
1787: dp_ord(O); DP = dp_ptod(P,V);
1788: N = length(B); DB = newvect(N);
1789: for ( I = N-1, IL = []; I >= 0; I-- ) {
1790: DB[I] = dp_ptod(B[I],V);
1791: IL = cons(I,IL);
1792: }
1793: L = dp_true_nf(IL,DP,DB,1);
1794: return [dp_dtop(L[0],V),L[1]];
1795: }
1796: end$
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>