## /usr/bin/env python3 # def lapack_test ( ): #*****************************************************************************80 # ## lapack_test() tests lapack(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 September 2025 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'lapack_test():' ) print ( ' python version: ' + platform.python_version ( ) ) print ( ' numpy version: ' + np.version.version ) print ( ' Test lapack().' ) dgbtrf_test ( ) dgecon_test ( ) dgeev_test ( ) dgeqrf_test ( ) dgesvd_test ( ) dgetrf_test ( ) dgetri_test ( ) dgtsv_test ( ) dpotrf_test ( ) dpotri_test ( ) dsyev_test ( ) # # Terminate. # print ( '' ) print ( 'lapack_test():' ) print ( ' Normal end of execution.' ) return 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 dgbtrf_test ( ): #*****************************************************************************80 # ## dgbtrf_test() tests dgbtrf(). # # Discussion: # # The problem is just an enlarged version of the # problem for n = 5, which is: # # Matrix A is ( 2 -1 0 0 0) right hand side b is (1) # (-1 2 -1 0 0) (0) # ( 0 -1 2 -1 0) (0) # ( 0 0 -1 2 -1) (0) # ( 0 0 0 -1 2) (1) # # # Solution is (1) # (1) # (1) # (1) # (1) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 September 2006 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dgbtrf from scipy.linalg.lapack import dgbtrs print ( '' ) print ( 'dgbtrf_test():' ) print ( ' dgbtrf() factors a general band matrix.' ) print ( ' dgbtrs() solves a factored system.' ) print ( ' For a double precision real matrix (D)' ) print ( ' in general band storage mode (GB):' ) n = 5 kl = 1 ku = 1 # # Define the matrix. # AB = np.zeros ( [ 2 * kl + 1 + ku, n ] ) m = kl + ku AB[m-1,1:n] = -1.0 AB[m,0:n] = 2.0 AB[m+1,0:n-1] = -1.0 print ( '' ) print ( ' Band matrix has kl = ', kl, ' ku = ', ku ) # # Assign values to matrix A and right hand side b. # b = np.zeros ( n ) b[0] = 1.0 b[n-1] = 1.0 # # Factor the matrix. # LU, ipiv, info = dgbtrf ( AB, kl, ku ) if ( info != 0 ): print ( '' ) print ( ' dgbtrf failed.' ) print ( ' info = ', info ) return # # Solve the linear system. # x, info = dgbtrs ( LU, kl, ku, b, ipiv ) if ( info != 0 ): print ( '' ) print ( ' dgbtrs failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' Solution x (all should be 1)' ) print ( x ) return def dgecon_test ( ): #*****************************************************************************80 # ## dgecon_test() tests dgecon(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg import norm from scipy.linalg.lapack import dgetrf from scipy.linalg.lapack import dgecon print ( '' ) print ( 'dgecon_test():' ) print ( ' dgecon() computes the condition of a matrix that has been' ) print ( ' factored by dgetrf.' ) # # Set the matrix. # A = np.array ( [ \ [ 1.0, 2.0, 3.0 ], \ [ 4.0, 5.0, 6.0 ], \ [ 7.0, 8.0, 0.0 ] ] ) print ( '' ) print ( ' The matrix A:' ) print ( A ) # # Compute matrix 1-norm. # anorm = norm ( A, 1 ) print ( '' ) print ( ' Matrix 1-norm is ', anorm ) # # Factor the matrix. # LU, piv, info = dgetrf ( A ) if ( info != 0 ): print ( '' ) print ( ' dgetrf failed.' ) print ( ' info = ', info ) return # # Compute the reciprocal condition number. # rcond, info = dgecon ( LU, anorm ) if ( info != 0 ): print ( '' ) print ( ' dgecon failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' Reciprocal condition number for 1-norm is', rcond ) return def dgeev_test ( ): #*****************************************************************************80 # ## dgeev_test() tests dgeev(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dgeev print ( '' ) print ( 'dgeev_test():' ) print ( ' dgeev() computes the eigenvalues and eigenvectors' ) print ( ' of a matrix.' ) n = 5 A = clement1_matrix ( n ) print ( '' ) print ( ' The matrix A:' ) print ( A ) # # Get the eigen information # wr, wi, vr, vi, info = dgeev ( A ) if ( info != 0 ): print ( '' ) print ( ' dgeev failed.' ) print ( ' info = ', info ) return # # Display values. # print ( '' ) print ( ' Eigenvalues:' ) print ( np.column_stack ( ( wr, wi ) ) ) print ( '' ) print ( ' Eigenvectors (real parts):' ) print ( vr ) print ( '' ) print ( ' Eigenvectors (imaginary parts):' ) print ( vi ) return def dgeqrf_test ( ): #*****************************************************************************80 # ## dgeqrf_test() tests dgeqrf(). # # Discussion: # # dgeqrf() computes the QR factorization of an M by N matrix A: # # A(MxN) = Q(MxK) * R(KxN) # # where K = min ( M, N ). # # dorgqr() computes the explicit form of the Q factor. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dgeqrf from scipy.linalg.lapack import dorgqr print ( '' ) print ( 'dgeqrf_test()' ) print ( ' dgeqrf() computes the QR factorization:' ) print ( ' A = Q * R' ) print ( ' dorgqr() computes the explicit form of the Q factor.' ) print ( ' For a double precision real matrix (D)' ) print ( ' in general storage mode (GE):' ) # # Set the matrix. # m = 4 n = 3 A = np.array ( [ \ [ 1.0, 2.0, 3.0 ], \ [ 4.0, 5.0, 6.0 ], \ [ 7.0, 8.0, 0.0 ], \ [ 5.0, 13.0, 3.0 ] ] ) print ( '' ) print ( ' The matrix A:' ) print ( A ) # # Compute the QR factorization. # QR, tau, work, info = dgeqrf ( A ) if ( info != 0 ): print ( '' ) print ( ' dgeqrf failed.' ) print ( ' info = ', info ) return # # Construct R. # k = min ( m, n ) R = np.zeros ( [ k, n ] ) for i in range ( 0, k ): R[i,i:n] = QR[i,i:n] # # Construct Q. # Q, work, info = dorgqr ( QR, tau ) if ( info != 0 ): print ( '' ) print ( ' dorgqr failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' Q:' ) print ( Q ) print ( '' ) print ( ' R:' ) print ( R ) QR = np.matmul ( Q, R ) print ( '' ) print ( ' Q * R:' ) print ( QR ) return def dgesvd_test ( ): #*****************************************************************************80 # ## dgesvd_test() tests dgesvd(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dgesvd print ( '' ) print ( 'dgesvd_test():' ) print ( ' dgesvd() computes the singular value decomposition' ) print ( ' of a matrix.' ) # # Set the matrix. # A = np.array ( [ \ [ 1.0, 2.0, 3.0 ], \ [ 4.0, 5.0, 6.0 ], \ [ 7.0, 8.0, 0.0 ] ] ) print ( '' ) print ( ' The matrix A:' ) print ( A ) # # Compute the SVD. # U, S, VT, info = dgesvd ( A ) if ( info != 0 ): print ( '' ) print ( ' dgesvd failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' U:' ) print ( U ) print ( ' S:' ) print ( S ) print ( ' vt:' ) print ( VT ) # # Reconstruct A = U * S * V'. # SV = np.dot ( np.diag ( S ), VT ) USV = np.dot ( U, SV ) print ( '' ) print ( ' U*S*VT:' ) print ( USV ) return def dgetrf_test ( ): #*****************************************************************************80 # ## dgetrf_test() tests dgetrf(). # # Discussion: # # The problem is just an enlarged version of the # problem for n = 5, which is: # # Matrix A is ( N -1 -1 -1 -1) right hand side b is (1) # (-1 N -1 -1 -1) (1) # (-1 -1 N -1 -1) (1) # (-1 -1 -1 N -1) (1) # (-1 -1 -1 -1 N) (1) # # Solution is (1) # (1) # (1) # (1) # (1) # # For this problem, no pivoting is required. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dgetrf from scipy.linalg.lapack import dgetrs print ( '' ) print ( 'dgetrf_test():' ) print ( ' DGETRF factors a general matrix;' ) print ( ' DGETRS solves a linear system;' ) print ( ' For a double precision real matrix (D)' ) print ( ' in general storage mode (GE):' ) n = 5 # # Assign values to matrix A and right hand side b. # A = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( i == j ): A[i,j] = n else: A[i,j] = -1.0 print ( '' ) print ( ' The matrix A:' ) print ( A ) b = np.ones ( n ) print ( '' ) print ( ' The right hand side b:' ) print ( b ) # # Factor the matrix. # LU, piv, info = dgetrf ( A ) if ( info != 0 ): print ( '' ) print ( ' dgetrf failed.' ) print ( ' info = ', info ) return # # Solve the linear system. # x, info = dgetrs ( LU, piv, b ) if ( info != 0 ): print ( '' ) print ( ' dgetrs failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' The solution x (all entries should equal 1):' ) print ( x ) return def dgetri_test ( ): #*****************************************************************************80 # ## dgetri_test() tests dgetri(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dgetrf from scipy.linalg.lapack import dgetri print ( '' ) print ( 'dgetri_test():' ) print ( ' dgetri() computes the inverse of a matrix that has been' ) print ( ' factored by dgetrf.' ) # # Set the matrix. # A = np.array ( [ \ [ 1.0, 2.0, 3.0 ], \ [ 4.0, 5.0, 6.0 ], \ [ 7.0, 8.0, 0.0 ] ] ) print ( '' ) print ( ' The matrix A:' ) print ( A ) # # Factor the matrix. # LU, piv, info = dgetrf ( A ) if ( info != 0 ): print ( '' ) print ( ' dgetrf failed.' ) print ( ' info = ', info ) return # # Compute the inverse. # Ainv, info = dgetri ( LU, piv ) if ( info != 0 ): print ( '' ) print ( ' dgetri failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' Ainv:' ) print ( Ainv ) # # Compute I = A * Ainv # I = np.dot ( A, Ainv ) print ( '' ) print ( ' A * Ainv:' ) print ( I ) return def dgtsv_test ( ): #*****************************************************************************80 # ## dgtsv_test() tests dgtsv(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dgtsv print ( '' ) print ( 'dgtsv_test():' ) print ( ' dgtsv() factors and solves a linear system' ) print ( ' with a general tridiagonal matrix' ) print ( ' for a double precision real matrix (D)' ) print ( ' in general tridiagonal storage mode (GT).' ) # # Subdiagonal. # Diagonal. # Superdiagonal. # n = 5 c = -1.0 * np.ones ( n - 1 ) d = 2.0 * np.ones ( n ) e = -1.0 * np.ones ( n - 1 ) # # Right hand side. # b = np.zeros ( n ) b[n-1] = n + 1 # # Factor and solve the linear system. # du2, d, du, x, info = dgtsv ( c, d, e, b ) if ( info != 0 ): print ( '' ) print ( ' dgtsv failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' Solution x (should be 1, 2, 3, ... ):' ) print ( x ) return def dpotrf_test ( ): #*****************************************************************************80 # ## dpotrf_test() tests dpotrf(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 September 2006 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dpotrf print ( '' ) print ( 'dpotrf_test():' ) print ( ' dpotrf() computes the Cholesky factorization R\'*R' ) print ( ' for a double precision real matrix (D)' ) print ( ' in positive definite storage mode (PO).' ) n = 5 # # Define a square matrix. # A = np.zeros ( [ n, n ] ) for i in range ( 1, n ): A[i,i-1] = -1.0 for i in range ( 0, n ): A[i,i] = 2.0 for i in range ( 0, n - 1 ): A[i,i+1] = -1.0 # # Positive definite storage mode only saves upper half. # So make a copy with only that information. # Ahalf = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( i, n ): Ahalf[i,j] = A[i,j] print ( '' ) print ( ' The matrix A:' ) print ( A ) # # Factor the matrix. # R, info = dpotrf ( Ahalf ) if ( info != 0 ): print ( '' ) print ( ' dpotrf failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' Cholesky factor R:' ) print ( R ) RtR = np.matmul ( R.T, R ) print ( '' ) print ( ' R\' * R:' ) print ( RtR ) return def dpotri_test ( ): #*****************************************************************************80 # ## dpotri_test() tests dpotri(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dpotrf from scipy.linalg.lapack import dpotri print ( '' ) print ( 'dpotri_test():' ) print ( ' dpotri() computes the inverse' ) print ( ' for a double precision real matrix (D)' ) print ( ' in positive definite storage mode (PO).' ) n = 5 # # Define a square matrix. # A = np.zeros ( [ n, n ] ) for i in range ( 1, n ): A[i,i-1] = -1.0 for i in range ( 0, n ): A[i,i] = 2.0 for i in range ( 0, n - 1 ): A[i,i+1] = -1.0 # # Positive definite storage mode only saves upper half. # So make a copy with only that information. # Ahalf = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( i, n ): Ahalf[i,j] = A[i,j] print ( '' ) print ( ' The matrix A:' ) print ( A ) # # Factor the matrix. # C, info = dpotrf ( Ahalf ) if ( info != 0 ): print ( '' ) print ( ' dpotrf failed.' ) print ( ' info = ', info ) return # # Print the Cholesky factor. # print ( '' ) print ( ' The Cholesky factor C:' ) print ( C ) # # Compute the inverse matrix. # Ainvhalf, info = dpotri ( C ) if ( info != 0 ): print ( '' ) print ( ' dpotri failed.' ) print ( ' info = ', info ) return # # We only got "half" the answer. # Make a full matrix copy. # Ainv = Ainvhalf.copy ( ) for i in range ( 0, n ): for j in range ( 0, i ): Ainv[i,j] = Ainv[j,i] print ( '' ) print ( ' Inverse matrix Ainv:' ) print ( Ainv ) I = np.matmul ( A, Ainv ) print ( '' ) print ( ' Product I = A * Ainv:' ) print ( I ) return def dsyev_test ( ): #*****************************************************************************80 # ## dsyev_test() tests dsyev(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 September 2025 # # Author: # # John Burkardt # import numpy as np from scipy.linalg.lapack import dsyev print ( '' ) print ( 'dsyev_test()' ) print ( ' dsyev() computes eigenvalues and eigenvectors' ) print ( ' For a double precision real matrix (D)' ) print ( ' in symmetric storage mode (SY).' ) # # Set A. # n = 5 A = clement1_matrix ( n ) Ahalf = A.copy() for i in range ( 0, n ): for j in range ( 0, i ): Ahalf[i,j] = 0.0 print ( '' ) print ( ' (symmetric half) of A:' ) print ( Ahalf ) # # Compute the eigenvalues and eigenvectors. # w, V, info = dsyev ( Ahalf ) if ( info != 0 ): print ( '' ) print ( ' dsyev failed.' ) print ( ' info = ', info ) return print ( '' ) print ( ' Eigenvalues w:' ) print ( w ) print ( '' ) print ( ' Eigenvector matrix V:' ) print ( V ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) lapack_test ( ) timestamp ( )