version 1.12, 2019/03/17 02:48:13 |
version 1.21, 2021/10/08 07:27:05 |
|
|
/* $OpenXM: OpenXM_contrib2/asir2018/engine/Q.c,v 1.11 2019/03/03 05:21:17 noro Exp $ */ |
/* $OpenXM: OpenXM_contrib2/asir2018/engine/Q.c,v 1.20 2020/12/04 08:09:33 noro Exp $ */ |
#include "ca.h" |
#include "ca.h" |
#include "gmp.h" |
#include "gmp.h" |
#include "base.h" |
#include "base.h" |
#include "inline.h" |
#include "inline.h" |
|
|
mpz_t ONEMPZ; |
mpz_t ONEMPZ; |
Z ONE; |
extern Z ONE; |
int lf_lazy; |
int lf_lazy; |
Z current_mod_lf; |
Z current_mod_lf; |
int current_mod_lf_size; |
int current_mod_lf_size; |
Line 17 extern int DP_Print; |
|
Line 17 extern int DP_Print; |
|
|
|
void isqrtz(Z a,Z *r); |
void isqrtz(Z a,Z *r); |
void bshiftz(Z a,int n,Z *r); |
void bshiftz(Z a,int n,Z *r); |
|
int mpz_inttorat(mpz_t c,mpz_t m,mpz_t b,mpz_t nm,mpz_t dn); |
|
int generic_gauss_elim_hensel64(MAT mat,MAT *nmmat,Z *dn,int **rindp,int **cindp,DP *mb); |
|
int find_lhs_and_lu_mod64(mp_limb_t **a,int row,int col,mp_limb_t md,int **rinfo,int **cinfo); |
|
void solve_by_lu_mod64(mp_limb_t **a,int n,mp_limb_t md,mp_limb_signed_t **b,int l,int normalize); |
|
|
void *gc_realloc(void *p,size_t osize,size_t nsize) |
void *gc_realloc(void *p,size_t osize,size_t nsize) |
{ |
{ |
Line 93 void dnq(Q q,Z *r) |
|
Line 97 void dnq(Q q,Z *r) |
|
int sgnq(Q q) |
int sgnq(Q q) |
{ |
{ |
if ( !q ) return 0; |
if ( !q ) return 0; |
else if ( q->z ) return mpz_sgn(BDY((Z)q)); |
else if ( q->z ) { |
else return mpz_sgn(mpq_numref(BDY(q))); |
int sgn; |
|
sgn = mpz_sgn(BDY((Z)q)); |
|
return sgn; |
|
} else return mpz_sgn(mpq_numref(BDY(q))); |
} |
} |
|
|
Q mpqtozq(mpq_t a) |
Q mpqtozq(mpq_t a) |
Line 388 int cmpz(Z q1,Z q2) |
|
Line 395 int cmpz(Z q1,Z q2) |
|
else if ( sgn < 0 ) return -1; |
else if ( sgn < 0 ) return -1; |
else return 0; |
else return 0; |
} |
} |
|
/* XXX */ |
|
return 0; |
} |
} |
|
|
void gcdz(Z n1,Z n2,Z *nq) |
void gcdz(Z n1,Z n2,Z *nq) |
Line 515 void factorialz(unsigned int n,Z *nr) |
|
Line 524 void factorialz(unsigned int n,Z *nr) |
|
{ |
{ |
mpz_t a; |
mpz_t a; |
mpz_init(a); |
mpz_init(a); |
mpz_fac_ui(a,n); |
mpz_fac_ui(a,(unsigned long)n); |
MPZTOZ(a,*nr); |
MPZTOZ(a,*nr); |
} |
} |
|
|
Line 558 void subq(Q n1,Q n2,Q *nr) |
|
Line 567 void subq(Q n1,Q n2,Q *nr) |
|
|
|
if ( !n1 ) { |
if ( !n1 ) { |
if ( !n2 ) *nr = 0; |
if ( !n2 ) *nr = 0; |
else if ( n1->z ) chsgnz((Z)n1,(Z *)nr); |
else if ( n2->z ) chsgnz((Z)n2,(Z *)nr); |
else { |
else { |
mpq_init(t); mpq_neg(t,BDY(n2)); MPQTOQ(t,*nr); |
mpq_init(t); mpq_neg(t,BDY(n2)); MPQTOQ(t,*nr); |
} |
} |
Line 665 void pwrq(Q n1,Q n,Q *nr) |
|
Line 674 void pwrq(Q n1,Q n,Q *nr) |
|
nm[0] = mpq_denref(BDY(n1))[0]; |
nm[0] = mpq_denref(BDY(n1))[0]; |
dn[0] = mpq_numref(BDY(n1))[0]; |
dn[0] = mpq_numref(BDY(n1))[0]; |
} |
} |
|
if ( mpz_sgn(dn)<0 ) { |
|
mpz_neg(nm,nm); |
|
mpz_neg(dn,dn); |
|
} |
} else { |
} else { |
if ( n1->z ) { |
if ( n1->z ) { |
nm[0] = BDY((Z)n1)[0]; |
nm[0] = BDY((Z)n1)[0]; |
Line 762 void lgp(P p,Z *g,Z *l); |
|
Line 775 void lgp(P p,Z *g,Z *l); |
|
|
|
void ptozp(P p,int sgn,Q *c,P *pr) |
void ptozp(P p,int sgn,Q *c,P *pr) |
{ |
{ |
Z nm,dn; |
Z nm,dn,nm1; |
|
|
if ( !p ) { |
if ( !p ) { |
*c = 0; *pr = 0; |
*c = 0; *pr = 0; |
} else { |
} else { |
lgp(p,&nm,&dn); |
lgp(p,&nm,&dn); |
|
if ( sgn < 0 ) { |
|
chsgnz(nm,&nm1); nm = nm1; |
|
} |
divz(nm,dn,(Z *)c); |
divz(nm,dn,(Z *)c); |
divsp(CO,p,(P)*c,pr); |
divsp(CO,p,(P)*c,pr); |
} |
} |
Line 780 void lgp(P p,Z *g,Z *l) |
|
Line 796 void lgp(P p,Z *g,Z *l) |
|
|
|
if ( NUM(p) ) { |
if ( NUM(p) ) { |
if ( ((Q)p)->z ) { |
if ( ((Q)p)->z ) { |
MPZTOZ(BDY((Z)p),*g); |
absz((Z)p,g); |
*l = ONE; |
*l = ONE; |
} else { |
} else { |
MPZTOZ(mpq_numref(BDY((Q)p)),*g); |
MPZTOZ(mpq_numref(BDY((Q)p)),g1); absz(g1,g); |
MPZTOZ(mpq_denref(BDY((Q)p)),*l); |
MPZTOZ(mpq_denref(BDY((Q)p)),*l); |
} |
} |
} else { |
} else { |
Line 1229 int intmtoratm(MAT mat,Z md,MAT nm,Z *dn) |
|
Line 1245 int intmtoratm(MAT mat,Z md,MAT nm,Z *dn) |
|
return 0; |
return 0; |
row = mat->row; col = mat->col; |
row = mat->row; col = mat->col; |
bshiftz(md,1,&t); |
bshiftz(md,1,&t); |
isqrt(t,&s); |
isqrtz(t,&s); |
bshiftz(s,64,&b); |
bshiftz(s,64,&b); |
if ( !b ) b = ONE; |
if ( !b ) b = ONE; |
dn0 = ONE; |
dn0 = ONE; |
Line 1337 int mpz_inttorat(mpz_t c,mpz_t m,mpz_t b,mpz_t nm,mpz_ |
|
Line 1353 int mpz_inttorat(mpz_t c,mpz_t m,mpz_t b,mpz_t nm,mpz_ |
|
} |
} |
if ( mpz_cmp(v1,b) >= 0 ) return 0; |
if ( mpz_cmp(v1,b) >= 0 ) return 0; |
else { |
else { |
|
mpz_gcd(t,v1,v2); |
|
if ( UNIMPZ(t) ) |
|
mpz_set_ui(r1,0); |
|
else { |
|
/* v1 /= t, v2 /= t, t=c*v1-v2, r1=t%m */ |
|
mpz_divexact(v1,v1,t); mpz_divexact(v2,v2,t); |
|
mpz_mul(t,c,v1); mpz_sub(t,t,v2); mpz_mod(r1,t,m); |
|
} |
|
if ( mpz_sgn(r1) ) return 0; |
if ( mpz_sgn(v1)<0 ) { |
if ( mpz_sgn(v1)<0 ) { |
mpz_neg(dn,v1); mpz_neg(nm,v2); |
mpz_neg(dn,v1); mpz_neg(nm,v2); |
} else { |
} else { |
Line 1348 int mpz_inttorat(mpz_t c,mpz_t m,mpz_t b,mpz_t nm,mpz_ |
|
Line 1373 int mpz_inttorat(mpz_t c,mpz_t m,mpz_t b,mpz_t nm,mpz_ |
|
|
|
int inttorat(Z c,Z m,Z b,Z *nmp,Z *dnp) |
int inttorat(Z c,Z m,Z b,Z *nmp,Z *dnp) |
{ |
{ |
Z qq,t,u1,v1,r1; |
Z qq,t,s,r,u1,v1,r1; |
Z q,u2,v2,r2; |
Z q,u2,v2,r2; |
|
|
u1 = 0; v1 = ONE; u2 = m; v2 = c; |
u1 = 0; v1 = ONE; u2 = m; v2 = c; |
Line 1358 int inttorat(Z c,Z m,Z b,Z *nmp,Z *dnp) |
|
Line 1383 int inttorat(Z c,Z m,Z b,Z *nmp,Z *dnp) |
|
} |
} |
if ( cmpz(v1,b) >= 0 ) return 0; |
if ( cmpz(v1,b) >= 0 ) return 0; |
else { |
else { |
if ( mpz_sgn(BDY(v1))<0 ) { |
/* reduction and check */ |
chsgnz(v1,dnp); chsgnz(v2,nmp); |
/* v2/v1 = u2/u1, c*u1-u2 = 0 mod m? */ |
|
gcdz(v1,v2,&t); |
|
if ( UNIZ(t) ) { |
|
u1 = v1; u2 = v2; r = 0; |
} else { |
} else { |
*dnp = v1; *nmp = v2; |
divsz(v1,t,&u1); divsz(v2,t,&u2); |
|
mulz(c,u1,&t); subz(t,u2,&s); remz(s,m,&r); |
} |
} |
|
if ( r ) return 0; |
|
if ( mpz_sgn(BDY(u1))<0 ) { |
|
chsgnz(u1,dnp); chsgnz(u2,nmp); |
|
} else { |
|
*dnp = u1; *nmp = u2; |
|
} |
return 1; |
return 1; |
} |
} |
} |
} |
Line 2241 int generic_gauss_elim_hensel64(MAT mat,MAT *nmmat,Z * |
|
Line 2276 int generic_gauss_elim_hensel64(MAT mat,MAT *nmmat,Z * |
|
for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ ) |
for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ ) |
wi[j] = mpz_fdiv_ui(bi[j],md); |
wi[j] = mpz_fdiv_ui(bi[j],md); |
/* wc = A^(-1)wc; wc is not normalized */ |
/* wc = A^(-1)wc; wc is not normalized */ |
solve_by_lu_mod64(w,rank,md,wc,ri,0); |
solve_by_lu_mod64(w,rank,md,(mp_limb_signed_t **)wc,ri,0); |
/* x += q*wc */ |
/* x += q*wc */ |
for ( i = 0; i < rank; i++ ) |
for ( i = 0; i < rank; i++ ) |
for ( j = 0, wi = wc[i]; j < ri; j++ ) |
for ( j = 0, wi = wc[i]; j < ri; j++ ) |