Ejemplo n.º 1
0
void main() 
{
    time_t t = time(NULL);
    double jd0 = julian_date(1272672000);
    // 1271474022);
    
    // jd -= 10.0;
    double i;

    for (i = 0.0; i < 300; i += 0.01) { 
        double jd = jd0 + i;
        
        double io[3];
        get_io_parent_coordsv(jd, io);

        double europa[3];
        get_europa_parent_coordsv(jd, europa);
        
        double ganymede[3];
        get_ganymede_parent_coordsv(jd, ganymede);

        double calisto[3];
        get_callisto_parent_coordsv(jd, calisto);

        double ioy = compute_delta(jd, io);
        double europay = compute_delta(jd, europa);
        double ganymedey = compute_delta(jd, ganymede);
        double calistoy = compute_delta(jd, calisto);

        /* printf("%10.10f %10.10f %10.10f\n", earth[0], earth[1], earth[2]); */
        /* printf("%10.10f %10.10f %10.10f\n", jupiter[0], jupiter[1], jupiter[2]); */
        /* printf("%10.10f %10.10f %10.10f\n", calisto[0], calisto[1], calisto[2]); */
        
        /* printf("A = %10.10f, B = %10.10f, C = %10.10f, y = %10.10f\n", A, B, C, y); */

        printf("%10.10f %10.10f %10.10f %10.10f %10.10f \n", jd, ioy, europay, ganymedey, calistoy);
    }
    //    printf("pow(A,2) = %10.10f, B = %10.10f, C = %10.10f, y = %10.10f\n", pow(A,2), (pow(C,2) + pow(A,2) - pow(B,2)) / C, ((pow(C,2) + pow(A,2) - pow(B,2))/(2*C)), y);

    /* for (i = 0.0; i < 30; i += 0.01) { */
    /*     double j = jd + i; */
    /*     // printf("JD = %f\n", jd); */
    /*     double xyz1[3]; */
    /*     double xyz2[3]; */
    /*     double xyz3[3]; */
    /*     double xyz4[3]; */
    /*     GetL1Coor(j, 0, xyz1); */
    /*     GetL1Coor(j, 1, xyz2); */
    /*     GetL1Coor(j, 2, xyz3); */
    /*     GetL1Coor(j, 3, xyz4); */
    /*     // printf("x: %10.10f, y: %10.10f, z: %10.10f\n", xyz[0], */
    /*     // xyz[1], xyz[2]); */
    /*     // printf("%10.10f %10.10f %10.10f %10.10f %10.10f\n", j, -1.0 * xyz1[1], -1.0 * xyz2[1], -1.0 * xyz3[1], -1.0 * xyz4[1]); */
    /*     printf("%10.10f %10.10f %10.10f %10.10f %10.10f\n", j, -1.0 * xyz1[0], -1.0 * xyz1[1], -1.0 * xyz4[0], -1.0 * xyz4[1]); */
    /* } */

}
Ejemplo n.º 2
0
 void pno_criterion_t::compute_limit()
 {
     //PRX_DEBUG_COLOR("Computing the limit...", PRX_TEXT_BLUE );
     //Need to seed \beta with something small.
     double lambda = 0.5;
     double step = 0.05;
     
     //Then, we need to compute the limit for this beta
     double last_deg = compute_delta( lambda );
     
     //Now perform hill climbing until resolution is small
     while( step > PRX_ZERO_CHECK )
     {
         //Get some idea about the neighborhood
         double low = compute_delta( lambda - step );
         double high = compute_delta( lambda + step );
         //PRX_PRINT(": " << low << " " << last_deg << " " << high, PRX_TEXT_LIGHTGRAY );
         //Check if we appear to be at a minimum
         if( last_deg < low && last_deg < high )
         {
             //Then, we need to decrease the step size
             step /= 1.5;
         }
         else if( low < high ) //We need to move to smaller beta
         {
             lambda -= step;
             last_deg = low;
         }
         else if( high < low ) //Need to move to larger beta
         {
             lambda += step;
             last_deg = high;
         }
         else //We've essentially bottomed out at this point
         {
             step = 0;
         }
         //Also slightly degrade the step size a bit so as to ensure it stops.
         step -= 0.0001;
         if( lambda > 0.5 )
         {
             lambda = 0.5;
             step = 0;
             last_deg = compute_delta( lambda );
         }
     }
     
     //Now, set the limit to the minimum n we found.
     beta = lambda*epsilon;
     achieved_bound = last_deg;
 }
