# naginterfaces.library.linsys.real_​gen_​solve¶

naginterfaces.library.linsys.real_gen_solve(a, b, tol, lwork)[source]

real_gen_solve finds the solution of a linear least squares problem, , where is a real matrix and is an element vector. If the matrix of observations is not of full rank, then the minimal least squares solution is returned.

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

https://www.nag.com/numeric/nl/nagdoc_28.7/flhtml/f04/f04jgf.html

Parameters
afloat, array-like, shape

The matrix .

bfloat, array-like, shape

The right-hand side vector .

tolfloat

A relative tolerance to be used to determine the rank of . should be chosen as approximately 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 . See Further Comments for a description of how is used to determine rank. If is outside the range , where is the machine precision, the value is used in place of . For most problems this is unreasonably small.

lworkint

The dimension of the array .

Returns
afloat, ndarray, shape

If is returned as , is overwritten by details of the factorization of .

If is returned as , the first rows of are overwritten by the right-hand singular vectors, stored by rows; and the remaining rows of the array are used as workspace.

bfloat, ndarray, shape

The first elements of contain the minimal least squares solution vector . The remaining elements are used for workspace.

svdbool

Is returned as if the least squares solution has been obtained from the factorization of . In this case is of full rank. is returned as if the least squares solution has been obtained from the singular value decomposition of .

sigmafloat

The standard error, i.e., the value when , and the value zero when . Here is the residual vector and is the rank of .

irankint

, the rank of the matrix . It should be noted that it is possible for to be returned as and to be returned as . This means that the matrix only just failed the test for nonsingularity.

workfloat, ndarray, shape

If is returned as , then the first elements of contain information on the factorization of (see argument above), and contains the condition number of the upper triangular matrix .

If is returned as , then the first elements of contain the singular values of arranged in descending order and contains the total number of iterations taken by the algorithm.

The rest of is used as workspace.

Raises
NagValueError
(errno )

On entry, is too small. Minimum size required: .

(errno )

On entry, .

Constraint: .

(errno )

On entry, and .

Constraint: .

(errno )

The algorithm has failed to converge to the singular values in iterations. This failure can only happen when the singular value decomposition is employed, but even then it is not likely to occur.

Notes

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

The minimal least squares solution of the problem is the vector of minimum (Euclidean) length which minimizes the length of the residual vector .

The real matrix is factorized as

where is an orthogonal matrix and is an upper triangular matrix. If is of full rank, then the least squares solution is given by

If is not of full rank, then the singular value decomposition of is obtained so that is factorized as

where and are orthogonal matrices and is the diagonal matrix

with , these being the singular values of . If the singular values are negligible, but is not negligible, relative to the data errors in , then the rank of is taken to be and the minimal least squares solution is given by

where .

The function also returns the value of the standard error

being the residual sum of squares and the rank of .

References

Lawson, C L and Hanson, R J, 1974, Solving Least Squares Problems, Prentice–Hall