# qr_solve

qr_solve, a MATLAB code which computes a linear least squares (LLS) solution of a system A*x=b, using the QR factorization.

There are many possible cases that can arise with the matrix A. Formally, we distinguish the cases M < N, M = N, and M > N, and we expect trouble whenever M is not equal to N. Trouble may also arise when M = N but the matrix is singular. Even if the matrix is, mathematically speaking, non-singular, it may be so close to singularity that an accurate solution is difficult to achieve.

When M > N, we are placing more conditions than we have degrees of freedom, so we suppose that such a linear system cannot be solved. However, it is possible that the extra conditions are illusory, being constructed from linear combinations of a fundamental set of N conditions. Thus, a system that we typically call "overdetermined" can have a solution in the ordinary sense, that satisfies all the conditions, as long as the right hand side is "consistent". Another way of saying this is that the system is solvable if the right hand side lies in the column space of A ... although that simply says that it is a linear combination of the columns of A, which just says A*x=b.

If A does not have full column rank, however, then even if the right hand side lies in the column space of A, there will be more than one linear combination of columns that produce b. Thus, the equations are consistent, the system is solvable, but not uniquely so.

If M < N, then we are placing fewer conditions than we have degrees of freedom. As long as the right hand side lies in the column space of A, we can guarantee that there will be multiple solutions.

Thus, the question of a "solution" to the problem A*x=b is complicated enough that it seems to defy a common algorithmic approach. Nonetheless, there are some sensible, robust procedures for producing an answer that corresponds to the classical solution, or solves the overdetermined problem when the right hand side is consistent. This is the linear least squares solution, which finds a vector x which minimizes the Euclidean norm of the residual: ||r|| = ||A*x-b||. This solution is produced by computing the QR factorization of the matrix A

When there are multiple solutions to the problem, the QR approach used here produces a solution. A more satisfactory approach, using the pseudoinverse, will produce a solution x which satisfies the additional constraint that it has the minimum norm of the entire family of solutions. That pseudoinverse approach is not implemented in this library. The singular value decomposition (SVD) can also produce this minimal solution.

For comparison, a solver that applies the normal equations is included. This approach requires M ≥ N, and that A have full column rank. It constructs and solves the NxN system A'*A*x=A'*b. This system has a condition number that is the square of the original system, so it also suffers from a significant loss in accuracy.

We also include an SVD solver, which uses the pseudoinverse approach. First compute A = U * S * V', where U and V are orthogonal, and S is MxN diagonal, then to solve A*x=b write x = V * S^ * U' * b, where S^ is the matrix formed by transposing S and then replacing each nonzero diagonal element s by 1/s. (However, very small elements should probably be zeroed instead of inverted.) This procedure will also produce a vector x which minimizes the Euclidean norm. However, it has one feature that the QR solver does not: in cases where the solution x is not unique (because A does not have full column rank) the SVD solver returns the unique vector x of minimum Euclidean norm.

The test program also needs the TEST_LLS library.

### Languages:

qr_solve is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version and a Python version.

### Related Data and Programs:

linpack_d, a MATLAB code which solves linear systems using double precision real arithmetic;

llsq, a MATLAB code which solves the simple linear least squares (LLS) problem of finding the formula of a straight line y=a*x+b which minimizes the root-mean-square error to a set of N data points.

r8lib, a MATLAB code which contains many utility routines using double precision real (R8) arithmetic.

test_lls, a MATLAB code which implements linear least squares test problems of the form A*x=b.

### Reference:

1. Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
LINPACK User's Guide,
SIAM, 1979,
ISBN13: 978-0-898711-72-1,
LC: QA214.L56.
2. David Kahaner, Cleve Moler, Steven Nash,
Numerical Methods and Software,
Prentice Hall, 1989,
ISBN: 0-13-627258-4,
LC: TA345.K34.

### Source Code:

• daxpy.m, adds a multiple of one vector to another;
• ddot.m, computes the dot product of two vectors;
• dnrm2.m, computes the Euclidean norm of a vector;
• dqrank.m, QR-factors a rectangular matrix and estimates its rank.
• dqrdc.m, computes the QR factorization of a rectangular matrix;
• dqrlss.m, solves a linear system in a least squares sense.
• dqrsl.m, computes transformations, projections, and least squares solutions;
• drot.m, applies a plane rotation;
• drotg.m, constructs a Givens plane rotation;
• dscal.m, scales a vector by a constant;
• dsvdc.m, computes the singular value decomposition of a real rectangular matrix.
• dswap.m, swaps two vectors;
• normal_solve.m, solves a rectangular linear system using the normal equations.
• qr_factor_solve.m, solves a rectangular linear system using the QR factorization.
• r8_mop.m, returns a power of -1 as an R8;
• r8_sign.m, returns the sign of an R8.
• r8mat_cholesky_factor.m, computes the Cholesky factor L*L' of a symmetric positive definite R8MAT;
• r8mat_cholesky_solve.m, solves a Cholesky factored linear system A * x = b;
• r8mat_l_solve.m, solves a lower triangular linear system;
• r8mat_lt_solve.m, solves a transposed lower triangular linear system;
• svd_solve.m, solves a rectangular linear system using the singular value decomposition (SVD).

Last revised on 11 January 2021.