version 1.3, 2001/06/25 01:35:21 |
version 1.9, 2001/07/23 05:05:41 |
|
|
/* $OpenXM: OpenXM_contrib2/asir2000/engine/Mgfs.c,v 1.2 2001/06/22 08:51:12 noro Exp $ */ |
/* $OpenXM: OpenXM_contrib2/asir2000/engine/Mgfs.c,v 1.8 2001/07/03 01:41:26 noro Exp $ */ |
|
|
#include "ca.h" |
#include "ca.h" |
|
|
|
extern int up_kara_mag, current_gfs_q1; |
|
extern int *current_gfs_plus1; |
|
|
void mulssfum(UM,int,UM); |
void mulssfum(UM,int,UM); |
|
void kmulsfummain(UM,UM,UM); |
|
|
|
|
|
#if defined(__GNUC__) |
|
#define INLINE inline |
|
#elif defined(VISUAL) |
|
#define INLINE __inline |
|
#else |
|
#define INLINE |
|
#endif |
|
|
|
INLINE int _ADDSF(a,b) |
|
int a,b; |
|
{ |
|
if ( !a ) |
|
return b; |
|
else if ( !b ) |
|
return a; |
|
|
|
a = IFTOF(a); b = IFTOF(b); |
|
if ( a > b ) { |
|
/* tab[a]+tab[b] = tab[b](tab[a-b]+1) */ |
|
a = current_gfs_plus1[a-b]; |
|
if ( a < 0 ) |
|
return 0; |
|
else { |
|
a += b; |
|
if ( a >= current_gfs_q1 ) |
|
a -= current_gfs_q1; |
|
return FTOIF(a); |
|
} |
|
} else { |
|
/* tab[a]+tab[b] = tab[a](tab[b-a]+1) */ |
|
b = current_gfs_plus1[b-a]; |
|
if ( b < 0 ) |
|
return 0; |
|
else { |
|
b += a; |
|
if ( b >= current_gfs_q1 ) |
|
b -= current_gfs_q1; |
|
return FTOIF(b); |
|
} |
|
} |
|
} |
|
|
|
INLINE int _MULSF(a,b) |
|
int a,b; |
|
{ |
|
if ( !a || !b ) |
|
return 0; |
|
else { |
|
a = IFTOF(a) + IFTOF(b); |
|
if ( a >= current_gfs_q1 ) |
|
a -= current_gfs_q1; |
|
return FTOIF(a); |
|
} |
|
} |
|
|
void addsfum(p1,p2,pr) |
void addsfum(p1,p2,pr) |
UM p1,p2,pr; |
UM p1,p2,pr; |
{ |
{ |
|
|
c1 = COEF(p2); c2 = COEF(p1); dmax = DEG(p2); dmin = DEG(p1); |
c1 = COEF(p2); c2 = COEF(p1); dmax = DEG(p2); dmin = DEG(p1); |
} |
} |
for ( i = 0, cr = COEF(pr); i <= dmin; i++ ) |
for ( i = 0, cr = COEF(pr); i <= dmin; i++ ) |
cr[i] = _addsf(c1[i],c2[i]); |
cr[i] = _ADDSF(c1[i],c2[i]); |
for ( ; i <= dmax; i++ ) |
for ( ; i <= dmax; i++ ) |
cr[i] = c1[i]; |
cr[i] = c1[i]; |
if ( dmax == dmin ) |
if ( dmax == dmin ) |
|
|
for ( i = 0; i <= d2; i++, cr++ ) |
for ( i = 0; i <= d2; i++, cr++ ) |
if ( mul = *c2++ ) |
if ( mul = *c2++ ) |
for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ ) |
for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ ) |
*pcr = _addsf(_mulsf(*pc1,mul),*pcr); |
if ( *pc1 ) |
|
*pcr = _ADDSF(_MULSF(*pc1,mul),*pcr); |
DEG(pr) = d1 + d2; |
DEG(pr) = d1 + d2; |
} |
} |
|
|
|
|
|
|
for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i; |
for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i; |
i >= 0; i--, dp--, sp-- ) |
i >= 0; i--, dp--, sp-- ) |
*dp = _mulsf(*sp,n); |
*dp = _MULSF(*sp,n); |
} |
} |
|
|
|
void kmulsfum(n1,n2,nr) |
|
UM n1,n2,nr; |
|
{ |
|
UM n,t,s,m,carry; |
|
int d,d1,d2,len,i,l; |
|
unsigned int *r,*r0; |
|
|
|
if ( !n1 || !n2 ) { |
|
nr->d = -1; return; |
|
} |
|
d1 = DEG(n1)+1; d2 = DEG(n2)+1; |
|
if ( (d1 < up_kara_mag) || (d2 < up_kara_mag) ) { |
|
mulsfum(n1,n2,nr); return; |
|
} |
|
if ( d1 < d2 ) { |
|
n = n1; n1 = n2; n2 = n; |
|
d = d1; d1 = d2; d2 = d; |
|
} |
|
if ( d2 > (d1+1)/2 ) { |
|
kmulsfummain(n1,n2,nr); return; |
|
} |
|
d = (d1/d2)+((d1%d2)!=0); |
|
len = (d+1)*d2; |
|
r0 = (unsigned int *)ALLOCA(len*sizeof(int)); |
|
bzero((char *)r0,len*sizeof(int)); |
|
m = W_UMALLOC(d2+1); |
|
carry = W_UMALLOC(d2+1); |
|
t = W_UMALLOC(d1+d2+1); |
|
s = W_UMALLOC(d1+d2+1); |
|
for ( DEG(carry) = -1, i = 0, r = r0; i < d; i++, r += d2 ) { |
|
extractum(n1,i*d2,d2,m); |
|
if ( m ) { |
|
kmulsfum(m,n2,t); |
|
addsfum(t,carry,s); |
|
c_copyum(s,d2,r); |
|
extractum(s,d2,d2,carry); |
|
} else { |
|
c_copyum(carry,d2,r); |
|
carry = 0; |
|
} |
|
} |
|
c_copyum(carry,d2,r); |
|
for ( l = len - 1; !r0[l]; l-- ); |
|
l++; |
|
DEG(nr) = l-1; |
|
bcopy((char *)r0,(char *)COEF(nr),l*sizeof(int)); |
|
} |
|
|
|
void kmulsfummain(n1,n2,nr) |
|
UM n1,n2,nr; |
|
{ |
|
int d1,d2,h,len; |
|
UM n1lo,n1hi,n2lo,n2hi,hi,lo,mid1,mid2,mid,s1,s2,t1,t2; |
|
|
|
d1 = DEG(n1)+1; d2 = DEG(n2)+1; h = (d1+1)/2; |
|
n1lo = W_UMALLOC(d1+1); n1hi = W_UMALLOC(d1+1); |
|
n2lo = W_UMALLOC(d2+1); n2hi = W_UMALLOC(d2+1); |
|
lo = W_UMALLOC(d1+d2+1); hi = W_UMALLOC(d1+d2+1); |
|
mid1 = W_UMALLOC(d1+d2+1); mid2 = W_UMALLOC(d1+d2+1); |
|
mid = W_UMALLOC(d1+d2+1); |
|
s1 = W_UMALLOC(d1+d2+1); s2 = W_UMALLOC(d1+d2+1); |
|
extractum(n1,0,h,n1lo); extractum(n1,h,d1-h,n1hi); |
|
extractum(n2,0,h,n2lo); extractum(n2,h,d2-h,n2hi); |
|
kmulsfum(n1hi,n2hi,hi); kmulsfum(n1lo,n2lo,lo); |
|
len = DEG(hi)+1+2*h; t1 = W_UMALLOC(len-1); DEG(t1) = len-1; |
|
bzero((char *)COEF(t1),len*sizeof(int)); |
|
if ( lo ) |
|
bcopy((char *)COEF(lo),(char *)COEF(t1),(DEG(lo)+1)*sizeof(int)); |
|
if ( hi ) |
|
bcopy((char *)COEF(hi),(char *)(COEF(t1)+2*h),(DEG(hi)+1)*sizeof(int)); |
|
|
|
addsfum(hi,lo,mid1); |
|
subsfum(n1hi,n1lo,s1); subsfum(n2lo,n2hi,s2); |
|
kmulsfum(s1,s2,mid2); addsfum(mid1,mid2,mid); |
|
if ( mid ) { |
|
len = DEG(mid)+1+h; t2 = W_UMALLOC(len-1); DEG(t2) = len-1; |
|
bzero((char *)COEF(t2),len*sizeof(int)); |
|
bcopy((char *)COEF(mid),(char *)(COEF(t2)+h),(DEG(mid)+1)*sizeof(int)); |
|
addsfum(t1,t2,nr); |
|
} else |
|
copyum(t1,nr); |
|
} |
|
|
int divsfum(p1,p2,pq) |
int divsfum(p1,p2,pq) |
UM p1,p2,pq; |
UM p1,p2,pq; |
{ |
{ |
|
|
if ( ( hd = c2[d2] ) != _onesf() ) { |
if ( ( hd = c2[d2] ) != _onesf() ) { |
inv = _invsf(hd); |
inv = _invsf(hd); |
for ( pc1 = c2 + d2; pc1 >= c2; pc1-- ) |
for ( pc1 = c2 + d2; pc1 >= c2; pc1-- ) |
*pc1 = _mulsf(*pc1,inv); |
*pc1 = _MULSF(*pc1,inv); |
} else |
} else |
inv = _onesf(); |
inv = _onesf(); |
for ( i = dd, ct = c1+d1; i >= 0; i-- ) |
for ( i = dd, ct = c1+d1; i >= 0; i-- ) |
if ( tmp = *ct-- ) { |
if ( tmp = *ct-- ) { |
tmp = _chsgnsf(tmp); |
tmp = _chsgnsf(tmp); |
for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- ) |
for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- ) |
*pct = _addsf(_mulsf(*pc1,tmp),*pct); |
*pct = _ADDSF(_MULSF(*pc1,tmp),*pct); |
} |
} |
if ( inv != _onesf() ) { |
if ( inv != _onesf() ) { |
for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ ) |
for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ ) |
*pc1 = _mulsf(*pc1,inv); |
*pc1 = _MULSF(*pc1,inv); |
for ( pc1 = c2, pct = c2+d2; pc1 <= pct; pc1++ ) |
for ( pc1 = c2, pct = c2+d2; pc1 <= pct; pc1++ ) |
*pc1 = _mulsf(*pc1,hd); |
*pc1 = _MULSF(*pc1,hd); |
} |
} |
for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- ); |
for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- ); |
for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- ) |
for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- ) |
|
|
|
|
for ( i = DEG(f), dp = COEF(fd)+i-1, sp = COEF(f)+i; |
for ( i = DEG(f), dp = COEF(fd)+i-1, sp = COEF(f)+i; |
i >= 1; i--, dp--, sp-- ) { |
i >= 1; i--, dp--, sp-- ) { |
*dp = _mulsf(*sp,_itosf(i)); |
*dp = _MULSF(*sp,_itosf(i)); |
} |
} |
degum(fd,DEG(f) - 1); |
degum(fd,DEG(f) - 1); |
} |
} |
|
|
i = DEG(f); sp = COEF(f)+i; |
i = DEG(f); sp = COEF(f)+i; |
inv = _invsf(*sp); |
inv = _invsf(*sp); |
for ( ; i >= 0; i--, sp-- ) |
for ( ; i >= 0; i--, sp-- ) |
*sp = _mulsf(*sp,inv); |
*sp = _MULSF(*sp,inv); |
} |
} |
|
|
void addsfarray(int,int *,int *); |
void addsfarray(int,int *,int *); |
void mulsfarray_trunc(int,int *,int *,int *); |
void mulsfarray_trunc(int,int *,int *,int *); |
|
|
void mulsflum(n,f1,f2,fr) |
/* f1 = f1->c[0]+f1->c[1]*y+..., f2 = f2->c[0]+f2->c[1]*y+... mod y^n */ |
int n; |
|
LUM f1,f2,fr; |
void mulsfbm(f1,f2,fr) |
|
BM f1,f2,fr; |
{ |
{ |
int max; |
UM mul,t,s; |
int i,j,**p1,**p2,*px; |
int i,j,h,d1,d2,dy; |
int *w,*w1,*w2; |
|
|
|
p1 = (int **)COEF(f1); p2 = (int **)COEF(f2); |
dy = MIN(DEG(f1),DEG(f2)); |
w = W_ALLOC(2*(n+1)); w1 = W_ALLOC(DEG(f1)); w2 = W_ALLOC(DEG(f2)); |
|
for ( i = DEG(f1); i >= 0; i-- ) { |
d1 = degbm(f1); |
for ( j = n - 1, px = p1[i]; ( j >= 0 ) && ( px[j] == 0 ); j-- ); |
d2 = degbm(f2); |
w1[i] = ( j == -1 ? 0 : 1 ); |
t = W_UMALLOC(d1+d2); |
} |
s = W_UMALLOC(d1+d2); |
for ( i = DEG(f2); i >= 0; i-- ) { |
|
for ( j = n - 1, px = p2[i]; ( j >= 0 ) && ( px[j] == 0 ); j-- ); |
for ( i = 0; i <= dy; i++ ) { |
w2[i] = ( j == -1 ? 0 : 1 ); |
mul = COEF(f2)[i]; |
} |
if ( DEG(mul) >= 0 ) |
for ( j = DEG(fr) = DEG(f1) + DEG(f2); j >= 0; j-- ) { |
for ( j = 0; i+j <= dy; j++ ) { |
for ( i = n - 1, px = COEF(fr)[j]; i >= 0; i-- ) |
if ( COEF(f1)[j] ) { |
px[i] = 0; |
kmulsfum(COEF(f1)[j],mul,t); |
for ( max = MIN(DEG(f1),j), i = MAX(0,j-DEG(f2)); i <= max; i++ ) |
addsfum(t,COEF(fr)[i+j],s); |
if ( w1[i] != 0 && w2[j - i] != 0 ) { |
cpyum(s,COEF(fr)[i+j]); |
mulsfarray_trunc(n,p1[i],p2[j - i],w); addsfarray(n,w,px); |
|
} |
} |
|
} |
} |
} |
|
DEG(fr) = dy; |
} |
} |
|
|
void addsfarray(n,a1,a2) |
int degbm(f) |
int n; |
BM f; |
int *a1,*a2; |
|
{ |
{ |
int i; |
int d,i,dy; |
|
|
for ( i = 0; i < n; i++, a1++, a2++ ) |
dy = DEG(f); |
if ( *a1 ) |
d = DEG(COEF(f)[0]); |
*a2 = _addsf(*a1,*a2); |
for ( i = 1; i <= dy; i++ ) |
|
d = MAX(DEG(COEF(f)[i]),d); |
|
return d; |
} |
} |
|
|
void mulsfarray_trunc(n,a1,a2,r) |
/* g += f */ |
int n; |
|
int *a1,*a2,*r; |
void addtosfbm(f,g) |
|
BM f,g; |
{ |
{ |
int mul,i,j; |
int i,d1,d2,dy; |
int *c1,*c2,*cr; |
UM t; |
int *pc1,*pc2,*pcr; |
|
|
|
bzero(r,n*sizeof(int)); |
dy = DEG(g); |
c1 = a1; c2 = a2; cr = r; |
if ( DEG(f) > dy ) |
for ( i = 0; i < n; i++, cr++ ) { |
error("addtosfbm : invalid input"); |
if ( mul = *c2++ ) |
dy = MIN(DEG(f),dy); |
for ( j = 0, pc1 = c1, pcr = cr; j+i < n; j++, pc1++, pcr++ ) |
d1 = degbm(f); |
if ( *pc1 ) |
d2 = degbm(g); |
*pcr = _addsf(_mulsf(*pc1,mul),*pcr); |
t = W_UMALLOC(MAX(d1,d2)); |
|
for ( i = 0; i <= dy; i++ ) { |
|
addsfum(COEF(f)[i],COEF(g)[i],t); |
|
cpyum(t,COEF(g)[i]); |
} |
} |
} |
} |
|
|
|
|
} |
} |
} |
} |
|
|
/* clear the body of f */ |
/* f(y) -> f(y+a) */ |
|
|
void clearsflum(bound,n,f) |
void shiftsfbm(f,a) |
int bound,n; |
BM f; |
LUM f; |
int a; |
{ |
{ |
int **c; |
int i,j,d,dy; |
int i; |
UM pwr,ya,w,t,s; |
|
UM *c; |
|
|
DEG(f) = 0; |
dy = DEG(f); |
for ( c = COEF(f), i = 0; i <= n; i++ ) |
c = COEF(f); |
bzero(c[i],(bound+1)*sizeof(int)); |
d = degbm(f); |
} |
|
|
|
/* f += g */ |
w = W_UMALLOC(d); |
|
t = W_UMALLOC(d); |
|
s = W_UMALLOC(dy); |
|
|
void addtosflum(bound,g,f) |
/* pwr = 1 */ |
int bound; |
pwr = W_UMALLOC(dy); DEG(pwr) = 0; COEF(pwr)[0] = _onesf(); |
LUM g,f; |
|
|
/* ya = y+a */ |
|
ya = W_UMALLOC(1); DEG(ya) = 1; |
|
COEF(ya)[0] = a; COEF(ya)[1] = _onesf(); |
|
|
|
for ( i = 0; i <= dy; i++ ) { |
|
/* c[i] does not change */ |
|
for ( j = 0; j < i; j++ ) { |
|
mulssfum(c[i],COEF(pwr)[j],w); |
|
addsfum(w,c[j],t); cpyum(t,c[j]); |
|
} |
|
if ( i < dy ) { |
|
mulsfum(pwr,ya,s); cpyum(s,pwr); |
|
} |
|
} |
|
} |
|
|
|
void clearbm(n,f) |
|
int n; |
|
BM f; |
{ |
{ |
int dg,i,j; |
int i,dy; |
int **cg,**cf; |
UM *c; |
|
|
dg = DEG(g); |
dy = DEG(f); |
cg = COEF(g); |
for ( c = COEF(f), i = 0; i <= dy; i++ ) |
cf = COEF(f); |
clearum(c[i],n); |
for ( i = 0; i <= dg; i++ ) |
|
for ( j = 0; j <= bound; j++ ) |
|
cf[i][j] = _addsf(cf[i][j],cg[i][j]); |
|
} |
} |