R3BROOT
R3B analysis software
Toggle main menu visibility
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
104
typedef
struct
LSQR_LONG_VECTOR
105
{
106
long
length
;
107
long
*
elements
;
108
}
lvec
;
109
110
typedef
struct
LSQR_DOUBLE_VECTOR
111
{
112
long
length
;
113
double
*
elements
;
114
}
dvec
;
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 estimate 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
193
typedef
struct
LSQR_INPUTS
194
{
195
long
num_rows
;
196
long
num_cols
;
197
double
damp_val
;
198
double
rel_mat_err
;
199
double
rel_rhs_err
;
200
double
cond_lim
;
201
long
max_iter
;
202
FILE*
lsqr_fp_out
;
203
dvec
*
rhs_vec
;
204
dvec
*
sol_vec
;
205
}
lsqr_input
;
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
302
typedef
struct
LSQR_OUTPUTS
303
{
304
long
term_flag
;
305
long
num_iters
;
306
double
frob_mat_norm
;
307
double
mat_cond_num
;
308
double
resid_norm
;
309
double
mat_resid_norm
;
310
double
sol_norm
;
311
dvec
*
sol_vec
;
312
dvec
*
std_err_vec
;
313
}
lsqr_output
;
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
333
typedef
struct
LSQR_WORK
334
{
335
dvec
*
bidiag_wrk_vec
;
336
dvec
*
srch_dir_vec
;
337
}
lsqr_work
;
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
/*---------------------*/
374
void
lsqr_error
(
const
char
*,
int
);
375
376
lvec
*
alloc_lvec
(
long
);
377
void
free_lvec
(
lvec
*);
378
379
dvec
*
alloc_dvec
(
long
);
380
void
free_dvec
(
dvec
*);
381
382
void
alloc_lsqr_mem
(
lsqr_input
**,
lsqr_output
**,
lsqr_work
**,
long
,
long
);
383
void
free_lsqr_mem
(
lsqr_input
*,
lsqr_output
*,
lsqr_work
*);
384
385
lsqr_input
*
alloc_lsqr_in
(
long
,
long
);
386
void
free_lsqr_in
(
lsqr_input
*);
387
388
lsqr_output
*
alloc_lsqr_out
(
long
,
long
);
389
void
free_lsqr_out
(
lsqr_output
*);
390
391
lsqr_work
*
alloc_lsqr_wrk
(
long
,
long
);
392
void
free_lsqr_wrk
(
lsqr_work
*);
393
394
void
lsqr
(
lsqr_input
*,
lsqr_output
*,
lsqr_work
*, std::function<
void
(
long
,
dvec
*,
dvec
*,
void
*)>,
void
*);
395
396
double
dvec_norm2
(
dvec
*);
397
void
dvec_scale
(
double
,
dvec
*);
398
void
dvec_copy
(
dvec
*,
dvec
*);
399
400
#endif
free_lvec
void free_lvec(lvec *lng_vec)
Definition
LSQR.cxx:117
alloc_dvec
dvec * alloc_dvec(long dvec_size)
Definition
LSQR.cxx:131
lsqr
void lsqr(lsqr_input *input, lsqr_output *output, lsqr_work *work, std::function< void(long, dvec *, dvec *, void *)> mat_vec_prod, void *prod)
Definition
LSQR.cxx:418
alloc_lsqr_wrk
lsqr_work * alloc_lsqr_wrk(long __attribute__((unused)) max_num_rows, long max_num_cols)
Definition
LSQR.cxx:312
dvec_copy
void dvec_copy(dvec *orig, dvec *copy)
Definition
LSQR.cxx:862
alloc_lsqr_mem
void alloc_lsqr_mem(lsqr_input **in_struct, lsqr_output **out_struct, lsqr_work **wrk_struct, long max_num_rows, long max_num_cols)
Definition
LSQR.cxx:173
free_lsqr_wrk
void free_lsqr_wrk(lsqr_work *wrk_struct)
Definition
LSQR.cxx:344
free_dvec
void free_dvec(dvec *dbl_vec)
Definition
LSQR.cxx:159
free_lsqr_out
void free_lsqr_out(lsqr_output *out_struct)
Definition
LSQR.cxx:298
dvec_norm2
double dvec_norm2(dvec *vec)
Definition
LSQR.cxx:821
alloc_lsqr_out
lsqr_output * alloc_lsqr_out(long __attribute__((unused)) max_num_rows, long max_num_cols)
Definition
LSQR.cxx:272
free_lsqr_mem
void free_lsqr_mem(lsqr_input *in_struct, lsqr_output *out_struct, lsqr_work *wrk_struct)
Definition
LSQR.cxx:210
alloc_lvec
lvec * alloc_lvec(long lvec_size)
Definition
LSQR.cxx:90
lsqr_error
void lsqr_error(const char *msg, int code)
Definition
LSQR.cxx:77
dvec_scale
void dvec_scale(double scal, dvec *vec)
Definition
LSQR.cxx:843
alloc_lsqr_in
lsqr_input * alloc_lsqr_in(long max_num_rows, long max_num_cols)
Definition
LSQR.cxx:225
free_lsqr_in
void free_lsqr_in(lsqr_input *in_struct)
Definition
LSQR.cxx:257
lvec
struct LSQR_LONG_VECTOR lvec
lsqr_output
struct LSQR_OUTPUTS lsqr_output
lsqr_work
struct LSQR_WORK lsqr_work
lsqr_input
struct LSQR_INPUTS lsqr_input
dvec
struct LSQR_DOUBLE_VECTOR dvec
LSQR_DOUBLE_VECTOR
Definition
LSQR.h:111
LSQR_DOUBLE_VECTOR::elements
double * elements
Definition
LSQR.h:113
LSQR_DOUBLE_VECTOR::length
long length
Definition
LSQR.h:112
LSQR_INPUTS
Definition
LSQR.h:194
LSQR_INPUTS::rhs_vec
dvec * rhs_vec
Definition
LSQR.h:203
LSQR_INPUTS::lsqr_fp_out
FILE * lsqr_fp_out
Definition
LSQR.h:202
LSQR_INPUTS::num_rows
long num_rows
Definition
LSQR.h:195
LSQR_INPUTS::damp_val
double damp_val
Definition
LSQR.h:197
LSQR_INPUTS::num_cols
long num_cols
Definition
LSQR.h:196
LSQR_INPUTS::cond_lim
double cond_lim
Definition
LSQR.h:200
LSQR_INPUTS::sol_vec
dvec * sol_vec
Definition
LSQR.h:204
LSQR_INPUTS::max_iter
long max_iter
Definition
LSQR.h:201
LSQR_INPUTS::rel_rhs_err
double rel_rhs_err
Definition
LSQR.h:199
LSQR_INPUTS::rel_mat_err
double rel_mat_err
Definition
LSQR.h:198
LSQR_LONG_VECTOR
Definition
LSQR.h:105
LSQR_LONG_VECTOR::length
long length
Definition
LSQR.h:106
LSQR_LONG_VECTOR::elements
long * elements
Definition
LSQR.h:107
LSQR_OUTPUTS
Definition
LSQR.h:303
LSQR_OUTPUTS::term_flag
long term_flag
Definition
LSQR.h:304
LSQR_OUTPUTS::sol_norm
double sol_norm
Definition
LSQR.h:310
LSQR_OUTPUTS::num_iters
long num_iters
Definition
LSQR.h:305
LSQR_OUTPUTS::resid_norm
double resid_norm
Definition
LSQR.h:308
LSQR_OUTPUTS::std_err_vec
dvec * std_err_vec
Definition
LSQR.h:312
LSQR_OUTPUTS::sol_vec
dvec * sol_vec
Definition
LSQR.h:311
LSQR_OUTPUTS::mat_cond_num
double mat_cond_num
Definition
LSQR.h:307
LSQR_OUTPUTS::mat_resid_norm
double mat_resid_norm
Definition
LSQR.h:309
LSQR_OUTPUTS::frob_mat_norm
double frob_mat_norm
Definition
LSQR.h:306
LSQR_WORK
Definition
LSQR.h:334
LSQR_WORK::bidiag_wrk_vec
dvec * bidiag_wrk_vec
Definition
LSQR.h:335
LSQR_WORK::srch_dir_vec
dvec * srch_dir_vec
Definition
LSQR.h:336
neuland
calibration
legacy
LSQR.h
Generated by
1.18.0