#! /usr/bin/env python
#
def qr_solve ( m, n, a, b ):
#*****************************************************************************80
#
## QR_SOLVE solves a linear system in the least squares sense.
#
# Discussion:
#
# If the matrix A has full column rank, then the solution X should be the
# unique vector that minimizes the Euclidean norm of the residual.
#
# If the matrix A does not have full column rank, then the solution is
# not unique; the vector X will minimize the residual norm, but so will
# various other vectors.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 27 August 2016
#
# Author:
#
# John Burkardt
#
# Reference:
#
# David Kahaner, Cleve Moler, Steven Nash,
# Numerical Methods and Software,
# Prentice Hall, 1989,
# ISBN: 0-13-627258-4,
# LC: TA345.K34.
#
# Parameters:
#
# Input, integer M, the number of rows of A.
#
# Input, integer N, the number of columns of A.
#
# Input, real A(M,N), the matrix.
#
# Input, real B(M), the right hand side.
#
# Output, real X(N), the least squares solution.
#
from dqrank import dqrank
from dqrlss import dqrlss
lda = m
eps = 2.220446049250313E-016
tol = 0.0
for i in range ( 0, m ):
for j in range ( 0, n ):
tol = max ( tol, abs ( a[i,j] ) )
tol = eps / tol
#
# Factor the matrix.
#
kr, jpvt, qraux, a = dqrank ( a, lda, m, n, tol )
#
# Solve the least-squares problem.
#
x, r = dqrlss ( a, lda, m, n, kr, b, jpvt, qraux )
return x
def qr_solve_test ( ):
#*****************************************************************************80
#
## QR_SOLVE_TEST tests QR_SOLVE.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 26 August 2016
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
from r8vec_norm import r8vec_norm
from test_ls import p00_a
from test_ls import p00_b
from test_ls import p00_m
from test_ls import p00_n
from test_ls import p00_prob_num
from test_ls import p00_x
print ( '' )
print ( 'QR_SOLVE_TEST' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' QR_SOLVE is a function with a simple interface which' )
print ( ' solves a linear system A*x = b in the least squares sense.' )
print ( ' Compare a tabulated solution X1 to the QR_SOLVE result X2.' )
prob_num = p00_prob_num ( )
print ( '' )
print ( ' Number of problems = %d' % ( prob_num ) )
print ( '' )
print ( ' Index M N ||B|| ||X1 - X2||' ),
print ( ' ||X1|| ||X2|| ||R1|| ||R2||' )
print ( '' )
for prob in range ( 1, prob_num + 1 ):
#
# Get problem size.
#
m = p00_m ( prob )
n = p00_n ( prob )
#
# Retrieve problem data.
#
a = p00_a ( prob, m, n )
b = p00_b ( prob, m )
x1 = p00_x ( prob, n )
b_norm = r8vec_norm ( m, b )
x1_norm = r8vec_norm ( n, x1 )
r1 = np.dot ( a, x1 ) - b
r1_norm = r8vec_norm ( m, r1 )
#
# Use QR_SOLVE on the problem.
# Since we want to compute residuals ourselves, we need
# to keep the originals of A and B, so we make copies
# to send to QR_SOLVE.
#
a_qr = a.copy ( )
b_qr = b.copy ( )
x2 = qr_solve ( m, n, a_qr, b_qr )
x2_norm = r8vec_norm ( n, x2 )
r2 = np.dot ( a, x2 ) - b
r2_norm = r8vec_norm ( m, r2 )
#
# Compare tabulated and computed solutions.
#
x_diff_norm = r8vec_norm ( n, x1 - x2 )
#
# Report results for this problem.
#
print ( ' %5d %4d %4d' % ( prob, m, n ) ),
print ( ' %12.4g %12.4g' % ( b_norm, x_diff_norm ) ),
print ( ' %12.4g %12.4g' % ( x1_norm, x2_norm ) ),
print ( ' %12.4g %12.4g' % ( r1_norm, r2_norm ) )
#
# Terminate.
#
print ( '' )
print ( 'QR_SOLVE_TEST' )
print ( ' Normal end of execution.' )
return
if ( __name__ == '__main__' ):
from timestamp import timestamp
timestamp ( )
qr_solve_test ( )
timestamp ( )