version 1.38, 2004/09/21 05:23:13 |
version 1.68, 2015/08/14 13:51:54 |
|
|
* 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.37 2004/09/15 01:43:32 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.67 2015/08/08 14:19:41 fujimoto 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" |
|
|
|
#include <sys/types.h> |
|
#include <sys/stat.h> |
|
#if !defined(_MSC_VER) |
|
#include <unistd.h> |
|
#endif |
|
|
#define F4_INTRAT_PERIOD 8 |
#define F4_INTRAT_PERIOD 8 |
|
|
#if 0 |
#if 0 |
Line 64 extern int DP_Print; /* XXX */ |
|
Line 70 extern int DP_Print; /* XXX */ |
|
|
|
void Pnewvect(), Pnewmat(), Psepvect(), Psize(), Pdet(), Pleqm(), Pleqm1(), Pgeninvm(); |
void Pnewvect(), Pnewmat(), Psepvect(), Psize(), Pdet(), Pleqm(), Pleqm1(), Pgeninvm(); |
void Pinvmat(); |
void Pinvmat(); |
void Pnewbytearray(); |
void Pnewbytearray(),Pmemoryplot_to_coord(); |
|
|
void Pgeneric_gauss_elim(); |
void Pgeneric_gauss_elim(); |
void Pgeneric_gauss_elim_mod(); |
void Pgeneric_gauss_elim_mod(); |
|
|
void Pmat(); |
void Pmat(); |
void Pmatc(); |
void Pmatc(); |
void Pnd_det(); |
void Pnd_det(); |
|
void Plu_mat(); |
|
void Pmat_col(); |
|
void Plusolve_prep(); |
|
void Plusolve_main(); |
|
|
struct ftab array_tab[] = { |
struct ftab array_tab[] = { |
|
{"lu_mat",Plu_mat,1}, |
{"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4}, |
{"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4}, |
{"lu_gfmmat",Plu_gfmmat,2}, |
{"lu_gfmmat",Plu_gfmmat,2}, |
{"mat_to_gfmmat",Pmat_to_gfmmat,2}, |
{"mat_to_gfmmat",Pmat_to_gfmmat,2}, |
Line 107 struct ftab array_tab[] = { |
|
Line 118 struct ftab array_tab[] = { |
|
{"matr",Pmat,-99999999}, |
{"matr",Pmat,-99999999}, |
{"matc",Pmatc,-99999999}, |
{"matc",Pmatc,-99999999}, |
{"newbytearray",Pnewbytearray,-2}, |
{"newbytearray",Pnewbytearray,-2}, |
|
{"memoryplot_to_coord",Pmemoryplot_to_coord,1}, |
{"sepmat_destructive",Psepmat_destructive,2}, |
{"sepmat_destructive",Psepmat_destructive,2}, |
{"sepvect",Psepvect,2}, |
{"sepvect",Psepvect,2}, |
{"qsort",Pqsort,-2}, |
{"qsort",Pqsort,-2}, |
Line 133 struct ftab array_tab[] = { |
|
Line 145 struct ftab array_tab[] = { |
|
{"nbpoly_up2",Pnbpoly_up2,2}, |
{"nbpoly_up2",Pnbpoly_up2,2}, |
{"mat_swap_row_destructive",Pmat_swap_row_destructive,3}, |
{"mat_swap_row_destructive",Pmat_swap_row_destructive,3}, |
{"mat_swap_col_destructive",Pmat_swap_col_destructive,3}, |
{"mat_swap_col_destructive",Pmat_swap_col_destructive,3}, |
|
{"mat_col",Pmat_col,2}, |
|
{"lusolve_prep",Plusolve_prep,1}, |
|
{"lusolve_main",Plusolve_main,1}, |
{0,0,0}, |
{0,0,0}, |
}; |
}; |
|
|
|
typedef struct _ent { int j; unsigned int e; } ent; |
|
|
|
ent *get_row(FILE *,int *l); |
|
void put_row(FILE *out,int l,ent *a); |
|
int lu_elim(int *l,ent **a,int k,int i,int mul,int mod); |
|
|
|
static int *ul,*ll; |
|
static ent **u,**l; |
|
static int modulus; |
|
|
|
void Plusolve_prep(NODE arg,Q *rp) |
|
{ |
|
char *fname; |
|
FILE *in; |
|
int len,i,rank; |
|
int *rhs; |
|
|
|
fname = BDY((STRING)ARG0(arg)); |
|
in = fopen(fname,"r"); |
|
modulus = getw(in); |
|
len = getw(in); |
|
ul = (int *)MALLOC_ATOMIC(len*sizeof(int)); |
|
u = (ent **)MALLOC(len*sizeof(ent *)); |
|
ll = (int *)MALLOC_ATOMIC(len*sizeof(int)); |
|
l = (ent **)MALLOC(len*sizeof(ent *)); |
|
for ( i = 0; i < len; i++ ) { |
|
u[i] = get_row(in,&ul[i]); |
|
} |
|
for ( i = 0; i < len; i++ ) { |
|
l[i] = get_row(in,&ll[i]); |
|
} |
|
fclose(in); |
|
*rp = ONE; |
|
} |
|
|
|
void Plusolve_main(NODE arg,VECT *rp) |
|
{ |
|
Q *d,*p; |
|
VECT v,r; |
|
int len,i; |
|
int *rhs; |
|
|
|
v = (VECT)ARG0(arg); len = v->len; |
|
d = (Q *)BDY(v); |
|
rhs = (int *)MALLOC_ATOMIC(len*sizeof(int)); |
|
for ( i = 0; i < len; i++ ) rhs[i] = QTOS(d[i]); |
|
solve_l(ll,l,len,rhs,modulus); |
|
solve_u(ul,u,len,rhs,modulus); |
|
NEWVECT(r); r->len = len; |
|
r->body = (pointer *)MALLOC(len*sizeof(pointer)); |
|
p = (Q *)r->body; |
|
for ( i = 0; i < len; i++ ) |
|
STOQ(rhs[i],p[i]); |
|
*rp = r; |
|
} |
|
|
|
ent *get_row(FILE *in,int *l) |
|
{ |
|
int len,i; |
|
ent *a; |
|
|
|
*l = len = getw(in); |
|
a = (ent *)MALLOC_ATOMIC(len*sizeof(ent)); |
|
for ( i = 0; i < len; i++ ) { |
|
a[i].j = getw(in); |
|
a[i].e = getw(in); |
|
} |
|
return a; |
|
} |
|
|
|
int lu_gauss(int *ul,ent **u,int *ll,ent **l,int n,int mod) |
|
{ |
|
int i,j,k,s,mul; |
|
unsigned int inv; |
|
int *ll2; |
|
|
|
ll2 = (int *)MALLOC_ATOMIC(n*sizeof(int)); |
|
for ( i = 0; i < n; i++ ) ll2[i] = 0; |
|
for ( i = 0; i < n; i++ ) { |
|
fprintf(stderr,"i=%d\n",i); |
|
inv = invm(u[i][0].e,mod); |
|
for ( k = i+1; k < n; k++ ) |
|
if ( u[k][0].j == n-i ) { |
|
s = u[k][0].e; |
|
DMAR(s,inv,0,mod,mul); |
|
lu_elim(ul,u,k,i,mul,mod); |
|
lu_append(ll,l,ll2,k,i,mul); |
|
} |
|
} |
|
} |
|
|
|
#define INITLEN 10 |
|
|
|
lu_append(int *l,ent **a,int *l2,int k,int i,int mul) |
|
{ |
|
int len; |
|
ent *p; |
|
|
|
len = l[k]; |
|
if ( !len ) { |
|
a[k] = p = (ent *)MALLOC_ATOMIC(INITLEN*sizeof(ent)); |
|
p[0].j = i; p[0].e = mul; |
|
l[k] = 1; l2[k] = INITLEN; |
|
} else { |
|
if ( l2[k] == l[k] ) { |
|
l2[k] *= 2; |
|
a[k] = REALLOC(a[k],l2[k]*sizeof(ent)); |
|
} |
|
p =a[k]; |
|
p[l[k]].j = i; p[l[k]].e = mul; |
|
l[k]++; |
|
} |
|
} |
|
|
|
/* a[k] = a[k]-mul*a[i] */ |
|
|
|
int lu_elim(int *l,ent **a,int k,int i,int mul,int mod) |
|
{ |
|
ent *ak,*ai,*w; |
|
int lk,li,j,m,p,q,r,s,t,j0; |
|
|
|
ak = a[k]; ai = a[i]; lk = l[k]; li = l[i]; |
|
w = (ent *)alloca((lk+li)*sizeof(ent)); |
|
p = 0; q = 0; j = 0; |
|
mul = mod-mul; |
|
while ( p < lk && q < li ) { |
|
if ( ak[p].j > ai[q].j ) { |
|
w[j] = ak[p]; j++; p++; |
|
} else if ( ak[p].j < ai[q].j ) { |
|
w[j].j = ai[q].j; |
|
t = ai[q].e; |
|
DMAR(t,mul,0,mod,r); |
|
w[j].e = r; |
|
j++; q++; |
|
} else { |
|
t = ai[q].e; s = ak[p].e; |
|
DMAR(t,mul,s,mod,r); |
|
if ( r ) { |
|
w[j].j = ai[q].j; w[j].e = r; j++; |
|
} |
|
p++; q++; |
|
} |
|
} |
|
if ( q == li ) |
|
while ( p < lk ) { |
|
w[j] = ak[p]; j++; p++; |
|
} |
|
else if ( p == lk ) |
|
while ( q < li ) { |
|
w[j].j = ai[q].j; |
|
t = ai[q].e; |
|
DMAR(t,mul,0,mod,r); |
|
w[j].e = r; |
|
j++; q++; |
|
} |
|
if ( j <= lk ) { |
|
for ( m = 0; m < j; m++ ) ak[m] = w[m]; |
|
} else { |
|
a[k] = ak = (ent *)MALLOC_ATOMIC(j*sizeof(ent)); |
|
for ( m = 0; m < j; m++ ) ak[m] = w[m]; |
|
} |
|
l[k] = j; |
|
} |
|
|
|
int solve_l(int *ll,ent **l,int n,int *rhs,int mod) |
|
{ |
|
int j,k,s,len; |
|
ent *p; |
|
|
|
for ( j = 0; j < n; j++ ) { |
|
len = ll[j]; p = l[j]; |
|
for ( k = 0, s = 0; k < len; k++ ) |
|
s = dmar(p[k].e,rhs[p[k].j],s,mod); |
|
rhs[j] -= s; |
|
if ( rhs[j] < 0 ) rhs[j] += mod; |
|
} |
|
} |
|
|
|
int solve_u(int *ul,ent **u,int n,int *rhs,int mod) |
|
{ |
|
int j,k,s,len,inv; |
|
ent *p; |
|
|
|
for ( j = n-1; j >= 0; j-- ) { |
|
len = ul[j]; p = u[j]; |
|
for ( k = 1, s = 0; k < len; k++ ) |
|
s = dmar(p[k].e,rhs[p[k].j],s,mod); |
|
rhs[j] -= s; |
|
if ( rhs[j] < 0 ) rhs[j] += mod; |
|
inv = invm((unsigned int)p[0].e,mod); |
|
rhs[j] = dmar(rhs[j],inv,0,mod); |
|
} |
|
} |
|
|
int comp_obj(Obj *a,Obj *b) |
int comp_obj(Obj *a,Obj *b) |
{ |
{ |
return arf_comp(CO,*a,*b); |
return arf_comp(CO,*a,*b); |
Line 143 int comp_obj(Obj *a,Obj *b) |
|
Line 352 int comp_obj(Obj *a,Obj *b) |
|
|
|
static FUNC generic_comp_obj_func; |
static FUNC generic_comp_obj_func; |
static NODE generic_comp_obj_arg; |
static NODE generic_comp_obj_arg; |
|
static NODE generic_comp_obj_option; |
|
|
int generic_comp_obj(Obj *a,Obj *b) |
int generic_comp_obj(Obj *a,Obj *b) |
{ |
{ |
Line 150 int generic_comp_obj(Obj *a,Obj *b) |
|
Line 360 int generic_comp_obj(Obj *a,Obj *b) |
|
|
|
BDY(generic_comp_obj_arg)=(pointer)(*a); |
BDY(generic_comp_obj_arg)=(pointer)(*a); |
BDY(NEXT(generic_comp_obj_arg))=(pointer)(*b); |
BDY(NEXT(generic_comp_obj_arg))=(pointer)(*b); |
r = (Q)bevalf(generic_comp_obj_func,generic_comp_obj_arg); |
r = (Q)bevalf_with_opts(generic_comp_obj_func,generic_comp_obj_arg,generic_comp_obj_option); |
if ( !r ) |
if ( !r ) |
return 0; |
return 0; |
else |
else |
Line 158 int generic_comp_obj(Obj *a,Obj *b) |
|
Line 368 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,n1; |
NODE n,n1; |
Line 197 void Pqsort(NODE arg,VECT *rp) |
|
Line 407 void Pqsort(NODE arg,VECT *rp) |
|
func = (FUNC)v->priv; |
func = (FUNC)v->priv; |
} |
} |
generic_comp_obj_func = func; |
generic_comp_obj_func = func; |
MKNODE(n,0,0); MKNODE(generic_comp_obj_arg,0,n); |
MKNODE(n,0,0); MKNODE(generic_comp_obj_arg,0,n); |
|
generic_comp_obj_option = current_option; |
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); |
} |
} |
if (OID(t) == O_LIST) { |
if (OID(t) == O_LIST) { |
Line 205 void Pqsort(NODE arg,VECT *rp) |
|
Line 416 void Pqsort(NODE arg,VECT *rp) |
|
for ( i = len - 1, n = 0; i >= 0; i-- ) { |
for ( i = len - 1, n = 0; i >= 0; i-- ) { |
MKNODE(n1,a[i],n); n = n1; |
MKNODE(n1,a[i],n); n = n1; |
} |
} |
MKLIST((LIST)*rp,n); |
MKLIST(*rp,n); |
}else { |
}else { |
*rp = vect; |
*rp = (LIST)vect; |
} |
} |
} |
} |
|
|
Line 331 void Psepmat_destructive(NODE arg,LIST *rp) |
|
Line 542 void Psepmat_destructive(NODE arg,LIST *rp) |
|
sgn = SGN(ent); |
sgn = SGN(ent); |
divn(nm,mod,&quo,&rem); |
divn(nm,mod,&quo,&rem); |
/* if ( quo != nm && rem != nm ) */ |
/* if ( quo != nm && rem != nm ) */ |
/* GC_free(nm); */ |
/* GCFREE(nm); */ |
/* GC_free(ent); */ |
/* GCFREE(ent); */ |
NTOQ(rem,sgn,a[i][j]); NTOQ(quo,sgn,a1[i][j]); |
NTOQ(rem,sgn,a[i][j]); NTOQ(quo,sgn,a1[i][j]); |
} |
} |
MKNODE(n1,mat1,0); MKNODE(n0,mat,n1); |
MKNODE(n1,mat1,0); MKNODE(n0,mat,n1); |
Line 384 void Pnewvect(NODE arg,VECT *rp) |
|
Line 595 void Pnewvect(NODE arg,VECT *rp) |
|
if ( argc(arg) == 2 ) { |
if ( argc(arg) == 2 ) { |
list = (LIST)ARG1(arg); |
list = (LIST)ARG1(arg); |
asir_assert(list,O_LIST,"newvect"); |
asir_assert(list,O_LIST,"newvect"); |
|
#if 0 |
for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) ); |
for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) ); |
if ( r > len ) { |
if ( r > len ) { |
*rp = vect; |
*rp = vect; |
return; |
return; |
} |
} |
|
#endif |
for ( i = 0, tn = BDY(list), vb = BDY(vect); tn; i++, tn = NEXT(tn) ) |
for ( i = 0, tn = BDY(list), vb = BDY(vect); tn; i++, tn = NEXT(tn) ) |
vb[i] = (pointer)BDY(tn); |
vb[i] = (pointer)BDY(tn); |
} |
} |
Line 396 void Pnewvect(NODE arg,VECT *rp) |
|
Line 609 void Pnewvect(NODE arg,VECT *rp) |
|
} |
} |
|
|
void Pvect(NODE arg,VECT *rp) { |
void Pvect(NODE arg,VECT *rp) { |
int len,i,r; |
int len,i; |
VECT vect; |
VECT vect; |
pointer *vb; |
pointer *vb; |
NODE tn; |
NODE tn; |
Line 443 void Pnewbytearray(NODE arg,BYTEARRAY *rp) |
|
Line 656 void Pnewbytearray(NODE arg,BYTEARRAY *rp) |
|
char *str; |
char *str; |
LIST list; |
LIST list; |
NODE tn; |
NODE tn; |
|
int ac; |
|
struct stat sbuf; |
|
char *fname; |
|
FILE *fp; |
|
|
asir_assert(ARG0(arg),O_N,"newbytearray"); |
ac = argc(arg); |
len = QTOS((Q)ARG0(arg)); |
if ( ac == 1 ) { |
if ( len < 0 ) |
if ( !OID((Obj)ARG0(arg)) ) error("newbytearray : invalid argument"); |
error("newbytearray : invalid size"); |
switch ( OID((Obj)ARG0(arg)) ) { |
MKBYTEARRAY(array,len); |
case O_STR: |
if ( argc(arg) == 2 ) { |
fname = BDY((STRING)ARG0(arg)); |
|
fp = fopen(fname,"rb"); |
|
if ( !fp ) error("newbytearray : fopen failed"); |
|
if ( stat(fname,&sbuf) < 0 ) |
|
error("newbytearray : stat failed"); |
|
len = sbuf.st_size; |
|
MKBYTEARRAY(array,len); |
|
fread(BDY(array),len,sizeof(char),fp); |
|
break; |
|
case O_N: |
|
if ( !RATN(ARG0(arg)) ) |
|
error("newbytearray : invalid argument"); |
|
len = QTOS((Q)ARG0(arg)); |
|
if ( len < 0 ) |
|
error("newbytearray : invalid size"); |
|
MKBYTEARRAY(array,len); |
|
break; |
|
default: |
|
error("newbytearray : invalid argument"); |
|
} |
|
} else if ( ac == 2 ) { |
|
asir_assert(ARG0(arg),O_N,"newbytearray"); |
|
len = QTOS((Q)ARG0(arg)); |
|
if ( len < 0 ) |
|
error("newbytearray : invalid size"); |
|
MKBYTEARRAY(array,len); |
if ( !ARG1(arg) ) |
if ( !ARG1(arg) ) |
error("newbytearray : invalid initialization"); |
error("newbytearray : invalid initialization"); |
switch ( OID((Obj)ARG1(arg)) ) { |
switch ( OID((Obj)ARG1(arg)) ) { |
Line 473 void Pnewbytearray(NODE arg,BYTEARRAY *rp) |
|
Line 715 void Pnewbytearray(NODE arg,BYTEARRAY *rp) |
|
if ( !ARG1(arg) ) |
if ( !ARG1(arg) ) |
error("newbytearray : invalid initialization"); |
error("newbytearray : invalid initialization"); |
} |
} |
} |
} else |
|
error("newbytearray : invalid argument"); |
*rp = array; |
*rp = array; |
} |
} |
|
|
|
#define MEMORY_GETPOINT(a,len,x,y) (((a)[(len)*(y)+((x)>>3)])&(1<<((x)&7))) |
|
|
|
void Pmemoryplot_to_coord(NODE arg,LIST *rp) |
|
{ |
|
int len,blen,y,i,j; |
|
unsigned char *a; |
|
NODE r0,r,n; |
|
LIST l; |
|
BYTEARRAY ba; |
|
Q iq,jq; |
|
|
|
asir_assert(ARG0(arg),O_LIST,"memoryplot_to_coord"); |
|
arg = BDY((LIST)ARG0(arg)); |
|
len = QTOS((Q)ARG0(arg)); |
|
blen = (len+7)/8; |
|
y = QTOS((Q)ARG1(arg)); |
|
ba = (BYTEARRAY)ARG2(arg); a = ba->body; |
|
r0 = 0; |
|
for ( j = 0; j < y; j++ ) |
|
for ( i = 0; i < len; i++ ) |
|
if ( MEMORY_GETPOINT(a,blen,i,j) ) { |
|
NEXTNODE(r0,r); |
|
STOQ(i,iq); STOQ(j,jq); |
|
n = mknode(2,iq,jq); |
|
MKLIST(l,n); |
|
BDY(r) = l; |
|
} |
|
if ( r0 ) NEXT(r) = 0; |
|
MKLIST(*rp,r0); |
|
} |
|
|
void Pnewmat(NODE arg,MAT *rp) |
void Pnewmat(NODE arg,MAT *rp) |
{ |
{ |
int row,col; |
int row,col; |
Line 621 void Pvtol(NODE arg,LIST *rp) |
|
Line 895 void Pvtol(NODE arg,LIST *rp) |
|
pointer *a; |
pointer *a; |
int len,i; |
int len,i; |
|
|
|
if ( OID(ARG0(arg)) == O_LIST ) { |
|
*rp = ARG0(arg); |
|
return; |
|
} |
asir_assert(ARG0(arg),O_VECT,"vtol"); |
asir_assert(ARG0(arg),O_VECT,"vtol"); |
v = (VECT)ARG0(arg); len = v->len; a = BDY(v); |
v = (VECT)ARG0(arg); len = v->len; a = BDY(v); |
for ( i = len - 1, n = 0; i >= 0; i-- ) { |
for ( i = len - 1, n = 0; i >= 0; i-- ) { |
Line 632 void Pvtol(NODE arg,LIST *rp) |
|
Line 910 void Pvtol(NODE arg,LIST *rp) |
|
void Pltov(NODE arg,VECT *rp) |
void Pltov(NODE arg,VECT *rp) |
{ |
{ |
NODE n; |
NODE n; |
VECT v; |
VECT v,v0; |
int len,i; |
int len,i; |
|
|
|
if ( OID(ARG0(arg)) == O_VECT ) { |
|
v0 = (VECT)ARG0(arg); len = v0->len; |
|
MKVECT(v,len); |
|
for ( i = 0; i < len; i++ ) { |
|
BDY(v)[i] = BDY(v0)[i]; |
|
} |
|
*rp = v; |
|
return; |
|
} |
asir_assert(ARG0(arg),O_LIST,"ltov"); |
asir_assert(ARG0(arg),O_LIST,"ltov"); |
n = (NODE)BDY((LIST)ARG0(arg)); |
n = (NODE)BDY((LIST)ARG0(arg)); |
len = length(n); |
len = length(n); |
Line 760 void Psize(NODE arg,LIST *rp) |
|
Line 1047 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 828 void Pinvmat(NODE arg,LIST *rp) |
|
Line 1119 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) |
{ |
{ |
NODE n0; |
NODE n0,opt,p; |
MAT m,nm; |
MAT m,nm; |
int *ri,*ci; |
int *ri,*ci; |
VECT rind,cind; |
VECT rind,cind; |
Q dn,q; |
Q dn,q; |
int i,j,k,l,row,col,t,rank; |
int i,row,col,t,rank; |
|
int is_hensel = 0; |
|
char *key; |
|
Obj value; |
|
|
|
if ( current_option ) { |
|
for ( opt = current_option; opt; opt = NEXT(opt) ) { |
|
p = BDY((LIST)BDY(opt)); |
|
key = BDY((STRING)BDY(p)); |
|
value = (Obj)BDY(NEXT(p)); |
|
if ( !strcmp(key,"hensel") && value ) { |
|
is_hensel = value ? 1 : 0; |
|
break; |
|
} |
|
} |
|
} |
asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim"); |
asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim"); |
m = (MAT)ARG0(arg); |
m = (MAT)ARG0(arg); |
row = m->row; col = m->col; |
row = m->row; col = m->col; |
rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci); |
if ( is_hensel ) |
|
rank = generic_gauss_elim_hensel(m,&nm,&dn,&ri,&ci); |
|
else |
|
rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci); |
t = col-rank; |
t = col-rank; |
MKVECT(rind,rank); |
MKVECT(rind,rank); |
MKVECT(cind,t); |
MKVECT(cind,t); |
Line 871 void Pgeneric_gauss_elim(NODE arg,LIST *rp) |
|
Line 1180 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 878 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
Line 1189 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 891 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
Line 1202 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 903 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
Line 1218 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 920 void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp) |
|
Line 1242 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); |
} |
} |
|
|
|
|
} |
} |
} |
} |
|
|
|
void lu_dec_cr(MAT mat,MAT lu,Q *dn,int **perm); |
|
|
|
/* XXX broken */ |
|
void lu_dec_cr(MAT mat,MAT lu,Q *dn,int **perm) |
|
{ |
|
Q **a0,**b; |
|
Q *aiq; |
|
N **a; |
|
N *ai; |
|
Q q,q1,dn2,a1,q0,bik; |
|
MAT m; |
|
unsigned int md; |
|
int n,ind,i,j,rank,t,inv,t1,ret,min,k; |
|
int **w; |
|
int *wi,*rinfo0,*rinfo; |
|
N m1,m2,m3,u,s; |
|
|
|
a0 = (Q **)mat->body; |
|
n = mat->row; |
|
if ( n != mat->col ) |
|
error("lu_dec_cr : non-square matrix"); |
|
w = (int **)almat(n,n); |
|
MKMAT(m,n,n); |
|
a = (N **)m->body; |
|
UTON(1,m1); |
|
rinfo0 = 0; |
|
ind = 0; |
|
while ( 1 ) { |
|
md = get_lprime(ind); |
|
/* mat mod md */ |
|
for ( i = 0; i < n; i++ ) |
|
for ( j = 0, aiq = a0[i], wi = w[i]; j < n; j++ ) |
|
if ( q = aiq[j] ) { |
|
t = rem(NM(q),md); |
|
if ( t && SGN(q) < 0 ) |
|
t = (md - t) % md; |
|
wi[j] = t; |
|
} else |
|
wi[j] = 0; |
|
|
|
if ( !lu_mod((unsigned int **)w,n,md,&rinfo) ) continue; |
|
printf("."); fflush(stdout); |
|
if ( !rinfo0 ) |
|
*perm = rinfo0 = rinfo; |
|
else { |
|
for ( i = 0; i < n; i++ ) |
|
if ( rinfo[i] != rinfo0[i] ) break; |
|
if ( i < n ) continue; |
|
} |
|
if ( UNIN(m1) ) { |
|
for ( i = 0; i < n; i++ ) |
|
for ( j = 0, ai = a[i], wi = w[i]; j < n; j++ ) { |
|
UTON(wi[j],u); ai[j] = u; |
|
} |
|
UTON(md,m1); |
|
} else { |
|
inv = invm(rem(m1,md),md); |
|
UTON(md,m2); muln(m1,m2,&m3); |
|
for ( i = 0; i < n; i++ ) |
|
for ( j = 0, ai = a[i], wi = w[i]; j < n; j++ ) |
|
if ( ai[i] ) { |
|
/* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */ |
|
t = rem(ai[j],md); |
|
if ( wi[j] >= t ) |
|
t = wi[j]-t; |
|
else |
|
t = md-(t-wi[j]); |
|
DMAR(t,inv,0,md,t1) |
|
UTON(t1,u); |
|
muln(m1,u,&s); |
|
addn(ai[j],s,&u); ai[j] = u; |
|
} else if ( wi[j] ) { |
|
/* f3 = m1*(m1 mod m2)^(-1)*f2 */ |
|
DMAR(wi[j],inv,0,md,t) |
|
UTON(t,u); |
|
muln(m1,u,&s); ai[j] = s; |
|
} |
|
m1 = m3; |
|
} |
|
if ( (++ind%8) == 0 ) { |
|
ret = intmtoratm(m,m1,lu,dn); |
|
if ( ret ) { |
|
b = (Q **)lu->body; |
|
mulq(*dn,*dn,&dn2); |
|
for ( i = 0; i < n; i++ ) { |
|
for ( j = 0; j < n; j++ ) { |
|
q = 0; |
|
min = MIN(i,j); |
|
for ( k = 0; k <= min; k++ ) { |
|
bik = k==i ? *dn : b[i][k]; |
|
mulq(bik,b[k][j],&q0); |
|
addq(q,q0,&q1); q = q1; |
|
} |
|
mulq(a0[rinfo0[i]][j],dn2,&q1); |
|
if ( cmpq(q,q1) ) break; |
|
} |
|
if ( j < n ) break; |
|
} |
|
if ( i == n ) |
|
return; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void nmat(N **m,int n) |
|
{ |
|
int i,j; |
|
|
|
for ( i = 0; i < n; i++ ) { |
|
for ( j = 0; j < n; j++ ) { |
|
printn(m[i][j]); printf(" "); |
|
} |
|
printf("\n"); |
|
} |
|
} |
|
|
int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn,int **rindp,int **cindp) |
int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn,int **rindp,int **cindp) |
{ |
{ |
MAT bmat,xmat; |
MAT bmat,xmat; |
Line 1171 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1610 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 1189 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1634 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
} else |
} else |
wi[j] = 0; |
wi[j] = 0; |
|
|
|
if ( DP_Print > 3 ) { |
|
fprintf(asir_out,"LU decomposition.."); fflush(asir_out); |
|
} |
rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo); |
rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo); |
|
if ( DP_Print > 3 ) { |
|
fprintf(asir_out,"done.\n"); fflush(asir_out); |
|
} |
a = (Q **)almat_pointer(rank,rank); /* lhs mat */ |
a = (Q **)almat_pointer(rank,rank); /* lhs mat */ |
MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */ |
MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */ |
for ( j = li = ri = 0; j < col; j++ ) |
for ( j = li = ri = 0; j < col; j++ ) |
Line 1219 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1670 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 1231 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1690 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 */ |
|
ptr = wx; |
|
for ( i = 0; i < rank; i++ ) |
|
for ( j = 0, wi = wc[i]; j < ri; j++ ) { |
|
if ( wi[j] ) |
|
muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr); |
|
ptr += nsize; |
|
} |
|
count++; |
get_eg(&tmp1); |
get_eg(&tmp1); |
add_eg(&eg_inv,&tmp0,&tmp1); |
add_eg(&eg_inv,&tmp0,&tmp1); |
/* x = x-q*wc */ |
get_eg(&tmp0); |
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; j < ri; j++ ) { |
STOQ(wi[j],u); mulq(q,u,&s); |
inner_product_mat_int_mod(a,wc,rank,i,j,&u); |
subq(xi[j],s,&u); xi[j] = u; |
addq(b[i][j],u,&s); |
|
if ( s ) { |
|
t = divin(NM(s),md,&tn); |
|
if ( t ) |
|
error("generic_gauss_elim_hensel:incosistent"); |
|
NTOQ(tn,SGN(s),b[i][j]); |
|
} else |
|
b[i][j] = 0; |
} |
} |
|
get_eg(&tmp1); |
|
add_eg(&eg_mul,&tmp0,&tmp1); |
|
/* q = q*md */ |
|
mulq(q,mdq,&u); q = u; |
|
if ( count == period ) { |
|
get_eg(&tmp0); |
|
ptr = wx; |
|
for ( i = 0; i < rank; i++ ) |
|
for ( j = 0, xi = x[i]; j < ri; |
|
j++, ptr += nsize ) { |
|
for ( k = nsize-1; k >= 0 && !ptr[k]; k-- ); |
|
if ( k >= 0 ) { |
|
wn = NALLOC(k+1); |
|
PL(wn) = k+1; |
|
for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l]; |
|
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 ) { |
|
rind = (int *)MALLOC_ATOMIC(rank*sizeof(int)); |
|
cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int)); |
|
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); |
|
} |
|
*rindp = rind; |
|
*cindp = cind; |
|
for ( j = k = 0; j < col; j++ ) |
|
if ( !cinfo[j] ) |
|
cind[k++] = j; |
|
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; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
int generic_gauss_elim_hensel_dalg(MAT mat,DP *mb,MAT *nmmat,Q *dn,int **rindp,int **cindp) |
|
{ |
|
MAT bmat,xmat; |
|
Q **a0,**a,**b,**x,**nm; |
|
Q *ai,*bi,*xi; |
|
int row,col; |
|
int **w; |
|
int *wi; |
|
int **wc; |
|
Q mdq,q,s,u; |
|
N tn; |
|
int ind,md,i,j,k,l,li,ri,rank; |
|
unsigned int t; |
|
int *cinfo,*rinfo; |
|
int *rind,*cind; |
|
int count; |
|
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; |
|
NumberField nf; |
|
DP m; |
|
int col1; |
|
|
|
a0 = (Q **)mat->body; |
|
row = mat->row; col = mat->col; |
|
w = (int **)almat(row,col); |
|
for ( ind = 0; ; 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++ ) |
|
if ( q = (Q)ai[j] ) { |
|
t = rem(NM(q),md); |
|
if ( t && SGN(q) < 0 ) |
|
t = (md - t) % md; |
|
wi[j] = t; |
|
} else |
|
wi[j] = 0; |
|
|
|
if ( DP_Print ) { |
|
fprintf(asir_out,"LU decomposition.."); fflush(asir_out); |
|
} |
|
rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo); |
|
if ( DP_Print ) { |
|
fprintf(asir_out,"done.\n"); fflush(asir_out); |
|
} |
|
for ( i = 0; i < col-1; i++ ) { |
|
if ( !cinfo[i] ) { |
|
m = mb[i]; |
|
for ( j = i+1; j < col-1; j++ ) |
|
if ( dp_redble(mb[j],m) ) |
|
cinfo[j] = -1; |
|
} |
|
} |
|
a = (Q **)almat_pointer(rank,rank); /* lhs mat */ |
|
MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */ |
|
for ( j = li = ri = 0; j < col; 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 if ( !cinfo[j] ) { |
|
/* the column is in rhs */ |
|
for ( i = 0; i < rank; i++ ) |
|
b[i][ri] = a0[rinfo[i]][j]; |
|
ri++; |
|
} |
|
|
|
/* solve Ax+B=0; A: rank x rank, B: rank x ri */ |
|
MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body; |
|
MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body; |
|
/* use the right part of w as work area */ |
|
wc = (int **)almat(rank,ri); |
|
for ( i = 0; i < rank; i++ ) |
|
wc[i] = w[i]+rank; |
|
*rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int)); |
|
*cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int)); |
|
init_eg(&eg_mul); init_eg(&eg_inv); |
|
init_eg(&eg_check); init_eg(&eg_intrat); |
|
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 ) |
|
fprintf(stderr,"o"); |
|
/* wc = -b mod md */ |
get_eg(&tmp0); |
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++ ) |
|
if ( u = (Q)bi[j] ) { |
|
t = rem(NM(u),md); |
|
if ( t && SGN(u) > 0 ) |
|
t = (md - t) % md; |
|
wi[j] = t; |
|
} else |
|
wi[j] = 0; |
|
/* wc = A^(-1)wc; wc is not normalized */ |
|
solve_by_lu_mod(w,rank,md,wc,ri,0); |
|
/* wx += q*wc */ |
|
ptr = wx; |
|
for ( i = 0; i < rank; i++ ) |
|
for ( j = 0, wi = wc[i]; j < ri; j++ ) { |
|
if ( wi[j] ) |
|
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); |
|
for ( i = 0; i < rank; i++ ) |
for ( j = 0; j < ri; j++ ) { |
for ( j = 0; j < ri; j++ ) { |
inner_product_mat_int_mod(a,wc,rank,i,j,&u); |
inner_product_mat_int_mod(a,wc,rank,i,j,&u); |
addq(b[i][j],u,&s); |
addq(b[i][j],u,&s); |
Line 1259 int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn |
|
Line 1912 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 % F4_INTRAT_PERIOD) && 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] > 0 ) |
|
rind[k++] = j; |
|
else if ( !cinfo[j] ) |
|
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 1610 void red_by_compress(int m,unsigned int *p,unsigned in |
|
Line 2295 void red_by_compress(int m,unsigned int *p,unsigned in |
|
|
|
void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len) |
void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len) |
{ |
{ |
register unsigned int up,lo; |
unsigned int up,lo,dmy; |
unsigned int dmy; |
|
|
|
*p++ = 0; r++; len--; |
*p++ = 0; r++; len--; |
for ( ; len; len--, r++, p++ ) |
for ( ; len; len--, r++, p++ ) |
Line 1720 int generic_gauss_elim_mod(int **mat0,int row,int col, |
|
Line 2404 int generic_gauss_elim_mod(int **mat0,int row,int col, |
|
return rank; |
return rank; |
} |
} |
|
|
|
int generic_gauss_elim_mod2(int **mat0,int row,int col,int md,int *colstat,int *rowstat) |
|
{ |
|
int i,j,k,l,inv,a,rank; |
|
unsigned int *t,*pivot,*pk; |
|
unsigned int **mat; |
|
|
|
for ( i = 0; i < row; i++ ) rowstat[i] = i; |
|
mat = (unsigned int **)mat0; |
|
for ( rank = 0, j = 0; j < col; j++ ) { |
|
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; |
|
} else |
|
colstat[j] = 1; |
|
if ( i != rank ) { |
|
t = mat[i]; mat[i] = mat[rank]; mat[rank] = t; |
|
k = rowstat[i]; rowstat[i] = rowstat[rank]; rowstat[rank] = k; |
|
} |
|
pivot = mat[rank]; |
|
inv = invm(pivot[j],md); |
|
for ( k = j, pk = pivot+k; k < col; k++, pk++ ) |
|
if ( *pk ) { |
|
if ( *pk >= (unsigned int)md ) |
|
*pk %= md; |
|
DMAR(*pk,inv,0,md,*pk) |
|
} |
|
for ( i = rank+1; i < row; i++ ) { |
|
t = mat[i]; |
|
if ( a = t[j] ) |
|
red_by_vect(md,t+j,pivot+j,md-a,col-j); |
|
} |
|
rank++; |
|
} |
|
for ( j = col-1, l = rank-1; j >= 0; j-- ) |
|
if ( colstat[j] ) { |
|
pivot = mat[l]; |
|
for ( i = 0; i < l; i++ ) { |
|
t = mat[i]; |
|
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] >= (unsigned int)md ) |
|
t[k] %= md; |
|
l++; |
|
} |
|
return rank; |
|
} |
|
|
int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat) |
int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat) |
{ |
{ |
int i,j,k,l,inv,a,rank; |
int i,j,k,l,inv,a,rank; |
Line 1863 int find_lhs_and_lu_mod(unsigned int **a,int row,int c |
|
Line 2607 int find_lhs_and_lu_mod(unsigned int **a,int row,int c |
|
return d; |
return d; |
} |
} |
|
|
|
int lu_mod(unsigned int **a,int n,unsigned int md,int **rinfo) |
|
{ |
|
int i,j,k; |
|
int *rp; |
|
unsigned int *t,*pivot; |
|
unsigned int inv,m; |
|
|
|
*rinfo = rp = (int *)MALLOC_ATOMIC(n*sizeof(int)); |
|
for ( i = 0; i < n; i++ ) rp[i] = i; |
|
for ( k = 0; k < n; k++ ) { |
|
for ( i = k; i < n && !a[i][k]; i++ ); |
|
if ( i == n ) return 0; |
|
if ( i != k ) { |
|
j = rp[i]; rp[i] = rp[k]; rp[k] = j; |
|
t = a[i]; a[i] = a[k]; a[k] = t; |
|
} |
|
pivot = a[k]; |
|
inv = invm(pivot[k],md); |
|
for ( i = k+1; i < n; i++ ) { |
|
t = a[i]; |
|
if ( m = t[k] ) { |
|
DMAR(inv,m,0,md,t[k]) |
|
for ( j = k+1, m = md - t[k]; j < n; j++ ) |
|
if ( pivot[j] ) { |
|
unsigned int tj; |
|
DMAR(m,pivot[j],t[j],md,tj) |
|
t[j] = tj; |
|
} |
|
} |
|
} |
|
} |
|
return 1; |
|
} |
|
|
/* |
/* |
Input |
Input |
a : n x n matrix; a result of LU-decomposition |
a : n x n matrix; a result of LU-decomposition |
Line 1872 int find_lhs_and_lu_mod(unsigned int **a,int row,int c |
|
Line 2650 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 1905 void solve_by_lu_mod(int **a,int n,int md,int **b,int |
|
Line 2683 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 2122 void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md, |
|
Line 2904 void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md, |
|
} |
} |
} |
} |
|
|
|
void Plu_mat(NODE arg,LIST *rp) |
|
{ |
|
MAT m,lu; |
|
Q dn; |
|
Q *v; |
|
int n,i; |
|
int *iperm; |
|
VECT perm; |
|
NODE n0; |
|
|
|
asir_assert(ARG0(arg),O_MAT,"lu_mat"); |
|
m = (MAT)ARG0(arg); |
|
n = m->row; |
|
MKMAT(lu,n,n); |
|
lu_dec_cr(m,lu,&dn,&iperm); |
|
MKVECT(perm,n); |
|
for ( i = 0, v = (Q *)perm->body; i < n; i++ ) |
|
STOQ(iperm[i],v[i]); |
|
n0 = mknode(3,lu,dn,perm); |
|
MKLIST(*rp,n0); |
|
} |
|
|
void Plu_gfmmat(NODE arg,LIST *rp) |
void Plu_gfmmat(NODE arg,LIST *rp) |
{ |
{ |
MAT m; |
MAT m; |
Line 2133 void Plu_gfmmat(NODE arg,LIST *rp) |
|
Line 2937 void Plu_gfmmat(NODE arg,LIST *rp) |
|
VECT perm; |
VECT perm; |
NODE n0; |
NODE n0; |
|
|
asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat"); |
asir_assert(ARG0(arg),O_MAT,"lu_gfmmat"); |
asir_assert(ARG1(arg),O_N,"mat_to_gfmmat"); |
asir_assert(ARG1(arg),O_N,"lu_gfmmat"); |
m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg)); |
m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg)); |
mat_to_gfmmat(m,md,&mm); |
mat_to_gfmmat(m,md,&mm); |
row = m->row; |
row = m->row; |
Line 2432 void inner_product_int(Q *a,Q *b,int n,Q *r) |
|
Line 3236 void inner_product_int(Q *a,Q *b,int n,Q *r) |
|
t = wma; wma = sum; sum = t; |
t = wma; wma = sum; sum = t; |
} |
} |
} |
} |
GC_free(wm); |
GCFREE(wm); |
GC_free(wma); |
GCFREE(wma); |
if ( !sgn ) { |
if ( !sgn ) { |
GC_free(sum); |
GCFREE(sum); |
*r = 0; |
*r = 0; |
} else |
} else |
NTOQ(sum,sgn,*r); |
NTOQ(sum,sgn,*r); |
Line 2490 void inner_product_mat_int_mod(Q **a,int **b,int n,int |
|
Line 3294 void inner_product_mat_int_mod(Q **a,int **b,int n,int |
|
t = wma; wma = sum; sum = t; |
t = wma; wma = sum; sum = t; |
} |
} |
} |
} |
GC_free(wm); |
GCFREE(wm); |
GC_free(wma); |
GCFREE(wma); |
if ( !sgn ) { |
if ( !sgn ) { |
GC_free(sum); |
GCFREE(sum); |
*r = 0; |
*r = 0; |
} else |
} else |
NTOQ(sum,sgn,*r); |
NTOQ(sum,sgn,*r); |
Line 2925 void Pnd_det(NODE arg,P *rp) |
|
Line 3729 void Pnd_det(NODE arg,P *rp) |
|
nd_det(0,ARG0(arg),rp); |
nd_det(0,ARG0(arg),rp); |
else |
else |
nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp); |
nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp); |
|
} |
|
|
|
void Pmat_col(NODE arg,VECT *rp) |
|
{ |
|
int i,j,n; |
|
MAT mat; |
|
VECT vect; |
|
|
|
asir_assert(ARG0(arg),O_MAT,"mat_col"); |
|
asir_assert(ARG1(arg),O_N,"mat_col"); |
|
mat = (MAT)ARG0(arg); |
|
j = QTOS((Q)ARG1(arg)); |
|
if ( j < 0 || j >= mat->col) { |
|
error("mat_col : Out of range"); |
|
} |
|
n = mat->row; |
|
MKVECT(vect,n); |
|
for(i=0; i<n; i++) { |
|
BDY(vect)[i] = BDY(mat)[i][j]; |
|
} |
|
*rp = vect; |
} |
} |