Example #1
0
int main() {
    APPROX int x;
    x = 5;

    // Flow violation.
    int y;
    y = x; // expected-error {{precision flow violation}}
    x = x; // OK
    y = y; // OK

    // Approximate & precise conditions.
    if (x) {} // expected-error {{approximate condition}}
    while (x) {} // expected-error {{approximate condition}}
    for (int i=2; i<x; ++i) {} // expected-error {{approximate condition}}
    switch (x) {} // expected-error {{approximate condition}}
    if (1) {} // OK
    if (y) {} // OK

    // Pointers and such.
    APPROX int* xp;
    int* yp;
    xp = &x; // OK
    yp = &x; // expected-error {{precision flow violation}}
    xp = &y; // expected-error {{precision flow violation}}
    yp = &y; // OK
    if (*yp) {} // OK
    if (*xp) {} // expected-error {{approximate condition}}
    APPROX int ax[] = {3, 4, 5};
    int ay[] = {3, 4, 5};
    if (ax[0]) {} // expected-error {{approximate condition}}
    if (ay[0]) {} // OK

    // Pointer types are incompatible.
    xp = xp; // OK
    yp = yp; // OK
    xp = yp; // expected-error {{precision flow violation}}
    yp = xp; // expected-error {{precision flow violation}}

    // Function calls.
    afunc(2); // OK
    pfunc(2); // OK
    aafunc(2); // OK
    afunc(x); // OK
    aafunc(x); // OK
    pfunc(x); // expected-error {{precision flow violation}}
    y = aafunc(x); // expected-error {{precision flow violation}}
    x = aafunc(x); // OK
    ppfunc(xp); // expected-error {{precision flow violation}}

    // Variable initialization.
    int pinit = x; // expected-error {{precision flow violation}}

    return 0;
}
Example #2
0
int
main(int argc, char **argv)	/* arguments aren't used */
{
	char *p, *b, *nb;

	printf("Text Locations:\n");
	printf("\tAddress of main: %p\n", main);
	printf("\tAddress of afunc: %p\n", afunc);

	printf("Stack Locations:\n");
	afunc();

	p = (char *) alloca(32);
	if (p != NULL) {
		printf("\tStart of alloca()'ed array: %p\n", p);
		printf("\tEnd of alloca()'ed array: %p\n", p + 31);
	}

	printf("Data Locations:\n");
	printf("\tAddress of data_var: %p\n", & data_var);

	printf("BSS Locations:\n");
	printf("\tAddress of bss_var: %p\n", & bss_var);

	b = sbrk((ptrdiff_t) 32);	/* grow address space */
	nb = sbrk((ptrdiff_t) 0);
	printf("Heap Locations:\n");
	printf("\tInitial end of heap: %p\n", b);
	printf("\tNew end of heap: %p\n", nb);

	b = sbrk((ptrdiff_t) -16);	/* shrink it */
	nb = sbrk((ptrdiff_t) 0);
	printf("\tFinal end of heap: %p\n", nb);
}
Example #3
0
void NR::svdfit(Vec_I_DP &x, Vec_I_DP &y, Vec_I_DP &sig, Vec_O_DP &a,
	Mat_O_DP &u, Mat_O_DP &v, Vec_O_DP &w, DP &chisq,
	void funcs(const DP, Vec_O_DP &))
{
	int i,j;
	const DP TOL=1.0e-13;
	DP wmax,tmp,thresh,sum;

	int ndata=x.size();
	int ma=a.size();
	Vec_DP b(ndata),afunc(ma);
	for (i=0;i<ndata;i++) {
		funcs(x[i],afunc);
		tmp=1.0/sig[i];
		for (j=0;j<ma;j++) u[i][j]=afunc[j]*tmp;
		b[i]=y[i]*tmp;
	}
	svdcmp(u,w,v);
	wmax=0.0;
	for (j=0;j<ma;j++)
		if (w[j] > wmax) wmax=w[j];
	thresh=TOL*wmax;
	for (j=0;j<ma;j++)
		if (w[j] < thresh) w[j]=0.0;
	svbksb(u,w,v,b,a);
	chisq=0.0;
	for (i=0;i<ndata;i++) {
		funcs(x[i],afunc);
		sum=0.0;
		for (j=0;j<ma;j++) sum += a[j]*afunc[j];
		chisq += (tmp=(y[i]-sum)/sig[i],tmp*tmp);
	}
}
void test_multilayer()
{
    std::ifstream f("test.txt");
    int n;
    f >> n;
    std::vector<std::vector<double>> input(n);
    std::vector<std::vector<double>> output(n);
    for(int i = 0; i < n; i++)
    {
        input[i].resize(3);
        output[i].resize(1);
        int temp;
        f >> temp >> input[i][0] >> input[i][1] >> input[i][2] >> output[i][0];
    }
    double scale = 100.0;
    for(int i = 0; i < n; i++)
        output[i][0] /= scale;
    af::MultiLayerPerceptron nn;
    std::vector<int> n_layers;
    n_layers.push_back(20);
    n_layers.push_back(5);
    //n_layers.push_back(3);
    std::tr1::shared_ptr<af::ActivationFunction> afunc(new af::Sigmoid());
    nn.init(3,n_layers,1,afunc,1000,0.5);
    nn.learning(input,output);
    for(int i = 0; i < (int)input.size(); i++)
        std::cout << nn.run(input[i])[0] * scale - output[i][0] * scale << std::endl;

}
/*void improved_euler(vector<Point>* list,void ffunc(vector<Point>* list,pfloat x,pfloat y,pfloat z),pfloat delta)
{

}*/
void euler(Point* list,int num,void afunc(pfloat* ax,pfloat* ay,pfloat* az,pfloat x,pfloat y,pfloat z),pfloat delta)
{
	int i=0;
	for(i=0;i<(int)num;i++)
	{
		Point* pt=(list+i);
		pfloat xf=0.0;
		pfloat yf=0.0;
		pfloat zf=0.0;
		afunc(&xf,&yf,&zf,pt->xpos->data,pt->ypos->data,pt->zpos->data);
		pt->xacc->front->data=xf;
		pt->yacc->front->data=yf;
		pt->zacc->front->data=zf;

		pt->xvel->front->data=pt->xvel->data+delta*(pt->xacc->data+pt->xacc->front->data)/2;
		pt->yvel->front->data=pt->yvel->data+delta*(pt->yacc->data+pt->yacc->front->data)/2;
		pt->zvel->front->data=pt->zvel->data+delta*(pt->zacc->data+pt->zacc->front->data)/2;

		pt->xpos->front->data=pt->xpos->data+delta*(pt->xvel->data+pt->xvel->front->data)/2;
		pt->ypos->front->data=pt->ypos->data+delta*(pt->yvel->data+pt->yvel->front->data)/2;
		pt->zpos->front->data=pt->zpos->data+delta*(pt->zvel->data+pt->zvel->front->data)/2;
	}
	for(i=0;i<(int)num;i++){
		advance(list+i);
	}
}
Example #6
0
/*ARGSUSED1*/
void
key(unsigned char key, int x, int y)
{
    switch (key) {
    case 'a':
	afunc();
	break;
    case 'b':
	bfunc();
	break;
    case 'h':
	help();
	break;
    case 't':
	tfunc();
	break;
    case 'e':
	explode();
	break;
    case '\033':
	exit(EXIT_SUCCESS);
	break;
    default:
	break;
    }
    glutPostRedisplay();
}
void euler(vector<Point>* list,void afunc(pfloat* ax,pfloat* ay,pfloat* az,pfloat x,pfloat y,pfloat z),pfloat delta)
{
	for(int i=0;i<(int)list->size();i++)
	{
		Point* pt=&(list->at(i));
		pfloat xf=0.0;
		pfloat yf=0.0;
		pfloat zf=0.0;
		afunc(&xf,&yf,&zf,pt->xpos->data,pt->ypos->data,pt->zpos->data);
		pt->xacc->front->data=xf;
		pt->yacc->front->data=yf;
		pt->zacc->front->data=zf;

		pt->xvel->front->data=pt->xvel->data+delta*(pt->xacc->data+pt->xacc->front->data)/2;
		pt->yvel->front->data=pt->yvel->data+delta*(pt->yacc->data+pt->yacc->front->data)/2;
		pt->zvel->front->data=pt->zvel->data+delta*(pt->zacc->data+pt->zacc->front->data)/2;

		pt->xpos->front->data=pt->xpos->data+delta*(pt->xvel->data+pt->xvel->front->data)/2;
		pt->ypos->front->data=pt->ypos->data+delta*(pt->yvel->data+pt->yvel->front->data)/2;
		pt->zpos->front->data=pt->zpos->data+delta*(pt->zvel->data+pt->zvel->front->data)/2;
	}
	for(int i=0;i<(int)list->size();i++){
		list->at(i).advance();
	}
}
Example #8
0
int main() {
    APPROX int x;
    x = 5;

    // Flow violation.
    int y;
    y = x; // error
    x = x; // OK
    y = y; // OK

    // Approximate & precise conditions.
    if (x) {} // error
    while (x) {} // error
    for (int i=2; i<x; ++i) {} // error
    switch (x) {} // error
    if (1) {} // OK
    if (y) {} // OK

    // Pointers and such.
    APPROX int* xp;
    int* yp;
    xp = &x; // OK
    yp = &x; // error
    xp = &y; // OK
    yp = &y; // OK
    if (*yp) {} // OK
    if (*xp) {} // error
    APPROX int ax[] = {3, 4, 5};
    int ay[] = {3, 4, 5};
    if (ax[0]) {} // error
    if (ay[0]) {} // OK

    // Function calls.
    afunc(2); // OK
    pfunc(2); // OK
    aafunc(2); // OK
    afunc(x); // OK
    aafunc(x); // OK
    pfunc(x); // error
    y = aafunc(x); // error
    x = aafunc(x); // OK

    // Variable initialization.
    int pinit = x; // error

    return 0;
}
Example #9
0
void lfit(vector_t &x, vector_t &y, vector_t &sig, vector_t &a,
 	  vector<bool> &ia, matrix_t &covar, double &chisq, matrix_t & X)
{
  int i,j,k,l,m,mfit=0;
  double ym,wt,sum,sig2i;
  
  int ndat=x.size();
  int ma=a.size();
  vector_t afunc(ma);
  matrix_t beta;
  sizeMatrix(beta,ma,1);
  for (j=0;j<ma;j++)
    if (ia[j]) mfit++;
  if (mfit == 0) error("lfit: no parameters to be fitted");
  for (j=0;j<mfit;j++) {
    for (k=0;k<mfit;k++) covar[j][k]=0.0;
    beta[j][0]=0.0;
  }
  for (i=0;i<ndat;i++) {
    afunc = X[i];

    ym=y[i];
    if (mfit < ma) {
      for (j=0;j<ma;j++)
	if (!ia[j]) ym -= a[j]*afunc[j];
    }
    sig2i=1.0/SQR(sig[i]);
    for (j=0,l=0;l<ma;l++) {
      if (ia[l]) {
	wt=afunc[l]*sig2i;
	for (k=0,m=0;m<=l;m++)
	  if (ia[m]) covar[j][k++] += wt*afunc[m];
	beta[j++][0] += ym*wt;
      }
    }
  }
  for (j=1;j<mfit;j++)
    for (k=0;k<j;k++)
      covar[k][j]=covar[j][k];
  vector<vector<double> >  temp;
  sizeMatrix(temp,mfit,mfit);
  for (j=0;j<mfit;j++)
    for (k=0;k<mfit;k++)
      temp[j][k]=covar[j][k];
  gaussj(temp,beta);
  for (j=0;j<mfit;j++)
    for (k=0;k<mfit;k++)
      covar[j][k]=temp[j][k];
  for (j=0,l=0;l<ma;l++)
    if (ia[l]) a[l]=beta[j++][0];
  chisq=0.0;
  for (i=0;i<ndat;i++) {
    afunc = X[i];
    sum=0.0;
    for (j=0;j<ma;j++) sum += a[j]*afunc[j];
    chisq += SQR((y[i]-sum)/sig[i]);
  }
  covsrt(covar,ia,mfit);
}
Example #10
0
void
afunc(void)
{
	static int level = 0;		/* recursion level */
	auto int stack_var;		/* automatic variable, on stack */

	if (++level == 3)		/* avoid infinite recursion */
		return;

	printf("\tStack level %d: address of stack_var: %p\n",
			level, & stack_var);
	afunc();			/* recursive call */
}
void vertlet_integration(vector<Point>* list,void afunc(pfloat* ax,pfloat* ay,pfloat* az,pfloat x,pfloat y,pfloat z),pfloat delta)
{
	pfloat dt2=delta*delta;
	for(int i=0;i<(int)list->size();i++){
		Point* pt=&(list->at(i));
		pfloat ax=0.0;
		pfloat ay=0.0;
		pfloat az=0.0;
		afunc(&ax,&ay,&az,pt->xpos->data,pt->ypos->data,pt->zpos->data);
		pt->xpos->front->data=2*pt->xpos->data-pt->xpos->back->data+ax*dt2;
		pt->ypos->front->data=2*pt->ypos->data-pt->ypos->back->data+ay*dt2;
		pt->zpos->front->data=2*pt->zpos->data-pt->zpos->back->data+az*dt2;
	}
	for(int i=0;i<(int)list->size();i++){
		list->at(i).advance();
	}
}
void vertlet_integration(Point* list,int num,void afunc(pfloat* ax,pfloat* ay,pfloat* az,pfloat x,pfloat y,pfloat z),pfloat delta)
{
	pfloat dt2=delta*delta;
	int i=0;
	for(i=0;i<(int)num;i++){
		Point* pt=(list+i);
		pfloat ax=0.0;
		pfloat ay=0.0;
		pfloat az=0.0;
		afunc(&ax,&ay,&az,pt->xpos->data,pt->ypos->data,pt->zpos->data);
		pt->xpos->front->data=2*pt->xpos->data-pt->xpos->back->data+ax*dt2;
		pt->ypos->front->data=2*pt->ypos->data-pt->ypos->back->data+ay*dt2;
		pt->zpos->front->data=2*pt->zpos->data-pt->zpos->back->data+az*dt2;
	}
	for(i=0;i<(int)num;i++){
		advance(list+i);
	}
}
void test_xor()
{
    //xor function
    std::vector<std::vector<double>> input(4);
    std::vector<double> output(4);
    for(int i = 0; i < (int)input.size(); i++)
    {
        input[i].resize(2);
    }
    input[0][0] = 0;
    input[0][1] = 0;
    input[1][0] = 0;
    input[1][1] = 1;
    input[2][0] = 1;
    input[2][1] = 0;
    input[3][0] = 1;
    input[3][1] = 1;

    output[0] = 0;
    output[1] = 1;
    output[2] = 1;
    output[3] = 1;

    if(false)
    {
        af::SinglePerceptron nn;
        std::tr1::shared_ptr<af::ActivationFunction> afunc(new af::Sigmoid());
        nn.init(2,afunc,100000,0.1);
        nn.learning(input,output);
        std::cout << "single perceptron" << std::endl;
        for(int i = 0; i < 4; i++)
            std::cout << input[i][0] << " " << input[i][1] << " " << nn.run(input[i]) << std::endl;
    }
    if(false)
    {
        af::LayerPerceptron nn;
        std::tr1::shared_ptr<af::ActivationFunction> afunc(new af::Sigmoid());
        nn.init(2,1,afunc,10000,0.1);
        std::vector<std::vector<double>> output1;
        std::vector<double> temp1;
        std::vector<double> temp2;
        temp1.push_back(0);
        temp2.push_back(1);
        output1.push_back(temp1);
        output1.push_back(temp2);
        output1.push_back(temp2);
        output1.push_back(temp1);
        nn.learning(input,output1);
        std::cout << "single layer perceptron" << std::endl;
        for(int i = 0; i < 4; i++)
            std::cout << input[i][0] << " " << input[i][1] << " " <<nn.run(input[i])[0] << std::endl;
    }
    //if(false)
    {
        af::MultiLayerPerceptron nn;
        std::vector<int> n_layers;
        n_layers.push_back(5);
        std::tr1::shared_ptr<af::ActivationFunction> afunc(new af::Sigmoid());
        nn.init(2,n_layers,1,afunc,100000,0.2);
        std::vector<std::vector<double>> output1;
        std::vector<double> temp1, temp2;
        temp1.push_back(0);
        temp2.push_back(1);
        output1.push_back(temp1);
        output1.push_back(temp2);
        output1.push_back(temp2);
        output1.push_back(temp1);
        nn.learning(input,output1);
        std::cout << "multi layer perceptron" << std::endl;
        for(int i = 0; i < 4; i++)
            std::cout << input[i][0] << " " << input[i][1] << " " << nn.run(input[i])[0] << std::endl;
    }
}
Example #14
0
void LinearModel::fitLM() 
{

  if (par::verbose)
    {
      for (int i=0; i<nind; i++)
	{
	  cout << "VO " << i << "\t"
	       << Y[i] << "\t";
 	  for (int j=0; j<np; j++)
	    cout << X[i][j] << "\t";
	  cout << "\n";
	}
    }

//   cout << "LM VIEW\n";
//       display(Y);
//       display(X);
//       cout << "---\n";

  coef.resize(np);
  sizeMatrix(S,np,np);

  if ( np==0 || nind==0 || ! all_valid )
    {
      return;
    }
  
  setVariance();

  if ( par::standard_beta )
    standardise();


  sig.resize(nind, sqrt(1.0/sqrt((double)nind)) );
  
  w.resize(np);
  sizeMatrix(u,nind,np);
  sizeMatrix(v,np,np);
  

  //  Perform "svdfit(C,Y,sig,b,u,v,w,chisq,function)"
  
  int i,j;
  const double TOL=1.0e-13;
  double wmax,tmp,thresh,sum;
    
  vector_t b(nind),afunc(np);
  for (i=0;i<nind;i++) {
    afunc = X[i];
    tmp=1.0/sig[i];
    for (j=0;j<np;j++) 
      u[i][j]=afunc[j]*tmp;     
    b[i]=Y[i]*tmp;
  }

  bool flag = svdcmp(u,w,v);
  
  if ( ! flag ) 
    {
      all_valid = false;
      return;
    }

  wmax=0.0;
  for (j=0;j<np;j++)
    if (w[j] > wmax) wmax=w[j];
  thresh=TOL*wmax;
  for (j=0;j<np;j++)
    if (w[j] < thresh) w[j]=0.0;
  
  svbksb(u,w,v,b,coef);
  
  chisq=0.0;
  for (i=0;i<nind;i++) {
    afunc=X[i];
    sum=0.0;
    for (j=0;j<np;j++) sum += coef[j]*afunc[j];
    chisq += (tmp=(Y[i]-sum)/sig[i],tmp*tmp);
  }



  /////////////////////////////////////////
  // Obtain covariance matrix of estimates


  // Robust cluster variance estimator
  // V_cluster = (X'X)^-1 * \sum_{j=1}^{n_C} u_{j}' * u_j * (X'X)^-1 
  // where u_j = \sum_j cluster e_i * x_i 

  // Above, e_i is the residual for the ith observation and x_i is a
  // row vector of predictors including the constant.

  // For simplicity, I omitted the multipliers (which are close to 1)
  // from the formulas for Vrob and Vclusters.

  // The formula for the clustered estimator is simply that of the
  // robust (unclustered) estimator with the individual ei*s replaced
  // by their sums over each cluster. 

  // http://www.stata.com/support/faqs/stat/cluster.html
  // SEE http://aje.oxfordjournals.org/cgi/content/full/kwm223v1#APP1

  // Williams, R. L. 2000.  A note on robust variance estimation for
  // cluster-correlated data. Biometrics 56: 64

  //  t ( y - yhat X  ) %*%  ( y - yhat)  / nind - np
  // = variance of residuals 
  // j <- ( t( y- m %*% t(b) ) %*% ( y - m %*% t(b) ) ) / ( N - p ) 
  // print( sqrt(kronecker( solve( t(m) %*% m ) , j )  ))
  

  ////////////////////////////////////////////////
  // OLS variance estimator = s^2 * ( X'X )^-1
  // where s^2 = (1/(N-k)) \sum_i=1^N e_i^2
  
  // 1. Calcuate S = (X'X)^-1
  
  matrix_t Xt;
  sizeMatrix(Xt, np, nind);
  for (int i=0; i<nind; i++)
    for (int j=0; j<np; j++) 
      Xt[j][i] = X[i][j];
  matrix_t S0; 
  multMatrix(Xt,X,S0);
  flag = true;
  S0 = svd_inverse(S0,flag);  
  if ( ! flag ) 
    {
      all_valid = false;
      return;
    }

  if (par::verbose)
    {
      cout << "beta...\n";
      display(coef);
      cout << "Sigma(S0b)\n";
      display(S0);
      cout << "\n";
    }


  ////////////////////////
  // Calculate s^2 (sigma)

  if (!cluster)
    {
      double sigma= 0.0;
      for (int i=0; i<nind; i++)
	{
	  double partial = 0.0;
	  for (int j=0; j<np; j++)
	    partial += coef[j] * X[i][j];
	  partial -= Y[i];
	  sigma += partial * partial;
	}
      sigma /= nind-np;	
            
      for (int i=0; i<np; i++)
	for (int j=0; j<np; j++)
	  S[i][j] = S0[i][j] * sigma;
    }
  

  ///////////////////////////
  // Robust-cluster variance

  if (cluster)
  {
    
    vector<vector_t> sc(nc);
    for (int i=0; i<nc; i++)
      sc[i].resize(np,0);

    for (int i=0; i<nind; i++)
      {
	double partial = 0.0;
	for (int j=0; j<np; j++)
	  partial += coef[j] * X[i][j];
	partial -= Y[i];
	
	for (int j=0; j<np; j++)
	  sc[clst[i]][j] += partial * X[i][j];
      }
    
    matrix_t meat;
    sizeMatrix(meat, np, np);
    for (int k=0; k<nc; k++)
     {      
       for (int i=0; i<np; i++)
	 for (int j=0; j<np; j++)
	   meat[i][j] += sc[k][i] * sc[k][j];
       
     }
    
   matrix_t tmp1;
   multMatrix( S0 , meat, tmp1);
   multMatrix( tmp1 , S0, S);
   
  }

  
  if (par::verbose)
    {
      cout << "coefficients:\n";
      display(coef);
      cout << "var-cov matrix:\n";
      display(S);
      cout << "\n";
    }
  
}