#! /usr/bin/env python3 # def disk_positive_distance_histogram ( n ): #*****************************************************************************80 # ## disk_positive_distance_histogram() histograms positive disk distance statistics. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 August 2023 # # Author: # # John Burkardt # # Input: # # integer N, the number of samples to use. # import matplotlib.pyplot as plt import numpy as np t = np.zeros ( n ) for i in range ( 0, n ): p = disk_positive_sample ( ) q = disk_positive_sample ( ) t[i] = np.linalg.norm ( p - q ) plt.hist ( t, bins = 20, rwidth = 0.95, density = True ) plt.grid ( True ) plt.xlabel ( '<-- Distance -->' ) plt.ylabel ( '<-- Frequency -->' ) plt.title ( 'Distance between a pair of random points in a unit positive disk' ) filename = 'disk_positive_distance_histogram.png' plt.savefig ( filename ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( block = False ) plt.close ( ) return def disk_positive_distance_stats ( n ): #*****************************************************************************80 # ## disk_positive_distance_stats() estimates disk distance statistics. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 August 2023 # # Author: # # John Burkardt # # Input: # # integer N, the number of sample points to use. # # Output: # # real MU, VAR, the estimated mean and variance of the # distance between two random points in the unit disk. # import numpy as np t = np.zeros ( n ) for i in range ( 0, n ): p = disk_positive_sample ( ) q = disk_positive_sample ( ) t[i] = np.linalg.norm ( p - q ) mu = np.mean ( t ) var = np.var ( t ) print ( '' ) print ( ' Using N = ', n, ' sample points,' ) print ( ' Estimated mean distance = ', mu ) print ( ' Estimated variance = ', var ) return mu, var def disk_positive_distance_test ( ): #*****************************************************************************80 # ## disk_positive_distance_test() tests disk_positive_distance(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 May 2019 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'disk_positive_distance_test():' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test disk_distance().' ) n = 10000 [ mu, var ] = disk_positive_distance_stats ( n ) n = 10000 disk_positive_distance_histogram ( n ) # # Terminate. # print ( '' ) print ( 'disk_positive_distance_test():' ) print ( ' Normal end of execution.' ) return def disk_positive_sample ( ): #*****************************************************************************80 # ## disk_positive_sample() uniformly samples the unit positive disk. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 August 2023 # # Author: # # John Burkardt # # Output: # # real P[2]: a point selected uniformly at random from # the positive disk of radius 1 and center (0,0). # from numpy.random import default_rng import numpy as np rng = default_rng ( ) theta = 0.5 * np.pi * rng.random ( ) r = np.sqrt ( rng.random ( ) ) x = np.abs ( r * np.cos ( theta ) ) y = np.abs ( r * np.sin ( theta ) ) p = np.array ( [ x, y ] ) return p def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) disk_positive_distance_test ( ) timestamp ( )