MathVector<T> DirichletPoisson<T>::getSolution(int numDivisions)
{
  numDivs = numDivisions;
  int dimensions = (numDivs - 1)*(numDivs - 1);

  MathMatrix<T> A(dimensions, dimensions);
  MathVector<T> b(dimensions);

  generate<fnLow, fnHigh, fnLeft, fnRight, fnForce>(A, b);

  return mySolver(A, b);
}
Example #2
0
///////////////////////////////////////////////////////////////////////////////
//
// Calculate the fundamental matrix and set to F, returning the residual of
// the fit.
//
///////////////////////////////////////////////////////////////////////////////
double FSolver::solve() {
  Frprmn<FSolver>	mySolver(*this, 1e-7);	// solver
  VecDoub 		p(9);			// parameters for solver
  int			n;
  transformMatrix	M1(inverse_intrinsic,1.0,1.0,0.0,0.0);

  // --- initial guess
  // -----------------
  F() = M1;

  for(n = 0; n<9; ++n) {
    p[n] = F()[n/3][n%3];
  }
  p = mySolver.minimize(p);
  for(n = 0; n<9; ++n) {
    F()[n/3][n%3] = p[n];
  }
  return((*this)(p));
}
Example #3
0
///////////////////////////////////////////////////////////////////////////////
//
// Constructor/solver
// f  = the fundamental matrix between two views
// m  = the intrinsic matrix of the reference camera
// mp = the intrinsic matrix of the second camera (M')
// 
// on construction, 'this' is set to be equal to the camera matrix of
// the second view.
//
// If F is exact, we have that 
// F = M'^{-1}RTM^{-1}
// so, if we let r be the residual matrix:
// r = M'^{-1}RTM^{-1} - F
// where
// M'^{-1T} 	= transpose of the inverse intrinsic matrix of the 2nd camera
// R		= rotation matrix (from reference to second view)
// T		= translation cross-product matrix (TQ = t x Q)
// M^{-1}	= inverse intrinsic matrix of the reference camera
// F		= the fundamental matrix
//
// then R and T are the unknowns with 6 degrees of freedom.
// These are calculated by minimising the sum of the squres of the elements
// of r. The camera matrix pair is then given by
//
// P = M[I|0] and P'=M'R[I|-t]
//
// So P' is the camera matrix we require.
//
///////////////////////////////////////////////////////////////////////////////
RTSolver::RTSolver(Matrix<double> &f,
		   Matrix<double> &m,
		   Matrix<double> &mp) : 
  transformMatrix(camera),
  N(inverse_intrinsic, m[0][0], m[1][1], m[0][2], m[1][2]),
  NPT(inverse_intrinsic, mp[0][0], mp[1][1], mp[0][2], mp[1][2])
 {
   transformMatrix	I(identity);
   Frprmn<RTSolver>	mySolver(*this, 1e-8);	// solver
   VecDoub 		p(6); 			// parameters for solver
   coord		t(3);			// translation vector

   F = f;
   NPT.transpose();

   // --- initial guess
   // -----------------
   p[0] = 1.2;
   p[1] = 0.0;
   p[2] = 0.0;
   p[3] = 0.0;
   p[4] = 0.0;
   p[5] = 0.0;

   // --- solve
   // ---------
   p = mySolver.minimize(p);

   // --- extract solution
   // --------------------
   t[0] = -p[0];
   t[1] = -p[1];
   t[2] = -p[2];
   R = transformMatrix(xrotate,p[3]) *
     transformMatrix(yrotate,p[4]) *
     transformMatrix(zrotate,p[5]);

   P() = mp*R*(I|t);
}
Example #4
0
int main(int argc, char *argv[]) {
    
#ifdef HAVE_MPI
  // Initialize MPI and setup an Epetra communicator
  MPI_Init(&argc,&argv);
  Epetra_MpiComm Comm(MPI_COMM_WORLD);
#else
  // If we aren't using MPI, then setup a serial communicator.
  Epetra_SerialComm Comm;
#endif

  // Some typedefs for oft-used data types
  typedef Epetra_MultiVector MV;
  typedef Epetra_Operator OP;
  typedef Belos::MultiVecTraits<double, Epetra_MultiVector> MVT;

  bool ierr;

  // Get our processor ID (0 if running serial)
  int MyPID = Comm.MyPID();

  // Verbose flag: only processor 0 should print
  bool verbose = (MyPID==0);

  // Initialize a Gallery object, from which we will select a matrix
  // Select a 2-D laplacian, of order 100
  Trilinos_Util::CrsMatrixGallery Gallery("laplace_2d", Comm);
  Gallery.Set("problem_size", 100);

  // Say hello and print some information about the gallery matrix
  if (verbose) {
    cout << "Belos Example: Block CG" << endl;
    cout << "Problem info:" << endl;
    cout << Gallery;
    cout << endl;
  }

  // Setup some more problem/solver parameters:

  // Block size
  int blocksize = 4;

  // Get a pointer to the system matrix, inside of a Teuchos::RCP
  // The Teuchos::RCP is a reference counting pointer that handles
  // garbage collection for us, so that we can perform memory allocation without
  // having to worry about freeing memory manually.
  Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( Gallery.GetMatrix(), false );

  // Create an Belos MultiVector, based on Epetra MultiVector
  const Epetra_Map * Map = &(A->RowMap());
  Teuchos::RCP<Epetra_MultiVector> B = 
    Teuchos::rcp( new Epetra_MultiVector(*Map,blocksize) );
  Teuchos::RCP<Epetra_MultiVector> X = 
    Teuchos::rcp( new Epetra_MultiVector(*Map,blocksize) );

  // Initialize the solution with zero and right-hand side with random entries
  X->PutScalar( 0.0 );
  B->Random();

  // Setup the linear problem, with the matrix A and the vectors X and B
  Teuchos::RCP< Belos::LinearProblem<double,MV,OP> > myProblem = 
    Teuchos::rcp( new Belos::LinearProblem<double,MV,OP>(A, X, B) );

  // The 2-D laplacian is symmetric. Specify this in the linear problem.
  myProblem->setHermitian();

  // Signal that we are done setting up the linear problem
  ierr = myProblem->setProblem();

  // Check the return from setProblem(). If this is true, there was an
  // error. This probably means we did not specify enough information for
  // the eigenproblem.
  assert(ierr == true);

  // Specify the verbosity level. Options include:
  // Belos::Errors 
  //   This option is always set
  // Belos::Warnings 
  //   Warnings (less severe than errors)
  // Belos::IterationDetails 
  //   Details at each iteration, such as the current eigenvalues
  // Belos::OrthoDetails 
  //   Details about orthogonality
  // Belos::TimingDetails
  //   A summary of the timing info for the solve() routine
  // Belos::FinalSummary 
  //   A final summary 
  // Belos::Debug 
  //   Debugging information
  int verbosity = Belos::Warnings + Belos::Errors + Belos::FinalSummary + Belos::TimingDetails;

  // Create the parameter list for the eigensolver
  Teuchos::RCP<Teuchos::ParameterList> myPL = Teuchos::rcp( new Teuchos::ParameterList() );
  myPL->set( "Verbosity", verbosity );
  myPL->set( "Block Size", blocksize );
  myPL->set( "Maximum Iterations", 100 );
  myPL->set( "Convergence Tolerance", 1.0e-8 );

  // Create the Block CG solver
  // This takes as inputs the linear problem and the solver parameters
  Belos::BlockCGSolMgr<double,MV,OP> mySolver(myProblem, myPL);

  // Solve the linear problem, and save the return code
  Belos::ReturnType solverRet = mySolver.solve();

  // Check return code of the solver: Unconverged, Failed, or OK
  switch (solverRet) {

  // UNCONVERGED
  case Belos::Unconverged:
    if (verbose) 
      cout << "Belos::BlockCGSolMgr::solve() did not converge!" << endl;
#ifdef HAVE_MPI
    MPI_Finalize();
#endif
    return 0;
    break;

  // CONVERGED
  case Belos::Converged:
    if (verbose) 
      cout << "Belos::BlockCGSolMgr::solve() converged!" << endl;
    break;
  }

  // Test residuals
  Epetra_MultiVector R( B->Map(), blocksize );

  // R = A*X
  A->Apply( *X, R );

  // R -= B 
  MVT::MvAddMv( -1.0, *B, 1.0, R, R );

  // Compute the 2-norm of each vector in the MultiVector
  // and store them to a std::vector<double>
  std::vector<double> normR(blocksize), normB(blocksize);
  MVT::MvNorm( R, normR );
  MVT::MvNorm( *B, normB );

  // Output results to screen
  if(verbose) {
    cout << scientific << setprecision(6) << showpoint;
    cout << "******************************************************\n"
         << "           Results (outside of linear solver)           \n" 
         << "------------------------------------------------------\n"
         << "  Linear System\t\tRelative Residual\n"
         << "------------------------------------------------------\n";
    for( int i=0 ; i<blocksize ; ++i ) {
      cout << "  " << i+1 << "\t\t\t" << normR[i]/normB[i] << endl;
    }
    cout << "******************************************************\n" << endl;
  }


#ifdef HAVE_MPI
  MPI_Finalize();
#endif

  return(EXIT_SUCCESS);
}