=================================================================== RCS file: /home/cvs/OpenXM/src/hgm/mh/src/jack-n.c,v retrieving revision 1.3 retrieving revision 1.25 diff -u -p -r1.3 -r1.25 --- OpenXM/src/hgm/mh/src/jack-n.c 2013/02/21 07:30:56 1.3 +++ OpenXM/src/hgm/mh/src/jack-n.c 2014/03/16 03:11:07 1.25 @@ -5,25 +5,29 @@ #include #include "sfile.h" /* -$OpenXM: OpenXM/src/hgm/mh/src/jack-n.c,v 1.2 2013/02/20 07:53:44 takayama Exp $ -Ref: copied from this11/misc-2011/A1/wishart/Prog -jack-n.c, translated from mh.rr or tk_jack.rr in the asir-contrib. License: LGPL -Koev-Edelman for higher order derivatives. -cf. 20-my-note-mh-jack-n.pdf, /Movies/oxvh/priv-j1/2011-12-14-ke-mh-jack.mov -Todo: -1. Cash the transposes of partitions. -2. Use the recurrence to obtain beta(). -3. Easier input data file format for mh-n.c -Changelog: -2012.02.21, porting to OpenXM/src/hgm -2011.12.22, --table option, which is experimental. -2011.12.19, bug fix. jjk() should return double. It can become more than max int. -2011.12.15, mh.r --> jack-n.c + $OpenXM: OpenXM/src/hgm/mh/src/jack-n.c,v 1.24 2014/03/16 00:00:46 takayama Exp $ + Ref: copied from this11/misc-2011/A1/wishart/Prog + jack-n.c, translated from mh.rr or tk_jack.rr in the asir-contrib. License: LGPL + Koev-Edelman for higher order derivatives. + cf. 20-my-note-mh-jack-n.pdf, /Movies/oxvh/priv-j1/2011-12-14-ke-mh-jack.mov + Todo: + 1. Cash the transposes of partitions. + 2. Use the recurrence to obtain beta(). + 3. Easier input data file format for mh-n.c + Changelog: + 2014.03.15 http://fe.math.kobe-u.ac.jp/Movies/oxvh/2014-03-11-jack-n-c-automatic see also hgm/doc/ref.html, @s/2014/03/15-my-note-jack-automatic-order-F_A-casta.pdf. + 2014.03.14, --automatic option. Output estimation data. + 2012.02.21, porting to OpenXM/src/hgm + 2011.12.22, --table option, which is experimental. + 2011.12.19, bug fix. jjk() should return double. It can become more than max int. + 2011.12.15, mh.r --> jack-n.c */ /****** from mh-n.c *****/ static int JK_byFile=1; +static int JK_deallocate=0; #define M_n_default 3 +#define Sample_default 1 static int M_n=0; /* global variables. They are set in setParam() */ static int Mg; /* n */ @@ -36,7 +40,7 @@ static double Ef; /* exponential factor at beta*x0 * static double Hg; /* step size of rk defined in rk.c */ static int Dp; /* Data sampling period */ static double Xng=0.0; /* the last point */ -static int Sample = 1; +static int Sample = Sample_default; /* for sample inputs */ static double *Iv2; @@ -54,7 +58,7 @@ static double Ef2; #define A_LEN 1 /* (a_1) , (a_1, ..., a_p)*/ #define B_LEN 1 /* (b_1) */ static int Debug = 0; -static int Alpha = 2;; +static int Alpha = 2; /* 2 implies the zonal polynomial */ static int *Darray = NULL; static int **Parray = NULL; /* array of partitions of size M_n */ static int *ParraySize = NULL; /* length of each partitions */ @@ -91,10 +95,69 @@ static double Xarray[M_nmx][M_m_MAX]; static double *M_qk=NULL; /* saves pochhammerb */ static double M_rel_error=0.0; /* relative errors */ +/* For automatic degree and X0g setting. */ +/* + If automatic == 1, then the series is reevaluated as long as t_success!=1 + by increasing X0g (evaluation point) and M_m (approx degree); + */ +static int M_automatic=0; +/* Estimated degree bound for series expansion. See mh_t */ +static int M_m_estimated_approx_deg=0; +/* Let F(i) be the approximation up to degree i. + The i-th series_error is defined + by |(F(i)-F(i-1))/F(i-1)|. +*/ +static double M_series_error; +/* + M_series_error < M_assigend_series_error (A) is required for the + estimated_approx_deg. + */ +static double M_assigned_series_error=0.00001; +/* + Let Ef be the exponential factor ( Ef=(4)/1F1 of [HNTT] ) + If F(M_m)*Ef < x0value_min (B), the success=0 and X0g is increased. + Note that minimal double is about 2e-308 + */ +static double M_x0value_min=1e-30; +/* + estimated_X0g is the suggested value of X0g. + */ +static double M_estimated_X0g=0.0; +/* + X0g should be less than M_X0g_bound. + */ +static double M_X0g_bound = 1e+100; +/* + success is set to 1 when (A) and (B) are satisfied. + */ +static int M_mh_t_success=1; +/* + recommended_abserr is the recommended value of the absolute error + for the Runge-Kutta method. It is defined as + assigend_series_error(standing for significant digits)*Ig[0](initial value) + */ +static double M_recommended_abserr; +/* + max of beta(i)*x/2 + */ +static double M_beta_i_x_o2_max; +/* + minimum of |beta_i-beta_j| + */ +static double M_beta_i_beta_j_min; +/* + Value of matrix hg +*/ +static double M_mh_t_value; +/* + Show the process of updating degree. + */ +int M_show_autosteps=1; + /* prototypes */ static void *mymalloc(int size); -static myfree(void *p); -static myerror(char *s); +static int myfree(void *p); +static int myerror(char *s); static double jack1(int K); static double jack1diff(int k); static double xval(int i,int p); /* x_i^p */ @@ -102,7 +165,7 @@ static int mysum(int L[]); static int plength(int P[]); static int plength_t(int P[]); static void ptrans(int P[M_nmx],int Pt[]); -static test_ptrans(); +static int test_ptrans(); static int huk(int K[],int I,int J); static int hdk(int K[],int I,int J); static double jjk(int K[]); @@ -112,9 +175,9 @@ static double mypower(double x,int n); static double qk(int K[],double A[A_LEN],double B[B_LEN]); static int bb(int N[],int K[],int M[],int I,int J); static double beta(int K[],int M[]); -static printp(int kappa[]); -static printp2(int kappa[]); -static test_beta(); +static int printp(int kappa[]); +static int printp2(int kappa[]); +static int test_beta(); static double q3_10(int K[],int M[],int SK); static double q3_5(double A[],double B[],int K[],int I); static void mtest4(); @@ -130,26 +193,26 @@ static int pListHS2(int From,int To,int Kap[]); static void hsExec_0(); static int pmn(int M,int N); static int *cloneP(int a[]); -static copyP(int p[],int a[]); +static int copyP(int p[],int a[]); static void pExec_darray(void); -static genDarray2(int M,int N); -static isHStrip(int Kap[],int Nu[]); +static int genDarray2(int M,int N); +static int isHStrip(int Kap[],int Nu[]); static void hsExec_beta(void); -static genBeta(int Kap[]); -static checkBeta1(); +static int genBeta(int Kap[]); +static int checkBeta1(); static int psublen(int Kap[],int Mu[]); -static genJack(int M,int N); -static checkJack1(int M,int N); -static checkJack2(int M,int N); -static mtest1b(); +static int genJack(int M,int N); +static int checkJack1(int M,int N); +static int checkJack2(int M,int N); +static int mtest1b(); static int imypower(int x,int n); -static usage(); -static setParamDefault(); -static next(struct SFILE *fp,char *s,char *msg); +static int usage(); +static int setParamDefault(); +static int next(struct SFILE *fp,char *s,char *msg); static int gopen_file(void); static int setParam(char *fname); -static int showParam(struct SFILE *fp); +static int showParam(struct SFILE *fp,int fd); static double iv_factor(void); static double gammam(double a,int n); static double mypower(double a,int n); @@ -157,30 +220,38 @@ static double mypower(double a,int n); double mh_t(double A[A_LEN],double B[B_LEN],int N,int M); double mh_t2(int J); struct MH_RESULT *jk_main(int argc,char *argv[]); +struct MH_RESULT *jk_main2(int argc,char *argv[],int automode,double newX0g,int newDegree); int jk_freeWorkArea(); int jk_initializeWorkArea(); int jk_freeWorkArea() { /* bug, p in the cloneP will not be deallocated. - Nk in genDarray2 will not be deallocated. - */ + Nk in genDarray2 will not be deallocated. + */ int i; + JK_deallocate=1; if (Darray) {myfree(Darray); Darray=NULL;} if (Parray) {myfree(Parray); Parray=NULL;} if (ParraySize) {myfree(ParraySize); ParraySize=NULL;} if (M_beta_0) {myfree(M_beta_0); M_beta_0=NULL;} if (M_beta_1) {myfree(M_beta_1); M_beta_1=NULL;} if (M_jack) { - for (i=0; M_jack[i] != NULL; i++) { - myfree(M_jack[i]); M_jack[i] = NULL; - } - myfree(M_jack); M_jack=NULL; + for (i=0; M_jack[i] != NULL; i++) { + if (Debug) printf("Free M_jack[%d]\n",i); + myfree(M_jack[i]); M_jack[i] = NULL; + } + myfree(M_jack); M_jack=NULL; } if (M_qk) {myfree(M_qk); M_qk=NULL;} if (P_pki) {myfree(P_pki); P_pki=NULL;} + JK_deallocate=0; + return(0); } int jk_initializeWorkArea() { int i,j; + JK_deallocate=1; + xval(0,0); + JK_deallocate=0; Darray=NULL; Parray=NULL; ParraySize=NULL; @@ -191,6 +262,7 @@ int jk_initializeWorkArea() { for (i=0; i 3 + (3,2,2,0,0) --> 3 */ static int plength(int P[]) { int I; @@ -298,7 +378,7 @@ static int plength_t(int P[]) { } /* - ptrans(P) returns Pt + ptrans(P) returns Pt */ static void ptrans(int P[M_nmx],int Pt[]) { /* Pt[M_m] */ extern int M_m; @@ -313,7 +393,7 @@ static void ptrans(int P[M_nmx],int Pt[]) { /* Pt[M_m] } } -static test_ptrans() { +static int test_ptrans() { extern int M_m; int p[M_n0]={5,3,2}; int pt[10]; @@ -321,6 +401,7 @@ static test_ptrans() { M_m = 10; ptrans(p,pt); if (Debug) {for (i=0; i<10; i++) printf("%d,",pt[i]); printf("\n");} + return(0); } @@ -413,7 +494,7 @@ static double mypower(double x,int n) { return(v); } /* Q_kappa -*/ + */ static double qk(int K[],double A[A_LEN],double B[B_LEN]) { extern int Alpha; int P,Q,I; @@ -433,8 +514,8 @@ static double qk(int K[],double A[A_LEN],double B[B_LE } /* - B^nu_{kappa,mu}(i,j) - bb(N,K,M,I,J) + B^nu_{kappa,mu}(i,j) + bb(N,K,M,I,J) */ static int bb(int N[],int K[],int M[],int I,int J) { int Kp[M_m_MAX]; int Mp[M_m_MAX]; @@ -442,8 +523,8 @@ static int bb(int N[],int K[],int M[],int I,int J) { ptrans(M,Mp); /* - printp(K); printf("K<--, "); printp2(Kp); printf("<--Kp\n"); - printp(M); printf("M<--, "); printp2(Mp); printf("<--Mp\n"); + printp(K); printf("K<--, "); printp2(Kp); printf("<--Kp\n"); + printp(M); printf("M<--, "); printp2(Mp); printf("<--Mp\n"); */ if ((plength_t(Kp) < J) || (plength_t(Mp) < J)) return(hdk(N,I,J)); @@ -479,15 +560,16 @@ static double beta(int K[],int M[]) { return(V); } -static printp(int kappa[]) { +static int printp(int kappa[]) { int i; printf("("); for (i=0; i= M_n) { - if (Debug) {fprintf(stderr,"Ksize >= M_n\n");} - continue; - } - for (i=0; i= M_n) { + if (Debug) {fprintf(stderr,"Ksize >= M_n\n");} + continue; + } + for (i=0; i N1) return(0); @@ -920,40 +1007,40 @@ static void hsExec_beta(void) { Done=0; for (J=M_beta_pt-1; J>=0; J--) { if (M_beta_1[J] == Nnu) { - K=I+1; - if (Debug) { - printf("Found at J=%d, K=%d, q3_10(Kap,Nu,K)=%lf,Nu,Mu= \n", - J,K,q3_10(M_beta_kap,Nu,K)); - printp(Nu); printf("\n"); - printp(Mu); printf("\n"); - } - /* Check other conditions. See Numata's mail on Dec 24, 2011. */ - rrMax = Nu[I]-1; - if ((plength_t(Kapt) < rrMax) || (plength_t(Nut) < rrMax)) { - if (Debug) printf(" is not taken (length). \n"); - break; - } - OK=1; - for (RR=0; RR= 2^I) aM_jack(I,J,K) = 0; - else aM_jack(I,J,K) = NAN; + else aM_jack(I,J,K) = NAN; } } } @@ -1114,43 +1203,46 @@ static genJack(int M,int N) { for (H=0; H 0. */ for (J=1; J0) partial_sum[i] += partial_sum[i-1]; + serror = myabs((partial_sum[i]-partial_sum[i-1])/partial_sum[i-1]); + if ((i>0)&&(M_m_estimated_approx_deg < 0)&&(serror M_m) M_mh_t_success=0; + + M_series_error = serror; + M_recommended_abserr = iv*M_assigned_series_error; + + if (M_show_autosteps) { + printf("%%%%serror=%lg, M_assigned_series_error=%lg, M_m_estimated_approx_deg=%d,M_m=%d\n",serror,M_assigned_series_error,M_m_estimated_approx_deg,M_m); + printf("%%%%x0value_min=%lg, x0g_bound=%lg\n",M_x0value_min, M_X0g_bound); + printf("%%%%F=%lg,Ef=%lg,M_estimated_X0g=%lg, X0g=%lg\n",F,iv_factor(),M_estimated_X0g,X0g); + fprintf(stderr,"%%%%(stderr) serror=%lg, M_assigned_series_error=%lg, M_m_estimated_approx_deg=%d,M_m=%d\n",serror,M_assigned_series_error,M_m_estimated_approx_deg,M_m); + fprintf(stderr,"%%%%(stderr) x0value_min=%lg, x0g_bound=%lg\n",M_x0value_min, M_X0g_bound); + fprintf(stderr,"%%%%(stderr) F=%lg,Ef=%lg,M_estimated_X0g=%lg, X0g=%lg\n",F,iv_factor(),M_estimated_X0g,X0g); + } + + M_mh_t_value=F; return(F); } double mh_t2(int J) { @@ -1255,12 +1403,12 @@ double mh_t2(int J) { F = 0; Pmn = P_pmn; for (K=0; Kmessage = NULL; + ans->t_success = 0; + ans->series_error = 1.0e+10; + ans->recommended_abserr = 1.0e-10; + } else ans = NULL; /* Output by a file=stdout */ ofp = mh_fopen("stdout","w",JK_byFile); @@ -1365,69 +1555,101 @@ struct MH_RESULT *jk_main(int argc,char *argv[]) { sprintf(swork,"%%%%Use --help option to see the help.\n"); mh_fputs(swork,ofp); sprintf(swork,"%%%%Mapprox=%d\n",Mapprox); mh_fputs(swork,ofp); if (M_n != Mg) { - myerror("Mg must be equal to M_n\n"); mh_exit(-1); + myerror("Mg must be equal to M_n\n"); mh_exit(-1); } - if (Debug) showParam(ofp); + if (Debug) showParam(NULL,1); for (i=0; i M_beta_i_x_o2_max) M_beta_i_x_o2_max = myabs(M_x[i]/2); + for (j=i+1; jx = X0g; - ans->rank = imypower(2,Mg); - ans->y = (double *)mymalloc(sizeof(double)*(ans->rank)); - for (i=0; irank; i++) (ans->y)[i] = Iv[i]; - ans->size=1; - ans->sfpp = (struct SFILE **)mymalloc(sizeof(struct SFILE *)*(ans->size)); - (ans->sfpp)[0] = ofp; + if (!JK_byFile) { + ans->x = X0g; + ans->rank = imypower(2,Mg); + ans->y = (double *)mymalloc(sizeof(double)*(ans->rank)); + for (i=0; irank; i++) (ans->y)[i] = Iv[i]; + ans->size=1; + ans->sfpp = (struct SFILE **)mymalloc(sizeof(struct SFILE *)*(ans->size)); + (ans->sfpp)[0] = ofp; + + ans->t_success = M_mh_t_success; + ans->series_error = M_series_error; + ans->recommended_abserr = M_recommended_abserr; } + if (Debug) printf("jk_freeWorkArea() starts\n"); jk_freeWorkArea(); + if (Debug) printf("jk_freeWorkArea() has finished.\n"); return(ans); } -static usage() { +static int usage() { fprintf(stderr,"Usages:\n"); - fprintf(stderr,"mh-m [--idata input_data_file --x0 x0 --degree approxm]\n"); - fprintf(stderr,"\nThe command mh-m [options] generates an input for w-m, Pr({y | ytest2.txt\n"); - fprintf(stderr," ./w-3 --idata test2.txt --gnuplotf test-g\n"); + fprintf(stderr,"[1] ./hgm_jack-n \n"); + fprintf(stderr,"[2] ./hgm_jack-n --x0 0.1 \n"); + fprintf(stderr,"[3] ./hgm_jack-n --x0 0.1 --degree 15 \n"); + fprintf(stderr,"[4] ./hgm_jack-n --idata Testdata/tmp-idata3.txt --degree 15 \n"); + fprintf(stderr,"[5] ./hgm_jack-n --degree 15 >test2.txt\n"); + fprintf(stderr," ./hgm_w-n --idata test2.txt --gnuplotf test-g\n"); fprintf(stderr," gnuplot -persist s); - mh_exit(-1); + if (JK_byFile) fprintf(stderr,"File %s is not found.\n",fp->s); + mh_exit(-1); } next(fp,s,"Mg(m)"); sscanf(s,"%d",&Mg); @@ -1481,24 +1715,29 @@ static setParam(char *fname) { Beta = (double *)mymalloc(sizeof(double)*Mg); for (i=0; i