# naginterfaces.library.linsys.real_​gen_​sparse_​lsqsol¶

naginterfaces.library.linsys.real_gen_sparse_lsqsol(n, b, aprod, damp=0.0, atol=- 1.0, btol=- 1.0, conlim=0.0, itnlim=0, msglvl=0, data=None, io_manager=None)[source]

real_gen_sparse_lsqsol solves sparse nonsymmetric equations, sparse linear least squares problems and sparse damped linear least squares problems, using a Lanczos algorithm.

For full information please refer to the NAG Library document for f04qa

https://www.nag.com/numeric/nl/nagdoc_28.5/flhtml/f04/f04qaf.html

Parameters
nint

, the number of columns of the matrix .

bfloat, array-like, shape

The right-hand side vector .

aprodcallable (mode, x, y) = aprod(mode, x, y, data=None)

must perform the operations and for given vectors and .

Parameters
modeint

Specifies which operation is to be performed.

must compute .

must compute .

xfloat, ndarray, shape

The vector .

yfloat, ndarray, shape

The vector .

dataarbitrary, optional, modifiable in place

User-communication data for callback functions.

Returns
modeint

May be used as a flag to indicate a failure in the computation of or . If is negative on exit from , real_gen_sparse_lsqsol will exit immediately with set to .

xfloat, array-like, shape

If , must be unchanged.

If , must contain .

yfloat, array-like, shape

If , must contain .

If , must be unchanged.

dampfloat, optional

The value . If either problem (1) or problem (2) is to be solved, must be supplied as zero.

atolfloat, optional

The tolerance, , of the convergence criteria (6) and (7); it should be an estimate of the largest relative error in the elements of . For example, if the elements of are correct to about significant figures, then should be set to about . If is supplied as less than , where is the machine precision, the value is used instead of .

btolfloat, optional

The tolerance, , of the convergence criterion (6); it should be an estimate of the largest relative error in the elements of . For example, if the elements of are correct to about significant figures, then should be set to about . If is supplied as less than , the value is used instead of .

conlimfloat, optional

The value of equation (8); it should be an upper limit on the condition number of . should not normally be chosen much larger than . If is supplied as zero, the value is used instead of .

itnlimint, optional

An upper limit on the number of iterations. If , the value is used in place of , but for ill-conditioned problems a higher value of is likely to be necessary.

msglvlint, optional

The level of printing from real_gen_sparse_lsqsol. If , then no printing occurs, but otherwise messages will be output on the file object associated with the advisory I/O unit (see FileObjManager). A description of the printed output is given in Further Comments. The level of printing is determined as follows:

No printing.

A brief summary is printed just prior to return from real_gen_sparse_lsqsol.

A summary line is printed periodically to monitor the progress of real_gen_sparse_lsqsol, together with a brief summary just prior to return from real_gen_sparse_lsqsol.

dataarbitrary, optional

User-communication data for callback functions.

io_managerFileObjManager, optional

Manager for I/O in this routine.

Returns
bfloat, ndarray, shape

is overwritten.

xfloat, ndarray, shape

The solution vector .

sefloat, ndarray, shape

The estimates of the standard errors of the components of . Thus contains an estimate of , where is the th diagonal element of the estimated variance-covariance matrix . The estimates returned in will be the lower bounds on the actual estimated standard errors, but will usually be correct to at least one significant figure.

itnlimint

Unchanged unless on entry, in which case it is set to .

itnint

The number of iterations performed.

anormfloat

An estimate of for the matrix of (4).

acondfloat

An estimate of which is a lower bound.

rnormfloat

An estimate of for the residual, , of (5) corresponding to the solution returned in . Note that is the function being minimized.

arnormfloat

An estimate of the corresponding to the solution returned in .

xnormfloat

An estimate of for the solution returned in .

informint

The reason for termination of real_gen_sparse_lsqsol.

The exact solution is . No iterations are performed in this case.

The termination criterion of (6) has been satisfied with and as the values supplied in and respectively.

The termination criterion of (7) has been satisfied with as the value supplied in .

The termination criterion of (6) has been satisfied with and/or as the value , where is the machine precision. One or both of the values supplied in and must have been less than and was too small for this machine.

The termination criterion of (7) has been satisfied with as the value . The value supplied in must have been less than and was too small for this machine.

