version 1.7, 2004/12/10 02:45:02 |
version 1.10, 2005/08/02 07:16:42 |
|
|
/* |
/* |
* $OpenXM: OpenXM_contrib2/asir2000/engine/dalg.c,v 1.6 2004/12/07 15:15:52 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/engine/dalg.c,v 1.9 2005/07/11 00:24:02 noro Exp $ |
*/ |
*/ |
|
|
#include "ca.h" |
#include "ca.h" |
|
|
static NumberField current_numberfield; |
static NumberField current_numberfield; |
extern struct order_spec *dp_current_spec; |
extern struct order_spec *dp_current_spec; |
void simpdalg(DAlg da,DAlg *r); |
void simpdalg(DAlg da,DAlg *r); |
void invdalg(DAlg a,DAlg *c); |
int invdalg(DAlg a,DAlg *c); |
void rmcontdalg(DAlg a, DAlg *c); |
void rmcontdalg(DAlg a, DAlg *c); |
void algtodalg(Alg a,DAlg *r); |
void algtodalg(Alg a,DAlg *r); |
void dalgtoalg(DAlg da,Alg *r); |
void dalgtoalg(DAlg da,Alg *r); |
Line 62 void setfield_dalg(NODE alist) |
|
Line 62 void setfield_dalg(NODE alist) |
|
nf->dim = dim = length(mblist); |
nf->dim = dim = length(mblist); |
nf->mb = mb = (DP *)MALLOC(dim*sizeof(DP)); |
nf->mb = mb = (DP *)MALLOC(dim*sizeof(DP)); |
for ( i = 0, t = mblist; t; t = NEXT(t), i++ ) |
for ( i = 0, t = mblist; t; t = NEXT(t), i++ ) |
mb[i] = (DP)BDY(t); |
mb[dim-i-1] = (DP)BDY(t); |
} |
} |
|
|
|
void setfield_gb(NODE gb,VL vl,struct order_spec *spec) |
|
{ |
|
NumberField nf; |
|
VL vl1,vl2; |
|
int n,i,dim; |
|
Alg *gen; |
|
P *defpoly; |
|
P p; |
|
Q c,iq,two; |
|
DP *ps,*mb; |
|
DP one; |
|
NODE t,b,b1,b2,hlist,mblist; |
|
struct order_spec *current_spec; |
|
|
|
nf = (NumberField)MALLOC(sizeof(struct oNumberField)); |
|
current_numberfield = nf; |
|
for ( vl1 = vl, n = 0; vl1; vl1 = NEXT(vl1), n++ ); |
|
nf->n = n; |
|
nf->psn = length(gb); |
|
nf->vl = vl; |
|
nf->defpoly = defpoly = (P *)MALLOC(nf->psn*sizeof(P)); |
|
nf->ps = ps = (DP *)MALLOC(nf->psn*sizeof(DP)); |
|
current_spec = dp_current_spec; |
|
nf->spec = spec; |
|
initd(nf->spec); |
|
for ( b = hlist = 0, i = 0, t = gb; i < nf->psn; t = NEXT(t), i++ ) { |
|
ptozp((P)BDY(t),1,&c,&defpoly[i]); |
|
ptod(CO,vl,defpoly[i],&ps[i]); |
|
STOQ(i,iq); MKNODE(b1,(pointer)iq,b); b = b1; |
|
MKNODE(b2,(pointer)ps[i],hlist); hlist = b2; |
|
} |
|
ptod(ALG,vl,(P)ONE,&one); |
|
MKDAlg(one,ONE,nf->one); |
|
nf->ind = b; |
|
dp_mbase(hlist,&mblist); |
|
initd(current_spec); |
|
nf->dim = dim = length(mblist); |
|
nf->mb = mb = (DP *)MALLOC(dim*sizeof(DP)); |
|
for ( i = 0, t = mblist; t; t = NEXT(t), i++ ) |
|
mb[dim-i-1] = (DP)BDY(t); |
|
} |
|
|
void qtodalg(Q q,DAlg *r) |
void qtodalg(Q q,DAlg *r) |
{ |
{ |
NumberField nf; |
NumberField nf; |
Line 294 void algtodalg(Alg a,DAlg *r) |
|
Line 336 void algtodalg(Alg a,DAlg *r) |
|
NTOQ(NM(c),SGN(c),c1); |
NTOQ(NM(c),SGN(c),c1); |
NTOQ(DN(c),1,d1); |
NTOQ(DN(c),1,d1); |
muldc(CO,nf->one->nm,(P)c1,&dp); |
muldc(CO,nf->one->nm,(P)c1,&dp); |
MKDAlg(dp,c1,*r); |
MKDAlg(dp,d1,*r); |
} |
} |
break; |
break; |
case N_A: |
case N_A: |
Line 358 void simpdalg(DAlg da,DAlg *r) |
|
Line 400 void simpdalg(DAlg da,DAlg *r) |
|
} |
} |
current_spec = dp_current_spec; initd(nf->spec); |
current_spec = dp_current_spec; initd(nf->spec); |
dp_true_nf(nf->ind,da->nm,nf->ps,1,&nm,&dn); |
dp_true_nf(nf->ind,da->nm,nf->ps,1,&nm,&dn); |
initd(current_spec); |
if ( !nm ) *r = 0; |
mulq(da->dn,dn,&dn1); |
else { |
MKDAlg(nm,dn1,d); |
initd(current_spec); |
rmcontdalg(d,r); |
mulq(da->dn,dn,&dn1); |
|
MKDAlg(nm,dn1,d); |
|
rmcontdalg(d,r); |
|
} |
} |
} |
|
|
void adddalg(DAlg a,DAlg b,DAlg *c) |
void adddalg(DAlg a,DAlg b,DAlg *c) |
Line 463 void muldalg(DAlg a,DAlg b,DAlg *c) |
|
Line 508 void muldalg(DAlg a,DAlg b,DAlg *c) |
|
void divdalg(DAlg a,DAlg b,DAlg *c) |
void divdalg(DAlg a,DAlg b,DAlg *c) |
{ |
{ |
DAlg inv,t; |
DAlg inv,t; |
|
int ret; |
|
|
if ( !current_numberfield ) |
if ( !current_numberfield ) |
error("divdalg : current_numberfield is not set"); |
error("divdalg : current_numberfield is not set"); |
Line 472 void divdalg(DAlg a,DAlg b,DAlg *c) |
|
Line 518 void divdalg(DAlg a,DAlg b,DAlg *c) |
|
c = 0; |
c = 0; |
else { |
else { |
qtodalg((Q)a,&t); a = t; qtodalg((Q)b,&t); b = t; |
qtodalg((Q)a,&t); a = t; qtodalg((Q)b,&t); b = t; |
invdalg(b,&inv); |
ret = invdalg(b,&inv); |
|
if ( !ret ) { |
|
error("divdalg : the denominator is not invertible"); |
|
} |
muldalg(a,inv,c); |
muldalg(a,inv,c); |
} |
} |
} |
} |
Line 496 void rmcontdalg(DAlg a, DAlg *r) |
|
Line 545 void rmcontdalg(DAlg a, DAlg *r) |
|
} |
} |
} |
} |
|
|
void invdalg(DAlg a,DAlg *c) |
int invdalg(DAlg a,DAlg *c) |
{ |
{ |
NumberField nf; |
NumberField nf; |
int dim,n,i,j; |
int dim,n,i,j,k,l; |
DP *mb; |
DP *mb; |
DP m,d,u; |
DP m,d,u; |
N ln,gn,qn; |
N ln,gn,qn; |
Line 510 void invdalg(DAlg a,DAlg *c) |
|
Line 559 void invdalg(DAlg a,DAlg *c) |
|
Q **mat,**solmat; |
Q **mat,**solmat; |
MP mp0,mp; |
MP mp0,mp; |
int *rinfo,*cinfo; |
int *rinfo,*cinfo; |
|
int rank,nparam; |
|
NODE nd0,nd,ndt; |
struct order_spec *current_spec; |
struct order_spec *current_spec; |
struct oEGT eg0,eg1; |
struct oEGT eg0,eg1; |
extern struct oEGT eg_le; |
extern struct oEGT eg_le; |
Line 530 void invdalg(DAlg a,DAlg *c) |
|
Line 581 void invdalg(DAlg a,DAlg *c) |
|
MKDAlg(u,ONE,a0); |
MKDAlg(u,ONE,a0); |
simp = (DAlg *)ALLOCA(dim*sizeof(DAlg)); |
simp = (DAlg *)ALLOCA(dim*sizeof(DAlg)); |
current_spec = dp_current_spec; initd(nf->spec); |
current_spec = dp_current_spec; initd(nf->spec); |
for ( i = dim-1; i >= 0; i-- ) { |
for ( i = 0; i < dim; i++ ) { |
m = mb[i]; |
m = mb[i]; |
for ( j = i+1; j < dim; j++ ) |
for ( j = i-1; j >= 0; j-- ) |
if ( dp_redble(m,mb[j]) ) |
if ( dp_redble(m,mb[j]) ) |
break; |
break; |
if ( j < dim ) { |
if ( j >= 0 ) { |
dp_subd(m,mb[j],&d); |
dp_subd(m,mb[j],&d); |
muld(CO,d,simp[j]->nm,&u); |
muld(CO,d,simp[j]->nm,&u); |
MKDAlg(u,simp[j]->dn,t); |
MKDAlg(u,simp[j]->dn,t); |
Line 551 void invdalg(DAlg a,DAlg *c) |
|
Line 602 void invdalg(DAlg a,DAlg *c) |
|
NTOQ(ln,1,dn); |
NTOQ(ln,1,dn); |
MKMAT(mobj,dim,dim+1); |
MKMAT(mobj,dim,dim+1); |
mat = (Q **)BDY(mobj); |
mat = (Q **)BDY(mobj); |
mulq(dn,a->dn,&mat[dim-1][dim]); |
mulq(dn,a->dn,&mat[0][dim]); |
for ( j = 0; j < dim; j++ ) { |
for ( j = 0; j < dim; j++ ) { |
divq(dn,simp[j]->dn,&mul); |
divq(dn,simp[j]->dn,&mul); |
for ( i = 0, mp = BDY(simp[j]->nm); mp && i < dim; i++ ) |
for ( i = dim-1, mp = BDY(simp[j]->nm); mp && i >= 0; i-- ) |
if ( dl_equal(n,BDY(mb[i])->dl,mp->dl) ) { |
if ( dl_equal(n,BDY(mb[i])->dl,mp->dl) ) { |
mulq(mul,(Q)mp->c,&mat[i][j]); |
mulq(mul,(Q)mp->c,&mat[i][j]); |
mp = NEXT(mp); |
mp = NEXT(mp); |
} |
} |
} |
} |
get_eg(&eg0); |
get_eg(&eg0); |
generic_gauss_elim_hensel(mobj,&sol,&dnsol,&rinfo,&cinfo); |
rank = generic_gauss_elim_hensel(mobj,&sol,&dnsol,&rinfo,&cinfo); |
get_eg(&eg1); add_eg(&eg_le,&eg0,&eg1); |
get_eg(&eg1); add_eg(&eg_le,&eg0,&eg1); |
solmat = (Q **)BDY(sol); |
if ( cinfo[0] == dim ) { |
for ( i = 0, mp0 = 0; i < dim; i++ ) |
/* the input is invertible */ |
if ( solmat[i][0] ) { |
solmat = (Q **)BDY(sol); |
|
for ( i = dim-1, mp0 = 0; i >= 0; i-- ) |
|
if ( solmat[i][0] ) { |
|
NEXTMP(mp0,mp); |
|
mp->c = (P)solmat[i][0]; |
|
mp->dl = BDY(mb[i])->dl; |
|
} |
|
NEXT(mp) = 0; MKDP(n,mp0,u); |
|
mulq(dnsol,nmc,&dn1); |
|
MKDAlg(u,dn1,r); |
|
rmcontdalg(r,c); |
|
return 1; |
|
} else |
|
return 0; |
|
} |
|
|
|
NODE inv_or_split_dalg(DAlg a,DAlg *c) |
|
{ |
|
NumberField nf; |
|
int dim,n,i,j,k,l; |
|
DP *mb; |
|
DP m,d,u; |
|
N ln,gn,qn; |
|
DAlg *simp; |
|
DAlg t,a0,r; |
|
Q dn,dnsol,mul,nmc,dn1; |
|
MAT mobj,sol; |
|
Q **mat,**solmat; |
|
MP mp0,mp; |
|
int *rinfo,*cinfo; |
|
int rank,nparam; |
|
NODE nd0,nd,ndt; |
|
struct order_spec *current_spec; |
|
struct oEGT eg0,eg1; |
|
extern struct oEGT eg_le; |
|
|
|
if ( !(nf=current_numberfield) ) |
|
error("invdalg : current_numberfield is not set"); |
|
if ( !a ) |
|
error("invdalg : division by 0"); |
|
else if ( NID(a) == N_Q ) { |
|
invq((Q)a,&dn); *c = (DAlg)dn; |
|
return; |
|
} |
|
dim = nf->dim; |
|
mb = nf->mb; |
|
n = nf->n; |
|
ln = ONEN; |
|
dp_ptozp(a->nm,&u); divq((Q)BDY(a->nm)->c,(Q)BDY(u)->c,&nmc); |
|
MKDAlg(u,ONE,a0); |
|
simp = (DAlg *)ALLOCA(dim*sizeof(DAlg)); |
|
current_spec = dp_current_spec; initd(nf->spec); |
|
for ( i = 0; i < dim; i++ ) { |
|
m = mb[i]; |
|
for ( j = i-1; j >= 0; j-- ) |
|
if ( dp_redble(m,mb[j]) ) |
|
break; |
|
if ( j >= 0 ) { |
|
dp_subd(m,mb[j],&d); |
|
muld(CO,d,simp[j]->nm,&u); |
|
MKDAlg(u,simp[j]->dn,t); |
|
simpdalg(t,&simp[i]); |
|
} else { |
|
MKDAlg(m,ONE,t); |
|
muldalg(t,a0,&simp[i]); |
|
} |
|
gcdn(NM(simp[i]->dn),ln,&gn); divsn(ln,gn,&qn); |
|
muln(NM(simp[i]->dn),qn,&ln); |
|
} |
|
initd(current_spec); |
|
NTOQ(ln,1,dn); |
|
MKMAT(mobj,dim,dim+1); |
|
mat = (Q **)BDY(mobj); |
|
mulq(dn,a->dn,&mat[0][dim]); |
|
for ( j = 0; j < dim; j++ ) { |
|
divq(dn,simp[j]->dn,&mul); |
|
for ( i = dim-1, mp = BDY(simp[j]->nm); mp && i >= 0; i-- ) |
|
if ( dl_equal(n,BDY(mb[i])->dl,mp->dl) ) { |
|
mulq(mul,(Q)mp->c,&mat[i][j]); |
|
mp = NEXT(mp); |
|
} |
|
} |
|
get_eg(&eg0); |
|
rank = generic_gauss_elim_hensel(mobj,&sol,&dnsol,&rinfo,&cinfo); |
|
get_eg(&eg1); add_eg(&eg_le,&eg0,&eg1); |
|
if ( cinfo[0] == dim ) { |
|
/* the input is invertible */ |
|
solmat = (Q **)BDY(sol); |
|
for ( i = dim-1, mp0 = 0; i >= 0; i-- ) |
|
if ( solmat[i][0] ) { |
|
NEXTMP(mp0,mp); |
|
mp->c = (P)solmat[i][0]; |
|
mp->dl = BDY(mb[i])->dl; |
|
} |
|
NEXT(mp) = 0; MKDP(n,mp0,u); |
|
mulq(dnsol,nmc,&dn1); |
|
MKDAlg(u,dn1,r); |
|
rmcontdalg(r,c); |
|
return 0; |
|
} else { |
|
/* the input is not invertible */ |
|
nparam = (dim+1)-rank; |
|
/* the index 'dim' should not be in cinfo[] */ |
|
solmat = (Q **)BDY(sol); |
|
for ( k = 0; k < nparam; k++ ) |
|
if ( cinfo[k] == dim ) |
|
error("invdalg : cannot happen"); |
|
nd0 = 0; |
|
for ( k = 0; k < nparam; k++ ) { |
|
m = mb[cinfo[k]]; |
|
for ( ndt = nd0; ndt; ndt = NEXT(ndt) ) { |
|
if ( dp_redble(m,(DP)BDY(ndt)) ) break; |
|
} |
|
/* skip a redundunt basis element */ |
|
if ( ndt ) continue; |
|
/* construct a new basis element */ |
|
mp0 = 0; |
NEXTMP(mp0,mp); |
NEXTMP(mp0,mp); |
mp->c = (P)solmat[i][0]; |
chsgnq(dnsol,&dn1); mp->c = (P)dn1; |
mp->dl = BDY(mb[i])->dl; |
mp->dl = BDY(m)->dl; |
|
/* skip the last parameter */ |
|
for ( l = rank-2; l >= 0; l-- ) { |
|
if ( solmat[l][k] ) { |
|
NEXTMP(mp0,mp); |
|
mp->c = (P)solmat[l][k]; |
|
mp->dl = BDY(mb[rinfo[l]])->dl; |
|
} |
|
} |
|
NEXT(mp) = 0; MKDP(n,mp0,u); |
|
NEXTNODE(nd0,nd); |
|
BDY(nd) = (pointer)u; |
|
NEXT(nd) = 0; |
} |
} |
NEXT(mp) = 0; MKDP(n,mp0,u); |
NEXT(nd) = 0; |
mulq(dnsol,nmc,&dn1); |
return nd0; |
MKDAlg(u,dn1,r); |
} |
rmcontdalg(r,c); |
|
} |
} |
|
|
void chsgndalg(DAlg a,DAlg *c) |
void chsgndalg(DAlg a,DAlg *c) |
Line 597 void pwrdalg(DAlg a,Q e,DAlg *c) |
|
Line 775 void pwrdalg(DAlg a,Q e,DAlg *c) |
|
Q q; |
Q q; |
N en,qn; |
N en,qn; |
int r; |
int r; |
|
int ret; |
|
|
if ( !(nf=current_numberfield) ) |
if ( !(nf=current_numberfield) ) |
error("pwrdalg : current_numberfield is not set"); |
error("pwrdalg : current_numberfield is not set"); |
Line 610 void pwrdalg(DAlg a,Q e,DAlg *c) |
|
Line 789 void pwrdalg(DAlg a,Q e,DAlg *c) |
|
*c = a; |
*c = a; |
else { |
else { |
if ( SGN(e) < 0 ) { |
if ( SGN(e) < 0 ) { |
invdalg(a,&t); a = t; |
ret = invdalg(a,&t); |
|
if ( !ret ) |
|
error("pwrdalg : the denominator is not invertible"); |
|
a = t; |
} |
} |
en = NM(e); |
en = NM(e); |
y = nf->one; |
y = nf->one; |
Line 638 int cmpdalg(DAlg a,DAlg b) |
|
Line 820 int cmpdalg(DAlg a,DAlg b) |
|
else |
else |
return SGN((Q)BDY(c->nm)->c); |
return SGN((Q)BDY(c->nm)->c); |
} |
} |
|
|
|
/* convert da to a univariate poly; return the position of variable */ |
|
|
|
int dalgtoup(DAlg da,P *up,Q *dn) |
|
{ |
|
int nv,i,hi,current_d; |
|
DCP dc0,dc; |
|
MP h,mp0,mp,t; |
|
DL hd,d; |
|
DP c; |
|
DAlg cc; |
|
P v; |
|
|
|
nv = da->nm->nv; |
|
h = BDY(da->nm); |
|
*dn = da->dn; |
|
hd = h->dl; |
|
for ( i = 0; i < nv; i++ ) |
|
if ( hd->d[i] ) break; |
|
hi = i; |
|
current_d = hd->d[i]; |
|
dc0 = 0; |
|
mp0 = 0; |
|
for ( t = h; t; t = NEXT(t) ) { |
|
NEWDL(d,nv); |
|
for ( i = 0; i <= hi; i++ ) d->d[i] = 0; |
|
for ( ; i < nv; i++ ) d->d[i] = t->dl->d[i]; |
|
d->td = t->dl->td - t->dl->d[hi]; |
|
if ( t->dl->d[hi] != current_d ) { |
|
NEXT(mp) = 0; MKDP(nv,mp0,c); MKDAlg(c,ONE,cc); |
|
NEXTDC(dc0,dc); STOQ(current_d,DEG(dc)); COEF(dc) = (P)cc; |
|
current_d = t->dl->d[hi]; |
|
mp0 = 0; |
|
} |
|
NEXTMP(mp0,mp); |
|
mp->c = t->c; mp->dl = d; |
|
} |
|
NEXT(mp) = 0; MKDP(nv,mp0,c); MKDAlg(c,ONE,cc); |
|
NEXTDC(dc0,dc); STOQ(current_d,DEG(dc)); COEF(dc) = (P)cc; |
|
NEXT(dc) = 0; |
|
makevar("x",&v); |
|
MKP(VR(v),dc0,*up); |
|
return hi; |
|
} |
|
|