version 1.11, 2003/07/18 10:13:13 |
version 1.14, 2003/07/22 07:12:41 |
|
|
* 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/engine/distm.c,v 1.10 2002/01/28 00:54:43 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/engine/distm.c,v 1.13 2003/07/21 01:57:51 noro Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "inline.h" |
#include "inline.h" |
Line 896 void adddl_dup(int n,DL d1,DL d2,DL *dr) |
|
Line 896 void adddl_dup(int n,DL d1,DL d2,DL *dr) |
|
#define INLINE |
#define INLINE |
#endif |
#endif |
|
|
|
#define REDTAB_LEN 32003 |
|
|
typedef struct oPGeoBucket { |
typedef struct oPGeoBucket { |
int m; |
int m; |
struct oND *body[32]; |
struct oND *body[32]; |
|
|
int nd_mod,nd_nvar; |
int nd_mod,nd_nvar; |
int is_rlex; |
int is_rlex; |
int nd_epw,nd_bpe,nd_wpd; |
int nd_epw,nd_bpe,nd_wpd; |
|
unsigned int nd_mask[32]; |
|
unsigned int nd_mask0; |
NM _nm_free_list; |
NM _nm_free_list; |
ND _nd_free_list; |
ND _nd_free_list; |
ND_pairs _ndp_free_list; |
ND_pairs _ndp_free_list; |
|
NM *nd_red; |
|
int nd_red_len; |
|
|
extern int Top,Reverse; |
extern int Top,Reverse; |
int nd_psn,nd_pslen; |
int nd_psn,nd_pslen; |
|
int nd_found,nd_create,nd_notfirst; |
|
|
void GC_gcollect(); |
void GC_gcollect(); |
NODE append_one(NODE,int); |
NODE append_one(NODE,int); |
Line 972 void _ND_alloc(); |
|
Line 979 void _ND_alloc(); |
|
int ndl_td(unsigned int *d); |
int ndl_td(unsigned int *d); |
ND nd_add(ND p1,ND p2); |
ND nd_add(ND p1,ND p2); |
ND nd_mul_nm(ND p,NM m0); |
ND nd_mul_nm(ND p,NM m0); |
|
ND nd_mul_term(ND p,int td,unsigned int *d); |
ND nd_sp(ND_pairs p); |
ND nd_sp(ND_pairs p); |
ND nd_reducer(ND p1,ND p2); |
ND nd_find_reducer(ND g); |
ND nd_nf(NODE b,ND g,ND *ps,int full); |
ND nd_nf(ND g,int full); |
|
ND nd_reduce(ND p1,ND p2); |
|
ND nd_reduce_special(ND p1,ND p2); |
|
void nd_free(ND p); |
void ndl_print(unsigned int *dl); |
void ndl_print(unsigned int *dl); |
void nd_print(ND p); |
void nd_print(ND p); |
void ndp_print(ND_pairs d); |
void ndp_print(ND_pairs d); |
int nd_length(ND p); |
int nd_length(ND p); |
void nd_monic(ND p); |
void nd_monic(ND p); |
|
void nd_mul_c(ND p,int mul); |
|
void nd_free_redlist(); |
|
void nd_append_red(unsigned int *d,int td,int i); |
|
|
void nd_free_private_storage() |
void nd_free_private_storage() |
{ |
{ |
_nd_free_list = 0; |
_nd_free_list = 0; |
_nm_free_list = 0; |
_nm_free_list = 0; |
|
nd_red = 0; |
GC_gcollect(); |
GC_gcollect(); |
} |
} |
|
|
Line 993 void _NM_alloc() |
|
Line 1008 void _NM_alloc() |
|
NM p; |
NM p; |
int i; |
int i; |
|
|
for ( i = 0; i < 1024; i++ ) { |
for ( i = 0; i < 16; i++ ) { |
p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int)); |
p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int)); |
p->next = _nm_free_list; _nm_free_list = p; |
p->next = _nm_free_list; _nm_free_list = p; |
} |
} |
Line 1015 void _NDP_alloc() |
|
Line 1030 void _NDP_alloc() |
|
ND_pairs p; |
ND_pairs p; |
int i; |
int i; |
|
|
for ( i = 0; i < 1024; i++ ) { |
for ( i = 0; i < 10240; i++ ) { |
p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs) |
p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs) |
+(nd_wpd-1)*sizeof(unsigned int)); |
+(nd_wpd-1)*sizeof(unsigned int)); |
p->next = _ndp_free_list; _ndp_free_list = p; |
p->next = _ndp_free_list; _ndp_free_list = p; |
Line 1038 INLINE nd_length(ND p) |
|
Line 1053 INLINE nd_length(ND p) |
|
INLINE int ndl_reducible(unsigned int *d1,unsigned int *d2) |
INLINE int ndl_reducible(unsigned int *d1,unsigned int *d2) |
{ |
{ |
unsigned int u1,u2; |
unsigned int u1,u2; |
int i; |
int i,j; |
|
|
switch ( nd_bpe ) { |
switch ( nd_bpe ) { |
case 4: |
case 4: |
Line 1055 INLINE int ndl_reducible(unsigned int *d1,unsigned int |
|
Line 1070 INLINE int ndl_reducible(unsigned int *d1,unsigned int |
|
} |
} |
return 1; |
return 1; |
break; |
break; |
|
case 6: |
|
for ( i = 0; i < nd_wpd; i++ ) { |
|
u1 = d1[i]; u2 = d2[i]; |
|
if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0; |
|
if ( (u1&0xfc0000) < (u2&0xfc0000) ) return 0; |
|
if ( (u1&0x3f000) < (u2&0x3f000) ) return 0; |
|
if ( (u1&0xfc0) < (u2&0xfc0) ) return 0; |
|
if ( (u1&0x3f) < (u2&0x3f) ) return 0; |
|
} |
|
return 1; |
|
break; |
case 8: |
case 8: |
for ( i = 0; i < nd_wpd; i++ ) { |
for ( i = 0; i < nd_wpd; i++ ) { |
u1 = d1[i]; u2 = d2[i]; |
u1 = d1[i]; u2 = d2[i]; |
Line 1079 INLINE int ndl_reducible(unsigned int *d1,unsigned int |
|
Line 1105 INLINE int ndl_reducible(unsigned int *d1,unsigned int |
|
return 1; |
return 1; |
break; |
break; |
default: |
default: |
error("ndl_reducible : not implemented yet"); |
for ( i = 0; i < nd_wpd; i++ ) { |
|
u1 = d1[i]; u2 = d2[i]; |
|
for ( j = 0; j < nd_epw; j++ ) |
|
if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0; |
|
} |
|
return 1; |
} |
} |
} |
} |
|
|
INLINE void ndl_lcm(unsigned int *d1,unsigned *d2,unsigned int *d) |
void ndl_lcm(unsigned int *d1,unsigned *d2,unsigned int *d) |
{ |
{ |
unsigned int t1,t2,u,u1,u2; |
unsigned int t1,t2,u,u1,u2; |
int i; |
int i,j; |
|
|
switch ( nd_bpe ) { |
switch ( nd_bpe ) { |
case 4: |
case 4: |
Line 1103 INLINE void ndl_lcm(unsigned int *d1,unsigned *d2,unsi |
|
Line 1134 INLINE void ndl_lcm(unsigned int *d1,unsigned *d2,unsi |
|
d[i] = u; |
d[i] = u; |
} |
} |
break; |
break; |
|
case 6: |
|
for ( i = 0; i < nd_wpd; i++ ) { |
|
u1 = d1[i]; u2 = d2[i]; |
|
t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2; |
|
t1 = (u1&0xfc0000); t2 = (u2&0xfc0000); u |= t1>t2?t1:t2; |
|
t1 = (u1&0x3f000); t2 = (u2&0x3f000); u |= t1>t2?t1:t2; |
|
t1 = (u1&0xfc0); t2 = (u2&0xfc0); u |= t1>t2?t1:t2; |
|
t1 = (u1&0x3f); t2 = (u2&0x3f); u |= t1>t2?t1:t2; |
|
d[i] = u; |
|
} |
|
break; |
case 8: |
case 8: |
for ( i = 0; i < nd_wpd; i++ ) { |
for ( i = 0; i < nd_wpd; i++ ) { |
u1 = d1[i]; u2 = d2[i]; |
u1 = d1[i]; u2 = d2[i]; |
Line 1128 INLINE void ndl_lcm(unsigned int *d1,unsigned *d2,unsi |
|
Line 1170 INLINE void ndl_lcm(unsigned int *d1,unsigned *d2,unsi |
|
} |
} |
break; |
break; |
default: |
default: |
error("ndl_lcm : not implemented yet"); |
for ( i = 0; i < nd_wpd; i++ ) { |
|
u1 = d1[i]; u2 = d2[i]; |
|
for ( j = 0, u = 0; j < nd_epw; j++ ) { |
|
t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2; |
|
} |
|
d[i] = u; |
|
} |
|
break; |
} |
} |
} |
} |
|
|
INLINE int ndl_td(unsigned int *d) |
int ndl_td(unsigned int *d) |
{ |
{ |
unsigned int t,u; |
unsigned int t,u; |
int i; |
int i,j; |
|
|
switch ( nd_bpe ) { |
for ( t = 0, i = 0; i < nd_wpd; i++ ) { |
case 4: |
u = d[i]; |
for ( t = 0, i = 0; i < nd_wpd; i++ ) { |
for ( j = 0; j < nd_epw; j++, u>>=nd_bpe ) |
u = d[i]; |
t += (u&nd_mask0); |
t += ((u&0xf0000000)>>28)+((u&0xf000000)>>24) |
|
+((u&0xf00000)>>20)+((u&0xf0000)>>16) |
|
+((u&0xf000)>>12)+((u&0xf00)>>8)+((u&0xf0)>>4)+(u&0xf); |
|
} |
|
break; |
|
case 8: |
|
for ( t = 0, i = 0; i < nd_wpd; i++ ) { |
|
u = d[i]; |
|
t += ((u&0xff000000)>>24)+((u&0xff0000)>>16) |
|
+((u&0xff00)>>8)+(u&0xff); |
|
} |
|
break; |
|
case 16: |
|
for ( t = 0, i = 0; i < nd_wpd; i++ ) { |
|
u = d[i]; |
|
t += ((u&0xffff0000)>>16)+(u&0xffff); |
|
} |
|
break; |
|
case 32: |
|
for ( t = 0, i = 0; i < nd_wpd; i++ ) |
|
t += d[i]; |
|
break; |
|
default: |
|
error("ndl_td : not implemented yet"); |
|
} |
} |
return t; |
return t; |
} |
} |
Line 1173 INLINE int ndl_compare(unsigned int *d1,unsigned int * |
|
Line 1198 INLINE int ndl_compare(unsigned int *d1,unsigned int * |
|
{ |
{ |
int i; |
int i; |
|
|
for ( i = 0; i < nd_wpd; i++ ) |
for ( i = 0; i < nd_wpd; i++, d1++, d2++ ) |
if ( d1[i] > d2[i] ) |
if ( *d1 > *d2 ) |
return is_rlex ? -1 : 1; |
return is_rlex ? -1 : 1; |
else if ( d1[i] < d2[i] ) |
else if ( *d1 < *d2 ) |
return is_rlex ? 1 : -1; |
return is_rlex ? 1 : -1; |
return 0; |
return 0; |
} |
} |
Line 1199 INLINE void ndl_add(unsigned int *d1,unsigned int *d2, |
|
Line 1224 INLINE void ndl_add(unsigned int *d1,unsigned int *d2, |
|
d[i] = d1[i]+d2[i]; |
d[i] = d1[i]+d2[i]; |
} |
} |
|
|
INLINE void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d) |
void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d) |
{ |
{ |
int i; |
int i; |
|
|
Line 1207 INLINE void ndl_sub(unsigned int *d1,unsigned int *d2, |
|
Line 1232 INLINE void ndl_sub(unsigned int *d1,unsigned int *d2, |
|
d[i] = d1[i]-d2[i]; |
d[i] = d1[i]-d2[i]; |
} |
} |
|
|
INLINE int ndl_disjoint(unsigned int *d1,unsigned int *d2) |
int ndl_disjoint(unsigned int *d1,unsigned int *d2) |
{ |
{ |
unsigned int t1,t2,u,u1,u2; |
unsigned int t1,t2,u,u1,u2; |
int i; |
int i,j; |
|
|
switch ( nd_bpe ) { |
switch ( nd_bpe ) { |
case 4: |
case 4: |
Line 1227 INLINE int ndl_disjoint(unsigned int *d1,unsigned int |
|
Line 1252 INLINE int ndl_disjoint(unsigned int *d1,unsigned int |
|
} |
} |
return 1; |
return 1; |
break; |
break; |
|
case 6: |
|
for ( i = 0; i < nd_wpd; i++ ) { |
|
u1 = d1[i]; u2 = d2[i]; |
|
t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0; |
|
t1 = u1&0xfc0000; t2 = u2&0xfc0000; if ( t1&&t2 ) return 0; |
|
t1 = u1&0x3f000; t2 = u2&0x3f000; if ( t1&&t2 ) return 0; |
|
t1 = u1&0xfc0; t2 = u2&0xfc0; if ( t1&&t2 ) return 0; |
|
t1 = u1&0x3f; t2 = u2&0x3f; if ( t1&&t2 ) return 0; |
|
} |
|
return 1; |
|
break; |
case 8: |
case 8: |
for ( i = 0; i < nd_wpd; i++ ) { |
for ( i = 0; i < nd_wpd; i++ ) { |
u1 = d1[i]; u2 = d2[i]; |
u1 = d1[i]; u2 = d2[i]; |
Line 1251 INLINE int ndl_disjoint(unsigned int *d1,unsigned int |
|
Line 1287 INLINE int ndl_disjoint(unsigned int *d1,unsigned int |
|
return 1; |
return 1; |
break; |
break; |
default: |
default: |
error("ndl_disjoint : not implemented yet"); |
for ( i = 0; i < nd_wpd; i++ ) { |
|
u1 = d1[i]; u2 = d2[i]; |
|
for ( j = 0; j < nd_epw; j++ ) { |
|
if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0; |
|
u1 >>= nd_bpe; u2 >>= nd_bpe; |
|
} |
|
} |
|
return 1; |
|
break; |
} |
} |
} |
} |
|
|
ND nd_add(ND p1,ND p2) |
ND nd_reduce(ND p1,ND p2) |
{ |
{ |
int n,c; |
int c,c1,c2,t,td,td2,mul; |
int t; |
NM m2,prev,head,cur,new; |
ND r; |
unsigned int *d; |
NM m1,m2,mr0,mr,s; |
|
|
|
if ( !p1 ) |
if ( !p1 ) |
return p2; |
return 0; |
else if ( !p2 ) |
|
return p1; |
|
else { |
else { |
for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) { |
c2 = invm(HC(p2),nd_mod); |
if ( m1->td > m2->td ) |
c1 = nd_mod-HC(p1); |
|
DMAR(c1,c2,0,nd_mod,mul); |
|
td = HTD(p1)-HTD(p2); |
|
d = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int)); |
|
ndl_sub(HDL(p1),HDL(p2),d); |
|
prev = 0; head = cur = BDY(p1); |
|
NEWNM(new); |
|
for ( m2 = BDY(p2); m2; ) { |
|
td2 = new->td = m2->td+td; |
|
ndl_add(m2->dl,d,new->dl); |
|
if ( !cur ) { |
|
c1 = C(m2); |
|
DMAR(c1,mul,0,nd_mod,c2); |
|
C(new) = c2; |
|
if ( !prev ) { |
|
prev = new; |
|
NEXT(prev) = 0; |
|
head = prev; |
|
} else { |
|
NEXT(prev) = new; |
|
NEXT(new) = 0; |
|
prev = new; |
|
} |
|
m2 = NEXT(m2); |
|
NEWNM(new); |
|
continue; |
|
} |
|
if ( cur->td > td2 ) |
c = 1; |
c = 1; |
else if ( m1->td < m2->td ) |
else if ( cur->td < td2 ) |
c = -1; |
c = -1; |
else |
else |
c = ndl_compare(m1->dl,m2->dl); |
c = ndl_compare(cur->dl,new->dl); |
switch ( c ) { |
switch ( c ) { |
case 0: |
case 0: |
t = ((C(m1))+(C(m2))) - nd_mod; |
c2 = C(m2); |
if ( t < 0 ) |
c1 = C(cur); |
t += nd_mod; |
DMAR(c2,mul,c1,nd_mod,t); |
s = m1; m1 = NEXT(m1); |
if ( t ) |
if ( t ) { |
C(cur) = t; |
NEXTNM2(mr0,mr,s); C(mr) = (t); |
else if ( !prev ) { |
|
head = NEXT(cur); |
|
FREENM(cur); |
|
cur = head; |
} else { |
} else { |
FREENM(s); |
NEXT(prev) = NEXT(cur); |
|
FREENM(cur); |
|
cur = NEXT(prev); |
} |
} |
s = m2; m2 = NEXT(m2); FREENM(s); |
m2 = NEXT(m2); |
break; |
break; |
case 1: |
case 1: |
s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s); |
prev = cur; |
|
cur = NEXT(cur); |
break; |
break; |
case -1: |
case -1: |
s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s); |
if ( !prev ) { |
|
/* cur = head */ |
|
prev = new; |
|
c2 = C(m2); |
|
DMAR(c2,mul,0,nd_mod,c1); |
|
C(prev) = c1; |
|
NEXT(prev) = head; |
|
head = prev; |
|
} else { |
|
c2 = C(m2); |
|
DMAR(c2,mul,0,nd_mod,c1); |
|
C(new) = c1; |
|
NEXT(prev) = new; |
|
NEXT(new) = cur; |
|
prev = new; |
|
} |
|
NEWNM(new); |
|
m2 = NEXT(m2); |
break; |
break; |
} |
} |
} |
} |
if ( !mr0 ) |
FREENM(new); |
if ( m1 ) |
if ( head ) { |
mr0 = m1; |
BDY(p1) = head; |
else if ( m2 ) |
p1->sugar = MAX(p1->sugar,p2->sugar+td); |
mr0 = m2; |
return p1; |
else |
} else { |
return 0; |
FREEND(p1); |
else if ( m1 ) |
return 0; |
NEXT(mr) = m1; |
|
else if ( m2 ) |
|
NEXT(mr) = m2; |
|
else |
|
NEXT(mr) = 0; |
|
BDY(p1) = mr0; |
|
p1->sugar = MAX(p1->sugar,p2->sugar); |
|
FREEND(p2); |
|
return p1; |
|
} |
|
} |
|
|
|
INLINE ND nd_mul_nm(ND p,NM m0) |
|
{ |
|
NM m,mr,mr0; |
|
unsigned int *d,*dt,*dm; |
|
int c,n,td; |
|
int *pt,*p1,*p2; |
|
ND r; |
|
|
|
if ( !p ) |
|
return 0; |
|
else { |
|
n = NV(p); m = BDY(p); |
|
d = m0->dl; td = m0->td; c = C(m0); |
|
mr0 = 0; |
|
for ( ; m; m = NEXT(m) ) { |
|
NEXTNM(mr0,mr); |
|
C(mr) = (C(m)*c)%nd_mod; |
|
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; |
|
} |
} |
} |
} |
|
|
ND nd_reduce(ND p1,ND p2) |
/* HDL(p1) = HDL(p2) */ |
|
|
|
ND nd_reduce_special(ND p1,ND p2) |
{ |
{ |
int c,t,td,td2,mul; |
int c,c1,c2,t,td,td2,mul; |
NM m2,prev,head,cur,new; |
NM m2,prev,head,cur,new; |
unsigned int *d; |
|
|
|
if ( !p1 ) |
if ( !p1 ) |
return 0; |
return 0; |
else { |
else { |
mul = ((nd_mod-HC(p1))*invm(HC(p2),nd_mod))%nd_mod; |
c2 = invm(HC(p2),nd_mod); |
td = HTD(p1)-HTD(p2); |
c1 = nd_mod-HC(p1); |
d = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int)); |
DMAR(c1,c2,0,nd_mod,mul); |
ndl_sub(HDL(p1),HDL(p2),d); |
|
prev = 0; head = cur = BDY(p1); |
prev = 0; head = cur = BDY(p1); |
NEWNM(new); |
NEWNM(new); |
for ( m2 = BDY(p2); m2; ) { |
for ( m2 = BDY(p2); m2; ) { |
td2 = new->td = m2->td+td; |
td2 = new->td = m2->td; |
ndl_add(m2->dl,d,new->dl); |
|
if ( !cur ) { |
if ( !cur ) { |
C(new) = (C(m2)*mul)%nd_mod; |
c1 = C(m2); |
|
DMAR(c1,mul,0,nd_mod,c2); |
|
C(new) = c2; |
|
bcopy(m2->dl,new->dl,nd_wpd*sizeof(unsigned int)); |
if ( !prev ) { |
if ( !prev ) { |
prev = new; |
prev = new; |
NEXT(prev) = 0; |
NEXT(prev) = 0; |
Line 1380 ND nd_reduce(ND p1,ND p2) |
|
Line 1439 ND nd_reduce(ND p1,ND p2) |
|
else if ( cur->td < td2 ) |
else if ( cur->td < td2 ) |
c = -1; |
c = -1; |
else |
else |
c = ndl_compare(cur->dl,new->dl); |
c = ndl_compare(cur->dl,m2->dl); |
switch ( c ) { |
switch ( c ) { |
case 0: |
case 0: |
t = (C(cur)+C(m2)*mul)%nd_mod; |
c2 = C(m2); |
|
c1 = C(cur); |
|
DMAR(c2,mul,c1,nd_mod,t); |
if ( t ) |
if ( t ) |
C(cur) = t; |
C(cur) = t; |
else if ( !prev ) { |
else if ( !prev ) { |
Line 1402 ND nd_reduce(ND p1,ND p2) |
|
Line 1463 ND nd_reduce(ND p1,ND p2) |
|
cur = NEXT(cur); |
cur = NEXT(cur); |
break; |
break; |
case -1: |
case -1: |
|
bcopy(m2->dl,new->dl,nd_wpd*sizeof(unsigned int)); |
if ( !prev ) { |
if ( !prev ) { |
/* cur = head */ |
/* cur = head */ |
prev = new; |
prev = new; |
C(prev) = (C(m2)*mul)%nd_mod; |
c2 = C(m2); |
|
DMAR(c2,mul,0,nd_mod,c1); |
|
C(prev) = c1; |
NEXT(prev) = head; |
NEXT(prev) = head; |
head = prev; |
head = prev; |
} else { |
} else { |
C(new) = (C(m2)*mul)%nd_mod; |
c2 = C(m2); |
|
DMAR(c2,mul,0,nd_mod,c1); |
|
C(new) = c1; |
NEXT(prev) = new; |
NEXT(prev) = new; |
NEXT(new) = cur; |
NEXT(new) = cur; |
prev = new; |
prev = new; |
Line 1452 ND nd_sp(ND_pairs p) |
|
Line 1518 ND nd_sp(ND_pairs p) |
|
return nd_add(t1,t2); |
return nd_add(t1,t2); |
} |
} |
|
|
ND nd_reducer(ND p1,ND p2) |
int ndl_hash_value(int td,unsigned int *d) |
{ |
{ |
NM m; |
int i; |
ND r; |
int r; |
|
|
NEWNM(m); |
r = td; |
C(m) = ((nd_mod-HC(p1))*invm(HC(p2),nd_mod))%nd_mod; |
for ( i = 0; i < nd_wpd; i++ ) |
m->td = HTD(p1)-HTD(p2); |
r = ((r<<16)+d[i])%REDTAB_LEN; |
ndl_sub(HDL(p1),HDL(p2),m->dl); |
|
NEXT(m) = 0; |
|
r = nd_mul_nm(p2,m); |
|
FREENM(m); |
|
return r; |
return r; |
} |
} |
|
|
|
ND nd_find_reducer(ND g) |
|
{ |
|
NM m; |
|
ND r,p; |
|
int i,c1,c2,c; |
|
int d,k; |
|
NM t; |
|
|
#if 1 |
#if 1 |
ND nd_nf(NODE b,ND g,ND *ps,int full) |
d = ndl_hash_value(HTD(g),HDL(g)); |
|
for ( m = nd_red[d], k = 0; m; m = NEXT(m), k++ ) { |
|
if ( HTD(g) == m->td && ndl_equal(HDL(g),m->dl) ) { |
|
#if 1 |
|
if ( k > 0 ) nd_notfirst++; |
|
#endif |
|
nd_found++; |
|
p = nps[m->c]; |
|
#if 1 |
|
c1 = invm(HC(p),nd_mod); |
|
c2 = nd_mod-HC(g); |
|
DMAR(c1,c2,0,nd_mod,c); |
|
NEWNM(m); |
|
C(m) = c; |
|
m->td = HTD(g)-HTD(p); |
|
ndl_sub(HDL(g),HDL(p),m->dl); |
|
NEXT(m) = 0; |
|
r = nd_mul_nm(p,m); |
|
FREENM(m); |
|
return r; |
|
#else |
|
return p; |
|
#endif |
|
} |
|
} |
|
#endif |
|
|
|
for ( i = 0; i < nd_psn; i++ ) { |
|
p = nps[i]; |
|
if ( HTD(g) >= HTD(p) && ndl_reducible(HDL(g),HDL(p)) ) { |
|
nd_create++; |
|
#if 1 |
|
NEWNM(m); |
|
c1 = invm(HC(p),nd_mod); |
|
c2 = nd_mod-HC(g); |
|
DMAR(c1,c2,0,nd_mod,c); |
|
C(m) = c; |
|
m->td = HTD(g)-HTD(p); |
|
ndl_sub(HDL(g),HDL(p),m->dl); |
|
NEXT(m) = 0; |
|
r = nd_mul_nm(p,m); |
|
FREENM(m); |
|
r->sugar = m->td + p->sugar; |
|
nd_append_red(HDL(g),HTD(g),i); |
|
return r; |
|
#else |
|
nd_append_red(HDL(g),HTD(g),i); |
|
return p; |
|
#endif |
|
} |
|
} |
|
return 0; |
|
} |
|
|
|
ND nd_find_monic_reducer(ND g) |
{ |
{ |
ND u,p,d,red; |
int *d; |
NODE l; |
ND p,r; |
NM m,mrd; |
int i; |
int sugar,psugar,n,h_reducible; |
|
|
|
if ( !g ) { |
for ( i = 0; i < nd_psn; i++ ) { |
|
p = nps[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) |
|
{ |
|
int n,c; |
|
int t; |
|
ND r; |
|
NM m1,m2,mr0,mr,s; |
|
|
|
if ( !p1 ) |
|
return p2; |
|
else if ( !p2 ) |
|
return p1; |
|
else { |
|
for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) { |
|
if ( m1->td > m2->td ) |
|
c = 1; |
|
else if ( m1->td < m2->td ) |
|
c = -1; |
|
else |
|
c = ndl_compare(m1->dl,m2->dl); |
|
switch ( c ) { |
|
case 0: |
|
t = ((C(m1))+(C(m2))) - nd_mod; |
|
if ( t < 0 ) |
|
t += nd_mod; |
|
s = m1; m1 = NEXT(m1); |
|
if ( t ) { |
|
NEXTNM2(mr0,mr,s); C(mr) = (t); |
|
} else { |
|
FREENM(s); |
|
} |
|
s = m2; m2 = NEXT(m2); FREENM(s); |
|
break; |
|
case 1: |
|
s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s); |
|
break; |
|
case -1: |
|
s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s); |
|
break; |
|
} |
|
} |
|
if ( !mr0 ) |
|
if ( m1 ) |
|
mr0 = m1; |
|
else if ( m2 ) |
|
mr0 = m2; |
|
else |
|
return 0; |
|
else if ( m1 ) |
|
NEXT(mr) = m1; |
|
else if ( m2 ) |
|
NEXT(mr) = m2; |
|
else |
|
NEXT(mr) = 0; |
|
BDY(p1) = mr0; |
|
p1->sugar = MAX(p1->sugar,p2->sugar); |
|
FREEND(p2); |
|
return p1; |
|
} |
|
} |
|
|
|
ND nd_mul_nm(ND p,NM m0) |
|
{ |
|
NM m,mr,mr0; |
|
unsigned int *d,*dt,*dm; |
|
int c,n,td,i,c1,c2; |
|
int *pt,*p1,*p2; |
|
ND r; |
|
|
|
if ( !p ) |
return 0; |
return 0; |
|
else { |
|
n = NV(p); m = BDY(p); |
|
d = m0->dl; td = m0->td; c = C(m0); |
|
mr0 = 0; |
|
for ( ; m; m = NEXT(m) ) { |
|
NEXTNM(mr0,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); |
|
} |
|
NEXT(mr) = 0; |
|
MKND(NV(p),mr0,r); |
|
r->sugar = p->sugar + td; |
|
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); |
|
mr0 = 0; |
|
NEWNM(mr0); |
|
C(mr0) = C(m); |
|
mr0->td = m->td+td; |
|
ndl_add(m->dl,d,mr0->dl); |
|
mr = mr0; |
|
m = NEXT(m); |
|
for ( ; m; m = NEXT(m) ) { |
|
NEWNM(NEXT(mr)); |
|
mr = NEXT(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 |
|
ND nd_nf(ND g,int full) |
|
{ |
|
ND p,d,red; |
|
NM m,mrd,tail; |
|
int n,sugar,psugar; |
|
|
|
if ( !g ) |
|
return 0; |
sugar = g->sugar; |
sugar = g->sugar; |
n = g->nv; |
n = NV(g); |
for ( d = 0; g; ) { |
for ( d = 0; g; ) { |
for ( h_reducible = 0, l = b; l; l = NEXT(l) ) { |
red = nd_find_reducer(g); |
p = ps[(int)BDY(l)]; |
if ( red ) { |
if ( HTD(g)>=HTD(p) && ndl_reducible(HDL(g),HDL(p)) ) { |
#if 1 |
h_reducible = 1; |
g = nd_add(g,red); |
psugar = HTD(g)-HTD(p) + p->sugar; |
sugar = MAX(sugar,red->sugar); |
#if 0 |
|
red = nd_reducer(g,p); |
|
g = nd_add(g,red); |
|
#else |
#else |
g = nd_reduce(g,p); |
psugar = (HTD(g)-HTD(red))+red->sugar; |
|
g = nd_reduce(g,red); |
|
sugar = MAX(sugar,psugar); |
#endif |
#endif |
sugar = MAX(sugar,psugar); |
} else if ( !full ) |
if ( !g ) { |
return g; |
if ( d ) |
else { |
d->sugar = sugar; |
m = BDY(g); |
return d; |
if ( NEXT(m) ) { |
} |
BDY(g) = NEXT(m); NEXT(m) = 0; |
break; |
} else { |
|
FREEND(g); g = 0; |
} |
} |
} |
if ( d ) { |
if ( !h_reducible ) { |
NEXT(tail)=m; |
/* head term is not reducible */ |
tail=m; |
if ( !full ) { |
|
if ( g ) |
|
g->sugar = sugar; |
|
return g; |
|
} else { |
} else { |
m = BDY(g); |
MKND(n,m,d); |
if ( NEXT(m) ) { |
tail = BDY(d); |
BDY(g) = NEXT(m); NEXT(m) = 0; |
|
} else { |
|
FREEND(g); g = 0; |
|
} |
|
if ( d ) { |
|
for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) ); |
|
NEXT(mrd) = m; |
|
} else { |
|
MKND(n,m,d); |
|
} |
|
} |
} |
} |
} |
} |
} |
Line 1632 ND normalize_pbucket(PGeoBucket g) |
|
Line 1879 ND normalize_pbucket(PGeoBucket g) |
|
return r; |
return r; |
} |
} |
|
|
ND nd_nf(NODE b,ND g,ND *ps,int full) |
ND nd_nf(ND g,int full) |
{ |
{ |
ND u,p,d,red; |
ND u,p,d,red; |
NODE l; |
NODE l; |
Line 1656 ND nd_nf(NODE b,ND g,ND *ps,int full) |
|
Line 1903 ND nd_nf(NODE b,ND g,ND *ps,int full) |
|
return d; |
return d; |
} |
} |
g = bucket->body[h]; |
g = bucket->body[h]; |
for ( h_reducible = 0, l = b; l; l = NEXT(l) ) { |
red = nd_find_reducer(g); |
p = ps[(int)BDY(l)]; |
if ( red ) { |
if ( ndl_reducible(HDL(g),HDL(p)) ) { |
bucket->body[h] = nd_remove_head(g); |
h_reducible = 1; |
red = nd_remove_head(red); |
psugar = HTD(g)-HTD(p) + p->sugar; |
add_pbucket(bucket,red); |
red = nd_reducer(g,p); |
sugar = MAX(sugar,red->sugar); |
bucket->body[h] = nd_remove_head(g); |
} else if ( !full ) { |
red = nd_remove_head(red); |
g = normalize_pbucket(bucket); |
add_pbucket(bucket,red); |
if ( g ) |
sugar = MAX(sugar,psugar); |
g->sugar = sugar; |
break; |
return g; |
|
} else { |
|
m = BDY(g); |
|
if ( NEXT(m) ) { |
|
BDY(g) = NEXT(m); NEXT(m) = 0; |
|
} else { |
|
FREEND(g); g = 0; |
} |
} |
} |
bucket->body[h] = g; |
if ( !h_reducible ) { |
NEXT(m) = 0; |
/* head term is not reducible */ |
if ( d ) { |
if ( !full ) { |
for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) ); |
g = normalize_pbucket(bucket); |
NEXT(mrd) = m; |
if ( g ) |
|
g->sugar = sugar; |
|
return g; |
|
} else { |
} else { |
m = BDY(g); |
MKND(n,m,d); |
if ( NEXT(m) ) { |
|
BDY(g) = NEXT(m); NEXT(m) = 0; |
|
} else { |
|
FREEND(g); g = 0; |
|
} |
|
bucket->body[h] = g; |
|
NEXT(m) = 0; |
|
if ( d ) { |
|
for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) ); |
|
NEXT(mrd) = m; |
|
} else { |
|
MKND(n,m,d); |
|
} |
|
} |
} |
} |
} |
} |
} |
Line 1699 ND nd_nf(NODE b,ND g,ND *ps,int full) |
|
Line 1936 ND nd_nf(NODE b,ND g,ND *ps,int full) |
|
|
|
NODE nd_gb(NODE f) |
NODE nd_gb(NODE f) |
{ |
{ |
int i,nh; |
int i,nh,sugar; |
NODE r,g,gall; |
NODE r,g,gall; |
ND_pairs d; |
ND_pairs d; |
ND_pairs l; |
ND_pairs l; |
Line 1711 NODE nd_gb(NODE f) |
|
Line 1948 NODE nd_gb(NODE f) |
|
g = update_base(g,i); |
g = update_base(g,i); |
gall = append_one(gall,i); |
gall = append_one(gall,i); |
} |
} |
|
sugar = 0; |
while ( d ) { |
while ( d ) { |
#if 0 |
#if 0 |
ndp_print(d); |
ndp_print(d); |
#endif |
#endif |
l = nd_minp(d,&d); |
l = nd_minp(d,&d); |
|
if ( l->sugar != sugar ) { |
|
sugar = l->sugar; |
|
fprintf(asir_out,"%d",sugar); |
|
} |
h = nd_sp(l); |
h = nd_sp(l); |
nf = nd_nf(gall,h,nps,!Top); |
nf = nd_nf(h,!Top); |
if ( nf ) { |
if ( nf ) { |
printf("+"); fflush(stdout); |
printf("+"); fflush(stdout); |
#if 0 |
#if 0 |
|
|
|
|
NODE nd_setup(NODE f) |
NODE nd_setup(NODE f) |
{ |
{ |
int i; |
int i,td; |
NODE s,s0,f0; |
NODE s,s0,f0; |
|
|
|
nd_found = 0; |
|
nd_notfirst = 0; |
|
nd_create = 0; |
#if 0 |
#if 0 |
f0 = f = NODE_sortb(f,1); |
f0 = f = NODE_sortb(f,1); |
#endif |
#endif |
nd_psn = length(f); nd_pslen = 2*nd_psn; |
nd_psn = length(f); nd_pslen = 2*nd_psn; |
nps = (ND *)MALLOC(nd_pslen*sizeof(ND)); |
nps = (ND *)MALLOC(nd_pslen*sizeof(ND)); |
nd_bpe = 4; |
nd_bpe = 6; |
nd_epw = (sizeof(unsigned int)*8)/nd_bpe; |
nd_epw = (sizeof(unsigned int)*8)/nd_bpe; |
nd_wpd = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0); |
nd_wpd = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0); |
|
if ( nd_bpe < 32 ) { |
|
nd_mask0 = (1<<nd_bpe)-1; |
|
} else { |
|
nd_mask0 = 0xffffffff; |
|
} |
|
bzero(nd_mask,sizeof(nd_mask)); |
|
for ( i = 0; i < nd_epw; i++ ) |
|
nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe)); |
nd_free_private_storage(); |
nd_free_private_storage(); |
|
td = 0; |
for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) { |
for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) { |
nps[i] = dptond((DP)BDY(f)); |
nps[i] = dptond((DP)BDY(f)); |
|
td = MAX(td,HTD(nps[i])); |
nd_monic(nps[i]); |
nd_monic(nps[i]); |
} |
} |
|
nd_red = (NM *)MALLOC(REDTAB_LEN*sizeof(NM)); |
for ( s0 = 0, i = 0; i < nd_psn; i++ ) { |
for ( s0 = 0, i = 0; i < nd_psn; i++ ) { |
NEXTNODE(s0,s); BDY(s) = (pointer)i; |
NEXTNODE(s0,s); BDY(s) = (pointer)i; |
} |
} |
Line 2077 void nd_gr(LIST f,LIST v,int m,struct order_spec *ord, |
|
Line 2333 void nd_gr(LIST f,LIST v,int m,struct order_spec *ord, |
|
} |
} |
if ( r0 ) NEXT(r) = 0; |
if ( r0 ) NEXT(r) = 0; |
MKLIST(*rp,r0); |
MKLIST(*rp,r0); |
|
fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n", |
|
nd_found,nd_notfirst,nd_create); |
} |
} |
|
|
void dltondl(int n,DL dl,unsigned int *r) |
void dltondl(int n,DL dl,unsigned int *r) |
Line 2085 void dltondl(int n,DL dl,unsigned int *r) |
|
Line 2343 void dltondl(int n,DL dl,unsigned int *r) |
|
int i; |
int i; |
|
|
d = dl->d; |
d = dl->d; |
|
bzero(r,nd_wpd*sizeof(unsigned int)); |
if ( is_rlex ) |
if ( is_rlex ) |
for ( i = 0; i < n; i++ ) |
for ( i = 0; i < n; i++ ) |
r[(n-1-i)/nd_epw] |= (d[i]<<((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe)); |
r[(n-1-i)/nd_epw] |= (d[i]<<((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe)); |
Line 2206 void ndp_print(ND_pairs d) |
|
Line 2465 void ndp_print(ND_pairs d) |
|
|
|
void nd_monic(ND p) |
void nd_monic(ND p) |
{ |
{ |
int mul; |
if ( !p ) |
|
return; |
|
else |
|
nd_mul_c(p,invm(HC(p),nd_mod)); |
|
} |
|
|
|
void nd_mul_c(ND p,int mul) |
|
{ |
NM m; |
NM m; |
|
int c,c1; |
|
|
if ( !p ) |
if ( !p ) |
return; |
return; |
mul = invm(HC(p),nd_mod); |
for ( m = BDY(p); m; m = NEXT(m) ) { |
for ( m = BDY(p); m; m = NEXT(m) ) |
c1 = C(m); |
C(m) = (C(m)*mul)%nd_mod; |
DMAR(c1,mul,0,nd_mod,c); |
|
C(m) = c; |
|
} |
|
} |
|
|
|
void nd_free(ND p) |
|
{ |
|
NM t,s; |
|
|
|
if ( !p ) |
|
return; |
|
t = BDY(p); |
|
while ( t ) { |
|
s = NEXT(t); |
|
FREENM(t); |
|
t = s; |
|
} |
|
FREEND(p); |
|
} |
|
|
|
void nd_append_red(unsigned int *d,int td,int i) |
|
{ |
|
NM m,m0; |
|
int h; |
|
|
|
NEWNM(m); |
|
h = ndl_hash_value(td,d); |
|
m->c = i; |
|
m->td = td; |
|
bcopy(d,m->dl,nd_wpd*sizeof(unsigned int)); |
|
NEXT(m) = nd_red[h]; |
|
nd_red[h] = m; |
} |
} |