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