Annotation of OpenXM_contrib2/asir2000/engine/H.c, Revision 1.12
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.12 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/H.c,v 1.11 2015/08/14 13:51:54 fujimoto Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "inline.h"
52: #include "base.h"
53: #include <math.h>
54:
55: #if 1
56: #define Mulum mulum
57: #define Divum divum
58: #define Mulsum mulsum
59: #define Gcdum gcdum
60: #endif
61:
62: #define FCTR 0
63: #define SQFR 1
64: #define DDD 2
65: #define NEWDDD 3
1.12 ! noro 66:
1.1 noro 67: LUM LUMALLOC();
68:
1.6 noro 69: void berle(int index,int count,P f,ML *listp)
1.1 noro 70: {
1.12 ! noro 71: UM wf,wf1,wf2,wfs,gcd;
! 72: ML flist;
! 73: int fn,fn1,fm,m,n,fhd;
! 74: register int i,j,inv,hd,*ptr,*ptr1;
! 75:
! 76: n = UDEG(f);
! 77: wf = W_UMALLOC(n); wf1 = W_UMALLOC(n); wf2 = W_UMALLOC(n);
! 78: wfs = W_UMALLOC(n); gcd = W_UMALLOC(n);
! 79: for ( j = 0, fn = n + 1; (j < count) && (fn > 1); ) {
! 80: m = sprime[index++];
! 81: if ( !rem(NM((Q)UCOEF(f)),m) )
! 82: continue;
! 83: ptoum(m,f,wf); cpyum(wf,wf1);
! 84: diffum(m,wf1,wf2); gcdum(m,wf1,wf2,gcd);
! 85: if ( DEG(gcd) > 0 )
! 86: continue;
! 87: hd = COEF(wf)[n]; inv = invm(hd,m);
! 88: for ( i = n, ptr = COEF(wf); i >= 0; i-- )
! 89: ptr[i] = ( ptr[i] * inv ) % m;
! 90: fn1 = berlecnt(m,wf);
! 91: if ( fn1 < fn ) {
! 92: fn = fn1; fm = m; fhd = hd;
! 93: for ( i = n, ptr = COEF(wf), ptr1 = COEF(wfs); i >= 0; i-- )
! 94: ptr1[i] = ptr[i];
! 95: }
! 96: j++;
! 97: }
! 98: DEG(wfs) = n;
! 99: *listp = flist = MLALLOC(fn); flist->n = fn; flist->mod = fm;
! 100: /* berlemain(fm,wfs,(UM *)flist->c); */
! 101: if ( fm == 2 )
! 102: berlemain(fm,wfs,(UM *)flist->c);
! 103: else
! 104: newddd(fm,wfs,(UM *)flist->c);
! 105: for ( i = DEG((UM)(flist->c[0])),
! 106: ptr = COEF((UM)(flist->c[0])),
! 107: hd = fhd, m = fm; i >= 0; i-- )
! 108: ptr[i] = ( ptr[i] * hd ) % m;
1.1 noro 109: }
110:
1.6 noro 111: int berlecnt(int mod,UM f)
1.1 noro 112: {
1.12 ! noro 113: register int i,j,**c;
! 114: int d,dr,n;
! 115: UM w,q;
! 116: int **almat();
! 117:
! 118: n = DEG(f); c = almat(n,n);
! 119: w = W_UMALLOC(mod + n); q = W_UMALLOC(mod + n);
! 120: for ( i = 1; ( d = ( mod * i ) ) < n; i++ )
! 121: c[d][i - 1] = 1;
! 122: DEG(w) = d; COEF(w)[d] = 1;
! 123: for ( j = d - 1; j >= 0; j-- )
! 124: COEF(w)[j] = 0;
! 125: for ( ; ( i < n ) && ( ( dr = divum(mod,w,f,q) ) != -1 ); i++ ) {
! 126: for ( j = dr; j >= 0; j-- )
! 127: COEF(w)[j + mod] = c[j][i - 1] = COEF(w)[j];
! 128: for ( j = mod - 1; j >= 0; j-- )
! 129: COEF(w)[j] = 0;
! 130: DEG(w) = dr + mod;
! 131: }
! 132: for ( i = 1; i < n; i++ )
! 133: c[i][i - 1] = ( c[i][i - 1] + mod - 1 ) % mod;
! 134: return berlecntmain(mod,n,n-1,c);
1.1 noro 135: }
136:
137: /* XXX berlecntmain should not be used for large mod */
138:
1.6 noro 139: int berlecntmain(int mod,int n,int m,int **c)
1.1 noro 140: {
1.12 ! noro 141: register int *p1,*p2,i,j,k,l,a;
! 142: int *tmp,inv;
! 143: int cfs;
! 144:
! 145: for ( cfs = 1, j = k = 0; j < m; j++ ) {
! 146: for ( i = k; ( n > i ) && ( c[i][j] == 0 ); i++ );
! 147: if ( i == n ) {
! 148: cfs++; continue;
! 149: }
! 150: if ( i != k ) {
! 151: tmp = c[i]; c[i] = c[k]; c[k] = tmp;
! 152: }
! 153: p1 = c[k]; inv = invm((p1[j] + mod) % mod,mod);
! 154: for ( l = j; l < m; l++ )
! 155: p1[l] = ( p1[l] * inv ) % mod;
! 156: for ( i = k + 1; i < n; c[i][j] = 0, i++ )
! 157: if ( i != k && ( a = -c[i][j] ) )
! 158: for ( l = j + 1, p2 = c[i]; l < m; l++ )
! 159: p2[l] = (a*p1[l] + p2[l]) % mod;
! 160: k++;
! 161: }
! 162: return ( cfs );
1.1 noro 163: }
164:
1.6 noro 165: UM *berlemain(int mod,UM f,UM *fp)
1.1 noro 166: {
1.12 ! noro 167: UM wg,ws,wf,f0,gcd,q;
! 168: int n;
! 169: register int i;
! 170:
! 171: n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
! 172: if ( DEG(wg) <= 0 ) {
! 173: f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
! 174: return ( fp );
! 175: }
! 176: f0 = W_UMALLOC(n); cpyum(f,f0);
! 177: ws = W_UMALLOC(n); wf = W_UMALLOC(n);
! 178: q = W_UMALLOC(n); gcd = W_UMALLOC(n);
! 179: for ( i = 0; i < mod; i++ ) {
! 180: cpyum(f0,wf); cpyum(wg,ws);
! 181: COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
! 182: gcdum(mod,wf,ws,gcd);
! 183: if ( DEG(gcd) > 0 ) {
! 184: if ( DEG(gcd) < n ) {
! 185: divum(mod,f0,gcd,q); f0 = q; fp = berlemain(mod,gcd,fp);
! 186: }
! 187: break;
! 188: }
! 189: }
! 190: fp = berlemain(mod,f0,fp);
! 191: return ( fp );
1.1 noro 192: }
193:
1.6 noro 194: void hensel(int index,int count,P f,ML *listp)
1.1 noro 195: {
1.12 ! noro 196: register int i,j;
! 197: int q,n,bound;
! 198: int *p;
! 199: int **pp;
! 200: ML blist,clist,bqlist,cqlist,rlist;
! 201: UM *b;
! 202: LUM fl,tl;
! 203: LUM *l;
! 204:
! 205: if ( UDEG(f) == 1 ) {
! 206: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
! 207: return;
! 208: }
! 209: berle(index,count,f,&blist);
! 210: if ( blist->n == 1 ) {
! 211: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
! 212: return;
! 213: }
! 214: gcdgen(f,blist,&clist); henprep(f,blist,clist,&bqlist,&cqlist);
! 215: n = bqlist->n; q = bqlist->mod;
! 216: bqlist->bound = cqlist->bound = bound = mignotte(q,f);
! 217: if ( bound == 1 ) {
! 218: *listp = rlist = MLALLOC(n);
! 219: rlist->n = n; rlist->mod = q; rlist->bound = bound;
! 220: for ( i = 0, b = (UM *)bqlist->c, l = (LUM *)rlist->c; i < n; i++ ) {
! 221: tl = LUMALLOC(DEG(b[i]),1); l[i] = tl; p = COEF(b[i]);
! 222: for ( j = 0, pp = COEF(tl); j <= DEG(tl); j++ )
! 223: pp[j][0] = p[j];
! 224: }
! 225: } else {
! 226: W_LUMALLOC((int)UDEG(f),bound,fl);
! 227: ptolum(q,bound,f,fl); henmain(fl,bqlist,cqlist,listp);
! 228: }
1.1 noro 229: }
230:
1.6 noro 231: void hensel2(int index,int count,P f,ML *listp)
1.4 noro 232: {
1.12 ! noro 233: register int i,j;
! 234: int mod,q,n,bound,dx;
! 235: ML blist,clist,bqlist,cqlist,rlist;
! 236: UM fm,qfm,gm,qgm,hm,qhm,qam,qbm,w;
! 237: UM *b;
! 238: LUM fl,tl;
! 239: int k;
! 240:
! 241: dx = UDEG(f);
! 242: if ( dx == 1 ) {
! 243: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
! 244: return;
! 245: }
! 246: berle(index,count,f,&blist);
! 247: n = blist->n;
! 248: mod = blist->mod;
! 249:
! 250: if ( n == 1 ) {
! 251: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
! 252: return;
! 253: }
! 254:
! 255: /* find k s.t. mod^k <= 2^27 < mod^(k+1); set q = mod^k */
! 256: for ( k = 1, q = mod; q <= ((1<<27)/mod); q *= mod, k++ );
! 257:
! 258: /* mignotte bound */
! 259: bound = mignotte(q,f);
! 260:
! 261: *listp = rlist = MLALLOC(n);
! 262: rlist->n = n;
! 263: rlist->mod = q;
! 264: rlist->bound = bound;
! 265:
! 266: if ( bound == 1 ) {
! 267: gcdgen(f,blist,&clist);
! 268: henprep(f,blist,clist,&bqlist,&cqlist);
! 269:
! 270: for ( i = 0, b = (UM *)bqlist->c; i < n; i++ ) {
! 271: COEF(rlist)[i] = tl = LUMALLOC(DEG(b[i]),1);
! 272: for ( j = 0; j <= DEG(tl); j++ )
! 273: COEF(tl)[j][0] = COEF(b[i])[j];
! 274: COEF(rlist)[i] = tl;
! 275: }
! 276: } else {
! 277: /* fl = f mod q */
! 278: fl = LUMALLOC(dx,bound);
! 279: ptolum(q,bound,f,fl);
! 280: /* fm = f mod mod */
! 281: fm = W_UMALLOC(dx);
! 282: ptoum(mod,f,fm);
! 283: /* fm = f mod q */
! 284: qfm = W_UMALLOC(dx);
! 285: ptoum(q,f,qfm);
! 286:
! 287: gm = W_UMALLOC(dx); qgm = W_UMALLOC(dx);
! 288: hm = W_UMALLOC(dx); qhm = W_UMALLOC(dx);
! 289: qam = W_UMALLOC(dx); qbm = W_UMALLOC(dx);
! 290: w = W_UMALLOC(dx);
! 291: for ( i = 0; i < n-1; i++ ) {
! 292: cpyum(COEF(blist)[i],gm);
! 293: cpyum(fm,w);
! 294: divum(mod,w,gm,hm);
! 295:
! 296: /* find am,bm s.t. qam*qgm+qbm*qhm=1 mod q, qgm=gm mod mod, qhm=hm mod mod */
! 297: henprep2(mod,q,k,qfm,gm,hm,qgm,qhm,qam,qbm);
! 298:
! 299: henmain2(fl,qgm,qhm,qam,qbm,q,bound,&tl);
! 300: rlist->c[i] = (pointer)tl;
! 301: cpyum(hm,fm);
! 302: cpyum(qhm,qfm);
! 303: }
! 304: rlist->c[i] = fl;
! 305: }
1.4 noro 306: }
307:
308: /*
1.12 ! noro 309: f = g0*h0 mod m -> f = gk*hk mod m^(bound), f is replaced by hk
1.4 noro 310: */
311:
1.6 noro 312: void henmain2(LUM f,UM g0,UM h0,UM a0,UM b0,int m,int bound,LUM *gp)
1.4 noro 313: {
1.12 ! noro 314: int n,dg,dh,i,k,j,dg1,dh1;
! 315: UM wu,wr,ws,wt,q,wh1,wg1,wc,wd,we,wz;
! 316: LUM wb0,wb1,wb2,fk,gk,hk;
! 317:
! 318: n = DEG(f); dg = DEG(g0); dh = DEG(h0);
! 319:
! 320: W_LUMALLOC(n,bound,wb0);
! 321: W_LUMALLOC(n,bound,wb1);
! 322: W_LUMALLOC(n,bound,wb2);
! 323:
! 324: wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n);
! 325: wr = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
! 326: q = W_UMALLOC(2*n);
! 327: wh1 = W_UMALLOC(2*n); wg1 = W_UMALLOC(2*n);
! 328:
! 329: /* gk = g0 */
! 330: gk = LUMALLOC(n,bound);
! 331: DEG(gk) = dg;
! 332: for ( i = 0; i <= dg; i++ )
! 333: COEF(gk)[i][0] = COEF(g0)[i];
! 334:
! 335: /* hk = h0 */
! 336: W_LUMALLOC(n,bound,hk);
! 337: DEG(hk) = dh;
! 338: for ( i = 0; i <= dh; i++ )
! 339: COEF(hk)[i][0] = COEF(h0)[i];
! 340:
! 341: /* fk = gk*hk */
! 342: W_LUMALLOC(n,bound,fk);
! 343: mullum(m,bound,gk,hk,fk);
1.4 noro 344:
1.12 ! noro 345: wc = W_UMALLOC(2*n); wd = W_UMALLOC(2*n);
! 346: we = W_UMALLOC(2*n); wz = W_UMALLOC(2*n);
1.4 noro 347:
348: #if 0
1.12 ! noro 349: mulum(m,a0,g0,wc);
! 350: mulum(m,b0,h0,wd);
! 351: addum(m,wc,wd,wz);
! 352: if ( DEG(wz) != 0 || COEF(wz)[0] != 1 )
! 353: error("henmain2 : cannot happen(extgcd)");
1.4 noro 354: #endif
355:
1.5 noro 356: #if 1
1.12 ! noro 357: fprintf(stderr,"bound=%d\n",bound);
1.5 noro 358: #endif
1.12 ! noro 359: for ( k = 1; k < bound; k++ ) {
1.5 noro 360: #if 1
1.12 ! noro 361: fprintf(stderr,".");
1.5 noro 362: #endif
1.8 noro 363:
1.11 fujimoto 364: #if defined(VISUAL) || defined(__MINGW32__)
1.12 ! noro 365: check_intr();
1.8 noro 366: #endif
1.12 ! noro 367: /* at this point, f = gk*hk mod y^k */
1.4 noro 368:
369: #if 0
1.12 ! noro 370: for ( j = 0; j < k; j++ )
! 371: for ( i = 0; i <= n; i++ )
! 372: if ( COEF(f)[i][j] != COEF(f)[i][j] )
! 373: error("henmain2 : cannot happen(f=fk)");
1.4 noro 374: #endif
375:
1.12 ! noro 376: /* wt = (f-gk*hk)/y^k */
! 377: for ( i = 0; i <= n; i++ )
! 378: COEF(ws)[i] = COEF(f)[i][k];
! 379: degum(ws,n);
! 380: for ( i = 0; i <= n; i++ )
! 381: COEF(wu)[i] = COEF(fk)[i][k];
! 382: degum(wu,n);
! 383: subum(m,ws,wu,wt);
! 384:
! 385: /* compute wf1,wg1 s.t. wh1*g0+wg1*h0 = wt */
! 386: mulum(m,a0,wt,wh1); DEG(wh1) = divum(m,wh1,h0,q);
! 387: mulum(m,wh1,g0,wc); subum(m,wt,wc,wd); DEG(wd) = divum(m,wd,h0,wg1);
1.4 noro 388:
1.12 ! noro 389: /* check */
1.4 noro 390: #if 0
1.12 ! noro 391: if ( DEG(wd) >= 0 || DEG(wg1) > dg )
! 392: error("henmain2 : cannot happen(adj)");
1.4 noro 393:
1.12 ! noro 394: mulum(m,wg1,h0,wc); mulum(m,wh1,g0,wd); addum(m,wc,wd,we);
! 395: subum(m,we,wt,wz);
! 396: if ( DEG(wz) >= 0 )
! 397: error("henmain2 : cannot happen(coef)");
1.4 noro 398: #endif
399:
1.12 ! noro 400: /* fk += ((wg1*hk+wh1*gk)*y^k+wg1*wh1*y^(2*k) mod m^bound */
1.4 noro 401:
1.12 ! noro 402: /* wb0 = wh1*y^k */
! 403: clearlum(n,bound,wb0);
! 404: DEG(wb0) = dh1 = DEG(wh1);
! 405: for ( i = 0; i <= dh1; i++ )
! 406: COEF(wb0)[i][k] = COEF(wh1)[i];
! 407:
! 408: /* wb2 = gk*wb0 mod y^bound */
! 409: clearlum(n,bound,wb2);
! 410: mullum(m,bound,gk,wb0,wb2);
! 411:
! 412: /* fk += wb2 */
! 413: addtolum(m,bound,wb2,fk);
! 414:
! 415: /* wb1 = wg1*y^k */
! 416: clearlum(n,bound,wb1);
! 417: DEG(wb1) = dg1 = DEG(wg1);
! 418: for ( i = 0; i <= n; i++ )
! 419: COEF(wb1)[i][k] = COEF(wg1)[i];
! 420:
! 421: /* wb2 = hk*wb1 mod y^bound */
! 422: clearlum(n,bound,wb2);
! 423: mullum(m,bound,hk,wb1,wb2);
! 424:
! 425: /* fk += wb2 */
! 426: addtolum(m,bound,wb2,fk);
! 427:
! 428: /* fk += wg1*wh1*y^(2*k) mod y^bound) */
! 429: if ( 2*k < bound ) {
! 430: clearlum(n,bound,wb2);
! 431: mullum(m,bound,wb0,wb1,wb2);
! 432: addtolum(m,bound,wb2,fk);
! 433: }
! 434:
! 435: /* gk += wg1*y^k, hk += wh1*y^k */
! 436: for ( i = 0; i <= DEG(wg1); i++ )
! 437: COEF(gk)[i][k] = COEF(wg1)[i];
! 438: for ( i = 0; i <= DEG(wh1); i++ )
! 439: COEF(hk)[i][k] = COEF(wh1)[i];
! 440: }
1.5 noro 441: #if 1
1.12 ! noro 442: fprintf(stderr,"\n");
1.5 noro 443: #endif
1.12 ! noro 444: *gp = gk;
! 445: clearlum(n,bound,f);
! 446: DEG(f) = dh;
! 447: for ( i = 0; i <= dh; i++ )
! 448: for ( j = 0; j < bound; j++ )
! 449: COEF(f)[i][j] = COEF(hk)[i][j];
1.4 noro 450: }
451:
1.6 noro 452: void clearlum(int n,int bound,LUM f)
1.4 noro 453: {
1.12 ! noro 454: int i;
1.4 noro 455:
1.12 ! noro 456: for ( i = 0; i <= n; i++ )
! 457: bzero(COEF(f)[i],bound*sizeof(int));
1.4 noro 458: }
459:
460: /* g += f */
461:
1.6 noro 462: void addtolum(int m,int bound,LUM f,LUM g)
1.4 noro 463: {
1.12 ! noro 464: int n,i;
1.4 noro 465:
1.12 ! noro 466: n = DEG(f);
! 467: for ( i = 0; i <= n; i++ )
! 468: addpadic(m,bound,COEF(f)[i],COEF(g)[i]);
1.4 noro 469: }
470:
1.6 noro 471: void hsq(int index,int count,P f,int *nindex,DCP *dcp)
1.1 noro 472: {
1.12 ! noro 473: register int i,j,k;
! 474: register int **pp,**fpp;
! 475: register int *px,*py;
! 476: int **wpp;
! 477: int n,dr,tmp,m,b,e,np,dt;
! 478: LUM fpa,wb0,wb1,lcpa,tpa,tlum;
! 479: struct oDUM *dct;
! 480: UM wt,wq0,wq,wr,wm,wm0,wa,ws,wb;
! 481: LUM *llist,*ll;
! 482: UM *dlist,*l,*c;
! 483: ML list,fp,cfp;
! 484: DCP dc;
! 485:
! 486: sqfrum(index,count,f,nindex,&dct,&fp);
! 487: np = fp->n; m = fp->mod;
! 488: if ( ( np == 1 ) && ( dct[0].n == 1 ) ) {
! 489: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0; *dcp = dc;
! 490: return;
! 491: }
! 492: for ( i = 0, dt = 0; i < np; i++ )
! 493: dt = MAX(DEG(dct[i].f),dt);
! 494: b = mig(m,dt,f); fp->bound = b;
! 495: if ( np == 1 ) {
! 496: nthrootchk(f,dct,fp,dcp);
! 497: return;
! 498: }
! 499: list = W_MLALLOC(np); list->n = np; list->mod = m; list->bound = 1;
! 500: for ( i = 0, ll = (LUM *)list->c; i < np; i++ ) {
! 501: W_LUMALLOC(DEG(dct[i].f),b,ll[i]);
! 502: for ( j = 0, px = COEF(dct[i].f), pp = COEF(ll[i]);
! 503: j <= DEG(ll[i]); j++ )
! 504: pp[j][0] = px[j];
! 505: }
! 506: dtestsql(f,list,dct,&dc);
! 507: if ( dc ) {
! 508: *dcp = dc;
! 509: return;
! 510: }
! 511: n = UDEG(f);
! 512: W_LUMALLOC(n,b,fpa); W_LUMALLOC(0,b,lcpa);
! 513: W_LUMALLOC(n,b,wb0); W_LUMALLOC(n,b,wb1);
! 514: W_LUMALLOC(n,b,tpa);
! 515: wt = W_UMALLOC(n); ws = W_UMALLOC(n);
! 516: wr = W_UMALLOC(n);
! 517: wq = W_UMALLOC(2*n); wq0 = W_UMALLOC(n);
! 518: wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
! 519: wa = W_UMALLOC(2*n);
! 520: ptolum(m,b,f,fpa); DEG(lcpa) = 0;
! 521: for ( i = 0, pp = COEF(lcpa), fpp = COEF(fpa); i < b; i++ )
! 522: pp[0][i] = fpp[n][i];
! 523: gcdgen(f,fp,&cfp);
! 524: llist = (LUM *) ALLOCA(np*sizeof(LUM));
! 525: dlist = (UM *) ALLOCA(np*sizeof(UM));
! 526: l = (UM *)fp->c; c = (UM *)cfp->c;
! 527: for ( i = 0; i < np; i++ ) {
! 528: W_LUMALLOC(DEG(l[i]),b,llist[i]);
! 529: for ( j = DEG(l[i]), pp = COEF(llist[i]), px = COEF(l[i]); j >= 0; j-- )
! 530: pp[j][0] = px[j];
! 531: if ( ( e = dct[i].n ) != 1 ) {
! 532: wb = dct[i].f;
! 533: dlist[i] = W_UMALLOC(DEG(wb)*e); cpyum(l[i],dlist[i]);
! 534: divum(m,dlist[i],wb,wq); DEG(dlist[i])= DEG(wq);
! 535: for ( k = 0; k <= DEG(wq); k++ )
! 536: COEF(dlist[i])[k] = dmb(m,COEF(wq)[k],e,&tmp);
! 537: }
! 538: }
! 539: for ( i = 1; i < b; i++ ) {
! 540: mullum(m,i+1,lcpa,llist[0],wb0);
! 541: for ( j = 1; j < np; j++ ) {
! 542: mullum(m,i+1,llist[j],wb0,wb1);
! 543: tlum = wb0; wb0 = wb1; wb1 = tlum;
! 544: }
! 545: for ( j = n, px = COEF(wt), pp = COEF(fpa), wpp = COEF(wb0);
! 546: j >= 0; j-- )
! 547: px[j] = ( pp[j][i] - wpp[j][i] + m ) % m;
! 548: degum(wt,n);
! 549: for ( j = n, px = COEF(wq0); j >= 0; j-- )
! 550: px[j] = 0;
! 551: for ( j = 1; j < np; j++ ) {
! 552: mulum(m,wt,c[j],wm); dr = divum(m,wm,l[j],wq);
! 553: for ( k = DEG(wq), px = COEF(wq0), py = COEF(wq); k >= 0; k-- )
! 554: px[k] = ( px[k] + py[k] ) % m;
! 555: for ( k = dr, pp = COEF(llist[j]), px = COEF(wm); k >= 0; k-- )
! 556: pp[k][i] = px[k];
! 557: }
! 558: degum(wq0,n); mulum(m,wq0,l[0],wm);
! 559: mulum(m,wt,c[0],wm0); addum(m,wm,wm0,wa);
! 560: for ( j = DEG(wa), pp = COEF(llist[0]), px = COEF(wa); j >= 0; j-- )
! 561: pp[j][i] = px[j];
! 562: for ( j = n, px = COEF(wq0); j >= 0; j-- )
! 563: px[j] = 0;
! 564: for ( j = 0; j < np; j++ )
! 565: if ( dct[j].n == 1 )
! 566: for ( k = 0,
! 567: pp = COEF(llist[j]),
! 568: wpp = COEF(((LUM *)list->c)[j]);
! 569: k <= DEG(llist[j]); k++ )
! 570: wpp[k][i] = pp[k][i];
! 571: else {
! 572: pwrlum(m,i+1,((LUM *)list->c)[j],dct[j].n,tpa);
! 573: for ( k = 0,
! 574: pp = COEF(llist[j]),
! 575: wpp = COEF(tpa);
! 576: k <= DEG(l[j]); k++ )
! 577: COEF(wt)[k] = (pp[k][i]-wpp[k][i]+m)%m;
! 578: degum(wt,DEG(l[j])); dr = divum(m,wt,dlist[j],ws);
! 579: if ( dr >= 0 ) {
! 580: *dcp = 0;
! 581: return;
! 582: } else
! 583: for ( k = 0,
! 584: pp = COEF(((LUM *)list->c)[j]);
! 585: k <= DEG(ws); k++ )
! 586: pp[k][i] = COEF(ws)[k];
! 587: }
! 588: list->bound = i+1; dtestsql(f,list,dct,&dc);
! 589: if ( dc ) {
! 590: *dcp = dc;
! 591: return;
! 592: }
! 593: }
! 594: *dcp = 0;
1.1 noro 595: }
596:
1.6 noro 597: void gcdgen(P f,ML blist,ML *clistp)
1.1 noro 598: {
1.12 ! noro 599: register int i;
! 600: int n,d,mod,np;
! 601: UM wf,wm,wx,wy,wu,wv,wa,wb,wg,q,tum;
! 602: UM *in,*out;
! 603: ML clist;
! 604:
! 605: n = UDEG(f); mod = blist->mod; np = blist->n;
! 606: d = 2*n;
! 607: q = W_UMALLOC(d); wf = W_UMALLOC(d);
! 608: wm = W_UMALLOC(d); wx = W_UMALLOC(d);
! 609: wy = W_UMALLOC(d); wu = W_UMALLOC(d);
! 610: wv = W_UMALLOC(d); wg = W_UMALLOC(d);
! 611: wa = W_UMALLOC(d); wb = W_UMALLOC(d);
! 612: ptoum(mod,f,wf); DEG(wg) = 0; COEF(wg)[0] = 1;
! 613: *clistp = clist = MLALLOC(np); clist->mod = mod; clist->n = np;
! 614: for ( i = 0, in = (UM *)blist->c, out = (UM *)clist->c; i < np; i++ ) {
! 615: divum(mod,wf,in[i],q); tum = wf; wf = q; q = tum;
! 616: cpyum(wf,wx); cpyum(in[i],wy);
! 617: eucum(mod,wx,wy,wa,wb); mulum(mod,wa,wg,wm);
! 618: DEG(wm) = divum(mod,wm,in[i],q); out[i] = UMALLOC(DEG(wm));
! 619: cpyum(wm,out[i]); mulum(mod,q,wf,wu);
! 620: mulum(mod,wg,wb,wv); addum(mod,wu,wv,wg);
! 621: }
1.1 noro 622: }
623:
1.4 noro 624: /* find a,b s.t. qa*qg+qb*qh=1 mod q, qg=g mod mod, qh=h mod mod */
625: /* q = mod^k */
626:
1.6 noro 627: void henprep2(int mod,int q,int k,UM f,UM g,UM h,UM qg,UM qh,UM qa,UM qb)
1.4 noro 628: {
1.12 ! noro 629: int n;
! 630: UM wg,wh,wa,wb;
! 631: ML bl,cl,bql,cql;
! 632: P ff;
! 633:
! 634: n = DEG(f);
! 635: wg = W_UMALLOC(2*n); wh = W_UMALLOC(2*n);
! 636: wa = W_UMALLOC(2*n); wb = W_UMALLOC(2*n);
! 637: cpyum(g,wg); cpyum(h,wh);
1.4 noro 638:
1.12 ! noro 639: /* wa*g+wb*h = 1 mod mod */
! 640: eucum(mod,wg,wh,wa,wb);
1.4 noro 641:
642: #if 0
1.12 ! noro 643: /* check */
! 644: wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
! 645: mulum(mod,wa,g,wt);
! 646: mulum(mod,wb,h,ws);
! 647: addum(mod,wt,ws,wu);
! 648: if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
! 649: error("henprep 1");
1.4 noro 650: #endif
651:
1.12 ! noro 652: bl = MLALLOC(2); bl->n = 2; bl->mod = mod; bl->c[0] = g; bl->c[1] = h;
! 653: cl = MLALLOC(2); cl->n = 2; cl->mod = mod; cl->c[0] = wb; cl->c[1] = wa;
! 654: umtop(CO->v,f,&ff); /* XXX */
! 655: henprep(ff,bl,cl,&bql,&cql); /* XXX */
1.4 noro 656:
1.12 ! noro 657: cpyum(bql->c[0],qg); cpyum(bql->c[1],qh);
! 658: cpyum(cql->c[0],qb); cpyum(cql->c[1],qa);
1.4 noro 659:
660: #if 0
1.12 ! noro 661: /* check */
! 662: mulum(q,qa,qg,wt);
! 663: mulum(q,qb,qh,ws);
! 664: addum(q,wt,ws,wu);
! 665: if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
! 666: error("henprep 2");
1.4 noro 667: #endif
668: }
669:
1.1 noro 670: /*
1.12 ! noro 671: henprep(f,blist,clist,&bqlist,&cqlist);
1.1 noro 672: */
673:
1.6 noro 674: void henprep(P f,ML blist,ML clist,ML *bqlistp,ML *cqlistp)
1.1 noro 675: {
1.12 ! noro 676: register int i,j,k,*px,*py,*pz;
! 677: int n,pmax,dr,tmp,p,p1,mod,np,b,q;
! 678: UM w,wm,wn,wa,wt,wq,wf,quot,tum,*in,*inc,*out,*outc;
! 679: ML bqlist,cqlist;
! 680:
! 681: n = UDEG(f); p = mod = blist->mod; np = blist->n;
! 682: /* for ( b = 1, q = mod; q <= (unsigned int)(LBASE / (L)mod); q *= mod, b++ ); */
! 683: for ( b = 1, q = mod; q <= ((1<<27) / mod); q *= mod, b++ );
! 684: w = W_UMALLOC(n); ptoum(q,f,w);
! 685: wm = W_UMALLOC(2*n); wn = W_UMALLOC(2*n);
! 686: wa = W_UMALLOC(2*n); wt = W_UMALLOC(2*n);
! 687: wq = W_UMALLOC(2*n); wf = W_UMALLOC(2*n);
! 688: quot = W_UMALLOC(2*n);
! 689: *bqlistp = bqlist = MLALLOC(np); *cqlistp = cqlist = MLALLOC(np);
! 690: for ( i = 0; i < n+2; i++ )
! 691: COEF(wq)[i] = 0;
! 692: for ( i = 0,
! 693: in = (UM *)blist->c, inc = (UM *)clist->c,
! 694: out = (UM *)bqlist->c, outc = (UM *)cqlist->c;
! 695: i < np; i++ ) {
! 696: out[i] = C_UMALLOC(n+1); cpyum(in[i],out[i]);
! 697: outc[i] = C_UMALLOC(n+1); cpyum(inc[i],outc[i]);
! 698: }
! 699: for ( pmax = 1, i = b; i > 0; i-- )
! 700: pmax *= mod;
! 701: for ( i = 1; i < b; i++, p = p1 ) {
! 702: cpyum(out[0],wm);
! 703: for ( j = 1; j < np; j++ ) {
! 704: mulum(pmax,wm,out[j],wn);
! 705: tum = wm; wm = wn; wn = tum;
! 706: }
! 707: for ( j = n, px = COEF(w), py = COEF(wm), pz = COEF(wt); j >= 0; j-- ) {
! 708: tmp = ( ( px[j] - py[j] ) / p ) % mod;
! 709: pz[j] = ( tmp >= 0? tmp : tmp + mod );
! 710: }
! 711: degum(wt,n);
! 712: for ( j = 1; j < np; j++ ) {
! 713: mulum(mod,wt,inc[j],wm); dr = divum(mod,wm,in[j],quot);
! 714: for ( k = DEG(quot); k >= 0; k-- )
! 715: COEF(wq)[k] = ( COEF(wq)[k] + COEF(quot)[k] ) % mod;
! 716: for ( k = dr, px = COEF(out[j]), py = COEF(wm); k >= 0; k-- )
! 717: px[k] += p * py[k];
! 718: }
! 719: degum(wq,n); mulum(mod,wq,in[0],wm);
! 720: mulum(mod,wt,inc[0],wn); addum(mod,wm,wn,wa);
! 721: for ( j = DEG(wa), px = COEF(out[0]), py = COEF(wa); j >= 0; j-- )
! 722: px[j] += p * py[j];
! 723: for ( j = n, px = COEF(wq); j >= 0; j-- )
! 724: px[j] = 0;
! 725: p1 = p * mod;
! 726: for ( j = n, px = COEF(wt); j >= 1; j-- )
! 727: px[j] = 0;
! 728: px[0] = 1;
! 729: for ( j = 0; j < np; j++ ) {
! 730: cpyum(w,wf);
! 731: for ( k = DEG(wf), px = COEF(wf); k >= 0; k-- )
! 732: px[k] %= p1;
! 733: divum(p1,wf,out[j],quot); mulum(p1,outc[j],quot,wm);
! 734: for ( k = DEG(wm), px = COEF(wt), py = COEF(wm); k >= 0; k-- )
! 735: px[k] = ( px[k] - py[k] ) % p1;
! 736: }
! 737: degum(wt,n);
! 738: for ( j = DEG(wt), px = COEF(wt); j >= 0; j-- )
! 739: px[j] = ((tmp=(px[j]/p)%mod)>= 0?tmp:tmp + mod);
! 740: for ( j = 0; j < np; j++ ) {
! 741: mulum(mod,wt,outc[j],wm); dr = divum(mod,wm,in[j],quot);
! 742: for ( k = dr, px = COEF(outc[j]), py = COEF(wm); k >= 0; k-- )
! 743: px[k] += p * py[k];
! 744: degum(outc[j],MAX(DEG(outc[j]),dr));
! 745: }
! 746: }
! 747: bqlist->n = cqlist->n = np;
! 748: bqlist->mod = cqlist->mod = q;
1.1 noro 749: }
750:
751: /*
1.12 ! noro 752: henmain(fl,bqlist,cqlist,listp)
1.1 noro 753: */
754:
1.6 noro 755: void henmain(LUM f,ML bqlist,ML cqlist,ML *listp)
1.1 noro 756: {
1.12 ! noro 757: register int i,j,k;
! 758: int *px,*py;
! 759: int **pp,**pp1;
! 760: int n,np,mod,bound,dr,tmp;
! 761: UM wt,wq0,wq,wr,wm,wm0,wa,q;
! 762: LUM wb0,wb1,tlum;
! 763: UM *b,*c;
! 764: LUM *l;
! 765: ML list;
! 766:
! 767: n = DEG(f); np = bqlist->n; mod = bqlist->mod; bound = bqlist->bound;
! 768: *listp = list = MLALLOC(n);
! 769: list->n = np; list->mod = mod; list->bound = bound;
! 770: W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
! 771: wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
! 772: wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
! 773: wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
! 774: b = (UM *)bqlist->c; c = (UM *)cqlist->c; l = (LUM *)list->c;
! 775: for ( i = 0; i < np; i++ ) {
! 776: l[i] = LUMALLOC(DEG(b[i]),bound);
! 777: for ( j = DEG(b[i]), pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
! 778: pp[j][0] = px[j];
! 779: }
1.5 noro 780: #if 0
1.12 ! noro 781: fprintf(stderr,"bound=%d\n",bound);
1.5 noro 782: #endif
1.12 ! noro 783: for ( i = 1; i < bound; i++ ) {
1.5 noro 784: #if 0
1.12 ! noro 785: fprintf(stderr,".");
1.5 noro 786: #endif
1.11 fujimoto 787: #if defined(VISUAL) || defined(__MINGW32__)
1.12 ! noro 788: check_intr();
1.8 noro 789: #endif
1.12 ! noro 790: mullum(mod,i+1,l[0],l[1],wb0);
! 791: for ( j = 2; j < np; j++ ) {
! 792: mullum(mod,i+1,l[j],wb0,wb1);
! 793: tlum = wb0; wb0 = wb1; wb1 = tlum;
! 794: }
! 795: for ( j = n, px = COEF(wt); j >= 0; j-- )
! 796: px[j] = 0;
! 797: for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
! 798: tmp = ( pp[j][i] - pp1[j][i] ) % mod;
! 799: COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
! 800: }
! 801: degum(wt,n);
! 802: for ( j = n, px = COEF(wq0); j >= 0; j-- )
! 803: px[j] = 0;
! 804: for ( j = 1; j < np; j++ ) {
! 805: mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
! 806: for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
! 807: px[k] = ( px[k] + py[k] ) % mod;
! 808: for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
! 809: pp[k][i] = px[k];
! 810: }
! 811: degum(wq0,n); mulum(mod,wq0,b[0],wm);
! 812: mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
! 813: for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
! 814: pp[j][i] = px[j];
! 815: for ( j = n, px = COEF(wq0); j >= 0; j-- )
! 816: px[j] = 0;
! 817: }
1.5 noro 818: #if 0
1.12 ! noro 819: fprintf(stderr,"\n");
1.5 noro 820: #endif
1.1 noro 821: }
822:
1.7 noro 823: /*
1.12 ! noro 824: henmain_incremental(fl,bqlist,cqlist,start)
1.7 noro 825: fl = bqlist[0]*... mod q^start
826: */
827:
828: void henmain_incremental(LUM f,LUM *bqlist,ML cqlist,
1.12 ! noro 829: int np, int mod, int start, int bound)
1.7 noro 830: {
1.12 ! noro 831: register int i,j,k;
! 832: int *px,*py;
! 833: int **pp,**pp1;
! 834: int n,dr,tmp;
! 835: UM wt,wq0,wq,wr,wm,wm0,wa,q;
! 836: LUM wb0,wb1,tlum;
! 837: UM *b,*c;
! 838: LUM *l;
! 839: ML list;
! 840:
! 841: n = DEG(f);
! 842: W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
! 843: wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
! 844: wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
! 845: wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
! 846: c = (UM *)cqlist->c; l = bqlist;
! 847: b = (UM *)ALLOCA(n*sizeof(UM));
! 848: for ( i = 0; i < np; i++ ) {
! 849: j = DEG(l[i]);
! 850: b[i] = W_UMALLOC(j);
! 851: DEG(b[i]) = j;
! 852: for ( pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
! 853: px[j] = pp[j][0];
! 854: }
1.7 noro 855: #if 0
1.12 ! noro 856: fprintf(stderr,"bound=%d\n",bound);
1.7 noro 857: #endif
1.12 ! noro 858: for ( i = start; i < bound; i++ ) {
1.7 noro 859: #if 0
1.12 ! noro 860: fprintf(stderr,".");
1.7 noro 861: #endif
1.12 ! noro 862: mullum(mod,i+1,l[0],l[1],wb0);
! 863: for ( j = 2; j < np; j++ ) {
! 864: mullum(mod,i+1,l[j],wb0,wb1);
! 865: tlum = wb0; wb0 = wb1; wb1 = tlum;
! 866: }
! 867: for ( j = n, px = COEF(wt); j >= 0; j-- )
! 868: px[j] = 0;
! 869: for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
! 870: tmp = ( pp[j][i] - pp1[j][i] ) % mod;
! 871: COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
! 872: }
! 873: degum(wt,n);
! 874: for ( j = n, px = COEF(wq0); j >= 0; j-- )
! 875: px[j] = 0;
! 876: for ( j = 1; j < np; j++ ) {
! 877: mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
! 878: for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
! 879: px[k] = ( px[k] + py[k] ) % mod;
! 880: for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
! 881: pp[k][i] = px[k];
! 882: }
! 883: degum(wq0,n); mulum(mod,wq0,b[0],wm);
! 884: mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
! 885: for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
! 886: pp[j][i] = px[j];
! 887: for ( j = n, px = COEF(wq0); j >= 0; j-- )
! 888: px[j] = 0;
! 889: }
1.7 noro 890: #if 0
1.12 ! noro 891: fprintf(stderr,"\n");
1.7 noro 892: #endif
893: }
894:
1.1 noro 895: static double M;
896: static int E;
897:
1.6 noro 898: int mignotte(int q,P f)
1.1 noro 899: {
1.12 ! noro 900: int p;
! 901: unsigned int *b;
! 902: N c;
! 903: DCP dc;
! 904:
! 905: for ( dc = DC(f), M = 0, E = 0; dc; dc = NEXT(dc) ) {
! 906: c = NM((Q)COEF(dc)); p = PL(c); b = BD(c);
! 907: sqad(b[p-1],(p-1)*BSH);
! 908: }
! 909: if (E % 2) M *= 2; M = ceil(sqrt(M)); E /= 2;
! 910: c = NM((Q)COEF(DC(f))); p = PL(c); M *= ((double)BD(c)[p-1]+1.0); E += (p-1) * BSH;
! 911: return (int)ceil( (0.31*(E+UDEG(f)+1)+log10((double)M)) / log10((double)q) );
1.1 noro 912: }
913:
1.6 noro 914: int mig(int q,int d,P f)
1.1 noro 915: {
1.12 ! noro 916: int p;
! 917: unsigned int *b;
! 918: N c;
! 919: DCP dc;
! 920:
! 921: for ( dc = DC(f), M = 0, E = 0; dc; dc = NEXT(dc) ) {
! 922: c = NM((Q)COEF(dc)); p = PL(c); b = BD(c);
! 923: sqad(b[p-1],(p-1)*BSH);
! 924: }
! 925: if (E % 2) M *= 2; M = ceil(sqrt(M)); E /= 2;
! 926: c = NM((Q)COEF(DC(f))); p = PL(c);
! 927: M *= (BD(c)[p-1]+1); E += (p-1) * BSH;
! 928: return (int)ceil( (0.31*(E+d+1)+log10((double)M)) / log10((double)q) );
1.1 noro 929: }
930:
1.6 noro 931: void sqad(unsigned int man,int exp)
1.1 noro 932: {
1.12 ! noro 933: int e,sqe;
! 934: unsigned int t;
! 935: double man1,d,sqm;
! 936: int diff;
! 937:
! 938: if ( man == BMASK ) {
! 939: e = BSH; man1 = 1.0;
! 940: } else {
! 941: man += 1;
! 942: for ( e = 0, t = man; t; e++, t >>= 1 );
! 943: e--; d = (double)(1<<e);
! 944: man1 = ((double)man)/d;
! 945: }
! 946: exp += e; sqm = man1 * man1; sqe = 2 * exp;
! 947: if ( sqm >= 2.0 ) {
! 948: sqm /= 2.0; sqe++;
! 949: }
! 950: diff = E - sqe;
! 951: if ( diff > 18 )
! 952: return;
! 953: if ( diff < -18 ) {
! 954: M = sqm; E = sqe;
! 955: return;
! 956: }
! 957: if ( diff >= 0 )
! 958: M += (sqm / (double)(1<<diff));
! 959: else {
! 960: M = ( ( M / (double)(1<<-diff)) + sqm ); E = sqe;
! 961: }
! 962: if ( M >= 2.0 ) {
! 963: M /= 2.0; E++;
! 964: }
1.1 noro 965: }
966:
1.6 noro 967: void ptolum(int q,int bound,P f,LUM fl)
1.1 noro 968: {
1.12 ! noro 969: DCP dc;
! 970: int i,j;
! 971: int **pp;
! 972: int d,br,s;
! 973: unsigned int r;
! 974: int *c;
! 975: unsigned int *m,*w;
! 976:
! 977: for ( dc = DC(f), pp = COEF(fl); dc; dc = NEXT(dc) ) {
! 978: d = PL(NM((Q)COEF(dc))); m = BD(NM((Q)COEF(dc)));
! 979: c = pp[QTOS(DEG(dc))]; w = (unsigned int *)W_ALLOC(d);
! 980: for ( i = 0; i < d; i++ )
! 981: w[i] = m[i];
! 982: for ( i = 0; i < bound && d >= 1; ) {
! 983: for ( j = d - 1, r = 0; j >= 0; j-- ) {
! 984: DSAB(q,r,w[j],w[j],r)
! 985: }
! 986: c[i++] = (int)r;
! 987: if ( !w[d-1] )
! 988: d--;
! 989: }
! 990: if ( SGN((Q)COEF(dc)) < 0 )
! 991: for (i = 0, br = 0; i < bound; i++ )
! 992: if ( ( s = -(c[i] + br) ) < 0 ) {
! 993: c[i] = s + q; br = 1;
! 994: } else {
! 995: c[i] = 0; br = 0;
! 996: }
! 997: }
1.1 noro 998: }
999:
1.6 noro 1000: void modfctrp(P p,int mod,int flag,DCP *dcp)
1.1 noro 1001: {
1.12 ! noro 1002: int cm,n,i,j,k;
! 1003: DCP dc,dc0;
! 1004: P zp;
! 1005: Q c,q;
! 1006: UM mp;
! 1007: UM *tl;
! 1008: struct oDUM *udc,*udc1;
! 1009:
! 1010: if ( !p ) {
! 1011: *dcp = 0; return;
! 1012: }
! 1013: ptozp(p,1,&c,&zp);
! 1014: if ( DN(c) || !(cm = rem(NM(c),mod)) ) {
! 1015: *dcp = 0; return;
! 1016: }
! 1017: mp = W_UMALLOC(UDEG(p));
! 1018: ptoum(mod,zp,mp);
! 1019: if ( (n = DEG(mp)) < 0 ) {
! 1020: *dcp = 0; return;
! 1021: } else if ( n == 0 ) {
! 1022: cm = dmar(cm,COEF(mp)[0],0,mod); STOQ(cm,q);
! 1023: NEWDC(dc); COEF(dc) = (P)q; DEG(dc) = ONE;
! 1024: NEXT(dc) = 0; *dcp = dc;
! 1025: return;
! 1026: }
! 1027: if ( COEF(mp)[n] != 1 ) {
! 1028: cm = dmar(cm,COEF(mp)[n],0,mod);
! 1029: i = invm(COEF(mp)[n],mod);
! 1030: for ( j = 0; j <= n; j++ )
! 1031: COEF(mp)[j] = dmar(COEF(mp)[j],i,0,mod);
! 1032: }
! 1033: W_CALLOC(n+1,struct oDUM,udc);
! 1034: gensqfrum(mod,mp,udc);
! 1035: switch ( flag ) {
! 1036: case FCTR:
! 1037: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
! 1038: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
! 1039: for ( i = 0,j = 0; udc[i].f; i++ )
! 1040: if ( DEG(udc[i].f) == 1 ) {
! 1041: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
! 1042: } else {
! 1043: bzero((char *)tl,(n+1)*sizeof(UM));
! 1044: berlemain(mod,udc[i].f,tl);
! 1045: for ( k = 0; tl[k]; k++, j++ ) {
! 1046: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
! 1047: }
! 1048: }
! 1049: udc = udc1; break;
! 1050: case SQFR:
! 1051: break;
! 1052: case DDD:
! 1053: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
! 1054: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
! 1055: for ( i = 0,j = 0; udc[i].f; i++ )
! 1056: if ( DEG(udc[i].f) == 1 ) {
! 1057: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
! 1058: } else {
! 1059: bzero((char *)tl,(n+1)*sizeof(UM));
! 1060: ddd(mod,udc[i].f,tl);
! 1061: for ( k = 0; tl[k]; k++, j++ ) {
! 1062: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
! 1063: }
! 1064: }
! 1065: udc = udc1; break;
! 1066: case NEWDDD:
! 1067: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
! 1068: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
! 1069: for ( i = 0,j = 0; udc[i].f; i++ )
! 1070: if ( DEG(udc[i].f) == 1 ) {
! 1071: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
! 1072: } else {
! 1073: bzero((char *)tl,(n+1)*sizeof(UM));
! 1074: if ( mod == 2 )
! 1075: berlemain(mod,udc[i].f,tl);
! 1076: else
! 1077: newddd(mod,udc[i].f,tl);
! 1078: for ( k = 0; tl[k]; k++, j++ ) {
! 1079: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
! 1080: }
! 1081: }
! 1082: udc = udc1; break;
! 1083: }
! 1084: NEWDC(dc0); STOQ(cm,q); COEF(dc0) = (P)q; DEG(dc0) = ONE; dc = dc0;
! 1085: for ( n = 0; udc[n].f; n++ ) {
! 1086: NEWDC(NEXT(dc)); dc = NEXT(dc);
! 1087: STOQ(udc[n].n,DEG(dc)); umtop(VR(p),udc[n].f,&COEF(dc));
! 1088: }
! 1089: NEXT(dc) = 0; *dcp = dc0;
1.1 noro 1090: }
1091:
1.6 noro 1092: void gensqfrum(int mod,UM p,struct oDUM *dc)
1.1 noro 1093: {
1.12 ! noro 1094: int n,i,j,d;
! 1095: UM t,s,g,f,f1,b;
1.1 noro 1096:
1.12 ! noro 1097: if ( (n = DEG(p)) == 1 ) {
! 1098: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
! 1099: return;
! 1100: }
! 1101: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
! 1102: f = W_UMALLOC(n); f1 = W_UMALLOC(n); b = W_UMALLOC(n);
! 1103: diffum(mod,p,t); cpyum(p,s); Gcdum(mod,t,s,g);
! 1104: if ( !DEG(g) ) {
! 1105: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
! 1106: return;
! 1107: }
! 1108: cpyum(p,b); cpyum(p,t); Divum(mod,t,g,f);
! 1109: for ( i = 0, d = 0; DEG(f); i++ ) {
! 1110: while ( 1 ) {
! 1111: cpyum(b,t);
! 1112: if ( Divum(mod,t,f,s) >= 0 )
! 1113: break;
! 1114: else {
! 1115: cpyum(s,b); d++;
! 1116: }
! 1117: }
! 1118: cpyum(b,t); cpyum(f,s); Gcdum(mod,t,s,f1);
! 1119: Divum(mod,f,f1,s); cpyum(f1,f);
! 1120: dc[i].f = UMALLOC(DEG(s)); cpyum(s,dc[i].f); dc[i].n = d;
! 1121: }
! 1122: if ( DEG(b) > 0 ) {
! 1123: d = 1;
! 1124: while ( 1 ) {
! 1125: cpyum(b,t);
! 1126: for ( j = DEG(t); j >= 0; j-- )
! 1127: if ( COEF(t)[j] && (j % mod) )
! 1128: break;
! 1129: if ( j >= 0 )
! 1130: break;
! 1131: else {
! 1132: DEG(s) = DEG(t)/mod;
! 1133: for ( j = 0; j <= DEG(t); j++ )
! 1134: COEF(s)[j] = COEF(t)[j*mod];
! 1135: cpyum(s,b); d *= mod;
! 1136: }
! 1137: }
! 1138: gensqfrum(mod,b,dc+i);
! 1139: for ( j = i; dc[j].f; j++ )
! 1140: dc[j].n *= d;
! 1141: }
1.1 noro 1142: }
1143:
1144: #if 0
1.6 noro 1145: void srchum(int mod,UM p1,UM p2,UM gr)
1.1 noro 1146: {
1.12 ! noro 1147: UM m,m1,m2,q,r,t,g1,g2;
! 1148: int lc,d,d1,d2,i,j,k,l,l1,l2,l3,tmp,adj;
! 1149: V v;
! 1150:
! 1151: d = MAX(DEG(p1),DEG(p2));
! 1152: g1 = W_UMALLOC(d); g2 = W_UMALLOC(d);
! 1153: bzero((char *)g1,(d+2)*sizeof(int)); bzero((char *)g2,(d+2)*sizeof(int));
! 1154: if ( d == DEG(p1) ) {
! 1155: cpyum(p1,g1); cpyum(p2,g2);
! 1156: } else {
! 1157: cpyum(p1,g2); cpyum(p2,g1);
! 1158: }
! 1159: if ( ( d1 = DEG(g1) ) > ( d2 = DEG(g2) ) ) {
! 1160: j = d1 - 1; adj = 1;
! 1161: } else
! 1162: j = d2;
! 1163: lc = 1;
! 1164: r = W_UMALLOC(d1+d2); q = W_UMALLOC(d1+d2);
! 1165: m1 = W_UMALLOC(d1+d2); t = W_UMALLOC(d1+d2);
! 1166: bzero((char *)r,(d1+d2+2)*sizeof(int)); bzero((char *)q,(d1+d2+2)*sizeof(int));
! 1167: bzero((char *)m1,(d1+d2+2)*sizeof(int)); bzero((char *)t,(d1+d2+2)*sizeof(int));
! 1168: m = W_UMALLOC(0); bzero((char *)m,2*sizeof(int));
! 1169: adj = pwrm(mod,COEF(g2)[DEG(g2)],DEG(g1));
! 1170: DEG(m) = 0; COEF(m)[0] = invm(COEF(g2)[DEG(g2)],mod);
! 1171: Mulum(mod,g2,m,r); cpyum(r,g2);
! 1172: while ( 1 ) {
! 1173: if ( ( k = DEG(g2) ) < 0 ) {
! 1174: DEG(gr) = -1;
! 1175: return;
! 1176: }
! 1177: if ( k == j ) {
! 1178: if ( k == 0 ) {
! 1179: DEG(m) = 0; COEF(m)[0] = adj;
! 1180: Mulum(mod,g2,m,gr);
! 1181: return;
! 1182: } else {
! 1183: DEG(m) = 0;
! 1184: COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
! 1185: Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,t);
! 1186: DEG(m) = 0; COEF(m)[0] = dmb(mod,lc,lc,&tmp);
! 1187: Divum(mod,r,m,q); cpyum(g2,g1); cpyum(q,g2);
! 1188: lc = COEF(g1)[DEG(g1)]; j = k - 1;
! 1189: }
! 1190: } else {
! 1191: d = j - k;
! 1192: DEG(m) = 0; COEF(m)[0] = pwrm(mod,COEF(g2)[DEG(g2)],d);
! 1193: Mulum(mod,g2,m,m1); l = pwrm(mod,lc,d);
! 1194: DEG(m) = 0; COEF(m)[0] = l; Divum(mod,m1,m,t);
! 1195: if ( k == 0 ) {
! 1196: DEG(m) = 0; COEF(m)[0] = adj;
! 1197: Mulum(mod,t,m,gr);
! 1198: return;
! 1199: } else {
! 1200: DEG(m) = 0;
! 1201: COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
! 1202: Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,q);
! 1203: l1 = dmb(mod,lc,lc,&tmp); l2 = dmb(mod,l,l1,&tmp);
! 1204: DEG(m) = 0; COEF(m)[0] = l2;
! 1205: Divum(mod,r,m,q); cpyum(t,g1); cpyum(q,g2);
! 1206: if ( d % 2 )
! 1207: for ( i = DEG(g2); i >= 0; i-- )
! 1208: COEF(g2)[i] = ( mod - COEF(g2)[i] ) % mod;
! 1209: lc = COEF(g1)[DEG(g1)]; j = k - 1;
! 1210: }
! 1211: }
! 1212: }
1.1 noro 1213: }
1214:
1.6 noro 1215: UM *resberle(int mod,UM f,UM *fp)
1.1 noro 1216: {
1.12 ! noro 1217: UM w,wg,ws,wf,f0,gcd,q,res;
! 1218: int n;
! 1219: register int i;
! 1220:
! 1221: n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
! 1222: if ( DEG(wg) <= 0 ) {
! 1223: f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
! 1224: return ( fp );
! 1225: }
! 1226: f0 = W_UMALLOC(n); cpyum(f,f0);
! 1227: ws = W_UMALLOC(n); wf = W_UMALLOC(n);
! 1228: q = W_UMALLOC(n); gcd = W_UMALLOC(n);
! 1229: res = W_UMALLOC(2*n);
! 1230: srchum(mod,f,wg,res);
! 1231: for ( i = 0; i < mod; i++ ) {
! 1232: if ( substum(mod,res,i) )
! 1233: continue;
! 1234: cpyum(f0,wf); cpyum(wg,ws);
! 1235: COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
! 1236: Gcdum(mod,wf,ws,gcd);
! 1237: if ( DEG(gcd) > 0 ) {
! 1238: if ( DEG(gcd) < n ) {
! 1239: Divum(mod,f0,gcd,q); f0 = q; fp = resberle(mod,gcd,fp);
! 1240: }
! 1241: break;
! 1242: }
! 1243: }
! 1244: fp = resberle(mod,f0,fp);
! 1245: return ( fp );
1.1 noro 1246: }
1247:
1.6 noro 1248: int substum(int mod,UM p,int a)
1.1 noro 1249: {
1.12 ! noro 1250: int i,j,s;
! 1251: int *c;
1.1 noro 1252:
1.12 ! noro 1253: if ( DEG(p) < 0 )
! 1254: return 0;
! 1255: if ( DEG(p) == 0 )
! 1256: return COEF(p)[0];
! 1257: for ( i = DEG(p), c = COEF(p), s = c[i]; i >= 0; ) {
! 1258: for ( j = i--; (i>=0) && !c[i]; i-- );
! 1259: if ( i >= 0 )
! 1260: s = (s*pwrm(mod,a,j-i)%mod+c[i])%mod;
! 1261: else
! 1262: s = s*pwrm(mod,a,j)%mod;
! 1263: }
! 1264: return s;
1.1 noro 1265: }
1266: #endif
1267:
1.6 noro 1268: void ddd(int mod,UM f,UM *r)
1.1 noro 1269: {
1.12 ! noro 1270: register int i,j;
! 1271: int d,n;
! 1272: UM q,s,t,u,v,w,g,x,m;
! 1273: UM *base;
! 1274:
! 1275: n = DEG(f);
! 1276: if ( n == 1 ) {
! 1277: r[0] = UMALLOC(1); cpyum(f,r[0]); r[1] = 0; return;
! 1278: }
! 1279: base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
! 1280: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
! 1281: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
! 1282: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
! 1283: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
! 1284: for ( i = 2; i < n; i++ ) {
! 1285: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
! 1286: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
! 1287: }
! 1288: v = W_UMALLOC(n); cpyum(f,v);
! 1289: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
! 1290: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
! 1291: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
! 1292: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
! 1293: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
! 1294: if ( COEF(w)[i] ) {
! 1295: Mulsum(mod,base[i],COEF(w)[i],s);
! 1296: addum(mod,s,t,u); cpyum(u,t);
! 1297: }
! 1298: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
! 1299: if ( DEG(g) >= 1 ) {
! 1300: canzas(mod,g,d,base,r+j); j += DEG(g)/d;
! 1301: Divum(mod,v,g,q); cpyum(q,v);
! 1302: DEG(w) = Divum(mod,w,v,q);
! 1303: for ( i = 0; i < DEG(v); i++ )
! 1304: DEG(base[i]) = Divum(mod,base[i],v,q);
! 1305: }
! 1306: }
! 1307: if ( DEG(v) ) {
! 1308: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
! 1309: }
! 1310: r[j] = 0;
1.1 noro 1311: }
1312:
1313: #if 0
1.6 noro 1314: void canzas(int mod,UM f,int d,UM *base,UM *r)
1.1 noro 1315: {
1.12 ! noro 1316: UM t,s,u,w,g,o,q;
! 1317: N n1,n2,n3,n4,n5;
! 1318: UM *b;
! 1319: int n,m,i;
! 1320:
! 1321: if ( DEG(f) == d ) {
! 1322: r[0] = UMALLOC(d); cpyum(f,r[0]);
! 1323: return;
! 1324: } else {
! 1325: n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)b,n*sizeof(UM));
! 1326: for ( i = 0, m = 0; i < n; i++ )
! 1327: m = MAX(m,DEG(base[i]));
! 1328: q = W_UMALLOC(m);
! 1329: for ( i = 0; i < n; i++ ) {
! 1330: b[i] = W_UMALLOC(DEG(base[i])); cpyum(base[i],b[i]);
! 1331: DEG(b[i]) = Divum(mod,b[i],f,q);
! 1332: }
! 1333: t = W_UMALLOC(2*d);
! 1334: s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
! 1335: w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
! 1336: o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = 1;
! 1337: STON(mod,n1); pwrn(n1,d,&n2); subn(n2,ONEN,&n3);
! 1338: STON(2,n4); divsn(n3,n4,&n5);
! 1339: while ( 1 ) {
! 1340: randum(mod,2*d,t); spwrum(mod,f,b,t,n5,s);
! 1341: subum(mod,s,o,u); cpyum(f,w); Gcdum(mod,w,u,g);
! 1342: if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
! 1343: canzas(mod,g,d,b,r);
! 1344: cpyum(f,w); Divum(mod,w,g,s);
! 1345: canzas(mod,s,d,b,r+DEG(g)/d);
! 1346: return;
! 1347: }
! 1348: }
! 1349: }
1.1 noro 1350: }
1351: #else
1.6 noro 1352: void canzas(int mod,UM f,int d,UM *base,UM *r)
1.1 noro 1353: {
1.12 ! noro 1354: UM t,s,u,w,g,o,q;
! 1355: N n1,n2,n3,n4,n5;
! 1356: UM *b;
! 1357: int n,m,i;
! 1358:
! 1359: if ( DEG(f) == d ) {
! 1360: r[0] = UMALLOC(d); cpyum(f,r[0]);
! 1361: return;
! 1362: } else {
! 1363: n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)b,n*sizeof(UM));
! 1364: for ( i = 0, m = 0; i < n; i++ )
! 1365: m = MAX(m,DEG(base[i]));
! 1366: q = W_UMALLOC(m);
! 1367: for ( i = 0; i < n; i++ ) {
! 1368: b[i] = W_UMALLOC(DEG(base[i])); cpyum(base[i],b[i]);
! 1369: DEG(b[i]) = Divum(mod,b[i],f,q);
! 1370: }
! 1371: t = W_UMALLOC(2*d);
! 1372: s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
! 1373: w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
! 1374: o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = 1;
! 1375: STON(mod,n1); pwrn(n1,d,&n2); subn(n2,ONEN,&n3);
! 1376: STON(2,n4); divsn(n3,n4,&n5);
! 1377: while ( 1 ) {
! 1378: randum(mod,2*d,t); spwrum0(mod,f,t,n5,s);
! 1379: subum(mod,s,o,u); cpyum(f,w); Gcdum(mod,w,u,g);
! 1380: if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
! 1381: canzas(mod,g,d,b,r);
! 1382: cpyum(f,w); Divum(mod,w,g,s);
! 1383: canzas(mod,s,d,b,r+DEG(g)/d);
! 1384: return;
! 1385: }
! 1386: }
! 1387: }
1.1 noro 1388: }
1389: #endif
1390:
1.6 noro 1391: void randum(int mod,int d,UM p)
1.1 noro 1392: {
1.12 ! noro 1393: unsigned int n;
! 1394: int i;
1.1 noro 1395:
1.12 ! noro 1396: n = ((unsigned int)random()) % d; DEG(p) = n; COEF(p)[n] = 1;
! 1397: for ( i = 0; i < (int)n; i++ )
! 1398: COEF(p)[i] = ((unsigned int)random()) % mod;
1.1 noro 1399: }
1400:
1.6 noro 1401: void pwrmodum(int mod,UM p,int e,UM f,UM pr)
1.1 noro 1402: {
1.12 ! noro 1403: UM wt,ws,q;
1.1 noro 1404:
1.12 ! noro 1405: if ( e == 0 ) {
! 1406: DEG(pr) = 0; COEF(pr)[0] = 1;
! 1407: } else if ( DEG(p) < 0 )
! 1408: DEG(pr) = -1;
! 1409: else if ( e == 1 ) {
! 1410: q = W_UMALLOC(DEG(p)); cpyum(p,pr);
! 1411: DEG(pr) = divum(mod,pr,f,q);
! 1412: } else if ( DEG(p) == 0 ) {
! 1413: DEG(pr) = 0; COEF(pr)[0] = pwrm(mod,COEF(p)[0],e);
! 1414: } else {
! 1415: wt = W_UMALLOC(2*DEG(f)); ws = W_UMALLOC(2*DEG(f));
! 1416: q = W_UMALLOC(2*DEG(f));
! 1417: pwrmodum(mod,p,e/2,f,wt);
! 1418: if ( !(e%2) ) {
! 1419: mulum(mod,wt,wt,pr); DEG(pr) = divum(mod,pr,f,q);
! 1420: } else {
! 1421: mulum(mod,wt,wt,ws); DEG(ws) = divum(mod,ws,f,q);
! 1422: mulum(mod,ws,p,pr); DEG(pr) = divum(mod,pr,f,q);
! 1423: }
! 1424: }
1.1 noro 1425: }
1426:
1.6 noro 1427: void spwrum(int mod,UM m,UM *base,UM f,N e,UM r)
1.1 noro 1428: {
1.12 ! noro 1429: int a,n,i;
! 1430: N e1,an;
! 1431: UM t,s,u,q,r1,r2;
! 1432:
! 1433: if ( !e ) {
! 1434: DEG(r) = 0; COEF(r)[0] = 1;
! 1435: } else if ( UNIN(e) )
! 1436: cpyum(f,r);
! 1437: else if ( (PL(e) == 1) && (BD(e)[0] < (unsigned int)mod) )
! 1438: spwrum0(mod,m,f,e,r);
! 1439: else {
! 1440: a = divin(e,mod,&e1); STON(a,an);
! 1441: n = DEG(m); t = W_UMALLOC(n); s = W_UMALLOC(n);
! 1442: u = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
! 1443: for ( DEG(t) = -1, i = 0; i <= DEG(f); i++ )
! 1444: if ( COEF(f)[i] ) {
! 1445: Mulsum(mod,base[i],COEF(f)[i],s);
! 1446: addum(mod,s,t,u); cpyum(u,t);
! 1447: }
! 1448: r1 = W_UMALLOC(n); spwrum0(mod,m,f,an,r1);
! 1449: r2 = W_UMALLOC(n); spwrum(mod,m,base,t,e1,r2);
! 1450: Mulum(mod,r1,r2,u); DEG(u) = Divum(mod,u,m,q);
! 1451: cpyum(u,r);
! 1452: }
1.1 noro 1453: }
1454:
1.6 noro 1455: void spwrum0(int mod,UM m,UM f,N e,UM r)
1.1 noro 1456: {
1.12 ! noro 1457: UM t,s,q;
! 1458: N e1;
! 1459: int a;
! 1460:
! 1461: if ( !e ) {
! 1462: DEG(r) = 0; COEF(r)[0] = 1;
! 1463: } else if ( UNIN(e) )
! 1464: cpyum(f,r);
! 1465: else {
! 1466: a = divin(e,2,&e1);
! 1467: t = W_UMALLOC(2*DEG(m)); spwrum0(mod,m,f,e1,t);
! 1468: s = W_UMALLOC(2*DEG(m)); q = W_UMALLOC(2*DEG(m));
! 1469: Mulum(mod,t,t,s); DEG(s) = Divum(mod,s,m,q);
! 1470: if ( a ) {
! 1471: Mulum(mod,s,f,t); DEG(t) = Divum(mod,t,m,q); cpyum(t,r);
1.1 noro 1472: } else
1.12 ! noro 1473: cpyum(s,r);
! 1474: }
1.1 noro 1475: }
1476:
1477: #if 0
1.6 noro 1478: void Mulum(int mod,UM p1,UM p2,UM pr)
1.1 noro 1479: {
1.12 ! noro 1480: register int *pc1,*pcr;
! 1481: register int mul,i,j,d1,d2;
! 1482: int *c1,*c2,*cr;
! 1483:
! 1484: if ( ( (d1 = DEG(p1)) < 0) || ( (d2 = DEG(p2)) < 0 ) ) {
! 1485: DEG(pr) = -1;
! 1486: return;
! 1487: }
! 1488: c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
! 1489: bzero((char *)cr,(d1+d2+1)*sizeof(int));
! 1490: for ( i = 0; i <= d2; i++, cr++ )
! 1491: if ( mul = *c2++ )
! 1492: for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ )
! 1493: *pcr = (*pc1 * mul + *pcr) % mod;
! 1494: DEG(pr) = d1 + d2;
1.1 noro 1495: }
1496:
1.6 noro 1497: void Mulsum(int mod,UM p,int n,UM pr)
1.1 noro 1498: {
1.12 ! noro 1499: register int *sp,*dp;
! 1500: register int i;
1.1 noro 1501:
1.12 ! noro 1502: for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i;
! 1503: i >= 0; i--, dp--, sp-- )
! 1504: *dp = (*sp * n) % mod;
1.1 noro 1505: }
1.12 ! noro 1506:
1.6 noro 1507: int Divum(int mod,UM p1,UM p2,UM pq)
1.1 noro 1508: {
1.12 ! noro 1509: register int *pc1,*pct;
! 1510: register int tmp,i,j,inv;
! 1511: int *c1,*c2,*ct;
! 1512: int d1,d2,dd,hd;
! 1513:
! 1514: if ( (d1 = DEG(p1)) < (d2 = DEG(p2)) ) {
! 1515: DEG(pq) = -1;
! 1516: return( d1 );
! 1517: }
! 1518: c1 = COEF(p1); c2 = COEF(p2); dd = d1-d2;
! 1519: if ( ( hd = c2[d2] ) != 1 ) {
! 1520: inv = invm(hd,mod);
! 1521: for ( pc1 = c2 + d2; pc1 >= c2; pc1-- )
! 1522: *pc1 = (*pc1 * inv) % mod;
! 1523: } else
! 1524: inv = 1;
! 1525: for ( i = dd, ct = c1+d1; i >= 0; i-- )
! 1526: if ( tmp = *ct-- ) {
! 1527: tmp = mod - tmp;
! 1528: for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- )
! 1529: *pct = (*pc1 * tmp + *pct) % mod;
! 1530: }
! 1531: if ( inv != 1 ) {
! 1532: for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ )
! 1533: *pc1 = (*pc1 * inv) % mod;
! 1534: for ( pc1 = c2, pct = c2+d2, inv = hd; pc1 <= pct; pc1++ )
! 1535: *pc1 = (*pc1 * inv) % mod;
! 1536: }
! 1537: for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- );
! 1538: for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- )
! 1539: *pct-- = *pc1--;
! 1540: return( i );
1.1 noro 1541: }
1542:
1.6 noro 1543: void Gcdum(int mod,UM p1,UM p2,UM pr)
1.1 noro 1544: {
1.12 ! noro 1545: register int *sp,*dp;
! 1546: register int i,inv;
! 1547: UM t1,t2,q,tum;
! 1548: int drem;
! 1549:
! 1550: if ( DEG(p1) < 0 )
! 1551: cpyum(p2,pr);
! 1552: else if ( DEG(p2) < 0 )
! 1553: cpyum(p1,pr);
! 1554: else {
! 1555: if ( DEG(p1) >= DEG(p2) ) {
! 1556: t1 = p1; t2 = p2;
! 1557: } else {
! 1558: t1 = p2; t2 = p1;
! 1559: }
! 1560: q = W_UMALLOC(DEG(t1));
! 1561: while ( ( drem = Divum(mod,t1,t2,q) ) >= 0 ) {
! 1562: tum = t1; t1 = t2; t2 = tum; DEG(t2) = drem;
! 1563: }
! 1564: inv = invm(COEF(t2)[DEG(t2)],mod);
! 1565: Mulsum(mod,t2,inv,pr);
! 1566: }
1.1 noro 1567: }
1568: #endif
1569:
1.6 noro 1570: void mult_mod_tab(UM p,int mod,UM *tab,UM r,int d)
1.1 noro 1571: {
1.12 ! noro 1572: UM w,w1,c;
! 1573: int n,i;
! 1574: int *pc;
! 1575:
! 1576: w = W_UMALLOC(d); w1 = W_UMALLOC(d);
! 1577: c = W_UMALLOC(1); DEG(c) = 0;
! 1578: n = DEG(p); DEG(r) = -1;
! 1579: for ( i = 0, pc = COEF(p); i <= n; i++ )
! 1580: if ( pc[i] ) {
! 1581: COEF(c)[0] = pc[i];
! 1582: mulum(mod,tab[i],c,w);
! 1583: addum(mod,r,w,w1);
! 1584: cpyum(w1,r);
! 1585: }
1.1 noro 1586: }
1587:
1.6 noro 1588: void make_qmat(UM p,int mod,UM *tab,int ***mp)
1.1 noro 1589: {
1.12 ! noro 1590: int n,i,j;
! 1591: int *c;
! 1592: UM q,r;
! 1593: int **mat;
! 1594:
! 1595: n = DEG(p);
! 1596: *mp = mat = almat(n,n);
! 1597: for ( j = 0; j < n; j++ ) {
! 1598: r = W_UMALLOC(DEG(tab[j])); q = W_UMALLOC(DEG(tab[j]));
! 1599: cpyum(tab[j],r); DEG(r) = divum(mod,r,p,q);
! 1600: for ( i = 0, c = COEF(r); i <= DEG(r); i++ )
! 1601: mat[i][j] = c[i];
! 1602: }
! 1603: for ( i = 0; i < n; i++ )
! 1604: mat[i][i] = (mat[i][i]+mod-1) % mod;
1.1 noro 1605: }
1606:
1.6 noro 1607: void null_mod(int **mat,int mod,int n,int *ind)
1.1 noro 1608: {
1.12 ! noro 1609: int i,j,l,s,h,inv;
! 1610: int *t,*u;
1.1 noro 1611:
1.12 ! noro 1612: bzero((char *)ind,n*sizeof(int));
! 1613: ind[0] = 0;
! 1614: for ( i = j = 0; j < n; i++, j++ ) {
! 1615: for ( ; j < n; j++ ) {
! 1616: for ( l = i; l < n; l++ )
! 1617: if ( mat[l][j] )
! 1618: break;
! 1619: if ( l < n ) {
! 1620: t = mat[i]; mat[i] = mat[l]; mat[l] = t; break;
! 1621: } else
! 1622: ind[j] = 1;
! 1623: }
! 1624: if ( j == n )
! 1625: break;
! 1626: inv = invm(mat[i][j],mod);
! 1627: for ( s = j, t = mat[i]; s < n; s++ )
! 1628: t[s] = dmar(t[s],inv,0,mod);
! 1629: for ( l = 0; l < n; l++ ) {
! 1630: if ( l == i )
! 1631: continue;
! 1632: for ( s = j, u = mat[l], h = (mod-u[j])%mod; s < n; s++ )
! 1633: u[s] = dmar(h,t[s],u[s],mod);
! 1634: }
! 1635: }
1.1 noro 1636: }
1637:
1.6 noro 1638: void null_to_sol(int **mat,int *ind,int mod,int n,UM *r)
1.1 noro 1639: {
1.12 ! noro 1640: int i,j,k,l;
! 1641: int *c;
! 1642: UM w;
! 1643:
! 1644: for ( i = 0, l = 0; i < n; i++ ) {
! 1645: if ( !ind[i] )
! 1646: continue;
! 1647: w = UMALLOC(n);
! 1648: for ( j = k = 0, c = COEF(w); j < n; j++ )
! 1649: if ( ind[j] )
! 1650: c[j] = 0;
! 1651: else
! 1652: c[j] = mat[k++][i];
! 1653: c[i] = mod-1;
! 1654: for ( j = n; j >= 0; j-- )
! 1655: if ( c[j] )
! 1656: break;
! 1657: DEG(w) = j;
! 1658: r[l++] = w;
! 1659: }
1.1 noro 1660: }
1661: /*
1662: make_qmat(p,mod,tab,mp)
1663: null_mod(mat,mod,n,ind)
1664: null_to_sol(mat,ind,mod,n,r)
1665: */
1666:
1.6 noro 1667: void newddd(int mod,UM f,UM *r)
1.1 noro 1668: {
1.12 ! noro 1669: register int i,j;
! 1670: int d,n;
! 1671: UM q,s,t,u,v,w,g,x,m;
! 1672: UM *base;
! 1673:
! 1674: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
! 1675: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
! 1676: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
! 1677: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
! 1678: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
! 1679: for ( i = 2; i < n; i++ ) {
! 1680: /* fprintf(stderr,"i=%d\n",i); */
! 1681: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
! 1682: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
! 1683: }
! 1684: v = W_UMALLOC(n); cpyum(f,v);
! 1685: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
! 1686: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
! 1687: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
! 1688: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
! 1689: /* fprintf(stderr,"d=%d\n",d); */
! 1690: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
! 1691: if ( COEF(w)[i] ) {
! 1692: Mulsum(mod,base[i],COEF(w)[i],s);
! 1693: addum(mod,s,t,u); cpyum(u,t);
! 1694: }
! 1695: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
! 1696: if ( DEG(g) >= 1 ) {
! 1697: berlekamp(g,mod,d,base,r+j); j += DEG(g)/d;
! 1698: Divum(mod,v,g,q); cpyum(q,v);
! 1699: DEG(w) = Divum(mod,w,v,q);
! 1700: for ( i = 0; i < DEG(v); i++ )
! 1701: DEG(base[i]) = Divum(mod,base[i],v,q);
! 1702: }
! 1703: }
! 1704: if ( DEG(v) ) {
! 1705: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
! 1706: }
! 1707: r[j] = 0;
1.1 noro 1708: }
1709:
1.6 noro 1710: int nfctr_mod(UM f,int mod)
1.1 noro 1711: {
1.12 ! noro 1712: register int i,j;
! 1713: int d,n;
! 1714: UM q,s,t,u,v,w,g,x,m;
! 1715: UM *base;
! 1716:
! 1717: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
! 1718: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
! 1719: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
! 1720: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
! 1721: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
! 1722: for ( i = 2; i < n; i++ ) {
! 1723: /* fprintf(stderr,"i=%d\n",i); */
! 1724: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
! 1725: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
! 1726: }
! 1727: v = W_UMALLOC(n); cpyum(f,v);
! 1728: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
! 1729: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
! 1730: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
! 1731: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
! 1732: /* fprintf(stderr,"d=%d\n",d); */
! 1733: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
! 1734: if ( COEF(w)[i] ) {
! 1735: Mulsum(mod,base[i],COEF(w)[i],s);
! 1736: addum(mod,s,t,u); cpyum(u,t);
! 1737: }
! 1738: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
! 1739: if ( DEG(g) >= 1 ) {
! 1740: j += DEG(g)/d;
! 1741: Divum(mod,v,g,q); cpyum(q,v);
! 1742: DEG(w) = Divum(mod,w,v,q);
! 1743: for ( i = 0; i < DEG(v); i++ )
! 1744: DEG(base[i]) = Divum(mod,base[i],v,q);
! 1745: }
! 1746: }
! 1747: if ( DEG(v) ) j++;
! 1748: return j;
1.1 noro 1749: }
1750:
1.6 noro 1751: int irred_check(UM f,int mod)
1.1 noro 1752: {
1.12 ! noro 1753: register int i,j;
! 1754: int d,n;
! 1755: UM q,s,t,u,v,w,g,x,m,f1,b;
! 1756: UM *base;
! 1757:
! 1758: if ( (n = DEG(f)) == 1 )
! 1759: return 1;
! 1760: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
! 1761: f1 = W_UMALLOC(n); b = W_UMALLOC(n);
! 1762: diffum(mod,f,t); cpyum(f,s); Gcdum(mod,t,s,g);
! 1763: if ( DEG(g) )
! 1764: return 0;
! 1765:
! 1766: base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
! 1767: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
! 1768: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
! 1769: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
! 1770: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
! 1771: for ( i = 2; i < n; i++ ) {
! 1772: /* fprintf(stderr,"i=%d\n",i); */
! 1773: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
! 1774: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
! 1775: }
! 1776: v = W_UMALLOC(n); cpyum(f,v);
! 1777: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
! 1778: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
! 1779: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
! 1780: for ( j = 0, d = 1; 2*d <= n; d++ ) {
! 1781: /* fprintf(stderr,"d=%d\n",d); */
! 1782: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
! 1783: if ( COEF(w)[i] ) {
! 1784: Mulsum(mod,base[i],COEF(w)[i],s);
! 1785: addum(mod,s,t,u); cpyum(u,t);
! 1786: }
! 1787: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
! 1788: if ( DEG(g) >= 1 )
! 1789: return 0;
! 1790: }
! 1791: return 1;
1.1 noro 1792: }
1793:
1.6 noro 1794: int berlekamp(UM p,int mod,int df,UM *tab,UM *r)
1.1 noro 1795: {
1.12 ! noro 1796: int n,i,j,k,nf,d,nr;
! 1797: int **mat;
! 1798: int *ind;
! 1799: UM mp,w,q,gcd,w1,w2;
! 1800: UM *u;
! 1801: int *root;
! 1802:
! 1803: n = DEG(p);
! 1804: ind = ALLOCA(n*sizeof(int));
! 1805: make_qmat(p,mod,tab,&mat);
! 1806: null_mod(mat,mod,n,ind);
! 1807: for ( i = 0, d = 0; i < n; i++ )
! 1808: if ( ind[i] )
! 1809: d++;
! 1810: if ( d == 1 ) {
! 1811: r[0] = UMALLOC(n); cpyum(p,r[0]); return 1;
! 1812: }
! 1813: u = ALLOCA(d*sizeof(UM *));
! 1814: r[0] = UMALLOC(n); cpyum(p,r[0]);
! 1815: null_to_sol(mat,ind,mod,n,u);
! 1816: root = ALLOCA(d*sizeof(int));
! 1817: w = W_UMALLOC(n); mp = W_UMALLOC(d);
! 1818: w1 = W_UMALLOC(n); w2 = W_UMALLOC(n);
! 1819: for ( i = 1, nf = 1; i < d; i++ ) {
! 1820: minipoly_mod(mod,u[i],p,mp);
! 1821: nr = find_root(mod,mp,root);
! 1822: for ( j = 0; j < nf; j++ ) {
! 1823: if ( DEG(r[j]) == df )
! 1824: continue;
! 1825: for ( k = 0; k < nr; k++ ) {
! 1826: cpyum(u[i],w1); cpyum(r[j],w2);
! 1827: COEF(w1)[0] = (mod-root[k]) % mod;
! 1828: gcdum(mod,w1,w2,w);
! 1829: if ( DEG(w) > 0 && DEG(w) < DEG(r[j]) ) {
! 1830: gcd = UMALLOC(DEG(w));
! 1831: q = UMALLOC(DEG(r[j])-DEG(w));
! 1832: cpyum(w,gcd); divum(mod,r[j],w,q);
! 1833: r[j] = q; r[nf++] = gcd;
! 1834: }
! 1835: if ( nf == d )
! 1836: return d;
! 1837: }
! 1838: }
! 1839: }
! 1840: /* NOTREACHED */
! 1841: error("berlekamp : cannot happen");
! 1842: return -1;
1.1 noro 1843: }
1844:
1.6 noro 1845: void minipoly_mod(int mod,UM f,UM p,UM mp)
1.1 noro 1846: {
1.12 ! noro 1847: struct p_pair *list,*l,*l1,*lprev;
! 1848: int n,d;
! 1849: UM u,p0,p1,np0,np1,q,w;
! 1850:
! 1851: list = (struct p_pair *)MALLOC(sizeof(struct p_pair));
! 1852: list->p0 = u = W_UMALLOC(0); DEG(u) = 0; COEF(u)[0] = 1;
! 1853: list->p1 = W_UMALLOC(0); cpyum(list->p0,list->p1);
! 1854: list->next = 0;
! 1855: n = DEG(p); w = UMALLOC(2*n);
! 1856: p0 = UMALLOC(2*n); cpyum(list->p0,p0);
! 1857: p1 = UMALLOC(2*n); cpyum(list->p1,p1);
! 1858: q = W_UMALLOC(2*n);
! 1859: while ( 1 ) {
! 1860: COEF(p0)[DEG(p0)] = 0; DEG(p0)++; COEF(p0)[DEG(p0)] = 1;
! 1861: mulum(mod,f,p1,w); DEG(w) = divum(mod,w,p,q); cpyum(w,p1);
! 1862: np0 = UMALLOC(n); np1 = UMALLOC(n);
! 1863: lnf_mod(mod,n,p0,p1,list,np0,np1);
! 1864: if ( DEG(np1) < 0 ) {
! 1865: cpyum(np0,mp); return;
! 1866: } else {
! 1867: l1 = (struct p_pair *)MALLOC(sizeof(struct p_pair));
! 1868: l1->p0 = np0; l1->p1 = np1;
! 1869: for ( l = list, lprev = 0, d = DEG(np1);
! 1870: l && (DEG(l->p1) > d); lprev = l, l = l->next );
! 1871: if ( lprev ) {
! 1872: lprev->next = l1; l1->next = l;
! 1873: } else {
! 1874: l1->next = list; list = l1;
! 1875: }
! 1876: }
! 1877: }
1.1 noro 1878: }
1879:
1.6 noro 1880: void lnf_mod(int mod,int n,UM p0,UM p1,struct p_pair *list,UM np0,UM np1)
1.1 noro 1881: {
1.12 ! noro 1882: int inv,h,d1;
! 1883: UM t0,t1,s0,s1;
! 1884: struct p_pair *l;
! 1885:
! 1886: cpyum(p0,np0); cpyum(p1,np1);
! 1887: t0 = W_UMALLOC(n); t1 = W_UMALLOC(n);
! 1888: s0 = W_UMALLOC(n); s1 = W_UMALLOC(n);
! 1889: for ( l = list; l; l = l->next ) {
! 1890: d1 = DEG(np1);
! 1891: if ( d1 == DEG(l->p1) ) {
! 1892: inv = invm((mod-COEF(l->p1)[d1])%mod,mod);
! 1893: h = dmar(COEF(np1)[d1],inv,0,mod);
! 1894: mulsum(mod,l->p0,h,t0); addum(mod,np0,t0,s0); cpyum(s0,np0);
! 1895: mulsum(mod,l->p1,h,t1); addum(mod,np1,t1,s1); cpyum(s1,np1);
! 1896: }
! 1897: }
1.1 noro 1898: }
1899:
1.6 noro 1900: int find_root(int mod,UM p,int *root)
1.1 noro 1901: {
1.12 ! noro 1902: UM *r;
! 1903: int i,j;
1.1 noro 1904:
1.12 ! noro 1905: r = ALLOCA((DEG(p)+1)*sizeof(UM));
! 1906: ddd(mod,p,r);
! 1907: for ( i = 0, j = 0; r[i]; i++ )
! 1908: if ( DEG(r[i]) == 1 )
! 1909: root[j++] = (mod - COEF(r[i])[0]) % mod;
! 1910: return j;
1.1 noro 1911: }
1912:
1.6 noro 1913: void showum(UM p)
1.1 noro 1914: {
1.12 ! noro 1915: int i;
! 1916: int *c;
1.1 noro 1917:
1.12 ! noro 1918: for ( i = DEG(p), c = COEF(p); i >= 0; i-- )
! 1919: if ( c[i] )
! 1920: printf("+%dx^%d",c[i],i);
! 1921: printf("\n");
1.1 noro 1922: }
1923:
1.6 noro 1924: void showumat(int **mat,int n)
1.1 noro 1925: {
1.12 ! noro 1926: int i,j;
1.1 noro 1927:
1.12 ! noro 1928: for ( i = 0; i < n; i++ ) {
! 1929: for ( j = 0; j < n; j++ )
! 1930: printf("%d ",mat[i][j]);
! 1931: printf("\n");
! 1932: }
1.1 noro 1933: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>