casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MatrixSolver.h
Go to the documentation of this file.
00001 //# MatrixSolver.h: the base class for solvers of AX=B
00002 //# Copyright (C) 1994,1995,1999
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //# 
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //# 
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //# 
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //# 
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: MatrixSolver.h 21024 2011-03-01 11:46:18Z gervandiepen $
00027 
00028 #ifndef SCIMATH_MATRIXSOLVER_H
00029 #define SCIMATH_MATRIXSOLVER_H
00030 
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/Array.h>
00034 #include <casa/Arrays/ArrayMath.h>
00035 #include <casa/Arrays/Matrix.h>
00036 #include <casa/Arrays/Vector.h>
00037 
00038 #include <casa/Logging/LogSink.h>
00039 #include <casa/Logging/LogMessage.h>
00040 
00041 namespace casa { //# NAMESPACE CASA - BEGIN
00042 
00043 typedef Float FType;  // floating type (Float, Double)
00044 
00045 //<summary>
00046 // MatrixSolver.h: the base class for solvers of linear equations AX=B
00047 //</summary>
00048 
00049 // <use visibility=local>
00050 
00051 // <reviewed reviewer="" date="",tests="" demos="">
00052 // </reviewed>
00053 
00054 // <prerequisite>
00055 //   <li> Matrix, Vector
00056 // </prerequisite>
00057 //
00058 // <etymology>
00059 // The MatrixSolver class name reflects its use as the base class for solving
00060 // Linear Equations of the form AX=B. This class is purely virtual
00061 // and provides the essential implementation for derived solvers
00062 // classes.  
00063 // </etymology>
00064 //
00065 // <synopsis> 
00066 // The MatrixSolver class is a purely virtual base class.  The programmer needs
00067 // to define the following functions in a class derived from MatrixSolver:
00068 // <ol>
00069 //  <li> the derived destructor.
00070 //  <li> <src>void setImageAndPsf(const Array<FType> & image, const 
00071 //  Array<FType> & psf);</src> Set the image and the Point Spread Function 
00072 //  (beam).  Setting this should reset the internal state, e.g. 
00073 //  CurrentIter()==0.
00074 //  <li> <src>Bool solve();</src>  Perform solution of AX=B.
00075 //       Returns True if algorithm has converged or stop criterium reached.
00076 // </ol>
00077 // </synopsis> 
00078 //
00079 // <todo asof="">
00080 // </todo>
00081 
00082 class MatrixSolver {
00083 public:
00084 
00085   // Default Constructor
00086   MatrixSolver();
00087   
00088   // Copy Constructor
00089   MatrixSolver(const MatrixSolver & other);
00090   
00091   // Create a MatrixSolver from a matrix A and a Vector B
00092   // <note role=warning> A and B are accessed by reference, so do not 
00093   // modify them during the lifetime of the MatrixSolver </note>
00094   MatrixSolver(const Matrix<FType> & A, const Vector<FType> & B);
00095   
00096   // Virtual destructor: calls all derived class destructors
00097   virtual ~MatrixSolver();
00098   
00099   // Assignment operator: uses reference semantics, i.e., it 
00100   // references the internal arrays of other
00101   MatrixSolver & operator=(const MatrixSolver & other);
00102 
00103   // Set A matrix and B vector
00104   void setAB(const Matrix<FType> & A, const Vector<FType> & B);
00105 
00106   // Set initial value of X
00107   void setX(const Vector<FType> & X);
00108   
00109   // Solve for the X vector.
00110   virtual Bool solve();
00111 
00112   // Is the current solution good enough?
00113   Bool accurateSolution();
00114 
00115   // Return residual vector B-AX
00116   const Vector<FType> & getResidual();
00117   
00118   // Return solution vector
00119   const Vector<FType> & getSolution();
00120 
00121   // Set the tolerance for solution
00122   void setTolerance(FType tol);
00123 
00124   // Return the tolerance for solution
00125   FType Tolerance();
00126   
00127   // Set the maximum number of iterations.
00128   void setMaxIters(uInt maxiters);
00129   
00130   // Return the maximum number of iterations.
00131   uInt MaxIters();
00132 
00133   // Set the gain for solution
00134   void setGain(FType g);
00135 
00136   // Return the gain for solution
00137   FType Gain();
00138 
00139   // Set status of solution
00140   void setSolved(Bool s);
00141 
00142   // Return status of solution
00143   Bool Solved();
00144 
00145   // Return norm of solution i.e. ||B-AX||
00146   FType getNorm();
00147   
00148 protected:
00149 
00150   LogSink logSink_p;
00151   virtual LogSink& logSink() {return logSink_p;}
00152 
00153   // the A matrix data member
00154   Matrix<FType> AMatrix;
00155 
00156   // the constraint vector data member
00157   Vector<FType> BVector;
00158 
00159   // The residual vector data member
00160   Vector<FType> RVector;
00161 
00162   // The solution vector data member
00163   Vector<FType> XVector;
00164 
00165   // The solution norm i.e. ||B-AX||
00166   FType RNorm;
00167 
00168   // The data norm i.e. ||B||
00169   FType BNorm;
00170 
00171 private:
00172 
00173   // Tolerance for solution i.e. ||B-AX||/||B|| must be less than this
00174   FType SolTolerance; 
00175  
00176   // Maximum number of iterations
00177   uInt MaxIterations;
00178 
00179   // Has a solution been found?
00180   Bool solved;
00181 
00182   // Gain
00183   FType gain;
00184 
00185 };
00186 
00187 inline void MatrixSolver::setTolerance(FType tol) 
00188 {SolTolerance=tol;}
00189 
00190 inline FType MatrixSolver::Tolerance() 
00191 {return SolTolerance;}
00192 
00193 inline void MatrixSolver::setMaxIters(uInt maxiters) 
00194 {MaxIterations = maxiters;}
00195 
00196 inline uInt MatrixSolver::MaxIters() 
00197 {return MaxIterations;}
00198 
00199 inline void MatrixSolver::setGain(FType g) 
00200 {gain=g;}
00201 
00202 inline FType MatrixSolver::Gain() 
00203 {return gain;}
00204 
00205 inline void MatrixSolver::setSolved(Bool s) 
00206 {solved=s;}
00207 
00208 inline Bool MatrixSolver::Solved() 
00209 {return solved;}
00210 
00211 inline FType MatrixSolver::getNorm()
00212 {return RNorm;}
00213 
00214 
00215 } //# NAMESPACE CASA - END
00216 
00217 #endif