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

Annotation of OpenXM/src/hgm/mh/src/mh.c, Revision 1.7

1.7     ! takayama    1: /* $OpenXM: OpenXM/src/hgm/mh/src/mh.c,v 1.6 2013/03/05 06:00:52 takayama Exp $ */
1.1       takayama    2: #include <stdio.h>
                      3: #include "sfile.h"
                      4: #include "mh.h"
                      5: #define WSIZE 1024
1.4       takayama    6: extern int MH_DEBUG;
1.1       takayama    7: static imypower(int x,int n) {
                      8:   int a,i;
                      9:   a = 1;
                     10:   for (i=0; i<n; i++) a = a*x;
                     11:   return(a);
                     12: }
                     13:
                     14: struct cWishart *new_cWishart(int rank) {
                     15:   struct cWishart *cwp;
                     16:   cwp = (struct cWishart *)mh_malloc(sizeof(struct cWishart));
                     17:   cwp->x=0;
                     18:   cwp->rank=rank;
                     19:   cwp->f = (double *)mh_malloc(sizeof(double)*rank);
                     20:   cwp->aux = cwp->aux2 = NULL;
                     21:   return(cwp);
                     22: }
                     23:
1.3       takayama   24: struct cWishart *mh_cwishart_gen(int m,int n,double beta[],double x0,
1.6       takayama   25:                              int approxDeg,double h, int dp, double x,int modep[]) {
                     26:   /*
                     27:      modep[0]. Do Koev-Edelman (ignored for now).
                     28:      modep[1]. Do the HGM
                     29:      modep[2]. Return modep[2] intermediate data.
                     30:    */
1.1       takayama   31:   struct SFILE *fp;
                     32:   char swork[WSIZE];
                     33:   char *argv[WSIZE];
                     34:   int i,rank;
                     35:   char *comm;
                     36:   struct MH_RESULT *rp;
                     37:   struct SFILE *sfp;
                     38:   struct cWishart *cw;
                     39:   argv[0]="dummy";
                     40:   argv[1] = "--bystring";
                     41:   argv[2] = "--idata";
                     42:   fp = mh_fopen("","w",0);
                     43:   mh_fputs("%%Mg=\n",fp);
                     44:   sprintf(swork,"%d\n",m); mh_fputs(swork,fp);
                     45:   rank = imypower(2,m);
                     46:   mh_fputs("%%Beta\n",fp);
                     47:   for (i=0; i<m; i++) {
                     48:     sprintf(swork,"%lf\n",beta[i]); mh_fputs(swork,fp);
                     49:   }
                     50:   mh_fputs("%%Ng=\n",fp); /* freedom param */
                     51:   sprintf(swork,"%d\n",n); mh_fputs(swork,fp);
                     52:   mh_fputs("%%X0g=\n",fp); /* initial point */
                     53:   sprintf(swork,"%lf\n",x0); mh_fputs(swork,fp);
                     54:   mh_fputs("%%Iv\n",fp); /* initial values, dummy */
                     55:   for (i=0; i<rank; i++) mh_fputs("0.0\n",fp);
                     56:   mh_fputs("%%Ef=\n1.0\n",fp); /* Below are dummy values */
                     57:   if (h <= 0.0) {fprintf(stderr,"h<=0.0, set to 0.1\n"); h=0.1;}
                     58:   mh_fputs("%%Hg=\n",fp);
                     59:   sprintf(swork,"%lf\n",h); mh_fputs(swork,fp);
                     60:   if (dp < 1) {fprintf(stderr,"dp<1, set to 1\n"); dp=1;}
                     61:   mh_fputs("%%Dp=\n",fp);
                     62:   sprintf(swork,"%d\n",dp); mh_fputs(swork,fp);
                     63:   if (x <= x0) {fprintf(stderr,"x <= x0, set to x=x0+10\n"); x=x0+10;}
                     64:   mh_fputs("%%Xng=\n",fp);
                     65:   sprintf(swork,"%lf\n",x); mh_fputs(swork,fp);
                     66:
                     67:   comm = (char *)mh_malloc(fp->len +1);
                     68:   mh_outstr(comm,fp->len+1,fp);
                     69:   mh_fclose(fp);
                     70:   argv[3] = comm;
                     71:
                     72:   argv[4] = "--degree";
                     73:   argv[5] = (char *)mh_malloc(128);
                     74:   sprintf(argv[5],"%d",approxDeg);
                     75:
1.4       takayama   76:   rp=jk_main(6,argv);
1.1       takayama   77:   if (rp == NULL) {
                     78:     fprintf(stderr,"rp is NULL.\n"); return(NULL);
                     79:   }
                     80:   cw = new_cWishart(rank);
                     81:   cw->x = rp->x;
                     82:   cw->rank = rp->rank;
1.5       takayama   83:   if (rank !=  cw->rank) {
                     84:     fprintf(stderr,"Rank error.\n"); return(NULL);
                     85:   }
1.4       takayama   86:   for (i=0; i<cw->rank; i++) (cw->f)[i] = (rp->y)[i];
                     87:   sfp = (rp->sfpp)[0];
1.5       takayama   88:   cw->aux = (char *) mh_malloc((sfp->len)+1);
                     89:   mh_outstr((char *)(cw->aux),(sfp->len)+1,sfp);
                     90:   /* todo, the following line seems to cause seg fault. */
1.1       takayama   91:   /* deallocate the memory */
                     92:   for (i=0; i<rp->size; i++) mh_fclose((rp->sfpp)[i]);
                     93:   /* todo, mh_free_??(rp);  free Iv's */
1.6       takayama   94:   if (!modep[1]) return(cw);
1.1       takayama   95:
1.5       takayama   96:   if (MH_DEBUG) printf("\n\n%s\n",(char *)cw->aux);
                     97:   /* This output is strange. */
1.1       takayama   98:   /* Starting HGM */
                     99:   argv[3] = (char *)cw->aux;
                    100:   argv[4] = "--dataf";
                    101:   argv[5] = "dummy-dataf";
                    102:   rp = mh_main(6,argv);
                    103:   if (rp == NULL) {
                    104:     fprintf(stderr,"rp is NULL in the second step.\n"); return(NULL);
                    105:   }
                    106:   cw = new_cWishart(rank);
                    107:   cw->x = rp->x;
                    108:   cw->rank = rp->rank;
                    109:   for (i=0; i<cw->rank; i++) (cw->f)[i] = (rp->y)[i];
                    110:   sfp = (rp->sfpp)[0];
                    111:   if (sfp) {
                    112:     cw->aux = (char *) mh_malloc(sfp->len+1);
                    113:     mh_outstr((char *)cw->aux,sfp->len+1,sfp);
                    114:   }
                    115:
                    116:   sfp = (rp->sfpp)[1];
                    117:   if (sfp) {
                    118:     cw->aux2 = (char *) mh_malloc(sfp->len+1);
                    119:     mh_outstr((char *)cw->aux2,sfp->len+1,sfp);
                    120:   }
                    121:   /* deallocate the memory */
                    122:   for (i=0; i<rp->size; i++) mh_fclose((rp->sfpp)[i]);
                    123:   mh_freeWorkArea();
                    124:   return(cw);
                    125: }
                    126: /* Cumulative probability distribution function of the first eigenvalue of
                    127:    Wishart matrix by Series */
                    128: struct cWishart *mh_cwishart_s(int m,int n,double beta[],double x0,
                    129:                               int approxDeg,double h, int dp, double x) {
1.6       takayama  130:   int modep[]={1,0,0};
                    131:   return(mh_cwishart_gen(m,n,beta,x0,approxDeg,h,dp,x,modep));
1.1       takayama  132: }
                    133:
                    134: /* Cumulative probability distribution function of the first eigenvalue of
                    135:    Wishart matrix by HGM */
                    136: struct cWishart *mh_cwishart_hgm(int m,int n,double beta[],double x0,
                    137:                                 int approxDeg, double h, int dp , double x)
                    138: {
1.6       takayama  139:   int modep[]={1,1,0};
                    140:   return(mh_cwishart_gen(m,n,beta,x0,approxDeg,h,dp,x,modep));
1.1       takayama  141: }
                    142:
                    143: #ifdef STANDALONE
                    144: main() {
                    145:   double beta[5]={1.0,2.0,3.0,4.0,5.0};
                    146:   struct cWishart *cw;
1.7     ! takayama  147:   struct SFILE *sfp;
        !           148:   char *s;
        !           149:   char str[1024];
        !           150:   double x;
1.1       takayama  151:   cw=mh_cwishart_hgm(3,5,beta,0.3,7,  0.01,1,10);
                    152:   if (cw != NULL) {
                    153:     printf("x=%lf, y=%lf\n",cw->x,(cw->f)[0]);
                    154:     /* printf("%s",(char *)cw->aux); */
1.4       takayama  155:   }
1.1       takayama  156:   cw=mh_cwishart_hgm(4,5,beta,0.3,7,  0.01,1,10);
                    157:   if (cw != NULL) {
                    158:     printf("x=%lf, y=%lf\n",cw->x,(cw->f)[0]);
1.7     ! takayama  159:     s = (char *)cw->aux;
1.1       takayama  160:     /* printf("%s",(char *)cw->aux); */
1.7     ! takayama  161:     sfp = mh_fopen(s,"r",0);
        !           162:     while (mh_fgets(str,1024,sfp)) {
        !           163:       sscanf(str,"%lg",&x); printf("%lg\n",x);
        !           164:     }
        !           165:     mh_fclose(sfp);
1.1       takayama  166:   }
                    167: }
                    168: main1() {
                    169:   double beta[5]={1.0,2.0,3.0,4.0,5.0};
                    170:   struct cWishart *cw;
                    171:   cw=mh_cwishart_s(3,5,beta,0.3,7,  0,0,0);
                    172:   if (cw != NULL) {
                    173:     printf("%s",(char *)cw->aux);
                    174:   }
                    175:   cw=mh_cwishart_s(4,5,beta,0.3,7,  0,0,0);
                    176:   if (cw != NULL) {
                    177:     printf("%s",(char *)cw->aux);
                    178:   }
                    179:   cw=mh_cwishart_s(5,5,beta,0.3,7,  0,0,0);
                    180:   if (cw != NULL) {
                    181:     printf("%s",(char *)cw->aux);
                    182:   }
                    183: }
                    184: #endif

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