#! /usr/bin/env python # def r8ge_fa ( n, a ): #*****************************************************************************80 # ## R8GE_FA performs a LINPACK style PLU factorization of a R8GE matrix. # # Discussion: # # The R8GE storage format is used for a general M by N matrix. A storage # space is made for each logical entry. The two dimensional logical # array is mapped to a vector, in which storage is by columns. # # R8GE_FA is a simplified version of the LINPACK routine R8GEFA. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # # Reference: # # Dongarra, Bunch, Moler, Stewart, # LINPACK User's Guide, # SIAM, 1979 # # Parameters: # # Input, integer N, the order of the matrix. # N must be positive. # # Input, real A(N,N), the matrix to be factored. # # Output, real A_LU(N,N), an upper triangular matrix and # the multipliers used to obtain it. The factorization # can be written A = L * U, where L is a product of # permutation and unit lower triangular matrices and U # is upper triangular. # # Output, integer PIVOT(N), a vector of pivot indices. # # Output, integer INFO, singularity flag. # 0, no singularity detected. # nonzero, the factorization failed on the INFO-th step. # import numpy as np from sys import exit a_lu = np.zeros ( [ n, n ], dtype = np.float64 ) for j in range ( 0, n ): for i in range ( 0, n ): a_lu[i,j] = a[i,j] info = 0 pivot = np.zeros ( n, dtype = np.int32 ) for k in range ( 0, n - 1 ): # # Find L, the index of the pivot row. # l = k for i in range ( k + 1, n ): if ( abs ( a_lu[l,k] ) < abs ( a_lu[i,k] ) ): l = i pivot[k] = l # # If the pivot index is zero, the algorithm has failed. # if ( a_lu[l,k] == 0.0 ): info = k return a_lu, pivot, info # # Interchange rows L and K if necessary. # if ( l != k ): t = a_lu[l,k] a_lu[l,k] = a_lu[k,k] a_lu[k,k] = t # # Normalize the values that lie below the pivot entry A(K,K). # for i in range ( k + 1, n ): a_lu[i,k] = - a_lu[i,k] / a_lu[k,k] # # Row elimination with column indexing. # for j in range ( k + 1, n ): if ( l != k ): t = a_lu[l,j] a_lu[l,j] = a_lu[k,j] a_lu[k,j] = t for i in range ( k + 1, n ): a_lu[i,j] = a_lu[i,j] + a_lu[i,k] * a_lu[k,j] pivot[n-1] = n - 1 if ( a_lu[n-1,n-1] == 0.0 ): info = n - 1 return a_lu, pivot, info def r8ge_fa_test01 ( ): #*****************************************************************************80 # ## R8GE_FA_TEST01 tests R8GE_FA, R8GE_SL. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # import numpy as np import platform from r8vec_print import r8vec_print n = 10 seed = 123456789 print ( '' ) print ( 'R8GE_FA_TEST01' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' For a matrix in general storage,' ) print ( ' R8GE_FA computes the LU factors,' ) print ( ' R8GE_SL solves a factored system.' ) print ( '' ) print ( ' Matrix order N = %d' % ( n ) ) # # Set the matrix. # a, seed = r8ge_random ( n, n, seed ) # # Set the desired solution. # x = np.zeros ( n ) for i in range ( 0, n ): x[i] = float ( i + 1 ) # # Compute the corresponding right hand side. # b = r8ge_mxv ( n, n, a, x ) # # Factor the matrix. # a_lu, pivot, info = r8ge_fa ( n, a ) if ( info != 0 ): print ( '' ) print ( 'R8GE_FA_TEST01 - Warning!' ) print ( ' R8GE_FA declares the matrix is singular!' ) print ( ' The value of INFO is %d' % ( info ) ) return # # Solve the linear system. # job = 0 x = r8ge_sl ( n, a_lu, pivot, b, job ) r8vec_print ( n, x, ' Solution:' ) # # Set the desired solution. # for i in range ( 0, n ): x[i] = 1.0 # # Compute the corresponding right hand side. # job = 0 b = r8ge_ml ( n, a_lu, pivot, x, job ) # # Solve the system # job = 0 x = r8ge_sl ( n, a_lu, pivot, b, job ) r8vec_print ( n, x, ' Solution:' ) # # Set the desired solution. # x = np.zeros ( n ) for i in range ( 0, n ): x[i] = float ( i + 1 ) # # Compute the corresponding right hand side. # job = 1 b = r8ge_ml ( n, a_lu, pivot, x, job ) # # Solve the system # job = 1 x = r8ge_sl ( n, a_lu, pivot, b, job ) r8vec_print ( n, x, ' Solution of transposed system:' ) # # Terminate. # print ( '' ) print ( 'R8GE_FA_TEST01' ) print ( ' Normal end of execution.' ) return def r8ge_fa_test02 ( ): #*****************************************************************************80 # ## R8GE_FA_TEST02 tests R8GE_FA, R8GE_SL. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # import numpy as np import platform from i4vec_print import i4vec_print from r8vec_print import r8vec_print n = 5 seed = 123456789 print ( '' ) print ( 'R8GE_FA_TEST02' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' For a matrix in general storage,' ) print ( ' R8GE_FA computes the LU factors,' ) print ( ' R8GE_SL solves a factored system.' ) print ( '' ) print ( ' Matrix order N = %d' % ( n ) ) # # Set the matrix. # a, seed = r8ge_random ( n, n, seed ) r8ge_print ( n, n, a, ' The matrix:' ) # # Set the desired solution. # x = np.zeros ( n ) for i in range ( 0, n ): x[i] = float ( i + 1 ) # # Compute the corresponding right hand side. # b = r8ge_mxv ( n, n, a, x ) # # Factor the matrix. # a_lu, pivot, info = r8ge_fa ( n, a ) if ( info != 0 ): print ( '' ) print ( 'R8GE_FA_TEST02 - Warning!' ) print ( ' R8GE_FA declares the matrix is singular!' ) print ( ' The value of INFO is %d' % ( info ) ) # # Display the gory details. # r8ge_print ( n, n, a_lu, ' The compressed LU factors:' ) i4vec_print ( n, pivot, ' The pivot vector P:' ) # # Solve the linear system. # job = 0 x = r8ge_sl ( n, a_lu, pivot, b, job ) r8vec_print ( n, x, ' Solution:' ) # # Terminate. # print ( '' ) print ( 'R8GE_FA_TEST02' ) print ( ' Normal end of execution.' ) return def r8ge_ml ( n, a_lu, pivot, x, job ): #*****************************************************************************80 # ## R8GE_ML computes A * x or A' * x, using R8GE_FA factors. # # Discussion: # # The R8GE storage format is used for a general M by N matrix. A storage # space is made for each logical entry. The two dimensional logical # array is mapped to a vector, in which storage is by columns. # # It is assumed that R8GE_FA has overwritten the original matrix # information by LU factors. R8GE_ML is able to reconstruct the # original matrix from the LU factor data. # # R8GE_ML allows the user to check that the solution of a linear # system is correct, without having to save an unfactored copy # of the matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # N must be positive. # # Input, real A_LU(N,N), the LU factors from R8GE_FA. # # Input, integer PIVOT(N), the pivot vector computed by R8GE_FA. # # Input, real X(N), the vector to be multiplied. # # Input, integer JOB, specifies the operation to be done: # JOB = 0, compute A * x. # JOB nonzero, compute A' * X. # # Output, real B(N), the result of the multiplication. # import numpy as np b = np.zeros ( n ) for i in range ( 0, n ): b[i] = x[i] if ( job == 0 ): # # Y = U * X. # for j in range ( 0, n ): for i in range ( 0, j ): b[i] = b[i] + a_lu[i,j] * b[j] b[j] = a_lu[j,j] * b[j] # # B = PL * Y = PL * U * X = A * x. # for j in range ( n - 2, -1, -1 ): for i in range ( j + 1, n ): b[i] = b[i] - a_lu[i,j] * b[j] k = pivot[j] if ( k != j ): t = b[k] b[k] = b[j] b[j] = t else: # # Y = (PL)' * X: # for j in range ( 0, n - 1 ): k = pivot[j] if ( k != j ): t = b[k] b[k] = b[j] b[j] = t for i in range ( j + 1, n ): b[j] = b[j] - b[i] * a_lu[i,j] # # B = U' * Y = ( PL * U )' * X = A' * X. # for i in range ( n - 1, -1, -1 ): for j in range ( i + 1, n ): b[j] = b[j] + b[i] * a_lu[i,j] b[i] = b[i] * a_lu[i,i] return b def r8ge_mxv ( m, n, a, x ): #*****************************************************************************80 # ## R8GE_MXV multiplies an R8GE matrix times a vector. # # Discussion: # # The R8GE storage format is used for a general M by N matrix. A storage # space is made for each logical entry. The two dimensional logical # array is mapped to a vector, in which storage is by columns. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, the number of rows of the matrix. # M must be positive. # # Input, integer N, the number of columns of the matrix. # N must be positive. # # Input, real A(M,N), the R8GE matrix. # # Input, real X(N), the vector to be multiplied by A. # # Output, real B(M), the product A * x. # import numpy as np b = np.zeros ( m ) for i in range ( 0, m ): for j in range ( 0, n ): b[i] = b[i] + a[i,j] * x[j] return b def r8ge_print ( m, n, a, title ): #*****************************************************************************80 # ## R8GE_PRINT prints an R8GE matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, the number of rows in A. # # Input, integer N, the number of columns in A. # # Input, real A(M,N), the matrix. # # Input, string TITLE, a title. # r8ge_print_some ( m, n, a, 0, 0, m - 1, n - 1, title ) return def r8ge_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ): #*****************************************************************************80 # ## R8GE_PRINT_SOME prints out a portion of an R8GE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, N, the number of rows and columns of the matrix. # # Input, real A(M,N), an M by N matrix to be printed. # # Input, integer ILO, JLO, the first row and column to print. # # Input, integer IHI, JHI, the last row and column to print. # # Input, string TITLE, a title. # incx = 5 print ( '' ) print ( title ) if ( m <= 0 or n <= 0 ): print ( '' ) print ( ' (None)' ) return for j2lo in range ( max ( jlo, 0 ), min ( jhi + 1, n ), incx ): j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n ) j2hi = min ( j2hi, jhi ) print ( '' ) print ( ' Col: ', end = '' ) for j in range ( j2lo, j2hi + 1 ): print ( '%7d ' % ( j ), end = '' ) print ( '' ) print ( ' Row' ) i2lo = max ( ilo, 0 ) i2hi = min ( ihi, m ) for i in range ( i2lo, i2hi + 1 ): print ( '%7d :' % ( i ), end = '' ) for j in range ( j2lo, j2hi + 1 ): print ( '%12g ' % ( a[i,j] ), end = '' ) print ( '' ) return def r8ge_random ( m, n, seed ): #*****************************************************************************80 # ## R8GE_RANDOM randomizes a R8GE matrix. # # Discussion: # # The R8GE storage format is used for a general M by N matrix. A storage # space is made for each logical entry. The two dimensional logical # array is mapped to a vector, in which storage is by columns. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, the number of rows of the matrix. # M must be positive. # # Input, integer N, the number of columns of the matrix. # N must be positive. # # Input, integer SEED, a seed for the random number generator. # # Output, real A(M,N), the R8GE matrix. # # Output, integer SEED, an updated seed for the random number generator. # import numpy from math import floor from sys import exit i4_huge = 2147483647 seed = floor ( seed ) if ( seed < 0 ): seed = seed + i4_huge if ( seed == 0 ): print ( '' ) print ( 'R8GE_RANDOM - Fatal error!' ) print ( ' Input SEED = 0!' ) exit ( 'R8GE_RANDOM - Fatal error!' ) r = numpy.zeros ( [ m, n ] ) for j in range ( 0, n ): for i in range ( 0, m ): k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 seed = ( seed % i4_huge ) if ( seed < 0 ): seed = seed + i4_huge r[i,j] = seed * 4.656612875E-10 return r, seed def r8ge_sl ( n, a_lu, pivot, b, job ): #*****************************************************************************80 # ## R8GE_SL solves a system factored by R8GE_FA. # # Discussion: # # The R8GE storage format is used for a general M by N matrix. A storage # space is made for each logical entry. The two dimensional logical # array is mapped to a vector, in which storage is by columns. # # R8GE_SL is a simplified version of the LINPACK routine R8GESL. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # N must be positive. # # Input, real A_LU(N,N), the LU factors from R8GE_FA. # # Input, integer PIVOT(N), the pivot vector from R8GE_FA. # # Input, real B(N), the right hand side vector. # # Input, integer JOB, specifies the operation. # 0, solve A * x = b. # nonzero, solve A' * x = b. # # Output, real X(N), the solution vector. # import numpy as np x = np.zeros ( n ) for i in range ( 0, n ): x[i] = b[i] # # Solve A * x = b. # if ( job == 0 ): # # Solve PL * Y = B. # for k in range ( 0, n - 1 ): l = pivot[k] if ( l != k ): t = x[l] x[l] = x[k] x[k] = t for i in range ( k + 1, n ): x[i] = x[i] + a_lu[i,k] * x[k] # # Solve U * X = Y. # for k in range ( n - 1, -1, -1 ): x[k] = x[k] / a_lu[k,k] for i in range ( 0, k ): x[i] = x[i] - a_lu[i,k] * x[k] # # Solve A' * X = B. # else: # # Solve U' * Y = B. # for k in range ( 0, n ): for i in range ( 0, k ): x[k] = x[k] - x[i] * a_lu[i,k] x[k] = x[k] / a_lu[k,k] # # Solve ( PL )' * X = Y. # for k in range ( n - 2, -1, -1 ): for i in range ( k + 1, n ): x[k] = x[k] + x[i] * a_lu[i,k] l = pivot[k] if ( l != k ): t = x[l] x[l] = x[k] x[k] = t return x if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) r8ge_fa_test01 ( ) r8ge_fa_test02 ( ) timestamp ( )