R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
LSQR.h
Go to the documentation of this file.
1#ifndef LSQR_H
2#define LSQR_H
3/*
4 * lsqr.h
5 * Contains auxiliary functions, data type definitions, and function
6 * prototypes for the iterative linear solver LSQR.
7 *
8 * 08 Sep 1999: First version from James W. Howse <jhowse@lanl.gov>
9 * 02 Sep 2007: Dima Sorkin <dima.sorkin@gmail.com> advises that
10 * in C++ the use of macros is strongly deprecated.
11 * Originally, sqr, max, min, round, TRUE, FALSE, PI
12 * were defined here. Now,
13 * min, round, TRUE, FALSE are gone (never used);
14 * PI is defined explicitly in test_lstp.c;
15 * max is changed to lsqr_max in test_lsqr.c;
16 */
17
18/*
19 *------------------------------------------------------------------------------
20 *
21 * LSQR finds a solution x to the following problems:
22 *
23 * 1. Unsymmetric equations -- solve A*x = b
24 *
25 * 2. Linear least squares -- solve A*x = b
26 * in the least-squares sense
27 *
28 * 3. Damped least squares -- solve ( A )*x = ( b )
29 * ( damp*I ) ( 0 )
30 * in the least-squares sense
31 *
32 * where 'A' is a matrix with 'm' rows and 'n' columns, 'b' is an
33 * 'm'-vector, and 'damp' is a scalar. (All quantities are real.)
34 * The matrix 'A' is intended to be large and sparse.
35 *
36 *
37 * Notation
38 * --------
39 *
40 * The following quantities are used in discussing the subroutine
41 * parameters:
42 *
43 * 'Abar' = ( A ), 'bbar' = ( b )
44 * ( damp*I ) ( 0 )
45 *
46 * 'r' = b - A*x, 'rbar' = bbar - Abar*x
47 *
48 * 'rnorm' = sqrt( norm(r)**2 + damp**2 * norm(x)**2 )
49 * = norm( rbar )
50 *
51 * 'rel_prec' = the relative precision of floating-point arithmetic
52 * on the machine being used. For example, on the IBM 370,
53 * 'rel_prec' is about 1.0E-6 and 1.0D-16 in single and double
54 * precision respectively.
55 *
56 * LSQR minimizes the function 'rnorm' with respect to 'x'.
57 *
58 *------------------------------------------------------------------------------
59 */
60
61/*---------------*/
62/* Include files */
63/*---------------*/
64
65#include <float.h>
66#include <functional>
67#include <limits.h>
68#include <math.h>
69#include <stdio.h>
70#include <stdlib.h>
71
72/* 02 Sep 2007: The following 7 macros
73 sqr, max, min, round, TRUE, FALSE, PI
74 are no longer defined here.
75 (min, round, TRUE, FALSE were never used anyway.)
76 "sqr" has been changed to lsqr_sqr.
77*/
78
79/*------------------------*/
80/* User-defined functions */
81/*------------------------*/
82
83#define lsqr_sqr(a) ((a) * (a))
84/*
85#define max(a,b) ( (a) < (b) ? (b) : (a) )
86#define min(a,b) ( (a) < (b) ? (a) : (b) )
87#define round(a) ( (a) > 0.0 ? (int)((a) + 0.5) : (int)((a) - 0.5) )
88*/
89
90/*----------------------*/
91/* Default declarations */
92/*----------------------*/
93
94/*
95#define TRUE (1)
96#define FALSE (0)
97#define PI (4.0 * atan(1.0))
98*/
99
100/*------------------*/
101/* Type definitions */
102/*------------------*/
103
104typedef struct LSQR_LONG_VECTOR
105{
106 long length;
107 long* elements;
109
110typedef struct LSQR_DOUBLE_VECTOR
111{
112 long length;
113 double* elements;
115
116/*
117 *------------------------------------------------------------------------------
118 *
119 * Input Quantities
120 * ----------------
121 *
122 * num_rows input The number of rows (e.g., 'm') in the matrix A.
123 *
124 * num_cols input The number of columns (e.g., 'n') in the matrix A.
125 *
126 * damp_val input The damping parameter for problem 3 above.
127 * ('damp_val' should be 0.0 for problems 1 and 2.)
128 * If the system A*x = b is incompatible, values
129 * of 'damp_val' in the range
130 * 0 to sqrt('rel_prec')*norm(A)
131 * will probably have a negligible effect.
132 * Larger values of 'damp_val' will tend to decrease
133 * the norm of x and reduce the number of
134 * iterations required by LSQR.
135 *
136 * The work per iteration and the storage needed
137 * by LSQR are the same for all values of 'damp_val'.
138 *
139 * rel_mat_err input An estimate of the relative error in the data
140 * defining the matrix 'A'. For example,
141 * if 'A' is accurate to about 6 digits, set
142 * rel_mat_err = 1.0e-6 .
143 *
144 * rel_rhs_err input An extimate of the relative error in the data
145 * defining the right hand side (rhs) vector 'b'. For
146 * example, if 'b' is accurate to about 6 digits, set
147 * rel_rhs_err = 1.0e-6 .
148 *
149 * cond_lim input An upper limit on cond('Abar'), the apparent
150 * condition number of the matrix 'Abar'.
151 * Iterations will be terminated if a computed
152 * estimate of cond('Abar') exceeds 'cond_lim'.
153 * This is intended to prevent certain small or
154 * zero singular values of 'A' or 'Abar' from
155 * coming into effect and causing unwanted growth
156 * in the computed solution.
157 *
158 * 'cond_lim' and 'damp_val' may be used separately or
159 * together to regularize ill-conditioned systems.
160 *
161 * Normally, 'cond_lim' should be in the range
162 * 1000 to 1/rel_prec.
163 * Suggested value:
164 * cond_lim = 1/(100*rel_prec) for compatible systems,
165 * cond_lim = 1/(10*sqrt(rel_prec)) for least squares.
166 *
167 * Note: If the user is not concerned about the parameters
168 * 'rel_mat_err', 'rel_rhs_err' and 'cond_lim', any or all of them
169 * may be set to zero. The effect will be the same as the values
170 * 'rel_prec', 'rel_prec' and 1/rel_prec respectively.
171 *
172 * max_iter input An upper limit on the number of iterations.
173 * Suggested value:
174 * max_iter = n/2 for well-conditioned systems
175 * with clustered singular values,
176 * max_iter = 4*n otherwise.
177 *
178 * lsqr_fp_out input Pointer to the file for sending printed output. If
179 * not NULL, a summary will be printed to the file that
180 * 'lsqr_fp_out' points to.
181 *
182 * rhs_vec input The right hand side (rhs) vector 'b'. This vector
183 * has a length of 'm' (i.e., 'num_rows'). The routine
184 * LSQR is designed to over-write 'rhs_vec'.
185 *
186 * sol_vec input The initial guess for the solution vector 'x'. This
187 * vector has a length of 'n' (i.e., 'num_cols'). The
188 * routine LSQR is designed to over-write 'sol_vec'.
189 *
190 *------------------------------------------------------------------------------
191 */
192
206
207/*
208 *------------------------------------------------------------------------------
209 *
210 * Output Quantities
211 * -----------------
212 *
213 * term_flag output An integer giving the reason for termination:
214 *
215 * 0 x = x0 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 'rel_mat_err' and 'rel_rhs_err'.
221 *
222 * 2 The system A*x = b is probably not
223 * compatible. A least-squares solution has
224 * been obtained that is sufficiently accurate,
225 * given the value of 'rel_mat_err'.
226 *
227 * 3 An estimate of cond('Abar') has exceeded
228 * 'cond_lim'. The system A*x = b appears to be
229 * ill-conditioned. Otherwise, there could be an
230 * error in subroutine APROD.
231 *
232 * 4 The equations A*x = b are probably
233 * compatible. Norm(A*x - b) is as small as
234 * seems reasonable on this machine.
235 *
236 * 5 The system A*x = b is probably not
237 * compatible. A least-squares solution has
238 * been obtained that is as accurate as seems
239 * reasonable on this machine.
240 *
241 * 6 Cond('Abar') seems to be so large that there is
242 * no point in doing further iterations,
243 * given the precision of this machine.
244 * There could be an error in subroutine APROD.
245 *
246 * 7 The iteration limit 'max_iter' was reached.
247 *
248 * num_iters output The number of iterations performed.
249 *
250 * frob_mat_norm output An estimate of the Frobenius norm of 'Abar'.
251 * This is the square-root of the sum of squares
252 * of the elements of 'Abar'.
253 * If 'damp_val' is small and if the columns of 'A'
254 * have all been scaled to have length 1.0,
255 * 'frob_mat_norm' should increase to roughly
256 * sqrt('n').
257 * A radically different value for 'frob_mat_norm'
258 * may indicate an error in subroutine APROD (there
259 * may be an inconsistency between modes 1 and 2).
260 *
261 * mat_cond_num output An estimate of cond('Abar'), the condition
262 * number of 'Abar'. A very high value of
263 * 'mat_cond_num'
264 * may again indicate an error in APROD.
265 *
266 * resid_norm output 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 * mat_resid_norm output An estimate of the final value of
272 * norm( Abar(transpose)*rbar ), the norm of
273 * the residual for the usual normal equations.
274 * This should be small in all cases.
275 * ('mat_resid_norm'
276 * will often be smaller than the true value
277 * computed from the output vector 'x'.)
278 *
279 * sol_norm output An estimate of the norm of the final
280 * solution vector 'x'.
281 *
282 * sol_vec output The vector which returns the computed solution
283 * 'x'.
284 * This vector has a length of 'n' (i.e.,
285 * 'num_cols').
286 *
287 * std_err_vec output The vector which returns the standard error
288 * estimates for the components of 'x'.
289 * This vector has a length of 'n'
290 * (i.e., 'num_cols'). For each i, std_err_vec(i)
291 * is set to the value
292 * 'resid_norm' * sqrt( sigma(i,i) / T ),
293 * where sigma(i,i) is an estimate of the i-th
294 * diagonal of the inverse of Abar(transpose)*Abar
295 * and T = 1 if m <= n,
296 * T = m - n if m > n and damp_val = 0,
297 * T = m if damp_val != 0.
298 *
299 *------------------------------------------------------------------------------
300 */
301
314
315/*
316 *------------------------------------------------------------------------------
317 *
318 * Workspace Quantities
319 * --------------------
320 *
321 * bidiag_wrk_vec workspace This float vector is a workspace for the
322 * current iteration of the
323 * Lanczos bidiagonalization.
324 * This vector has length 'n' (i.e., 'num_cols').
325 *
326 * srch_dir_vec workspace This float vector contains the search direction
327 * at the current iteration. This vector has a
328 * length of 'n' (i.e., 'num_cols').
329 *
330 *------------------------------------------------------------------------------
331 */
332
338
339/*
340 *------------------------------------------------------------------------------
341 *
342 * Functions Called
343 * ----------------
344 *
345 * mat_vec_prod functions A pointer to a function that performs the
346 * matrix-vector multiplication. The function
347 * has the calling parameters:
348 *
349 * APROD ( mode, x, y, prod_data ),
350 *
351 * and it must perform the following functions:
352 *
353 * If MODE = 0, compute y = y + A*x,
354 * If MODE = 1, compute x = x + A^T*y.
355 *
356 * The vectors x and y are input parameters in
357 * both cases.
358 * If mode = 0, y should be altered without
359 * changing x.
360 * If mode = 2, x should be altered without
361 * changing y.
362 * The argument prod_data is a pointer to a
363 * user-defined structure that contains
364 * any data need by the function APROD that is
365 * not used by LSQR. If no additional data is
366 * needed by APROD, then prod_data should be
367 * the NULL pointer.
368 *------------------------------------------------------------------------------
369 */
370
371/*---------------------*/
372/* Function prototypes */
373/*---------------------*/
374void lsqr_error(const char*, int);
375
376lvec* alloc_lvec(long);
377void free_lvec(lvec*);
378
379dvec* alloc_dvec(long);
380void free_dvec(dvec*);
381
382void alloc_lsqr_mem(lsqr_input**, lsqr_output**, lsqr_work**, long, long);
384
385lsqr_input* alloc_lsqr_in(long, long);
387
390
393
394void lsqr(lsqr_input*, lsqr_output*, lsqr_work*, std::function<void(long, dvec*, dvec*, void*)>, void*);
395
396double dvec_norm2(dvec*);
397void dvec_scale(double, dvec*);
398void dvec_copy(dvec*, dvec*);
399
400#endif
lsqr_input * alloc_lsqr_in(long, long)
Definition LSQR.cxx:225
lsqr_output * alloc_lsqr_out(long, long)
struct LSQR_LONG_VECTOR lvec
void free_lsqr_out(lsqr_output *)
Definition LSQR.cxx:298
double dvec_norm2(dvec *)
Definition LSQR.cxx:821
void dvec_scale(double, dvec *)
Definition LSQR.cxx:843
void free_lsqr_mem(lsqr_input *, lsqr_output *, lsqr_work *)
Definition LSQR.cxx:210
lvec * alloc_lvec(long)
Definition LSQR.cxx:90
struct LSQR_OUTPUTS lsqr_output
void lsqr_error(const char *, int)
Definition LSQR.cxx:77
dvec * alloc_dvec(long)
Definition LSQR.cxx:131
void free_lsqr_wrk(lsqr_work *)
Definition LSQR.cxx:344
void lsqr(lsqr_input *, lsqr_output *, lsqr_work *, std::function< void(long, dvec *, dvec *, void *)>, void *)
Definition LSQR.cxx:418
struct LSQR_WORK lsqr_work
void alloc_lsqr_mem(lsqr_input **, lsqr_output **, lsqr_work **, long, long)
Definition LSQR.cxx:173
lsqr_work * alloc_lsqr_wrk(long, long)
void free_lvec(lvec *)
Definition LSQR.cxx:117
struct LSQR_INPUTS lsqr_input
void free_dvec(dvec *)
Definition LSQR.cxx:159
void free_lsqr_in(lsqr_input *)
Definition LSQR.cxx:257
struct LSQR_DOUBLE_VECTOR dvec
void dvec_copy(dvec *, dvec *)
Definition LSQR.cxx:862
double * elements
Definition LSQR.h:113
dvec * rhs_vec
Definition LSQR.h:203
FILE * lsqr_fp_out
Definition LSQR.h:202
long num_rows
Definition LSQR.h:195
double damp_val
Definition LSQR.h:197
long num_cols
Definition LSQR.h:196
double cond_lim
Definition LSQR.h:200
dvec * sol_vec
Definition LSQR.h:204
long max_iter
Definition LSQR.h:201
double rel_rhs_err
Definition LSQR.h:199
double rel_mat_err
Definition LSQR.h:198
long * elements
Definition LSQR.h:107
long term_flag
Definition LSQR.h:304
double sol_norm
Definition LSQR.h:310
long num_iters
Definition LSQR.h:305
double resid_norm
Definition LSQR.h:308
dvec * std_err_vec
Definition LSQR.h:312
dvec * sol_vec
Definition LSQR.h:311
double mat_cond_num
Definition LSQR.h:307
double mat_resid_norm
Definition LSQR.h:309
double frob_mat_norm
Definition LSQR.h:306
dvec * bidiag_wrk_vec
Definition LSQR.h:335
dvec * srch_dir_vec
Definition LSQR.h:336