Annotation of OpenXM_contrib2/asir2018/engine/Q.c, Revision 1.19
1.19 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/Q.c,v 1.18 2020/10/04 03:14:09 noro Exp $ */
1.1 noro 2: #include "ca.h"
3: #include "gmp.h"
4: #include "base.h"
5: #include "inline.h"
6:
7: mpz_t ONEMPZ;
1.17 noro 8: extern Z ONE;
1.1 noro 9: int lf_lazy;
10: Z current_mod_lf;
11: int current_mod_lf_size;
12: gmp_randstate_t GMP_RAND;
13:
1.9 noro 14: #define F4_INTRAT_PERIOD 4
1.6 noro 15:
16: extern int DP_Print;
17:
1.1 noro 18: void isqrtz(Z a,Z *r);
19: void bshiftz(Z a,int n,Z *r);
1.18 noro 20: int mpz_inttorat(mpz_t c,mpz_t m,mpz_t b,mpz_t nm,mpz_t dn);
21: int generic_gauss_elim_hensel64(MAT mat,MAT *nmmat,Z *dn,int **rindp,int **cindp,DP *mb);
22: int find_lhs_and_lu_mod64(mp_limb_t **a,int row,int col,mp_limb_t md,int **rinfo,int **cinfo);
23: void solve_by_lu_mod64(mp_limb_t **a,int n,mp_limb_t md,mp_limb_signed_t **b,int l,int normalize);
1.1 noro 24:
25: void *gc_realloc(void *p,size_t osize,size_t nsize)
26: {
27: return (void *)Risa_GC_realloc(p,nsize);
28: }
29:
30: void gc_free(void *p,size_t size)
31: {
32: Risa_GC_free(p);
33: }
34:
35: void init_gmpq()
36: {
1.10 noro 37: mp_set_memory_functions(Risa_GC_malloc,gc_realloc,gc_free);
1.1 noro 38:
39: mpz_init(ONEMPZ); mpz_set_ui(ONEMPZ,1); MPZTOZ(ONEMPZ,ONE);
40: gmp_randinit_default(GMP_RAND);
41: }
42:
1.7 noro 43: void printexpr(VL,Obj);
44:
1.3 noro 45: void pmat(Z **a,int row,int col)
46: {
47: int i,j;
48:
49: for ( i = 0; i < row; i++, printf("\n") )
50: for ( j = 0; j < col; j++, printf(" ") )
1.7 noro 51: printexpr(CO,(Obj)a[i][j]);
1.3 noro 52: printf("\n");
53: }
54:
1.1 noro 55: Z utoz(unsigned int u)
56: {
57: mpz_t z;
58: Z r;
59:
60: if ( !u ) return 0;
61: mpz_init(z); mpz_set_ui(z,u); MPZTOZ(z,r); return r;
62: }
63:
64: Z stoz(int s)
65: {
66: mpz_t z;
67: Z r;
68:
69: if ( !s ) return 0;
70: mpz_init(z); mpz_set_si(z,s); MPZTOZ(z,r); return r;
71: }
72:
73: int sgnz(Z z)
74: {
75: if ( !z ) return 0;
76: else return mpz_sgn(BDY(z));
77: }
78:
79: void nmq(Q q,Z *r)
80: {
81: if ( !q ) *r = 0;
82: else if ( INT(q) ) *r = (Z)q;
83: else {
84: MPZTOZ(mpq_numref(BDY(q)),*r);
85: }
86: }
87:
88: void dnq(Q q,Z *r)
89: {
90: if ( !q ) *r = 0;
91: else if ( INT(q) ) *r = ONE;
92: else {
93: MPZTOZ(mpq_denref(BDY(q)),*r);
94: }
95: }
96:
97: int sgnq(Q q)
98: {
99: if ( !q ) return 0;
100: else if ( q->z ) return mpz_sgn(BDY((Z)q));
101: else return mpz_sgn(mpq_numref(BDY(q)));
102: }
103:
104: Q mpqtozq(mpq_t a)
105: {
106: Z z;
107: Q q;
108:
109: if ( INTMPQ(a) ) {
110: MPZTOZ(mpq_numref(a),z); return (Q)z;
111: } else {
112: MPQTOQ(a,q); return q;
113: }
114: }
115:
116: void dupz(Z a,Z *b)
117: {
118: mpz_t t;
119:
120: if ( !a ) *b = a;
121: else {
122: mpz_init(t); mpz_set(t,BDY(a)); MPZTOZ(t,*b);
123: }
124: }
125:
126: int n_bits_z(Z a)
127: {
128: return a ? mpz_sizeinbase(BDY(a),2) : 0;
129: }
130:
131: void addz(Z n1,Z n2,Z *nr)
132: {
133: mpz_t t;
134: int s1,s2;
135:
136: if ( !n1 ) *nr = n2;
137: else if ( !n2 ) *nr = n1;
138: else if ( !n1->z || !n2->z )
139: error("addz : invalid argument");
140: else {
141: mpz_init(t); mpz_add(t,BDY(n1),BDY(n2)); MPZTOZ(t,*nr);
142: }
143: }
144:
145: void subz(Z n1,Z n2,Z *nr)
146: {
147: mpz_t t;
148:
149: if ( !n1 ) {
150: if ( !n2 )
151: *nr = 0;
152: else
153: chsgnz(n2,nr);
154: } else if ( !n2 )
155: *nr = n1;
156: else if ( n1 == n2 )
157: *nr = 0;
158: else if ( !n1->z || !n2->z )
159: error("subz : invalid argument");
160: else {
161: mpz_init(t); mpz_sub(t,BDY(n1),BDY(n2)); MPZTOZ(t,*nr);
162: }
163: }
164:
165: void mulz(Z n1,Z n2,Z *nr)
166: {
167: mpz_t t;
168:
169: if ( !n1 || !n2 ) *nr = 0;
170: else if ( !n1->z || !n2->z )
171: error("mulz : invalid argument");
172: else if ( UNIQ(n1) ) *nr = n2;
173: else if ( UNIQ(n2) ) *nr = n1;
174: else if ( MUNIQ(n1) ) chsgnz(n2,nr);
175: else if ( MUNIQ(n2) ) chsgnz(n1,nr);
176: else {
177: mpz_init(t); mpz_mul(t,BDY(n1),BDY(n2)); MPZTOZ(t,*nr);
178: }
179: }
180:
181: /* nr += n1*n2 */
182:
183: void muladdtoz(Z n1,Z n2,Z *nr)
184: {
1.3 noro 185: #if 0
1.1 noro 186: Z t;
187:
188: if ( n1 && n2 ) {
189: if ( !(*nr) ) {
190: NEWZ(t); mpz_init(BDY(t)); *nr = t;
191: }
192: mpz_addmul(BDY(*nr),BDY(n1),BDY(n2));
1.2 noro 193: if ( !mpz_sgn(BDY(*nr)) )
194: *nr = 0;
1.3 noro 195: }
1.2 noro 196: #else
197: Z t,s;
198:
199: mulz(n1,n2,&t); addz(*nr,t,&s); *nr = s;
200: #endif
1.1 noro 201: }
202:
203: /* nr += n1*u */
204:
205: void mul1addtoz(Z n1,long u,Z *nr)
206: {
1.3 noro 207: #if 0
1.1 noro 208: Z t;
209:
210: if ( n1 && u ) {
211: if ( !(*nr) ) {
212: NEWZ(t); mpz_init(BDY(t)); *nr = t;
213: }
214: if ( u >= 0 )
215: mpz_addmul_ui(BDY(*nr),BDY(n1),(unsigned long)u);
216: else
217: mpz_submul_ui(BDY(*nr),BDY(n1),(unsigned long)(-u));
1.2 noro 218: if ( !mpz_sgn(BDY(*nr)) )
219: *nr = 0;
1.1 noro 220: }
1.3 noro 221: #else
222: Z t,s;
223:
224: mul1z(n1,u,&t); addz(*nr,t,&s); *nr = s;
225: #endif
1.1 noro 226: }
227:
228: void mul1z(Z n1,long n2,Z *nr)
229: {
230: mpz_t t;
231:
232: if ( !n1 || !n2 ) *nr = 0;
233: else {
234: mpz_init(t); mpz_mul_si(t,BDY(n1),n2); MPZTOZ(t,*nr);
235: }
236: }
237:
238: void divz(Z n1,Z n2,Z *nq)
239: {
240: mpz_t t;
241: mpq_t a, b, q;
242:
243: if ( !n2 ) {
244: error("division by 0");
245: *nq = 0;
246: } else if ( !n1 )
247: *nq = 0;
248: else if ( n1 == n2 ) {
249: mpz_init(t); mpz_set_ui(t,1); MPZTOZ(t,*nq);
250: } else {
251: MPZTOMPQ(BDY(n1),a); MPZTOMPQ(BDY(n2),b);
252: mpq_init(q); mpq_div(q,a,b); *nq = (Z)mpqtozq(q);
253: }
254: }
255:
256: void remz(Z n1,Z n2,Z *nr)
257: {
258: mpz_t r;
259:
260: if ( !n2 ) {
261: error("division by 0");
262: *nr = 0;
263: } else if ( !n1 || n1 == n2 )
264: *nr = 0;
265: else if ( !n1->z || !n2->z )
266: error("remz : invalid argument");
267: else {
268: mpz_init(r);
269: mpz_mod(r,BDY(n1),BDY(n2));
270: if ( !mpz_sgn(r) ) *nr = 0;
271: else MPZTOZ(r,*nr);
272: }
273: }
274:
275: void divqrz(Z n1,Z n2,Z *nq,Z *nr)
276: {
277: mpz_t t, a, b, q, r;
278:
279: if ( !n2 ) {
280: error("division by 0");
281: *nq = 0; *nr = 0;
282: } else if ( !n1 ) {
283: *nq = 0; *nr = 0;
284: } else if ( !n1->z || !n2->z )
285: error("divqrz : invalid argument");
286: else if ( n1 == n2 ) {
287: mpz_init(t); mpz_set_ui(t,1); MPZTOZ(t,*nq); *nr = 0;
288: } else {
289: mpz_init(q); mpz_init(r);
290: mpz_fdiv_qr(q,r,BDY(n1),BDY(n2));
291: if ( !mpz_sgn(q) ) *nq = 0;
292: else MPZTOZ(q,*nq);
293: if ( !mpz_sgn(r) ) *nr = 0;
294: else MPZTOZ(r,*nr);
295: }
296: }
297:
298: void divsz(Z n1,Z n2,Z *nq)
299: {
300: mpz_t t;
301: mpq_t a, b, q;
302:
303: if ( !n2 ) {
304: error("division by 0");
305: *nq = 0;
306: } else if ( !n1 )
307: *nq = 0;
308: else if ( !n1->z || !n2->z )
309: error("divsz : invalid argument");
310: else if ( n1 == n2 ) {
311: mpz_init(t); mpz_set_ui(t,1); MPZTOZ(t,*nq);
312: } else {
313: mpz_init(t); mpz_divexact(t,BDY(n1),BDY(n2)); MPZTOZ(t,*nq);
314: }
315: }
316:
317: void chsgnz(Z n,Z *nr)
318: {
319: mpz_t t;
320:
321: if ( !n )
322: *nr = 0;
323: else if ( !n->z )
324: error("chsgnz : invalid argument");
325: else {
326: t[0] = BDY(n)[0]; mpz_neg(t,t); MPZTOZ(t,*nr);
327: }
328: }
329:
330: void absz(Z n,Z *nr)
331: {
332: if ( !n ) *nr = 0;
333: else if ( !n->z )
334: error("absz : invalid argument");
335: else if ( sgnz(n) < 0 ) chsgnz(n,nr);
336: else *nr = n;
337: }
338:
339: int evenz(Z n)
340: {
341: return !n ? 1 : mpz_even_p(BDY(n));
342: }
343:
344: int smallz(Z n)
345: {
346: if ( !n ) return 1;
347: else if ( INT(n) && mpz_fits_sint_p(BDY(n)) ) return 1;
348: else return 0;
349: }
350:
351: void pwrz(Z n1,Z n,Z *nr)
352: {
353: mpq_t t,q;
354: mpz_t z;
355: Q p,r;
356:
357: if ( !n || UNIQ(n1) ) *nr = ONE;
358: else if ( !n1 ) *nr = 0;
359: else if ( !n->z || !n1->z )
360: error("pwrz : invalid argument");
361: else if ( MUNIQ(n1) ) {
362: if ( mpz_even_p(BDY((Z)n)) ) *nr = ONE;
363: else *nr = n1;
364: } else if ( !smallz(n) ) {
365: error("exponent too big."); *nr = 0;
366: } else if ( n1->z && mpz_sgn(BDY((Z)n))>0 ) {
1.5 noro 367: mpz_init(z); mpz_pow_ui(z,BDY(n1),ZTOS(n)); MPZTOZ(z,*nr);
1.1 noro 368: } else {
369: MPZTOMPQ(BDY(n1),q); MPQTOQ(q,r);
370: pwrq(r,(Q)n,&p); *nr = (Z)p;
371: }
372: }
373:
374: int cmpz(Z q1,Z q2)
375: {
376: int sgn;
377:
378: if ( !q1 ) {
379: if ( !q2 )
380: return 0;
381: else
382: return -mpz_sgn(BDY(q2));
383: } else if ( !q2 )
384: return mpz_sgn(BDY(q1));
385: else if ( !q1->z || !q2->z )
386: error("mpqz : invalid argument");
387: else if ( (sgn = mpz_sgn(BDY(q1))) != mpz_sgn(BDY(q2)) )
388: return sgn;
389: else {
390: sgn = mpz_cmp(BDY(q1),BDY(q2));
391: if ( sgn > 0 ) return 1;
392: else if ( sgn < 0 ) return -1;
393: else return 0;
394: }
1.19 ! noro 395: /* XXX */
! 396: return 0;
1.1 noro 397: }
398:
399: void gcdz(Z n1,Z n2,Z *nq)
400: {
401: mpz_t t;
402:
403: if ( !n1 ) *nq = n2;
404: else if ( !n2 ) *nq = n1;
405: else if ( !n1->z || !n2->z )
406: error("gcdz : invalid argument");
407: else {
408: mpz_init(t); mpz_gcd(t,BDY(n1),BDY(n2));
409: MPZTOZ(t,*nq);
410: }
411: }
412:
413: void invz(Z n1,Z n2,Z *nq)
414: {
415: mpz_t t;
416:
417: if ( !n1 || !n2 || !n1->z || !n2->z )
418: error("invz : invalid argument");
419: mpz_init(t); mpz_invert(t,BDY(n1),BDY(n2));
420: MPZTOZ(t,*nq);
421: }
422:
423: void lcmz(Z n1,Z n2,Z *nq)
424: {
425: Z g,t;
426:
427: if ( !n1 || !n2 ) *nq = 0;
428: else if ( !n1->z || !n2->z )
429: error("lcmz : invalid argument");
430: else {
431: gcdz(n1,n2,&g); divsz(n1,g,&t);
432: mulz(n2,t,nq);
433: }
434: }
435:
436: void gcdvz(VECT v,Z *q)
437: {
438: int n,i;
439: Z *b;
440: Z g,g1;
441:
442: n = v->len;
443: b = (Z *)v->body;
444: g = b[0];
445: for ( i = 1; i < n; i++ ) {
446: gcdz(g,b[i],&g1); g = g1;
447: }
448: *q = g;
449: }
450:
451: void gcdvz_estimate(VECT v,Z *q)
452: {
453: int n,m,i;
454: Z s,t,u;
455: Z *b;
456:
457: n = v->len;
458: b = (Z *)v->body;
459: if ( n == 1 ) {
460: if ( mpz_sgn(BDY(b[0]))<0 ) chsgnz(b[0],q);
461: else *q = b[0];
462: }
463: m = n/2;
464: for ( i = 0, s = 0; i < m; i++ ) {
465: if ( b[i] && mpz_sgn(BDY(b[i]))<0 ) subz(s,b[i],&u);
466: else addz(s,b[i],&u);
467: s = u;
468: }
1.4 noro 469: for ( t = 0; i < n; i++ ) {
1.1 noro 470: if ( b[i] && mpz_sgn(BDY(b[i]))<0 ) subz(t,b[i],&u);
471: else addz(t,b[i],&u);
472: t = u;
473: }
474: gcdz(s,t,q);
475: }
476:
1.4 noro 477: void gcdv_mpz_estimate(mpz_t g,mpz_t *b,int n)
478: {
479: int m,m2,i,j;
480: mpz_t s,t;
481:
482: mpz_init(g);
483: for ( i = 0, m = 0; i < n; i++ )
484: if ( mpz_sgn(b[i]) ) m++;
485: if ( !m ) {
486: mpz_set_ui(g,0);
487: return;
488: }
489: if ( m == 1 ) {
490: for ( i = 0, m = 0; i < n; i++ )
491: if ( mpz_sgn(b[i]) ) break;
492: if ( mpz_sgn(b[i])<0 ) mpz_neg(g,b[i]);
493: else mpz_set(g,b[i]);
494: return ;
495: }
496: m2 = m/2;
497: mpz_init_set_ui(s,0);
498: for ( i = j = 0; j < m2; i++ ) {
499: if ( mpz_sgn(b[i]) ) {
500: if ( mpz_sgn(b[i])<0 )
501: mpz_sub(s,s,b[i]);
502: else
503: mpz_add(s,s,b[i]);
504: j++;
505: }
506: }
507: mpz_init_set_ui(t,0);
508: for ( ; i < n; i++ ) {
509: if ( mpz_sgn(b[i]) ) {
510: if ( mpz_sgn(b[i])<0 )
511: mpz_sub(t,t,b[i]);
512: else
513: mpz_add(t,t,b[i]);
514: }
515: }
516: mpz_gcd(g,s,t);
517: }
518:
519:
1.1 noro 520: void factorialz(unsigned int n,Z *nr)
521: {
522: mpz_t a;
523: mpz_init(a);
1.13 noro 524: mpz_fac_ui(a,(unsigned long)n);
1.1 noro 525: MPZTOZ(a,*nr);
526: }
527:
528: void randomz(int blen,Z *nr)
529: {
530: mpz_t z;
531:
532: mpz_init(z);
533: mpz_urandomb(z,GMP_RAND,blen);
534: MPZTOZ(z,*nr);
535: }
536:
537: int tstbitz(Z n,int k)
538: {
539: if ( !n || !n->z )
540: error("tstbitz : invalid argument");
541: return !n ? 0 : mpz_tstbit(BDY(n),k);
542: }
543:
544: void addq(Q n1,Q n2,Q *nr)
545: {
546: mpq_t q1,q2,t;
547:
548: if ( !n1 ) *nr = n2;
549: else if ( !n2 ) *nr = n1;
550: else if ( n1->z && n2->z )
551: addz((Z)n1,(Z)n2,(Z *)nr);
552: else {
553: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
554: else q1[0] = BDY(n1)[0];
555: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
556: else q2[0] = BDY(n2)[0];
557: mpq_init(t); mpq_add(t,q1,q2); *nr = mpqtozq(t);
558: }
559: }
560:
561: void subq(Q n1,Q n2,Q *nr)
562: {
563: mpq_t q1,q2,t;
564:
565: if ( !n1 ) {
566: if ( !n2 ) *nr = 0;
1.15 noro 567: else if ( n2->z ) chsgnz((Z)n2,(Z *)nr);
1.1 noro 568: else {
569: mpq_init(t); mpq_neg(t,BDY(n2)); MPQTOQ(t,*nr);
570: }
571: } else if ( !n2 ) *nr = n1;
572: else if ( n1 == n2 ) *nr = 0;
573: else if ( n1->z && n2->z )
574: subz((Z)n1,(Z)n2,(Z *)nr);
575: else {
576: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
577: else q1[0] = BDY(n1)[0];
578: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
579: else q2[0] = BDY(n2)[0];
580: mpq_init(t); mpq_sub(t,q1,q2); *nr = mpqtozq(t);
581: }
582: }
583:
584: void mulq(Q n1,Q n2,Q *nr)
585: {
586: mpq_t t,q1,q2;
587:
588: if ( !n1 || !n2 ) *nr = 0;
589: else if ( n1->z && n2->z )
590: mulz((Z)n1,(Z)n2,(Z *)nr);
591: else {
592: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
593: else q1[0] = BDY(n1)[0];
594: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
595: else q2[0] = BDY(n2)[0];
596: mpq_init(t); mpq_mul(t,q1,q2); *nr = mpqtozq(t);
597: }
598: }
599:
600: void divq(Q n1,Q n2,Q *nq)
601: {
602: mpq_t t,q1,q2;
603:
604: if ( !n2 ) {
605: error("division by 0");
606: *nq = 0;
607: return;
608: } else if ( !n1 ) *nq = 0;
609: else if ( n1 == n2 ) *nq = (Q)ONE;
610: else {
611: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
612: else q1[0] = BDY(n1)[0];
613: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
614: else q2[0] = BDY(n2)[0];
615: mpq_init(t); mpq_div(t,q1,q2); *nq = mpqtozq(t);
616: }
617: }
618:
619: void invq(Q n,Q *nr)
620: {
621: Z nm,dn;
622:
623: if ( INT(n) )
624: divq((Q)ONE,n,nr);
625: else {
626: nmq(n,&nm);
627: dnq(n,&dn);
628: divq((Q)dn,(Q)nm,nr);
629: }
630: }
631:
632: void chsgnq(Q n,Q *nr)
633: {
634: mpq_t t;
635:
636: if ( !n ) *nr = 0;
637: else if (n->z ) chsgnz((Z)n,(Z *)nr);
638: else {
639: mpq_init(t); mpq_neg(t,BDY(n)); MPQTOQ(t,*nr);
640: }
641: }
642:
643: void absq(Q n,Q *nr)
644: {
645: if ( !n ) *nr = 0;
646: else if ( n->z ) absz((Z)n,(Z *)nr);
647: else if ( sgnq(n) < 0 ) chsgnq(n,nr);
648: else *nr = n;
649: }
650:
651: void pwrq(Q n1,Q n,Q *nr)
652: {
653: int e;
654: mpz_t nm,dn;
655: mpq_t t;
656:
657: if ( !n || UNIQ((Z)n1) || UNIQ(n1) ) *nr = (Q)ONE;
658: else if ( !n1 ) *nr = 0;
659: else if ( !INT(n) ) {
660: error("can't calculate fractional power."); *nr = 0;
661: } else if ( !smallz((Z)n) ) {
662: error("exponent too big."); *nr = 0;
663: } else {
1.5 noro 664: e = ZTOS(n);
1.1 noro 665: if ( e < 0 ) {
666: e = -e;
667: if ( n1->z ) {
668: nm[0] = ONEMPZ[0];
669: dn[0] = BDY((Z)n1)[0];
670: } else {
671: nm[0] = mpq_denref(BDY(n1))[0];
672: dn[0] = mpq_numref(BDY(n1))[0];
673: }
674: } else {
675: if ( n1->z ) {
676: nm[0] = BDY((Z)n1)[0];
677: dn[0] = ONEMPZ[0];
678: } else {
679: nm[0] = mpq_numref(BDY(n1))[0];
680: dn[0] = mpq_denref(BDY(n1))[0];
681: }
682: }
683: mpq_init(t);
684: mpz_pow_ui(mpq_numref(t),nm,e); mpz_pow_ui(mpq_denref(t),dn,e);
685: *nr = mpqtozq(t);
686: }
687: }
688:
689: int cmpq(Q n1,Q n2)
690: {
691: mpq_t q1,q2;
692: int sgn;
693:
694: if ( !n1 ) {
695: if ( !n2 ) return 0;
696: else return (n2->z) ? -mpz_sgn(BDY((Z)n2)) : -mpq_sgn(BDY(n2));
697: } if ( !n2 ) return (n1->z) ? mpz_sgn(BDY((Z)n1)) : mpq_sgn(BDY(n1));
698: else if ( n1->z && n2->z )
699: return cmpz((Z)n1,(Z)n2);
700: else if ( (sgn = mpq_sgn(BDY(n1))) != mpq_sgn(BDY(n2)) ) return sgn;
701: else {
702: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
703: else q1[0] = BDY(n1)[0];
704: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
705: else q2[0] = BDY(n2)[0];
706: sgn = mpq_cmp(q1,q2);
707: if ( sgn > 0 ) return 1;
708: else if ( sgn < 0 ) return -1;
709: else return 0;
710: }
711: }
712:
713: /* t = [nC0 nC1 ... nCn] */
714:
715: void mkbc(int n,Z *t)
716: {
717: int i;
718: Z c,d,iq;
719:
720: for ( t[0] = ONE, i = 1; i <= n/2; i++ ) {
1.5 noro 721: STOZ(n-i+1,c); mulz(t[i-1],c,&d);
722: STOZ(i,iq); divsz(d,iq,&t[i]);
1.1 noro 723: }
724: for ( ; i <= n; i++ )
725: t[i] = t[n-i];
726: }
727:
728: /*
729: * Dx^k*x^l = W(k,l,0)*x^l*Dx^k+W(k,l,1)*x^(l-1)*x^(k-1)*+...
730: *
731: * t = [W(k,l,0) W(k,l,1) ... W(k,l,min(k,l)]
732: * where W(k,l,i) = i! * kCi * lCi
733: */
734:
735: /* mod m table */
736: /* XXX : should be optimized */
737:
738: void mkwcm(int k,int l,int m,int *t)
739: {
740: int i,n;
741: Z *s;
742:
743: n = MIN(k,l);
744: s = (Z *)ALLOCA((n+1)*sizeof(Q));
745: mkwc(k,l,s);
746: for ( i = 0; i <= n; i++ ) {
747: t[i] = remqi((Q)s[i],m);
748: }
749: }
750:
751: void mkwc(int k,int l,Z *t)
752: {
753: mpz_t a,b,q,nm,z,u;
754: int i,n;
755:
756: n = MIN(k,l);
757: mpz_init_set_ui(z,1);
758: mpz_init(u); mpz_set(u,z); MPZTOZ(u,t[0]);
759: mpz_init(a); mpz_init(b); mpz_init(nm);
760: for ( i = 1; i <= n; i++ ) {
761: mpz_set_ui(a,k-i+1); mpz_set_ui(b,l-i+1); mpz_mul(nm,a,b);
762: mpz_mul(z,BDY(t[i-1]),nm); mpz_fdiv_q_ui(z,z,i);
763: mpz_init(u); mpz_set(u,z); MPZTOZ(u,t[i]);
764: }
765: }
766:
767: void lgp(P p,Z *g,Z *l);
768:
769: void ptozp(P p,int sgn,Q *c,P *pr)
770: {
1.16 noro 771: Z nm,dn,nm1;
1.1 noro 772:
773: if ( !p ) {
774: *c = 0; *pr = 0;
775: } else {
776: lgp(p,&nm,&dn);
1.16 noro 777: if ( sgn < 0 ) {
778: chsgnz(nm,&nm1); nm = nm1;
779: }
1.1 noro 780: divz(nm,dn,(Z *)c);
781: divsp(CO,p,(P)*c,pr);
782: }
783: }
784:
785: void lgp(P p,Z *g,Z *l)
786: {
787: DCP dc;
788: Z g1,g2,l1,l2,l3,l4;
789:
790: if ( NUM(p) ) {
791: if ( ((Q)p)->z ) {
792: MPZTOZ(BDY((Z)p),*g);
793: *l = ONE;
794: } else {
795: MPZTOZ(mpq_numref(BDY((Q)p)),*g);
796: MPZTOZ(mpq_denref(BDY((Q)p)),*l);
797: }
798: } else {
799: dc = DC(p); lgp(COEF(dc),g,l);
800: for ( dc = NEXT(dc); dc; dc = NEXT(dc) ) {
801: lgp(COEF(dc),&g1,&l1); gcdz(*g,g1,&g2); *g = g2;
802: gcdz(*l,l1,&l2); mulz(*l,l1,&l3); divz(l3,l2,l);
803: }
804: }
805: }
806:
807: void qltozl(Q *w,int n,Z *dvr)
808: {
809: Z nm,dn;
810: Z g,g1,l1,l2,l3;
811: Q c;
812: int i;
813: struct oVECT v;
814:
815: for ( i = 0; i < n; i++ )
816: if ( w[i] && !w[i]->z )
817: break;
818: if ( i == n ) {
819: v.id = O_VECT; v.len = n; v.body = (pointer *)w;
820: gcdvz(&v,dvr); return;
821: }
822: for ( i = 0; !w[i]; i++ );
823: c = w[i];
824: if ( !c->z ) {
825: MPZTOZ(mpq_numref(BDY(c)),nm); MPZTOZ(mpq_denref(BDY(c)),dn);
826: } else {
827: MPZTOZ(BDY((Z)c),nm); dn = ONE;
828: }
829: for ( i++; i < n; i++ ) {
830: c = w[i];
831: if ( !c ) continue;
832: if ( !c->z ) {
833: MPZTOZ(mpq_numref(BDY(c)),g1); MPZTOZ(mpq_denref(BDY(c)),l1);
834: } else {
835: MPZTOZ(BDY((Z)c),g1); l1 = ONE;
836: }
837: gcdz(nm,g1,&g); nm = g;
838: gcdz(dn,l1,&l2); mulz(dn,l1,&l3); divz(l3,l2,&dn);
839: }
840: divz(nm,dn,dvr);
841: }
842:
843: int z_bits(Q q)
844: {
845: if ( !q ) return 0;
846: else if ( q->z ) return mpz_sizeinbase(BDY((Z)q),2);
847: else
848: return mpz_sizeinbase(mpq_numref(BDY(q)),2)
849: + mpz_sizeinbase(mpq_denref(BDY(q)),2);
850: }
851:
852: int zp_mag(P p)
853: {
854: int s;
855: DCP dc;
856:
857: if ( !p ) return 0;
858: else if ( OID(p) == O_N ) return z_bits((Q)p);
859: else {
860: for ( dc = DC(p), s = 0; dc; dc = NEXT(dc) ) s += zp_mag(COEF(dc));
861: return s;
862: }
863: }
864:
865: void makesubstz(VL v,NODE *s)
866: {
867: NODE r,r0;
868: Z q;
869: unsigned int n;
870:
871: for ( r0 = 0; v; v = NEXT(v) ) {
872: NEXTNODE(r0,r); BDY(r) = (pointer)v->v;
873: #if defined(_PA_RISC1_1)
874: n = mrand48()&BMASK; q = utoz(n);
875: #else
876: n = random(); q = utoz(n);
877: #endif
878: NEXTNODE(r0,r); BDY(r) = (pointer)q;
879: }
880: if ( r0 ) NEXT(r) = 0;
881: *s = r0;
882: }
883:
884: unsigned int remqi(Q a,unsigned int mod)
885: {
886: unsigned int c,nm,dn;
887: mpz_t r;
888:
889: if ( !a ) return 0;
890: else if ( a->z ) {
891: mpz_init(r);
892: c = mpz_fdiv_r_ui(r,BDY((Z)a),mod);
893: } else {
894: mpz_init(r);
895: nm = mpz_fdiv_r_ui(r,mpq_numref(BDY(a)),mod);
896: dn = mpz_fdiv_r_ui(r,mpq_denref(BDY(a)),mod);
897: dn = invm(dn,mod);
898: DMAR(nm,dn,0,mod,c);
899: }
900: return c;
901: }
902:
903: int generic_gauss_elim(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp)
904: {
905: int **wmat;
906: Z **bmat,**tmat,*bmi,*tmi;
907: Z q,m1,m2,m3,s,u;
908: int *wmi,*colstat,*wcolstat,*rind,*cind;
909: int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv;
910: MAT r,crmat;
911: int ret;
1.8 noro 912: MAT mat2,nm2;
913: Z dn2;
914: int *rind2,*cind2;
915: int ret2;
1.1 noro 916:
1.6 noro 917: #if SIZEOF_LONG == 8
1.8 noro 918: ret = generic_gauss_elim64(mat,nm,dn,rindp,cindp);
919: return ret;
1.6 noro 920: #endif
1.1 noro 921: bmat = (Z **)mat->body;
922: row = mat->row; col = mat->col;
923: wmat = (int **)almat(row,col);
924: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
925: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
926: for ( ind = 0; ; ind++ ) {
927: if ( DP_Print ) {
928: fprintf(asir_out,"."); fflush(asir_out);
929: }
930: md = get_lprime(ind);
931: for ( i = 0; i < row; i++ )
932: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
933: wmi[j] = remqi((Q)bmi[j],md);
934: rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
935: if ( !ind ) {
936: RESET:
937: m1 = utoz(md);
938: rank0 = rank;
939: bcopy(wcolstat,colstat,col*sizeof(int));
940: MKMAT(crmat,rank,col-rank);
941: MKMAT(r,rank,col-rank); *nm = r;
942: tmat = (Z **)crmat->body;
943: for ( i = 0; i < rank; i++ )
944: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
945: if ( !colstat[j] ) tmi[k++] = utoz(wmi[j]);
946: } else {
947: if ( rank < rank0 ) {
948: if ( DP_Print ) {
949: fprintf(asir_out,"lower rank matrix; continuing...\n");
950: fflush(asir_out);
951: }
952: continue;
953: } else if ( rank > rank0 ) {
954: if ( DP_Print ) {
955: fprintf(asir_out,"higher rank matrix; resetting...\n");
956: fflush(asir_out);
957: }
958: goto RESET;
959: } else {
960: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
961: if ( j < col ) {
962: if ( DP_Print ) {
963: fprintf(asir_out,"inconsitent colstat; resetting...\n");
964: fflush(asir_out);
965: }
966: goto RESET;
967: }
968: }
969:
970: inv = invm(remqi((Q)m1,md),md);
971: m2 = utoz(md); mulz(m1,m2,&m3);
972: for ( i = 0; i < rank; i++ )
973: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
974: if ( !colstat[j] ) {
975: if ( tmi[k] ) {
976: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
977: t = remqi((Q)tmi[k],md);
978: if ( wmi[j] >= t ) t = wmi[j]-t;
979: else t = md-(t-wmi[j]);
980: DMAR(t,inv,0,md,t1)
981: u = utoz(t1); mulz(m1,u,&s);
982: addz(tmi[k],s,&u); tmi[k] = u;
983: } else if ( wmi[j] ) {
984: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
985: DMAR(wmi[j],inv,0,md,t)
986: u = utoz(t); mulz(m1,u,&s); tmi[k] = s;
987: }
988: k++;
989: }
990: m1 = m3;
991: if ( ind % F4_INTRAT_PERIOD )
992: ret = 0;
993: else
994: ret = intmtoratm(crmat,m1,*nm,dn);
995: if ( ret ) {
996: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
997: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
998: for ( j = k = l = 0; j < col; j++ )
999: if ( colstat[j] ) rind[k++] = j;
1000: else cind[l++] = j;
1001: if ( gensolve_check(mat,*nm,*dn,rind,cind) )
1002: return rank;
1003: }
1004: }
1005: }
1006: }
1007:
1008: int generic_gauss_elim2(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp)
1009: {
1010:
1011: MAT full;
1012: Z **bmat,**b;
1013: Z *bmi;
1014: Z dn0;
1015: int row,col,md,i,j,rank,ret;
1016: int **wmat;
1017: int *wmi;
1018: int *colstat,*rowstat;
1019:
1020: bmat = (Z **)mat->body;
1021: row = mat->row; col = mat->col;
1022: wmat = (int **)almat(row,col);
1023: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1024: rowstat = (int *)MALLOC_ATOMIC(row*sizeof(int));
1025: /* XXX */
1026: md = get_lprime(0);
1027: for ( i = 0; i < row; i++ )
1028: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
1029: wmi[j] = remqi((Q)bmi[j],md);
1030: rank = generic_gauss_elim_mod2(wmat,row,col,md,colstat,rowstat);
1031: b = (Z **)MALLOC(rank*sizeof(Z));
1032: for ( i = 0; i < rank; i++ ) b[i] = bmat[rowstat[i]];
1033: NEWMAT(full); full->row = rank; full->col = col; full->body = (pointer **)b;
1034: ret = generic_gauss_elim_full(full,nm,dn,rindp,cindp);
1035: if ( !ret ) {
1036: rank = generic_gauss_elim(mat,nm,&dn0,rindp,cindp);
1037: for ( i = 0; i < rank; i++ ) dn[i] = dn0;
1038: }
1039: return rank;
1040: }
1041:
1042: int generic_gauss_elim_full(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp)
1043: {
1044: int **wmat;
1045: int *stat;
1046: Z **bmat,**tmat,*bmi,*tmi,*ri;
1047: Z q,m1,m2,m3,s,u;
1048: int *wmi,*colstat,*wcolstat,*rind,*cind;
1049: int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv,h;
1050: MAT r,crmat;
1051: int ret,initialized,done;
1052:
1053: initialized = 0;
1054: bmat = (Z **)mat->body;
1055: row = mat->row; col = mat->col;
1056: wmat = (int **)almat(row,col);
1057: stat = (int *)MALLOC_ATOMIC(row*sizeof(int));
1058: for ( i = 0; i < row; i++ ) stat[i] = 0;
1059: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1060: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1061: for ( ind = 0; ; ind++ ) {
1062: if ( DP_Print ) {
1063: fprintf(asir_out,"."); fflush(asir_out);
1064: }
1065: md = get_lprime(ind);
1066: for ( i = 0; i < row; i++ )
1067: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
1068: wmi[j] = remqi((Q)bmi[j],md);
1069: rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
1070: if ( rank < row ) continue;
1071: if ( !initialized ) {
1072: m1 = utoz(md);
1073: bcopy(wcolstat,colstat,col*sizeof(int));
1074: MKMAT(crmat,row,col-row);
1075: MKMAT(r,row,col-row); *nm = r;
1076: tmat = (Z **)crmat->body;
1077: for ( i = 0; i < row; i++ )
1078: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
1079: if ( !colstat[j] ) tmi[k++] = utoz(wmi[j]);
1080: initialized = 1;
1081: } else {
1082: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
1083: if ( j < col ) continue;
1084:
1085: inv = invm(remqi((Q)m1,md),md);
1086: m2 = utoz(md); mulz(m1,m2,&m3);
1087: for ( i = 0; i < row; i++ )
1088: switch ( stat[i] ) {
1089: case 1:
1090: /* consistency check */
1091: ri = (Z *)BDY(r)[i]; wmi = wmat[i];
1092: for ( j = 0; j < col; j++ ) if ( colstat[j] ) break;
1093: h = md-remqi((Q)dn[i],md);
1094: for ( j++, k = 0; j < col; j++ )
1095: if ( !colstat[j] ) {
1096: t = remqi((Q)ri[k],md);
1097: DMAR(wmi[i],h,t,md,t1);
1098: if ( t1 ) break;
1099: }
1100: if ( j == col ) { stat[i]++; break; }
1101: else {
1102: /* fall to the case 0 */
1103: stat[i] = 0;
1104: }
1105: case 0:
1106: tmi = tmat[i]; wmi = wmat[i];
1107: for ( j = k = 0; j < col; j++ )
1108: if ( !colstat[j] ) {
1109: if ( tmi[k] ) {
1110: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
1111: t = remqi((Q)tmi[k],md);
1112: if ( wmi[j] >= t ) t = wmi[j]-t;
1113: else t = md-(t-wmi[j]);
1114: DMAR(t,inv,0,md,t1)
1115: u = utoz(t1); mulz(m1,u,&s);
1116: addz(tmi[k],s,&u); tmi[k] = u;
1117: } else if ( wmi[j] ) {
1118: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
1119: DMAR(wmi[j],inv,0,md,t)
1120: u = utoz(t); mulz(m1,u,&s); tmi[k] = s;
1121: }
1122: k++;
1123: }
1124: break;
1125: case 2: default:
1126: break;
1127: }
1128: m1 = m3;
1129: if ( ind % 4 )
1130: ret = 0;
1131: else
1132: ret = intmtoratm2(crmat,m1,*nm,dn,stat);
1133: if ( ret ) {
1134: *rindp = rind = (int *)MALLOC_ATOMIC(row*sizeof(int));
1135: *cindp = cind = (int *)MALLOC_ATOMIC((col-row)*sizeof(int));
1136: for ( j = k = l = 0; j < col; j++ )
1137: if ( colstat[j] ) rind[k++] = j;
1138: else cind[l++] = j;
1139: return gensolve_check2(mat,*nm,dn,rind,cind);
1140: }
1141: }
1142: }
1143: }
1144:
1145: int generic_gauss_elim_direct(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp){
1146: Z **bmat,*s;
1147: Z u,v,w,x,d,t,y;
1148: int row,col,i,j,k,l,m,rank;
1149: int *colstat,*colpos,*cind;
1150: MAT r,in;
1151:
1152: row = mat->row; col = mat->col;
1153: MKMAT(in,row,col);
1154: for ( i = 0; i < row; i++ )
1155: for ( j = 0; j < col; j++ ) in->body[i][j] = mat->body[i][j];
1156: bmat = (Z **)in->body;
1157: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1158: *rindp = colpos = (int *)MALLOC_ATOMIC(row*sizeof(int));
1159: for ( j = 0, rank = 0, d = ONE; j < col; j++ ) {
1160: for ( i = rank; i < row && !bmat[i][j]; i++ );
1161: if ( i == row ) { colstat[j] = 0; continue; }
1162: else { colstat[j] = 1; colpos[rank] = j; }
1163: if ( i != rank )
1164: for ( k = j; k < col; k++ ) {
1165: t = bmat[i][k]; bmat[i][k] = bmat[rank][k]; bmat[rank][k] = t;
1166: }
1167: for ( i = rank+1, v = bmat[rank][j]; i < row; i++ )
1168: for ( k = j, u = bmat[i][j]; k < col; k++ ) {
1169: mulz(bmat[i][k],v,&w); mulz(bmat[rank][k],u,&x);
1170: subz(w,x,&y); divsz(y,d,&bmat[i][k]);
1171: }
1172: d = v; rank++;
1173: }
1174: *dn = d;
1175: s = (Z *)MALLOC(col*sizeof(Z));
1176: for ( i = rank-1; i >= 0; i-- ) {
1177: for ( k = colpos[i]; k < col; k++ ) mulz(bmat[i][k],d,&s[k]);
1178: for ( m = rank-1; m > i; m-- ) {
1179: for ( k = colpos[m], u = bmat[i][k]; k < col; k++ ) {
1180: mulz(bmat[m][k],u,&w); subz(s[k],w,&x); s[k] = x;
1181: }
1182: }
1183: for ( k = colpos[i], u = bmat[i][k]; k < col; k++ )
1184: divz(s[k],u,&bmat[i][k]);
1185: }
1186: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
1187: MKMAT(r,rank,col-rank); *nm = r;
1188: for ( j = 0, k = 0; j < col; j++ )
1189: if ( !colstat[j] ) {
1190: cind[k] = j;
1191: for ( i = 0; i < rank; i++ ) r->body[i][k] = bmat[i][j];
1192: k++;
1193: }
1194: return rank;
1195: }
1196:
1.8 noro 1197: int mpz_intmtoratm(mpz_t **mat,int row,int col,mpz_t md,mpz_t **nm,mpz_t dn)
1198: {
1199: mpz_t t,s,b,u,nm1,dn1;
1200: int i,j,k,l,ret;
1201: mpz_t *mi,*nmk;
1202:
1203: if ( UNIMPZ(md) )
1204: return 0;
1205: mpz_init(t); mpz_init(s); mpz_init(b); mpz_init(u);
1206: mpz_init(nm1); mpz_init(dn1);
1207: mpz_fdiv_q_2exp(t,md,1); mpz_sqrt(s,t); mpz_fdiv_q_2exp(b,s,64);
1208: if ( !mpz_sgn(b) ) mpz_set_ui(b,1);
1209: mpz_set_ui(dn,1);
1210: for ( i = 0; i < row; i++ )
1211: for ( j = 0, mi = mat[i]; j < col; j++ )
1212: if ( mpz_sgn(mi[j]) ) {
1213: mpz_mul(s,mi[j],dn);
1214: mpz_mod(u,s,md);
1215: ret = mpz_inttorat(u,md,b,nm1,dn1);
1216: if ( !ret )
1217: return 0;
1218: else {
1219: if ( !UNIMPZ(dn1) ) {
1220: for ( k = 0; k < i; k++ )
1221: for ( l = 0, nmk = nm[k]; l < col; l++ ) mpz_mul(nmk[l],nmk[l],dn1);
1222: for ( l = 0, nmk = nm[i]; l < j; l++ ) mpz_mul(nmk[l],nmk[l],dn1);
1223: }
1224: mpz_set(nm[i][j],nm1);
1225: mpz_mul(dn,dn,dn1);
1226: }
1227: }
1228: return 1;
1229: }
1230:
1.1 noro 1231: int intmtoratm(MAT mat,Z md,MAT nm,Z *dn)
1232: {
1233: Z t,s,b,dn0,dn1,nm1,q,u,unm,udn,dmy;
1234: int i,j,k,l,row,col,sgn,ret;
1235: Z **rmat,**tmat,*tmi,*nmk;
1236:
1237: if ( UNIQ(md) )
1238: return 0;
1239: row = mat->row; col = mat->col;
1240: bshiftz(md,1,&t);
1.18 noro 1241: isqrtz(t,&s);
1.1 noro 1242: bshiftz(s,64,&b);
1243: if ( !b ) b = ONE;
1244: dn0 = ONE;
1245: tmat = (Z **)mat->body;
1246: rmat = (Z **)nm->body;
1247: for ( i = 0; i < row; i++ )
1248: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1249: if ( tmi[j] ) {
1250: mulz(tmi[j],dn0,&s);
1251: divqrz(s,md,&dmy,&u);
1252: ret = inttorat(u,md,b,&nm1,&dn1);
1253: if ( !ret ) return 0;
1254: else {
1255: if ( !UNIQ(dn1) ) {
1256: for ( k = 0; k < i; k++ )
1257: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1258: mulz(nmk[l],dn1,&q); nmk[l] = q;
1259: }
1260: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1261: mulz(nmk[l],dn1,&q); nmk[l] = q;
1262: }
1263: }
1264: rmat[i][j] = nm1;
1265: mulz(dn0,dn1,&q); dn0 = q;
1266: }
1267: }
1268: *dn = dn0;
1269: return 1;
1270: }
1271:
1272: int intmtoratm2(MAT mat,Z md,MAT nm,Z *dn,int *stat)
1273: {
1274: int row,col,i,j,ret;
1275: Z dn0,dn1,t,s,b;
1276: Z *w,*tmi;
1277: Z **tmat;
1278:
1279: bshiftz(md,1,&t);
1280: isqrtz(t,&s);
1281: bshiftz(s,64,&b);
1282: tmat = (Z **)mat->body;
1283: if ( UNIQ(md) ) return 0;
1284: row = mat->row; col = mat->col;
1285: dn0 = ONE;
1286: for ( i = 0; i < row; i++ )
1287: if ( cmpz(dn[i],dn0) > 0 ) dn0 = dn[i];
1288: w = (Z *)MALLOC(col*sizeof(Z));
1289: for ( i = 0; i < row; i++ )
1290: if ( stat[i] == 0 ) {
1291: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1292: mulz(tmi[j],dn0,&w[j]);
1293: ret = intvtoratv(w,col,md,b,(Z *)BDY(nm)[i],&dn[i]);
1294: if ( ret ) {
1295: stat[i] = 1;
1296: mulz(dn0,dn[i],&t); dn[i] = t; dn0 = t;
1297: }
1298: }
1299: for ( i = 0; i < row; i++ ) if ( !stat[i] ) break;
1300: if ( i == row ) return 1;
1301: else return 0;
1302: }
1303:
1304: int intvtoratv(Z *v,int n,Z md,Z b,Z *nm,Z *dn)
1305: {
1306: Z dn0,dn1,q,s,u,nm1,unm,udn,dmy;
1307: Z *nmk;
1308: int j,l,col,ret,sgn;
1309:
1310: for ( j = 0; j < n; j++ ) nm[j] = 0;
1311: dn0 = ONE;
1312: for ( j = 0; j < n; j++ ) {
1313: if ( !v[j] ) continue;
1314: mulz(v[j],dn0,&s);
1315: divqrz(s,md,&dmy,&u);
1316: ret = inttorat(u,md,b,&nm1,&dn1);
1317: if ( !ret ) return 0;
1318: if ( !UNIQ(dn1) )
1319: for ( l = 0; l < j; l++ ) {
1320: mulz(nm[l],dn1,&q); nm[l] = q;
1321: }
1322: nm[j] = nm1;
1323: mulz(dn0,dn1,&q); dn0 = q;
1324: }
1325: *dn = dn0;
1326: return 1;
1327: }
1328:
1329: /* assuming 0 < c < m */
1330:
1.8 noro 1331: int mpz_inttorat(mpz_t c,mpz_t m,mpz_t b,mpz_t nm,mpz_t dn)
1332: {
1333: mpz_t u1,v1,u2,v2,r1,r2;
1334: mpz_t q,t;
1335:
1336: mpz_init_set_ui(u1,0); mpz_init_set_ui(v1,1);
1337: mpz_init_set(u2,m); mpz_init_set(v2,c);
1338: mpz_init(q); mpz_init(t); mpz_init(r1); mpz_init(r2);
1339: while ( mpz_cmp(v2,b) >= 0 ) {
1340: /* r2 = u2-q*v2 */
1341: mpz_fdiv_qr(q,r2,u2,v2);
1342: mpz_set(u2,v2); mpz_set(v2,r2);
1343: /* r1 = u1-q*v1 */
1344: mpz_mul(t,q,v1); mpz_sub(r1,u1,t);
1345: mpz_set(u1,v1); mpz_set(v1,r1);
1346: }
1347: if ( mpz_cmp(v1,b) >= 0 ) return 0;
1348: else {
1.14 noro 1349: mpz_gcd(t,v1,v2);
1350: if ( UNIMPZ(t) )
1351: mpz_set_ui(r1,0);
1352: else {
1353: /* v1 /= t, v2 /= t, t=c*v1-v2, r1=t%m */
1354: mpz_divexact(v1,v1,t); mpz_divexact(v2,v2,t);
1355: mpz_mul(t,c,v1); mpz_sub(t,t,v2); mpz_mod(r1,t,m);
1356: }
1357: if ( mpz_sgn(r1) ) return 0;
1.8 noro 1358: if ( mpz_sgn(v1)<0 ) {
1359: mpz_neg(dn,v1); mpz_neg(nm,v2);
1360: } else {
1361: mpz_set(dn,v1); mpz_set(nm,v2);
1362: }
1363: return 1;
1364: }
1365: }
1366:
1.1 noro 1367: int inttorat(Z c,Z m,Z b,Z *nmp,Z *dnp)
1368: {
1.14 noro 1369: Z qq,t,s,r,u1,v1,r1;
1.1 noro 1370: Z q,u2,v2,r2;
1371:
1372: u1 = 0; v1 = ONE; u2 = m; v2 = c;
1373: while ( cmpz(v2,b) >= 0 ) {
1374: divqrz(u2,v2,&q,&r2); u2 = v2; v2 = r2;
1375: mulz(q,v1,&t); subz(u1,t,&r1); u1 = v1; v1 = r1;
1376: }
1377: if ( cmpz(v1,b) >= 0 ) return 0;
1378: else {
1.14 noro 1379: /* reduction and check */
1380: /* v2/v1 = u2/u1, c*u1-u2 = 0 mod m? */
1381: gcdz(v1,v2,&t);
1382: if ( UNIZ(t) ) {
1383: u1 = v1; u2 = v2; r = 0;
1384: } else {
1385: divsz(v1,t,&u1); divsz(v2,t,&u2);
1386: mulz(c,u1,&t); subz(t,u2,&s); remz(s,m,&r);
1387: }
1388: if ( r ) return 0;
1389: if ( mpz_sgn(BDY(u1))<0 ) {
1390: chsgnz(u1,dnp); chsgnz(u2,nmp);
1.1 noro 1391: } else {
1.14 noro 1392: *dnp = u1; *nmp = u2;
1.1 noro 1393: }
1394: return 1;
1395: }
1396: }
1397:
1398: extern int f4_nocheck;
1399:
1.12 noro 1400: int mpz_gensolve_check(MAT mat,mpz_t **nm,mpz_t dn,int rank,int clen,int *rind,int *cind)
1.8 noro 1401: {
1.12 noro 1402: int row,col,i,j,k,l;
1.8 noro 1403: mpz_t t;
1404: mpz_t *w;
1405: Z *mati;
1406: mpz_t *nmk;
1407:
1408: if ( f4_nocheck ) return 1;
1.12 noro 1409: row = mat->row; col = mat->col;
1.8 noro 1410: w = (mpz_t *)MALLOC(clen*sizeof(mpz_t));
1411: mpz_init(t);
1412: for ( i = 0; i < clen; i++ ) mpz_init(w[i]);
1413: for ( i = 0; i < row; i++ ) {
1414: mati = (Z *)mat->body[i];
1415: for ( l = 0; l < clen; l++ ) mpz_set_ui(w[l],0);
1416: for ( k = 0; k < rank; k++ )
1417: for ( l = 0, nmk = (mpz_t *)nm[k]; l < clen; l++ ) {
1418: /* w[l] += mati[rind[k]]*nmk[k] */
1419: if ( mati[rind[k]] ) mpz_addmul(w[l],BDY(mati[rind[k]]),nmk[l]);
1420: }
1421: for ( j = 0; j < clen; j++ ) {
1422: if ( mati[cind[j]] ) mpz_mul(t,dn,BDY(mati[cind[j]]));
1423: else mpz_set_ui(t,0);
1424: if ( mpz_cmp(w[j],t) ) break;
1425: }
1426: if ( j != clen ) break;
1427: }
1428: if ( i != row ) return 0;
1429: else return 1;
1430: }
1431:
1.1 noro 1432: int gensolve_check(MAT mat,MAT nm,Z dn,int *rind,int *cind)
1433: {
1434: int row,col,rank,clen,i,j,k,l;
1435: Z s,t;
1436: Z *w;
1437: Z *mati,*nmk;
1438:
1439: if ( f4_nocheck ) return 1;
1440: row = mat->row; col = mat->col; rank = nm->row; clen = nm->col;
1441: w = (Z *)MALLOC(clen*sizeof(Z));
1442: for ( i = 0; i < row; i++ ) {
1443: mati = (Z *)mat->body[i];
1444: bzero(w,clen*sizeof(Z));
1445: for ( k = 0; k < rank; k++ )
1446: for ( l = 0, nmk = (Z *)nm->body[k]; l < clen; l++ ) {
1447: mulz(mati[rind[k]],nmk[l],&t); addz(w[l],t,&s); w[l] = s;
1448: }
1449: for ( j = 0; j < clen; j++ ) {
1450: mulz(dn,mati[cind[j]],&t);
1451: if ( cmpz(w[j],t) ) break;
1452: }
1453: if ( j != clen ) break;
1454: }
1455: if ( i != row ) return 0;
1456: else return 1;
1457: }
1458:
1459: int gensolve_check2(MAT mat,MAT nm,Z *dn,int *rind,int *cind)
1460: {
1461: int row,col,rank,clen,i,j,k,l;
1462: Z s,t,u,d;
1463: Z *w,*m;
1464: Z *mati,*nmk;
1465:
1466: if ( f4_nocheck ) return 1;
1467: row = mat->row; col = mat->col; rank = nm->row; clen = nm->col;
1468: w = (Z *)MALLOC(clen*sizeof(Z));
1469: m = (Z *)MALLOC(clen*sizeof(Z));
1470: for ( d = dn[0], i = 1; i < rank; i++ ) {
1471: lcmz(d,dn[i],&t); d = t;
1472: }
1473: for ( i = 0; i < rank; i++ ) divsz(d,dn[i],&m[i]);
1474: for ( i = 0; i < row; i++ ) {
1475: mati = (Z *)mat->body[i];
1476: bzero(w,clen*sizeof(Z));
1477: for ( k = 0; k < rank; k++ ) {
1478: mulz(mati[rind[k]],m[k],&u);
1479: for ( l = 0, nmk = (Z *)nm->body[k]; l < clen; l++ ) {
1480: mulz(u,nmk[l],&t); addz(w[l],t,&s); w[l] = s;
1481: }
1482: }
1483: for ( j = 0; j < clen; j++ ) {
1484: mulz(d,mati[cind[j]],&t);
1485: if ( cmpz(w[j],t) ) break;
1486: }
1487: if ( j != clen ) break;
1488: }
1489: if ( i != row ) return 0;
1490: else return 1;
1491: }
1492:
1493: void isqrtz(Z a,Z *r)
1494: {
1495: int k;
1496: Z x,t,x2,xh,quo,rem;
1497: Z two;
1498:
1499: if ( !a ) *r = 0;
1.11 noro 1500: else if ( UNIZ(a) ) *r = ONE;
1.1 noro 1501: else {
1502: k = z_bits((Q)a); /* a <= 2^k-1 */
1503: bshiftz(ONE,-((k>>1)+(k&1)),&x); /* a <= x^2 */
1.5 noro 1504: STOZ(2,two);
1.1 noro 1505: while ( 1 ) {
1506: pwrz(x,two,&t);
1507: if ( cmpz(t,a) <= 0 ) {
1508: *r = x; return;
1509: } else {
1510: if ( mpz_tstbit(BDY(x),0) ) addz(x,a,&t);
1511: else t = a;
1512: bshiftz(x,-1,&x2); divqrz(t,x2,&quo,&rem);
1513: bshiftz(x,1,&xh); addz(quo,xh,&x);
1514: }
1515: }
1516: }
1517: }
1518:
1519: void bshiftz(Z a,int n,Z *r)
1520: {
1521: mpz_t t;
1522:
1523: if ( !a ) *r = 0;
1524: else if ( n == 0 ) *r = a;
1525: else if ( n < 0 ) {
1526: mpz_init(t); mpz_mul_2exp(t,BDY(a),-n); MPZTOZ(t,*r);
1527: } else {
1528: mpz_init(t); mpz_fdiv_q_2exp(t,BDY(a),n);
1529: if ( !mpz_sgn(t) ) *r = 0;
1530: else MPZTOZ(t,*r);
1531: }
1532: }
1533:
1534: void addlf(Z a,Z b,Z *c)
1535: {
1536: addz(a,b,c);
1537: if ( !lf_lazy ) {
1538: if ( cmpz(*c,current_mod_lf) >= 0 ) {
1539: subz(*c,current_mod_lf,c);
1540: }
1541: }
1542: }
1543:
1544: void sublf(Z a,Z b,Z *c)
1545: {
1546: subz(a,b,c);
1547: if ( !lf_lazy ) {
1548: remz(*c,current_mod_lf,c);
1549: }
1550: }
1551:
1552: void mullf(Z a,Z b,Z *c)
1553: {
1554: mulz(a,b,c);
1555: if ( !lf_lazy ) {
1556: remz(*c,current_mod_lf,c);
1557: }
1558: }
1559:
1560: void divlf(Z a,Z b,Z *c)
1561: {
1562: Z inv;
1563:
1564: invz(b,current_mod_lf,&inv);
1565: mulz(a,inv,c);
1566: if ( !lf_lazy ) {
1567: remz(*c,current_mod_lf,c);
1568: }
1569: }
1570:
1571: void chsgnlf(Z a,Z *c)
1572: {
1573: chsgnz(a,c);
1574: if ( !lf_lazy ) {
1575: remz(*c,current_mod_lf,c);
1576: }
1577: }
1578:
1579: void lmtolf(LM a,Z *b)
1580: {
1581: if ( !a ) *b = 0;
1582: else {
1583: MPZTOZ(BDY(a),*b);
1584: }
1585: }
1586:
1587: void setmod_lf(Z p)
1588: {
1589: current_mod_lf = p;
1590: current_mod_lf_size = mpz_size(BDY(current_mod_lf))+1;
1591: }
1592:
1593: void simplf_force(Z a,Z *b)
1594: {
1595: remz(a,current_mod_lf,b);
1596: }
1597:
1598: int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Z *dn,int **rindp,int **cindp)
1599: {
1600: MAT bmat,xmat;
1601: Z **a0,**a,**b,**x,**nm;
1602: Z *ai,*bi,*xi;
1603: int row,col;
1604: int **w;
1605: int *wi;
1606: int **wc;
1607: Z mdq,q,s,u;
1608: Z tn;
1609: int ind,md,i,j,k,l,li,ri,rank;
1610: unsigned int t;
1611: int *cinfo,*rinfo;
1612: int *rind,*cind;
1613: int count;
1614: int ret;
1.3 noro 1615: struct oEGT eg_mul1,eg_mul2,tmp0,tmp1,tmp2;
1.1 noro 1616: int period;
1617: int *wx,*ptr;
1618: int wxsize,nsize;
1619: Z wn;
1620: Z wq;
1621:
1.9 noro 1622: #if SIZEOF_LONG == 8
1.11 noro 1623: return generic_gauss_elim_hensel64(mat,nmmat,dn,rindp,cindp,0);
1.9 noro 1624: #endif
1.3 noro 1625: init_eg(&eg_mul1); init_eg(&eg_mul2);
1.1 noro 1626: a0 = (Z **)mat->body;
1627: row = mat->row; col = mat->col;
1628: w = (int **)almat(row,col);
1629: for ( ind = 0; ; ind++ ) {
1630: md = get_lprime(ind);
1.5 noro 1631: STOZ(md,mdq);
1.1 noro 1632: for ( i = 0; i < row; i++ )
1633: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1634: wi[j] = remqi((Q)ai[j],md);
1635:
1636: if ( DP_Print > 3 ) {
1637: fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
1638: }
1639: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1640: if ( DP_Print > 3 ) {
1641: fprintf(asir_out,"done.\n"); fflush(asir_out);
1642: }
1643: a = (Z **)almat_pointer(rank,rank); /* lhs mat */
1644: MKMAT(bmat,rank,col-rank); b = (Z **)bmat->body; /* lhs mat */
1645: for ( j = li = ri = 0; j < col; j++ )
1646: if ( cinfo[j] ) {
1647: /* the column is in lhs */
1648: for ( i = 0; i < rank; i++ ) {
1649: w[i][li] = w[i][j];
1650: a[i][li] = a0[rinfo[i]][j];
1651: }
1652: li++;
1653: } else {
1654: /* the column is in rhs */
1655: for ( i = 0; i < rank; i++ )
1656: b[i][ri] = a0[rinfo[i]][j];
1657: ri++;
1658: }
1659:
1660: /* solve Ax=B; A: rank x rank, B: rank x ri */
1661: /* algorithm
1662: c <- B
1663: x <- 0
1664: q <- 1
1665: do
1666: t <- A^(-1)c mod p
1667: x <- x+qt
1668: c <- (c-At)/p
1669: q <- qp
1670: end do
1671: then Ax-B=0 mod q and b=(B-Ax)/q hold after "do".
1672: */
1673: MKMAT(xmat,rank,ri); x = (Z **)(xmat)->body;
1674: MKMAT(*nmmat,rank,ri); nm = (Z **)(*nmmat)->body;
1675: wc = (int **)almat(rank,ri);
1676: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1677: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
1678:
1679: period = F4_INTRAT_PERIOD;
1680: for ( q = ONE, count = 0; ; ) {
1.3 noro 1681: /* check Ax=B mod q */
1.1 noro 1682: if ( DP_Print > 3 )
1683: fprintf(stderr,"o");
1684: /* wc = b mod md */
1685: for ( i = 0; i < rank; i++ )
1.3 noro 1686: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1.1 noro 1687: wi[j] = remqi((Q)bi[j],md);
1.3 noro 1688: /* wc = A^(-1)wc; wc is not normalized */
1689: solve_by_lu_mod(w,rank,md,wc,ri,0);
1.1 noro 1690: /* x += q*wc */
1.3 noro 1691: get_eg(&tmp0);
1.1 noro 1692: for ( i = 0; i < rank; i++ )
1693: for ( j = 0, wi = wc[i]; j < ri; j++ ) mul1addtoz(q,wi[j],&x[i][j]);
1.3 noro 1694: /* b =(b-A*wc)/md */
1695: get_eg(&tmp1); add_eg(&eg_mul1,&tmp0,&tmp1);
1.1 noro 1696: for ( i = 0; i < rank; i++ )
1697: for ( j = 0; j < ri; j++ ) {
1.3 noro 1698: mpz_t uz;
1699:
1700: if ( b[i][j] )
1701: mpz_init_set(uz,BDY(b[i][j]));
1702: else
1703: mpz_init_set_ui(uz,0);
1704: for ( k = 0; k < rank; k++ ) {
1705: if ( a[i][k] && wc[k][j] ) {
1706: if ( wc[k][j] < 0 )
1707: mpz_addmul_ui(uz,BDY(a[i][k]),-wc[k][j]);
1708: else
1709: mpz_submul_ui(uz,BDY(a[i][k]),wc[k][j]);
1710: }
1711: }
1712: MPZTOZ(uz,u);
1.1 noro 1713: divsz(u,mdq,&b[i][j]);
1714: }
1.3 noro 1715: get_eg(&tmp2); add_eg(&eg_mul2,&tmp1,&tmp2);
1.1 noro 1716: count++;
1717: /* q = q*md */
1718: mulz(q,mdq,&u); q = u;
1719: if ( count == period ) {
1720: ret = intmtoratm(xmat,q,*nmmat,dn);
1721: if ( ret ) {
1.3 noro 1722: print_eg("MUL1",&eg_mul1);
1723: print_eg("MUL2",&eg_mul2);
1.1 noro 1724: for ( j = k = l = 0; j < col; j++ )
1725: if ( cinfo[j] )
1726: rind[k++] = j;
1727: else
1728: cind[l++] = j;
1729: ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
1730: if ( ret ) {
1731: *rindp = rind;
1732: *cindp = cind;
1733: for ( j = k = 0; j < col; j++ )
1734: if ( !cinfo[j] )
1735: cind[k++] = j;
1736: return rank;
1.11 noro 1737: } else
1738: goto reset;
1.1 noro 1739: } else {
1.11 noro 1740: reset:
1.1 noro 1741: period = period*3/2;
1742: count = 0;
1743: }
1744: }
1745: }
1746: }
1747: }
1748:
1749: /* for inv_or_split_dalg */
1750:
1751: int generic_gauss_elim_hensel_dalg(MAT mat,DP *mb,MAT *nmmat,Z *dn,int **rindp,int **cindp)
1752: {
1753: MAT bmat,xmat;
1754: Z **a0,**a,**b,**x,**nm;
1755: Z *ai,*bi,*xi;
1756: int row,col;
1757: int **w;
1758: int *wi;
1759: int **wc;
1760: Z mdq,q,s,u;
1761: Z tn;
1762: int ind,md,i,j,k,l,li,ri,rank;
1763: unsigned int t;
1764: int *cinfo,*rinfo;
1765: int *rind,*cind;
1766: int count;
1767: int ret;
1768: struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1;
1769: int period;
1770: int *wx,*ptr;
1771: int wxsize,nsize;
1772: Z wn;
1773: Z wq;
1774: DP m;
1775:
1.11 noro 1776: #if SIZEOF_LONG == 8
1777: return generic_gauss_elim_hensel64(mat,nmmat,dn,rindp,cindp,mb);
1778: #endif
1.1 noro 1779: a0 = (Z **)mat->body;
1780: row = mat->row; col = mat->col;
1781: w = (int **)almat(row,col);
1782: for ( ind = 0; ; ind++ ) {
1783: md = get_lprime(ind);
1.5 noro 1784: STOZ(md,mdq);
1.1 noro 1785: for ( i = 0; i < row; i++ )
1786: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1787: wi[j] = remqi((Q)ai[j],md);
1788:
1789: if ( DP_Print > 3 ) {
1790: fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
1791: }
1792: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1793: if ( DP_Print > 3 ) {
1794: fprintf(asir_out,"done.\n"); fflush(asir_out);
1795: }
1796:
1797: /* this part is added for inv_or_split_dalg */
1798: for ( i = 0; i < col-1; i++ ) {
1799: if ( !cinfo[i] ) {
1800: m = mb[i];
1801: for ( j = i+1; j < col-1; j++ )
1802: if ( dp_redble(mb[j],m) )
1803: cinfo[j] = -1;
1804: }
1805: }
1806:
1807: a = (Z **)almat_pointer(rank,rank); /* lhs mat */
1808: MKMAT(bmat,rank,col-rank); b = (Z **)bmat->body; /* lhs mat */
1809: for ( j = li = ri = 0; j < col; j++ )
1.4 noro 1810: if ( cinfo[j] > 0 ) {
1.1 noro 1811: /* the column is in lhs */
1812: for ( i = 0; i < rank; i++ ) {
1813: w[i][li] = w[i][j];
1814: a[i][li] = a0[rinfo[i]][j];
1815: }
1816: li++;
1.4 noro 1817: } else if ( !cinfo[j] ) {
1.1 noro 1818: /* the column is in rhs */
1819: for ( i = 0; i < rank; i++ )
1820: b[i][ri] = a0[rinfo[i]][j];
1821: ri++;
1822: }
1823:
1824: /* solve Ax=B; A: rank x rank, B: rank x ri */
1825: /* algorithm
1826: c <- B
1827: x <- 0
1828: q <- 1
1829: do
1830: t <- A^(-1)c mod p
1831: x <- x+qt
1832: c <- (c-At)/p
1833: q <- qp
1834: end do
1835: then Ax-B=0 mod q and b=(B-Ax)/q hold after "do".
1836: */
1837: MKMAT(xmat,rank,ri); x = (Z **)(xmat)->body;
1838: MKMAT(*nmmat,rank,ri); nm = (Z **)(*nmmat)->body;
1839: wc = (int **)almat(rank,ri);
1840: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1841: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
1842:
1843: period = F4_INTRAT_PERIOD;
1844: for ( q = ONE, count = 0; ; ) {
1845: if ( DP_Print > 3 )
1846: fprintf(stderr,"o");
1847: /* wc = b mod md */
1848: for ( i = 0; i < rank; i++ )
1.3 noro 1849: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1.1 noro 1850: wi[j] = remqi((Q)bi[j],md);
1.11 noro 1851: /* wc = A^(-1)wc; wc is not normalized */
1852: solve_by_lu_mod(w,rank,md,wc,ri,0);
1.1 noro 1853: /* x += q*wc */
1854: for ( i = 0; i < rank; i++ )
1855: for ( j = 0, wi = wc[i]; j < ri; j++ ) mul1addtoz(q,wi[j],&x[i][j]);
1.3 noro 1856: /* b =(b-A*wc)/md */
1.1 noro 1857: for ( i = 0; i < rank; i++ )
1858: for ( j = 0; j < ri; j++ ) {
1.3 noro 1859: mpz_t uz;
1860:
1861: if ( b[i][j] )
1862: mpz_init_set(uz,BDY(b[i][j]));
1863: else
1864: mpz_init_set_ui(uz,0);
1.11 noro 1865: for ( k = 0; k < rank; k++ )
1866: if ( a[i][k] && wc[k][j] )
1.3 noro 1867: mpz_submul_ui(uz,BDY(a[i][k]),wc[k][j]);
1868: MPZTOZ(uz,u);
1.1 noro 1869: divsz(u,mdq,&b[i][j]);
1870: }
1.11 noro 1871:
1.1 noro 1872: count++;
1873: /* q = q*md */
1874: mulz(q,mdq,&u); q = u;
1875: if ( count == period ) {
1876: ret = intmtoratm(xmat,q,*nmmat,dn);
1877: if ( ret ) {
1878: for ( j = k = l = 0; j < col; j++ )
1879: if ( cinfo[j] > 0 )
1880: rind[k++] = j;
1881: else if ( !cinfo[j] )
1882: cind[l++] = j;
1883: ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
1884: if ( ret ) {
1885: *rindp = rind;
1886: *cindp = cind;
1887: for ( j = k = 0; j < col; j++ )
1888: if ( !cinfo[j] )
1889: cind[k++] = j;
1890: return rank;
1.11 noro 1891: } else
1892: goto reset;
1.1 noro 1893: } else {
1.11 noro 1894: reset:
1.1 noro 1895: period = period*3/2;
1896: count = 0;
1897: }
1898: }
1899: }
1900: }
1901: }
1.6 noro 1902:
1903: #if SIZEOF_LONG == 8
1904: mp_limb_t remqi64(Q a,mp_limb_t mod)
1905: {
1906: mp_limb_t c,nm,dn;
1907: mpz_t r;
1908:
1909: if ( !a ) return 0;
1910: else if ( a->z ) {
1911: mpz_init(r);
1912: c = mpz_fdiv_r_ui(r,BDY((Z)a),mod);
1913: } else {
1914: mpz_init(r);
1915: nm = mpz_fdiv_r_ui(r,mpq_numref(BDY(a)),mod);
1916: dn = mpz_fdiv_r_ui(r,mpq_denref(BDY(a)),mod);
1917: dn = invmod64(dn,mod);
1918: c = mulmod64(nm,dn,mod);
1919: }
1920: return c;
1921: }
1922:
1923: int generic_gauss_elim_mod64(mp_limb_t **mat,int row,int col,mp_limb_t md,int *colstat);
1924: mp_limb_t get_lprime64(int ind);
1925:
1.8 noro 1926: void mpz_print(mpz_t a)
1927: {
1928: mpz_out_str(stdout,10,a); printf("\n");
1929: }
1930:
1931: void mpz_printmat(mpz_t **a,int row,int col)
1932: {
1933: int i,j;
1934: for ( i = 0; i < row; i++ ) {
1935: for ( j = 0; j < col; j++ ) {
1936: mpz_out_str(stdout,10,a[i][j]); printf(" ");
1937: }
1938: printf("\n");
1939: }
1940: }
1941:
1942: mpz_t **mpz_allocmat(int row,int col)
1943: {
1944: mpz_t **p;
1945: int i,j;
1946:
1947: p = (mpz_t **)MALLOC(row*sizeof(mpz_t *));
1948: for ( i = 0; i < row; i++ ) {
1949: p[i] = (mpz_t *)MALLOC(col*sizeof(mpz_t));
1950: for ( j = 0; j < col; j++ ) mpz_init(p[i][j]);
1951: }
1952: return p;
1953: }
1954:
1955: #if 1
1956: int generic_gauss_elim64(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp)
1957: {
1958: mp_limb_t **wmat;
1959: mp_limb_t *wmi;
1960: mp_limb_t md,inv,t,t1;
1961: Z z;
1962: Z **bmat,*bmi;
1963: mpz_t **tmat,**num;
1964: mpz_t *tmi;
1965: mpz_t den;
1966: mpz_t q,m1,m3,s,u;
1967: int *colstat,*wcolstat,*rind,*cind;
1968: int row,col,ind,i,j,k,l,rank,rank0;
1969: MAT r;
1970: int ret;
1971:
1972: bmat = (Z **)mat->body;
1973: row = mat->row; col = mat->col;
1974: wmat = (mp_limb_t **)almat64(row,col);
1975: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1976: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1977: mpz_init(m1); mpz_init(m3); mpz_init(den);
1978: for ( ind = 0; ; ind++ ) {
1979: if ( DP_Print ) {
1980: fprintf(asir_out,"."); fflush(asir_out);
1981: }
1982: md = get_lprime64(ind);
1983: for ( i = 0; i < row; i++ )
1984: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
1985: wmi[j] = bmi[j]==0?0:mpz_fdiv_ui(BDY(bmi[j]),md);
1986: rank = generic_gauss_elim_mod64(wmat,row,col,md,wcolstat);
1987: if ( !ind ) {
1988: RESET:
1989: mpz_set_ui(m1,md);
1990: rank0 = rank;
1991: bcopy(wcolstat,colstat,col*sizeof(int));
1992: // crmat
1993: tmat = mpz_allocmat(rank,col-rank);
1994: //
1995: num = mpz_allocmat(rank,col-rank);
1996: for ( i = 0; i < rank; i++ )
1997: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
1998: if ( !colstat[j] ) { mpz_set_ui(tmi[k],wmi[j]); k++; }
1999: } else {
2000: if ( rank < rank0 ) {
2001: if ( DP_Print ) {
2002: fprintf(asir_out,"lower rank matrix; continuing...\n");
2003: fflush(asir_out);
2004: }
2005: continue;
2006: } else if ( rank > rank0 ) {
2007: if ( DP_Print ) {
2008: fprintf(asir_out,"higher rank matrix; resetting...\n");
2009: fflush(asir_out);
2010: }
2011: goto RESET;
2012: } else {
2013: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
2014: if ( j < col ) {
2015: if ( DP_Print ) {
2016: fprintf(asir_out,"inconsitent colstat; resetting...\n");
2017: fflush(asir_out);
2018: }
2019: goto RESET;
2020: }
2021: }
2022:
2023: inv = invmod64(mpz_fdiv_ui(m1,md),md);
2024: mpz_mul_ui(m3,m1,md);
2025: for ( i = 0; i < rank; i++ )
2026: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
2027: if ( !colstat[j] ) {
2028: if ( mpz_sgn(tmi[k]) ) {
2029: /* f3 = f1+m1*(m1 mod md)^(-1)*(f2 - f1 mod md) */
2030: t = mpz_fdiv_ui(tmi[k],md);
2031: if ( wmi[j] >= t ) t = wmi[j]-t;
2032: else t = md-(t-wmi[j]);
2033: mpz_addmul_ui(tmi[k],m1,mulmod64(t,inv,md));
2034: } else if ( wmi[j] ) {
2035: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
2036: mpz_mul_ui(tmi[k],m1,mulmod64(wmi[j],inv,md));
2037: }
2038: k++;
2039: }
2040: mpz_set(m1,m3);
2041: if ( ind % F4_INTRAT_PERIOD )
2042: ret = 0;
2043: else
2044: ret = mpz_intmtoratm(tmat,rank,col-rank,m1,num,den);
2045: if ( ret ) {
2046: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
2047: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
2048: for ( j = k = l = 0; j < col; j++ )
2049: if ( colstat[j] ) rind[k++] = j;
2050: else cind[l++] = j;
1.12 noro 2051: if ( mpz_gensolve_check(mat,num,den,rank,col-rank,rind,cind) ) {
1.8 noro 2052: MKMAT(r,rank,col-rank); *nm = r;
2053: for ( i = 0; i < rank; i++ )
2054: for ( j = 0; j < col-rank; j++ ) {
2055: MPZTOZ(num[i][j],z); BDY(r)[i][j] = z;
2056: }
2057: MPZTOZ(den,*dn);
2058: return rank;
2059: }
2060: }
2061: }
2062: }
2063: }
2064: #else
1.6 noro 2065: int generic_gauss_elim64(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp)
2066: {
2067: mp_limb_t **wmat;
2068: mp_limb_t *wmi;
2069: mp_limb_t md,inv,t,t1;
2070: Z **bmat,**tmat,*bmi,*tmi;
2071: Z q,m1,m2,m3,s,u;
2072: int *colstat,*wcolstat,*rind,*cind;
2073: int row,col,ind,i,j,k,l,rank,rank0;
2074: MAT r,crmat;
2075: int ret;
2076:
2077: bmat = (Z **)mat->body;
2078: row = mat->row; col = mat->col;
2079: wmat = (mp_limb_t **)almat64(row,col);
2080: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
2081: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
2082: for ( ind = 0; ; ind++ ) {
2083: if ( DP_Print ) {
2084: fprintf(asir_out,"."); fflush(asir_out);
2085: }
2086: md = get_lprime64(ind);
2087: for ( i = 0; i < row; i++ )
2088: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
2089: wmi[j] = remqi64((Q)bmi[j],md);
2090: rank = generic_gauss_elim_mod64(wmat,row,col,md,wcolstat);
2091: if ( !ind ) {
2092: RESET:
2093: UTOZ(md,m1);
2094: rank0 = rank;
2095: bcopy(wcolstat,colstat,col*sizeof(int));
2096: MKMAT(crmat,rank,col-rank);
2097: MKMAT(r,rank,col-rank); *nm = r;
2098: tmat = (Z **)crmat->body;
2099: for ( i = 0; i < rank; i++ )
2100: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
2101: if ( !colstat[j] ) { UTOZ(wmi[j],tmi[k]); k++; }
2102: } else {
2103: if ( rank < rank0 ) {
2104: if ( DP_Print ) {
2105: fprintf(asir_out,"lower rank matrix; continuing...\n");
2106: fflush(asir_out);
2107: }
2108: continue;
2109: } else if ( rank > rank0 ) {
2110: if ( DP_Print ) {
2111: fprintf(asir_out,"higher rank matrix; resetting...\n");
2112: fflush(asir_out);
2113: }
2114: goto RESET;
2115: } else {
2116: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
2117: if ( j < col ) {
2118: if ( DP_Print ) {
2119: fprintf(asir_out,"inconsitent colstat; resetting...\n");
2120: fflush(asir_out);
2121: }
2122: goto RESET;
2123: }
2124: }
2125:
2126: inv = invmod64(remqi64((Q)m1,md),md);
2127: UTOZ(md,m2); mulz(m1,m2,&m3);
2128: for ( i = 0; i < rank; i++ )
2129: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
2130: if ( !colstat[j] ) {
2131: if ( tmi[k] ) {
2132: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
2133: t = remqi64((Q)tmi[k],md);
2134: if ( wmi[j] >= t ) t = wmi[j]-t;
2135: else t = md-(t-wmi[j]);
2136: t1 = mulmod64(t,inv,md);
2137: UTOZ(t1,u); mulz(m1,u,&s);
2138: addz(tmi[k],s,&u); tmi[k] = u;
2139: } else if ( wmi[j] ) {
2140: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
2141: t = mulmod64(wmi[j],inv,md);
2142: UTOZ(t,u); mulz(m1,u,&s); tmi[k] = s;
2143: }
2144: k++;
2145: }
2146: m1 = m3;
2147: if ( ind % F4_INTRAT_PERIOD )
2148: ret = 0;
2149: else
2150: ret = intmtoratm(crmat,m1,*nm,dn);
2151: if ( ret ) {
2152: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
2153: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
2154: for ( j = k = l = 0; j < col; j++ )
2155: if ( colstat[j] ) rind[k++] = j;
2156: else cind[l++] = j;
2157: if ( gensolve_check(mat,*nm,*dn,rind,cind) )
2158: return rank;
2159: }
2160: }
2161: }
2162: }
2163: #endif
1.8 noro 2164:
1.11 noro 2165: int generic_gauss_elim_hensel64(MAT mat,MAT *nmmat,Z *dn,int **rindp,int **cindp,DP *mb)
1.9 noro 2166: {
2167: MAT r;
2168: Z z;
2169: Z **a0;
2170: Z *ai;
2171: mpz_t **a,**b,**x,**nm;
2172: mpz_t *bi,*xi;
2173: mpz_t q,u,den;
2174: mp_limb_t **w;
2175: mp_limb_t *wi;
2176: mp_limb_t **wc;
2177: mp_limb_t md;
2178: int row,col;
2179: int ind,i,j,k,l,li,ri,rank;
2180: int *cinfo,*rinfo;
2181: int *rind,*cind;
2182: int count;
2183: int ret;
2184: int period;
1.11 noro 2185: DP m;
1.9 noro 2186:
2187: a0 = (Z **)mat->body;
2188: row = mat->row; col = mat->col;
2189: w = (mp_limb_t **)almat64(row,col);
2190: for ( ind = 0; ; ind++ ) {
2191: md = get_lprime64(ind);
2192: for ( i = 0; i < row; i++ )
2193: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
2194: wi[j] = remqi64((Q)ai[j],md);
2195:
2196: if ( DP_Print > 3 ) {
2197: fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
2198: }
2199: rank = find_lhs_and_lu_mod64(w,row,col,md,&rinfo,&cinfo);
2200: if ( DP_Print > 3 ) {
2201: fprintf(asir_out,"done.\n"); fflush(asir_out);
2202: }
1.11 noro 2203:
2204: if ( mb ) {
2205: /* this part is added for inv_or_split_dalg */
2206: for ( i = 0; i < col-1; i++ ) {
2207: if ( !cinfo[i] ) {
2208: m = mb[i];
2209: for ( j = i+1; j < col-1; j++ )
2210: if ( dp_redble(mb[j],m) )
2211: cinfo[j] = -1;
2212: }
2213: }
2214: }
2215:
1.9 noro 2216: a = (mpz_t **)mpz_allocmat(rank,rank); /* lhs mat */
2217: b = (mpz_t **)mpz_allocmat(rank,col-rank);
2218: for ( j = li = ri = 0; j < col; j++ )
1.11 noro 2219: if ( cinfo[j] > 0 ) {
1.9 noro 2220: /* the column is in lhs */
2221: for ( i = 0; i < rank; i++ ) {
2222: w[i][li] = w[i][j];
2223: if ( a0[rinfo[i]][j] )
2224: mpz_set(a[i][li],BDY(a0[rinfo[i]][j]));
2225: else
2226: mpz_set_ui(a[i][li],0);
2227: }
2228: li++;
1.11 noro 2229: } else if ( !cinfo[j] ) {
1.9 noro 2230: /* the column is in rhs */
2231: for ( i = 0; i < rank; i++ ) {
2232: if ( a0[rinfo[i]][j] )
2233: mpz_set(b[i][ri],BDY(a0[rinfo[i]][j]));
2234: else
2235: mpz_set_ui(b[i][ri],0);
2236: }
2237: ri++;
2238: }
2239:
2240: /* solve Ax=B; A: rank x rank, B: rank x ri */
2241: /* algorithm
2242: c <- B
2243: x <- 0
2244: q <- 1
2245: do
2246: t <- A^(-1)c mod p
2247: x <- x+qt
2248: c <- (c-At)/p
2249: q <- qp
2250: end do
2251: then Ax-B=0 mod q and b=(B-Ax)/q hold after "do".
2252: */
2253: x = (mpz_t **)mpz_allocmat(rank,ri);
2254: nm = (mpz_t **)mpz_allocmat(rank,ri);
2255: wc = (mp_limb_t **)almat64(rank,ri);
2256: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
2257: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
2258:
2259: period = F4_INTRAT_PERIOD;
2260: mpz_init_set_ui(q,1);
2261: mpz_init(u);
2262: mpz_init(den);
2263: for ( count = 0; ; ) {
2264: /* check Ax=B mod q */
2265: if ( DP_Print > 3 )
2266: fprintf(stderr,"o");
2267: /* wc = b mod md */
2268: for ( i = 0; i < rank; i++ )
2269: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
2270: wi[j] = mpz_fdiv_ui(bi[j],md);
2271: /* wc = A^(-1)wc; wc is not normalized */
1.19 ! noro 2272: solve_by_lu_mod64(w,rank,md,(mp_limb_signed_t **)wc,ri,0);
1.9 noro 2273: /* x += q*wc */
2274: for ( i = 0; i < rank; i++ )
2275: for ( j = 0, wi = wc[i]; j < ri; j++ )
2276: if ( wi[j] > 0 )
2277: mpz_addmul_ui(x[i][j],q,wi[j]);
2278: else if ( wi[j] < 0 )
2279: mpz_submul_ui(x[i][j],q,-wi[j]);
2280: /* b =(b-A*wc)/md */
2281: for ( i = 0; i < rank; i++ )
2282: for ( j = 0; j < ri; j++ ) {
2283: mpz_set(u,b[i][j]);
2284: for ( k = 0; k < rank; k++ ) {
2285: if ( a[i][k] && wc[k][j] ) {
2286: if ( wc[k][j] < 0 )
2287: mpz_addmul_ui(u,a[i][k],-wc[k][j]);
2288: else
2289: mpz_submul_ui(u,a[i][k],wc[k][j]);
2290: }
2291: }
2292: mpz_divexact_ui(b[i][j],u,md);
2293: }
2294: count++;
2295: /* q = q*md */
2296: mpz_mul_ui(q,q,md);
2297: if ( count == period ) {
2298: ret = mpz_intmtoratm(x,rank,ri,q,nm,den);
2299: if ( ret ) {
2300: for ( j = k = l = 0; j < col; j++ )
1.11 noro 2301: if ( cinfo[j] > 0 )
1.9 noro 2302: rind[k++] = j;
1.11 noro 2303: else if ( !cinfo[j] )
1.9 noro 2304: cind[l++] = j;
1.12 noro 2305: ret = mpz_gensolve_check(mat,nm,den,rank,ri,rind,cind);
1.9 noro 2306: if ( ret ) {
2307: *rindp = rind;
2308: *cindp = cind;
2309: for ( j = k = 0; j < col; j++ )
2310: if ( !cinfo[j] )
2311: cind[k++] = j;
2312: MKMAT(r,rank,ri); *nmmat = r;
2313: for ( i = 0; i < rank; i++ )
2314: for ( j = 0; j < ri; j++ ) {
2315: MPZTOZ(nm[i][j],z); BDY(r)[i][j] = z;
2316: }
2317: MPZTOZ(den,*dn);
2318: return rank;
1.11 noro 2319: } else
2320: goto reset;
1.9 noro 2321: } else {
1.11 noro 2322: reset:
1.9 noro 2323: fprintf(stderr,"F");
2324: period = period*3/2;
2325: count = 0;
2326: }
2327: }
2328: }
2329: }
2330: }
2331:
1.8 noro 2332: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>