Ejemplo n.º 1
0
/* Very fast binomial sampler. 
 * Returns the number of successes out of n trials, with success probability p.
 */
int Binomial(RndState *S, double p, int n)
{
  int r = 0;
  if(isnan(p)) return 0;
  if(p < DBL_EPSILON) return 0;
  if(p >= 1-DBL_EPSILON) return n;
  if((p > 0.5) && (n < 15)) {
    /* Coin flip method. This takes O(n) time. */
    int i;
    for(i=0;i<n;i++) {
      if(Uniform(S) < p) r++;
    }
    return r;
  }
  if(n*p < 10) {
    /* Waiting time method.  This takes O(np) time. */
    double q = -log(1-p), e = -log(Uniform(S)), s;
    r = n;
    for(s = e/r; s <= q; s += e/r) {
      r--;
      if(r == 0) break;
      e = -log(Uniform(S));
    }
    r = n-r;
    return r;
  }
  if (1) {
    /* Recursive method.  This makes O(log(log(n))) recursive calls. */
    int i = (int)floor(p*(n+1));
    double b = Beta(S,i, n+1-i);
    if(b <= p) r = i + Binomial(S,(p-b)/(1-b), n-i);
    else r = i - 1 - Binomial(S,(b-p)/b, i-1);
    return r;
  }
}
Ejemplo n.º 2
0
ReedMullerCoder::ReedMullerCoder(int order, int length_param) :
  LinearCoder(),
  order_(order),
  length_param_(length_param)
{
  signal_length_ = 0;
  for (int i = 0; i <= order_; ++i) {
    signal_length_ += Binomial(length_param_, i);
  }

  code_length_ = pow(2, length_param_);
  generator_ = std::valarray<bool>(signal_length_ * code_length_);

  std::vector<std::valarray<bool>> monom_degree(code_length_);
  for (int i = 0; i < code_length_; ++i) {
    monom_degree[i] = std::valarray<bool>(length_param_);
    for (int j = 0; j < length_param_; ++j) {
      monom_degree[i][j] = i & (1 << (length_param_ - 1 - j));
    }
  }

  std::sort(monom_degree.begin(), monom_degree.end(), MonomDegreeOrder);

  int generator_index = 0;
  std::valarray<bool> generator_column(length_param_);
  for (int row = 0; row < signal_length_; ++row) {
    for (int column = code_length_ - 1; column >= 0; --column) {
      for (int column_bit = 0; column_bit < length_param_; ++column_bit) {
        generator_column[column_bit] = static_cast<bool>(column & (1 << (length_param_ - 1 - column_bit)));
      }
      generator_[generator_index] = ((monom_degree[row] * generator_column).max() == 0);
      generator_index++;
    }
  }
}
Ejemplo n.º 3
0
void StochasticLib1::Multinomial (int32_t * destination, int32_t * source, int32_t n, int colors) {
   // same as above, with integer source
   int32_t x, p, sum;
   int i;
   if (n < 0 || colors < 0) FatalError("Parameter negative in multinomial function");
   if (colors == 0) return;

   // compute sum of probabilities
   for (i=0, sum=0; i<colors; i++) { 
      p = source[i];
      if (p < 0) FatalError("Parameter negative in multinomial function");
      sum += p;
   }
   if (sum == 0 && n > 0) FatalError("Zero sum in multinomial function");

   for (i=0; i<colors-1; i++) { 
      // generate output by calling binomial (colors-1) times
      if (sum == 0) {
         destination[i] = 0; continue;
      }
      p = source[i];
      x = Binomial(n, (double)p/sum);
      n -= x; sum -= p;
      destination[i] = x;
   }
   // get the last one
   destination[i] = n;
}
Ejemplo n.º 4
0
Binomial Binomial::operator*(int x)
{
	int constant = this->getConstant() * x;
	int firstPower = this->getFirstPower() * x;
	int secondPower = this->getSecondPower() * x;
	return Binomial(constant, firstPower, secondPower);
}
Ejemplo n.º 5
0
Binomial Binomial::operator+(const Binomial& b)
{
	int constant = this->getConstant() + b.getConstant();
	int firstPower = this->getFirstPower() + b.getFirstPower();
	int secondPower = this->getSecondPower() + b.getSecondPower();
	return Binomial(constant, firstPower, secondPower);
}
Ejemplo n.º 6
0
bool TargaImage::Filter_Gaussian_N( unsigned int N )
{
	assert(N > 1);
	//assert(N < 15); //Overflows value
	unsigned char *rgb = To_RGB_All();

	int n = (int) N;
	int i, j, b, c;
	//Overwrite original image so that unfiltered edge pixels have their alpha multiplied out
	memcpy(data, rgb, sizeof(unsigned char) * width * height * 4);

	vector < int > coeffs(n); //Binomial coefficients
	for(i = 0; i<=n/2; ++i) {
		b = Binomial((n-1),i);
		coeffs[i] = b; //Fill first half
		coeffs[n - i - 1] = b; //Fill second half
	}

	vector< vector<int> > filter(n, vector<int>(n)); //nxn gaussian filter
	int x, y;
	for(y=0; y<n; ++y) {
		for(x=0; x<n; ++x) {
			filter[y][x] = coeffs[x]*coeffs[y];
		}
	}
	
	double scalar = 1/pow(2.0, 2*(n-1)); //Used to keep sum of filter elements equal to 1

	//Apply the filter to the image, read from rgb[], write to data[]
	int offset = n/2; //Closest to the edge the filter can be centered
	unsigned __int64 value; //Large type to prevent common overflows

	for (c=0; c<3; ++c) { //For each color channel
		for (y = offset; y < (height - offset); ++y) {
			for (x = offset; x < (width - offset); ++x) { //For each point in the image

				value = 0; //Reset for each filtering
				for (j=0; j<n; ++j) {  //For each value in the filter
					for (i=0; i<n; ++i) {
						value += rgb[((y - offset+j)*width +(x - offset+i))*4 + c] * filter[j][i];
					}//i
				}//j

				//Assign value to result image
				data[(y*width + x)*4 + c] = (int) floor(scalar*value + 0.5);
			}//x
		}//y
	}//c

	delete rgb;
	return true;
}// Filter_Gaussian_N
Ejemplo n.º 7
0
/***********************************************************************
Multinomial distribution
***********************************************************************/
void StochasticLib1::Multinomial (int32_t * destination, double * source, int32_t n, int colors) {
   /*
   This function generates a vector of random variates, each with the
   binomial distribution.

   The multinomial distribution is the distribution you get when drawing
   balls from an urn with more than two colors, with replacement.

   Parameters:
   destination:    An output array to receive the number of balls of each 
   color. Must have space for at least 'colors' elements.
   source:         An input array containing the probability or fraction 
   of each color in the urn. Must have 'colors' elements.
   All elements must be non-negative. The sum doesn't have
   to be 1, but the sum must be positive.
   n:              The number of balls drawn from the urn.                   
   colors:         The number of possible colors. 
   */
   double s, sum;
   int32_t x;
   int i;
   if (n < 0 || colors < 0) FatalError("Parameter negative in multinomial function");
   if (colors == 0) return;

   // compute sum of probabilities
   for (i=0, sum=0; i<colors; i++) { 
      s = source[i];
      if (s < 0) FatalError("Parameter negative in multinomial function");
      sum += s;
   }
   if (sum == 0 && n > 0) FatalError("Zero sum in multinomial function");

   for (i=0; i<colors-1; i++) { 
      // generate output by calling binomial (colors-1) times
      s = source[i];
      if (sum <= s) {
         // this fixes two problems:
         // 1. prevent division by 0 when sum = 0
         // 2. prevent s/sum getting bigger than 1 in case of rounding errors
         x = n;
      }
      else {    
         x = Binomial(n, s/sum);
      }
      n -= x; sum -= s;
      destination[i] = x;
   }
   // get the last one
   destination[i] = n;
}
Ejemplo n.º 8
0
Point BezierCurve::pointForT(float t) {
	Point pt(0, 0, 0);
	for (int i=0; i<curveOrder+1; i++) {
		float oneMinusT = 1;
		float T = 1;
		for (int j=0; j<curveOrder - i; j++) {
			oneMinusT *= 1-t;
		}
		for (int j=0; j<i; j++)
			T *= t;
		pt = pt + controlPoints[i]*Binomial(curveOrder, i)*oneMinusT*T;
	}
	return pt;
}
Ejemplo n.º 9
0
long Poisson(RndState *S, double lambda)
{
	long r;
	if (lambda>=15) {
		double m=floor(lambda*7/8);
		double x=Gamma(S,m);

		if (x>lambda) r=Binomial(S,lambda/x,m-1);
		else          r=m+Poisson(S,lambda-x);
	} else {
		double p, elambda = exp(-lambda);
		for (p=1, r=-1; p>elambda; p*=Uniform(S)) r++; 
	}
	return r;
}
Ejemplo n.º 10
0
Point BezierCurve::TangentVectorAtT(float t) {
	Point tangent(0, 0, 0);
	for (int i=0; i<curveOrder; i++) {
		float oneMinusT = 1;
		float T = 1;
		for (int j=0; j<curveOrder - i - 1; j++) {
			oneMinusT *= 1-t;
		}
		for (int j=0; j<i; j++)
			T *= t;
		tangent = tangent + (controlPoints[i+1]+controlPoints[i]*(-1))*Binomial(curveOrder-1, i)*oneMinusT*T;
	}
	tangent = tangent*curveOrder;

	return tangent;
}
Ejemplo n.º 11
0
int CountLHsTree (void)
{
/* This counts the number of labeled histories for a given rooted tree.
*/
   int i,k, nLH, nLR[NS-1][2], change, *sons, j;
   double y=0;

   for(i=com.ns; i<tree.nnode; i++) 
     if(nodes[i].nson!=2) error2("this works for rooted trees only");
   for(i=com.ns; i<tree.nnode; i++) 
      nLR[i-com.ns][0] = nLR[i-com.ns][1] = -1;
   for(k=0; k<com.ns; k++) {
      for(i=tree.nnode-1,change=0; i>=com.ns; i--) {
         sons = nodes[i].sons;
         for(j=0; j<2; j++) {
            if(nLR[i-com.ns][j] != -1) continue;
            if(sons[j] < com.ns) {
               nLR[i-com.ns][j] = 0;
               change = 1;
            }
            else if(nLR[sons[j]-com.ns][0] != -1 && nLR[sons[j]-com.ns][1] != -1) {
               nLR[i-com.ns][j] = nLR[sons[j]-com.ns][0] + nLR[sons[j]-com.ns][1] + 1;
               change = 1;
            }
         }
      }
      if(!change) break;
   }
   for(i=0,nLH=1; i<tree.nnode-com.ns; i++) {
      /*
      printf("\nnode %2d (%2d %2d): %2d %2d ", i+com.ns, nodes[i+com.ns].sons[0], nodes[i+com.ns].sons[1], nLR[i][0], nLR[i][1]);
      */
      if(nLR[i][0]==-1 || nLR[i][1]==-1)
         error2("nLR = -1");
      if(nLR[i][0] && nLR[i][1]) {
         nLH *= (int)Binomial((double)(nLR[i][0]+nLR[i][1]), min2(nLR[i][0], nLR[i][1]), &y);
         if(y) error2("y!=0 not considered");
      }
   }
   return(nLH);   
}
Ejemplo n.º 12
0
Archivo: check.c Proyecto: tmisawa/HPhi
/** 
 * @brief A program to check size of dimension for hirbert-space.
 * 
 * @param[in,out] X  Common data set used in HPhi.
 * 
 * @author Takahiro Misawa (The University of Tokyo)
 * @author Kazuyoshi Yoshimi (The University of Tokyo)
 * @return 
 */
