00001 """
00002 DESCRIPTION:
00003 LSQR For SLIMpy
00004
00005 Attempts to solve the least squares problem that minimizes norm(y-A*x)
00006 if A is inconsistent, otherwise it attemps to solve the system of linear equations A*x=y
00007
00008 Inputs:
00009 modelspace -- The space of the model, to generate initial guess.
00010 maxiter -- Maximum number of iteration for solver.
00011 tol -- Desired tolerance for solution.
00012 fastlsqr -- bool to calcualte reside if governed by max tol.
00013
00014 A -- operator object containing a complete
00015 y -- vector instance
00016 Outputs:
00017 x -- The solution (N vector) to the problem.
00018 """
00019 __copyright__ = """
00020 Copyright 2008 Sean Ross-Ross
00021 """
00022 __license__ = """
00023 This file is part of SLIMpy .
00024
00025 SLIMpy is free software: you can redistribute it and/or modify
00026 it under the terms of the GNU Lesser General Public License as published by
00027 the Free Software Foundation, either version 3 of the License, or
00028 (at your option) any later version.
00029
00030 SLIMpy is distributed in the hope that it will be useful,
00031 but WITHOUT ANY WARRANTY; without even the implied warranty of
00032 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00033 GNU Lesser General Public License for more details.
00034
00035 You should have received a copy of the GNU Lesser General Public License
00036 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00037 """
00038
00039 """
00040 \author Peyman P. Moghaddam
00041
00042 Seismic Laboratory for Imaging and Modeling (SLIM)
00043 Department of Earth & Ocean Sciences (EOS)
00044 The University of British Columbia at Vancouver (UBC)
00045
00046 \par Acknowledgments:
00047 Deli Wang
00048 Seismic Laboratory for Imaging and Modeling (SLIM)
00049 Department of Earth & Ocean Sciences (EOS)
00050 The University of British Columbia at Vancouver (UBC)
00051
00052 C. Brown
00053 Seismic Laboratory for Imaging and Modeling (SLIM)
00054 Department of Earth & Ocean Sciences (EOS)
00055 The University of British Columbia at Vancouver (UBC)
00056
00057 Copyright: You may use this code only under the conditions and terms of
00058 the license contained in the file license.doc, provided with this software.
00059 If you do not agree to these terms you may not use this software.
00060
00061 """
00062
00063 from slimpy_contrib.ana.utils.AbstractSolver import solver
00064
00065 class LSQRsolver(solver):
00066 """
00067 LSQR For SLIMpy
00068
00069 Attempts to solve the least squares problem that minimizes norm(y-A*x)
00070 if A is inconsistent, otherwise it attemps to solve the system of linear equations A*x=y
00071 """
00072 def __init__(self, modelSpace=None, maxiter=200, tol=1e-6, fastlsqr=True):
00073 """
00074 Constructor
00075 @param modelSpace The space of the model, to generate initial guess.
00076 @param maxiter Maximum number of iteration for solver.
00077 @param tol Desired tolerance for solution.
00078 @param fastlsqr bool to calcualte reside if governed by max tol.
00079
00080 """
00081 self.modelSpace = modelSpace
00082 self.maxiter = maxiter
00083 self.tol = tol
00084 self.fastlsqr = fastlsqr
00085
00086
00087 def solve(self, A, y):
00088 """
00089 @param A -- operator object containing a complete
00090 @param y -- vector instance
00091 \return x The solution (N vector) to the problem.
00092
00093 """
00094
00095 if self.modelSpace is None:
00096 x = A.domain().zeros()
00097 else:
00098 x = self.modelSpace.zeros()
00099
00100 beta = y.norm()
00101 u = y/beta
00102 v = A.adj()*u
00103 alpha = v.norm()
00104 v /= alpha
00105 w = v
00106
00107 phibar=beta
00108 rhobar=alpha
00109
00110 resid = beta
00111 counter = 0
00112 import pdb
00113 pdb.set_trace()
00114 while ((resid>self.tol) and (counter<self.maxiter)):
00115 counter += 1
00116
00117 u = (A*v)-alpha*u
00118 beta = u.norm()
00119 u /= beta
00120
00121 v = (A.adj()*u)-beta*v
00122 alpha = v.norm()
00123 v /= alpha
00124
00125 rho = (rhobar**2+beta**2)**.5
00126 c = rhobar/rho
00127 s = beta/rho
00128
00129 theta = s*alpha
00130 rhobar = c*alpha
00131 phi = c*phibar
00132 phibar *= -s
00133
00134 x += phi/rho*w
00135 w = v-theta/rho*w
00136
00137 if not self.fastlsqr:
00138 r = y-(A*x)
00139 rT = A.adj()*r
00140 resid = r.norm()
00141 resid_mod = rT.norm()
00142 condMa = rT.max()
00143 condMi = rT.min()
00144
00145
00146
00147
00148
00149
00150 return x