def triangle01_sample ( n, seed ): #*****************************************************************************80 # ## TRIANGLE01_SAMPLE samples the interior of the unit triangle in 2D. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 18 April 2015 # # Author: # # John Burkardt # # Reference: # # Reuven Rubinstein, # Monte Carlo Optimization, Simulation, and Sensitivity # of Queueing Networks, # Krieger, 1992, # ISBN: 0894647644, # LC: QA298.R79. # # Parameters: # # Input, integer N, the number of points. # # Input/output, integer SEED, a seed for the random # number generator. # # Output, real X[2,N], the points. # import numpy as np from r8vec_uniform_01 import r8vec_uniform_01 x = np.zeros ( ( 2, n ) ) m = 2 for j in range ( 0, n ): e, seed = r8vec_uniform_01 ( m + 1, seed ) for i in range ( 0, m + 1 ): e[i] = - np.log ( e[i] ) e_sum = 0.0 for i in range ( 0, m + 1 ): e_sum = e_sum + e[i] for i in range ( 0, m ): x[i,j] = e[i] / e_sum return x, seed
def triangle01_sample(n, seed): #*****************************************************************************80 # ## TRIANGLE01_SAMPLE samples the interior of the unit triangle in 2D. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 18 April 2015 # # Author: # # John Burkardt # # Reference: # # Reuven Rubinstein, # Monte Carlo Optimization, Simulation, and Sensitivity # of Queueing Networks, # Krieger, 1992, # ISBN: 0894647644, # LC: QA298.R79. # # Parameters: # # Input, integer N, the number of points. # # Input/output, integer SEED, a seed for the random # number generator. # # Output, real X[2,N], the points. # import numpy as np from r8vec_uniform_01 import r8vec_uniform_01 x = np.zeros((2, n)) m = 2 for j in range(0, n): e, seed = r8vec_uniform_01(m + 1, seed) for i in range(0, m + 1): e[i] = -np.log(e[i]) e_sum = 0.0 for i in range(0, m + 1): e_sum = e_sum + e[i] for i in range(0, m): x[i, j] = e[i] / e_sum return x, seed
def cauchy_determinant_test(): #*****************************************************************************80 # ## CAUCHY_DETERMINANT_TEST tests CAUCHY_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 January 2015 # # Author: # # John Burkardt # from r8mat_print import r8mat_print from r8vec_uniform_01 import r8vec_uniform_01 print '' print 'CAUCHY_DETERMINANT_TEST' print ' CAUCHY_DETERMINANT computes the CAUCHY determinant.' m = 4 n = 4 seed = 123456789 x, seed = r8vec_uniform_01(n, seed) y, seed = r8vec_uniform_01(n, seed) a = cauchy(n, x, y) r8mat_print(m, n, a, ' CAUCHY matrix:') value = cauchy_determinant(n, x, y) print '' print ' Value = %g' % (value) print '' print 'CAUCHY_DETERMINANT_TEST' print ' Normal end of execution.' return
def cauchy_determinant_test ( ): #*****************************************************************************80 # ## CAUCHY_DETERMINANT_TEST tests CAUCHY_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 January 2015 # # Author: # # John Burkardt # from r8mat_print import r8mat_print from r8vec_uniform_01 import r8vec_uniform_01 print '' print 'CAUCHY_DETERMINANT_TEST' print ' CAUCHY_DETERMINANT computes the CAUCHY determinant.' m = 4 n = 4 seed = 123456789 x, seed = r8vec_uniform_01 ( n, seed ) y, seed = r8vec_uniform_01 ( n, seed ) a = cauchy ( n, x, y ) r8mat_print ( m, n, a, ' CAUCHY matrix:' ) value = cauchy_determinant ( n, x, y ) print '' print ' Value = %g' % ( value ) print '' print 'CAUCHY_DETERMINANT_TEST' print ' Normal end of execution.' return
def cauchy_test(): #*****************************************************************************80 # ## CAUCHY_TEST tests CAUCHY. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 January 2015 # # Author: # # John Burkardt # from r8mat_print import r8mat_print from r8vec_uniform_01 import r8vec_uniform_01 print '' print 'CAUCHY_TEST' print ' CAUCHY computes the CAUCHY matrix.' m = 4 n = m seed = 123456789 x, seed = r8vec_uniform_01(n, seed) y, seed = r8vec_uniform_01(n, seed) a = cauchy(n, x, y) r8mat_print(n, n, a, ' CAUCHY matrix:') print '' print 'CAUCHY_TEST' print ' Normal end of execution.' return
def cauchy_test ( ): #*****************************************************************************80 # ## CAUCHY_TEST tests CAUCHY. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 January 2015 # # Author: # # John Burkardt # from r8mat_print import r8mat_print from r8vec_uniform_01 import r8vec_uniform_01 print '' print 'CAUCHY_TEST' print ' CAUCHY computes the CAUCHY matrix.' m = 4 n = m seed = 123456789 x, seed = r8vec_uniform_01 ( n, seed ) y, seed = r8vec_uniform_01 ( n, seed ) a = cauchy ( n, x, y ) r8mat_print ( n, n, a, ' CAUCHY matrix:' ) print '' print 'CAUCHY_TEST' print ' Normal end of execution.' return
def pds_random_determinant(n, key): #*****************************************************************************80 # ## PDS_RANDOM_DETERMINANT returns the determinant of the PDS_RANDOM matrix. # # Discussion: # # This routine will only work properly if the SAME value of SEED # is input that was input to PDS_RANDOM. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Input, integer KEY, a positive value that selects the data. # # Output, real VALUE, the determinant. # import numpy as np from r8vec_uniform_01 import r8vec_uniform_01 seed = key lam, seed = r8vec_uniform_01(n, seed) value = np.prod(lam) return value
def pds_random_determinant ( n, key ): #*****************************************************************************80 # ## PDS_RANDOM_DETERMINANT returns the determinant of the PDS_RANDOM matrix. # # Discussion: # # This routine will only work properly if the SAME value of SEED # is input that was input to PDS_RANDOM. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Input, integer KEY, a positive value that selects the data. # # Output, real VALUE, the determinant. # import numpy as np from r8vec_uniform_01 import r8vec_uniform_01 seed = key lam, seed = r8vec_uniform_01 ( n, seed ) value = np.prod ( lam ) return value
def r8vec_transpose_print_test(): #*****************************************************************************80 # ## R8VEC_TRANSPOSE_PRINT_TEST tests R8VEC_TRANSPOSE_PRINT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 March 2016 # # Author: # # John Burkardt # import platform from r8vec_uniform_01 import r8vec_uniform_01 n = 12 seed = 123456789 print('') print('R8VEC_TRANSPOSE_PRINT_TEST') print(' Python version: %s' % (platform.python_version())) print(' R8VEC_TRANSPOSE_PRINT prints an R8VEC "tranposed",') print(' that is, placing multiple entries on a line.') x, seed = r8vec_uniform_01(n, seed) r8vec_transpose_print(n, x, ' The vector X:') # # Terminate. # print('') print('R8VEC_TRANSPOSE_PRINT_TEST') print(' Normal end of execution.') return
def pds_random_eigen_right ( n, key ): #*****************************************************************************80 # ## PDS_RANDOM_EIGEN_RIGHT returns the right eigenvectors of the PDS_RANDOM matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Input, integer KEY, a positive value that selects the data. # # Output, real Q(N,N), the matrix. # from orth_random import orth_random from r8vec_uniform_01 import r8vec_uniform_01 # # Get a random set of eigenvalues. # seed = key lam, seed = r8vec_uniform_01 ( n, seed ) # # Get a random orthogonal matrix Q. # q = orth_random ( n, key ) return q
def pds_random_eigen_right(n, key): #*****************************************************************************80 # ## PDS_RANDOM_EIGEN_RIGHT returns the right eigenvectors of the PDS_RANDOM matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Input, integer KEY, a positive value that selects the data. # # Output, real Q(N,N), the matrix. # from orth_random import orth_random from r8vec_uniform_01 import r8vec_uniform_01 # # Get a random set of eigenvalues. # seed = key lam, seed = r8vec_uniform_01(n, seed) # # Get a random orthogonal matrix Q. # q = orth_random(n, key) return q
def pds_random_eigenvalues(n, key): #*****************************************************************************80 # ## PDS_RANDOM_EIGENVALUES returns the eigenvalues of the PDS_RANDOM matrix. # # Discussion: # # This routine will only work properly if the SAME value of SEED # is input that was input to PDS_RANDOM. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Input, integer KEY, a positive value that selects the data. # # Output, real LAM(N), the eigenvalues. # from r8vec_uniform_01 import r8vec_uniform_01 seed = key lam, seed = r8vec_uniform_01(n, seed) return lam
def pds_random_eigenvalues ( n, key ): #*****************************************************************************80 # ## PDS_RANDOM_EIGENVALUES returns the eigenvalues of the PDS_RANDOM matrix. # # Discussion: # # This routine will only work properly if the SAME value of SEED # is input that was input to PDS_RANDOM. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Input, integer KEY, a positive value that selects the data. # # Output, real LAM(N), the eigenvalues. # from r8vec_uniform_01 import r8vec_uniform_01 seed = key lam, seed = r8vec_uniform_01 ( n, seed ) return lam
def wathen_test01 ( ): #*****************************************************************************80 # ## WATHEN_TEST01 assembles, factor and solve using WATHEN_GE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 September 2014 # # Author: # # John Burkardt # import numpy as np import scipy.linalg as la from numpy.linalg import norm from r8vec_uniform_01 import r8vec_uniform_01 from wathen_ge import wathen_ge from wathen_order import wathen_order print '' print 'WATHEN_TEST01' print ' Assemble, factor and solve a Wathen system' print ' defined by WATHEN_GE.' print '' nx = 4 ny = 4 print ' Elements in X direction NX = %d' % ( nx ) print ' Elements in Y direction NY = %d' % ( ny ) print ' Number of elements = %d' % ( nx * ny ) # # Compute the number of unknowns. # n = wathen_order ( nx, ny ) print ' Number of nodes N = %d' % ( n ) # # Set up a random solution X1. # seed = 123456789 x1, seed = r8vec_uniform_01 ( n, seed ) # # Compute the matrix. # seed = 123456789 a, seed = wathen_ge ( nx, ny, n, seed ) # # Compute the corresponding right hand side B. # b = np.dot ( a, x1 ) # # Solve the linear system. # x2 = la.solve ( a, b ) # # Compute the norm of the solution error. # e = norm ( x1 - x2 ) print ' Norm of solution error is %g' % ( e ) return
def wathen_test08 ( ): #*****************************************************************************80 # ## WATHEN_TEST08 assemble, factor and solve using WATHEN_ST + CG_ST. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # import numpy as np from cg_st import cg_st from mv_st import mv_st from numpy.linalg import norm from r8vec_uniform_01 import r8vec_uniform_01 from wathen_order import wathen_order from wathen_st import wathen_st from wathen_st_size import wathen_st_size print '' print 'WATHEN_TEST08' print ' Assemble, factor and solve a Wathen system' print ' defined by WATHEN_ST and CG_ST.' print '' nx = 1 ny = 1 print ' Elements in X direction NX = %d' % ( nx ) print ' Elements in Y direction NY = %d' % ( ny ) print ' Number of elements = %d' % ( nx * ny ) # # Compute the number of unknowns. # n = wathen_order ( nx, ny ) print ' Number of nodes N = %d' % ( n ) # # Compute the matrix size. # nz_num = wathen_st_size ( nx, ny ) print ' Number of nonzeros = %d\n' % ( nz_num ) # # Set up a random solution X1. # seed = 123456789 x1, seed = r8vec_uniform_01 ( n, seed ) # # Compute the matrix. # seed = 123456789 row, col, a, seed = wathen_st ( nx, ny, nz_num, seed ) # # Compute the corresponding right hand side B. # b = mv_st ( n, n, nz_num, row, col, a, x1 ) # # Solve the linear system. # x2 = np.ones ( n ) x2 = cg_st ( n, nz_num, row, col, a, b, x2 ) # # Compute the solution error norm. # e = norm ( x1 - x2 ) print ' Maximum solution error is %g' % ( e ) return
def wathen_test06(): #*****************************************************************************80 # ## WATHEN_TEST06 assembles, factor and solves using WATHEN_GE + CG_GE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # import numpy as np from cg_ge import cg_ge from numpy.linalg import norm from r8vec_uniform_01 import r8vec_uniform_01 from wathen_ge import wathen_ge from wathen_order import wathen_order print '' print 'WATHEN_TEST06' print ' Assemble, factor and solve a Wathen system' print ' defined by WATHEN_GE and CG_GE.' print '' nx = 2 ny = 2 print ' Elements in X direction NX = %d' % (nx) print ' Elements in Y direction NY = %d' % (ny) print ' Number of elements = %d' % (nx * ny) # # Compute the number of unknowns. # n = wathen_order(nx, ny) print ' Number of nodes N = %d' % (n) # # Set up a random solution X1. # seed = 123456789 x1, seed = r8vec_uniform_01(n, seed) # # Compute the matrix. # seed = 123456789 a, seed = wathen_ge(nx, ny, n, seed) # # Compute the corresponding right hand side B. # b = np.dot(a, x1) # # Solve the linear system. # x2 = np.ones(n) x2 = cg_ge(n, a, b, x2) # # Compute the maximum solution error. # e = norm(x1 - x2) print ' Maximum solution error is %g' % (e) return
def pds_random(n, key): #*****************************************************************************80 # ## PDS_RANDOM returns a random positive definite symmetric 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 GNU LGPL license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Input, integer KEY, a positive value that selects the data. # # Output, real A(N,N), the matrix. # import numpy as np from orth_random import orth_random from r8vec_uniform_01 import r8vec_uniform_01 # # Get a random set of eigenvalues. # seed = key lam, seed = r8vec_uniform_01(n, seed) # # Get a random orthogonal matrix Q. # q = orth_random(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 wathen_test04(): #*****************************************************************************80 # ## WATHEN_TEST04 times WATHEN_CSC assembly and solution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 September 2014 # # Author: # # John Burkardt # import numpy as np import scipy.sparse.linalg as ssl import time from numpy.linalg import norm from r8vec_uniform_01 import r8vec_uniform_01 from wathen_csc import wathen_csc from wathen_order import wathen_order print '' print 'WATHEN_TEST04' print ' For various problem sizes,' print ' time the assembly and factorization of a Wathen system' print ' using the WATHEN_CSC function.' print '' print ' NX Elements Nodes Assembly ', print 'Factor Error' print '' nx = 1 ny = 1 for test in range(0, 7): # # Compute the number of unknowns. # n = wathen_order(nx, ny) # # Set up a random solution X1. # seed = 123456789 x1, seed = r8vec_uniform_01(n, seed) # # Compute the matrix. # seed = 123456789 t0 = time.clock() a, seed = wathen_csc(nx, ny, seed) t1 = (time.clock() - t0) # # Compute the corresponding right hand side B. # b = a.dot(x1) # # Solve the system. # t0 = time.clock() x2 = ssl.spsolve(a, b) t2 = (time.clock() - t0) # # Compute the norm of the solution error. # e = norm(x1 - x2) # # Report. # print ' %4d %4d %6d %10.2e %10.2e %10.2e' % \ ( nx, nx * ny, n, t1, t2, e ) # # Ready for next iteration. # nx = nx * 2 ny = ny * 2 return
def pds_random ( n, key ): #*****************************************************************************80 # ## PDS_RANDOM returns a random positive definite symmetric 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 GNU LGPL license. # # Modified: # # 20 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Input, integer KEY, a positive value that selects the data. # # Output, real A(N,N), the matrix. # import numpy as np from orth_random import orth_random from r8vec_uniform_01 import r8vec_uniform_01 # # Get a random set of eigenvalues. # seed = key lam, seed = r8vec_uniform_01 ( n, seed ) # # Get a random orthogonal matrix Q. # q = orth_random ( 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 wathen_test07 ( ): #*****************************************************************************80 # ## WATHEN_TEST06 assembles, factor and solves using WATHEN_CSC + CG_CSC. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 September 2014 # # Author: # # John Burkardt # import numpy as np from cg_csc import cg_csc from numpy.linalg import norm from r8vec_uniform_01 import r8vec_uniform_01 from wathen_csc import wathen_csc from wathen_order import wathen_order print '' print 'WATHEN_TEST07' print ' Assemble, factor and solve a Wathen system' print ' defined by WATHEN_CSC and CG_CSC.' print '' nx = 2 ny = 2 print ' Elements in X direction NX = %d' % ( nx ) print ' Elements in Y direction NY = %d' % ( ny ) print ' Number of elements = %d' % ( nx * ny ) # # Compute the number of unknowns. # n = wathen_order ( nx, ny ) print ' Number of nodes N = %d' % ( n ) # # Set up a random solution X1. # seed = 123456789 x1, seed = r8vec_uniform_01 ( n, seed ) # # Compute the matrix. # seed = 123456789 a, seed = wathen_csc ( nx, ny, seed ) # # Compute the corresponding right hand side B. # b = a.dot ( x1 ) # # Solve the linear system. # x2 = np.ones ( n ) x2 = cg_csc ( n, a, b, x2 ) # # Compute the maximum solution error. # e = norm ( x1 - x2 ) print ' Maximum solution error is %g' % ( e ) return
def wathen_test03 ( ): #*****************************************************************************80 # ## WATHEN_TEST03 times WATHEN_GE assembly and solution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 September 2014 # # Author: # # John Burkardt # import numpy as np import scipy.linalg as la import time from numpy.linalg import norm from r8vec_uniform_01 import r8vec_uniform_01 from wathen_ge import wathen_ge from wathen_order import wathen_order print '' print 'WATHEN_TEST03' print ' For various problem sizes,' print ' time the assembly and factorization of a Wathen system' print ' using the WATHEN_GE function.' print '' print ' NX Elements Nodes Storage Assembly ', print 'Factor Error' print '' nx = 1 ny = 1 for test in range ( 0, 6 ): # # Compute the number of unknowns. # n = wathen_order ( nx, ny ) storage_ge = n * n # # Set up a random solution X1. # seed = 123456789 x1, seed = r8vec_uniform_01 ( n, seed ) # # Compute the matrix. # seed = 123456789 t0 = time.clock ( ) a, seed = wathen_ge ( nx, ny, n, seed ) t1 = ( time.clock ( ) - t0 ) # # Compute the corresponding right hand side B. # b = np.dot ( a, x1 ) # # Solve the system. # t0 = time.clock ( ) x2 = la.solve ( a, b ) t2 = ( time.clock ( ) - t0 ) # # Compute the norm of the solution error. # e = norm ( x1 - x2 ) # # Report. # print ' %4d %4d %6d %8d %10.2e %10.2e %10.2e' % \ ( nx, nx * ny, n, storage_ge, t1, t2, e ) # # Ready for next iteration. # nx = nx * 2 ny = ny * 2 return