int check(struct BindStruct *X){
    
  FILE *fp;
  long unsigned int i,tmp_sdim;
  int NLocSpn,NCond,Nup,Ndown;
  long unsigned int u_tmp;
  long unsigned int tmp;
  long unsigned int Ns,comb_1,comb_2,comb_3,comb_sum, comb_up, comb_down;
  int u_loc;
  int mfint[7];
  long int **comb;    
  
  Ns = X->Def.Nsite;
  li_malloc2(comb, Ns+1,Ns+1);
  //idim_max
  switch(X->Def.iCalcModel){
  case HubbardGC:
    //comb_sum = 2^(2*Ns)=4^Ns
    comb_sum = 1;
    for(i=0;i<2*X->Def.Nsite;i++){
      comb_sum= 2*comb_sum;     
    }
    break;
  case SpinGC:
    //comb_sum = 2^(Ns)
    comb_sum = 1;
    for(i=0;i<X->Def.Nsite;i++){
      comb_sum= 2*comb_sum;     
    }
    break;

  case Hubbard:
    comb_up= Binomial(Ns, X->Def.Nup, comb, Ns);
    comb_down= Binomial(Ns, X->Def.Ndown, comb, Ns);
    comb_sum=comb_up*comb_down;
    break;
  case Kondo:
    //idim_max
    // calculation of dimension
    // Nup      = u_loc+u_cond
    // Ndown    = d_loc+d_cond
    // NLocSpn  = u_loc+d_loc
    // Ncond    = Nsite-NLocSpn
    // idim_max = \sum_{u_loc=0}^{u_loc=Nup} 
    //              Binomial(NLocSpn,u_loc)
    //             *Binomial(NCond,Nup-u_loc)
    //             *Binomial(NCond,Ndown+u_loc-NLocSpn)
    //comb_1 = Binomial(NLocSpn,u_loc)
    //comb_2 = Binomial(NCond,Nup-u_loc)
    //comb_3 = Binomial(NCond,Ndown+u_loc-NLocSpn)
    Nup     = X->Def.Nup;
    Ndown   = X->Def.Ndown;
    NCond   = X->Def.Nsite-X->Def.NLocSpn;
    NLocSpn = X->Def.NLocSpn;
    comb_sum = 0;
    for(u_loc=0;u_loc<=X->Def.Nup;u_loc++){
      comb_1     = Binomial(NLocSpn,u_loc,comb,Ns);
      comb_2     = Binomial(NCond,Nup-u_loc,comb,Ns);
      comb_3     = Binomial(NCond,Ndown+u_loc-NLocSpn,comb,Ns);
      comb_sum  += comb_1*comb_2*comb_3;
    }
    break;
  case KondoGC:
    comb_sum = 1;
    NCond   = X->Def.Nsite-X->Def.NLocSpn;
    NLocSpn = X->Def.NLocSpn;
    //4^Nc*2^Ns
    for(i=0;i<(2*NCond+NLocSpn);i++){
      comb_sum= 2*comb_sum;     
    }
    break;
  case Spin:
    comb_sum= Binomial(Ns, X->Def.Ne, comb, Ns);
    break;
  default:
    printf(cErrNoModel, X->Def.iCalcModel);
    i_free2(comb, Ns+1, Ns+1);
    return -1;
  }  

  printf("comb_sum= %ld \n",comb_sum);
  X->Check.idim_max = comb_sum;
  X->Check.max_mem=(3+2+1)*X->Check.idim_max*16.0/(pow(10,9));
  printf("MAX DIMENSION idim_max=%ld \n",X->Check.idim_max);
  printf("APPROXIMATE REQUIRED MEMORY  max_mem=%lf GB \n",X->Check.max_mem);
  if(childfopen(cFileNameCheckMemory,"w", &fp)!=0){
    i_free2(comb, Ns+1, Ns+1);
    return -1;
  }
  fprintf(fp,"MAX DIMENSION idim_max=%ld \n",X->Check.idim_max);
  fprintf(fp,"APPROXIMATE REQUIRED MEMORY  max_mem=%lf GB \n",X->Check.max_mem);
  fclose(fp);

  //sdim 
  tmp=1;
  tmp_sdim=1;

  switch(X->Def.iCalcModel){
  case HubbardGC:
  case KondoGC:
  case Hubbard:
  case Kondo:
    while(tmp <= X->Def.Nsite){
      tmp_sdim=tmp_sdim*2;
      tmp+=1;
    }
    break;
  case Spin:
  case SpinGC:
    while(tmp <= X->Def.Nsite/2){
      tmp_sdim=tmp_sdim*2;
      tmp+=1;
    }
    break;
  default:
    printf(cErrNoModel, X->Def.iCalcModel);
    i_free2(comb, Ns+1, Ns+1);
    return -1;
  }  
  X->Check.sdim=tmp_sdim;
  
  if(childfopen(cFileNameCheckSdim,"w", &fp)!=0){
    i_free2(comb, Ns+1, Ns+1);
    return -1;
  }

  switch(X->Def.iCalcModel){
  case HubbardGC:
  case KondoGC:
  case Hubbard:
  case Kondo:
    printf("sdim=%ld =2^%d\n",X->Check.sdim,X->Def.Nsite);
    fprintf(fp,"sdim=%ld =2^%d\n",X->Check.sdim,X->Def.Nsite);
    break;
  case Spin:
  case SpinGC:
    printf("sdim=%ld =2^%d\n",X->Check.sdim,X->Def.Nsite/2);
    fprintf(fp,"sdim=%ld =2^%d\n",X->Check.sdim,X->Def.Nsite/2);
  default:
    break;
  }
  
  u_tmp=1;
  X->Def.Tpow[0]=u_tmp;
  switch(X->Def.iCalcModel){
  case HubbardGC:
  case KondoGC:
    for(i=1;i<=2*X->Def.Nsite;i++){
      u_tmp=u_tmp*2;
      X->Def.Tpow[i]=u_tmp;
      fprintf(fp,"%ld %ld \n",i,u_tmp);
    }
    break;
  case Hubbard:
  case Kondo:
    for(i=1;i<=2*X->Def.Nsite-1;i++){
      u_tmp=u_tmp*2;
      X->Def.Tpow[i]=u_tmp;
      fprintf(fp,"%ld %ld \n",i,u_tmp);
    }
    break;
 case SpinGC:
    for(i=1;i<=X->Def.Nsite;i++){
      u_tmp=u_tmp*2;
      X->Def.Tpow[i]=u_tmp;
      fprintf(fp,"%ld %ld \n",i,u_tmp);
    }
    break;
 case Spin:
    for(i=1;i<=X->Def.Nsite-1;i++){
      u_tmp=u_tmp*2;
      X->Def.Tpow[i]=u_tmp;
      fprintf(fp,"%ld %ld \n",i,u_tmp);
    }
    break;
  default:
    printf(cErrNoModel, X->Def.iCalcModel);
    i_free2(comb, Ns+1, Ns+1);
    return -1;
  }  
  fclose(fp);	 
 
  i_free2(comb, Ns+1, Ns+1);
  printf("Indices and Parameters of Definition files(*.def) are complete.\n");
  return 0;
}    
Ejemplo n.º 13
0
void unipoly::weight_enumerator_MDS_code(INT n, INT k, INT q, INT f_v, INT f_vv, INT f_vvv)
{
	INT j, h, l;
	base a, b, c, d, e;
	
	m_l_n(n + 1);
	e.m_i_i(-1);
	s_i(0).m_i_i(1);
	for (j = n - k + 1; j <= n; j++) {
		l = k - n + j - 1;
		a.change_to_integer();
		b.change_to_integer();
		c.change_to_integer();
		d.change_to_integer();
		if (f_vvv) {
			cout << "j=" << j << endl;
			}
		Binomial(n, j, a);
		if (f_vvv) {
			cout << " \\binom{" << n << "}{" << j << "}=a=" << a << endl;
			}
		b.m_i_i(0);
		for (h = 0; h <= l; h++) {
			if (f_vvv) {
				cout << " h=" << h;
				}
			Binomial(j, h, c);
			if (f_vvv) {
				cout << "  {j \\choose h} = c=" << c << endl;
				}
			d.m_i_i(q);
			d.power_int(l - h + 1);
			if (f_vvv) {
				cout << "  q^" << l - h + 1 << "=" << d << endl;
				}
			d += e;
			if (f_vvv) {
				cout << "  minus 1, d=" << d << endl;
				}
			c *= d;
			if (ODD(h)) {
				c.negate();
				}
			if (f_vvv) {
				cout << "  c=" << c << endl;
				}
			b += c;
			if (f_vvv) {
				cout << "  new b=" << b << endl;
				}
			}
		b *= a;
		if (f_vv) {
			cout << " coeff=" << b << endl;
			}
		s_i(j) = b;
		}
	if (f_v) {
		cout << "unipoly::weight_enumerator_MDS_code() n=" << n << " k=" << k << " q=" << q << endl;
		cout << *this << endl;
		}
}
Ejemplo n.º 14
0
Archivo: check.c Proyecto: QLMS/HPhi
/** 
 * @brief A program to check size of dimension for hirbert-space.
 * 
 * @param[in,out] X  Common data set used in HPhi.
 * 
 * @retval TRUE normally finished
 * @retval FALSE unnormally finished
 * @retval MPIFALSE CheckMPI unormally finished
 * @version 0.2
 * @details add function of calculating hirbert space for canonical ensemble.
 *  
 * @version 0.1
 * @author Takahiro Misawa (The University of Tokyo)
 * @author Kazuyoshi Yoshimi (The University of Tokyo)
 */
