SmartBuffer *reallocSB(SmartBuffer *sb, size_t incr) { SmartBuffer *sbNew; size_t i; if (sb == NULL) { sbNew = (SmartBuffer*) gblock((size_t) sizeof(SmartBuffer)); (sbNew -> buff) = cvector(0, incr - 1); (sbNew -> size) = incr; sbNew -> ptr = sbNew -> buff; } else { sbNew = (SmartBuffer*) gblock((size_t) sizeof(SmartBuffer)); (sbNew -> buff) = cvector(0, (sb -> size) + incr - 1); (sbNew -> size) = (sb -> size) + incr; sb -> ptr = sb -> buff; sbNew -> ptr = sbNew -> buff; for (i = 1; i <= sb -> size; i++) { *(sbNew -> ptr) = *(sb -> ptr); ++(sbNew -> ptr); ++(sb -> ptr); } freeSB(sb); } sbNew -> delim = ','; sbNew -> tokenCnt = 0; sbNew -> tokenSize = 0; sbNew -> token = NULL; return sbNew; }
void mpsqrt(unsigned char w[], unsigned char u[], unsigned char v[], int n, int m) { void mplsh(unsigned char u[], int n); void mpmov(unsigned char u[], unsigned char v[], int n); void mpmul(unsigned char w[], unsigned char u[], unsigned char v[], int n, int m); void mpneg(unsigned char u[], int n); void mpsdv(unsigned char w[], unsigned char u[], int n, int iv, int *ir); int i,ir,j,mm; float fu,fv; unsigned char *r,*s; r=cvector(1,n<<1); s=cvector(1,n<<1); mm=IMIN(m,MF); fv=(float) v[mm]; for (j=mm-1;j>=1;j--) { fv *= BI; fv += v[j]; } fu=1.0/sqrt(fv); for (j=1;j<=n;j++) { i=(int) fu; u[j]=(unsigned char) i; fu=256.0*(fu-i); } for (;;) { mpmul(r,u,u,n,n); mplsh(r,n); mpmul(s,r,v,n,IMIN(m,n)); mplsh(s,n); mpneg(s,n); s[1] -= 253; mpsdv(s,s,n,2,&ir); for (j=2;j<n;j++) { if (s[j]) { mpmul(r,s,u,n,n); mpmov(u,&r[1],n); break; } } if (j<n) continue; mpmul(r,u,v,n,IMIN(m,n)); mpmov(w,&r[1],n); free_cvector(s,1,n<<1); free_cvector(r,1,n<<1); return; } }
SmartBuffer *parseLineSB(FILE *fPtr, char delim, size_t sbSize) { size_t size; SmartBuffer *sb = readLineSB(fPtr, sbSize); (sb -> delim) = delim; size = 0; while((*(sb -> ptr)) != '\0') { if ((*(sb -> ptr)) == delim) { (sb -> tokenCnt) ++; if (size > (sb -> tokenSize)) { (sb -> tokenSize) = size; } size = 0; } else { ++ size; } ++(sb -> ptr); } if ((sb -> tokenCnt) > 0) { (sb -> tokenCnt) ++; (sb -> token) = cvector(0, sb -> tokenSize); } sb -> ptr = sb -> buff; return sb; }
void mpinv(unsigned char u[], unsigned char v[], int n, int m) { void mpmov(unsigned char u[], unsigned char v[], int n); void mpmul(unsigned char w[], unsigned char u[], unsigned char v[], int n, int m); void mpneg(unsigned char u[], int n); unsigned char *rr,*s; int i,j,maxmn,mm; float fu,fv; maxmn=IMAX(n,m); rr=cvector(1,1+(maxmn<<1)); s=cvector(1,maxmn); mm=IMIN(MF,m); fv=(float) v[mm]; for (j=mm-1;j>=1;j--) { fv *= BI; fv += v[j]; } fu=1.0/fv; for (j=1;j<=n;j++) { i=(int) fu; u[j]=(unsigned char) i; fu=256.0*(fu-i); } for (;;) { mpmul(rr,u,v,n,m); mpmov(s,&rr[1],n); mpneg(s,n); s[1] -= 254; mpmul(rr,s,u,n,n); mpmov(u,&rr[1],n); for (j=2;j<n;j++) if (s[j]) break; if (j==n) { free_cvector(s,1,maxmn); free_cvector(rr,1,1+(maxmn<<1)); return; } } }
CHARVECTOR *new_charvector( long nh) { CHARVECTOR *m; m=(CHARVECTOR *)malloc(sizeof(CHARVECTOR)); if (!m) t_error("allocation failure in CHARVECTOR()"); m->isdynamic=isDynamic; m->nl=NL; m->nh=nh; m->co=cvector(m->nl,nh); return m; }
void int_split(char *s,int *nums,int num) { int cnt=0,i; char *p; p=cvector(0,MAXLINE-1); while(*s !='\0'&&cnt<num) { while(isspace(*s)) ++s; //skip white space if(*s!='\0') //found a word { i=0; while(!isspace(*s)&&*s!='\0') //translate the word into float number { p[i++]=*s++;} p[i]='\0'; nums[cnt++]=atoi(p); } } free_cvector(p,0,MAXLINE-1); }
int main(void) { int k; unsigned long i,j,lc,lcode=MAXLINE,n,nch,nrad,nt,nfreq[257],tmp,zero=0; unsigned char *code,mess[MAXLINE],ness[MAXLINE]; arithcode acode; FILE *fp; code=cvector(0,MAXLINE); acode.ilob=lvector(1,NWK); acode.iupb=lvector(1,NWK); acode.ncumfq=lvector(1,MC+2); if ((fp = fopen("text.dat","r")) == NULL) nrerror("Input file text.dat not found.\n"); for (j=1;j<=256;j++) nfreq[j]=0; while ((k=getc(fp)) != EOF) { if ((k -= 31) >= 1) nfreq[k]++; } fclose(fp); nch=96; nrad=256; /* here is the initialization that constructs the code */ arcmak(nfreq,(int)nch,(int)nrad,&acode); /* now ready to prompt for lines to encode */ for (;;) { printf("Enter a line:\n"); if (gets((char *)&mess[1]) == NULL) break; n=strlen((char *)&mess[1]); /* shift from 256 character alphabet to 96 printing characters */ for (j=1;j<=n;j++) mess[j] -= 32; /* message initialization */ lc=1; arcode(&zero,&code,&lcode,&lc,0,&acode); /* here we arithmetically encode mess(1:n) */ for (j=1;j<=n;j++) { tmp=mess[j]; arcode(&tmp,&code,&lcode,&lc,1,&acode); } /* message termination */ arcode(&nch,&code,&lcode,&lc,1,&acode); printf("Length of line input, coded= %lu %lu\n",n,lc-1); /* here we decode the message, hopefully to get the original back */ lc=1; arcode(&zero,&code,&lcode,&lc,0,&acode); for (j=1;j<=lcode;j++) { arcode(&i,&code,&lcode,&lc,-1,&acode); if (i == nch) break; else ness[j]=(unsigned char)i; } if (j > lcode) nrerror("Arith. coding: Never get here"); nt=j-1; printf("Decoded output:\n"); for (j=1;j<=nt;j++) printf("%c",(char)(ness[j]+32)); printf("\n"); if (nt != n) printf("Error ! j decoded != n input.\n"); } free_cvector(code,0,MAXLINE); free_lvector(acode.ncumfq,1,MC+2); free_lvector(acode.iupb,1,NWK); free_lvector(acode.ilob,1,NWK); printf("Normal completion\n"); return 0; }
/* * Function read_init is used to read in the initial values of selfing rates (S) for MCMC updating * Input argument: initialfilename is the directory of the file containing the initial values, * if it is NULL,then using random number generator to generate the INIT structure * chainnum is the number of MCMC chains, determining the number of sets of initial values * popnum is the number of subpopulation assumed * chainnum and popnum determines the dimensions of the "initd" element of the INIT struture * Output argument: this application returns an INIT structure, which contains the "chainnum" sets of initial values for F per subpopulation * The data in the file should be ranged as: * >chain_name1 each set of the initial values should begin with ">S" * num1 num2.. * * >chain_name2 * .... */ INIT read_init(char *initialfilename,int chainnum,int popnum,long update,long burnin,int thinning) { //char sign='#'; int i,j,temp,cnt_chn=0; FILE *initfp; char *line; INIT initial; initial.chainnum=chainnum; initial.update=update; initial.burnin=burnin; initial.thinning=thinning; initial.popnum=popnum; initial.initd=matrix(0,chainnum-1,0,popnum-1); initial.name_len=ivector(0,chainnum-1); initial.chn_name=cmatrix(0,chainnum-1,0,MAXLEN-1); if(initialfilename==NULL) { for(i=0;i<chainnum;i++) { for(j=0;j<popnum;j++) initial.initd[i][j]=ran1(); strcpy(initial.chn_name[i],"Chain#"); strcat(initial.chn_name[i],int_to_string(i+1)); initial.name_len[i]=strlen(initial.chn_name[i])+1; initial.chn_name[i][initial.name_len[i]-1]='\0'; } } else{ if((initfp=fopen(initialfilename,"r"))==NULL) { nrerror("Cannot open inital file!");} line=cvector(0,MAXLINE-1); cnt_chn=0; for(i=0;i<chainnum&&(!feof(initfp));i++) { while(!feof(initfp)) { fgets(line,MAXLINE,initfp); if(line[0]=='>') break; } for(j=1;j<strlen(line)&&line[j]!='\n';j++) { initial.chn_name[i][j-1]=line[j]; } initial.chn_name[j]='\0'; //printf("%s\n",initial.chn_name[i]); initial.name_len[i]=j; fgets(line,MAXLINE,initfp); temp=word_cnt(line); if(temp!=popnum) { nrerror("The number of initial values for selfing rates is not equal the number of subpopulation assumed!\n"); } word_split(line,initial.initd[cnt_chn],popnum); cnt_chn++; } if(cnt_chn<=chainnum) { for(i=cnt_chn;i<chainnum;i++) { for(j=0;j<popnum;j++) initial.initd[i][j]=ran1(); strcpy(initial.chn_name[i],"Chain#"); strcat(initial.chn_name[i],int_to_string(i+1)); initial.name_len[i]=strlen(initial.chn_name[i])+1; initial.chn_name[i][initial.name_len[i]-1]='\0'; } } if(cnt_chn>chainnum) { nrerror("The number of chain starting points is greater than the number of chains!\n"); } fclose(initfp); free_cvector(line,0,MAXLINE-1); } /*for(i=0;i<chainnum;i++) { for(j=0;j<popnum;j++) fprintf(stdout,"%f\t",initial.initd[i][j]); fprintf(stdout,"\n"); }*/ return(initial); }