version 1.6, 2003/07/25 04:19:54 |
version 1.9, 2003/07/25 09:04:47 |
|
|
/* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.5 2003/07/24 03:45:41 noro Exp $ */ |
/* $OpenXM$ */ |
|
|
#include "ca.h" |
#include "ca.h" |
#include "inline.h" |
#include "inline.h" |
|
|
ND ndv_add(ND p1,NDV p2); |
ND ndv_add(ND p1,NDV p2); |
NDV ndtondv(ND p); |
NDV ndtondv(ND p); |
void ndv_mul_nm(NDV pv,NM m,NDV r); |
void ndv_mul_nm(NDV pv,NM m,NDV r); |
|
ND ndv_mul_nm_create(NDV p,NM m0); |
#endif |
#endif |
|
|
void nd_free_private_storage() |
void nd_free_private_storage() |
Line 773 int ndl_check_bound2(int index,unsigned int *d2) |
|
Line 774 int ndl_check_bound2(int index,unsigned int *d2) |
|
} |
} |
} |
} |
|
|
|
#if USE_NDV |
int nd_sp(ND_pairs p,ND *rp) |
int nd_sp(ND_pairs p,ND *rp) |
{ |
{ |
NM m; |
NM m; |
ND p1,p2,t1,t2; |
NDV p1,p2; |
|
ND t1,t2; |
unsigned int *lcm; |
unsigned int *lcm; |
int td; |
int td; |
|
|
|
p1 = nd_psv[p->i1]; |
|
p2 = nd_psv[p->i2]; |
|
lcm = p->lcm; |
|
td = p->td; |
|
NEWNM(m); |
|
C(m) = HC(p2); m->td = td-HTD(p1); ndl_sub(lcm,HDL(p1),m->dl); |
|
if ( ndl_check_bound2(p->i1,m->dl) ) |
|
return 0; |
|
t1 = ndv_mul_nm_create(p1,m); |
|
C(m) = nd_mod-HC(p1); m->td = td-HTD(p2); ndl_sub(lcm,HDL(p2),m->dl); |
|
if ( ndl_check_bound2(p->i2,m->dl) ) { |
|
nd_free(t1); |
|
return 0; |
|
} |
|
ndv_mul_nm(p2,m,ndv_red); |
|
FREENM(m); |
|
*rp = ndv_add(t1,ndv_red); |
|
return 1; |
|
} |
|
#else |
|
int nd_sp(ND_pairs p,ND *rp) |
|
{ |
|
NM m; |
|
ND p1,p2; |
|
ND t1,t2; |
|
unsigned int *lcm; |
|
int td; |
|
|
p1 = nd_ps[p->i1]; |
p1 = nd_ps[p->i1]; |
p2 = nd_ps[p->i2]; |
p2 = nd_ps[p->i2]; |
lcm = p->lcm; |
lcm = p->lcm; |
td = p->td; |
td = p->td; |
NEWNM(m); |
NEWNM(m); |
C(m) = HC(p2); m->td = td-HTD(p1); ndl_sub(lcm,HDL(p1),m->dl); NEXT(m) = 0; |
C(m) = HC(p2); m->td = td-HTD(p1); ndl_sub(lcm,HDL(p1),m->dl); |
if ( ndl_check_bound2(p->i1,m->dl) ) |
if ( ndl_check_bound2(p->i1,m->dl) ) |
return 0; |
return 0; |
t1 = nd_mul_nm(p1,m); |
t1 = nd_mul_ind_nm(p->i1,m); |
C(m) = nd_mod-HC(p1); m->td = td-HTD(p2); ndl_sub(lcm,HDL(p2),m->dl); |
C(m) = nd_mod-HC(p1); m->td = td-HTD(p2); ndl_sub(lcm,HDL(p2),m->dl); |
if ( ndl_check_bound2(p->i2,m->dl) ) { |
if ( ndl_check_bound2(p->i2,m->dl) ) { |
nd_free(t1); |
nd_free(t1); |
return 0; |
return 0; |
} |
} |
t2 = nd_mul_nm(p2,m); |
t2 = nd_mul_ind_nm(p->i2,m); |
FREENM(m); |
FREENM(m); |
*rp = nd_add(t1,t2); |
*rp = nd_add(t1,t2); |
return 1; |
return 1; |
} |
} |
|
#endif |
|
|
INLINE int ndl_hash_value(int td,unsigned int *d) |
INLINE int ndl_hash_value(int td,unsigned int *d) |
{ |
{ |
Line 837 INLINE int nd_find_reducer(ND g) |
|
Line 869 INLINE int nd_find_reducer(ND g) |
|
return -1; |
return -1; |
} |
} |
|
|
ND nd_find_monic_reducer(ND g) |
|
{ |
|
int *d; |
|
ND p,r; |
|
int i; |
|
|
|
for ( i = 0; i < nd_psn; i++ ) { |
|
p = nd_ps[i]; |
|
if ( HTD(g) >= HTD(p) && ndl_reducible(HDL(g),HDL(p)) ) { |
|
d = (int *)ALLOCA(nd_wpd*sizeof(int)); |
|
ndl_sub(HDL(g),HDL(p),d); |
|
r = nd_mul_term(p,HTD(g)-HTD(p),d); |
|
return r; |
|
} |
|
} |
|
return 0; |
|
} |
|
|
|
ND nd_add(ND p1,ND p2) |
ND nd_add(ND p1,ND p2) |
{ |
{ |
int n,c; |
int n,c; |
Line 1001 ND nd_mul_ind_nm(int index,NM m0) |
|
Line 1015 ND nd_mul_ind_nm(int index,NM m0) |
|
return r; |
return r; |
} |
} |
|
|
ND nd_mul_term(ND p,int td,unsigned int *d) |
|
{ |
|
NM m,mr,mr0; |
|
int c,n; |
|
ND r; |
|
|
|
if ( !p ) |
|
return 0; |
|
else { |
|
n = NV(p); m = BDY(p); |
|
for ( mr0 = 0; m; m = NEXT(m) ) { |
|
NEXTNM(mr0,mr); |
|
C(mr) = C(m); |
|
mr->td = m->td+td; |
|
ndl_add(m->dl,d,mr->dl); |
|
} |
|
NEXT(mr) = 0; |
|
MKND(NV(p),mr0,r); |
|
r->sugar = p->sugar + td; |
|
return r; |
|
} |
|
} |
|
|
|
#if 1 |
#if 1 |
/* ret=1 : success, ret=0 : overflow */ |
/* ret=1 : success, ret=0 : overflow */ |
int nd_nf(ND g,int full,ND *rp) |
int nd_nf(ND g,int full,ND *rp) |
{ |
{ |
ND p,d; |
ND p,d; |
NM m,mrd,tail; |
NM m,mrd,tail; |
struct oNM mul; |
NM mul; |
int n,sugar,psugar,stat,index; |
int n,sugar,psugar,stat,index; |
int c,c1,c2; |
int c,c1,c2; |
#if USE_NDV |
#if USE_NDV |
Line 1045 int nd_nf(ND g,int full,ND *rp) |
|
Line 1036 int nd_nf(ND g,int full,ND *rp) |
|
} |
} |
sugar = g->sugar; |
sugar = g->sugar; |
n = NV(g); |
n = NV(g); |
|
mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int)); |
for ( d = 0; g; ) { |
for ( d = 0; g; ) { |
index = nd_find_reducer(g); |
index = nd_find_reducer(g); |
if ( index >= 0 ) { |
if ( index >= 0 ) { |
p = nd_ps[index]; |
p = nd_ps[index]; |
ndl_sub(HDL(g),HDL(p),mul.dl); |
ndl_sub(HDL(g),HDL(p),mul->dl); |
mul.td = HTD(g)-HTD(p); |
mul->td = HTD(g)-HTD(p); |
if ( ndl_check_bound2(index,mul.dl) ) { |
if ( ndl_check_bound2(index,mul->dl) ) { |
nd_free(g); nd_free(d); |
nd_free(g); nd_free(d); |
return 0; |
return 0; |
} |
} |
c1 = invm(HC(p),nd_mod); c2 = nd_mod-HC(g); |
c1 = invm(HC(p),nd_mod); c2 = nd_mod-HC(g); |
DMAR(c1,c2,0,nd_mod,c); mul.c = c; |
DMAR(c1,c2,0,nd_mod,c); C(mul) = c; |
#if USE_NDV |
#if USE_NDV |
ndv_mul_nm(nd_psv[index],&mul,ndv_red); |
ndv_mul_nm(nd_psv[index],mul,ndv_red); |
g = ndv_add(g,ndv_red); |
g = ndv_add(g,ndv_red); |
sugar = MAX(sugar,ndv_red->sugar); |
sugar = MAX(sugar,ndv_red->sugar); |
#else |
#else |
red = nd_mul_ind_nm(index,&mul); |
red = nd_mul_ind_nm(index,mul); |
g = nd_add(g,red); |
g = nd_add(g,red); |
sugar = MAX(sugar,red->sugar); |
sugar = MAX(sugar,red->sugar); |
#endif |
#endif |
Line 1892 void nd_append_red(unsigned int *d,int td,int i) |
|
Line 1884 void nd_append_red(unsigned int *d,int td,int i) |
|
h = ndl_hash_value(td,d); |
h = ndl_hash_value(td,d); |
m->c = i; |
m->c = i; |
m->td = td; |
m->td = td; |
bcopy(d,m->dl,nd_wpd*sizeof(unsigned int)); |
ndl_copy(d,m->dl); |
NEXT(m) = nd_red[h]; |
NEXT(m) = nd_red[h]; |
nd_red[h] = m; |
nd_red[h] = m; |
} |
} |
Line 1901 unsigned int *dp_compute_bound(DP p) |
|
Line 1893 unsigned int *dp_compute_bound(DP p) |
|
{ |
{ |
unsigned int *d,*d1,*d2,*t; |
unsigned int *d,*d1,*d2,*t; |
MP m; |
MP m; |
int i; |
int i,l; |
|
|
if ( !p ) |
if ( !p ) |
return 0; |
return 0; |
Line 1915 unsigned int *dp_compute_bound(DP p) |
|
Line 1907 unsigned int *dp_compute_bound(DP p) |
|
d2[i] = d[i] > d1[i] ? d[i] : d1[i]; |
d2[i] = d[i] > d1[i] ? d[i] : d1[i]; |
t = d1; d1 = d2; d2 = t; |
t = d1; d1 = d2; d2 = t; |
} |
} |
t = (unsigned int *)MALLOC_ATOMIC(nd_nvar*sizeof(unsigned int)); |
l = ((nd_nvar+(sizeof(unsigned int)-1)) |
|
/sizeof(unsigned int))*sizeof(unsigned int); |
|
t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int)); |
|
bzero(t,l*sizeof(unsigned int)); |
bcopy(d1,t,nd_nvar*sizeof(unsigned int)); |
bcopy(d1,t,nd_nvar*sizeof(unsigned int)); |
return t; |
return t; |
} |
} |
Line 1923 unsigned int *dp_compute_bound(DP p) |
|
Line 1918 unsigned int *dp_compute_bound(DP p) |
|
unsigned int *nd_compute_bound(ND p) |
unsigned int *nd_compute_bound(ND p) |
{ |
{ |
unsigned int *d1,*d2,*t; |
unsigned int *d1,*d2,*t; |
int i; |
int i,l; |
NM m; |
NM m; |
|
|
if ( !p ) |
if ( !p ) |
Line 1935 unsigned int *nd_compute_bound(ND p) |
|
Line 1930 unsigned int *nd_compute_bound(ND p) |
|
ndl_lcm(m->dl,d1,d2); |
ndl_lcm(m->dl,d1,d2); |
t = d1; d1 = d2; d2 = t; |
t = d1; d1 = d2; d2 = t; |
} |
} |
t = (unsigned int *)MALLOC_ATOMIC(nd_nvar*sizeof(unsigned int)); |
l = ((nd_nvar+(sizeof(unsigned int)-1)) |
bzero(t,nd_nvar*sizeof(unsigned int)); |
/sizeof(unsigned int))*sizeof(unsigned int); |
|
t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int)); |
|
bzero(t,l*sizeof(unsigned int)); |
for ( i = 0; i < nd_nvar; i++ ) |
for ( i = 0; i < nd_nvar; i++ ) |
t[i] = (d1[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))&nd_mask0; |
t[i] = (d1[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))&nd_mask0; |
return t; |
return t; |
Line 2105 void ndv_mul_nm(NDV p,NM m0,NDV r) |
|
Line 2102 void ndv_mul_nm(NDV p,NM m0,NDV r) |
|
n = NV(p); m = BDY(p); len = p->len; |
n = NV(p); m = BDY(p); len = p->len; |
d = m0->dl; td = m0->td; c = C(m0); |
d = m0->dl; td = m0->td; c = C(m0); |
mr = BDY(r); |
mr = BDY(r); |
for ( i = 0; i < len; i++, NMV_ADV(m), NMV_ADV(mr) ) { |
for ( ; len > 0; len--, NMV_ADV(m), NMV_ADV(mr) ) { |
|
c1 = C(m); DMAR(c1,c,0,nd_mod,c2); C(mr) = c2; |
|
mr->td = m->td+td; ndl_add(m->dl,d,mr->dl); |
|
} |
|
NV(r) = NV(p); |
|
r->len = p->len; |
|
r->sugar = p->sugar + td; |
|
} |
|
} |
|
|
|
ND ndv_mul_nm_create(NDV p,NM m0) |
|
{ |
|
NM mr,mr0; |
|
NMV m; |
|
unsigned int *d,*dt,*dm; |
|
int c,n,td,i,c1,c2,len; |
|
ND r; |
|
|
|
if ( !p ) |
|
return 0; |
|
else { |
|
n = NV(p); m = BDY(p); |
|
d = m0->dl; td = m0->td; c = C(m0); |
|
len = p->len; |
|
mr0 = 0; |
|
for ( i = 0; i < len; i++, NMV_ADV(m) ) { |
|
NEXTNM(mr0,mr); |
c1 = C(m); |
c1 = C(m); |
DMAR(c1,c,0,nd_mod,c2); |
DMAR(c1,c,0,nd_mod,c2); |
C(mr) = c2; |
C(mr) = c2; |
mr->td = m->td+td; |
mr->td = m->td+td; |
ndl_add(m->dl,d,mr->dl); |
ndl_add(m->dl,d,mr->dl); |
} |
} |
NV(r) = NV(p); |
NEXT(mr) = 0; |
r->len = p->len; |
MKND(NV(p),mr0,r); |
r->sugar = p->sugar + td; |
r->sugar = p->sugar + td; |
|
return r; |
} |
} |
} |
} |
|
|
ND ndv_add(ND p1,NDV p2) |
ND ndv_add(ND p1,NDV p2) |
{ |
{ |
|
register NM prev,cur,new; |
int c,c1,c2,t,td,td2,mul,len,i; |
int c,c1,c2,t,td,td2,mul,len,i; |
NM prev,head,cur,new; |
NM head; |
unsigned int *d; |
unsigned int *d; |
NMV m2; |
NMV m2; |
|
|
Line 2129 ND ndv_add(ND p1,NDV p2) |
|
Line 2154 ND ndv_add(ND p1,NDV p2) |
|
return 0; |
return 0; |
else { |
else { |
prev = 0; head = cur = BDY(p1); |
prev = 0; head = cur = BDY(p1); |
NEWNM(new); |
NEWNM(new); len = p2->len; |
len = p2->len; |
for ( m2 = BDY(p2), i = 0; cur && i < len; ) { |
for ( m2 = BDY(p2), i = 0; i < len; ) { |
|
td2 = new->td = m2->td; |
td2 = new->td = m2->td; |
if ( !cur ) { |
if ( cur->td > td2 ) c = 1; |
C(new) = C(m2); |
else if ( cur->td < td2 ) c = -1; |
ndl_copy(m2->dl,new->dl); |
else c = ndl_compare(cur->dl,m2->dl); |
if ( !prev ) { |
|
prev = new; |
|
NEXT(prev) = 0; |
|
head = prev; |
|
} else { |
|
NEXT(prev) = new; |
|
NEXT(new) = 0; |
|
prev = new; |
|
} |
|
NMV_ADV(m2); i++; |
|
NEWNM(new); |
|
continue; |
|
} |
|
if ( cur->td > td2 ) |
|
c = 1; |
|
else if ( cur->td < td2 ) |
|
c = -1; |
|
else |
|
c = ndl_compare(cur->dl,m2->dl); |
|
switch ( c ) { |
switch ( c ) { |
case 0: |
case 0: |
t = C(m2)+C(cur)-nd_mod; |
t = C(m2)+C(cur)-nd_mod; |
if ( t < 0 ) |
if ( t < 0 ) t += nd_mod; |
t += nd_mod; |
if ( t ) C(cur) = t; |
if ( t ) |
|
C(cur) = t; |
|
else if ( !prev ) { |
else if ( !prev ) { |
head = NEXT(cur); |
head = NEXT(cur); FREENM(cur); cur = head; |
FREENM(cur); |
|
cur = head; |
|
} else { |
} else { |
NEXT(prev) = NEXT(cur); |
NEXT(prev) = NEXT(cur); FREENM(cur); cur = NEXT(prev); |
FREENM(cur); |
|
cur = NEXT(prev); |
|
} |
} |
NMV_ADV(m2); i++; |
NMV_ADV(m2); i++; |
break; |
break; |
case 1: |
case 1: |
prev = cur; |
prev = cur; cur = NEXT(cur); |
cur = NEXT(cur); |
|
break; |
break; |
case -1: |
case -1: |
ndl_copy(m2->dl,new->dl); |
ndl_copy(m2->dl,new->dl); |
|
C(new) = C(m2); |
if ( !prev ) { |
if ( !prev ) { |
/* cur = head */ |
/* cur = head */ |
prev = new; |
prev = new; NEXT(prev) = head; head = prev; |
C(prev) = C(m2); |
|
NEXT(prev) = head; |
|
head = prev; |
|
} else { |
} else { |
C(new) = C(m2); |
NEXT(prev) = new; NEXT(new) = cur; prev = new; |
NEXT(prev) = new; |
|
NEXT(new) = cur; |
|
prev = new; |
|
} |
} |
NEWNM(new); |
NEWNM(new); NMV_ADV(m2); i++; |
NMV_ADV(m2); i++; |
|
break; |
break; |
} |
} |
} |
} |
|
for ( ; i < len; i++, NMV_ADV(m2) ) { |
|
td2 = new->td = m2->td; C(new) = C(m2); ndl_copy(m2->dl,new->dl); |
|
if ( !prev ) { |
|
prev = new; NEXT(prev) = 0; head = prev; |
|
} else { |
|
NEXT(prev) = new; NEXT(new) = 0; prev = new; |
|
} |
|
NEWNM(new); |
|
} |
FREENM(new); |
FREENM(new); |
if ( head ) { |
if ( head ) { |
BDY(p1) = head; |
BDY(p1) = head; p1->sugar = MAX(p1->sugar,p2->sugar); |
p1->sugar = MAX(p1->sugar,p2->sugar+td); |
|
return p1; |
return p1; |
} else { |
} else { |
FREEND(p1); |
FREEND(p1); |
Line 2222 NDV ndtondv(ND p) |
|
Line 2222 NDV ndtondv(ND p) |
|
m0 = m = (NMV)MALLOC_ATOMIC(len*nmv_adv); |
m0 = m = (NMV)MALLOC_ATOMIC(len*nmv_adv); |
for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) { |
for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) { |
m->td = t->td; |
m->td = t->td; |
bcopy(t->dl,m->dl,nd_wpd*sizeof(unsigned int)); |
ndl_copy(t->dl,m->dl); |
m->c = t->c; |
m->c = t->c; |
} |
} |
MKNDV(NV(p),m0,len,d); |
MKNDV(NV(p),m0,len,d); |