Example #1
0
double checkReactions(gsl_vector *haz, gsl_vector *residuals, gsl_vector *fast_params, 
                      double deltat,  gsl_vector *sps, gsl_matrix *PostPre)
{
  int i;
  double small_residual = -1000000.0;

  double total_hazard = 0.0;
  for(i=0; i < fast_params->size; i++) {
    if(checkHazard(haz, PostPre, sps, i, deltat) && 
       checkSpecies(PostPre, sps, i))  {
      VSET(residuals, i, small_residual);
    } else if(VGET(residuals, i) < (small_residual + 10000))  {
      total_hazard += VGET(haz, i);
      VSET(residuals, i, log(gsl_ran_flat(r, 0.0, 1.0)));
      VSET(fast_params, i, 0.0);
      //      running_totals1[i]++;
    } else {
      total_hazard += VGET(haz, i);
      VSET(fast_params, i, 0.0);
      //      running_totals1[i]++;
    }
    //printf("%d ", running_totals1[i]);
  }
  //  printf("\n");
  return total_hazard;        
}
Example #2
0
static
void
VI_qsortmag_i(vsip_vview_i *v, vsip_index left, vsip_index right, vsip_vview_vi *vi){
   vsip_index i,last;
   vsip_index iright;
   if(left >= right){
      return;
   } else {
      iright = (vsip_index)(left+right)/2;
   }
   SWAP(v,left, iright);
   if(vi) SWAP_VI(vi,left,iright);
   last = left;
   for(i=left+1; i<= right; i++){
      if(abs(VGET(v,i)) < abs(VGET(v,left))){
         last++;
         SWAP(v,last,i);
         if(vi) SWAP_VI(vi,last,i);
      }
   }
   SWAP(v,left,last);
   if(vi) SWAP_VI(vi,left,last);
   if(last > 0) VI_qsortmag_i(v,left,last-1,vi);
   VI_qsortmag_i(v,last+1,right,vi);
   return;
}
Example #3
0
static VALUE 
rb_cpApplyDampedSpring(VALUE klass, VALUE a, VALUE b, 
  VALUE anchr1, VALUE anchr2, VALUE rlen, VALUE k, VALUE dmp, VALUE dt)
{
  cpApplyDampedSpring(BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2), 
  NUM2DBL(rlen), NUM2DBL(k), NUM2DBL(dmp), NUM2DBL(dt));
  return klass;
} 
Example #4
0
/// Returns the fraction along the segment query the cpBB is hit. Returns INFINITY if it doesn't hit.
static VALUE
rb_cpBBSegmentQuery(VALUE self, VALUE va, VALUE vb) {
  cpVect *a, *b;
  a = VGET(va);
  b = VGET(vb);
  if(a && b) {
    return DBL2NUM(cpBBSegmentQuery(*BBGET(self), *a, *b));
  }
  rb_raise(rb_eArgError, "query requires 2 Vect2 arguments");
  return Qnil; 
}
Example #5
0
/*
  Hybrid method
*/
void updateHazard(gsl_vector *haz, gsl_vector *params, gsl_vector *sps)
{
  VSET(haz, 0, VGET(params, 0));
  VSET(haz, 1, VGET(params, 1));
  VSET(haz, 2, VGET(params, 2)*VGET(sps, 0));        
  VSET(haz, 3, VGET(params, 3)*VGET(sps, 1));        
  VSET(haz, 4, VGET(params, 4)*VGET(sps, 0)*VGET(sps, 1)); 
}           
Example #6
0
static VALUE
rb_cpSegmentInitialize(VALUE self, VALUE body, VALUE a, VALUE b, VALUE r)
{
	cpSegmentShape *seg = (cpSegmentShape *)SHAPE(self);
	
	cpSegmentShapeInit(seg, BODY(body), *VGET(a), *VGET(b), NUM2DBL(r));
	seg->shape.data = (void *)self;
	seg->shape.collision_type = Qnil;

	rb_ivar_set(self, id_body, body);
	
	return self;
}
Example #7
0
static VALUE
rb_momentForPoly(VALUE self, VALUE m, VALUE arr, VALUE offset)
{
	Check_Type(arr, T_ARRAY);
	int numVerts = RARRAY_LEN(arr);
	cpVect verts[numVerts];
	
	for(int i=0; i<numVerts; i++)
		verts[i] = *VGET(RARRAY_PTR(arr)[i]);
	
	cpFloat inertia = cpMomentForPoly(NUM2DBL(m), numVerts, verts, *VGET(offset));
	return rb_float_new(inertia);
}
Example #8
0
/*A bit inefficient to have two separate functions. Just easier
 * Corresponds to equation 9
 */
