Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members  

DiagonalPreconditioner.h

00001 /****************************************************************************
00002  *                                                                          *
00003  *  Program: FDDlib                                                         *
00004  *  Version: 1.1                                                            *
00005  *                                                                          *
00006  *  Copyright (C) 2002 - 2004 by Eric Miller and Dana Brooks                *
00007  *  All rights reserved.                                                    *
00008  *                                                                          *
00009  *  This software is Version 1.1 of the fddlib tomography toolbox.          *
00010  *  It is not to be redistributed or used for any commercial purpose        *
00011  *  without the prior written consent of the authors and Northeastern       *
00012  *  University.                                                             *
00013  *                                                                          *
00014  *  This software is provided as is, and any express or implied warranties, *
00015  *  including but not limited to the implied warranty of merchantability    *
00016  *  and the implied warranty of fitness for a particular purpose, are dis-  *
00017  *  claimed.  In no event shall the authors or Northeastern University be   *
00018  *  liable for any direct, indirect, incidental, special, exemplary, or     *
00019  *  consequential damages (including but not limited to procurement of      * 
00020  *  substitute goods or services; loss of use, data, or profits; or busi-   *
00021  *  ness interruption) however caused and on any theory of liability,       *
00022  *  whether in contract, strict liability, or tort (including negligence or *
00023  *  otherwise) arising in any way from the use of this software, even if    *
00024  *  advised of the possibility of such damage.                              *
00025  *                                                                          *
00026  *  Portions of this code benefit from ideas from Kyle Guilbert, Greg       *
00027  *  Boverman, Derek Uluski, David Kaeli, and Jennifer Black                 *
00028  *                                                                          *
00029  ****************************************************************************/
00030 
00031 #ifndef _DIAGONAL_PRECONDITIONER_H_
00032 #define _DIAGONAL_PRECONDITIONER_H_
00033 
00034 #include "Preconditioner.h"
00035 #include "SparseRowMatrix.h"
00036 #include "RealVector.h"
00037 #include <string>
00038 
00039 namespace FDDlib {
00040 
00046 class DiagonalPreconditioner : public Preconditioner
00047 {
00048 private:
00050   double*   invDiag_;
00051 
00052 public:
00053 
00057   DiagonalPreconditioner(SparseRowMatrix<double>& SRMD)
00058   {
00059     int i;
00060 
00061     len_ = SRMD.getNumRows();
00062     invDiag_ = new double[len_];
00063     for (i = 0; i < len_; i++)
00064     {
00065       invDiag_[i] = 1 / SRMD.val(i,i);
00066     }
00067   }
00068 
00070   DiagonalPreconditioner()
00071   {
00072     len_ = 0;
00073     invDiag_ = (double*) NULL;
00074   }
00075 
00079   void init(SparseRowMatrix<double>& SRMD) throw(std::string)
00080   {
00081     int i;
00082 
00083     len_ = SRMD.getNumRows();
00084     if (invDiag_ != (double*) NULL)
00085       delete invDiag_;
00086     invDiag_ = new double[len_];
00087     for (i = 0; i < len_; i++)
00088     {
00089       if (SRMD.val(i,i) == 0)
00090         throw std::string("DiagonalPreconditioner::init: zero on matrix diagonal.");
00091       invDiag_[i] = 1 / SRMD.val(i,i);
00092     }
00093   }
00094 
00096   ~DiagonalPreconditioner()
00097   {
00098     if (invDiag_ != (double*) NULL)
00099       delete[] invDiag_;
00100   }
00101 
00106   RealVector<double> solve(const RealVector<double>& x) const
00107   {
00108 
00109     RealVector<double> r(len_);
00110     int    i;
00111 
00112     for (i = 0; i < len_; i++)
00113     {
00114       r(i) = invDiag_[i]*x(i);
00115     }
00116 
00117     return r;
00118   }
00119 
00120 
00121 
00122 };
00123 
00124 // end namespace
00125 }  
00126 #endif

Generated on Mon Aug 30 15:41:06 2004 for FDDLib by doxygen1.2.18