power_method.cpp

Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //               Epetra: Linear Algebra Services Package 
00005 //                 Copyright (2001) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ************************************************************************
00027 //@HEADER
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include "Epetra_Comm.h"
00032 #include "Epetra_Map.h"
00033 #include "Epetra_Vector.h"
00034 #include "Epetra_CrsMatrix.h"
00035 // Simple Power method algorithm
00036 double power_method(const Epetra_CrsMatrix& A) {  
00037   // variable needed for iteration
00038   double lambda = 0.0;
00039   int niters = A.RowMap().NumGlobalElements()*10;
00040   double tolerance = 1.0e-10;
00041   // Create vectors
00042   Epetra_Vector q(A.RowMap());
00043   Epetra_Vector z(A.RowMap());
00044   Epetra_Vector resid(A.RowMap());
00045   // Fill z with random Numbers
00046   z.Random();
00047   // variable needed for iteration
00048   double normz;
00049   double residual = 0;
00050   int iter = 0;
00051   while (iter==0 || (iter < niters && residual > tolerance)) {
00052     z.Norm2(&normz); // Compute 2-norm of z
00053     q.Scale(1.0/normz, z);
00054     A.Multiply(false, q, z); // Compute z = A*q
00055     q.Dot(z, &lambda); // Approximate maximum eigenvalue
00056     if (iter%10==0 || iter+1==niters) {
00057       // Compute A*q - lambda*q every 10 iterations
00058       resid.Update(1.0, z, -lambda, q, 0.0);
00059       resid.Norm2(&residual);
00060       if (q.Map().Comm().MyPID()==0)
00061   cout << "Iter = " << iter << "  Lambda = " << lambda 
00062        << "  Two-norm of A*q - lambda*q = " 
00063        << residual << endl;
00064     } 
00065     iter++;
00066   }
00067   return(lambda);
00068 }

Generated on Wed May 12 21:41:06 2010 for Epetra Package Browser (Single Doxygen Collection) by  doxygen 1.4.7