Ejemplo n.º 3
0
Archivo: tbt.cpp Proyecto: dromer/TBT
void TBT::append(uint64_t key) {
  TBTEntry *ent;
  ent = new TBTEntry();

  // compute time delay since last key
  // the NOW is WHEN this operation is executed
  compute_delta( ent );

  // normalize ambiguous keycodes
  switch( key ) {

  case KEY_BACKSPACE_ASCII:
  case KEY_BACKSPACE:
  case KEY_BACKSPACE_APPLE:
  case KEY_BACKSPACE_SOMETIMES:
    ent->key = (uint64_t)KEY_BACKSPACE_ASCII;
    break;
    
  case KEY_NEWLINE:
  case KEY_ENTER:
    ent->key  = (uint64_t)KEY_ENTER;
    break;

  default:
    ent->key = (uint64_t)key;

  }

  buffer->append(ent);
}
Ejemplo n.º 4
0
void    aff_percent(double *opt, double *output, int optSize, double gen,
		    int actual)
{
  system("clear");
  printf("\nCompleted %d%% from generation %d\n",
         (int) (100 - compute_delta(opt, output, optSize) * 100),
         (((int) gen > pow(2, (sizeof(int) * 8))) ?
          ((int) (pow(2, (sizeof(int) * 8)) - 1)) : ((int) gen)));
  aff_time((int) (time(0) - actual));
  printf("\n");
}
Ejemplo n.º 5
0
void
utpms_intr(struct uhidev *addr, void *ibuf, unsigned int len)
{
	struct utpms_softc *sc = (struct utpms_softc *)addr;
	unsigned char *data;
	int dx, dy, dz, i, s;
	uint32_t buttons;

	/* Ignore incomplete data packets. */
	if (len != sc->sc_datalen)
		return;
	data = ibuf;

	/* The last byte is 1 if the button is pressed and 0 otherwise. */
	buttons = !!data[sc->sc_datalen - 1];

	/* Everything below assumes that the sample is reordered. */
	reorder_sample(sc, sc->sc_sample, data);

	/* Is this the first sample? */
	if (!(sc->sc_status & UTPMS_VALID)) {
		sc->sc_status |= UTPMS_VALID;
		sc->sc_x = sc->sc_y = -1;
		sc->sc_x_raw = sc->sc_y_raw = -1;
		memcpy(sc->sc_prev, sc->sc_sample, sizeof(sc->sc_prev));
		bzero(sc->sc_acc, sizeof(sc->sc_acc));
		return;
	}
	/* Accumulate the sensor change while keeping it nonnegative. */
	for (i = 0; i < UTPMS_SENSORS; i++) {
		sc->sc_acc[i] +=
			(signed char)(sc->sc_sample[i] - sc->sc_prev[i]);

		if (sc->sc_acc[i] < 0)
			sc->sc_acc[i] = 0;
	}
	memcpy(sc->sc_prev, sc->sc_sample, sizeof(sc->sc_prev));

	/* Compute change. */
	dx = dy = dz = 0;
	if (!compute_delta(sc, &dx, &dy, &dz, &buttons))
		return;

	/* Report to wsmouse. */
	if ((dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) &&
	    sc->sc_wsmousedev != NULL) {
		s = spltty();
		wsmouse_input(sc->sc_wsmousedev, buttons, dx, -dy, dz, 0,
		    WSMOUSE_INPUT_DELTA);
		splx(s);
	}
	sc->sc_buttons = buttons;
}
Ejemplo n.º 6
0
static int
set (void *vstate, gsl_multiroot_function * func, gsl_vector * x, gsl_vector * f, gsl_vector * dx, int scale)
{
  hybrid_state_t *state = (hybrid_state_t *) vstate;

  gsl_matrix *J = state->J;
  gsl_matrix *q = state->q;
  gsl_matrix *r = state->r;
  gsl_vector *tau = state->tau;
  gsl_vector *diag = state->diag;
  
  GSL_MULTIROOT_FN_EVAL (func, x, f);

  gsl_multiroot_fdjacobian (func, x, f, GSL_SQRT_DBL_EPSILON, J) ;

  state->iter = 1;
  state->fnorm = enorm (f);
  state->ncfail = 0;
  state->ncsuc = 0;
  state->nslow1 = 0;
  state->nslow2 = 0;

  gsl_vector_set_all (dx, 0.0);

  /* Store column norms in diag */

  if (scale)
    compute_diag (J, diag);
  else
    gsl_vector_set_all (diag, 1.0);

  /* Set delta to factor |D x| or to factor if |D x| is zero */

  state->delta = compute_delta (diag, x);

  /* Factorize J into QR decomposition */

  gsl_linalg_QR_decomp (J, tau);
  gsl_linalg_QR_unpack (J, tau, q, r);

  return GSL_SUCCESS;
}
Ejemplo n.º 7
0
int		best_doubtab(double *doubtab[], double *out, int size)
{
  double	delta[GENSIZE];
  double	buf;
  int		i;
  int		bufi;

  i = -1;
  while (++i < GENSIZE)
    delta[i] = compute_delta(doubtab[i], out, size);
  bufi = 0;
  buf = delta[bufi];
  while (--i >= 0)
    if (delta[i] < buf)
      {
	buf = delta[i];
	bufi = i;
      }
  return (bufi);
}
Ejemplo n.º 8
0
static int
iterate (void *vstate, gsl_multiroot_function * func, gsl_vector * x, gsl_vector * f, gsl_vector * dx, int scale)
{
  hybrid_state_t *state = (hybrid_state_t *) vstate;

  const double fnorm = state->fnorm;

  gsl_matrix *J = state->J;
  gsl_matrix *q = state->q;
  gsl_matrix *r = state->r;
  gsl_vector *tau = state->tau;
  gsl_vector *diag = state->diag;
  gsl_vector *qtf = state->qtf;
  gsl_vector *x_trial = state->x_trial;
  gsl_vector *f_trial = state->f_trial;
  gsl_vector *df = state->df;
  gsl_vector *qtdf = state->qtdf;
  gsl_vector *rdx = state->rdx;
  gsl_vector *w = state->w;
  gsl_vector *v = state->v;

  double prered, actred;
  double pnorm, fnorm1, fnorm1p;
  double ratio;
  double p1 = 0.1, p5 = 0.5, p001 = 0.001, p0001 = 0.0001;

  /* Compute qtf = Q^T f */

  compute_qtf (q, f, qtf);

  /* Compute dogleg step */

  dogleg (r, qtf, diag, state->delta, state->newton, state->gradient, dx);

  /* Take a trial step */

  compute_trial_step (x, dx, state->x_trial);

  pnorm = scaled_enorm (diag, dx);

  if (state->iter == 1)
    {
      if (pnorm < state->delta)
	{
	  state->delta = pnorm;
	}
    }

  /* Evaluate function at x + p */

  {
    int status = GSL_MULTIROOT_FN_EVAL (func, x_trial, f_trial);

    if (status != GSL_SUCCESS) 
      {
        return GSL_EBADFUNC;
      }
  }
  
  /* Set df = f_trial - f */

  compute_df (f_trial, f, df);

  /* Compute the scaled actual reduction */

  fnorm1 = enorm (f_trial);

  actred = compute_actual_reduction (fnorm, fnorm1);

  /* Compute rdx = R dx */

  compute_rdx (r, dx, rdx);

  /* Compute the scaled predicted reduction phi1p = |Q^T f + R dx| */

  fnorm1p = enorm_sum (qtf, rdx);

  prered = compute_predicted_reduction (fnorm, fnorm1p);

  /* Compute the ratio of the actual to predicted reduction */

  if (prered > 0)
    {
      ratio = actred / prered;
    }
  else
    {
      ratio = 0;
    }

  /* Update the step bound */

  if (ratio < p1)
    {
      state->ncsuc = 0;
      state->ncfail++;
      state->delta *= p5;
    }
  else
    {
      state->ncfail = 0;
      state->ncsuc++;

      if (ratio >= p5 || state->ncsuc > 1)
	state->delta = GSL_MAX (state->delta, pnorm / p5);
      if (fabs (ratio - 1) <= p1)
	state->delta = pnorm / p5;
    }

  /* Test for successful iteration */

  if (ratio >= p0001)
    {
      gsl_vector_memcpy (x, x_trial);
      gsl_vector_memcpy (f, f_trial);
      state->fnorm = fnorm1;
      state->iter++;
    }

  /* Determine the progress of the iteration */

  state->nslow1++;
  if (actred >= p001)
    state->nslow1 = 0;

  if (actred >= p1)
    state->nslow2 = 0;

  if (state->ncfail == 2)
    {
      gsl_multiroot_fdjacobian (func, x, f, GSL_SQRT_DBL_EPSILON, J) ;

      state->nslow2++;

      if (state->iter == 1)
	{
          if (scale)
            compute_diag (J, diag);
	  state->delta = compute_delta (diag, x);
	}
      else
        {
          if (scale)
            update_diag (J, diag);
        }

      /* Factorize J into QR decomposition */

      gsl_linalg_QR_decomp (J, tau);
      gsl_linalg_QR_unpack (J, tau, q, r);

      return GSL_SUCCESS;
    }

  /* Compute qtdf = Q^T df, w = (Q^T df - R dx)/|dx|,  v = D^2 dx/|dx| */

  compute_qtf (q, df, qtdf);

  compute_wv (qtdf, rdx, dx, diag, pnorm, w, v);

  /* Rank-1 update of the jacobian Q'R' = Q(R + w v^T) */

  gsl_linalg_QR_update (q, r, w, v);

  /* No progress as measured by jacobian evaluations */

  if (state->nslow2 == 5)
    {
      return GSL_ENOPROGJ;
    }

  /* No progress as measured by function evaluations */

  if (state->nslow1 == 10)
    {
      return GSL_ENOPROG;
    }

  return GSL_SUCCESS;
}
Ejemplo n.º 9
0
/** Train the unsupervised SOM network.
 *
 * The network is initialized with random (non-graduate) values. It is then
 * trained with the image pixels (RGB). Once the network is done training the
 * centroids of the resulting clusters is returned.
 *
 * @note The length of the clusters depends on the parameter 'n'. The greatest
 *  n, the more colors in the clusters, the less posterized the image.
 *
 * @param[out] res       The resulting R, G and B clusters centroids.
 * @param[in]  imgPixels The original image pixels.
 * @param[in]  nbPixels  The number of pixels of th image (height * width).
 * @param[in]  nbNeurons The posterization leveldefined by its number of 
 *                       neurons.
 * @param[in]  noEpoch   Number of training passes (a too small number of epochs
 *  won't be enough for the network to correctly know the image but a too high
 *  value will force the network to modify the wieghts so much that the image
 *  can actually looks "ugly").
 * @param[in]  thresh    The threshold value. The SOM delta parameter and the
 *  training rate are dicreasing from one epoch to the next. The treshold value
 *  set a stop condifition in case the value has fallen under this minimum 
 *  value.
 *
 * @return SOM_OK if everything goes right or SOM_NO_MEMORY if a memory 
 *  allocation (malloc) fail.
 */
