version 1.1, 2013/02/19 03:06:19 |
version 1.2, 2013/02/19 08:03:14 |
|
|
/* |
/* |
|
$OpenXM$ |
License: LGPL |
License: LGPL |
$Id$ |
|
*/ |
*/ |
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
|
|
|
int MH_Mg; /* m */ |
int MH_Mg; /* m */ |
double *MH_Beta; /* beta[0], ..., beta[m-1] */ |
double *MH_Beta; /* beta[0], ..., beta[m-1] */ |
double *Ng; /* freedom n. c=(m+1)/2+n/2; Note that it is a pointer */ |
double *MH_Ng; /* freedom n. c=(m+1)/2+n/2; Note that it is a pointer */ |
double X0g; /* initial point */ |
double MH_X0g; /* initial point */ |
double *Iv; /* Initial values of mhg sorted by mhbase() in rd.rr at beta*x0 */ |
static double *Iv; /* Initial values of mhg sorted by mhbase() in rd.rr at beta*x0 */ |
double Ef; /* exponential factor at beta*x0 */ |
static double Ef; /* exponential factor at beta*x0 */ |
extern double Hg; /* step size of rk defined in rk.c */ |
extern double MH_Hg; /* step size of rk defined in rk.c */ |
int Dp; /* Data sampling period */ |
int MH_Dp; /* Data sampling period */ |
double Xng=0.0; /* the last point */ |
static double Xng=0.0; /* the last point */ |
int RawName = 0; |
int MH_RawName = 0; |
int Testrank=0; |
static int Testrank=0; |
extern int Verbose; |
extern int MH_Verbose; |
|
|
extern int P95; /* 95 % points */ |
extern int MH_P95; /* 95 % points */ |
int gopen_file(void); |
int mh_gopen_file(void); |
double rkmain(double x0,double y0[],double xn); |
double mh_rkmain(double x0,double y0[],double xn); |
int setParam(char *fname); |
static int setParamTest(void); |
int showParam(void); |
static int setParamDefault(void); |
|
static int setParam(char *fname); |
|
static int showParam(void); |
|
|
/* #define DEBUG */ |
/* #define DEBUG */ |
#ifdef DEBUG |
#ifdef DEBUG |
char *MH_Dfname; char *MH_Gfname; double Hg; |
char *MH_Dfname; char *MH_Gfname; double MH_Hg; |
int gopen_file(void) { } |
int mh_gopen_file(void) { } |
double rkmain(double x0,double y0[],double xn) { } |
double mh_rkmain(double x0,double y0[],double xn) { } |
#endif |
#endif |
|
|
void *mymalloc(int s) { |
void *mh_malloc(int s) { |
void *p; |
void *p; |
p = (void*)malloc(s); |
p = (void*)malloc(s); |
if (p == NULL) { |
if (p == NULL) { |
fprintf(stderr,"No memory.\n"); exit(-1); |
fprintf(stderr,"No memory.\n"); mh_exit(-1); |
} |
} |
return(p); |
return(p); |
} |
} |
|
mh_free(void *p) { |
|
free(p); |
|
return(0); |
|
} |
static mypower(int x,int n) { |
static mypower(int x,int n) { |
int a,i; |
int a,i; |
a = 1; |
a = 1; |
for (i=0; i<n; i++) a = a*x; |
for (i=0; i<n; i++) a = a*x; |
return(a); |
return(a); |
} |
} |
|
#ifdef STANDALONE |
main(int argc,char *argv[]) { |
main(int argc,char *argv[]) { |
|
return mh_main(argc,char *argv[]); |
|
} |
|
#endif |
|
mh_main(int argc,char *argv[]) { |
double *y0; |
double *y0; |
double x0,xn; |
double x0,xn; |
double ef; |
double ef; |
int i,rank; |
int i,rank; |
setParam(NULL); MH_Gfname = MH_Dfname = NULL; Verbose=1; |
setParam(NULL); MH_Gfname = MH_Dfname = NULL; MH_Verbose=1; |
for (i=1; i<argc; i++) { |
for (i=1; i<argc; i++) { |
if (strcmp(argv[i],"--idata")==0) { |
if (strcmp(argv[i],"--idata")==0) { |
inci(i); |
inci(i); |
setParam(argv[i]); Verbose=0; |
setParam(argv[i]); MH_Verbose=0; |
}else if (strcmp(argv[i],"--gnuplotf")==0) { |
}else if (strcmp(argv[i],"--gnuplotf")==0) { |
inci(i); |
inci(i); |
MH_Gfname = (char *)mymalloc(SMAX); |
MH_Gfname = (char *)mh_malloc(SMAX); |
strncpy(MH_Gfname,argv[i],SMAX-1); |
strncpy(MH_Gfname,argv[i],SMAX-1); |
}else if (strcmp(argv[i],"--dataf")==0) { |
}else if (strcmp(argv[i],"--dataf")==0) { |
inci(i); |
inci(i); |
MH_Dfname = (char *)mymalloc(SMAX); |
MH_Dfname = (char *)mh_malloc(SMAX); |
strncpy(MH_Dfname,argv[i],SMAX-1); |
strncpy(MH_Dfname,argv[i],SMAX-1); |
}else if (strcmp(argv[i],"--xmax")==0) { |
}else if (strcmp(argv[i],"--xmax")==0) { |
inci(i); |
inci(i); |
sscanf(argv[i],"%lf",&Xng); |
sscanf(argv[i],"%lf",&Xng); |
}else if (strcmp(argv[i],"--step")==0) { |
}else if (strcmp(argv[i],"--step")==0) { |
inci(i); |
inci(i); |
sscanf(argv[i],"%lg",&Hg); |
sscanf(argv[i],"%lg",&MH_Hg); |
}else if (strcmp(argv[i],"--help")==0) { |
}else if (strcmp(argv[i],"--help")==0) { |
usage(); return(0); |
mh_usage(); return(0); |
}else if (strcmp(argv[i],"--raw")==0) { |
}else if (strcmp(argv[i],"--raw")==0) { |
RawName = 1; |
MH_RawName = 1; |
}else if (strcmp(argv[i],"--test")==0) { |
}else if (strcmp(argv[i],"--test")==0) { |
inci(i); |
inci(i); |
sscanf(argv[i],"%d",&Testrank); |
sscanf(argv[i],"%d",&Testrank); |
setParamTest(); |
setParamTest(); |
}else if (strcmp(argv[i],"--95")==0) { |
}else if (strcmp(argv[i],"--95")==0) { |
P95=1; |
MH_P95=1; |
}else if (strcmp(argv[i],"--verbose")==0) { |
}else if (strcmp(argv[i],"--verbose")==0) { |
Verbose=1; |
MH_Verbose=1; |
}else { |
}else { |
fprintf(stderr,"Unknown option %s\n",argv[i]); |
fprintf(stderr,"Unknown option %s\n",argv[i]); |
usage(); |
mh_usage(); |
return(-1); |
return(-1); |
} |
} |
} |
} |
if (Verbose) showParam(); |
if (MH_Verbose) showParam(); |
x0 = X0g; |
x0 = MH_X0g; |
xn = Xng; |
xn = Xng; |
ef = Ef; |
ef = Ef; |
rank = mypower(2,MH_Mg); |
rank = mypower(2,MH_Mg); |
y0 = (double *) mymalloc(sizeof(double)*rank); |
y0 = (double *) mh_malloc(sizeof(double)*rank); |
for (i=0; i<rank; i++) y0[i] = ef*Iv[i]; |
for (i=0; i<rank; i++) y0[i] = ef*Iv[i]; |
gopen_file(); |
mh_gopen_file(); |
if (Verbose) {for (i=0; i<rank; i++) printf("%lf\n",y0[i]); } |
if (MH_Verbose) {for (i=0; i<rank; i++) printf("%lf\n",y0[i]); } |
rkmain(x0,y0,xn); |
mh_rkmain(x0,y0,xn); |
} |
} |
|
|
usage() { |
mh_usage() { |
fprintf(stderr,"Usages:\n"); |
fprintf(stderr,"Usages:\n"); |
fprintf(stderr,"w-m [--idata input_data_file --gnuplotf gnuplot_file_name\n"); |
fprintf(stderr,"w-m [--idata input_data_file --gnuplotf gnuplot_file_name\n"); |
fprintf(stderr," --dataf output_data_file --raw --xmax xmax --test m --step h]\n"); |
fprintf(stderr," --dataf output_data_file --raw --xmax xmax --test m --step h]\n"); |
|
|
fprintf(stderr,"All the eigenvalues of sigma must be simple.\n"); |
fprintf(stderr,"All the eigenvalues of sigma must be simple.\n"); |
fprintf(stderr,"Parameters are specified by the input_data_file.\n"); |
fprintf(stderr,"Parameters are specified by the input_data_file.\n"); |
fprintf(stderr,"The format of the input_data_file.\n"); |
fprintf(stderr,"The format of the input_data_file.\n"); |
fprintf(stderr," MH_Mg: m, MH_Beta: beta=sigma^(-1)/2 (diagonized), Ng: n, Iv: initial values at X0g*MH_Beta (see our paper how to order them), \n"); |
fprintf(stderr," MH_Mg: m, MH_Beta: beta=sigma^(-1)/2 (diagonized), MH_Ng: n, Iv: initial values at MH_X0g*MH_Beta (see our paper how to order them), \n"); |
fprintf(stderr," Ef: a scalar factor to the initial value. It may set to 1.\n"); |
fprintf(stderr," Ef: a scalar factor to the initial value. It may set to 1.\n"); |
fprintf(stderr," Hg: h (step size), X0g: starting value of x, Xng: terminating value of x\n"); |
fprintf(stderr," MH_Hg: h (step size), MH_X0g: starting value of x, Xng: terminating value of x\n"); |
fprintf(stderr," Dp: output data is stored in every Dp steps when output_data_file is specified.\n"); |
fprintf(stderr," MH_Dp: output data is stored in every MH_Dp steps when output_data_file is specified.\n"); |
fprintf(stderr," --95: output the 95% point. --verbose: verbose mode.\n"); |
fprintf(stderr," --95: output the 95% point. --verbose: verbose mode.\n"); |
fprintf(stderr," The line started with %% is a comment line.\n"); |
fprintf(stderr," The line started with %% is a comment line.\n"); |
fprintf(stderr," An example format of the input_data_file can be obtained by executing w-2 with no option.\n"); |
fprintf(stderr," An example format of the input_data_file can be obtained by executing w-2 with no option.\n"); |
|
|
fprintf(stderr," tmp-graph is the table of x and the values of Pr({y | y<x}).\n"); |
fprintf(stderr," tmp-graph is the table of x and the values of Pr({y | y<x}).\n"); |
} |
} |
|
|
setParamTest() { |
static setParamTest() { |
int rank; |
int rank; |
int i; |
int i; |
extern int Testrank; |
extern int Testrank; |
extern int Verbose; |
extern int MH_Verbose; |
Verbose=1; |
MH_Verbose=1; |
MH_M= MH_Mg = Testrank ; |
MH_M= MH_Mg = Testrank ; |
MH_RANK = rank = mypower(2,MH_Mg); |
MH_RANK = rank = mypower(2,MH_Mg); |
MH_Beta = (double *)mymalloc(sizeof(double)*MH_Mg); |
MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg); |
for (i=0; i<MH_Mg; i++) MH_Beta[i] = 1.0+0.1*i; |
for (i=0; i<MH_Mg; i++) MH_Beta[i] = 1.0+0.1*i; |
Ng = (double *)mymalloc(sizeof(double)); *Ng = 3.0; |
MH_Ng = (double *)mh_malloc(sizeof(double)); *MH_Ng = 3.0; |
Iv = (double *)mymalloc(sizeof(double)*rank); |
Iv = (double *)mh_malloc(sizeof(double)*rank); |
for (i=0; i<rank; i++) Iv[i] = 0; |
for (i=0; i<rank; i++) Iv[i] = 0; |
Iv[0] = 0.001; |
Iv[0] = 0.001; |
Ef = 1; |
Ef = 1; |
X0g = 0.3; |
MH_X0g = 0.3; |
Hg = 0.001; |
MH_Hg = 0.001; |
Dp = 1; |
MH_Dp = 1; |
Xng = 10.0; |
Xng = 10.0; |
} |
} |
setParamDefault() { |
static setParamDefault() { |
int rank; |
int rank; |
MH_M=MH_Mg = 2 ; |
MH_M=MH_Mg = 2 ; |
MH_RANK=rank = mypower(2,MH_Mg); |
MH_RANK=rank = mypower(2,MH_Mg); |
MH_Beta = (double *)mymalloc(sizeof(double)*MH_Mg); |
MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg); |
MH_Beta[0] = 1.0; MH_Beta[1] = 2.0; |
MH_Beta[0] = 1.0; MH_Beta[1] = 2.0; |
Ng = (double *)mymalloc(sizeof(double)); *Ng = 3.0; |
MH_Ng = (double *)mh_malloc(sizeof(double)); *MH_Ng = 3.0; |
Iv = (double *)mymalloc(sizeof(double)*rank); |
Iv = (double *)mh_malloc(sizeof(double)*rank); |
Iv[0] = 1.58693; |
Iv[0] = 1.58693; |
Iv[1] = 0.811369; |
Iv[1] = 0.811369; |
Iv[2] = 0.846874; |
Iv[2] = 0.846874; |
Iv[3] = 0.413438; |
Iv[3] = 0.413438; |
Ef = 0.01034957388338225707; |
Ef = 0.01034957388338225707; |
X0g = 0.3; |
MH_X0g = 0.3; |
Hg = 0.001; |
MH_Hg = 0.001; |
Dp = 1; |
MH_Dp = 1; |
Xng = 10.0; |
Xng = 10.0; |
} |
} |
|
|
Line 183 next(FILE *fp,char *s,char *msg) { |
|
Line 194 next(FILE *fp,char *s,char *msg) { |
|
while (s[0] == '%') { |
while (s[0] == '%') { |
if (!fgets(s,SMAX,fp)) { |
if (!fgets(s,SMAX,fp)) { |
fprintf(stderr,"Data format error at %s\n",msg); |
fprintf(stderr,"Data format error at %s\n",msg); |
exit(-1); |
mh_exit(-1); |
} |
} |
if (s[0] != '%') return(0); |
if (s[0] != '%') return(0); |
} |
} |
} |
} |
setParam(char *fname) { |
static setParam(char *fname) { |
int rank; |
int rank; |
char s[SMAX]; |
char s[SMAX]; |
FILE *fp; |
FILE *fp; |
Line 197 setParam(char *fname) { |
|
Line 208 setParam(char *fname) { |
|
|
|
if ((fp=fopen(fname,"r")) == NULL) { |
if ((fp=fopen(fname,"r")) == NULL) { |
fprintf(stderr,"File %s is not found.\n",fname); |
fprintf(stderr,"File %s is not found.\n",fname); |
exit(-1); |
mh_exit(-1); |
} |
} |
next(fp,s,"MH_Mg(m)"); |
next(fp,s,"MH_Mg(m)"); |
sscanf(s,"%d",&MH_Mg); MH_M=MH_Mg; |
sscanf(s,"%d",&MH_Mg); MH_M=MH_Mg; |
MH_RANK=rank = mypower(2,MH_Mg); |
MH_RANK=rank = mypower(2,MH_Mg); |
|
|
MH_Beta = (double *)mymalloc(sizeof(double)*MH_Mg); |
MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg); |
for (i=0; i<MH_Mg; i++) { |
for (i=0; i<MH_Mg; i++) { |
next(fp,s,"MH_Beta"); |
next(fp,s,"MH_Beta"); |
sscanf(s,"%lf",&(MH_Beta[i])); |
sscanf(s,"%lf",&(MH_Beta[i])); |
} |
} |
|
|
Ng = (double *)mymalloc(sizeof(double)); |
MH_Ng = (double *)mh_malloc(sizeof(double)); |
next(fp,s,"Ng(freedom parameter n)"); |
next(fp,s,"MH_Ng(freedom parameter n)"); |
sscanf(s,"%lf",Ng); |
sscanf(s,"%lf",MH_Ng); |
|
|
next(fp,s,"X0g(initial point)"); |
next(fp,s,"MH_X0g(initial point)"); |
sscanf(s,"%lf",&X0g); |
sscanf(s,"%lf",&MH_X0g); |
|
|
Iv = (double *)mymalloc(sizeof(double)*rank); |
Iv = (double *)mh_malloc(sizeof(double)*rank); |
for (i=0; i<rank; i++) { |
for (i=0; i<rank; i++) { |
next(fp,s,"Iv(initial values)"); |
next(fp,s,"Iv(initial values)"); |
sscanf(s,"%lg",&(Iv[i])); |
sscanf(s,"%lg",&(Iv[i])); |
Line 225 setParam(char *fname) { |
|
Line 236 setParam(char *fname) { |
|
next(fp,s,"Ef(exponential factor)"); |
next(fp,s,"Ef(exponential factor)"); |
sscanf(s,"%lg",&Ef); |
sscanf(s,"%lg",&Ef); |
|
|
next(fp,s,"Hg (step size of rk)"); |
next(fp,s,"MH_Hg (step size of rk)"); |
sscanf(s,"%lg",&Hg); |
sscanf(s,"%lg",&MH_Hg); |
|
|
next(fp,s,"Dp (data sampling period)"); |
next(fp,s,"MH_Dp (data sampling period)"); |
sscanf(s,"%d",&Dp); |
sscanf(s,"%d",&MH_Dp); |
|
|
next(fp,s,"Xng (the last point, cf. --xmax)"); |
next(fp,s,"Xng (the last point, cf. --xmax)"); |
sscanf(s,"%lf",&Xng); |
sscanf(s,"%lf",&Xng); |
|
|
for (i=0; i<MH_Mg; i++) { |
for (i=0; i<MH_Mg; i++) { |
printf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]); |
printf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]); |
} |
} |
printf("%%Ng=\n%lf\n",*Ng); |
printf("%%MH_Ng=\n%lf\n",*MH_Ng); |
printf("%%X0g=\n%lf\n",X0g); |
printf("%%MH_X0g=\n%lf\n",MH_X0g); |
for (i=0; i<rank; i++) { |
for (i=0; i<rank; i++) { |
printf("%%Iv[%d]=\n%lg\n",i,Iv[i]); |
printf("%%Iv[%d]=\n%lg\n",i,Iv[i]); |
} |
} |
printf("%%Ef=\n%lf\n",Ef); |
printf("%%Ef=\n%lf\n",Ef); |
printf("%%Hg=\n%lf\n",Hg); |
printf("%%MH_Hg=\n%lf\n",MH_Hg); |
printf("%%Dp=\n%d\n",Dp); |
printf("%%MH_Dp=\n%d\n",MH_Dp); |
printf("%%Xng=\n%lf\n",Xng); |
printf("%%Xng=\n%lf\n",Xng); |
} |
} |
|
|
|
mh_exit(int n) { |
|
#ifdef STANDALONE |
|
exit(n); |
|
#else |
|
fprintf(stderr,"Fatal error mh_exit(%d) in mh-w-n.\n",n); |
|
#endif |
|
} |