def ijfact1_determinant ( n ): #*****************************************************************************80 # ## IJFACT1_DETERMINANT computes the determinant of the IJFACT1 matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the determinant. # from r8_factorial import r8_factorial value = 1.0 for i in range ( 1, n ): value = value * r8_factorial ( i + 1 ) * r8_factorial ( n - i ) value = value * r8_factorial ( n + 1 ) return value
def ijfact1_determinant(n): #*****************************************************************************80 # ## IJFACT1_DETERMINANT computes the determinant of the IJFACT1 matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the determinant. # from r8_factorial import r8_factorial value = 1.0 for i in range(1, n): value = value * r8_factorial(i + 1) * r8_factorial(n - i) value = value * r8_factorial(n + 1) return value
def laguerre_determinant ( n ): #*****************************************************************************80 # ## LAGUERRE_DETERMINANT computes the determinant of the LAGUERRE matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the determinant. # from r8_factorial import r8_factorial value = 1.0 p = + 1.0 for i in range ( 0, n ): value = value * p / r8_factorial ( i ) p = - p return value
def laguerre_determinant(n): #*****************************************************************************80 # ## LAGUERRE_DETERMINANT computes the determinant of the LAGUERRE matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the determinant. # from r8_factorial import r8_factorial value = 1.0 p = +1.0 for i in range(0, n): value = value * p / r8_factorial(i) p = -p return value
def carry_inverse(n, alpha): #*****************************************************************************80 # ## CARRY_INVERSE returns the inverse of the CARRY matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ALPHA, the numeric base used in the addition. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # from diagonal import diagonal from r8_factorial import r8_factorial from r8mat_mm import r8mat_mm v = carry_eigen_left(n, alpha) d = carry_eigenvalues(n, alpha) for i in range(0, n): d[i] = 1.0 / d[i] d_inv = diagonal(n, n, d) u = carry_eigen_right(n, alpha) dv = r8mat_mm(n, n, n, d_inv, v) a = r8mat_mm(n, n, n, u, dv) t = r8_factorial(n) for j in range(0, n): for i in range(0, n): a[i, j] = a[i, j] / t return a
def carry_inverse ( n, alpha ): #*****************************************************************************80 # ## CARRY_INVERSE returns the inverse of the CARRY matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ALPHA, the numeric base used in the addition. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # from diagonal import diagonal from r8_factorial import r8_factorial from r8mat_mm import r8mat_mm v = carry_eigen_left ( n, alpha ) d = carry_eigenvalues ( n, alpha ) for i in range ( 0, n ): d[i] = 1.0 / d[i] d_inv = diagonal ( n, n, d ) u = carry_eigen_right ( n, alpha ) dv = r8mat_mm ( n, n, n, d_inv, v ) a = r8mat_mm ( n, n, n, u, dv ) t = r8_factorial ( n ) for j in range ( 0, n ): for i in range ( 0, n ): a[i,j] = a[i,j] / t return a
def legendre_associated_normalized(n, m, x): #*****************************************************************************80 # ## LEGENDRE_ASSOCIATED_NORMALIZED evaluates the associated Legendre functions. # # Discussion: # # The unnormalized associated Legendre functions P_N^M(X) have # the property that # # Integral ( -1 <= X <= 1 ) ( P_N^M(X) )^2 dX # = 2 * ( N + M )! / ( ( 2 * N + 1 ) * ( N - M )! ) # # By dividing the function by the square root of this term, # the normalized associated Legendre functions have norm 1. # # However, we plan to use these functions to build spherical # harmonics, so we use a slightly different normalization factor of # # sqrt ( ( ( 2 * N + 1 ) * ( N - M )! ) / ( 4 * pi * ( N + M )! ) ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 February 2015 # # Author: # # John Burkardt # # Reference: # # Milton Abramowitz and Irene Stegun, # Handbook of Mathematical Functions, # US Department of Commerce, 1964. # # Parameters: # # Input, integer N, the maximum first index of the Legendre # function, which must be at least 0. # # Input, integer M, the second index of the Legendre function, # which must be at least 0, and no greater than N. # # Input, real X, the point at which the function is to be # evaluated. X must satisfy -1 <= X <= 1. # # Output, real CX(1:N+1), the values of the first N+1 function. # import numpy as np from r8_factorial import r8_factorial if (m < 0): print '' print 'LEGENDRE_ASSOCIATED_NORMALIZED - Fatal error!' print ' Input value of M is %d' % (m) print ' but M must be nonnegative.' if (n < m): print '' print 'LEGENDRE_ASSOCIATED_NORMALIZED - Fatal error!' print ' Input value of M = %d' % (m) print ' Input value of N = %d' % (n) print ' but M must be less than or equal to N.' if (x < -1.0): print '' print 'LEGENDRE_ASSOCIATED_NORMALIZED - Fatal error!' print ' Input value of X = %f' % (x) print ' but X must be no less than -1.' if (1.0 < x): print '' print 'LEGENDRE_ASSOCIATED_NORMALIZED - Fatal error!' print ' Input value of X = %f' % (x) print ' but X must be no more than 1.' cx = np.zeros(n + 1) cx[m] = 1.0 somx2 = np.sqrt(1.0 - x * x) fact = 1.0 for i in range(0, m): cx[m] = -cx[m] * fact * somx2 fact = fact + 2.0 if (m != n): cx[m + 1] = x * float(2 * m + 1) * cx[m] for i in range(m + 2, n + 1): cx[i] = ( float ( 2 * i - 1 ) * x * cx[i-1] \ + float ( - i - m + 1 ) * cx[i-2] ) \ / float ( i - m ) # # Normalization. # for mm in range(m, n + 1): factor = np.sqrt ( ( ( 2 * mm + 1 ) * r8_factorial ( mm - m ) ) \ / ( 4.0 * np.pi * r8_factorial ( mm + m ) ) ) cx[mm] = cx[mm] * factor return cx
def ijfact2 ( n ): #*****************************************************************************80 # ## IJFACT2 returns the IJFACT2 matrix. # # Formula: # # A(I,J) = 1 / ( (I+J)! ) # # Example: # # N = 4 # # 1/2 1/6 1/24 1/120 # 1/6 1/24 1/120 1/720 # 1/24 1/120 1/720 1/5040 # 1/120 1/720 1/5040 1/40320 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is a Hankel matrix: constant along anti-diagonals. # # A is integral: int ( A ) = A. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 February 2015 # # Author: # # John Burkardt # # Reference: # # MJC Gover, # The explicit inverse of factorial Hankel matrices, # Department of Mathematics, University of Bradford, 1993. # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real A(N,N), the matrix. # import numpy as np from r8_factorial import r8_factorial a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = 1.0 / r8_factorial ( i + j + 2 ) return a
def simplex_coordinates2_test(m): #*****************************************************************************80 # ## SIMPLEX_COORDINATES2_TEST tests SIMPLEX_COORDINATES2. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 June 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, the spatial dimension. # import numpy as np import platform import r8_factorial import r8mat_transpose_print from simplex_volume import simplex_volume print('') print('SIMPLEX_COORDINATES2_TEST') print(' Python version: %s' % (platform.python_version())) print(' Test SIMPLEX_COORDINATES2') x = simplex_coordinates2(m) #r8mat_transpose_print.r8mat_transpose_print ( m, m + 1, x, ' Simplex vertex coordinates:' ) s = 0.0 for i in range(0, m): s = s + (x[i, 0] - x[i, 1])**2 side = np.sqrt(s) #volume = simplex_volume ( m, x ) volume2 = np.sqrt(m + 1) / r8_factorial.r8_factorial(m) / np.sqrt( 2.0**m) * side**m print('') print(' Side length = %g' % (side)) print(' Volume = %g' % (volume2)) print(' Expected volume = %g' % (volume2)) xtx = np.dot(np.transpose(x), x) print(xtx) #r8mat_transpose_print.r8mat_transpose_print ( m + 1, m + 1, xtx, ' Dot product matrix:' ) # # Terminate. # print('') print('SIMPLEX_COORDINATES2_TEST') print(' Normal end of execution.') return
def ijfact1(n): #*****************************************************************************80 # ## IJFACT1 returns the IJFACT1 matrix. # # Formula: # # A(I,J) = (I+J)! # # Example: # # N = 4 # # 2 6 24 120 # 6 24 120 720 # 24 120 720 5040 # 120 720 5040 40320 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is a Hankel matrix: constant along anti-diagonals. # # A is integral: int ( A ) = A. # # The family of matrices is nested as a function of N. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 February 2015 # # Author: # # John Burkardt # # Reference: # # MJC Gover, # The explicit inverse of factorial Hankel matrices, # Department of Mathematics, University of Bradford, 1993. # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real A(N,N), the matrix. # import numpy as np from r8_factorial import r8_factorial a = np.zeros([n, n]) for i in range(0, n): for j in range(0, n): a[i, j] = r8_factorial(i + j + 2) return a
def legendre_associated_normalized ( n, m, x ): #*****************************************************************************80 # ## LEGENDRE_ASSOCIATED_NORMALIZED evaluates the associated Legendre functions. # # Discussion: # # The unnormalized associated Legendre functions P_N^M(X) have # the property that # # Integral ( -1 <= X <= 1 ) ( P_N^M(X) )^2 dX # = 2 * ( N + M )! / ( ( 2 * N + 1 ) * ( N - M )! ) # # By dividing the function by the square root of this term, # the normalized associated Legendre functions have norm 1. # # However, we plan to use these functions to build spherical # harmonics, so we use a slightly different normalization factor of # # sqrt ( ( ( 2 * N + 1 ) * ( N - M )! ) / ( 4 * pi * ( N + M )! ) ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 February 2015 # # Author: # # John Burkardt # # Reference: # # Milton Abramowitz and Irene Stegun, # Handbook of Mathematical Functions, # US Department of Commerce, 1964. # # Parameters: # # Input, integer N, the maximum first index of the Legendre # function, which must be at least 0. # # Input, integer M, the second index of the Legendre function, # which must be at least 0, and no greater than N. # # Input, real X, the point at which the function is to be # evaluated. X must satisfy -1 <= X <= 1. # # Output, real CX(1:N+1), the values of the first N+1 function. # import numpy as np from r8_factorial import r8_factorial if ( m < 0 ): print '' print 'LEGENDRE_ASSOCIATED_NORMALIZED - Fatal error!' print ' Input value of M is %d' % ( m ) print ' but M must be nonnegative.' if ( n < m ): print '' print 'LEGENDRE_ASSOCIATED_NORMALIZED - Fatal error!' print ' Input value of M = %d' % ( m ) print ' Input value of N = %d' % ( n ) print ' but M must be less than or equal to N.' if ( x < -1.0 ): print '' print 'LEGENDRE_ASSOCIATED_NORMALIZED - Fatal error!' print ' Input value of X = %f' % ( x ) print ' but X must be no less than -1.' if ( 1.0 < x ): print '' print 'LEGENDRE_ASSOCIATED_NORMALIZED - Fatal error!' print ' Input value of X = %f' % ( x ) print ' but X must be no more than 1.' cx = np.zeros ( n + 1 ) cx[m] = 1.0 somx2 = np.sqrt ( 1.0 - x * x ) fact = 1.0 for i in range ( 0, m ): cx[m] = - cx[m] * fact * somx2 fact = fact + 2.0 if ( m != n ): cx[m+1] = x * float ( 2 * m + 1 ) * cx[m] for i in range ( m + 2, n + 1 ): cx[i] = ( float ( 2 * i - 1 ) * x * cx[i-1] \ + float ( - i - m + 1 ) * cx[i-2] ) \ / float ( i - m ) # # Normalization. # for mm in range ( m, n + 1 ): factor = np.sqrt ( ( ( 2 * mm + 1 ) * r8_factorial ( mm - m ) ) \ / ( 4.0 * np.pi * r8_factorial ( mm + m ) ) ) cx[mm] = cx[mm] * factor return cx
def bernoulli_number3 ( n ): #*****************************************************************************80 # ## BERNOULLI_NUMBER3 computes the value of the Bernoulli number B(N). # # Discussion: # # The Bernoulli numbers are rational. # # If we define the sum of the M-th powers of the first N integers as: # # SIGMA(M,N) = sum ( 0 <= I <= N ) I^M # # and let C(I,J) be the combinatorial coefficient: # # C(I,J) = I! / ( ( I - J )! * J! ) # # then the Bernoulli numbers B(J) satisfy: # # SIGMA(M,N) = 1/(M+1) * sum ( 0 <= J <= M ) C(M+1,J) B(J) * (N+1)^(M+1-J) # # First values: # # B0 1 = 1.00000000000 # B1 -1/2 = -0.50000000000 # B2 1/6 = 1.66666666666 # B3 0 = 0 # B4 -1/30 = -0.03333333333 # B5 0 = 0 # B6 1/42 = 0.02380952380 # B7 0 = 0 # B8 -1/30 = -0.03333333333 # B9 0 = 0 # B10 5/66 = 0.07575757575 # B11 0 = 0 # B12 -691/2730 = -0.25311355311 # B13 0 = 0 # B14 7/6 = 1.16666666666 # B15 0 = 0 # B16 -3617/510 = -7.09215686274 # B17 0 = 0 # B18 43867/798 = 54.97117794486 # B19 0 = 0 # B20 -174611/330 = -529.12424242424 # B21 0 = 0 # B22 854513/138 = 6192.123 # B23 0 = 0 # B24 -236364091/2730 = -86580.257 # B25 0 = 0 # B26 8553103/6 = 1425517.16666 # B27 0 = 0 # B28 -23749461029/870 = -27298231.0678 # B29 0 = 0 # B30 8615841276005/14322 = 601580873.901 # # Recursion: # # With C(N+1,K) denoting the standard binomial coefficient, # # B(0) = 1.0 # B(N) = - ( sum ( 0 <= K < N ) C(N+1,K) * B(K) ) / C(N+1,N) # # Special Values: # # Except for B(1), all Bernoulli numbers of odd index are 0. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 December 2014 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the Bernoulli number to compute. # # Output, real B, the desired Bernoulli number. # from r8_factorial import r8_factorial itmax = 1000 r8_pi = 3.141592653589793 tol = 5.0E-07 if ( n < 0 ): b = 0.0 elif ( n == 0 ): b = 1.0 elif ( n == 1 ): b = -0.5 elif ( n == 2 ): b = 1.0 / 6.0 elif ( ( n % 2 ) == 1 ): b = 0.0 else: sum2 = 0.0 for i in range ( 1, itmax + 1 ): term = 1.0 / float ( i ) ** n sum2 = sum2 + term if ( abs ( term ) < tol or abs ( term ) < tol * abs ( sum2 ) ): break b = 2.0 * sum2 * r8_factorial ( n ) / ( 2.0 * r8_pi ) ** n if ( ( n % 4 ) == 0 ): b = -b return b