The values , or correspond to failure with = 2, 3 and 4 respectively (see Exceptions) and when is negative will be set to the same negative value.

Raises
NagValueError
(errno )

On entry, .

Constraint: .

(errno )

On entry, .

Constraint: .

(errno )

Termination criteria not satisfied, but the condition number bound supplied in has been met.

(errno )

Termination criteria not satisfied, but the condition number is bounded by /machine.precision.

(errno )

Maximum number of iterations reached.

Warns
NagAlgorithmicWarning
(errno )

in has been set to: .

Notes

No equivalent traditional C interface for this routine exists in the NAG Library.

real_gen_sparse_lsqsol can be used to solve a system of linear equations

where is an sparse nonsymmetric matrix, or can be used to solve linear least squares problems, so that real_gen_sparse_lsqsol minimizes the value given by

where is an sparse matrix and denotes the Euclidean length of so that . A damping argument, , may be included in the least squares problem in which case real_gen_sparse_lsqsol minimizes the value given by

is supplied as the argument and should of course be zero if the solution to problems (1) and (2) is required. Minimizing in (3) is often called ridge regression.

real_gen_sparse_lsqsol is based upon algorithm LSQR (see Paige and Saunders (1982a) and Paige and Saunders (1982b)) and solves the problems by an algorithm based upon the Lanczos process. The function does not require explicitly, but is specified via which must perform the operations and for a given -element vector and element vector . An argument to specifies which of the two operations is required on a given entry.

The function also returns estimates of the standard errors of the sample regression coefficients ( , for ) given by the diagonal elements of the estimated variance-covariance matrix . When problem (2) is being solved and is of full rank, then is given by

and when problem (3) is being solved then is given by

Let denote the matrix

let denote the residual vector

corresponding to an iterate , so that is the function being minimized, and let denote the Frobenius (Euclidean) norm of . Then the function accepts as a solution if it is estimated that one of the following two conditions is satisfied:

where and are user-supplied tolerances which estimate the relative errors in and respectively. Condition (6) is appropriate for compatible problems where, in theory, we expect the residual to be zero and will be satisfied by an acceptable solution to a compatible problem. Condition (7) is appropriate for incompatible systems where we do not expect the residual to be zero and is based on the observation that, in theory,

when is a solution to the least squares problem, and so (7) will be satisfied by an acceptable solution to a linear least squares problem.

The function also includes a test to prevent convergence to solutions, , with unacceptably large elements. This can happen if is nearly singular or is nearly rank deficient. If we let the singular values of be

then the condition number of is defined as

where is the smallest nonzero singular value of and hence is the rank of . When , then is rank deficient, the least squares solution is not unique and real_gen_sparse_lsqsol will normally converge to the minimal length solution. In practice will not have exactly zero singular values, but may instead have small singular values that we wish to regard as zero.

The function provides for this possibility by terminating if

where is a user-supplied limit on the condition number of . For problem (1) termination with this condition indicates that is nearly singular and for problem (2) indicates that is nearly rank deficient and so has near linear dependencies in its columns. In this case inspection of , and , which are all returned by the function, will indicate whether or not an acceptable solution has been found. Condition (8), perhaps in conjunction with , can be used to try and ‘regularize’ least squares solutions. A full discussion of the stopping criteria is given in Section 6 of Paige and Saunders (1982a).

Introduction of a nonzero damping argument tends to reduce the size of the computed solution and to make its components less sensitive to changes in the data, and real_gen_sparse_lsqsol is applicable when a value of is known a priori. To have an effect, should normally be at least where is the machine precision. For further discussion see Paige and Saunders (1982b) and the references given there.

Whenever possible the matrix should be scaled so that the relative errors in the elements of are all of comparable size. Such a scaling helps to prevent the least squares problem from being unnecessarily sensitive to data errors and will normally reduce the number of iterations required. At the very least, in the absence of better information, the columns of should be scaled to have roughly equal column length.

References

Paige, C C and Saunders, M A, 1982, LSQR: An algorithm for sparse linear equations and sparse least squares, ACM Trans. Math. Software (8), 43–71

Paige, C C and Saunders, M A, 1982, Algorithm 583 LSQR: Sparse linear equations and least squares problems, ACM Trans. Math. Software (8), 195–209