int checkHazard(gsl_vector *haz,gsl_matrix *PostPre, gsl_vector *sps, 
                int reaction_no, double deltat) 
{
  int i;
  double cond,  epsilon=0.25;

  for(i=0; i<sps->size; i++) {
    cond =  fabs(MGET(PostPre, reaction_no, i));
    cond *= max(1.0, VGET(haz, reaction_no)*deltat);
    if(cond > epsilon*VGET(sps, i)) return(0);
  }
  return(1);
}
Example #9
0
/* MCMC Var-Cov tuning matrix */
st_mcmc_update *initMCMCUpdate(char *dir)
{
  st_mcmc_update *mcmc_update;

  dir = addInputPath(dir);
  char *tuning_file = createPath(dir, "tuning.csv");
  gsl_matrix *tuning = readMatrix(tuning_file);

  char *fixed_file = createPath(dir, "fixed.csv");
  gsl_vector *fixed = readVector(fixed_file);

  if(tuning->size1 != tuning->size2 || tuning->size1 != fixed->size) {
    printf("Error in initMCMCUpdate\n");
    exit(GSL_FAILURE);
  }
  gsl_vector *z = gsl_vector_alloc(fixed->size);

  mcmc_update = (st_mcmc_update *) malloc(sizeof(st_mcmc_update));
  mcmc_update->tuning = tuning;
  mcmc_update->fixed=fixed;
  mcmc_update->z = z;
  mcmc_update->always_accept = 1;

  int i;
  for(i=0; i<fixed->size; i++) 
    mcmc_update->always_accept *= (int) VGET(mcmc_update->fixed, i);

  return(mcmc_update);
}
Example #10
0
/**
 * Returns a vector of the vo-th parameter, built from
 * the list
 * @param kl
 * @param pl
 * @param el
 * @return 
 */
