#! /usr/bin/env python # def normal_solve ( m, n, a, b ): #*****************************************************************************80 # ## NORMAL_SOLVE solves a linear system using the normal equations. # # Discussion: # # Given a presumably rectangular MxN system of equations A*x=b, this routine # sets up the NxN system A'*A*x=A'b. Assuming N <= M, and that A has full # column rank, the system will be solvable, and the vector x that is returned # will minimize the Euclidean norm of the residual. # # One drawback to this approach is that the condition number of the linear # system A'*A is effectively the square of the condition number of A, # meaning that there is a substantial loss of accuracy. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 29 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. # It must be the case that N <= M. # # Input, real A(M,N), the matrix. # The matrix must have full column rank. # # Input, real B(M), the right hand side. # # Output, real X(N), the least squares solution. # # Output, integer FLAG, # 0, no error was detected. # 1, an error occurred. # import numpy as np from r8mat_cholesky_factor import r8mat_cholesky_factor from r8mat_cholesky_solve import r8mat_cholesky_solve flag = False x = np.zeros ( n ) if ( m < n ): flag = True else: ata = np.dot ( a.transpose ( ), a ) atb = np.dot ( a.transpose ( ), b ) ata_c, flag = r8mat_cholesky_factor ( n, ata ) if ( not flag ): x = r8mat_cholesky_solve ( n, ata_c, atb ) return x, flag def normal_solve_test ( ): #*****************************************************************************80 # ## NORMAL_SOLVE_TEST tests NORMAL_SOLVE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 29 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 ( 'NORMAL_SOLVE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' NORMAL_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 NORMAL_SOLVE result X2.' ) print ( '' ) print ( ' NORMAL_SOLVE cannot be applied when N < M,' ) print ( ' or if the matrix does not have full column rank.' ) 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 NORMAL_SOLVE on the problem. # x2, flag = normal_solve ( m, n, a, b ) if ( flag ): print ( ' %5d %4d %4d' % ( prob, m, n ) ), print ( ' %12.4g ------------' % ( b_norm ) ), print ( ' %12.4g ------------' % ( x1_norm ) ), print ( ' %12.4g ------------' % ( r1_norm ) ) else: 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 ( 'NORMAL_SOLVE_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) normal_solve_test ( ) timestamp ( )