/**
 * According to "A Practical Analytic Model for Daylight".
 */
static scalar get_sky_luminance(
	const vector &ray_dir,
	const vector &sun_dir,
	scalar   t)
{
	scalar cos_gamma = dot(ray_dir, sun_dir);
	if(cos_gamma < 0.0f)
	{
		cos_gamma = 0.0f;
	}
	if(cos_gamma > 1.0f)
	{
		cos_gamma = 2.0f - cos_gamma;
	}
	scalar gamma         = acosf(cos_gamma);
	scalar cos_theta     = ray_dir.y;
	scalar cos_theta_sun = sun_dir.y;
	scalar theta_sun     = acosf(cos_theta_sun);

	scalar a =   0.178721f * t - 1.463037f;
	scalar b = - 0.355402f * t + 0.427494f;
	scalar c = - 0.022669f * t + 5.325056f;
	scalar d =   0.120647f * t - 2.577052f;
	scalar e = - 0.066967f * t + 0.370275f;
	scalar ratio = ((1.0f + a * fastexp(b / cos_theta)) * ((1.0f + c * fastexp(d * gamma)) + (e * cos_gamma) * cos_gamma)) / ((1.0f + a * fastexp(b)) * ((1.0f + c * fastexp(d * theta_sun)) + (e * cos_theta_sun) * cos_theta_sun));
	return ratio;
}
Example #2
0
static void 
mexsoftmax(float* y, float* shift, mwSize m, mwSize n) {
  __m128 i1, i2;
  __m128 o1, o2;
 
  while (m>0)
    {
      mwSize curn = n;
      float sum = 0.0f;
      declconst128(zero, 0.0f);
      
      while (curn>0 && ((unsigned long)(y+curn) & 15) != 0)
        {
          --curn;
          y[curn]=fastexp(y[curn]-*shift);
          sum += y[curn];
        }

      __m128 s1 = _mm_load1_ps (shift);
      __m128 sum1 = zero;

      while (curn>7) {
        i1 = _mm_load_ps (y+curn-4);
        i2 = _mm_load_ps (y+curn-8);
        i1 = _mm_sub_ps (i1, s1);
        i2 = _mm_sub_ps (i2, s1);
        o1 = vfastexp(i1);
        o2 = vfastexp(i2);
        _mm_store_ps (y+curn-4, o1);
        sum1 = _mm_add_ps (sum1, o1);
        _mm_store_ps (y+curn-8, o2);
        sum1 = _mm_add_ps (sum1, o2);
        curn-=8;
      }

      sum1 = _mm_hadd_ps (sum1, sum1);
      sum1 = _mm_hadd_ps (sum1, sum1);
      sum += _mm_cvtss_f32 (sum1);
     
      while(curn>0) {
        --curn;
        y[curn]=fastexp(y[curn]-*shift);
        sum += y[curn];
      }

      sum = 1.0f / sum;

      ptrdiff_t n_pdt = n;
      ptrdiff_t one_pdt = 1;

      sscal (&n_pdt, &sum, y, &one_pdt);

      ++shift;
      y+=n;
      --m;
    }
}
Example #3
0
main ()
   /* I just realized that this program finds things in the range of hundreds
      thousands, I originally had it working for 7,500,000 to 7,510,000.  So 
      it should work for even much bigger numbers.   (As long as the size of 
      a number squared does not overflow a double.) */

{
   long int a = 2;
   int  counter = 0;
   int  b = 750001;
   long int z = 0;
   long int jac = 0;
   long int  exp = 0;
   int prime = TRUE;

   printf("The following are prime:\n");
   while(b <= 751000) {
      a = 2;
      prime = TRUE;
      while(a<=20) {
	 if (gcd(a,b) == 1) {
	    jac = j((long int)a,(long int)b);
	    jac = (jac + b)%b;
	    exp = fastexp(a,(b-1)/2, b);

	    if (jac != exp)
	       prime = FALSE;
	 } 
	 a++;
      }
      if (prime)
	 printf("%d\n", b);
      b += 2;
   }
}
Example #4
0
double AnovaDotKernel::operator () (const vector<double> &x, const vector<double> &y) const {
    assert(x.size() == y.size());
    double acc = 0.0;
    for(size_t i=0; i<x.size(); ++i) {
        acc += fastexp(-o.sigma*(x[i] - y[i])*(x[i] - y[i]));
    }
    return fastpow(acc, o.power);
}
/**
 * According to "Spatial distribution of daylight CIE standard general sky".
 */
