int gaussSeidel(MAT &A,VEC b,VEC &x,int maxIter,double tol)
{
    int k = 0;
    double theta=0.0;
    VEC newx(A.dim()),ans(A.dim()),temp(A.dim());
    MAT LU(A.dim()),a = A;

    LU = luFact(a);       //LU in-place decomposition
    temp=fwdSubs(LU,b);   //forward substitution
    ans= bckSubs(LU,temp);//backward substitution
   
    while( k < maxIter)
    {
		x = newx;
		for(int i=0; i< A.dim(); i++)
		{
			theta = 0.0;
			for(int j=0; j < A.dim();j++){
				if(i!=j){
					theta += (A[i][j]*newx[j]);
				}
			}
			newx[i]= (b[i]-theta)/A[i][i];
		}
		k++;        
		if (linfnorm(newx - x) < tol)
			break;
    }
    printf("Diffrence w.r.t. hw4: %E\n",linfnorm(newx-ans));
    
    return k;
}
Beispiel #2
0
bool PreconditionedDownhillType::improve_energy(bool verbose) {
  iter++;
  const double old_energy = energy();
  const VectorXd g = pgrad();
  // Let's immediately free the cached gradient stored internally!
  invalidate_cache();
  // We waste some memory storing newx (besides *x itself), but this
  // avoids roundoff weirdness of trying to add nu*g back to *x, which
  // won't always get us back to the same value.
  Grid newx(gd, *x - nu*g);
  double newE = f.integral(kT, newx);
  int num_tries = 0;
  while (better(old_energy,newE)) {
    nu *= 0.5;
    newx = *x - nu*g;
    newE = f.integral(kT, newx);
    if (num_tries++ > 40) {
      printf("PreconditionedDownhill giving up after %d tries...\n", num_tries);
      return false; // It looks like we can't do any better with this algorithm.
    }
  }
  *x = newx;
  invalidate_cache();
  nu *= 1.1;
  if (verbose) {
    //lm->print_info();
    print_info();
  }
  return true;
}
Beispiel #3
0
int main(int argc, char** argv)
{
  time_t tm0, tm1;

  assert(argc==2);
  map<string, string> opts; optionsCreate(argv[1], opts);
  map<string,string>::iterator mi;
  
  int m;
  mi = opts.find("-m"); assert(mi!=opts.end());
  { istringstream ss((*mi).second); ss>>m; }
  int n;
  mi = opts.find("-n"); assert(mi!=opts.end());
  { istringstream ss((*mi).second); ss>>n; }
  int p;
  mi = opts.find("-p"); assert(mi!=opts.end());
  { istringstream ss((*mi).second); ss>>p; }
  
  int nbscales;
  mi = opts.find("-nbscales"); assert(mi!=opts.end());
  { istringstream ss((*mi).second); ss>>nbscales; }
  
  int nbdstz_coarse;
  mi = opts.find("-nbdstz_coarse"); assert(mi!=opts.end());
  { istringstream ss((*mi).second); ss>>nbdstz_coarse; }
  
  srand48( (long)time(NULL) );
  CpxNumTns x(m,n,p);
  for(int i=0; i<m; i++)	 for(int j=0; j<n; j++)		for(int k=0; k<p; k++)		  x(i,j,k) = cpx(drand48(), 0);
  
  tm0 = time(NULL);
  
  vector< vector<double> > fxs,fys,fzs;
  vector< vector<int> > nxs,nys,nzs;
  fdct3d_param(m, n, p, nbscales, nbdstz_coarse, fxs,fys,fzs,nxs,nys,nzs);
  tm1 = time(NULL);  cout<<"fdct3d_param "<<difftime(tm1,tm0)<<" seconds"<<endl;  tm0 = tm1;
  
  //1. fdct3d
  CpxCrvletOcr c("tmpc");
  CpxNumTns w;
  fdct3d_forward(m, n, p, nbscales, nbdstz_coarse, x, c,w);
  tm1 = time(NULL);  cout<<"fdct3d_forward "<<difftime(tm1,tm0)<<" seconds"<<endl;  tm0 = tm1;
  
  //2. ifdct3d
  //fdct3d_inverse(m, n, p, nbscales, nbdstz_coarse, c,w, x);
  //tm1 = time(NULL);  cout<<"fdct3d_inverse "<<difftime(tm1,tm0)<<" seconds"<<endl;  tm0 = tm1;
  
  CpxNumTns newx(x); clear(newx);
  fdct3d_inverse(m, n, p, nbscales, nbdstz_coarse, c,w, newx);
  tm1 = time(NULL);  cout<<"fdct3d_inverse "<<difftime(tm1,tm0)<<" seconds"<<endl;  tm0 = tm1;  //cerr<<energy(newx)<<endl;
  
  double mv = 0.0;
  for(int i=0; i<m; i++)	 for(int j=0; j<n; j++)		for(int k=0; k<p; k++)		  mv = max(mv, abs(newx(i,j,k)-x(i,j,k)));
  cerr<<"max error "<<mv<<endl;


  return 0;
}
Beispiel #4
0
EditorSpashScreen::EditorSpashScreen(QPixmap &pixmap)
{
    opacity=1;
    QPixmap newx(pixmap.width(), pixmap.height()+50);
    newx.fill(QColor(Qt::transparent));
    QPainter x(&newx);
    x.drawPixmap(0,0, pixmap.width(), pixmap.height(), pixmap);
    x.end();
    setPixmap(newx);
    construct();
}
int cg(MAT &A,VEC b,VEC &x,int maxIter, double tol)
{	
	//A_p: A * p
	VEC  A_p(A.dim());
    int node = std::sqrt(A.dim());
    //p: search direction; 
    VEC p(A.dim()), r(A.dim()), newr(A.dim()), newx(A.dim());//,ans(A.dim()),temp(A.dim());
    //MAT LU(A.dim()),a = A;
    //r2: rT * r
    double alpha, beta, r2, newr2, err;//,g;
    //g = (double)(node-1)/2000.0;
    int iter = 0;//
    /*
    LU = luFact(a);       //LU in-place decomposition
    temp=fwdSubs(LU,b);   //forward substitution
    ans= bckSubs(LU,temp);//backward substitution
    */

    
    //Initial condition for CGM
    p = r = b - (A * x);
    r2 = r * r;
   
    while(iter < maxIter)
    {
	A_p = A * p;
	alpha = r2 / (p * A_p);
	newx = x + (alpha * p);
	err = std::sqrt(r2/A.dim());
	if ( err < tol )
            break;

	newr = r - (alpha * A_p);
	newr2 = newr * newr;
	beta = newr2 / r2;
	p = newr + beta * p;


	//Re-initialization for next iteration
	x = newx;
	r = newr;
	r2 = newr2;
	//////////////////////////////////////
	iter++;		
    }
    //printf("cg:Vne: %lf; Vsw: %lf; Vse: %lf; R:%lf\n",newx[node-1],newx[(node-1)*node],newx[node*node-1], std::abs( 1/(g*(newx[0]-newx[1])+g*(newx[0]-newx[node] )) ) );
    //printf("LU:Vne: %lf; Vsw: %lf; Vse: %lf; R:%lf\n",ans[node-1],ans[(node-1)*node],ans[node*node-1], std::abs( 1/(g*(ans[0]-ans[1])+g*(ans[0]-ans[node] )) )  );
    //printf("Difference w.r.t. hw4: %E\n",linfnorm(ans - newx));
    return iter;
}
Beispiel #6
0
void Rigid::translateCenterOfMass(const Point3F &oldPos,const Point3F &newPos)
{
   // I + mass * (crossmatrix(centerOfMass)^2 - crossmatrix(newCenter)^2)
   MatrixF oldx,newx;
   oldx.setCrossProduct(oldPos);
   newx.setCrossProduct(newPos);
   for (int row = 0; row < 3; row++)
      for (int col = 0; col < 3; col++) {
         F32 n = newx(row,col), o = oldx(row,col);
         objectInertia(row,col) += mass * ((o * o) - (n * n));
      }

   // Make sure the matrix is symetrical
   objectInertia(1,0) = objectInertia(0,1);
   objectInertia(2,0) = objectInertia(0,2);
   objectInertia(2,1) = objectInertia(1,2);
}