=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/builtin/array.c,v retrieving revision 1.9 retrieving revision 1.22 diff -u -p -r1.9 -r1.22 --- OpenXM_contrib2/asir2000/builtin/array.c 2000/11/08 08:02:49 1.9 +++ OpenXM_contrib2/asir2000/builtin/array.c 2001/09/17 08:37:30 1.22 @@ -45,7 +45,7 @@ * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. * - * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.8 2000/09/21 09:19:25 noro Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.21 2001/09/17 07:16:58 noro Exp $ */ #include "ca.h" #include "base.h" @@ -57,7 +57,7 @@ #define DMAR(a1,a2,a3,d,r) (r)=dmar(a1,a2,a3,d); #endif -extern int Print; /* XXX */ +extern int DP_Print; /* XXX */ void inner_product_mat_int_mod(Q **,int **,int,int,int,Q *); void solve_by_lu_mod(int **,int,int,int **,int); @@ -67,6 +67,7 @@ void mat_to_gfmmat(MAT,unsigned int,GFMMAT *); int generic_gauss_elim_mod(int **,int,int,int,int *); int generic_gauss_elim(MAT ,MAT *,Q *,int **,int **); +void reduce_sp_by_red_mod_compress (int *,CDP *,int *,int,int,int); int gauss_elim_mod(int **,int,int,int); int gauss_elim_mod1(int **,int,int,int); @@ -90,6 +91,7 @@ void Px962_irredpoly_up2(); void Pirredpoly_up2(); void Pnbpoly_up2(); void Pqsort(); +void Pexponent_vector(); struct ftab array_tab[] = { {"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4}, @@ -97,7 +99,10 @@ struct ftab array_tab[] = { {"mat_to_gfmmat",Pmat_to_gfmmat,2}, {"generic_gauss_elim_mod",Pgeneric_gauss_elim_mod,2}, {"newvect",Pnewvect,-2}, + {"vector",Pnewvect,-2}, + {"exponent_vector",Pexponent_vector,-99999999}, {"newmat",Pnewmat,-3}, + {"matrix",Pnewmat,-3}, {"newbytearray",Pnewbytearray,-2}, {"sepmat_destructive",Psepmat_destructive,2}, {"sepvect",Psepvect,2}, @@ -376,6 +381,13 @@ VECT *rp; *rp = vect; } +void Pexponent_vector(arg,rp) +NODE arg; +DP *rp; +{ + nodetod(arg,rp); +} + void Pnewbytearray(arg,rp) NODE arg; BYTEARRAY *rp; @@ -383,6 +395,7 @@ BYTEARRAY *rp; int len,i,r; BYTEARRAY array; unsigned char *vb; + char *str; LIST list; NODE tn; @@ -392,15 +405,29 @@ BYTEARRAY *rp; error("newbytearray : invalid size"); MKBYTEARRAY(array,len); if ( argc(arg) == 2 ) { - list = (LIST)ARG1(arg); - asir_assert(list,O_LIST,"newbytearray"); - for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) ); - if ( r > len ) { - *rp = array; - return; + if ( !ARG1(arg) ) + error("newbytearray : invalid initialization"); + switch ( OID((Obj)ARG1(arg)) ) { + case O_LIST: + list = (LIST)ARG1(arg); + asir_assert(list,O_LIST,"newbytearray"); + for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) ); + if ( r <= len ) { + for ( i = 0, tn = BDY(list), vb = BDY(array); tn; + i++, tn = NEXT(tn) ) + vb[i] = (unsigned char)QTOS((Q)BDY(tn)); + } + break; + case O_STR: + str = BDY((STRING)ARG1(arg)); + r = strlen(str); + if ( r <= len ) + bcopy(str,BDY(array),r); + break; + default: + if ( !ARG1(arg) ) + error("newbytearray : invalid initialization"); } - for ( i = 0, tn = BDY(list), vb = BDY(array); tn; i++, tn = NEXT(tn) ) - vb[i] = (unsigned char)QTOS((Q)BDY(tn)); } *rp = array; } @@ -790,10 +817,10 @@ int **rindp,**cindp; colstat = (int *)MALLOC_ATOMIC(col*sizeof(int)); wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int)); for ( ind = 0; ; ind++ ) { - if ( Print ) { + if ( DP_Print ) { fprintf(asir_out,"."); fflush(asir_out); } - md = lprime[ind]; + md = get_lprime(ind); get_eg(&tmp0); for ( i = 0; i < row; i++ ) for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ ) @@ -827,13 +854,13 @@ RESET: } } else { if ( rank < rank0 ) { - if ( Print ) { + if ( DP_Print ) { fprintf(asir_out,"lower rank matrix; continuing...\n"); fflush(asir_out); } continue; } else if ( rank > rank0 ) { - if ( Print ) { + if ( DP_Print ) { fprintf(asir_out,"higher rank matrix; resetting...\n"); fflush(asir_out); } @@ -841,7 +868,7 @@ RESET: } else { for ( j = 0; (jrow; col = mat->col; w = (int **)almat(row,col); for ( ind = 0; ; ind++ ) { - md = lprime[ind]; + md = get_lprime(ind); STOQ(md,mdq); for ( i = 0; i < row; i++ ) for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ ) @@ -1020,7 +1050,7 @@ int **rindp,**cindp; add_eg(&eg_mul,&tmp0,&tmp1); /* q = q*md */ mulq(q,mdq,&u); q = u; - if ( !(count % 2) && intmtoratm_q(xmat,NM(q),*nmmat,dn) ) { + if ( !(count % 16) && intmtoratm_q(xmat,NM(q),*nmmat,dn) ) { for ( j = k = l = 0; j < col; j++ ) if ( cinfo[j] ) rind[k++] = j; @@ -1274,7 +1304,7 @@ int md; ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1 } - for ( ; k >= 0; k-- ) { + for ( ; k > 0; k-- ) { if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++; } } @@ -1361,25 +1391,114 @@ int md; hc = md-hc; s = redmat[i]+j; tj = sp+j; - for ( k = col-j; k >= 0; k-- ) { + for ( k = col-j; k > 0; k-- ) { if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++; } } } } +/* + mat[i] : compressed reducers (i=0,...,nred-1) + mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order +*/ + +int red_by_compress(m,p,r,ri,hc,len) +int m; +unsigned int *p; +register unsigned int *r; +register unsigned int *ri; +unsigned int hc; +register int len; +{ + unsigned int up,lo; + unsigned int dmy; + unsigned int *pj; + + p[*ri] = 0; r++; ri++; + for ( len--; len; len--, r++, ri++ ) { + pj = p+ *ri; + DMA(*r,hc,*pj,up,lo); + if ( up ) { + DSAB(m,up,lo,dmy,*pj); + } else + *pj = lo; + } +} + +/* p -= hc*r */ + +int red_by_vect(m,p,r,hc,len) +int m; +unsigned int *p,*r; +unsigned int hc; +int len; +{ + register unsigned int up,lo; + unsigned int dmy; + + *p++ = 0; r++; len--; + for ( ; len; len--, r++, p++ ) + if ( *r ) { + DMA(*r,hc,*p,up,lo); + if ( up ) { + DSAB(m,up,lo,dmy,*p); + } else + *p = lo; + } +} + +extern unsigned int **psca; + +void reduce_sp_by_red_mod_compress (sp,redmat,ind,nred,col,md) +int *sp; +CDP *redmat; +int *ind; +int nred,col; +int md; +{ + int i,j,k,len; + unsigned int *tj; + CDP ri; + unsigned int hc,up,lo,up1,lo1,c; + unsigned int *usp; + + usp = (unsigned int *)sp; + /* reduce the spolys by redmat */ + for ( i = nred-1; i >= 0; i-- ) { + /* reduce sp by redmat[i] */ + usp[ind[i]] %= md; + if ( hc = usp[ind[i]] ) { + /* sp = sp-hc*redmat[i] */ + hc = md-hc; + ri = redmat[i]; + len = ri->len; + red_by_compress(md,usp,psca[ri->psindex],ri->body,hc,len); + } + } + for ( i = 0; i < col; i++ ) + if ( usp[i] >= md ) + usp[i] %= md; +} + #define ONE_STEP2 if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++; -int generic_gauss_elim_mod(mat,row,col,md,colstat) -int **mat; +int generic_gauss_elim_mod(mat0,row,col,md,colstat) +int **mat0; int row,col,md; int *colstat; { int i,j,k,l,inv,a,rank,zzz; - int *t,*pivot,*pk,*tk; + unsigned int *t,*pivot,*pk,*tk; + unsigned int **mat; + mat = (unsigned int **)mat0; for ( rank = 0, j = 0; j < col; j++ ) { - for ( i = rank; i < row && !mat[i][j]; i++ ); + for ( i = rank; i < row; i++ ) + mat[i][j] %= md; + for ( i = rank; i < row; i++ ) + if ( mat[i][j] ) + break; if ( i == row ) { colstat[j] = 0; continue; @@ -1392,35 +1511,14 @@ int *colstat; inv = invm(pivot[j],md); for ( k = j, pk = pivot+k; k < col; k++, pk++ ) if ( *pk ) { + if ( *pk >= md ) + *pk %= md; DMAR(*pk,inv,0,md,*pk) } for ( i = rank+1; i < row; i++ ) { t = mat[i]; - if ( a = t[j] ) { - a = md - a; pk = pivot+j; tk = t+j; - k = col-j; - for ( ; k >= 64; k -= 64 ) { - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - } - for ( ; k >= 0; k -- ) { - if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++; - } - } + if ( a = t[j] ) + red_by_vect(md,t+j,pivot+j,md-a,col-j); } rank++; } @@ -1429,33 +1527,19 @@ int *colstat; pivot = mat[l]; for ( i = 0; i < l; i++ ) { t = mat[i]; - if ( a = t[j] ) { - a = md-a; pk = pivot+j; tk = t+j; - k = col-j; - for ( ; k >= 64; k -= 64 ) { - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2 - } - for ( ; k >= 0; k -- ) { - if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++; - } - } + t[j] %= md; + if ( a = t[j] ) + red_by_vect(md,t+j,pivot+j,md-a,col-j); } l--; + } + for ( j = 0, l = 0; l < rank; j++ ) + if ( colstat[j] ) { + t = mat[l]; + for ( k = j; k < col; k++ ) + if ( t[k] >= md ) + t[k] %= md; + l++; } return rank; }