version 1.34, 2003/11/27 02:20:51 |
version 1.47, 2005/11/27 00:07:05 |
|
|
* DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, |
* DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, |
* PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. |
* PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. |
* |
* |
* $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.33 2003/11/08 01:12:02 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.46 2005/02/08 18:06:05 saito Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "base.h" |
#include "base.h" |
#include "parse.h" |
#include "parse.h" |
#include "inline.h" |
#include "inline.h" |
|
|
|
#define F4_INTRAT_PERIOD 8 |
|
|
#if 0 |
#if 0 |
#undef DMAR |
#undef DMAR |
#define DMAR(a1,a2,a3,d,r) (r)=dmar(a1,a2,a3,d); |
#define DMAR(a1,a2,a3,d,r) (r)=dmar(a1,a2,a3,d); |
Line 87 void Pmat_swap_col_destructive(); |
|
Line 89 void Pmat_swap_col_destructive(); |
|
void Pvect(); |
void Pvect(); |
void Pmat(); |
void Pmat(); |
void Pmatc(); |
void Pmatc(); |
|
void Pnd_det(); |
|
|
struct ftab array_tab[] = { |
struct ftab array_tab[] = { |
{"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4}, |
{"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4}, |
Line 111 struct ftab array_tab[] = { |
|
Line 114 struct ftab array_tab[] = { |
|
{"ltov",Pltov,1}, |
{"ltov",Pltov,1}, |
{"size",Psize,1}, |
{"size",Psize,1}, |
{"det",Pdet,-2}, |
{"det",Pdet,-2}, |
|
{"nd_det",Pnd_det,-2}, |
{"invmat",Pinvmat,-2}, |
{"invmat",Pinvmat,-2}, |
{"leqm",Pleqm,2}, |
{"leqm",Pleqm,2}, |
{"leqm1",Pleqm1,2}, |
{"leqm1",Pleqm1,2}, |
Line 154 int generic_comp_obj(Obj *a,Obj *b) |
|
Line 158 int generic_comp_obj(Obj *a,Obj *b) |
|
} |
} |
|
|
|
|
void Pqsort(NODE arg,VECT *rp) |
void Pqsort(NODE arg,LIST *rp) |
{ |
{ |
VECT vect; |
VECT vect; |
NODE n; |
NODE n,n1; |
P p; |
P p; |
V v; |
V v; |
FUNC func; |
FUNC func; |
|
int len,i; |
|
pointer *a; |
|
Obj t; |
|
|
asir_assert(ARG0(arg),O_VECT,"qsort"); |
t = ARG0(arg); |
vect = (VECT)ARG0(arg); |
if (OID(t) == O_LIST) { |
|
n = (NODE)BDY((LIST)t); |
|
len = length(n); |
|
MKVECT(vect,len); |
|
for ( i = 0; i < len; i++, n = NEXT(n) ) { |
|
BDY(vect)[i] = BDY(n); |
|
} |
|
|
|
}else if (OID(t) != O_VECT) { |
|
error("qsort : invalid argument"); |
|
}else { |
|
vect = (VECT)t; |
|
} |
if ( argc(arg) == 1 ) |
if ( argc(arg) == 1 ) |
qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))comp_obj); |
qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))comp_obj); |
else { |
else { |
Line 181 void Pqsort(NODE arg,VECT *rp) |
|
Line 200 void Pqsort(NODE arg,VECT *rp) |
|
MKNODE(n,0,0); MKNODE(generic_comp_obj_arg,0,n); |
MKNODE(n,0,0); MKNODE(generic_comp_obj_arg,0,n); |
qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))generic_comp_obj); |
qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))generic_comp_obj); |
} |
} |
*rp = vect; |
if (OID(t) == O_LIST) { |
|
a = BDY(vect); |
|
for ( i = len - 1, n = 0; i >= 0; i-- ) { |
|
MKNODE(n1,a[i],n); n = n1; |
|
} |
|
MKLIST(*rp,n); |
|
}else { |
|
*rp = (LIST)vect; |
|
} |
} |
} |
|
|
void PNBmul_gf2n(NODE arg,GF2N *rp) |
void PNBmul_gf2n(NODE arg,GF2N *rp) |
Line 733 void Psize(NODE arg,LIST *rp) |
|
Line 760 void Psize(NODE arg,LIST *rp) |
|
n = ((MAT)ARG0(arg))->row; m = ((MAT)ARG0(arg))->col; |
n = ((MAT)ARG0(arg))->row; m = ((MAT)ARG0(arg))->col; |
STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s); |
STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s); |
break; |
break; |
|
case O_IMAT: |
|
n = ((IMAT)ARG0(arg))->row; m = ((IMAT)ARG0(arg))->col; |
|
STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s); |
|
break; |
default: |
default: |
error("size : invalid argument"); break; |
error("size : invalid argument"); break; |
} |
} |
Line 801 void Pinvmat(NODE arg,LIST *rp) |
|
Line 832 void Pinvmat(NODE arg,LIST *rp) |
|
input : a row x col matrix A |
input : a row x col matrix A |
A[I] <-> A[I][0]*x_0+A[I][1]*x_1+... |
A[I] <-> A[I][0]*x_0+A[I][1]*x_1+... |
|
|
output : [B,R,C] |
output : [B,D,R,C] |
B : a rank(A) x col-rank(A) matrix |
B : a rank(A) x col-rank(A) matrix |
|
D : the denominator |
R : a vector of length rank(A) |
R : a vector of length rank(A) |
C : a vector of length col-rank(A) |
C : a vector of length col-rank(A) |
B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+... |
B[I] <-> D*x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+... |
*/ |
*/ |
|
|
void Pgeneric_gauss_elim(NODE arg,LIST *rp) |
void Pgeneric_gauss_elim(NODE arg,LIST *rp) |
Line 844 void Pgeneric_gauss_elim(NODE arg,LIST *rp) |
|
Line 876 void Pgeneric_gauss_elim(NODE arg,LIST *rp) |
|
B : a rank(A) x col-rank(A) matrix |
B : a rank(A) x col-rank(A) matrix |
R : a vector of length rank(A) |
R : a vector of length rank(A) |
C : a vector of length col-rank(A) |
C : a vector of length col-rank(A) |
|
RN : a vector of length rank(A) indicating useful rows |
|
|
B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+... |
B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+... |
*/ |
*/ |
|
|
Line 851 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
Line 885 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
{ |
{ |
NODE n0; |
NODE n0; |
MAT m,mat; |
MAT m,mat; |
VECT rind,cind; |
VECT rind,cind,rnum; |
Q **tmat; |
Q **tmat; |
int **wmat; |
int **wmat,**row0; |
Q *rib,*cib; |
Q *rib,*cib,*rnb; |
int *colstat; |
int *colstat,*p; |
Q q; |
Q q; |
int md,i,j,k,l,row,col,t,rank; |
int md,i,j,k,l,row,col,t,rank; |
|
|
Line 864 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
Line 898 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg)); |
m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg)); |
row = m->row; col = m->col; tmat = (Q **)m->body; |
row = m->row; col = m->col; tmat = (Q **)m->body; |
wmat = (int **)almat(row,col); |
wmat = (int **)almat(row,col); |
|
|
|
row0 = (int **)ALLOCA(row*sizeof(int *)); |
|
for ( i = 0; i < row; i++ ) row0[i] = wmat[i]; |
|
|
colstat = (int *)MALLOC_ATOMIC(col*sizeof(int)); |
colstat = (int *)MALLOC_ATOMIC(col*sizeof(int)); |
for ( i = 0; i < row; i++ ) |
for ( i = 0; i < row; i++ ) |
for ( j = 0; j < col; j++ ) |
for ( j = 0; j < col; j++ ) |
Line 876 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
Line 914 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
wmat[i][j] = 0; |
wmat[i][j] = 0; |
rank = generic_gauss_elim_mod(wmat,row,col,md,colstat); |
rank = generic_gauss_elim_mod(wmat,row,col,md,colstat); |
|
|
|
MKVECT(rnum,rank); |
|
rnb = (Q *)rnum->body; |
|
for ( i = 0; i < rank; i++ ) |
|
for ( j = 0, p = wmat[i]; j < row; j++ ) |
|
if ( p == row0[j] ) |
|
STOQ(j,rnb[i]); |
|
|
MKMAT(mat,rank,col-rank); |
MKMAT(mat,rank,col-rank); |
tmat = (Q **)mat->body; |
tmat = (Q **)mat->body; |
for ( i = 0; i < rank; i++ ) |
for ( i = 0; i < rank; i++ ) |
Line 893 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
Line 938 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
} else { |
} else { |
STOQ(j,cib[l]); l++; |
STOQ(j,cib[l]); l++; |
} |
} |
n0 = mknode(3,mat,rind,cind); |
n0 = mknode(4,mat,rind,cind,rnum); |
MKLIST(*rp,n0); |
MKLIST(*rp,n0); |
} |
} |
|
|
|
|
add_eg(&eg_chrem_split,&tmp0,&tmp1); |
add_eg(&eg_chrem_split,&tmp0,&tmp1); |
|
|
get_eg(&tmp0); |
get_eg(&tmp0); |
if ( ind % 16 ) |
if ( ind % F4_INTRAT_PERIOD ) |
ret = 0; |
ret = 0; |
else |
else |
ret = intmtoratm(crmat,m1,*nm,dn); |
ret = intmtoratm(crmat,m1,*nm,dn); |
Line 1144 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1189 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
int *cinfo,*rinfo; |
int *cinfo,*rinfo; |
int *rind,*cind; |
int *rind,*cind; |
int count; |
int count; |
struct oEGT eg_mul,eg_inv,tmp0,tmp1; |
int ret; |
|
struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1; |
|
int period; |
|
int *wx,*ptr; |
|
int wxsize,nsize; |
|
N wn; |
|
Q wq; |
|
|
a0 = (Q **)mat->body; |
a0 = (Q **)mat->body; |
row = mat->row; col = mat->col; |
row = mat->row; col = mat->col; |
Line 1192 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1243 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
*cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int)); |
*cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int)); |
|
|
init_eg(&eg_mul); init_eg(&eg_inv); |
init_eg(&eg_mul); init_eg(&eg_inv); |
for ( q = ONE, count = 0; ; count++ ) { |
init_eg(&eg_check); init_eg(&eg_intrat); |
fprintf(stderr,"."); |
period = F4_INTRAT_PERIOD; |
|
nsize = period; |
|
wxsize = rank*ri*nsize; |
|
wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int)); |
|
for ( i = 0; i < wxsize; i++ ) wx[i] = 0; |
|
for ( q = ONE, count = 0; ; ) { |
|
if ( DP_Print > 3 ) |
|
fprintf(stderr,"o"); |
/* wc = -b mod md */ |
/* wc = -b mod md */ |
|
get_eg(&tmp0); |
for ( i = 0; i < rank; i++ ) |
for ( i = 0; i < rank; i++ ) |
for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ ) |
for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ ) |
if ( u = (Q)bi[j] ) { |
if ( u = (Q)bi[j] ) { |
Line 1204 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1263 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
wi[j] = t; |
wi[j] = t; |
} else |
} else |
wi[j] = 0; |
wi[j] = 0; |
/* wc = A^(-1)wc; wc is normalized */ |
/* wc = A^(-1)wc; wc is not normalized */ |
get_eg(&tmp0); |
solve_by_lu_mod(w,rank,md,wc,ri,0); |
solve_by_lu_mod(w,rank,md,wc,ri); |
/* wx += q*wc */ |
get_eg(&tmp1); |
ptr = wx; |
add_eg(&eg_inv,&tmp0,&tmp1); |
|
/* x = x-q*wc */ |
|
for ( i = 0; i < rank; i++ ) |
for ( i = 0; i < rank; i++ ) |
for ( j = 0, xi = x[i], wi = wc[i]; j < ri; j++ ) { |
for ( j = 0, wi = wc[i]; j < ri; j++ ) { |
STOQ(wi[j],u); mulq(q,u,&s); |
if ( wi[j] ) |
subq(xi[j],s,&u); xi[j] = u; |
muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr); |
|
ptr += nsize; |
} |
} |
|
count++; |
|
get_eg(&tmp1); |
|
add_eg(&eg_inv,&tmp0,&tmp1); |
get_eg(&tmp0); |
get_eg(&tmp0); |
for ( i = 0; i < rank; i++ ) |
for ( i = 0; i < rank; i++ ) |
for ( j = 0; j < ri; j++ ) { |
for ( j = 0; j < ri; j++ ) { |
Line 1232 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1293 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
add_eg(&eg_mul,&tmp0,&tmp1); |
add_eg(&eg_mul,&tmp0,&tmp1); |
/* q = q*md */ |
/* q = q*md */ |
mulq(q,mdq,&u); q = u; |
mulq(q,mdq,&u); q = u; |
if ( !(count % 16) && intmtoratm_q(xmat,NM(q),*nmmat,dn) ) { |
if ( count == period ) { |
for ( j = k = l = 0; j < col; j++ ) |
get_eg(&tmp0); |
if ( cinfo[j] ) |
ptr = wx; |
rind[k++] = j; |
for ( i = 0; i < rank; i++ ) |
else |
for ( j = 0, xi = x[i]; j < ri; |
cind[l++] = j; |
j++, ptr += nsize ) { |
if ( gensolve_check(mat,*nmmat,*dn,rind,cind) ) { |
for ( k = nsize-1; k >= 0 && !ptr[k]; k-- ); |
fprintf(stderr,"\n"); |
if ( k >= 0 ) { |
print_eg("INV",&eg_inv); |
wn = NALLOC(k+1); |
print_eg("MUL",&eg_mul); |
PL(wn) = k+1; |
fflush(asir_out); |
for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l]; |
return rank; |
NTOQ(wn,1,wq); |
|
subq(xi[j],wq,&u); xi[j] = u; |
|
} |
|
} |
|
ret = intmtoratm_q(xmat,NM(q),*nmmat,dn); |
|
get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1); |
|
if ( ret ) { |
|
for ( j = k = l = 0; j < col; j++ ) |
|
if ( cinfo[j] ) |
|
rind[k++] = j; |
|
else |
|
cind[l++] = j; |
|
get_eg(&tmp0); |
|
ret = gensolve_check(mat,*nmmat,*dn,rind,cind); |
|
get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1); |
|
if ( ret ) { |
|
if ( DP_Print > 3 ) { |
|
fprintf(stderr,"\n"); |
|
print_eg("INV",&eg_inv); |
|
print_eg("MUL",&eg_mul); |
|
print_eg("INTRAT",&eg_intrat); |
|
print_eg("CHECK",&eg_check); |
|
fflush(asir_out); |
|
} |
|
return rank; |
|
} |
|
} else { |
|
period = period*3/2; |
|
count = 0; |
|
nsize += period; |
|
wxsize += rank*ri*nsize; |
|
wx = (int *)REALLOC(wx,wxsize*sizeof(int)); |
|
for ( i = 0; i < wxsize; i++ ) wx[i] = 0; |
} |
} |
} |
} |
} |
} |
Line 1845 int find_lhs_and_lu_mod(unsigned int **a,int row,int c |
|
Line 1938 int find_lhs_and_lu_mod(unsigned int **a,int row,int c |
|
b = a^(-1)b |
b = a^(-1)b |
*/ |
*/ |
|
|
void solve_by_lu_mod(int **a,int n,int md,int **b,int l) |
void solve_by_lu_mod(int **a,int n,int md,int **b,int l,int normalize) |
{ |
{ |
unsigned int *y,*c; |
unsigned int *y,*c; |
int i,j,k; |
int i,j,k; |
Line 1878 void solve_by_lu_mod(int **a,int n,int md,int **b,int |
|
Line 1971 void solve_by_lu_mod(int **a,int n,int md,int **b,int |
|
DMAR(t,a[i][i],0,md,c[i]) |
DMAR(t,a[i][i],0,md,c[i]) |
} |
} |
/* copy c to b[.][k] with normalization */ |
/* copy c to b[.][k] with normalization */ |
for ( i = 0; i < n; i++ ) |
if ( normalize ) |
b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]); |
for ( i = 0; i < n; i++ ) |
|
b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]); |
|
else |
|
for ( i = 0; i < n; i++ ) |
|
b[i][k] = c[i]; |
} |
} |
} |
} |
|
|
Line 2890 void printimat(int **mat,int row,int col) |
|
Line 2987 void printimat(int **mat,int row,int col) |
|
} |
} |
printf("\n"); |
printf("\n"); |
} |
} |
|
} |
|
|
|
void Pnd_det(NODE arg,P *rp) |
|
{ |
|
if ( argc(arg) == 1 ) |
|
nd_det(0,ARG0(arg),rp); |
|
else |
|
nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp); |
} |
} |