gsl_vector* KL_getPars(const ok_list* kl, const int vo) {
    gsl_vector* v = gsl_vector_alloc(kl->size);
    
    for (int i = 0; i < kl->size; i++)
        VSET(v, i, VGET(kl->kernels[i]->params, vo));
    
    return v;
}
Example #11
0
void diffusion2(gsl_vector *mean_cand, gsl_vector *param1, double ddeltat, gsl_matrix *disp_mat)
{
  double v1,v2,v12,k1,k2,k3,k4,k5,r1,r2;
  k1=VGET(param1, 0);
  k2=VGET(param1, 1);
  k3=VGET(param1, 2);
  k4=VGET(param1, 3);
  k5=VGET(param1, 4);
  r1=VGET(mean_cand, 0);
  r2=VGET(mean_cand, 1);

  v1 = ddeltat*(k1 + k3*r1 + k5*r1*r2);
  v2 = ddeltat*(k2 + k4*r2 + 400*k5*r1*r2);/*Change: 20^2*k5...*/
  v12 = -ddeltat*(20*k5*r1*r2);/*20*k5*...*/
  if((k1+k2+k3+k4) < 0.00000001) {
      postive_definite = 0;
      /*printf("#######################\n");*/
    }
  else
    postive_definite = 1;
  MSET(disp_mat,0,0,v1);
  MSET(disp_mat,1,1,v2);
  MSET(disp_mat,0,1,v12);
  MSET(disp_mat,1,0,v12);
}
Example #12
0
void mvn_sample(gsl_vector *mean_cand, gsl_matrix *var)
{
  /* Takes a mean vec, mean and var matrix, 
   * var and gives vector of MVN(mean,var) realisations, x 
   */
  int i, j;
  int dimen = var -> size1;
  double value;
  gsl_matrix *disp;
  gsl_vector *ran;
  gsl_matrix *fast_species;
  
  fast_species = gsl_matrix_alloc(2, 2);
  gsl_matrix_set_identity(fast_species);
  
  for(i=0;i<dimen; i++) {
    if(MGET(var, i, i) <0.00000000001) {
      MSET(var, i, i, 1.0);
      MSET(fast_species, i, i, 0.0);
    }
  }
  
  disp = gsl_matrix_alloc(2, 2);
  ran = gsl_vector_alloc(2);
  gsl_matrix_memcpy(disp, var);
  if(postive_definite == 1) {
    gsl_linalg_cholesky_decomp(disp);
    for(i=0;i<dimen;i++) {
      for (j=i+1;j<dimen;j++) {
        MSET(disp,i,j,0.0);
      }
    }
  }else{
    value = pow(MGET(disp, 0 ,0), 0.5);
    gsl_matrix_set_identity(disp);
    MSET(disp, 0,0, value);
    MSET(disp, 1,1, value);       
  }

  for (j=0;j<dimen;j++) {
    VSET(ran,j,gsl_ran_gaussian(r,1.0));
  }

  /*remove update from slow species*/
  gsl_matrix_mul_elements(disp, fast_species);
    
  /*Add noise to mean cand*/
  gsl_blas_dgemv(CblasNoTrans,1.0, disp, ran, 1.0, mean_cand);
  for(i=0; i<2; i++)  {
    if(VGET(mean_cand,i)<=0.0001 && MGET(fast_species, i, i) > 0.000001)
      VSET(mean_cand,i,0.0001);
  }
  gsl_vector_free(ran);
  gsl_matrix_free(disp);
  gsl_matrix_free(fast_species);
}
Example #13
0
File: main.c Project: Julow/rtv1
static void		render(t_main *main, uint32_t scene, uint32_t camera)
{
	uint32_t		tmp;

	main->current_scene = (scene + main->scenes.length) % main->scenes.length;
	tmp = VGET(t_scene, main->scenes, main->current_scene).cameras.length;
	main->current_camera = (camera + tmp) % tmp;
	mlx_clear_window(main->mlx, main->win.win_id);
	mlx_string_put(main->mlx, main->win.win_id, 15, 25, 0xFF0000, "Loading...");
	main->should_render = true;
}
Example #14
0
void drift2(gsl_vector *mean_cand, gsl_vector *param1, double ddeltat)
{
  double m1,m2,k1,k2,k3,k4,k5,r1,r2;
  k1=VGET(param1,0);
  k2=VGET(param1,1);
  k3=VGET(param1,2);
  k4=VGET(param1,3);
  k5=VGET(param1,4);
  r1=VGET(mean_cand,0);
  r2=VGET(mean_cand,1);
    
  m1=ddeltat*(k1-k3*r1-k5*r1*r2);
  m2=ddeltat*(k2-k4*r2+20*k5*r1*r2);/*Change to 20*k5*r1*r2*/
    
  VSET(mean_cand, 0, m1+VGET(mean_cand, 0));
  VSET(mean_cand, 1, m2+VGET(mean_cand, 1));
}
Example #15
0
static VALUE
rb_cpPolyInitialize(VALUE self, VALUE body, VALUE arr, VALUE offset)
{
	cpPolyShape *poly = (cpPolyShape *)SHAPE(self);
	
	Check_Type(arr, T_ARRAY);
	int numVerts = RARRAY_LEN(arr);
	VALUE *ary_ptr = RARRAY_PTR(arr);
	cpVect verts[numVerts];
	
	for(int i=0; i<numVerts; i++)
		verts[i] = *VGET(ary_ptr[i]);
	
	cpPolyShapeInit(poly, BODY(body), numVerts, verts, *VGET(offset));
	poly->shape.data = (void *)self;
	poly->shape.collision_type = Qnil;

	rb_ivar_set(self, id_body, body);
	
	return self;
}
Example #16
0
/*Corresponds to equation 10*/
int checkSpecies(gsl_matrix *PostPre, gsl_vector *sps, int reaction_no)
{
  int i;
  double cond, epsilon_st, N_st;

  N_st = 15; epsilon_st = 0.25;
  for(i=0; i<sps->size; i++) {
    cond = fabs(MGET(PostPre, reaction_no, i))*N_st;
    if(cond > (epsilon_st*VGET(sps, i))) return(0);
  }
  return(1);
}
Example #17
0
static VALUE
rb_cpCentroidForPoly(VALUE self,  VALUE arr) {
  Check_Type(arr, T_ARRAY);
  long numVerts   = RARRAY_LEN(arr);
  VALUE *ary_ptr = RARRAY_PTR(arr);
  cpVect verts[numVerts];

  for(long i = 0; i < numVerts; i++)
    verts[i] = *VGET(ary_ptr[i]);

  return VNEW(cpCentroidForPoly(numVerts, verts));
}
Example #18
0
int
gc_set_config_repl(generic_cache_t *gc, const char *repl)
{
    repl_interface_t *ri;
    int i = 0;

    if (!VLEN(repl_policies)) {
        SIM_log_error(&gc->log, GC_Log_Repl,
                      "Cache has no replacement policies registered.");
        return -1;
    }

    ri = VGET(repl_policies, i);
    while (ri != NULL) {
        if (strcmp(repl, ri->get_name()) == 0) {
            MM_FREE(gc->config.repl_data);
            memcpy(&gc->config.repl_fun, ri, sizeof(*ri));
            gc->config.repl_data =
                gc->config.repl_fun.new_instance(gc);
            gc->config.repl_fun.update_config(
                gc->config.repl_data, gc);
            return 0;
        }
        i++;
        ri = VGET(repl_policies, i);
    }

    SIM_log_info(1, &gc->log, GC_Log_Repl,
                 "replacement: possible values are :");

    i = 0;
    ri = VGET(repl_policies, i);
    while (ri != NULL) {
        SIM_log_info(1, &gc->log, GC_Log_Repl, "   %s",
                     ri->get_name());
        i++;
        ri = VGET(repl_policies, i);
    }
    return -1;
}
Example #19
0
static VALUE
rb_cpCircleInitialize(VALUE self, VALUE body, VALUE radius, VALUE offset)
{
	cpCircleShape *circle = (cpCircleShape *)SHAPE(self);
	
	cpCircleShapeInit(circle, BODY(body), NUM2DBL(radius), *VGET(offset));
	circle->shape.data = (void *)self;
	circle->shape.collision_type = Qnil;

	rb_ivar_set(self, id_body, body);
	
	return self;
}
Example #20
0
static VALUE
rb_cpAreaForPoly(VALUE self, VALUE arr) {
  Check_Type(arr, T_ARRAY);
  long numVerts   = RARRAY_LEN(arr);
  VALUE *ary_ptr = RARRAY_PTR(arr);
  cpVect verts[numVerts];

  for(long i = 0; i < numVerts; i++)
    verts[i] = *VGET(ary_ptr[i]);

  cpFloat area   = cpAreaForPoly(numVerts, verts);
  return rb_float_new(area);
}
Example #21
0
void vsip_vfreqswap_f(const vsip_vview_f *a){
   vsip_length N=a->length;
   vsip_length n;
   vsip_index i;
   if(N > 1){
      if(N & 1){ /* odd */
         vsip_scalar_f t = VGET(a,N-1);
         n = N/2 - 1;
         for(i=0; i<=n; i++)
            swapOdd(a,n-i,N-i-2);
         VPUT(a,N/2,t);
      } else { /* even */
         n=N/2;
         for(i=0; i<N/2; i++)
            swapEven(a,i,i+n);      
      }
   }
}
Example #22
0
static VALUE
rb_cpBBInitialize(int argc, VALUE *argv, VALUE self) {
  VALUE l, b, r, t;
  cpBB *bb = BBGET(self);

  rb_scan_args(argc, argv, "04", &l, &b, &r, &t);
  // initialize as a circle bounds box if ony 2 params
  if (NIL_P(r)) {
    if(NIL_P(l)) { 
       (*bb) = cpBBNew(0, 0, 1, 1); // unit box. 
    } else {
       cpVect * p =  VGET(l);
       (*bb) = cpBBNewForCircle(*p, NUM2DBL(b));
    }   
  } else {
   (*bb)  = cpBBNew(NUM2DBL(l), NUM2DBL(b), NUM2DBL(r), NUM2DBL(t));
  }  
  return self;
}
Example #23
0
/**
 * Flattens a list into a double array. Used by the R wrapper.
 * @param kl
 * @param out
 */
