Annotation of OpenXM_contrib2/asir2018/engine/Q.c, Revision 1.3
1.1 noro 1: #include "ca.h"
2: #include "gmp.h"
3: #include "base.h"
4: #include "inline.h"
5:
6: mpz_t ONEMPZ;
7: Z ONE;
8: int lf_lazy;
9: Z current_mod_lf;
10: int current_mod_lf_size;
11: gmp_randstate_t GMP_RAND;
12:
13: void isqrtz(Z a,Z *r);
14: void bshiftz(Z a,int n,Z *r);
15:
16: void *gc_realloc(void *p,size_t osize,size_t nsize)
17: {
18: return (void *)Risa_GC_realloc(p,nsize);
19: }
20:
21: void gc_free(void *p,size_t size)
22: {
23: Risa_GC_free(p);
24: }
25:
26: void init_gmpq()
27: {
1.3 ! noro 28: mp_set_memory_functions(Risa_GC_malloc_atomic,gc_realloc,gc_free);
1.1 noro 29:
30: mpz_init(ONEMPZ); mpz_set_ui(ONEMPZ,1); MPZTOZ(ONEMPZ,ONE);
31: gmp_randinit_default(GMP_RAND);
32: }
33:
1.3 ! noro 34: void pmat(Z **a,int row,int col)
! 35: {
! 36: int i,j;
! 37:
! 38: for ( i = 0; i < row; i++, printf("\n") )
! 39: for ( j = 0; j < col; j++, printf(" ") )
! 40: printexpr(CO,a[i][j]);
! 41: printf("\n");
! 42: }
! 43:
1.1 noro 44: Z utoz(unsigned int u)
45: {
46: mpz_t z;
47: Z r;
48:
49: if ( !u ) return 0;
50: mpz_init(z); mpz_set_ui(z,u); MPZTOZ(z,r); return r;
51: }
52:
53: Z stoz(int s)
54: {
55: mpz_t z;
56: Z r;
57:
58: if ( !s ) return 0;
59: mpz_init(z); mpz_set_si(z,s); MPZTOZ(z,r); return r;
60: }
61:
62: int sgnz(Z z)
63: {
64: if ( !z ) return 0;
65: else return mpz_sgn(BDY(z));
66: }
67:
68: void nmq(Q q,Z *r)
69: {
70: if ( !q ) *r = 0;
71: else if ( INT(q) ) *r = (Z)q;
72: else {
73: MPZTOZ(mpq_numref(BDY(q)),*r);
74: }
75: }
76:
77: void dnq(Q q,Z *r)
78: {
79: if ( !q ) *r = 0;
80: else if ( INT(q) ) *r = ONE;
81: else {
82: MPZTOZ(mpq_denref(BDY(q)),*r);
83: }
84: }
85:
86: int sgnq(Q q)
87: {
88: if ( !q ) return 0;
89: else if ( q->z ) return mpz_sgn(BDY((Z)q));
90: else return mpz_sgn(mpq_numref(BDY(q)));
91: }
92:
93: Q mpqtozq(mpq_t a)
94: {
95: Z z;
96: Q q;
97:
98: if ( INTMPQ(a) ) {
99: MPZTOZ(mpq_numref(a),z); return (Q)z;
100: } else {
101: MPQTOQ(a,q); return q;
102: }
103: }
104:
105: void dupz(Z a,Z *b)
106: {
107: mpz_t t;
108:
109: if ( !a ) *b = a;
110: else {
111: mpz_init(t); mpz_set(t,BDY(a)); MPZTOZ(t,*b);
112: }
113: }
114:
115: int n_bits_z(Z a)
116: {
117: return a ? mpz_sizeinbase(BDY(a),2) : 0;
118: }
119:
120: void addz(Z n1,Z n2,Z *nr)
121: {
122: mpz_t t;
123: int s1,s2;
124:
125: if ( !n1 ) *nr = n2;
126: else if ( !n2 ) *nr = n1;
127: else if ( !n1->z || !n2->z )
128: error("addz : invalid argument");
129: else {
130: mpz_init(t); mpz_add(t,BDY(n1),BDY(n2)); MPZTOZ(t,*nr);
131: }
132: }
133:
134: void subz(Z n1,Z n2,Z *nr)
135: {
136: mpz_t t;
137:
138: if ( !n1 ) {
139: if ( !n2 )
140: *nr = 0;
141: else
142: chsgnz(n2,nr);
143: } else if ( !n2 )
144: *nr = n1;
145: else if ( n1 == n2 )
146: *nr = 0;
147: else if ( !n1->z || !n2->z )
148: error("subz : invalid argument");
149: else {
150: mpz_init(t); mpz_sub(t,BDY(n1),BDY(n2)); MPZTOZ(t,*nr);
151: }
152: }
153:
154: void mulz(Z n1,Z n2,Z *nr)
155: {
156: mpz_t t;
157:
158: if ( !n1 || !n2 ) *nr = 0;
159: else if ( !n1->z || !n2->z )
160: error("mulz : invalid argument");
161: else if ( UNIQ(n1) ) *nr = n2;
162: else if ( UNIQ(n2) ) *nr = n1;
163: else if ( MUNIQ(n1) ) chsgnz(n2,nr);
164: else if ( MUNIQ(n2) ) chsgnz(n1,nr);
165: else {
166: mpz_init(t); mpz_mul(t,BDY(n1),BDY(n2)); MPZTOZ(t,*nr);
167: }
168: }
169:
170: /* nr += n1*n2 */
171:
172: void muladdtoz(Z n1,Z n2,Z *nr)
173: {
1.3 ! noro 174: #if 0
1.1 noro 175: Z t;
176:
177: if ( n1 && n2 ) {
178: if ( !(*nr) ) {
179: NEWZ(t); mpz_init(BDY(t)); *nr = t;
180: }
181: mpz_addmul(BDY(*nr),BDY(n1),BDY(n2));
1.2 noro 182: if ( !mpz_sgn(BDY(*nr)) )
183: *nr = 0;
1.3 ! noro 184: }
1.2 noro 185: #else
186: Z t,s;
187:
188: mulz(n1,n2,&t); addz(*nr,t,&s); *nr = s;
189: #endif
1.1 noro 190: }
191:
192: /* nr += n1*u */
193:
194: void mul1addtoz(Z n1,long u,Z *nr)
195: {
1.3 ! noro 196: #if 0
1.1 noro 197: Z t;
198:
199: if ( n1 && u ) {
200: if ( !(*nr) ) {
201: NEWZ(t); mpz_init(BDY(t)); *nr = t;
202: }
203: if ( u >= 0 )
204: mpz_addmul_ui(BDY(*nr),BDY(n1),(unsigned long)u);
205: else
206: mpz_submul_ui(BDY(*nr),BDY(n1),(unsigned long)(-u));
1.2 noro 207: if ( !mpz_sgn(BDY(*nr)) )
208: *nr = 0;
1.1 noro 209: }
1.3 ! noro 210: #else
! 211: Z t,s;
! 212:
! 213: mul1z(n1,u,&t); addz(*nr,t,&s); *nr = s;
! 214: #endif
1.1 noro 215: }
216:
217: void mul1z(Z n1,long n2,Z *nr)
218: {
219: mpz_t t;
220:
221: if ( !n1 || !n2 ) *nr = 0;
222: else {
223: mpz_init(t); mpz_mul_si(t,BDY(n1),n2); MPZTOZ(t,*nr);
224: }
225: }
226:
227: void divz(Z n1,Z n2,Z *nq)
228: {
229: mpz_t t;
230: mpq_t a, b, q;
231:
232: if ( !n2 ) {
233: error("division by 0");
234: *nq = 0;
235: } else if ( !n1 )
236: *nq = 0;
237: else if ( n1 == n2 ) {
238: mpz_init(t); mpz_set_ui(t,1); MPZTOZ(t,*nq);
239: } else {
240: MPZTOMPQ(BDY(n1),a); MPZTOMPQ(BDY(n2),b);
241: mpq_init(q); mpq_div(q,a,b); *nq = (Z)mpqtozq(q);
242: }
243: }
244:
245: void remz(Z n1,Z n2,Z *nr)
246: {
247: mpz_t r;
248:
249: if ( !n2 ) {
250: error("division by 0");
251: *nr = 0;
252: } else if ( !n1 || n1 == n2 )
253: *nr = 0;
254: else if ( !n1->z || !n2->z )
255: error("remz : invalid argument");
256: else {
257: mpz_init(r);
258: mpz_mod(r,BDY(n1),BDY(n2));
259: if ( !mpz_sgn(r) ) *nr = 0;
260: else MPZTOZ(r,*nr);
261: }
262: }
263:
264: void divqrz(Z n1,Z n2,Z *nq,Z *nr)
265: {
266: mpz_t t, a, b, q, r;
267:
268: if ( !n2 ) {
269: error("division by 0");
270: *nq = 0; *nr = 0;
271: } else if ( !n1 ) {
272: *nq = 0; *nr = 0;
273: } else if ( !n1->z || !n2->z )
274: error("divqrz : invalid argument");
275: else if ( n1 == n2 ) {
276: mpz_init(t); mpz_set_ui(t,1); MPZTOZ(t,*nq); *nr = 0;
277: } else {
278: mpz_init(q); mpz_init(r);
279: mpz_fdiv_qr(q,r,BDY(n1),BDY(n2));
280: if ( !mpz_sgn(q) ) *nq = 0;
281: else MPZTOZ(q,*nq);
282: if ( !mpz_sgn(r) ) *nr = 0;
283: else MPZTOZ(r,*nr);
284: }
285: }
286:
287: void divsz(Z n1,Z n2,Z *nq)
288: {
289: mpz_t t;
290: mpq_t a, b, q;
291:
292: if ( !n2 ) {
293: error("division by 0");
294: *nq = 0;
295: } else if ( !n1 )
296: *nq = 0;
297: else if ( !n1->z || !n2->z )
298: error("divsz : invalid argument");
299: else if ( n1 == n2 ) {
300: mpz_init(t); mpz_set_ui(t,1); MPZTOZ(t,*nq);
301: } else {
302: mpz_init(t); mpz_divexact(t,BDY(n1),BDY(n2)); MPZTOZ(t,*nq);
303: }
304: }
305:
306: void chsgnz(Z n,Z *nr)
307: {
308: mpz_t t;
309:
310: if ( !n )
311: *nr = 0;
312: else if ( !n->z )
313: error("chsgnz : invalid argument");
314: else {
315: t[0] = BDY(n)[0]; mpz_neg(t,t); MPZTOZ(t,*nr);
316: }
317: }
318:
319: void absz(Z n,Z *nr)
320: {
321: if ( !n ) *nr = 0;
322: else if ( !n->z )
323: error("absz : invalid argument");
324: else if ( sgnz(n) < 0 ) chsgnz(n,nr);
325: else *nr = n;
326: }
327:
328: int evenz(Z n)
329: {
330: return !n ? 1 : mpz_even_p(BDY(n));
331: }
332:
333: int smallz(Z n)
334: {
335: if ( !n ) return 1;
336: else if ( INT(n) && mpz_fits_sint_p(BDY(n)) ) return 1;
337: else return 0;
338: }
339:
340: void pwrz(Z n1,Z n,Z *nr)
341: {
342: mpq_t t,q;
343: mpz_t z;
344: Q p,r;
345:
346: if ( !n || UNIQ(n1) ) *nr = ONE;
347: else if ( !n1 ) *nr = 0;
348: else if ( !n->z || !n1->z )
349: error("pwrz : invalid argument");
350: else if ( MUNIQ(n1) ) {
351: if ( mpz_even_p(BDY((Z)n)) ) *nr = ONE;
352: else *nr = n1;
353: } else if ( !smallz(n) ) {
354: error("exponent too big."); *nr = 0;
355: } else if ( n1->z && mpz_sgn(BDY((Z)n))>0 ) {
356: mpz_init(z); mpz_pow_ui(z,BDY(n1),QTOS(n)); MPZTOZ(z,*nr);
357: } else {
358: MPZTOMPQ(BDY(n1),q); MPQTOQ(q,r);
359: pwrq(r,(Q)n,&p); *nr = (Z)p;
360: }
361: }
362:
363: int cmpz(Z q1,Z q2)
364: {
365: int sgn;
366:
367: if ( !q1 ) {
368: if ( !q2 )
369: return 0;
370: else
371: return -mpz_sgn(BDY(q2));
372: } else if ( !q2 )
373: return mpz_sgn(BDY(q1));
374: else if ( !q1->z || !q2->z )
375: error("mpqz : invalid argument");
376: else if ( (sgn = mpz_sgn(BDY(q1))) != mpz_sgn(BDY(q2)) )
377: return sgn;
378: else {
379: sgn = mpz_cmp(BDY(q1),BDY(q2));
380: if ( sgn > 0 ) return 1;
381: else if ( sgn < 0 ) return -1;
382: else return 0;
383: }
384: }
385:
386: void gcdz(Z n1,Z n2,Z *nq)
387: {
388: mpz_t t;
389:
390: if ( !n1 ) *nq = n2;
391: else if ( !n2 ) *nq = n1;
392: else if ( !n1->z || !n2->z )
393: error("gcdz : invalid argument");
394: else {
395: mpz_init(t); mpz_gcd(t,BDY(n1),BDY(n2));
396: MPZTOZ(t,*nq);
397: }
398: }
399:
400: void invz(Z n1,Z n2,Z *nq)
401: {
402: mpz_t t;
403:
404: if ( !n1 || !n2 || !n1->z || !n2->z )
405: error("invz : invalid argument");
406: mpz_init(t); mpz_invert(t,BDY(n1),BDY(n2));
407: MPZTOZ(t,*nq);
408: }
409:
410: void lcmz(Z n1,Z n2,Z *nq)
411: {
412: Z g,t;
413:
414: if ( !n1 || !n2 ) *nq = 0;
415: else if ( !n1->z || !n2->z )
416: error("lcmz : invalid argument");
417: else {
418: gcdz(n1,n2,&g); divsz(n1,g,&t);
419: mulz(n2,t,nq);
420: }
421: }
422:
423: void gcdvz(VECT v,Z *q)
424: {
425: int n,i;
426: Z *b;
427: Z g,g1;
428:
429: n = v->len;
430: b = (Z *)v->body;
431: g = b[0];
432: for ( i = 1; i < n; i++ ) {
433: gcdz(g,b[i],&g1); g = g1;
434: }
435: *q = g;
436: }
437:
438: void gcdvz_estimate(VECT v,Z *q)
439: {
440: int n,m,i;
441: Z s,t,u;
442: Z *b;
443:
444: n = v->len;
445: b = (Z *)v->body;
446: if ( n == 1 ) {
447: if ( mpz_sgn(BDY(b[0]))<0 ) chsgnz(b[0],q);
448: else *q = b[0];
449: }
450: m = n/2;
451: for ( i = 0, s = 0; i < m; i++ ) {
452: if ( b[i] && mpz_sgn(BDY(b[i]))<0 ) subz(s,b[i],&u);
453: else addz(s,b[i],&u);
454: s = u;
455: }
456: for ( i = 0, t = 0; i < n; i++ ) {
457: if ( b[i] && mpz_sgn(BDY(b[i]))<0 ) subz(t,b[i],&u);
458: else addz(t,b[i],&u);
459: t = u;
460: }
461: gcdz(s,t,q);
462: }
463:
464: void factorialz(unsigned int n,Z *nr)
465: {
466: mpz_t a;
467: mpz_init(a);
468: mpz_fac_ui(a,n);
469: MPZTOZ(a,*nr);
470: }
471:
472: void randomz(int blen,Z *nr)
473: {
474: mpz_t z;
475:
476: mpz_init(z);
477: mpz_urandomb(z,GMP_RAND,blen);
478: MPZTOZ(z,*nr);
479: }
480:
481: int tstbitz(Z n,int k)
482: {
483: if ( !n || !n->z )
484: error("tstbitz : invalid argument");
485: return !n ? 0 : mpz_tstbit(BDY(n),k);
486: }
487:
488: void addq(Q n1,Q n2,Q *nr)
489: {
490: mpq_t q1,q2,t;
491:
492: if ( !n1 ) *nr = n2;
493: else if ( !n2 ) *nr = n1;
494: else if ( n1->z && n2->z )
495: addz((Z)n1,(Z)n2,(Z *)nr);
496: else {
497: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
498: else q1[0] = BDY(n1)[0];
499: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
500: else q2[0] = BDY(n2)[0];
501: mpq_init(t); mpq_add(t,q1,q2); *nr = mpqtozq(t);
502: }
503: }
504:
505: void subq(Q n1,Q n2,Q *nr)
506: {
507: mpq_t q1,q2,t;
508:
509: if ( !n1 ) {
510: if ( !n2 ) *nr = 0;
511: else if ( n1->z ) chsgnz((Z)n1,(Z *)nr);
512: else {
513: mpq_init(t); mpq_neg(t,BDY(n2)); MPQTOQ(t,*nr);
514: }
515: } else if ( !n2 ) *nr = n1;
516: else if ( n1 == n2 ) *nr = 0;
517: else if ( n1->z && n2->z )
518: subz((Z)n1,(Z)n2,(Z *)nr);
519: else {
520: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
521: else q1[0] = BDY(n1)[0];
522: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
523: else q2[0] = BDY(n2)[0];
524: mpq_init(t); mpq_sub(t,q1,q2); *nr = mpqtozq(t);
525: }
526: }
527:
528: void mulq(Q n1,Q n2,Q *nr)
529: {
530: mpq_t t,q1,q2;
531:
532: if ( !n1 || !n2 ) *nr = 0;
533: else if ( n1->z && n2->z )
534: mulz((Z)n1,(Z)n2,(Z *)nr);
535: else {
536: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
537: else q1[0] = BDY(n1)[0];
538: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
539: else q2[0] = BDY(n2)[0];
540: mpq_init(t); mpq_mul(t,q1,q2); *nr = mpqtozq(t);
541: }
542: }
543:
544: void divq(Q n1,Q n2,Q *nq)
545: {
546: mpq_t t,q1,q2;
547:
548: if ( !n2 ) {
549: error("division by 0");
550: *nq = 0;
551: return;
552: } else if ( !n1 ) *nq = 0;
553: else if ( n1 == n2 ) *nq = (Q)ONE;
554: else {
555: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
556: else q1[0] = BDY(n1)[0];
557: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
558: else q2[0] = BDY(n2)[0];
559: mpq_init(t); mpq_div(t,q1,q2); *nq = mpqtozq(t);
560: }
561: }
562:
563: void invq(Q n,Q *nr)
564: {
565: Z nm,dn;
566:
567: if ( INT(n) )
568: divq((Q)ONE,n,nr);
569: else {
570: nmq(n,&nm);
571: dnq(n,&dn);
572: divq((Q)dn,(Q)nm,nr);
573: }
574: }
575:
576: void chsgnq(Q n,Q *nr)
577: {
578: mpq_t t;
579:
580: if ( !n ) *nr = 0;
581: else if (n->z ) chsgnz((Z)n,(Z *)nr);
582: else {
583: mpq_init(t); mpq_neg(t,BDY(n)); MPQTOQ(t,*nr);
584: }
585: }
586:
587: void absq(Q n,Q *nr)
588: {
589: if ( !n ) *nr = 0;
590: else if ( n->z ) absz((Z)n,(Z *)nr);
591: else if ( sgnq(n) < 0 ) chsgnq(n,nr);
592: else *nr = n;
593: }
594:
595: void pwrq(Q n1,Q n,Q *nr)
596: {
597: int e;
598: mpz_t nm,dn;
599: mpq_t t;
600:
601: if ( !n || UNIQ((Z)n1) || UNIQ(n1) ) *nr = (Q)ONE;
602: else if ( !n1 ) *nr = 0;
603: else if ( !INT(n) ) {
604: error("can't calculate fractional power."); *nr = 0;
605: } else if ( !smallz((Z)n) ) {
606: error("exponent too big."); *nr = 0;
607: } else {
608: e = QTOS(n);
609: if ( e < 0 ) {
610: e = -e;
611: if ( n1->z ) {
612: nm[0] = ONEMPZ[0];
613: dn[0] = BDY((Z)n1)[0];
614: } else {
615: nm[0] = mpq_denref(BDY(n1))[0];
616: dn[0] = mpq_numref(BDY(n1))[0];
617: }
618: } else {
619: if ( n1->z ) {
620: nm[0] = BDY((Z)n1)[0];
621: dn[0] = ONEMPZ[0];
622: } else {
623: nm[0] = mpq_numref(BDY(n1))[0];
624: dn[0] = mpq_denref(BDY(n1))[0];
625: }
626: }
627: mpq_init(t);
628: mpz_pow_ui(mpq_numref(t),nm,e); mpz_pow_ui(mpq_denref(t),dn,e);
629: *nr = mpqtozq(t);
630: }
631: }
632:
633: int cmpq(Q n1,Q n2)
634: {
635: mpq_t q1,q2;
636: int sgn;
637:
638: if ( !n1 ) {
639: if ( !n2 ) return 0;
640: else return (n2->z) ? -mpz_sgn(BDY((Z)n2)) : -mpq_sgn(BDY(n2));
641: } if ( !n2 ) return (n1->z) ? mpz_sgn(BDY((Z)n1)) : mpq_sgn(BDY(n1));
642: else if ( n1->z && n2->z )
643: return cmpz((Z)n1,(Z)n2);
644: else if ( (sgn = mpq_sgn(BDY(n1))) != mpq_sgn(BDY(n2)) ) return sgn;
645: else {
646: if ( n1->z ) MPZTOMPQ(BDY((Z)n1),q1);
647: else q1[0] = BDY(n1)[0];
648: if ( n2->z ) MPZTOMPQ(BDY((Z)n2),q2);
649: else q2[0] = BDY(n2)[0];
650: sgn = mpq_cmp(q1,q2);
651: if ( sgn > 0 ) return 1;
652: else if ( sgn < 0 ) return -1;
653: else return 0;
654: }
655: }
656:
657: /* t = [nC0 nC1 ... nCn] */
658:
659: void mkbc(int n,Z *t)
660: {
661: int i;
662: Z c,d,iq;
663:
664: for ( t[0] = ONE, i = 1; i <= n/2; i++ ) {
665: STOQ(n-i+1,c); mulz(t[i-1],c,&d);
666: STOQ(i,iq); divsz(d,iq,&t[i]);
667: }
668: for ( ; i <= n; i++ )
669: t[i] = t[n-i];
670: }
671:
672: /*
673: * Dx^k*x^l = W(k,l,0)*x^l*Dx^k+W(k,l,1)*x^(l-1)*x^(k-1)*+...
674: *
675: * t = [W(k,l,0) W(k,l,1) ... W(k,l,min(k,l)]
676: * where W(k,l,i) = i! * kCi * lCi
677: */
678:
679: /* mod m table */
680: /* XXX : should be optimized */
681:
682: void mkwcm(int k,int l,int m,int *t)
683: {
684: int i,n;
685: Z *s;
686:
687: n = MIN(k,l);
688: s = (Z *)ALLOCA((n+1)*sizeof(Q));
689: mkwc(k,l,s);
690: for ( i = 0; i <= n; i++ ) {
691: t[i] = remqi((Q)s[i],m);
692: }
693: }
694:
695: void mkwc(int k,int l,Z *t)
696: {
697: mpz_t a,b,q,nm,z,u;
698: int i,n;
699:
700: n = MIN(k,l);
701: mpz_init_set_ui(z,1);
702: mpz_init(u); mpz_set(u,z); MPZTOZ(u,t[0]);
703: mpz_init(a); mpz_init(b); mpz_init(nm);
704: for ( i = 1; i <= n; i++ ) {
705: mpz_set_ui(a,k-i+1); mpz_set_ui(b,l-i+1); mpz_mul(nm,a,b);
706: mpz_mul(z,BDY(t[i-1]),nm); mpz_fdiv_q_ui(z,z,i);
707: mpz_init(u); mpz_set(u,z); MPZTOZ(u,t[i]);
708: }
709: }
710:
711: void lgp(P p,Z *g,Z *l);
712:
713: void ptozp(P p,int sgn,Q *c,P *pr)
714: {
715: Z nm,dn;
716:
717: if ( !p ) {
718: *c = 0; *pr = 0;
719: } else {
720: lgp(p,&nm,&dn);
721: divz(nm,dn,(Z *)c);
722: divsp(CO,p,(P)*c,pr);
723: }
724: }
725:
726: void lgp(P p,Z *g,Z *l)
727: {
728: DCP dc;
729: Z g1,g2,l1,l2,l3,l4;
730:
731: if ( NUM(p) ) {
732: if ( ((Q)p)->z ) {
733: MPZTOZ(BDY((Z)p),*g);
734: *l = ONE;
735: } else {
736: MPZTOZ(mpq_numref(BDY((Q)p)),*g);
737: MPZTOZ(mpq_denref(BDY((Q)p)),*l);
738: }
739: } else {
740: dc = DC(p); lgp(COEF(dc),g,l);
741: for ( dc = NEXT(dc); dc; dc = NEXT(dc) ) {
742: lgp(COEF(dc),&g1,&l1); gcdz(*g,g1,&g2); *g = g2;
743: gcdz(*l,l1,&l2); mulz(*l,l1,&l3); divz(l3,l2,l);
744: }
745: }
746: }
747:
748: void qltozl(Q *w,int n,Z *dvr)
749: {
750: Z nm,dn;
751: Z g,g1,l1,l2,l3;
752: Q c;
753: int i;
754: struct oVECT v;
755:
756: for ( i = 0; i < n; i++ )
757: if ( w[i] && !w[i]->z )
758: break;
759: if ( i == n ) {
760: v.id = O_VECT; v.len = n; v.body = (pointer *)w;
761: gcdvz(&v,dvr); return;
762: }
763: for ( i = 0; !w[i]; i++ );
764: c = w[i];
765: if ( !c->z ) {
766: MPZTOZ(mpq_numref(BDY(c)),nm); MPZTOZ(mpq_denref(BDY(c)),dn);
767: } else {
768: MPZTOZ(BDY((Z)c),nm); dn = ONE;
769: }
770: for ( i++; i < n; i++ ) {
771: c = w[i];
772: if ( !c ) continue;
773: if ( !c->z ) {
774: MPZTOZ(mpq_numref(BDY(c)),g1); MPZTOZ(mpq_denref(BDY(c)),l1);
775: } else {
776: MPZTOZ(BDY((Z)c),g1); l1 = ONE;
777: }
778: gcdz(nm,g1,&g); nm = g;
779: gcdz(dn,l1,&l2); mulz(dn,l1,&l3); divz(l3,l2,&dn);
780: }
781: divz(nm,dn,dvr);
782: }
783:
784: int z_bits(Q q)
785: {
786: if ( !q ) return 0;
787: else if ( q->z ) return mpz_sizeinbase(BDY((Z)q),2);
788: else
789: return mpz_sizeinbase(mpq_numref(BDY(q)),2)
790: + mpz_sizeinbase(mpq_denref(BDY(q)),2);
791: }
792:
793: int zp_mag(P p)
794: {
795: int s;
796: DCP dc;
797:
798: if ( !p ) return 0;
799: else if ( OID(p) == O_N ) return z_bits((Q)p);
800: else {
801: for ( dc = DC(p), s = 0; dc; dc = NEXT(dc) ) s += zp_mag(COEF(dc));
802: return s;
803: }
804: }
805:
806: void makesubstz(VL v,NODE *s)
807: {
808: NODE r,r0;
809: Z q;
810: unsigned int n;
811:
812: for ( r0 = 0; v; v = NEXT(v) ) {
813: NEXTNODE(r0,r); BDY(r) = (pointer)v->v;
814: #if defined(_PA_RISC1_1)
815: n = mrand48()&BMASK; q = utoz(n);
816: #else
817: n = random(); q = utoz(n);
818: #endif
819: NEXTNODE(r0,r); BDY(r) = (pointer)q;
820: }
821: if ( r0 ) NEXT(r) = 0;
822: *s = r0;
823: }
824:
825: unsigned int remqi(Q a,unsigned int mod)
826: {
827: unsigned int c,nm,dn;
828: mpz_t r;
829:
830: if ( !a ) return 0;
831: else if ( a->z ) {
832: mpz_init(r);
833: c = mpz_fdiv_r_ui(r,BDY((Z)a),mod);
834: } else {
835: mpz_init(r);
836: nm = mpz_fdiv_r_ui(r,mpq_numref(BDY(a)),mod);
837: dn = mpz_fdiv_r_ui(r,mpq_denref(BDY(a)),mod);
838: dn = invm(dn,mod);
839: DMAR(nm,dn,0,mod,c);
840: }
841: return c;
842: }
843:
844: extern int DP_Print;
845:
846: #define F4_INTRAT_PERIOD 8
847:
848: int generic_gauss_elim(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp)
849: {
850: int **wmat;
851: Z **bmat,**tmat,*bmi,*tmi;
852: Z q,m1,m2,m3,s,u;
853: int *wmi,*colstat,*wcolstat,*rind,*cind;
854: int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv;
855: MAT r,crmat;
856: int ret;
857:
858: bmat = (Z **)mat->body;
859: row = mat->row; col = mat->col;
860: wmat = (int **)almat(row,col);
861: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
862: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
863: for ( ind = 0; ; ind++ ) {
864: if ( DP_Print ) {
865: fprintf(asir_out,"."); fflush(asir_out);
866: }
867: md = get_lprime(ind);
868: for ( i = 0; i < row; i++ )
869: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
870: wmi[j] = remqi((Q)bmi[j],md);
871: rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
872: if ( !ind ) {
873: RESET:
874: m1 = utoz(md);
875: rank0 = rank;
876: bcopy(wcolstat,colstat,col*sizeof(int));
877: MKMAT(crmat,rank,col-rank);
878: MKMAT(r,rank,col-rank); *nm = r;
879: tmat = (Z **)crmat->body;
880: for ( i = 0; i < rank; i++ )
881: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
882: if ( !colstat[j] ) tmi[k++] = utoz(wmi[j]);
883: } else {
884: if ( rank < rank0 ) {
885: if ( DP_Print ) {
886: fprintf(asir_out,"lower rank matrix; continuing...\n");
887: fflush(asir_out);
888: }
889: continue;
890: } else if ( rank > rank0 ) {
891: if ( DP_Print ) {
892: fprintf(asir_out,"higher rank matrix; resetting...\n");
893: fflush(asir_out);
894: }
895: goto RESET;
896: } else {
897: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
898: if ( j < col ) {
899: if ( DP_Print ) {
900: fprintf(asir_out,"inconsitent colstat; resetting...\n");
901: fflush(asir_out);
902: }
903: goto RESET;
904: }
905: }
906:
907: inv = invm(remqi((Q)m1,md),md);
908: m2 = utoz(md); mulz(m1,m2,&m3);
909: for ( i = 0; i < rank; i++ )
910: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
911: if ( !colstat[j] ) {
912: if ( tmi[k] ) {
913: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
914: t = remqi((Q)tmi[k],md);
915: if ( wmi[j] >= t ) t = wmi[j]-t;
916: else t = md-(t-wmi[j]);
917: DMAR(t,inv,0,md,t1)
918: u = utoz(t1); mulz(m1,u,&s);
919: addz(tmi[k],s,&u); tmi[k] = u;
920: } else if ( wmi[j] ) {
921: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
922: DMAR(wmi[j],inv,0,md,t)
923: u = utoz(t); mulz(m1,u,&s); tmi[k] = s;
924: }
925: k++;
926: }
927: m1 = m3;
928: if ( ind % F4_INTRAT_PERIOD )
929: ret = 0;
930: else
931: ret = intmtoratm(crmat,m1,*nm,dn);
932: if ( ret ) {
933: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
934: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
935: for ( j = k = l = 0; j < col; j++ )
936: if ( colstat[j] ) rind[k++] = j;
937: else cind[l++] = j;
938: if ( gensolve_check(mat,*nm,*dn,rind,cind) )
939: return rank;
940: }
941: }
942: }
943: }
944:
945: int generic_gauss_elim2(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp)
946: {
947:
948: MAT full;
949: Z **bmat,**b;
950: Z *bmi;
951: Z dn0;
952: int row,col,md,i,j,rank,ret;
953: int **wmat;
954: int *wmi;
955: int *colstat,*rowstat;
956:
957: bmat = (Z **)mat->body;
958: row = mat->row; col = mat->col;
959: wmat = (int **)almat(row,col);
960: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
961: rowstat = (int *)MALLOC_ATOMIC(row*sizeof(int));
962: /* XXX */
963: md = get_lprime(0);
964: for ( i = 0; i < row; i++ )
965: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
966: wmi[j] = remqi((Q)bmi[j],md);
967: rank = generic_gauss_elim_mod2(wmat,row,col,md,colstat,rowstat);
968: b = (Z **)MALLOC(rank*sizeof(Z));
969: for ( i = 0; i < rank; i++ ) b[i] = bmat[rowstat[i]];
970: NEWMAT(full); full->row = rank; full->col = col; full->body = (pointer **)b;
971: ret = generic_gauss_elim_full(full,nm,dn,rindp,cindp);
972: if ( !ret ) {
973: rank = generic_gauss_elim(mat,nm,&dn0,rindp,cindp);
974: for ( i = 0; i < rank; i++ ) dn[i] = dn0;
975: }
976: return rank;
977: }
978:
979: int generic_gauss_elim_full(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp)
980: {
981: int **wmat;
982: int *stat;
983: Z **bmat,**tmat,*bmi,*tmi,*ri;
984: Z q,m1,m2,m3,s,u;
985: int *wmi,*colstat,*wcolstat,*rind,*cind;
986: int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv,h;
987: MAT r,crmat;
988: int ret,initialized,done;
989:
990: initialized = 0;
991: bmat = (Z **)mat->body;
992: row = mat->row; col = mat->col;
993: wmat = (int **)almat(row,col);
994: stat = (int *)MALLOC_ATOMIC(row*sizeof(int));
995: for ( i = 0; i < row; i++ ) stat[i] = 0;
996: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
997: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
998: for ( ind = 0; ; ind++ ) {
999: if ( DP_Print ) {
1000: fprintf(asir_out,"."); fflush(asir_out);
1001: }
1002: md = get_lprime(ind);
1003: for ( i = 0; i < row; i++ )
1004: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
1005: wmi[j] = remqi((Q)bmi[j],md);
1006: rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
1007: if ( rank < row ) continue;
1008: if ( !initialized ) {
1009: m1 = utoz(md);
1010: bcopy(wcolstat,colstat,col*sizeof(int));
1011: MKMAT(crmat,row,col-row);
1012: MKMAT(r,row,col-row); *nm = r;
1013: tmat = (Z **)crmat->body;
1014: for ( i = 0; i < row; i++ )
1015: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
1016: if ( !colstat[j] ) tmi[k++] = utoz(wmi[j]);
1017: initialized = 1;
1018: } else {
1019: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
1020: if ( j < col ) continue;
1021:
1022: inv = invm(remqi((Q)m1,md),md);
1023: m2 = utoz(md); mulz(m1,m2,&m3);
1024: for ( i = 0; i < row; i++ )
1025: switch ( stat[i] ) {
1026: case 1:
1027: /* consistency check */
1028: ri = (Z *)BDY(r)[i]; wmi = wmat[i];
1029: for ( j = 0; j < col; j++ ) if ( colstat[j] ) break;
1030: h = md-remqi((Q)dn[i],md);
1031: for ( j++, k = 0; j < col; j++ )
1032: if ( !colstat[j] ) {
1033: t = remqi((Q)ri[k],md);
1034: DMAR(wmi[i],h,t,md,t1);
1035: if ( t1 ) break;
1036: }
1037: if ( j == col ) { stat[i]++; break; }
1038: else {
1039: /* fall to the case 0 */
1040: stat[i] = 0;
1041: }
1042: case 0:
1043: tmi = tmat[i]; wmi = wmat[i];
1044: for ( j = k = 0; j < col; j++ )
1045: if ( !colstat[j] ) {
1046: if ( tmi[k] ) {
1047: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
1048: t = remqi((Q)tmi[k],md);
1049: if ( wmi[j] >= t ) t = wmi[j]-t;
1050: else t = md-(t-wmi[j]);
1051: DMAR(t,inv,0,md,t1)
1052: u = utoz(t1); mulz(m1,u,&s);
1053: addz(tmi[k],s,&u); tmi[k] = u;
1054: } else if ( wmi[j] ) {
1055: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
1056: DMAR(wmi[j],inv,0,md,t)
1057: u = utoz(t); mulz(m1,u,&s); tmi[k] = s;
1058: }
1059: k++;
1060: }
1061: break;
1062: case 2: default:
1063: break;
1064: }
1065: m1 = m3;
1066: if ( ind % 4 )
1067: ret = 0;
1068: else
1069: ret = intmtoratm2(crmat,m1,*nm,dn,stat);
1070: if ( ret ) {
1071: *rindp = rind = (int *)MALLOC_ATOMIC(row*sizeof(int));
1072: *cindp = cind = (int *)MALLOC_ATOMIC((col-row)*sizeof(int));
1073: for ( j = k = l = 0; j < col; j++ )
1074: if ( colstat[j] ) rind[k++] = j;
1075: else cind[l++] = j;
1076: return gensolve_check2(mat,*nm,dn,rind,cind);
1077: }
1078: }
1079: }
1080: }
1081:
1082: int generic_gauss_elim_direct(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp){
1083: Z **bmat,*s;
1084: Z u,v,w,x,d,t,y;
1085: int row,col,i,j,k,l,m,rank;
1086: int *colstat,*colpos,*cind;
1087: MAT r,in;
1088:
1089: row = mat->row; col = mat->col;
1090: MKMAT(in,row,col);
1091: for ( i = 0; i < row; i++ )
1092: for ( j = 0; j < col; j++ ) in->body[i][j] = mat->body[i][j];
1093: bmat = (Z **)in->body;
1094: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1095: *rindp = colpos = (int *)MALLOC_ATOMIC(row*sizeof(int));
1096: for ( j = 0, rank = 0, d = ONE; j < col; j++ ) {
1097: for ( i = rank; i < row && !bmat[i][j]; i++ );
1098: if ( i == row ) { colstat[j] = 0; continue; }
1099: else { colstat[j] = 1; colpos[rank] = j; }
1100: if ( i != rank )
1101: for ( k = j; k < col; k++ ) {
1102: t = bmat[i][k]; bmat[i][k] = bmat[rank][k]; bmat[rank][k] = t;
1103: }
1104: for ( i = rank+1, v = bmat[rank][j]; i < row; i++ )
1105: for ( k = j, u = bmat[i][j]; k < col; k++ ) {
1106: mulz(bmat[i][k],v,&w); mulz(bmat[rank][k],u,&x);
1107: subz(w,x,&y); divsz(y,d,&bmat[i][k]);
1108: }
1109: d = v; rank++;
1110: }
1111: *dn = d;
1112: s = (Z *)MALLOC(col*sizeof(Z));
1113: for ( i = rank-1; i >= 0; i-- ) {
1114: for ( k = colpos[i]; k < col; k++ ) mulz(bmat[i][k],d,&s[k]);
1115: for ( m = rank-1; m > i; m-- ) {
1116: for ( k = colpos[m], u = bmat[i][k]; k < col; k++ ) {
1117: mulz(bmat[m][k],u,&w); subz(s[k],w,&x); s[k] = x;
1118: }
1119: }
1120: for ( k = colpos[i], u = bmat[i][k]; k < col; k++ )
1121: divz(s[k],u,&bmat[i][k]);
1122: }
1123: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
1124: MKMAT(r,rank,col-rank); *nm = r;
1125: for ( j = 0, k = 0; j < col; j++ )
1126: if ( !colstat[j] ) {
1127: cind[k] = j;
1128: for ( i = 0; i < rank; i++ ) r->body[i][k] = bmat[i][j];
1129: k++;
1130: }
1131: return rank;
1132: }
1133:
1134: int intmtoratm(MAT mat,Z md,MAT nm,Z *dn)
1135: {
1136: Z t,s,b,dn0,dn1,nm1,q,u,unm,udn,dmy;
1137: int i,j,k,l,row,col,sgn,ret;
1138: Z **rmat,**tmat,*tmi,*nmk;
1139:
1140: if ( UNIQ(md) )
1141: return 0;
1142: row = mat->row; col = mat->col;
1143: bshiftz(md,1,&t);
1144: isqrtz(t,&s);
1145: bshiftz(s,64,&b);
1146: if ( !b ) b = ONE;
1147: dn0 = ONE;
1148: tmat = (Z **)mat->body;
1149: rmat = (Z **)nm->body;
1150: for ( i = 0; i < row; i++ )
1151: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1152: if ( tmi[j] ) {
1153: mulz(tmi[j],dn0,&s);
1154: divqrz(s,md,&dmy,&u);
1155: ret = inttorat(u,md,b,&nm1,&dn1);
1156: if ( !ret ) return 0;
1157: else {
1158: if ( !UNIQ(dn1) ) {
1159: for ( k = 0; k < i; k++ )
1160: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1161: mulz(nmk[l],dn1,&q); nmk[l] = q;
1162: }
1163: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1164: mulz(nmk[l],dn1,&q); nmk[l] = q;
1165: }
1166: }
1167: rmat[i][j] = nm1;
1168: mulz(dn0,dn1,&q); dn0 = q;
1169: }
1170: }
1171: *dn = dn0;
1172: return 1;
1173: }
1174:
1175: int intmtoratm2(MAT mat,Z md,MAT nm,Z *dn,int *stat)
1176: {
1177: int row,col,i,j,ret;
1178: Z dn0,dn1,t,s,b;
1179: Z *w,*tmi;
1180: Z **tmat;
1181:
1182: bshiftz(md,1,&t);
1183: isqrtz(t,&s);
1184: bshiftz(s,64,&b);
1185: tmat = (Z **)mat->body;
1186: if ( UNIQ(md) ) return 0;
1187: row = mat->row; col = mat->col;
1188: dn0 = ONE;
1189: for ( i = 0; i < row; i++ )
1190: if ( cmpz(dn[i],dn0) > 0 ) dn0 = dn[i];
1191: w = (Z *)MALLOC(col*sizeof(Z));
1192: for ( i = 0; i < row; i++ )
1193: if ( stat[i] == 0 ) {
1194: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1195: mulz(tmi[j],dn0,&w[j]);
1196: ret = intvtoratv(w,col,md,b,(Z *)BDY(nm)[i],&dn[i]);
1197: if ( ret ) {
1198: stat[i] = 1;
1199: mulz(dn0,dn[i],&t); dn[i] = t; dn0 = t;
1200: }
1201: }
1202: for ( i = 0; i < row; i++ ) if ( !stat[i] ) break;
1203: if ( i == row ) return 1;
1204: else return 0;
1205: }
1206:
1207: int intvtoratv(Z *v,int n,Z md,Z b,Z *nm,Z *dn)
1208: {
1209: Z dn0,dn1,q,s,u,nm1,unm,udn,dmy;
1210: Z *nmk;
1211: int j,l,col,ret,sgn;
1212:
1213: for ( j = 0; j < n; j++ ) nm[j] = 0;
1214: dn0 = ONE;
1215: for ( j = 0; j < n; j++ ) {
1216: if ( !v[j] ) continue;
1217: mulz(v[j],dn0,&s);
1218: divqrz(s,md,&dmy,&u);
1219: ret = inttorat(u,md,b,&nm1,&dn1);
1220: if ( !ret ) return 0;
1221: if ( !UNIQ(dn1) )
1222: for ( l = 0; l < j; l++ ) {
1223: mulz(nm[l],dn1,&q); nm[l] = q;
1224: }
1225: nm[j] = nm1;
1226: mulz(dn0,dn1,&q); dn0 = q;
1227: }
1228: *dn = dn0;
1229: return 1;
1230: }
1231:
1232: /* assuming 0 < c < m */
1233:
1234: int inttorat(Z c,Z m,Z b,Z *nmp,Z *dnp)
1235: {
1236: Z qq,t,u1,v1,r1;
1237: Z q,u2,v2,r2;
1238:
1239: u1 = 0; v1 = ONE; u2 = m; v2 = c;
1240: while ( cmpz(v2,b) >= 0 ) {
1241: divqrz(u2,v2,&q,&r2); u2 = v2; v2 = r2;
1242: mulz(q,v1,&t); subz(u1,t,&r1); u1 = v1; v1 = r1;
1243: }
1244: if ( cmpz(v1,b) >= 0 ) return 0;
1245: else {
1246: if ( mpz_sgn(BDY(v1))<0 ) {
1247: chsgnz(v1,dnp); chsgnz(v2,nmp);
1248: } else {
1249: *dnp = v1; *nmp = v2;
1250: }
1251: return 1;
1252: }
1253: }
1254:
1255: extern int f4_nocheck;
1256:
1257: int gensolve_check(MAT mat,MAT nm,Z dn,int *rind,int *cind)
1258: {
1259: int row,col,rank,clen,i,j,k,l;
1260: Z s,t;
1261: Z *w;
1262: Z *mati,*nmk;
1263:
1264: if ( f4_nocheck ) return 1;
1265: row = mat->row; col = mat->col; rank = nm->row; clen = nm->col;
1266: w = (Z *)MALLOC(clen*sizeof(Z));
1267: for ( i = 0; i < row; i++ ) {
1268: mati = (Z *)mat->body[i];
1269: bzero(w,clen*sizeof(Z));
1270: for ( k = 0; k < rank; k++ )
1271: for ( l = 0, nmk = (Z *)nm->body[k]; l < clen; l++ ) {
1272: mulz(mati[rind[k]],nmk[l],&t); addz(w[l],t,&s); w[l] = s;
1273: }
1274: for ( j = 0; j < clen; j++ ) {
1275: mulz(dn,mati[cind[j]],&t);
1276: if ( cmpz(w[j],t) ) break;
1277: }
1278: if ( j != clen ) break;
1279: }
1280: if ( i != row ) return 0;
1281: else return 1;
1282: }
1283:
1284: int gensolve_check2(MAT mat,MAT nm,Z *dn,int *rind,int *cind)
1285: {
1286: int row,col,rank,clen,i,j,k,l;
1287: Z s,t,u,d;
1288: Z *w,*m;
1289: Z *mati,*nmk;
1290:
1291: if ( f4_nocheck ) return 1;
1292: row = mat->row; col = mat->col; rank = nm->row; clen = nm->col;
1293: w = (Z *)MALLOC(clen*sizeof(Z));
1294: m = (Z *)MALLOC(clen*sizeof(Z));
1295: for ( d = dn[0], i = 1; i < rank; i++ ) {
1296: lcmz(d,dn[i],&t); d = t;
1297: }
1298: for ( i = 0; i < rank; i++ ) divsz(d,dn[i],&m[i]);
1299: for ( i = 0; i < row; i++ ) {
1300: mati = (Z *)mat->body[i];
1301: bzero(w,clen*sizeof(Z));
1302: for ( k = 0; k < rank; k++ ) {
1303: mulz(mati[rind[k]],m[k],&u);
1304: for ( l = 0, nmk = (Z *)nm->body[k]; l < clen; l++ ) {
1305: mulz(u,nmk[l],&t); addz(w[l],t,&s); w[l] = s;
1306: }
1307: }
1308: for ( j = 0; j < clen; j++ ) {
1309: mulz(d,mati[cind[j]],&t);
1310: if ( cmpz(w[j],t) ) break;
1311: }
1312: if ( j != clen ) break;
1313: }
1314: if ( i != row ) return 0;
1315: else return 1;
1316: }
1317:
1318: void isqrtz(Z a,Z *r)
1319: {
1320: int k;
1321: Z x,t,x2,xh,quo,rem;
1322: Z two;
1323:
1324: if ( !a ) *r = 0;
1325: else if ( UNIQ(a) ) *r = ONE;
1326: else {
1327: k = z_bits((Q)a); /* a <= 2^k-1 */
1328: bshiftz(ONE,-((k>>1)+(k&1)),&x); /* a <= x^2 */
1329: STOQ(2,two);
1330: while ( 1 ) {
1331: pwrz(x,two,&t);
1332: if ( cmpz(t,a) <= 0 ) {
1333: *r = x; return;
1334: } else {
1335: if ( mpz_tstbit(BDY(x),0) ) addz(x,a,&t);
1336: else t = a;
1337: bshiftz(x,-1,&x2); divqrz(t,x2,&quo,&rem);
1338: bshiftz(x,1,&xh); addz(quo,xh,&x);
1339: }
1340: }
1341: }
1342: }
1343:
1344: void bshiftz(Z a,int n,Z *r)
1345: {
1346: mpz_t t;
1347:
1348: if ( !a ) *r = 0;
1349: else if ( n == 0 ) *r = a;
1350: else if ( n < 0 ) {
1351: mpz_init(t); mpz_mul_2exp(t,BDY(a),-n); MPZTOZ(t,*r);
1352: } else {
1353: mpz_init(t); mpz_fdiv_q_2exp(t,BDY(a),n);
1354: if ( !mpz_sgn(t) ) *r = 0;
1355: else MPZTOZ(t,*r);
1356: }
1357: }
1358:
1359: void addlf(Z a,Z b,Z *c)
1360: {
1361: addz(a,b,c);
1362: if ( !lf_lazy ) {
1363: if ( cmpz(*c,current_mod_lf) >= 0 ) {
1364: subz(*c,current_mod_lf,c);
1365: }
1366: }
1367: }
1368:
1369: void sublf(Z a,Z b,Z *c)
1370: {
1371: subz(a,b,c);
1372: if ( !lf_lazy ) {
1373: remz(*c,current_mod_lf,c);
1374: }
1375: }
1376:
1377: void mullf(Z a,Z b,Z *c)
1378: {
1379: mulz(a,b,c);
1380: if ( !lf_lazy ) {
1381: remz(*c,current_mod_lf,c);
1382: }
1383: }
1384:
1385: void divlf(Z a,Z b,Z *c)
1386: {
1387: Z inv;
1388:
1389: invz(b,current_mod_lf,&inv);
1390: mulz(a,inv,c);
1391: if ( !lf_lazy ) {
1392: remz(*c,current_mod_lf,c);
1393: }
1394: }
1395:
1396: void chsgnlf(Z a,Z *c)
1397: {
1398: chsgnz(a,c);
1399: if ( !lf_lazy ) {
1400: remz(*c,current_mod_lf,c);
1401: }
1402: }
1403:
1404: void lmtolf(LM a,Z *b)
1405: {
1406: if ( !a ) *b = 0;
1407: else {
1408: MPZTOZ(BDY(a),*b);
1409: }
1410: }
1411:
1412: void setmod_lf(Z p)
1413: {
1414: current_mod_lf = p;
1415: current_mod_lf_size = mpz_size(BDY(current_mod_lf))+1;
1416: }
1417:
1418: void simplf_force(Z a,Z *b)
1419: {
1420: remz(a,current_mod_lf,b);
1421: }
1422:
1423: int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Z *dn,int **rindp,int **cindp)
1424: {
1425: MAT bmat,xmat;
1426: Z **a0,**a,**b,**x,**nm;
1427: Z *ai,*bi,*xi;
1428: int row,col;
1429: int **w;
1430: int *wi;
1431: int **wc;
1432: Z mdq,q,s,u;
1433: Z tn;
1434: int ind,md,i,j,k,l,li,ri,rank;
1435: unsigned int t;
1436: int *cinfo,*rinfo;
1437: int *rind,*cind;
1438: int count;
1439: int ret;
1.3 ! noro 1440: struct oEGT eg_mul1,eg_mul2,tmp0,tmp1,tmp2;
1.1 noro 1441: int period;
1442: int *wx,*ptr;
1443: int wxsize,nsize;
1444: Z wn;
1445: Z wq;
1446:
1.3 ! noro 1447: init_eg(&eg_mul1); init_eg(&eg_mul2);
1.1 noro 1448: a0 = (Z **)mat->body;
1449: row = mat->row; col = mat->col;
1450: w = (int **)almat(row,col);
1451: for ( ind = 0; ; ind++ ) {
1452: md = get_lprime(ind);
1453: STOQ(md,mdq);
1454: for ( i = 0; i < row; i++ )
1455: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1456: wi[j] = remqi((Q)ai[j],md);
1457:
1458: if ( DP_Print > 3 ) {
1459: fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
1460: }
1461: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1462: if ( DP_Print > 3 ) {
1463: fprintf(asir_out,"done.\n"); fflush(asir_out);
1464: }
1465: a = (Z **)almat_pointer(rank,rank); /* lhs mat */
1466: MKMAT(bmat,rank,col-rank); b = (Z **)bmat->body; /* lhs mat */
1467: for ( j = li = ri = 0; j < col; j++ )
1468: if ( cinfo[j] ) {
1469: /* the column is in lhs */
1470: for ( i = 0; i < rank; i++ ) {
1471: w[i][li] = w[i][j];
1472: a[i][li] = a0[rinfo[i]][j];
1473: }
1474: li++;
1475: } else {
1476: /* the column is in rhs */
1477: for ( i = 0; i < rank; i++ )
1478: b[i][ri] = a0[rinfo[i]][j];
1479: ri++;
1480: }
1481:
1482: /* solve Ax=B; A: rank x rank, B: rank x ri */
1483: /* algorithm
1484: c <- B
1485: x <- 0
1486: q <- 1
1487: do
1488: t <- A^(-1)c mod p
1489: x <- x+qt
1490: c <- (c-At)/p
1491: q <- qp
1492: end do
1493: then Ax-B=0 mod q and b=(B-Ax)/q hold after "do".
1494: */
1495: MKMAT(xmat,rank,ri); x = (Z **)(xmat)->body;
1496: MKMAT(*nmmat,rank,ri); nm = (Z **)(*nmmat)->body;
1497: wc = (int **)almat(rank,ri);
1498: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1499: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
1500:
1501: period = F4_INTRAT_PERIOD;
1502: for ( q = ONE, count = 0; ; ) {
1.3 ! noro 1503: /* check Ax=B mod q */
1.1 noro 1504: if ( DP_Print > 3 )
1505: fprintf(stderr,"o");
1506: /* wc = b mod md */
1507: for ( i = 0; i < rank; i++ )
1.3 ! noro 1508: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1.1 noro 1509: wi[j] = remqi((Q)bi[j],md);
1.3 ! noro 1510: /* wc = A^(-1)wc; wc is not normalized */
! 1511: solve_by_lu_mod(w,rank,md,wc,ri,0);
1.1 noro 1512: /* x += q*wc */
1.3 ! noro 1513: get_eg(&tmp0);
1.1 noro 1514: for ( i = 0; i < rank; i++ )
1515: for ( j = 0, wi = wc[i]; j < ri; j++ ) mul1addtoz(q,wi[j],&x[i][j]);
1.3 ! noro 1516: /* b =(b-A*wc)/md */
! 1517: get_eg(&tmp1); add_eg(&eg_mul1,&tmp0,&tmp1);
1.1 noro 1518: for ( i = 0; i < rank; i++ )
1519: for ( j = 0; j < ri; j++ ) {
1.3 ! noro 1520: mpz_t uz;
! 1521:
! 1522: if ( b[i][j] )
! 1523: mpz_init_set(uz,BDY(b[i][j]));
! 1524: else
! 1525: mpz_init_set_ui(uz,0);
! 1526: for ( k = 0; k < rank; k++ ) {
! 1527: if ( a[i][k] && wc[k][j] ) {
! 1528: if ( wc[k][j] < 0 )
! 1529: mpz_addmul_ui(uz,BDY(a[i][k]),-wc[k][j]);
! 1530: else
! 1531: mpz_submul_ui(uz,BDY(a[i][k]),wc[k][j]);
! 1532: }
! 1533: }
! 1534: MPZTOZ(uz,u);
1.1 noro 1535: divsz(u,mdq,&b[i][j]);
1536: }
1.3 ! noro 1537: get_eg(&tmp2); add_eg(&eg_mul2,&tmp1,&tmp2);
1.1 noro 1538: count++;
1539: /* q = q*md */
1540: mulz(q,mdq,&u); q = u;
1541: if ( count == period ) {
1542: ret = intmtoratm(xmat,q,*nmmat,dn);
1543: if ( ret ) {
1.3 ! noro 1544: print_eg("MUL1",&eg_mul1);
! 1545: print_eg("MUL2",&eg_mul2);
1.1 noro 1546: for ( j = k = l = 0; j < col; j++ )
1547: if ( cinfo[j] )
1548: rind[k++] = j;
1549: else
1550: cind[l++] = j;
1551: ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
1552: if ( ret ) {
1553: *rindp = rind;
1554: *cindp = cind;
1555: for ( j = k = 0; j < col; j++ )
1556: if ( !cinfo[j] )
1557: cind[k++] = j;
1558: return rank;
1559: }
1560: } else {
1561: period = period*3/2;
1562: count = 0;
1563: }
1564: }
1565: }
1566: }
1567: }
1568:
1569: /* for inv_or_split_dalg */
1570:
1571: int generic_gauss_elim_hensel_dalg(MAT mat,DP *mb,MAT *nmmat,Z *dn,int **rindp,int **cindp)
1572: {
1573: MAT bmat,xmat;
1574: Z **a0,**a,**b,**x,**nm;
1575: Z *ai,*bi,*xi;
1576: int row,col;
1577: int **w;
1578: int *wi;
1579: int **wc;
1580: Z mdq,q,s,u;
1581: Z tn;
1582: int ind,md,i,j,k,l,li,ri,rank;
1583: unsigned int t;
1584: int *cinfo,*rinfo;
1585: int *rind,*cind;
1586: int count;
1587: int ret;
1588: struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1;
1589: int period;
1590: int *wx,*ptr;
1591: int wxsize,nsize;
1592: Z wn;
1593: Z wq;
1594: DP m;
1595:
1596: a0 = (Z **)mat->body;
1597: row = mat->row; col = mat->col;
1598: w = (int **)almat(row,col);
1599: for ( ind = 0; ; ind++ ) {
1600: md = get_lprime(ind);
1601: STOQ(md,mdq);
1602: for ( i = 0; i < row; i++ )
1603: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1604: wi[j] = remqi((Q)ai[j],md);
1605:
1606: if ( DP_Print > 3 ) {
1607: fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
1608: }
1609: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1610: if ( DP_Print > 3 ) {
1611: fprintf(asir_out,"done.\n"); fflush(asir_out);
1612: }
1613:
1614: /* this part is added for inv_or_split_dalg */
1615: for ( i = 0; i < col-1; i++ ) {
1616: if ( !cinfo[i] ) {
1617: m = mb[i];
1618: for ( j = i+1; j < col-1; j++ )
1619: if ( dp_redble(mb[j],m) )
1620: cinfo[j] = -1;
1621: }
1622: }
1623:
1624: a = (Z **)almat_pointer(rank,rank); /* lhs mat */
1625: MKMAT(bmat,rank,col-rank); b = (Z **)bmat->body; /* lhs mat */
1626: for ( j = li = ri = 0; j < col; j++ )
1627: if ( cinfo[j] ) {
1628: /* the column is in lhs */
1629: for ( i = 0; i < rank; i++ ) {
1630: w[i][li] = w[i][j];
1631: a[i][li] = a0[rinfo[i]][j];
1632: }
1633: li++;
1634: } else {
1635: /* the column is in rhs */
1636: for ( i = 0; i < rank; i++ )
1637: b[i][ri] = a0[rinfo[i]][j];
1638: ri++;
1639: }
1640:
1641: /* solve Ax=B; A: rank x rank, B: rank x ri */
1642: /* algorithm
1643: c <- B
1644: x <- 0
1645: q <- 1
1646: do
1647: t <- A^(-1)c mod p
1648: x <- x+qt
1649: c <- (c-At)/p
1650: q <- qp
1651: end do
1652: then Ax-B=0 mod q and b=(B-Ax)/q hold after "do".
1653: */
1654: MKMAT(xmat,rank,ri); x = (Z **)(xmat)->body;
1655: MKMAT(*nmmat,rank,ri); nm = (Z **)(*nmmat)->body;
1656: wc = (int **)almat(rank,ri);
1657: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1658: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
1659:
1660: period = F4_INTRAT_PERIOD;
1661: for ( q = ONE, count = 0; ; ) {
1662: if ( DP_Print > 3 )
1663: fprintf(stderr,"o");
1664: /* wc = b mod md */
1665: for ( i = 0; i < rank; i++ )
1.3 ! noro 1666: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1.1 noro 1667: wi[j] = remqi((Q)bi[j],md);
1668: /* wc = A^(-1)wc; wc is normalized */
1669: solve_by_lu_mod(w,rank,md,wc,ri,1);
1670: /* x += q*wc */
1671: for ( i = 0; i < rank; i++ )
1672: for ( j = 0, wi = wc[i]; j < ri; j++ ) mul1addtoz(q,wi[j],&x[i][j]);
1.3 ! noro 1673: /* b =(b-A*wc)/md */
1.1 noro 1674: for ( i = 0; i < rank; i++ )
1675: for ( j = 0; j < ri; j++ ) {
1.3 ! noro 1676: mpz_t uz;
! 1677:
! 1678: if ( b[i][j] )
! 1679: mpz_init_set(uz,BDY(b[i][j]));
! 1680: else
! 1681: mpz_init_set_ui(uz,0);
! 1682: for ( k = 0; k < rank; k++ ) {
! 1683: if ( a[i][k] && wc[k][j] ) {
! 1684: if ( wc[k][j] < 0 )
! 1685: mpz_addmul_ui(uz,BDY(a[i][k]),-wc[k][j]);
! 1686: else
! 1687: mpz_submul_ui(uz,BDY(a[i][k]),wc[k][j]);
! 1688: }
! 1689: }
! 1690: MPZTOZ(uz,u);
1.1 noro 1691: divsz(u,mdq,&b[i][j]);
1692: }
1693: count++;
1694: /* q = q*md */
1695: mulz(q,mdq,&u); q = u;
1696: if ( count == period ) {
1697: ret = intmtoratm(xmat,q,*nmmat,dn);
1698: if ( ret ) {
1699: for ( j = k = l = 0; j < col; j++ )
1700: if ( cinfo[j] > 0 )
1701: rind[k++] = j;
1702: else if ( !cinfo[j] )
1703: cind[l++] = j;
1704: ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
1705: if ( ret ) {
1706: *rindp = rind;
1707: *cindp = cind;
1708: for ( j = k = 0; j < col; j++ )
1709: if ( !cinfo[j] )
1710: cind[k++] = j;
1711: return rank;
1712: }
1713: } else {
1714: period = period*3/2;
1715: count = 0;
1716: }
1717: }
1718: }
1719: }
1720: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>