=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/engine/PU.c,v retrieving revision 1.1 retrieving revision 1.14 diff -u -p -r1.1 -r1.14 --- OpenXM_contrib2/asir2000/engine/PU.c 1999/12/03 07:39:08 1.1 +++ OpenXM_contrib2/asir2000/engine/PU.c 2010/01/31 03:25:54 1.14 @@ -1,10 +1,55 @@ -/* $OpenXM: OpenXM/src/asir99/engine/PU.c,v 1.1.1.1 1999/11/10 08:12:26 noro Exp $ */ +/* + * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED + * All rights reserved. + * + * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited, + * non-exclusive and royalty-free license to use, copy, modify and + * redistribute, solely for non-commercial and non-profit purposes, the + * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and + * conditions of this Agreement. For the avoidance of doubt, you acquire + * only a limited right to use the SOFTWARE hereunder, and FLL or any + * third party developer retains all rights, including but not limited to + * copyrights, in and to the SOFTWARE. + * + * (1) FLL does not grant you a license in any way for commercial + * purposes. You may use the SOFTWARE only for non-commercial and + * non-profit purposes only, such as academic, research and internal + * business use. + * (2) The SOFTWARE is protected by the Copyright Law of Japan and + * international copyright treaties. If you make copies of the SOFTWARE, + * with or without modification, as permitted hereunder, you shall affix + * to all such copies of the SOFTWARE the above copyright notice. + * (3) An explicit reference to this SOFTWARE and its copyright owner + * shall be made on your publication or presentation in any form of the + * results obtained by use of the SOFTWARE. + * (4) In the event that you modify the SOFTWARE, you shall notify FLL by + * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification + * for such modification or the source code of the modified part of the + * SOFTWARE. + * + * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL + * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND + * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES' + * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY + * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY. + * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT, + * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL + * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES + * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES + * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY + * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF + * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART + * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY + * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, + * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. + * + * $OpenXM: OpenXM_contrib2/asir2000/engine/PU.c,v 1.13 2010/01/28 08:56:26 noro Exp $ +*/ #include "ca.h" -void reorderp(nvl,ovl,p,pr) -VL nvl,ovl; -P p; -P *pr; +void reorderp(VL nvl,VL ovl,P p,P *pr) { DCP dc; P x,m,s,t,c; @@ -28,11 +73,7 @@ P *pr; } } -void substp(vl,p,v0,p0,pr) -VL vl; -V v0; -P p,p0; -P *pr; +void substp(VL vl,P p,V v0,P p0,P *pr) { P x,t,m,c,s,a; DCP dc; @@ -73,13 +114,60 @@ P *pr; } } -void detp(vl,rmat,n,dp) -VL vl; -P **rmat; -int n; -P *dp; +void substpp(VL vl,P p,V *vvect,P *svect,int nv,P *pr); + +void substpp(VL vl,P p,V *vvect,P *svect,int nv,P *pr) { - int i,j,k,sgn; + P x,t,m,c,s,a,p0,c1; + DCP dc; + Q d; + V v; + int i; + + if ( !p ) + *pr = 0; + else if ( NUM(p) ) + *pr = p; + else { + v = VR(p); + for ( i = 0; i < nv; i++ ) if ( vvect[i] == v ) break; + if ( svect[i] && OID(svect[i]) < 0 ) { + MKV(VR(p),x); + for ( c = 0, dc = DC(p); dc; dc = NEXT(dc) ) { + substpp(vl,COEF(dc),vvect,svect,nv,&t); + if ( DEG(dc) ) { + pwrp(vl,x,DEG(dc),&s); mulp(vl,s,t,&m); + addp(vl,m,c,&a); + c = a; + } else { + addp(vl,t,c,&a); + c = a; + } + } + *pr = c; + } else { + p0 = svect[i]; + dc = DC(p); + substpp(vl,COEF(dc),vvect,svect,nv,&c); + for ( d = DEG(dc), dc = NEXT(dc); + dc; d = DEG(dc), dc = NEXT(dc) ) { + subq(d,DEG(dc),(Q *)&t); pwrp(vl,p0,(Q)t,&s); + mulp(vl,s,c,&m); + substpp(vl,COEF(dc),vvect,svect,nv,&c1); + addp(vl,m,c1,&c); + } + if ( d ) { + pwrp(vl,p0,d,&t); mulp(vl,t,c,&m); + c = m; + } + *pr = c; + } + } +} + +void detp(VL vl,P **rmat,int n,P *dp) +{ + int i,j,k,l,sgn,nmin,kmin,lmin,ntmp; P mjj,mij,t,s,u,d; P **mat; P *mi,*mj; @@ -93,12 +181,22 @@ P *dp; if ( i == n ) { *dp = 0; return; } - for ( k = i; k < n; k++ ) - if ( mat[k][j] && (nmonop(mat[k][j]) < nmonop(mat[i][j]) ) ) - i = k; - if ( j != i ) { - mj = mat[j]; mat[j] = mat[i]; mat[i] = mj; sgn = -sgn; + nmin = nmonop(mat[i][j]); + kmin=i; lmin=j; + for ( k = j; k < n; k++ ) + for ( l = j; l < n; l++ ) + if ( mat[k][l] && ((ntmp=nmonop(mat[k][l])) < nmin) ) { + kmin = k; lmin = l; nmin = ntmp; + } + if ( kmin != j ) { + mj = mat[j]; mat[j] = mat[kmin]; mat[kmin] = mj; sgn = -sgn; } + if ( lmin != j ) { + for ( k = j; k < n; k++ ) { + t = mat[k][j]; mat[k][j] = mat[k][lmin]; mat[k][lmin] = t; + } + sgn = -sgn; + } for ( i = j + 1, mj = mat[j], mjj = mj[j]; i < n; i++ ) for ( k = j + 1, mi = mat[i], mij = mi[j]; k < n; k++ ) { mulp(vl,mi[k],mjj,&t); mulp(vl,mj[k],mij,&s); @@ -112,11 +210,61 @@ P *dp; *dp = d; } -void reordvar(vl,v,nvlp) -VL vl; -V v; -VL *nvlp; +void invmatp(VL vl,P **rmat,int n,P ***imatp,P *dnp) { + int i,j,k,l,n2; + P mjj,mij,t,s,u,d; + P **mat,**imat; + P *mi,*mj,*w; + + n2 = n<<1; + mat = (P **)almat_pointer(n,n2); + for ( i = 0; i < n; i++ ) { + for ( j = 0; j < n; j++ ) + mat[i][j] = rmat[i][j]; + mat[i][i+n] = (P)ONE; + } + for ( j = 0, d = (P)ONE; j < n; j++ ) { + for ( i = j; (i < n) && !mat[i][j]; i++ ); + if ( i == n ) { + error("invmatp : input is singular"); + } + for ( k = i; k < n; k++ ) + if ( mat[k][j] && (nmonop(mat[k][j]) < nmonop(mat[i][j]) ) ) + i = k; + if ( j != i ) { + mj = mat[j]; mat[j] = mat[i]; mat[i] = mj; + } + for ( i = j + 1, mj = mat[j], mjj = mj[j]; i < n; i++ ) + for ( k = j + 1, mi = mat[i], mij = mi[j]; k < n2; k++ ) { + mulp(vl,mi[k],mjj,&t); mulp(vl,mj[k],mij,&s); + subp(vl,t,s,&u); divsp(vl,u,d,&mi[k]); + } + d = mjj; + } + /* backward substitution */ + w = (P *)ALLOCA(n2*sizeof(P)); + for ( i = n-2; i >= 0; i-- ) { + bzero(w,n2*sizeof(P)); + for ( k = i+1; k < n; k++ ) + for ( l = k, u = mat[i][l]; l < n2; l++ ) { + mulp(vl,mat[k][l],u,&t); addp(vl,w[l],t,&s); w[l] = s; + } + for ( j = i, u = mat[i][j]; j < n2; j++ ) { + mulp(vl,mat[i][j],d,&t); subp(vl,t,w[j],&s); + divsp(vl,s,u,&mat[i][j]); + } + } + imat = (P **)almat_pointer(n,n); + for ( i = 0; i < n; i++ ) + for ( j = 0; j < n; j++ ) + imat[i][j] = mat[i][j+n]; + *imatp = imat; + *dnp = d; +} + +void reordvar(VL vl,V v,VL *nvlp) +{ VL nvl,nvl0; for ( NEWVL(nvl0), nvl0->v = v, nvl = nvl0; @@ -132,9 +280,7 @@ VL *nvlp; *nvlp = nvl0; } -void gcdprsp(vl,p1,p2,pr) -VL vl; -P p1,p2,*pr; +void gcdprsp(VL vl,P p1,P p2,P *pr) { P g1,g2,gc1,gc2,gp1,gp2,g,gc,gp,gcr; V v1,v2; @@ -172,9 +318,7 @@ P p1,p2,*pr; } } -void gcdcp(vl,p,pr) -VL vl; -P p,*pr; +void gcdcp(VL vl,P p,P *pr) { P g,g1; DCP dc; @@ -192,10 +336,7 @@ P p,*pr; } } -void sprs(vl,v,p1,p2,pr) -VL vl; -V v; -P p1,p2,*pr; +void sprs(VL vl,V v,P p1,P p2,P *pr) { P q1,q2,m,m1,m2,x,h,r,g1,g2; int d; @@ -233,10 +374,7 @@ P p1,p2,*pr; *pr = g2; } -void resultp(vl,v,p1,p2,pr) -VL vl; -V v; -P p1,p2,*pr; +void resultp(VL vl,V v,P p1,P p2,P *pr) { P q1,q2,m,m1,m2,lc,q,r,t,g1,g2,adj; int d,d1,d2,j,k; @@ -328,10 +466,7 @@ P p1,p2,*pr; } } -void srch2(vl,v,p1,p2,pr) -VL vl; -V v; -P p1,p2,*pr; +void srch2(VL vl,V v,P p1,P p2,P *pr) { P q1,q2,m,m1,m2,lc,q,r,t,s,g1,g2,adj; int d,d1,d2,j,k; @@ -420,10 +555,7 @@ P p1,p2,*pr; } } -void srcr(vl,v,p1,p2,pr) -VL vl; -V v; -P p1,p2,*pr; +void srcr(VL vl,V v,P p1,P p2,P *pr) { P q1,q2,c,c1; P tg,tg1,tg2,resg; @@ -452,9 +584,7 @@ P p1,p2,*pr; factorial(QTOS(n)+QTOS(m),&t); mulq(u,t,&s); addq(s,s,&f); for ( index = 0, q = ONE, c = 0; cmpq(f,q) >= 0; ) { - mod = lprime[index++]; - if ( !mod ) - error("sqfrum : lprime[] exhausted."); + mod = get_lprime(index++); ptomp(mod,LC(q1),&tg); if ( !tg ) continue; @@ -469,10 +599,7 @@ P p1,p2,*pr; *pr = c; } -void res_ch_det(vl,v,p1,p2,pr) -VL vl; -V v; -P p1,p2,*pr; +void res_ch_det(VL vl,V v,P p1,P p2,P *pr) { P q1,q2,c,c1; P tg,tg1,tg2,resg; @@ -501,9 +628,7 @@ P p1,p2,*pr; factorial(QTOS(n)+QTOS(m),&t); mulq(u,t,&s); addq(s,s,&f); for ( index = 0, q = ONE, c = 0; cmpq(f,q) >= 0; ) { - mod = lprime[index++]; - if ( !mod ) - error("sqfrum : lprime[] exhausted."); + mod = get_lprime(index++); ptomp(mod,LC(q1),&tg); if ( !tg ) continue; @@ -518,12 +643,7 @@ P p1,p2,*pr; *pr = c; } -void res_detmp(vl,mod,v,p1,p2,dp) -VL vl; -int mod; -V v; -P p1,p2; -P *dp; +void res_detmp(VL vl,int mod,V v,P p1,P p2,P *dp) { int n1,n2,n,sgn; int i,j,k; @@ -569,36 +689,29 @@ P *dp; } #if 0 -showmat(vl,mat,n) -VL vl; -P **mat; -int n; +showmat(VL vl,P **mat,int n) { int i,j; P t; for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { - mptop(mat[i][j],&t); printp(vl,t); fprintf(out," "); + mptop(mat[i][j],&t); asir_printp(vl,t); fprintf(out," "); } fprintf(out,"\n"); } fflush(out); } -showmp(vl,p) -VL vl; -P p; +showmp(VL vl,P p) { P t; - mptop(p,&t); printp(vl,t); fprintf(out,"\n"); + mptop(p,&t); asir_printp(vl,t); fprintf(out,"\n"); } #endif -void premp(vl,p1,p2,pr) -VL vl; -P p1,p2,*pr; +void premp(VL vl,P p1,P p2,P *pr) { P m,m1,m2; P *pw; @@ -651,18 +764,17 @@ P p1,p2,*pr; } } -void ptozp0(p,pr) -P p; -P *pr; +void ptozp0(P p,P *pr) { Q c; - ptozp(p,1,&c,pr); + if ( qpcheck((Obj)p) ) + ptozp(p,1,&c,pr); + else + *pr = p; } -void mindegp(vl,p,mvlp,pr) -VL vl,*mvlp; -P p,*pr; +void mindegp(VL vl,P p,VL *mvlp,P *pr) { P t; VL nvl,tvl,avl; @@ -684,9 +796,7 @@ P p,*pr; } } -void maxdegp(vl,p,mvlp,pr) -VL vl,*mvlp; -P p,*pr; +void maxdegp(VL vl,P p,VL *mvlp,P *pr) { P t; VL nvl,tvl,avl; @@ -708,9 +818,7 @@ P p,*pr; } } -void min_common_vars_in_coefp(vl,p,mvlp,pr) -VL vl,*mvlp; -P p,*pr; +void min_common_vars_in_coefp(VL vl,P p,VL *mvlp,P *pr) { P u,p0; VL tvl,cvl,svl,uvl,avl,vl0; @@ -742,9 +850,7 @@ P p,*pr; *pr = p0; *mvlp = vl0; } -void minlcdegp(vl,p,mvlp,pr) -VL vl,*mvlp; -P p,*pr; +void minlcdegp(VL vl,P p,VL *mvlp,P *pr) { P u,p0; VL tvl,uvl,avl,vl0; @@ -762,9 +868,7 @@ P p,*pr; *pr = p0; *mvlp = vl0; } -void sort_by_deg(n,p,pr) -int n; -P *p,*pr; +void sort_by_deg(int n,P *p,P *pr) { int j,k,d,k0; V v; @@ -782,9 +886,7 @@ P *p,*pr; } } -void sort_by_deg_rev(n,p,pr) -int n; -P *p,*pr; +void sort_by_deg_rev(int n,P *p,P *pr) { int j,k,d,k0; V v; @@ -803,10 +905,7 @@ P *p,*pr; } -void getmindeg(v,p,dp) -V v; -P p; -Q *dp; +void getmindeg(V v,P p,Q *dp) { Q dt,d; DCP dc; @@ -828,9 +927,7 @@ Q *dp; } } -void minchdegp(vl,p,mvlp,pr) -VL vl,*mvlp; -P p,*pr; +void minchdegp(VL vl,P p,VL *mvlp,P *pr) { P t; VL tvl,nvl,avl; @@ -859,9 +956,7 @@ P p,*pr; } } -int getchomdeg(v,p) -V v; -P p; +int getchomdeg(V v,P p) { int m,m1; DCP dc; @@ -879,10 +974,7 @@ P p; } } -int getlchomdeg(v,p,d) -V v; -P p; -int *d; +int getlchomdeg(V v,P p,int *d) { int m0,m1,d0,d1; DCP dc; @@ -907,8 +999,7 @@ int *d; } } -int nmonop(p) -P p; +int nmonop(P p) { int s; DCP dc; @@ -928,8 +1019,7 @@ P p; } } -int qpcheck(p) -Obj p; +int qpcheck(Obj p) { DCP dc; @@ -951,8 +1041,7 @@ Obj p; /* check if p is univariate and all coeffs are INT or LM */ -int uzpcheck(p) -Obj p; +int uzpcheck(Obj p) { DCP dc; P c; @@ -966,7 +1055,7 @@ Obj p; case O_P: for ( dc = DC((P)p); dc; dc = NEXT(dc) ) { c = COEF(dc); - if ( !NUM(c) || !uzpcheck(c) ) + if ( !NUM(c) || !uzpcheck((Obj)c) ) return 0; } return 1; @@ -975,8 +1064,7 @@ Obj p; } } -int p_mag(p) -P p; +int p_mag(P p) { int s; DCP dc; @@ -988,6 +1076,24 @@ P p; else { for ( dc = DC(p), s = 0; dc; dc = NEXT(dc) ) s += p_mag(COEF(dc)); + return s; + } +} + +int maxblenp(P p) +{ + int s,t; + DCP dc; + + if ( !p ) + return 0; + else if ( OID(p) == O_N ) + return n_bits(NM((Q)p))+(INT((Q)p)?0:n_bits(DN((Q)p))); + else { + for ( dc = DC(p), s = 0; dc; dc = NEXT(dc) ) { + t = maxblenp(COEF(dc)); + s = MAX(t,s); + } return s; } }