| 1 | /*========================================================================= |
| 2 | * |
| 3 | * Copyright Insight Software Consortium |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | *=========================================================================*/ |
| 18 | #ifndef LSQR_lsqr_h |
| 19 | #define LSQR_lsqr_hcxx . . $ `Modules/ThirdParty/VNL/src/vxl/v3p/netlib/linalg/lsqrBase.h:19:9`! |
| 20 | |
| 21 | #include <iosfwd> |
| 22 | |
| 23 | /** \class lsqrBase |
| 24 | * |
| 25 | * \brief implement a solver for a set of linear equations. |
| 26 | * |
| 27 | * LSQR finds a solution x to the following problems: |
| 28 | * |
| 29 | * 1. Unsymmetric equations: Solve A*x = b |
| 30 | * |
| 31 | * 2. Linear least squares: Solve A*x = b |
| 32 | * in the least-squares sense |
| 33 | * |
| 34 | * 3. Damped least squares: Solve ( A )*x = ( b ) |
| 35 | * ( damp*I ) ( 0 ) |
| 36 | * in the least-squares sense |
| 37 | * |
| 38 | * where A is a matrix with m rows and n columns, b is an m-vector, |
| 39 | * and damp is a scalar. (All quantities are real.) |
| 40 | * The matrix A is treated as a linear operator. It is accessed |
| 41 | * by means of subroutine calls with the following purpose: |
| 42 | * |
| 43 | * call Aprod1(m,n,x,y) must compute y = y + A*x without altering x. |
| 44 | * call Aprod2(m,n,x,y) must compute x = x + A'*y without altering y. |
| 45 | * |
| 46 | * LSQR uses an iterative method to approximate the solution. |
| 47 | * The number of iterations required to reach a certain accuracy |
| 48 | * depends strongly on the scaling of the problem. Poor scaling of |
| 49 | * the rows or columns of A should therefore be avoided where |
| 50 | * possible. |
| 51 | * |
| 52 | * For example, in problem 1 the solution is unaltered by |
| 53 | * row-scaling. If a row of A is very small or large compared to |
| 54 | * the other rows of A, the corresponding row of ( A b ) should be |
| 55 | * scaled up or down. |
| 56 | * |
| 57 | * In problems 1 and 2, the solution x is easily recovered |
| 58 | * following column-scaling. Unless better information is known, |
| 59 | * the nonzero columns of A should be scaled so that they all have |
| 60 | * the same Euclidean norm (e.g., 1.0). |
| 61 | * |
| 62 | * In problem 3, there is no freedom to re-scale if damp is |
| 63 | * nonzero. However, the value of damp should be assigned only |
| 64 | * after attention has been paid to the scaling of A. |
| 65 | * |
| 66 | * The parameter damp is intended to help regularize |
| 67 | * ill-conditioned systems, by preventing the true solution from |
| 68 | * being very large. Another aid to regularization is provided by |
| 69 | * the parameter Acond, which may be used to terminate iterations |
| 70 | * before the computed solution becomes very large. |
| 71 | * |
| 72 | * This class is a direct C++ translation from the Fortran90 version |
| 73 | * of the solver that is available at |
| 74 | * http://www.stanford.edu/group/SOL/software.html |
| 75 | * distributed under a BSD license. |
| 76 | * |
| 77 | * This class is a replacement for the lsqr code taken from netlib. |
| 78 | * That code had to be removed because it is copyrighted by ACM and |
| 79 | * its license was incompatible with a BSD license. |
| 80 | * |
| 81 | */ |
| 82 | class lsqrBasecxx . . $ lsqrBase# |
| 83 | { |
| 84 | public: |
| 85 | |
| 86 | lsqrBasecxx . . $ lsqrBase#lsqrBase(49f6e7a06ebc5aa8).(); |
| 87 | virtual ~cxx . . $ lsqrBase#`~lsqrBase`(ced63f7c635d850d).lsqrBasecxx . . $ lsqrBase#(); |
| 88 | |
| 89 | /** |
| 90 | * computes y = y + A*x without altering x, |
| 91 | * where A is a matrix of dimensions A[m][n]. |
| 92 | * The size of the vector x is n. |
| 93 | * The size of the vector y is m. |
| 94 | */ |
| 95 | virtual void Aprod1cxx . . $ lsqrBase#Aprod1(86869b44bfc8fcbf).(unsigned int m, unsigned int n, const double * x, double * y ) const = 0; |
| 96 | |
| 97 | /** |
| 98 | * computes x = x + A'*y without altering y, |
| 99 | * where A is a matrix of dimensions A[m][n]. |
| 100 | * The size of the vector x is n. |
| 101 | * The size of the vector y is m. |
| 102 | */ |
| 103 | virtual void Aprod2cxx . . $ lsqrBase#Aprod2(99cadc94f826541e).(unsigned int m, unsigned int n, double * x, const double * y ) const = 0; |
| 104 | |
| 105 | /** |
| 106 | * returns sqrt( a**2 + b**2 ) |
| 107 | * with precautions to avoid overflow. |
| 108 | */ |
| 109 | double D2Normcxx . . $ lsqrBase#D2Norm(726307d3d9a4aee5).( double a, double b ) const; |
| 110 | |
| 111 | /** |
| 112 | * returns sqrt( x' * x ) |
| 113 | * with precautions to avoid overflow. |
| 114 | */ |
| 115 | double Dnrm2cxx . . $ lsqrBase#Dnrm2(ffb79a6a58f8aacf).( unsigned int n, const double *x ) const; |
| 116 | |
| 117 | /** |
| 118 | * Scale a vector by multiplying with a constant |
| 119 | */ |
| 120 | void Scalecxx . . $ lsqrBase#Scale(76dbe1506afaa130).( unsigned int n, double factor, double *x ) const; |
| 121 | |
| 122 | /** A logical variable to say if the array se(*) of standard error estimates |
| 123 | * should be computed. If m > n or damp > 0, the system is overdetermined |
| 124 | * and the standard errors may be useful. (See the first LSQR reference.) |
| 125 | * Otherwise (m <= n and damp = 0) they do not mean much. Some time and |
| 126 | * storage can be saved by setting wantse = .false. and using any convenient |
| 127 | * array for se(*), which won't be touched. If you call this method with the |
| 128 | * flag ON, then you MUST provide a working memory array to store the standard |
| 129 | * error estimates, via the method SetStandardErrorEstimates() |
| 130 | */ |
| 131 | void SetStandardErrorEstimatesFlagcxx . . $ lsqrBase#SetStandardErrorEstimatesFlag(b45ddcbc8423892c).( bool ); |
| 132 | |
| 133 | /** An estimate of the relative error in the data |
| 134 | * defining the matrix A. For example, if A is |
| 135 | * accurate to about 6 digits, set atol = 1.0e-6. |
| 136 | */ |
| 137 | void SetToleranceAcxx . . $ lsqrBase#SetToleranceA(ba8836b59fb42830).( double ); |
| 138 | |
| 139 | /** An estimate of the relative error in the data |
| 140 | * defining the rhs b. For example, if b is |
| 141 | * accurate to about 6 digits, set btol = 1.0e-6. |
| 142 | */ |
| 143 | void SetToleranceBcxx . . $ lsqrBase#SetToleranceB(ba8836b59fb42830).( double ); |
| 144 | |
| 145 | /** An upper limit on cond(Abar), the apparent |
| 146 | * condition number of the matrix Abar. |
| 147 | * Iterations will be terminated if a computed |
| 148 | * estimate of cond(Abar) exceeds conlim. |
| 149 | * This is intended to prevent certain small or |
| 150 | * zero singular values of A or Abar from |
| 151 | * coming into effect and causing unwanted growth |
| 152 | * in the computed solution. |
| 153 | * |
| 154 | * conlim and damp may be used separately or |
| 155 | * together to regularize ill-conditioned systems. |
| 156 | * |
| 157 | * Normally, conlim should be in the range |
| 158 | * 1000 to 1/eps. |
| 159 | * Suggested value: |
| 160 | * conlim = 1/(100*eps) for compatible systems, |
| 161 | * conlim = 1/(10*sqrt(eps)) for least squares. |
| 162 | * |
| 163 | * Note: Any or all of atol, btol, conlim may be set to zero. |
| 164 | * The effect will be the same as the values eps, eps, 1/eps. |
| 165 | * |
| 166 | */ |
| 167 | void SetUpperLimitOnConditionalcxx . . $ lsqrBase#SetUpperLimitOnConditional(ba8836b59fb42830).( double ); |
| 168 | |
| 169 | /** the relative precision of floating-point arithmetic. |
| 170 | * On most machines, eps is about 1.0e-7 and 1.0e-16 |
| 171 | * in single and double precision respectively. |
| 172 | * We expect eps to be about 1e-16 always. |
| 173 | */ |
| 174 | void SetEpsiloncxx . . $ lsqrBase#SetEpsilon(ba8836b59fb42830).( double ); |
| 175 | |
| 176 | /** |
| 177 | * The damping parameter for problem 3 above. |
| 178 | * (damp should be 0.0 for problems 1 and 2.) |
| 179 | * If the system A*x = b is incompatible, values |
| 180 | * of damp in the range 0 to sqrt(eps)*norm(A) |
| 181 | * will probably have a negligible effect. |
| 182 | * Larger values of damp will tend to decrease |
| 183 | * the norm of x and reduce the number of |
| 184 | * iterations required by LSQR. |
| 185 | * |
| 186 | * The work per iteration and the storage needed |
| 187 | * by LSQR are the same for all values of damp. |
| 188 | * |
| 189 | */ |
| 190 | void SetDampcxx . . $ lsqrBase#SetDamp(ba8836b59fb42830).( double ); |
| 191 | |
| 192 | /** An upper limit on the number of iterations. |
| 193 | * Suggested value: |
| 194 | * itnlim = n/2 for well-conditioned systems |
| 195 | * with clustered singular values, |
| 196 | * itnlim = 4*n otherwise. |
| 197 | */ |
| 198 | void SetMaximumNumberOfIterationscxx . . $ lsqrBase#SetMaximumNumberOfIterations(cb2f890c2fdad230).( unsigned int ); |
| 199 | |
| 200 | /** |
| 201 | * If provided, a summary will be printed out to this stream during |
| 202 | * the execution of the Solve function. |
| 203 | */ |
| 204 | void SetOutputStreamcxx . . $ lsqrBase#SetOutputStream(f7f45734e6842120).( stdcxx . . $ std/::ostreamcxx . . $ std/ostream# & os ); |
| 205 | |
| 206 | /** Provide the array where the standard error estimates will be stored. |
| 207 | * You MUST provide this working memory array if you turn on the computation |
| 208 | * of standard error estimates with teh method SetStandardErrorEstimatesFlag(). |
| 209 | */ |
| 210 | void SetStandardErrorEstimatescxx . . $ lsqrBase#SetStandardErrorEstimates(9757410f5581be28).( double * array ); |
| 211 | |
| 212 | /** |
| 213 | * Returns an integer giving the reason for termination: |
| 214 | * |
| 215 | * 0 x = 0 is the exact solution. |
| 216 | * No iterations were performed. |
| 217 | * |
| 218 | * 1 The equations A*x = b are probably compatible. |
| 219 | * Norm(A*x - b) is sufficiently small, given the |
| 220 | * values of atol and btol. |
| 221 | * |
| 222 | * 2 damp is zero. The system A*x = b is probably |
| 223 | * not compatible. A least-squares solution has |
| 224 | * been obtained that is sufficiently accurate, |
| 225 | * given the value of atol. |
| 226 | * |
| 227 | * 3 damp is nonzero. A damped least-squares |
| 228 | * solution has been obtained that is sufficiently |
| 229 | * accurate, given the value of atol. |
| 230 | * |
| 231 | * 4 An estimate of cond(Abar) has exceeded conlim. |
| 232 | * The system A*x = b appears to be ill-conditioned, |
| 233 | * or there could be an error in Aprod1 or Aprod2. |
| 234 | * |
| 235 | * 5 The iteration limit itnlim was reached. |
| 236 | * |
| 237 | */ |
| 238 | unsigned int GetStoppingReasoncxx . . $ lsqrBase#GetStoppingReason(ddd8539a2fcdd4ce).() const; |
| 239 | |
| 240 | |
| 241 | /** Returns the actual number of iterations performed. */ |
| 242 | unsigned int GetNumberOfIterationsPerformedcxx . . $ lsqrBase#GetNumberOfIterationsPerformed(ddd8539a2fcdd4ce).() const; |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * An estimate of the Frobenius norm of Abar. |
| 247 | * This is the square-root of the sum of squares |
| 248 | * of the elements of Abar. |
| 249 | * If damp is small and the columns of A |
| 250 | * have all been scaled to have length 1.0, |
| 251 | * Anorm should increase to roughly sqrt(n). |
| 252 | * A radically different value for Anorm may |
| 253 | * indicate an error in Aprod1 or Aprod2. |
| 254 | */ |
| 255 | double GetFrobeniusNormEstimateOfAbarcxx . . $ lsqrBase#GetFrobeniusNormEstimateOfAbar(21b6c25e127938e2).() const; |
| 256 | |
| 257 | |
| 258 | /** |
| 259 | * An estimate of cond(Abar), the condition |
| 260 | * number of Abar. A very high value of Acond |
| 261 | * may again indicate an error in Aprod1 or Aprod2. |
| 262 | */ |
| 263 | double GetConditionNumberEstimateOfAbarcxx . . $ lsqrBase#GetConditionNumberEstimateOfAbar(21b6c25e127938e2).() const; |
| 264 | |
| 265 | |
| 266 | /** An estimate of the final value of norm(rbar), |
| 267 | * the function being minimized (see notation |
| 268 | * above). This will be small if A*x = b has |
| 269 | * a solution. |
| 270 | */ |
| 271 | double GetFinalEstimateOfNormRbarcxx . . $ lsqrBase#GetFinalEstimateOfNormRbar(21b6c25e127938e2).() const; |
| 272 | |
| 273 | |
| 274 | /** An estimate of the final value of |
| 275 | * norm( Abar(transpose)*rbar ), the norm of |
| 276 | * the residual for the normal equations. |
| 277 | * This should be small in all cases. (Arnorm |
| 278 | * will often be smaller than the true value |
| 279 | * computed from the output vector x.) |
| 280 | */ |
| 281 | double GetFinalEstimateOfNormOfResidualscxx . . $ lsqrBase#GetFinalEstimateOfNormOfResiduals(21b6c25e127938e2).() const; |
| 282 | |
| 283 | |
| 284 | /** |
| 285 | * An estimate of norm(x) for the final solution x. |
| 286 | */ |
| 287 | double GetFinalEstimateOfNormOfXcxx . . $ lsqrBase#GetFinalEstimateOfNormOfX(21b6c25e127938e2).() const; |
| 288 | |
| 289 | |
| 290 | /** |
| 291 | * Execute the solver |
| 292 | * |
| 293 | * solves Ax = b or min ||Ax - b|| with or without damping, |
| 294 | * |
| 295 | * m is the size of the input vector b |
| 296 | * n is the size of the output vector x |
| 297 | */ |
| 298 | void Solvecxx . . $ lsqrBase#Solve(e7601b19bca5ff52).( unsigned int m, unsigned int n, const double * b, double * x ); |
| 299 | |
| 300 | private: |
| 301 | |
| 302 | void TerminationPrintOutcxx . . $ lsqrBase#TerminationPrintOut(49f6e7a06ebc5aa8).(); |
| 303 | |
| 304 | double Anormcxx . . $ lsqrBase#Anorm.; |
| 305 | double Acondcxx . . $ lsqrBase#Acond.; |
| 306 | double bnormcxx . . $ lsqrBase#bnorm.; |
| 307 | double rnormcxx . . $ lsqrBase#rnorm.; |
| 308 | double Arnormcxx . . $ lsqrBase#Arnorm.; |
| 309 | double xnormcxx . . $ lsqrBase#xnorm.; |
| 310 | double dxmaxcxx . . $ lsqrBase#dxmax.; |
| 311 | |
| 312 | double atolcxx . . $ lsqrBase#atol.; |
| 313 | double btolcxx . . $ lsqrBase#btol.; |
| 314 | double conlimcxx . . $ lsqrBase#conlim.; |
| 315 | |
| 316 | double epscxx . . $ lsqrBase#eps.; |
| 317 | double dampcxx . . $ lsqrBase#damp.; |
| 318 | bool dampedcxx . . $ lsqrBase#damped.; |
| 319 | |
| 320 | unsigned int itnlimcxx . . $ lsqrBase#itnlim.; |
| 321 | unsigned int itncxx . . $ lsqrBase#itn.; |
| 322 | |
| 323 | unsigned int istopcxx . . $ lsqrBase#istop.; |
| 324 | |
| 325 | unsigned int maxdxcxx . . $ lsqrBase#maxdx.; |
| 326 | |
| 327 | stdcxx . . $ std/::ostreamcxx . . $ std/ostream# * noutcxx . . $ lsqrBase#nout.; |
| 328 | |
| 329 | bool wantsecxx . . $ lsqrBase#wantse.; |
| 330 | double * secxx . . $ lsqrBase#se.; |
| 331 | }; |
| 332 | |
| 333 | #endif |