static color get_cie_standard_sky_color(
	const vector &ray_dir,
	const vector &sun_dir,
	const color  &zenith_color,
	scalar   a,
	scalar   b,
	scalar   c,
	scalar   d,
	scalar   e)
{
	eiVector2 ray_pos;
	ray_pos.x = acosf(ray_dir.y);
	ray_pos.y = atan2f(ray_dir.z, ray_dir.x);

	eiVector2 sun_pos;
	sun_pos.x = acosf(sun_dir.y);
	sun_pos.y = atan2f(sun_dir.z, sun_dir.x);

	scalar cos_z_sun = fastercosfull(sun_pos.x);
	scalar cos_z = fastercosfull(ray_pos.x);
	scalar sin_z_sun = fastersinfull(sun_pos.x);
	scalar sin_z = fastersinfull(ray_pos.x);

	scalar chi = acosf(cos_z_sun * cos_z + sin_z_sun * sin_z * fastercosfull(absf(ray_pos.y - sun_pos.y)));
	scalar cos_chi = fastercosfull(chi);

	const scalar pi_over_2 = static_cast<scalar>(eiPI / 2.0f);
	scalar f_chi   = 1.0f + c * (fastexp(d * chi) - fastexp(d * pi_over_2)) + e * cos_chi * cos_chi;
	scalar phi_z   = 1.0f + a * fastexp(b / cos_z);
	scalar f_z_sun = 1.0f + c * (fastexp(d * sun_pos.x) - fastexp(d * pi_over_2)) + e * cos_z_sun * cos_z_sun;
	scalar phi_0   = 1.0f + a * fastexp(b);
	scalar ratio   = (f_chi * phi_z) / (f_z_sun * phi_0);

	return zenith_color * ratio;
}
Example #6
0
void Softmax(std::vector<float>* vec) {
  CHECK_NOTNULL(vec);
  // TODO(wdai): Figure out why this is necessary. Doubt it is.
	for (int i = 0; i < vec->size(); ++i) {
		if (std::abs((*vec)[i]) < kCutoff) {
			(*vec)[i] = kCutoff;
    }
	}
	double lsum = LogSumVec(*vec);
	for (int i = 0; i < vec->size(); ++i) {
		(*vec)[i] = fastexp((*vec)[i] - lsum);
		//(*vec)[i] = exp((*vec)[i] - lsum);
    (*vec)[i] = (*vec)[i] > 1 ? 1. : (*vec)[i];
  }
}
Example #7
0
float pipe_tick(struct pipe* p) {
  int silencecounter = p->silencecounter;
  double airflow = p->airflow;
  double const airflowtarget = p->airflowtarget;

  airflow += (airflowtarget-airflow) * p->airflowspeed;

  /*
  p->fir_state[3]=p->fir_state[2];
  p->fir_state[2]=p->fir_state[1];
  p->fir_state[1]=p->fir_state[0];
  p->fir_state[0]=delay_read(&p->delay);
  
  double const fdout
    = 1.0e-6
    + p->fir_state[0]*p->fir_coeff[0]
    + p->fir_state[1]*p->fir_coeff[1]
    + p->fir_state[2]*p->fir_coeff[2]
    + p->fir_state[3]*p->fir_coeff[3];
  */

  double const fdout = thiran1_tick(&p->fd_state, p->fd_coeff,
                                    delay_read(&p->delay));

  double const delayout =
    onepole_tick(&p->airlossfilter1,
                 onepole_tick(&p->airlossfilter2,
                              fdout));
  
  double const pipeout = onepole_tick(&p->reflectionfilter,delayout);
  double const reflected = pipeout - delayout;
  double const r = reflected-0.5;
  
  double const pipein = airflow * fastexp(-r*r) * (0.9+0.2*rand()/RAND_MAX);
  double const delayin = pipein + reflected;
  delay_write(&p->delay,delayin);
  if(airflow > 1.0e-4 || fabs(pipeout) > 1.0e-5) {
    silencecounter = p->delay.length*2+1;
  }
  else {
    silencecounter = silencecounter - 1;
  }
  p->silencecounter = silencecounter;
  p->airflow = airflow;
  return pipeout*p->gain;
}
Example #8
0
int 
main (int   argc,
      char *argv[])
{
  char buf[4096];

  (void) argc;

  float x;
  for (x = -50; x > -1000; x -= 100)
    {
      assert (fastexp (x) >= 0);
      assert (fasterexp (x) >= 0);
#ifdef __SSE2__
      v4sf vx = v4sfl (x);
      assert (v4sf_index (vfastexp (vx), 0) >= 0);
      assert (v4sf_index (vfasterexp (vx), 0) >= 0);
#endif
    }

  srand48 (69);

  strncpy (buf, argv[0], sizeof (buf) - 5);
  strncat (buf, ".out", 5);

  fclose (stderr);
  stderr = fopen (buf, "w");


  test_fastexp ();
  test_fasterexp ();
  test_vfastexp ();
  test_vfasterexp ();

  time_fastexp ();
  time_fasterexp ();
  time_vfastexp ();
  time_vfasterexp ();

  return 0;
}
int main() {
    int n, t = 1;
    while (scanf(" %d", &n), n != 0) {
        printf("Teste %d\n%d\n\n", t++, fastexp(2, n) - 1);
    }
}
int fastexp(int n, int p) {
    if (p == 0) return 1;
    if (!(p % 2)) return fastexp (n * n, p/2);
    return n * fastexp (n * n, p/2);
}
Example #11
0
double MotifSearch::score_site(double* score_matrix, const int c, const int p, const bool s) {
	double ms = motif.score_site(score_matrix, c, p, s);
	double bs = bgmodel.score_site(motif.first_column(), motif.last_column(), motif.get_width(), c, p, s);
	return fastexp(ms - bs);
}
Example #12
0
float LogSum(float log_a, float log_b) {
  return (log_a < log_b) ? log_b + fastlog(1 + fastexp(log_a - log_b)) :
      log_a + fastlog(1 + fastexp(log_b-log_a));
}
Example #13
0
void propagator_derive_chain_UO_ana_mors(int ny, double x, double *rdm,
			     double* y, double* dydx)
{
	//halmiltonian chain: see paper THE JOURNAL OF CHEMICAL PHYSICS 128, 224710 (2008)
	/*
	  y[0]---------------x_0
	  y[1]---------------y_1
	  y[2]---------------x_1
	  y[3]---------------p_1
	  y[2*i]-------------x_{i}
	  y[2*i+1]-----------p_{i}
	  y[2*N]-------------x_N
	  y[2*N+1]-----------p_N
	  y[2*(N+1)]---------x_{N+1}
	  y[2*(N+1)+1]-------y_N
	  y[2*(N+2)]-------eta_0
	  y[2*(N+2)+1]-----eta_N
	*/
  int N=ny/2-3;
  //calculate the pair twice
//   int i;
//   for(i=1;i<=N;i++)
//   {
//     dydx[2*i]=y[2*i+1]/mass;//    (d/dt)x_i=p_i/m
//     double r1=y[2*i+2]-y[2*i]-x_eq;//r1=x_{i+1}-x_i-x_eq
//     double r2=y[2*i]-y[2*i-2]-x_eq;//r2=x_{i}-x_{i-1}-x_eqD
//     r1=fastexp(-a*r1);
//     r2=fastexp(-a*r2);
//     dydx[2*i+1]=(r2*(r2-1)-r1*(r1-1))*2*a*D;//see eq.1 and eq.2@ hamiltonian.jpg
//   }
//   dydx[3]=dydx[3]-y[1]+mass*rdm[1];
//   dydx[2*N+1]=dydx[2*N+1]-y[2*N+3]+mass*rdm[0];
//   dydx[1]=(epselon_l/tau_l)*y[3]-y[1]/tau_l;
//   dydx[2*N+3]=(epselon_r/tau_r)*y[2*N+1]-y[2*N+3]/tau_r;//see propagator_derive_chain_UO.jpg
  //calculate force pair once
  double r1;
  bzero(dydx,sizeof(double)*ny);
  int i;
#define fastexp exp
  for(i=1;i<N;i++)
  {
    dydx[2*i]=y[2*i+1]/mass;//    (d/dt)x_i=p_i/mq
    r1=y[2*i+2]-y[2*i]-x_eq;
    r1=fastexp(-a*r1);
    dydx[2*i+1]+=(-r1*(r1-1)*2.0*a*D);
    dydx[2*(i+1)+1]-=(-r1*(r1-1)*2.0*a*D);
  }
  dydx[2*N]=y[2*N+1]/mass;
  r1=y[2]-y[0]-x_eq;
  r1=fastexp(-a*r1);
  dydx[3]-=(-r1*(r1-1)*2.0*a*D);
  r1=y[2*(N+1)]-y[2*N]-x_eq;
  r1=fastexp(-a*r1);
  dydx[2*N+1]+=(-r1*(r1-1)*2.0*a*D);
  dydx[3]=dydx[3]-y[1]+mass*y[2*(N+2)];
  dydx[2*N+1]=dydx[2*N+1]-y[2*N+3]+mass*y[2*(N+2)+1];
  dydx[1]=(epselon_l/tau_l)*y[3]-y[1]/tau_l;
  dydx[2*N+3]=(epselon_r/tau_r)*y[2*N+1]-y[2*N+3]/tau_r;//see propagator_derive_chain_UO.jpg
  dydx[2*(N+2)]=(-y[2*(N+2)]+rdm[1])/tau_l;//rdm=sqrt(2*\epselon_l*k_b*T_l/m)\mu is generated by white_rand_gener_2bath(2*nt, dt/2.0, T_r, T_l, &rand, i%4), where mean(\mu(t)\mu(t'))=\delta(t-t');
  dydx[2*(N+2)+1]=(-y[2*(N+2)+1]+rdm[0])/tau_r;
//  printf("%f\n",y[2*(N+2)]);
//  printf("%f\n",rdm[1]);
#undef fastexp
  //test x_0 periodic and x_{N+1} damping:
//  dydx[0]=0.1*sin(x);
//  dydx[2*N+1]-=y[2*N+1];
}
Example #14
0
int main() {
	
	a = exp(a);
	b = expf(b);
	c = fastexp(c);
	d = fasterexp(c);
	printf("%f %f %f %f\n", a, b, c, d);
	
	a = log(a);
	b = logf(b);
	c = fastlog(c);
	d = fasterlog(c);
	printf("%f %f %f %f\n", a, b, c, d);	
	
	a = pow(a,a);
	b = pow(b,b);
	c = fastpow(c,c);
	d = fasterpow(c,c);
	printf("%f %f %f %f\n", a, b, c, d);			

	
	a = sin(a);
	b = sinf(b);
	c = fastsin(c);
	d = fastersin(c);
	printf("%f %f %f %f\n", a, b, c, d);
	
	a = cos(a);
	b = cosf(b);
	c = fastcos(c);
	d = fastercos(c);
	printf("%f %f %f %f\n", a, b, c, d);
	
	a = tan(a);
	b = tanf(b);
	c = fasttan(c);
	d = fastertan(c);
	printf("%f %f %f %f\n", a, b, c, d);	
	
	a = asin(a);
	b = asinf(b);
	printf("%f %f %f %f\n", a, b, c, d);
	
	a = acos(a);
	b = acosf(b);
	printf("%f %f %f %f\n", a, b, c, d);
	
	a = atan(a);
	b = atanf(b);
	printf("%f %f %f %f\n", a, b, c, d);	
	
	a = sinh(a);
	b = sinhf(b);
	c = fastsinh(c);
	d = fastersinh(c);
	printf("%f %f %f %f\n", a, b, c, d);
	
	a = cosh(a);
	b = coshf(b);
	c = fastcosh(c);
	d = fastercosh(c);
	printf("%f %f %f %f\n", a, b, c, d);
	
	a = tanh(a);
	b = tanhf(b);
	c = fasttanh(c);
	d = fastertanh(c);
	printf("%f %f %f %f\n", a, b, c, d);		
	
	a = lgamma(a);
	b = lgammaf(b);
	c = fastlgamma(c);
	d = fasterlgamma(c);
	printf("%f %f %f %f\n", a, b, c, d);		
	
	return 0;
	
}
static color get_haze_driven_sky_color(
	const vector &ray_dir,
	const vector &sun_dir,
	scalar   t)
{
	scalar cos_gamma = dot(ray_dir, sun_dir);
	if(cos_gamma < 0.0f)
	{
		cos_gamma = 0.0f;
	}
	if(cos_gamma > 1.0f)
	{
		cos_gamma = 2.0f - cos_gamma;
	}
	scalar gamma = acosf(cos_gamma);

	scalar cos_theta     = ray_dir.y;
	scalar cos_theta_sun = sun_dir.y;

	scalar t2 = t * t;

	scalar theta_sun  = acosf(cos_theta_sun);
	scalar theta_sun2 = theta_sun * theta_sun;
	scalar theta_sun3 = theta_sun * theta_sun2;

	scalar zenith_x = ((((0.001650f * theta_sun3 - 0.003742f * theta_sun2) + 0.002088f * theta_sun) + 0) * t2 + (((-0.029028f * theta_sun3 + 0.063773f * theta_sun2) - 0.032020f * theta_sun) + 0.003948f) * t) + (((+0.116936f * theta_sun3 - 0.211960f * theta_sun2) + 0.060523f * theta_sun) + 0.258852f);
	scalar zenith_y = ((((0.002759f * theta_sun3 - 0.006105f * theta_sun2) + 0.003162f * theta_sun) + 0) * t2 + (((-0.042149f * theta_sun3 + 0.089701f * theta_sun2) - 0.041536f * theta_sun) + 0.005158f) * t) + (((+0.153467f * theta_sun3 - 0.267568f * theta_sun2) + 0.066698f * theta_sun) + 0.266881f);

	scalar chi = (4.0f / 9.0f - t / 120.0f) * (static_cast<scalar>(eiPI) - 2.0f * theta_sun);
	scalar zenith_Y = ((1000.0f * (4.0453f * t - 4.9710f)) * fastertanfull(chi) - 0.2155f * t) + 2.4192f;
	zenith_Y *= get_sky_luminance(ray_dir, sun_dir, t);

	//
	scalar a, b, c, d, e;
	a = - 0.019257f * t - (0.29f - Fast_sqrt(cos_theta_sun) * 0.09f);
	b = - 0.066513f * t + 0.000818f;
	c = - 0.000417f * t + 0.212479f;
	d = - 0.064097f * t - 0.898875f;
	e = - 0.003251f * t + 0.045178f;
	scalar ratio_x = ((1.0f + a * fastexp(b / cos_theta)) * (1.0f + c * fastexp(d * gamma) + e * cos_gamma * cos_gamma)) /
	                  ((1.0f + a * fastexp(b)) * (1.0f + c * fastexp(d * theta_sun) + e * cos_theta_sun * cos_theta_sun));

	a = - 0.016698f * t - 0.260787f;
	b = - 0.094958f * t + 0.009213f;
	c = - 0.007928f * t + 0.210230f;
	d = - 0.044050f * t - 1.653694f;
	e = - 0.010922f * t + 0.052919f;
	scalar ratio_y = ((1.0f + a * fastexp(b / cos_theta)) * (1.0f + c * fastexp(d * gamma) + e * cos_gamma * cos_gamma)) /
	                  ((1.0f + a * fastexp(b)) * (1.0f + c * fastexp(d * theta_sun) + e * cos_theta_sun * cos_theta_sun));
	scalar chromaticity_x = zenith_x * ratio_x;
	scalar chromaticity_y = zenith_y * ratio_y;

	//
	color cie_XYZ;
	cie_XYZ.g = zenith_Y;
	cie_XYZ.r = chromaticity_x / chromaticity_y * cie_XYZ.g;
	cie_XYZ.b = (1.0f - chromaticity_x - chromaticity_y) / chromaticity_y * cie_XYZ.g;

	color linear_RGB;
	linear_RGB.r =   3.241f * cie_XYZ.r - 1.537f * cie_XYZ.g - 0.499f * cie_XYZ.b;
	linear_RGB.g = - 0.969f * cie_XYZ.r + 1.876f * cie_XYZ.g + 0.042f * cie_XYZ.b;
	linear_RGB.b =   0.056f * cie_XYZ.r - 0.204f * cie_XYZ.g + 1.057f * cie_XYZ.b;
	return linear_RGB;
}
Example #16
0
static FP_TYPE* synth_noise(llsm_parameters param, llsm_conf conf, FP_TYPE** wrapped_spectrogram, int winsize) {
/*
  To suppress glitches caused by aliasing, we apply MLT sine window twice, before analysis and after synthesis respectively;
  Overlapping factor should be greater or equal to 4 for MLT sine window;
  To preserve time resolution, we actually lower the hopsize by half, meanwhile double sampling wrapped_spectrogram.
*/
  conf.nhop /= 2;
  int nfft = winsize * conf.nhop * 2;
  int ny = conf.nfrm * conf.nhop * 2 + nfft * 16;
  FP_TYPE* y = calloc(ny, sizeof(FP_TYPE));
  FP_TYPE* w = hanning(nfft);
  FP_TYPE* realbuff = calloc(nfft, sizeof(FP_TYPE));
  FP_TYPE* imagbuff = calloc(nfft, sizeof(FP_TYPE));
  FP_TYPE* yfrm = calloc(nfft, sizeof(FP_TYPE));
  FP_TYPE fftbuff[65536];
  FP_TYPE norm_factor = 0.5 * sumfp(w, nfft);
  FP_TYPE norm_factor_win = 0;
  {
    int i = 0;
    while(i < nfft) {
      norm_factor_win += w[i];
      i += conf.nhop;
    }
  }
  for(int j = 0; j < nfft; j ++)
    w[j] = sqrt(w[j]);
  
  FP_TYPE* x = white_noise(1.0, ny);
  FP_TYPE* freqwrap = llsm_wrap_freq(0, conf.nosf, conf.nnos, conf.noswrap);
  for(int i = 0; i < conf.nfrm * 2; i ++) {
    int t = i * conf.nhop;
    FP_TYPE* spec = llsm_spectrum_from_envelope(freqwrap, wrapped_spectrogram[i / 2], conf.nnos, nfft / 2, param.s_fs);
    FP_TYPE* xfrm = fetch_frame(x, ny, t, nfft);
    for(int j = 0; j < nfft; j ++)
      xfrm[j] *= w[j];
    fft(xfrm, NULL, realbuff, imagbuff, nfft, fftbuff);
    for(int j = 0; j < nfft / 2; j ++) {
      FP_TYPE a = fastexp(spec[j]) * norm_factor; // amplitude
      FP_TYPE p = fastatan2(imagbuff[j], realbuff[j]);
      realbuff[j] = a * cos(p);
      imagbuff[j] = a * sin(p);
    }
    complete_symm (realbuff, nfft);
    complete_asymm(imagbuff, nfft);
    ifft(realbuff, imagbuff, yfrm, NULL, nfft, fftbuff);
    for(int j = 0; j < nfft; j ++) {
      int idx = t + j - nfft / 2;
      if(idx >= 0)
        y[idx] += yfrm[j] * w[j] / norm_factor_win;
    }
    free(spec);
    free(xfrm);
  }
  
  free(w);
  free(x);
  free(yfrm);
  free(realbuff);
  free(imagbuff);
  free(freqwrap);
  return y;
}