def i4_normal_ab(mu, sigma, seed): #*****************************************************************************80 # ## I4_NORMAL_AB returns a scaled pseudonormal I4. # # Discussion: # # The normal probability distribution function (PDF) is sampled, # with mean MU and standard deviation SIGMA. # # The result is rounded to the nearest integer. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, the mean of the PDF. # # Input, real SIGMA, the standard deviation of the PDF. # # Input, integer SEED, a seed for the random number generator. # # Output, integer VALUE, a normally distributed # random value. # # Output, integer SEED, an updated seed for the random # number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 r1, seed = r8_uniform_01(seed) r2, seed = r8_uniform_01(seed) value = np.sqrt(-2.0 * np.log(r1)) * np.cos(2.0 * np.pi * r2) value = int(mu + sigma * value) return value, seed
def i4_normal_ab ( mu, sigma, seed ): #*****************************************************************************80 # ## I4_NORMAL_AB returns a scaled pseudonormal I4. # # Discussion: # # The normal probability distribution function (PDF) is sampled, # with mean MU and standard deviation SIGMA. # # The result is rounded to the nearest integer. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, the mean of the PDF. # # Input, real SIGMA, the standard deviation of the PDF. # # Input, integer SEED, a seed for the random number generator. # # Output, integer VALUE, a normally distributed # random value. # # Output, integer SEED, an updated seed for the random # number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 r1, seed = r8_uniform_01 ( seed ) r2, seed = r8_uniform_01 ( seed ) value = np.sqrt ( - 2.0 * np.log ( r1 ) ) * np.cos ( 2.0 * np.pi * r2 ) value = int ( mu + sigma * value ) return value, seed
def combin_condition_test ( ): #*****************************************************************************80 # ## COMBIN_CONDITION_TEST tests COMBIN_CONDITION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'COMBIN_CONDITION_TEST' print ' COMBIN_CONDITION computes the condition of the COMBIN matrix.' print '' seed = 123456789 n = 3 seed = 123456789 alpha, seed = r8_uniform_01 ( seed ) alpha = round ( 50.0 * alpha ) / 5.0 beta, seed = r8_uniform_01 ( seed ) 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 ) print '' print 'COMBIN_CONDITION_TEST' print ' Normal end of execution.' return
def combin_condition_test(): #*****************************************************************************80 # ## COMBIN_CONDITION_TEST tests COMBIN_CONDITION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'COMBIN_CONDITION_TEST' print ' COMBIN_CONDITION computes the condition of the COMBIN matrix.' print '' seed = 123456789 n = 3 seed = 123456789 alpha, seed = r8_uniform_01(seed) alpha = round(50.0 * alpha) / 5.0 beta, seed = r8_uniform_01(seed) 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) print '' print 'COMBIN_CONDITION_TEST' print ' Normal end of execution.' return
def normal_01_sample ( seed ): #*****************************************************************************80 # ## NORMAL_01_SAMPLE samples the standard normal probability distribution. # # Discussion: # # The standard normal probability distribution function (PDF) has # mean 0 and standard deviation 1. # # Method: # # The Box-Muller method is used, which is efficient, but # generates two values at a time. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer SEED, a seed for the random number generator. # # Output, real VALUE, a sample of the standard normal PDF. # # Output, integer SEED, an updated seed for the random number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 r1, seed = r8_uniform_01 ( seed ) r2, seed = r8_uniform_01 ( seed ) value = np.sqrt ( - 2.0 * np.log ( r1 ) ) * np.cos ( 2.0 * np.pi * r2 ) return value, seed
def polar_to_c8_test(): #*****************************************************************************80 # ## POLAR_TO_C8_TEST tests POLAR_TO_C8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # import numpy as np from r8_uniform_01 import r8_uniform_01 from c8_to_polar import c8_to_polar print '' print 'POLAR_TO_C8_TEST' print ' POLAR_TO_C8 computes the polar form of a C8.' print '' print ' R1,T1=R8_UNIFORM_01 C2=POLAR_TO_C8(R1,T1) R3,T3=C8_TO_POLAR(C2)' print ' --------------------- --------------------- ---------------------' print '' seed = 123456789 for i in range(0, 10): r1, seed = r8_uniform_01(seed) t1, seed = r8_uniform_01(seed) t1 = t1 * 2.0 * np.pi c2 = polar_to_c8(r1, t1) r3, t3 = c8_to_polar(c2) print ' (%12f,%12f) (%12f,%12f) (%12f,%12f)' % (r1, t1, c2.real, c2.imag, r3, t3) print '' print 'POLAR_TO_C8_TEST:' print ' Normal end of execution.' return
def r8_normal_01 ( seed ): #*****************************************************************************80 # ## R8_NORMAL_01 samples the standard normal probability distribution. # # Discussion: # # The standard normal probability distribution function (PDF) has # mean 0 and standard deviation 1. # # The Box-Muller method is used. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 25 July 2014 # # Author: # # John Burkardt # # Parameters: # # Input, integer SEED, a seed for the random number generator. # # Output, real X, a sample of the standard normal PDF. # # Output, integer SEED, an updated seed for the random number generator. # from math import cos from math import log from math import sqrt from r8_uniform_01 import r8_uniform_01 r8_pi = 3.141592653589793 r1, seed = r8_uniform_01 ( seed ) r2, seed = r8_uniform_01 ( seed ) x = sqrt ( - 2.0 * log ( r1 ) ) * cos ( 2.0 * r8_pi * r2 ) return x, seed
def combin_determinant_test(): #*****************************************************************************80 # ## COMBIN_DETERMINANT_TEST tests COMBIN_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'COMBIN_DETERMINANT_TEST' print ' COMBIN_DETERMINANT computes the COMBIN determinant.' m = 4 n = 4 seed = 123456789 alpha, seed = r8_uniform_01(seed) alpha = round(50.0 * alpha) / 5.0 beta, seed = r8_uniform_01(seed) 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) print '' print 'COMBIN_DETERMINANT_TEST' print ' Normal end of execution.' return
def polar_to_c8_test ( ): #*****************************************************************************80 # ## POLAR_TO_C8_TEST tests POLAR_TO_C8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 February 2015 # # Author: # # John Burkardt # import numpy as np from r8_uniform_01 import r8_uniform_01 from c8_to_polar import c8_to_polar print '' print 'POLAR_TO_C8_TEST' print ' POLAR_TO_C8 computes the polar form of a C8.' print '' print ' R1,T1=R8_UNIFORM_01 C2=POLAR_TO_C8(R1,T1) R3,T3=C8_TO_POLAR(C2)' print ' --------------------- --------------------- ---------------------' print '' seed = 123456789 for i in range ( 0, 10 ): r1, seed = r8_uniform_01 ( seed ) t1, seed = r8_uniform_01 ( seed ) t1 = t1 * 2.0 * np.pi c2 = polar_to_c8 ( r1, t1 ) r3, t3 = c8_to_polar ( c2 ) print ' (%12f,%12f) (%12f,%12f) (%12f,%12f)' % ( r1, t1, c2.real, c2.imag, r3, t3 ) print '' print 'POLAR_TO_C8_TEST:' print ' Normal end of execution.' return
def cartesian_to_c8_test(): #*****************************************************************************80 # ## CARTESIAN_TO_C8_TEST tests CARTESIAN_TO_C8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 February 2015 # # Author: # # John Burkardt # import numpy as np from r8_uniform_01 import r8_uniform_01 from c8_to_cartesian import c8_to_cartesian print '' print 'CARTESIAN_TO_C8_TEST' print ' CARTESIAN_TO_C8 computes the cartesian form of a C8.' print '' print ' X1,Y1=R8_UNIFORM_01 C2=CARTESIAN_TO_C8(X1,Y1) X3,Y3=C8_TO_CARTESIAN(C2)' print ' --------------------- --------------------- ---------------------' print '' seed = 123456789 for i in range(0, 10): x1, seed = r8_uniform_01(seed) y1, seed = r8_uniform_01(seed) c2 = cartesian_to_c8(x1, y1) x3, y3 = c8_to_cartesian(c2) print ' (%12f,%12f) (%12f,%12f) (%12f,%12f)' % (x1, y1, c2.real, c2.imag, x3, y3) print '' print 'CARTESIAN_TO_C8_TEST:' print ' Normal end of execution.' return
def combin_determinant_test ( ): #*****************************************************************************80 # ## COMBIN_DETERMINANT_TEST tests COMBIN_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'COMBIN_DETERMINANT_TEST' print ' COMBIN_DETERMINANT computes the COMBIN determinant.' m = 4 n = 4 seed = 123456789 alpha, seed = r8_uniform_01 ( seed ) alpha = round ( 50.0 * alpha ) / 5.0 beta, seed = r8_uniform_01 ( seed ) 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 ) print '' print 'COMBIN_DETERMINANT_TEST' print ' Normal end of execution.' return
def cartesian_to_c8_test(): # *****************************************************************************80 # ## CARTESIAN_TO_C8_TEST tests CARTESIAN_TO_C8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 February 2015 # # Author: # # John Burkardt # import numpy as np from r8_uniform_01 import r8_uniform_01 from c8_to_cartesian import c8_to_cartesian print "" print "CARTESIAN_TO_C8_TEST" print " CARTESIAN_TO_C8 computes the cartesian form of a C8." print "" print " X1,Y1=R8_UNIFORM_01 C2=CARTESIAN_TO_C8(X1,Y1) X3,Y3=C8_TO_CARTESIAN(C2)" print " --------------------- --------------------- ---------------------" print "" seed = 123456789 for i in range(0, 10): x1, seed = r8_uniform_01(seed) y1, seed = r8_uniform_01(seed) c2 = cartesian_to_c8(x1, y1) x3, y3 = c8_to_cartesian(c2) print " (%12f,%12f) (%12f,%12f) (%12f,%12f)" % (x1, y1, c2.real, c2.imag, x3, y3) print "" print "CARTESIAN_TO_C8_TEST:" print " Normal end of execution." return
def bab_condition_test ( ): #*****************************************************************************80 # ## BAB_CONDITION_TEST tests BAB_CONDITION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'BAB_CONDITION_TEST' print ' BAB_CONDITION computes the condition of the BAB matrix.' print '' seed = 123456789 n = 5 alpha, seed = r8_uniform_01 ( seed ) beta, seed = r8_uniform_01 ( seed ) a = bab ( n, alpha, beta ) r8mat_print ( n, n, a, ' BAB matrix:' ) value = bab_condition ( n, alpha, beta ) print '' print ' Valule = %g' % ( value ) print '' print 'BAB_CONDITION_TEST' print ' Normal end of execution.' return
def bab_condition_test(): #*****************************************************************************80 # ## BAB_CONDITION_TEST tests BAB_CONDITION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'BAB_CONDITION_TEST' print ' BAB_CONDITION computes the condition of the BAB matrix.' print '' seed = 123456789 n = 5 alpha, seed = r8_uniform_01(seed) beta, seed = r8_uniform_01(seed) a = bab(n, alpha, beta) r8mat_print(n, n, a, ' BAB matrix:') value = bab_condition(n, alpha, beta) print '' print ' Valule = %g' % (value) print '' print 'BAB_CONDITION_TEST' print ' Normal end of execution.' return
def c8_normal_01 ( seed ): #*****************************************************************************80 # ## C8_NORMAL_01 returns a unit normally distributed complex number. # # Discussion: # # The value has mean 0 and standard deviation 1. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer SEED, a seed for the random number generator. # # Output, complex C, a sample of the PDF. # # Output, integer SEED, a seed for the random number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 v1, seed = r8_uniform_01 ( seed ) v2, seed = r8_uniform_01 ( seed ) x = np.sqrt ( - 2.0 * np.log ( v1 ) ) * np.cos ( 2.0 * np.pi * v2 ) y = np.sqrt ( - 2.0 * np.log ( v1 ) ) * np.sin ( 2.0 * np.pi * v2 ) c = x + y * 1j return c, seed
def r8_to_dec_test ( ): #*****************************************************************************80 # ## R8_TO_DEC tests R8_TO_DEC. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 25 May 2015 # # Author: # # John Burkardt # from i4_uniform_ab import i4_uniform_ab from r8_uniform_01 import r8_uniform_01 print '' print 'R8_TO_DEC_TEST' print ' R8_TO_DEC converts a real number to a decimal;' dec_digit = 5 print '' print ' The number of decimal digits is %d' % ( dec_digit ) seed = 123456789 print '' print ' R => A * 10^B' print '' for i in range ( 0, 10 ): r, seed = r8_uniform_01 ( seed ) e, seed = i4_uniform_ab ( 0, +10, seed ) r = r * 10.0 ** e a, b = r8_to_dec ( r, dec_digit ) print ' %12g %6d %6d' % ( r, a, b ) # # Terminate. # print '' print 'R8_TO_DEC_TEST' print ' Normal end of execution.' return
def bis_condition_test ( ): #*****************************************************************************80 # ## BIS_CONDITION_TEST tests BIS_CONDITION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 January 2015 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'BIS_CONDITION_TEST' print ' BIS_CONDITION computes the BIS condition.' seed = 123456789 n = 5 alpha, seed = r8_uniform_01 ( seed ) beta, seed = r8_uniform_01 ( seed ) a = bis ( alpha, beta, n, n ) r8mat_print ( n, n, a, ' BIS matrix:' ) value = bis_condition ( alpha, beta, n ) print ' Value = %g' % ( value ) print '' print 'BIS_CONDITION_TEST' print ' Normal end of execution.' return
def bis_determinant_test ( ): #*****************************************************************************80 # ## BIS_DETERMINANT_TEST tests BIS_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'BIS_DETERMINANT_TEST' print ' BIS_DETERMINANT computes the BIS determinant.' seed = 123456789 n = 5 alpha, seed = r8_uniform_01 ( seed ) beta, seed = r8_uniform_01 ( seed ) a = bis ( alpha, beta, n, n ) r8mat_print ( n, n, a, ' BIS matrix:' ) value = bis_determinant ( alpha, beta, n ) print ' Value = %g' % ( value ) print '' print 'BIS_DETERMINANT_TEST' print ' Normal end of execution.' return
def r8_normal_01(seed): #*****************************************************************************80 # ## R8_NORMAL_01 returns a unit pseudonormal R8. # # Discussion: # # The standard normal probability distribution function (PDF) has # mean 0 and standard deviation 1. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 03 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer SEED, a seed for the random number generator. # # Output, real VALUE, a normally distributed # random value. # # Output, integer SEED, an updated seed for the random # number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 r1, seed = r8_uniform_01(seed) r2, seed = r8_uniform_01(seed) value = np.sqrt(-2.0 * np.log(r1)) * np.cos(2.0 * np.pi * r2) return value, seed
def bab_determinant_test(): #*****************************************************************************80 # ## BAB_DETERMINANT_TEST tests BAB_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 05 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'BAB_DETERMINANT_TEST' print ' BAB_DETERMINANT computes the BAB determinant.' seed = 123456789 n = 5 alpha, seed = r8_uniform_01(seed) beta, seed = r8_uniform_01(seed) a = bab(n, alpha, beta) r8mat_print(n, n, a, ' BAB matrix:') value = bab_determinant(n, alpha, beta) print ' Value = %g' % (value) print '' print 'BAB_DETERMINANT_TEST' print ' Normal end of execution.' return
def chow_test ( ): #*****************************************************************************80 # ## CHOW_TEST tests CHOW. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'CHOW_TEST' print ' CHOW computes the CHOW matrix.' m = 5 n = 5 seed = 123456789 alpha, seed = r8_uniform_01 ( seed ) alpha = round ( 50.0 * alpha ) / 5.0 beta, seed = r8_uniform_01 ( seed ) beta = round ( 50.0 * beta ) / 5.0 a = chow ( alpha, beta, m, n ) r8mat_print ( m, n, a, ' CHOW matrix:' ) print '' print 'CHOW_TEST' print ' Normal end of execution.' return
def c8_normal_01 ( seed ): #*****************************************************************************80 # ## C8_NORMAL_01 returns a unit pseudonormal C8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer SEED, a seed for the random number generator. # # Output, real VALUE, a normally distributed # random value. # # Output, integer SEED, an updated seed for the random # number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 v1, seed = r8_uniform_01 ( seed ) v2, seed = r8_uniform_01 ( seed ) x_r = np.sqrt ( - 2.0 * np.log ( v1 ) ) * np.cos ( 2.0 * np.pi * v2 ) x_c = np.sqrt ( - 2.0 * np.log ( v1 ) ) * np.sin ( 2.0 * np.pi * v2 ) value = complex ( x_r, x_c ) return value, seed
def c8_normal_01(seed): #*****************************************************************************80 # ## C8_NORMAL_01 returns a unit pseudonormal C8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer SEED, a seed for the random number generator. # # Output, real VALUE, a normally distributed # random value. # # Output, integer SEED, an updated seed for the random # number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 v1, seed = r8_uniform_01(seed) v2, seed = r8_uniform_01(seed) x_r = np.sqrt(-2.0 * np.log(v1)) * np.cos(2.0 * np.pi * v2) x_c = np.sqrt(-2.0 * np.log(v1)) * np.sin(2.0 * np.pi * v2) value = complex(x_r, x_c) return value, seed
def bis_test ( ): #*****************************************************************************80 # ## BIS_TEST tests BIS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'BIS_TEST' print ' BIS computes the BIS matrix.' seed = 123456789 m = 5 n = 5 alpha, seed = r8_uniform_01 ( seed ) beta, seed = r8_uniform_01 ( seed ) a = bis ( alpha, beta, m, n ) r8mat_print ( m, n, a, ' BIS matrix:' ) print '' print 'BIS_TEST' print ' Normal end of execution.' return
def bab_inverse_test ( ): #*****************************************************************************80 # ## BAB_INVERSE_TEST tests BAB_INVERSE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'BAB_INVERSE_TEST' print ' BAB_INVERSE computes the inverse of the BAB matrix.' seed = 123456789 n = 5 alpha, seed = r8_uniform_01 ( seed ) beta, seed = r8_uniform_01 ( seed ) b = bab_inverse ( n, alpha, beta ) r8mat_print ( n, n, b, ' BAB inverse:' ) print '' print 'BAB_INVERSE_TEST' print ' Normal end of execution.' return
def bab_inverse_test(): #*****************************************************************************80 # ## BAB_INVERSE_TEST tests BAB_INVERSE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'BAB_INVERSE_TEST' print ' BAB_INVERSE computes the inverse of the BAB matrix.' seed = 123456789 n = 5 alpha, seed = r8_uniform_01(seed) beta, seed = r8_uniform_01(seed) b = bab_inverse(n, alpha, beta) r8mat_print(n, n, b, ' BAB inverse:') print '' print 'BAB_INVERSE_TEST' print ' Normal end of execution.' return
def bernstein_poly_01_test2 ( ): #*****************************************************************************80 # ## BERNSTEIN_POLY_01_TEST2 tests the Partition-of-Unity property. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 03 December 2015 # # Author: # # John Burkardt # import numpy as np import platform from r8_uniform_01 import r8_uniform_01 print ( '' ) print ( 'BERNSTEIN_POLY_01_TEST2:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' BERNSTEIN_POLY_01 evaluates the Bernstein polynomials' ) print ( ' based on the interval [0,1].' ) print ( '' ) print ( ' Here we test the partition of unity property.' ) print ( '' ) print ( ' N X Sum ( 0 <= K <= N ) BP01(N,K)(X)' ) print ( '' ) seed = 123456789 for n in range ( 0, 11 ): x, seed = r8_uniform_01 ( seed ) bvec = bernstein_poly_01 ( n, x ) print ( ' %4d %7.4f %14.6g' % ( n, x, np.sum ( bvec ) ) ) # # Terminate. # print ( '' ) print ( 'BERNSTEIN_POLY_01_TEST2:' ) print ( ' Normal end of execution.' ) return
def truncated_normal_ab_sample(mu, sigma, a, b, seed): #*****************************************************************************80 # ## TRUNCATED_NORMAL_AB_SAMPLE samples the Truncated Normal distribution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 08 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the mean and standard deviation of the # parent Normal distribution. # # Input, real A, B, the lower and upper truncation limits. # # Input, integer SEED, a seed for the random number generator. # # Output, real X, a sample of the PDF. # # Output, integer SEED, a seed for the random number generator. # from normal_01_cdf import normal_01_cdf from normal_01_cdf_inv import normal_01_cdf_inv from r8_uniform_01 import r8_uniform_01 alpha = (a - mu) / sigma beta = (b - mu) / sigma alpha_cdf = normal_01_cdf(alpha) beta_cdf = normal_01_cdf(beta) u, seed = r8_uniform_01(seed) xi_cdf = alpha_cdf + u * (beta_cdf - alpha_cdf) xi = normal_01_cdf_inv(xi_cdf) x = mu + sigma * xi return x, seed
def truncated_normal_ab_sample ( mu, sigma, a, b, seed ): #*****************************************************************************80 # ## TRUNCATED_NORMAL_AB_SAMPLE samples the Truncated Normal distribution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 08 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the mean and standard deviation of the # parent Normal distribution. # # Input, real A, B, the lower and upper truncation limits. # # Input, integer SEED, a seed for the random number generator. # # Output, real X, a sample of the PDF. # # Output, integer SEED, a seed for the random number generator. # from normal_01_cdf import normal_01_cdf from normal_01_cdf_inv import normal_01_cdf_inv from r8_uniform_01 import r8_uniform_01 alpha = ( a - mu ) / sigma beta = ( b - mu ) / sigma alpha_cdf = normal_01_cdf ( alpha ) beta_cdf = normal_01_cdf ( beta ) u, seed = r8_uniform_01 ( seed ) xi_cdf = alpha_cdf + u * ( beta_cdf - alpha_cdf ) xi = normal_01_cdf_inv ( xi_cdf ) x = mu + sigma * xi return x, seed
def c8_div_r8_test(): #*****************************************************************************80 # ## C8_DIV_R8_TEST tests C8_DIV_R8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 12 February 2015 # # Author: # # John Burkardt # import numpy as np from c8_uniform_01 import c8_uniform_01 from r8_uniform_01 import r8_uniform_01 print '' print 'C8_DIV_R8_TEST' print ' C8_DIV_R8 divides a C8 by an R8.' print '' print ' C1=C8_UNIFORM_01 R2=R8_UNIFORM_01 C3=C8_DIV_R8(C1,RC2) C4=C1/R2' print ' --------------------- --------------------- --------------------- ---------------------' print '' seed = 123456789 for i in range(0, 10): c1, seed = c8_uniform_01(seed) r2, seed = r8_uniform_01(seed) c3 = c8_div_r8(c1, r2) c4 = c1 / r2 print ' (%12f,%12f) %12f (%12f,%12f) (%12f,%12f)' \ % ( c1.real, c1.imag, r2, c3.real, c3.imag, c4.real, c4.imag ) print '' print 'C8_DIV_R8_TEST:' print ' Normal end of execution.' return
def c8_div_r8_test ( ): #*****************************************************************************80 # ## C8_DIV_R8_TEST tests C8_DIV_R8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 12 February 2015 # # Author: # # John Burkardt # import numpy as np from c8_uniform_01 import c8_uniform_01 from r8_uniform_01 import r8_uniform_01 print '' print 'C8_DIV_R8_TEST' print ' C8_DIV_R8 divides a C8 by an R8.' print '' print ' C1=C8_UNIFORM_01 R2=R8_UNIFORM_01 C3=C8_DIV_R8(C1,RC2) C4=C1/R2' print ' --------------------- --------------------- --------------------- ---------------------' print '' seed = 123456789 for i in range ( 0, 10 ): c1, seed = c8_uniform_01 ( seed ) r2, seed = r8_uniform_01 ( seed ) c3 = c8_div_r8 ( c1, r2 ) c4 = c1 / r2 print ' (%12f,%12f) %12f (%12f,%12f) (%12f,%12f)' \ % ( c1.real, c1.imag, r2, c3.real, c3.imag, c4.real, c4.imag ) print '' print 'C8_DIV_R8_TEST:' print ' Normal end of execution.' return
def kahan_determinant_test(): #*****************************************************************************80 # ## KAHAN_DETERMINANT_TEST tests KAHAN_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 December 2015 # # Author: # # John Burkardt # from kahan import kahan from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'KAHAN_DETERMINANT_TEST' print ' KAHAN_DETERMINANT computes the KAHAN determinant.' seed = 123456789 m = 5 n = m alpha, seed = r8_uniform_01(seed) a = kahan(alpha, m, n) r8mat_print(m, n, a, ' KAHAN matrix:') value = kahan_determinant(alpha, n) print '' print ' Value = %g' % (value) print '' print 'KAHAN_DETERMINANT_TEST' print ' Normal end of execution.' return
def kahan_determinant_test ( ): #*****************************************************************************80 # ## KAHAN_DETERMINANT_TEST tests KAHAN_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 December 2015 # # Author: # # John Burkardt # from kahan import kahan from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'KAHAN_DETERMINANT_TEST' print ' KAHAN_DETERMINANT computes the KAHAN determinant.' seed = 123456789 m = 5 n = m alpha, seed = r8_uniform_01 ( seed ) a = kahan ( alpha, m, n ) r8mat_print ( m, n, a, ' KAHAN matrix:' ) value = kahan_determinant ( alpha, n ) print '' print ' Value = %g' % ( value ) print '' print 'KAHAN_DETERMINANT_TEST' print ' Normal end of execution.' return
def jordan_condition_test(): # *****************************************************************************80 # ## JORDAN_CONDITION_TEST tests JORDAN_CONDITION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 February 2015 # # Author: # # John Burkardt # from jordan import jordan from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print "" print "JORDAN_CONDITION_TEST" print " JORDAN_CONDITION computes the condition of the JORDAN matrix." print "" seed = 123456789 m = 5 n = m alpha, seed = r8_uniform_01(seed) a = jordan(m, n, alpha) r8mat_print(n, n, a, " JORDAN matrix:") value = jordan_condition(n, alpha) print "" print " Value = %g" % (value) print "" print "JORDAN_CONDITION_TEST" print " Normal end of execution." return
def jordan_condition_test(): #*****************************************************************************80 # ## JORDAN_CONDITION_TEST tests JORDAN_CONDITION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 February 2015 # # Author: # # John Burkardt # from jordan import jordan from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'JORDAN_CONDITION_TEST' print ' JORDAN_CONDITION computes the condition of the JORDAN matrix.' print '' seed = 123456789 m = 5 n = m alpha, seed = r8_uniform_01(seed) a = jordan(m, n, alpha) r8mat_print(n, n, a, ' JORDAN matrix:') value = jordan_condition(n, alpha) print '' print ' Value = %g' % (value) print '' print 'JORDAN_CONDITION_TEST' print ' Normal end of execution.' return
def kahan_test ( ): #*****************************************************************************80 # ## KAHAN_TEST tests KAHAN. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 February 2015 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'KAHAN_TEST' print ' KAHAN computes the KAHAN matrix.' seed = 123456789 m = 5 n = 5 alpha, seed = r8_uniform_01 ( seed ) a = kahan ( alpha, m, n ) r8mat_print ( m, n, a, ' KAHAN matrix:' ) print '' print 'KAHAN_TEST' print ' Normal end of execution.' return
def kahan_test(): #*****************************************************************************80 # ## KAHAN_TEST tests KAHAN. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 February 2015 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'KAHAN_TEST' print ' KAHAN computes the KAHAN matrix.' seed = 123456789 m = 5 n = 5 alpha, seed = r8_uniform_01(seed) a = kahan(alpha, m, n) r8mat_print(m, n, a, ' KAHAN matrix:') print '' print 'KAHAN_TEST' print ' Normal end of execution.' return
def lauchli_test(): #*****************************************************************************80 # ## LAUCHLI_TEST tests LAUCHLI. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 08 March 2015 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'LAUCHLI_TEST' print ' LAUCHLI computes the LAUCHLI matrix.' seed = 123456789 m = 5 n = m - 1 alpha, seed = r8_uniform_01(seed) a = lauchli(alpha, m, n) r8mat_print(m, n, a, ' LAUCHLI matrix:') print '' print 'LAUCHLI_TEST' print ' Normal end of execution.' return
def lauchli_test ( ): #*****************************************************************************80 # ## LAUCHLI_TEST tests LAUCHLI. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 08 March 2015 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'LAUCHLI_TEST' print ' LAUCHLI computes the LAUCHLI matrix.' seed = 123456789 m = 5 n = m - 1 alpha, seed = r8_uniform_01 ( seed ) a = lauchli ( alpha, m, n ) r8mat_print ( m, n, a, ' LAUCHLI matrix:' ) print '' print 'LAUCHLI_TEST' print ' Normal end of execution.' return
def tri_upper_test(): #*****************************************************************************80 # ## TRI_UPPER_TEST tests TRI_UPPER. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'TRI_UPPER_TEST' print ' TRI_UPPER computes the TRI_UPPER matrix.' seed = 123456789 n = 5 alpha, seed = r8_uniform_01(seed) a = tri_upper(alpha, n) r8mat_print(n, n, a, ' TRI_UPPER matrix:') print '' print 'TRI_UPPER_TEST' print ' Normal end of execution.' return
def tri_upper_test ( ): #*****************************************************************************80 # ## TRI_UPPER_TEST tests TRI_UPPER. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 December 2014 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 from r8mat_print import r8mat_print print '' print 'TRI_UPPER_TEST' print ' TRI_UPPER computes the TRI_UPPER matrix.' seed = 123456789 n = 5 alpha, seed = r8_uniform_01 ( seed ) a = tri_upper ( alpha, n ) r8mat_print ( n, n, a, ' TRI_UPPER matrix:' ) print '' print 'TRI_UPPER_TEST' print ' Normal end of execution.' return
def ksub_random2(n, k, seed): #*****************************************************************************80 # ## KSUB_RANDOM2 selects a random subset of size K from a set of size N. # # Discussion: # # This algorithm is designated Algorithm RKS2 in the reference. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 December 2014 # # Author: # # John Burkardt. # # Reference: # # Albert Nijenhuis, Herbert Wilf, # Combinatorial Algorithms, # Academic Press, 1978, second edition, # ISBN 0-12-519260-6. # # Parameters: # # Input, integer N, the size of the set from which subsets are drawn. # # Input, integer K, number of elements in desired subsets. K must # be between 0 and N. # # Input, integer SEED, a seed for the random number generator. # # Output, integer A(K). A(I) is the I-th element of the # output set. # # Output, integer SEED, an updated seed for the random number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 from sys import exit if (k < 0): print '' print 'KSUB_RANDOM - Fatal error!' print ' K = %d' % (k) print ' but 0 < K is required!' exit('KSUB_RANDOM - Fatal error!') if (n < k): print '' print 'KSUB_RANDOM - Fatal error!' print ' N = %d' % (n) print ' K = %d' % (k) print ' K <= N is required!' exit('KSUB_RANDOM - Fatal error!') a = np.zeros(k) if (k == 0): return a need = k have = 0 available = n candidate = 0 while (True): candidate = candidate + 1 r, seed = r8_uniform_01(seed) if (available * r <= need): need = need - 1 a[have] = candidate have = have + 1 if (need <= 0): break available = available - 1 return a, seed
def equiv_random ( n, seed ): #*****************************************************************************80 # ## EQUIV_RANDOM selects a random partition of a set. # # Discussion: # # The user does not control the number of parts in the partition. # # The equivalence classes are numbered in no particular order. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 May 2015 # # Author: # # John Burkardt. # # Reference: # # Albert Nijenhuis, Herbert Wilf, # Combinatorial Algorithms, # Academic Press, 1978, second edition, # ISBN 0-12-519260-6. # # Parameters: # # Input, integer N, the number of elements in the set to be partitioned. # # Input, integer SEED, a seed for the random number generator. # # Output, integer NPART, the number of classes or parts in the # partition. NPART will be between 1 and N. # # Output, integer A(N), indicates the class to which each element # is assigned. # # Output, integer SEED, an updated seed for the random number generator. # import numpy as np from i4_uniform_ab import i4_uniform_ab from r8_uniform_01 import r8_uniform_01 b = np.zeros ( n ) b[0] = 1.0 for l in range ( 1, n ): sum1 = 1.0 / float ( l ) for k in range ( 1, l ): sum1 = ( sum1 + b[k-1] ) / float ( l - k ) b[l] = ( sum1 + b[l-1] ) / float ( l + 1 ) a = np.zeros ( n, dtype = np.int32 ) m = n npart = 0 while ( True ): z, seed = r8_uniform_01 ( seed ) z = m * b[m-1] * z k = 0 npart = npart + 1 while ( 0.0 <= z ): a[m-1] = npart m = m - 1 if ( m == 0 ): break z = z - b[m-1] k = k + 1 z = z * k if ( m == 0 ): break # # Randomly permute the assignments. # for i in range ( 1, n ): j, seed = i4_uniform_ab ( i, n, seed ) t = a[i-1] a[i-1] = a[j-1] a[j-1] = t return npart, a, seed
def ksub_random4 ( n, k, seed ): #*****************************************************************************80 # ## KSUB_RANDOM4 selects a random subset of size K from a set of size N. # # Discussion: # # This routine is somewhat impractical for the given problem, but # it is included for comparison, because it is an interesting # approach that is superior for certain applications. # # The approach is mainly interesting because it is "incremental"; # it proceeds by considering every element of the set, and does not # need to know how many elements there are. # # This makes this approach ideal for certain cases, such as the # need to pick 5 lines at random from a text file of unknown length, # or to choose 6 people who call a certain telephone number on a # given day. Using this technique, it is possible to make the # selection so that, whenever the input stops, a valid uniformly # random subset has been chosen. # # Obviously, if the number of items is known in advance, and # it is easy to extract K items directly, there is no need for # this approach, and it is less efficient since, among other costs, # it has to generate a random number for each item, and make an # acceptance/rejection test. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 May 2015 # # Author: # # John Burkardt # # Reference: # # Tom Christiansen and Nathan Torkington, # "8.6: Picking a Random Line from a File", # Perl Cookbook, pages 284-285, # O'Reilly, 1999. # # Parameters: # # Input, integer N, the size of the set from which subsets are drawn. # # Input, integer K, number of elements in desired subsets. K must # be between 0 and N. # # Input, integer SEED, a seed for the random number generator. # # Output, integer A(K), contains the indices of the selected items. # # Output, integer SEED, a seed for the random number generator. # import numpy as np from i4_uniform_ab import i4_uniform_ab from r8_uniform_01 import r8_uniform_01 a = np.zeros ( k ) next = 0 # # Here, we use a DO WHILE to suggest that the algorithm # proceeds to the next item, without knowing how many items # there are in total. # # Note that this is really the only place where N occurs, # so other termination criteria could be used, and we really # don't need to know the value of N! # while ( next < n ): if ( next < k ): i = next a[i] = next else: r, seed = r8_uniform_01 ( seed ) if ( r * ( next + 1 ) <= k ): i4_lo = 0 i4_hi = k - 1 i, seed = i4_uniform_ab ( i4_lo, i4_hi, seed ) a[i] = next next = next + 1 return a, seed
def ksub_random2 ( n, k, seed ): #*****************************************************************************80 # ## KSUB_RANDOM2 selects a random subset of size K from a set of size N. # # Discussion: # # This algorithm is designated Algorithm RKS2 in the reference. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 December 2014 # # Author: # # John Burkardt. # # Reference: # # Albert Nijenhuis, Herbert Wilf, # Combinatorial Algorithms, # Academic Press, 1978, second edition, # ISBN 0-12-519260-6. # # Parameters: # # Input, integer N, the size of the set from which subsets are drawn. # # Input, integer K, number of elements in desired subsets. K must # be between 0 and N. # # Input, integer SEED, a seed for the random number generator. # # Output, integer A(K). A(I) is the I-th element of the # output set. # # Output, integer SEED, an updated seed for the random number generator. # import numpy as np from r8_uniform_01 import r8_uniform_01 from sys import exit if ( k < 0 ): print '' print 'KSUB_RANDOM - Fatal error!' print ' K = %d' % ( k ) print ' but 0 < K is required!' exit ( 'KSUB_RANDOM - Fatal error!' ) if ( n < k ): print '' print 'KSUB_RANDOM - Fatal error!' print ' N = %d' % ( n ) print ' K = %d' % ( k ) print ' K <= N is required!' exit ( 'KSUB_RANDOM - Fatal error!' ) a = np.zeros ( k ) if ( k == 0 ): return a need = k have = 0 available = n candidate = 0 while ( True ): candidate = candidate + 1 r, seed = r8_uniform_01 ( seed ) if ( available * r <= need ): need = need - 1; a[have] = candidate have = have + 1 if ( need <= 0 ): break available = available - 1 return a, seed
def beta_sample(a, b, seed): #*****************************************************************************80 # ## BETA_SAMPLE samples the Beta PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 March 2016 # # Author: # # John Burkardt # # Reference: # # William Kennedy and James Gentle, # Algorithm BN, # Statistical Computing, # Dekker, 1980. # # Parameters: # # Input, real A, B, the parameters of the PDF. # 0.0 < A, # 0.0 < B. # # Input, integer SEED, a seed for the random number generator. # # Output, real X, a sample of the PDF. # # Output, integer SEED, an updated seed for the random number generator. # import numpy as np from normal_01 import normal_01_sample from r8_uniform_01 import r8_uniform_01 mu = (a - 1.0) / (a + b - 2.0) stdev = 0.5 / np.sqrt(a + b - 2.0) while (True): y, seed = normal_01_sample(seed) x = mu + stdev * y if (x < 0.0 or 1.0 < x): continue u, seed = r8_uniform_01(seed) test = ( a - 1.0 ) * np.log ( x / ( a - 1.0 ) ) \ + ( b - 1.0 ) * np.log ( ( 1.0 - x ) / ( b - 1.0 ) ) \ + ( a + b - 2.0 ) * np.log ( a + b - 2.0 ) + 0.5 * y ** 2 if (np.log(u) <= test): break return x, seed
def wathen_ge ( nx, ny, n, seed ): #*****************************************************************************80 # ## WATHEN_GE 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 GNU LGPL 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, Number 4, October 1987, pages 449-457. # # Parameters: # # Input, integer NX, NY, values which determine the size of the matrix. # # Input, integer N, the number of variables. # # Input/output, integer SEED, the random number seed. # # Output, real A(N,N), the matrix. # import numpy as np from r8_uniform_01 import r8_uniform_01 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 ) 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, seed ] = r8_uniform_01 ( seed ) rho = 100.0 * rho 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, seed
def wathen_ge(nx, ny, n, seed): #*****************************************************************************80 # ## WATHEN_GE 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 GNU LGPL 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, Number 4, October 1987, pages 449-457. # # Parameters: # # Input, integer NX, NY, values which determine the size of the matrix. # # Input, integer N, the number of variables. # # Input/output, integer SEED, the random number seed. # # Output, real A(N,N), the matrix. # import numpy as np from r8_uniform_01 import r8_uniform_01 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) 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, seed] = r8_uniform_01(seed) rho = 100.0 * rho 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, seed
def equiv_random(n, seed): #*****************************************************************************80 # ## EQUIV_RANDOM selects a random partition of a set. # # Discussion: # # The user does not control the number of parts in the partition. # # The equivalence classes are numbered in no particular order. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 May 2015 # # Author: # # John Burkardt. # # Reference: # # Albert Nijenhuis, Herbert Wilf, # Combinatorial Algorithms, # Academic Press, 1978, second edition, # ISBN 0-12-519260-6. # # Parameters: # # Input, integer N, the number of elements in the set to be partitioned. # # Input, integer SEED, a seed for the random number generator. # # Output, integer NPART, the number of classes or parts in the # partition. NPART will be between 1 and N. # # Output, integer A(N), indicates the class to which each element # is assigned. # # Output, integer SEED, an updated seed for the random number generator. # import numpy as np from i4_uniform_ab import i4_uniform_ab from r8_uniform_01 import r8_uniform_01 b = np.zeros(n) b[0] = 1.0 for l in range(1, n): sum1 = 1.0 / float(l) for k in range(1, l): sum1 = (sum1 + b[k - 1]) / float(l - k) b[l] = (sum1 + b[l - 1]) / float(l + 1) a = np.zeros(n, dtype=np.int32) m = n npart = 0 while (True): z, seed = r8_uniform_01(seed) z = m * b[m - 1] * z k = 0 npart = npart + 1 while (0.0 <= z): a[m - 1] = npart m = m - 1 if (m == 0): break z = z - b[m - 1] k = k + 1 z = z * k if (m == 0): break # # Randomly permute the assignments. # for i in range(1, n): j, seed = i4_uniform_ab(i, n, seed) t = a[i - 1] a[i - 1] = a[j - 1] a[j - 1] = t return npart, a, seed
def wathen_csc(nx, ny, seed): #*****************************************************************************80 # ## WATHEN_CSC: Wathen matrix stored in Compressed Sparse Column (CSC) format. # # Discussion: # # When dealing with sparse matrices in MATLAB, it can be much more efficient # to work first with a triple of I, J, and X vectors, and only once # they are complete, convert to MATLAB's sparse format. # # 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 GNU LGPL license. # # Modified: # # 01 September 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, Number 4, October 1987, pages 449-457. # # Parameters: # # Input, integer NX, NY, values which determine the size of the matrix. # # Input, integer SEED, the random number seed. # # Output, real A(*,*), the compressed sparxe column version of the matrix. # # Output, integer SEED, the random number seed. # import numpy as np from r8_uniform_01 import r8_uniform_01 from scipy.sparse import coo_matrix 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) st_num = 8 * 8 * nx * ny row = np.zeros(st_num) col = np.zeros(st_num) v = np.zeros(st_num) k = 0 for j in range(0, ny): for i in range(0, nx): 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, seed = r8_uniform_01(seed) rho = 100.0 * rho for krow in range(0, 8): for kcol in range(0, 8): row[k] = node[krow] col[k] = node[kcol] v[k] = rho * em[krow, kcol] k = k + 1 # # Convert triplet to a Python COO matrix. # a = coo_matrix((v, (row, col))) # # Convert COO matrix to CSC format. # a = a.tocsc() return a, seed
def wathen_st(nx, ny, nz_num, seed): # *****************************************************************************80 # ## WATHEN_ST: Wathen matrix stored in sparse triplet format. # # Discussion: # # When dealing with sparse matrices in MATLAB, it can be much more efficient # to work first with a triple of I, J, and X vectors, and only once # they are complete, convert to MATLAB's sparse format. # # 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 GNU LGPL 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, Number 4, October 1987, pages 449-457. # # Parameters: # # Input, integer NX, NY, values which determine the size of the matrix. # # Input, integer NZ_NUM, the number of values used to describe the matrix. # # Input/output, integer SEED, the random number seed. # # Output, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column indices # of the nonzero entries. # # Output, real A(NZ_NUM), the nonzero values. # import numpy as np from r8_uniform_01 import r8_uniform_01 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) row = np.zeros(nz_num) col = np.zeros(nz_num) a = np.zeros(nz_num) k = 0 for j in range(0, ny): for i in range(0, nx): 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, seed = r8_uniform_01(seed) rho = 100.0 * rho for krow in range(0, 8): for kcol in range(0, 8): row[k] = node[krow] col[k] = node[kcol] a[k] = rho * em[krow, kcol] k = k + 1 return row, col, a, seed
def r8_roundx_test ( ): #*****************************************************************************80 # ## R8_ROUNDX_TEST tests R8_ROUNDX. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 July 2014 # # Author: # # John Burkardt # from math import pi from r8_roundx import r8_roundx from r8_uniform_01 import r8_uniform_01 seed = 123456789 x = pi print '' print 'R8_ROUNDX_TEST' print ' R8_ROUNDX rounds a number to a' print ' specified number of decimal digits.' print '' print ' Test effect on PI:' print ' X = %f' % ( x ) print '' print ' NPLACE XROUND' print '' for i in range ( 0, 11 ): nplace = i xround = r8_roundx ( nplace, x ) print ' %6d %f' % ( i, xround ) print '' print ' Test effect on random values:' print '' print ' NPLACE X XROUND' print '' for i in range ( 1, 6 ): x, seed = r8_uniform_01 ( seed ) print '' for i in range ( 0, 6 ): nplace = 2 * i xround = r8_roundx ( nplace, x ) print ' %6d %f %f' % ( nplace, x, xround ) # # Terminate. # print '' print 'R8_ROUNDX_TEST' print ' Normal end of execution.' return
def get_seed_test ( ): #*****************************************************************************80 # ## GET_SEED_TEST tests GET_SEED. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 April 2013 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 print '' print 'GET_SEED_TEST' print ' GET_SEED picks an initial seed value for R8_UNIFORM_01.' print ' The value chosen should vary over time, because' print ' the seed is based on reading the clock.' print '' print ' This is just the "calendar" clock, which does' print ' not change very fast, so calling GET_SEED several' print ' times in a row may result in the same value.' seed = 12345678 seed_old = seed print '' print ' Initial seed is %d' % ( seed ) print '' print ' Next 3 values of R8_UNIFORM_01:' print '' for j in range ( 0, 3 ): [ r, seed ] = r8_uniform_01 ( seed ) print ' %f' % ( r ) for i in range ( 0, 4 ): while ( True ): seed = get_seed ( ) if ( seed != seed_old ): seed_old = seed break print '' print ' New seed from GET_SEED is = %d' % ( seed ) print '' print ' Next 3 values of R8_UNIFORM_01:' print '' for j in range ( 0, 3 ): [ r, seed ] = r8_uniform_01 ( seed ) print ' %f' % ( r ) print '' print 'GET_SEED_TEST:' print ' Normal end of execution.' return
def get_seed_test(): #*****************************************************************************80 # ## GET_SEED_TEST tests GET_SEED. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 April 2013 # # Author: # # John Burkardt # from r8_uniform_01 import r8_uniform_01 print '' print 'GET_SEED_TEST' print ' GET_SEED picks an initial seed value for R8_UNIFORM_01.' print ' The value chosen should vary over time, because' print ' the seed is based on reading the clock.' print '' print ' This is just the "calendar" clock, which does' print ' not change very fast, so calling GET_SEED several' print ' times in a row may result in the same value.' seed = 12345678 seed_old = seed print '' print ' Initial seed is %d' % (seed) print '' print ' Next 3 values of R8_UNIFORM_01:' print '' for j in range(0, 3): [r, seed] = r8_uniform_01(seed) print ' %f' % (r) for i in range(0, 4): while (True): seed = get_seed() if (seed != seed_old): seed_old = seed break print '' print ' New seed from GET_SEED is = %d' % (seed) print '' print ' Next 3 values of R8_UNIFORM_01:' print '' for j in range(0, 3): [r, seed] = r8_uniform_01(seed) print ' %f' % (r) print '' print 'GET_SEED_TEST:' print ' Normal end of execution.' return