#! /usr/bin/env python3 # from __future__ import division def a123_determinant ( ): #*****************************************************************************80 # ## a123_determinant() returns the determinant of the A123 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 March 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = 0.0 return value def a123_eigen_left ( ): #*****************************************************************************80 # ## a123_eigen_left() returns the left eigenvectors of the A123 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Output: # # real A(3,3), the eigenvectors. # import numpy as np a = np.array ( [ \ [ -0.464547273387671, -0.570795531228578, -0.677043789069485 ], \ [ -0.882905959653586, -0.239520420054206, 0.403865119545174 ], \ [ 0.408248290463862, -0.816496580927726, 0.408248290463863 ] ] ) return a def a123_eigen_right ( ): #*****************************************************************************80 # ## a123_eigen_right() returns the right eigenvectors of the A123 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Output: # # real A(3,3), the right eigenvector matrix. # import numpy as np a = np.array ( [ \ [ -0.231970687246286, -0.785830238742067, 0.408248290463864 ], \ [ -0.525322093301234, -0.086751339256628, -0.816496580927726 ], \ [ -0.818673499356181, 0.612327560228810, 0.408248290463863 ] ] ) return a def a123_eigenvalues ( ): #*****************************************************************************80 # ## a123_eigenvalues() returns the eigenvalues of the A123 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 March 2015 # # Author: # # John Burkardt # # Output: # # real LAM(3), the eigenvalues. # import numpy as np lam = np.array ( [ \ 16.116843969807043, \ -1.116843969807043, \ 0.0 ] ) return lam def a123_matrix ( ): #*****************************************************************************80 # ## a123_matrix() returns the A123 matrix. # # Example: # # 1 2 3 # 4 5 6 # 7 8 9 # # Properties: # # A is integral. # # A is not symmetric. # # A is singular. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 March 2015 # # Author: # # John Burkardt # # Output: # # real A(3,3), the matrix. # import numpy as np a = np.zeros ( ( 3, 3 ) ) k = 0 for i in range ( 0, 3 ): for j in range ( 0, 3 ): k = k + 1 a[i,j] = float ( k ) return a def a123_null_left ( ): #*****************************************************************************80 # ## a123_null_left() returns a left null vector for the A123 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 March 2015 # # Author: # # John Burkardt # # Output: # # real X(3), a left null vector. # import numpy as np x = np.array ( [ [ 1.0 ], [ -2.0 ], [ 1.0 ] ] ) return x def a123_null_right ( ): #*****************************************************************************80 # ## a123_null_right() returns a right null vector for the A123 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 March 2015 # # Author: # # John Burkardt # # Output: # # real X(3), a right null vector. # import numpy as np x = np.array ( [ [ 1.0 ], [ -2.0 ], [ 1.0 ] ] ) return x def a123_plu ( ): #*****************************************************************************80 # ## a123_plu() returns the PLU factors of the A123 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 May 2020 # # Author: # # John Burkardt # # Output: # # real P(3,3), L(3,3), U(3,3), the PLU factors. # import numpy as np p = np.array ( [ \ [ 0.0, 1.0, 0.0 ], \ [ 0.0, 0.0, 1.0 ], \ [ 1.0, 0.0, 0.0 ] ] ) l = np.array ( [ \ [ 1.000000000000000, 0.0, 0.0 ], \ [ 0.142857142857143, 1.000000000000000, 0.0 ], \ [ 0.571428571428571, 0.500000000000000, 1.000000000000000 ] ] ) u = np.array ( [ \ [ 7.000000000000000, 8.000000000000000, 9.000000000000000 ], \ [ 0.0, 0.857142857142857, 1.714285714285714 ], \ [ 0.0, 0.0, 0.000000000000000 ] ] ) return p, l, u def a123_rhs ( ): #*****************************************************************************80 # ## a123_rhs() returns the A123 right hand side. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 March 2015 # # Author: # # John Burkardt # # Output: # # real B(3), the right hand side vector. # import numpy as np b = np.array ( [ [ 10.0 ], [ 28.0 ], [ 46.0 ] ] ) return b def a123_solution ( ): #*****************************************************************************80 # ## a123_solution() returns the A123 solution vector. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 March 2015 # # Author: # # John Burkardt # # Output: # # real X(3), the solution. # import numpy as np x = np.array ( [ [ 3.0 ], [ 2.0 ], [ 1.0 ] ] ) return x def aegerter_condition ( n ): #*****************************************************************************80 # ## aegerter_condition() returns the L1 condition of the AEGERTER matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real COND, the L1 condition. # import numpy as np a = aegerter_matrix ( n ) a_norm = np.linalg.norm ( a, 1 ) b = aegerter_inverse ( n ) b_norm = np.linalg.norm ( b, 1 ) rcond = a_norm * b_norm return rcond def aegerter_determinant ( n ): #*****************************************************************************80 # ## aegerter_determinant() returns the determinant of the AEGERTER matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 October 2007 # # Author: # # John Burkardt # # Input: # # integer N, the number of rows and columns of A. # # Output: # # real DETERM, the determinant. # determ = ( n - ( ( n - 1 ) * n * ( 2 * n - 1 ) ) / 6 ) return determ def aegerter_eigenvalues ( n ): #*****************************************************************************80 # ## aegerter_eigenvalues() returns the eigenvalues of the AEGERTER matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the number of rows and columns of A. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n ) determ = n - ( ( n - 1 ) * n * ( 2 * n - 1 ) ) / 6 lam[0] = 0.5 * ( n + 1 - np.sqrt ( ( n + 1 ) ** 2 - 4.0 * determ ) ) for i in range ( 1, n - 1 ): lam[i] = 1.0 lam[n-1] = 0.5 * ( n + 1 + np.sqrt ( ( n + 1 ) ** 2 - 4.0 * determ ) ) return lam def aegerter_inverse ( n ): #*****************************************************************************80 # ## aegerter_inverse() returns the inverse of the AEGERTER matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 December 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of rows and columns of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) v = np.zeros ( n ) for i in range ( 0, n - 1 ): v[i] = i + 1 for j in range ( 0, n - 1 ): a[j,j] = 1.0 for i in range ( 0, n - 1 ): a[i,j] = a[i,j] - v[i] * v[j] / ( n * n ) for i in range ( 0, n - 1 ): a[i,n-1] = v[i] / ( n * n ) for j in range ( 0, n - 1 ): a[n-1,j] = v[j] / ( n * n ) a[n-1,n-1] = - 1.0 / ( n * n ) return a def aegerter_matrix ( n ): #*****************************************************************************80 # ## aegerter_matrix() returns the AEGERTER matrix. # # Formula: # # if ( I == N ) # A(I,J) = J # else if ( J == N ) # A(I,J) = I # else if ( I == J ) # A(I,J) = 1 # else # A(I,J) = 0 # # Example: # # N = 5 # # 1 0 0 0 1 # 0 1 0 0 2 # 0 0 1 0 3 # 0 0 0 1 4 # 1 2 3 4 5 # # Square Properties: # # A is integral: int ( A ) = A. # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is border-banded. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the number of rows and columns of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == n - 1 ): a[i,j] = j + 1 elif ( j == n - 1 ): a[i,j] = i + 1 elif ( i == j ): a[i,j] = 1.0 else: a[i,j] = 0.0 return a def antisummation_condition ( n ): #*****************************************************************************80 # ## antisummation_condition() returns the L1 condition of the ANTISUMMATION matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 October 2022 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real COND, the L1 condition. # norm_A = n norm_inv = 1.0 twoi = 1.0 for i in range ( 0, n - 1 ): norm_inv = norm_inv + twoi twoi = twoi * 2.0 cond = norm_A * norm_inv; return cond def antisummation_determinant ( n ): #*****************************************************************************80 # ## antisummation_determinant() returns the determinant of the ANTISUMMATION matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 October 2022 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 return value def antisummation_inverse ( n ): #*****************************************************************************80 # ## antisummation_inverse() returns the inverse of the ANTISUMMATION matrix. # # Example: # # N = 5 # # 1 0 0 0 0 # 1 1 0 0 0 # 2 1 1 0 0 # 4 2 1 1 0 # 8 4 2 1 1 # # Properties: # # A is lower triangular. # # A is Toeplitz: constant along diagonals. # # A is nonsingular. # # det ( A ) = 1. # # A is the inverse of the antisummation matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 October 2022 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = 1.0 elif ( j < i ): a[i,j] = 2.0 ** ( i - j - 1 ) else: a[i,j] = 0.0 return a def antisummation_matrix ( m, n ): #*****************************************************************************80 # ## antisummation_matrix() returns the ANTISUMMATION matrix. # # Example: # # M = 5, N = 5 # # 1 0 0 0 0 # -1 1 0 0 0 # -1 -1 1 0 0 # -1 -1 -1 1 0 # -1 -1 -1 -1 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is lower triangular. # # A is Toeplitz: constant along diagonals. # # A is nonsingular. # # det ( A ) = 1. # # A is the Cholesky factor of the MOLER3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 October 2022 # # Author: # # John Burkardt # # Reference: # # Nick Higham, # Seven Sins of Numerical Linear Algebra, # https://nhigham.com/2022/10/11/seven-sins-of-numerical-linear-algebra/ # # Input: # # integer M, N, the order of the matrix. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( [ m, n ] ) for j in range ( 0, n ): for i in range ( 0, m ): if ( j == i ): a[i,j] = 1.0 elif ( j <= i ): a[i,j] = -1.0 else: a[i,j] = 0.0 return a def antisymmetric_random_determinant ( n, key ): #*****************************************************************************80 # ## antisymmetric_random_determinant(): determinant of ANTISYMMETRIC_RANDOM matrix. # # Discussion: # # If N is odd, the determinant is 0. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # # Reference: # # Michael Wimmer, # Algorithm 923: Efficient Numerical Computation of the Pfaffian for # Dense and Banded Skew-Symmetric Matrices, # ACM Transactions on Mathematical Software, # Volume 38, Number 4, Article 30, August 2012. # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real DETERM, the determinant. # if ( ( n % 2 ) == 1 ): determ = 0.0 else: A = antisymmetric_random_matrix ( n, key ) determ = ( pfaffian_ltl ( A ) ) ** 2 return determ def antisymmetric_random_matrix ( n, key ): #*****************************************************************************80 # ## antisymmetric_random_matrix() returns the ANTISYMMETRIC_RANDOM matrix. # # Discussion: # # This is a random antisymmetric matrix. # # Example: # # N = 5 # # 0.0000 -0.1096 0.0813 0.9248 -0.0793 # 0.1096 0.0000 0.1830 0.1502 0.8244 # -0.0813 -0.1830 0.0000 0.0899 -0.2137 # -0.9248 -0.1502 -0.0899 0.0000 -0.4804 # 0.0793 -0.8244 0.2137 0.4804 0.0000 # # Properties: # # A is generally not symmetric: A' /= A. # # A is antisymmetric: A' = -A. # # Because A is antisymmetric, it is normal: A*A' = A'*A. # # Because A is normal, it is diagonalizable. # # Because A is diagonalizable, it is not defective: # it has a full set of eigenvectors. # # The diagonal of A is zero. # # All the eigenvalues of A are imaginary. # # If N is odd, then det ( A ) = 0. # # A is not diagonally dominant. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 29 June 2008 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real A(N,N), the matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) A = np.zeros ( [ n, n ] ) n2 = ( n * ( n - 1 ) ) // 2 t = rng.standard_normal ( size = n2 ) k = 0 for i in range ( 0, n ): for j in range ( i + 1, n ): A[i,j] = t[k] A[j,i] = - t[k] k = k + 1 return A def archimedes_matrix ( ): #*****************************************************************************80 # ## archimedes_matrix() returns the 7 by 8 ARCHIMEDES matrix. # # Formula: # # 6 -5 0 -6 0 0 0 0 # 0 20 -9 -20 0 0 0 0 # -13 0 42 -42 0 0 0 0 # 0 -7 0 0 12 -7 0 0 # 0 0 -9 0 0 20 -9 0 # 0 0 0 -11 0 0 30 -11 # -13 0 0 0 -13 0 0 42 # # Discussion: # # "The sun god had a herd of cattle, consisting of bulls and cows, # one part of which was white, a second black, a third spotted, and # a fourth brown. Among the bulls, the number of white ones was # one half plus one third the number of the black greater than # the brown; the number of the black, one quarter plus one fifth # the number of the spotted greater than the brown; the number of # the spotted, one sixth and one seventh the number of the white # greater than the brown. Among the cows, the number of white ones # was one third plus one quarter of the total black cattle; the number # of the black, one quarter plus one fifth the total of the spotted # cattle; the number of spotted, one fifth plus one sixth the total # of the brown cattle; the number of the brown, one sixth plus one # seventh the total of the white cattle. What was the composition # of the herd?" # # The 7 relations involving the 8 numbers W, X, Y, Z, w, x, y, z, # have the form: # # W = ( 5/ 6) * X + Z # X = ( 9/20) * Y + Z # Y = (13/42) * W + Z # w = ( 7/12) * ( X + x ) # x = ( 9/20) * ( Y + y ) # y = (11/30) * ( Z + z ) # z = (13/42) * ( W + w ) # # These equations may be multiplied through to an integral form # that is embodied in the above matrix. # # A more complicated second part of the problem imposes additional # constraints (W+X must be square, Y+Z must be a triangular number). # # Properties: # # A is integral: int ( A ) = A. # # A has full row rank. # # It is desired to know a solution X in positive integers of # # A * X = 0. # # The null space of A is spanned by multiples of the null vector: # # [ 10,366,482 ] # [ 7,460,514 ] # [ 7,358,060 ] # [ 4,149,387 ] # [ 7,206,360 ] # [ 4,893,246 ] # [ 3,515,820 ] # [ 5,439,213 ] # # and this is the smallest positive integer solution of the # equation A * X = 0. # # Thus, for the "simple" part of Archimedes's problem, the total number # of cattle of the Sun is 50,389,082. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 September 2007 # # Author: # # John Burkardt # # Reference: # # Eric Weisstein, # CRC Concise Encyclopedia of Mathematics, # CRC Press, 1998. # # Output: # # integer A(7,8), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 6, -5, 0, -6, 0, 0, 0, 0 ], \ [ 0, 20, -9, -20, 0, 0, 0, 0 ], \ [ -13, 0, 42, -42, 0, 0, 0, 0 ], \ [ 0, -7, 0, 0, 12, -7, 0, 0 ], \ [ 0, 0, -9, 0, 0, 20, -9, 0 ], \ [ 0, 0, 0, -11, 0, 0, 30, -11 ], \ [ -13, 0, 0, 0, -13, 0, 0, 42 ] ] ) return a def archimedes_null_right ( ): #*****************************************************************************80 # ## archimedes_null_right() returns a right null vector for the ARCHIMEDES matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Output: # # real X(8), a right null vector. # import numpy as np x = np.array ( [ \ [ 10366482.0 ], \ [ 7460514.0 ], \ [ 7358060.0 ], \ [ 4149387.0 ], \ [ 7206360.0 ], \ [ 4893246.0 ], \ [ 3515820.0 ], \ [ 5439213.0 ] ] ) return x def bab_condition ( n, alpha, beta ): #*****************************************************************************80 # ## bab_condition() returns the L1 condition of the BAB matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real ALPHA, BETA, the parameters. # # Output: # # real COND, the L1 condition number. # import numpy as np a = bab_matrix ( n, alpha, beta ) a_norm = np.linalg.norm ( a, 1 ) b = bab_inverse ( n, alpha, beta ) b_norm = np.linalg.norm ( b, 1 ) cond = a_norm * b_norm return cond def bab_determinant ( n, alpha, beta ): #*****************************************************************************80 # ## bab_determinant() returns the determinant of the BAB matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real ALPHA, BETA, parameters that define the matrix. # # Output: # # real DETERM, the determinant. # determ_nm1 = alpha if ( n == 1 ): determ = determ_nm1 return determ determ_nm2 = determ_nm1 determ_nm1 = alpha * alpha - beta * beta if ( n == 2 ): determ = determ_nm1 return determ for i in range ( n - 2, 0, -1 ): determ = alpha * determ_nm1 - beta * beta * determ_nm2 determ_nm2 = determ_nm1 determ_nm1 = determ return determ def bab_eigen_right ( n, alpha, beta ): #*****************************************************************************80 # ## bab_eigen_right() returns the right eigenvectors of the BAB matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real ALPHA, BETA, the parameters. # # Output: # # real A(N,N), the right eigenvector matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): angle = float ( ( i + 1 ) * ( j + 1 ) ) * np.pi / float ( n + 1 ) a[i,j] = np.sqrt ( 2.0 / float ( n + 1 ) ) * np.sin ( angle ) return a def bab_eigenvalues ( n, alpha, beta ): #*****************************************************************************80 # ## bab_eigenvalues() returns the eigenvalues of the BAB matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real ALPHA, BETA, the parameters. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n ) for i in range ( 0, n ): angle = float ( i + 1 ) * np.pi / float ( n + 1 ) lam[i] = alpha + 2.0 * beta * np.cos ( angle ) return lam def bab_inverse ( n, alpha, beta ): #*****************************************************************************80 # ## bab_inverse() returns the inverse of the BAB matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real ALPHA, BETA, the parameters. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) if ( beta == 0.0 ): if ( alpha == 0.0 ): print ( '' ) print ( 'bab_inverse(): Fatal error!' ) print ( ' ALPHA = BETA = 0.' ) raise Exception ( 'bab_inverse(): Fatal error!' ) for i in range ( 0, n ): a[i,i] = 1.0 / alpha else: x = 0.5 * alpha / beta u = cheby_u_polynomial ( n, x ) for i in range ( 1, n + 1 ): for j in range ( 1, i + 1 ): a[i-1,j-1] = r8_mop ( i + j ) * u[j-1] * u[n-i] / u[n] / beta for j in range ( i + 1, n + 1 ): a[i-1,j-1] = r8_mop ( i + j ) * u[i-1] * u[n-j] / u[n] / beta return a def bab_matrix ( n, alpha, beta ): #*****************************************************************************80 # ## bab_matrix() returns the BAB matrix. # # Example: # # N = 5 # ALPHA = 5, BETA = 2 # # 5 2 . . . # 2 5 2 . . # . 2 5 2 . # . . 2 5 2 # . . . 2 5 # # Properties: # # A is banded, with bandwidth 3. # # A is tridiagonal. # # Because A is tridiagonal, it has property A (bipartite). # # A is Toeplitz: constant along diagonals. # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 November 2007 # # Author: # # John Burkardt # # Reference: # # CM da Fonseca, J Petronilho, # Explicit Inverses of Some Tridiagonal Matrices, # Linear Algebra and Its Applications, # Volume 325, 2001, pages 7-21. # # Input: # # integer N, the order of the matrix. # # real ALPHA, BETA, the parameters. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): a[i,i] = alpha for i in range ( 0, n - 1 ): a[i,i+1] = beta a[i+1,i] = beta return a def bauer_condition ( ): #*****************************************************************************80 # ## bauer_condition() returns the L1 condition of the BAUER matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 March 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the condition. # a_norm = 307.0 b_norm = 27781.0 value = a_norm * b_norm return value def bauer_determinant ( ): #*****************************************************************************80 # ## bauer_determinant() returns the determinant of the BAUER matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 March 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = 1.0 return value def bauer_inverse ( ): #*****************************************************************************80 # ## bauer_inverse() returns the inverse of the BAUER matrix. # # Example: # # 1 0 -7 -40 131 -84 # 0 1 7 35 -112 70 # -2 2 29 155 -502 319 # 15 -12 -192 -1034 3354 -2130 # 43 -42 -600 -3211 10406 -6595 # -56 52 764 4096 -13276 8421 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 March 2015 # # Author: # # John Burkardt # # Output: # # real A(6,6), the matrix. # import numpy as np a = np.array ( [ \ [ 1.0, 0.0, -7.0, -40.0, 131.0, -84.0 ], \ [ 0.0, 1.0, 7.0, 35.0, -112.0, 70.0 ], \ [ -2.0, 2.0, 29.0, 155.0, -502.0, 319.0 ], \ [ 15.0, -12.0, -192.0, -1034.0, 3354.0, -2130.0 ], \ [ 43.0, -42.0, -600.0, -3211.0, 10406.0, -6595.0 ], \ [ -56.0, 52.0, 764.0, 4096.0, -13276.0, 8421.0 ] ] ) return a def bauer_matrix ( ): #*****************************************************************************80 # ## bauer_matrix() returns the BAUER matrix. # # Example: # # -74 80 18 -11 -4 -8 # 14 -69 21 28 0 7 # 66 -72 -5 7 1 4 # -12 66 -30 -23 3 -3 # 3 8 -7 -4 1 0 # 4 -12 4 4 0 1 # # Properties: # # The matrix is integral. # # The inverse matrix is integral. # # The matrix is ill-conditioned. # # The determinant is 1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 March 2015 # # Author: # # John Burkardt # # Reference: # # Virginia Klema, Alan Laub, # The Singular Value Decomposition: Its Computation and Some Applications, # IEEE Transactions on Automatic Control, # Volume 25, Number 2, April 1980. # # Output: # # real A(6,6), the matrix. # import numpy as np a = np.array ( [ \ [ -74.0, 80.0, 18.0, -11.0, -4.0, -8.0 ], \ [ 14.0, -69.0, 21.0, 28.0, 0.0, 7.0 ], \ [ 66.0, -72.0, -5.0, 7.0, 1.0, 4.0 ], \ [ -12.0, 66.0, -30.0, -23.0, 3.0, -3.0 ], \ [ 3.0, 8.0, -7.0, -4.0, 1.0, 0.0 ], \ [ 4.0, -12.0, 4.0, 4.0, 0.0, 1.0 ] ] ) return a def bernstein_condition ( n ): #*****************************************************************************80 # ## bernstein_condition() returns the L1 condition of the BERNSTEIN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 November 2021 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real C, the condition number. # import numpy as np A = bernstein_matrix ( n ) anorm = np.linalg.norm ( A, 1 ) B = bernstein_inverse ( n ) bnorm = np.linalg.norm ( B, 1 ) c = anorm * bnorm return c def bernstein_determinant ( n ): #*****************************************************************************80 # ## bernstein_determinant() returns the determinant of the BERNSTEIN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # from scipy.special import comb value = 1.0 for i in range ( 0, n ): value = value * comb ( n - 1, i ) return value def bernstein_inverse ( n ): #*****************************************************************************80 # ## bernstein_inverse() returns the inverse of the BERNSTEIN matrix. # # Discussion: # # The Monomial-to-Bernstein matrix of degree N is an N+1xN+1 matrix A which can # be used to transform the N+1 coefficients of a polynomial of degree N # from a vector P of coefficients of the power basis (1,x,x^2,...,x^n) # to a vector B of Bernstein basis polynomial coefficients ((1-x)^n,...,x^n). # # If we are using N=4-th degree polynomials, the matrix has the form: # # 1 1 1 1 1 # 0 1/4 1/2 3/4 1 # A = 0 0 1/6 1/2 1 # 0 0 0 1/4 1 # 0 0 0 0 1 # # and a polynomial # p(x) = 3x - 6x^2 + 3x^3 # whose power coefficient vector is # P = ( 0, 3, -6, 3, 0 ) # will have the Bernstein basis coefficients # B = A * P = ( 0, 3/4, 1/2, 0, 0 ). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 February 2024 # # Author: # # John Burkardt # # Input: # # integer N, the degree of the polynomials. # # Output: # # real A[0:N,0:N], the Monomial-to-Bernstein matrix. # from scipy.special import comb import numpy as np a = np.zeros ( [ n + 1, n + 1 ] ) for j in range ( 0, n + 1 ): for i in range ( 0, j + 1 ): a[i,j] = ( -1 ) ** (j - i ) * comb ( n - i, j - i ) * comb ( n, i ) return a def bernstein_matrix ( n ): #*****************************************************************************80 # ## bernstein_matrix() returns the BERNSTEIN matrix. # # Discussion: # # The Bernstein matrix of order N is an NxN matrix A which can be used to # transform a vector of power basis coefficients C representing a polynomial # P(X) to a corresponding Bernstein basis coefficient vector B: # # B = A * C # # The N power basis vectors are ordered as (1,X,X^2,...X^(N-1)) and the N # Bernstein basis vectors as ((1-X)^(N-1), X*(1-X)^(N-2),...,X^(N-1)). # # Example: # # N = 4 # # 1 -4 6 -4 1 # 0 4 -12 12 -4 # 0 0 6 -12 6 # 0 0 0 4 -4 # 0 0 0 0 1 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 February 2024 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N+1,N+1), the matrix. # import numpy as np from scipy.special import comb a = np.zeros ( ( n + 1, n + 1 ) ) for i in range ( 0, n + 1 ): for j in range ( 0, i + 1 ): a[i,j] = ( - 1 ) ** ( i - j ) * comb ( n - j, i - j ) * comb ( n, j ) return a def bernstein_poly_01 ( n, x ): #*****************************************************************************80 # ## bernstein_poly_01() evaluates the Bernstein polynomials defined on [0,1]. # # Discussion: # # The Bernstein polynomials are assumed to be based on [0,1]. # # Formula: # # B(N,I)(X) = [N!/(I!*(N-I)!)] * (1-X)^(N-I) * X^I # # First values: # # B(0,0)(X) = 1 # # B(1,0)(X) = 1-X # B(1,1)(X) = X # # B(2,0)(X) = (1-X)^2 # B(2,1)(X) = 2 * (1-X) * X # B(2,2)(X) = X^2 # # B(3,0)(X) = (1-X)^3 # B(3,1)(X) = 3 * (1-X)^2 * X # B(3,2)(X) = 3 * (1-X) * X^2 # B(3,3)(X) = X^3 # # B(4,0)(X) = (1-X)^4 # B(4,1)(X) = 4 * (1-X)^3 * X # B(4,2)(X) = 6 * (1-X)^2 * X^2 # B(4,3)(X) = 4 * (1-X) * X^3 # B(4,4)(X) = X^4 # # Special values: # # B(N,I)(X) has a unique maximum value at X = I/N. # # B(N,I)(X) has an I-fold zero at 0 and and N-I fold zero at 1. # # B(N,I)(1/2) = C(N,K) / 2^N # # For a fixed X and N, the polynomials add up to 1: # # Sum ( 0 <= I <= N ) B(N,I)(X) = 1 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 December 2015 # # Author: # # John Burkardt # # Input: # # integer N, the degree of the Bernstein polynomials to be # used. For any N, there is a set of N+1 Bernstein polynomials, # each of degree N, which form a basis for polynomials on [0,1]. # # real X, the evaluation point. # # Output: # # real B[0:N], the values of the N+1 Bernstein polynomials at X. # import numpy as np b = np.zeros ( n + 1 ) if ( n == 0 ): b[0] = 1.0 elif ( 0 < n ): b[0] = 1.0 - x b[1] = x for i in range ( 1, n ): b[i+1] = x * b[i] for j in range ( i - 1, -1, -1 ): b[j+1] = x * b[j] + ( 1.0 - x ) * b[j+1] b[0] = ( 1.0 - x ) * b[0] return b def bernstein_vandermonde ( n ): #*****************************************************************************80 # ## bernstein_vandermonde() returns the Bernstein Vandermonde matrix. # # Discussion: # # The Bernstein Vandermonde matrix of order N is constructed by # evaluating the N Bernstein polynomials of degree N-1 at N equally # spaced points between 0 and 1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 December 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the Bernstein Vandermonde matrix. # import numpy as np v = np.zeros ( [ n, n ] ) if ( n == 1 ): v[0,0] = 1.0 return v for i in range ( 0, n ): x = float ( i ) / float ( n - 1 ) b = bernstein_poly_01 ( n - 1, x ) for j in range ( 0, n ): v[i,j] = b[j] return v def bimarkov_random_matrix ( n, key ): #*****************************************************************************80 # ## bimarkov_random_matrix() returns the BIMARKOV_RANDOM matrix. # # Discussion: # # This is a random biMarkov or doubly stochastic matrix. # # Example: # # N = 5 # # 1/5 1/5 1/5 1/5 1/5 # 1/2 1/2 0 0 0 # 1/6 1/6 2/3 0 0 # 1/12 1/12 1/12 3/4 0 # 1/20 1/20 1/20 1/20 4/5 # # Properties: # # A is generally not symmetric: A' /= A. # # 0 <= A(I,J) <= 1.0 for every I and J. # # A has constant row sum 1. # # A has constant column sum 1. # # All the eigenvalues of A have modulus 1. # # 1 is always an eigenvalue of A, with eigenvector (1,1,...,1). # # The eigenvalue 1 lies on the boundary of all the Gershgorin # row or column sum disks. # # Every doubly stochastic matrix is a combination # A = w1 * P1 + w2 * P2 + ... + wk * Pk # of permutation matrices, with positive weights w that sum to 1. # (Birkhoff's theorem, see Horn and Johnson.) # # A is a Markov matrix. # # A is a transition matrix. # # A is not diagonally dominant. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 May 2020 # # Author: # # John Burkardt # # Reference: # # Roger Horn, Charles Johnson, # Matrix Analysis, # Cambridge, 1985. # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real A(N,N), the matrix. # # # Get a random orthogonal matrix. # a = orthogonal_random_matrix ( n, key ) # # Square each entry. # a = a**2 return a def bis_matrix ( alpha, beta, m, n ): #*****************************************************************************80 # ## bis_matrix() returns the BIS matrix(). # # Discussion: # # BIS is a bidiagonal scalar matrix. # # Formula: # # if ( I = J ) # A(I,J) = ALPHA # else if ( J = I+1 ) # A(I,J) = BETA # else # A(I,J) = 0 # # Example: # # ALPHA = 7, BETA = 2, M = 5, N = 4 # # 7 2 0 0 # 0 7 2 0 # 0 0 7 2 # 0 0 0 7 # 0 0 0 0 # # Rectangular Properties: # # A is bidiagonal. # # Because A is bidiagonal, it has property A (bipartite). # # A is upper triangular. # # A is banded with bandwidth 2. # # A is Toeplitz: constant along diagonals. # # Square Properties: # # A is generally not symmetric: A' /= A. # # A is nonsingular if and only if ALPHA is nonzero. # # det ( A ) = ALPHA^N. # # LAMBDA(1:N) = ALPHA. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 December 2014 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, the scalars which define the # diagonal and first superdiagonal of the matrix. # # integer M, N, the number of rows and columns of A. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( [ m, n ] ) for i in range ( 0, m ): for j in range ( 0, n ): if ( j == i ): a[i,j] = alpha elif ( j == i + 1 ): a[i,j] = beta else: a[i,j] = 0.0 return a def bis_condition ( alpha, beta, n ): #*****************************************************************************80 # ## bis_condition() computes the L1 condition of the BIS matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 January 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, the scalars which define the # diagonal and first superdiagonal of the matrix. # # integer N, the order of the matrix. # # Output: # # real VALUE, the L1 condition. # a_norm = abs ( alpha ) + abs ( beta ) ba = abs ( beta / alpha ) b_norm = ( ba ** n - 1.0 ) / ( ba - 1.0 ) / abs ( alpha ) value = a_norm * b_norm return value def bis_determinant ( alpha, beta, n ): #*****************************************************************************80 # ## bis_determinant() returns the determinant of the BIS matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 December 2014 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, the scalars which define the # diagonal and first superdiagonal of the matrix. # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # determ = alpha ** n return determ def bis_eigenvalues ( alpha, beta, n ): #*****************************************************************************80 # ## bis_eigenvalues() returns the eigenvalues of the BIS matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2016 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, the scalars which define the # diagonal and first superdiagonal of the matrix. # # integer N, the order of the matrix. # # Output: # # real LAM(N), the eigenvalues of the matrix. # import numpy as np lam = np.ones ( n ) lam = lam * alpha return lam def bis_inverse ( alpha, beta, n ): #*****************************************************************************80 # ## bis_inverse() returns the inverse of a bidiagonal scalar matrix. # # Formula: # # if ( I <= J ) # A(I,J) = (-BETA)^(J-I) / ALPHA^(J+1-I) # else # A(I,J) = 0 # # Example: # # ALPHA = 7.0, BETA = 2.0, N = 4 # # 0.1429 -0.0408 0.0117 -0.0033 # 0 0.1429 -0.0408 0.0117 # 0 0 0.1429 -0.0408 # 0 0 0 0.1429 # # Properties: # # A is generally not symmetric: A' /= A. # # A is upper triangular # # A is Toeplitz: constant along diagonals. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # det ( A ) = (1/ALPHA)^N. # # LAMBDA(1:N) = 1 / ALPHA. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, the scalars which define the # diagonal and first superdiagonal of the matrix. # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) if ( alpha == 0.0 ): print ( '' ) print ( 'bis_inverse(): Fatal error!' ) print ( ' The input parameter ALPHA was 0.' ) raise Exception ( 'bis_inverse(): Fatal error!' ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i <= j ): a[i,j] = ( - beta ) ** ( j - i ) / alpha ** ( j - i + 1 ) return a def biw_matrix ( n ): #*****************************************************************************80 # ## biw_matrix() returns the BIW matrix. # # Discussion: # # BIW is a bidiagonal matrix of Wilkinson. Originally, this matrix # was considered for N = 100. # # Formula: # # if ( I == J ) # A(I,J) = 0.5 + I / ( 10 * N ) # else if ( J == I+1 ) # A(I,J) = -1.0 # else # A(I,J) = 0 # # Example: # # N = 5 # # 0.52 -1.00 0.00 0.00 0.00 # 0.00 0.54 -1.00 0.00 0.00 # 0.00 0.00 0.56 -1.00 0.00 # 0.00 0.00 0.00 0.58 -1.00 # 0.00 0.00 0.00 0.00 0.60 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): a[i,i] = 0.5 + float ( i + 1 ) / float ( 10 * n ) for i in range ( 0, n - 1 ): a[i,i+1] = - 1.0 return a def biw_condition ( n ): #*****************************************************************************80 # ## biw_condition() computes the L1 condition of the BIW matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the L1 condition. # if ( n == 1 ): a_norm = 0.6 else: a_norm = 1.6 b_norm = 0.0 j = n for i in range ( n, 0, -1 ): aii = 0.5 + float ( i ) / float ( 10 * n ) if ( i == j ): bij = 1.0 / aii elif ( i < j ): bij = bij / aii b_norm = b_norm + abs ( bij ) value = a_norm * b_norm return value def biw_determinant ( n ): #*****************************************************************************80 # ## biw_determinant() returns the determinant of the BIW matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 for i in range ( 0, n ): value = value * ( 0.5 + float ( i + 1 ) / float ( 10 * n ) ) return value def biw_inverse ( n ): #*****************************************************************************80 # ## biw_inverse() returns the inverse of the BIW matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np b = np.zeros ( [ n, n ] ) for j in range ( n - 1, -1, -1 ): for i in range ( n - 1, -1, -1 ): aii = 0.5 + float ( i + 1 ) / float ( 10 * n ) aiip1 = - 1.0 if ( i == j ): b[i,j] = 1.0 / aii elif ( i < j ): t = aiip1 * b[i+1,j] b[i,j] = - t / aii return b def bodewig_condition ( ): #*****************************************************************************80 # ## bodewig_condition() returns the L1 condition of the BODEWIG matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # # Output: # # real VALUE, the L1 condition number. # value = 10.436619718309862 return value def bodewig_determinant ( ): #*****************************************************************************80 # ## bodewig_determinant() returns the determinant of the BODEWIG matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 December 2014 # # Author: # # John Burkardt # # Output: # # real DETERM, the determinant. # determ = 568.0 return determ def bodewig_eigen_right ( ): #*****************************************************************************80 # ## bodewig_eigen_right() returns the right eigenvectors of the BODEWIG matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 0.263462395147524, 0.560144509774526, \ 0.378702689441644, -0.688047939843040 ], \ [ 0.659040718046439, 0.211632763260098, \ 0.362419048574935, 0.624122855455373 ], \ [ -0.199633529128396, 0.776708263894565, \ -0.537935161097828, 0.259800864702728 ], \ [ -0.675573350827063, 0.195381612446620, \ 0.660198809976478, 0.263750269148100 ] ] ) return a def bodewig_eigenvalues ( ): #*****************************************************************************80 # ## bodewig_eigenvalues() returns the eigenvalues of the BODEWIG matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Output: # # real LAMBDA(4,1), the eigenvalues. # import numpy as np lam = np.array ( [ \ -8.028578352396531, \ 7.932904717870018, \ 5.668864372830019, \ -1.573190738303506 ] ) return lam def bodewig_inverse ( ): #*****************************************************************************80 # ## bodewig_inverse() returns the inverse of the BODEWIG matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ -139.0, 165.0, 79.0, 111.0 ], \ [ 165.0, -155.0, -57.0, -1.0 ], \ [ 79.0, -57.0, 45.0, -59.0 ], \ [ 111.0, -1.0, -59.0, -11.0 ] ] ) for j in range ( 0, 4 ): for i in range ( 0, 4 ): a[i,j] = a[i,j] / 568.0 return a def bodewig_lu ( ): #*****************************************************************************80 # ## bodewig_lu() returns the LU factors of the BODEWIG matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 November 2021 # # Author: # # John Burkardt # # Output: # # real L(4,4), U(4,4), the factors. # import numpy as np l = np.array ( [ \ [ 1.00, 0.00, 0.00, 0.00 ], \ [ 0.50, 1.00, 0.00, 0.00 ], \ [ 1.50, 0.142857142857143, 1.00, 0.00 ], \ [ 2.00, -0.857142857142857, -5.363636363636364, 1.00 ] ] ) u = np.array ( [ \ [ 2.00, 1.00, 3.00, 4.00 ], \ [ 0.00, -3.50, -0.50, 3.00 ], \ [ 0.00, 0.00, 1.571428571428571, -8.428571428571429 ], \ [ 0.00, 0.00, 0.00, -51.6363636363636478 ] ] ) return l, u def bodewig_matrix ( ): #*****************************************************************************80 # ## bodewig_matrix() returns the BODEWIG matrix. # # Example: # # 2 1 3 4 # 1 -3 1 5 # 3 1 6 -2 # 4 5 -2 -1 # # Properties: # # A is symmetric: A' = A. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 May 2020 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # Test Matrix A27, # A Handbook of Numerical Matrix Inversion and Solution of Linear Equations, # John Wiley, 1968. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.array ( [ \ [ 2.0, 1.0, 3.0, 4.0 ], \ [ 1.0, -3.0, 1.0, 5.0 ], \ [ 3.0, 1.0, 6.0, -2.0 ], \ [ 4.0, 5.0, -2.0, -1.0 ] ] ) return a def bodewig_plu ( ): #*****************************************************************************80 # ## bodewig_plu() returns the PLU factors of the BODEWIG matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # # Output: # # real P(4,4), L(4,4), U(4,4), the factors. # import numpy as np # # Note that the matrix entries are listed by row. # p = np.array ( [ \ [ 0.0, 0.0, 0.0, 1.0 ], \ [ 0.0, 1.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 1.0, 0.0 ], \ [ 1.0, 0.0, 0.0, 0.0 ] ] ) l = np.array ( [ \ [ 1.00, 0.00, 0.00, 0.00 ], \ [ 0.25, 1.00, 0.00, 0.00 ], \ [ 0.75, 0.647058823529412, 1.00, 0.00 ], \ [ 0.50, 0.352941176470588, 0.531531531531532, 1.00 ] ] ) u = np.array ( [ \ [ 4.00, 5.00, -2.00, -1.00 ], \ [ 0.00, -4.25, 1.50, 5.25 ], \ [ 0.00, 0.00, 6.529411764705882, -4.647058823529412 ], \ [ 0.00, 0.00, 0.00, 5.117117117117118 ] ] ) return p, l, u def bodewig_rhs ( ): #*****************************************************************************80 # ## bodewig_rhs() returns the BODEWIG right hand side. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 March 2015 # # Author: # # John Burkardt # # Output: # # real B(4,1), the right hand side vector. # import numpy as np b = np.array ( [ [ 29.0 ], [ 18.0 ], [ 15.0 ], [ 4.0 ] ] ) return b def bodewig_solution ( ): #*****************************************************************************80 # ## bodewig_solution() returns the bodewig_solution # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 March 2015 # # Author: # # John Burkardt # # Output: # # real X(4,1), the solution. # import numpy as np x = np.array ( [ [ 1.0 ], [ 2.0 ], [ 3.0 ], [ 4.0 ] ] ) return x def boothroyd_matrix ( n ): #*****************************************************************************80 # ## boothroyd_matrix() returns the BOOTHROYD matrix. # # Formula: # # A(I,J) = C(N+I-1,I-1) * C(N-1,N-J) * N / ( I + J - 1 ) # # Example: # # N = 5 # # 5 10 10 5 1 # 15 40 45 24 5 # 35 105 126 70 15 # 70 224 280 160 35 # 126 420 540 315 70 # # Properties: # # A is not symmetric. # # A is positive definite. # # det ( A ) = 1. # # The inverse matrix has the same entries, but with alternating sign. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 September 2007 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # from scipy.special import comb import numpy as np a = np.zeros ( [ n, n ] ) for j in range ( 0, n ): for i in range ( 0, n ): a[i,j] = comb ( n + i, i ) * comb ( n - 1, n - j - 1 ) * float ( n ) \ / float ( i + j + 1 ) return a def boothroyd_condition ( n ): #*****************************************************************************80 # ## boothroyd_condition() computes the L1 condition of the BOOTHROYD matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the L1 condition. # from scipy.special import comb a_norm = 0.0 for j in range ( 0, n ): s = 0.0 for i in range ( 0, n ): s = s + comb ( n + i, i ) * comb ( n - 1, n - j - 1 ) * float ( n ) \ / float ( i + j + 1 ) a_norm = max ( a_norm, s ) b_norm = a_norm value = a_norm * b_norm return value def boothroyd_determinant ( n ): #*****************************************************************************80 # ## boothroyd_determinant() returns the determinant of the BOOTHROYD matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 return value def boothroyd_inverse ( n ): #*****************************************************************************80 # ## boothroyd_inverse() returns the inverse of the BOOTHROYD matrix. # # Example: # # N = 5 # # 5 -10 10 -5 1 # -15 40 -45 24 -5 # 35 -105 126 -70 15 # -70 224 -280 160 -35 # 126 -420 540 -315 70 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # from scipy.special import comb import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = r8_mop ( i + j ) * comb ( n + i, i ) \ * comb ( n-1, n-j-1 ) * float ( n ) / float ( i + j + 1 ) return a def borderband_matrix ( n ): #*****************************************************************************80 # ## borderband_matrix() returns the BORDERBAND matrix. # # Formula: # # If ( I = J ) # A(I,I) = 1 # else if ( I = N ) # A(N,J) = 2^(1-J) # else if ( J = N ) # A(I,N) = 2^(1-I) # else # A(I,J) = 0 # # Example: # # N = 5 # # 1 0 0 0 1 # 0 1 0 0 1/2 # 0 0 1 0 1/4 # 0 0 0 1 1/8 # 1 1/2 1/4 1/8 1 # # Properties: # # A is symmetric: A' = A. # # A is border-banded. # # A has N-2 eigenvalues of 1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 29 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = 1.0 elif ( j == n - 1 ): a[i,j] = 1.0 / ( 2 ** i ) elif ( i == n - 1 ): a[i,j] = 1.0 / ( 2 ** j ) else: a[i,j] = 0.0 return a def borderband_determinant ( n ): #*****************************************************************************80 # ## borderband_determinant() returns the determinant of the BORDERBAND matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 29 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # determ = 0.0 for i in range ( 1, n ): determ = determ - 2.0 ** ( 2 - 2 * i ) determ = determ + 1.0 return determ def borderband_inverse ( n ): #*****************************************************************************80 # ## borderband_inverse() returns the inverse of the BORDERBAND matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the inverse matrix. # import numpy as np 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 borderband_lu ( n ): #*****************************************************************************80 # ## borderband_lu() returns the LU factors of the BORDERBAND matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 October 2021 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real L(N,N), U(N,N), the factors. # import numpy as np l = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, n ): if ( i == j ): l[i,j] = 1.0 elif ( i == n - 1 ): l[i,j] = 1.0 / 2.0 ** j u = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, n ): if ( i == n - 1 and j == n - 1 ): u[i,j] = 0.0 for k in range ( 2, n ): u[i,j] = u[i,j] - 2.0 ** ( 2 - 2 * k ) elif ( i == j ): u[i,j] = 1.0 elif ( j == n - 1 ): u[i,j] = 1.0 / 2.0 ** i return l, u def borderband_plu ( n ): #*****************************************************************************80 # ## borderband_plu() returns the PLU factors of the BORDERBAND matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 June 2011 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real P(N,N), L(N,N), U(N,N), the PLU factors. # import numpy as np p = np.zeros ( ( n, n ) ) for i in range ( 0, n ): p[i,i] = 1.0 l = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, n ): if ( i == j ): l[i,j] = 1.0 elif ( i == n - 1 ): l[i,j] = 1.0 / 2.0 ** j u = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, n ): if ( i == n - 1 and j == n - 1 ): u[i,j] = 0.0 for k in range ( 2, n ): u[i,j] = u[i,j] - 2.0 ** ( 2 - 2 * k ) elif ( i == j ): u[i,j] = 1.0 elif ( j == n - 1 ): u[i,j] = 1.0 / 2.0 ** i return p, l, u def bvec_next_grlex ( n, bvec ): #*****************************************************************************80 # ## bvec_next() generates the next binary vector in GRLEX order. # # Discussion: # # N = 3 # # Input Output # ----- ------ # 0 0 0 => 0 0 1 # 0 0 1 => 0 1 0 # 0 1 0 => 1 0 0 # 1 0 0 => 0 1 1 # 0 1 1 => 1 0 1 # 1 0 1 => 1 1 0 # 1 1 0 => 1 1 1 # 1 1 1 => 0 0 0 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the dimension of the vectors. # # integer BVEC[N], a vector. # # Output: # # integer BVEC[N], the successor vector. # # # Initialize locations of 0 and 1. # if ( bvec[0] == 0 ): z = 0 o = -1 else: z = -1 o = 0 # # Moving from right to left, search for a "1", preceded by a "0". # for i in range ( n - 1, 0, -1 ): if ( bvec[i] == 1 ): o = i if ( bvec[i-1] == 0 ): z = i - 1 break # # BVEC = 0 # if ( o == -1 ): bvec[n-1] = 1 # # 01 never occurs. So for sure, B(1) = 1. # elif ( z == -1 ): s = 0 for i in range ( 0, n ): s = s + bvec[i] if ( s == n ): for i in range ( 0, n ): bvec[i] = 0 else: for i in range ( 0, n - s - 1 ): bvec[i] = 0 for i in range ( n - s - 1, n ): bvec[i] = 1 type ( n - s - 1 ) # # Found the rightmost "01" string. # Replace it by "10". # Shift following 1's to the right. # else: bvec[z] = 1 bvec[o] = 0 s = 0 for i in range ( o + 1, n ): s = s + bvec[i] for i in range ( o + 1, n - s ): bvec[i] = 0 for i in range ( n - s, n ): bvec[i] = 1 return bvec def bvec_next_grlex_test ( ): #*****************************************************************************80 # ## bvec_next_grlex_test() tests bvec_next_grlex(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 March 2015 # # Author: # # John Burkardt # import numpy as np n = 4 print ( '' ) print ( 'bvec_next_grlex_test():' ) print ( ' bvec_next_grlex() computes binary vectors in GRLEX order.' ) print ( '' ) b = np.zeros ( n, dtype = np.int32 ) for i in range ( 0, 17 ): print ( ' %2d: ' % ( i ), end = '' ) for j in range ( 0, n ): print ( '%1d' % ( b[j] ), end = '' ) print ( '' ) b = bvec_next_grlex ( n, b ) return def c8_i ( ): #*****************************************************************************80 # ## c8_i() returns the value of the imaginary unit as a C8. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the value of the imaginary unit. # value = 1j return value def c8mat_identity ( n ): #*****************************************************************************80 # ## c8mat_identity() returns the complex identity matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # complex C(N,N), the identity matrix. # import numpy as np c = np.zeros ( ( n, n ), 'complex' ) for i in range ( 0, n ): c[i][i] = complex ( 1.0, 0.0 ) return c def c8mat_is_eigen_right ( n, k, a, x, lam ): #*****************************************************************************80 # ## c8mat_is_eigen_right() determines the error in a right eigensystem. # # Discussion: # # A C8MAT is a matrix of complex 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 MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer K, the number of eigenvectors. # K is usually 1 or N. # # complex A(N,N), the matrix. # # complex X(N,K), the K eigenvectors. # # complex LAM(K), the K eigenvalues. # # Output: # # complex 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. # c = c8mat_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 = c8mat_norm_fro ( n, k, c ) return value def c8mat_indicator ( m, n ): #*****************************************************************************80 # ## c8mat_indicator() returns the complex indicator matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 February 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # Output: # # complex C(M,N), the indicator matrix. # import numpy c = numpy.zeros ( ( m, n ), 'complex' ) for j in range ( 0, n ): for i in range ( 0, m ): c[i][j] = complex ( i + 1, - j - 1 ) return c def c8mat_indicator_test ( ): #*****************************************************************************80 # ## c8mat_indicator_test() tests c8mat_indicator(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 February 2015 # # Author: # # John Burkardt # import numpy as np m = 5 n = 3 print ( '' ) print ( 'c8mat_indicator_test' ) print ( ' c8mat_indicator returns the complex indicator matrix.' ) c = c8mat_indicator ( m, n ) c8mat_print ( m, n, c, ' Indicator matrix:' ) return def c8mat_mm ( n1, n2, n3, a, b ): #*****************************************************************************80 # ## c8mat_mm() multiplies two C8MAT's. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 May 2021 # # Author: # # John Burkardt # # Input: # # integer N1, N2, N3, the order of the matrices. # # complex A(N1,N2), B(N2,N3), the matrices to multiply. # # Output: # # complex C(N1,N3), the product matrix C = A * B. # import numpy as np c = np.zeros ( ( n1, n3 ), dtype = np.complex64 ) for j in range ( 0, n3 ): for i in range ( 0, n1 ): for k in range ( 0, n2 ): c[i,j] = c[i,j] + a[i,k] * b[k,j] return c def c8mat_norm_fro ( m, n, a ): #*****************************************************************************80 # ## c8mat_norm_fro() returns the Frobenius norm of a C8MAT. # # Discussion: # # The Frobenius norm is defined as # # c8mat_norm_fro = sqrt ( # sum ( 1 <= I <= M ) sum ( 1 <= j <= N ) A(I,J) * A(I,J) ) # # The matrix Frobenius norm is not derived from a vector norm, but # is compatible with the vector L2 norm, so that: # # c8vec_norm_l2 ( A * x ) <= c8mat_norm_fro ( A ) * c8vec_norm_l2 ( x ). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 February 2015 # # Author: # # John Burkardt # # Input: # # integer M, the number of rows in A. # # integer N, the number of columns in A. # # complex A(M,N), the matrix whose norm is desired. # # Output: # # real VALUE, the norm of A. # import numpy as np value = \ np.sqrt \ ( \ np.sum \ ( \ np.sum \ ( \ ( \ np.abs \ ( \ a \ ) \ ) ** 2 \ ) \ ) \ ) return value def c8mat_print ( m, n, a, title ): #*****************************************************************************80 # ## c8mat_print() prints a C8MAT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # # Input: # # integer M, the number of rows in A. # # integer N, the number of columns in A. # # complex A(M,N), the matrix. # # string TITLE, a title. # c8mat_print_some ( m, n, a, 0, 0, m - 1, n - 1, title ) return def c8mat_print_test ( ): #*****************************************************************************80 # ## c8mat_print_test() tests c8mat_print(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 December 2014 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'c8mat_print_test' ) print ( ' c8mat_print prints an C8MAT.' ) m = 4 n = 3 v = np.array ( [ \ [ complex(10.0, 1.0), complex(10.0, 2.0), complex(10.0, 3.0) ], \ [ complex(20.0, 1.0), complex(20.0, 2.0), complex(20.0, 3.0) ], \ [ complex(30.0, 1.0), complex(30.0, 2.0), complex(30.0, 3.0) ], \ [ complex(40.0, 1.0), complex(40.0, 2.0), complex(40.0, 3.0) ] ], \ dtype = np.complex128 ) c8mat_print ( m, n, v, ' Here is a C8MAT:' ) return def c8mat_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ): #*****************************************************************************80 # ## c8mat_print_some() prints out a portion of an C8MAT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 December 2014 # # Author: # # John Burkardt # # Input: # # integer M, N, the number of rows and columns of the matrix. # # complex A(M,N), an M by N matrix to be printed. # # integer ILO, JLO, the first row and column to print. # # integer IHI, JHI, the last row and column to print. # # string TITLE, a title. # incx = 4 print ( '' ) print ( title ) if ( m <= 0 or n <= 0 ): print ( '' ) print ( ' (None)' ) return for j2lo in range ( max ( jlo, 0 ), min ( jhi, n - 1 ), incx ): j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n - 1 ) j2hi = min ( j2hi, jhi ) print ( '' ) print ( ' Col: ' ), for j in range ( j2lo, j2hi + 1 ): print ( ' %7d ' % ( j ) ), print ( '' ) print ( ' Row' ) i2lo = max ( ilo, 0 ) i2hi = min ( ihi, m - 1 ) for i in range ( i2lo, i2hi + 1 ): print ( '%7d :' % ( i ) ), for j in range ( j2lo, j2hi + 1 ): print ( '%12g %12gi ' % ( a.real[i,j], a.imag[i,j] ) ), print ( '' ) return def c8mat_print_some_test ( ): #*****************************************************************************80 # ## c8mat_print_some_test() tests c8mat_print_some(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 December 2014 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'c8mat_print_some_test' ) print ( ' c8mat_print_some prints some of an C8MAT.' ) m = 4 n = 6 v = np.array ( [ \ [ complex(10.0, 1.0), complex(10.0, 2.0), complex(10.0, 3.0), \ complex(10.0, 4.0), complex(10.0, 5.0), complex(10.0, 6.0) ], \ [ complex(20.0, 1.0), complex(20.0, 2.0), complex(20.0, 3.0), \ complex(20.0, 4.0), complex(20.0, 5.0), complex(20.0, 6.0) ], \ [ complex(30.0, 1.0), complex(30.0, 2.0), complex(30.0, 3.0), \ complex(30.0, 4.0), complex(30.0, 5.0), complex(30.0, 6.0) ], \ [ complex(40.0, 1.0), complex(40.0, 2.0), complex(40.0, 3.0), \ complex(40.0, 4.0), complex(40.0, 5.0), complex(40.0, 6.0) ] ], \ dtype = np.complex128 ) c8mat_print_some ( m, n, v, 0, 3, 2, 5, ' Here is a C8MAT:' ) return def c8mat_uniform_01 ( m, n, seed ): #*****************************************************************************80 # ## c8mat_uniform_01() returns a unit pseudorandom C8MAT. # # Discussion: # # The angles should be uniformly distributed between 0 and 2 * PI, # the square roots of the radius uniformly distributed between 0 and 1. # # This results in a uniform distribution of values in the unit circle. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 April 2013 # # Author: # # John Burkardt # # Reference: # # Paul Bratley, Bennett Fox, Linus Schrage, # A Guide to Simulation, # Second Edition, # Springer, 1987, # ISBN: 0387964673, # LC: QA76.9.C65.B73. # # Bennett Fox, # Algorithm 647: # Implementation and Relative Efficiency of Quasirandom # Sequence Generators, # ACM Transactions on Mathematical Software, # Volume 12, Number 4, December 1986, pages 362-376. # # Pierre L'Ecuyer, # Random Number Generation, # in Handbook of Simulation, # edited by Jerry Banks, # Wiley, 1998, # ISBN: 0471134031, # LC: T57.62.H37. # # Peter Lewis, Allen Goodman, James Miller, # A Pseudo-Random Number Generator for the System/360, # IBM Systems Journal, # Volume 8, Number 2, 1969, pages 136-143. # # Input: # # integer M, N, the number of rows and columns in the matrix. # # integer SEED, a seed for the random number generator. # # Output: # # complex C(M,N), the pseudorandom complex matrix. # # integer SEED, a seed for the random number generator. # import numpy as np i4_huge = 2147483647 seed = np.floor ( seed ) if ( seed < 0 ): seed = seed + i4_huge if ( seed == 0 ): print ( '' ) print ( 'c8mat_uniform_01(): Fatal error!' ) print ( ' Input SEED = 0!' ) raise Exception ( 'c8mat_uniform_01(): Fatal error!' ) c = np.zeros ( ( m, n ), 'complex' ) for i2 in range ( 0, n ): for i1 in range ( 0, m ): k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ): seed = seed + i4_huge r = np.sqrt ( seed * 4.656612875E-10 ) k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ): seed = seed + i4_huge theta = 2.0 * np.pi * seed * 4.656612875E-10 c[i1][i2] = r * complex ( np.cos ( theta ), np.sin ( theta ) ) return c, seed def c8mat_uniform_01_test ( ): #*****************************************************************************80 # ## c8mat_uniform_01_test() tests c8mat_uniform_01(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 December 2014 # # Author: # # John Burkardt # import numpy as np m = 5 n = 3 seed = 123456789 print ( '' ) print ( 'c8mat_uniform_01_test' ) print ( ' c8mat_uniform_01 computes a random C8MAT.' ) print ( '' ) print ( ' 0 <= X <= 1' ) print ( ' Initial seed is %d' % ( seed ) ) v, seed = c8mat_uniform_01 ( m, n, seed ) c8mat_print ( m, n, v, ' Random C8MAT:' ) return def c8_uniform_01 ( seed ): #*****************************************************************************80 # ## c8_uniform_01() returns a unit pseudorandom C8. # # Discussion: # # The angle should be uniformly distributed between 0 and 2 * PI, # the square root of the radius uniformly distributed between 0 and 1. # # This results in a uniform distribution of values in the unit circle. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 April 2013 # # Author: # # John Burkardt # # Reference: # # Paul Bratley, Bennett Fox, Linus Schrage, # A Guide to Simulation, # Second Edition, # Springer, 1987, # ISBN: 0387964673, # LC: QA76.9.C65.B73. # # Bennett Fox, # Algorithm 647: # Implementation and Relative Efficiency of Quasirandom # Sequence Generators, # ACM Transactions on Mathematical Software, # Volume 12, Number 4, December 1986, pages 362-376. # # Pierre L'Ecuyer, # Random Number Generation, # in Handbook of Simulation, # edited by Jerry Banks, # Wiley, 1998, # ISBN: 0471134031, # LC: T57.62.H37. # # Peter Lewis, Allen Goodman, James Miller, # A Pseudo-Random Number Generator for the System/360, # IBM Systems Journal, # Volume 8, Number 2, 1969, pages 136-143. # # Input: # # integer SEED, a seed for the random number generator. # # Output: # # complex C, the pseudorandom complex value. # # integer SEED, a seed for the random number generator. # import numpy as np i4_huge = 2147483647 seed = np.floor ( seed ) seed = ( seed % i4_huge ) if ( seed < 0 ): seed = seed + i4_huge if ( seed == 0 ): print ( '' ) print ( 'c8_uniform_01(): Fatal error!' ) print ( ' Input SEED = 0!' ) raise Exception ( 'c8_uniform_01(): Fatal error!' ) k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ): seed = seed + i4_huge r = np.sqrt ( seed * 4.656612875E-10 ) k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ): seed = seed + i4_huge theta = 2.0 * np.pi * seed * 4.656612875E-10 c = r * complex ( np.cos ( theta ), np.sin ( theta ) ) return c, seed def c8_uniform_01_test ( ): #*****************************************************************************80 # ## c8_uniform_01_test() tests c8_uniform_01(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # seed = 123456789 print ( '' ) print ( 'c8_uniform_01_test' ) print ( ' c8_uniform_01 computes pseudorandom complex values' ) print ( ' in the unit circle.' ) print ( '' ) print ( ' The initial seed is %d' % ( seed ) ) print ( '' ) for i in range ( 0, 10 ): x, seed = c8_uniform_01 ( seed ) print ( ' %6d ( %g, %g )' % ( i, x.real, x.imag ) ) return def c8vec_print ( n, a, title ): #*****************************************************************************80 # ## c8vec_print() prints a C8VEC. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the dimension of the vector. # # complex A(N), the vector to be printed. # # string TITLE, a title. # print ( '' ) print ( title ) print ( '' ) for i in range ( 0, n ): print ( '%6d %12g %12g' % ( i, a.real[i], a.imag[i] ) ) def c8vec_print_test ( ): #*****************************************************************************80 # ## c8vec_print_test() tests c8vec_print(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 December 2014 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'c8vec_print_test' ) print ( ' c8vec_print prints an C8VEC.' ) n = 4 v = np.array ( [ complex ( 1.0, 2.0 ), \ complex ( 3.0, 4.0 ), \ complex ( 5.0, 6.0 ), \ complex ( 7.0, 8.0 ) ], dtype = np.complex128 ) c8vec_print ( n, v, ' Here is a C8VEC:' ) return def c8vec_uniform_01 ( n, seed ): #*****************************************************************************80 # ## c8vec_uniform_01() returns a unit pseudorandom C8VEC. # # Discussion: # # The angles should be uniformly distributed between 0 and 2 * PI, # the square roots of the radius uniformly distributed between 0 and 1. # # This results in a uniform distribution of values in the unit circle. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # # Reference: # # Paul Bratley, Bennett Fox, Linus Schrage, # A Guide to Simulation, # Second Edition, # Springer, 1987, # ISBN: 0387964673, # LC: QA76.9.C65.B73. # # Bennett Fox, # Algorithm 647: # Implementation and Relative Efficiency of Quasirandom # Sequence Generators, # ACM Transactions on Mathematical Software, # Volume 12, Number 4, December 1986, pages 362-376. # # Pierre L'Ecuyer, # Random Number Generation, # in Handbook of Simulation, # edited by Jerry Banks, # Wiley, 1998, # ISBN: 0471134031, # LC: T57.62.H37. # # Peter Lewis, Allen Goodman, James Miller, # A Pseudo-Random Number Generator for the System/360, # IBM Systems Journal, # Volume 8, Number 2, 1969, pages 136-143. # # Input: # # integer N, the number of values to compute. # # integer SEED, a seed for the random number generator. # # Output: # # complex C(N), the pseudorandom complex vector. # # integer SEED, a seed for the random number generator. # import numpy as np i4_huge = 2147483647 seed = np.floor ( seed ) if ( seed < 0 ): seed = seed + i4_huge if ( seed == 0 ): print ( '' ) print ( 'c8vec_uniform_01(): Fatal error!' ) print ( ' Input SEED = 0!' ) raise Exception ( 'c8vec_uniform_01(): Fatal error!' ) c = np.zeros ( n, 'complex' ) for j in range ( 0, n ): k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ): seed = seed + i4_huge r = np.sqrt ( seed * 4.656612875E-10 ) k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ): seed = seed + i4_huge theta = 2.0 * np.pi * seed * 4.656612875E-10 c[j] = r * complex ( np.cos ( theta ), np.sin ( theta ) ) return c, seed def c8vec_uniform_01_test ( ): #*****************************************************************************80 # ## c8vec_uniform_01_test() tests c8vec_uniform_01(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # seed = 123456789 print ( '' ) print ( 'c8vec_uniform_01_test' ) print ( ' c8vec_uniform_01 computes pseudorandom complex values' ) print ( ' in the unit circle.' ) print ( '' ) print ( ' The initial seed is %d' % ( seed ) ) print ( '' ) n = 10 [ x, seed ] = c8vec_uniform_01 ( n, seed ) for i in range ( 0, n ): print ( ' %6d ( %f, %f )' % ( i, x[i].real, x[i].imag ) ) return def c8vec_unity ( n ): #*****************************************************************************80 # ## c8vec_unity() returns the N roots of unity. # # Discussion: # # X(1:N) = exp ( 2 * PI * (0:N-1) / N ) # # X(1:N)^N = ( (1,0), (1,0), ..., (1,0) ). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of elements. # # Output: # # complex A(N), the array. # import numpy as np a = np.zeros ( n, 'complex' ) for i in range ( 0, n ): t = 2.0 * np.pi * float ( i ) / float ( n ) a[i] = np.cos ( t ) + 1j * np.sin ( t ) return a def c8vec_unity_test ( ): #*****************************************************************************80 # ## c8vec_unity_test() tests c8vec_unity(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 February 2015 # # Author: # # John Burkardt # print ( '' ) print ( 'c8vec_unity_test' ) print ( ' c8vec_unity returns the N roots of unity.' ) n = 12 x = c8vec_unity ( n ) c8vec_print ( n, x, ' The N roots of unity:' ) return def carry_matrix ( n, alpha ): #*****************************************************************************80 # ## carry_matrix() returns the CARRY matrix. # # Discussion: # # We assume that arithmetic is being done in base ALPHA. We are adding # a column of N digits base ALPHA, as part of adding N random numbers. # We know the carry digit, between 0 and N-1, that is being carried into the # column sum (the incarry digit), and we want to know the probability of # the various carry digits 0 through N-1 (the outcarry digit) that could # be carried out of the column sum. # # The carry matrix summarizes this data. The entry A(I,J) represents # the probability that, given that the incarry digit is I-1, the # outcarry digit will be J-1. # # Formula: # # A(I,J) = ( 1 / ALPHA )^N * sum ( 0 <= K <= J-1 - floor ( I-1 / ALPHA ) ) # (-1)^K * C(N+1,K) * C(N-I+(J-K)*ALPHA, N ) # # Example: # # ALPHA = 10, N = 4 # # 0.0715 0.5280 0.3795 0.0210 # 0.0495 0.4840 0.4335 0.0330 # 0.0330 0.4335 0.4840 0.0495 # 0.0210 0.3795 0.5280 0.0715 # # Square Properties: # # A is generally not symmetric: A' /= A. # # A is a Markov matrix. # # A is centrosymmetric: A(I,J) = A(N+1-I,N+1-J). # # LAMBDA(I) = 1 / ALPHA^(I-1) # # det ( A ) = 1 / ALPHA^((N*(N-1))/2) # # The eigenvectors do not depend on ALPHA. # # A is generally not normal: A' * A /= A * A'. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 December 2014 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Input: # # integer N, the order of the matrix. # # integer ALPHA, the numeric base used in the addition. # # Output: # # real A(N,N), the matrix. # from scipy.special import comb import numpy as np a = np.zeros ( [ n, n ] ) for j in range ( 0, n ): for i in range ( 0, n ): temp = 0.0 s = -1.0 for k in range ( 0, j - ( i // int ( alpha ) ) + 1 ): s = - s c1 = comb ( n + 1, k ) c2 = comb ( n - i - 1 + ( j + 1 - k ) * alpha, n ) temp = temp + s * c1 * c2 a[i,j] = temp / alpha ** n return a def carry_determinant ( n, alpha ): #*****************************************************************************80 # ## carry_determinant() returns the determinant of the CARRY matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer ALPHA, the numeric base used in the addition. # # Output: # # real DETERM, the determinant. # power = ( n * ( n - 1 ) ) // 2 determ = 1.0 / alpha ** power return determ def carry_eigen_left ( n, alpha ): #*****************************************************************************80 # ## carry_eigen_left() returns the left eigenvectors for the CARRY matrix. # # Formula: # # A(I,J) = sum ( 0 <= K <= J-1 ) # (-1)^K * C(N+1,K) * ( J - K )^(N+1-I) # # Example: # # N = 4 # # 1 11 11 1 # 1 3 -3 -1 # 1 -1 -1 1 # 1 -3 3 -1 # # Properties: # # A is generally not symmetric: A' /= A. # # Column 1 is all 1's, and column N is (-1)^(I+1). # # The top row is proportional to a row of Eulerian numbers, and # can be normalized to represent the stationary probablities # for the carrying process when adding N random numbers. # # The bottom row is proportional to a row of Pascal's triangle, # with alternating signs. # # The product of the left and right eigenvector matrices of # order N is N! times the identity. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Input: # # integer N, the order of the matrix. # # integer ALPHA, the numeric base used in the addition. # # Output: # # real A(N,N), the matrix. # from scipy.special import comb import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): s = -1.0 for k in range ( 0, j + 1 ): s = - s a[i,j] = a[i,j] + s * comb ( n + 1, k ) * ( j + 1 - k ) ** ( n - i ) return a def carry_eigen_right ( n, alpha ): #*****************************************************************************80 # ## carry_eigen_right() returns the right eigenvectors of the CARRY matrix. # # Formula: # # A(I,J) = sum ( N+1-J) <= K <= N ) # S1(N,K) * C(K,N+1-J) ( N - I )^(K-N+J-1) # # where S1(N,K) is a signed Sterling number of the first kind. # # Example: # # N = 4 # # 1 6 11 6 # 1 2 -1 -2 # 1 -2 -1 2 # 1 -6 11 -6 # # Properties: # # A is generally not symmetric: A' /= A. # # The first column is all 1's. # # The last column is reciprocals of binomial coefficients with # alternating sign multiplied by (N-1). # # The top and bottom rows are the unsigned and signed Stirling numbers # of the first kind. # # The entries in the J-th column are a degree (J-1) polynomial # in the row index I. (Column 1 is constant, the first difference # in column 2 is constant, the second difference in column 3 is # constant, and so on.) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Input: # # integer N, the order of the matrix. # # integer ALPHA, the numeric base used in the addition. # # Output: # # real A(N,N), the matrix. # from scipy.special import comb import numpy as np s1 = stirling_matrix ( n, n ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): for k in range ( n - j, n + 1 ): if ( n - 1 - i == 0 and k - n + j == 0 ): a[i,j] = a[i,j] + s1[n-1,k-1] * comb ( k, n - j ) else: a[i,j] = a[i,j] + s1[n-1,k-1] * comb ( k, n - j ) \ * ( n - i - 1 ) ** ( k - n + j ) return a def carry_eigenvalues ( n, alpha ): #*****************************************************************************80 # ## carry_eigenvalues() returns the eigenvalues of the CARRY matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer ALPHA, the numeric base used in the addition. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n ) for i in range ( 0, n ): lam[i] = 1.0 / alpha ** i return lam def carry_inverse ( n, alpha ): #*****************************************************************************80 # ## carry_inverse() returns the inverse of the CARRY matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer ALPHA, the numeric base used in the addition. # # integer ALPHA, the numeric base used in the addition. # # Output: # # real A(N,N), the matrix. # from scipy.special import factorial v = carry_eigen_left ( n, alpha ) d = carry_eigenvalues ( n, alpha ) d[0:n] = 1.0 / d[0:n] d_inv = diagonal_matrix ( 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 = factorial ( n ) for j in range ( 0, n ): for i in range ( 0, n ): a[i,j] = a[i,j] / t return a def cauchy_matrix ( n, x, y ): #*****************************************************************************80 # ## cauchy_matrix() returns the CAUCHY matrix. # # Formula: # # A(I,J) = 1.0 / ( X(I) + Y(J) ) # # Example: # # N = 5, X = ( 1, 3, 5, 8, 7 ), Y = ( 2, 4, 6, 10, 9 ) # # 1/3 1/5 1/7 1/11 1/10 # 1/5 1/7 1/9 1/13 1/12 # 1/7 1/9 1/11 1/15 1/14 # 1/10 1/12 1/14 1/18 1/17 # 1/9 1/11 1/13 1/17 1/16 # # or, in decimal form, # # 0.333333 0.200000 0.142857 0.0909091 0.100000 # 0.200000 0.142857 0.111111 0.0769231 0.0833333 # 0.142857 0.111111 0.0909091 0.0666667 0.0714286 # 0.100000 0.0833333 0.0714286 0.0555556 0.0588235 # 0.111111 0.0909091 0.0769231 0.0588235 0.0625000 # # Properties: # # A is generally not symmetric: A' /= A. # # A is totally positive if 0 < X(1) < ... < X(N) and 0 < Y1 < ... < Y(N). # # A will be singular if any X(I) equals X(J), or # any Y(I) equals Y(J), or if any X(I)+Y(J) equals zero. # # A is generally not normal: A' * A /= A * A'. # # The Hilbert matrix is a special case of the Cauchy matrix. # # The Parter matrix is a special case of the Cauchy matrix. # # The Ris or "ding-dong" matrix is a special case of the Cauchy matrix. # # det ( A ) = product ( 1 <= I < J <= N ) ( X(J) - X(I) )* ( Y(J) - Y(I) ) # / product ( 1 <= I <= N, 1 <= J <= N ) ( X(I) + Y(J) ) # # The inverse of A is # # INVERSE(A)(I,J) = product ( 1 <= K <= N ) [ (X(J)+Y(K)) * (X(K)+Y(I)) ] / # [ (X(J)+Y(I)) * product ( 1 <= K <= N, K /= J ) (X(J)-X(K)) # * product ( 1 <= K <= N, K /= I ) (Y(I)-Y(K)) ] # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 January 2015 # # Author: # # John Burkardt # # Reference: # # Robert Gregory, David Karney, # Example 3.26, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, New York, 1969, page 54, # LC: QA263.G68. # # Nicholas Higham, # Accuracy and Stability of Numerical Algorithms, # SIAM, 1996. # # Donald Knuth, # The Art of Computer Programming, # Volume 1, Fundamental Algorithms, Second Edition # Addison-Wesley, Reading, Massachusetts, 1973, page 36. # # Olga Taussky, Marvin Marcus, # Eigenvalues of finite matrices, # in Survey of Numerical Analysis, # Edited by John Todd, # McGraw-Hill, New York, pages 279-313, 1962. # # Evgeny Tyrtyshnikov, # Cauchy-Toeplitz matrices and some applications, # Linear Algebra and Applications, # Volume 149, 1991, pages 1-18. # # Input: # # integer N, the order of A. # # real X(N), Y(N), vectors that determine A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( x[i] + y[j] == 0.0 ): print ( '' ) print ( 'CAUCHY(): Fatal error!' ) print ( ' The denominator X(I)+Y(J) was zero' ) print ( ' for I = %d' % ( i ) ) print ( ' X(I) = %g' % ( x[i] ) ) print ( ' and J = %d' % ( j ) ) print ( ' Y(J) = %g' % ( y[j] ) ) raise Exception ( 'CAUCHY(): Fatal error!' ) a[i,j] = 1.0 / ( x[i] + y[j] ) return a def cauchy_determinant ( n, x, y ): #*****************************************************************************80 # ## cauchy_determinant() returns the determinant of the CAUCHY matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N), Y(N), vectors that determine A. # # Output: # # real DETERM, the determinant. # top = 1.0 for i in range ( 0, n ): for j in range ( i + 1, n ): top = top * ( x[j] - x[i] ) * ( y[j] - y[i] ) bottom = 1.0 for i in range ( 0, n ): for j in range ( 0, n ): bottom = bottom * ( x[i] + y[j] ) determ = top / bottom return determ def cauchy_inverse ( n, x, y ): #*****************************************************************************80 # ## cauchy_inverse() returns the inverse of the CAUCHY matrix. # # Formula: # # A(I,J) = product ( 1 <= K <= N ) [(X(J)+Y(K))*(X(K)+Y(I))] / # [ (X(J)+Y(I)) * product ( 1 <= K <= N, K /= J ) (X(J)-X(K)) # * product ( 1 <= K <= N, K /= I ) (Y(I)-Y(K)) ] # # Example: # # N = 5, X = ( 1, 3, 5, 8, 7 ), Y = ( 2, 4, 6, 10, 9 ) # # 241.70 -2591.37 9136.23 10327.50 -17092.97 # -2382.19 30405.38 -116727.19 -141372.00 229729.52 # 6451.76 -89667.70 362119.56 459459.00 -737048.81 # 10683.11 -161528.55 690983.38 929857.44 -1466576.75 # -14960.00 222767.98 -942480.06 -1253376.00 1983696.00 # # Properties: # # A is generally not symmetric: A' /= A. # # The sum of the entries of A equals the sum of the entries of X and Y. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Reference: # # Donald Knuth, # The Art of Computer Programming, # Volume 1, Fundamental Algorithms, Second Edition, # Addison-Wesley, Reading, Massachusetts, 1973, page 36. # # Input: # # integer N, the order of A. # # real X(N), Y(N), vectors that determine A. # The following conditions on X and Y must hold: # X(I)+Y(J) must not be zero for any I and J; # X(I) must never equal X(J); # Y(I) must never equal Y(J). # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) # # Check the data. # for i in range ( 0, n ): for j in range ( 0, n ): if ( x[i] + y[j] == 0.0 ): print ( '' ) print ( 'cauchy_inverse(): Fatal error!' ) print ( ' The denominator X(I)+Y(J) was zero' ) print ( ' for I = %d' % ( i ) ) print ( ' and J = %d' % ( j ) ) raise Exception ( 'cauchy_inverse(): Fatal error!' ) if ( i != j and x[i] == x[j] ): print ( '' ) print ( 'cauchy_inverse(): Fatal error!' ) print ( ' X(I) equals X(J)' ) print ( ' for I = %d' % ( i ) ) print ( ' and J = %d' % ( j ) ) raise Exception ( 'cauchy_inverse(): Fatal error!' ) if ( i != j and y[i] == y[j] ): print ( '' ) print ( 'cauchy_inverse(): Fatal error!' ) print ( ' Y(I) equals Y(J)' ) print ( ' for I =%d' % ( i ) ) print ( ' and J = %d' % ( j ) ) raise Exception ( 'cauchy_inverse(): Fatal error!' ) for i in range ( 0, n ): for j in range ( 0, n ): top = 1.0 bot1 = 1.0 bot2 = 1.0 for k in range ( 0, n ): top = top * ( x[j] + y[k] ) * ( x[k] + y[i] ) if ( k != j ): bot1 = bot1 * ( x[j] - x[k] ) if ( k != i ): bot2 = bot2 * ( y[i] - y[k] ) a[i,j] = top / ( ( x[j] + y[i] ) * bot1 * bot2 ) return a def cheby_diff1_matrix ( n ): #*****************************************************************************80 # ## cheby_diff1_matrix() returns the cheby_diff1 matrix. # # Discussion: # # cheby_diff1 is the Chebyshev Differentiation matrix. # # Example: # # N = 6 # # 8.5000 -10.4721 2.8944 -1.5279 1.1056 -0.5000 # 2.6180 -1.1708 -2.0000 0.8944 -0.6810 0.2764 # -0.7236 2.0000 -0.1708 1.6180 0.8944 -0.3820 # 0.3820 -0.8944 1.6180 0.1708 -2.0000 0.7236 # -0.2764 0.6180 -0.8944 2.0000 1.1708 -2.6180 # 0.5000 -1.1056 1.5279 -2.8944 10.4721 -8.5000 # # Properties: # # A is antisymmetric. # # If 1 < N, then det ( A ) = 0. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 December 2014 # # Author: # # John Burkardt # # Reference: # # Lloyd Trefethen, # Spectral Methods in MATLAB, # SIAM, 2000, page 54. # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) if ( n == 1 ): a[0,0] = 1.0 return a c = np.zeros ( n ) c[0] = 2.0 for i in range ( 1, n - 1 ): c[i] = 1.0 c[n-1] = 2.0 # # Get the Chebyshev points. # x = np.zeros ( n + 1 ) for i in range ( 0, n + 1 ): x[i] = np.cos ( np.pi * float ( i ) / float ( n - 1 ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i != j ): a[i,j] = ( -1.0 ) ** ( i + j ) * c[i] / ( c[j] * ( x[i] - x[j] ) ) elif ( i == 0 ): a[i,i] = float ( 2 * ( n - 1 ) ** 2 + 1 ) / 6.0 elif ( i == n - 1 ): a[i,i] = - float ( 2 * ( n - 1 ) ** 2 + 1 ) / 6.0 else: a[i,i] = - 0.5 * x[i] / ( 1.0 - x[i] ** 2 ) return a def cheby_diff1_determinant ( n ): #*****************************************************************************80 # ## cheby_diff1_determinant() returns the determinant of the cheby_diff1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # if ( 1 == n ): determ = 1.0 else: determ = 0.0 return determ def cheby_diff1_null_left ( m, n ): #*****************************************************************************80 # ## cheby_diff1_null_left() returns a left null vector for the cheby_diff1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # Output: # # real X(M), the vector. # import numpy as np if ( ( m % 2 ) == 1 ): x = np.zeros ( m ) x[0] = 1.0 t = -2.0 for i in range ( 1, m - 1 ): x[i] = t t = -t x[m-1] = 1.0 else: x = np.zeros ( m ) return x def cheby_diff1_null_right ( m, n ): #*****************************************************************************80 # ## cheby_diff1_null_right() returns a right null vector for the cheby_diff1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # Output: # # real X(N), the vector. # import numpy as np if ( n % 2 == 1 ): x = np.ones ( n ) else: x = np.zeros ( n ) return x def cheby_t_matrix ( n ): #*****************************************************************************80 # ## cheby_t_matrix() returns the cheby_t matrix. # # Example: # # N = 11 # # 1 . -1 . 1 . -1 . 1 . -1 # . 1 . -3 . 5 . -7 . 9 . # . . 2 . -8 . 18 . -32 . 50 # . . . 4 . -20 . 56 . -120 . # . . . . 8 . -48 . 160 . -400 # . . . . . 16 . -112 . 432 . # . . . . . . 32 . -256 . 1120 # . . . . . . . 64 . -576 . # . . . . . . . . 128 . -1280 # . . . . . . . . . 256 . # . . . . . . . . . . 512 # # Properties: # # A is generally not symmetric: A' /= A. # # A is integral: int ( A ) = A. # # A is reducible. # # A is lower triangular. # # Each row of A sums to 1. # # det ( A ) = 2^( (N-1) * (N-2) / 2 ) # # A is not normal: A' * A /= A * A'. # # For I = 1: # LAMBDA(1) = 1 # For 1 < I # LAMBDA(I) = 2^(I-2) # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 April 2024 # # Author: # # John Burkardt # # Input: # # integer N: the order of A. # # Output: # # real A(N,N): the matrix. # import numpy as np A = np.zeros ( [ n, n ] ) A[0,0] = 1.0 if ( n == 1 ): return A A[1,1] = 1.0 for j in range ( 2, n ): for i in range ( 0, n ): A[i,j] = - A[i,j-2] for i in range ( 1, n ): A[i,j] = A[i,j] + 2.0 * A[i-1,j-1] return A def cheby_t_determinant ( n ): #*****************************************************************************80 # ## cheby_t_determinant() returns the determinant of the cheby_t matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 02 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # power = ( ( n - 1 ) * ( n - 2 ) ) // 2 determ = 2 ** power return determ def cheby_t_inverse ( n ): #*****************************************************************************80 # ## cheby_t_inverse() returns the inverse of the cheby_t matrix. # # Example: # # N = 11 # Each column must be divided by the divisor below it. # # 1 . 1 . 3 . 10 . 35 . 126 # . 1 . 3 . 10 . 35 . 126 . # . . 1 . 4 . 15 . 56 . 210 # . . . 1 . 5 . 21 . 84 . # . . . . 1 . 6 . 28 . 120 # . . . . . 1 . 7 . 36 . # . . . . . . 1 . 8 . 45 # . . . . . . . 1 . 9 . # . . . . . . . . 1 . 10 # . . . . . . . . . 1 . # . . . . . . . . . . 1 # /1 /1 /2 /4 /8 /16 /32 /64 /128 /256 /512 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 April 2024 # # Author: # # John Burkardt # # Input: # # integer N: the order of A. # # Output: # # real A(N,N): the matrix. # import numpy as np A = np.zeros ( ( n, n ) ) A[0,0] = 1.0 if ( 1 < n ): A[1,1] = 1.0 if ( 2 < n ): for j in range ( 2, n ): for i in range ( 0, n ): if ( i == 0 ): A[i,j] = A[i+1,j-1] / 2.0 elif ( i == 1 ): A[i,j] = ( 2.0 * A[i-1,j-1] + A[i+1,j-1] ) / 2.0 elif ( i < n - 1 ): A[i,j] = ( A[i-1,j-1] + A[i+1,j-1] ) / 2.0 else: A[i,j] = A[i-1,j-1] / 2.0 return A def cheby_u_polynomial ( n, x ): #*****************************************************************************80 # ## cheby_u_polynomial() evaluates the Chebyshev polynomials of the second kind. # # Differential equation: # # (1-X*X) Y'' - 3 X Y' + N (N+2) Y = 0 # # First terms: # # U(0)(X) = 1 # U(1)(X) = 2 X # U(2)(X) = 4 X^2 - 1 # U(3)(X) = 8 X^3 - 4 X # U(4)(X) = 16 X^4 - 12 X^2 + 1 # U(5)(X) = 32 X^5 - 32 X^3 + 6 X # U(6)(X) = 64 X^6 - 80 X^4 + 24 X^2 - 1 # U(7)(X) = 128 X^7 - 192 X^5 + 80 X^3 - 8X # # Recursion: # # U(0)(X) = 1, # U(1)(X) = 2 * X, # U(N)(X) = 2 * X * U(N-1)(X) - U(N-2)(X) # # Norm: # # Integral ( -1 <= X <= 1 ) ( 1 - X^2 ) * U(N)(X)^2 dX = PI/2 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the highest polynomial to compute. # # real X, the point at which the polynomials are to be computed. # # Output: # # real CX(1:N+1), the values of the N+1 Chebyshev polynomials. # import numpy as np cx = np.zeros ( n + 1 ) cx[0] = 1.0 if ( n < 1 ): return cx cx[1] = 2.0 * x for i in range ( 2, n + 1 ): cx[i] = 2.0 * x * cx[i-1] - cx[i-2] return cx def cheby_u_matrix ( n ): #*****************************************************************************80 # ## cheby_u_matrix() returns the cheby_u matrix. # # Example: # # N = 11 # # 1 . . . . . . . . . . # . 2 . . . . . . . . . # -1 . 4 . . . . . . . . # . -4 . 8 . . . . . . . # 1 . -12 . 16 . . . . . . # . 6 . -32 . 32 . . . . . # -1 . 24 . -80 . 64 . . . . # . -8 . 80 . -192 . 128 . . . # 1 . -40 . 240 . -448 . 256 . . # . 10 . -160 . 672 . -1024 . 512 . # -1 . 60 . -560 . 1792 . -2304 . 1024 # # Properties: # # A is generally not symmetric: A' /= A. # # A is integral: int ( A ) = A. # # A is generally not normal: A' * A /= A * A'. # # A is lower triangular. # # A is reducible. # # The entries of row N sum to N. # # det ( A ) = 2^((N*(N-1))/2). # # LAMBDA(I) = 2^(I-1) # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 01 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) a[0,0] = 1.0 if ( n == 1 ): return a a[1,1] = 2.0 if ( n == 2 ): return a for i in range ( 2, n ): for j in range ( 0, n ): if ( j == 0 ): a[i,j] = - a[i-2,j] else: a[i,j] = 2.0 * a[i-1,j-1] - a[i-2,j] return a def cheby_u_determinant ( n ): #*****************************************************************************80 # ## cheby_u_determinant() returns the determinant of the cheby_u matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 01 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # power = ( n * ( n - 1 ) ) // 2 determ = 2.0 ** power return determ def cheby_u_inverse ( n ): #*****************************************************************************80 # ## cheby_u_inverse() returns the inverse of the cheby_u matrix. # # Example: # # N = 11 # # 1 . . . . . . . . . . # . 1 . . . . . . . . . / 2 # 1 . 1 . . . . . . . . / 4 # . 2 . 1 . . . . . . . / 8 # 2 . 3 . 1 . . . . . . / 16 # . 5 . 4 . 1 . . . . . / 32 # 5 . 9 . 5 . 1 . . . . / 64 # . 14 . 14 . 6 . 1 . . . / 128 # 14 . 28 . 20 . 7 . 1 . . / 256 # . 42 . 48 . 27 . 8 . 1 . / 512 # 42 . 90 . 75 . 35 . 9 . 1 / 1024 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) a[0,0] = 1.0 if ( 1 < n ): a[1,1] = 0.5 if ( 2 < n ): for i in range ( 2, n ): for j in range ( 0, n ): if ( j == 0 ): a[i,j] = a[i-1,j+1] / 2.0 elif ( j < n - 1 ): a[i,j] = ( a[i-1,j-1] + a[i-1,j+1] ) / 2.0 else: a[i,j] = a[i-1,j-1] / 2.0 return a def cheby_van1_matrix ( m, a, b, n, x ): #*****************************************************************************80 # ## cheby_van1_matrix() returns the Chebyshev Vandermonde-like matrix for [A,B]. # # Discussion: # # Normally, the Chebyshev polynomials are defined on -1 <= XI <= +1. # Here, we assume the Chebyshev polynomials have been defined on the # interval A <= X <= B, using the mapping # XI = ( - ( B - X ) + ( X - A ) ) / ( B - A ) # so that # ChebyAB(A,B;X) = Cheby(XI). # # if ( I == 1 ) then # V(1,1:N) = 1; # elseif ( I == 2 ) then # V(2,1:N) = XI(1:N); # else # V(I,1:N) = 2.0 * XI(1:N) * V(I-1,1:N) - V(I-2,1:N); # # Example: # # M = 5, A = -1, B = +1, N = 5, X = ( 1, 2, 3, 4, 5 ) # # 1 1 1 1 1 # 1 2 3 4 5 # 1 7 17 31 49 # 1 26 99 244 485 # 1 97 577 1921 4801 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Reference: # # Nicholas Higham, # Stability analysis of algorithms for solving confluent # Vandermonde-like systems, # SIAM Journal on Matrix Analysis and Applications, # Volume 11, 1990, pages 23-41. # # Input: # # integer M, the number of rows of the matrix. # # real A, B, the interval. # # integer N, the number of values in X, and the number # of columns in the matrix. # # real X(N), the abscissas. # # Output: # # real V(M,N), the matrix. # import numpy as np # # Compute the normalized abscissas in [-1,+1]. # xi = np.zeros ( n ) for i in range ( 0, n ): xi[i] = ( - 1.0 * ( b - x[i] ) \ + 1.0 * ( x[i] - a ) ) \ / ( b - a ) # # Compute the matrix. # v = np.zeros ( [ m, n ] ) for j in range ( 0, n ): v[0,j] = 1.0 v[1,j] = xi[j] for i in range ( 2, m ): v[i,j] = 2.0 * xi[j] * v[i-1,j] - v[i-2,j] return v def cheby_van2_matrix ( n ): #*****************************************************************************80 # ## cheby_van2_matrix() returns the cheby_van2 matrix. # # Discussion: # # cheby_van2 is the Chebyshev Vandermonde-like matrix. # # Discussion: # # The formula for this matrix has been slightly modified, by a scaling # factor, in order to make it closer to its inverse. # # Formula: # # A(I,J) = ( 1 / sqrt ( N - 1 ) ) * cos ( (I-1) * (J-1) * PI / (N-1) ) # # Example: # # N = 4 # # 1 1 1 1 # 1/sqrt(3) * 1 COS(PI/3) COS(2*PI/3) COS(3*PI/3) # 1 COS(2*PI/3) COS(4*PI/3) COS(6*PI/3) # 1 COS(3*PI/3) COS(6*PI/3) COS(9*PI/3) # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # The entries of A are based on the extrema of the Chebyshev # polynomial T(n-1). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) if ( n == 1 ): a[0,0] = 1.0 return a t = np.sqrt ( float ( n - 1 ) ) for i in range ( 0, n ): for j in range ( 0, n ): angle = float ( i * j ) * np.pi / float ( n - 1 ) a[i,j] = np.cos ( angle ) / t return a def cheby_van2_determinant ( n ): #*****************************************************************************80 # ## cheby_van2_determinant() returns the determinant of the cheby_van2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # import numpy as np if ( n <= 0 ): determ = 0.0 elif ( n == 1 ): determ = 1.0 else: determ = r8_mop ( float ( n // 2 ) ) * np.sqrt ( 2.0 ) ** ( 4 - n ) return determ def cheby_van2_inverse ( n ): #*****************************************************************************80 # ## cheby_van2_inverse() inverts the cheby_van2 matrix. # # Discussion: # # cheby_van2 is the Chebyshev Vandermonde-like matrix. # # Formula: # # if ( I == 1 or N ) .and. ( J == 1 or N ) then # A(I,J) = ( 1 / (2*sqrt(N-1)) ) * cos ( (I-1) * (J-1) * PI / (N-1) ) # else if ( I == 1 or N ) .or. ( J == 1 or N ) then # A(I,J) = ( 1 / ( sqrt(N-1)) ) * cos ( (I-1) * (J-1) * PI / (N-1) ) # else # A(I,J) = ( 2 / sqrt(N-1) ) * cos ( (I-1) * (J-1) * PI / (N-1) ) # # # Example: # # N = 4 # # 1/2 1 1 1/2 # 1/sqrt(3) * 1 2*COS(PI/3) 2*COS(2*PI/3) COS(3*PI/3) # 1 2*COS(2*PI/3) 2*COS(4*PI/3) COS(6*PI/3) # 1/2 COS(3*PI/3) COS(6*PI/3) 1/2 * COS(9*PI/3) # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # The entries of A are based on the extrema of the Chebyshev # polynomial T(n-1). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): angle = float ( i * j ) * np.pi / float ( n - 1 ) a[i,j] = np.cos ( angle ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = 2.0 * a[i,j] / np.sqrt ( float ( n - 1 ) ) for j in range ( 0, n ): a[0,j] = 0.5 * a[0,j] a[n-1,j] = 0.5 * a[n-1,j] for i in range ( 0, n ): a[i,0] = 0.5 * a[i,0] a[i,n-1] = 0.5 * a[i,n-1] return a def cheby_van3_matrix ( n ): #*****************************************************************************80 # ## cheby_van3_matrix() returns the cheby_van3 matrix. # # Discussion: # # cheby_van3 is the Chebyshev Vandermonde-like matrix. # # Formula: # # A(I,J) = cos ( (I-1) * (J-1/2) * PI / N ) # # Example: # # N = 4 # # 1 1 1 1 # COS( PI/8) COS(3*PI/8) COS( 5*PI/8) COS( 7*PI/8) # COS(2*PI/8) COS(6*PI/8) COS(10*PI/8) COS(14*PI/8) # COS(3*PI/8) COS(9*PI/8) COS(15*PI/8) COS(21*PI/8) # # Properties: # # A is generally not symmetric: A' /= A. # # A is "almost" orthogonal. A * A' = a diagonal matrix. # # The entries of A are based on the zeros of the Chebyshev polynomial T(n). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): angle = float ( i * ( 2 * j + 1 ) ) * np.pi / float ( 2 * n ) a[i,j] = np.cos ( angle ) return a def cheby_van3_determinant ( n ): #*****************************************************************************80 # ## cheby_van3_determinant() returns the determinant of the cheby_van3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # import numpy as np determ = r8_mop ( n + 1 ) * np.sqrt ( float ( n ) ** n ) \ / np.sqrt ( 2.0 ** ( n - 1 ) ) return determ def cheby_van3_inverse ( n ): #*****************************************************************************80 # ## cheby_van3_inverse() inverts the cheby_van3 matrix. # # Discussion: # # cheby_van3 is the Chebyshev Vandermonde-like matrix. # # Formula: # # if J == 1 then # A(I,J) = (1/N) * cos ( (I-1/2) * (J-1) * PI / N ) # else if 1 < J then # A(I,J) = (2/N) * cos ( (I-1/2) * (J-1) * PI / N ) # # Example: # # N = 4 # # 1/4 1/2 cos( PI/8) 1/2 cos( 2*PI/8) 1/2 cos( 3*PI/8) # 1/4 1/2 cos(3*PI/8) 1/2 cos( 6*PI/8) 1/2 cos( 9*PI/8) # 1/4 1/2 cos(5*PI/8) 1/2 cos(10*PI/8) 1/2 cos(15*PI/8) # 1/4 1/2 cos(7*PI/8) 1/2 cos(14*PI/8) 1/2 cos(21*PI/8) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): angle = float ( ( 2 * i + 1 ) * j ) * np.pi / float ( 2 * n ) a[i,j] = np.cos ( angle ) / float ( n ) for i in range ( 0, n ): for j in range ( 1, n ): a[i,j] = 2.0 * a[i,j] return a def chow_matrix ( alpha, beta, m, n ): #*****************************************************************************80 # ## chow_matrix() returns the CHOW matrix. # # Discussion: # # By making ALPHA small compared with BETA, the eigenvalues can # all be made very close to BETA, and this is useful as a test # of eigenvalue computing routines. # # Formula: # # if ( I = J ) # A(I,J) = ALPHA + BETA # else if ( J <= I+1 ) then # A(I,J) = ALPHA^(I+1-J) # else # A(I,J) = 0 # # Example: # # ALPHA = 2, BETA = 3, M = 5, N = 5 # # 5 1 0 0 0 # 4 5 1 0 0 # 8 4 5 1 0 # 16 8 4 5 1 # 32 16 8 4 5 # # ALPHA = ALPHA, BETA = BETA, M = 5, N = 5 # # ALPHA+BETA 1 0 0 0 # ALPHA^2 ALPHA+BETA 1 0 0 # ALPHA^3 ALPHA^2 ALPHA+BETA 1 0 # ALPHA^4 ALPHA^3 ALPHA^2 ALPHA+BETA 1 # ALPHA^5 ALPHA^4 ALPHA^3 ALPHA^2 ALPHA+BETA # # Rectangular Properties: # # A is Toeplitz: constant along diagonals. # # A is lower Hessenberg. # # Square Properties: # # A is generally not symmetric: A' /= A. # # If ALPHA is 0.0, then A is singular if and only if BETA is 0.0. # # If BETA is 0.0, then A will be singular if 1 < N. # # If BETA is 0.0 and N = 1, then A will be singular if ALPHA is 0.0. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # For 1 <= I < N-(N+1)/2, # # LAMBDA(I) = BETA + 4 * ALPHA * cos ( i * pi / ( N+2 ) )^2, # # For N-(N+1)/2+1 <= I <= N # # LAMBDA(I) = BETA # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 December 2014 # # Author: # # John Burkardt # # Reference: # # TS Chow, # A class of Hessenberg matrices with known eigenvalues and inverses, # SIAM Review, # Volume 11, Number 3, 1969, pages 391-395. # # Graeme Fairweather, # On the eigenvalues and eigenvectors of a class of Hessenberg matrices, # SIAM Review, # Volume 13, Number 2, 1971, pages 220-221. # # Input: # # real ALPHA, the ALPHA value. A typical value is 1.0. # # real BETA, the BETA value. A typical value is 0.0. # # integer M, N, the number of rows and columns of A. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( ( m, n ) ) for i in range ( 0, m ): for j in range ( 0, n ): if ( i == j - 1 ): a[i,j] = 1.0 elif ( i == j ): a[i,j] = alpha + beta elif ( j + 1 <= i ): a[i,j] = alpha ** ( i + 1 - j ) else: a[i,j] = 0.0 return a def chow_determinant ( alpha, beta, n ): #*****************************************************************************80 # ## chow_determinant() returns the determinant of the CHOW matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 December 2014 # # Author: # # John Burkardt # # Input: # # real ALPHA, the ALPHA value. A typical value is 1.0. # # real BETA, the BETA value. A typical value is 0.0. # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # import numpy as np determ = 1.0 k = n - ( n // 2 ) for i in range ( 1, k + 1 ): angle = float ( i ) * np.pi / float ( n + 2 ) determ = determ * ( beta + 4.0 * alpha * ( np.cos ( angle ) ) ** 2 ) determ = determ * beta ** ( n - k ) return determ def chow_eigen_left ( alpha, beta, n ): #*****************************************************************************80 # ## chow_eigen_left() returns the left eigenvector matrix for the CHOW matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the ALPHA value. A typical value is 1.0. # # real BETA, the BETA value. A typical value is 0.0. # # integer N, the order of the matrix. # # Output: # # real V(N,N), the left eigenvector matrix. # import numpy as np v = np.zeros ( ( n, n ) ) k = n - ( ( n + 1 ) // 2 ) for i in range ( 0, k ): angle = float ( i + 1 ) * np.pi / float ( n + 2 ) for j in range ( 0, n ): v[i,j] = alpha ** ( n - j - 1 ) * 2.0 ** ( n - j - 2 ) \ * ( np.cos ( angle ) ) ** ( n - j ) \ * np.sin ( ( n - j + 1 ) * angle ) / np.sin ( angle ) for i in range ( k, n ): v[i,n-2] = -alpha v[i,n-1] = 1.0 return v def chow_eigen_right ( alpha, beta, n ): #*****************************************************************************80 # ## chow_eigen_right() returns the right eigenvectors of the CHOW matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the ALPHA value. A typical value is 1.0. # # real BETA, the BETA value. A typical value is 0.0. # # integer N, the order of the matrix. # # Output: # # real U(N,N), the right eigenvector matrix. # import numpy as np u = np.zeros ( ( n, n ) ) k = n - ( ( n + 1 ) // 2 ) for j in range ( 0, k ): angle = float ( j + 1 ) * np.pi / float ( n + 2 ) for i in range ( 0, n ): u[i,j] = alpha ** ( i ) * 2.0 ** ( i - 1 ) \ * ( np.cos ( angle ) ) ** ( i - 1 ) \ * np.sin ( float ( i + 2 ) * angle ) / np.sin ( angle ) for j in range ( k, n ): u[0,j] = 1.0 u[1,j] = -alpha return u def chow_eigenvalues ( alpha, beta, n ): #*****************************************************************************80 # ## chow_eigenvalues() returns the eigenvalues of the CHOW matrix. # # Example: # # ALPHA = 2, BETA = 3, N = 5 # # 9.49395943 # 6.10991621 # 3.0 # 3.0 # 3.0 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the ALPHA value. A typical value is 1.0. # # real BETA, the BETA value. A typical value is 0.0. # # integer N, the order of A. # # Output: # # real LAM(N), the eigenvalues of A. # import numpy as np lam = np.zeros ( n ) k = n - ( ( n + 1 ) // 2 ) for i in range ( 0, k ): angle = float ( i + 1 ) * np.pi / float ( n + 2 ) lam[i] = beta + 4.0 * alpha * ( np.cos ( angle ) ) ** 2 for i in range ( k, n ): lam[i] = beta return lam def chow_inverse ( alpha, beta, n ): #*****************************************************************************80 # ## chow_inverse() returns the inverse of the Chow matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the ALPHA value. A typical value is 1.0. # # real BETA, the BETA value. A typical value is 0.0. # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) if ( 0.0 == alpha and beta == 0.0 ): print ( '' ) print ( 'chow_inverse(): Fatal error!' ) print ( ' The Chow matrix is not invertible, because' ) print ( ' ALPHA = 0 and BETA = 0.' ) raise Exception ( 'chow_inverse(): Fatal error!' ) elif ( 0.0 == alpha and beta != 0.0 ): for i in range ( 0, n ): for j in range ( 0, n ): if ( i <= j ): a[i,j] = r8_mop ( j - i ) / beta ** ( j + 1 - i ) elif ( 0.0 != alpha and beta == 0.0 ): if ( 1 < n ): print ( '' ) print ( 'chow_inverse(): Fatal error!' ) print ( ' The Chow matrix is not invertible, because' ) print ( ' BETA = 0 and 1 < N.' ) raise Exception ( 'chow_inverse(): Fatal error!' ) a[0.0] = 1.0 / alpha else: d = np.zeros ( n + 1 ) d[0] = 1.0 d[1] = beta for i in range ( 2, n + 1 ): d[i] = beta * d[i-1] + alpha * beta * d[i-2] dp = np.zeros ( n + 2 ) dp[0] = 1.0 / beta dp[1] = 1.0 dp[2] = alpha + beta for i in range ( 2, n + 1 ): dp[i+1] = d[i] + alpha * d[i-1] for i in range ( 0, n ): for j in range ( 0, i ): a[i,j] = - alpha * ( alpha * beta ) ** ( i - j ) * dp[j] * d[n-1-i] \ / dp[n+1] for j in range ( i, n ): a[i,j] = r8_mop ( i + j ) * dp[i+1] * d[n-j] / ( beta * dp[n+1] ) return a def circulant_matrix ( m, n, x ): #*****************************************************************************80 # ## circulant_matrix() returns the CIRCULANT matrix. # # Formula: # # K = 1 + mod ( J-I, N ) # A(I,J) = X(K) # # Example: # # M = 4, N = 4, X = ( 1, 2, 3, 4 ) # # 1 2 3 4 # 4 1 2 3 # 3 4 1 2 # 2 3 4 1 # # M = 4, N = 5, X = ( 1, 2, 3, 4, 5 ) # # 1 2 3 4 5 # 5 1 2 3 4 # 4 5 1 2 3 # 3 4 5 1 2 # # M = 5, N = 4, X = ( 1, 2, 3, 4 ) # # 1 2 3 4 # 5 1 2 3 # 4 5 1 2 # 3 4 5 1 # 1 2 3 4 # # Discussion: # # Westlake lists the following "special" circulants: # # B2, X = ( T^2, 1, 2, ..., T, T+1, T, T-1, ..., 1 ), # with T = ( N - 2 ) / 2 # # B3, X = ( N+1, 1, 1, ..., 1 ) # # B5, X = ( 1, 2, 3, ..., N ). # # Rectangular Properties: # # The product of two circulant matrices is a circulant matrix. # # The transpose of a circulant matrix is a circulant matrix. # # A circulant matrix C, whose first row is (c1, c2, ..., cn), can be # written as a polynomial in the upshift matrix U: # # C = c1 * I + c2 * U + c3 * U^2 + ... + cn * U^(n-1). # # A is a circulant: each row is shifted once to get the next row. # # Square Properties: # # A is generally not symmetric: A' /= A. # # A is Toeplitz: constant along diagonals. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A commutes with any other circulant matrix. # # A is normal. # # The transpose of A is also a circulant matrix. # # The inverse of A is also a circulant matrix. # # The Fourier matrix is the eigenvector matrix for every circulant matrix. # # Because the Fourier matrix F diagonalizes A, the inverse (or # pseudoinverse, if any LAMBDA is circulant) can be written # # inverse ( A ) = (F*) * 1/LAMBDA * F # # A is symmetric if, for all I, X(I+1) = X(N-I+1). # # If R is an N-th root of unity, that is, R is a complex number such # that R^N = 1, then # # Y = X(1) + X(2)*R + X(3)*R^2 + ... + X(N)*R^(N-1) # # is an eigenvalue of A, with eigenvector # # ( 1, R, R^2, ..., R^(N-1) ) # # and left eigenvector # # ( R^(N-1), R^(N-2), ..., R^2, R, 1 ). # # Although there are exactly N distinct roots of unity, the circulant # may have repeated eigenvalues, because of the behavior of the polynomial. # However, the matrix is guaranteed to have N linearly independent # eigenvectors. # # A is not diagonally dominant. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 01 October 2007 # # Author: # # John Burkardt # # Reference: # # Philip Davis, # Circulant Matrices, # John Wiley, 1979, QA188.D37. # # Robert Gregory, David Karney, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, New York, 1969, page 22, # LC: QA263.G68. # # Joan Westlake, # Test Matrix A24, # A Handbook of Numerical Matrix Inversion and Solution of Linear Equations, # John Wiley, 1968. # # Input: # # integer M, N, the number of rows and columns of A. # # real X(N), the values in the first row of A. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( [ m, n ] ) for i in range ( 0, m ): for j in range ( 0, n ): k = i4_modp ( j - i, n ) a[i,j] = x[k] return a def clement1_matrix ( n ): #*****************************************************************************80 # ## clement1_matrix() returns the CLEMENT1 matrix. # # Formula: # # if ( J = I+1 ) # A(I,J) = sqrt(I*(N-I)) # else if ( I = J+1 ) # A(I,J) = sqrt(J*(N-J)) # else # A(I,J) = 0 # # Example: # # N = 5 # # . sqrt(4) . . . # sqrt(4) . sqrt(6) . . # . sqrt(6) . sqrt(6) . # . . sqrt(6) . sqrt(4) # . . . sqrt(4) . # # Properties: # # A is tridiagonal. # # A is banded, with bandwidth 3. # # Because A is tridiagonal, it has property A (bipartite). # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # The diagonal of A is zero. # # A is singular if N is odd. # # About 64 percent of the entries of the inverse of A are zero. # # The eigenvalues are plus and minus the numbers # # N-1, N-3, N-5, ..., (1 or 0). # # If N is even, # # det ( A ) = (-1)**(N/2) * (N-1) * (N+1)**(N/2) # # and if N is odd, # # det ( A ) = 0 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 December 2014 # # Author: # # John Burkardt # # Reference: # # Paul Clement, # A class of triple-diagonal matrices for test purposes, # SIAM Review, # Volume 1, 1959, pages 50-52. # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j == i + 1 ): a[i,j] = np.sqrt ( float ( ( i + 1 ) * ( n - i - 1 ) ) ) elif ( i == j + 1 ): a[i,j] = np.sqrt ( float ( ( j + 1 ) * ( n - j - 1 ) ) ) else: a[i,j] = 0.0 return a def clement1_determinant ( n ): #*****************************************************************************80 # ## clement1_determinant() returns the determinant of the CLEMENT1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # if ( ( n % 2 ) == 1 ): determ = 0.0 else: determ = 1.0 for i in range ( 1, n, 2 ): determ = determ * float ( ( i ) * ( n - i ) ) if ( ( n // 2 ) % 2 == 1 ): determ = - determ return determ def clement1_inverse ( n ): #*****************************************************************************80 # ## clement1_inverse() returns the inverse of the CLEMENT1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. N must not be odd% # # Output: # # real A(N,N), the matrix. # import numpy as np if ( ( n % 2 ) == 1 ): print ( '' ) print ( 'clement1_inverse(): Fatal error!' ) print ( ' The Clement matrix is singular for odd N.' ) raise Exception ( 'clement1_inverse(): Fatal error!' ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): if ( ( i % 2 ) == 0 ): for j in range ( i, n - 1, 2 ): if ( j == i ): prod = 1.0 / np.sqrt ( float ( ( j + 1 ) * ( n - j - 1 ) ) ) else: prod = - prod \ * np.sqrt ( float ( ( j ) * ( n - j ) ) ) \ / np.sqrt ( float ( ( j + 1 ) * ( n - j - 1 ) ) ) a[i,j+1] = prod a[j+1,i] = prod return a def clement2_matrix ( n, x, y ): #*****************************************************************************80 # ## clement2_matrix() returns the CLEMENT2 matrix. # # Formula: # # if ( J = I + 1 ) then # A(I,J) = X(I) # else if ( I = J + 1 ) then # A(I,J) = Y(J) # else # A(I,J) = 0 # # Example: # # N = 5, X and Y arbitrary: # # . X(1) . . . # Y(1) . X(2) . . # . Y(2) . X(3) . # . . Y(3) . X(4) # . . . Y(4) . # # N = 5, X=(1,2,3,4), Y=(5,6,7,8): # # . 1 . . . # 5 . 2 . . # . 6 . 3 . # . . 7 . 4 # . . . 8 . # # Properties: # # A is generally not symmetric: A' /= A. # # A is tridiagonal. # # Because A is tridiagonal, it has property A (bipartite). # # A is banded, with bandwidth 3. # # The diagonal of A is zero. # # A is singular if N is odd. # # About 64 percent of the entries of the inverse of A are zero. # # If N is even, # # det ( A ) = (-1)^(N/2) * product ( 1 <= I <= N/2 ) # ( X(2*I-1) * Y(2*I-1) ) # # and if N is odd, # # det ( A ) = 0. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 December 2014 # # Author: # # John Burkardt # # Reference: # # Paul Clement, # A class of triple-diagonal matrices for test purposes, # SIAM Review, # Volume 1, 1959, pages 50-52. # # Alan Edelman, Eric Kostlan, # The road from Kac's matrix to Kac's random polynomials. # In Proceedings of the Fifth SIAM Conference on Applied Linear Algebra, # edited by John Lewis, # SIAM, 1994, # pages 503-507. # # Robert Gregory, David Karney, # Example 3.19, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, New York, 1969, page 46, # LC: QA263.G68. # # Olga Taussky, John Todd, # Another look at a matrix of Mark Kac, # Linear Algebra and Applications, # Volume 150, 1991, pages 341-360. # # Input: # # integer N, the order of A. # # real X(N-1), Y(N-1), the first super and # subdiagonals of the matrix A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j == i + 1 ): a[i,j] = x[i] elif ( i == j + 1 ): a[i,j] = y[j] else: a[i,j] = 0.0 return a def clement2_determinant ( n, x, y ): #*****************************************************************************80 # ## clement2_determinant() returns the determinant of the CLEMENT2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N-1), Y(N-1), the first super and # subdiagonals of the matrix A. # # Output: # # real DETERM, the determinant. # if ( ( n % 2 ) == 1 ): determ = 0.0 else: determ = 1.0 for i in range ( 0, n - 1, 2 ): determ = determ * x[i] * y[i] if ( ( n // 2 ) % 2 == 1 ): determ = - determ return determ def clement2_inverse ( n, x, y ): #*****************************************************************************80 # ## clement2_inverse() returns the inverse of the Clement3 matrix. # # Example: # # N = 6, X and Y arbitrary: # # 0 1/Y1 0 -X2/(Y1*Y3) 0 X2*X4/(Y1*Y3*Y5) # 1/X1 0 0 0 0 0 # 0 0 0 1/Y3 0 -X4/(Y3*Y5) # -Y2/(X1*X3) 0 1/X3 0 0 0 # 0 0 0 0 0 1/Y5 # Y2*Y4/(X1*X3*X5) 0 -Y4/(X3*X5) 0 1/X5 0 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 March 2015 # # Author: # # John Burkardt # # Reference: # # Paul Clement, # A class of triple-diagonal matrices for test purposes, # SIAM Review, # Volume 1, 1959, pages 50-52. # # Input: # # integer N, the order of A. N must not be odd% # # real X(N-1), Y(N-1), the first super and # subdiagonals of the matrix A. None of the entries # of X or Y may be zero. # # Output: # # real A(N,N), the matrix. # import numpy as np if ( ( n % 2 ) == 1 ): print ( '' ) print ( 'clement2_inverse(): Fatal error!' ) print ( ' The Clement matrix is singular for odd N.' ) raise Exception ( 'clement2_inverse(): Fatal error!' ) for i in range ( 0, n - 1 ): if ( x[i] == 0.0 ): print ( '' ) print ( 'clement2_inverse(): Fatal error!' ) print ( ' The matrix is singular' ) print ( ' X(I) = 0 for I = %d' % ( i ) ) raise Exception ( 'clement2_inverse(): Fatal error!' ) elif ( y[i] == 0.0 ): print ( '' ) print ( 'clement2_inverse(): Fatal error!' ) print ( ' The matrix is singular' ) print ( ' Y(I) = 0 for I = %d' % ( i ) ) raise Exception ( 'clement2_inverse(): Fatal error!' ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): if ( ( i % 2 ) == 0 ): for j in range ( i, n - 1, 2 ): if ( j == i ): prod1 = 1.0 / y[j] prod2 = 1.0 / x[j] else: prod1 = - prod1 * x[j-1] / y[j] prod2 = - prod2 * y[j-1] / x[j] a[i,j+1] = prod1 a[j+1,i] = prod2 return a def clement3_matrix ( n, x, y ): #*****************************************************************************80 # ## clement3_matrix() returns the Clement3 matrix. # # Formula: # # if ( J = I + 1 ) then # A(I,J) = X(I) # else if ( I = J + 1 ) then # A(I,J) = Y(J) # else # A(I,J) = 0 # # Example: # # N = 5, X and Y arbitrary: # # . X(1) . . . # Y(1) . X(2) . . # . Y(2) . X(3) . # . . Y(3) . X(4) # . . . Y(4) . # # N = 5, X=(1,2,3,4), Y=(5,6,7,8): # # . 1 . . . # 5 . 2 . . # . 6 . 3 . # . . 7 . 4 # . . . 8 . # # Properties: # # A is generally not symmetric: A' /= A. # # The Clement1 and Clement2 matrices are special cases of this one. # # A is tridiagonal. # # Because A is tridiagonal, it has property A (bipartite). # # A is banded, with bandwidth 3. # # The diagonal of A is zero. # # A is singular if N is odd. # # About 64 percent of the entries of the inverse of A are zero. # # If N is even, # # det ( A ) = (-1)^(N/2) * product ( 1 <= I <= N/2 ) # ( X(2*I-1) * Y(2*I-1) ) # # and if N is odd, # # det ( A ) = 0. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 December 2014 # # Author: # # John Burkardt # # Reference: # # Paul Clement, # A class of triple-diagonal matrices for test purposes, # SIAM Review, # Volume 1, 1959, pages 50-52. # # Alan Edelman, Eric Kostlan, # The road from Kac's matrix to Kac's random polynomials. # In Proceedings of the Fifth SIAM Conference on Applied Linear Algebra, # edited by John Lewis, # SIAM, 1994, # pages 503-507. # # Robert Gregory, David Karney, # Example 3.19, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, New York, 1969, page 46, # LC: QA263.G68. # # Olga Taussky, John Todd, # Another look at a matrix of Mark Kac, # Linear Algebra and Applications, # Volume 150, 1991, pages 341-360. # # Input: # # integer N, the order of A. # # real X(N-1), Y(N-1), the first super and # subdiagonals of the matrix A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j == i + 1 ): a[i,j] = x[i] elif ( i == j + 1 ): a[i,j] = y[j] else: a[i,j] = 0.0 return a def clement3_determinant ( n, x, y ): #*****************************************************************************80 # ## clement3_determinant() returns the determinant of the CLEMENT3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N-1), Y(N-1), the first super and # subdiagonals of the matrix A. # # Output: # # real DETERM, the determinant. # if ( ( n % 2 ) == 1 ): determ = 0.0 else: determ = 1.0 for i in range ( 0, n - 1, 2 ): determ = determ * x[i] * y[i] if ( ( n // 2 ) % 2 == 1 ): determ = - determ return determ def colleague_matrix ( n, c ): #*****************************************************************************80 # ## colleague_matrix() returns the COLLEAGUE matrix. # # Discussion: # # The colleague matrix is an analog of the companion matrix, adapted # for use with polynomials represented by a sum of Chebyshev polynomials. # # Let the N-th degree polynomial be defined by # # P(X) = C(N)*T_N(X) + C(N-1)*T_(N-1)(X) + ... + C(1)*T1(X) + C(0)*T0(X) # # where T_I(X) is the I-th Chebyshev polynomial. # # Then the roots of P(X) are the eigenvalues of the colleague matrix A(C): # # 0 1 0 ... 0 0 0 0 ... 0 # 1/2 0 1/2 ... 0 0 0 0 ... 0 # 0 1/2 0 ... 0 - 1/(2*C(N)) * 0 0 0 ... 0 # ... ... ... ... ... ... ... ... ... ... # ... ... ... 0 1/2 ... ... ... ... 0 # ... ... ... 1/2 0 C(0) C(1) C(2) ... C(N-1) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Reference: # # I J Good, # The Colleague Matrix: A Chebyshev Analogue of the Companion Matrix, # The Quarterly Journal of Mathematics, # Volume 12, Number 1, 1961, pages 61-68. # # Input: # # integer N, the order of the matrix. # # real C(0:N), the coefficients of the polynomial. # C(N) should not be zero# # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) if ( n == 1 ): a[0,0] = - c[0] / c[1] else: a[0,1] = 1.0 for i in range ( 1, n ): a[i,i-1] = 0.5 for i in range ( 1, n - 1 ): a[i,i+1] = 0.5 for j in range ( 0, n ): a[n-1,j] = a[n-1,j] - 0.5 * c[j] / c[n] return a def combin_matrix ( alpha, beta, n ): #*****************************************************************************80 # ## combin_matrix() returns the COMBIN matrix. # # Formula: # # If ( I = J ) then # A(I,J) = ALPHA + BETA # else # A(I,J) = BETA # # Example: # # N = 5, ALPHA = 2, BETA = 3 # # 5 3 3 3 3 # 3 5 3 3 3 # 3 3 5 3 3 # 3 3 3 5 3 # 3 3 3 3 5 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # det ( A ) = ALPHA^(N-1) * ( ALPHA + N * BETA ). # # LAMBDA(1:N-1) = ALPHA, # LAMBDA(N) = ALPHA + N * BETA. # # The eigenvector associated with LAMBDA(N) is (1,1,1,...,1)/sqrt(N). # # The other N-1 eigenvectors are simply any (orthonormal) basis # for the space perpendicular to (1,1,1,...,1). # # A is nonsingular if ALPHA /= 0 and ALPHA + N * BETA /= 0. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 October 2007 # # Author: # # John Burkardt # # Reference: # # Robert Gregory, David Karney, # Example 3.25, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, New York, 1969, page 53, # LC: QA263.G68. # # Donald Knuth, # The Art of Computer Programming, # Volume 1, Fundamental Algorithms, Second Edition, # Addison-Wesley, Reading, Massachusetts, 1973, page 36. # # Input: # # real ALPHA, BETA, scalars that define A. # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for j in range ( 0, n ): for i in range ( 0, n ): a[i,j] = beta a[j,j] = a[j,j] + alpha return a def combin_condition ( alpha, beta, n ): #*****************************************************************************80 # ## combin_condition() returns the L1 condition of the COMBIN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 December 2014 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, scalars that define A. # # integer N, the order of the matrix. # # Output: # # real COND, the L1 condition. # a_norm = abs ( alpha + beta ) + ( n - 1 ) * abs ( beta ) b_norm_top = abs ( alpha + ( n - 1 ) * beta ) + ( n - 1 ) * abs ( beta ) b_norm_bot = abs ( alpha * ( alpha + n * beta ) ) b_norm = b_norm_top / b_norm_bot cond = a_norm * b_norm return cond def combin_determinant ( alpha, beta, n ): #*****************************************************************************80 # ## combin_determinant() returns the determinant of the COMBIN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 December 2014 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, scalars that define the matrix. # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # determ = alpha ** ( n - 1 ) * ( alpha + n * beta ) return determ def combin_eigen_right ( alpha, beta, n ): #*****************************************************************************80 # ## combin_eigen_right() returns the right eigenvectors of the COMBIN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, scalars that define A. # # integer N, the order of the matrix. # # Output: # # real X(N,N), the right eigenvectors. # import numpy as np x = np.zeros ( ( n, n ) ) for j in range ( 0, n - 1 ): x[ 0,j] = +1.0 x[j+1,j] = -1.0 j = n - 1 for i in range ( 0, n ): x[i,j] = 1.0 return x def combin_eigenvalues ( alpha, beta, n ): #*****************************************************************************80 # ## combin_eigenvalues() returns the eigenvalues of the COMBIN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, BETA, scalars that define A. # # integer N, the order of A. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n ) for i in range ( 0, n - 1 ): lam[i] = alpha lam[n-1] = alpha + n * beta return lam def combin_inverse ( alpha, beta, n ): #*****************************************************************************80 # ## combin_inverse() returns the inverse of the combinatorial matrix A. # # Formula: # # if ( I = J ) # A(I,J) = (ALPHA+(N-1)*BETA) / (ALPHA*(ALPHA+N*BETA)) # else # A(I,J) = - BETA / (ALPHA*(ALPHA+N*BETA)) # # Example: # # N = 5, ALPHA = 2, BETA = 3 # # 14 -3 -3 -3 -3 # -3 14 -3 -3 -3 # 1/34 * -3 -3 14 -3 -3 # -3 -3 -3 14 -3 # -3 -3 -3 -3 14 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A is Toeplitz: constant along diagonals. # # det ( A ) = 1 / (ALPHA^(N-1) * (ALPHA+N*BETA)). # # A is well defined if ALPHA /= 0.0 and ALPHA+N*BETA /= 0. # # A is also a combinatorial matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Reference: # # Donald Knuth, # The Art of Computer Programming, # Volume 1, Fundamental Algorithms, Second Edition, # Addison-Wesley, Reading, Massachusetts, 1973, page 36. # # Input: # # real ALPHA, BETA, scalars that define the matrix. # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) if ( alpha == 0.0 ): print ( '' ) print ( 'combin_inverse(): Fatal error!' ) print ( ' The entries of the matrix are undefined' ) print ( ' because ALPHA = 0.' ) raise Exception ( 'combin_inverse(): Fatal error!' ) elif ( alpha + n * beta == 0.0 ): print ( '' ) print ( 'combin_inverse(): Fatal error!' ) print ( ' The entries of the matrix are undefined' ) print ( ' because ALPHA+N*BETA is zero.' ) raise Exception ( 'combin_inverse(): Fatal error!' ) bot = alpha * ( alpha + n * beta ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = ( alpha + float ( n - 1 ) * beta ) / bot else: a[i,j] = - beta / bot return a def companion_matrix ( n, x ): #*****************************************************************************80 # ## companion_matrix() returns the companion matrix A for a monic polynomial. # # Formula: # # Let the monic N-th degree polynomial be defined by # # P(t) = t^N + X(N)*t^(N-1) + X(N-1)*t^(N-2) + ... + X(2)*t + X(1) # # Then # # A(1,J) = X(N+1-J) for J=1 to N # A(I,I-1) = 1 for I=2 to N # A(I,J) = 0 otherwise # # A is called the companion matrix of the polynomial P(t), and the # characteristic equation of A is P(t) = 0. # # Matrices of this form are also called Frobenius matrices. # # The determinant of a matrix is unaffected by being transposed, # and only possibly changes sign if the rows are "reflected", so # there are actually many possible ways to write a companion matrix: # # A B C D A 1 0 0 0 1 0 0 0 0 1 0 0 0 1 A # 1 0 0 0 B 0 1 0 0 0 1 0 0 1 0 0 0 1 0 B # 0 1 0 0 C 0 0 1 0 0 0 1 1 0 0 0 1 0 0 C # 0 0 1 0 D 0 0 0 D C B A A B C D 0 0 0 D # # Example: # # N = 5, X = ( 1, 2, 3, 4, 5 ) # # 5 4 3 2 1 # 1 0 0 0 0 # 0 1 0 0 0 # 0 0 1 0 0 # 0 0 0 1 0 # # Properties: # # A is generally not symmetric: A' /= A. # # A is nonsingular if and only if X(1) is nonzero. # # The eigenvalues of A are the roots of P(t) = 0. # # If LAMBDA is an eigenvalue of A, then a corresponding eigenvector is # ( 1, LAMBDA, LAMBDA^2, ..., LAMBDA^(N-1) ). # # If LAMBDA is an eigenvalue of multiplicity 2, then a second # corresponding generalized eigenvector is # # ( 0, 1, 2 * LAMBDA, ..., (N-1)*LAMBDA^(N-2) ). # # For higher multiplicities, repeatedly differentiate with respect to LAMBDA. # # Any matrix with characteristic polynomial P(t) is similar to A. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Reference: # # Gene Golub, Charles Van Loan, # Matrix Computations, second edition, # Johns Hopkins University Press, Baltimore, Maryland, 1989, # section 7.4.6. # # Charles Kenney, Alan Laub, # Controllability and stability radii for companion form systems, # Math. Control Signals Systems, 1 (1988), pages 239-256. # (Gives explicit formulas for the singular values.) # # James Wilkinson, # The Algebraic Eigenvalue Problem, # Oxford University Press, # 1965, page 12. # # Input: # # integer N, the order of A. # # real X(N), the coefficients of the polynomial # which define A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for j in range ( 0, n ): for i in range ( 0, n ): if ( i == 0 ): a[i,j] = x[n-1-j] elif ( i == j + 1 ): a[i,j] = 1.0 else: a[i,j] = 0.0 return a def companion_condition ( n, x ): #*****************************************************************************80 # ## companion_condition() returns the L1 condition of the COMPANION matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N), the polynomial coefficients. # # Output: # # real VALUE, the L1 condition. # a_norm = abs ( x[0] ) for i in range ( 1, n ): a_norm = max ( a_norm, 1.0 + abs ( x[i] ) ) b_norm = 1.0 / abs ( x[0] ) for i in range ( 1, n ): b_norm = max ( b_norm, 1.0 + abs ( x[i] ) / abs ( x[0] ) ) value = a_norm * b_norm return value def companion_determinant ( n, x ): #*****************************************************************************80 # ## companion_determinant() returns the determinant of the COMPANION matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N), the polynomial coefficients. # # Output: # # real DETERM, the determinant. # if ( ( n % 2 ) == 1 ): determ = + x[0] else: determ = - x[0] return determ def companion_inverse ( n, x ): #*****************************************************************************80 # ## companion_inverse() returns the inverse of the COMPANION matrix. # # Example: # # N = 5, X = ( 1, 2, 3, 4, 5 ) # # 0 1 0 0 0 # 0 0 1 0 0 # 0 0 0 1 0 # 0 0 0 0 1 # 1/1 -5/1 -4/1 -3/1 -2/1 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Reference: # # Gene Golub, Charles Van Loan, # Matrix Computations, second edition, # Johns Hopkins University Press, Baltimore, Maryland, 1989, # section 7.4.6. # # Charles Kenney, Alan Laub, # Controllability and stability radii for companion form systems, # Math. Control Signals Systems, # Volume 1, 1988, pages 239-256. # # James Wilkinson, # The Algebraic Eigenvalue Problem, # Oxford University Press, # 1965, page 12. # # Input: # # integer N, the order of the matrix. # # real X(N), the coefficients of the polynomial # which define the matrix. X(1) must not be zero. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, n ): if ( i == n - 1 ): if ( j == 0 ): a[i,j] = 1.0 / x[0] else: a[i,j] = - x[n-j] / x[0] elif ( i == j - 1 ): a[i,j] = 1.0 return a def complete_symmetric_poly ( n, r, x ): #*****************************************************************************80 # ## complete_symmetric_poly() evaluates a complete symmetric polynomial. # # Discussion: # # N\R 0 1 2 3 # +-------------------------------------------------------- # 0 | 1 0 0 0 # 1 | 1 X1 X1^2 X1^3 # 2 | 1 X1+X2 X1^2+X1X2+X2^2 X1^3+X1^2X2+X1X2^2+X2^3 # 3 | 1 X1+X2+X3 ... # # If X = ( 1, 2, 3, 4, 5, ... ) then # # N\R 0 1 2 3 4 ... # +-------------------------------------------------------- # 0 | 1 0 0 0 0 # 1 | 1 1 1 1 1 # 2 | 1 3 7 15 31 # 3 | 1 6 25 90 301 # 4 | 1 10 65 350 1701 # 5 | 1 15 140 1050 6951 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of variables. # 0 <= N. # # integer R, the degree of the polynomial. # 0 <= R. # # real X(N), the value of the variables. # # Output: # # real VALUE, the value of TAU(N,R)(X). # import numpy as np if ( n < 0 ): print ( '' ) print ( 'complete_symmetric_poly(): Fatal error!' ) print ( ' N < 0.' ) raise Exception ( 'complete_symmetric_poly(): Fatal error!' ) if ( r < 0 ): print ( '' ) print ( 'complete_symmetric_poly(): Fatal error!' ) print ( ' R < 0.' ) raise Exception ( 'complete_symmetric_poly(): Fatal error!' ) tau_length = max ( n, r ) + 1 tau = np.zeros ( tau_length ) tau[0] = 1.0 for nn in range ( 0, n ): for rr in range ( 1, r + 1 ): tau[rr] = tau[rr] + x[nn] * tau[rr-1] value = tau[r] return value def complete_symmetric_poly_test ( ): #*****************************************************************************80 # ## complete_symmetric_poly_test() tests complete_symmetric_poly(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 January 2015 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'complete_symmetric_poly_test' ) print ( ' complete_symmetric_poly() evaluates a complete symmetric' ) print ( ' polynomial in a given set of variables X.' ) n = 5 x = np.array ( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) r8vec_print ( n, x, ' Variable vector X:' ) print ( '' ) print ( ' N\\R 0 1 2 3 4 5' ) print ( '' ) for nn in range ( 0, n + 1 ): print ( ' %2d' % ( nn ), end = '' ) for rr in range ( 0, 6 ): value = complete_symmetric_poly ( nn, rr, x ) print ( ' %6d' % ( value ), end = '' ) print ( '' ) return def complex_i_matrix ( ): #*****************************************************************************80 # ## complex_i_matrix() returns the complex_i matrix. # # Discussion: # # This is a 2 by 2 matrix that behaves like the imaginary unit. # # Formula: # # 0 1 # -1 0 # # Properties: # # A is integral: int ( A ) = A. # # A is anti-involutional: A * A = - I # # A * A * A * A = I # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Output: # # real A(2,2): the matrix. # import numpy as np a = np.array ( [ [ 0.0, 1.0 ], \ [ -1.0, 0.0 ] \ ] ) return a def complex_i_determinant ( ): #*****************************************************************************80 # ## complex_i_determinant() returns the determinant of the complex_i matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Output: # # real DETERM, the determinant. # determ = 1.0 return determ def complex_i_inverse ( ): #*****************************************************************************80 # ## complex_i_inverse() returns the inverse of the complex_i matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 March 2015 # # Author: # # John Burkardt # # Output: # # real A(2,2), the matrix. # import numpy as np a = np.array ( [ \ [ 0.0, -1.0 ], \ [ +1.0, 0.0 ] ] ) return a def conex1_matrix ( alpha ): #*****************************************************************************80 # ## conex1_matrix() returns the CONEX1 matrix. # # Discussion: # # The CONEX1 matrix is a counterexample to the LINPACK condition # number estimator RCOND available in the LINPACK routine DGECO. # # Formula: # # 1 -1 -2*ALPHA 0 # 0 1 ALPHA -ALPHA # 0 1 1+ALPHA -1-ALPHA # 0 0 0 ALPHA # # Example: # # ALPHA = 100 # # 1 -1 -200 0 # 0 1 100 -100 # 0 1 101 -101 # 0 0 0 100 # # Properties: # # A is generally not symmetric: A' /= A. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Reference: # # Alan Cline, RK Rew, # A set of counterexamples to three condition number estimators, # SIAM Journal on Scientific and Statistical Computing, # Volume 4, 1983, pages 602-611. # # Input: # # real ALPHA, the scalar defining A. # A common value is 100.0. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.array ( [ [ 1.0, -1.0, -2.0 * alpha, 0.0 ], \ [ 0.0, 1.0, alpha, - alpha ], \ [ 0.0, 1.0, 1.0 + alpha, - 1.0 - alpha ], \ [ 0.0, 0.0, 0.0, alpha ] \ ] ) return a def conex1_condition ( alpha ): #*****************************************************************************80 # ## conex1_condition() returns the L1 condition of the CONEX1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 February 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the scalar defining A. # A common value is 100.0. # # Output: # # real VALUE, the L1 condition. # a_norm = max ( 3.0, 3.0 * abs ( alpha ) + abs ( 1.0 + alpha ) ) v1 = abs ( 1.0 - alpha ) + abs ( 1.0 + alpha ) + 1.0 v2 = 2.0 * abs ( alpha ) + 1.0 v3 = 2.0 + 2.0 / abs ( alpha ) b_norm = max ( v1, max ( v2, v3 ) ) value = a_norm * b_norm return value def conex1_determinant ( alpha ): #*****************************************************************************80 # ## conex1_determinant() returns the determinant of the CONEX1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the scalar defining A. # A common value is 100.0. # # Output: # # real DETERM, the determinant. # determ = alpha return determ def conex1_inverse ( alpha ): #*****************************************************************************80 # ## conex1_inverse() returns the inverse of the CONEX1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the scalar defining A. # A common value is 100.0. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.zeros ( ( 4, 4 ) ) a[0,0] = 1.0 a[0,1] = 1.0 - alpha a[0,2] = alpha a[0,3] = 2.0 a[1,0] = 0.0 a[1,1] = 1.0 + alpha a[1,2] = - alpha a[1,3] = 0.0 a[2,0] = 0.0 a[2,1] = -1.0 a[2,2] = 1.0 a[2,3] = 1.0 / alpha a[3,0] = 0.0 a[3,1] = 0.0 a[3,2] = 0.0 a[3,3] = 1.0 / alpha return a def conex2_matrix ( alpha ): #*****************************************************************************80 # ## conex2_matrix() returns the CONEX2 matrix. # # Discussion: # # CONEX2 is a 3 by 3 LINPACK condition number counterexample. # # Formula: # # 1 1-1/ALPHA^2 -2 # 0 1/ALPHA -1/ALPHA # 0 0 1 # # Example: # # ALPHA = 100 # # 1 0.9999 -2 # 0 0.01 -0.01 # 0 0 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is upper triangular. # # det ( A ) = 1 / ALPHA. # # LAMBDA = ( 1, 1/ALPHA, 1 ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Reference: # # Alan Cline, RK Rew, # A set of counterexamples to three condition number estimators, # SIAM Journal on Scientific and Statistical Computing, # Volume 4, 1983, pages 602-611. # # Input: # # real ALPHA, the scalar defining A. # A common value is 100.0. ALPHA must not be zero. # # Output: # # real A(3,3), the matrix. # import numpy as np if ( alpha == 0.0 ): print ( '' ) print ( 'CONEX2(): Fatal error!' ) print ( ' The input value of ALPHA was zero.' ) raise Exception ( 'CONEX2(): Fatal error!' ) a = np.array ( [ \ [ 1.0, ( alpha * alpha - 1.0 ) / ( alpha * alpha ), -2.0 ], \ [ 0.0, 1.0 / alpha, -1.0 / alpha ], \ [ 0.0, 0.0, 1.0 ] \ ] ) return a def conex2_condition ( alpha ): #*****************************************************************************80 # ## conex2_condition() returns the L1 condition of the CONEX2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 February 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the scalar defining A. # # Output: # # real VALUE, the L1 condition. # c1 = 1.0 c2 = abs ( 1.0 - 1.0 / alpha ** 2 ) + 1.0 / abs ( alpha ) c3 = 3.0 + 1.0 / abs ( alpha ) a_norm = max ( c1, max ( c2, c3 ) ) c1 = 1.0 c2 = abs ( ( 1.0 - alpha * alpha ) / alpha ) + abs ( alpha ) c3 = abs ( ( 1.0 + alpha * alpha ) / alpha ** 2 ) + 2.0 b_norm = max ( c1, max ( c2, c3 ) ) value = a_norm * b_norm return value def conex2_determinant ( alpha ): #*****************************************************************************80 # ## conex2_determinant() returns the determinant of the CONEX2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the scalar defining A. # A common value is 100.0. # # Output: # # real DETERM, the determinant. # determ = 1.0 / alpha return determ def conex2_inverse ( alpha ): #*****************************************************************************80 # ## conex2_inverse() returns the inverse of the CONEX2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, the scalar defining A. # A common value is 100.0. ALPHA must not be zero. # # Output: # # real A(3,3), the matrix. # import numpy as np a = np.zeros ( ( 3, 3 ) ) if ( alpha == 0.0 ): print ( '' ) print ( 'conex2_inverse(): Fatal error!' ) print ( ' The input value of ALPHA was zero.' ) raise Exception ( 'conex2_inverse(): Fatal error!' ) a[0,0] = 1.0 a[0,1] = ( 1.0 - alpha * alpha ) / alpha a[0,2] = ( 1.0 + alpha * alpha ) / alpha ** 2 a[1,0] = 0.0 a[1,1] = alpha a[1,2] = 1.0 a[2,0] = 0.0 a[2,1] = 0.0 a[2,2] = 1.0 return a def conex3_matrix ( n ): #*****************************************************************************80 # ## conex3_matrix() returns the CONEX3 matrix. # # Formula: # # if ( I = J and I < N ) # A(I,J) = 1.0 for 1<=I0 sigma = np.dot(x[1:],x[1:]) if sigma == 0: return (np.zeros(x.shape[0]), 0, x[0]) else: norm_x=math.sqrt(x[0]**2+sigma) v=x.copy() #depending on whether x[0] is positive or negatvie #choose the sign if x[0]<=0: v[0]-=norm_x alpha=+norm_x else: v[0]+=norm_x alpha=-norm_x v/=np.linalg.norm(v) return (v, 2, alpha) def householder_complex(x): #*****************************************************************************80 # ## householder_complex(): Householder transformation for a complex vector. # """(v, tau, alpha) = householder_real(x) Compute a Householder transformation such that (1-tau v v^T) x = alpha e_1 where x and v a complex vectors, tau is 0 or 2, and alpha a complex number (e_1 is the first unit vector) """ import numpy as np import scipy.linalg as la import scipy.sparse as sp import math import cmath import numpy.linalg import numpy.matlib assert x.shape[0]>0 sigma=np.dot(np.conj(x[1:]), x[1:]) if sigma==0: return (np.zeros(x.shape[0]), 0, x[0]) else: norm_x=cmath.sqrt(x[0].conjugate()*x[0]+sigma) v=x.copy() phase=cmath.exp(1j*math.atan2(x[0].imag, x[0].real)) v[0]+=phase*norm_x v/=np.linalg.norm(v) return (v, 2, -phase*norm_x) def skew_tridiagonalize ( A, overwrite_a = False, calc_q = True ): #*****************************************************************************80 # ## skew_tridiagonalize() tridiagonalizes a skew-symmetric matrix. # """ T, Q = skew_tridiagonalize(A, overwrite_a, calc_q=True) or T = skew_tridiagonalize(A, overwrite_a, calc_q=False) Bring a real or complex skew-symmetric matrix (A=-A^T) into tridiagonal form T (with zero diagonal) with a orthogonal (real case) or unitary (complex case) matrix U such that A = Q T Q^T (Note that Q^T and *not* Q^dagger also in the complex case) A is overwritten if overwrite_a=True (default: False), and Q only calculated if calc_q=True (default: True) """ import numpy as np import scipy.linalg as la import scipy.sparse as sp import math import cmath import numpy.linalg import numpy.matlib #Check if matrix is square assert A.shape[0] == A.shape[1] > 0 #Check if it's skew-symmetric assert abs((A+A.T).max())<1e-14 n = A.shape[0] A = np.asarray(A) #the slice views work only properly for arrays #Check if we have a complex data type if np.issubdtype(A.dtype, np.complexfloating): householder = householder_complex elif not np.issubdtype(A.dtype, np.number): raise TypeError("pfaffian() can only work on numeric input") else: householder = householder_real if not overwrite_a: A = A.copy() if calc_q: Q = np.eye(A.shape[0], dtype=A.dtype) for i in range(A.shape[0]-2): #Find a Householder vector to eliminate the i-th column v, tau, alpha = householder(A[i+1:,i]) A[i+1, i] = alpha A[i, i+1] = -alpha A[i+2:, i] = 0 A[i, i+2:] = 0 #Update the matrix block A(i+1:N,i+1:N) w = tau*np.dot(A[i+1:, i+1:], v.conj()) A[i+1:,i+1:]+=np.outer(v,w)-np.outer(w,v) if calc_q: #Accumulate the individual Householder reflections #Accumulate them in the form P_1*P_2*..., which is # (..*P_2*P_1)^dagger y = tau*np.dot(Q[:, i+1:], v) Q[:, i+1:]-=np.outer(y,v.conj()) if calc_q: return (np.asmatrix(A), np.asmatrix(Q)) else: return np.asmatrix(A) def skew_ltl ( A, overwrite_a = False, calc_l = True, calc_P = True ): #*****************************************************************************80 # ## skew_ltl tridiagonalizes a skew-symmetric matrix. # """ T, L, P = skew_ltl(A, overwrite_a, calc_q=True) Bring a real or complex skew-symmetric matrix (A=-A^T) into tridiagonal form T (with zero diagonal) with a lower unit triangular matrix L such that P A P^T= L T L^T A is overwritten if overwrite_a=True (default: False), L and P only calculated if calc_l=True or calc_P=True, respectively (default: True). """ import numpy as np import scipy.linalg as la import scipy.sparse as sp import math import cmath import numpy.linalg import numpy.matlib #Check if matrix is square assert A.shape[0] == A.shape[1] > 0 #Check if it's skew-symmetric assert abs((A+A.T).max())<1e-14 n = A.shape[0] A = np.asarray(A) #the slice views work only properly for arrays if not overwrite_a: A = A.copy() if calc_l: L = np.eye(n, dtype=A.dtype) if calc_P: Pv = np.arange(n) for k in range(n-2): #First, find the largest entry in A[k+1:,k] and #permute it to A[k+1,k] kp = k+1+np.abs(A[k+1:,k]).argmax() #Check if we need to pivot if kp != k+1: #interchange rows k+1 and kp temp = A[k+1,k:].copy() A[k+1,k:] = A[kp,k:] A[kp,k:] = temp #Then interchange columns k+1 and kp temp = A[k:,k+1].copy() A[k:,k+1] = A[k:,kp] A[k:,kp] = temp if calc_l: #permute L accordingly temp = L[k+1,1:k+1].copy() L[k+1,1:k+1] = L[kp,1:k+1] L[kp,1:k+1] = temp if calc_P: #accumulate the permutation matrix temp = Pv[k+1] Pv[k+1] = Pv[kp] Pv[kp] = temp #Now form the Gauss vector if A[k+1,k] != 0.0: tau = A[k+2:,k].copy() tau /= A[k+1,k] #clear eliminated row and column A[k+2:,k] = 0.0 A[k,k+2:] = 0.0 #Update the matrix block A(k+2:,k+2) A[k+2:,k+2:] += np.outer(tau, A[k+2:,k+1]) A[k+2:,k+2:] -= np.outer(A[k+2:,k+1], tau) if calc_l: L[k+2:,k+1] = tau if calc_P: #form the permutation matrix as a sparse matrix P = sp.csr_matrix( (np.ones(n), (np.arange(n), Pv)) ) if calc_l: if calc_P: return (np.asmatrix(A), np.asmatrix(L), P) else: return (np.asmatrix(A), np.asmatrix(L)) else: if calc_P: return (np.asmatrix(A), P) else: return np.asmatrix(A) def pfaffian ( A, overwrite_a = False, method = 'P' ): #*****************************************************************************80 # ## pfaffian() computes the Pfaffian of a skew-symmetric matrix. # """ pfaffian(A, overwrite_a=False, method='P') Compute the Pfaffian of a real or complex skew-symmetric matrix A (A=-A^T). If overwrite_a=True, the matrix A is overwritten in the process. This function uses either the Parlett-Reid algorithm (method='P', default), or the Householder tridiagonalization (method='H') """ import numpy as np import scipy.linalg as la import scipy.sparse as sp import math import cmath import numpy.linalg import numpy.matlib #Check if matrix is square assert A.shape[0] == A.shape[1] > 0 #Check if it's skew-symmetric assert abs((A+A.T).max())<1e-14 #Check that the method variable is appropriately set assert method == 'P' or method == 'H' if method == 'P': return pfaffian_ltl(A, overwrite_a) else: return pfaffian_householder(A, overwrite_a) def pfaffian_ltl ( A, overwrite_a = False ): #*****************************************************************************80 # ## pfaffian_ltl() computes the Pfaffian of a skew-symmetric matrix. # """ pfaffian_ltl(A, overwrite_a=False) Compute the Pfaffian of a real or complex skew-symmetric matrix A (A=-A^T). If overwrite_a=True, the matrix A is overwritten in the process. This function uses the Parlett-Reid algorithm. """ import numpy as np import scipy.linalg as la import scipy.sparse as sp import math import cmath import numpy.linalg import numpy.matlib #Check if matrix is square assert A.shape[0] == A.shape[1] > 0 #Check if it's skew-symmetric assert abs((A+A.T).max())<1e-14 n = A.shape[0] A = np.asarray(A) #the slice views work only properly for arrays #Quick return if possible if n%2==1: return 0 if not overwrite_a: A = A.copy() pfaffian_val = 1.0 for k in range(0, n-1, 2): #First, find the largest entry in A[k+1:,k] and #permute it to A[k+1,k] kp = k+1+np.abs(A[k+1:,k]).argmax() #Check if we need to pivot if kp != k+1: #interchange rows k+1 and kp temp = A[k+1,k:].copy() A[k+1,k:] = A[kp,k:] A[kp,k:] = temp #Then interchange columns k+1 and kp temp = A[k:,k+1].copy() A[k:,k+1] = A[k:,kp] A[k:,kp] = temp #every interchange corresponds to a "-" in det(P) pfaffian_val *= -1 #Now form the Gauss vector if A[k+1,k] != 0.0: tau = A[k,k+2:].copy() tau /= A[k,k+1] pfaffian_val *= A[k,k+1] if k+2 0 #Check if it's skew-symmetric assert abs((A+A.T).max())<1e-14 n = A.shape[0] #Quick return if possible if n%2==1: return 0 #Check if we have a complex data type if np.issubdtype(A.dtype, np.complexfloating): householder=householder_complex elif not np.issubdtype(A.dtype, np.number): raise TypeError("pfaffian() can only work on numeric input") else: householder=householder_real A = np.asarray(A) #the slice views work only properly for arrays if not overwrite_a: A = A.copy() pfaffian_val = 1. for i in range(A.shape[0]-2): #Find a Householder vector to eliminate the i-th column v, tau, alpha = householder(A[i+1:,i]) A[i+1, i] = alpha A[i, i+1] = -alpha A[i+2:, i] = 0 A[i, i+2:] = 0 #Update the matrix block A(i+1:N,i+1:N) w = tau*np.dot(A[i+1:, i+1:], v.conj()) A[i+1:,i+1:]+=np.outer(v,w)-np.outer(w,v) if tau!=0: pfaffian_val *= 1-tau if i%2==0: pfaffian_val *= -alpha pfaffian_val *= A[n-2,n-1] return pfaffian_val def pfaffian_schur ( A, overwrite_a = False ): #*****************************************************************************80 # ## pfaffian_schur() computes the Pfaffian of a real antisymmetric matrix. # """Calculate Pfaffian of a real antisymmetric matrix using the Schur decomposition. (Hessenberg would in principle be faster, but scipy-0.8 messed up the performance for scipy.linalg.hessenberg()). This function does not make use of the skew-symmetry of the matrix A, but uses a LAPACK routine that is coded in FORTRAN and hence faster than python. As a consequence, pfaffian_schur is only slightly slower than pfaffian(). """ import numpy as np import scipy.linalg as la import scipy.sparse as sp import math import cmath import numpy.linalg import numpy.matlib assert np.issubdtype(A.dtype, np.number) and not np.issubdtype(A.dtype, np.complexfloating) assert A.shape[0] == A.shape[1] > 0 assert abs(A + A.T).max() < 1e-14 #Quick return if possible if A.shape[0]%2 == 1: return 0 (t, z) = la.schur(A, output='real', overwrite_a=overwrite_a) l = np.diag(t, 1) return np.prod(l[::2]) * la.det(z) def test_pfaffian ( ): #*****************************************************************************80 # ## test_pfaffian() compares the output of Pfaffian routines and determinant. # import numpy as np import scipy.linalg as la import scipy.sparse as sp import math import cmath import numpy.linalg import numpy.matlib print ( '' ) print ( 'test_pfaffian():' ) print ( ' Compare the output of Pfaffian routines and determinant.' ) # # A is a real antisymmetric matrix. # A = numpy.matlib.rand ( 100, 100 ) A = A - A.T pfa1 = pfaffian ( A ) pfa2 = pfaffian ( A, method='H' ) pfa3 = pfaffian_schur ( A ) deta = numpy.linalg.det ( A ) print ( '' ) print ( ' Real matrix:' ) print ( ' pfaffian(A) = ', pfa1 ) print ( ' pfaffian(A,method=H) = ', pfa2 ) print ( ' pfaffian_schur(A) = ', pfa3 ) print ( '' ) print ( ' pfaffian(A)^2 = ', pfa1**2 ) print ( ' pfaffian(A,method=H)^2 =', pfa2**2 ) print ( ' pfaffian_schur(A)^2 = ', pfa3**2 ) print ( ' numpy.linalg.det(A) = ', deta ) assert abs((pfa1-pfa2)/pfa1) < 1e-13 assert abs((pfa1-pfa3)/pfa1) < 1e-13 assert abs((pfa1**2-deta)/deta) < 1e-13 # # A is a complex skew-symmetric matrix. # A = numpy.matlib.rand(100,100)+1.j*numpy.matlib.rand(100,100) A = A-A.T pfa1 = pfaffian(A) pfa2 = pfaffian(A, method='H') deta = numpy.linalg.det(A) print ( '' ) print ( ' Complex matrix:' ) print ( ' pfaffian(A) = ', pfa1 ) print ( ' pfaffian(A,method=H) = ', pfa2 ) print ( '' ) print ( ' pfaffian(A)^2 = ', pfa1**2 ) print ( ' pfaffian(A,method=H)^2 = ', pfa2**2 ) print ( ' numpy.linalg.det(A) = ', deta ) assert abs((pfa1-pfa2)/pfa1) < 1e-13 assert abs((pfa1**2-deta)/deta) < 1e-13 return def test_decompositions ( ): #*****************************************************************************80 # ## test_decompositions() tests the LTL^T and Householder decompositions # import cmath import math import numpy as np import numpy.linalg import numpy.matlib import scipy.linalg as la import scipy.sparse as sp print ( '' ) print ( 'test_decompositions():' ) print ( ' Test the LTL^T and Householder decompositions.' ) # # Real matrices # A = numpy.matlib.rand(100,100) A = A-A.T T, L, P = skew_ltl(A) assert numpy.linalg.norm(P*A*P.T-L*T*L.T)/numpy.linalg.norm(A) < 1e-13 T, Q = skew_tridiagonalize(A) assert numpy.linalg.norm(A-Q*T*Q.T)/numpy.linalg.norm(A) < 1e-13 # # Complex matrices # A = numpy.matlib.rand(100,100)+1.0j*numpy.matlib.rand(100,100) A = A-A.T T, L, P = skew_ltl(A) assert numpy.linalg.norm(P*A*P.T-L*T*L.T)/numpy.linalg.norm(A) < 1e-13 T, Q = skew_tridiagonalize(A) assert numpy.linalg.norm(A-Q*T*Q.T)/numpy.linalg.norm(A) < 1e-13 return def pfaffian_test ( ): #*****************************************************************************80 # ## pfaffian_test() tests pfaffian(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # print ( '' ) print ( 'pfaffian_test:' ) print ( ' Test pfaffian.' ) test_pfaffian ( ) test_decompositions ( ) return def plu_matrix ( n, pivot ): #*****************************************************************************80 # ## plu_matrix() returns a matrix with known P, L and U factors. # # Example: # # Input: # # N = 5 # PIVOT = ( 1, 3, 3, 5, 5 ) # # Output: # # A: # # 11 12 13 14 15 # 1.375 9.75 43.25 44.75 46.25 # 2.75 25 26.25 27.5 28.75 # 0.34375 2.4375 7.71875 17.625 73.125 # 0.6875 4.875 15.4375 60 61.5625 # # P: # # 1 0 0 0 0 # 0 0 1 0 0 # 0 1 0 0 0 # 0 0 0 0 1 # 0 0 0 1 0 # # L: # # 1 0 0 0 0 # 0.25 1 0 0 0 # 0.125 0.375 1 0 0 # 0.0625 0.1875 0.3125 1 0 # 0.03125 0.09375 0.15625 0.21875 1 # # U: # # 11 12 13 14 15 # 0 22 23 24 25 # 0 0 33 34 35 # 0 0 0 44 45 # 0 0 0 0 55 # # Note: # # The LINPACK routine DGEFA will factor the above A as: # # 11 12 13 14 15 # -0.125 22 23 24 25 # -0.25 -0.375 33 34 35 # -0.03125 -0.09375 -0.15625 44 45 # -0.0625 -0.1875 -0.3125 -0.21875 55 # # and the pivot information in the vector IPVT as: # # ( 1, 3, 3, 5, 5 ). # # The LAPACK routine DGETRF will factor the above A as: # # 11 12 13 14 15 # 0.25 22 23 24 25 # 0.125 0.375 33 34 35 # 0.0625 0.1875 0.3125 44 45 # 0.03125 0.09375 0.15625 0.21875 55 # # and the pivot information in the vector IPIV as: # # ( 1, 3, 3, 5, 5 ). # # Method: # # The L factor will have unit diagonal, and subdiagonal entries # L(I,J) = ( 2 * J - 1 ) / 2^I, which should result in a unique # value for every entry. # # The U factor of A will have entries # U(I,J) = 10 * I + J, which should result in "nice" entries as long # as N < 10. # # The P factor can be deduced by applying the pivoting operations # specified by PIVOT in reverse order to the rows of the identity. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 October 2007 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer PIVOT(N), the list of pivot rows. PIVOT(I) # must be a value between I and N, reflecting the choice of # pivot row on the I-th step. For no pivoting, set PIVOT(I) = I. # # Output: # # real P(N,N), L(N,N), U(N,N), the P, L and U factors # of A, as defined by Gaussian elimination with partial pivoting. # P is a permutation matrix, L is unit lower triangular, and U # is upper trianguler. # # real A(N,N), the matrix. # p, l, u = plu_plu ( n, pivot ) lu = r8mat_mm ( n, n, n, l, u ) a = r8mat_mm ( n, n, n, p, lu ) return a def plu_determinant ( n, pivot ): #*****************************************************************************80 # ## plu_determinant() returns the determinant of the PLU matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer PIVOT(N), the list of pivot rows. PIVOT(I) # must be a value between I and N, reflecting the choice of # pivot row on the I-th step. For no pivoting, set PIVOT(I) = I. # # Output: # # real VALUE, the determinant. # p, l, u = plu_plu ( n, pivot ) value = 1.0 for i in range ( 0, n ): value = value * u[i,i] for i in range ( 0, n ): found = False for i2 in range ( i, n ): if ( p[i2,i] == 1.0 ): found = True if ( i2 != i ): for j in range ( 0, n ): t = p[i2,j] p[i2,j] = p[i,j] p[i,j] = t value = - value if ( not found ): print ( '' ) print ( 'plu_determinant(): Fatal error!' ) print ( ' Permutation matrix is illegal.' ) raise Exception ( 'plu_determinant(): Fatal error!' ) return value def plu_inverse ( n, pivot ): #*****************************************************************************80 # ## plu_inverse() returns the inverse of a PLU matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer PIVOT(N), the list of pivot rows. PIVOT(I) # must be a value between I and N, reflecting the choice of # pivot row on the I-th step. For no pivoting, set PIVOT(I) = I. # # Output: # # real A(N,N), the inverse matrix. # import numpy as np p, l, u = plu_plu ( n, pivot ) 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 plu_plu ( n, pivot ): #*****************************************************************************80 # ## plu_plu() returns the PLU factors of the PLU matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer PIVOT(N), the list of pivot rows. PIVOT(I) # must be a value between I and N, reflecting the choice of # pivot row on the I-th step. For no pivoting, set PIVOT(I) = I. # # Output: # # real P(N,N), L(N,N), U(N,N), the P, L and U factors. # import numpy as np # # Check that the pivot vector is legal. # for i in range ( 0, n ): if ( pivot[i] < i or n - 1 < pivot[i] ): print ( '' ) print ( 'PLU(): Fatal error!' ) print ( ' PIVOT[%d] = %d' % ( i, pivot[i] ) ) print ( ' but must be between %d and %d.' % ( i, n - 1 ) ) raise Exception ( 'PLU(): Fatal error!' ) # # Compute U. # u = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i <= j ): u[i,j] = float ( 10 * ( i + 1 ) + ( j + 1 ) ) # # Compute L. # l = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, i ): l[i,j] = float ( 2 * j + 1 ) / float ( 2 ** ( i + 1 ) ) l[i,i] = 1.0 # # Compute P. # p = np.zeros ( [ n, n ] ) for i in range ( 0, n ): p[i,i] = 1.0 for i in range ( n - 1, -1, -1 ): k = pivot[i] if ( k != i ): for j in range ( 0, n ): t = p[i,j] p[i,j] = p[k,j] p[k,j] = t return p, l, u def poisson_matrix ( nrow, ncol ): #*****************************************************************************80 # ## poisson_matrix() returns the POISSON matrix. # # Formula: # # if ( I = J ) # A(I,J) = 4.0 # elseif ( I = J+1 or I = J-1 or I = J+NROW or I = J-NROW ) # A(I,J) = -1.0 # else # A(I,J) = 0.0 # # Example: # # NROW = NCOL = 3 # # 4 -1 0 | -1 0 0 | 0 0 0 # -1 4 -1 | 0 -1 0 | 0 0 0 # 0 -1 4 | 0 0 -1 | 0 0 0 # ---------------------------- # -1 0 0 | 4 -1 0 | -1 0 0 # 0 -1 0 | -1 4 -1 | 0 -1 0 # 0 0 -1 | 0 -1 4 | 0 0 -1 # ---------------------------- # 0 0 0 | -1 0 0 | 4 -1 0 # 0 0 0 | 0 -1 0 | -1 4 -1 # 0 0 0 | 0 0 -1 | 0 -1 4 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A results from discretizing Poisson's equation with the # 5 point operator on a square mesh of N points. # # A has eigenvalues # # LAMBDA(I,J) = 4 - 2 * COS(I*PI/(N+1)) # - 2 * COS(J*PI/(M+1)), I = 1 to N, J = 1 to M. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Reference: # # Gene Golub, Charles Van Loan, # Matrix Computations, second edition, # Johns Hopkins University Press, Baltimore, Maryland, 1989 # (Section 4.5.4). # # Input: # # integer NROW, NCOL, the number of rows and columns # in the grid. # # Output: # # real A(NROW*NCOL,NROW*NCOL), the matrix. # import numpy as np n = nrow * ncol a = np.zeros ( ( n, n ) ) i = 0 for i1 in range ( 0, nrow ): for j1 in range ( 0, ncol ): if ( 0 < i1 ): j = i - ncol a[i,j] = -1.0 if ( 0 < j1 ): j = i - 1 a[i,j] = -1.0 j = i a[i,j] = 4.0 if ( j1 < ncol - 1 ): j = i + 1 a[i,j] = -1.0 if ( i1 < nrow - 1 ): j = i + ncol a[i,j] = -1.0 i = i + 1 return a def poisson_determinant ( nrow, ncol ): #*****************************************************************************80 # ## poisson_determinant() returns the determinant of the Poisson matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Input: # # integer NROW, NCOL, the number of rows and columns # in the grid. # # Output: # # real VALUE, the determinant. # import numpy as np cr = np.zeros ( nrow ) for i in range ( 0, nrow ): angle = float ( i + 1 ) * np.pi / float ( nrow + 1 ) cr[i] = np.cos ( angle ) cc = np.zeros ( ncol ) for j in range ( 0, ncol ): angle = float ( j + 1 ) * np.pi / float ( ncol + 1 ) cc[j] = np.cos ( angle ) value = 1.0 for i in range ( 0, nrow ): for j in range ( 0, ncol): value = value * ( 4.0 - 2.0 * cr[i] - 2.0 * cc[j] ) return value def poisson_rhs ( nrow, ncol, rhs_num ): #*****************************************************************************80 # ## poisson_rhs() returns the right hand side of a Poisson linear system. # # Discussion: # # The Poisson matrix is associated with an NROW by NCOL rectangular # grid of points. # # Assume that the points are numbered from left to right, bottom to top. # # If the K-th point is in row I and column J, set X = I + J. # # This will be the solution to the linear system. # # The right hand side is easily determined from X. It is 0 for every # interior point. # # Example: # # NROW = 3, NCOL = 3 # # ^ # | 7 8 9 # J 4 5 6 # | 1 2 3 # | # +-----I----> # # Solution vector X = ( 2, 3, 4, 3, 4, 5, 4, 5, 6 ) # # Right hand side B = ( 2, 2, 8, 2, 0, 6, 8, 6, 14 ). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Reference: # # Gene Golub, Charles Van Loan, # Matrix Computations, second edition, # Johns Hopkins University Press, Baltimore, Maryland, 1989 # (Section 4.5.4). # # Input: # # integer NROW, NCOL, the number of rows and columns # in the grid. # # integer RHS_NUM, the number of right hand sides. # # Output: # # real B(NROW*NCOL,RHS_NUM), the right hand side. # import numpy as np n = nrow * ncol b = np.zeros ( ( n, 1 ) ) k = 0 for i in range ( 0, nrow ): for j in range ( 0, ncol ): if ( i == 0 ): b[k,0] = b[k,0] + i + j + 1 if ( j == 0 ): b[k,0] = b[k,0] + i + j + 1 if ( j == ncol - 1 ): b[k,0] = b[k,0] + i + j + 3 if ( i == nrow - 1 ): b[k,0] = b[k,0] + i + j + 3 k = k + 1 return b def poisson_solution ( nrow, ncol, rhs_num ): #*****************************************************************************80 # ## poisson_solution() returns the solution of a Poisson linear system. # # Discussion: # # The Poisson matrix is associated with an NROW by NCOL rectangular # grid of points. # # Assume that the points are numbered from left to right, bottom to top. # # If the K-th point is in row I and column J, set X = I + J. # # This will be the solution to the linear system. # # Example: # # NROW = 3, NCOL = 3 # # ^ # | 7 8 9 # J 4 5 6 # | 1 2 3 # | # +-----I----> # # Solution vector X = ( 2, 3, 4, 3, 4, 5, 4, 5, 6 ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Reference: # # Gene Golub, Charles Van Loan, # Matrix Computations, second edition, # Johns Hopkins University Press, Baltimore, Maryland, 1989 # (Section 4.5.4). # # Input: # # integer NROW, NCOL, the number of rows and columns # in the grid. # # integer RHS_NUM, the number of right hand sides. # # Output: # # real X(NROW*NCOL,RHS_NUM), the solution. # import numpy as np n = nrow * ncol x = np.zeros ( ( n, rhs_num ) ) k = 0 for i in range ( 0, nrow ): for j in range ( 0, ncol ): x[k,0] = i + j + 2 k = k + 1 return x def prime ( n ): #*****************************************************************************80 # ## prime() returns returns any of the first prime_MAX prime numbers. # # Discussion: # # prime_MAX is 1600, and the largest prime stored is 13499. # # Thanks to Bart Vandewoestyne for pointing out a typo, 18 February 2005. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 December 2014 # # Author: # # John Burkardt # # Reference: # # Milton Abramowitz and Irene Stegun, # Handbook of Mathematical Functions, # US Department of Commerce, 1964, pages 870-873. # # Daniel Zwillinger, # CRC Standard Mathematical Tables and Formulae, # 30th Edition, # CRC Press, 1996, pages 95-98. # # Input: # # integer N, the index of the desired prime number. # In general, is should be true that 0 <= N <= prime_MAX. # N = -1 returns prime_MAX, the index of the largest prime available. # N = 0 is legal, returning PRIME = 1. # # Output: # # integer P, the N-th prime. If N is out of range, P # is returned as -1. # prime_max = 1600 prime_vector = ( ( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, \ 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, \ 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, \ 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, \ 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, \ 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, \ 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, \ 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, \ 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, \ 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, \ 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, \ 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, \ 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, \ 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, \ 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, \ 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, \ 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, \ 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, \ 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, \ 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, \ 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, \ 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, \ 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, \ 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, \ 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, \ 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, \ 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, \ 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, \ 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, \ 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, \ 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, \ 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, \ 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, \ 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, \ 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, \ 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, \ 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, \ 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, \ 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, \ 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, \ 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, \ 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, \ 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, \ 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, \ 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, \ 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, \ 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, \ 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, \ 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, \ 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, \ 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, \ 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, \ 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, \ 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, \ 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, \ 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, \ 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, \ 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, \ 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, \ 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, \ 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, \ 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, \ 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, \ 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, \ 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, \ 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, \ 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, \ 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, \ 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, \ 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, \ 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, \ 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, \ 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, \ 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, \ 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, \ 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, \ 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, \ 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, \ 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, \ 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, \ 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, \ 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, \ 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, \ 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, \ 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, \ 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, \ 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, \ 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, \ 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, \ 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, \ 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, \ 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, \ 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, \ 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, \ 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, \ 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, \ 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, \ 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, \ 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, \ 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, \ 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, \ 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, \ 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, \ 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, \ 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, \ 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, \ 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, \ 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, \ 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, \ 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, \ 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, \ 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, \ 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, \ 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, \ 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, \ 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, \ 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, \ 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, \ 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, \ 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, \ 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, \ 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, \ 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973,10007, \ 10009,10037,10039,10061,10067,10069,10079,10091,10093,10099, \ 10103,10111,10133,10139,10141,10151,10159,10163,10169,10177, \ 10181,10193,10211,10223,10243,10247,10253,10259,10267,10271, \ 10273,10289,10301,10303,10313,10321,10331,10333,10337,10343, \ 10357,10369,10391,10399,10427,10429,10433,10453,10457,10459, \ 10463,10477,10487,10499,10501,10513,10529,10531,10559,10567, \ 10589,10597,10601,10607,10613,10627,10631,10639,10651,10657, \ 10663,10667,10687,10691,10709,10711,10723,10729,10733,10739, \ 10753,10771,10781,10789,10799,10831,10837,10847,10853,10859, \ 10861,10867,10883,10889,10891,10903,10909,10937,10939,10949, \ 10957,10973,10979,10987,10993,11003,11027,11047,11057,11059, \ 11069,11071,11083,11087,11093,11113,11117,11119,11131,11149, \ 11159,11161,11171,11173,11177,11197,11213,11239,11243,11251, \ 11257,11261,11273,11279,11287,11299,11311,11317,11321,11329, \ 11351,11353,11369,11383,11393,11399,11411,11423,11437,11443, \ 11447,11467,11471,11483,11489,11491,11497,11503,11519,11527, \ 11549,11551,11579,11587,11593,11597,11617,11621,11633,11657, \ 11677,11681,11689,11699,11701,11717,11719,11731,11743,11777, \ 11779,11783,11789,11801,11807,11813,11821,11827,11831,11833, \ 11839,11863,11867,11887,11897,11903,11909,11923,11927,11933, \ 11939,11941,11953,11959,11969,11971,11981,11987,12007,12011, \ 12037,12041,12043,12049,12071,12073,12097,12101,12107,12109, \ 12113,12119,12143,12149,12157,12161,12163,12197,12203,12211, \ 12227,12239,12241,12251,12253,12263,12269,12277,12281,12289, \ 12301,12323,12329,12343,12347,12373,12377,12379,12391,12401, \ 12409,12413,12421,12433,12437,12451,12457,12473,12479,12487, \ 12491,12497,12503,12511,12517,12527,12539,12541,12547,12553, \ 12569,12577,12583,12589,12601,12611,12613,12619,12637,12641, \ 12647,12653,12659,12671,12689,12697,12703,12713,12721,12739, \ 12743,12757,12763,12781,12791,12799,12809,12821,12823,12829, \ 12841,12853,12889,12893,12899,12907,12911,12917,12919,12923, \ 12941,12953,12959,12967,12973,12979,12983,13001,13003,13007, \ 13009,13033,13037,13043,13049,13063,13093,13099,13103,13109, \ 13121,13127,13147,13151,13159,13163,13171,13177,13183,13187, \ 13217,13219,13229,13241,13249,13259,13267,13291,13297,13309, \ 13313,13327,13331,13337,13339,13367,13381,13397,13399,13411, \ 13417,13421,13441,13451,13457,13463,13469,13477,13487,13499 ) ) if ( n == -1 ): p = prime_max elif ( n == 0 ): p = 1 elif ( n <= prime_max ): p = prime_vector[n-1] else: p = -1 return p def prime_test ( ): #*****************************************************************************80 # ## prime_test() tests prime(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 December 2014 # # Author: # # John Burkardt # print ( '' ) print ( 'prime_test' ) print ( ' prime() returns primes from a table.' ) n = -1 prime_max = prime ( n ) print ( '' ) print ( ' Number of primes stored is %d' % ( prime_max ) ) print ( '' ) print ( ' I Prime(I)' ) print ( '' ) for i in range ( 1, 11 ): print ( ' %4d %6d' % ( i, prime(i) ) ) print ( '' ) for i in range ( prime_max - 10, prime_max + 1 ): print ( ' %4d %6d' % ( i, prime(i) ) ) return def projection_random_determinant ( n, k, key ): #*****************************************************************************80 # ## projection_random_determinant(): determinant of a random projection matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 May 2020 # # Author: # # John Burkardt # # Input: # # integer N: the order of A. # # integer K: the rank of the matrix. # 0 <= k <= n. # # integer KEY: a positive integer that selects the data. # # Output: # # real VALUE: the determinant. # if ( k == n ): value = 1.0 else: value = 0.0 return value def projection_random_matrix ( n, k, key ): #*****************************************************************************80 # ## projection_random_matrix() returns a random projection matrix. # # Properties: # # If k == n, det(A) = 1; Otherwise det(A) = 0. # # The only eigenvalues of A are 0 and 1. # # A*A=A. # # A is symmetric. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 May 2020 # # Author: # # John Burkardt # # Input: # # integer N: the order of A. # # integer K: the rank of the matrix. # 0 <= k <= n. # # integer KEY: a positive integer that selects the data. # # Output: # # real A(N,N), the matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) V = rng.random ( size = [ n, k ] ) VT = np.transpose ( V ) VTV = np.matmul ( VT, V ) VTVinv = np.linalg.inv ( VTV ) A = np.matmul ( np.matmul ( V, VTVinv ), VT ) return A def r8mat_house_axh ( n, a, v ): #*****************************************************************************80 # ## r8mat_house_axh() computes A*H where H is a compact Householder matrix. # # Discussion: # # The Householder matrix H(V) is defined by # # H(V) = I - 2 * v * v' / ( v' * v ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 April 2013 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real A(N,N), the matrix to be postmultiplied. # # real V(N), a vector defining a Householder matrix. # # Output: # # real AH(N,N), the product A*H. # import numpy as np vtv = 0.0 for i in range ( 0, n ): vtv = vtv + v[i] ** 2 ah = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, n ): ah[i,j] = a[i,j] for k in range ( 0, n ): ah[i,j] = ah[i,j] - 2.0 * a[i,k] * v[k] * v[j] / vtv return ah def r8mat_house_axh_test ( rng ): #*****************************************************************************80 # ## r8mat_house_axh_test() tests r8mat_house_axh(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 February 2015 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np n = 5 print ( '' ) print ( 'r8mat_house_axh_test():' ) print ( ' r8mat_house_axh() multiplies a matrix A times a' ) print ( ' compact Householder matrix.' ) r8_lo = -5.0 r8_hi = +5.0 a = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = [ n, n ] ) r8mat_print ( n, n, a, ' Matrix A:' ) # # Request V, the compact form of the Householder matrix H # such that H*A packs column 3 of A. # k = 3 km1 = k - 1 a_col = np.zeros ( n ) for i in range ( 0, n ): a_col[i] = a[i,km1] v = r8vec_house_column ( n, a_col, km1 ) r8vec_print ( n, v, ' Compact vector V so column 3 of H*A is packed:' ) h = r8mat_house_form ( n, v ) r8mat_print ( n, n, h, ' Householder matrix H:' ) # # Compute A*H. # ah = r8mat_house_axh ( n, a, v ) r8mat_print ( n, n, ah, ' Indirect product A*H:' ) # # Compare with a direct calculation. # ah = r8mat_mm ( n, n, n, a, h ) r8mat_print ( n, n, ah, ' Direct product A*H:' ) # # Compute H*A to demonstrate packed column 3: # ha = r8mat_mm ( n, n, n, h, a ) r8mat_print ( n, n, ha, ' H*A should pack column 3:' ) return def r8mat_house_form ( n, v ): #*****************************************************************************80 # ## r8mat_house_form() constructs a Householder matrix from its compact form. # # Discussion: # # H(v) = I - 2 * v * v' / ( v' * v ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 April 2013 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real V(N,1), the vector defining the Householder matrix. # # Output: # # real H(N,N), the Householder matrix. # import numpy as np v_dot_v = 0.0 for i in range ( 0, n ): v_dot_v = v_dot_v + v[i] * v[i] c = - 2.0 / v_dot_v h = np.zeros ( ( n, n ) ) for j in range ( 0, n ): h[j,j] = 1.0 for i in range ( 0, n ): h[i,j] = h[i,j] + c * v[i] * v[j] return h def r8mat_house_form_test ( ): #*****************************************************************************80 # ## r8mat_house_form_test() tests r8mat_house_form(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 February 2015 # # Author: # # John Burkardt # import numpy as np n = 5 v = np.array ( ( 0.0, 0.0, 1.0, 2.0, 3.0 ) ) print ( '' ) print ( 'r8mat_house_form_test' ) print ( ' r8mat_house_form forms a Householder' ) print ( ' matrix from its compact form.' ) r8vec_print ( n, v, ' Compact vector form V:' ) h = r8mat_house_form ( n, v ) r8mat_print ( n, n, h, ' Householder matrix H:' ) return def r8mat_indicator ( m, n ): #*****************************************************************************80 # ## r8mat_indicator() sets up an indicator R8MAT. # # Discussion: # # The value of each entry suggests its location, as in: # # 11 12 13 14 # 21 22 23 24 # 31 32 33 34 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 December 2014 # # Author: # # John Burkardt # # Input: # # integer M, the number of rows of the matrix. # M must be positive. # # integer N, the number of columns of the matrix. # N must be positive. # # Output: # # real TABLE(M,N), the indicator table. # import numpy as np table = np.zeros ( ( m, n ), dtype = np.float64 ) fac = 10 ** ( i4_log_10 ( n ) + 1 ) for i in range ( 0, m ): for j in range ( 0, n ): table[i,j] = fac * ( i + 1 ) + ( j + 1 ) return table def r8mat_indicator_test ( ): #*****************************************************************************80 # ## r8mat_indicator_test() tests r8mat_indicator(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 December 2014 # # Author: # # John Burkardt # print ( '' ) print ( 'r8mat_indicator_test' ) print ( ' r8mat_indicator creates an "indicator" R8MAT.' ) m = 5 n = 4 a = r8mat_indicator ( m, n ) r8mat_print ( m, n, a, ' The indicator matrix:' ) return def r8mat_is_adjacency ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_adjacency() checks whether A is an adjacency matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # bool VALUE, is True if the matrix is an adjacency matrix. # value = True if ( not r8mat_is_square ( m, n, a ) ): value = False return value if ( not r8mat_is_symmetric ( m, n, a ) ): value = False return value if ( not r8mat_is_zero_one ( m, n, a ) ): value = False return value value = True return value def r8mat_is_adjacency_test ( ): #*****************************************************************************80 # ## r8mat_is_adjacency_test() tests r8mat_is_adjacency(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_adjacency_test' ) print ( ' r8mat_is_adjacency reports whether a matrix' ) print ( ' is an adjacency matrix.' ) # # Not square. # m = 5 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, min ( m, n ) ): a[i,i] = 1.0 r8mat_print ( m, n, a, ' Not square matrix:' ) value = r8mat_is_adjacency ( m, n, a ) print ( '' ) print ( ' Adjacency = %s' % ( value ) ) # # Square, but not symmetric. # m = 4 n = 4 a = np.array ( [ \ [ 1, 0, 1, 0 ], \ [ 0, 1, 0, 0 ], \ [ 1, 0, 1, 0 ], \ [ 0, 0, 1, 1 ] ] ) r8mat_print ( m, n, a, ' Not symmetric matrix:' ) value = r8mat_is_adjacency ( m, n, a ) print ( '' ) print ( ' Adjacency = %s' % ( value ) ) # # Square, symmetric, but not zero/one. # m = 4 n = 4 a = np.array ( [ \ [ 1, 0, 2, 0 ], \ [ 0, 1, 0, 0 ], \ [ 2, 0, 1, 1 ], \ [ 0, 0, 1, 1 ] ] ) r8mat_print ( m, n, a, ' Not zero/one matrix:' ) value = r8mat_is_adjacency ( m, n, a ) print ( '' ) print ( ' Adjacency = %s' % ( value ) ) # # Square, symmetric, zero/one. # m = 4 n = 4 a = np.array ( [ \ [ 1, 0, 1, 0 ], \ [ 0, 1, 0, 0 ], \ [ 1, 0, 1, 1 ], \ [ 0, 0, 1, 1 ] ] ) r8mat_print ( m, n, a, ' Adjacency matrix:' ) value = r8mat_is_adjacency ( m, n, a ) print ( '' ) print ( ' Adjacency = %s' % ( value ) ) return def r8mat_is_anticirculant ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_anticirculant() checks whether A is an anticirculant matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the row and column dimensions of # the matrix. M and N must be positive. # # real A(M,N), the matrix. # # Output: # # bool VALUE, is True if the matrix is an # anticirculant matrix. # value = True for i in range ( 1, m ): for j in range ( 0, n ): k = ( ( j + i ) % n ) if ( a[i,j] != a[0,k] ): value = False return value return value def r8mat_is_anticirculant_test ( ): #*****************************************************************************80 # ## r8mat_is_anticirculant_test() tests r8mat_is_anticirculant(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_anticirculant_test' ) print ( ' r8mat_is_anticirculant reports whether a matrix' ) print ( ' is an anticirculant matrix.' ) # # Circulant # m = 4 n = 5 a = np.array ( [ \ [ 0, 1, 2, 3, 4 ], \ [ 4, 0, 1, 2, 3 ], \ [ 3, 4, 0, 1, 2 ], \ [ 2, 3, 4, 0, 1 ] ] ) r8mat_print ( m, n, a, ' Circulant matrix:' ) value = r8mat_is_anticirculant ( m, n, a ) print ( '' ) print ( ' Anticirculant = %s' % ( value ) ) # # Anticirculant. # m = 4 n = 5 a = np.array ( [ \ [ 0, 1, 2, 3, 4 ], \ [ 1, 2, 3, 4, 0 ], \ [ 2, 3, 4, 0, 1 ], \ [ 3, 4, 0, 1, 2 ] ] ) r8mat_print ( m, n, a, ' Anticirculant matrix:' ) value = r8mat_is_anticirculant ( m, n, a ) print ( '' ) print ( ' Anticirculant = %s' % ( value ) ) return def r8mat_is_antipersymmetric ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_antipersymmetric() checks an R8MAT for antipersymmetry. # # Discussion: # # An R8MAT is a matrix of real values. # # A is persymmetric if A(I,J) = -A(N+1-J,N+1-I). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # real ERROR_fROBENIUS, the Frobenius error # in antipersymmetry, which will be 0 if the matrix is exactly # antipersymmetric. # import numpy as np if ( not r8mat_is_square ( m, n, a ) ): error_frobenius = float ( 'Inf' ) return error_frobenius error_frobenius = 0.0 for i in range ( 0, m ): for j in range ( n - 1, -1, -1 ): error_frobenius = error_frobenius + ( a[i,j] + a[n-1-j,m-1-i] ) ** 2 error_frobenius = np.sqrt ( error_frobenius ) return error_frobenius def r8mat_is_antipersymmetric_test ( ): #*****************************************************************************80 # ## r8mat_is_antipersymmetric_test() tests r8mat_is_antipersymmetric(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_antipersymmetric_test' ) print ( ' r8mat_is_antipersymmetric reports whether a matrix' ) print ( ' is antipersymmetric.' ) # # Not square. # m = 5 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, min ( m, n ) ): a[i,i] = 1.0 r8mat_print ( m, n, a, ' Not square matrix:' ) value = r8mat_is_antipersymmetric ( m, n, a ) print ( '' ) print ( ' Antipersymmetric = %g' % ( value ) ) # # Square, but not antipersymmetric. # m = 4 n = 4 a = np.array ( [ \ [ 4, 3,-2, 0 ], \ [-7,-6, 0, 3 ], \ [ 9, 8, 6,-3 ], \ [ 0,-7, 7,-4 ] ] ) r8mat_print ( m, n, a, ' Not antipersymmetric matrix:' ) value = r8mat_is_antipersymmetric ( m, n, a ) print ( '' ) print ( ' Antipersymmetric = %g' % ( value ) ) # # Antipersymmetric. # m = 4 n = 4 a = np.array ( [ \ [ 4, 3, -2, 0 ], \ [ -7, -6, 0, 2 ], \ [ 9, 0, 6, -3 ], \ [ 0, -9, 7, -4 ] ] ) r8mat_print ( m, n, a, ' Antipersymmetric matrix:' ) value = r8mat_is_antipersymmetric ( m, n, a ) print ( '' ) print ( ' Antipersymmetric = %g' % ( value ) ) return def r8mat_is_antisymmetric ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_antisymmetric() checks whether A is an antisymmetric matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # bool r8mat_is_antisymmetric, True if the matrix is antisymmetric. # import numpy as np if ( not r8mat_is_square ( m, n, a ) ): return False t = 0.0 for i in range ( 0, m ): t = t + a[i,i] ** 2 for j in range ( i + 1, n ): t = t + ( a[i,j] + a[j,i] ) ** 2 tol = 0.00001 if ( t < tol ): value = True else: value = False return value def r8mat_is_antisymmetric_test ( ): #*****************************************************************************80 # ## r8mat_is_antisymmetric_test() tests r8mat_is_antisymmetric(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_antisymmetric_test' ) print ( ' r8mat_is_antisymmetric reports whether a matrix' ) print ( ' is antisymmetric.' ) # # Not square. # m = 5 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, min ( m, n ) ): a[i,i] = 1.0 r8mat_print ( m, n, a, ' Not square matrix:' ) value = r8mat_is_antisymmetric ( m, n, a ) print ( '' ) print ( ' Antisymmetric = %s' % ( value ) ) # # Square, but not antisymmetric. # m = 4 n = 4 a = np.array ( [ \ [ 0, 5, 1, -2 ], \ [ -5, 0, 3, 0 ], \ [ 1, -3, 6, 4 ], \ [ 2, 0, -4, 0 ] ] ) r8mat_print ( m, n, a, ' Not antisymmetric matrix:' ) value = r8mat_is_antisymmetric ( m, n, a ) print ( '' ) print ( ' Antisymmetric = %s' % ( value ) ) # # Antisymmetric. # m = 4 n = 4 a = np.array ( [ \ [ 0, 5, -1, -2 ], \ [ -5, 0, 3, 0 ], \ [ 1, -3, 0, 4 ], \ [ 2, 0, -4, 0 ] ] ) r8mat_print ( m, n, a, ' Antisymmetric matrix:' ) value = r8mat_is_antisymmetric ( m, n, a ) print ( '' ) print ( ' Antisymmetric = %s' % ( value ) ) 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 MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer K, the number of eigenvectors. # K is usually 1 or N. # # real A(N,N), the matrix. # # real X(K,N), the K eigenvectors. # # real LAM(K), the K eigenvalues. # # Output: # # real VALUE, the Frobenius norm of X * A - LAM * X. # import numpy as np 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 = np.linalg.norm ( c, 'fro' ) return value def r8mat_is_eigen_left_test ( ): #*****************************************************************************80 # ## r8mat_is_eigen_left_test() tests r8mat_is_eigen_left(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # import numpy as np # # This is the CARRY ( 4.0, 4 ) matrix. # m = 4 n = m a = np.array ( [ \ [ 0.13671875, 0.60546875, 0.25390625, 0.00390625 ], \ [ 0.05859375, 0.52734375, 0.39453125, 0.01953125 ], \ [ 0.01953125, 0.39453125, 0.52734375, 0.05859375 ], \ [ 0.00390625, 0.25390625, 0.60546875, 0.13671875 ] ] ) k = 4 x = np.array ( [ \ [ 1.0, 1.0, 1.0, 1.0 ], \ [ 11.0, 3.0, -1.0, -3.0 ], \ [ 11.0, -3.0, -1.0, 3.0 ], \ [ 1.0, -1.0, 1.0, -1.0 ] ] ) lam = np.array ( [ \ 1.000000000000000, \ 0.250000000000000, \ 0.062500000000000, \ 0.015625000000000 ] ) print ( '' ) print ( 'r8mat_is_eigen_left_test:' ) print ( ' r8mat_is_eigen_left tests the error in the left eigensystem' ) print ( ' A\' * X - X * LAMBDA = 0' ) r8mat_print ( n, n, a, ' Matrix A:' ) r8mat_print ( n, k, x, ' Eigenmatrix X:' ) r8vec_print ( n, lam, ' Eigenvalues LAM:' ) value = r8mat_is_eigen_left ( n, k, a, x, lam ) print ( '' ) print ( ' Frobenius norm of A\'*X-X*LAMBDA is ', value ) return 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 MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # integer K, the number of eigenvectors. # K is usually 1 or N. # # real A(N,N), the matrix. # # real X(N,K), the K eigenvectors. # # 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. # import numpy as np 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 = np.linalg.norm ( c, 'fro' ) return value def r8mat_is_eigen_right_test ( ): #*****************************************************************************80 # ## r8mat_is_eigen_right_test() tests r8mat_is_eigen_right(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # import numpy as np # # This is the CARRY ( 4.0, 4 ) matrix. # m = 4 n = m a = np.array ( [ \ [ 0.13671875, 0.60546875, 0.25390625, 0.00390625 ], \ [ 0.05859375, 0.52734375, 0.39453125, 0.01953125 ], \ [ 0.01953125, 0.39453125, 0.52734375, 0.05859375 ], \ [ 0.00390625, 0.25390625, 0.60546875, 0.13671875 ] ] ) k = 4 x = np.array ( [ \ [ 1.0, 6.0, 11.0, 6.0 ], \ [ 1.0, 2.0, -1.0, -2.0 ], \ [ 1.0, -2.0, -1.0, 2.0 ], \ [ 1.0, -6.0, 11.0, -6.0 ] ] ) lam = np.array ( [ \ 1.000000000000000, \ 0.250000000000000, \ 0.062500000000000, \ 0.015625000000000 ] ) print ( '' ) print ( 'r8mat_is_eigen_right_test():' ) print ( ' r8mat_is_eigen_right() tests the error in the right eigensystem' ) print ( ' A * X - X * LAMBDA = 0' ) r8mat_print ( n, n, a, ' Matrix A:' ) r8mat_print ( n, k, x, ' Eigenmatrix X:' ) r8vec_print ( n, lam, ' Eigenvalues LAM:' ) value = r8mat_is_eigen_right ( n, k, a, x, lam ) print ( '' ) print ( ' Frobenius norm of A*X-X*LAMBDA is %g' % ( value ) ) return def r8mat_is_identity ( n, a ): #*****************************************************************************80 # ## r8mat_is_identity() determines if a matrix is the identity. # # Discussion: # # The routine returns the Frobenius norm of A - I. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 December 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real A(N,N), the matrix. # # Output: # # real ERROR_fROBENIUS, the Frobenius norm # of the difference matrix A - I, which would be exactly zero # if A were the identity matrix. # import numpy as np error_frobenius = 0.0 for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): error_frobenius = error_frobenius + ( a[i,j] - 1.0 ) ** 2 else: error_frobenius = error_frobenius + a[i,j] ** 2 error_frobenius = np.sqrt ( error_frobenius ) return error_frobenius def r8mat_is_identity_test ( ): #*****************************************************************************80 # ## r8mat_is_identity_test() tests r8mat_is_identity(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 December 2015 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_identity_test():' ) print ( ' r8mat_is_identity reports the Frobenius norm difference' ) print ( ' between a given matrix A and the identity matrix.' ) n = 4 a = np.zeros ( [ n, n ] ) r8mat_print ( n, n, a, ' Zero matrix:' ) e = r8mat_is_identity ( n, a ) print ( '' ) print ( ' Difference is %g' % ( e ) ) for i in range ( 0, n ): a[i,i] = 1.0 r8mat_print ( n, n, a, ' Identity matrix:' ) e = r8mat_is_identity ( n, a ) print ( '' ) print ( ' Difference is %g' % ( e ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = a[i,j] + float ( i * j ) / 1000 r8mat_print ( n, n, a, ' Almost identity matrix:' ) e = r8mat_is_identity ( n, a ) print ( '' ) print ( ' Difference is %g' % ( e ) ) return def r8mat_is_integer ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_integer() checks whether A has only integer entries. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # real ERROR_fROBENIUS, the Frobenius norm of the # difference between A and the nearest integer matrix. # import numpy as np error_frobenius = 0.0 for j in range ( 0, n ): for i in range ( 0, m ): t = a[i,j] - np.round ( a[i,j] ) error_frobenius = error_frobenius + t * t error_frobenius = np.sqrt ( error_frobenius ) return error_frobenius def r8mat_is_integer_test ( ): #*****************************************************************************80 # ## r8mat_is_integer_test() tests r8mat_is_integer(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_integer_test()' ) print ( ' r8mat_is_integer reports the Frobenius norm of the' ) print ( ' distance between a matrix A and the nearest integer matrix.' ) m = 5 n = 4 a = maxij_matrix ( m, n ) r8mat_print ( m, n, a, ' MAXIJ matrix:' ) value = r8mat_is_integer ( m, n, a ) print ( '' ) print ( ' Frobenius norm = %g' % ( value ) ) n = 4 a = involutory_matrix ( n ) r8mat_print ( n, n, a, ' INVOLUTORY matrix:' ) value = r8mat_is_integer ( n, n, a ) print ( '' ) print ( ' Frobenius norm = %g' % ( value ) ) return 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 MIT license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real A(N,N), the matrix. # # real B(M,N), the inverse matrix. # # Output: # # real VALUE, the Frobenius norm # of the difference matrices A * B - I and B * A - I. # import numpy as np ab = r8mat_mm ( n, n, n, a, b ) ba = r8mat_mm ( n, n, n, b, a ) id = identity_matrix ( n, n ) value = np.linalg.norm ( ab - id, 'fro' ) + np.linalg.norm ( ba - id, 'fro' ) return value def r8mat_is_inverse_test ( ): #*****************************************************************************80 # ## r8mat_is_inverse_test() tests r8mat_is_inverse(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # import numpy as np # # This is the BODEWIG matrix. # m = 4 n = m a = np.array ( [ \ [ 2.0, 1.0, 3.0, 4.0 ], \ [ 1.0, -3.0, 1.0, 5.0 ], \ [ 3.0, 1.0, 6.0, -2.0 ], \ [ 4.0, 5.0, -2.0, -1.0 ] ] ) b = np.array ( [ \ [ -139.0 / 568.0, 165.0 / 568.0, 79.0 / 568.0, 111.0 / 568.0 ], \ [ 165.0 / 568.0, -155.0 / 568.0, -57.0 / 568.0, -1.0 / 568.0 ], \ [ 79.0 / 568.0, -57.0 / 568.0, 45.0 / 568.0, -59.0 / 568.0 ], \ [ 111.0 / 568.0, -1.0 / 568.0, -59.0 / 568.0, -11.0 / 568.0 ] ] ) print ( '' ) print ( 'r8mat_is_inverse_test():' ) print ( ' r8mat_is_inverse tests the error in a matrix inverse:' ) print ( ' A * B - I = 0' ) print ( ' B * A - I = 0' ) r8mat_print ( m, n, a, ' Matrix A:' ) r8mat_print ( m, n, b, ' Inverse matrix B:' ) value = r8mat_is_inverse ( n, a, b ) print ( '' ) print ( ' Frobenius norm of error is %g' % ( value ) ) return def r8mat_is_llt ( m, n, a, l ): #*****************************************************************************80 # ## r8mat_is_llt() measures the error in a lower triangular Cholesky factorization. # # Discussion: # # This routine simply returns the Frobenius norm of the M x M matrix: # A - L * L' # where L is an M by N lower triangular matrix presumed to be the # Cholesky factor of A. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the matrix dimensions. # # real A(M,M), the matrix. # # real L(M,N), the Cholesky factor. # # Output: # # real VALUE, the Frobenius norm of A-L*L'. # import numpy as np # # D = L * L'. # d = r8mat_mmt ( m, n, m, l, l ) # # Take the norm # value = np.linalg.norm ( a - d, 'fro' ) return value def r8mat_is_llt_test ( ): #*****************************************************************************80 # ## r8mat_is_llt_test() tests r8mat_is_llt(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # import numpy as np m = 4 n = 4 a = np.array ( [ \ [ 2.0, 1.0, 0.0, 0.0 ], \ [ 1.0, 2.0, 1.0, 0.0 ], \ [ 0.0, 1.0, 2.0, 1.0 ], \ [ 0.0, 0.0, 1.0, 2.0 ] ] ) l = np.array ( [ \ [ 1.414213562373095, 0.0, 0.0, 0.0 ], \ [ 0.707106781186547, 1.224744871391589, 0.0, 0.0 ], \ [ 0.0, 0.816496580927726, 1.154700538379251, 0.0 ], \ [ 0.0, 0.0, 0.866025403784439, 1.118033988749895 ] ] ) print ( '' ) print ( 'r8mat_is_llt_test():' ) print ( ' r8mat_is_llt tests the error in a lower triangular Cholesky' ) print ( ' factorization ||A-L*L\'||.' ) r8mat_print ( m, m, a, ' Matrix A:' ) r8mat_print ( m, n, l, ' Factor L:' ) value = r8mat_is_llt ( m, n, a, l ) print ( '' ) print ( ' Frobenius norm of A-L*L\' is %g' % (value ) ) return def r8mat_is_lu ( m, n, a, l, u ): #*****************************************************************************80 # ## r8mat_is_lu() measures the error in an LU factorization. # # Discussion: # # This routine computes the Frobenius norm of # # A - L * U # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 October 2021 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # real L(M,M), U(M,N), the LU factors. # # Output: # # real VALUE, the Frobenius norm of A - L * U. # import numpy as np lu = np.dot ( l, u ) value = np.linalg.norm ( a - lu ) return value def r8mat_is_null_left ( m, n, a, x ): #*****************************************************************************80 # ## r8mat_is_null_left() determines if x is a left null vector of matrix A. # # Discussion: # # The nonzero M vector x is a left null vector of the MxN matrix A if # # x'*A = A'*x = 0 # # If A is a square matrix, then this implies that A is singular. # # If A is a square matrix, this implies that 0 is an eigenvalue of A, # and that x is an associated eigenvector. # # This routine returns 0 if x is exactly a left null vector of A. # # It returns a "huge" value if x is the zero vector. # # Otherwise, it returns the L2 norm of A' * x divided by the L2 norm of x: # # ERROR_l2 = NORM_l2 ( A' * x ) / NORM_l2 ( x ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the row and column dimensions of # the matrix. M and N must be positive. # # real A(M,N), the matrix. # # real X(M), the vector. # # Output: # # real VALUE, the result. # 0.0 indicates that X is exactly a left null vector. # A "huge" value indicates that ||x|| = 0; # Otherwise, the value returned is a relative error ||A'*x||/||x||. # import numpy as np # # X_norm # x_norm = np.linalg.norm ( x ) # # ATX = A'*X # atx = np.dot ( np.transpose ( a ), x ) # # ATX_norm # atx_norm = np.linalg.norm ( atx ) # # Value # value = atx_norm / x_norm return value def r8mat_is_null_left_test ( ): #*****************************************************************************80 # ## r8mat_is_null_left_test() tests r8mat_is_null_left(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 March 2015 # # Author: # # John Burkardt # import numpy as np m = 3 n = 3 a = np.array ( [ \ [ 1.0, 2.0, 3.0 ], \ [ 4.0, 5.0, 6.0 ], \ [ 7.0, 8.0, 9.0 ] ]) x = np.array ( [ 1.0, -2.0, 1.0 ] ) print ( '' ) print ( 'r8mat_is_null_left_test():' ) print ( ' r8mat_is_null_left tests whether the M vector X' ) print ( ' is a left null vector of A, that is, A\'*x=0.' ) r8mat_print ( m, n, a, ' Matrix A:' ) r8vec_print ( m, x, ' Vector X:' ) enorm = r8mat_is_null_left ( m, n, a, x ) print ( '' ) print ( ' Frobenius norm of A\'*x is %g' % ( enorm ) ) return def r8mat_is_null_right ( m, n, a, x ): #*****************************************************************************80 # ## r8mat_is_null_right() determines if x is a right null vector of matrix A. # # Discussion: # # The nonzero N vector x is a right null vector of the MxN matrix A if # # A * x = 0 # # If A is a square matrix, then this implies that A is singular. # # If A is a square matrix, this implies that 0 is an eigenvalue of A, # and that x is an associated eigenvector. # # This routine returns 0 if x is exactly a right null vector of A. # # It returns a "huge" value if x is the zero vector. # # Otherwise, it returns the L2 norm of A * x divided by the L2 norm of x: # # ERROR_l2 = NORM_l2 ( A * x ) / NORM_l2 ( x ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the row and column dimensions of # the matrix. M and N must be positive. # # real A(M,N), the matrix. # # real X(N), the vector. # # Output: # # real VALUE, the result. # 0.0 indicates that X is exactly a null vector. # A "huge" value indicates that ||x|| = 0; # Otherwise, the value returned is a relative error ||A*x||/||x||. # import numpy as np # # X_norm # x_norm = np.linalg.norm ( x ) # # AX = A*X # ax = np.dot ( a, x ) # # AX_norm # ax_norm = np.linalg.norm ( ax ) # # Value # value = ax_norm / x_norm return value def r8mat_is_null_right_test ( ): #*****************************************************************************80 # ## r8mat_is_null_right_test() tests r8mat_is_null_right(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 March 2015 # # Author: # # John Burkardt # import numpy as np m = 3 n = 3 a = np.array ( [ \ [ 1.0, 2.0, 3.0 ], \ [ 4.0, 5.0, 6.0 ], \ [ 7.0, 8.0, 9.0 ] ]) x = np.array ( [ 1.0, -2.0, 1.0 ] ) print ( '' ) print ( 'r8mat_is_null_right_test():' ) print ( ' r8mat_is_null_right tests whether the N vector X' ) print ( ' is a right null vector of A, that is, A*x=0.' ) r8mat_print ( m, n, a, ' Matrix A:' ) r8vec_print ( n, x, ' Vector X:' ) enorm = r8mat_is_null_right ( m, n, a, x ) print ( '' ) print ( ' Frobenius norm of A*x is %g' % ( enorm ) ) return def r8mat_is_orthogonal_column ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_orthogonal_column() checks whether an R8MAT is column orthogonal. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the row and column dimensions of # the matrix. M and N must be positive. # # real A(M,N), the matrix. # # Output: # # real ERROR_fROBENIUS, the sum of the errors. # import numpy as np b = np.dot ( a.transpose ( ), a ) error_frobenius = r8mat_is_identity ( n, b ) return error_frobenius def r8mat_is_orthogonal_column_test ( ): #*****************************************************************************80 # ## r8mat_is_orthogonal_column_test() tests r8mat_is_orthogonal_column(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_orthogonal_column_test():' ) print ( ' r8mat_is_orthogonal_column reports the Frobenius norm difference' ) print ( ' between A\'*A and the MxM identity matrix.' ) nb = 4 key = 123456789 b = orthogonal_random_matrix ( nb, key ) m = 4 n = 4 a = b.copy ( ) r8mat_print ( m, n, a, ' Random 4x4 orthogonal matrix:' ) e = r8mat_is_orthogonal_column ( m, n, a ) print ( '' ) print ( ' Frobenius error = %g' % ( e ) ) m = 4 n = 3 a = b[0:m,0:n] r8mat_print ( m, n, a, ' 3 columns of random 4x4 orthogonal matrix:' ) e = r8mat_is_orthogonal_column ( m, n, a ) print ( '' ) print ( ' Frobenius error = %g' % ( e ) ) m = 3 n = 4 a = b[0:m,0:n] r8mat_print ( m, n, a, ' 3 rows of random 4x4 orthogonal matrix:' ) e = r8mat_is_orthogonal_column ( m, n, a ) print ( '' ) print ( ' Frobenius error = %g' % ( e ) ) return def r8mat_is_orthogonal ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_orthogonal() determines if a matrix is orthogonal. # # Discussion: # # The routine returns the Frobenius norm of A'*A - I. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # real ERROR_fROBENIUS, the Frobenius orthogonality # error, which is zero if the matrix is exactly orthogonal. # import numpy as np if ( m != n ): error_frobenius = float ( 'Inf' ) return error_frobenius b = np.dot ( a.transpose ( ), a ) error_frobenius = r8mat_is_identity ( n, b ) return error_frobenius def r8mat_is_orthogonal_test ( ): #*****************************************************************************80 # ## r8mat_is_orthogonal_test() tests r8mat_is_orthogonal(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_orthogonal_test():' ) print ( ' r8mat_is_orthogonal reports the Frobenius norm difference' ) print ( ' between A\'*A and the identity matrix.' ) n = 4 key = 123456789 a = orthogonal_random_matrix ( n, key ) r8mat_print ( n, n, a, ' Random orthogonal matrix:' ) e = r8mat_is_orthogonal ( n, n, a ) print ( '' ) print ( ' Frobenius error = %g' % ( e ) ) n = 4 a = summation_matrix ( n, n ) r8mat_print ( n, n, a, ' Summation matrix:' ) e = r8mat_is_orthogonal ( n, n, a ) print ( '' ) print ( ' Frobenius error = %g' % ( e ) ) return def r8mat_is_orthogonal_row ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_orthogonal_row() checks whether an R8MAT is row orthogonal. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the row and column dimensions of # the matrix. M and N must be positive. # # real A(M,N), the matrix. # # Output: # # real ERROR_FROBENIUS, the sum of the errors. # import numpy as np b = np.dot ( a, a.transpose ( ) ) error_frobenius = r8mat_is_identity ( m, b ) return error_frobenius def r8mat_is_orthogonal_row_test ( ): #*****************************************************************************80 # ## r8mat_is_orthogonal_row_test() tests r8mat_is_orthogonal_row(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_orthogonal_row_test():' ) print ( ' r8mat_is_orthogonal_row reports the Frobenius norm difference' ) print ( ' between A*A\' and the NxN identity matrix.' ) nb = 4 key = 123456789 b = orthogonal_random_matrix ( nb, key ) m = 4 n = 4 a = b.copy ( ) r8mat_print ( m, n, a, ' Random 4x4 orthogonal matrix:' ) e = r8mat_is_orthogonal_row ( m, n, a ) print ( '' ) print ( ' Frobenius error = %g' % ( e ) ) m = 4 n = 3 a = b[0:m,0:n] r8mat_print ( m, n, a, ' 3 columns of random 4x4 orthogonal matrix:' ) e = r8mat_is_orthogonal_row ( m, n, a ) print ( '' ) print ( ' Frobenius error = %g' % ( e ) ) m = 3 n = 4 a = b[0:m,0:n] r8mat_print ( m, n, a, ' 3 rows of random 4x4 orthogonal matrix:' ) e = r8mat_is_orthogonal_row ( m, n, a ) print ( '' ) print ( ' Frobenius error = %g' % ( e ) ) return def r8mat_is_permutation ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_permutation() checks whether A is a permutation matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # integer IVAL: # -1, the matrix is not square; # -2, the matrix is not a zero-one matrix. # -3, there is a row that does not sum to 1. # -4, there is a column that does not sum to 1. # 1, the matrix is a permutation matrix, # if ( m != n ): ival = -1 return ival jval = r8mat_is_zero_one ( m, n, a ) if ( jval != 1 ): ival = -2 return ival for i in range ( 0, m ): s = 0 for j in range ( 0, n ): if ( a[i,j] == 1 ): s = s + 1 if ( s != 1 ): ival = -3 return ival for j in range ( 0, n ): s = 0 for i in range ( 0, m ): if ( a[i,j] == 1 ): s = s + 1 if ( s != 1 ): ival = -4 return ival ival = 1 return ival def r8mat_is_permutation_test ( ): #*****************************************************************************80 # ## r8mat_is_permutation_test() tests r8mat_is_permutation(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_permutation_test():' ) print ( ' r8mat_is_permutation reports whether A is a permutation matrix.' ) m = 4 n = 4 a = np.zeros ( [ m, n ] ) title = 'Zero matrix' r8mat_print ( m, n, a, title ) ival = r8mat_is_permutation ( m, n, a ) if ( ival == 1 ): print ( '%s is a permutation matrix.' % ( title ) ) else: print ( '%s is NOT a permutation matrix.' % ( title ) ) m = 4 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, m ): a[i,i] = 1.0 title = 'Identity matrix' r8mat_print ( m, n, a, title ) ival = r8mat_is_permutation ( m, n, a ) if ( ival == 1 ): print ( '%s is a permutation matrix.' % ( title ) ) else: print ( '%s is NOT a permutation matrix.' % ( title ) ) m = 4 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, m ): a[i,i] = 2.0 title = '2 * Identity matrix' r8mat_print ( m, n, a, title ) ival = r8mat_is_permutation ( m, n, a ) if ( ival == 1 ): print ( '%s is a permutation matrix.' % ( title ) ) else: print ( '%s is NOT a permutation matrix.' % ( title ) ) m = 4 n = 4 a = np.array ( [\ [0,0,1,0],\ [0,0,0,1],\ [1,0,0,0],\ [0,1,0,0] ]) title = 'M1' r8mat_print ( m, n, a, title ) ival = r8mat_is_permutation ( m, n, a ) if ( ival == 1 ): print ( '%s is a permutation matrix.' % ( title ) ) else: print ( '%s is NOT a permutation matrix.' % ( title ) ) m = 4 n = 4 a = np.array ( [\ [0,0,1,0],\ [0,1,0,1],\ [1,0,0,0],\ [0,0,0,0] ]) title = 'M2' r8mat_print ( m, n, a, title ) ival = r8mat_is_permutation ( m, n, a ) if ( ival == 1 ): print ( '%s is a permutation matrix.' % ( title ) ) else: print ( '%s is NOT a permutation matrix.' % ( title ) ) return def r8mat_is_persymmetric ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_persymmetric() checks an R8MAT for persymmetry. # # Discussion: # # An R8MAT is a matrix of real values. # # A is persymmetric if A(I,J) = A(N+1-J,N+1-I). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # real ERROR_fROBENIUS, the Frobenius error # in persymmetry, which will be 0 if the matrix is exactly # persymmetric. # import numpy as np if ( not r8mat_is_square ( m, n, a ) ): error_frobenius = float ( 'Inf' ) return error_frobenius error_frobenius = 0.0 for i in range ( 0, m ): for j in range ( n - 1, -1, -1 ): error_frobenius = error_frobenius + ( a[i,j] - a[n-1-j,m-1-i] ) ** 2 error_frobenius = np.sqrt ( error_frobenius ) return error_frobenius def r8mat_is_persymmetric_test ( ): #*****************************************************************************80 # ## r8mat_is_persymmetric_test() tests r8mat_is_persymmetric(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_persymmetric_test():' ) print ( ' r8mat_is_persymmetric reports whether a matrix' ) print ( ' is persymmetric.' ) # # Not square. # m = 5 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, min ( m, n ) ): a[i,i] = 1.0 r8mat_print ( m, n, a, ' Not square matrix:' ) value = r8mat_is_persymmetric ( m, n, a ) print ( '' ) print ( ' Persymmetric = %g' % ( value ) ) # # Square, but not persymmetric. # m = 4 n = 4 a = np.array ( [ \ [ 4, 3, 2, 1 ], \ [ 7, 6, 5, 3 ], \ [ 9, 8, 6, 3 ], \ [10, 7, 7, 4 ] ] ) r8mat_print ( m, n, a, ' Not persymmetric matrix:' ) value = r8mat_is_persymmetric ( m, n, a ) print ( '' ) print ( ' Persymmetric = %g' % ( value ) ) # # Persymmetric. # m = 4 n = 4 a = np.array ( [ \ [ 4, 3, 2, 1 ], \ [ 7, 6, 5, 2 ], \ [ 9, 8, 6, 3 ], \ [10, 9, 7, 4 ] ] ) r8mat_print ( m, n, a, ' Persymmetric matrix:' ) value = r8mat_is_persymmetric ( m, n, a ) print ( '' ) print ( ' Persymmetric = %g' % ( value ) ) return 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 MIT license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # 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. # import numpy as np lu = r8mat_mm ( m, m, n, l, u ) plu = r8mat_mm ( m, m, n, p, lu ) value = np.linalg.norm ( a - plu, 'fro' ) return value def r8mat_is_plu_test ( ): #*****************************************************************************80 # ## r8mat_is_plu_test() tests r8mat_is_plu(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # import numpy as np # # This is the CARRY ( 4.0, 4 ) matrix. # m = 4 n = m a = np.array ( [ \ [ 5.0, 7.0, 6.0, 5.0 ], \ [ 7.0, 10.0, 8.0, 7.0 ], \ [ 6.0, 8.0, 10.0, 9.0 ], \ [ 5.0, 7.0, 9.0, 10.0 ] ] ) p = np.array ( [ \ [ 0.0, 0.0, 0.0, 1.0 ], \ [ 1.0, 0.0, 0.0, 0.0 ], \ [ 0.0, 1.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 1.0, 0.0 ] ] ) l = np.array ( [ \ [ 1.0, 0.00, 0.00, 0.00 ], \ [ 0.857142857142857, 1.00, 0.00, 0.00 ], \ [ 0.714285714285714, 0.25, 1.00, 0.00 ], \ [ 0.714285714285714, 0.25, -0.20, 1.00 ] ] ) u = np.array ( [ \ [ 7.00, 10.0, 8.0, 7.00 ], \ [ 0.00, -0.571428571428571, 3.142857142857143, 3.00 ], \ [ 0.00, 0.0, 2.50, 4.25 ], \ [ 0.00, 0.0, 0.0, 0.10 ] ] ) print ( '' ) print ( 'r8mat_is_plu_test():' ) print ( ' r8mat_is_plu tests the error in the P*L*U factorization:' ) print ( ' A - P * L * U = 0' ) r8mat_print ( m, n, a, ' Matrix A:' ) r8mat_print ( m, m, p, ' Permutation P:' ) r8mat_print ( m, m, l, ' Lower triangular L:' ) r8mat_print ( m, n, u, ' Upper triangular U:' ) value = r8mat_is_plu ( m, n, a, p, l, u ) print ( '' ) print ( ' Frobenius norm of A-P*L*U is %g' % ( value ) ) return 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 MIT license. # # Modified: # # 02 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, K, the order of the matrices. # # 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. # import numpy as np # # AX = A*X # ax = r8mat_mm ( m, n, k, a, x ) # # Value = ||AX-B|| # value = np.linalg.norm ( ax - b, 'fro' ) return value def r8mat_is_solution_test ( rng ): #*****************************************************************************80 # ## r8mat_is_solution_test() tests r8mat_is_solution(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 02 March 2015 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np 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. # m = rng.integers ( low = 1, high = 10, endpoint = True ) n = rng.integers ( low = 1, high = 10, endpoint = True ) k = rng.integers ( low = 1, high = 10, endpoint = True ) # # Get a random A. # r8_lo = -5.0 r8_hi = +5.0 a = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = [ m, n ] ) # # Get a random X. # r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = [ n, k ] ) # # 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 ) ) return def r8mat_is_sparse ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_sparse() checks whether a matrix is sparse. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # real FNORM, the number of nonzero entries divided by M * N. # ival = 0 for j in range ( 0, n ): for i in range ( 0, m ): if ( a[i,j] != 0.0 ): ival = ival + 1 fnorm = float ( ival ) / float ( m ) / float ( n ) return fnorm def r8mat_is_sparse_test ( ): #*****************************************************************************80 # ## r8mat_is_sparse_test() tests r8mat_is_sparse(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_sparse_test' ) print ( ' r8mat_is_sparse reports whether a matrix' ) print ( ' is sparse.' ) # # Maximal sparse # m = 3 n = 4 a = np.zeros ( [ m, n ] ) r8mat_print ( m, n, a, ' Zero matrix:' ) value = r8mat_is_sparse ( m, n, a ) print ( '' ) print ( ' Sparseness = %g' % ( value ) ) # # Rather sparse # m = 3 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, min ( m, n ) ): a[i,i] = 1.0 r8mat_print ( m, n, a, ' Identity-like matrix:' ) value = r8mat_is_sparse ( m, n, a ) print ( '' ) print ( ' Sparseness = %g' % ( value ) ) # # Hardly sparse # m = 4 n = 4 a = np.array ( [ \ [ 0, 1, 2, 3 ], \ [ 4, 5, 6, 7 ], \ [ 8, 9, 10, 11 ], \ [ 12, 13, 14, 15 ] ] ) r8mat_print ( m, n, a, ' Hardly sparse:' ) value = r8mat_is_sparse ( m, n, a ) print ( '' ) print ( ' Sparseness = %g' % ( value ) ) return def r8mat_is_square ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_square() checks whether A is square. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # bool r8mat_is_square, is True if the matrix is square. # if ( m == n ): value = True else: value = False return value def r8mat_is_square_test ( ): #*****************************************************************************80 # ## r8mat_is_square_test() tests r8mat_is_square(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_square_test' ) print ( ' r8mat_is_square reports whether a matrix' ) print ( ' is square.' ) # # Not square. # m = 5 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, min ( m, n ) ): a[i,i] = 1.0 r8mat_print ( m, n, a, ' Not square matrix:' ) value = r8mat_is_square ( m, n, a ) print ( '' ) print ( ' Square = %s' % ( value ) ) # # Square. # m = 4 n = 4 a = np.array ( [ \ [ 1, 0, 1, 0 ], \ [ 0, 1, 0, 0 ], \ [ 1, 0, 1, 0 ], \ [ 0, 0, 1, 1 ] ] ) r8mat_print ( m, n, a, ' Square matrix:' ) value = r8mat_is_square ( m, n, a ) print ( '' ) print ( ' Square = %s' % ( value ) ) return def r8mat_is_symmetric ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_symmetric() checks whether A is a symmetric matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # bool r8mat_is_symmetric, is True if the matrix is symmetric. # import numpy as np if ( not r8mat_is_square ( m, n, a ) ): return False t = 0.0 for i in range ( 0, m ): for j in range ( i + 1, n ): t = t + ( a[i,j] - a[j,i] ) ** 2 print ( 'T = %g' % ( t ) ) tol = 0.00001 if ( t < tol ): value = True else: value = False return value def r8mat_is_symmetric_test ( ): #*****************************************************************************80 # ## r8mat_is_symmetric_test() tests r8mat_is_symmetric(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_symmetric_test' ) print ( ' r8mat_is_symmetric reports whether a matrix' ) print ( ' is symmetric.' ) # # Not square. # m = 5 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, min ( m, n ) ): a[i,i] = 1.0 r8mat_print ( m, n, a, ' Not square matrix:' ) value = r8mat_is_symmetric ( m, n, a ) print ( '' ) print ( ' Symmetric = %s' % ( value ) ) # # Square, but not symmetric. # m = 4 n = 4 a = np.array ( [ \ [ 1, 0, 1, 0 ], \ [ 0, 1, 0, 0 ], \ [ 1, 0, 1, 0 ], \ [ 0, 0, 1, 1 ] ] ) r8mat_print ( m, n, a, ' Not symmetric matrix:' ) value = r8mat_is_symmetric ( m, n, a ) print ( '' ) print ( ' Symmetric = %s' % ( value ) ) # # Symmetric. # m = 4 n = 4 a = np.array ( [ \ [ 1, 0, 2, 0 ], \ [ 0, 1, 0, 0 ], \ [ 2, 0, 1, 1 ], \ [ 0, 0, 1, 1 ] ] ) r8mat_print ( m, n, a, ' Symmetric matrix:' ) value = r8mat_is_symmetric ( m, n, a ) print ( '' ) print ( ' Symmetric = %s' % ( value ) ) return def r8mat_is_zero_one ( m, n, a ): #*****************************************************************************80 # ## r8mat_is_zero_one() checks for a zero-one matrix. # # Discussion: # # The routine returns the Frobenius norm of A - I. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 April 2017 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # real A(M,N), the matrix. # # Output: # # bool r8mat_is_zero_one, is True if the matrix is a # zero-one matrix, and False otherwise. # value = True for i in range ( 0, m ): for j in range ( 0, n ): if ( a[i,j] != 0.0 and a[i,j] != 1.0 ): value = False return value return value def r8mat_is_zero_one_test ( ): #*****************************************************************************80 # ## r8mat_is_zero_one_test() tests r8mat_is_zero_one(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 April 2017 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8mat_is_zero_one_test' ) print ( ' r8mat_is_zero_one reports whether a matrix' ) print ( ' only has entries of 0 and 1.' ) m = 5 n = 4 a = np.zeros ( [ m, n ] ) r8mat_print ( m, n, a, ' Zero matrix:' ) value = r8mat_is_zero_one ( m, n, a ) print ( '' ) print ( ' Zero/one = %s' % ( value ) ) m = 5 n = 4 a = np.zeros ( [ m, n ] ) for i in range ( 0, min ( m, n ) ): a[i,i] = 1.0 r8mat_print ( m, n, a, ' Identity matrix:' ) value = r8mat_is_zero_one ( m, n, a ) print ( '' ) print ( ' Zero/one = %s' % ( value ) ) for i in range ( 0, m ): for j in range ( 0, n ): a[i,j] = a[i,j] + float ( i * j ) / 1000 r8mat_print ( m, n, a, ' Almost identity matrix:' ) value = r8mat_is_zero_one ( m, n, a ) print ( '' ) print ( ' Zero/one = %s' % ( value ) ) return def r8mat_l1_inverse ( A ): #*****************************************************************************80 # ## r8mat_l1_inverse() inverts a unit lower triangular R8MAT. # # Discussion: # # A unit lower triangular matrix is a matrix with only 1's on the main # diagonal, and only 0's above the main diagonal. # # The inverse of a unit lower triangular matrix is also # a unit lower triangular matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt. # # Reference: # # Albert Nijenhuis, Herbert Wilf, # Combinatorial Algorithms, # Academic Press, 1978, second edition, # ISBN 0-12-519260-6. # # Input: # # real A(N,N), the unit lower triangular matrix. # # Output: # # real B(N,N), the inverse matrix. # import numpy as np n = np.size ( A, 0 ) B = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i < j ): B[i,j] = 0.0 elif ( j == i ): B[i,j] = 1.0 else: B[i,j] = - np.sum ( A[i,0:i] * B[0:i,j] ) return B def r8mat_l1_inverse_test ( ): #*****************************************************************************80 # ## r8mat_l1_inverse_test() tests r8mat_l1_inverse(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # import numpy as np n = 6 # # Each row of this definition is a COLUMN of the matrix. # A = np.array ( [ \ [ 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], \ [ 2.0, 1.0, 0.0, 0.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 ], \ [ 5.0, 0.0, 3.0, 1.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ], \ [ 75.0, 0.0, 0.0, 6.0, 4.0, 1.0 ] ] ) print ( '' ) print ( 'r8mat_l1_inverse_test' ) print ( ' r8mat_l1_inverse inverts a unit lower triangular matrix.' ) r8mat_print ( n, n, A, ' Matrix A to be inverted:' ) B = r8mat_l1_inverse ( A ) r8mat_print ( n, n, B, ' Inverse matrix B:' ) C = np.matmul ( A, B ) r8mat_print ( n, n, C, ' Product C = A * B:' ) return def r8mat_mm ( n1, n2, n3, a, b ): #*****************************************************************************80 # ## r8mat_mm() multiplies two R8MAT's. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 February 2015 # # Author: # # John Burkardt # # Input: # # integer N1, N2, N3, the order of the matrices. # # real A(N1,N2), B(N2,N3), the matrices to multiply. # # Output: # # real C(N1,N3), the product matrix C = A * B. # import numpy as np c = np.zeros ( ( n1, n3 ) ) for j in range ( 0, n3 ): for i in range ( 0, n1 ): for k in range ( 0, n2 ): c[i,j] = c[i,j] + a[i,k] * b[k,j] return c def r8mat_mm_test ( ): #*****************************************************************************80 # ## r8mat_mm_test() tests r8mat_mm(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 February 2015 # # Author: # # John Burkardt # import numpy as np n1 = 4 n2 = 3 n3 = n1 print ( '' ) print ( 'r8mat_mm_test' ) print ( ' r8mat_mm computes a matrix-matrix product C = A * B;' ) a = np.zeros ( ( n1, n2 ) ) for i in range ( 0, n1 ): for j in range ( 0, n2 ): if ( j == 0 ): a[i,j] = 1.0 elif ( i == 0 ): a[i,j] = 0.0 else: a[i,j] = a[i-1,j-1] + a[i-1,j] b = np.transpose ( a ) c = r8mat_mm ( n1, n2, n3, a, b ) r8mat_print ( n1, n2, a, ' A:' ) r8mat_print ( n2, n3, b, ' B:' ) r8mat_print ( n1, n3, c, ' C = A*B:' ) return def r8mat_mmt ( n1, n2, n3, a, b ): #*****************************************************************************80 # ## r8mat_mmt() multiplies an R8MAT times a transposed R8MAT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Input: # # integer N1, N2, N3, the order of the matrices. # # real A(N1,N2), B(N3,N2), the matrices to multiply. # # Output: # # real C(N1,N3), the product matrix C = A * B'. # import numpy as np c = np.zeros ( ( n1, n3 ) ) for j in range ( 0, n3 ): for i in range ( 0, n1 ): for k in range ( 0, n2 ): c[i,j] = c[i,j] + a[i,k] * b[j,k] return c def r8mat_mmt_test ( ): #*****************************************************************************80 # ## r8mat_mmt_test() tests r8mat_mmt(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 February 2015 # # Author: # # John Burkardt # import numpy as np n1 = 4 n2 = 3 n3 = n1 print ( '' ) print ( 'r8mat_mmt_test' ) print ( ' r8mat_mmt computes a matrix-matrix product C = A * B\';' ) a = np.zeros ( ( n1, n2 ) ) for i in range ( 0, n1 ): for j in range ( 0, n2 ): if ( j == 0 ): a[i,j] = 1.0 elif ( i == 0 ): a[i,j] = 0.0 else: a[i,j] = a[i-1,j-1] + a[i-1,j] b = np.copy ( a ) c = r8mat_mmt ( n1, n2, n3, a, b ) r8mat_print ( n1, n2, a, ' A:' ) r8mat_print ( n3, n2, b, ' B:' ) r8mat_print ( n1, n3, c, ' C = A*B\':' ) return def r8mat_mtm ( n1, n2, n3, a, b ): #*****************************************************************************80 # ## r8mat_mtm() computes A' * B for two R8MAT's. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 March 2015 # # Author: # # John Burkardt # # Input: # # integer N1, N2, N3, the order of the matrices. # # real A(N2,N1), B(N2,N3), the matrices to multiply. # # Output: # # real C(N1,N3), the product matrix C = A' * B. # import numpy as np c = np.zeros ( ( n1, n3 ) ) for j in range ( 0, n3 ): for i in range ( 0, n1 ): for k in range ( 0, n2 ): c[i,j] = c[i,j] + a[k,i] * b[k,j] return c def r8mat_mtm_test ( ): #*****************************************************************************80 # ## r8mat_mtm_test() tests r8mat_mtm(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 March 2015 # # Author: # # John Burkardt # import numpy as np n1 = 4 n2 = 3 n3 = n1 print ( '' ) print ( 'r8mat_mtm_test' ) print ( ' r8mat_mtm computes a matrix-matrix product C = A\' * B;' ) a = np.zeros ( ( n2, n1 ) ) for i in range ( 0, n2 ): for j in range ( 0, n1 ): if ( j == 0 ): a[i,j] = 1.0 elif ( i == 0 ): a[i,j] = 0.0 else: a[i,j] = a[i-1,j-1] + a[i-1,j] b = a c = r8mat_mtm ( n1, n2, n3, a, b ) r8mat_print ( n2, n1, a, ' A:' ) r8mat_print ( n2, n3, b, ' B:' ) r8mat_print ( n1, n3, c, ' C = A\'*B:' ) return def r8mat_print ( m, n, a, title ): #*****************************************************************************80 # ## r8mat_print() prints an R8MAT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 May 2020 # # Author: # # John Burkardt # # Input: # # integer M, the number of rows in A. # # integer N, the number of columns in A. # # real A(M,N), the matrix. # # string TITLE, a title. # r8mat_print_some ( m, n, a, 0, 0, m - 1, n - 1, title ) return def r8mat_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ): #*****************************************************************************80 # ## r8mat_print_some() prints out a portion of an R8MAT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 May 2020 # # Author: # # John Burkardt # # Input: # # integer M, N, the number of rows and columns of the matrix. # # real A(M,N), an M by N matrix to be printed. # # integer ILO, JLO, the first row and column to print. # # integer IHI, JHI, the last row and column to print. # # 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 r8mat_u1_inverse ( A ): #*****************************************************************************80 # ## r8mat_u1_inverse() inverts a unit upper triangular R8MAT. # # Discussion: # # A unit upper triangular matrix is a matrix with only 1's on the main # diagonal, and only 0's below the main diagonal. # # The inverse of a unit upper triangular matrix is also # a unit upper triangular matrix. # # This routine can invert a matrix in place, that is, with no extra # storage. If the matrix is stored in A, then the call # # call r8mat_u1_inverse ( n, a, a ) # # will result in A being overwritten by its inverse. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt. # # Reference: # # Albert Nijenhuis, Herbert Wilf, # Combinatorial Algorithms, # Academic Press, 1978, second edition, # ISBN 0-12-519260-6. # # Input: # # real A(N,N), the unit upper triangular matrix. # # Output: # # real B(N,N), the inverse matrix. # import numpy as np n = np.size ( A, 0 ) B = np.zeros ( [ n, n ] ) for j in range ( n - 1, -1, -1 ): for i in range ( n - 1, -1, -1 ): if ( j < i ): B[i,j] = 0.0 elif ( i == j ): B[i,j] = 1.0 else: B[i,j] = - np.sum ( A[i,i+1:j+1] * B[i+1:j+1,j] ) return B def r8mat_u1_inverse_test ( ): #*****************************************************************************80 # ## r8mat_u1_inverse_test() tests r8mat_u1_inverse(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # import numpy as np n = 6 # # Each row of this definition is a COLUMN of the matrix. # A = np.array ( [ \ [ 1.0, 2.0, 0.0, 5.0, 0.0, 75.0 ], \ [ 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 1.0, 3.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 0.0, 1.0, 0.0, 6.0 ], \ [ 0.0, 0.0, 0.0, 0.0, 1.0, 4.0 ], \ [ 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] ] ) print ( '' ) print ( 'r8mat_u1_inverse_test' ) print ( ' r8mat_u1_inverse inverts a unit upper triangular matrix.' ) r8mat_print ( n, n, A, ' Input matrix A' ) B = r8mat_u1_inverse ( A ) r8mat_print ( n, n, B, ' Inverse matrix B:' ) C = np.matmul ( A, B ) r8mat_print ( n, n, C, ' Product C = A * B:' ) return def r8_mop ( i ): #*****************************************************************************80 # ## r8_mop() returns the I-th power of -1 as an R8 value. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 01 June 2013 # # Author: # # John Burkardt # # Input: # # integer I, the power of -1. # # Output: # # real r8_mop, the I-th power of -1. # if ( ( i % 2 ) == 0 ): value = + 1.0 else: value = - 1.0 return value def r8_mop_test ( rng ): #*****************************************************************************80 # ## r8_mop_test() tests r8_mop(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 December 2014 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'r8_mop_test' ) print ( ' r8_mop evaluates (-1.0)^I4 as an R8.' ) print ( '' ) print ( ' I4 r8_mop(I4)' ) print ( '' ) for test in range ( 0, 10 ): i4 = rng.integers ( low = -100, high = +100, endpoint = True ) r8 = r8_mop ( i4 ) print ( ' %4d %4.1f' % ( i4, r8 ) ) return def r8poly_degree ( m, a ): #*****************************************************************************80 # ## r8poly_degree() returns the degree of a polynomial. # # Discussion: # # The degree of a polynomial is the index of the highest power # of X with a nonzero coefficient. # # The degree of a constant polynomial is 0. The degree of the # zero polynomial is debatable, but this routine returns the # degree as 0. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 January 2015 # # Author: # # John Burkardt # # Input: # # integer M, the nominal degree of A. # # real A(M+1), the coefficients of the polynomials. # # Output: # # integer VALUE, the degree of A. # value = m while ( 0 < value ): if ( a[value] != 0.0 ): break value = value - 1 return value def r8poly_degree_test ( ): #*****************************************************************************80 # ## r8poly_degree_test() tests r8poly_degree(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 January 2015 # # Author: # # John Burkardt # import numpy as np c1 = np.array ( [ 1.0, 2.0, 3.0, 4.0 ] ) c2 = np.array ( [ 1.0, 2.0, 3.0, 0.0 ] ) c3 = np.array ( [ 1.0, 2.0, 0.0, 4.0 ] ) c4 = np.array ( [ 1.0, 0.0, 0.0, 0.0 ] ) c5 = np.array ( [ 0.0, 0.0, 0.0, 0.0 ] ) print ( '' ) print ( 'r8poly_degree_test' ) print ( ' r8poly_degree determines the degree of an R8POLY.' ) m = 3 r8poly_print ( m, c1, ' The R8POLY:' ) d = r8poly_degree ( m, c1 ) print ( ' Dimensioned degree = %d, Actual degree = %d' % ( m, d ) ) r8poly_print ( m, c2, ' The R8POLY:' ) d = r8poly_degree ( m, c2 ) print ( ' Dimensioned degree = %d, Actual degree = %d' % ( m, d ) ) r8poly_print ( m, c3, ' The R8POLY:' ) d = r8poly_degree ( m, c3 ) print ( ' Dimensioned degree = %d, Actual degree = %d' % ( m, d ) ) r8poly_print ( m, c4, ' The R8POLY:' ) d = r8poly_degree ( m, c4 ) print ( ' Dimensioned degree = %d, Actual degree = %d' % ( m, d ) ) r8poly_print ( m, c5, ' The R8POLY:' ) d = r8poly_degree ( m, c5 ) print ( ' Dimensioned degree = %d, Actual degree = %d' % ( m, d ) ) print ( '' ) print ( 'r8poly_degree_test:' ) print ( ' Normal end of execution.' ) return def r8poly_print ( m, a, title ): #*****************************************************************************80 # ## r8poly_print() prints out a polynomial. # # Discussion: # # The power sum form is: # # p(x) = a(0) + a(1) * x + ... + a(m-1) * x^(m-1) + a(m) * x^(m) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 July 2015 # # Author: # # John Burkardt # # Input: # # integer M, the nominal degree of the polynomial. # # real A[0:M], the polynomial coefficients. # A[0] is the constant term and # A[M] is the coefficient of X^M. # # string TITLE, a title. # if ( 0 < len ( title ) ): print ( '' ) print ( title ) print ( '' ) if ( a[m] < 0.0 ): plus_minus = '-' else: plus_minus = ' ' mag = abs ( a[m] ) if ( 2 <= m ): print ( ' p(x) = %c %g * x^%d' % ( plus_minus, mag, m ) ) elif ( m == 1 ): print ( ' p(x) = %c %g * x' % ( plus_minus, mag ) ) elif ( m == 0 ): print ( ' p(x) = %c %g' % ( plus_minus, mag ) ) for i in range ( m - 1, -1, -1 ): if ( a[i] < 0.0 ): plus_minus = '-' else: plus_minus = '+' mag = abs ( a[i] ) if ( mag != 0.0 ): if ( 2 <= i ): print ( ' %c %g * x^%d' % ( plus_minus, mag, i ) ) elif ( i == 1 ): print ( ' %c %g * x' % ( plus_minus, mag ) ) elif ( i == 0 ): print ( ' %c %g' % ( plus_minus, mag ) ) def r8poly_print_test ( ): #*****************************************************************************80 # ## r8poly_print_test() tests r8poly_print(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 January 2015 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8poly_print_test' ) print ( ' r8poly_print prints an R8POLY.' ) m = 5 c = np.array ( [ 12.0, -3.4, 56.0, 0.0, 0.78, 9.0 ] ) r8poly_print ( m, c, ' The R8POLY:' ) return def r8_rise ( x, n ): #*****************************************************************************80 # ## r8_rise() computes the rising factorial function [X]^N. # # Discussion: # # [X]^N = X * ( X + 1 ) * ( X + 2 ) * ... * ( X + N - 1 ). # # Note that the number of ways of arranging N objects in M ordered # boxes is [M]^N. (Here, the ordering of the objects in each box matters). # Thus, 2 objects in 2 boxes have the following 6 possible arrangements: # # -|12, 1|2, 12|-, -|21, 2|1, 21|-. # # Moreover, the number of non-decreasing maps from a set of # N to a set of M ordered elements is [M]^N / N!. Thus the set of # nondecreasing maps from (1,2,3) to (a,b,c,d) is the 20 elements: # # aaa, abb, acc, add, aab, abc, acd, aac, abd, aad # bbb, bcc, bdd, bbc, bcd, bbd, ccc, cdd, ccd, ddd. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 July 2014 # # Author: # # John Burkardt # # Input: # # real X, the argument of the rising factorial function. # # integer N, the order of the rising factorial function. # If N = 0, RISE = 1, if N = 1, RISE = X. Note that if N is # negative, a "falling" factorial will be computed. # # Output: # # real VALUE, the value of the rising factorial function. # value = 1.0 arg = x if ( 0 < n ): for i in range ( 0, n ): value = value * arg arg = arg + 1.0 elif ( n < 0 ): for i in range ( n, 0 ): value = value * arg arg = arg - 1.0 return value def r8_rise_test ( ): #*****************************************************************************80 # ## r8_rise_test() tests r8_rise(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 December 2014 # # Author: # # John Burkardt # print ( '' ) print ( 'r8_rise_test' ) print ( ' r8_rise evaluates the rising factorial Rise(X,N).' ) print ( '' ) print ( ' X N Exact Computed' ) print ( '' ) n_data = 0 while ( True ): n_data, x, n, f1 = r8_rise_values ( n_data ) if ( n_data == 0 ): break f2 = r8_rise ( x, n ) print ( ' %8.4g %4d %24.16g %24.16g' % ( x, n, f1, f2 ) ) return def r8_rise_values ( n_data ): #*****************************************************************************80 # ## r8_rise_values() returns values of the rising factorial function. # # Discussion: # # The rising factorial function is sometimes symbolized by (m)_n. # # The definition is # # (m)_n = (m-1+n)! / (m-1)! # = ( m ) * ( m + 1 ) * ( m + 2 ) \ * ( m - 1 + n ) # = Gamma ( m + n ) / Gamma ( m ) # # We assume 0 <= N <= M. # # In Mathematica, the function can be evaluated by: # # Pochhammer[m,n] # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 December 2014 # # Author: # # John Burkardt # # Reference: # # Milton Abramowitz and Irene Stegun, # Handbook of Mathematical Functions, # US Department of Commerce, 1964. # # Stephen Wolfram, # The Mathematica Book, # Fourth Edition, # Wolfram Media / Cambridge University Press, 1999. # # Input: # # integer N_DATA. The user sets N_DATA to 0 before the first call. # # Output: # # integer N_DATA. On each call, the routine increments N_DATA by 1, and # returns the corresponding data; when there is no more data, the # output value of N_DATA will be 0 again. # # real X, integer N, the arguments of the function. # # real F, the value of the function. # import numpy as np n_max = 15 f_vec = np.array ( [ 1680.000000000000, \ 1962.597656250000, \ 2279.062500000000, \ 2631.972656250000, \ 3024.000000000000, \ 1.000000000000000, \ 7.500000000000000, \ 63.75000000000000, \ 605.6250000000000, \ 6359.062500000000, \ 73129.21875000000, \ 914115.2343750000, \ 1.234055566406250E+07, \ 1.789380571289063E+08, \ 2.773539885498047E+09 ] ) n_vec = np.array ( [ 4, \ 4, \ 4, \ 4, \ 4, \ 0, \ 1, \ 2, \ 3, \ 4, \ 5, \ 6, \ 7, \ 8, \ 9 ] ) x_vec = np.array ( [ 5.00, \ 5.25, \ 5.50, \ 5.75, \ 6.00, \ 7.50, \ 7.50, \ 7.50, \ 7.50, \ 7.50, \ 7.50, \ 7.50, \ 7.50, \ 7.50, \ 7.50 ] ) if ( n_data < 0 ): n_data = 0 if ( n_max <= n_data ): n_data = 0 x = 0.0 n = 0 f = 0.0 else: x = x_vec[n_data] n = n_vec[n_data] f = f_vec[n_data] n_data = n_data + 1 return n_data, x, n, f def r8_sign ( x ): #*****************************************************************************80 # ## r8_sign() returns the sign of an R8. # # Discussion: # # The value is +1 if the number is positive or zero, and it is -1 otherwise. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 June 2013 # # Author: # # John Burkardt # # Input: # # real X, the number whose sign is desired. # # Output: # # real VALUE, the sign of X. # if ( x < 0.0 ): value = -1.0 else: value = +1.0 return value def r8_sign_test ( ): #*****************************************************************************80 # ## r8_sign_test() tests r8_sign(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 September 2014 # # Author: # # John Burkardt # import numpy as np test_num = 5 r8_test = np.array ( [ -1.25, -0.25, 0.0, +0.5, +9.0 ] ) print ( '' ) print ( 'r8_sign_test' ) print ( ' r8_sign returns the sign of an R8.' ) print ( '' ) print ( ' R8 r8_sign(R8)' ) print ( '' ) for test in range ( 0, test_num ): r8 = r8_test[test] s = r8_sign ( r8 ) print ( ' %8.4f %8.0f' % ( r8, s ) ) return def r8vec_asum ( n, a ): #*****************************************************************************80 # ## r8vec_asum() sums the absolute values of the entries of an R8VEC. # # Discussion: # # An R8VEC is a vector of R8's. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of entries in the vector. # # real A(N), the vector. # # Output: # # real VALUE, the sum of the absolute values of the entries. # value = 0.0 for i in range ( 0, n ): value = value + abs ( a[i] ) return value def r8vec_asum_test ( rng ): #*****************************************************************************80 # ## r8vec_asum_test() tests r8vec_asum(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 January 2015 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'r8vec_asum_test' ) print ( ' r8vec_asum sums the absolute values of the entries in an R8VEC.' ) n = 10 a_lo = - 10.0 a_hi = + 10.0 a = a_lo + ( a_hi - a_lo ) * rng.random ( size = n ) r8vec_print ( n, a, ' Input vector:' ) value = r8vec_asum ( n, a ) print ( '' ) print ( ' Sum of absolute values of entries = %g' % ( value ) ) return def r8vec_house_column ( n, a_vec, k ): #*****************************************************************************80 # ## r8vec_house_column() defines a Householder premultiplier that "packs" a column. # # Discussion: # # The routine returns a vector V that defines a Householder # premultiplier matrix H(V) that zeros out the subdiagonal entries of # column K of the matrix A. # # H(V) = I - 2 * v * v' # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix A. # # real A_VEC(N), a row or column of the matrix A. # # integer K, the "special" entry in A_VEC. # The Householder matrix will zero out the entries after this. # # Output: # # real V(N), a vector of unit L2 norm which defines an # orthogonal Householder premultiplier matrix H with the property # that the K-th column of H*A is zero below the diagonal. # import numpy as np v = np.zeros ( n ) if ( k < 0 or n - 1 <= k ): return v s = 0.0 for i in range ( k, n ): s = s + a_vec[i] ** 2 s = np.sqrt ( s ) if ( s == 0.0 ): return v if ( a_vec[k] < 0.0 ): v[k] = a_vec[k] - abs ( s ) else: v[k] = a_vec[k] + abs ( s ) for i in range ( k + 1, n ): v[i] = a_vec[i] s = 0.0 for i in range ( k, n ): s = s + v[i] ** 2 s = np.sqrt ( s ) for i in range ( k, n ): v[i] = v[i] / s return v def r8vec_house_column_test ( rng ): #*****************************************************************************80 # ## r8vec_house_column_test() tests r8vec_house_column(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 February 2015 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np 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 a = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = [ n, n ] ) 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 return def r8vec_nint ( n, a ): #*****************************************************************************80 # ## r8vec_nint() rounds entries of an R8VEC. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the number of entries in the vector. # # real A(N), the vector to be rounded. # # Output: # # real A(N), the rounded vector. # for i in range ( 0, n ): a[i] = round ( a[i] ) return a def r8vec_nint_test ( rng ): #*****************************************************************************80 # ## r8vec_nint_test() tests r8vec_nint(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 December 2014 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'r8vec_nint_test' ) print ( ' r8vec_nint rounds an R8VEC.' ) n = 5 x1 = -5.0 x2 = +5.0 a = x1 + ( x2 - x1 ) * rng.random ( size = n ) r8vec_print ( n, a, ' Vector A:' ) a = r8vec_nint ( n, a ) r8vec_print ( n, a, ' Rounded vector A:' ) return def r8vec_print ( n, a, title ): #*****************************************************************************80 # ## r8vec_print() prints an R8VEC. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # # Input: # # integer N, the dimension of the vector. # # real A(N), the vector to be printed. # # string TITLE, a title. # print ( '' ) print ( title ) print ( '' ) for i in range ( 0, n ): print ( '%6d: %12g' % ( i, a[i] ) ) return def redheffer_matrix ( n ): #*****************************************************************************80 # ## redheffer_matrix() returns the REDHEFFER matrix. # # Formula: # # if ( J = 1 or mod ( J, I ) == 0 ) # A(I,J) = 1 # else # A(I,J) = 0 # # Example: # # N = 5 # # 1 1 1 1 1 # 1 1 0 1 0 # 1 0 1 0 0 # 1 0 0 1 0 # 1 0 0 0 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # The diagonal entries of A are all 1. # # A is a zero/one matrix. # # N - int ( log2 ( N ) ) - 1 eigenvalues are equal to 1. # # There is a real eigenvalue of magnitude approximately sqrt ( N ), # which is the spectral radius of the matrix. # # There is a negative eigenvalue of value approximately -sqrt ( N ). # # The remaining eigenvalues are "small", and there is a conjecture # that they lie inside the unit circle in the complex plane. # # The determinant is equal to the Mertens function M(N). # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 February 2015 # # Author: # # John Burkardt # # Reference: # # Wayne Barrett, Tyler Jarvis, # Spectral Properties of a Matrix of Redheffer, # Linear Algebra and Applications, # Volume 162, 1992, pages 673-683. # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j == 0 or ( ( j + 1 ) % ( i + 1 ) ) == 0 ): a[i,j] = 1.0 return a def redheffer_determinant ( n ): #*****************************************************************************80 # ## redheffer_determinant() returns the determinant of the REDHEFFER matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = mertens ( n ) return value def reflection_random_determinant ( n, key ): #*****************************************************************************80 # ## reflection_random_determinant(): determinant of the REFLECTION_RANDOM matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive integer that selects the data. # # Output: # # real DET: the determinant. # det = - 1.0 return det def reflection_random_eigen_right ( n, key ): #*****************************************************************************80 # ## reflection_random_eigen_right(): right eigenvectors of REFLECTION_RANDOM matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive integer that selects the data. # # Output: # # real Q(N,N), the eigenvector matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) # # Get a random vector v. # v = rng.standard_normal ( size = n ) v = v.reshape ( n, 1 ) # # Get the QR factorization. # Q, R = np.linalg.qr ( v, mode = 'complete' ) return Q def reflection_random_eigenvalues ( n, key ): #*****************************************************************************80 # ## reflection_random_eigenvalues(): eigenvalues of the REFLECTION_RANDOM matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive integer that selects the data. # # Output: # # real LAMDA(N), the eigenvalues. # import numpy as np lamda = np.ones ( n ) lamda[0] = -1.0 return lamda def reflection_random_inverse ( n, key ): #*****************************************************************************80 # ## reflection_random_inverse(): inverse of the REFLECTION_RANDOM matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive integer that selects the data. # # Output: # # real A(N,N), the inverse matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) # # Get a random vector v. # v = rng.standard_normal ( size = n ) # # Normalize. # v = v / np.linalg.norm ( v ) # # A = I - 2 * v v' # A = np.identity ( n ) - 2.0 * np.outer ( v, v ) # # Inverse is transpose. # A = np.transpose ( A ) return A def reflection_random_matrix ( n, key ): #*****************************************************************************80 # ## reflection_random_matrix() returns the REFLECTION_RANDOM matrix. # # Properties: # # A has determinant -1. # # A will have a single eigenvalue of -1. All other eigenvalues # will be +1. # # A is an orthogonal matrix. # # A*v = -v. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive integer that selects the data. # # Output: # # real A(N,N), the matrix. # # real V(N): the vector that is exactly reflected by A. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) # # Get a random vector v. # v = rng.standard_normal ( size = n ) # # Normalize. # v = v / np.linalg.norm ( v ) # # A = I - 2 * v v' # A = np.identity ( n ) - 2.0 * np.outer ( v, v ) return A, v def ring_adj_matrix ( m, n ): #*****************************************************************************80 # ## ring_adj_matrix() returns the ring_adj matrix. # # Discussion: # # This is the adjacency matrix for a set of points on a circle. # # Example: # # N = 5 # # 0 1 0 0 1 # 1 0 1 0 0 # 0 1 0 1 0 # 0 0 1 0 1 # 1 0 0 1 0 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # The determinant for N = 1 is 1, for N = 2 is -1, and for 2 < N, # mod ( N, 4 ) = 1 ==> det ( A ) = 2 # mod ( N, 4 ) = 2 ==> det ( A ) = -4 # mod ( N, 4 ) = 3 ==> det ( A ) = 2 # mod ( N, 4 ) = 0 ==> det ( A ) = 0 # # A is a zero/one matrix. # # A is an adjacency matrix. # # A has a zero diagonal. # # A is cyclic tridiagonal. # # A is a circulant matrix: each row is shifted once to get the next row. # # A has a constant row sum of 2. # # Because it has a constant row sum of 2, # A has an eigenvalue of 2, and # a (right) eigenvector of ( 1, 1, 1, ..., 1 ). # # A has a constant column sum of 2. # # Because it has a constant column sum of 2, # A has an eigenvalue of 2, and # a (left) eigenvector of ( 1, 1, 1, ..., 1 ). # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A is centrosymmetric: A(I,J) = A(N+1-I,N+1-J). # # A has an eigenvector of ( 1, 1, 1, ..., 1 ) with eigenvalue of 2. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j == i + 1 or j == i - 1 or j == i + 1 - n or j == i - 1 + n ): a[i,j] = 1.0 return a def ring_adj_determinant ( n ): #*****************************************************************************80 # ## ring_adj_determinant() returns the determinant of the ring_adj matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # if ( n == 1 ): value = 1.0 elif ( n == 2 ): value = -1.0 elif ( ( n % 4 ) == 0 ): value = 0.0 elif ( ( n % 4 ) == 1 ): value = 2.0 elif ( ( n % 4 ) == 2 ): value = -4.0 elif ( ( n % 4 ) == 3 ): value = 2.0 return value def ring_adj_null_left ( m, n ): #*****************************************************************************80 # ## ring_adj_null_left() returns a left null vector of the ring_adj matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # Output: # # real X(N), the null vector. # import numpy as np if ( ( m % 4 ) != 0 ): print ( '' ) print ( 'ring_adj_null_left(): Fatal error!' ) print ( ' M must be a multiple of 4.' ) raise Exception ( 'ring_adj_null_left(): Fatal error!' ) x = np.zeros ( m ) for i in range ( 0, m, 4 ): x[i] = + 1.0 x[i+1] = + 1.0 x[i+2] = - 1.0 x[i+3] = - 1.0 return x def ring_adj_null_right ( m, n ): #*****************************************************************************80 # ## ring_adj_null_right() returns a right null vector of the ring_adj matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # Output: # # real X(N), the null vector. # import numpy as np if ( ( n % 4 ) != 0 ): print ( '' ) print ( 'ring_adj_null_right(): Fatal error!' ) print ( ' N must be a multiple of 4.' ) raise Exception ( 'ring_adj_null_right(): Fatal error!' ) x = np.zeros ( n ) for i in range ( 0, n, 4 ): x[i] = + 1.0 x[i+1] = + 1.0 x[i+2] = - 1.0 x[i+3] = - 1.0 return x def ris_matrix ( n ): #*****************************************************************************80 # ## ris_matrix() returns the RIS matrix. # # Discussion: # # This is sometimes called the "dingdong" matrix, invented by F N Ris. # # Formula: # # A(I,J) = 1 / ( 3 + 2 * N - 2 * I - 2 * J ) # # Example: # # N = 5 # # 1/9 1/7 1/5 1/3 1 # 1/7 1/5 1/3 1 -1 # 1/5 1/3 1 -1 -1/3 # 1/3 1 -1 -1/3 -1/5 # 1 -1 -1/3 -1/5 -1/7 # # Properties: # # A is a Cauchy matrix. # # A is a Hankel matrix: constant along anti-diagonals. # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # The eigenvalues of A cluster around PI/2 and -PI/2. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Reference: # # John Nash, # Compact Numerical Methods for Computers: Linear Algebra and # Function Minimisation, # John Wiley, 1979, page 210. # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = 1.0 / float ( 2 * n - 2 * i - 2 * j - 1 ) return a def ris_determinant ( n ): #*****************************************************************************80 # ## ris_determinant() returns the determinant of the RIS matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # top = 1.0 for i in range ( 1, n + 1 ): for j in range ( i + 1, n + 1 ): top = top * float ( 4 * ( i - j ) * ( i - j ) ) bottom = 1.0 for i in range ( 1, n + 1 ): for j in range ( 1, n + 1 ): bottom = bottom * float ( 3 + 2 * n - 2 * i - 2 * j ) value = top / bottom return value def ris_inverse ( n ): #*****************************************************************************80 # ## ris_inverse() returns the inverse of the RIS matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): top = 1.0 bot1 = 1.0 bot2 = 1.0 for k in range ( 0, n ): top = top * float ( 3 + 2 * n - 2 * ( j + 1 ) - 2 * ( k + 1 ) ) \ * ( 3 + 2 * n - 2 * ( k + 1 ) - 2 * ( i + 1 ) ) if ( k != j ): bot1 = bot1 * float ( 2 * ( k - j ) ) if ( k != i ): bot2 = bot2 * float ( 2 * ( k - i ) ) a[i,j] = top / ( float ( 3 + 2 * n - 2 * ( j + 1 ) - 2 * ( i + 1 ) ) \ * bot1 * bot2 ) return a def rodman_matrix ( m, n, alpha ): #*****************************************************************************80 # ## rodman_matrix() returns the RODMAN matrix. # # Formula: # # If ( I = J ) # A(I,J) = 1 # else # A(I,J) = ALPHA # # Example: # # M = 5, N = 5, ALPHA = 2 # # 1 2 2 2 2 # 2 1 2 2 2 # 2 2 1 2 2 # 2 2 2 1 2 # 2 2 2 2 1 # # Properties: # # A is a special case of the combinatorial matrix. # # A is Toeplitz: constant along diagonals. # # A is a circulant matrix: each row is shifted once to get the next row. # # A has constant row sum. # # Because it has a constant row sum of 1+(N-1)*ALPHA, # A has an eigenvalue of 1+(N-1)*ALPHA, and # a (right) eigenvector of ( 1, 1, 1, ..., 1 ). # # A has constant column sum. # # Because it has a constant column sum of 1+(N-1)*ALPHA, # A has an eigenvalue of 1+(N-1)*ALPHA, and # a (left) eigenvector of ( 1, 1, 1, ..., 1 ). # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A is centrosymmetric: A(I,J) = A(N+1-I,N+1-J). # # A is positive definite for ALPHA < 1. # # The eigenvalues and eigenvectors of A are: # # For I = 1 to N-1: # # LAMBDA(I) = 1 - ALPHA # V(I) = ( - sum ( 2 <= J <= N ) X(J), X(2), X(3), ..., X(N) ) # # For I = N: # # LAMBDA(I) = 1 + ALPHA * ( N - 1 ) # V(I) = ( 1, 1, 1, ..., 1 ) # # det ( A ) = ( 1 - ALPHA )^(N-1) * ( 1 + ALPHA * ( N - 1 ) ). # # A is nonsingular if ALPHA is not 1, and ALPHA is not -1/(N-1). # # The inverse of A is known. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Input: # # integer M, N, the order of the matrix. # # real ALPHA, the parameter. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( ( m, n ) ) for i in range ( 0, m ): for j in range ( 0, n ): if ( i == j ): a[i,j] = 1.0 else: a[i,j] = alpha return a def rodman_condition ( n, alpha ): #*****************************************************************************80 # ## rodman_condition() computes the L1 condition of the RODMAN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real ALPHA, the parameter. # # Output: # # real VALUE, the L1 condition. # a_norm = 1.0 + float ( n - 1 ) * abs ( alpha ) top = abs ( 1.0 + alpha * float ( n - 2 ) ) \ + float ( n - 1 ) * abs ( alpha ) bot = abs ( 1.0 + alpha * float ( n - 2 ) \ - alpha * alpha * float ( n - 1 ) ) b_norm = top / bot value = a_norm * b_norm return value def rodman_determinant ( n, alpha ): #*****************************************************************************80 # ## rodman_determinant() returns the determinant of the RODMAN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real ALPHA, the parameter. # # Output: # # real VALUE, the determinant. # value = ( 1.0 - alpha ) ** ( n - 1 ) * ( 1.0 + alpha * float ( n - 1 ) ) return value def rodman_eigen_right ( n, alpha ): #*****************************************************************************80 # ## rodman_eigen_right() returns the right eigenvectors of the RODMAN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real ALPHA, the parameter. # # Output: # # real X(N,N), the right eigenvectors. # import numpy as np x = np.zeros ( ( n, n ) ) for j in range ( 0, n - 1 ): x[ 0,j] = +1.0 x[j+1,j] = -1.0 for i in range ( 0, n ): x[i,n-1] = 1.0 return x def rodman_eigenvalues ( n, alpha ): #*****************************************************************************80 # ## rodman_eigenvalues() returns the eigenvalues of the RODMAN matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real ALPHA, the parameter. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n ) for i in range ( 0, n - 1 ): lam[i] = 1.0 - alpha lam[n-1] = 1.0 + alpha * float ( n - 1 ) return lam def rodman_inverse ( n, alpha ): #*****************************************************************************80 # ## rodman_inverse() returns the inverse of the RODMAN matrix. # # Formula: # # If ( I = J ) # A(I,J) = ( 1 + ALPHA * ( N - 2 ) ) / # ( 1 + ALPHA * ( N - 2 ) - ALPHA^2 * ( N - 1 ) ) # else # A(I,J) = - ALPHA / # ( 1 + ALPHA * ( N - 2 ) - ALPHA^2 * ( N - 1 ) ) # # Example: # # N = 5, ALPHA = 2.0 # # -0.7778 0.2222 0.2222 0.2222 0.2222 # 0.2222 -0.7778 0.2222 0.2222 0.2222 # 0.2222 0.2222 -0.7778 0.2222 0.2222 # 0.2222 0.2222 0.2222 -0.7778 0.2222 # 0.2222 0.2222 0.2222 0.2222 -0.7778 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Input: # # integer N, the order of the matrix. # # real ALPHA, the parameter. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) bot = 1.0 + alpha * float ( n - 2 ) - alpha * alpha * float ( n - 1 ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = ( 1.0 + alpha * float ( n - 2 ) ) / bot else: a[i,j] = - alpha / bot return a def rosser1_matrix ( ): #*****************************************************************************80 # ## rosser1_matrix() returns the ROSSER1 matrix. # # Formula: # # 611 196 -192 407 -8 -52 -49 29 # 196 899 113 -192 -71 -43 -8 -44 # -192 113 899 196 61 49 8 52 # 407 -192 196 611 8 44 59 -23 # -8 -71 61 8 411 -599 208 208 # -52 -43 49 44 -599 411 208 208 # -49 -8 8 59 208 208 99 -911 # 29 -44 52 -23 208 208 -911 99 # # Properties: # # A is singular. # # det ( A ) = 0. # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # The eigenvalues of A are: # # a = sqrt(10405), b = sqrt(26), # # LAMBDA = (-10*a, 0, 510-100*b, 1000, 1000, 510+100*b, 1020, 10*a) # # ( 10*a = 1020.04901843, 510-100*b = 0.09804864072 ) # # The eigenvectors are # # ( 2, 1, 1, 2, 102+a, 102+a, -204-2a, -204-2a ) # ( 1, 2, -2, -1, 14, 14, 7, 7 ) # ( 2, -1, 1, -2, 5-b, -5+b, -10+2b, 10-2b ) # ( 7, 14, -14, -7, -2, -2, -1, -1 ) # ( 1, -2, -2, 1, -2, 2, -1, 1 ) # ( 2, -1, 1, -2, 5+b, -5-b, -10-2b, 10+2b ) # ( 1, -2, -2, 1, 2, -2, 1, -1 ) # ( 2, 1, 1, 2, 102-a, 102-a, -204+2a, -204+2a ) # # trace ( A ) = 4040. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Reference: # # Robert Gregory, David Karney, # Example 4.10, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, 1969, page 61, # LC: QA263.G68. # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(8,8), the matrix. # import numpy as np a = np.array ( [ \ [ 611.0, 196.0, -192.0, 407.0, -8.0, -52.0, -49.0, 29.0 ], \ [ 196.0, 899.0, 113.0, -192.0, -71.0, -43.0, -8.0, -44.0 ], \ [ -192.0, 113.0, 899.0, 196.0, 61.0, 49.0, 8.0, 52.0 ], \ [ 407.0, -192.0, 196.0, 611.0, 8.0, 44.0, 59.0, -23.0 ], \ [ -8.0, -71.0, 61.0, 8.0, 411.0, -599.0, 208.0, 208.0 ], \ [ -52.0, -43.0, 49.0, 44.0, -599.0, 411.0, 208.0, 208.0 ], \ [ -49.0, -8.0, 8.0, 59.0, 208.0, 208.0, 99.0, -911.0 ], \ [ 29.0, -44.0, 52.0, -23.0, 208.0, 208.0, -911.0, 99.0 ] ] ) return a def rosser1_determinant ( ): #*****************************************************************************80 # ## rosser1_determinant() returns the determinant of the ROSSER1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = 0.0 return value def rosser1_eigen_left ( ): #*****************************************************************************80 # ## rosser1_eigen_left() returns left eigenvectors of the ROSSER1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Output: # # real X(8,8), the eigenvector matrix. # import numpy as np a = np.sqrt ( 10405.0 ) b = np.sqrt ( 26.0 ) x = np.zeros ( ( 8, 8 ) ) # # Note that the matrix entries are listed by ROW # x = np.array ( [ \ [ 2.0, 1.0, 1.0, 2.0, 102.0 + a, 102.0 + a, -204.0 - 2.0 * a, -204.0 - 2.0 * a ], \ [ 1.0, 2.0, -2.0, -1.0, 14.0, 14.0, 7.0, 7.0 ], \ [ 2.0, -1.0, 1.0, -2.0, 5.0 - b, -5.0 + b, -10.0 + 2.0 * b, 10.0 - 2.0 * b ], \ [ 7.0, 14.0, -14.0, -7.0, -2.0, -2.0, -1.0, -1.0 ], \ [ 1.0, -2.0, -2.0, 1.0, -2.0, 2.0, -1.0, 1.0 ], \ [ 2.0, -1.0, 1.0, -2.0, 5.0 + b, -5.0 - b, -10.0 - 2.0 * b, 10.0 + 2.0 * b ], \ [ 1.0, -2.0, -2.0, 1.0, 2.0, -2.0, 1.0, -1.0 ], \ [ 2.0, 1.0, 1.0, 2.0, 102.0 - a, 102.0 - a, -204.0 + 2.0 * a, -204.0 + 2.0 * a ] ] ) return x def rosser1_eigen_right ( ): #*****************************************************************************80 # ## rosser1_eigen_right() returns right eigenvectors of the ROSSER1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Output: # # real X(8,8), the eigenvector matrix. # import numpy as np a = np.sqrt ( 10405.0 ) b = np.sqrt ( 26.0 ) x = np.zeros ( ( 8, 8 ) ) # # Note that the matrix entries are listed by ROW # x = np.array ( [ \ [ 2.0, 1.0, 1.0, 2.0, 102.0 + a, 102.0 + a, -204.0 - 2.0 * a, -204.0 - 2.0 * a ], \ [ 1.0, 2.0, -2.0, -1.0, 14.0, 14.0, 7.0, 7.0 ], \ [ 2.0, -1.0, 1.0, -2.0, 5.0 - b, -5.0 + b, -10.0 + 2.0 * b, 10.0 - 2.0 * b ], \ [ 7.0, 14.0, -14.0, -7.0, -2.0, -2.0, -1.0, -1.0 ], \ [ 1.0, -2.0, -2.0, 1.0, -2.0, 2.0, -1.0, 1.0 ], \ [ 2.0, -1.0, 1.0, -2.0, 5.0 + b, -5.0 - b, -10.0 - 2.0 * b, 10.0 + 2.0 * b ], \ [ 1.0, -2.0, -2.0, 1.0, 2.0, -2.0, 1.0, -1.0 ], \ [ 2.0, 1.0, 1.0, 2.0, 102.0 - a, 102.0 - a, -204.0 + 2.0 * a, -204.0 + 2.0 * a ] ] ) x = np.transpose ( x ) return x def rosser1_eigenvalues ( ): #*****************************************************************************80 # ## rosser1_eigenvalues() returns the eigenvalues of the ROSSER1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 March 2015 # # Author: # # John Burkardt # # Output: # # real LAMBDA(8,1), the eigenvalues. # import numpy as np a = np.sqrt ( 10405.0 ) b = np.sqrt ( 26.0 ) lam = np.array ( [ \ -10.0 * a, \ 0.0, \ 510.0 - 100.0 * b, \ 1000.0, \ 1000.0, \ 510.0 + 100.0 * b, \ 1020.0, \ 10.0 * a ] ) return lam def rosser1_null_left ( ): #*****************************************************************************80 # ## rosser1_null_left() returns a left null vector of the ROSSER1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 March 2015 # # Author: # # John Burkardt # # Output: # # real X(8), the null vector. # import numpy as np x = np.array ( [ \ [ 1.0 ], \ [ 2.0 ], \ [ -2.0 ], \ [ -1.0 ], \ [ 14.0 ], \ [ 14.0 ], \ [ 7.0 ], \ [ 7.0 ] ] ) return x def rosser1_null_right ( ): #*****************************************************************************80 # ## rosser1_null_right() returns a right null vector of the ROSSER1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 March 2015 # # Author: # # John Burkardt # # Output: # # real X(8), the null vector. # import numpy as np x = np.array ( [ \ [ 1.0 ], \ [ 2.0 ], \ [ -2.0 ], \ [ -1.0 ], \ [ 14.0 ], \ [ 14.0 ], \ [ 7.0 ], \ [ 7.0 ] ] ) return x def routh_matrix ( n, x ): #*****************************************************************************80 # ## routh_matrix() returns the ROUTH matrix. # # Formula: # # A is tridiagonal. # A(1,1) = X(1). # A(I-1,I) = sqrt ( X(I) ), for I = 2 to N. # A(I,I-1) = - sqrt ( X(I) ), for I = 2 to N. # # Example: # # N = 5, X = ( 1, 4, 9, 16, 25 ) # # 1 -2 0 0 0 # 2 0 -3 0 0 # 0 3 0 -4 0 # 0 0 4 0 -5 # 0 0 0 5 0 # # Properties: # # A is generally not symmetric: A' /= A. # # A is tridiagonal. # # Because A is tridiagonal, it has property A (bipartite). # # A is banded, with bandwidth 3. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # det ( A ) = product ( X(N) * X(N-2) * X(N-4) * ... * X(N+1-2*(N/2)) ) # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real X(N), the data that defines the matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == 0 and j == 0 ): a[i,j] = abs ( x[0] ) elif ( i == j + 1 ): a[i,j] = np.sqrt ( abs ( x[i] ) ) elif ( i == j - 1 ): a[i,j] = - np.sqrt ( abs ( x[i+1] ) ) return a def routh_determinant ( n, x ): #*****************************************************************************80 # ## routh_determinant() returns the determinant of the ROUTH matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N-1), the elements. # # Output: # # real VALUE, the determinant. # value = 1.0 for i in range ( n - 1, -1, -2 ): value = value * x[i] return value def rutis1_matrix ( ): #*****************************************************************************80 # ## rutis1_matrix() returns the RUTIS1 matrix. # # Example: # # 6 4 4 1 # 4 6 1 4 # 4 1 6 4 # 1 4 4 6 # # Properties: # # A is symmetric: A' = A. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # A has constant row sums. # # Because it has a constant row sum of 15, # A has an eigenvalue of 15, and # a (right) eigenvector of ( 1, 1, 1, 1 ). # # A has constant column sums. # # Because it has a constant column sum of 15, # A has an eigenvalue of 15, and # a (left) eigenvector of ( 1, 1, 1, ..., 1 ). # # A has a repeated eigenvalue. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 January 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.array ( [ \ [ 6.0, 4.0, 4.0, 1.0 ], \ [ 4.0, 6.0, 1.0, 4.0 ], \ [ 4.0, 1.0, 6.0, 4.0 ], \ [ 1.0, 4.0, 4.0, 6.0 ] ] ) return a def rutis1_condition ( ): #*****************************************************************************80 # ## rutis1_condition() returns the L1 condition of the RUTIS1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 January 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the condition. # a_norm = 15.0 b_norm = 1.0 value = a_norm * b_norm return value def rutis1_determinant ( ): #*****************************************************************************80 # ## rutis1_determinant() returns the determinant of the RUTIS1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 January 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = - 375.0 return value def rutis1_eigen_right ( ): #*****************************************************************************80 # ## rutis1_eigen_right() returns the right eigenvectors of the RUTIS1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 March 2015 # # Author: # # John Burkardt # # Output: # # real A(4,4), the right eigenvector matrix. # import numpy as np a = np.array ( [ \ [ 1.0, 1.0, 0.0, 1.0 ], \ [ 1.0, 0.0, 1.0, -1.0 ], \ [ 1.0, 0.0, -1.0, -1.0 ], \ [ 1.0, -1.0, 0.0, 1.0 ] ] ) return a def rutis1_eigenvalues ( ): #*****************************************************************************80 # ## rutis1_eigenvalues() returns the eigenvalues of the RUTIS1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 March 2015 # # Author: # # John Burkardt # # Output: # # real LAM(4), the eigenvalues. # import numpy as np lam = np.array ( [ \ 15.0, \ 5.0, \ 5.0, \ -1.0 ] ) return lam def rutis1_inverse ( ): #*****************************************************************************80 # ## rutis1_inverse() returns the inverse of the RUTIS1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ -2.0, 4.0, 4.0, -5.0 ], \ [ 4.0, -2.0, -5.0, 4.0 ], \ [ 4.0, -5.0, -2.0, 4.0 ], \ [ -5.0, 4.0, 4.0, -2.0 ] ] ) for i in range ( 0, 4 ): for j in range ( 0, 4 ): a[i,j] = a[i,j] / 15.0 return a def rutis2_matrix ( ): #*****************************************************************************80 # ## rutis2_matrix() returns the RUTIS2 matrix. # # Example: # # 5 4 1 1 # 4 5 1 1 # 1 1 4 2 # 1 1 2 4 # # Properties: # # A is symmetric: A' = A. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # A has distinct eigenvalues. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 January 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.array ( [ \ [ 5.0, 4.0, 1.0, 1.0 ], \ [ 4.0, 5.0, 1.0, 1.0 ], \ [ 1.0, 1.0, 4.0, 2.0 ], \ [ 1.0, 1.0, 2.0, 4.0 ] ] ) return a def rutis2_condition ( ): #*****************************************************************************80 # ## rutis2_condition() returns the L1 condition of the RUTIS2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 January 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the condition. # a_norm = 11.0 b_norm = 1.04 value = a_norm * b_norm return value def rutis2_determinant ( ): #*****************************************************************************80 # ## rutis2_determinant() returns the determinant of the RUTIS2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 January 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = 100.0 return value def rutis2_eigen_right ( ): #*****************************************************************************80 # ## rutis2_eigen_right() returns the right eigenvectors of the RUTIS2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Output: # # real A(4,4), the right eigenvector matrix. # import numpy as np a = np.array ( [ \ [ 2.0, -1.0, 0.0, -1.0 ], \ [ 2.0, -1.0, 0.0, 1.0 ], \ [ 1.0, 2.0, -1.0, 0.0 ], \ [ 1.0, 2.0, 1.0, 0.0 ] ] ) return a def rutis2_eigenvalues ( ): #*****************************************************************************80 # ## rutis2_eigenvalues() returns the eigenvalues of the RUTIS2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Output: # # real LAM(4), the eigenvalues. # import numpy as np lam = np.array ( [ \ 10.0, \ 5.0, \ 2.0, \ 1.0 ] ) return lam def rutis2_inverse ( ): #*****************************************************************************80 # ## rutis2_inverse() returns the inverse of the RUTIS2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 28.0, -22.0, -1.0, -1.0 ], \ [ -22.0, 28.0, -1.0, -1.0 ], \ [ -1.0, -1.0, 17.0, -8.0 ], \ [ -1.0, -1.0, -8.0, 17.0 ] ] ) for j in range ( 0, 4 ): for i in range ( 0, 4 ): a[i,j] = a[i,j] / 50.0 return a def rutis3_condition ( ): #*****************************************************************************80 # ## rutis3_condition() returns the L1 condition of the RUTIS3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the condition. # a_norm = 12.0 b_norm = 0.5 value = a_norm * b_norm return value def rutis3_determinant ( ): #*****************************************************************************80 # ## rutis3_determinant() returns the determinant of the RUTIS3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = 624.0 return value def rutis3_inverse ( ): #*****************************************************************************80 # ## rutis3_inverse() returns the inverse of the RUTIS3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 103.0, 125.0, -5.0, 79.0 ], \ [ 5.0, 103.0, -79.0, 125.0 ], \ [ -125.0, -79.0, 103.0, -5.0 ], \ [ 79.0, 5.0, -125.0, 103.0 ] ] ) for i in range ( 0, 4 ): for j in range ( 0, 4 ): a[i,j] = a[i,j] / 624.0 return a def rutis3_matrix ( ): #*****************************************************************************80 # ## rutis3_matrix() returns the RUTIS3 matrix. # # Example: # # 4 -5 0 3 # 0 4 -3 -5 # 5 -3 4 0 # 3 0 5 4 # # Properties: # # A is not symmetric: A' /= A. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # A has distinct eigenvalues. # # A has a pair of complex eigenvalues. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 February 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.array ( [ \ [ 4.0, -5.0, 0.0, 3.0 ], \ [ 0.0, 4.0, -3.0, -5.0 ], \ [ 5.0, -3.0, 4.0, 0.0 ], \ [ 3.0, 0.0, 5.0, 4.0 ] ] ) return a def rutis4_condition ( n ): #*****************************************************************************80 # ## rutis4_condition() returns the L1 condition of the RUTIS4 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 October 2021 # # Author: # # John Burkardt # # Input: # # integer N: the order of the matrix. # # Output: # # real VALUE, the condition. # import numpy as np A = rutis4_matrix ( n ) a_norm = np.linalg.norm ( A, 1 ) B = rutis4_inverse ( n ) b_norm = np.linalg.norm ( B, 1 ) value = a_norm * b_norm return value def rutis4_matrix ( n ): #*****************************************************************************80 # ## rutis4_matrix() returns the RUTIS4 matrix. # # Example: # # N = 6 # # 14 14 6 1 0 0 # 14 20 15 6 1 0 # 6 15 20 15 6 1 # 1 6 15 20 15 6 # 0 1 6 15 20 14 # 0 0 1 6 14 14 # # Properties: # # A is symmetric: A' = A. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # A is banded with a bandwidth of 7. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A is the cube of the scalar tridiagonal matrix whose diagonals # are ( 1, 2, 1 ). # # LAMBDA(I) = 64 * ( cos ( i * pi / ( 2 * ( n + 1 ) ) ) )^6 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): if ( 0 <= i - 3 ): a[i,i-3] = 1.0 if ( 0 <= i - 2 ): a[i,i-2] = 6.0 if ( 0 <= i - 1 ): a[i,i-1] = 15.0 a[i,i] = 20.0 if ( i + 1 <= n - 1 ): a[i,i+1] = 15.0 if ( i + 2 <= n - 1 ): a[i,i+2] = 6.0 if ( i + 3 <= n - 1 ): a[i,i+3] = 1.0 a[0,0] = 14.0 a[0,1] = 14.0 a[1,0] = 14.0 a[n-1,n-1] = 14.0 a[n-2,n-1] = 14.0 a[n-1,n-2] = 14.0 return a def rutis4_determinant ( n ): #*****************************************************************************80 # ## rutis4_determinant() returns the determinant of the RUTIS4 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real VALUE, the determinant. # import numpy as np value = 1.0 for i in range ( 0, n ): angle = float ( i + 1 ) * np.pi / float ( 2 * ( n + 1 ) ) value = value * 64.0 * ( np.cos ( angle ) ) ** 6 return value def rutis4_eigenvalues ( n ): #*****************************************************************************80 # ## rutis4_eigenvalues() returns the eigenvalues of the RUTIS4 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n ) for i in range ( 0, n ): angle = float ( i + 1 ) * np.pi / float ( 2 * ( n + 1 ) ) lam[i] = 64.0 * ( np.cos ( angle ) ) ** 6 return lam def rutis4_inverse ( n ): #*****************************************************************************80 # ## rutis4_inverse() returns the inverse of the RUTIS4 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np c = oto_inverse ( n ) b = r8mat_mm ( n, n, n, c, c ) a = r8mat_mm ( n, n, n, c, b ) return a def rutis5_matrix ( ): #*****************************************************************************80 # ## rutis5_matrix() returns the RUTIS5 matrix. # # Example: # # 10 1 4 0 # 1 10 5 -1 # 4 5 10 7 # 0 -1 7 9 # # Properties: # # A is symmetric: A' = A. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 October 2007 # # Author: # # John Burkardt # # Reference: # # John Todd, # Basic Numerical Mathematics, Volume 2: Numerical Algebra, # Academic Press, 1977. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.array ( [ \ [ 10.0, 1.0, 4.0, 0.0 ], \ [ 1.0, 10.0, 5.0, -1.0 ], \ [ 4.0, 5.0, 10.0, 7.0 ], \ [ 0.0, -1.0, 7.0, 9.0 ] ] ) return a def rutis5_condition ( ): #*****************************************************************************80 # ## rutis5_condition() returns the L1 condition of the RUTIS5 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 December 2014 # # Author: # # John Burkardt # # Output: # # real COND, the L1 condition number. # cond = 62608.0 return cond def rutis5_determinant ( ): #*****************************************************************************80 # ## rutis5_determinant() returns the determinant of the RUTIS5 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 January 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = 1.0 return value def rutis5_eigen_right ( ): #*****************************************************************************80 # ## rutis5_eigen_right() returns the right eigenvectors of the RUTIS5 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Output: # # real A(4,4), the right eigenvector matrix. # import numpy as np a = np.array ( [ \ [ 0.356841883715928, \ 0.382460905084129, \ 0.718205429169617, \ 0.458877421126365 ], \ [ -0.341449101169948, \ -0.651660990948502, \ 0.087555987078632, \ 0.671628180850787 ], \ [ 0.836677864423576, \ -0.535714651223808, \ -0.076460316709461, \ -0.084461728708607 ], \ [ -0.236741488801405, \ -0.376923628103094, \ 0.686053008598214, \ - 0.575511351279045 ] ] ) a = np.transpose ( a ) return a def rutis5_eigenvalues ( ): #*****************************************************************************80 # ## rutis5_eigenvalues() returns the eigenvalues of the RUTIS5 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Output: # # real LAM(4), the eigenvalues. # import numpy as np lam = np.array ( [ \ 19.122479087555860, \ 10.882816916492464, \ 8.994169735037230, \ 0.000534260914449 ] ) return lam def rutis5_inverse ( ): #*****************************************************************************80 # ## rutis5_inverse() returns the inverse of the RUTIS5 matrix. # # Example: # # 105 167 -304 255 # 167 266 -484 406 # -304 -484 881 -739 # 255 406 -739 620 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Reference: # # John Todd, # Basic Numerical Mathematics, Volume 2: Numerical Algebra, # Academic Press, 1977. # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 105.0, 167.0, -304.0, 255.0 ], \ [ 167.0, 266.0, -484.0, 406.0 ], \ [ -304.0, -484.0, 881.0, -739.0 ], \ [ 255.0, 406.0, -739.0, 620.0 ] ] ) return a def schur_block_matrix ( n, x, y ): #*****************************************************************************80 # ## schur_block_matrix() returns the SCHUR BLOCK matrix. # # Formula: # # if ( i == j ) # a(i,j) = x( (i+1)/2 ) # else ( mod ( i, 2 ) == 1 & j == i + 1 ) # a(i,j) = y( (i+1)/2 ) # else ( mod ( i, 2 ) == 0 & j == i - 1 ) # a(i,j) = -y( (i+1)/2 ) # else # a(i,j) = 0.0 # # Example: # # N = 5, X = ( 1, 2, 3 ), Y = ( 4, 5 ) # # 1 4 0 0 0 # -4 1 0 0 0 # 0 0 2 5 0 # 0 0 -5 2 0 # 0 0 0 0 3 # # Properties: # # A is generally not symmetric: A' /= A. # # A is block diagonal, with the blocks being 2 by 2 or 1 by 1 in size. # # A is in real Schur form. # # The eigenvalues of A are X(I) +/- sqrt ( - 1 ) * Y(I) # # A is tridiagonal. # # A is banded, with bandwidth 3. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 March 2015 # # Author: # # John Burkardt # # Reference: # # Francoise Chatelin, # Section 4.2.7, # Eigenvalues of Matrices, # John Wiley, 1993. # # Francoise Chatelin, Valerie Fraysse, # Qualitative computing: Elements of a theory for finite precision # computation, Lecture notes, # CERFACS, Toulouse, France and THOMSON-CSF, Orsay, France, June 1993. # # Input: # # integer N, the order of A. # # real X( (N+1)/2 ), specifies the diagonal elements # of A. # # real Y( N/2 ), specifies the off-diagonal elements # of the Schur blocks. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): ih = ( i // 2 ) for j in range ( 0, n ): if ( i == j ): a[i,j] = x[ih] elif ( ( i % 2 ) == 0 and j == i + 1 ): a[i,j] = y[ih] elif ( ( i % 2 ) == 1 and j == i - 1 ): a[i,j] = - y[ih] return a def schur_block_determinant ( n, x, y ): #*****************************************************************************80 # ## schur_block_determinant() returns the determinant of the SCHUR_BLOCK matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real X( (N+1)/2 ), specifies the diagonal # elements of A. # # real Y( N/2 ), specifies the off-diagonal # elements of the Schur blocks. # # Output: # # real VALUE, the determinant of A. # value = 1.0 ihi = ( n // 2 ) for i in range ( 0, ihi ): value = value * ( x[i] ** 2 + y[i] ** 2 ) if ( ( n % 2 ) == 1 ): value = value * x[ihi] return value def schur_block_inverse ( n, x, y ): #*****************************************************************************80 # ## schur_block_inverse() returns the inverse of the SCHUR_BLOCK matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real X( (N+1)/2 ), specifies the diagonal elements # of A. # # real Y( N/2 ), specifies the off-diagonal elements # of the Schur blocks. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): k = ( i // 2 ) if ( i == j ): if ( i == n - 1 and ( n % 2 ) == 1 ): a[i,j] = 1.0 / x[k] else: a[i,j] = x[k] / ( x[k] ** 2 + y[k] ** 2 ) elif ( ( i % 2 ) == 0 and j == i + 1 ): a[i,j] = - y[k] / ( x[k] ** 2 + y[k] ** 2 ) elif ( ( i % 2 ) == 1 and j == i - 1 ): a[i,j] = y[k] / ( x[k] ** 2 + y[k] ** 2 ) return a def spd_random_matrix ( n, key ): #*****************************************************************************80 # ## spd_random_matrix() returns a random symmetric positive definite matrix. # # Discussion: # # The matrix returned will have eigenvalues in the range [0,1]. # # Properties: # # A is symmetric: A' = A. # # A is positive definite: 0 < x'*A*x for nonzero x. # # The eigenvalues of A will be real. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real A(N,N), the matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) # # Get a random set of eigenvalues. # lam = rng.random ( size = n ) # # Get a random orthogonal matrix Q. # q = orthogonal_random_matrix ( n, key ) # # Set A = Q * Lambda * Q'. # a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): for k in range ( 0, n ): a[i,j] = a[i,j] + q[i,k] * lam[k] * q[j,k] return a def spd_random_determinant ( n, key ): #*****************************************************************************80 # ## spd_random_determinant() returns the determinant of the spd random matrix. # # Discussion: # # This routine will only work properly if the SAME value of SEED # is input that was input to spd_random. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real VALUE, the determinant. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) lam = rng.random ( size = n ) value = np.prod ( lam ) return value def spd_random_eigen_right ( n, key ): #*****************************************************************************80 # ## spd_random_eigen_right() returns the right eigenvectors of the spd random matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real Q(N,N), the matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) # # Get a random set of eigenvalues. # lam = rng.random ( size = n ) # # Get a random orthogonal matrix Q. # q = orthogonal_random_matrix ( n, key ) return q def spd_random_eigenvalues ( n, key ): #*****************************************************************************80 # ## spd_random_eigenvalues() returns the eigenvalues of the spd random matrix. # # Discussion: # # This routine will only work properly if the SAME value of SEED # is input that was input to spd_random_matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real LAM(N), the eigenvalues. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) lam = rng.random ( size = n ) return lam def spd_random_inverse ( n, key ): #*****************************************************************************80 # ## spd_random_inverse() returns the inverse of the spd random matrix. # # Discussion: # # The matrix returned will have eigenvalues in the range [0,1]. # # Properties: # # A is symmetric: A' = A. # # A is positive definite: 0 < x'*A*x for nonzero x. # # The eigenvalues of A will be real. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real A(N,N), the matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) a = np.zeros ( ( n, n ) ) # # Get a random set of eigenvalues. # lam = rng.random ( size = n ) # # Get a random orthogonal matrix Q. # q = orthogonal_random_matrix ( n, key ) # # Set A = Q * Lambda * Q'. # for i in range ( 0, n ): for j in range ( 0, n ): for k in range ( 0, n ): a[i,j] = a[i,j] + q[i,k] * ( 1.0 / lam[k] ) * q[j,k] return a def spline_matrix ( n, x ): #*****************************************************************************80 # ## spline_matrix() returns the SPLINE matrix. # # Discussion: # # This matrix arises during interpolation with cubic splines. # # Formula: # # if ( I = 1 and J = I ) # A(I,J) = 2 * X(I) # elseif ( I = 1 and J = I + 1 ) # A(I,J) = X(I) # elseif ( I = N and J = I ) # A(I,J) = 2 * X(N-1) # elseif ( I = N and J = I - 1 ) # A(I,J) = X(N-1) # elseif ( J = I ) # A(I,J) = 2 * (X(I-1)+X(I)) # elseif ( J = I-1 ) # A(I,J) = X(I-1) # elseif ( J = I + 1 ) # A(I,J) = X(I) # else # A(I,J) = 0 # # Example: # # N = 5 # X = ( 1, 1, 1, 1 ) # # 2 1 0 0 0 # 1 4 1 0 0 # 0 1 4 1 0 # 0 0 1 4 1 # 0 0 0 1 2 # # N = 5 # X = ( 1, 2, 3, 4 ) # # 2 1 0 0 0 # 1 6 2 0 0 # 0 2 10 3 0 # 0 0 3 14 4 # 0 0 0 4 8 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is tridiagonal. # # Because A is tridiagonal, it has property A (bipartite). # # A is banded, with bandwidth 3. # # If the entries of X are positive, then A is positive definite. # # If the entries of X are all of one sign, then A is diagonally dominant. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real X(N-1), values that represent the spacing # between points, and which define the entries of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == 0 and j == 0 ): a[i,j] = 2.0 * x[0] elif ( i == 0 and j == i + 1 ): a[i,j] = x[0] elif ( i == n - 1 and j == i ): a[i,j] = 2.0 * x[n-2] elif ( i == n and j == i - 1 ): a[i,j] = x[n-2] elif ( j == i ): a[i,j] = 2.0 * ( x[i-1] + x[i] ) elif ( j == i - 1 ): a[i,j] = x[i-1] elif ( j == i + 1 ): a[i,j] = x[i] return a def spline_determinant ( n, x ): #*****************************************************************************80 # ## spline_determinant() returns the determinant of the SPLINE matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N-1), the elements. # # Output: # # real VALUE, the determinant. # determ_nm1 = 2.0 * x[n-2] if ( n == 1 ): value = determ_nm1 return value determ_nm2 = determ_nm1 if ( n == 2 ): determ_nm1 = 4.0 * x[n-2] * x[n-2] - x[n-2] * x[n-2] else: determ_nm1 = 4.0 * ( x[n-3] + x[n-2] ) * x[n-2] - x[n-2] * x[n-2] if ( n == 2 ): value = determ_nm1 return value for i in range ( n - 3, -1, -1 ): if ( i == 0 ): value = 2.0 * x[i] * determ_nm1 - x[i] * x[i] * determ_nm2 else: value = 2.0 * ( x[i-1] + x[i] ) * determ_nm1 - x[i] * x[i] * determ_nm2 determ_nm2 = determ_nm1 determ_nm1 = value return value def spline_inverse ( n, x ): #*****************************************************************************80 # ## spline_inverse() returns the inverse of the SPLINE matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 March 2015 # # Author: # # John Burkardt # # Reference: # # CM daFonseca, J Petronilho, # Explicit Inverses of Some Tridiagonal Matrices, # Linear Algebra and Its Applications, # Volume 325, 2001, pages 7-21. # # Input: # # integer N, the order of the matrix. # # real X(N-1), the parameters. # # Output: # # real A(N,N), the inverse of the matrix. # import numpy as np d = np.zeros ( n ) d[n-1] = 2.0 * x[n-2] for i in range ( n - 2, 0, -1 ): d[i] = 2.0 * ( x[i-1] + x[i] ) - x[i] * x[i] / d[i+1] d[0] = 2.0 * x[0] - x[0] * x[0] / d[1] e = np.zeros ( n ) e[0] = 2.0 * x[0] for i in range ( 1, n - 1 ): e[i] = 2.0 * ( x[i-1] + x[i] ) - x[i-1] * x[i-1] / e[i-1] e[n-1] = 2.0 * x[n-2] - x[n-2] * x[n-2] / e[n-2] a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, i + 1 ): p1 = 1.0 for k in range ( j, i ): p1 = p1 * x[k] p2 = 1.0 for k in range ( i + 1, n ): p2 = p2 * d[k] p3 = 1.0 for k in range ( j, n ): p3 = p3 * e[k] a[i,j] = r8_mop ( i + j ) * p1 * p2 / p3 for j in range ( i + 1, n ): p1 = 1.0 for k in range ( i, j ): p1 = p1 * x[k] p2 = 1.0 for k in range ( j + 1, n ): p2 = p2 * d[k] p3 = 1.0 for k in range ( i, n ): p3 = p3 * e[k] a[i,j] = r8_mop ( i + j ) * p1 * p2 / p3 return a def stirling_matrix ( m, n ): #*****************************************************************************80 # ## stirling_matrix() returns the STIRLING matrix. # # Comments: # # The absolute value of the Stirling number S1(I,J) gives the number # of permutations on I objects having exactly J cycles, while the # sign of the Stirling number records the sign (odd or even) of # the permutations. For example, there are six permutations on 3 objects: # # A B C 3 cycles (A) (B) (C) # A C B 2 cycles (A) (BC) # B A C 2 cycles (AB) (C) # B C A 1 cycle (ABC) # C A B 1 cycle (ABC) # C B A 2 cycles (AC) (B) # # There are # # 2 permutations with 1 cycle, and S1(3,1) = 2 # 3 permutations with 2 cycles, and S1(3,2) = -3, # 1 permutation with 3 cycles, and S1(3,3) = 1. # # Since there are N! permutations of N objects, the sum of the absolute # values of the Stirling numbers in a given row, # # sum ( 1 <= J <= I ) abs ( S1(I,J) ) = N! # # First terms: # # I/J: 1 2 3 4 5 6 7 8 # # 1 1 0 0 0 0 0 0 0 # 2 -1 1 0 0 0 0 0 0 # 3 2 -3 1 0 0 0 0 0 # 4 -6 11 -6 1 0 0 0 0 # 5 24 -50 35 -10 1 0 0 0 # 6 -120 274 -225 85 -15 1 0 0 # 7 720 -1764 1624 -735 175 -21 1 0 # 8 -5040 13068 -13132 6769 -1960 322 -28 1 # # Recursion: # # S1(I,1) = (-1)^(I-1) * (I-1)! for all I. # S1(I,I) = 1 for all I. # S1(I,J) = 0 if I < J. # # S1(I,J) = S1(I-1,J-1) - (I-1) * S1(I-1,J) # # Properties: # # A is generally not symmetric: A' /= A. # # A is integral: int ( A ) = A. # # A is lower triangular. # # det ( A ) = 1. # # A is unimodular. # # LAMBDA(1:N) = 1. # # After row 1, each row sums to 0. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 February 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the number of rows and columns of A. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( ( m, n ) ) a[0,0] = 1.0 for i in range ( 1, m ): a[i,0] = - float ( i ) * a[i-1,0] for j in range ( 1, n ): a[i,j] = a[i-1,j-1] - float ( i ) * a[i-1,j] return a def stirling_determinant ( n ): #*****************************************************************************80 # ## stirling_determinant() returns the determinant of the STIRLING matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 return value def stirling_inverse ( n ): #*****************************************************************************80 # ## stirling_inverse() returns the inverse of the STIRLING matrix. # # Comments: # # The inverse of S1, the matrix of Stirling numbers of the first kind, # is S2, the matrix of Stirling numbers of the second kind. # # S2(I,J) represents the number of distinct partitions of I elements # into J nonempty sets. For any I, the sum over J of the Stirling # numbers S2(I,J) is represented by B(I), called "Bell's number", # and represents the number of distinct partitions of I elements. # # For example, with 4 objects, there are: # # 1 partition into 1 set: # # (A,B,C,D) # # 7 partitions into 2 sets: # # (A,B,C) (D) # (A,B,D) (C) # (A,C,D) (B) # (A) (B,C,D) # (A,B) (C,D) # (A,C) (B,D) # (A,D) (B,C) # # 6 partitions into 3 sets: # # (A,B) (C) (D) # (A) (B,C) (D) # (A) (B) (C,D) # (A,C) (B) (D) # (A,D) (B) (C) # (A) (B,D) (C) # # 1 partition into 4 sets: # # (A) (B) (C) (D) # # So S2(4,1) = 1, S2(4,2) = 7, S2(4,3) = 6, S2(4,4) = 1, and B(4) = 15. # # # First terms: # # I/J: 1 2 3 4 5 6 7 8 # # 1 1 0 0 0 0 0 0 0 # 2 1 1 0 0 0 0 0 0 # 3 1 3 1 0 0 0 0 0 # 4 1 7 6 1 0 0 0 0 # 5 1 15 25 10 1 0 0 0 # 6 1 31 90 65 15 1 0 0 # 7 1 63 301 350 140 21 1 0 # 8 1 127 966 1701 1050 266 28 1 # # Recursion: # # S2(I,1) = 1 for all I. # S2(I,I) = 1 for all I. # S2(I,J) = 0 if I < J. # # S2(I,J) = J * S2(I-1,J) + S2(I-1,J-1) # # Properties: # # A is generally not symmetric: A' /= A. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # A is lower triangular. # # A is nonnegative. # # det ( A ) = 1. # # A is unimodular. # # LAMBDA(1:N) = 1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) a[0,0] = 1.0 for i in range ( 1, n ): a[i,0] = 1.0 for j in range ( 1, n ): a[i,j] = float ( j + 1 ) * a[i-1,j] + a[i-1,j-1] return a def sudoku_adj_matrix ( ): #*****************************************************************************80 # ## sudoku_adj_matrix() returns the Sudoku adjacency matrix. # # Discussion: # # A Sudoko is a 9x9 array, subdivided into 9 3x3 blocks. # # Two elements of the 9x9 array are adjacent if they lie in the same # row, column, or 3x3 subblock. # # The eigenvalues of the Sudoku adjacency matrix are all integers. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 February 2018 # # Author: # # John Burkardt # # Output: # # real A(81,81), the matrix. # import numpy as np a = np.zeros ( [ 81, 81 ] ) for i in range ( 0, 81 ): rowi = i // 9 coli = i % 9 browi = rowi // 3 bcoli = coli // 3 for j in range ( 0, 81 ): rowj = j // 9 colj = j % 9 browj = rowj // 3 bcolj = colj // 3 if ( rowi == rowj or coli == colj or ( browi == browj and bcoli == bcolj ) ): a[i,j] = 1 return a def summation_condition ( n ): #*****************************************************************************80 # ## summation_condition() returns the L1 condition of the SUMMATION matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 December 2014 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real COND, the L1 condition. # if ( n == 1 ): cond = 1.0 else: cond = 2.0 * float ( n ) return cond def summation_determinant ( n ): #*****************************************************************************80 # ## summation_determinant() returns the determinant of the SUMMATION matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 return value def summation_inverse ( n ): #*****************************************************************************80 # ## summation_inverse() returns the inverse of the summation matrix. # # Example: # # N = 5 # # 1 0 0 0 0 # -1 1 0 0 0 # 0 -1 1 0 0 # 0 0 -1 1 0 # 0 0 0 -1 1 # # Properties: # # A is lower triangular. # # A is lower bidiagonal. # # Because A is bidiagonal, it has property A (bipartite). # # A is Toeplitz: constant along diagonals. # # A is nonsingular. # # det ( A ) = 1. # # A is unimodular. # # LAMBDA(1:N) = 1. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A is the inverse of the summation matrix. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = 1.0 elif ( i == j + 1 ): a[i,j] = -1.0 return a def summation_matrix ( m, n ): #*****************************************************************************80 # ## summation_matrix() returns the summation matrix. # # Example: # # M = 5, N = 5 # # 1 0 0 0 0 # 1 1 0 0 0 # 1 1 1 0 0 # 1 1 1 1 0 # 1 1 1 1 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is lower triangular. # # A is a 0/1 matrix. # # The vector Y = A * X contains the partial sums of the vector X. # # A is Toeplitz: constant along diagonals. # # A is nonsingular. # # det ( A ) = 1. # # A is unimodular. # # LAMBDA(1:N) = 1. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A is the Cholesky factor of the MINIJ matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 December 2014 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( [ m, n ] ) for j in range ( 0, n ): for i in range ( 0, m ): if ( j <= i ): a[i,j] = 1.0 return a def sweet1_matrix ( ): #*****************************************************************************80 # ## sweet1_matrix() returns the SWEET1 matrix. # # Example: # # 20.0 15.0 2.5 6.0 1.0 -2.0 # 15.0 20.0 15.0 2.5 6.0 1.0 # 2.5 15.0 20.0 15.0 2.5 6.0 # 6.0 2.5 15.0 20.0 15.0 2.5 # 1.0 6.0 2.5 15.0 20.0 15.0 # -2.0 1.0 6.0 2.5 15.0 20.0 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is Toeplitz: constant along diagonals. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 February 2015 # # Author: # # John Burkardt # # Reference: # # Per Hansen, Tony Chan, # FORTRAN Subroutines for General Toeplitz Systems, # ACM Transactions on Mathematical Software, # Volume 18, Number 3, September 1992, pages 256-273. # # Douglas Sweet, # The use of pivoting to improve the numerical performance of # Toeplitz solvers, # In "Advanced Algorithms and Architectures for Signal Processing", # Edited by J M Speiser, # Proceedings SPIE 696, 1986, pages 8-18. # # Output: # # real A(6,6), the matrix. # import numpy as np n = 6 value = np.array ( [ 20.0, 15.0, 2.5, 6.0, 1.0, -2.0 ] ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = value[abs(j-i)] return a def sweet1_condition ( ): #*****************************************************************************80 # ## sweet1_condition() returns the L1 condition of the SWEET1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the condition. # a_norm = 61.0 b_norm = 0.278145899201815 value = a_norm * b_norm return value def sweet1_determinant ( ): #*****************************************************************************80 # ## sweet1_determinant() returns the determinant of the SWEET1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = - 2.0468186E+07 return value def sweet1_inverse ( ): #*****************************************************************************80 # ## sweet1_inverse() returns the inverse of the SWEET1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Output: # # real A(6,6), the matrix. # import numpy as np a = np.array ( [ \ [ 0.073125159943338, -0.029629732454063, -0.020045010339460, \ 0.032364910109767, -0.056244145182187, 0.052945000841794 ], \ [ -0.029629732454063, 0.046796984109877, 0.019214941666057, \ -0.056592264698005, 0.069667831091627, -0.056244145182187 ], \ [ -0.020045010339460, 0.019214941666057, 0.009031577102143, \ 0.035236537326757, -0.056592264698005, 0.032364910109767 ], \ [ 0.032364910109767, -0.056592264698005, 0.035236537326757, \ 0.009031577102143, 0.019214941666057, -0.020045010339460 ], \ [ -0.056244145182187, 0.069667831091627, -0.056592264698005, \ 0.019214941666057, 0.046796984109877, -0.029629732454063 ], \ [ 0.052945000841794, -0.056244145182187, 0.032364910109767, \ -0.020045010339460, -0.029629732454063, 0.073125159943338 ] ] ) return a def sweet2_matrix ( ): #*****************************************************************************80 # ## sweet2_matrix() returns the SWEET2 matrix. # # Example: # # 4.0 8.0 1.0 6.0 2.0 3.0 # 6.0 4.0 8.0 1.0 6.0 2.0 # 71/15 6.0 4.0 8.0 1.0 6.0 # 5.0 71/15 6.0 4.0 8.0 1.0 # 3.0 5.0 71/15 6.0 4.0 8.0 # 1.0 3.0 5.0 71/15 6.0 4.0 # # Properties: # # A is Toeplitz: constant along diagonals. # # A is generally not symmetric: A' /= A. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Reference: # # Per Hansen, Tony Chan, # FORTRAN Subroutines for General Toeplitz Systems, # ACM Transactions on Mathematical Software, # Volume 18, Number 3, September 1992, pages 256-273. # # Douglas Sweet, # The use of pivoting to improve the numerical performance of # Toeplitz solvers, # In "Advanced Algorithms and Architectures for Signal Processing", # Edited by J M Speiser, # Proceedings SPIE 696, 1986, pages 8-18. # # Output: # # real A(6,6), the matrix. # import numpy as np n = 6 v = np.array ( [ 1.0, 3.0, 5.0, 71.0 / 15.0, 6.0, 4.0, \ 8.0, 1.0, 6.0, 2.0, 3.0 ] ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = v[j-i+5] return a def sweet2_condition ( ): #*****************************************************************************80 # ## sweet2_condition() returns the L1 condition of the SWEET2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the condition. # a_norm = 30.733333333333334 b_norm = 1.601605164968818 value = a_norm * b_norm return value def sweet2_determinant ( ): #*****************************************************************************80 # ## sweet2_determinant() returns the determinant of the SWEET2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = 9.562518834567902E+03 return value def sweet2_inverse ( ): #*****************************************************************************80 # ## sweet2_inverse() returns the inverse of the SWEET2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Output: # # real A(6,6), the matrix. # import numpy as np a = np.array ( [ \ [ -0.188192659589482, -0.145188896312202, 0.063613055049687, \ 0.406962974759668, 0.271408731947181, -0.526238847310597 ], \ [ 0.324411348442568, 0.213721529181228, -0.131983821377206, \ -0.344055452089408, -0.168794206390780, 0.271408731947181 ], \ [ 0.038585525550130, 0.275974273184732, 0.137312031652403, \ -0.366985595257679, -0.344055452089408, 0.406962974759669 ], \ [ -0.105091418281329, -0.159756451255461, 0.216482246086901, \ 0.137312031652403, -0.131983821377206, 0.063613055049687 ], \ [ -0.043938024069266, -0.157319070822594, -0.159756451255461, \ 0.275974273184732, 0.213721529181228, -0.145188896312202 ], \ [ -0.054227038968746, -0.043938024069265, -0.105091418281329, \ 0.038585525550129, 0.324411348442568, -0.188192659589482 ] ] ) return a def sweet3_matrix ( ): #*****************************************************************************80 # ## sweet3_matrix() returns the SWEET3 matrix. # # Example: # # 8 4 1 6 2 3 # 4 8 4 1 6 2 # -34 4 8 4 1 6 # 5 -34 4 8 4 1 # 3 5 -34 4 8 4 # 1 3 5 -34 4 8 # # Properties: # # A is Toeplitz: constant along diagonals. # # A is generally not symmetric: A' /= A. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Reference: # # Per Hansen, Tony Chan, # FORTRAN Subroutines for General Toeplitz Systems, # ACM Transactions on Mathematical Software, # Volume 18, Number 3, September 1992, pages 256-273. # # Douglas Sweet, # The use of pivoting to improve the numerical performance of # Toeplitz solvers, # In "Advanced Algorithms and Architectures for Signal Processing", # Edited by J M Speiser, # Proceedings SPIE 696, 1986, pages 8-18. # # Output: # # real A(6,6), the matrix. # import numpy as np n = 6 v = np.array ( [ 1.0, 3.0, 5.0, -34.0, 4.0, 8.0, 4.0, \ 1.0, 6.0, 2.0, 3.0 ] ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = v[j-i+5] return a def sweet3_condition ( ): #*****************************************************************************80 # ## sweet3_condition() returns the L1 condition of the SWEET3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the condition. # a_norm = 58.0 b_norm = 0.427215561206108 value = a_norm * b_norm return value def sweet3_determinant ( ): #*****************************************************************************80 # ## sweet3_determinant() returns the determinant of the SWEET3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = - 5.4056067E+07 return value def sweet3_inverse ( ): #*****************************************************************************80 # ## sweet3_inverse() returns the inverse of the SWEET3 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Output: # # real A(6,6), the matrix. # import numpy as np a = np.array ( [ \ [ 0.041073816931594, -0.007888550234334, -0.020859268211281, \ 0.000304369165444, -0.003979664299291, 0.004165693371662 ], \ [ 0.008091247186000, 0.017910145035154, 0.000156985153951, \ -0.024742218112169, -0.001114102511380, -0.003979664299291 ], \ [ 0.006256245020564, 0.027534337635034, 0.003121055773444, \ 0.003970174152700, -0.024742218112169, 0.000304369165444 ], \ [ 0.038877153234252, -0.002789344626201, 0.008678729808441, \ 0.003121055773444, 0.000156985153951, -0.020859268211281 ], \ [ -0.119845197024785, 0.170102571465290, -0.002789344626201, \ 0.027534337635034, 0.017910145035154, -0.007888550234334 ], \ [ 0.213071901808913, -0.119845197024785, 0.038877153234252, \ 0.006256245020564, 0.008091247186000, 0.041073816931594 ] ] ) return a def sweet4_matrix ( ): #*****************************************************************************80 # ## sweet4_matrix() returns the SWEET4 matrix. # # Example: # # 5.0 -1.0 6.0 2.0 5.6 5.8 3.0 -5.0 -2.0 -7.0 1.0 10.0 -15.0 # 1.0 5.0 -1.0 6.0 2.0 5.6 5.8 3.0 -5.0 -2.0 -7.0 1.0 10.0 # -3.0 1.0 5.0 -1.0 6.0 2.0 5.6 5.8 3.0 -5.0 -2.0 -7.0 1.0 # 12.7 -3.0 1.0 5.0 -1.0 6.0 2.0 5.6 5.8 3.0 -5.0 -2.0 -7.0 # -19.6 12.7 -3.0 1.0 5.0 -1.0 6.0 2.0 5.6 5.8 3.0 -5.0 -2.0 # 28.3 -19.6 12.7 -3.0 1.0 5.0 -1.0 6.0 2.0 5.6 5.8 3.0 -5.0 # -7.0 28.3 -19.6 12.7 -3.0 1.0 5.0 -1.0 6.0 2.0 5.6 5.8 3.0 # -1.0 -7.0 28.3 -19.6 12.7 -3.0 1.0 5.0 -1.0 6.0 2.0 5.6 5.8 # 2.0 -1.0 -7.0 28.3 -19.6 12.7 -3.0 1.0 5.0 -1.0 6.0 2.0 5.6 # 1.0 2.0 -1.0 -7.0 28.3 -19.6 12.7 -3.0 1.0 5.0 -1.0 6.0 2.0 # -6.0 1.0 2.0 -1.0 -7.0 28.3 -19.6 12.7 -3.0 1.0 5.0 -1.0 6.0 # 1.0 -6.0 1.0 2.0 -1.0 -7.0 28.3 -19.6 12.7 -3.0 1.0 5.0 -1.0 # -0.5 1.0 -6.0 1.0 2.0 -1.0 -7.0 28.3 -19.6 12.7 -3.0 1.0 5.0 # # Properties: # # A is Toeplitz: constant along diagonals. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Reference: # # Per Hansen, Tony Chan, # FORTRAN Subroutines for General Toeplitz Systems, # ACM Transactions on Mathematical Software, # Volume 18, Number 3, September 1992, pages 256-273. # # Douglas Sweet, # The use of pivoting to improve the numerical performance of # Toeplitz solvers, # In "Advanced Algorithms and Architectures for Signal Processing", # Edited by J M Speiser, # Proceedings SPIE 696, 1986, pages 8-18. # # Output: # # real A(6,6), the matrix. # import numpy as np n = 13 v = np.array ( [ \ -0.5, 1.0, -6.0, 1.0, 2.0, \ -1.0, -7.0, 28.361, -19.656, 12.755, \ -3.0, 1.0, 5.0, -1.0, 6.0, \ 2.0, 5.697, 5.850, 3.0, -5.0, \ -2.0, -7.0, 1.0, 10.0, -15.0 ] ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = v[j-i+12] return a def sweet4_condition ( ): #*****************************************************************************80 # ## sweet4_condition() returns the L1 condition of the SWEET4 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the condition. # a_norm = 100.3190000000000 b_norm = 0.510081684645161 value = a_norm * b_norm return value def sweet4_determinant ( ): #*****************************************************************************80 # ## sweet4_determinant() returns the determinant of the SWEET4 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Output: # # real VALUE, the determinant. # value = - 6.463481763930611E+16 return value def sweet4_inverse ( ): #*****************************************************************************80 # ## sweet4_inverse() returns the inverse of the SWEET4 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Output: # # real A(13,13), the matrix. # import numpy as np # # Note that matrix entries are given by row. # a = np.array ( [ \ [ -0.006395453515049, 0.030690839549686, \ -0.002288997065175, -0.008539260151857, \ -0.001015137652004, 0.040513470913244, \ 0.017598472282428, -0.008312925397734, \ -0.015546543686421, -0.010969455314610, \ -0.017014452081345, -0.017669033095207, \ -0.013805699365025 ], \ [ 0.004338135763774, 0.039852868508471, \ -0.006409462970417, -0.010789166315387, \ 0.023605183638394, 0.023524498024753, \ 0.032221111978773, 0.010175588114759, \ -0.018129776994110, -0.028500341074603, \ -0.029318921760199, -0.030615698849391, \ -0.017669033095207 ], \ [ 0.011852844358462, 0.033292080046396, \ -0.005374341111703, -0.008875487063420, \ 0.031350558988152, 0.015098401236510, \ -0.004426214105193, 0.030910853378811, \ 0.012927937004693, -0.023901509668313, \ -0.035222171390576, -0.029318921760199, \ -0.017014452081345 ], \ [ 0.013846756886370, 0.028058421670586, \ -0.009388803334490, -0.004500416153857, \ 0.032089285374445, 0.007746385727172, \ -0.018511813509106, -0.002525445590655, \ 0.039475608232317, 0.011543138436698, \ -0.023901509668313, -0.028500341074603, \ -0.010969455314610 ], \ [ 0.009447720973799, 0.021796805754657, \ 0.000727759422194, -0.008130365160809, \ 0.021992767390463, 0.013573971521042, \ -0.015354921685074, -0.016609776210723, \ 0.004261697864111, 0.039475608232316, \ 0.012927937004693, -0.018129776994110, \ -0.015546543686421 ], \ [ 0.009432787993907, 0.039704365747118, \ -0.018354056201609, -0.002772215599655, \ 0.028789202755591, 0.020818744033636, \ -0.008277808905384, -0.017802710611741, \ -0.016609776210723, -0.002525445590655, \ 0.030910853378811, 0.010175588114759, \ -0.008312925397734 ], \ [ 0.006050784346575, 0.020779138484695, \ 0.018595613535238, -0.018881036665831, \ 0.017128957468121, 0.021782629702447, \ 0.006363468918819, -0.008277808905384, \ -0.015354921685074, -0.018511813509106, \ -0.004426214105193, 0.032221111978773, \ 0.017598472282428 ], \ [ -0.001688517566864, -0.071337491505107, \ 0.069446707802933, 0.034560078451674, \ -0.059246627902032, -0.038486648845696, \ 0.021782629702447, 0.020818744033636, \ 0.013573971521042, 0.007746385727172, \ 0.015098401236510, 0.023524498024753, \ 0.040513470913244 ], \ [ -0.024098383394697, -0.082853404494777, \ 0.033466389466084, 0.079212314240954, \ -0.061573703805162, -0.059246627902032, \ 0.017128957468121, 0.028789202755591, \ 0.021992767390463, 0.032089285374445, \ 0.031350558988152, 0.023605183638394, \ -0.001015137652004 ], \ [ -0.014571843537603, 0.050761162107706, \ -0.090910979018549, 0.012959017667649, \ 0.079212314240954, 0.034560078451674, \ -0.018881036665831, -0.002772215599655, \ -0.008130365160809, -0.004500416153857, \ -0.008875487063420, -0.010789166315387, \ -0.008539260151857 ], \ [ 0.006620954487991, -0.004862149070269, \ 0.029222791279654, -0.090910979018549, \ 0.033466389466084, 0.069446707802933, \ 0.018595613535238, -0.018354056201609, \ 0.000727759422194, -0.009388803334490, \ -0.005374341111703, -0.006409462970417, \ -0.002288997065175 ], \ [ 0.017905883190490, -0.068187074515203, \ -0.004862149070269, 0.050761162107706, \ -0.082853404494777, -0.071337491505107, \ 0.020779138484695, 0.039704365747118, \ 0.021796805754657, 0.028058421670586, \ 0.033292080046396, 0.039852868508471, \ 0.030690839549686 ], \ [ -0.031068329896258, 0.017905883190490, \ 0.006620954487991, -0.014571843537603, \ -0.024098383394697, -0.001688517566864, \ 0.006050784346575, 0.009432787993907, \ 0.009447720973799, 0.013846756886370, \ 0.011852844358462, 0.004338135763774, \ -0.006395453515049 ] ] ) return a def sylvester_kac_matrix ( n ): #*****************************************************************************80 # ## sylvester_kac_matrix() returns the SYLVESTER_KAC matrix. # # Formula: # # If J = I - 1 # A(I,J) = N + 1 - I # If J = I + 1 # A(I,J) = I # # Example: # # N = 5, # # 0 1 0 0 0 # 4 0 2 0 0 # 0 3 0 3 0 # 0 0 2 0 4 # 0 0 0 1 0 # # Properties: # # A is generally not symmetric: A' /= A. # # A is tridiagonal. # # If N is odd, the eigenvalues are: # -(N-1), -(N-3), ..., -2, 0, 2, ... (N-3), (N-1). # # If N is even, the eigenvalues are: # -(N-1), -(N-3), ..., -1, +1, ..., (N-3), (N-1). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 April 2015 # # Author: # # John Burkardt # # Reference: # # Paul Clement, # A class of triple-diagonal matrices for test purposes, # SIAM Review, # Volume 1, 1959, pages 50-52. # # Input: # # integer N, the number of rows and columns of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n - 1 ): a[i,i+1] = float ( i + 1 ) a[i+1,i] = float ( n - i - 1 ) return a def sylvester_kac_determinant ( n ): #*****************************************************************************80 # ## sylvester_kac_determinant() returns the determinant of the SYLVESTER_KAC matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # if ( ( n % 2 ) == 1 ): value = 0.0 else: value = 1.0 for i in range ( - n + 1, n + 1, 2 ): value = value * float ( i ) return value def sylvester_kac_eigen_right ( n ): #*****************************************************************************80 # ## sylvester_kac_eigen_right(): right eigenvectors of the SYLVESTER_KAC matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real V(N,N), the right eigenvectors. # import numpy as np b = np.zeros ( n - 1 ) for i in range ( 0, n - 1 ): b[i] = float ( i + 1 ) c = np.zeros ( n - 1 ) for i in range ( 0, n - 1 ): c[i] = float ( n - 1 - i ) v = np.zeros ( ( n, n ) ) for j in range ( 0, n ): lam = float ( - n + 1 + 2 * j ) a = np.zeros ( n ) a[0] = 1.0 a[1] = - lam for i in range ( 2, n ): a[i] = - lam * a[i-1] - b[i-2] * c[i-2] * a[i-2] bot = 1.0 v[0,j] = 1.0 for i in range ( 1, n ): bot = bot * b[i-1] v[i,j] = r8_mop ( i ) * a[i] / bot return v def sylvester_kac_eigenvalues ( n ): #*****************************************************************************80 # ## sylvester_kac_eigenvalues() returns the eigenvalues of the SYLVESTER_KAC matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n ) for i in range ( 0, n ): lam[i] = float ( - n + 1 + 2 * i ) return lam def sylvester_kac_inverse ( n ): #*****************************************************************************80 # ## sylvester_kac_inverse() returns the inverse of the SYLVESTER_KAC matrix. # # Example: # # N = 6: # # 0 1/5 0 -2/15 0 8/15 # 1 0 0 0 0 0 # 0 0 0 1/3 0 -4/3 # -4/3 0 1/3 0 0 0 # 0 0 0 0 0 1 # 8/15 0 -2/15 0 1/5 0 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np if ( ( n % 2 ) == 1 ): print ( '' ) print ( 'sylvester_kac_inverse(): Fatal error!' ) print ( ' The matrix is singular for odd N.' ) raise Exception ( 'sylvester_kac_inverse(): Fatal error!' ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): if ( ( i % 2 ) == 0 ): for j in range ( i, n - 1, 2 ): if ( j == i ): prod1 = 1.0 / float ( n - 1 - j ) prod2 = 1.0 / float ( 1 + j ) else: prod1 = - prod1 * float ( j ) / float ( n - 1 - j ) prod2 = - prod2 * float ( n - j ) / float ( 1 + j ) a[i,j+1] = prod1 a[j+1,i] = prod2 return a def symmetric_random_matrix ( n, d, key ): #*****************************************************************************80 # ## symmetric_random_matrix() returns the symmetric_random matrix. # # Properties: # # A is symmetric: A' = A. # # The eigenvalues of A will be real. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real D(N), the desired eigenvalues for the matrix. # # integer KEY, a positive integer that selects the data. # # Output: # # real A(N,N), the matrix. # import numpy as np # # Get a random orthogonal matrix Q. # q = orthogonal_random_matrix ( n, key ) # # Set A = Q * Lambda * Q'. # a = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, n ): for k in range ( 0, n ): a[i,j] = a[i,j] + q[i,k] * d[k] * q[j,k] return a def symmetric_random_determinant ( n, d, key ): #*****************************************************************************80 # ## symmetric_random_determinant(): determinant of the symmetric_random matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real D(N), the desired eigenvalues for the matrix. # # integer KEY, a positive integer that selects the data. # # Output: # # real VALUE, the determinant. # import numpy as np value = np.prod ( d ) return value def symmetric_random_eigen_left ( n, d, key ): #*****************************************************************************80 # ## symmetric_random_eigen_left(): left eigenvectors for the symmetric_random matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real D(N), the desired eigenvalues for the matrix. # # integer KEY, a positive integer that selects the data. # # Output: # # real V(N,N), the vectors. # # # Get a random orthogonal matrix Q. # x = orthogonal_random_matrix ( n, key ) # # Transpose. # for i in range ( 0, n ): for j in range ( 0, i ): t = x[i,j] x[i,j] = x[j,i] x[j,i] = t return x def symmetric_random_eigen_right ( n, d, key ): #*****************************************************************************80 # ## symmetric_random_eigen_right(): right eigenvectors for the symmetric_random matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real D(N), the desired eigenvalues for the matrix. # # integer KEY, a positive integer that selects the data. # # Output: # # real V(N,N), the vectors. # # # Get a random orthogonal matrix Q. # x = orthogonal_random_matrix ( n, key ) return x def symmetric_random_eigenvalues ( n, d, key ): #*****************************************************************************80 # ## symmetric_random_eigenvalues(): eigenvalues for the symmetric_random matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real D(N), the desired eigenvalues for the matrix. # # integer KEY, a positive integer that selects the data. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.copy ( d ) return lam def symmetric_random_inverse ( n, d, key ): #*****************************************************************************80 # ## symmetric_random_inverse(): inverse of the symmetric_random matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real D(N), the desired eigenvalues for the matrix. # # integer KEY, a positive integer that selects the data. # # Output: # # real A(N,N), the matrix. # import numpy as np # # Get a random orthogonal matrix Q. # q = orthogonal_random_matrix ( n, key ) # # Set A = Q * 1/Lambda * Q'. # a = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, n ): for k in range ( 0, n ): a[i,j] = a[i,j] + q[i,k] * ( 1.0 / d[k] ) * q[j,k] return a def test_condition ( rng ): #*****************************************************************************80 # ## test_condition() tests the L1 condition number computations. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 October 2022 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_condition()' ) print ( ' Compute the L1 condition number of test matrices' ) print ( '' ) print ( ' Title N COND COND COND' ) print ( ' Reported by Norms np.linalg.cond()' ) print ( '' ) # # aegerter # title = 'aegerter' n = 5 cond1 = aegerter_condition ( n ) a = aegerter_matrix ( n ) b = aegerter_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # antisummation # title = 'antisummation' n = 5 cond1 = antisummation_condition ( n ) a = antisummation_matrix ( n, n ) b = antisummation_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # bab # title = 'bab' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = bab_condition ( n, alpha, beta ) a = bab_matrix ( n, alpha, beta ) b = bab_inverse ( n, alpha, beta ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # bauer # title = 'bauer' n = 6 cond1 = bauer_condition ( ) a = bauer_matrix ( ) b = bauer_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # bernstein # title = 'bernstein' n = 5 cond1 = bernstein_condition ( n ) a = bernstein_matrix ( n ) b = bernstein_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # bis # title = 'bis' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = bis_condition ( alpha, beta, n ) a = bis_matrix ( alpha, beta, n, n ) b = bis_inverse ( alpha, beta, n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # biw # title = 'biw' n = 5 cond1 = biw_condition ( n ) a = biw_matrix ( n ) b = biw_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # bodewig # title = 'bodewig' n = 4 cond1 = bodewig_condition ( ) a = bodewig_matrix ( ) b = bodewig_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # boothroyd # title = 'boothroyd' n = 5 cond1 = boothroyd_condition ( n ) a = boothroyd_matrix ( n ) b = boothroyd_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # combin # title = 'combin' n = 3 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = combin_condition ( alpha, beta, n ) a = combin_matrix ( alpha, beta, n ) b = combin_inverse ( alpha, beta, n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # companion # title = 'companion' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) cond1 = companion_condition ( n, x ) a = companion_matrix ( n, x ) b = companion_inverse ( n, x ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # conex1 # title = 'conex1' n = 4 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = conex1_condition ( alpha ) a = conex1_matrix ( alpha ) b = conex1_inverse ( alpha ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # conex2 # title = 'conex2' n = 3 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = conex2_condition ( alpha ) a = conex2_matrix ( alpha ) b = conex2_inverse ( alpha ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # conex3 # title = 'conex3' n = 5 cond1 = conex3_condition ( n ) a = conex3_matrix ( n ) b = conex3_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # conex4 # title = 'conex4' n = 4 cond1 = conex4_condition ( ) a = conex4_matrix ( ) b = conex4_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # daub2 # title = 'daub2' n = 4 cond1 = daub2_condition ( n ) a = daub2_matrix ( n ) b = daub2_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # daub4 # title = 'daub4' n = 8 cond1 = daub4_condition ( n ) a = daub4_matrix ( n ) b = daub4_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # daub6 # title = 'daub6' n = 12 cond1 = daub6_condition ( n ) a = daub6_matrix ( n ) b = daub6_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # daub8 # title = 'daub8' n = 16 cond1 = daub8_condition ( n ) a = daub8_matrix ( n ) b = daub8_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # daub10 # title = 'daub10' n = 20 cond1 = daub10_condition ( n ) a = daub10_matrix ( n ) b = daub10_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # daub12 # title = 'daub12' n = 24 cond1 = daub12_condition ( n ) a = daub12_matrix ( n ) b = daub12_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # diagonal # title = 'diagonal' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) cond1 = diagonal_condition ( n, x ) a = diagonal_matrix ( n, n, x ) b = diagonal_inverse ( n, x ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # dif2 # title = 'dif2' n = 5 cond1 = dif2_condition ( n ) a = dif2_matrix ( n, n ) b = dif2_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # downshift # title = 'downshift' n = 5 cond1 = downshift_condition ( n ) a = downshift_matrix ( n ) b = downshift_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # exchange # title = 'exchange' n = 5 cond1 = exchange_condition ( n ) a = exchange_matrix ( n, n ) b = exchange_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # fibonacci2 # title = 'fibonacci2' n = 5 cond1 = fibonacci2_condition ( n ) a = fibonacci2_matrix ( n ) b = fibonacci2_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # gfpp # title = 'gfpp' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = gfpp_condition ( n, alpha ) a = gfpp_matrix ( n, alpha ) b = gfpp_inverse ( n, alpha ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # givens # title = 'givens' n = 5 cond1 = givens_condition ( n ) a = givens_matrix ( n, n ) b = givens_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) # # golub # title = 'golub' n = 5 key = 123456789 cond1 = golub_condition ( n, key ) a = golub_matrix ( n, key ) b = golub_inverse ( n, key ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # hankel_n # title = 'hankel_n' n = 5 cond1 = hankel_n_condition ( n ) a = hankel_n_matrix ( n ) b = hankel_n_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # hanowa # title = 'hanowa' alpha = rng.standard_normal ( ) n = 6 cond1 = hanowa_condition ( alpha, n ) a = hanowa_matrix ( alpha, n ) b = hanowa_inverse ( alpha, n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # harman # title = 'harman' n = 8 cond1 = harman_condition ( ) a = harman_matrix ( ) b = harman_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # hartley # title = 'hartley' n = 5 cond1 = hartley_condition ( n ) a = hartley_matrix ( n ) b = hartley_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # helmert # title = 'helmert' n = 5 cond1 = helmert_condition ( n ) a = helmert_matrix ( n ) b = helmert_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # herndon # title = 'herndon' n = 5 cond1 = herndon_condition ( n ) a = herndon_matrix ( n ) b = herndon_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # hilbert # title = 'hilbert' n = 5 cond1 = hilbert_condition ( n ) a = hilbert_matrix ( n, n ) b = hilbert_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # identity # title = 'identity' n = 5 cond1 = identity_condition ( n ) a = identity_matrix ( n, n ) b = identity_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # ill3 # title = 'ill3' n = 3 cond1 = ill3_condition ( ) a = ill3_matrix ( ) b = ill3_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # jordan # title = 'jordan' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = jordan_condition ( n, alpha ) a = jordan_matrix ( n, n, alpha ) b = jordan_inverse ( n, alpha ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # kahan # title = 'kahan' n = 5 alpha = 2.0 * rng.standard_normal ( ) cond1 = kahan_condition ( alpha, n ) a = kahan_matrix ( alpha, n, n ) b = kahan_inverse ( alpha, n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # kershaw # title = 'kershaw' n = 4 cond1 = kershaw_condition ( ) a = kershaw_matrix ( ) b = kershaw_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # lietzke # title = 'lietzke' n = 5 cond1 = lietzke_condition ( n ) a = lietzke_matrix ( n ) b = lietzke_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # maxij # title = 'maxij' n = 5 cond1 = maxij_condition ( n ) a = maxij_matrix ( n, n ) b = maxij_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # minij # title = 'minij' n = 5 cond1 = minij_condition ( n ) a = minij_matrix ( n, n ) b = minij_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # moler3 # title = 'moler3' n = 5 cond1 = moler3_condition ( n ) a = moler3_matrix ( n, n ) b = moler3_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # orthogonal_symmetric # title = 'orthogonal_symmetric' n = 5 cond1 = orthogonal_symmetric_condition ( n ) a = orthogonal_symmetric_matrix ( n ) b = orthogonal_symmetric_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # oto # title = 'oto' n = 5 cond1 = oto_condition ( n ) a = oto_matrix ( n, n ) b = oto_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # pascal1 # title = 'pascal1' n = 5 cond1 = pascal1_condition ( n ) a = pascal1_matrix ( n ) b = pascal1_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # pascal3 # title = 'pascal3' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = pascal3_condition ( n, alpha ) a = pascal3_matrix ( n, alpha ) b = pascal3_inverse ( n, alpha ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # pei # title = 'pei' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = pei_condition ( alpha, n ) a = pei_matrix ( alpha, n ) b = pei_inverse ( alpha, n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # rodman # title = 'rodman' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = rodman_condition ( n, alpha ) a = rodman_matrix ( n, n, alpha ) b = rodman_inverse ( n, alpha ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # rutis1 # title = 'rutis1' n = 4 cond1 = rutis1_condition ( ) a = rutis1_matrix ( ) b = rutis1_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # rutis2 # title = 'rutis2' n = 4 cond1 = rutis2_condition ( ) a = rutis2_matrix ( ) b = rutis2_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # rutis3 # title = 'rutis3' n = 4 cond1 = rutis3_condition ( ) a = rutis3_matrix ( ) b = rutis3_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # rutis4 # title = 'rutis4' n = 5 cond1 = rutis4_condition ( n ) a = rutis4_matrix ( n ) b = rutis4_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # rutis5 # title = 'rutis5' n = 4 cond1 = rutis5_condition ( ) a = rutis5_matrix ( ) b = rutis5_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # summation # title = 'summation' n = 5 cond1 = summation_condition ( n ) a = summation_matrix ( n, n ) b = summation_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # sweet1 # title = 'sweet1' n = 6 cond1 = sweet1_condition ( ) a = sweet1_matrix ( ) b = sweet1_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # sweet2 # title = 'sweet2' n = 6 cond1 = sweet2_condition ( ) a = sweet2_matrix ( ) b = sweet2_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # sweet3 # title = 'sweet3' n = 6 cond1 = sweet3_condition ( ) a = sweet3_matrix ( ) b = sweet3_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # sweet4 # title = 'sweet4' n = 13 cond1 = sweet4_condition ( ) a = sweet4_matrix ( ) b = sweet4_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # tri_upper # title = 'tri_upper' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) cond1 = tri_upper_condition ( alpha, n ) a = tri_upper_matrix ( alpha, n ) b = tri_upper_inverse ( alpha, n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # upshift # title = 'upshift' n = 5 cond1 = upshift_condition ( n ) a = upshift_matrix ( n ) b = upshift_inverse ( n ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # wilk03 # title = 'wilk03' n = 3 cond1 = wilk03_condition ( ) a = wilk03_matrix ( ) b = wilk03_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # wilk04 # title = 'wilk04' n = 4 cond1 = wilk04_condition ( ) a = wilk04_matrix ( ) b = wilk04_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # wilk05 # title = 'wilk05' n = 5 cond1 = wilk05_condition ( ) a = wilk05_matrix ( ) b = wilk05_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) # # wilson # title = 'wilson' n = 4 cond1 = wilson_condition ( ) a = wilson_matrix ( ) b = wilson_inverse ( ) a_norm = np.linalg.norm ( a, 1 ) b_norm = np.linalg.norm ( b, 1 ) cond2 = a_norm * b_norm cond3 = np.linalg.cond ( a, 1 ) print ( ' %-20s %4d %14.6g %14.6g %14.6g' % ( title, n, cond1, cond2, cond3 ) ) return def test_determinant ( rng ): #*****************************************************************************80 # ## test_determinant() tests the determinant computations. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 October 2022 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_determinant():' ) print ( ' Compute the determinants of an example of each' ) print ( ' test matrix compare with the determinant routine,' ) print ( ' if available. Print the matrix Frobenius norm' ) print ( ' for an estimate of magnitude.' ) print ( '' ) print ( ' Title N Determ Determ ||A||' ) print ( '' ) # # a123 # title = 'a123' n = 3 a = a123_matrix ( ) determ1 = a123_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # aegerter # title = 'aegerter' n = 5 a = aegerter_matrix ( n ) determ1 = aegerter_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # anticirculant # title = 'anticirculant' for n in range ( 3, 6 ): r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) # a = anticirculant_matrix ( n, n, x ) # determ1 = anticirculant_determinant ( n, x ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # ANTIHADAMARD # title = 'ANTIHADAMARD' n = 5 # a = antihadamard_matrix ( n ) # determ1 = antihadamard_determinant ( n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # antisummation # title = 'antisummation' n = 5 a = antisummation_matrix ( n, n ) determ1 = antisummation_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # antisymmetric_random # title = 'antisymmetric_random' for n in range ( 5, 7 ): key = 123456789 a = antisymmetric_random_matrix ( n, key ) determ1 = antisymmetric_random_determinant ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # bab # title = 'bab' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = bab_matrix ( n, alpha, beta ) determ1 = bab_determinant ( n, alpha, beta ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # bauer # title = 'bauer' n = 6 a = bauer_matrix ( ) determ1 = bauer_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # bernstein # title = 'bernstein' n = 5 a = bernstein_matrix ( n ) determ1 = bernstein_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # bimarkov_random # title = 'bimarkov_random' n = 5 key = 123456789 a = bimarkov_random_matrix ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # bis # title = 'bis' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = bis_matrix ( alpha, beta, n, n ) determ1 = bis_determinant ( alpha, beta, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # biw # title = 'biw' n = 5 a = biw_matrix ( n ) determ1 = biw_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # bodewig # title = 'bodewig' n = 4 a = bodewig_matrix ( ) determ1 = bodewig_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # boothroyd # title = 'boothroyd' n = 5 a = boothroyd_matrix ( n ) determ1 = boothroyd_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # borderband # title = 'borderband' n = 5 a = borderband_matrix ( n ) determ1 = borderband_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # carry # title = 'carry' n = 5 k = rng.integers ( low = 2, high = 20, endpoint = True ) a = carry_matrix ( n, k ) determ1 = carry_determinant ( n, k ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # cauchy # title = 'cauchy' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = cauchy_matrix ( n, x, y ) determ1 = cauchy_determinant ( n, x, y ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # cheby_diff1 # title = 'cheby_diff1' for n in range ( 5, 7 ): a = cheby_diff1_matrix ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # cheby_t # title = 'cheby_t' n = 5 a = cheby_t_matrix ( n ) determ1 = cheby_t_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # cheby_u # title = 'cheby_u' n = 5 a = cheby_u_matrix ( n ) determ1 = cheby_u_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # cheby_van1 # title = 'cheby_van1' n = 5 r8_lo = -1.0 r8_hi = +1.0 x = np.linspace ( r8_lo, r8_hi, n ) a = cheby_van1_matrix ( n, r8_lo, r8_hi, n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # cheby_van2 # for n in range ( 2, 11 ): title = 'cheby_van2' a = cheby_van2_matrix ( n ) determ1 = cheby_van2_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # cheby_van3 # title = 'cheby_van3' n = 5 a = cheby_van3_matrix ( n ) determ1 = cheby_van3_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # chow # title = 'chow' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = chow_matrix ( alpha, beta, n, n ) determ1 = chow_determinant ( alpha, beta, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # circulant # title = 'circulant' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) # a = circulant_matrix ( n, n, x ) # determ1 = circulant_determinant ( n, x ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # circulant2 # title = 'CIRCULANT2' # for n in range ( 3, 6 ): # a = circulant2_matrix ( n ) # determ1 = circulant2_determinant ( n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # clement1 # title = 'clement1' for n in range ( 5, 7 ): a = clement1_matrix ( n ) determ1 = clement1_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # clement2 # title = 'clement2' for n in range ( 5, 7 ): r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) a = clement2_matrix ( n, x, y ) determ1 = clement2_determinant ( n, x, y ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # combin # title = 'combin' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = combin_matrix ( alpha, beta, n ) determ1 = combin_determinant ( alpha, beta, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # companion # title = 'companion' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = companion_matrix ( n, x ) determ1 = companion_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # complex_i # title = 'complex_i' n = 2 a = complex_i_matrix ( ) determ1 = complex_i_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # conex1 # title = 'conex1' n = 4 alpha = - 5.0 + 10.0 * rng.random ( ) a = conex1_matrix ( alpha ) determ1 = conex1_determinant ( alpha ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # conex2 # title = 'conex2' n = 3 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = conex2_matrix ( alpha ) determ1 = conex2_determinant ( alpha ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # conex3 # title = 'conex3' n = 5 a = conex3_matrix ( n ) determ1 = conex3_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # conex4 # title = 'conex4' n = 4 a = conex4_matrix ( ) determ1 = conex1_determinant ( alpha ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # conference # N-1 must be an odd prime, or a power of an odd prime. # title = 'conference' n = 6 a = conference_matrix ( n ) determ1 = conference_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # creation # title = 'creation' n = 5 a = creation_matrix ( n, n ) determ1 = creation_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # daub2 # title = 'daub2' n = 4 a = daub2_matrix ( n ) determ1 = daub2_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # daub4 # title = 'daub4' n = 8 a = daub4_matrix ( n ) determ1 = daub4_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # daub6 # title = 'daub6' n = 12 a = daub6_matrix ( n ) determ1 = daub6_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # daub8 # title = 'daub8' n = 16 a = daub8_matrix ( n ) determ1 = daub8_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # daub10 # title = 'daub10' n = 20 a = daub10_matrix ( n ) determ1 = daub10_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # daub12 # title = 'daub12' n = 24 a = daub12_matrix ( n ) determ1 = daub12_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # defective # title = 'defective' n = 2 a = defective_matrix ( ) determ1 = defective_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # diagonal # title = 'diagonal' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = diagonal_matrix ( n, n, x ) determ1 = diagonal_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # dif1 # title = 'dif1' for n in range ( 5, 7 ): a = dif1_matrix ( n, n ) determ1 = dif1_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # dif1cyclic # title = 'dif1cyclic' n = 5 a = dif1cyclic_matrix ( n ) determ1 = dif1cyclic_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # dif2 # title = 'dif2' n = 5 a = dif2_matrix ( n, n ) determ1 = dif2_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # dif2cyclic # title = 'dif2cyclic' n = 5 a = dif2cyclic_matrix ( n ) determ1 = dif2cyclic_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # dorr # title = 'dorr' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = dorr_matrix ( alpha, n ) determ1 = dorr_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # downshift # title = 'downshift' n = 5 a = downshift_matrix ( n ) determ1 = downshift_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # eberlein # title = 'eberlein' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = eberlein_matrix ( alpha, n ) determ1 = eberlein_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # eulerian # title = 'eulerian' n = 5 a = eulerian_matrix ( n, n ) determ1 = eulerian_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # exchange # title = 'exchange' n = 5 a = exchange_matrix ( n, n ) determ1 = exchange_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # fibonacci1 # title = 'fibonacci1' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = fibonacci1_matrix ( n, alpha, beta ) determ1 = fibonacci1_determinant ( n, alpha, beta ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # fibonacci2 # title = 'fibonacci2' n = 5 a = fibonacci2_matrix ( n ) determ1 = fibonacci2_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # fibonacci3 # title = 'fibonacci3' n = 5 a = fibonacci3_matrix ( n ) determ1 = fibonacci3_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # fiedler # title = 'fiedler' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = fiedler_matrix ( n, n, x ) determ1 = fiedler_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # forsythe # title = 'forsythe' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = forsythe_matrix ( alpha, beta, n ) determ1 = forsythe_determinant ( alpha, beta, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # fourier_cosine # title = 'fourier_cosine' n = 5 a = fourier_cosine_matrix ( n ) determ1 = fourier_cosine_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # fourier_sine # title = 'fourier_sine' n = 5 a = fourier_sine_matrix ( n ) determ1 = fourier_sine_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # frank # title = 'frank' n = 5 a = frank_matrix ( n ) determ1 = frank_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # gfpp # title = 'gfpp' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = gfpp_matrix ( n, alpha ) determ1 = gfpp_determinant ( n, alpha ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # givens # title = 'givens' n = 5 a = givens_matrix ( n, n ) determ1 = givens_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # gk323 # title = 'gk323' n = 5 a = gk323_matrix ( n, n ) determ1 = gk323_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # gk324 # title = 'gk324' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) a = gk324_matrix ( n, n, x ) determ1 = gk324_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # golub # title = 'golub' n = 5 key = 123456789 a = golub_matrix ( n, key ) determ1 = golub_determinant ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # grcar # title = 'grcar' m = 5 n = 5 i4_lo = 1 i4_hi = n - 1 k = rng.integers ( low = 1, high = n - 1, endpoint = True ) a = grcar_matrix ( m, n, k ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # hadamard # title = 'hadamard' n = 5 # a = hadamard ( n, n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # hankel # title = 'hankel' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = 2 * n - 1 ) a = hankel_matrix ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # hankel_n # title = 'hankel_n' n = 5 a = hankel_n_matrix ( n ) determ1 = hankel_n_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # hanowa # title = 'hanowa' n = 6 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = hanowa_matrix ( alpha, n ) determ1 = hanowa_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # harman # title = 'harman' n = 8 a = harman_matrix ( ) determ1 = harman_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # hartley # for n in range ( 5, 9 ): title = 'hartley' a = hartley_matrix ( n ) determ1 = hartley_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # helmert # title = 'helmert' n = 5 a = helmert_matrix ( n ) determ1 = helmert_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # helmert2 # title = 'helmert2' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = helmert2_matrix ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # hermite # title = 'hermite' n = 5 a = hermite_matrix ( n ) determ1 = hermite_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # herndon # title = 'herndon' n = 5 a = herndon_matrix ( n ) determ1 = herndon_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # hilbert # title = 'hilbert' n = 5 a = hilbert_matrix ( n, n ) determ1 = hilbert_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # householder # title = 'householder' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = householder_matrix ( n, x ) determ1 = householder_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # idempotent_random # title = 'idempotent_random' n = 5 i4_lo = 0 i4_hi = n rank = rng.integers ( low = 0, high = n, endpoint = True ) key = 123456789 a = idempotent_random_matrix ( n, rank, key ) determ1 = idempotent_random_determinant ( n, rank, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # identity # title = 'identity' n = 5 a = identity_matrix ( n, n ) determ1 = identity_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # ijfact1 # title = 'ijfact1' n = 5 a = ijfact1_matrix ( n ) determ1 = ijfact1_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # ijfact2 # title = 'ijfact2' n = 5 a = ijfact2_matrix ( n ) determ1 = ijfact2_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # ill3 # title = 'ill3' n = 3 a = ill3_matrix ( ) determ1 = ill3_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # integration # title = 'integration' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = integration_matrix ( alpha, n ) determ1 = integration_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # involutory # title = 'involutory' n = 5 a = involutory_matrix ( n ) determ1 = involutory_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # involutory_random # title = 'involutory_random' n = 5 i4_lo = 0 i4_hi = n rank = rng.integers ( low = 0, high = n, endpoint = True ) key = 123456789 a = involutory_random_matrix ( n, rank, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # jacobi # title = 'jacobi' for n in range ( 5, 7 ): a = jacobi_matrix ( n, n ) determ1 = jacobi_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # jordan # title = 'jordan' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = jordan_matrix ( n, n, alpha ) determ1 = jordan_determinant ( n, alpha ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # kahan # title = 'kahan' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = kahan_matrix ( alpha, n, n ) determ1 = kahan_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # kershaw # title = 'kershaw' n = 4 a = kershaw_matrix ( ) determ1 = kershaw_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # kershawtri # title = 'kershawtri' n = 5 x_n = ( ( n + 1 ) // 2 ) r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = x_n ) a = kershawtri_matrix ( n, x ) determ1 = kershawtri_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # kms # title = 'kms' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = kms_matrix ( alpha, n, n ) determ1 = kms_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # laguerre # title = 'laguerre' n = 5 a = laguerre_matrix ( n ) determ1 = laguerre_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # legendre # title = 'legendre' n = 5 a = legendre_matrix ( n ) determ1 = legendre_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # lehmer # title = 'lehmer' n = 5 a = lehmer_matrix ( n, n ) determ1 = lehmer_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # leslie # title = 'leslie' n = 4 b = 0.025 di = 0.010 da = 0.100 a = leslie_matrix ( b, di, da ) determ1 = leslie_determinant ( b, di, da ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # lesp # title = 'lesp' n = 5 a = lesp_matrix ( n, n ) determ1 = lesp_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # lietzke # title = 'lietzke' n = 5 a = lietzke_matrix ( n ) determ1 = lietzke_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # lights_out # title = 'lights_out' row_num = 5 col_num = 5 # n, a = lights_out_matrix ( row_num, col_num ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # line_adj # title = 'line_adj' for n in range ( 5, 7 ): a = line_adj_matrix ( n ) determ1 = line_adj_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # line_loop_adj # title = 'line_loop_adj' n = 5 a = line_loop_adj_matrix ( n ) determ1 = line_loop_adj_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # loewner # title = 'loewner' n = 5 r8_lo = -5.0 r8_hi = +5.0 w = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) z = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) # a = loewner_matrix ( w, x, y, z, n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # lotkin # title = 'lotkin' n = 5 a = lotkin_matrix ( n, n ) determ1 = lotkin_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # markov_random # title = 'markov_random' n = 5 key = 123456789 a = markov_random_matrix ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # maxij # title = 'maxij' n = 5 a = maxij_matrix ( n, n ) determ1 = maxij_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # milnes # title = 'milnes' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = milnes_matrix ( n, n, x ) determ1 = milnes_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # minij # title = 'minij' n = 5 a = minij_matrix ( n, n ) determ1 = minij_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # moler1 # title = 'moler1' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = moler1_matrix ( alpha, n, n ) determ1 = moler1_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # moler2 # title = 'moler2' n = 5 a = moler2_matrix ( ) determ1 = moler2_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # moler3 # title = 'moler3' n = 5 a = moler3_matrix ( n, n ) determ1 = moler3_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # moler4 # title = 'moler4' n = 4 a = moler4_matrix ( ) determ1 = moler4_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # neumann # title = 'neumann' row_num = 5 col_num = 5 n = row_num * col_num a = neumann_matrix ( row_num, col_num ) determ1 = neumann_determinant ( row_num, col_num ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # one # title = 'one' n = 5 a = one_matrix ( n, n ) determ1 = one_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # ortega # title = 'ortega' n = 5 r8_lo = -5.0 r8_hi = +5.0 v1 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) v2 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) v3 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = ortega_matrix ( n, v1, v2, v3 ) determ1 = ortega_determinant ( n, v1, v2, v3 ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # orthogonal_random # title = 'orthogonal_random' n = 5 key = 123456789 a = orthogonal_random_matrix ( n, key ) determ1 = orthogonal_random_determinant ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # orthogonal_symmetric # title = 'orthogonal_symmetric' n = 5 a = orthogonal_symmetric_matrix ( n ) determ1 = orthogonal_symmetric_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # oto # title = 'oto' n = 5 a = oto_matrix ( n, n ) determ1 = oto_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # parter # title = 'parter' n = 5 a = parter_matrix ( n, n ) determ1 = parter_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # pascal1 # title = 'pascal1' n = 5 a = pascal1_matrix ( n ) determ1 = pascal1_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # pascal2 # title = 'pascal2' n = 5 a = pascal2_matrix ( n ) determ1 = pascal2_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # pascal3 # title = 'pascal3' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = pascal3_matrix ( n, alpha ) determ1 = pascal3_determinant ( n, alpha ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # pei # title = 'pei' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = pei_matrix ( alpha, n ) determ1 = pei_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # permutation_random # title = 'permutation_random' n = 5 key = 123456789 a = permutation_random_matrix ( n, key ) determ1 = permutation_random_determinant ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # plu # title = 'plu' m = 5 n = 5 pivot = np.zeros ( n, dtype = np.int32 ) for i in range ( 0, n ): i4_lo = i i4_hi = n - 1 pivot[i] = rng.integers ( low = i, high = n - 1, endpoint = True ) a = plu_matrix ( n, pivot ) determ1 = plu_determinant ( n, pivot ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # poisson # title = 'poisson' row_num = 5 col_num = 5 n = row_num * col_num a = poisson_matrix ( row_num, col_num ) determ1 = poisson_determinant ( row_num, col_num ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # projection_random # k = n # title = 'projection_random' n = 5 k = 5 key = 123456789 a = projection_random_matrix ( n, k, key ) determ1 = projection_random_determinant ( n, k, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # projection_random # k < n # title = 'projection_random' n = 5 k = 3 key = 123456789 a = projection_random_matrix ( n, k, key ) determ1 = projection_random_determinant ( n, k, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # prolate # title = 'prolate' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) # a = prolate_matrix ( alpha, n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # rectangle_adj # title = 'rectangle_adj' row_num = 5 col_num = 5 n = row_num * col_num # a = rectangle_adj_matrix ( row_num, col_num, n ) # determ1 = rectangle_adj_determinant ( row_num, col_num ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # redheffer # title = 'redheffer' n = 5 a = redheffer_matrix ( n ) determ1 = redheffer_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # ref_random # title = 'ref_random' n = 5 prob = 0.85 key = 123456789 # a = ref_random_matrix ( n, n, prob, key ) # determ1 = ref_random_determinant ( n, prob, key ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # reflection_random # title = 'reflection_random' n = 5 key = 123456789 a, v = reflection_random_matrix ( n, key ) determ1 = reflection_random_determinant ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # riemann # title = 'riemann' n = 5 # a = riemann_matrix ( n, n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # ring_adj # for m in range ( 1, 9 ): n = m title = 'ring_adj' a = ring_adj_matrix ( m, n ) determ1 = ring_adj_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # ris # title = 'ris' n = 5 a = ris_matrix ( n ) determ1 = ris_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # rodman # title = 'rodman' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = rodman_matrix ( n, n, alpha ) determ1 = rodman_determinant ( n, alpha ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # rosser1 # title = 'rosser1' n = 8 a = rosser1_matrix ( ) determ1 = rosser1_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # routh # title = 'routh' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = routh_matrix ( n, x ) determ1 = routh_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # rutis1 # title = 'rutis1' n = 4 a = rutis1_matrix ( ) determ1 = rutis1_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # rutis2 # title = 'rutis2' n = 4 a = rutis2_matrix ( ) determ1 = rutis2_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # rutis3 # title = 'rutis3' n = 4 a = rutis3_matrix ( ) determ1 = rutis3_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # rutis4 # title = 'rutis4' n = 5 a = rutis4_matrix ( n ) determ1 = rutis4_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # rutis5 # title = 'rutis5' n = 4 a = rutis5_matrix ( ) determ1 = rutis5_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # schur_block # title = 'schur_block' n = 5 x_n = ( ( n + 1 ) // 2 ) r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = x_n ) y_n = ( n // 2 ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = y_n ) a = schur_block_matrix ( n, x, y ) determ1 = schur_block_determinant ( n, x, y ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # spd_random # title = 'spd_random' n = 5 key = 123456789 a = spd_random_matrix ( n, key ) determ1 = spd_random_determinant ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # skew_circulant # title = 'skew_circulant' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) # a = skew_circulant_matrix ( n, n, x ) # determ1 = skew_circulant_determinant ( n, x ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # smoke1 # (complex matrix) # if ( False ): title = 'smoke1' n = 5 # a = smoke1_matrix ( n ) # determ1 = smoke1_determinant ( n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # smoke2 # (complex matrix) # if ( False ): title = 'smoke2' n = 5 # a = smoke2_matrix ( n ) # determ1 = smoke2_determinant ( n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %14g %10.2g' % ( \ # title, n, determ1, determ2, norm_frobenius ) ) # # spline # title = 'spline' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) a = spline_matrix ( n, x ) determ1 = spline_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # stirling # title = 'stirling' n = 5 a = stirling_matrix ( n, n ) determ1 = stirling_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # stripe # title = 'stripe' n = 5 # a = stripe_matrix ( n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # summation # title = 'summation' n = 5 a = summation_matrix ( n, n ) determ1 = summation_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # sweet1 # title = 'sweet1' n = 6 a = sweet1_matrix ( ) determ1 = sweet1_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # sweet2 # title = 'sweet2' n = 6 a = sweet2_matrix ( ) determ1 = sweet2_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # sweet3 # title = 'sweet3' n = 6 a = sweet3_matrix ( ) determ1 = sweet3_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # sweet4 # title = 'sweet4' n = 13 a = sweet4_matrix ( ) determ1 = sweet4_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # sylvester # title = 'sylvester' n = 5 x_n = 3 + 1 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = x_n ) y_n = 2 + 1 y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = y_n ) # a = sylvester_matrix ( n, x_n - 1, x, y_n - 1, y ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # sylvester_kac # title = 'sylvester_kac' n = 5 a = sylvester_kac_matrix ( n ) determ1 = sylvester_kac_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # sylvester_kac # title = 'sylvester_kac' n = 6 a = sylvester_kac_matrix ( n ) determ1 = sylvester_kac_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # symmetric_random # title = 'symmetric_random' n = 5 r8_lo = -5.0 r8_hi = +5.0 d = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) key = 123456789 a = symmetric_random_matrix ( n, d, key ) determ1 = symmetric_random_determinant ( n, d, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # toeplitz # title = 'toeplitz' n = 5 x_n = 2 * n - 1 r8_lo = -5.0 r8_hi = +5.0 x = rng.standard_normal ( size = x_n ) a = toeplitz_matrix ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # toeplitz_5diag # title = 'toeplitz_5diag' n = 5 r8_lo = -5.0 r8_hi = +5.0 d1= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) d2= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) d3= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) d4= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) d5= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) # a = toeplitz_5diag_matrix ( n, d1, d2, d3, d4, d5 ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # toeplitz_5s # title = 'toeplitz_5s' row_num = 5 col_num = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) gamma = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) gamma = round ( 50.0 * gamma - 25.0 ) / 5.0 # n, a = toeplitz_5s_matrix ( row_num, col_num, alpha, beta, gamma ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # toeplitz_spd # title = 'toeplitz_spd' m = 3 n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = m ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = m ) # y_sum = r8vec_sum ( m, y ) # for i in range ( 0, m ): # y[i] = y[i] / y_sum # a = toeplitz_spd_matrix ( m, n, x, y ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # tournament_random # title = 'tournament_random' for n in range ( 7, 9 ): key = 123456789 a = tournament_random_matrix ( n, key ) determ1 = tournament_random_determinant ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # transition_random # title = 'transition_random' n = 5 key = 123456789 a = transition_random_matrix ( n, key ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # trench # title = 'trench' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) # a = trench_matrix ( alpha, n, n ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) # # tri_upper # title = 'tri_upper' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = tri_upper_matrix ( alpha, n ) determ1 = tri_upper_determinant ( alpha, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # tris # title = 'tris' n = 5 r8_lo = -5.0 r8_hi = +5.0 d1= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) d2= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) d3= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = tris_matrix ( n, n, d1, d2, d3 ) determ1 = tris_determinant ( n, d1, d2, d3 ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # triv # title = 'triv' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) z = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) a = triv_matrix ( n, x, y, z ) determ1 = triv_determinant ( n, x, y, z ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # triw # title = 'triw' n = 5 i4_lo = 0 i4_hi = n - 1 k = rng.integers ( low = 0, high = n - 1, endpoint = True ) r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = triw_matrix ( alpha, k, n ) determ1 = triw_determinant ( alpha, k, n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # upshift # title = 'upshift' n = 5 a = upshift_matrix ( n ) determ1 = upshift_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # vand1 # title = 'vand1' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = vand1_matrix ( n, x ) determ1 = vand1_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # vand2 # title = 'vand2' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = vand2_matrix ( n, x ) determ1 = vand2_determinant ( n, x ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # wathen # title = 'wathen' nx = 5 ny = 5 key = 123456789 A = wathen_matrix ( nx, ny, key ) m, n = A.shape determ2 = np.linalg.det ( A ) norm_frobenius = np.linalg.norm ( A, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # wilk03 # title = 'wilk03' n = 3 a = wilk03_matrix ( ) determ1 = wilk03_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # wilk04 # title = 'wilk04' n = 4 a = wilk04_matrix ( ) determ1 = wilk04_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # wilk05 # title = 'wilk05' n = 5 a = wilk05_matrix ( ) determ1 = wilk05_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # wilk12 # title = 'wilk12' n = 12 a = wilk12_matrix ( ) determ1 = wilk12_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # wilk20 # title = 'wilk20' n = 20 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = wilk20_matrix ( alpha ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %10.2g' % ( \ title, n, determ2, norm_frobenius ) ) # # wilk21 # title = 'wilk21' n = 21 a = wilk21_matrix ( n ) determ1 = wilk21_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # wilson # title = 'wilson' n = 4 a = wilson_matrix ( ) determ1 = wilson_determinant ( ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # zero # title = 'zero' n = 5 a = zero_matrix ( n, n ) determ1 = zero_determinant ( n ) determ2 = np.linalg.det ( a ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %14g %14g %10.2g' % ( \ title, n, determ1, determ2, norm_frobenius ) ) # # zielke # title = 'zielke' n = 5 r8_lo = -5.0 r8_hi = +5.0 d1= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) d2= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) d3= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) # a = zielke ( n, d1, d2, d3 ) # determ2 = np.linalg.det ( a ) # norm_frobenius = np.linalg.norm ( a, 'fro' ) # print ( ' %-20s %4d %14g %10.2g' % ( \ # title, n, determ2, norm_frobenius ) ) return def test_eigen_left ( rng ): #*****************************************************************************80 # ## test_eigen_left() tests left eigensystems. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 May 2020 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_eigen_left():' ) print ( ' Compute the Frobenius norm of the left eigensystem error:' ) print ( ' X * A - LAMBDA * X' ) print ( ' given K left eigenvectors X and eigenvalues lambda.' ) print ( '' ) print ( ' Title N K ||A|| ||X*A-LAMBDA*X||' ) print ( '' ) # # a123 # title = 'a123' n = 3 k = 3 a = a123_matrix ( ) lam = a123_eigenvalues ( ) x = a123_eigen_left ( ) error_frobenius = r8mat_is_eigen_left ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # carry # title = 'carry' n = 5 k = 5 i4_lo = 2 i4_hi = 20 i1 = rng.integers ( low = 2, high = 20, endpoint = True ) a = carry_matrix ( n, i1 ) lam = carry_eigenvalues ( n, i1 ) x = carry_eigen_left ( n, i1 ) error_frobenius = r8mat_is_eigen_left ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # chow # title = 'chow' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = chow_matrix ( alpha, beta, n, n ) lam = chow_eigenvalues ( alpha, beta, n ) x = chow_eigen_left ( alpha, beta, n ) error_frobenius = r8mat_is_eigen_left ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # diagonal # title = 'diagonal' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 d = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = diagonal_matrix ( n, n, d ) lam = diagonal_eigenvalues ( n, d ) x = diagonal_eigen_left ( n, d ) error_frobenius = r8mat_is_eigen_left ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # rosser1 # title = 'rosser1' n = 8 k = 8 a = rosser1_matrix ( ) lam = rosser1_eigenvalues ( ) x = rosser1_eigen_left ( ) error_frobenius = r8mat_is_eigen_left ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # symmetric_random # title = 'symmetric_random' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 d = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) key = 123456789 a = symmetric_random_matrix ( n, d, key ) lam = symmetric_random_eigenvalues ( n, d, key ) x = symmetric_random_eigen_left ( n, d, key ) error_frobenius = r8mat_is_eigen_left ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) return def test_eigen_right ( rng ): #*****************************************************************************80 # ## test_eigen_right(): tests right eigensystems. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2021 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_eigen_right():' ) print ( ' Compute the Frobenius norm of the right eigensystem error:' ) print ( ' A * X - X * lambda' ) print ( ' given K right eigenvectors X and eigenvalues lambda.' ) print ( '' ) print ( ' Title N K ||A|| ||A*X-X*LAMBDA||' ) print ( '' ) # # a123 # title = 'a123' n = 3 k = 3 a = a123_matrix ( ) lam = a123_eigenvalues ( ) x = a123_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # bab # title = 'bab' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = bab_matrix ( n, alpha, beta ) lam = bab_eigenvalues ( n, alpha, beta ) x = bab_eigen_right ( n, alpha, beta ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # bodewig # title = 'bodewig' n = 4 k = 4 a = bodewig_matrix ( ) lam = bodewig_eigenvalues ( ) x = bodewig_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # carry # title = 'carry' n = 5 k = 5 i4_lo = 2 i4_hi = 20 i1 = rng.integers ( low = 2, high = 20, endpoint = True ) a = carry_matrix ( n, i1 ) lam = carry_eigenvalues ( n, i1 ) x = carry_eigen_right ( n, i1 ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # chow # title = 'chow' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = chow_matrix ( alpha, beta, n, n ) lam = chow_eigenvalues ( alpha, beta, n ) x = chow_eigen_right ( alpha, beta, n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # combin # title = 'combin' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = combin_matrix ( alpha, beta, n ) lam = combin_eigenvalues ( alpha, beta, n ) x = combin_eigen_right ( alpha, beta, n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # dif2 # title = 'dif2' n = 5 k = 5 a = dif2_matrix ( n, n ) lam = dif2_eigenvalues ( n ) x = dif2_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # exchange # title = 'exchange' n = 5 k = 5 a = exchange_matrix ( n, n ) lam = exchange_eigenvalues ( n ) x = exchange_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # fibonacci2 # title = 'fibonacci2' n = 5 k = 5 a = fibonacci2_matrix ( n ) lam = fibonacci2_eigenvalues ( n ) x = fibonacci2_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # idempotent_random # title = 'idempotent_random' n = 5 k = 5 rank = 3 key = 123456789 a = idempotent_random_matrix ( n, rank, key ) lam = idempotent_random_eigenvalues ( n, rank, key ) x = idempotent_random_eigen_right ( n, rank, key ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # identity # title = 'identity' n = 5 k = 5 a = identity_matrix ( n, n ) lam = identity_eigenvalues ( n ) x = identity_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # ill3 # title = 'ill3' n = 3 k = 3 a = ill3_matrix ( ) lam = ill3_eigenvalues ( ) x = ill3_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # kershaw # title = 'kershaw' n = 4 k = 4 a = kershaw_matrix ( ) lam = kershaw_eigenvalues ( ) x = kershaw_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # kms # Eigenvalue information requires 0 <= ALPHA <= 1. # title = 'kms' n = 5 k = 5 r8_lo = +0.0 r8_hi = +1.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = kms_matrix ( alpha, n, n ) lam = kms_eigenvalues ( alpha, n ) x = kms_eigen_right ( alpha, n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # line_adj # title = 'line_adj' n = 5 k = 5 a = line_adj_matrix ( n ) lam = line_adj_eigenvalues ( n ) x = line_adj_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # line_loop_adj # title = 'line_loop_adj' n = 5 k = 5 a = line_loop_adj_matrix ( n ) lam = line_loop_adj_eigenvalues ( n ) x = line_loop_adj_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # one # title = 'one' n = 5 k = 5 a = one_matrix ( n, n ) lam = one_eigenvalues ( n ) x = one_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # ortega # title = 'ortega' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 v1 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) v2 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) v3 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = ortega_matrix ( n, v1, v2, v3 ) lam = ortega_eigenvalues ( n, v1, v2, v3 ) x = ortega_eigen_right ( n, v1, v2, v3 ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # oto # title = 'oto' n = 5 k = 5 a = oto_matrix ( n, n ) lam = oto_eigenvalues ( n ) x = oto_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # pei # title = 'pei' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = pei_matrix ( alpha, n ) lam = pei_eigenvalues ( alpha, n ) x = pei_eigen_right ( alpha, n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # reflection_random # title = 'reflection_random' n = 5 key = 123456789 a, v = reflection_random_matrix ( n, key ) lam = reflection_random_eigenvalues ( n, key ) x = reflection_random_eigen_right ( n, key ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # rodman # title = 'rodman' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = rodman_matrix ( n, n, alpha ) lam = rodman_eigenvalues ( n, alpha ) x = rodman_eigen_right ( n, alpha ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # rosser1 # title = 'rosser1' n = 8 k = 8 a = rosser1_matrix ( ) lam = rosser1_eigenvalues ( ) x = rosser1_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # rutis1 # title = 'rutis1' n = 4 k = 4 a = rutis1_matrix ( ) lam = rutis1_eigenvalues ( ) x = rutis1_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # rutis2 # title = 'rutis2' n = 4 k = 4 a = rutis2_matrix ( ) lam = rutis2_eigenvalues ( ) x = rutis2_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # rutis5 # title = 'rutis5' n = 4 k = 4 a = rutis5_matrix ( ) lam = rutis5_eigenvalues ( ) x = rutis5_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # spd_random # title = 'spd_random' n = 5 k = 5 key = 123456789 a = spd_random_matrix ( n, key ) lam = spd_random_eigenvalues ( n, key ) x = spd_random_eigen_right ( n, key ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # sylvester_kac # title = 'sylvester_kac' n = 5 k = 5 a = sylvester_kac_matrix ( n ) lam = sylvester_kac_eigenvalues ( n ) x = sylvester_kac_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # symmetric_random # title = 'symmetric_random' n = 5 k = 5 r8_lo = -5.0 r8_hi = +5.0 d = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) key = 123456789 a = symmetric_random_matrix ( n, d, key ) lam = symmetric_random_eigenvalues ( n, d, key ) x = symmetric_random_eigen_right ( n, d, key ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # tribonacci2 # title = 'tribonacci2' n = 5 k = 5 a = tribonacci2_matrix ( n ) lam = tribonacci2_eigenvalues ( n ) x = tribonacci2_eigen_right ( n ) error_frobenius = c8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = c8mat_norm_fro ( n, n, a ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # wilk12 # title = 'wilk12' n = 12 k = 12 a = wilk12_matrix ( ) lam = wilk12_eigenvalues ( ) x = wilk12_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # wilson # title = 'wilson' n = 4 k = 4 a = wilson_matrix ( ) lam = wilson_eigenvalues ( ) x = wilson_eigen_right ( ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) # # zero # title = 'zero' n = 5 k = 5 a = zero_matrix ( n, n ) lam = zero_eigenvalues ( n ) x = zero_eigen_right ( n ) error_frobenius = r8mat_is_eigen_right ( n, k, a, x, lam ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, n, k, norm_frobenius, error_frobenius ) ) return def test_inverse ( rng ): #*****************************************************************************80 # ## test_inverse() tests the inverse computations. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 October 2022 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_inverse()' ) print ( ' A = a test matrix of order N' ) print ( ' B = inverse as computed by a routine.' ) print ( ' C = inverse as computed by the numpy.linalg.inv() function.' ) print ( '' ) print ( ' ||A|| = Frobenius norm of A.' ) print ( ' ||C|| = Frobenius norm of C.' ) print ( ' ||I-AC|| = Frobenius norm of I-A*C.' ) print ( ' ||I-AB|| = Frobenius norm of I-A*B.' ) print ( '' ) print ( ' Title N ||A|| ||C|| ||I-AC|| ||I-AB||' ) print ( '' ) # # aegerter # title = 'aegerter' n = 5 a = aegerter_matrix ( n ) b = aegerter_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # antisummation # title = 'antisummation' n = 5 a = antisummation_matrix ( n, n ) b = antisummation_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # bab # title = 'bab' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = bab_matrix ( n, alpha, beta ) b = bab_inverse ( n, alpha, beta ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # bauer # title = 'bauer' n = 6 a = bauer_matrix ( ) b = bauer_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # bernstein # title = 'bernstein' n = 5 a = bernstein_matrix ( n ) b = bernstein_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # bis # title = 'bis' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = bis_matrix ( alpha, beta, n, n ) b = bis_inverse ( alpha, beta, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # biw # title = 'biw' n = 5 a = biw_matrix ( n ) b = biw_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # bodewig # title = 'bodewig' n = 4 a = bodewig_matrix ( ) b = bodewig_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # boothroyd # title = 'boothroyd' n = 5 a = boothroyd_matrix ( n ) b = boothroyd_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # borderband # title = 'borderband' n = 5 a = borderband_matrix ( n ) b = borderband_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # carry # title = 'carry' n = 5 i4_lo = 2 i4_hi = 20 k = rng.integers ( low = 2, high = 20, endpoint = True ) a = carry_matrix ( n, k ) b = carry_inverse ( n, k ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # cauchy # title = 'cauchy' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = cauchy_matrix ( n, x, y ) b = cauchy_inverse ( n, x, y ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # cheby_t # title = 'cheby_t' n = 5 a = cheby_t_matrix ( n ) b = cheby_t_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # cheby_u # title = 'cheby_u' n = 5 a = cheby_u_matrix ( n ) b = cheby_u_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # cheby_van2 # title = 'cheby_van2' n = 5 a = cheby_van2_matrix ( n ) b = cheby_van2_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # cheby_van3 # title = 'cheby_van3' n = 5 a = cheby_van3_matrix ( n ) b = cheby_van3_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # chow # title = 'chow' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = chow_matrix ( alpha, beta, n, n ) b = chow_inverse ( alpha, beta, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # CIRCULANT # Requires complex arithmetic. Not now! # title = 'CIRCULANT' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) # a = circulant_matrix ( n, n, x ) # b = circulant_inverse ( n, x ) # c = np.linalg.inv ( a ) # error_ab = r8mat_is_inverse ( n, a, b ) # error_ac = r8mat_is_inverse ( n, a, c ) # norma_frobenius = np.linalg.norm ( a, 'fro' ) # normc_frobenius = np.linalg.norm ( c, 'fro' ) # print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ # % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # CIRCULANT2 # Requires complex arithmetic. Not now! # title = 'CIRCULANT2' n = 5 # a = circulant2_matrix ( n ) # b = circulant2_inverse ( n ) # c = np.linalg.inv ( a ) # error_ab = r8mat_is_inverse ( n, a, b ) # error_ac = r8mat_is_inverse ( n, a, c ) # norma_frobenius = np.linalg.norm ( a, 'fro' ) # normc_frobenius = np.linalg.norm ( c, 'fro' ) # print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ # % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # clement1 # N must be even. # title = 'clement1' n = 6 a = clement1_matrix ( n ) b = clement1_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # clement2 # N must be even. # title = 'clement2' n = 6 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) a = clement2_matrix ( n, x, y ) b = clement2_inverse ( n, x, y ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # combin # title = 'combin' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = combin_matrix ( alpha, beta, n ) b = combin_inverse ( alpha, beta, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # companion # title = 'companion' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = companion_matrix ( n, x ) b = companion_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # complex_i # title = 'complex_i' n = 2 a = complex_i_matrix ( ) b = complex_i_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # conex1 # title = 'conex1' n = 4 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = conex1_matrix ( alpha ) b = conex1_inverse ( alpha ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # conex2 # title = 'conex2' n = 3 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = conex2_matrix ( alpha ) b = conex2_inverse ( alpha ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # conex3 # title = 'conex3' n = 5 a = conex3_matrix ( n ) b = conex3_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # conex4 # title = 'conex4' n = 4 a = conex4_matrix ( ) b = conex4_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # conference # N-1 must be an odd prime, or a power of an odd prime. # title = 'conference' n = 6 a = conference_matrix ( n ) b = conference_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # daub2 # title = 'daub2' n = 4 a = daub2_matrix ( n ) b = daub2_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # daub4 # title = 'daub4' n = 8 a = daub4_matrix ( n ) b = daub4_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # daub6 # title = 'daub6' n = 12 a = daub6_matrix ( n ) b = daub6_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # daub8 # title = 'daub8' n = 16 a = daub8_matrix ( n ) b = daub8_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # daub10 # title = 'daub10' n = 20 a = daub10_matrix ( n ) b = daub10_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # daub12 # title = 'daub12' n = 24 a = daub12_matrix ( n ) b = daub12_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # diagonal # title = 'diagonal' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = diagonal_matrix ( n, n, x ) b = diagonal_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # dif1 # N must be even. # title = 'dif1' n = 6 a = dif1_matrix ( n, n ) b = dif1_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # dif2 # title = 'dif2' n = 5 a = dif2_matrix ( n, n ) b = dif2_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # dorr # title = 'dorr' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = dorr_matrix ( alpha, n ) b = dorr_inverse ( alpha, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # downshift # title = 'downshift' n = 5 a = downshift_matrix ( n ) b = downshift_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # eulerian # title = 'eulerian' n = 5 a = eulerian_matrix ( n, n ) b = eulerian_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # exchange # title = 'exchange' n = 5 a = exchange_matrix ( n, n ) b = exchange_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # fibonacci2 # title = 'fibonacci2' n = 5 a = fibonacci2_matrix ( n ) b = fibonacci2_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # fibonacci3 # title = 'fibonacci3' n = 5 a = fibonacci3_matrix ( n ) b = fibonacci3_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # fiedler. # The fiedler_inverse routine assumes the X vector is sorted. # title = 'fiedler' n = 7 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) x = np.sort ( x ) a = fiedler_matrix ( n, n, x ) b = fiedler_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # forsythe # title = 'forsythe' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = forsythe_matrix ( alpha, beta, n ) b = forsythe_inverse ( alpha, beta, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # fourier_cosine # title = 'fourier_cosine' n = 5 a = fourier_cosine_matrix ( n ) b = fourier_cosine_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # fourier_sine # title = 'fourier_sine' n = 5 a = fourier_sine_matrix ( n ) b = fourier_sine_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # frank # title = 'frank' n = 5 a = frank_matrix ( n ) b = frank_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # gfpp # title = 'gfpp' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = gfpp_matrix ( n, alpha ) b = gfpp_inverse ( n, alpha ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # givens # title = 'givens' n = 5 a = givens_matrix ( n, n ) b = givens_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # gk323 # title = 'gk323' n = 5 a = gk323_matrix ( n, n ) b = gk323_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # gk324 # title = 'gk324' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) a = gk324_matrix ( n, n, x ) b = gk324_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # golub # title = 'golub' n = 5 key = 123456789 a = golub_matrix ( n, key ) b = golub_inverse ( n, key ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # hankel # title = 'hankel' n = 5 x = rng.standard_normal ( size = ( 2 * n - 1 ) ) a = hankel_matrix ( n, x ) b = hankel_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # hankel_n # title = 'hankel_n' n = 5 a = hankel_n_matrix ( n ) b = hankel_n_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # hanowa # N must be even. # title = 'hanowa' n = 6 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = hanowa_matrix ( alpha, n ) b = hanowa_inverse ( alpha, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # harman # title = 'harman' n = 8 a = harman_matrix ( ) b = harman_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # hartley # title = 'hartley' n = 5 a = hartley_matrix ( n ) b = hartley_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # helmert # title = 'helmert' n = 5 a = helmert_matrix ( n ) b = helmert_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # helmert2 # title = 'helmert2' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = helmert2_matrix ( n, x ) b = helmert2_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # hermite # title = 'hermite' n = 5 a = hermite_matrix ( n ) b = hermite_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # herndon # title = 'herndon' n = 5 a = herndon_matrix ( n ) b = herndon_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # hilbert # title = 'hilbert' n = 5 a = hilbert_matrix ( n, n ) b = hilbert_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # householder # title = 'householder' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = householder_matrix ( n, x ) b = householder_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # identity # title = 'identity' n = 5 a = identity_matrix ( n, n ) b = identity_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # ill3 # title = 'ill3' n = 3 a = ill3_matrix ( ) b = ill3_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # integration # title = 'integration' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = integration_matrix ( alpha, n ) b = integration_inverse ( alpha, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # involutory # title = 'involutory' n = 5 a = involutory_matrix ( n ) b = involutory_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # jacobi # N must be even. # title = 'jacobi' n = 6 a = jacobi_matrix ( n, n ) b = jacobi_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # jordan # title = 'jordan' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = jordan_matrix ( n, n, alpha ) b = jordan_inverse ( n, alpha ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # kahan # title = 'kahan' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = kahan_matrix ( alpha, n, n ) b = kahan_inverse ( alpha, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # kershaw # title = 'kershaw' n = 4 a = kershaw_matrix ( ) b = kershaw_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # kershawtri # title = 'kershawtri' n = 5 x_n = ( ( n + 1 ) // 2 ) r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = x_n ) a = kershawtri_matrix ( n, x ) b = kershawtri_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # kms # title = 'kms' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = kms_matrix ( alpha, n, n ) b = kms_inverse ( alpha, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # laguerre # title = 'laguerre' n = 5 a = laguerre_matrix ( n ) b = laguerre_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # legendre # title = 'legendre' n = 5 a = legendre_matrix ( n ) b = legendre_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # lehmer # title = 'lehmer' n = 5 a = lehmer_matrix ( n, n ) b = lehmer_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # lesp # title = 'lesp' n = 5 a = lesp_matrix ( n, n ) b = lesp_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # lietzke # title = 'lietzke' n = 5 a = lietzke_matrix ( n ) b = lietzke_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # line_adj # N must be even. # title = 'line_adj' n = 6 a = line_adj_matrix ( n ) b = line_adj_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # lotkin # title = 'lotkin' n = 5 a = lotkin_matrix ( n, n ) b = lotkin_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # maxij # title = 'maxij' n = 5 a = maxij_matrix ( n, n ) b = maxij_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # milnes # title = 'milnes' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = milnes_matrix ( n, n, x ) b = milnes_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # minij # title = 'minij' n = 5 a = minij_matrix ( n, n ) b = minij_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # moler1 # title = 'moler1' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = moler1_matrix ( alpha, n, n ) b = moler1_inverse ( alpha, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # moler3 # title = 'moler3' n = 5 a = moler3_matrix ( n, n ) b = moler3_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # ortega # title = 'ortega' n = 5 r8_lo = -5.0 r8_hi = +5.0 v1 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) v2 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) v3 = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = ortega_matrix ( n, v1, v2, v3 ) b = ortega_inverse ( n, v1, v2, v3 ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # orthogonal_random # title = 'orthogonal_random' n = 5 key = 123456789 a = orthogonal_random_matrix ( n, key ) b = orthogonal_random_inverse ( n, key ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # orthogonal_symmetric # title = 'orthogonal_symmetric' n = 5 a = orthogonal_symmetric_matrix ( n ) b = orthogonal_symmetric_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # oto # title = 'oto' n = 5 a = oto_matrix ( n, n ) b = oto_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # parter # title = 'parter' n = 5 a = parter_matrix ( n, n ) b = parter_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # pascal1 # title = 'pascal1' n = 5 a = pascal1_matrix ( n ) b = pascal1_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # pascal2 # title = 'pascal2' n = 5 a = pascal2_matrix ( n ) b = pascal2_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # pascal3 # title = 'pascal3' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = pascal3_matrix ( n, alpha ) b = pascal3_inverse ( n, alpha ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # pei # title = 'pei' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = pei_matrix ( alpha, n ) b = pei_inverse ( alpha, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # permutation_random # title = 'permutation_random' n = 5 key = 123456789 a = permutation_random_matrix ( n, key ) b = permutation_random_inverse ( n, key ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # plu # title = 'plu' n = 5 pivot = np.zeros ( n, dtype = np.int32 ) for i in range ( 0, n ): i4_lo = i i4_hi = n - 1 pivot[i] = rng.integers ( low = i, high = n - 1, endpoint = True ) a = plu_matrix ( n, pivot ) b = plu_inverse ( n, pivot ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # reflection_random # title = 'reflection_random' n = 5 key = 123456789 a, v = reflection_random_matrix ( n, key ) b = reflection_random_inverse ( n, key ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # ris # title = 'ris' n = 5 a = ris_matrix ( n ) b = ris_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # rodman # title = 'rodman' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = rodman_matrix ( n, n, alpha ) b = rodman_inverse ( n, alpha ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # rutis1 # title = 'rutis1' n = 4 a = rutis1_matrix ( ) b = rutis1_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # rutis2 # title = 'rutis2' n = 4 a = rutis2_matrix ( ) b = rutis2_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # rutis3 # title = 'rutis3' n = 4 a = rutis3_matrix ( ) b = rutis3_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # rutis4 # title = 'rutis4' n = 4 a = rutis4_matrix ( n ) b = rutis4_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # rutis5 # title = 'rutis5' n = 4 a = rutis5_matrix ( ) b = rutis5_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # schur_block # title = 'schur_block' n = 5 r8_lo = -5.0 r8_hi = +5.0 x_n = ( ( n + 1 ) // 2 ) x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = x_n ) y_n = ( n // 2 ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = y_n ) a = schur_block_matrix ( n, x, y ) b = schur_block_inverse ( n, x, y ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # spd_random # title = 'spd_random' n = 5 key = 123456789 a = spd_random_matrix ( n, key ) b = spd_random_inverse ( n, key ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # spline # title = 'spline' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) a = spline_matrix ( n, x ) b = spline_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # stirling # title = 'stirling' n = 5 a = stirling_matrix ( n, n ) b = stirling_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # summation # title = 'summation' n = 5 a = summation_matrix ( n, n ) b = summation_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # sweet1 # title = 'sweet1' n = 6 a = sweet1_matrix ( ) b = sweet1_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # sweet2 # title = 'sweet2' n = 6 a = sweet2_matrix ( ) b = sweet2_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # sweet3 # title = 'sweet3' n = 6 a = sweet3_matrix ( ) b = sweet3_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # sweet4 # title = 'sweet4' n = 13 a = sweet4_matrix ( ) b = sweet4_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # sylvester_kac # N must be even. # title = 'sylvester_kac' n = 6 a = sylvester_kac_matrix ( n ) b = sylvester_kac_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # symmetric_random # title = 'symmetric_random' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) key = 123456789 a = symmetric_random_matrix ( n, x, key ) b = symmetric_random_inverse ( n, x, key ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # toeplitz # title = 'toeplitz' n = 5 x_n = 2 * n - 1 r8_lo = -5.0 r8_hi = +5.0 key = 123456789 x = rng.standard_normal ( size = x_n ) a = toeplitz_matrix ( n, x ) b = toeplitz_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # tri_upper # title = 'tri_upper' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = tri_upper_matrix ( alpha, n ) b = tri_upper_inverse ( alpha, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # tris # title = 'tris' n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) beta = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) gamma = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = tris_matrix ( n, n, alpha, beta, gamma ) b = tris_inverse ( n, alpha, beta, gamma ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # triv # title = 'triv' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) y = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) z = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n - 1 ) a = triv_matrix ( n, x, y, z ) b = triv_inverse ( n, x, y, z ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # triw # title = 'triw' n = 5 i4_lo = 0 i4_hi = n - 1 r8_lo = -5.0 r8_hi = +5.0 k = rng.integers ( low = 0, high = n - 1, endpoint = True ) alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = triw_matrix ( alpha, k, n ) b = triw_inverse ( alpha, k, n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # unitary_random # title = 'unitary_random' n = 5 key = 123456789 # a = unitary_random_matrix ( n, key ) # b = unitary_random_inverse ( n, key ) # c = np.linalg.inv ( a ) # error_ab = c8mat_is_inverse ( n, a, b ) # error_ac = c8mat_is_inverse ( n, a, c ) # norma_frobenius = np.linalg.norm ( a, 'fro' ) # normc_frobenius = np.linalg.norm ( c, 'fro' ) # print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ # % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # upshift # title = 'upshift' n = 5 a = upshift_matrix ( n ) b = upshift_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # vand1 # title = 'vand1' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = vand1_matrix ( n, x ) b = vand1_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # vand2 # title = 'vand2' n = 5 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = n ) a = vand2_matrix ( n, x ) b = vand2_inverse ( n, x ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # wilk03 # title = 'wilk03' n = 3 a = wilk03_matrix ( ) b = wilk03_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # wilk04 # title = 'wilk04' n = 4 a = wilk04_matrix ( ) b = wilk04_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # wilk05 # title = 'wilk05' n = 5 a = wilk05_matrix ( ) b = wilk05_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # wilk21 # title = 'wilk21' n = 21 a = wilk21_matrix ( n ) b = wilk21_inverse ( n ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) # # wilson # title = 'wilson' n = 4 a = wilson_matrix ( ) b = wilson_inverse ( ) c = np.linalg.inv ( a ) error_ab = r8mat_is_inverse ( n, a, b ) error_ac = r8mat_is_inverse ( n, a, c ) norma_frobenius = np.linalg.norm ( a, 'fro' ) normc_frobenius = np.linalg.norm ( c, 'fro' ) print ( ' %-20s %4d %10.2g %10.2g %10.2g %10.2g' \ % ( title, n, norma_frobenius, normc_frobenius, error_ac, error_ab ) ) return def test_llt ( rng ): #*****************************************************************************80 # ## test_llt() tests LLT factors. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 May 2020 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_llt():' ) print ( ' A = a test matrix of order M by M' ) print ( ' L is an M by N lower triangular Cholesky factor.' ) print ( '' ) print ( ' ||A|| = Frobenius norm of A.' ) print ( ' ||A-LLT|| = Frobenius norm of A-L*L\'.' ) print ( '' ) print ( ' Title M N ||A|| ||A-LLT||' ) print ( '' ) # # dif2 # title = 'dif2' m = 5 n = 5 a = dif2_matrix ( m, n ) l = dif2_llt ( n ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # givens # title = 'givens' m = 5 n = 5 a = givens_matrix ( m, n ) l = givens_llt ( n ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # kershaw # title = 'kershaw' m = 4 n = 4 a = kershaw_matrix ( ) l = kershaw_llt ( ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # lehmer # title = 'lehmer' m = 5 n = 5 a = lehmer_matrix ( n, n ) l = lehmer_llt ( n ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # minij # title = 'minij' m = 5 n = 5 a = minij_matrix ( n, n ) l = minij_llt ( n ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # moler1 # title = 'moler1' m = 5 n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = moler1_matrix ( alpha, m, n ) l = moler1_llt ( alpha, n ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # moler3 # title = 'moler3' m = 5 n = 5 a = moler3_matrix ( m, n ) l = moler3_llt ( n ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # oto # title = 'oto' m = 5 n = 5 a = oto_matrix ( m, n ) l = oto_llt ( n ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # pascal2 # title = 'pascal2' m = 5 n = 5 a = pascal2_matrix ( n ) l = pascal2_llt ( n ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # wilson # title = 'wilson' m = 4 n = 4 a = wilson_matrix ( ) l = wilson_llt ( ) error_frobenius = r8mat_is_llt ( m, n, a, l ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) return def test_lu ( rng ): #*****************************************************************************80 # ## test_lu() tests LU factor functions. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 November 2021 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_lu():' ) print ( ' A = a test matrix of order M by N' ) print ( ' L, U are the LU factors.' ) print ( '' ) print ( ' ||A|| = Frobenius norm of A.' ) print ( ' ||A-LU|| = Frobenius norm of A-L*U.' ) print ( '' ) print ( ' Title M N ||A|| ||A-LU||' ) print ( '' ) # # borderband # title = 'bodewig' m = 4 n = 4 a = bodewig_matrix ( ) l, u = bodewig_lu ( ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # borderband # title = 'borderband' m = 5 n = 5 a = borderband_matrix ( n ) l, u = borderband_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # dif2 # title = 'dif2' m = 5 n = 5 a = dif2_matrix ( m, n ) l, u = dif2_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # fibonacci2 # title = 'fibonacci2' m = 5 n = 5 a = fibonacci2_matrix ( n ) l, u = fibonacci2_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # gfpp # title = 'gfpp' m = 5 n = 5 alpha = 1.0 a = gfpp_matrix ( n, alpha ) l, u = gfpp_lu ( n, alpha ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # givens # title = 'givens' m = 5 n = 5 a = givens_matrix ( m, n ) l, u = givens_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # golub # title = 'golub' m = 5 n = 5 key = 123456789 a = golub_matrix ( n, key ) l, u = golub_lu ( n, key ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # kms # title = 'kms' m = 5 n = 5 alpha = 2.0 a = kms_matrix ( alpha, m, n ) l, u = kms_lu ( alpha, n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # lehmer # title = 'lehmer' m = 5 n = 5 a = lehmer_matrix ( m, n ) l, u = lehmer_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # minij # title = 'minij' m = 5 n = 5 a = minij_matrix ( m, n ) l, u = minij_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # moler1 # title = 'moler1' m = 5 n = 5 alpha = 2.0 a = moler1_matrix ( alpha, m, n ) l, u = moler1_lu ( alpha, n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # moler3 # title = 'moler3' m = 5 n = 5 a = moler3_matrix ( m, n ) l, u = moler3_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # oto # title = 'oto' m = 5 n = 5 a = oto_matrix ( m, n ) l, u = oto_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # pascal2 # title = 'pascal2' m = 5 n = 5 a = pascal2_matrix ( n ) l, u = pascal2_lu ( n ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # vand2 # title = 'vand2' m = 5 n = 5 x = 5.0 * rng.standard_normal ( size = n ) a = vand2_matrix ( n, x ) l, u = vand2_lu ( n, x ) error_frobenius = r8mat_is_lu ( m, n, a, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) return def test_matrix_test ( ): #*****************************************************************************80 # ## test_matrix_test() tests test_matrix(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 January 2024 # # Author: # # John Burkardt # from numpy.random import default_rng import platform import numpy as np import scipy as sp print ( '' ) print ( 'test_matrix_test():' ) print ( ' python version: ' + platform.python_version ( ) ) print ( ' numpy version: ' + np.version.version ) print ( ' scipy version: ' + sp.__version__ ) print ( ' test_matrix() defines various matrices with known properties.' ) rng = default_rng ( ) test_all = False # # Utilities. # if ( test_all ): bvec_next_grlex_test ( ) c8_uniform_01_test ( ) c8mat_indicator_test ( ) c8mat_uniform_01_test ( ) c8vec_print_test ( ) c8vec_uniform_01_test ( ) c8vec_unity_test ( ) complete_symmetric_poly_test ( ) i4_factor_test ( ) i4_is_prime_test ( ) i4_log_10_test ( ) i4_modp_test ( ) i4_rise_test ( ) i4_sign_test ( ) i4_wrap_test ( ) i4vec_indicator0_test ( ) legendre_symbol_test ( ) mertens_test ( ) moebius_test ( ) prime_test ( ) r8_mop_test ( rng ) r8_rise_test ( ) r8_sign_test ( ) r8mat_house_axh_test ( ) r8mat_house_form_test ( ) r8mat_indicator_test ( ) r8mat_is_adjacency_test ( ) r8mat_is_anticirculant_test ( ) r8mat_is_antisymmetric_test ( ) r8mat_is_eigen_left_test ( ) r8mat_is_eigen_right_test ( ) r8mat_is_identity_test ( ) r8mat_is_integer_test ( ) r8mat_is_inverse_test ( ) r8mat_is_llt_test ( ) r8mat_is_null_left_test ( ) r8mat_is_null_right_test ( ) r8mat_is_orthogonal_test ( ) r8mat_is_orthogonal_column_test ( ) r8mat_is_orthogonal_row_test ( ) r8mat_is_permutation_test ( ) r8mat_is_plu_test ( ) r8mat_is_solution_test ( ) r8mat_is_sparse_test ( ) r8mat_is_square_test ( ) r8mat_is_symmetric_test ( ) r8mat_is_zero_one_test ( ) r8mat_mm_test ( ) r8mat_mtm_test ( ) r8poly_print_test ( ) r8vec_house_column_test ( ) r8vec_nint_test ( ) # # Interesting stuff. # test_condition ( rng ) test_determinant ( rng ) test_eigen_left ( rng ) test_eigen_right ( rng ) test_inverse ( rng ) test_llt ( rng ) test_lu ( rng ) test_null_left ( rng ) test_null_right ( rng ) test_plu ( rng ) test_solution ( rng ) # # Terminate. # print ( '' ) print ( 'test_matrix_test():' ) print ( ' Normal end of execution.' ) return def test_null_left ( rng ): #*****************************************************************************80 # ## test_null_left() tests left null vectors. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 March 2021 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_null_left():' ) print ( ' A = a test matrix of order M by N' ) print ( ' x = an M vector, candidate for a left null vector.' ) print ( '' ) print ( ' ||A|| = Frobenius norm of A.' ) print ( ' ||x|| = L2 norm of x.' ) print ( ' ||A''*x||/||x|| = L2 norm of A\'*x over L2 norm of x.' ) print ( '' ) print ( ' Title M N ||A|| ||x|| ||A\'*x||/||x||' ) print ( '' ) # # a123 # title = 'a123' m = 3 n = 3 a = a123_matrix ( ) x = a123_null_left ( ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # cheby_diff1 # title = 'cheby_diff1' m = 5 n = m a = cheby_diff1_matrix ( n ) x = cheby_diff1_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # creation # title = 'creation' m = 5 n = m a = creation_matrix ( m, n ) x = creation_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # dif1 # title = 'dif1' m = 5 n = 5 a = dif1_matrix ( m, n ) x = dif1_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # dif1cyclic # title = 'dif1cyclic' m = 5 n = m a = dif1cyclic_matrix ( n ) x = dif1cyclic_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # dif2cyclic # title = 'dif2cyclic' m = 5 n = 5 a = dif2cyclic_matrix ( n ) x = dif2cyclic_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # eberlein # title = 'eberlein' m = 5 n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = eberlein_matrix ( alpha, n ) x = eberlein_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # fibonacci1 # title = 'fibonacci1' m = 5 n = m r8_lo = -5.0 r8_hi = +5.0 f1= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) f2= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = fibonacci1_matrix ( n, f1, f2 ) x = fibonacci1_null_left ( m, n, f1, f2 ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # lauchli # title = 'lauchli' m = 6 n = m - 1 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = lauchli_matrix ( alpha, m, n ) x = lauchli_null_left ( alpha, m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # line_adj # title = 'line_adj' m = 7 n = m a = line_adj_matrix ( n ) x = line_adj_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # moler2 # title = 'moler2' m = 5 n = m a = moler2_matrix ( ) x = moler2_null_left ( ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # one # title = 'one' m = 5 n = 5 a = one_matrix ( m, n ) x = one_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # ring_adj # N must be a multiple of 4 for there to be a null vector. # title = 'ring_adj' m = 12 n = 12 a = ring_adj_matrix ( m, n ) x = ring_adj_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # rosser1 # title = 'rosser1' m = 8 n = 8 a = rosser1_matrix ( ) x = rosser1_null_left ( ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # zero # title = 'zero' m = 5 n = 5 a = zero_matrix ( m, n ) x = zero_null_left ( m, n ) error_l2 = r8mat_is_null_left ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) return def test_null_right ( rng ): #*****************************************************************************80 # ## test_null_right() tests right null vectors. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 March 2021 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_null_right():' ) print ( ' A = a test matrix of order M by N' ) print ( ' x = an N vector, candidate for a right null vector.' ) print ( '' ) print ( ' ||A|| = Frobenius norm of A.' ) print ( ' ||x|| = L2 norm of x.' ) print ( ' ||A*x||/||x|| = L2 norm of A*x over L2 norm of x.' ) print ( '' ) print ( ' Title M N ||A|| ||x|| ||A*x||/||x||' ) print ( '' ) # # a123 # title = 'a123' m = 3 n = 3 a = a123_matrix ( ) x = a123_null_right ( ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # archimedes # title = 'archimedes' m = 7 n = 8 a = archimedes_matrix ( ) x = archimedes_null_right ( ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # cheby_diff1 # title = 'cheby_diff1' m = 5 n = m a = cheby_diff1_matrix ( n ) x = cheby_diff1_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # creation # title = 'creation' m = 5 n = m a = creation_matrix ( m, n ) x = creation_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # dif1 # Only has null vectors for N odd. # title = 'dif1' m = 5 n = m a = dif1_matrix ( m, n ) x = dif1_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # dif1cyclic # title = 'dif1cyclic' m = 5 n = m a = dif1cyclic_matrix ( n ) x = dif1cyclic_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # dif2cyclic # title = 'dif2cyclic' m = 5 n = m a = dif2cyclic_matrix ( n ) x = dif2cyclic_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # fibonacci1 # title = 'fibonacci1' m = 5 n = m r8_lo = -5.0 r8_hi = +5.0 f1= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) f2= r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = fibonacci1_matrix ( n, f1, f2 ) x = fibonacci1_null_right ( m, n, f1, f2 ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # hamming # title = 'hamming' m = 5 n = ( 2 ** m ) - 1 a = hamming_matrix ( m, n ) x = hamming_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # line_adj # title = 'line_adj' m = 7 n = m a = line_adj_matrix ( n ) x = line_adj_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # moler2 # title = 'moler2' m = 5 n = 5 a = moler2_matrix ( ) x = moler2_null_right ( ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # neumann # title = 'neumann' row_num = 5 col_num = 5 m = row_num * col_num n = row_num * col_num a = neumann_matrix ( row_num, col_num ) x = neumann_null_right ( row_num, col_num ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # one # title = 'one' m = 5 n = 5 a = one_matrix ( m, n ) x = one_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # ring_adj # N must be a multiple of 4 for there to be a null vector. # title = 'ring_adj' m = 12 n = 12 a = ring_adj_matrix ( m, n ) x = ring_adj_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # rosser1 # title = 'rosser1' m = 8 n = 8 a = rosser1_matrix ( ) x = rosser1_null_right ( ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) # # zero # title = 'zero' m = 5 n = 5 a = zero_matrix ( m, n ) x = zero_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) norm_x_l2 = np.linalg.norm ( x ) print ( ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) ) return def test_plu ( rng ): #*****************************************************************************80 # ## test_plu() tests PLU factor functions. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_plu():' ) print ( ' A = a test matrix of order M by N' ) print ( ' P, L, U are the PLU factors.' ) print ( '' ) print ( ' ||A|| = Frobenius norm of A.' ) print ( ' ||A-PLU|| = Frobenius norm of A-P*L*U.' ) print ( '' ) print ( ' Title M N ||A|| ||A-PLU||' ) print ( '' ) # # a123 # title = 'a123' m = 3 n = 3 a = a123_matrix ( ) p, l, u = a123_plu ( ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # bodewig # title = 'bodewig' m = 4 n = 4 a = bodewig_matrix ( ) p, l, u = bodewig_plu ( ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # borderband # title = 'borderband' m = 5 n = 5 a = borderband_matrix ( n ) p, l, u = borderband_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # dif2 # title = 'dif2' m = 5 n = 5 a = dif2_matrix ( m, n ) p, l, u = dif2_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # gfpp # title = 'gfpp' m = 5 n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = gfpp_matrix ( n, alpha ) p, l, u = gfpp_plu ( n, alpha ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # givens # title = 'givens' m = 5 n = 5 a = givens_matrix ( m, n ) p, l, u = givens_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # golub # title = 'golub' n = 5 key= 123456789 a = golub_matrix ( n, key ) p, l, u = golub_plu ( n, key ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # kms # title = 'kms' m = 5 n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = kms_matrix ( alpha, m, n ) p, l, u = kms_plu ( alpha, n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # lehmer # title = 'lehmer' m = 5 n = 5 a = lehmer_matrix ( m, n ) p, l, u = lehmer_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # maxij # title = 'maxij' m = 5 n = 5 a = maxij_matrix ( m, n ) p, l, u = maxij_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # minij # title = 'minij' m = 5 n = 5 a = minij_matrix ( n, n ) p, l, u = minij_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # moler1 # title = 'moler1' m = 5 n = 5 r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * rng.random ( ) a = moler1_matrix ( alpha, n, n ) p, l, u = moler1_plu ( alpha, n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # moler3 # title = 'moler3' m = 5 n = 5 a = moler3_matrix ( m, n ) p, l, u = moler3_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # oto # title = 'oto' m = 5 n = 5 a = oto_matrix ( m, n ) p, l, u = oto_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # pascal2 # title = 'pascal2' m = 5 n = 5 a = pascal2_matrix ( n ) p, l, u = pascal2_plu ( n ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # plu # title = 'plu' m = 5 n = 5 pivot = np.zeros ( n, dtype = np.int32 ) for i in range ( 0, n ): i4_lo = i i4_hi = n - 1 pivot[i] = rng.integers ( low = i, high = n - 1, endpoint = True ) a = plu_matrix ( n, pivot ) p, l, u = plu_plu ( n, pivot ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # vand2 # title = 'vand2' m = 4 n = 4 r8_lo = -5.0 r8_hi = +5.0 x = r8_lo + ( r8_hi - r8_lo ) * rng.random ( size = m ) a = vand2_matrix ( m, x ) p, l, u = vand2_plu ( m, x ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) # # wilson # title = 'wilson' m = 4 n = 4 a = wilson_matrix ( ) p, l, u = wilson_plu ( ) error_frobenius = r8mat_is_plu ( m, n, a, p, l, u ) norm_a_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %14g %14g' \ % ( title, m, n, norm_a_frobenius, error_frobenius ) ) return def test_solution ( rng ): #*****************************************************************************80 # ## test_solution() tests the linear solution computations. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 May 2020 # # Author: # # John Burkardt # # Input: # # rng: the current random number generator. # import numpy as np print ( '' ) print ( 'test_solution():' ) print ( ' Compute the Frobenius norm of the solution error:' ) print ( ' A * X - B' ) print ( ' given MxN matrix A, NxK solution X, MxK right hand side B.' ) print ( '' ) print ( ' Title M N K ||A|| ||A*X-B||' ) print ( '' ) # # a123 # title = 'a123' m = 3 n = 3 k = 1 a = a123_matrix ( ) b = a123_rhs ( ) x = a123_solution ( ) error_frobenius = r8mat_is_solution ( m, n, k, a, x, b ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %4d %14g %14g' \ % ( title, m, n, k, norm_frobenius, error_frobenius ) ) # # bodewig # title = 'bodewig' m = 4 n = 4 k = 1 a = bodewig_matrix ( ) b = bodewig_rhs ( ) x = bodewig_solution ( ) error_frobenius = r8mat_is_solution ( m, n, k, a, x, b ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %4d %14g %14g' \ % ( title, m, n, k, norm_frobenius, error_frobenius ) ) # # dif2 # title = 'dif2' m = 10 n = 10 k = 2 a = dif2_matrix ( m, n ) b = dif2_rhs ( m, k ) x = dif2_solution ( n, k ) error_frobenius = r8mat_is_solution ( m, n, k, a, x, b ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %4d %14g %14g' \ % ( title, m, n, k, norm_frobenius, error_frobenius ) ) # # frank # title = 'frank' m = 10 n = 10 k = 2 a = frank_matrix ( n ) b = frank_rhs ( m, k ) x = frank_solution ( n, k ) error_frobenius = r8mat_is_solution ( m, n, k, a, x, b ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %4d %14g %14g' \ % ( title, m, n, k, norm_frobenius, error_frobenius ) ) # # poisson # title = 'poisson' nrow = 4 ncol = 5 m = nrow * ncol n = nrow * ncol k = 1 a = poisson_matrix ( nrow, ncol ) b = poisson_rhs ( nrow, ncol, k ) x = poisson_solution ( nrow, ncol, k ) error_frobenius = r8mat_is_solution ( m, n, k, a, x, b ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %4d %14g %14g' \ % ( title, m, n, k, norm_frobenius, error_frobenius ) ) # # wilk03 # title = 'wilk03' m = 3 n = 3 k = 1 a = wilk03_matrix ( ) b = wilk03_rhs ( ) x = wilk03_solution ( ) error_frobenius = r8mat_is_solution ( m, n, k, a, x, b ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %4d %14g %14g' \ % ( title, m, n, k, norm_frobenius, error_frobenius ) ) # # wilk04 # title = 'wilk04' m = 4 n = 4 k = 1 a = wilk04_matrix ( ) b = wilk04_rhs ( ) x = wilk04_solution ( ) error_frobenius = r8mat_is_solution ( m, n, k, a, x, b ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %4d %14g %14g' \ % ( title, m, n, k, norm_frobenius, error_frobenius ) ) # # wilson # title = 'wilson' m = 4 n = 4 k = 1 a = wilson_matrix ( ) b = wilson_rhs ( ) x = wilson_solution ( ) error_frobenius = r8mat_is_solution ( m, n, k, a, x, b ) norm_frobenius = np.linalg.norm ( a, 'fro' ) print ( ' %-20s %4d %4d %4d %14g %14g' \ % ( title, m, n, k, norm_frobenius, error_frobenius ) ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return None def toeplitz_inverse ( n, x ): #*****************************************************************************80 # ## toeplitz_inverse() computes the inverse of a Toeplitz matrix. # # Discussion: # # This function uses the fact that if T is a Toeplitz matrix, # and J is the "exchange" matrix, then H=J*T is a Hankel matrix. # # By lucky chance, the vector x defining T is the same vector x # used to define J*T. # # Hence, we can use a known algorithm for the inverse of a Hankel matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 May 2020 # # Author: # # John Burkardt # # Reference: # # Miroslav Fiedler, # Hankel and Loewner Matrices # # Input: # # integer N, the order of the matrix. # # real X(2*N-1), the vector that defines the matrix. # # Output: # # real B(N,N), the inverse matrix. # import numpy as np # # Get the Exchange matrix J. # J = exchange_matrix ( n, n ) # # Define the Toeplitz matrix A. # A = toeplitz_matrix ( n, x ) # # Define the corresponding Hankel matrix H = J * A. # H = np.matmul ( J, A ) # # Solve two linear systems. # p = np.zeros ( n ) p[0:n-1] = x[n:2*n-1] p[n-1] = 0.0 u = np.linalg.solve ( H, p ) q = np.zeros ( n ) q[n-1] = 1.0 v = np.linalg.solve ( H, q ) # # Construct four matrices. # z1 = np.zeros ( n ) w1 = np.concatenate ( [ v[1:n], z1 ] ) M1 = hankel_matrix ( n, w1 ) z2 = np.zeros ( n-1 ) w2 = np.concatenate ( [ z2, u ] ) M2 = toeplitz_matrix ( n, w2 ) z3 = np.zeros ( n ) z3[0] = -1.0 w3 = np.concatenate ( [ u[1:n], z3 ] ) M3 = hankel_matrix ( n, w3 ) z4 = np.zeros ( n-1 ) w4 = np.concatenate ( [ z4, v ] ) M4 = toeplitz_matrix ( n, w4 ) # # Construct K, the inverse of the Hankel matrix. # K = np.matmul ( M1, M2 ) - np.matmul ( M3, M4 ) # # Compute B, the inverse of the Toeplitz matrix. # B = np.matmul ( K, J ) return B def toeplitz_matrix ( n, x ): #*****************************************************************************80 # ## toeplitz_matrix() returns a TOEPLITZ matrix. # # Formula: # # A(I,J) = X(N+J-I) # # Example: # # N = 5, X = ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) # # 5 6 7 8 9 # 4 5 6 7 8 # 3 4 5 6 7 # 2 3 4 5 6 # 1 2 3 4 5 # # Properties: # # A is generally not symmetric: A' /= A. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A is Toeplitz: constant along diagonals. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 May 2020 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real X(2*N-1), the diagonals of A, with X(1) being # the A(N,1) entry, X(N) being the main diagonal value of A, # and X(2*N-1) being the A(1,N) entry. # # Output: # # real A(N,N), the matrix. # import numpy as np A = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): A[i,j] = x[n-i+j-1] return A def toeplitz_test ( ): #*****************************************************************************80 # ## toeplitz_test() tests toeplitz_matrix(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 May 2020 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'toeplitz_test' ) print ( ' toeplitz_matrix() computes the toeplitz matrix.' ) n = 5 x = np.array ( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ) a = toeplitz_matrix ( n, x ) r8mat_print ( n, n, a, ' toeplitz matrix:' ) return def tournament_random_determinant ( n, key ): #*****************************************************************************80 # ## tournament_random_determinant(): determinant of the TOURNAMENT_RANDOM matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 May 2020 # # Author: # # John Burkardt # # Reference: # # Michael Wimmer, # Algorithm 923: Efficient Numerical Computation of the Pfaffian for # Dense and Banded Skew-Symmetric Matrices, # ACM Transactions on Mathematical Software, # Volume 38, Number 4, Article 30, August 2012. # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real DETERM, the determinant. # if ( ( n % 2 ) == 1 ): determ = 0.0 else: A = tournament_random_matrix ( n, key ) determ = ( pfaffian_ltl ( A ) ) ** 2 return determ def tournament_random_matrix ( n, key ): #*****************************************************************************80 # ## tournament_random_matrix() returns a random tournament matrix. # # Example: # # N = 5 # # 0 -1 1 1 -1 # 1 0 1 1 1 # -1 -1 0 1 -1 # -1 -1 -1 0 -1 # 1 -1 1 1 0 # # Properties: # # A is generally not symmetric: A' /= A. # # A is antisymmetric: A' = -A. # # Because A is antisymmetric, it is normal. # # Because A is normal, it is diagonalizable. # # The diagonal of A is zero. # # All the eigenvalues of A are imaginary. # # If N is odd, then A is singular. # # If N is even, then A is nonsingular. # # A is not diagonally dominant. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 May 2020 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real A(N,N), the matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) A = rng.random ( size = [ n, n ] ) for i in range ( 0, n ): A[i,i] = 0.0 for j in range ( i + 1, n ): if ( 0.5 < A[i,j] ): A[i,j] = + 1.0 else: A[i,j] = - 1.0 A[j,i] = - A[i,j] return A def tournament_random_test ( ): #*****************************************************************************80 # ## tournament_random_test() tests tournament_random_matrix(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 May 2020 # # Author: # # John Burkardt # print ( '' ) print ( 'tournament_random_test' ) print ( ' tournament_random_matrix() computes the tournament_random matrix.' ) n = 5 key = 123456789 A = tournament_random_matrix ( n, key ) r8mat_print ( n, n, A, ' tournament matrix:' ) return def transition_random_matrix ( n, key ): #*****************************************************************************80 # ## transition_random_matrix() returns the TRANSITION_RANDOM matrix. # # Discussion: # # A transition matrix is distinguished by two properties: # * All matrix entries are nonnegative; # * The sum of the entries in each column is 1. # # Example: # # N = 4 # # 1/10 1 5/10 2/10 2/10 # 2/10 0 2/10 2/10 2/10 # 3/10 0 3/10 2/10 2/10 # 4/10 0 0/10 4/10 4/10 # # Properties: # # A is generally not symmetric: A' /= A. # # A is nonnegative. # # 0 <= A(I,J) <= 1.0 for every I and J. # # The sum of the entries in each column of A is 1. # # Because A has a constant column sum of 1, # it has an eigenvalue of 1, # and it has a left eigenvector of (1,1,1,...,1). # # All the eigenvalues of A have modulus no greater than 1. # # A is not diagonally dominant. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 May 2020 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # integer KEY, a positive value that selects the data. # # Output: # # real A(N,N), the matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) a = rng.random ( size = [ n, n ] ) for j in range ( 0, n ): col_sum = np.sum ( a[0:n,j] ) a[0:n,j] = a[0:n,j] / col_sum return a def tri_l1_inverse ( n, a ): #*****************************************************************************80 # ## tri_l1_inverse() inverts a unit lower triangular matrix. # # Discussion: # # A unit lower triangular matrix is a matrix with only 1's on the main # diagonal, and only 0's above the main diagonal. # # The inverse of a unit lower triangular matrix is also # a unit lower triangular matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Reference: # # A Nijenhuis, H Wilf, # Combinatorial Algorithms, # Academic Press, 1978, second edition, # ISBN 0-12-519260-6. # # Input: # # integer N, number of rows and columns in the matrix. # # real A(N,N), the unit lower triangular matrix. # # Output: # # real B(N,N), the inverse matrix. # import numpy as np b = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j == i ): b[i,j] = 1.0 elif ( j < i ): t = 0.0 for k in range ( 0, i ): t = t + a[i,k] * b[k,j] b[i,j] = - t return b def tribonacci_roots ( ): #*****************************************************************************80 # ## tribonacci_roots() returns the Tribonacci roots. # # Discussion: # # The Nth Tribonacci number is defined by: # T(N) = T(N-1) + T(N-2) + T(N-3) # with # T(1) = 0, T(2) = 0, T(3) = 1. # # The related polynomial equation # x^3 - x^2 - x - 1 = 0 # # ALPHA, BETA, and GAMMA are the roots of this equation. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 May 2021 # # Author: # # John Burkardt # # Reference: # # W R Spickerman, # Binet's formula for the Tribonacci sequence, # Fibonacci Quarterly, # Volume 20, Number 2, pages 118-120, May 1982. # # Output: # # real ALPHA, complex BETA, complex GAMMA, the roots. # import numpy as np rho = np.cbrt ( 19.0 + 3.0 * np.sqrt ( 33.0 ) ) tau = np.cbrt ( 19.0 - 3.0 * np.sqrt ( 33.0 ) ) a = 2.0 - rho - tau b = np.sqrt ( 3.0 ) * ( rho - tau ) alpha = ( 1.0 + rho + tau ) / 3.0 beta = ( a + b * 1j ) / 6.0 gamma = ( a - b * 1j ) / 6.0 return alpha, beta, gamma def tribonacci2_determinant ( n ): #*****************************************************************************80 # ## tribonacci2_determinant() returns the determinant of the TRIBONACCI2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 May 2021 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real DETERM, the determinant. # if ( n <= 2 ): determ = 0.0 else: determ = 1.0 return determ def tribonacci2_eigen_right ( n ): #*****************************************************************************80 # ## tribonacci2_eigen_right(): right eigenvectors of the TRIBONACCI2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 May 2021 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # complex V(N,N), the right eigenvectors. # import numpy as np v = np.zeros ( [ n, n ], dtype = np.complex64 ) if ( 3 <= n ): alpha, beta, gamma = tribonacci_roots ( ) p = 1.0 for i in range ( 0, n ): v[i,0] = p p = p * alpha v[n-1,1:n-2] = 1.0 p = 1.0 for i in range ( 0, n ): v[i,n-2] = p p = p * beta p = 1.0 for i in range ( 0, n ): v[i,n-1] = p p = p * gamma return v def tribonacci2_eigenvalues ( n ): #*****************************************************************************80 # ## tribonacci2_eigenvalues() returns the eigenvalues of the TRIBONACCI2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 May 2021 # # Author: # # John Burkardt # # Input: # # integer N, the number of rows and columns of A. # # Output: # # complex LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n, dtype = np.complex64 ) if ( 2 < n ): alpha, beta, gamma = tribonacci_roots ( ) lam[0] = alpha for i in range ( 1, n - 2 ): lam[i] = 1.0 lam[n-2] = beta lam[n-1] = gamma return lam def tribonacci2_matrix ( n ): #*****************************************************************************80 # ## tribonacci2_matrix() returns the TRIBONACCI2 matrix. # # Example: # # N = 5 # # 0 1 0 0 0 # 0 0 1 0 0 # 1 1 1 0 0 # 0 1 1 1 0 # 0 0 1 1 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is banded, with bandwidth 4. # # A is integral: int ( A ) = A. # # A is a zero/one matrix. # # If N <= 2 then # det ( A ) = 0 # else # det ( A ) = 1 # # A is defective, for 4 < N. # # The family of matrices is nested as a function of N. # # A is not diagonally dominant. # # For 3 <= N, A has the eigenvalues: # # 1 (N-3) times, # A once, 1/3 * (1+4 cosh(1/3 acosh(2+3/8) ) ) approx 1.83928 # B+Ci once, approx -0.4196 + 0.6063i # B-Ci once, approx -0.4196 - 0.6063i # where A, B+Ci and B-Ci are the roots of x^3+x^2-x-1=0. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2021 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) if ( n <= 1 ): pass elif ( n == 2 ): a[0,1] = 1.0 else: for i in range ( 0, n ): if ( i == 0 ): a[i,1] = 1.0 elif ( i == 1 ): a[i,2] = 1.0 else: a[i,i-2:i+1] = 1.0 return a def tris_eigenvalues ( n, x, y, z ): #*****************************************************************************80 # ## tris_eigenvalues() returns the eigenvalues of the tridiagonal scalar matrix. # # Discussion: # # The eigenvalues will be complex if X * Z < 0. # # Modified: # # 23 September 2022 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real X, Y, Z, the scalars that define A. # # Output: # # complex lam(N), the eigenvalues. The eigenvalues are # complex if X * Z < 0. # import numpy as np lam = np.zeros ( n, dtype = complex ) # # np.emath.sqrt() correctly returns complex results for negative input. # for i in range ( 0, n ): angle = ( i + 1 ) * np.pi / ( n + 1 ) lam[i] = y + 2.0 * np.emath.sqrt ( x * z ) * np.cos ( angle ) return lam def tris_matrix ( m, n, x, y, z ): #*****************************************************************************80 # ## tris_matrix() returns the TRIS matrix. # # Formula: # # if ( J = I-1 ) # A(I,J) = X # elseif ( J = I ) # A(I,J) = Y # elseif ( J = I + 1 ) # A(I,J) = Z # else # A(I,J) = 0 # # Example: # # M = 5, N = 5, X = 1, Y = 2, Z = 3 # # 2 3 0 0 0 # 1 2 3 0 0 # 0 1 2 3 0 # 0 0 1 2 3 # 0 0 0 1 2 # # Properties: # # A is generally not symmetric: A' /= A. # # A is tridiagonal. # # Because A is tridiagonal, it has property A (bipartite). # # A is banded, with bandwidth 3. # # A is Toeplitz: constant along diagonals. # # If Y is not zero, then for A to be singular, it must be the case that # # 0.5 * Y / sqrt ( X * Z ) < 1 # # and # # cos (K*PI/(N+1)) = - 0.5 * Y / sqrt ( X * Z ) for some 1 <= K <= N. # # If Y is zero, then A is singular when N is odd, or if X or Z is zero. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A has eigenvalues # # LAMBDA(I) = Y + 2 * sqrt(X*Z) * COS(I*PI/(N+1)) # # The eigenvalues will be complex if X * Z < 0. # # If X = Z, the matrix is symmetric. # # As long as X and Z are nonzero, the matrix is irreducible. # # If X = Z = -1, and Y = 2, the matrix is a symmetric, positive # definite M matrix, the negative of the second difference matrix. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 February 2015 # # Author: # # John Burkardt # # Reference: # # John Todd, # Basic Numerical Mathematics, # Volume 2: Numerical Algebra, # Academic Press, 1978, page 155. # # Input: # # integer M, N, the order of A. # # real X, Y, Z, the scalars that define A. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( ( m, n ) ) for i in range ( 0, m ): for j in range ( 0, n ): if ( j == i - 1 ): a[i,j] = x elif ( j == i ): a[i,j] = y elif ( j == i + 1 ): a[i,j] = z return a def tris_determinant ( n, x, y, z ): #*****************************************************************************80 # ## tris_determinant() returns the determinant of the TRIS matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X, Y, Z, parameters. # # Output: # # real VALUE, the determinant. # import numpy as np value = 1.0 if ( 0.0 <= x * z ): for i in range ( 1, n + 1 ): angle = float ( i ) * np.pi / float ( n + 1 ) value = value * ( y + 2.0 * np.sqrt ( x * z ) * np.cos ( angle ) ) else: i_hi = ( n // 2 ) for i in range ( 1, i_hi + 1 ): angle = float ( i ) * np.pi / float ( n + 1 ) value = value * ( y * y - 4.0 * x * z * ( np.cos ( angle ) ) ** 2 ) if ( ( n % 2 ) == 1 ): value = value * y return value def tris_inverse ( n, alpha, beta, gam ): #*****************************************************************************80 # ## tris_inverse() returns the inverse of the TRIS matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 March 2015 # # Author: # # John Burkardt # # Reference: # # CM daFonseca, J Petronilho, # Explicit Inverses of Some Tridiagonal Matrices, # Linear Algebra and Its Applications, # Volume 325, 2001, pages 7-21. # # Input: # # integer N, the order of the matrix. # # real ALPHA, BETA, GAM, the constant values associated with the # subdiagonal, diagonal and superdiagonal of the matrix. # # Output: # # real A(N,N), the inverse of the matrix. # import numpy as np d = np.zeros ( n ) d[n-1] = beta for i in range ( n - 2, -1, -1 ): d[i] = beta - alpha * gam / d[i+1] a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, i + 1 ): p1 = 1.0 for k in range ( i + 1, n ): p1 = p1 * d[k] p2 = 1.0 for k in range ( 0, n - j ): p2 = p2 * d[k] a[i,j] = r8_mop ( i + j ) * alpha ** ( i - j ) * p1 / p2 for j in range ( i + 1, n ): p1 = 1.0 for k in range ( j + 1, n ): p1 = p1 * d[k] p2 = 1.0 for k in range ( 0, n - i ): p2 = p2 * d[k] a[i,j] = r8_mop ( i + j ) * gam ** ( j - i ) * p1 / p2 return a def tri_u_inverse ( n, a ): #*****************************************************************************80 # ## tri_u_inverse() inverts an upper triangular R8MAT. # # Discussion: # # An R8MAT is an array of R8 values. # # An upper triangular matrix is a matrix whose only nonzero entries # occur on or above the diagonal. # # The inverse of an upper triangular matrix is an upper triangular matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt. # # Reference: # # Albert Nijenhuis, Herbert Wilf, # Combinatorial Algorithms, # Academic Press, 1978, second edition, # ISBN 0-12-519260-6. # # Input: # # integer N, the order of the matrix. # # real A(N,N), the upper triangular matrix. # # Output: # # real B(N,N), the inverse matrix. # import numpy as np b = np.zeros ( ( n, n ) ) for j in range ( n - 1, -1, -1 ): for i in range ( n - 1, -1, -1 ): if ( i == j ): b[i,j] = 1.0 / a[i,j] elif ( i < j ): t = 0.0 for k in range ( i + 1, j + 1 ): t = t + a[i,k] * b[k,j] b[i,j] = - t / a[i,i] return b def tri_upper_matrix ( alpha, n ): #*****************************************************************************80 # ## tri_upper_matrix() returns the tri_upper matrix. # # Discussion: # # This matrix is known as the Wilkinson upper triangular matrix. # # Formula: # # if ( I = J ) # A(I,J) = 1 # if ( I < J ) # A(I,J) = ALPHA # else # A(I,J) = 0 # # Example: # # ALPHA = 3, N = 5 # # 1 3 3 3 3 # 0 1 3 3 3 # 0 0 1 3 3 # 0 0 0 1 3 # 0 0 0 0 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is nonsingular. # # A is upper triangular. # # det ( A ) = 1. # # A is unimodular. # # LAMBDA(1:N) = 1. # # A is Toeplitz: constant along diagonals. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 December 2014 # # Author: # # John Burkardt # # Input: # # real ALPHA, value used on the superdiagonals. # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = 1.0 elif ( i < j ): a[i,j] = alpha else: a[i,j] = 0.0 return a def tri_upper_condition ( alpha, n ): #*****************************************************************************80 # ## tri_upper_condition() returns the L1 condition of the tri_upper matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 January 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, value used on the superdiagonals. # # integer N, the order of the matrix. # # Output: # # real COND, the L1 condition number. # a_norm = ( n - 1 ) * abs ( alpha ) + 1.0 b_norm = 1.0 + abs ( alpha ) \ * ( ( abs ( alpha - 1.0 ) ) ** ( n - 1 ) - 1.0 ) \ / ( abs ( alpha - 1.0 ) - 1.0 ) cond = a_norm * b_norm return cond def tri_upper_determinant ( alpha, n ): #*****************************************************************************80 # ## tri_upper_determinant() returns the determinant of the tri_upper matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 February 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, value used on the superdiagonals. # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 return value def tri_upper_inverse ( alpha, n ): #*****************************************************************************80 # ## tri_upper_inverse() returns the inverse of the tri_upper matrix. # # Formula: # # if ( I = J ) # A(I,J) = 1 # elseif ( I = J - 1 ) # A(I,J) = -ALPHA # elseif ( I < J ) # A(I,J) = - ALPHA * ( 1-ALPHA)^(J-I-1) # else # A(I,J) = 0 # # Example: # # ALPHA = 3, N = 5 # # 1 -3 6 -12 24 # 0 1 -3 6 -12 # 0 0 1 -3 6 # 0 0 0 1 -3 # 0 0 0 0 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is nonsingular. # # A is upper triangular. # # A is Toeplitz: constant along diagonals. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # det ( A ) = 1. # # A is unimodular. # # LAMBDA(1:N) = 1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 October 2007 # # Author: # # John Burkardt # # Input: # # real ALPHA, value used on the superdiagonals. # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = 1.0 elif ( i == j - 1 ): a[i,j] = - alpha elif ( i < j ): a[i,j] = - alpha * ( 1.0 - alpha ) ** ( j - i - 1 ) return a def triv_matrix ( n, x, y, z ): #*****************************************************************************80 # ## triv_matrix() returns the TRIV matrix. # # Discussion: # # The three vectors define the subdiagonal, main diagonal, and # superdiagonal. # # Formula: # # if ( J = I - 1 ) # A(I,J) = X(J) # elseif ( J = I ) # A(I,J) = Y(I) # elseif ( J = I + 1 ) # A(I,J) = Z(I) # else # A(I,J) = 0 # # Example: # # N = 5, X = ( 1, 2, 3, 4 ), Y = ( 5, 6, 7, 8, 9 ), Z = ( 10, 11, 12, 13 ) # # 5 10 0 0 0 # 1 6 11 0 0 # 0 2 7 12 0 # 0 0 3 8 13 # 0 0 0 4 9 # # Properties: # # A is tridiagonal. # # A is banded, with bandwidth 3. # # A is generally not symmetric: A' /= A. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of A. # # real X(N-1), Y(N), Z(N-1), the vectors that define # the subdiagonal, diagonal, and superdiagonal of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j == i - 1 ): a[i,j] = x[j] elif ( j == i ): a[i,j] = y[i] elif ( j == i + 1 ): a[i,j] = z[i] return a def triv_determinant ( n, x, y, z ): #*****************************************************************************80 # ## triv_determinant() returns the determinant of the TRIV matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N-1), Y(N), Z(N-1), the vectors that define # the subdiagonal, diagonal, and superdiagonal of A. # # Output: # # real VALUE, the determinant. # determ_nm1 = y[n-1] if ( n == 1 ): value = determ_nm1 return value determ_nm2 = determ_nm1 determ_nm1 = y[n-2] * y[n-1] - z[n-2] * x[n-2] if ( n == 2 ): value = determ_nm1 return value for i in range ( n - 3, -1, -1 ): value = y[i] * determ_nm1 - z[i] * x[i] * determ_nm2 determ_nm2 = determ_nm1 determ_nm1 = value return value def triv_inverse ( n, x, y, z ): #*****************************************************************************80 # ## triv_inverse() returns the inverse of the TRIV matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 March 2015 # # Author: # # John Burkardt # # Reference: # # CM daFonseca, J Petronilho, # Explicit Inverses of Some Tridiagonal Matrices, # Linear Algebra and Its Applications, # Volume 325, 2001, pages 7-21. # # Input: # # integer N, the order of the matrix. # # real X(N-1), Y(N), Z(N-1), the vectors that define # the subdiagonal, diagonal, and superdiagonal of A. # No entry of Y can be zero. # # Output: # # real A(N,N), the inverse of the matrix. # import numpy as np for i in range ( 0, n ): if ( y[i] == 0 ): print ( '' ) print ( 'triv_inverse(): Fatal error!' ) print ( ' No entry of Y can be zero!' ) raise Exception ( 'triv_inverse(): Fatal error!' ) d = np.zeros ( n ) d[n-1] = y[n-1] for i in range ( n - 2, -1, -1 ): d[i] = y[i] - x[i] * z[i] / d[i+1] e = np.zeros ( n ) e[0] = y[0] for i in range ( 1, n ): e[i] = y[i] - x[i-1] * z[i-1] / e[i-1] a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, i + 1 ): p1 = 1.0 for k in range ( j, i ): p1 = p1 * x[k] p2 = 1.0 for k in range ( i + 1, n ): p2 = p2 * d[k] p3 = 1.0 for k in range ( j, n ): p3 = p3 * e[k] a[i,j] = r8_mop ( i + j ) * p1 * p2 / p3 for j in range ( i + 1, n ): p1 = 1.0 for k in range ( i, j ): p1 = p1 * z[k] p2 = 1.0 for k in range ( j + 1, n ): p2 = p2 * d[k] p3 = 1.0 for k in range ( i, n ): p3 = p3 * e[k] a[i,j] = r8_mop ( i + j ) * p1 * p2 / p3 return a def triw_matrix ( alpha, k, n ): #*****************************************************************************80 # ## triw_matrix() returns the TRIW matrix. # # Discussion: # # TRIW is the Wilkinson banded upper triangular matrix. # # Formula: # # if ( I = J ) # A(I,J) = 1 # elseif ( I < J and J <= K + I ) # A(I,J) = ALPHA # else # A(I,J) = 0 # # Example: # # ALPHA = 3, K = 2, N = 5 # # 1 3 3 0 0 # 0 1 3 3 0 # 0 0 1 3 3 # 0 0 0 1 3 # 0 0 0 0 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is nonsingular. # # A is upper triangular. # # det ( A ) = 1. # # A is unimodular. # # LAMBDA(1:N) = 1. # # A is Toeplitz: constant along diagonals. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # Adding -2^(2-N) to the (N,1) element makes the matrix singular. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 February 2015 # # Author: # # John Burkardt # # Reference: # # Gene Golub, James Wilkinson, # Ill-conditioned eigensystems and the computation of the Jordan # canonical form, # SIAM Review, # Volume 18, Number 4, 1976, pages 578-619. # # W Kahan, # Numerical linear algebra, # Canadian Mathematical Bulletin, # Volume 9, 1966, pages 757-801. # # AM Ostrowski, # On the spectrum of a one-parametric family of matrices, # Journal fuer Reine und Angewandte Mathematik, # Volume 193, Number (3/4), 1954, pages 143-160. # # James Wilkinson, # Singular-value decomposition - basic aspects, # in Numerical Software - Needs and Availability, # edited by DAH Jacobs, # Academic Press, London, 1978, pages 109-135. # # Input: # # real ALPHA, the superdiagonal value. # A typical value is -1. # # integer K, the number of nonzero superdiagonals. # A typical value is N-1. # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = 1.0 elif ( i < j and j - i <= k ): a[i,j] = alpha return a def triw_determinant ( alpha, k, n ): #*****************************************************************************80 # ## triw_determinant() returns the determinant of the TRIW matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 February 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, value used on the superdiagonals. # # integer K, the number of nonzero superdiagonals. # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 return value def triw_inverse ( alpha, k, n ): #*****************************************************************************80 # ## triw_inverse() returns the inverse of the TRIW matrix. # # Example: # # ALPHA = 3, K = 2, N = 5 # # 1 -3 6 -9 9 # 0 1 -3 6 -9 # 0 0 1 -3 6 # 0 0 0 1 -3 # 0 0 0 0 1 # # Properties: # # A is nonsingular. # # A is upper triangular. # # A is Toeplitz: constant along diagonals. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # det ( A ) = 1. # # A is unimodular. # # LAMBDA(1:N) = 1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Input: # # real ALPHA, value used on the superdiagonals. # # integer K, the number of nonzero superdiagonals. # # integer N, the order of A. # # Output: # # real A(N,N), the matrix. # import numpy as np k = int ( k ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): a[i,i] = 1.0 # # Compute the product of row 1 of the inverse with columns 2, # 3,..., N of the original matrix, up to, but not including, # the next unknown entry of the inverse. That unknown entry # is multiplied by 1, and the resulting sum must be zero. # So the unknown entry equals minus the sum of all the # other products. And all the entries along its superdiagonal # have the same value. # for j in range ( 1, n ): prod = 0.0 klo = max ( 0, j - k ) for kk in range ( klo, j ): prod = prod + a[0,kk] * alpha for i in range ( 0, n - j ): a[i,i+j] = - prod return a def upshift_matrix ( n ): #*****************************************************************************80 # ## upshift_matrix() returns the UPSHIFT matrix. # # Formula: # # if ( J-I == 1 mod ( n ) ) # A(I,J) = 1 # else # A(I,J) = 0 # # Example: # # N = 4 # # 0 1 0 0 # 0 0 1 0 # 0 0 0 1 # 1 0 0 0 # # Rectangular properties: # # A is integral: int ( A ) = A. # # A is a zero/one matrix. # # Square Properties: # # A is generally not symmetric: A' /= A. # # A is nonsingular. # # A is a permutation matrix. # # If N is even, det ( A ) = -1. # If N is odd, det ( A ) = +1. # # A is unimodular. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # A is a Hankel matrix: constant along anti-diagonals. # # A is an N-th root of the identity matrix. # # The inverse of A is the downshift matrix. # # A circulant matrix C, whose first row is (c1, c2, ..., cn), can be # written as a polynomial in A: # # C = c1 * I + c2 * A + c3 * A**2 + ... + cn * A**n-1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of rows and columns # of the matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i4_modp ( j - i, n ) == 1 ): a[i,j] = 1.0 return a def upshift_condition ( n ): #*****************************************************************************80 # ## upshift_condition() computes the L1 condition of the UPSHIFT matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the L1 condition. # a_norm = 1.0 b_norm = 1.0 value = a_norm * b_norm return value def upshift_determinant ( n ): #*****************************************************************************80 # ## upshift_determinant() returns the determinant of the UPSHIFT matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # if ( ( n % 2 ) == 0 ): value = - 1.0 else: value = 1.0 return value def upshift_inverse ( n ): #*****************************************************************************80 # ## upshift_inverse() returns the inverse of the UPSHIFT matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the matrix. # a = downshift_matrix ( n ) return a def vand1_matrix ( n, x ): #*****************************************************************************80 # ## vand1_matrix() returns the VAND1 matrix. # # Formula: # # A(I,J) = X(J)^(I-1) # # Example: # # N = 5, X = ( 2, 3, 4, 5, 6 ) # # 1 1 1 1 1 # 2 3 4 5 6 # 4 9 16 25 36 # 8 27 64 125 216 # 16 81 256 625 1296 # # Properties: # # A is generally not symmetric: A' /= A. # # A is nonsingular if, and only if, the X values are distinct. # # det ( A ) = product ( 1 <= I <= N ) ( 1 <= J < I ) ( X(I) - X(J) ). # = product ( 1 <= J <= N ) X(J) # * product ( 1 <= I < J ) ( X(J) - X(I) ). # # A is generally ill-conditioned. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Reference: # # Robert Gregory, David Karney, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, 1969, page 27, # LC: QA263.G68. # # Nicholas Higham, # Stability analysis of algorithms for solving confluent # Vandermonde-like systems, # SIAM Journal on Matrix Analysis and Applications, # Volume 11, 1990, pages 23-41. # # Input: # # integer N, the order of the matrix desired. # # real X(N), the values that define A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n): if ( i == 0 and x[j] == 0.0 ): a[i,j] = 1.0 else: a[i,j] = x[j] ** i return a def vand1_determinant ( n, x ): #*****************************************************************************80 # ## vand1_determinant() returns the determinant of the VAND1 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N), the parameters. # # Output: # # real VALUE, the determinant. # value = 1.0 for i in range ( 0, n ): for j in range ( 0, i ): value = value * ( x[i] - x[j] ) return value def vand1_inverse ( n, x ): #*****************************************************************************80 # ## vand1_inverse() returns the inverse of the VAND1 matrix. # # Formula: # # A(I,J) = coefficient of X^(J-1) in I-th Lagrange basis polynomial. # # Example: # # N = 5, # X = ( 2, 3, 4, 5, 6 ) # # 15.00 -14.25 4.96 -0.75 0.04 # -40.00 44.67 -17.33 2.83 -0.17 # 45.00 -54.00 22.75 -4.00 0.25 # -24.00 30.00 -13.33 2.50 -0.17 # 5.00 -6.42 2.96 -0.58 0.04 # # Properties: # # The sum of the entries of A is # # 1 - product ( 1 <= I <= N ) ( 1 - 1 / X(I) ). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N), the values that define A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): a[i,0] = 1.0 for i in range ( 0, n ): index = 0 for k in range ( 0, n ): if ( k != i ): for j in range ( index + 1, -1, -1 ): a[i,j] = - x[k] * a[i,j] / ( x[i] - x[k] ) if ( 0 < j ): a[i,j] = a[i,j] + a[i,j-1] / ( x[i] - x[k] ) index = index + 1 return a def vand2_matrix ( n, x ): #*****************************************************************************80 # ## vand2_matrix() returns the VAND2 matrix. # # Discussion: # # This is the Vandermonde matrix with 1's in the first column. # # Formula: # # A(I,J) = X(J)^(J-1) # # Example: # # N = 5, X = ( 2, 3, 4, 5, 6 ) # # 1 2 4 8 16 # 1 3 9 27 81 # 1 4 16 64 256 # 1 5 25 125 625 # 1 6 36 216 1296 # # Properties: # # A is generally not symmetric: A' /= A. # # A is nonsingular if, and only if, the X values are distinct. # # det ( A ) = product ( 1 <= I <= N ) ( 1 <= J < I ) ( X(I) - X(J) ). # = product ( 1 <= J <= N ) X(J) # * product ( 1 <= I < J ) ( X(J) - X(I) ). # # A is generally ill-conditioned. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Reference: # # Robert Gregory, David Karney, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, 1969, page 27, # LC: QA263.G68. # # Nicholas Higham, # Stability analysis of algorithms for solving confluent # Vandermonde-like systems, # SIAM Journal on Matrix Analysis and Applications, # Volume 11, 1990, pages 23-41. # # Input: # # integer N, the order of the matrix desired. # # real X(N), the values that define A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n): if ( j == 0 and x[i] == 0.0 ): a[i,j] = 1.0 else: a[i,j] = x[i] ** j return a def vand2_determinant ( n, x ): #*****************************************************************************80 # ## vand2_determinant() returns the determinant of the VAND2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N), the parameters. # # Output: # # real VALUE, the determinant. # value = 1.0 for i in range ( 0, n ): for j in range ( 0, i ): value = value * ( x[i] - x[j] ) return value def vand2_inverse ( n, x ): #*****************************************************************************80 # ## vand2_inverse() returns the inverse of the VAND2 matrix. # # Formula: # # A(I,J) = coefficient of X^(I-1) in J-th Lagrange basis polynomial. # # Example: # # N = 5, X = ( 2, 3, 4, 5, 6 ) # # 15.00 -40.00 45.00 -24.00 5.00 # -14.25 44.67 -54.00 30.00 -6.42 # 4.96 -17.33 22.75 -13.33 2.96 # -0.75 2.83 -4.00 2.50 -0.58 # 0.04 -0.17 0.25 -0.17 0.04 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # real X(N), the values that define A. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for j in range ( 0, n ): a[0,j] = 1.0 for i in range ( 0, n ): index = 0 for k in range ( 0, n ): if ( k != i ): for j in range ( index + 1, -1, -1 ): a[j,i] = - x[k] * a[j,i] / ( x[i] - x[k] ) if ( 0 < j ): a[j,i] = a[j,i] + a[j-1,i] / ( x[i] - x[k] ) index = index + 1 return a def vand2_lu ( n, x ): #*****************************************************************************80 # ## vand2_lu() returns the LU factors of the Vandermonde2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 October 2021 # # Author: # # John Burkardt # # Reference: # # Halil Oruc, George Phillips, # Explicit factorization of the Vandermonde matrix, # Linear Algebra and its Applications, # Volume 315, Number 1-3, 15 August 2000, pages 113-123. # # Input: # # integer N, the order of the matrix. # # real X(N), the values that define the matrix. # # Output: # # real L(N,N), U(N,N), the LU factors. # import numpy as np l = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, i + 1 ): l[i,j] = 1.0 for k in range ( 0, j ): l[i,j] = l[i,j] * ( x[i] - x[k] ) / ( x[j] - x[k] ) u = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( i, n ): u[i,j] = complete_symmetric_poly ( i + 1, j - i, x ) for k in range ( 0, i ): u[i,j] = u[i,j] * ( x[i] - x[k] ) return l, u def vand2_plu ( n, x ): #*****************************************************************************80 # ## vand2_plu() returns the PLU factors of the Vandermonde2 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Reference: # # Halil Oruc, George Phillips, # Explicit factorization of the Vandermonde matrix, # Linear Algebra and its Applications, # Volume 315, Number 1-3, 15 August 2000, pages 113-123. # # Input: # # integer N, the order of the matrix. # # real X(N), the values that define the matrix. # # Output: # # real P(N,N), L(N,N), U(N,N), the PLU factors. # import numpy as np p = np.zeros ( ( n, n ) ) for j in range ( 0, n ): p[j,j] = 1.0 l = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, i + 1 ): l[i,j] = 1.0 for k in range ( 0, j ): l[i,j] = l[i,j] * ( x[i] - x[k] ) / ( x[j] - x[k] ) u = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( i, n ): u[i,j] = complete_symmetric_poly ( i + 1, j - i, x ) for k in range ( 0, i ): u[i,j] = u[i,j] * ( x[i] - x[k] ) return p, l, u def wathen_matrix ( nx, ny, key ): #*****************************************************************************80 # ## wathen_matrix() returns the Wathen matrix, using general (GE) storage. # # Discussion: # # The Wathen matrix is a finite element matrix which is sparse. # # The entries of the matrix depend in part on a physical quantity # related to density. That density is here assigned random values between # 0 and 100. # # The matrix order N is determined by the input quantities NX and NY, # which would usually be the number of elements in the X and Y directions. # The value of N is # # N = 3*NX*NY + 2*NX + 2*NY + 1, # # The matrix is the consistent mass matrix for a regular NX by NY grid # of 8 node serendipity elements. # # The local element numbering is # # 3--2--1 # | | # 4 8 # | | # 5--6--7 # # Here is an illustration for NX = 3, NY = 2: # # 23-24-25-26-27-28-29 # | | | | # 19 20 21 22 # | | | | # 12-13-14-15-16-17-18 # | | | | # 8 9 10 11 # | | | | # 1--2--3--4--5--6--7 # # For this example, the total number of nodes is, as expected, # # N = 3 * 3 * 2 + 2 * 2 + 2 * 3 + 1 = 29 # # The matrix is symmetric positive definite for any positive values of the # density RHO(X,Y). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 May 2020 # # Author: # # John Burkardt # # Reference: # # Nicholas Higham, # Algorithm 694: A Collection of Test Matrices in MATLAB, # ACM Transactions on Mathematical Software, # Volume 17, Number 3, September 1991, pages 289-305. # # Andrew Wathen, # Realistic eigenvalue bounds for the Galerkin mass matrix, # IMA Journal of Numerical Analysis, # Volume 7, Number 4, October 1987, pages 449-457. # # Input: # # integer NX, NY, values which determine the size of the matrix. # # integer KEY, the random number seed. # # Output: # # real A(N,N), the matrix. # from numpy.random import default_rng import numpy as np rng = default_rng ( key ) n = wathen_order ( nx, ny ) A = np.zeros ( ( n, n ) ) em = np.array \ ( \ ( ( 6.0, -6.0, 2.0, -8.0, 3.0, -8.0, 2.0, -6.0 ), \ (-6.0, 32.0, -6.0, 20.0, -8.0, 16.0, -8.0, 20.0 ), \ ( 2.0, -6.0, 6.0, -6.0, 2.0, -8.0, 3.0, -8.0 ), \ (-8.0, 20.0, -6.0, 32.0, -6.0, 20.0, -8.0, 16.0 ), \ ( 3.0, -8.0, 2.0, -6.0, 6.0, -6.0, 2.0, -8.0 ), \ (-8.0, 16.0, -8.0, 20.0, -6.0, 32.0, -6.0, 20.0 ), \ ( 2.0, -8.0, 3.0, -8.0, 2.0, -6.0, 6.0, -6.0 ), \ (-6.0, 20.0, -8.0, 16.0, -8.0, 20.0, -6.0, 32.0 ) )\ ) node = np.zeros ( 8, dtype = np.int32 ) for j in range ( 0, ny ): for i in range ( 0, nx ): # # For the element (I,J), determine the indices of the 8 nodes. # node[0] = ( 3 * ( j + 1 ) ) * nx + 2 * ( j + 1 ) + 2 * ( i + 1 ) + 1 - 1 node[1] = node[0] - 1 node[2] = node[0] - 2 node[3] = ( 3 * ( j + 1 ) - 1 ) * nx + 2 * ( j + 1 ) + ( i + 1 ) - 1 - 1 node[7] = node[3] + 1 node[4] = ( 3 * ( j + 1 ) - 3 ) * nx + 2 * ( j + 1 ) + 2 * ( i + 1 ) - 3 - 1 node[5] = node[4] + 1 node[6] = node[4] + 2 rho = 100.0 * rng.random ( ) for krow in range ( 0, 8 ): for kcol in range ( 0, 8 ): A[node[krow],node[kcol]] = A[node[krow],node[kcol]] \ + rho * em[krow,kcol] return A def wathen_order ( nx, ny ): #*****************************************************************************80 # ## wathen_order() returns the order of the WATHEN matrix. # # Discussion: # # N = 3 * 3 * 2 + 2 * 2 + 2 * 3 + 1 = 29 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # # Reference: # # Nicholas Higham, # Algorithm 694: A Collection of Test Matrices in MATLAB, # ACM Transactions on Mathematical Software, # Volume 17, Number 3, September 1991, pages 289-305. # # Andrew Wathen, # Realistic eigenvalue bounds for the Galerkin mass matrix, # IMA Journal of Numerical Analysis, # Volume 7, 1987, pages 449-457. # # Input: # # integer NX, NY, values which determine the size of A. # # Output: # # integer N, the order of the matrix, # as determined by NX and NY. # n = 3 * nx * ny + 2 * nx + 2 * ny + 1 return n def wilk03_matrix ( ): #*****************************************************************************80 # ## wilk03_matrix() returns the wilk03 matrix. # # Formula: # # 1.0E-10 0.9 -0.4 # 0 0.9 -0.4 # 0 0 1.0E-10 # # Discussion: # # The linear equation under study is # A * X = B, # where A is the 3 by 3 Wilkinson matrix, and # B = ( 0, 0, 1 )' # and the correct solution is # X = ( 0, 4.0E+10 / 9.0, 1.0E+10 ) # # Since the matrix is already in upper triangular form, errors can # occur only in the backsubstitution. # # Properties: # # A is generally not symmetric: A' /= A. # # A is upper triangular. # # det ( A ) = 0.9E-20 # # LAMBDA = ( 1.0E-10, 0.9, 1.0E-10 ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 December 2014 # # Author: # # John Burkardt # # Reference: # # James Wilkinson, # Error Analysis of Direct Methods of Matrix Inversion, # Journal of the Association for Computing Machinery, # Volume 8, 1961, pages 281-330. # # Output: # # real A(3,3), the matrix. # import numpy as np a = np.array ( [ \ [ 1.0E-10, 0.9, -0.4 ], \ [ 0.0, 0.9, -0.4 ], \ [ 0.0, 0.0, 1.0E-10 ] ] ) return a def wilk03_condition ( ): #*****************************************************************************80 # ## wilk03_condition() returns the L1 condition of the wilk03 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real COND, the L1 condition number. # cond = 1.8 * ( 13.0 * 1.0E+10 / 9.0 ) return cond def wilk03_determinant ( ): #*****************************************************************************80 # ## wilk03_determinant() returns the determinant of the wilk03 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 0.9E-20 return value def wilk03_inverse ( ): #*****************************************************************************80 # ## wilk03_inverse() returns the inverse of the wilk03 matrix. # # Formula: # # 1.0D+10 -1.0D+10 0 # 0 10/9 4/9 * 1.0D+10 # 0 0 1.0D+10 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Output: # # real A(3,3), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 1.0E+10, - 1.0E+10, 0.0 ], \ [ 0.0, 10.0 / 9.0, 4.0E+10 / 9.0 ], \ [ 0.0, 0.0, 1.0E+10 ] ] ) return a def wilk03_rhs ( ): #*****************************************************************************80 # ## wilk03_rhs() returns the right hand side of the wilk03 linear system. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Output: # # real B(3,1), the right hand side of the system. # import numpy as np b = np.array ( [ [ 0.0 ], [ 0.0 ], [ 1.0 ] ] ) return b def wilk03_solution ( ): #*****************************************************************************80 # ## wilk03_solution() returns the solution of the wilk03 linear system. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Output: # # real X(3,1), the solution of the linear system. # import numpy as np x = np.array ( [ [ 0.0 ], [ 4.0E+10 / 9.0 ], [ 1.0E+10 ] ] ) return x def wilk04_matrix ( ): #*****************************************************************************80 # ## wilk04_matrix() returns the wilk04 matrix. # # Formula: # # 0.9143E-04 0.0 0.0 0.0 # 0.8762 0.7156E-04 0.0 0.0 # 0.7943 0.8143 0.9504E-04 0.0 # 0.8017 0.6123 0.7165 0.7123E-04 # # Properties: # # A is lower triangular. # # LAMBDA = ( 0.9143E-04, 0.7156E-04, 0.9504E-04, 0.7123E-04 ). # # Discussion: # # Since the matrix is already in lower triangular form, errors can # occur only in the backsubstitution. However, even a double # precision calculation will show a significant degradation in the # solution. It is also instructive to compare the actual error in # the solution to the residual error, A*x-b. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Reference: # # James Wilkinson, # Rounding Errors in Algebraic Processes, # Prentice Hall, 1963, page 105. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.array ( [ \ [ 0.9143E-04, 0.8762, 0.7943, 0.8017 ], \ [ 0.0000, 0.7156E-04, 0.8143, 0.6123 ], \ [ 0.0000, 0.0000, 0.9504E-04, 0.7165 ], \ [ 0.0000, 0.0000, 0.0000, 0.7123E-04 ] ] ) return a def wilk04_condition ( ): #*****************************************************************************80 # ## wilk04_condition() returns the L1 condition of the wilk04 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the L1 condition. # a_norm = 2.1306 b_norm = 1.154098458240528E+16 value = a_norm * b_norm return value def wilk04_determinant ( ): #*****************************************************************************80 # ## wilk04_determinant() returns the determinant of the wilk04 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 0.9143E-04 * 0.7156E-04 * 0.9504E-04 * 0.7123E-04 return value def wilk04_inverse ( ): #*****************************************************************************80 # ## wilk04_inverse() returns the inverse of the wilk04 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 0.000000000001094E+16, \ -0.000000013391962E+16, \ 0.000114732803288E+16, \ -1.153978022391245E+16 ], \ [ 0.000000000000000, \ 0.000000000001397E+16, \ -0.000000011973129E+16, \ 0.000120425263952E+16 ], \ [ 0.000000000000000, \ 0.000000000000000, \ 0.000000000001052E+16, \ -0.000000010583927E+16 ], \ [ 0.000000000000000, \ 0.000000000000000, \ 0.000000000000000, \ 0.000000000001404E+16 ] ] ) return a def wilk04_rhs ( ): #*****************************************************************************80 # ## wilk04_rhs() returns the right hand side of the wilk04 linear system. # # Formula: # # 0.6524 # 0.3127 # 0.4186 # 0.7853 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Output: # # real B(4,1), the right hand side of the system. # import numpy as np b = np.array ( [ [ 0.6524 ], [ 0.3127 ], [ 0.4186 ], [ 0.7853 ] ] ) return b def wilk04_solution ( ): #*****************************************************************************80 # ## wilk04_solution() returns the solution of the wilk04 linear system. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Output: # # real X(4,1), the solution of the system. # import numpy as np x = np.array ( [ \ [ -9.061709180193406e+15 ], \ [ 9.456494826647572e+11 ], \ [ -8.311117178175363e+07 ], \ [ 1.102484908044364e+04 ] ] ) return x def wilk05_matrix ( ): #*****************************************************************************80 # ## wilk05_matrix() returns the wilk05 matrix. # # Formula: # # A(I,J) = 1.8144 / ( I + J + 1 ) # # Example: # # 0.604800 0.453600 0.362880 0.302400 0.259200 # 0.453600 0.362880 0.302400 0.259200 0.226800 # 0.362880 0.302400 0.259200 0.226800 0.201600 # 0.302400 0.259200 0.226800 0.201600 0.181440 # 0.259200 0.226800 0.201600 0.181440 0.164945 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is essentially a scaled portion of the Hilbert matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Reference: # # James Wilkinson, # The Algebraic Eigenvalue Problem, # Oxford University Press, 1965, # page 234. # # Output: # # real A(5,5), the matrix. # import numpy as np n = 5 a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = 1.8144 / float ( i + j + 3 ) return a def wilk05_condition ( ): #*****************************************************************************80 # ## wilk05_condition() returns the L1 condition of the wilk05 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the L1 condition. # a_norm = 1.98288 b_norm = 4.002777777857721E+06 value = a_norm * b_norm return value def wilk05_determinant ( ): #*****************************************************************************80 # ## wilk05_determinant() returns the determinant of the wilk05 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 3.7995E-15 return value def wilk05_inverse ( ): #*****************************************************************************80 # ## wilk05_inverse() returns the inverse of the wilk05 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Output: # # real A(5,5), the matrix. # import numpy as np a = np.array ( [ \ [ 0.002025462963002E+06, \ -0.016203703704040E+06, \ 0.043750000000952E+06, \ -0.048611111112203E+06, \ 0.019097222222661E+06 ], \ [ -0.016203703704042E+06, \ 0.138271604941179E+06, \ -0.388888888897095E+06, \ 0.444444444453843E+06, \ -0.178240740744515E+06 ], \ [ 0.043750000000962E+06, \ -0.388888888897136E+06, \ 1.125000000023251E+06, \ -1.312500000026604E+06, \ 0.534722222232897E+06 ], \ [ -0.048611111112219E+06, \ 0.444444444453930E+06, \ -1.312500000026719E+06, \ 1.555555555586107E+06, \ -0.641666666678918E+06 ], \ [ 0.019097222222669E+06, \ -0.178240740744564E+06, \ 0.534722222232983E+06, \ -0.641666666678964E+06, \ 0.267361111116040E+06 ] ] ) return a def wilk12_matrix ( ): #*****************************************************************************80 # ## wilk12_matrix() returns the wilk12 matrix. # # Formula: # # 12 11 0 0 0 0 0 0 0 0 0 0 # 11 11 10 0 0 0 0 0 0 0 0 0 # 10 10 10 9 0 0 0 0 0 0 0 0 # 9 9 9 9 8 0 0 0 0 0 0 0 # 8 8 8 8 8 7 0 0 0 0 0 0 # 7 7 7 7 7 7 6 0 0 0 0 0 # 6 6 6 6 6 6 6 5 0 0 0 0 # 5 5 5 5 5 5 5 5 4 0 0 0 # 4 4 4 4 4 4 4 4 4 3 0 0 # 3 3 3 3 3 3 3 3 3 3 2 0 # 2 2 2 2 2 2 2 2 2 2 2 1 # 1 1 1 1 1 1 1 1 1 1 1 1 # # Properties: # # A is generally not symmetric: A' /= A. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # det ( A ) = 1. # # A is lower Hessenberg. # # The smaller eigenvalues are very ill conditioned. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 February 2015 # # Author: # # John Burkardt # # Reference: # # James Wilkinson, # Rounding Errors in Algebraic Processes, # Prentice Hall, 1963, # page 151. # # Output: # # real A(12,12), the matrix. # import numpy as np n = 12 a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j <= i + 1 ): a[i,j] = float ( n - max ( i, j ) ) return a def wilk12_condition ( ): #*****************************************************************************80 # ## wilk12_condition() returns the L1 condition of the wilk12 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 April 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the L1 condition. # a_norm = 78.0 b_norm = 87909427.13689443 value = a_norm * b_norm return value def wilk12_determinant ( ): #*****************************************************************************80 # ## wilk12_determinant() returns the determinant of the wilk12 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 return value def wilk12_eigen_right ( ): #*****************************************************************************80 # ## wilk12_eigen_right() returns the right eigenvectors of the wilk12 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Output: # # real X(12,12), the right eigenvector matrix. # import numpy as np x = np.array ( [ \ [ 0.075953934362606, 0.139678536121698, \ 0.212972721043730, 0.286424756003626, \ 0.349485357102525, 0.392486174053140, \ 0.408397328102426, 0.393960067241308, \ 0.350025473229225, 0.281131870150006, \ 0.194509944233873, 0.098787565402021 ], \ [ 0.047186270176379, 0.035170881219766, \ -0.019551243493406, -0.113663824929275, \ -0.229771631994320, -0.342302599090153, \ -0.425606879283194, -0.461118871576638, \ -0.441461339130489, -0.370865208095037, \ -0.262574394436703, -0.134619530658877 ], \ [ 0.087498415888682, 0.002474434526797, \ -0.095923839958749, -0.124601769209776, \ -0.044875899531161, 0.121565513387420, \ 0.312274076477727, 0.458792947263280, \ 0.515554022627437, 0.471997957002961, \ 0.348267903145709, 0.181505588624358 ], \ [ 0.356080027225304, -0.163099766915005, \ -0.325820728704039, -0.104423010988819, \ 0.176053383568728, 0.245040317292912, \ 0.069840787629820, -0.207165420169259, \ -0.418679217847974, -0.475318237218216, \ -0.383234018094179, -0.206444528035974 ], \ [ -0.709141914617340, 0.547208974924657, \ 0.370298143032545, -0.087024255226817, \ -0.174710647675812, -0.026657290116937, \ 0.077762060814618, 0.057335745807230, \ -0.018499801182824, -0.070417566622935, \ -0.072878348819266, -0.042488463457934 ], \ [ -0.713561589955660, 0.677624765946043, \ 0.144832629941422, -0.095987754186127, \ -0.033167043991408, 0.015790103726845, \ 0.009303310423290, -0.002909858414229, \ -0.003536176142936, 0.000317090937139, \ 0.002188160441481, 0.001613099168127 ], \ [ 0.694800915350134, -0.717318445412803, \ -0.021390540433709, 0.047257308713196, \ 0.000033398195785, -0.003862799912030, \ 0.000145902034404, 0.000419891505074, \ -0.000039486945846, -0.000069994145516, \ 0.000013255774472, 0.000029720715023 ], \ [ 0.684104842982405, -0.728587222991804, \ 0.028184117194646, 0.019000894182572, \ -0.002364147875169, -0.000483008341150, \ 0.000145689574886, 0.000006899341493, \ -0.000009588938470, 0.000001123011584, \ 0.000000762677095, -0.000000504464129 ], \ [ 0.679348386306787, -0.732235872680797, \ 0.047657921019166, 0.006571283153133, \ -0.001391439772868, 0.000028271472280, \ 0.000025702435813, -0.000004363907083, \ -0.000000016748075, 0.000000170826901, \ -0.000000050888575, 0.000000010256625 ], \ [ 0.677141058069838, -0.733699103817717, \ 0.056254187307821, 0.000845330889853, \ -0.000600573479254, 0.000060575011829, \ -0.000000899585454, -0.000000703890529, \ 0.000000147573166, -0.000000020110423, \ 0.000000002229508, -0.000000000216223 ], \ [ 0.675994567035284, -0.734406182106934, \ 0.060616915148887, -0.002116889869553, \ -0.000112561724387, 0.000026805640571, \ -0.000002875297806, 0.000000236938971, \ -0.000000016773740, 0.000000001068110, \ -0.000000000062701, 0.000000000003446 ], \ [ -0.675318870608569, 0.734806603365595, \ -0.063156546323253, 0.003858723645845, \ -0.000198682768218, 0.000009145253582, \ -0.000000387365950, 0.000000015357316, \ -0.000000000576294, 0.000000000020662, \ -0.000000000000713, 0.000000000000023 ] ] ) x = np.transpose ( x ) return x def wilk12_eigenvalues ( ): #*****************************************************************************80 # ## wilk12_eigenvalues() returns the eigenvalues of the wilk12 matrix. # # Formula: # # 32.2288915 # 20.1989886 # 12.3110774 # 6.96153309 # 3.51185595 # 1.55398871 # 0.643505319 # 0.284749721 # 0.143646520 # 0.081227659240405 # 0.049507429185278 # 0.031028060644010 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Output: # # real LAM(12), the eigenvalues. # import numpy as np lam = np.array ( [ \ 32.2288915, \ 20.1989886, \ 12.3110774, \ 6.96153309, \ 3.51185595, \ 1.55398871, \ 0.643505319, \ 0.284749721, \ 0.143646520, \ 0.081227659240405, \ 0.049507429185278, \ 0.031028060644010 ] ) return lam def wilk20_matrix ( alpha ): #*****************************************************************************80 # ## wilk20_matrix() returns the wilk20 matrix. # # Formula: # # if ( I = J ) # A(I,J) = I # elseif ( I = J-1 ) # A(I,J) = 20 # elseif ( I = N, J = 1 ) # A(I,J) = ALPHA # else # A(I,J) = 0 # # Example: # # 1 20 . . . . . . . . . . . . . . . . . . # . 2 20 . . . . . . . . . . . . . . . . . # . . 3 20 . . . . . . . . . . . . . . . . # . . . 4 20 . . . . . . . . . . . . . . . # . . . . 5 20 . . . . . . . . . . . . . . # . . . . . 6 20 . . . . . . . . . . . . . # . . . . . . 7 20 . . . . . . . . . . . . # . . . . . . . 8 20 . . . . . . . . . . . # . . . . . . . . 9 20 . . . . . . . . . . # . . . . . . . . . 10 20 . . . . . . . . . # . . . . . . . . . . 11 20 . . . . . . . . # . . . . . . . . . . . 12 20 . . . . . . . # . . . . . . . . . . . . 13 20 . . . . . . # . . . . . . . . . . . . . 14 20 . . . . . # . . . . . . . . . . . . . . 15 20 . . . . # . . . . . . . . . . . . . . . 16 20 . . . # . . . . . . . . . . . . . . . . 17 20 . . # . . . . . . . . . . . . . . . . . 18 20 . # . . . . . . . . . . . . . . . . . . 19 20 # ALPHA. . . . . . . . . . . . . . . . . . 20 # # Properties: # # A is generally not symmetric: A' /= A. # # A is not diagonally dominant. # # If ALPHA = 0, then # LAMBDA(I) = i # and the characteristic equation is # product ( 1 <= I <= 20 ) ( I - LAMBDA ) = 0 # and the condition number of eigenvalue I is # COND(LAMBDA(I)) = (20-I)! * (I-1)! / 20^19. # # If ALPHA is nonzero, the characteristic equation is # product ( 1 <= I <= 20 ) ( I - LAMBDA ) = 20^19 * ALPHA. # # If ALPHA = 1.0E-10, there are 6 real eigenvalues, and 14 complex # eigenvalues with considerable imaginary parts. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 October 2007 # # Author: # # John Burkardt # # Reference: # # Robert Gregory, David Karney, # Example 5.23, # A Collection of Matrices for Testing Computational Algorithms, # Wiley, 1969, page 104, # LC: QA263.G68. # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # James Wilkinson, # Rounding Errors in Algebraic Processes, # Prentice Hall, 1963, # page 138. # # Input: # # real ALPHA, the perturbation. # # Output: # # real A(20,20), the matrix. # import numpy as np n = 20 a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = i + 1 elif ( j == i + 1 ): a[i,j] = n else: a[i,j] = 0.0 a[n-1,0] = alpha return a def wilk21_matrix ( n ): #*****************************************************************************80 # ## wilk21_matrix() returns the wilk21 matrix. # # Discussion: # # By using values of N not equal to 21, wilk21 can return a variety # of related matrices. # # Formula: # # if ( I = J ) # A(I,J) = nint ( abs ( i - ( n+1 ) / 2 ) ) # elseif ( I = J - 1 or I = J + 1 ) # A(I,J) = 1 # else # A(I,J) = 0 # # Example: # # N = 21 # # 10 1 . . . . . . . . . . . . . . . . . . . # 1 9 1 . . . . . . . . . . . . . . . . . . # . 1 8 1 . . . . . . . . . . . . . . . . . # . . 1 7 1 . . . . . . . . . . . . . . . . # . . . 1 6 1 . . . . . . . . . . . . . . . # . . . . 1 5 1 . . . . . . . . . . . . . . # . . . . . 1 4 1 . . . . . . . . . . . . . # . . . . . . 1 3 1 . . . . . . . . . . . . # . . . . . . . 1 2 1 . . . . . . . . . . . # . . . . . . . . 1 1 1 . . . . . . . . . . # . . . . . . . . . 1 0 1 . . . . . . . . . # . . . . . . . . . . 1 1 1 . . . . . . . . # . . . . . . . . . . . 1 2 1 . . . . . . . # . . . . . . . . . . . . 1 3 1 . . . . . . # . . . . . . . . . . . . . 1 4 1 . . . . . # . . . . . . . . . . . . . . 1 5 1 . . . . # . . . . . . . . . . . . . . . 1 6 1 . . . # . . . . . . . . . . . . . . . . 1 7 1 . . # . . . . . . . . . . . . . . . . . 1 8 1 . # . . . . . . . . . . . . . . . . . . 1 9 1 # . . . . . . . . . . . . . . . . . . . 1 10 # # Properties: # # A is tridiagonal. # # Because A is tridiagonal, it has property A (bipartite). # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Reference: # # James Wilkinson, # The Algebraic Eigenvalue Problem, # Oxford University Press, 1965, # page 308. # # Input: # # integer N, the order of the desired matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): a[i,j] = float ( abs ( i + 1 - ( n + 1 ) // 2 ) ) elif ( j == i + 1 ): a[i,j] = 1.0 elif ( j == i - 1 ): a[i,j] = 1.0 return a def wilk21_determinant ( n ): #*****************************************************************************80 # ## wilk21_determinant() returns the determinant of the wilk21 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # import numpy as np d = np.zeros ( n ) for i in range ( 0, n ): d[i] = abs ( float ( i + 1 - ( n + 1 ) // 2 ) ) determ_nm1 = d[n-1] if ( n == 1 ): value = determ_nm1 return value determ_nm2 = determ_nm1 determ_nm1 = d[n-2] * d[n-1] - 1.0 if ( n == 2 ): value = determ_nm1 return value for i in range ( n - 3, -1, -1 ): value = d[i] * determ_nm1 - determ_nm2 determ_nm2 = determ_nm1 determ_nm1 = value return value def wilk21_inverse ( n ): #*****************************************************************************80 # ## wilk21_inverse() returns the inverse of the wilk21 matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Reference: # # CM daFonseca, J Petronilho, # Explicit Inverses of Some Tridiagonal Matrices, # Linear Algebra and Its Applications, # Volume 325, 2001, pages 7-21. # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the inverse of the matrix. # import numpy as np y = np.zeros ( n ) for i in range ( 0, n ): y[i] = float ( abs ( i + 1 - ( n + 1 ) // 2 ) ) d = np.zeros ( n ) d[n-1] = y[n-1] for i in range ( n - 2, -1, -1 ): d[i] = y[i] - 1.0 / d[i+1] e = np.zeros ( n ) e[0] = y[0] for i in range ( 1, n ): e[i] = y[i] - 1.0 / e[i-1] a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, i + 1 ): p1 = 1.0 for k in range ( i + 1, n ): p1 = p1 * d[k] p2 = 1.0 for k in range ( j, n ): p2 = p2 * e[k] a[i,j] = r8_mop ( i + j ) * p1 / p2 for j in range ( i + 1, n ): p1 = 1.0 for k in range ( j + 1, n ): p1 = p1 * d[k] p2 = 1.0 for k in range ( i, n ): p2 = p2 * e[k] a[i,j] = r8_mop ( i + j ) * p1 / p2 return a def wilson_matrix ( ): #*****************************************************************************80 # ## wilson_matrix() returns the Wilson matrix. # # Formula: # # A = # 5 7 6 5 # 7 10 8 7 # 6 8 10 9 # 5 7 9 10 # # Properties: # # The Higham/MATLAB version of this matrix has rows and columns # 1 and 2 interchanged. # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is positive definite. # # det ( A ) = 1. # # A is ill-conditioned. # # A * X = B, where X is the Wilson solution vector, and B is the # Wilson right hand side. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 December 2014 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(4,4), the matrix. # import numpy as np a = np.array ( [ \ [ 5.0, 7.0, 6.0, 5.0 ], \ [ 7.0, 10.0, 8.0, 7.0 ], \ [ 6.0, 8.0, 10.0, 9.0 ], \ [ 5.0, 7.0, 9.0, 10.0 ] ] ) return a def wilson_condition ( ): #*****************************************************************************80 # ## wilson_condition() returns the L1 condition of the WILSON matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 January 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real COND, the L1 condition number. # cond = 4488.0 return cond def wilson_determinant ( ): #*****************************************************************************80 # ## wilson_determinant() returns the determinant of the WILSON matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 1.0 return value def wilson_eigen_right ( ): #*****************************************************************************80 # ## wilson_eigen_right() returns the right eigenvectors of the WILSON matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Output: # # real A(4,4), the right eigenvector matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 0.380262074390714, \ 0.396305561186082, \ 0.093305039089285, \ 0.830443752841578 ], \ [ 0.528567849528642, \ 0.614861280394151, \ -0.301652326903523, \ -0.501565058582058 ], \ [ 0.551954849631663, \ -0.271601039711768, \ 0.760318430013036, \ -0.208553600252039 ], \ [ 0.520924780743657, \ -0.625396181050490, \ -0.567640668325261, \ 0.123697458332363 ] ] ) return a def wilson_eigenvalues ( ): #*****************************************************************************80 # ## wilson_eigenvalues() returns the eigenvalues of the WILSON matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Output: # # real LAM(4), the eigenvalues.. # import numpy as np lam = np.array ( [ \ 30.288685345802129, \ 3.858057455944950, \ 0.843107149855033, \ 0.010150048397892 ] ) return lam def wilson_inverse ( ): #*****************************************************************************80 # ## wilson_inverse() returns the inverse of the Wilson matrix. # # Formula: # # 68 -41 -17 10 # -41 25 10 -6 # -17 10 5 -3 # 10 -6 -3 2 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 March 2015 # # Author: # # John Burkardt # # Reference: # # Joan Westlake, # A Handbook of Numerical Matrix Inversion and Solution of # Linear Equations, # John Wiley, 1968, # ISBN13: 978-0471936756, # LC: QA263.W47. # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 68.0, -41.0, -17.0, 10.0 ], \ [ -41.0, 25.0, 10.0, -6.0 ], \ [ -17.0, 10.0, 5.0, -3.0 ], \ [ 10.0, -6.0, -3.0, 2.0 ] ] ) return a def wilson_llt ( ): #*****************************************************************************80 # ## wilson_llt() returns the lower triangular Cholesky factor of the WILSON matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Output: # # real A(4,4), the matrix. # import numpy as np # # Note that the matrix entries are listed by row. # a = np.array ( [ \ [ 2.236067977499790, 0.0, \ 0.0, 0.0 ], \ [ 3.130495168499706, 0.447213595499957, \ 0.0, 0.0 ], \ [ 2.683281572999748, -0.894427190999918, \ 1.414213562373093, 0.0 ], \ [ 2.236067977499790, 0.0, \ 2.121320343559645, 0.707106781186539 ] ] ) return a def wilson_plu ( ): #*****************************************************************************80 # ## wilson_plu() returns the PLU factors of the WILSON matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # # Output: # # P(4,4), L(4,4), U(4,4), the PLU factors. # import numpy as np p = np.array ( [ \ [ 0.0, 0.0, 0.0, 1.0 ], \ [ 1.0, 0.0, 0.0, 0.0 ], \ [ 0.0, 1.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 1.0, 0.0 ] ] ) l = np.array ( [ \ [ 1.0, 0.00, 0.00, 0.00 ], \ [ 0.857142857142857, 1.00, 0.00, 0.00 ], \ [ 0.714285714285714, 0.25, 1.00, 0.00 ], \ [ 0.714285714285714, 0.25, -0.20, 1.00 ] ] ) u = np.array ( [ \ [ 7.00, 10.0, 8.0, 7.00 ], \ [ 0.00, -0.571428571428571, 3.142857142857143, 3.00 ], \ [ 0.00, 0.0, 2.50, 4.25 ], \ [ 0.00, 0.0, 0.0, 0.10 ] ] ) return p, l, u def wilson_rhs ( ): #*****************************************************************************80 # ## wilson_rhs() returns the WILSON right hand side. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Output: # # real B(4,1), the right hand side vector. # import numpy as np b = np.array ( [ [ 23.0 ], [ 32.0 ], [ 33.0 ], [ 31.0 ] ] ) return b def wilson_solution ( ): #*****************************************************************************80 # ## wilson_solution() returns the WILSON solution. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Output: # # real X(4,1), the solution vector. # import numpy as np x = np.array ( [ [ 1.0 ], [ 1.0 ], [ 1.0 ], [ 1.0 ] ] ) return x def zero_matrix ( m, n ): #*****************************************************************************80 # ## zero_matrix() returns the ZERO matrix. # # Formula: # # A(I,J) = 0 # # Example: # # M = 4, N = 5 # # 0 0 0 0 0 # 0 0 0 0 0 # 0 0 0 0 0 # 0 0 0 0 0 # # Properties: # # A is integral. # # A is Toeplitz: constant along diagonals. # # A is a Hankel matrix: constant along anti-diagonals. # # A is a circulant matrix: each row is shifted once to get the next row. # # A is an anticirculant matrix. # # A is singular. # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # LAMBDA(1:N) = 0. # # The matrix of eigenvectors of A is I. # # det ( A ) = 0. # # For any vector v, A*v = 0. # # For any matrix B, A*B = B*A = 0. # # A is persymmetric: A(I,J) = A(N+1-J,N+1-I). # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the number of rows and columns of # the matrix. # # Output: # # real A(M,N), the matrix. # import numpy as np a = np.zeros ( [ m, n ] ) return a def zero_determinant ( n ): #*****************************************************************************80 # ## zero_determinant() returns the determinant of the ZERO matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real VALUE, the determinant. # value = 0.0 return value def zero_eigen_right ( n ): #*****************************************************************************80 # ## zero_eigen_right() returns the right eigenvectors of the ZERO matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real A(N,N), the matrix. # import numpy as np a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): a[i,i] = 1.0 return a def zero_eigenvalues ( n ): #*****************************************************************************80 # ## zero_eigenvalues() returns the eigenvalues of the ZERO matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Input: # # integer N, the order of the matrix. # # Output: # # real LAM(N), the eigenvalues. # import numpy as np lam = np.zeros ( n ) return lam def zero_null_left ( m, n ): #*****************************************************************************80 # ## zero_null_left() returns a left null vector of the ZERO matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # Output: # # real X(M), the left null vector. # import numpy as np x = np.ones ( m ) return x def zero_null_right ( m, n ): #*****************************************************************************80 # ## zero_null_right() returns a right null vector of the ZERO matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 March 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the order of the matrix. # # Output: # # real X(N), the right null vector. # import numpy as np x = np.ones ( n ) return x if ( __name__ == '__main__' ): timestamp ( ) test_matrix_test ( ) timestamp ( )