/*************************************************************************** * $Id: rayleigh2.cpp,v 1.6 2004/02/27 17:57:16 prakash Exp $ * * An example of the Rayleigh quotient method with a complex matrix. * * Copyright (C) 2001-2003 by Prakash Dayal * Matthias Troyer * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * **************************************************************************/ #include "solver.h" #include #include #include #include #include #include #include #include typedef boost::numeric::ublas::matrix , boost::numeric::ublas::column_major> Matrix; typedef boost::numeric::ublas::vector > Vector; typedef ietl::vectorspace Vecspace; typedef boost::lagged_fibonacci607 Gen; int main() { std::cout << "Rayleigh Quotient Iteration v0.14\n"; std::cout << "-----------------------------------------------\n\n"; // Dimension of Matrix int n = 3; // Absolute Error Tolerance double abs_tol = 5. * std::numeric_limits::epsilon(); // Relative Error Tolerance double rel_tol = std::numeric_limits::epsilon(); // Maximum Iterations int max_iter = 20; // Create iteration object ietl::basic_iteration iter(max_iter, rel_tol, abs_tol); // Initialize Matrix Matrix mat(n,n); mat(0,0) = std::complex(2,0); mat(0,1) = std::complex(2,1); mat(0,2) = std::complex(0,-2); mat(1,0) = std::complex(2,-1); mat(1,1) = std::complex(-3,0); mat(1,2) = std::complex(-1,3); mat(2,0) = std::complex(0,2); mat(2,1) = std::complex(-1,-3); mat(2,2) = std::complex(4,0); // Create Vectorspace Vecspace vec(n); // Create Generator for the starting vector Gen mygen; // Create the solver Solver mysolver; // Show the Matrix std::cout << mat << std::endl; // Calculate the eigenvalue and the eigenvector std::pair result = ietl::rayleigh(mat, mygen, mysolver, iter, vec); // Print out the obtained results std::cout.precision(20); std::cout << "Eigenvalue: "<< result.first << std::endl; std::cout << "Eigenvector: "<< result.second << std::endl; // Calculate the error as the norm of (Av-theta*v) Vector error = ietl::new_vector(vec); ietl::mult(mat,result.second,error); error -= result.first*result.second; std::cout << "error: " << ietl::two_norm(error) << std::endl; return 0; }