[BACK]Return to wmain.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / hgm / mh / src

Diff for /OpenXM/src/hgm/mh/src/wmain.c between version 1.1 and 1.32

version 1.1, 2013/02/19 03:06:19 version 1.32, 2016/02/16 02:17:00
Line 1 
Line 1 
 /*  /*
 License: LGPL    $OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.31 2016/02/13 22:56:50 takayama Exp $
 $Id$    License: LGPL
  */  */
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <math.h>  #include <math.h>
 #include <string.h>  #include <string.h>
   #include "sfile.h"
   #include "mh.h"
 #define SMAX 4096  #define SMAX 4096
 #define inci(i) { i++; if (i >= argc) { fprintf(stderr,"Option argument is not given.\n"); return(-1); }}  #define inci(i) { i++; if (i >= argc) { oxprintfe("Option argument is not given.\n"); return(NULL); }}
   #define VSTRING "%!version2.0"
   int MH_deallocate=0;
   
   /*
     changelog
     2016.02.13  code-n.c and code-n-2f1.c are linked together. New idata format.
     2016.02.04  MH_Ef_type  exponential or scalar factor
     2016.02.01  C_2F1
     2014.03.15  --strategy 1 is default. A new parser in setParam()
    */
 extern char *MH_Gfname;  extern char *MH_Gfname;
 extern char *MH_Dfname;  extern char *MH_Dfname;
   
 /* global variables. They are set in setParam() */  /* global variables. They are set in setParam() */
   int MH_byFile=1;
 int MH_RANK;  int MH_RANK;
 int MH_M;  int MH_M;
   
 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;  /* If MH_success is set to 1, then strategy, MH_abserr, MH_relerr seem to
     be properly set.
   */
   int MH_success=0;
   /*
     Estimation of the maximal coeff of A in y'=Ay.
     This might be too rough estimate.
    */
   double MH_coeff_max;
   /*
     Estimation of h by MH_coeff_max;
    */
   double MH_estimated_start_step;
   
 extern int P95;  /* 95 % points */  int MH_P_pFq=1;
 int gopen_file(void);  int MH_Q_pFq=1;
 double rkmain(double x0,double y0[],double xn);  double *MH_A_pFq=NULL;
 int setParam(char *fname);  double *MH_B_pFq=NULL;
 int showParam(void);  int MH_Ef_type=1;
   MH_RF mh_rf=NULL;  /* function pointer to mh_rf */
   
   extern int MH_Verbose;
   extern int MH_strategy;
   extern double MH_abserr;
   extern double MH_relerr;
   
   extern int MH_P95;  /* 95 % points */
   int mh_gopen_file(void);
   static int setParamTest(void);
   static int setParamDefault(void);
   static int setParam(char *fname);
   static int showParam(void);
   static int next(struct SFILE *fp,char *s, char *msg);
   static double estimateHg(int m, double beta[],double x0);
   
   
 /* #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) { }  struct MH_RESULT mh_rkmain(double x0,double y0[],double xn) { }
 #endif  #endif
   
 void *mymalloc(int s) {  void mh_freeWorkArea(void) {
   void *p;    extern int MH_deallocate;
   p = (void*)malloc(s);    MH_deallocate=1; /* switch to deallocation mode. */
   if (p == NULL) {    mh_main(0,NULL);
         fprintf(stderr,"No memory.\n"); exit(-1);    setParam(NULL);
   }    mh_rkmain(0.0, NULL, 0.0);
   return(p);    if (mh_rf) (*mh_rf)(0.0, NULL, 0, NULL, 0);
     MH_deallocate=0; /* switch to the normal mode. */
 }  }
 static mypower(int x,int n) {  static int 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);
 }  }
 main(int argc,char *argv[]) {  #ifdef STANDALONE2
   double *y0;  int main(int argc,char *argv[]) {
     int strategy=STRATEGY_DEFAULT;
     double err[2]={-1.0,-1.0};
     int i;
     mh_exit(MH_RESET_EXIT); /* standalone mode */
     /*  mh_main(argc,argv);
         mh_freeWorkArea(); */
     mh_main(argc,argv);
     /* showParam(); */
     return(0);
   }
   #endif
   struct MH_RESULT *mh_main(int argc,char *argv[]) {
     static double *y0=NULL;
   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;    struct MH_RESULT *rp=NULL;
     extern int MH_deallocate;
     extern int MH_byFile;
     extern double Ef;
     Ef = 1;
     MH_byFile=1;
     if (MH_deallocate) { if (y0) mh_free(y0); return(rp); }
     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);  
           setParam(argv[i]); Verbose=0;  
         }else if (strcmp(argv[i],"--gnuplotf")==0) {  
           inci(i);  
           MH_Gfname = (char *)mymalloc(SMAX);  
           strncpy(MH_Gfname,argv[i],SMAX-1);  
         }else if (strcmp(argv[i],"--dataf")==0) {  
           inci(i);  
           MH_Dfname = (char *)mymalloc(SMAX);  
           strncpy(MH_Dfname,argv[i],SMAX-1);  
         }else if (strcmp(argv[i],"--xmax")==0) {  
       inci(i);        inci(i);
           sscanf(argv[i],"%lf",&Xng);        setParam(argv[i]); MH_Verbose=0;
         }else if (strcmp(argv[i],"--step")==0) {      }else if (strcmp(argv[i],"--gnuplotf")==0) {
       inci(i);        inci(i);
           sscanf(argv[i],"%lg",&Hg);        MH_Gfname = (char *)mh_malloc(SMAX);
         }else if (strcmp(argv[i],"--help")==0) {        strcpy(MH_Gfname,argv[i]);
           usage(); return(0);      }else if (strcmp(argv[i],"--dataf")==0) {
         }else if (strcmp(argv[i],"--raw")==0) {        inci(i);
           RawName = 1;        MH_Dfname = (char *)mh_malloc(SMAX);
         }else if (strcmp(argv[i],"--test")==0) {        strcpy(MH_Dfname,argv[i]);
           inci(i);      }else if (strcmp(argv[i],"--xmax")==0) {
           sscanf(argv[i],"%d",&Testrank);        inci(i);
           setParamTest();        sscanf(argv[i],"%lf",&Xng);
         }else if (strcmp(argv[i],"--95")==0) {      }else if (strcmp(argv[i],"--step")==0) {
           P95=1;        inci(i);
         }else if (strcmp(argv[i],"--verbose")==0) {        sscanf(argv[i],"%lg",&MH_Hg);
           Verbose=1;      }else if (strcmp(argv[i],"--help")==0) {
         }else {        mh_usage(); return(rp);
           fprintf(stderr,"Unknown option %s\n",argv[i]);      }else if (strcmp(argv[i],"--raw")==0) {
           usage();        MH_RawName = 1;
           return(-1);      }else if (strcmp(argv[i],"--test")==0) {
         }        inci(i);
         sscanf(argv[i],"%d",&Testrank);
         setParamTest();
       }else if (strcmp(argv[i],"--95")==0) {
         MH_P95=1;
       }else if (strcmp(argv[i],"--verbose")==0) {
         MH_Verbose=1;
       }else if (strcmp(argv[i],"--bystring")==0) {
         MH_byFile = 0;
       }else if (strcmp(argv[i],"--strategy")==0) {
         i++; sscanf(argv[i],"%d",&MH_strategy);
       }else if (strcmp(argv[i],"--abserr")==0) {
         i++; sscanf(argv[i],"%lg",&MH_abserr);
       }else if (strcmp(argv[i],"--relerr")==0) {
         i++; sscanf(argv[i],"%lg",&MH_relerr);
       }else {
         oxprintfe("Unknown option %s\n",argv[i]);
         mh_usage();
         return(rp);
       }
   }    }
   if (Verbose) showParam();    x0 = MH_X0g;
   x0 = 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]); }    rp = (struct MH_RESULT*) mh_malloc(sizeof(struct MH_RESULT));
   rkmain(x0,y0,xn);  
     if (MH_strategy) {
       if (MH_abserr > SIGDIGIT_DEFAULT*myabs(y0[0])) {
         MH_success = 0;
         oxprintfe("%%%%Warning, abserr seems not to be small enough, abserr=%lg, y[0]=%lg. Increasing the starting point (q0 or X0g(standalone case)) may or making abserr (err[1] or abserror(standalone case))  smaller will help.\n",MH_abserr,y0[0]);
       }else{
         MH_success = 1;
       }
     }else{
       MH_success = 0;
     }
     MH_estimated_start_step = estimateHg(MH_Mg,MH_Beta,MH_X0g);
     if (MH_Verbose) showParam();
     if (MH_Verbose) {for (i=0; i<rank; i++) oxprintf("%lf\n",y0[i]); }
   
     *rp=mh_rkmain(x0,y0,xn);
     return(rp);
 }  }
   
 usage() {  int mh_usage() {
   fprintf(stderr,"Usages:\n");  
   fprintf(stderr,"w-m [--idata input_data_file --gnuplotf gnuplot_file_name\n");  #include "usage-w-n.h"
   fprintf(stderr," --dataf output_data_file --raw --xmax xmax --test m --step h]\n");  
   fprintf(stderr,"[ --95 --verbose] \n");    return(0);
   fprintf(stderr,"--raw does not add data parameters to the output_data_file.\n");  
   fprintf(stderr,"\nThe command w-m [options] evaluates Pr({y | y<xmax}), which is the cumulative distribution function of the largest root of the m by m Wishart matrix with n degrees of freedom and the covariantce matrix sigma.\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,"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," 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," Dp: output data is stored in every Dp steps when output_data_file is specified.\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," An example format of the input_data_file can be obtained by executing w-2 with no option.\n");  
   fprintf(stderr,"\nExamples:\n");  
   fprintf(stderr,"[1] ./w-2 \n");  
   fprintf(stderr,"[2] ./w-2 --xmax 20\n");  
   fprintf(stderr,"[3] ./w-6 --test 6\n");  
   fprintf(stderr,"   A test run of w-6.\n");  
   fprintf(stderr,"   The number m and mm must agree for  ./w-m --test mm.\n");  
   fprintf(stderr,"[4] ./w-5 --idata tmp-inm-m5* --gnuplot tmp-graph\n");  
   fprintf(stderr,"  tmp-inm-m5* is a sample input data distributed with this file.\n");  
   fprintf(stderr,"  tmp-graph-gp.txt is an input file of the gnuplot\n");  
   fprintf(stderr,"  It can be executed with the load command in the gnuplot.\n");  
   fprintf(stderr,"  tmp-graph is the table of x and the values of Pr({y | y<x}).\n");  
 }  }
   
 setParamTest() {  static int 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; return(0);
 }  }
 setParamDefault() {  static int setParamDefault() {
   int rank;    int rank;
     double a[1];
     double b[1];
   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);    a[0] = (MH_M+1.0)/2.0;
     b[0] = a[0]+(*MH_Ng)/2.0;
     mh_setA(a,1); mh_setB(b,1);
     mh_rf = & mh_rf_ef_type_1;
     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; return(0);
 }  }
   #ifdef STANDALONE
 next(FILE *fp,char *s,char *msg) {  /* may remove */
   static int next_old1(struct SFILE *sfp,char *s,char *msg) {
   s[0] = '%';    s[0] = '%';
   while (s[0] == '%') {    while (s[0] == '%') {
         if (!fgets(s,SMAX,fp)) {      if (!mh_fgets(s,SMAX,sfp)) {
           fprintf(stderr,"Data format error at %s\n",msg);        oxprintfe("Data format error at %s\n",msg);
           exit(-1);        mh_exit(-1);
       }
       if (s[0] != '%') return(0);
     } return(0);
   }
   #endif
   static int next(struct SFILE *sfp,char *s,char *msg) {
     static int check=1;
     char *ng="%%Ng=";
     // int i;
     s[0] = '%';
     while ((s[0] == '%') || (s[0] == '#')) {
       if (!mh_fgets(s,SMAX,sfp)) {
         oxprintfe("Data format error at %s\n",msg);
         oxprintfe("Is it version 2.0 format? If so, add\n%s\nat the top.\n",VSTRING);
         mh_exit(-1);
       }
           if ((s[0] == '%') && (s[1] == '%')) continue;
       if (s[0] == '#') continue;
       if (strncmp(s,VSTRING,strlen(VSTRING)) == 0) {
             return(2);
         }          }
         if (s[0] != '%') return(0);      if (check && (strncmp(msg,ng,4)==0)) {
         if (strncmp(s,ng,5) != 0) {
           oxprintfe("Warning, there is no %%Ng= at the border of Beta's and Ng, s=%s\n",s);
         }
         /* check=0; */
       }
       if (s[0] != '%') return(0);
   }    }
     return(0);
 }  }
 setParam(char *fname) {  
   static int setParam(char *fname) {
   int rank;    int rank;
   char s[SMAX];    char s[SMAX];
   FILE *fp;    struct SFILE *fp;
   int i;    int i;
     int version;
     double a[1]; double b[1]; /* for old inputs */
     struct mh_token tk;
     extern int MH_deallocate;
     extern int MH_byFile;
     if (MH_deallocate) {
       if (MH_Beta) {mh_free(MH_Beta); MH_Beta=NULL;}
       if (MH_Ng) {mh_free(MH_Ng); MH_Ng=NULL;}
       if (Iv) {mh_free(Iv); Iv = NULL;}
           if (MH_A_pFq) {mh_free(MH_A_pFq); MH_A_pFq=NULL; MH_P_pFq = 0;}
           if (MH_B_pFq) {mh_free(MH_B_pFq); MH_B_pFq=NULL; MH_Q_pFq = 0;}
       return(0);
     }
   if (fname == NULL) return(setParamDefault());    if (fname == NULL) return(setParamDefault());
   
   if ((fp=fopen(fname,"r")) == NULL) {    if ((fp=mh_fopen(fname,"r",MH_byFile)) == NULL) {
         fprintf(stderr,"File %s is not found.\n",fname);      oxprintfe("File %s is not found.\n",fname);
         exit(-1);      mh_exit(-1);
   }    }
   next(fp,s,"MH_Mg(m)");    /* set default initial values */
     MH_Mg=MH_M=-1;  /* number of variables */
     MH_Ng=(double *) mh_malloc(sizeof(double)); *MH_Ng=-1; /* *Ng is the degree of freedom 1F1 */
     MH_X0g=0.1;   /* evaluation point */
     Ef=1.0;    /* exponential factor */
     MH_Ef_type=1;
     MH_Hg=0.01;   /* step size for RK */
     MH_Dp = 1;   /* sampling rate */
     Xng = 10.0; /* terminal point for RK */
   
     /* Parser for the old style (version <2.0) input */
     version=next(fp,s,"Mg(m)");
     if (version == 2) goto mh_myparse;
   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));    next(fp,s,"MH_Ng(freedom parameter n)");
   next(fp,s,"Ng(freedom parameter n)");    sscanf(s,"%lf",MH_Ng);
   sscanf(s,"%lf",Ng);    /* for old style input only for 1F1 ef_type==1. */
     MH_P_pFq = 1; MH_Q_pFq = 1;
     a[0] = (MH_M+1.0)/2.0;
     b[0] = a[0]+(*MH_Ng)/2.0;
     mh_setA(a,1); mh_setB(b,1);
     mh_rf = & mh_rf_ef_type_1;
   
   next(fp,s,"X0g(initial point)");  
   sscanf(s,"%lf",&X0g);  
   
   Iv = (double *)mymalloc(sizeof(double)*rank);    next(fp,s,"MH_X0g(initial point)");
     sscanf(s,"%lf",&MH_X0g);
   
     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]));
   }    }
   
   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);
   fclose(fp);  
     /* Reading the optional parameters */
    mh_myparse:
     while ((tk = mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_EOF) {
       /* expect ID */
       if (tk.type != MH_TOKEN_ID) {
         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
       }
       if ((strcmp(s,"abserr")==0) || (strcmp(s,"abserror")==0)) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
           MH_abserr = tk.dval;
         }else if (tk.type == MH_TOKEN_INT) {
           MH_abserr = tk.ival;
         }else{
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         continue;
       }
       if ((strcmp(s,"relerr")==0) || (strcmp(s,"relerror")==0)) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
           MH_relerr = tk.dval;
         }else if (tk.type == MH_TOKEN_INT) {
           MH_relerr = tk.dval;
         }else{
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         continue;
       }
       if (strcmp(s,"strategy")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         MH_strategy = tk.ival;
         continue;
       }
       // Format: #p_pFq=2  1.5  3.2   override by new input format.
       if (strcmp(s,"p_pFq")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         MH_P_pFq = tk.ival;
             mh_setA(NULL,tk.ival); /* allocate mem */
         for (i=0; i<MH_P_pFq; i++) {
           if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
             MH_A_pFq[i] = tk.dval;
           }else if (tk.type == MH_TOKEN_INT) {
             MH_A_pFq[i] = tk.ival;
           }else{
             oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
           }
         }
         continue;
       }
       if (strcmp(s,"q_pFq")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         MH_Q_pFq = tk.ival;
             mh_setB(NULL,tk.ival); /* allocate mem */
         for (i=0; i<MH_Q_pFq; i++) {
           if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
             MH_B_pFq[i] = tk.dval;
           }else if (tk.type == MH_TOKEN_INT) {
             MH_B_pFq[i] = tk.ival;
           }else{
             oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
           }
         }
         continue;
       }
       if (strcmp(s,"ef_type")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         MH_Ef_type = tk.ival;
         continue;
       }
   
       if (strcmp(s,"Mg")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         MH_Mg = tk.ival;  MH_M = MH_Mg;
         MH_RANK = rank = mypower(2,MH_Mg);
         continue;
       }
       if (strcmp(s,"Beta")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
             if (MH_Mg <= 0) {
           oxprintfe("Mg should be set before reading Beta.\n"); mh_exit(-1);
         }
         MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
             for (i=0; i<MH_Mg; i++) {
           if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
              MH_Beta[i] = tk.dval;
           }else if (tk.type == MH_TOKEN_INT) {
              MH_Beta[i] = tk.ival;
           }else {
             oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
           }
         }
         Iv = (double *)mh_malloc(sizeof(double)*rank);
             for (i=0; i<rank; i++) {
                   Iv[i] = 0.0;
             }
         continue;
       }
       if (strcmp(s,"Ng")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
   
         if ((tk=mh_getoken(s,SMAX-1,fp)).type== MH_TOKEN_DOUBLE) {
                   *MH_Ng = tk.dval;
         }else if (tk.type == MH_TOKEN_INT) {
           *MH_Ng = tk.ival;
         }else{
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         continue;
       }
       if (strcmp(s,"X0g")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                   MH_X0g = tk.dval;
             }else if (tk.type == MH_TOKEN_INT) {
                   MH_X0g = tk.ival;
         }else{
                   oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
             }
         continue;
       }
           if (strcmp(s,"Iv")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
             for (i=0; i<rank; i++) {
           if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                     Iv[i] = tk.dval;
                   }else if (tk.type == MH_TOKEN_INT) {
                     Iv[i] = tk.ival;
           }else{
             oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                   }
         }
         continue;
       }
       if (strcmp(s,"Ef")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                   Ef = tk.dval;
             }else if (tk.type == MH_TOKEN_INT) {
                   Ef = tk.ival;
         }else{
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
             }
         continue;
       }
       if (strcmp(s,"Hg")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                   MH_Hg = tk.dval;
             }else if (tk.type == MH_TOKEN_INT) {
                   MH_Hg = tk.ival;
         }else{
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         continue;
       }
       if (strcmp(s,"Dp")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         MH_Dp = tk.dval;
         continue;
       }
       if (strcmp(s,"Xng")==0) {
         if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
         }
         if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                   Xng = tk.dval;
             }else if (tk.type == MH_TOKEN_INT) {
                   Xng = tk.ival;
         }else{
           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
             }
         continue;
       }
   
       oxprintfe("Unknown ID for wmain.c (old...) at %s.\n",s);  mh_exit(-1);
     }
     if (MH_Ef_type == 1) {
           mh_rf = &mh_rf_ef_type_1;
     }else if (MH_Ef_type == 2) {
           mh_rf = &mh_rf_ef_type_2;
     }else{
           oxprintfe("Ef_type = %d is not implemented.\n",MH_Ef_type); mh_exit(-1);
     }
     if ((MH_M < 0) || (MH_A_pFq==NULL)) {
           oxprintfe("MH_M and p_pFq, q_pFq are compulsory parameters.\n"); mh_exit(-1);
     }
   
     mh_fclose(fp); return(0);
 }  }
   
 showParam() {  static int showParam() {
   int rank,i;    int rank,i;
     extern int MH_strategy;
     extern double MH_abserr;
     extern double MH_relerr;
   rank = mypower(2,MH_Mg);    rank = mypower(2,MH_Mg);
   printf("%%MH_Mg=\n%d\n",MH_Mg);    oxprintf("%%MH_Mg=\n%d\n",MH_Mg);
   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]);      oxprintf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]);
   }    }
   printf("%%Ng=\n%lf\n",*Ng);    oxprintf("%%MH_Ng=\n%lf\n",*MH_Ng);
   printf("%%X0g=\n%lf\n",X0g);    oxprintf("%%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]);      oxprintf("%%Iv[%d]=\n%lg\n",i,Iv[i]);
   }    }
   printf("%%Ef=\n%lf\n",Ef);    oxprintf("%%Ef=\n%lf\n",Ef);
   printf("%%Hg=\n%lf\n",Hg);    oxprintf("%%MH_Hg=\n%lf\n",MH_Hg);
   printf("%%Dp=\n%d\n",Dp);    oxprintf("%%MH_Dp=\n%d\n",MH_Dp);
   printf("%%Xng=\n%lf\n",Xng);    oxprintf("%%Xng=\n%lf\n",Xng);
     oxprintf("%%strategy=%d\n",MH_strategy);
     oxprintf("%%abserr=%lg, %%relerr=%lg\n",MH_abserr,MH_relerr);
     oxprintf("#MH_success=%d\n",MH_success);
     oxprintf("#MH_coeff_max=%lg\n",MH_coeff_max);
     oxprintf("#MH_estimated_start_step=%lg\n",MH_estimated_start_step);
     oxprintf("%%ef_type=%d\n",MH_Ef_type);
     oxprintf("%%q_pFq=%d, ",MH_P_pFq);
     for (i=0; i<MH_P_pFq; i++) {
       if (i != MH_P_pFq-1) oxprintf(" %lg,",MH_A_pFq[i]);
       else oxprintf(" %lg\n",MH_A_pFq[i]);
     }
     oxprintf("%%q_pFq=%d, ",MH_Q_pFq);
     for (i=0; i<MH_Q_pFq; i++) {
       if (i != MH_Q_pFq-1) oxprintf(" %lg,",MH_B_pFq[i]);
       else oxprintf(" %lg\n",MH_B_pFq[i]);
     }
     return(0);
 }  }
   
   static double estimateHg(int m, double beta[],double x0) {
     int i,j;
     double dmin;
     double cmax;
     double h;
     /* mynote on 2014.03.15 */
     if (m>1) dmin = myabs(beta[1]-beta[0]);
     else dmin=myabs(beta[0]);
     for (i=0; i<m; i++) {
       for (j=i+1; j<m; j++) {
         if (myabs(beta[i]-beta[j]) < dmin) dmin = myabs(beta[i]-beta[j]);
       }
     }
     dmin = dmin*x0*2;
     cmax = 1.0;
     for (i=0; i<m; i++) cmax = cmax*dmin;
     cmax = 1.0/cmax;
     MH_coeff_max=cmax;
     h = exp(log(MH_abserr/cmax)/5.0);
     MH_estimated_start_step = h;
     return h;
   }
   
   void mh_setA(double a[],int alen) {
     int i;
     if (alen < 0) {
           if (MH_A_pFq != NULL) mh_free(MH_A_pFq);
           MH_A_pFq=NULL; MH_P_pFq=-1;
           return;
     }
     if (alen == 0) {
           MH_P_pFq=0; return;
     }
     MH_P_pFq=alen;
     MH_A_pFq = (double *)mh_malloc(MH_P_pFq*sizeof(double));
     if (a != NULL) {
           for (i=0; i<alen; i++) MH_A_pFq[i] = a[i];
     }else{
           for (i=0; i<alen; i++) MH_A_pFq[i] = 0.0;
     }
     return;
   }
   void mh_setB(double b[],int blen) {
     int i;
     if (blen < 0) {
           if (MH_B_pFq != NULL) mh_free(MH_B_pFq);
           MH_B_pFq=NULL; MH_Q_pFq=-1;
           return;
     }
     if (blen == 0) {
           MH_Q_pFq=0; return;
     }
     MH_Q_pFq=blen;
     MH_B_pFq = (double *)mh_malloc(MH_Q_pFq*sizeof(double));
     if (b != NULL) {
           for (i=0; i<blen; i++) MH_B_pFq[i] = b[i];
     }else{
           for (i=0; i<blen; i++) MH_B_pFq[i] = 0.0;
     }
     return;
   }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.32

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>