version 1.24, 2003/06/19 07:08:19 |
version 1.36, 2005/11/25 07:18:32 |
|
|
* 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/dist.c,v 1.23 2003/05/28 07:32:32 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/engine/dist.c,v 1.35 2005/11/25 02:43:39 noro Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
|
|
Line 72 int (*primitive_cmpdl[3])() = {cmpdl_revgradlex,cmpdl_ |
|
Line 72 int (*primitive_cmpdl[3])() = {cmpdl_revgradlex,cmpdl_ |
|
int do_weyl; |
int do_weyl; |
|
|
int dp_nelim,dp_fcoeffs; |
int dp_nelim,dp_fcoeffs; |
struct order_spec dp_current_spec; |
struct order_spec *dp_current_spec; |
|
struct modorder_spec *dp_current_modspec; |
int *dp_dl_work; |
int *dp_dl_work; |
|
|
void comm_muld_trunc(VL vl,DP p1,DP p2,DL dl,DP *pr); |
void comm_muld_trunc(VL vl,DP p1,DP p2,DL dl,DP *pr); |
Line 80 void comm_quod(VL vl,DP p1,DP p2,DP *pr); |
|
Line 81 void comm_quod(VL vl,DP p1,DP p2,DP *pr); |
|
void muldm_trunc(VL vl,DP p,MP m0,DL dl,DP *pr); |
void muldm_trunc(VL vl,DP p,MP m0,DL dl,DP *pr); |
void muldc_trunc(VL vl,DP p,P c,DL dl,DP *pr); |
void muldc_trunc(VL vl,DP p,P c,DL dl,DP *pr); |
|
|
|
void order_init() |
|
{ |
|
struct order_spec *spec; |
|
|
|
create_order_spec(0,0,&spec); |
|
initd(spec); |
|
create_modorder_spec(0,0,&dp_current_modspec); |
|
} |
|
|
int has_sfcoef(DP f) |
int has_sfcoef(DP f) |
{ |
{ |
MP t; |
MP t; |
Line 111 int has_sfcoef_p(P f) |
|
Line 121 int has_sfcoef_p(P f) |
|
void initd(struct order_spec *spec) |
void initd(struct order_spec *spec) |
{ |
{ |
switch ( spec->id ) { |
switch ( spec->id ) { |
|
case 3: |
|
cmpdl = cmpdl_composite; |
|
dp_dl_work = (int *)MALLOC_ATOMIC(spec->nv*sizeof(int)); |
|
break; |
case 2: |
case 2: |
cmpdl = cmpdl_matrix; |
cmpdl = cmpdl_matrix; |
dp_dl_work = (int *)MALLOC_ATOMIC(spec->nv*sizeof(int)); |
dp_dl_work = (int *)MALLOC_ATOMIC(spec->nv*sizeof(int)); |
Line 151 void initd(struct order_spec *spec) |
|
Line 165 void initd(struct order_spec *spec) |
|
} |
} |
break; |
break; |
} |
} |
dp_current_spec = *spec; |
dp_current_spec = spec; |
} |
} |
|
|
void ptod(VL vl,VL dvl,P p,DP *pr) |
void ptod(VL vl,VL dvl,P p,DP *pr) |
Line 286 void addd(VL vl,DP p1,DP p2,DP *pr) |
|
Line 300 void addd(VL vl,DP p1,DP p2,DP *pr) |
|
int n; |
int n; |
MP m1,m2,mr,mr0; |
MP m1,m2,mr,mr0; |
P t; |
P t; |
|
DL d; |
|
|
if ( !p1 ) |
if ( !p1 ) |
*pr = p2; |
*pr = p2; |
else if ( !p2 ) |
else if ( !p2 ) |
*pr = p1; |
*pr = p1; |
else { |
else { |
|
if ( OID(p1) <= O_R ) { |
|
n = NV(p2); NEWDL(d,n); |
|
NEWMP(m1); m1->dl = d; C(m1) = (P)p1; NEXT(m1) = 0; |
|
MKDP(n,m1,p1); (p1)->sugar = 0; |
|
} |
|
if ( OID(p2) <= O_R ) { |
|
n = NV(p1); NEWDL(d,n); |
|
NEWMP(m2); m2->dl = d; C(m2) = (P)p2; NEXT(m2) = 0; |
|
MKDP(n,m2,p2); (p2)->sugar = 0; |
|
} |
for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) |
for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) |
switch ( (*cmpdl)(n,m1->dl,m2->dl) ) { |
switch ( (*cmpdl)(n,m1->dl,m2->dl) ) { |
case 0: |
case 0: |
Line 385 void symb_addd(DP p1,DP p2,DP *pr) |
|
Line 410 void symb_addd(DP p1,DP p2,DP *pr) |
|
NODE symb_merge(NODE m1,NODE m2,int n) |
NODE symb_merge(NODE m1,NODE m2,int n) |
{ |
{ |
NODE top,prev,cur,m,t; |
NODE top,prev,cur,m,t; |
|
int c,i; |
|
DL d1,d2; |
|
|
if ( !m1 ) |
if ( !m1 ) |
return m2; |
return m2; |
Line 405 NODE symb_merge(NODE m1,NODE m2,int n) |
|
Line 432 NODE symb_merge(NODE m1,NODE m2,int n) |
|
prev = top; cur = NEXT(top); |
prev = top; cur = NEXT(top); |
/* BDY(prev) > BDY(m) always holds */ |
/* BDY(prev) > BDY(m) always holds */ |
while ( cur && m ) { |
while ( cur && m ) { |
|
d1 = (DL)BDY(cur); |
|
d2 = (DL)BDY(m); |
|
#if 1 |
switch ( (*cmpdl)(n,(DL)BDY(cur),(DL)BDY(m)) ) { |
switch ( (*cmpdl)(n,(DL)BDY(cur),(DL)BDY(m)) ) { |
|
#else |
|
/* XXX only valid for DRL */ |
|
if ( d1->td > d2->td ) |
|
c = 1; |
|
else if ( d1->td < d2->td ) |
|
c = -1; |
|
else { |
|
for ( i = n-1; i >= 0 && d1->d[i] == d2->d[i]; i-- ); |
|
if ( i < 0 ) |
|
c = 0; |
|
else if ( d1->d[i] < d2->d[i] ) |
|
c = 1; |
|
else |
|
c = -1; |
|
} |
|
switch ( c ) { |
|
#endif |
case 0: |
case 0: |
m = NEXT(m); |
m = NEXT(m); |
prev = cur; cur = NEXT(cur); |
prev = cur; cur = NEXT(cur); |
Line 549 void subd(VL vl,DP p1,DP p2,DP *pr) |
|
Line 596 void subd(VL vl,DP p1,DP p2,DP *pr) |
|
void chsgnd(DP p,DP *pr) |
void chsgnd(DP p,DP *pr) |
{ |
{ |
MP m,mr,mr0; |
MP m,mr,mr0; |
|
Obj r; |
|
|
if ( !p ) |
if ( !p ) |
*pr = 0; |
*pr = 0; |
else { |
else if ( OID(p) <= O_R ) { |
|
chsgnr((Obj)p,&r); *pr = (DP)r; |
|
} else { |
for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) { |
for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) { |
NEXTMP(mr0,mr); chsgnp(C(m),&C(mr)); mr->dl = m->dl; |
NEXTMP(mr0,mr); chsgnp(C(m),&C(mr)); mr->dl = m->dl; |
} |
} |
Line 1114 int cmpdl_gradlex(int n,DL d1,DL d2) |
|
Line 1164 int cmpdl_gradlex(int n,DL d1,DL d2) |
|
|
|
int cmpdl_revgradlex(int n,DL d1,DL d2) |
int cmpdl_revgradlex(int n,DL d1,DL d2) |
{ |
{ |
register int i; |
register int i,c; |
register int *p1,*p2; |
register int *p1,*p2; |
|
|
if ( d1->td > d2->td ) |
if ( d1->td > d2->td ) |
Line 1122 int cmpdl_revgradlex(int n,DL d1,DL d2) |
|
Line 1172 int cmpdl_revgradlex(int n,DL d1,DL d2) |
|
else if ( d1->td < d2->td ) |
else if ( d1->td < d2->td ) |
return -1; |
return -1; |
else { |
else { |
for ( i= n - 1, p1 = d1->d+n-1, p2 = d2->d+n-1; |
i = n-1; |
i >= 0 && *p1 == *p2; i--, p1--, p2-- ); |
p1 = d1->d+n-1; |
return i < 0 ? 0 : (*p1 < *p2 ? 1 : -1); |
p2 = d2->d+n-1; |
|
while ( i >= 7 ) { |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
i -= 8; |
|
} |
|
switch ( i ) { |
|
case 6: |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
return 0; |
|
case 5: |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
return 0; |
|
case 4: |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
return 0; |
|
case 3: |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
return 0; |
|
case 2: |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
return 0; |
|
case 1: |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
return 0; |
|
case 0: |
|
c = (*p1--) - (*p2--); if ( c ) goto LAST; |
|
return 0; |
|
default: |
|
return 0; |
|
} |
|
LAST: |
|
if ( c > 0 ) return -1; |
|
else return 1; |
} |
} |
} |
} |
|
|
Line 1385 int cmpdl_order_pair(int n,DL d1,DL d2) |
|
Line 1495 int cmpdl_order_pair(int n,DL d1,DL d2) |
|
int len,head; |
int len,head; |
struct order_pair *pair; |
struct order_pair *pair; |
|
|
len = dp_current_spec.ord.block.length; |
len = dp_current_spec->ord.block.length; |
pair = dp_current_spec.ord.block.order_pair; |
pair = dp_current_spec->ord.block.order_pair; |
|
|
head = 0; |
head = 0; |
for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) { |
for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) { |
Line 1435 int cmpdl_order_pair(int n,DL d1,DL d2) |
|
Line 1545 int cmpdl_order_pair(int n,DL d1,DL d2) |
|
return 0; |
return 0; |
} |
} |
|
|
|
int cmpdl_composite(int nv,DL d1,DL d2) |
|
{ |
|
int n,i,j,k,start,s,len; |
|
int *dw; |
|
struct sparse_weight *sw; |
|
struct weight_or_block *worb; |
|
int *w,*t1,*t2; |
|
|
|
n = dp_current_spec->ord.composite.length; |
|
worb = dp_current_spec->ord.composite.w_or_b; |
|
w = dp_dl_work; |
|
for ( i = 0, t1 = d1->d, t2 = d2->d; i < nv; i++ ) |
|
w[i] = t1[i]-t2[i]; |
|
for ( i = 0; i < n; i++, worb++ ) { |
|
len = worb->length; |
|
switch ( worb->type ) { |
|
case IS_DENSE_WEIGHT: |
|
dw = worb->body.dense_weight; |
|
for ( j = 0, s = 0; j < len; j++ ) |
|
s += dw[j]*w[j]; |
|
if ( s > 0 ) return 1; |
|
else if ( s < 0 ) return -1; |
|
break; |
|
case IS_SPARSE_WEIGHT: |
|
sw = worb->body.sparse_weight; |
|
for ( j = 0, s = 0; j < len; j++ ) |
|
s += sw[j].value*w[sw[j].pos]; |
|
if ( s > 0 ) return 1; |
|
else if ( s < 0 ) return -1; |
|
break; |
|
case IS_BLOCK: |
|
start = worb->body.block.start; |
|
switch ( worb->body.block.order ) { |
|
case 0: |
|
for ( j = 0, k = start, s = 0; j < len; j++, k++ ) { |
|
s += MUL_WEIGHT(w[k],k); |
|
} |
|
if ( s > 0 ) return 1; |
|
else if ( s < 0 ) return -1; |
|
else { |
|
for ( j = k-1; j >= start && w[j] == 0; j-- ); |
|
if ( j >= start ) |
|
return w[j] < 0 ? 1 : -1; |
|
} |
|
break; |
|
case 1: |
|
for ( j = 0, k = start, s = 0; j < len; j++, k++ ) { |
|
s += MUL_WEIGHT(w[k],k); |
|
} |
|
if ( s > 0 ) return 1; |
|
else if ( s < 0 ) return -1; |
|
else { |
|
for ( j = 0, k = start; j < len && w[j] == 0; j++, k++ ); |
|
if ( j < len ) |
|
return w[j] > 0 ? 1 : -1; |
|
} |
|
break; |
|
case 2: |
|
for ( j = 0, k = start; j < len && w[j] == 0; j++, k++ ); |
|
if ( j < len ) |
|
return w[j] > 0 ? 1 : -1; |
|
break; |
|
} |
|
break; |
|
} |
|
} |
|
return 0; |
|
} |
|
|
int cmpdl_matrix(int n,DL d1,DL d2) |
int cmpdl_matrix(int n,DL d1,DL d2) |
{ |
{ |
int *v,*w,*t1,*t2; |
int *v,*w,*t1,*t2; |
Line 1443 int cmpdl_matrix(int n,DL d1,DL d2) |
|
Line 1622 int cmpdl_matrix(int n,DL d1,DL d2) |
|
|
|
for ( i = 0, t1 = d1->d, t2 = d2->d, w = dp_dl_work; i < n; i++ ) |
for ( i = 0, t1 = d1->d, t2 = d2->d, w = dp_dl_work; i < n; i++ ) |
w[i] = t1[i]-t2[i]; |
w[i] = t1[i]-t2[i]; |
len = dp_current_spec.ord.matrix.row; |
len = dp_current_spec->ord.matrix.row; |
matrix = dp_current_spec.ord.matrix.matrix; |
matrix = dp_current_spec->ord.matrix.matrix; |
for ( j = 0; j < len; j++ ) { |
for ( j = 0; j < len; j++ ) { |
v = matrix[j]; |
v = matrix[j]; |
for ( i = 0, s = 0; i < n; i++ ) |
for ( i = 0, s = 0; i < n; i++ ) |
Line 1455 int cmpdl_matrix(int n,DL d1,DL d2) |
|
Line 1634 int cmpdl_matrix(int n,DL d1,DL d2) |
|
return -1; |
return -1; |
} |
} |
return 0; |
return 0; |
|
} |
|
|
|
GeoBucket create_bucket() |
|
{ |
|
GeoBucket g; |
|
|
|
g = CALLOC(1,sizeof(struct oGeoBucket)); |
|
g->m = 32; |
|
return g; |
|
} |
|
|
|
void add_bucket(GeoBucket g,NODE d,int nv) |
|
{ |
|
int l,k,m; |
|
|
|
l = length(d); |
|
for ( k = 0, m = 1; l > m; k++, m <<= 1 ); |
|
/* 2^(k-1) < l <= 2^k */ |
|
d = symb_merge(g->body[k],d,nv); |
|
for ( ; length(d) > (1<<(k)); k++ ) { |
|
g->body[k] = 0; |
|
d = symb_merge(g->body[k+1],d,nv); |
|
} |
|
g->body[k] = d; |
|
g->m = MAX(g->m,k); |
|
} |
|
|
|
DL remove_head_bucket(GeoBucket g,int nv) |
|
{ |
|
int j,i,c,m; |
|
DL d; |
|
|
|
j = -1; |
|
m = g->m; |
|
for ( i = 0; i <= m; i++ ) { |
|
if ( !g->body[i] ) |
|
continue; |
|
if ( j < 0 ) j = i; |
|
else { |
|
c = (*cmpdl)(nv,g->body[i]->body,g->body[j]->body); |
|
if ( c > 0 ) |
|
j = i; |
|
else if ( c == 0 ) |
|
g->body[i] = NEXT(g->body[i]); |
|
} |
|
} |
|
if ( j < 0 ) |
|
return 0; |
|
else { |
|
d = g->body[j]->body; |
|
g->body[j] = NEXT(g->body[j]); |
|
return d; |
|
} |
|
} |
|
|
|
/* DPV functions */ |
|
|
|
void adddv(VL vl,DPV p1,DPV p2,DPV *pr) |
|
{ |
|
int i,len; |
|
DP *e; |
|
|
|
if ( !p1 || !p2 ) |
|
error("adddv : invalid argument"); |
|
else if ( p1->len != p2->len ) |
|
error("adddv : size mismatch"); |
|
else { |
|
len = p1->len; |
|
e = (DP *)MALLOC(p1->len*sizeof(DP)); |
|
for ( i = 0; i < len; i++ ) |
|
addd(vl,p1->body[i],p2->body[i],&e[i]); |
|
MKDPV(len,e,*pr); |
|
(*pr)->sugar = MAX(p1->sugar,p2->sugar); |
|
} |
|
} |
|
|
|
void subdv(VL vl,DPV p1,DPV p2,DPV *pr) |
|
{ |
|
int i,len; |
|
DP *e; |
|
|
|
if ( !p1 || !p2 ) |
|
error("subdv : invalid argument"); |
|
else if ( p1->len != p2->len ) |
|
error("subdv : size mismatch"); |
|
else { |
|
len = p1->len; |
|
e = (DP *)MALLOC(p1->len*sizeof(DP)); |
|
for ( i = 0; i < len; i++ ) |
|
subd(vl,p1->body[i],p2->body[i],&e[i]); |
|
MKDPV(len,e,*pr); |
|
(*pr)->sugar = MAX(p1->sugar,p2->sugar); |
|
} |
|
} |
|
|
|
void chsgndv(DPV p1,DPV *pr) |
|
{ |
|
int i,len; |
|
DP *e; |
|
|
|
if ( !p1 ) |
|
error("subdv : invalid argument"); |
|
else { |
|
len = p1->len; |
|
e = (DP *)MALLOC(p1->len*sizeof(DP)); |
|
for ( i = 0; i < len; i++ ) |
|
chsgnd(p1->body[i],&e[i]); |
|
MKDPV(len,e,*pr); |
|
(*pr)->sugar = p1->sugar; |
|
} |
|
} |
|
|
|
void muldv(VL vl,DP p1,DPV p2,DPV *pr) |
|
{ |
|
int i,len; |
|
DP *e; |
|
|
|
len = p2->len; |
|
e = (DP *)MALLOC(p2->len*sizeof(DP)); |
|
if ( !p1 ) { |
|
MKDPV(len,e,*pr); |
|
(*pr)->sugar = 0; |
|
} else { |
|
for ( i = 0; i < len; i++ ) |
|
muld(vl,p1,p2->body[i],&e[i]); |
|
MKDPV(len,e,*pr); |
|
(*pr)->sugar = p1->sugar + p2->sugar; |
|
} |
|
} |
|
|
|
int compdv(VL vl,DPV p1,DPV p2) |
|
{ |
|
int i,t,len; |
|
|
|
if ( p1->len != p2->len ) |
|
error("compdv : size mismatch"); |
|
else { |
|
len = p1->len; |
|
for ( i = 0; i < len; i++ ) |
|
if ( t = compd(vl,p1->body[i],p2->body[i]) ) |
|
return t; |
|
return 0; |
|
} |
|
} |
|
|
|
int ni_next(int *a,int n) |
|
{ |
|
int i,j,k,kj; |
|
|
|
/* find the first nonzero a[j] */ |
|
for ( j = 0; j < n && a[j] == 0; j++ ); |
|
/* find the first zero a[k] after a[j] */ |
|
for ( k = j; k < n && a[k] == 1; k++ ); |
|
if ( k == n ) return 0; |
|
/* a[0] = 0, ... , a[j-1] = 0, a[j] = 1, ..., a[k-1] = 1, a[k] = 0 */ |
|
/* a[0] = 1,..., a[k-j-2] = 1, a[k-j-1] = 0, ..., a[k-1] = 0, a[k] = 1 */ |
|
kj = k-j-1; |
|
for ( i = 0; i < kj; i++ ) a[i] = 1; |
|
for ( ; i < k; i++ ) a[i] = 0; |
|
a[k] = 1; |
|
return 1; |
|
} |
|
|
|
int comp_nbm(NBM a,NBM b) |
|
{ |
|
int d,i,w; |
|
int *ab,*bb; |
|
|
|
if ( a->d > b->d ) return 1; |
|
else if ( a->d < b->d ) return -1; |
|
else { |
|
d = a->d; ab = a->b; bb = b->b; |
|
w = (d+31)/32; |
|
for ( i = 0; i < w; i++ ) |
|
if ( ab[i] > bb[i] ) return 1; |
|
else if ( ab[i] < bb[i] ) return -1; |
|
return 0; |
|
} |
|
} |
|
|
|
NBM mul_nbm(NBM a,NBM b) |
|
{ |
|
int ad,bd,d,i,j; |
|
int *ab,*bb,*mb; |
|
NBM m; |
|
Q c,c1; |
|
NODE r; |
|
NBP u; |
|
|
|
ad = a->d; bd = b->d; ab = a->b; bb = b->b; |
|
d = ad + bd; |
|
NEWNBM(m); NEWNBMBDY(m,d); |
|
m->d = d; mulq(a->c,b->c,&m->c); mb = m->b; |
|
j = 0; |
|
for ( i = 0; i < ad; i++, j++ ) |
|
if ( NBM_GET(ab,i) ) NBM_SET(mb,j); |
|
else NBM_CLR(mb,j); |
|
for ( i = 0; i < bd; i++, j++ ) |
|
if ( NBM_GET(bb,i) ) NBM_SET(mb,j); |
|
else NBM_CLR(mb,j); |
|
return m; |
|
} |
|
|
|
NBP shuffle_mul_nbm(NBM a,NBM b) |
|
{ |
|
int ad,bd,d,i,ai,bi,bit,s; |
|
int *ab,*bb,*wmb,*w; |
|
NBM wm,tm; |
|
Q c,c1; |
|
NODE r,t,t1,p; |
|
NBP u; |
|
|
|
ad = a->d; bd = b->d; ab = a->b; bb = b->b; |
|
d = ad + bd; |
|
w = (int *)ALLOCA(d*sizeof(int)); |
|
NEWNBM(wm); NEWNBMBDY(wm,d); wmb = wm->b; |
|
for ( i = 0; i < ad; i++ ) w[i] = 1; |
|
for ( ; i < d; i++ ) w[i] = 0; |
|
mulq(a->c,b->c,&c); |
|
r = 0; |
|
do { |
|
wm->d = d; wm->c = c; |
|
ai = 0; bi = 0; |
|
for ( i = 0; i < d; i++ ) { |
|
if ( w[i] ) { bit = NBM_GET(ab,ai); ai++; } |
|
else { bit = NBM_GET(bb,bi); bi++; } |
|
if ( bit ) NBM_SET(wmb,i); |
|
else NBM_CLR(wmb,i); |
|
} |
|
for ( p = 0, t = r; t; p = t, t = NEXT(t) ) { |
|
tm = (NBM)BDY(t); |
|
s = comp_nbm(tm,wm); |
|
if ( s < 0 ) { |
|
/* insert */ |
|
MKNODE(t1,wm,t); |
|
if ( !p ) r = t1; |
|
else NEXT(p) = t1; |
|
NEWNBM(wm); NEWNBMBDY(wm,d); wmb = wm->b; |
|
break; |
|
} else if ( s == 0 ) { |
|
/* add coefs */ |
|
addq(tm->c,c,&c1); |
|
if ( c1 ) tm->c = c1; |
|
else NEXT(p) = NEXT(t); |
|
break; |
|
} |
|
} |
|
if ( !t ) { |
|
/* append */ |
|
MKNODE(t1,wm,t); |
|
if ( !p ) r = t1; |
|
else NEXT(p) = t1; |
|
NEWNBM(wm); NEWNBMBDY(wm,d); wmb = wm->b; |
|
} |
|
} while ( ni_next(w,d) ); |
|
MKNBP(u,r); |
|
return u; |
|
} |
|
|
|
int nbmtoxky(NBM a,int *b) |
|
{ |
|
int d,i,j,k; |
|
int *p; |
|
|
|
d = a->d; p = a->b; |
|
for ( i = j = 0, k = 1; i < d; i++ ) { |
|
if ( !NBM_GET(p,i) ) { |
|
b[j++] = k; |
|
k = 1; |
|
} else k++; |
|
} |
|
return j; |
|
} |
|
|
|
NBP harmonic_mul_nbm(NBM a,NBM b) |
|
{ |
|
int da,db,d,la,lb,lmax,lmin,l,lab,la1,lb1,lab1; |
|
int i,j,k,ia,ib,s; |
|
int *wa,*wb,*w,*wab,*wa1,*wmb; |
|
Q c,c1; |
|
NBM wm,tm; |
|
NODE r,t1,t,p; |
|
NBP u; |
|
|
|
da = a->d; db = b->d; d = da+db; |
|
wa = (int *)ALLOCA(da*sizeof(int)); |
|
wb = (int *)ALLOCA(db*sizeof(int)); |
|
la = nbmtoxky(a,wa); |
|
lb = nbmtoxky(b,wb); |
|
mulq(a->c,b->c,&c); |
|
/* wa[0],..,wa[la-1] <-> x^wa[0]y x^wa[1]y .. */ |
|
/* lmax : total length */ |
|
lmax = la+lb; |
|
lmin = la>lb?la:lb; |
|
w = (int *)ALLOCA(lmax*sizeof(int)); |
|
/* position of a+b */ |
|
wab = (int *)ALLOCA(lmax*sizeof(int)); |
|
/* position of a */ |
|
wa1 = (int *)ALLOCA(lmax*sizeof(int)); |
|
NEWNBM(wm); NEWNBMBDY(wm,d); wmb = wm->b; |
|
for ( l = lmin, r = 0; l <= lmax; l++ ) { |
|
lab = lmax - l; |
|
la1 = la - lab; |
|
lb1 = lb - lab; |
|
lab1 = l-lab; |
|
/* partion l into three parts: a, b, a+b */ |
|
/* initialize wab */ |
|
for ( i = 0; i < lab; i++ ) wab[i] = 1; |
|
for ( ; i < l; i++ ) wab[i] = 0; |
|
do { |
|
/* initialize wa1 */ |
|
for ( i = 0; i < la1; i++ ) wa1[i] = 1; |
|
for ( ; i < lab1; i++ ) wa1[i] = 0; |
|
do { |
|
ia = 0; ib = 0; |
|
for ( i = j = 0; i < l; i++ ) |
|
if ( wab[i] ) w[i] = wa[ia++]+wb[ib++]; |
|
else if ( wa1[j++] ) w[i] = wa[ia++]; |
|
else w[i] = wb[ib++]; |
|
for ( i = j = 0; i < l; i++ ) { |
|
for ( k = w[i]-1; k > 0; k--, j++ ) NBM_SET(wmb,j); |
|
NBM_CLR(wmb,j); j++; |
|
} |
|
wm->d = j; wm->c = c; |
|
for ( p = 0, t = r; t; p = t, t = NEXT(t) ) { |
|
tm = (NBM)BDY(t); |
|
s = comp_nbm(tm,wm); |
|
if ( s < 0 ) { |
|
/* insert */ |
|
MKNODE(t1,wm,t); |
|
if ( !p ) r = t1; |
|
else NEXT(p) = t1; |
|
NEWNBM(wm); NEWNBMBDY(wm,d); wmb = wm->b; |
|
break; |
|
} else if ( s == 0 ) { |
|
/* add coefs */ |
|
addq(tm->c,c,&c1); |
|
if ( c1 ) tm->c = c1; |
|
else NEXT(p) = NEXT(t); |
|
break; |
|
} |
|
} |
|
if ( !t ) { |
|
/* append */ |
|
MKNODE(t1,wm,t); |
|
if ( !p ) r = t1; |
|
else NEXT(p) = t1; |
|
NEWNBM(wm); NEWNBMBDY(wm,d); wmb = wm->b; |
|
} |
|
} while ( ni_next(wa1,lab1) ); |
|
} while ( ni_next(wab,l) ); |
|
} |
|
MKNBP(u,r); |
|
return u; |
|
} |
|
|
|
void addnbp(VL vl,NBP p1,NBP p2, NBP *rp) |
|
{ |
|
NODE b1,b2,br,br0; |
|
NBM m1,m2,m; |
|
Q c; |
|
|
|
if ( !p1 ) |
|
*rp = p2; |
|
else if ( !p2 ) |
|
*rp = p1; |
|
else { |
|
for ( b1 = BDY(p1), b2 = BDY(p2), br0 = 0; b1 && b2; ) { |
|
m1 = (NBM)BDY(b1); m2 = (NBM)BDY(b2); |
|
switch ( comp_nbm(m1,m2) ) { |
|
case 0: |
|
addq(m1->c,m2->c,&c); |
|
if ( c ) { |
|
NEXTNODE(br0,br); |
|
NEWNBM(m); m->d = m1->d; m->c = c; m->b = m1->b; |
|
BDY(br) = (pointer)m; |
|
} |
|
b1 = NEXT(b1); b2 = NEXT(b2); break; |
|
case 1: |
|
NEXTNODE(br0,br); BDY(br) = BDY(b1); |
|
b1 = NEXT(b1); break; |
|
case -1: |
|
NEXTNODE(br0,br); BDY(br) = BDY(b2); |
|
b2 = NEXT(b2); break; |
|
} |
|
} |
|
if ( !br0 ) |
|
if ( b1 ) |
|
br0 = b1; |
|
else if ( b2 ) |
|
br0 = b2; |
|
else { |
|
*rp = 0; |
|
return; |
|
} |
|
else if ( b1 ) |
|
NEXT(br) = b1; |
|
else if ( b2 ) |
|
NEXT(br) = b2; |
|
else |
|
NEXT(br) = 0; |
|
MKNBP(*rp,br0); |
|
} |
|
} |
|
|
|
void subnbp(VL vl,NBP p1,NBP p2, NBP *rp) |
|
{ |
|
NBP t; |
|
|
|
chsgnnbp(p2,&t); |
|
addnbp(vl,p1,t,rp); |
|
} |
|
|
|
void chsgnnbp(NBP p,NBP *rp) |
|
{ |
|
NODE r0,r,b; |
|
NBM m,m1; |
|
|
|
for ( r0 = 0, b = BDY(p); b; b = NEXT(b) ) { |
|
NEXTNODE(r0,r); |
|
m = (NBM)BDY(b); |
|
NEWNBM(m1); m1->d = m->d; m1->b = m->b; chsgnq(m->c,&m1->c); |
|
BDY(r) = m1; |
|
} |
|
if ( r0 ) NEXT(r) = 0; |
|
MKNBP(*rp,r0); |
|
} |
|
|
|
void mulnbmnbp(VL vl,NBM m,NBP p, NBP *rp); |
|
void mulnbpnbm(VL vl,NBP p,NBM m, NBP *rp); |
|
|
|
void mulnbp(VL vl,NBP p1,NBP p2, NBP *rp) |
|
{ |
|
NODE b,n; |
|
NBP r,t,s; |
|
NBM m; |
|
|
|
if ( !p1 || !p2 ) { |
|
*rp = 0; return; |
|
} |
|
if ( OID(p1) != O_NBP ) { |
|
if ( !NUM(p1) || !RATN(p1) ) error("mulnbp : invalid argument"); |
|
NEWNBM(m); m->d = 0; m->b = 0; m->c = (Q)p1; |
|
MKNODE(n,m,0); MKNBP(p1,n); |
|
} |
|
if ( OID(p2) != O_NBP ) { |
|
if ( !NUM(p2) || !RATN(p2) ) error("mulnbp : invalid argument"); |
|
NEWNBM(m); m->d = 0; m->b = 0; m->c = (Q)p2; |
|
MKNODE(n,m,0); MKNBP(p2,n); |
|
} |
|
if ( length(BDY(p1)) < length(BDY(p2)) ) { |
|
for ( r = 0, b = BDY(p1); b; b = NEXT(b) ) { |
|
mulnbmnbp(vl,(NBM)BDY(b),p2,&t); |
|
addnbp(vl,r,t,&s); r = s; |
|
} |
|
*rp = r; |
|
} else { |
|
for ( r = 0, b = BDY(p2); b; b = NEXT(b) ) { |
|
mulnbpnbm(vl,p1,(NBM)BDY(b),&t); |
|
addnbp(vl,r,t,&s); r = s; |
|
} |
|
*rp = r; |
|
} |
|
} |
|
|
|
void mulnbmnbp(VL vl,NBM m,NBP p, NBP *rp) |
|
{ |
|
NODE b,r0,r; |
|
|
|
if ( !p ) *rp = 0; |
|
else { |
|
for ( r0 = 0, b = BDY(p); b; b = NEXT(b) ) { |
|
NEXTNODE(r0,r); |
|
BDY(r) = mul_nbm(m,(NBM)BDY(b)); |
|
} |
|
if ( r0 ) NEXT(r) = 0; |
|
MKNBP(*rp,r0); |
|
} |
|
} |
|
|
|
void mulnbpnbm(VL vl,NBP p,NBM m, NBP *rp) |
|
{ |
|
NODE b,r0,r; |
|
|
|
if ( !p ) *rp = 0; |
|
else { |
|
for ( r0 = 0, b = BDY(p); b; b = NEXT(b) ) { |
|
NEXTNODE(r0,r); |
|
BDY(r) = mul_nbm((NBM)BDY(b),m); |
|
} |
|
if ( r0 ) NEXT(r) = 0; |
|
MKNBP(*rp,r0); |
|
} |
|
} |
|
|
|
void pwrnbp(VL vl,NBP a,Q q,NBP *c) |
|
{ |
|
int t; |
|
NBP a1,a2; |
|
N n1; |
|
Q q1; |
|
NBM m; |
|
NODE r; |
|
|
|
if ( !q ) { |
|
NEWNBM(m); m->d = 0; m->c = ONE; m->b = 0; |
|
MKNODE(r,m,0); MKNBP(*c,r); |
|
} else if ( !a ) |
|
*c = 0; |
|
else if ( UNIQ(q) ) |
|
*c = a; |
|
else { |
|
t = divin(NM(q),2,&n1); NTOQ(n1,1,q1); |
|
pwrnbp(vl,a,q1,&a1); |
|
mulnbp(vl,a1,a1,&a2); |
|
if ( t ) |
|
mulnbp(vl,a2,a,c); |
|
else |
|
*c = a2; |
|
} |
|
} |
|
|
|
void shuffle_mulnbp(VL vl,NBP p1,NBP p2, NBP *rp) |
|
{ |
|
NODE b1,b2,n; |
|
NBP r,t,s; |
|
NBM m; |
|
|
|
if ( !p1 || !p2 ) { |
|
*rp = 0; return; |
|
} |
|
if ( OID(p1) != O_NBP ) { |
|
if ( !NUM(p1) || !RATN(p1) ) error("mulnbp : invalid argument"); |
|
NEWNBM(m); m->d = 0; m->b = 0; m->c = (Q)p1; |
|
MKNODE(n,m,0); MKNBP(p1,n); |
|
} |
|
if ( OID(p2) != O_NBP ) { |
|
if ( !NUM(p2) || !RATN(p2) ) error("mulnbp : invalid argument"); |
|
NEWNBM(m); m->d = 0; m->b = 0; m->c = (Q)p2; |
|
MKNODE(n,m,0); MKNBP(p2,n); |
|
} |
|
for ( r = 0, b1 = BDY(p1); b1; b1 = NEXT(b1) ) |
|
for ( m = BDY(b1), b2 = BDY(p2); b2; b2 = NEXT(b2) ) { |
|
t = shuffle_mul_nbm(m,(NBM)BDY(b2)); |
|
addnbp(vl,r,t,&s); r = s; |
|
} |
|
*rp = r; |
|
} |
|
|
|
void harmonic_mulnbp(VL vl,NBP p1,NBP p2, NBP *rp) |
|
{ |
|
NODE b1,b2,n; |
|
NBP r,t,s; |
|
NBM m; |
|
|
|
if ( !p1 || !p2 ) { |
|
*rp = 0; return; |
|
} |
|
if ( OID(p1) != O_NBP ) { |
|
if ( !NUM(p1) || !RATN(p1) ) error("mulnbp : invalid argument"); |
|
NEWNBM(m); m->d = 0; m->b = 0; m->c = (Q)p1; |
|
MKNODE(n,m,0); MKNBP(p1,n); |
|
} |
|
if ( OID(p2) != O_NBP ) { |
|
if ( !NUM(p2) || !RATN(p2) ) error("mulnbp : invalid argument"); |
|
NEWNBM(m); m->d = 0; m->b = 0; m->c = (Q)p2; |
|
MKNODE(n,m,0); MKNBP(p2,n); |
|
} |
|
for ( r = 0, b1 = BDY(p1); b1; b1 = NEXT(b1) ) |
|
for ( m = BDY(b1), b2 = BDY(p2); b2; b2 = NEXT(b2) ) { |
|
t = harmonic_mul_nbm(m,(NBM)BDY(b2)); |
|
addnbp(vl,r,t,&s); r = s; |
|
} |
|
*rp = r; |
} |
} |