int som_train(float **res, float **imgPixels, unsigned int nbPixels,
              int nbNeurons, int noEpoch, float thresh){
    int mapWidth =              // Map width. This can be calculated from its
        (int)sqrt(nbNeurons);   // number of neurons as the map is square.
    int mapHeight =             // Map height. This can be calculated from its
        (int)sqrt(nbNeurons);   // number of neurons as the map is square.
    float delta = INT_MAX;      // Start with an almost impossible value
    int it = 0;                 // Count the network iterations
    unsigned int pick;          // The choosen input pixel
    float rad;                  // Neighbooring radius (keep dicreasing)
    float eta;                  // Learning rate (keep dicreasing)
    float pickRGB[3] = {0};     // Contains the choosen input RGB vector
    size_t choosen;             // Best Matching Unit (BMU)
    int choosen_x;              // BMU abscissa
    int choosen_y;              // BMU ordinate
    float *neigh =              // Neighbooring mask
        malloc(sizeof(float) * nbNeurons);
	if(neigh == NULL){
		return SOM_NO_MEMORY;
    }
    memset(neigh, 0, nbNeurons);

    float *WR =                 // RED part of the network weight vectors
        malloc(sizeof(float) * nbNeurons);
	if(WR == NULL){
		return SOM_NO_MEMORY;
    }
    memset(WR, 0, nbNeurons);
    float *WG =                 // GREEN part of the network weight vectors
        malloc(sizeof(float) * nbNeurons);
	if(WG == NULL){
		return SOM_NO_MEMORY;
    }
    memset(WG, 0, nbNeurons);
    float *WB =                 // BLUE part of the network weight vectors
        malloc(sizeof(float) * nbNeurons);
	if(WB == NULL){
		return SOM_NO_MEMORY;
    }
    memset(WB, 0, nbNeurons);

    float *deltaR =             // New RED part of the network weight vectors
        malloc(sizeof(float) * nbNeurons);
	if(deltaR == NULL){
		return SOM_NO_MEMORY;
    }
    memset(deltaR, 0, nbNeurons);
    float *deltaG =             // New GREEN part of the network weight vectors
        malloc(sizeof(float) * nbNeurons);
	if(deltaG == NULL){
		return SOM_NO_MEMORY;
    }
    memset(deltaG, 0, nbNeurons);
    float *deltaB =             // New BLUE part of the network weight vectors
        malloc(sizeof(float) * nbNeurons);
	if(deltaB == NULL){
		return SOM_NO_MEMORY;
    }
    memset(deltaB, 0, nbNeurons);

    float *absDeltaR =          // Absolute value of deltaR
        malloc(sizeof(float) * nbNeurons);
	if(absDeltaR == NULL){
		return SOM_NO_MEMORY;
    }
    memset(absDeltaR, 0, nbNeurons);
    float *absDeltaG =          // Absolute value of deltaG
        malloc(sizeof(float) * nbNeurons);
	if(absDeltaG == NULL){
		return SOM_NO_MEMORY;
    }
    memset(absDeltaG, 0, nbNeurons);
    float *absDeltaB =          // Absolute value of deltaB
        malloc(sizeof(float) * nbNeurons);
	if(absDeltaB == NULL){
		return SOM_NO_MEMORY;
    }
    memset(absDeltaB, 0, nbNeurons);

    float *dists =              // Vectors euclidian distances from input form
        malloc(sizeof(float) * nbNeurons);
	if(dists == NULL){
		return SOM_NO_MEMORY;
    }
    memset(dists, 0, nbNeurons);

    /* Randomly initialize weight vectors */
    srand(time(NULL));
    random_sample(WR, nbNeurons);
    random_sample(WG, nbNeurons);
    random_sample(WB, nbNeurons);

    while(it < noEpoch && delta >= thresh){
        /* Randomly choose an input form */
        pick = random_uint(nbPixels);
        pickRGB[0] = imgPixels[pick][0];
        pickRGB[1] = imgPixels[pick][1];
        pickRGB[2] = imgPixels[pick][2];

        /* Compute every vectors euclidian distance from the input form */
        euclidian(dists, nbNeurons, WR, WG, WB, pickRGB);
        /* Determine the BMU */
        choosen = arr_min_idx(dists, nbNeurons);
        choosen_x = (int)choosen % mapWidth;
        choosen_y = choosen / mapHeight;

        /* Compute the new neighbooring radius */
        rad = som_radius(it, noEpoch, mapWidth, mapHeight);
        /* Find the BMU neighboors */
        som_neighbourhood(neigh, choosen_x, choosen_y, rad, mapWidth, 
                          mapHeight);

        /* Compute the new learning rate */
        eta = som_learning_rate(it, noEpoch);
        /* Compute new value of the network weight vectors */
        compute_delta(deltaR, eta, neigh, nbNeurons, pickRGB[0], WR);
        compute_delta(deltaG, eta, neigh, nbNeurons, pickRGB[1], WG);
        compute_delta(deltaB, eta, neigh, nbNeurons, pickRGB[2], WB);
        /* Update the network weight vectors values */
        arr_add(WR, deltaR, nbNeurons);
        arr_add(WG, deltaG, nbNeurons);
        arr_add(WB, deltaB, nbNeurons);

        arr_abs(absDeltaR, deltaR, nbNeurons);
        arr_abs(absDeltaG, deltaG, nbNeurons);
        arr_abs(absDeltaB, deltaB, nbNeurons);
        delta = arr_sum(absDeltaR, nbNeurons) +
                arr_sum(absDeltaG, nbNeurons) +
                arr_sum(absDeltaB, nbNeurons);

        it++;
    }
    res[0] = WR;
    res[1] = WG;
    res[2] = WB;

    free(dists);
    free(deltaR);
    free(deltaG);
    free(deltaB);
    free(absDeltaR);
    free(absDeltaG);
    free(absDeltaB);
    free(neigh);

    return SOM_OK;
}
Ejemplo n.º 10
0
static int
set (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale)
{
  lmder_state_t *state = (lmder_state_t *) vstate;

  gsl_matrix *r = state->r;
  gsl_vector *tau = state->tau;
  gsl_vector *diag = state->diag;
  gsl_vector *work1 = state->work1;
  gsl_permutation *perm = state->perm;

  int signum;

  /* Evaluate function at x */
  /* return immediately if evaluation raised error */
  {
    int status = GSL_MULTIFIT_FN_EVAL_F_DF (fdf, x, f, J);
    if (status)
      return status;
  }

  state->par = 0;
  state->iter = 1;
  state->fnorm = enorm (f);

  gsl_vector_set_all (dx, 0.0);

  /* store column norms in diag */

  if (scale)
    {
      compute_diag (J, diag);
    }
  else
    {
      gsl_vector_set_all (diag, 1.0);
    }

  /* set delta to 100 |D x| or to 100 if |D x| is zero */

  state->xnorm = scaled_enorm (diag, x);
  state->delta = compute_delta (diag, x);

  /* Factorize J into QR decomposition */

  gsl_matrix_memcpy (r, J);
  gsl_linalg_QRPT_decomp (r, tau, perm, &signum, work1);

  gsl_vector_set_zero (state->rptdx);
  gsl_vector_set_zero (state->w);

  /* Zero the trial vector, as in the alloc function */

  gsl_vector_set_zero (state->f_trial);

#ifdef DEBUG
  printf("r = "); gsl_matrix_fprintf(stdout, r, "%g");
  printf("perm = "); gsl_permutation_fprintf(stdout, perm, "%d");
  printf("tau = "); gsl_vector_fprintf(stdout, tau, "%g");
#endif

  return GSL_SUCCESS;
}