void KL_to_ptr(const ok_list* kl, double* out) {
    int idx = 0;
    int np = KL_getNplanets(kl);
    
    for (int row = 0; row < kl->size; row++) {
        for (int i = 1; i <= np; i++) {
            for (int j = 0; j < ALL_ELEMENTS_SIZE; j++)
                out[idx++] = MGET(kl->kernels[row]->elements, i, j);
        }
        
        
        for (int i = 0; i < PARAMS_SIZE; i++)
            out[idx++] = VGET(kl->kernels[row]->params, i);
        
        out[idx++] = kl->kernels[row]->merit;
        out[idx++] = kl->kernels[row]->merit_pr;
        out[idx++] = kl->kernels[row]->merit_li;
    }
}
Example #24
0
/**
 * Get a summary statistic for the parameters; for instance,
 * the median value calculated over all the elements of the list.
 * @param kl List
 * @param what Can be one of: STAT_MEAN, STAT_MEDIAN, STAT_STDDEV, STAT_MAD. 
 * @return A vector whose entries are the summary statistic for the 
 * corresponding orbital parameter.
 */
gsl_vector* KL_getParsStats(const ok_list* kl, const int what) {

    gsl_vector* v = gsl_vector_alloc(kl->size);
    gsl_vector* ret = gsl_vector_calloc(PARAMS_SIZE + 1);
    
    
    for (int j = 0; j < PARAMS_SIZE + 1; j++) {
        if (j == PARAMS_SIZE)
                for (int n = 0; n < kl->size; n++) {
                        VSET(v, n, kl->kernels[n]->merit);
                }
        else
                for (int n = 0; n < kl->size; n++) {
                        VSET(v, n, VGET(kl->kernels[n]->params, j));
                }

        switch (what) {
            case STAT_MEAN:
                VSET(ret, j, gsl_stats_mean(v->data, 1, v->size));
                break;
            case STAT_STDDEV:
                VSET(ret, j, gsl_stats_sd(v->data, 1, v->size));
                break;
            case STAT_MEDIAN:
                gsl_sort_vector(v);
                VSET(ret, j, gsl_stats_median_from_sorted_data(v->data, 1, v->size));
                break;
            case STAT_MAD:
                gsl_sort_vector(v);
                double med = gsl_stats_median_from_sorted_data(v->data, 1, v->size);
                VSET(ret, j, 1.4826 * ok_mad(v->data, v->size, med));
                break;
            default:
                // percentiles
                gsl_sort_vector(v);
                VSET(v , j, gsl_stats_quantile_from_sorted_data(v->data, 1, v->size, (double)(what)/100.));
        };
    };
            
    gsl_vector_free(v);
    return ret;
}
Example #25
0
/*SDE solution*/
void sde(gsl_vector *par, double tlen, gsl_vector *sps)
{

  double ddeltat, t=0;
  double total_par = 0.0;
  int i;
  for(i=0; i< par->size; i++)
    total_par += VGET(par, i);

  if(total_par < 0.000000000001)
    return;
  gsl_matrix *disp_mat = gsl_matrix_alloc(2, 2);

  while((tlen-t) > 0.000000001) {
    ddeltat = get_min(tlen-t, 0.005);/* SDE time step */
    diffusion2(sps, par, ddeltat, disp_mat);  
    drift2(sps, par, ddeltat);
    mvn_sample(sps, disp_mat);
    t += ddeltat;
  }
  gsl_matrix_free(disp_mat);
}
Example #26
0
void KL_fprintf(const ok_list* kl, FILE* out, const char* fmt, const char* lfmt) {
    lfmt = (lfmt != NULL ? lfmt : "%10s%d");
    
    int np = MROWS(kl->kernels[0]->elements)-1;
    int vo = PARAMS_SIZE;
    
    fprintf(out, "# Planets = %d\n", np);
    fprintf(out, "# Trials = %d\n", kl->size);
    fprintf(out, "# Mstar = %e\n", K_getMstar(kl->prototype));
    fprintf(out, "# Epoch = %e\n", K_getEpoch(kl->prototype));
    
    
    
    for (int i = 0; i < ALL_ELEMENTS_SIZE; i++)
        for (int j = 1; j <= np; j++)
            fprintf(out, lfmt, ok_all_orb_labels[i], j);
    for (int i = 0; i < vo; i++)
        fprintf(out, lfmt, "PARAM", i);
    fprintf(out, "\n");
    
    for (int m = 0; m < kl->size; m++) {

        gsl_matrix* ae = kl->kernels[m]->elements;
        for (int i = 0; i < ALL_ELEMENTS_SIZE; i++)
            for (int j = 1; j <= np; j++)
                fprintf(out, fmt, MGET(ae, j, i));
        
        for (int i = 0; i < vo; i++)
            fprintf(out, fmt, VGET(kl->kernels[m]->params, i));
        
        fprintf(out, fmt, kl->kernels[m]->merit);
        fprintf(out, fmt, kl->kernels[m]->merit_pr);
        fprintf(out, fmt, kl->kernels[m]->merit_li);
        fprintf(out, fmt, kl->kernels[m]->tag);
        fprintf(out, " \n");
        
    }
    
}
Example #27
0
static void swapOdd(const vsip_vview_f *a, vsip_index i, vsip_index j){
   vsip_scalar_f t = VGET(a,i);
   VPUT(a,i,VGET(a,j));
   VPUT(a,j+1,t);
}
Example #28
0
static VALUE
rb_cpBBWrapVect(VALUE self, VALUE v)
{
    return VNEW(cpBBWrapVect(*BBGET(self), *VGET(v)));
}
Example #29
0
static VALUE
rb_momentForCircle(VALUE self, VALUE m, VALUE r1, VALUE r2, VALUE offset)
{
	cpFloat i = cpMomentForCircle(NUM2DBL(m), NUM2DBL(r1), NUM2DBL(r2), *VGET(offset));
	return rb_float_new(i);
}
Example #30
0
static VALUE
rb_dampedSpring(VALUE self, VALUE a, VALUE b, VALUE r1, VALUE r2, VALUE len, VALUE k, VALUE dmp, VALUE dt)
{
	cpDampedSpring(BODY(a), BODY(b), *VGET(r1), *VGET(r2), NUM2DBL(len), NUM2DBL(k), NUM2DBL(dmp), NUM2DBL(dt));
	return Qnil;
}