=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2018/engine/Q.c,v retrieving revision 1.3 retrieving revision 1.7 diff -u -p -r1.3 -r1.7 --- OpenXM_contrib2/asir2018/engine/Q.c 2018/09/24 22:26:43 1.3 +++ OpenXM_contrib2/asir2018/engine/Q.c 2018/10/01 05:54:09 1.7 @@ -1,3 +1,4 @@ +/* $OpenXM: OpenXM_contrib2/asir2018/engine/Q.c,v 1.6 2018/10/01 05:49:06 noro Exp $ */ #include "ca.h" #include "gmp.h" #include "base.h" @@ -10,6 +11,10 @@ Z current_mod_lf; int current_mod_lf_size; gmp_randstate_t GMP_RAND; +#define F4_INTRAT_PERIOD 8 + +extern int DP_Print; + void isqrtz(Z a,Z *r); void bshiftz(Z a,int n,Z *r); @@ -31,13 +36,15 @@ void init_gmpq() gmp_randinit_default(GMP_RAND); } +void printexpr(VL,Obj); + void pmat(Z **a,int row,int col) { int i,j; for ( i = 0; i < row; i++, printf("\n") ) for ( j = 0; j < col; j++, printf(" ") ) - printexpr(CO,a[i][j]); + printexpr(CO,(Obj)a[i][j]); printf("\n"); } @@ -353,7 +360,7 @@ void pwrz(Z n1,Z n,Z *nr) } else if ( !smallz(n) ) { error("exponent too big."); *nr = 0; } else if ( n1->z && mpz_sgn(BDY((Z)n))>0 ) { - mpz_init(z); mpz_pow_ui(z,BDY(n1),QTOS(n)); MPZTOZ(z,*nr); + mpz_init(z); mpz_pow_ui(z,BDY(n1),ZTOS(n)); MPZTOZ(z,*nr); } else { MPZTOMPQ(BDY(n1),q); MPQTOQ(q,r); pwrq(r,(Q)n,&p); *nr = (Z)p; @@ -453,7 +460,7 @@ void gcdvz_estimate(VECT v,Z *q) else addz(s,b[i],&u); s = u; } - for ( i = 0, t = 0; i < n; i++ ) { + for ( t = 0; i < n; i++ ) { if ( b[i] && mpz_sgn(BDY(b[i]))<0 ) subz(t,b[i],&u); else addz(t,b[i],&u); t = u; @@ -461,6 +468,49 @@ void gcdvz_estimate(VECT v,Z *q) gcdz(s,t,q); } +void gcdv_mpz_estimate(mpz_t g,mpz_t *b,int n) +{ + int m,m2,i,j; + mpz_t s,t; + + mpz_init(g); + for ( i = 0, m = 0; i < n; i++ ) + if ( mpz_sgn(b[i]) ) m++; + if ( !m ) { + mpz_set_ui(g,0); + return; + } + if ( m == 1 ) { + for ( i = 0, m = 0; i < n; i++ ) + if ( mpz_sgn(b[i]) ) break; + if ( mpz_sgn(b[i])<0 ) mpz_neg(g,b[i]); + else mpz_set(g,b[i]); + return ; + } + m2 = m/2; + mpz_init_set_ui(s,0); + for ( i = j = 0; j < m2; i++ ) { + if ( mpz_sgn(b[i]) ) { + if ( mpz_sgn(b[i])<0 ) + mpz_sub(s,s,b[i]); + else + mpz_add(s,s,b[i]); + j++; + } + } + mpz_init_set_ui(t,0); + for ( ; i < n; i++ ) { + if ( mpz_sgn(b[i]) ) { + if ( mpz_sgn(b[i])<0 ) + mpz_sub(t,t,b[i]); + else + mpz_add(t,t,b[i]); + } + } + mpz_gcd(g,s,t); +} + + void factorialz(unsigned int n,Z *nr) { mpz_t a; @@ -605,7 +655,7 @@ void pwrq(Q n1,Q n,Q *nr) } else if ( !smallz((Z)n) ) { error("exponent too big."); *nr = 0; } else { - e = QTOS(n); + e = ZTOS(n); if ( e < 0 ) { e = -e; if ( n1->z ) { @@ -662,8 +712,8 @@ void mkbc(int n,Z *t) Z c,d,iq; for ( t[0] = ONE, i = 1; i <= n/2; i++ ) { - STOQ(n-i+1,c); mulz(t[i-1],c,&d); - STOQ(i,iq); divsz(d,iq,&t[i]); + STOZ(n-i+1,c); mulz(t[i-1],c,&d); + STOZ(i,iq); divsz(d,iq,&t[i]); } for ( ; i <= n; i++ ) t[i] = t[n-i]; @@ -841,10 +891,6 @@ unsigned int remqi(Q a,unsigned int mod) return c; } -extern int DP_Print; - -#define F4_INTRAT_PERIOD 8 - int generic_gauss_elim(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp) { int **wmat; @@ -855,6 +901,9 @@ int generic_gauss_elim(MAT mat,MAT *nm,Z *dn,int **rin MAT r,crmat; int ret; +#if SIZEOF_LONG == 8 + return generic_gauss_elim64(mat,nm,dn,rindp,cindp); +#endif bmat = (Z **)mat->body; row = mat->row; col = mat->col; wmat = (int **)almat(row,col); @@ -1326,7 +1375,7 @@ void isqrtz(Z a,Z *r) else { k = z_bits((Q)a); /* a <= 2^k-1 */ bshiftz(ONE,-((k>>1)+(k&1)),&x); /* a <= x^2 */ - STOQ(2,two); + STOZ(2,two); while ( 1 ) { pwrz(x,two,&t); if ( cmpz(t,a) <= 0 ) { @@ -1450,7 +1499,7 @@ init_eg(&eg_mul1); init_eg(&eg_mul2); w = (int **)almat(row,col); for ( ind = 0; ; ind++ ) { md = get_lprime(ind); - STOQ(md,mdq); + STOZ(md,mdq); for ( i = 0; i < row; i++ ) for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ ) wi[j] = remqi((Q)ai[j],md); @@ -1598,7 +1647,7 @@ int generic_gauss_elim_hensel_dalg(MAT mat,DP *mb,MAT w = (int **)almat(row,col); for ( ind = 0; ; ind++ ) { md = get_lprime(ind); - STOQ(md,mdq); + STOZ(md,mdq); for ( i = 0; i < row; i++ ) for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ ) wi[j] = remqi((Q)ai[j],md); @@ -1624,14 +1673,14 @@ int generic_gauss_elim_hensel_dalg(MAT mat,DP *mb,MAT a = (Z **)almat_pointer(rank,rank); /* lhs mat */ MKMAT(bmat,rank,col-rank); b = (Z **)bmat->body; /* lhs mat */ for ( j = li = ri = 0; j < col; j++ ) - if ( cinfo[j] ) { + if ( cinfo[j] > 0 ) { /* the column is in lhs */ for ( i = 0; i < rank; i++ ) { w[i][li] = w[i][j]; a[i][li] = a0[rinfo[i]][j]; } li++; - } else { + } else if ( !cinfo[j] ) { /* the column is in rhs */ for ( i = 0; i < rank; i++ ) b[i][ri] = a0[rinfo[i]][j]; @@ -1718,3 +1767,126 @@ int generic_gauss_elim_hensel_dalg(MAT mat,DP *mb,MAT } } } + +#if SIZEOF_LONG == 8 +mp_limb_t remqi64(Q a,mp_limb_t mod) +{ + mp_limb_t c,nm,dn; + mpz_t r; + + if ( !a ) return 0; + else if ( a->z ) { + mpz_init(r); + c = mpz_fdiv_r_ui(r,BDY((Z)a),mod); + } else { + mpz_init(r); + nm = mpz_fdiv_r_ui(r,mpq_numref(BDY(a)),mod); + dn = mpz_fdiv_r_ui(r,mpq_denref(BDY(a)),mod); + dn = invmod64(dn,mod); + c = mulmod64(nm,dn,mod); + } + return c; +} + +int generic_gauss_elim_mod64(mp_limb_t **mat,int row,int col,mp_limb_t md,int *colstat); +mp_limb_t get_lprime64(int ind); + +int generic_gauss_elim64(MAT mat,MAT *nm,Z *dn,int **rindp,int **cindp) +{ + mp_limb_t **wmat; + mp_limb_t *wmi; + mp_limb_t md,inv,t,t1; + Z **bmat,**tmat,*bmi,*tmi; + Z q,m1,m2,m3,s,u; + int *colstat,*wcolstat,*rind,*cind; + int row,col,ind,i,j,k,l,rank,rank0; + MAT r,crmat; + int ret; + + bmat = (Z **)mat->body; + row = mat->row; col = mat->col; + wmat = (mp_limb_t **)almat64(row,col); + colstat = (int *)MALLOC_ATOMIC(col*sizeof(int)); + wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int)); + for ( ind = 0; ; ind++ ) { + if ( DP_Print ) { + fprintf(asir_out,"."); fflush(asir_out); + } + md = get_lprime64(ind); + for ( i = 0; i < row; i++ ) + for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ ) + wmi[j] = remqi64((Q)bmi[j],md); + rank = generic_gauss_elim_mod64(wmat,row,col,md,wcolstat); + if ( !ind ) { +RESET: + UTOZ(md,m1); + rank0 = rank; + bcopy(wcolstat,colstat,col*sizeof(int)); + MKMAT(crmat,rank,col-rank); + MKMAT(r,rank,col-rank); *nm = r; + tmat = (Z **)crmat->body; + for ( i = 0; i < rank; i++ ) + for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ ) + if ( !colstat[j] ) { UTOZ(wmi[j],tmi[k]); k++; } + } else { + if ( rank < rank0 ) { + if ( DP_Print ) { + fprintf(asir_out,"lower rank matrix; continuing...\n"); + fflush(asir_out); + } + continue; + } else if ( rank > rank0 ) { + if ( DP_Print ) { + fprintf(asir_out,"higher rank matrix; resetting...\n"); + fflush(asir_out); + } + goto RESET; + } else { + for ( j = 0; (j