def gfpp_inverse ( n, alpha ): #*****************************************************************************80 # ## GFPP_INVERSE returns the inverse of the GFPP matrix. # # Example: # # N = 5, ALPHA = 1 # # 0.5000 -0.2500 -0.1250 -0.0625 -0.0625 # 0 0.5000 -0.2500 -0.1250 -0.1250 # 0 0 0.5000 -0.2500 -0.2500 # 0 0 0 0.5000 -0.5000 # 0.5000 0.2500 0.1250 0.0625 0.0625 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 April 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Input, real ALPHA, determines subdiagonal elements. # # Output, real A(N,N), the inverse matrix. # import numpy as np from r8mat_mm import r8mat_mm from tri_l1_inverse import tri_l1_inverse from tri_u_inverse import tri_u_inverse p, l, u = gfpp_plu ( n, alpha ) p_inverse = np.transpose ( p ) l_inverse = tri_l1_inverse ( n, l ) u_inverse = tri_u_inverse ( n, u ) lp_inverse = r8mat_mm ( n, n, n, l_inverse, p_inverse ) a = r8mat_mm ( n, n, n, u_inverse, lp_inverse ) return a
def gfpp_inverse(n, alpha): #*****************************************************************************80 # ## GFPP_INVERSE returns the inverse of the GFPP matrix. # # Example: # # N = 5, ALPHA = 1 # # 0.5000 -0.2500 -0.1250 -0.0625 -0.0625 # 0 0.5000 -0.2500 -0.1250 -0.1250 # 0 0 0.5000 -0.2500 -0.2500 # 0 0 0 0.5000 -0.5000 # 0.5000 0.2500 0.1250 0.0625 0.0625 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 April 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Input, real ALPHA, determines subdiagonal elements. # # Output, real A(N,N), the inverse matrix. # import numpy as np from r8mat_mm import r8mat_mm from tri_l1_inverse import tri_l1_inverse from tri_u_inverse import tri_u_inverse p, l, u = gfpp_plu(n, alpha) p_inverse = np.transpose(p) l_inverse = tri_l1_inverse(n, l) u_inverse = tri_u_inverse(n, u) lp_inverse = r8mat_mm(n, n, n, l_inverse, p_inverse) a = r8mat_mm(n, n, n, u_inverse, lp_inverse) return a
def r8mat_is_inverse ( n, a, b ): #*****************************************************************************80 # ## R8MAT_IS_INVERSE measures the error in a matrix inverse. # # Discussion: # # An R8MAT is a matrix of real values. # # This routine returns the sum of the Frobenius norms of # A * B - I and B * A - I. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Input, real A(N,N), the matrix. # # Input, real B(M,N), the inverse matrix. # # Output, real VALUE, the Frobenius norm # of the difference matrices A * B - I and B * A - I. # from identity import identity from r8mat_mm import r8mat_mm from r8mat_sub import r8mat_sub from r8mat_norm_fro import r8mat_norm_fro ab = r8mat_mm ( n, n, n, a, b ) ba = r8mat_mm ( n, n, n, b, a ) id = identity ( n, n ) d1 = r8mat_sub ( n, n, ab, id ) d2 = r8mat_sub ( n, n, ba, id ) value = r8mat_norm_fro ( n, n, d1 ) \ + r8mat_norm_fro ( n, n, d2 ) return value
def carry_inverse(n, alpha): #*****************************************************************************80 # ## CARRY_INVERSE returns the inverse of the CARRY matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ALPHA, the numeric base used in the addition. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # from diagonal import diagonal from r8_factorial import r8_factorial from r8mat_mm import r8mat_mm v = carry_eigen_left(n, alpha) d = carry_eigenvalues(n, alpha) for i in range(0, n): d[i] = 1.0 / d[i] d_inv = diagonal(n, n, d) u = carry_eigen_right(n, alpha) dv = r8mat_mm(n, n, n, d_inv, v) a = r8mat_mm(n, n, n, u, dv) t = r8_factorial(n) for j in range(0, n): for i in range(0, n): a[i, j] = a[i, j] / t return a
def carry_inverse ( n, alpha ): #*****************************************************************************80 # ## CARRY_INVERSE returns the inverse of the CARRY matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ALPHA, the numeric base used in the addition. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # from diagonal import diagonal from r8_factorial import r8_factorial from r8mat_mm import r8mat_mm v = carry_eigen_left ( n, alpha ) d = carry_eigenvalues ( n, alpha ) for i in range ( 0, n ): d[i] = 1.0 / d[i] d_inv = diagonal ( n, n, d ) u = carry_eigen_right ( n, alpha ) dv = r8mat_mm ( n, n, n, d_inv, v ) a = r8mat_mm ( n, n, n, u, dv ) t = r8_factorial ( n ) for j in range ( 0, n ): for i in range ( 0, n ): a[i,j] = a[i,j] / t return a
def r8mat_is_plu ( m, n, a, p, l, u ): #*****************************************************************************80 # ## R8MAT_IS_PLU measures the error in a PLU factorization. # # Discussion: # # An R8MAT is a matrix of real values. # # This routine computes the Frobenius norm of # # A - P * L * U # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, N, the order of the matrix. # # Input, real A(M,N), the matrix. # # Input, real P(M,M), L(M,M), U(M,N), the PLU factors. # # Output, real VALUE, the Frobenius norm # of the difference matrix A - P * L * U. # from r8mat_mm import r8mat_mm from r8mat_sub import r8mat_sub from r8mat_norm_fro import r8mat_norm_fro lu = r8mat_mm ( m, m, n, l, u ) plu = r8mat_mm ( m, m, n, p, lu ) d = r8mat_sub ( m, n, a, plu ) value = r8mat_norm_fro ( m, n, d ) return value
def r8mat_is_solution(m, n, k, a, x, b): #*****************************************************************************80 # ## R8MAT_IS_SOLUTION measures the error in a linear system solution. # # Discussion: # # An R8MAT is a matrix of real values. # # The system matrix A is an M x N matrix. # It is not required that A be invertible. # # The solution vector X is actually allowed to be an N x K matrix. # # The right hand side "vector" B is actually allowed to be an M x K matrix. # # This routine simply returns the Frobenius norm of the M x K matrix: # A * X - B. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, N, K, the order of the matrices. # # Input, real A(M,N), X(N,K), B(M,K), the matrices. # # Output, real VALUE, the Frobenius norm # of the difference matrix A * X - B, which would be exactly zero # if X was the "solution" of the linear system. # from r8mat_norm_fro import r8mat_norm_fro from r8mat_mm import r8mat_mm from r8mat_sub import r8mat_sub # # AX = A*X # ax = r8mat_mm(m, n, k, a, x) # # AXMB = AX-B. # axmb = r8mat_sub(m, k, ax, b) # # Value = ||AX-B|\ # value = r8mat_norm_fro(m, k, axmb) return value
def r8mat_is_solution ( m, n, k, a, x, b ): #*****************************************************************************80 # ## R8MAT_IS_SOLUTION measures the error in a linear system solution. # # Discussion: # # An R8MAT is a matrix of real values. # # The system matrix A is an M x N matrix. # It is not required that A be invertible. # # The solution vector X is actually allowed to be an N x K matrix. # # The right hand side "vector" B is actually allowed to be an M x K matrix. # # This routine simply returns the Frobenius norm of the M x K matrix: # A * X - B. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, N, K, the order of the matrices. # # Input, real A(M,N), X(N,K), B(M,K), the matrices. # # Output, real VALUE, the Frobenius norm # of the difference matrix A * X - B, which would be exactly zero # if X was the "solution" of the linear system. # from r8mat_norm_fro import r8mat_norm_fro from r8mat_mm import r8mat_mm from r8mat_sub import r8mat_sub # # AX = A*X # ax = r8mat_mm ( m, n, k, a, x ) # # AXMB = AX-B. # axmb = r8mat_sub ( m, k, ax, b ) # # Value = ||AX-B|\ # value = r8mat_norm_fro ( m, k, axmb ) return value
def borderband_inverse(n): #*****************************************************************************80 # ## BORDERBAND_INVERSE returns the inverse of the BORDERBAND matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real A(N,N), the inverse matrix. # import numpy as np from r8mat_mm import r8mat_mm from tri_l1_inverse import tri_l1_inverse from tri_u_inverse import tri_u_inverse p, l, u = borderband_plu(n) p_inverse = np.transpose(p) l_inverse = tri_l1_inverse(n, l) u_inverse = tri_u_inverse(n, u) lipi = r8mat_mm(n, n, n, l_inverse, p_inverse) a = r8mat_mm(n, n, n, u_inverse, lipi) return a
def r8mat_is_eigen_left ( n, k, a, x, lam ): #*****************************************************************************80 # ## R8MAT_IS_EIGEN_LEFT determines the error in a left eigensystem. # # Discussion: # # An R8MAT is a matrix of real values. # # This routine computes the Frobenius norm of # # X * A - LAMBDA * X # # where # # A is an N by N matrix, # X is an K by N matrix (each of K columns is an eigenvector) # LAMBDA is a K by K diagonal matrix of eigenvalues. # # This routine assumes that A, X and LAMBDA are all real. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer K, the number of eigenvectors. # K is usually 1 or N. # # Input, real A(N,N), the matrix. # # Input, real X(K,N), the K eigenvectors. # # Input, real LAM(K), the K eigenvalues. # # Output, real VALUE, the Frobenius norm of X * A - LAM * X. # from r8mat_mm import r8mat_mm from r8mat_norm_fro import r8mat_norm_fro c = r8mat_mm ( k, n, n, x, a ) for i in range ( 0, k ): for j in range ( 0, n ): c[i,j] = c[i,j] - lam[i] * x[i,j] value = r8mat_norm_fro ( k, n, c ) return value
def r8mat_is_eigen_right ( n, k, a, x, lam ): #*****************************************************************************80 # ## R8MAT_IS_EIGEN_RIGHT determines the error in a right eigensystem. # # Discussion: # # An R8MAT is a matrix of real values. # # This routine computes the Frobenius norm of # # A * X - X * LAMBDA # # where # # A is an N by N matrix, # X is an N by K matrix (each of K columns is an eigenvector) # LAMBDA is a K by K diagonal matrix of eigenvalues. # # This routine assumes that A, X and LAMBDA are all real. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer K, the number of eigenvectors. # K is usually 1 or N. # # Input, real A(N,N), the matrix. # # Input, real X(N,K), the K eigenvectors. # # Input, real LAM(K), the K eigenvalues. # # Output, real VALUE, the Frobenius norm # of the difference matrix A * X - X * LAM, which would be exactly zero # if X and LAM were exact eigenvectors and eigenvalues of A. # from r8mat_mm import r8mat_mm from r8mat_norm_fro import r8mat_norm_fro c = r8mat_mm ( n, n, k, a, x ) for j in range ( 0, k ): for i in range ( 0, n ): c[i,j] = c[i,j] - lam[j] * x[i,j] value = r8mat_norm_fro ( n, k, c ) return value
def r8mat_is_eigen_right(n, k, a, x, lam): #*****************************************************************************80 # ## R8MAT_IS_EIGEN_RIGHT determines the error in a right eigensystem. # # Discussion: # # An R8MAT is a matrix of real values. # # This routine computes the Frobenius norm of # # A * X - X * LAMBDA # # where # # A is an N by N matrix, # X is an N by K matrix (each of K columns is an eigenvector) # LAMBDA is a K by K diagonal matrix of eigenvalues. # # This routine assumes that A, X and LAMBDA are all real. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer K, the number of eigenvectors. # K is usually 1 or N. # # Input, real A(N,N), the matrix. # # Input, real X(N,K), the K eigenvectors. # # Input, real LAM(K), the K eigenvalues. # # Output, real VALUE, the Frobenius norm # of the difference matrix A * X - X * LAM, which would be exactly zero # if X and LAM were exact eigenvectors and eigenvalues of A. # from r8mat_mm import r8mat_mm from r8mat_norm_fro import r8mat_norm_fro c = r8mat_mm(n, n, k, a, x) for j in range(0, k): for i in range(0, n): c[i, j] = c[i, j] - lam[j] * x[i, j] value = r8mat_norm_fro(n, k, c) return value
def r8mat_is_solution_test(): #*****************************************************************************80 # ## R8MAT_IS_SOLUTION_TEST tests R8MAT_IS_SOLUTION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 March 2015 # # Author: # # John Burkardt # from i4_uniform_ab import i4_uniform_ab from r8mat_mm import r8mat_mm from r8mat_uniform_ab import r8mat_uniform_ab print '' print 'R8MAT_IS_SOLUTION_TEST:' print ' R8MAT_IS_SOLUTION tests whether X is the solution of' print ' A*X=B by computing the Frobenius norm of the residual.' # # Get random shapes. # i4_lo = 1 i4_hi = 10 seed = 123456789 m, seed = i4_uniform_ab(i4_lo, i4_hi, seed) n, seed = i4_uniform_ab(i4_lo, i4_hi, seed) k, seed = i4_uniform_ab(i4_lo, i4_hi, seed) # # Get a random A. # r8_lo = -5.0 r8_hi = +5.0 a, seed = r8mat_uniform_ab(m, n, r8_lo, r8_hi, seed) # # Get a random X. # r8_lo = -5.0 r8_hi = +5.0 x, seed = r8mat_uniform_ab(n, k, r8_lo, r8_hi, seed) # # Compute B = A * X. # b = r8mat_mm(m, n, k, a, x) # # Compute || A*X-B|| # enorm = r8mat_is_solution(m, n, k, a, x, b) print '' print ' A is %d by %d' % (m, n) print ' X is %d by %d' % (n, k) print ' B is %d by %d' % (m, k) print ' Frobenius error in A*X-B is %g' % (enorm) print '' print 'R8MAT_IS_SOLUTION_TEST' print ' Normal end of execution.' return
def r8mat_is_solution_test ( ): #*****************************************************************************80 # ## R8MAT_IS_SOLUTION_TEST tests R8MAT_IS_SOLUTION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 March 2015 # # Author: # # John Burkardt # from i4_uniform_ab import i4_uniform_ab from r8mat_mm import r8mat_mm from r8mat_uniform_ab import r8mat_uniform_ab print '' print 'R8MAT_IS_SOLUTION_TEST:' print ' R8MAT_IS_SOLUTION tests whether X is the solution of' print ' A*X=B by computing the Frobenius norm of the residual.' # # Get random shapes. # i4_lo = 1 i4_hi = 10 seed = 123456789 m, seed = i4_uniform_ab ( i4_lo, i4_hi, seed ) n, seed = i4_uniform_ab ( i4_lo, i4_hi, seed ) k, seed = i4_uniform_ab ( i4_lo, i4_hi, seed ) # # Get a random A. # r8_lo = -5.0 r8_hi = +5.0 a, seed = r8mat_uniform_ab ( m, n, r8_lo, r8_hi, seed ) # # Get a random X. # r8_lo = -5.0 r8_hi = +5.0 x, seed = r8mat_uniform_ab ( n, k, r8_lo, r8_hi, seed ) # # Compute B = A * X. # b = r8mat_mm ( m, n, k, a, x ) # # Compute || A*X-B|| # enorm = r8mat_is_solution ( m, n, k, a, x, b ) print '' print ' A is %d by %d' % ( m, n ) print ' X is %d by %d' % ( n, k ) print ' B is %d by %d' % ( m, k ) print ' Frobenius error in A*X-B is %g' % ( enorm ) print '' print 'R8MAT_IS_SOLUTION_TEST' print ' Normal end of execution.' return
def r8mat_is_eigen_left(n, k, a, x, lam): #*****************************************************************************80 # ## R8MAT_IS_EIGEN_LEFT determines the error in a left eigensystem. # # Discussion: # # An R8MAT is a matrix of real values. # # This routine computes the Frobenius norm of # # X * A - LAMBDA * X # # where # # A is an N by N matrix, # X is an K by N matrix (each of K columns is an eigenvector) # LAMBDA is a K by K diagonal matrix of eigenvalues. # # This routine assumes that A, X and LAMBDA are all real. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer K, the number of eigenvectors. # K is usually 1 or N. # # Input, real A(N,N), the matrix. # # Input, real X(K,N), the K eigenvectors. # # Input, real LAM(K), the K eigenvalues. # # Output, real VALUE, the Frobenius norm of X * A - LAM * X. # from r8mat_mm import r8mat_mm from r8mat_norm_fro import r8mat_norm_fro c = r8mat_mm(k, n, n, x, a) for i in range(0, k): for j in range(0, n): c[i, j] = c[i, j] - lam[i] * x[i, j] value = r8mat_norm_fro(k, n, c) return value
def r8vec_house_column_test ( ): #*****************************************************************************80 # ## R8VEC_HOUSE_COLUMN_TEST tests R8VEC_HOUSE_COLUMN. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 February 2015 # # Author: # # John Burkardt # import numpy as np from r8mat_house_form import r8mat_house_form from r8mat_mm import r8mat_mm from r8mat_print import r8mat_print from r8mat_uniform_ab import r8mat_uniform_ab print '' print 'R8VEC_HOUSE_COLUMN_TEST' print ' R8VEC_HOUSE_COLUMN returns the compact form of' print ' a Householder matrix that "packs" a column' print ' of a matrix.' # # Get a random matrix. # n = 4 r8_lo = 0.0 r8_hi = 5.0 seed = 123456789; a, seed = r8mat_uniform_ab ( n, n, r8_lo, r8_hi, seed ) r8mat_print ( n, n, a, ' Matrix A:' ) a_col = np.zeros ( n ) for k in range ( 0, n - 1 ): print '' print ' Working on column K = %d' % ( k ) for i in range ( 0, n ): a_col[i] = a[i,k] v = r8vec_house_column ( n, a_col, k ) h = r8mat_house_form ( n, v ) r8mat_print ( n, n, h, ' Householder matrix H:' ) ha = r8mat_mm ( n, n, n, h, a ) r8mat_print ( n, n, ha, ' Product H*A:' ) # # If we set A := HA, then we can successively convert A to upper # triangular form. # a = ha print '' print 'R8VEC_HOUSE_COLUMN_TEST' print ' Normal end of execution.' return
def r8vec_house_column_test ( ): #*****************************************************************************80 # ## R8VEC_HOUSE_COLUMN_TEST tests R8VEC_HOUSE_COLUMN. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 February 2015 # # Author: # # John Burkardt # import numpy as np from r8mat_house_form import r8mat_house_form from r8mat_mm import r8mat_mm from r8mat_print import r8mat_print from r8mat_uniform_ab import r8mat_uniform_ab print '' print 'R8VEC_HOUSE_COLUMN_TEST' print ' R8VEC_HOUSE_COLUMN returns the compact form of' print ' a Householder matrix that "packs" a column' print ' of a matrix.' # # Get a random matrix. # n = 4 r8_lo = 0.0 r8_hi = 5.0 seed = 123456789; a, seed = r8mat_uniform_ab ( n, n, r8_lo, r8_hi, seed ) r8mat_print ( n, n, a, ' Matrix A:' ) a_col = np.zeros ( n ) for k in range ( 0, n - 1 ): print '' print ' Working on column K = %d' % ( k ) for i in range ( 0, n ): a_col[i] = a[i,k] v = r8vec_house_column ( n, a_col, k ) h = r8mat_house_form ( n, v ) r8mat_print ( n, n, h, ' Householder matrix H:' ) ha = r8mat_mm ( n, n, n, h, a ) r8mat_print ( n, n, ha, ' Product H*A:' ) # # If we set A := HA, then we can successively convert A to upper # triangular form. # a = ha # # Terminate. # print '' print 'R8VEC_HOUSE_COLUMN_TEST' print ' Normal end of execution.' return