def r8_gamma_log_test(): #*****************************************************************************80 # ## R8_GAMMA_LOG_TEST tests R8_GAMMA_LOG. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 25 July 2014 # # Author: # # John Burkardt # import platform from gamma_log_values import gamma_log_values from r8_gamma_log import r8_gamma_log print('') print('R8_GAMMA_LOG_TEST:') print(' Python version: %s' % (platform.python_version())) print(' R8_GAMMA_LOG evaluates the logarithm of the Gamma function.') print('') print(' X GAMMA_LOG(X) R8_GAMMA_LOG(X)') print('') n_data = 0 while (True): n_data, x, fx1 = gamma_log_values(n_data) if (n_data == 0): break fx2 = r8_gamma_log(x) print(' %12g %24.16g %24.16g' % (x, fx1, fx2)) # # Terminate. # print('') print('R8_GAMMA_LOG_TEST') print(' Normal end of execution.') return
def r8_gamma_log_test(): # *****************************************************************************80 # ## R8_GAMMA_LOG_TEST tests R8_GAMMA_LOG. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 25 July 2014 # # Author: # # John Burkardt # from gamma_log_values import gamma_log_values from r8_gamma_log import r8_gamma_log print "" print "R8_GAMMA_LOG_TEST:" print " R8_GAMMA_LOG evaluates the logarithm of the Gamma function." print "" print " X GAMMA_LOG(X) R8_GAMMA_LOG(X)" print "" n_data = 0 while True: n_data, x, fx1 = gamma_log_values(n_data) if n_data == 0: break fx2 = r8_gamma_log(x) print " %12g %24.16g %24.16g" % (x, fx1, fx2) # # Terminate. # print "" print "R8_GAMMA_LOG_TEST" print " Normal end of execution." return
def r8_gamma_log_test(): #*****************************************************************************80 # ## R8_GAMMA_LOG_TEST tests R8_GAMMA_LOG. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 25 July 2014 # # Author: # # John Burkardt # from gamma_log_values import gamma_log_values from r8_gamma_log import r8_gamma_log print '' print 'R8_GAMMA_LOG_TEST:' print ' R8_GAMMA_LOG evaluates the logarithm of the Gamma function.' print '' print ' X GAMMA_LOG(X) R8_GAMMA_LOG(X)' print '' n_data = 0 while (True): n_data, x, fx1 = gamma_log_values(n_data) if (n_data == 0): break fx2 = r8_gamma_log(x) print ' %12g %24.16g %24.16g' % (x, fx1, fx2) print '' print 'R8_GAMMA_LOG_TEST' print ' Normal end of execution.' return
def r8_gamma_log_test ( ): #*****************************************************************************80 # ## R8_GAMMA_LOG_TEST tests R8_GAMMA_LOG. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 25 July 2014 # # Author: # # John Burkardt # from gamma_log_values import gamma_log_values from r8_gamma_log import r8_gamma_log print '' print 'R8_GAMMA_LOG_TEST:' print ' R8_GAMMA_LOG evaluates the logarithm of the Gamma function.' print '' print ' X GAMMA_LOG(X) R8_GAMMA_LOG(X)' print '' n_data = 0 while ( True ): n_data, x, fx1 = gamma_log_values ( n_data ) if ( n_data == 0 ): break fx2 = r8_gamma_log ( x ) print ' %12g %24.16g %24.16g' % ( x, fx1, fx2 ) print '' print 'R8_GAMMA_LOG_TEST' print ' Normal end of execution.' return
def r8_factorial_log ( n ): #*****************************************************************************80 # ## R8_FACTORIAL_LOG computes the natural logarithm of the factorial N! # # Discussion: # # LOG ( FACTORIAL ( N ) ) # = LOG ( product ( 1 <= I <= N ) I ) # = sum ( ( 1 <= I <= N ) LOG ( I ) ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 February 2010 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the argument of the factorial function. # If N is less than 1, VALUE is returned as 0. # # Output, real VALUE, the logarithm of the factorial of N. # from r8_gamma_log import r8_gamma_log value = r8_gamma_log ( float ( n + 1 ) ) return value
def commul ( n, nfactor, iarray ): #*****************************************************************************80 # ## COMMUL computes a multinomial combinatorial coefficient. # # Discussion: # # The multinomial coefficient is a generalization of the binomial # coefficient. It may be interpreted as the number of combinations of # N objects, where IARRAY(1) objects are indistinguishable of type 1, # ... and IARRAY(K) are indistinguishable of type NFACT. # # The formula is # # NCOMB = N! / ( IARRAY(1)! IARRAY(2)! ... IARRAY(NFACT)! ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 January 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, determines the numerator. # # Input, integer NFACTOR, the number of factors in the numerator. # # Input, integer IARRAY(NFACTOR). # IARRAY contains the NFACT values used in the denominator. # Note that the sum of these entries should be N, # and that all entries should be nonnegative. # # Output, integer NCOMB, the value of the multinomial coefficient. # import numpy as np from r8_gamma_log import r8_gamma_log from sys import exit for i in range ( 0, nfactor ): if ( iarray[i] < 0 ): print '' print 'COMMUL - Fatal error!' print ' Entry %d of IARRAY = %d' % ( i, iarray[i] ) print 1, ' But this value must be nonnegative.' exit ( 'COMMUL - Fatal error!' ) isum = np.sum ( iarray ) if ( isum != n ): print '' print 'COMMUL - Fatal error!' print ' The sum of the IARRAY entries is %d' % (isum ) print ' But it must equal N = %d' % ( n ) exit ( 'COMMUL - Fatal error!' ) facn = r8_gamma_log ( float ( n + 1 ) ) for i in range ( 0, nfactor ): fack = r8_gamma_log ( float ( iarray[i] + 1 ) ) facn = facn - fack ncomb = int ( np.round ( np.exp ( facn ) ) ) return ncomb
def commul(n, nfactor, iarray): #*****************************************************************************80 # ## COMMUL computes a multinomial combinatorial coefficient. # # Discussion: # # The multinomial coefficient is a generalization of the binomial # coefficient. It may be interpreted as the number of combinations of # N objects, where IARRAY(1) objects are indistinguishable of type 1, # ... and IARRAY(K) are indistinguishable of type NFACT. # # The formula is # # NCOMB = N! / ( IARRAY(1)! IARRAY(2)! ... IARRAY(NFACT)! ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 January 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, determines the numerator. # # Input, integer NFACTOR, the number of factors in the numerator. # # Input, integer IARRAY(NFACTOR). # IARRAY contains the NFACT values used in the denominator. # Note that the sum of these entries should be N, # and that all entries should be nonnegative. # # Output, integer NCOMB, the value of the multinomial coefficient. # import numpy as np from r8_gamma_log import r8_gamma_log from sys import exit for i in range(0, nfactor): if (iarray[i] < 0): print '' print 'COMMUL - Fatal error!' print ' Entry %d of IARRAY = %d' % (i, iarray[i]) print 1, ' But this value must be nonnegative.' exit('COMMUL - Fatal error!') isum = np.sum(iarray) if (isum != n): print '' print 'COMMUL - Fatal error!' print ' The sum of the IARRAY entries is %d' % (isum) print ' But it must equal N = %d' % (n) exit('COMMUL - Fatal error!') facn = r8_gamma_log(float(n + 1)) for i in range(0, nfactor): fack = r8_gamma_log(float(iarray[i] + 1)) facn = facn - fack ncomb = int(np.round(np.exp(facn))) return ncomb
def r8_choose(n, k): #*****************************************************************************80 # ## R8_CHOOSE computes the binomial coefficient C(N,K) as an R8. # # Discussion: # # The value is calculated in such a way as to avoid overflow and # roundoff. The calculation is done in R8 arithmetic. # # The formula used is: # # C(N,K) = N! / ( K! * (N-K)! ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, K, are the values of N and K. # # Output, real VALUE, the number of combinations of N # things taken K at a time. # import numpy as np from r8_gamma_log import r8_gamma_log if (n < 0): value = 0.0 elif (k == 0): value = 1.0 elif (k == 1): value = float(n) elif (1 < k and k < n - 1): facn = r8_gamma_log(float(n + 1)) fack = r8_gamma_log(float(k + 1)) facnmk = r8_gamma_log(float(n - k + 1)) value = round(np.exp(facn - fack - facnmk)) elif (k == n - 1): value = float(n) elif (k == n): value = 1.0 else: value = 0.0 return value