#! /usr/bin/env python3 # def combin ( alpha, beta, n ): #*****************************************************************************80 # ## COMBIN 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_condition_test ( ): #*****************************************************************************80 # ## COMBIN_CONDITION_TEST tests COMBIN_CONDITION. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 December 2014 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'COMBIN_CONDITION_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' COMBIN_CONDITION computes the condition of the COMBIN matrix.' ) n = 3 alpha = np.random.rand ( ) alpha = round ( 50.0 * alpha ) / 5.0 beta = np.random.rand ( ) beta = round ( 50.0 * beta ) / 5.0 a = combin ( alpha, beta, n ) r8mat_print ( n, n, a, ' COMBIN matrix:' ) value = combin_condition ( alpha, beta, n ) print ( '' ) print ( ' Value = %g' % ( value ) ) # # Terminate. # print ( '' ) print ( 'COMBIN_CONDITION_TEST' ) print ( ' Normal end of execution.' ) return def combin_determinant ( alpha, beta, n ): #*****************************************************************************80 # ## COMBIN_DETERMINANT computes 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_determinant_test ( ): #*****************************************************************************80 # ## COMBIN_DETERMINANT_TEST tests COMBIN_DETERMINANT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 December 2014 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'COMBIN_DETERMINANT_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' COMBIN_DETERMINANT computes the COMBIN determinant.' ) m = 4 n = 4 alpha = np.random.rand ( ) alpha = round ( 50.0 * alpha ) / 5.0 beta = np.random.rand ( ) beta = round ( 50.0 * beta ) / 5.0 a = combin ( alpha, beta, n ) r8mat_print ( m, n, a, ' COMBIN matrix:' ) value = combin_determinant ( alpha, beta, n ) print ( ' Value = %g' % ( value ) ) # # Terminate. # print ( '' ) print ( 'COMBIN_DETERMINANT_TEST' ) print ( ' Normal end of execution.' ) return 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 combin_test ( ): #*****************************************************************************80 # ## COMBIN_TEST tests COMBIN. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 December 2014 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'COMBIN_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' COMBIN computes the COMBIN matrix.' ) n = 4 alpha = np.random.rand ( ) alpha = np.round ( 50.0 * alpha ) / 5.0 beta = np.random.rand ( ) beta = np.round ( 50.0 * beta ) / 5.0 a = combin ( alpha, beta, n ) r8mat_print ( n, n, a, ' COMBIN matrix:' ) # # Terminate. # print ( '' ) print ( 'COMBIN_TEST' ) print ( ' Normal end of execution.' ) return def condition_hager ( n, a ): #*****************************************************************************80 # ## CONDITION_HAGER estimates the L1 condition number of a matrix. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # # Reference: # # William Hager, # Condition Estimates, # SIAM Journal on Scientific and Statistical Computing, # Volume 5, Number 2, June 1984, pages 311-316. # # Input: # # integer N, the dimension of the matrix. # # real A(N,N), the matrix. # # Output: # # real VALUE, an estimate of the L1 condition number. # import numpy as np i1 = -1 c1 = 0.0 # # Factor the matrix. # b = np.zeros ( n ) for i in range ( 0, n ): b[i] = 1.0 / float ( n ) while ( True ): b2 = np.linalg.solve ( a, b ) for i in range ( 0, n ): b[i] = b2[i] c2 = 0.0 for i in range ( 0, n ): c2 = c2 + abs ( b[i] ) for i in range ( 0, n ): b[i] = r8_sign ( b[i] ) b2 = np.linalg.solve ( np.transpose ( a ), b ) for i in range ( 0, n ): b[i] = b2[i] i2 = r8vec_max_abs_index ( n, b ) if ( 0 <= i1 ): if ( i1 == i2 or c2 <= c1 ): break i1 = i2 c1 = c2 for i in range ( 0, n ): b[i] = 0.0 b[i1] = 1.0 value = c2 * r8mat_norm_l1 ( n, n, a ) return value def condition_hager_test ( ): #*****************************************************************************80 # ## CONDITION_HAGER_TEST tests CONDITION_HAGER. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'CONDITION_HAGER_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONDITION_HAGER estimates the L1 condition number' ) print ( ' for a matrix in general storage.' ) print ( '' ) print ( ' Matrix Order Condition Hager' ) print ( '' ) # # Combinatorial matrix. # name = 'Combinatorial' n = 4 alpha = 2.0 beta = 3.0 a = combin ( alpha, beta, n ) a_inverse = combin_inverse ( alpha, beta, n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_hager ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # CONEX1 # name = 'CONEX1' n = 4 alpha = 100.0 a = conex1 ( alpha ) a_inverse = conex1_inverse ( alpha ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_hager ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # CONEX2 # name = 'CONEX2' n = 3 alpha = 100.0 a = conex2 ( alpha ) a_inverse = conex2_inverse ( alpha ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_hager ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # CONEX3 # name = 'CONEX3' n = 5 a = conex3 ( n ) a_inverse = conex3_inverse ( n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_hager ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # CONEX4 # name = 'CONEX4' n = 4 a = conex4 ( ) a_inverse = conex4_inverse ( ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_hager ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # KAHAN # name = 'KAHAN' n = 4 alpha = 0.25 a = kahan ( alpha, n, n ) a_inverse = kahan_inverse ( alpha, n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_hager ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # Random # for j in range ( 0, 5 ): name = 'RANDOM' m = 4 n = 4 a = np.random.rand ( m, n ) a_inverse = np.linalg.inv ( a ) a_norm_l1 = r8mat_norm_l1 ( m, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( m, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_hager ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # Terminate. # print ( '' ) print ( 'CONDITION_HAGER_TEST' ) print ( ' Normal end of execution.' ) return def condition_linpack ( n, a ): #*****************************************************************************80 # ## CONDITION_LINPACK estimates the L1 condition number of a matrix. # # Discussion: # # The R8GE storage format is used for a general M by N matrix. A storage # space is made for each logical entry. The two dimensional logical # array is mapped to a vector, in which storage is by columns. # # For the system A * X = B, relative perturbations in A and B # of size EPSILON may cause relative perturbations in X of size # EPSILON*RCOND. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 July 2015 # # Author: # # Original FORTRAN77 version by Dongarra, Bunch, Moler, Stewart. # Python version by John Burkardt. # # Reference: # # Dongarra, Bunch, Moler, Stewart, # LINPACK User's Guide, # SIAM, 1979 # # Input: # # integer N, the order of the matrix A. # # real A(N,N), a matrix to be factored. # # Output: # # real VALUE, an estimate of the condition number of A. # import numpy as np # # Compute the L1 norm of A. # anorm = r8mat_norm_l1 ( n, n, a ) # # Compute the LU factorization. # a_lu, pivot, info = r8ge_fa ( n, a ) # # COND = norm(A) * (estimate of norm(inverse(A))) # # estimate of norm(inverse(A)) = norm(Z) / norm(Y) # # where # A * Z = Y # and # A' * Y = E # # The components of E are chosen to cause maximum local growth in the # elements of W, where U'*W = E. The vectors are frequently rescaled # to avoid overflow. # # Solve U' * W = E. # ek = 1.0 z = np.zeros ( n ) for k in range ( 0, n ): if ( z[k] != 0.0 ): ek = - r8_sign ( z[k] ) * abs ( ek ) if ( abs ( a_lu[k,k] ) < abs ( ek - z[k] ) ): s = abs ( a_lu[k,k] ) / abs ( ek - z[k] ) for i in range ( 0, n ): z[i] = s * z[i] ek = s * ek wk = ek - z[k] wkm = - ek - z[k] s = abs ( wk ) sm = abs ( wkm ) if ( a_lu[k,k] != 0.0 ): wk = wk / a_lu[k,k] wkm = wkm / a_lu[k,k] else: wk = 1.0 wkm = 1.0 if ( k + 1 <= n - 1 ): for j in range ( k + 1, n ): sm = sm + abs ( z[j] + wkm * a_lu[k,j] ) z[j] = z[j] + wk * a_lu[k,j] s = s + abs ( z[j] ) if ( s < sm ): t = wkm - wk wk = wkm for j in range ( k + 1, n ): z[j] = z[j] + t * a_lu[k,j] z[k] = wk t = 0.0 for i in range ( 0, n ): t = t + abs ( z[i] ) for i in range ( 0, n ): z[i] = z[i] / t # # Solve L' * Y = W # for k in range ( n - 1, -1, -1 ): for i in range ( k + 1, n ): z[k] = z[k] + z[i] * a_lu[i,k] t = abs ( z[k] ) if ( 1.0 < t ): for i in range ( 0, n ): z[i] = z[i] / t l = pivot[k] t = z[l] z[l] = z[k] z[k] = t t = 0.0 for i in range ( 0, n ): t = t + abs ( z[i] ) for i in range ( 0, n ): z[i] = z[i] / t ynorm = 1.0 # # Solve L * V = Y. # for k in range ( 0, n ): l = pivot[k] t = z[l] z[l] = z[k] z[k] = t for i in range ( k + 1, n ): z[i] = z[i] + t * a_lu[i,k] if ( 1.0 < abs ( z[k] ) ): t = abs ( z[k] ) ynorm = ynorm / t for i in range ( 0, n ): z[i] = z[i] / t t = 0.0 for i in range ( 0, n ): t = t + abs ( z[i] ) for i in range ( 0, n ): z[i] = z[i] / t ynorm = ynorm / t # # Solve U * Z = V. # for k in range ( n - 1, -1, -1 ): if ( abs ( a_lu[k,k] ) < abs ( z[k] ) ): s = abs ( a_lu[k,k] ) / abs ( z[k] ) for i in range ( 0, n ): z[i] = s * z[i] ynorm = s * ynorm if ( a_lu[k,k] != 0.0 ): z[k] = z[k] / a_lu[k,k] else: z[k] = 1.0 for i in range ( 0, k ): z[i] = z[i] - a_lu[i,k] * z[k] # # Normalize Z in the L1 norm. # t = 0.0 for i in range ( 0, n ): t = t + abs ( z[i] ) for i in range ( 0, n ): z[i] = z[i] / t ynorm = ynorm / t value = anorm / ynorm return value def condition_linpack_test ( ): #*****************************************************************************80 # ## CONDITION_LINPACK_TEST tests CONDITION_LINPACK. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'CONDITION_LINPACK_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONDITION_LINPACK estimates the L1 condition number' ) print ( ' for a matrix in general storage.' ) print ( '' ) print ( ' Matrix Order Condition Linpack' ) print ( '' ) # # Combinatorial matrix. # name = 'Combinatorial' n = 4 alpha = 2.0 beta = 3.0 a = combin ( alpha, beta, n ) a_inverse = combin_inverse ( alpha, beta, n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_linpack ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # CONEX1 # name = 'CONEX1' n = 4 alpha = 100.0 a = conex1 ( alpha ) a_inverse = conex1_inverse ( alpha ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_linpack ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # CONEX2 # name = 'CONEX2' n = 3 alpha = 100.0 a = conex2 ( alpha ) a_inverse = conex2_inverse ( alpha ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_linpack ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # CONEX3 # name = 'CONEX3' n = 5 a = conex3 ( n ) a_inverse = conex3_inverse ( n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_linpack ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # CONEX4 # name = 'CONEX4' n = 4 a = conex4 ( ) a_inverse = conex4_inverse ( ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_linpack ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # KAHAN # name = 'KAHAN' n = 4 alpha = 0.25 a = kahan ( alpha, n, n ) a_inverse = kahan_inverse ( alpha, n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_linpack ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # Random # for j in range ( 0, 5 ): name = 'RANDOM' m = 4 n = 4 a = np.random.rand ( m, n ) a_inverse = np.linalg.inv ( a ) a_norm_l1 = r8mat_norm_l1 ( m, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( m, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond = condition_linpack ( n, a ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond ) ) # # Terminate. # print ( '' ) print ( 'CONDITION_LINPACK_TEST' ) print ( ' Normal end of execution.' ) return def condition_sample1 ( n, a, m ): #*****************************************************************************80 # ## CONDITION_SAMPLE1 estimates the L1 condition number of a matrix. # # Discussion: # # A naive sampling method is used. # # Only "forward" sampling is used, that is, we only look at results # of the form y=A*x. # # Presumably, solving systems A*y=x would give us a better idea of # the inverse matrix. # # Moreover, a power sequence y1 = A*x, y2 = A*y1, ... and the same for # the inverse might work better too. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 October 2012 # # Author: # # John Burkardt # # Input: # # integer N, the dimension of the matrix. # # real A(N,N), the matrix. # # integer M, the number of samples to use. # # Output: # # real VALUE, an estimate of the L1 condition number. # import numpy as np a_norm = 0.0 ainv_norm = 0.0 for i in range ( 0, m ): x = r8vec_uniform_unit ( n ) x_norm = r8vec_norm_l1 ( n, x ) ax = np.dot ( a, x ) ax_norm = r8vec_norm_l1 ( n, ax ) if ( ax_norm == 0.0 ): value = 0.0 return value a_norm = max ( a_norm, ax_norm / x_norm ) ainv_norm = max ( ainv_norm, x_norm / ax_norm ) value = a_norm * ainv_norm return value def condition_sample1_test ( ): #*****************************************************************************80 # ## CONDITION_SAMPLE1_TEST tests CONDITION_SAMPLE1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # import numpy as np import platform m_test = np.array ( [ 10, 1000, 100000 ] ) print ( '' ) print ( 'CONDITION_SAMPLE1_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONDITION_SAMPLE1 estimates the L1 condition number using sampling' ) print ( ' for a matrix in general storage.' ) print ( '' ) print ( ' Matrix Samples Order Condition Estimate' ) # # Combinatorial matrix. # name = 'Combinatorial' n = 4 alpha = 2.0 beta = 3.0 a = combin ( alpha, beta, n ) a_inverse = combin_inverse ( alpha, beta, n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 print ( '' ) for i in range ( 0, 3 ): m = m_test[i] cond = condition_sample1 ( n, a, m ) print ( ' %20s %8d %4d %14.6g %14.6g' % ( name, m, n, cond_l1, cond ) ) # # CONEX1 # name = 'CONEX1' n = 4 alpha = 100.0 a = conex1 ( alpha ) a_inverse = conex1_inverse ( alpha ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 print ( '' ) for i in range ( 0, 3 ): m = m_test[i] cond = condition_sample1 ( n, a, m ) print ( ' %20s %8d %4d %14.6g %14.6g' % ( name, m, n, cond_l1, cond ) ) # # CONEX2 # name = 'CONEX2' n = 3 alpha = 100.0 a = conex2 ( alpha ) a_inverse = conex2_inverse ( alpha ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 print ( '' ) for i in range ( 0, 3 ): m = m_test[i] cond = condition_sample1 ( n, a, m ) print ( ' %20s %8d %4d %14.6g %14.6g' % ( name, m, n, cond_l1, cond ) ) # # CONEX3 # name = 'CONEX3' n = 5 a = conex3 ( n ) a_inverse = conex3_inverse ( n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 print ( '' ) for i in range ( 0, 3 ): m = m_test[i] cond = condition_sample1 ( n, a, m ) print ( ' %20s %8d %4d %14.6g %14.6g' % ( name, m, n, cond_l1, cond ) ) # # CONEX4 # name = 'CONEX4' n = 4 a = conex4 ( ) a_inverse = conex4_inverse ( ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 print ( '' ) for i in range ( 0, 3 ): m = m_test[i] cond = condition_sample1 ( n, a, m ) print ( ' %20s %8d %4d %14.6g %14.6g' % ( name, m, n, cond_l1, cond ) ) # # KAHAN # name = 'KAHAN' n = 4 alpha = 0.25 a = kahan ( alpha, n, n ) a_inverse = kahan_inverse ( alpha, n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 print ( '' ) for i in range ( 0, 3 ): m = m_test[i] cond = condition_sample1 ( n, a, m ) print ( ' %20s %8d %4d %14.6g %14.6g' % ( name, m, n, cond_l1, cond ) ) # # Random # for j in range ( 0, 5 ): name = 'RANDOM' m = 4 n = 4 a = np.random.rand ( m, n ) a_inverse = np.linalg.inv ( a ) a_norm_l1 = r8mat_norm_l1 ( m, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( m, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 print ( '' ) for i in range ( 0, 3 ): m = m_test[i] cond = condition_sample1 ( n, a, m ) print ( ' %20s %8d %4d %14.6g %14.6g' % ( name, m, n, cond_l1, cond ) ) # # Terminate. # print ( '' ) print ( 'CONDITION_SAMPLE1_TEST' ) print ( ' Normal end of execution.' ) return def condition_test ( ): #*****************************************************************************80 # ## condition_test tests condition(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'CONDITION_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test condition()' ) # # Utilities: # combin_test ( ) conex1_test ( ) conex2_test ( ) conex3_test ( ) conex4_test ( ) i4vec_print_test ( ) kahan_test ( ) r8_sign_test ( ) r8ge_fa_test01 ( ) r8ge_fa_test02 ( ) r8mat_norm_l1_test ( ) r8mat_print_test ( ) r8mat_print_some_test ( ) r8vec_max_abs_index_test ( ) r8vec_norm_l1_test ( ) r8vec_print_test ( ) r8vec_uniform_unit_test ( ) # # Library. # cond_test ( ) condition_hager_test ( ) condition_linpack_test ( ) condition_sample1_test ( ) # # Terminate. # print ( '' ) print ( 'condition_test:' ) print ( ' Normal end of execution.' ) return def cond_test ( ): #*****************************************************************************80 # ## COND_TEST tests COND. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 July 2015 # # Author: # # John Burkardt # import numpy as np import platform m_test = np.array ( [ 10, 1000, 100000 ] ) print ( '' ) print ( 'COND_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' COND is the condition number estimator built into Python.' ) print ( '' ) print ( ' Matrix Order Condition Estimate' ) print ( '' ) # # Combinatorial matrix. # name = 'Combinatorial' n = 4 alpha = 2.0 beta = 3.0 a = combin ( alpha, beta, n ) a_inverse = combin_inverse ( alpha, beta, n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond2 = np.linalg.cond ( a, 1 ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond2 ) ) # # CONEX1 # name = 'CONEX1' n = 4 alpha = 100.0 a = conex1 ( alpha ) a_inverse = conex1_inverse ( alpha ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond2 = np.linalg.cond ( a, 1 ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond2 ) ) # # CONEX2 # name = 'CONEX2' n = 3 alpha = 100.0 a = conex2 ( alpha ) a_inverse = conex2_inverse ( alpha ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond2 = np.linalg.cond ( a, 1 ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond2 ) ) # # CONEX3 # name = 'CONEX3' n = 5 a = conex3 ( n ) a_inverse = conex3_inverse ( n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond2 = np.linalg.cond ( a, 1 ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond2 ) ) # # CONEX4 # name = 'CONEX4' n = 4 a = conex4 ( ) a_inverse = conex4_inverse ( ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond2 = np.linalg.cond ( a, 1 ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond2 ) ) # # KAHAN # name = 'KAHAN' n = 4 alpha = 0.25 a = kahan ( alpha, n, n ) a_inverse = kahan_inverse ( alpha, n ) a_norm_l1 = r8mat_norm_l1 ( n, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( n, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond2 = np.linalg.cond ( a, 1 ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond2 ) ) # # Random # for j in range ( 0, 5 ): name = 'RANDOM' m = 4 n = 4 a = np.random.rand ( m, n ) a_inverse = np.linalg.inv ( a ) a_norm_l1 = r8mat_norm_l1 ( m, n, a ) a_inverse_norm_l1 = r8mat_norm_l1 ( m, n, a_inverse ) cond_l1 = a_norm_l1 * a_inverse_norm_l1 cond2 = np.linalg.cond ( a, 1 ) print ( ' %20s %4d %14.6g %14.6g' % ( name, n, cond_l1, cond2 ) ) # # Terminate. # print ( '' ) print ( 'COND_TEST' ) print ( ' Normal end of execution.' ) return def conex1 ( alpha ): #*****************************************************************************80 # ## CONEX1 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_condition_test ( ): #*****************************************************************************80 # ## CONEX1_CONDITION_TEST tests CONEX1_CONDITION. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 February 2015 # # Author: # # John Burkardt # import platform from conex1 import conex1 from r8mat_print import r8mat_print print ( '' ) print ( 'CONEX1_CONDITION_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONEX1_CONDITION computes the condition of the CONEX1 matrix.' ) m = 4 n = m r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * np.random.rand ( ) a = conex1 ( alpha ) r8mat_print ( m, n, a, ' CONEX1 matrix:' ) value = conex1_condition ( alpha ) print ( '' ) print ( ' Value = %g' % ( value ) ) # # Terminate. # print ( '' ) print ( 'CONEX1_CONDITION_TEST' ) print ( ' Normal end of execution.' ) return 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_determinant_test ( ): #*****************************************************************************80 # ## CONEX1_DETERMINANT_TEST tests CONEX1_DETERMINANT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'CONEX1_DETERMINANT_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONEX1_DETERMINANT computes the determinant of the CONEX1 matrix.' ) m = 4 n = m alpha_lo = 1.0 alpha_hi = 100.0 alpha = alpha_lo + ( alpha_hi - alpha_lo ) * np.random.rand ( ) a = conex1 ( alpha ) r8mat_print ( m, n, a, ' CONEX1 matrix:' ) value = conex1_determinant ( alpha ) print ( '' ) print ( ' Value = %g' % ( value ) ) # # Terminate. # print ( '' ) print ( 'CONEX1_DETERMINANT_TEST' ) print ( ' Normal end of execution.' ) return 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 conex1_test ( ): #*****************************************************************************80 # ## CONEX1_TEST tests CONEX1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'CONEX1_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONEX1 computes the CONEX1 matrix.' ) m = 4 n = m alpha_lo = 1.0 alpha_hi = 100.0 alpha = alpha_lo + ( alpha_hi - alpha_lo ) * np.random.rand ( ) a = conex1 ( alpha ) r8mat_print ( m, n, a, ' CONEX1 matrix:' ) # # Terminate. # print ( '' ) print ( 'CONEX1_TEST' ) print ( ' Normal end of execution.' ) return def conex2 ( alpha ): #*****************************************************************************80 # ## CONEX2 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 atrix. # 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_condition_test ( ): #*****************************************************************************80 # ## CONEX2_CONDITION_TEST tests CONEX2_CONDITION. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 February 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'CONEX2_CONDITION_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONEX2_CONDITION computes the condition of the CONEX2 matrix.' ) print ( '' ) m = 3 n = m r8_lo = -5.0 r8_hi = +5.0 alpha = r8_lo + ( r8_hi - r8_lo ) * np.random.rand ( ) a = conex2 ( alpha ) r8mat_print ( m, n, a, ' CONEX2 matrix:' ) value = conex2_condition ( alpha ) print ( '' ) print ( ' Value = %g' % ( value ) ) # # Terminate. # print ( '' ) print ( 'CONEX2_CONDITION_TEST' ) print ( ' Normal end of execution.' ) return 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_determinant_test ( ): #*****************************************************************************80 # ## CONEX2_DETERMINANT_TEST tests CONEX2_DETERMINANT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'CONEX2_DETERMINANT_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONEX2_DETERMINANT computes the determinant of the CONEX2 matrix.' ) print ( '' ) m = 3 n = m alpha_lo = 1.0 alpha_hi = 100.0 alpha = alpha_lo + ( alpha_hi - alpha_lo ) * np.random.rand ( ) a = conex2 ( alpha ) r8mat_print ( m, n, a, ' CONEX2 matrix:' ) value = conex2_determinant ( alpha ) print ( '' ) print ( ' Value = %g' % ( value ) ) # # Terminate. # print ( '' ) print ( 'CONEX2_DETERMINANT_TEST' ) print ( ' Normal end of execution.' ) return 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 conex2_test ( ): #*****************************************************************************80 # ## CONEX2_TEST tests CONEX2. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 January 2015 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'CONEX2_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CONEX2 computes the CONEX2 matrix.' ) m = 3 n = m alpha_lo = 1.0 alpha_hi = 100.0 alpha = alpha_lo + ( alpha_hi - alpha_lo ) * np.random.rand ( ) a = conex2 ( alpha ) r8mat_print ( m, n, a, ' CONEX2 matrix:' ) # # Terminate. # print ( '' ) print ( 'CONEX2_TEST' ) print ( ' Normal end of execution.' ) return def conex3 ( n ): #*****************************************************************************80 # ## CONEX3 returns the CONEX3 matrix. # # Formula: # # if ( I = J and I < N ) # A(I,J) = 1.0 for 1<=I