Esempio n. 1
0
File: latools.c Progetto: cran/Bmix
int* irep(int val, int n)
{
  int i;
  int *v = new_ivec(n);
  for(i=0; i<n; i++) v[i] = val;
  return v;
}
Esempio n. 2
0
File: latools.c Progetto: cran/Bmix
int* new_izero(int n)
{
  int i;
  int *v = new_ivec(n);
  for(i=0; i<n; i++) v[i] = 0;
  return v;
}
Esempio n. 3
0
File: latools.c Progetto: cran/Bmix
void la_dgesv(int Arow, int Bcol, double **A, double **B)
{
  int info;
  int *ip = new_ivec(Arow);  /* pivot indices which define P; 
			      row i was interchanged with row ip[i]*/
  dgesv_(&Arow, &Bcol, *A, &Arow, ip, *B, &Arow, &info);
  assert(info==0); // if info = -i, i'th arg is wrong.  if info > 0, A is not pos-def.
}
Esempio n. 4
0
File: latools.c Progetto: cran/Bmix
int* new_iseq(int from, int to)
{
  int n,i;
  
  assert( from <= to);
  n =  (to - from) + 1;    
  int *v = new_ivec(n);
  v[0] = from;
  for(i=1; i<n; i++) v[i] = v[i-1] + 1;
  return v;
}
Esempio n. 5
0
int genrmt(char *infile, char *outfile)
{
  int i,j;
  FILE *fp;
  double x,t0,t1;
  char *cbuf,*fext;

  /* open file */
  switch(seqmode) {
  case SEQ_MOLPHY: fext=fext_molphy; break;
  case SEQ_PAML: fext=fext_paml; break;
  case SEQ_PAUP: fext=fext_paup; break;
  case SEQ_PUZZLE: fext=fext_puzzle; break;
  case SEQ_PHYML: fext=fext_phyml; break;
  case SEQ_MT: 
  default: fext=fext_mt; break;
  }
  if(infile) {
    fp=openfp(infile,fext,"r",&cbuf);
    printf("\n# reading %s",cbuf);
  } else {
    fp=STDIN;
    printf("\n# reading from stdin");
  }

  /* read file */
  mm=nn=0;
  switch(seqmode) {
  case SEQ_MOLPHY: 
    datmat = fread_mat_lls(fp, &mm, &nn); break;
  case SEQ_PAML: 
    datmat = fread_mat_lfh(fp, &mm, &nn); break;
  case SEQ_PAUP: 
    datmat = fread_mat_paup(fp, &mm, &nn); break;
  case SEQ_PUZZLE: 
    datmat = fread_mat_puzzle(fp, &mm, &nn); break;
  case SEQ_PHYML: 
    datmat = fread_mat_phyml(fp, &mm, &nn); break;
  case SEQ_MT: 
  default: 
    datmat = fread_mat(fp, &mm, &nn); break;  
  }
  if(infile) {fclose(fp);  FREE(cbuf);}
  printf("\n# M:%d N:%d",mm,nn);

  /* allocating buffers */
  datvec=new_vec(mm);
  bn=new_ivec(kk); rr1=new_vec(kk);

  /* calculate the log-likelihoods */
  for(i=0;i<mm;i++) {
    x=0; for(j=0;j<nn;j++) x+=datmat[i][j];
    datvec[i]=x;
  }
  
  /* calculate scales */
  for(i=0;i<kk;i++) {
    bn[i]=(int)(rr[i]*nn); /* sample size for bootstrap */
    rr1[i]=(double)bn[i]/nn; /* recalculate rr for integer adjustment */
  }

  /* open out file */
  if(outfile) {
    /* vt ascii write to file */
    fp=openfp(outfile,fext_vt,"w",&cbuf);
    printf("\n# writing %s",cbuf);
    fwrite_vec(fp,datvec,mm);
    fclose(fp); FREE(cbuf);
    /* rmt binary write to file */
    fp=openfp(outfile,fext_rmt,"wb",&cbuf);
    printf("\n# writing %s",cbuf);
    fwrite_bvec(fp,datvec,mm);
    fwrite_bvec(fp,rr1,kk);
    fwrite_bivec(fp,bb,kk);
    fwrite_bi(fp,kk);
  } else {
    /* rmt ascii write to stdout */
    printf("\n# writing to stdout");
    printf("\n# OBS:\n"); write_vec(datvec,mm);
    printf("\n# R:\n"); write_vec(rr1,kk);
    printf("\n# B:\n"); write_ivec(bb,kk);
    printf("\n# RMAT:\n");
    printf("%d\n",kk);
  }


  /* generating the replicates by resampling*/
  for(i=j=0;i<kk;i++) j+=bb[i];
  printf("\n# start generating total %d replicates for %d items",j,mm);
  fflush(STDOUT);
  t0=get_time();

  for(i=0;i<kk;i++) {
    repmat=new_lmat(mm,bb[i]);
    scaleboot(datmat,repmat,mm,nn,bn[i],bb[i]);
    if(outfile) {
      fwrite_bmat(fp,repmat,mm,bb[i]);
      putdot();
    } else {
      printf("\n## RMAT[%d]:\n",i); write_mat(repmat,mm,bb[i]);
    }
    free_lmat(repmat,mm);
  }

  t1=get_time();
  printf("\n# time elapsed for bootstrap t=%g sec",t1-t0);

  if(outfile) {
    fclose(fp); FREE(cbuf);
  }

  /* freeing buffers */
  free_vec(bn); free_vec(rr1); free_vec(datvec); free_mat(datmat);

  return 0;
}
Esempio n. 6
0
File: latools.c Progetto: cran/Bmix
int* new_dup_ivec(int *v, int n)
{
  int* iv_new = new_ivec(n);
  copy_ivec(iv_new, v, n);
  return iv_new;
}
Esempio n. 7
0
int main() {
	int i;
	ivec_t* vec = new_ivec(100);
	for (i = 0; i < 10000; ++i) {
		assert(ivec_push_back(vec, i));
	}

	printf("size = %u, empty = %d, capacity = %u\n", ivec_size(vec), ivec_empty(vec), ivec_capacity(vec));
	printf("popback = %d\n", ivec_pop_back(vec));
	printf("size = %u, empty = %d, capacity = %u\n", ivec_size(vec), ivec_empty(vec), ivec_capacity(vec));

	int* d = ivec_data(vec);
	for (i = 0; i < 9999; ++i) {
		assert(*(d+i) == i && i == ivec_at(vec, i));
	}

	printf("resize to 10, and shrink_to_fit\n");
	ivec_resize_default(vec, 10);
	ivec_shrink_to_fit(vec);
	printf("size = %u, empty = %d, capacity = %u\n", ivec_size(vec), ivec_empty(vec), ivec_capacity(vec));
	
	char buf[1000];
	ivec_dump(buf, 1000, vec);
	printf("%s\n", buf);

	printf("resize to 15\n", ivec_resize_default(vec, 15));
	printf("size = %u, empty = %d, capacity = %u\n", ivec_size(vec), ivec_empty(vec), ivec_capacity(vec));
	ivec_dump(buf, 1000, vec);
	printf("%s\n", buf);

	ivec_destroy(vec);

	svec_t* svec = new_svec(100);
	for (i = 0; i < 100; ++i) {
		snprintf(buf, 1000, "%d", i);
		assert(svec_push_back(svec, buf));
	}

	printf("size = %u, empty = %d, capacity = %u\n", svec_size(svec), svec_empty(svec), svec_capacity(svec));
	char* ret = svec_pop_back(svec);
	printf("popback = %s\n", ret);
	free(ret);
	printf("size = %u, empty = %d, capacity = %u\n", svec_size(svec), svec_empty(svec), svec_capacity(svec));

	char** s = svec_data(svec);
	for (i = 0; i < 99; ++i) {
		snprintf(buf, 1000, "%d", i);
		assert(!strcmp(*(s+i), buf) && !strcmp(buf, svec_at(svec, i)));
	}

	printf("resize to 10, and shrink to fit\n");
	svec_resize_default(svec, 10);
	svec_shrink_to_fit(svec);
	printf("size = %u, empty = %d, capacity = %u\n", svec_size(svec), svec_empty(svec), svec_capacity(svec));
	
	svec_dump(buf, 1000, svec);
	printf("%s\n", buf);

	printf("resize to 15\n", svec_resize_default(svec, 15));
	printf("size = %u, empty = %d, capacity = %u\n", svec_size(svec), svec_empty(svec), svec_capacity(svec));
	svec_dump(buf, 1000, svec);
	printf("%s\n", buf);

	svec_destroy(svec);

	return 0;
}