int check(struct BindStruct *X){
    
  FILE *fp;
  long unsigned int i,tmp_sdim;
  int NLocSpn,NCond,Nup,Ndown;
  long unsigned int u_tmp;
  long unsigned int tmp;
  long unsigned int Ns,comb_1,comb_2,comb_3,comb_sum, comb_up, comb_down;
  int u_loc;
  int mfint[7];
  long int **comb;    
  long unsigned int idimmax=0;
  long unsigned int idim=0;
  long unsigned int isite=0;
  int tmp_sz=0;
  int iMinup=0;
  int iAllup=X->Def.Ne;

  /*
    Set Site number per MPI process 
  */
  if(CheckMPI(X)!=TRUE){
    return MPIFALSE;
  }

  Ns = X->Def.Nsite;

  li_malloc2(comb, Ns+1,Ns+1);

  //idim_max
  switch(X->Def.iCalcModel){
  case HubbardGC:
    //comb_sum = 2^(2*Ns)=4^Ns
    comb_sum = 1;
    for(i=0;i<2*X->Def.Nsite;i++){
      comb_sum= 2*comb_sum;     
    }
    break;
  case SpinGC:
    //comb_sum = 2^(Ns)
    comb_sum = 1;
    if(X->Def.iFlgGeneralSpin ==FALSE){
      for(i=0;i<X->Def.Nsite;i++){
	comb_sum= 2*comb_sum;     
      }
    }
    else{
      for(i=0; i<X->Def.Nsite;i++){
	comb_sum=comb_sum*X->Def.SiteToBit[i];
      }
    }
    break;

  case Hubbard:
    comb_up= Binomial(Ns, X->Def.Nup, comb, Ns);
    comb_down= Binomial(Ns, X->Def.Ndown, comb, Ns);
    comb_sum=comb_up*comb_down;
    break;

  case HubbardNConserved:
    comb_sum=0;
    if(X->Def.Ne > X->Def.Nsite){
      iMinup = X->Def.Ne-X->Def.Nsite;
      iAllup = X->Def.Nsite;
    }

    for(i=iMinup; i<= iAllup; i++){
      comb_up= Binomial(Ns, i, comb, Ns);
      comb_down= Binomial(Ns, X->Def.Ne-i, comb, Ns);
      comb_sum +=comb_up*comb_down;
    }
    break;
    
  case Kondo:
    //idim_max
    // calculation of dimension
    // Nup      = u_loc+u_cond
    // Ndown    = d_loc+d_cond
    // NLocSpn  = u_loc+d_loc
    // Ncond    = Nsite-NLocSpn
    // idim_max = \sum_{u_loc=0}^{u_loc=Nup} 
    //              Binomial(NLocSpn,u_loc)
    //             *Binomial(NCond,Nup-u_loc)
    //             *Binomial(NCond,Ndown+u_loc-NLocSpn)
    //comb_1 = Binomial(NLocSpn,u_loc)
    //comb_2 = Binomial(NCond,Nup-u_loc)
    //comb_3 = Binomial(NCond,Ndown+u_loc-NLocSpn)
    Nup     = X->Def.Nup;
    Ndown   = X->Def.Ndown;
    NCond   = X->Def.Nsite-X->Def.NLocSpn;
    NLocSpn = X->Def.NLocSpn;
    comb_sum = 0;
    for(u_loc=0;u_loc<=X->Def.Nup;u_loc++){
      comb_1     = Binomial(NLocSpn,u_loc,comb,Ns);
      comb_2     = Binomial(NCond,Nup-u_loc,comb,Ns);
      comb_3     = Binomial(NCond,Ndown+u_loc-NLocSpn,comb,Ns);
      comb_sum  += comb_1*comb_2*comb_3;
    }
    break;
  case KondoGC:
    comb_sum = 1;
    NCond   = X->Def.Nsite-X->Def.NLocSpn;
    NLocSpn = X->Def.NLocSpn;
    //4^Nc*2^Ns
    for(i=0;i<(2*NCond+NLocSpn);i++){
      comb_sum= 2*comb_sum;     
    }
    break;
  case Spin:

    if(X->Def.iFlgGeneralSpin ==FALSE){
      if(X->Def.Nup+X->Def.Ndown != X->Def.Nsite){
	fprintf(stderr, " 2Sz is incorrect.\n");
	return FALSE;
      }
      comb_sum= Binomial(Ns, X->Def.Ne, comb, Ns);
    }
    else{
      idimmax = 1;
      X->Def.Tpow[0]=idimmax;
      for(isite=0; isite<X->Def.Nsite;isite++){
	idimmax=idimmax*X->Def.SiteToBit[isite];
	X->Def.Tpow[isite+1]=idimmax;
      }
      comb_sum=0;
#pragma omp parallel for default(none) reduction(+:comb_sum) private(tmp_sz, isite) firstprivate(idimmax, X) 
      for(idim=0; idim<idimmax; idim++){
	tmp_sz=0;
	for(isite=0; isite<X->Def.Nsite;isite++){
	  tmp_sz += GetLocal2Sz(isite+1,idim, X->Def.SiteToBit, X->Def.Tpow );	  
	}
	if(tmp_sz == X->Def.Total2Sz){
	  comb_sum +=1;
	}
      }
      
    }
    
    break;
  default:
    fprintf(stderr, cErrNoModel, X->Def.iCalcModel);
    i_free2(comb, Ns+1, Ns+1);
    return FALSE;
  }  

  if(comb_sum==0){
    fprintf(stderr, "%s", cErrNoHilbertSpace);
    //    return FALSE;
  }
  
  //fprintf(stdoutMPI, "comb_sum= %ld \n",comb_sum);

  X->Check.idim_max = comb_sum;
  switch(X->Def.iCalcType){
  case Lanczos:
    switch(X->Def.iCalcModel){
    case Hubbard:
    case HubbardNConserved:
    case Kondo:
    case KondoGC:
    case Spin:
      X->Check.max_mem=5.5*X->Check.idim_max*8.0/(pow(10,9));
      break;
    case HubbardGC:
    case SpinGC:
      X->Check.max_mem=4.5*X->Check.idim_max*8.0/(pow(10,9));
      break;
    }
    break;
  case TPQCalc:
    switch(X->Def.iCalcModel){
    case Hubbard:
    case HubbardNConserved:
    case Kondo:
    case KondoGC:
    case Spin:
      if(X->Def.iFlgCalcSpec != CALCSPEC_NOT){
        X->Check.max_mem=(2)*X->Check.idim_max*16.0/(pow(10,9));
      }
      else {
        X->Check.max_mem = 4.5 * X->Check.idim_max * 16.0 / (pow(10, 9));
      }
      break;
    case HubbardGC:
    case SpinGC:
      if(X->Def.iFlgCalcSpec != CALCSPEC_NOT){
        X->Check.max_mem=(2)*X->Check.idim_max*16.0/(pow(10,9));
      }
      else {
        X->Check.max_mem = 3.5 * X->Check.idim_max * 16.0 / (pow(10, 9));
      }
      break;
    }
    break;
  case FullDiag:
    X->Check.max_mem=X->Check.idim_max*8.0*X->Check.idim_max*8.0/(pow(10,9));
    break;
  default:
    return FALSE;
    //break;
  }

  //fprintf(stdoutMPI, "  MAX DIMENSION idim_max=%ld \n",X->Check.idim_max);
  //fprintf(stdoutMPI, "  APPROXIMATE REQUIRED MEMORY  max_mem=%lf GB \n",X->Check.max_mem);
  unsigned long int li_dim_max=MaxMPI_li(X->Check.idim_max);
  fprintf(stdoutMPI, "  MAX DIMENSION idim_max=%ld \n",li_dim_max);
  double dmax_mem=MaxMPI_d(X->Check.max_mem);
  fprintf(stdoutMPI, "  APPROXIMATE REQUIRED MEMORY  max_mem=%lf GB \n",dmax_mem);
  if(childfopenMPI(cFileNameCheckMemory,"w", &fp)!=0){
    i_free2(comb, Ns+1, Ns+1);
    return FALSE;
  }
  fprintf(fp,"  MAX DIMENSION idim_max=%ld \n", li_dim_max);
  fprintf(fp,"  APPROXIMATE REQUIRED MEMORY  max_mem=%lf GB \n", dmax_mem);

  
  /*
  fprintf(fp,"  MAX DIMENSION idim_max=%ld \n",X->Check.idim_max);
  fprintf(fp,"  APPROXIMATE REQUIRED MEMORY  max_mem=%lf GB \n",X->Check.max_mem);
  */
  fclose(fp);

  //sdim 
  tmp=1;
  tmp_sdim=1;

  switch(X->Def.iCalcModel){
  case HubbardGC:
  case KondoGC:
  case HubbardNConserved:
  case Hubbard:
  case Kondo:
    while(tmp <= X->Def.Nsite){
      tmp_sdim=tmp_sdim*2;
      tmp+=1;
    }
    break;
  case Spin:
  case SpinGC:
    if(X->Def.iFlgGeneralSpin==FALSE){ 
      while(tmp <= X->Def.Nsite/2){
	tmp_sdim=tmp_sdim*2;
	tmp+=1;
      }
    }
    else{
      GetSplitBitForGeneralSpin(X->Def.Nsite, &tmp_sdim, X->Def.SiteToBit);
    }
    break;
  default:
    fprintf(stdoutMPI, cErrNoModel, X->Def.iCalcModel);
    i_free2(comb, Ns+1, Ns+1);
    return FALSE;
  }  
  X->Check.sdim=tmp_sdim;
  
  if(childfopenMPI(cFileNameCheckSdim,"w", &fp)!=0){
    i_free2(comb, Ns+1, Ns+1);
    return FALSE;
  }

  switch(X->Def.iCalcModel){
  case HubbardGC:
  case KondoGC:
  case HubbardNConserved:
  case Hubbard:
  case Kondo:
    //fprintf(stdoutMPI, "sdim=%ld =2^%d\n",X->Check.sdim,X->Def.Nsite);
    fprintf(fp,"sdim=%ld =2^%d\n",X->Check.sdim,X->Def.Nsite);
    break;
  case Spin:
  case SpinGC:
    if(X->Def.iFlgGeneralSpin==FALSE){
      //fprintf(stdoutMPI, "sdim=%ld =2^%d\n",X->Check.sdim,X->Def.Nsite/2);
      fprintf(fp,"sdim=%ld =2^%d\n",X->Check.sdim,X->Def.Nsite/2);
    }
    break;
  default:
    break;
  }  
 
  i_free2(comb, Ns+1, Ns+1);

  u_tmp=1;
  X->Def.Tpow[0]=u_tmp;
  switch(X->Def.iCalcModel){
  case HubbardGC:
  case KondoGC:
    for(i=1;i<=2*X->Def.Nsite;i++){
      u_tmp=u_tmp*2;
      X->Def.Tpow[i]=u_tmp;
      fprintf(fp,"%ld %ld \n",i,u_tmp);
    }
    break;
  case HubbardNConserved:
  case Hubbard:
  case Kondo:
    for(i=1;i<=2*X->Def.Nsite-1;i++){
      u_tmp=u_tmp*2;
      X->Def.Tpow[i]=u_tmp;
      fprintf(fp,"%ld %ld \n",i,u_tmp);
    }
    break;
 case SpinGC:
   if(X->Def.iFlgGeneralSpin==FALSE){
     for(i=1;i<=X->Def.Nsite;i++){
       u_tmp=u_tmp*2;
       X->Def.Tpow[i]=u_tmp;
       fprintf(fp,"%ld %ld \n",i,u_tmp);
     }
   }
   else{
     X->Def.Tpow[0]=u_tmp;
     fprintf(fp,"%d %ld \n", 0, u_tmp);
      for(i=1;i<X->Def.Nsite;i++){
	u_tmp=u_tmp*X->Def.SiteToBit[i-1];
	X->Def.Tpow[i]=u_tmp;
	fprintf(fp,"%ld %ld \n",i,u_tmp);
      }
   }
   break;
 case Spin:
   if(X->Def.iFlgGeneralSpin==FALSE){
     for(i=1;i<=X->Def.Nsite-1;i++){
       u_tmp=u_tmp*2;
       X->Def.Tpow[i]=u_tmp;
       fprintf(fp,"%ld %ld \n",i,u_tmp);
     }
   }
   else{
     for(i=0;i<X->Def.Nsite;i++){
       fprintf(fp,"%ld %ld \n",i,X->Def.Tpow[i]);
     }
   }     
    break;
  default:
    fprintf(stdoutMPI, cErrNoModel, X->Def.iCalcModel);
    i_free2(comb, Ns+1, Ns+1);
    return FALSE;
  }  
  fclose(fp);	 
  /*
    Print MPI-site information and Modify Tpow 
    in the inter process region.
  */
  CheckMPI_Summary(X);
  
  return TRUE;
}    
Ejemplo n.º 15
0
Point BezierCurve::AccelerationVectorAtT(float t) {
	Point accVector(0, 0, 0);
	for (int i=0; i<curveOrder - 1; i++) {
		float oneMinusT = 1;
		float T = 1;
		for (int j=0; j<curveOrder - 2 - i; j++)
			oneMinusT *= 1-t;
		for (int j=0; j<i; j++)
			T *= t;
		accVector = accVector + (controlPoints[i+2] + controlPoints[i+1]*(-2) + controlPoints[i])*Binomial(curveOrder - 2, i)*oneMinusT*T;
	}
	accVector = accVector*curveOrder*(curveOrder - 1);

	return accVector;
}