def lotkin_determinant ( n ): #*****************************************************************************80 # ## LOTKIN_DETERMINANT returns the determinant of the LOTKIN matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 18 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the determinant. # from r8_choose import r8_choose delta = 1.0 for i in range ( 2, n + 1 ): delta = - r8_choose ( 2 * i - 2, i - 2 ) * r8_choose ( 2 * i - 2, i - 1 ) \ * float ( 2 * i - 1 ) * delta value = 1.0 / delta return value
def boothroyd ( n ): #*****************************************************************************80 # ## BOOTHROYD returns the BOOTHROYD matrix. # # Formula: # # A(I,J) = C(N+I-1,I-1) * C(N-1,N-J) * N / ( I + J - 1 ) # # Example: # # N = 5 # # 5 10 10 5 1 # 15 40 45 24 5 # 35 105 126 70 15 # 70 224 280 160 35 # 126 420 540 315 70 # # Properties: # # A is not symmetric. # # A is positive definite. # # det ( A ) = 1. # # The inverse matrix has the same entries, but with alternating sign. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 September 2007 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose a = np.zeros ( [ n, n ] ) for j in range ( 0, n ): for i in range ( 0, n ): a[i,j] = r8_choose ( n + i, i ) * r8_choose ( n - 1, n - j - 1 ) * n \ / ( i + j + 1 ); return a
def pascal1_condition ( n ): #*****************************************************************************80 # ## PASCAL1_CONDITION returns the L1 condition of the PASCAL1 matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the L1 condition. # from r8_choose import r8_choose nhalf = ( ( n + 1 ) // 2 ) a_norm = r8_choose ( n, nhalf ) b_norm = r8_choose ( n, nhalf ) value = a_norm * b_norm return value
def lotkin_determinant(n): #*****************************************************************************80 # ## LOTKIN_DETERMINANT returns the determinant of the LOTKIN matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 18 February 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the determinant. # from r8_choose import r8_choose delta = 1.0 for i in range(2, n + 1): delta = - r8_choose ( 2 * i - 2, i - 2 ) * r8_choose ( 2 * i - 2, i - 1 ) \ * float ( 2 * i - 1 ) * delta value = 1.0 / delta return value
def bernstein_to_power(n): #*****************************************************************************80 # ## BERNSTEIN_TO_POWER returns the Bernstein-to-Power matrix. # # Discussion: # # The Bernstein-to-Power matrix of degree N is an N+1xN+1 matrix A which can # be used to transform the N+1 coefficients of a polynomial of degree N # from a vector B of Bernstein basis polynomial coefficients ((1-x)^n,...,x^n). # to a vector P of coefficients of the power basis (1,x,x^2,...,x^n). # # If we are using N=4-th degree polynomials, the matrix has the form: # # 1 0 0 0 0 # -4 4 0 0 0 # 6 -12 6 0 0 # -4 12 -12 4 0 # 1 -4 6 -4 1 # # and a polynomial with the Bernstein basis representation # p(x) = 3/4 * b(4,1) + 1/2 b(4,2) # whose Bernstein coefficient vector is # B = ( 0, 3/4, 1/2, 0, 0 ) # will have the Bernstein basis coefficients # P = A * B = ( 0, 3, -6, 3, 0 ). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the degree of the polynomials. # # Output, real A(N+1,N+1), the Bernstein-to-Power matrix. # import numpy as np from r8_choose import r8_choose from r8_mop import r8_mop a = np.zeros([n + 1, n + 1]) for j in range(0, n + 1): for i in range(0, j + 1): a[n-i,n-j] = r8_mop ( j - i ) * r8_choose ( n - i, j - i ) \ * r8_choose ( n, i ) return a
def bernstein ( n ): #*****************************************************************************80 # ## BERNSTEIN returns the BERNSTEIN matrix. # # Discussion: # # The Bernstein matrix of order N is an NxN matrix A which can be used to # transform a vector of power basis coefficients C representing a polynomial # P(X) to a corresponding Bernstein basis coefficient vector B: # # B = A * C # # The N power basis vectors are ordered as (1,X,X^2,...X^(N-1)) and the N # Bernstein basis vectors as ((1-X)^(N-1), X*(1_X)^(N-2),...,X^(N-1)). # # Example: # # N = 5 # # 1 -4 6 -4 1 # 0 4 -12 12 -4 # 0 0 6 -12 6 # 0 0 0 4 -4 # 0 0 0 0 1 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real A(N,N), the Bernstein matrix. # import numpy as np from r8_choose import r8_choose from r8_mop import r8_mop a = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, j + 1 ): a[i,j] = r8_mop ( j - i ) * r8_choose ( n - 1 - i, j - i ) \ * r8_choose ( n - 1, i ) return a
def bernstein_matrix(n): #*****************************************************************************80 # ## BERNSTEIN_MATRIX returns the Bernstein matrix. # # Discussion: # # The Bernstein matrix of order N is an NxN matrix A which can be used to # transform a vector of power basis coefficients C representing a polynomial # P(X) to a corresponding Bernstein basis coefficient vector B: # # B = A * C # # The N power basis vectors are ordered as (1,X,X^2,...X^(N-1)) and the N # Bernstein basis vectors as ((1-X)^(N-1), X*(1-X)^(N-2),...,X^(N-1)). # # Example: # # N = 5 # # 1 -4 6 -4 1 # 0 4 -12 12 -4 # 0 0 6 -12 6 # 0 0 0 4 -4 # 0 0 0 0 1 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real A(N,N), the Bernstein matrix. # import numpy as np from r8_choose import r8_choose from r8_mop import r8_mop a = np.zeros((n, n)) for j in range(0, n): for i in range(0, j + 1): a[i,j] = r8_mop ( j - i ) * r8_choose ( n - 1 - i, j - i ) \ * r8_choose ( n - 1, i ) return a
def bernstein_inverse(n): #*****************************************************************************80 # ## BERNSTEIN_INVERSE returns the inverse of the BERNSTEIN matrix. # # Discussion: # # The inverse Bernstein matrix of order N is an NxN matrix A which can # be used to transform a vector of Bernstein basis coefficients B # representing a polynomial P(X) to a corresponding power basis # coefficient vector C: # # C = A * B # # The N power basis vectors are ordered as (1,X,X^2,...X^(N-1)) and the N # Bernstein basis vectors as ((1-X)^(N-1), X*(1_X)^(N-2),...,X^(N-1)). # # Example: # # N = 5 # # 1.0000 1.0000 1.0000 1.0000 1.0000 # 0 0.2500 0.5000 0.7500 1.0000 # 0 0 0.1667 0.5000 1.0000 # 0 0 0 0.2500 1.0000 # 0 0 0 0 1.0000 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real A(N,N), the inverse Bernstein matrix. # import numpy as np from r8_choose import r8_choose a = np.zeros((n, n)) for j in range(0, n): for i in range(0, j + 1): a[i, j] = r8_choose(j, i) / r8_choose(n - 1, i) return a
def bernstein_inverse ( n ): #*****************************************************************************80 # ## BERNSTEIN_INVERSE returns the inverse of the BERNSTEIN matrix. # # Discussion: # # The inverse Bernstein matrix of order N is an NxN matrix A which can # be used to transform a vector of Bernstein basis coefficients B # representing a polynomial P(X) to a corresponding power basis # coefficient vector C: # # C = A * B # # The N power basis vectors are ordered as (1,X,X^2,...X^(N-1)) and the N # Bernstein basis vectors as ((1-X)^(N-1), X*(1_X)^(N-2),...,X^(N-1)). # # Example: # # N = 5 # # 1.0000 1.0000 1.0000 1.0000 1.0000 # 0 0.2500 0.5000 0.7500 1.0000 # 0 0 0.1667 0.5000 1.0000 # 0 0 0 0.2500 1.0000 # 0 0 0 0 1.0000 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real A(N,N), the inverse Bernstein matrix. # import numpy as np from r8_choose import r8_choose a = np.zeros ( ( n, n ) ) for j in range ( 0, n ): for i in range ( 0, j + 1 ): a[i,j] = r8_choose ( j, i ) / r8_choose ( n - 1, i ) return a
def power_to_bernstein(n): #*****************************************************************************80 # ## POWER_TO_BERNSTEIN returns the Power-to-Bernstein matrix. # # Discussion: # # The Power-to-Bernstein matrix of degree N is an N+1xN+1 matrix A which can # be used to transform the N+1 coefficients of a polynomial of degree N # from a vector P of coefficients of the power basis (1,x,x^2,...,x^n) # to a vector B of Bernstein basis polynomial coefficients ((1-x)^n,...,x^n). # # If we are using N=4-th degree polynomials, the matrix has the form: # # 1 0 0 0 0 # 1 1/4 0 0 0 # A = 1 1/2 1/6 0 0 # 1 3/4 1/2 1/4 1 # 1 1 1 1 1 # # and a polynomial # p(x) = 3x - 6x^2 + 3x^3 # whose power coefficient vector is # P = ( 0, 3, -6, 3, 0 ) # will have the Bernstein basis coefficients # B = A * P = ( 0, 3/4, 1/2, 0, 0 ). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the degree of the polynomials. # # Output, real A[0:N,0:N], the Power-to-Bernstein matrix. # import numpy as np from r8_choose import r8_choose a = np.zeros([n + 1, n + 1]) for j in range(0, n + 1): for i in range(0, j + 1): a[n - i, n - j] = r8_choose(j, i) / r8_choose(n, i) return a
def bernstein_determinant ( n ): #*****************************************************************************80 # ## BERNSTEIN_DETERMINANT returns the determinant of the BERNSTEIN matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the determinant. # from r8_choose import r8_choose value = 1.0 for i in range ( 0, n ): value = value * r8_choose ( n - 1, i ) return value
def bernstein_matrix_determinant(n): #*****************************************************************************80 # ## BERNSTEIN_MATRIX_DETERMINANT returns the determinant of the Bernstein matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 15 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the determinant. # from r8_choose import r8_choose value = 1.0 for i in range(0, n): value = value * r8_choose(n - 1, i) return value
def bernstein_to_legendre(n): #*****************************************************************************80 # ## BERNSTEIN_TO_LEGENDRE returns the Bernstein-to-Legendre matrix. # # Discussion: # # The Legendre polynomials are often defined on [-1,+1], while the # Bernstein polynomials are defined on [0,1]. For this function, # the Legendre polynomials have been shifted to share the [0,1] # interval of definition. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the maximum degree of the polynomials. # # Output, real A(N+1,N+1), the Bernstein-to-Legendre matrix. # import numpy as np from r8_choose import r8_choose from r8_mop import r8_mop a = np.zeros([n + 1, n + 1]) for i in range(0, n + 1): for j in range(0, n + 1): for k in range(0, i + 1): a[i,j] = a[i,j] \ + r8_mop ( i + k ) * r8_choose ( i, k ) ** 2 \ / r8_choose ( n + i, j + k ) a[i,j] = a[i,j] * r8_choose ( n, j ) \ * ( 2 * i + 1 ) / ( n + i + 1 ) return a
def boothroyd_inverse ( n ): #*****************************************************************************80 # ## BOOTHROYD_INVERSE returns the inverse of the BOOTHROYD matrix. # # Example: # # N = 5 # # 5 -10 10 -5 1 # -15 40 -45 24 -5 # 35 -105 126 -70 15 # -70 224 -280 160 -35 # 126 -420 540 -315 70 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose from r8_mop import r8_mop a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): a[i,j] = r8_mop ( i + j ) * r8_choose ( n + i, i ) \ * r8_choose ( n-1, n-j-1 ) * float ( n ) / float ( i + j + 1 ) return a
def normal_ms_moment(order, mu, sigma): #*****************************************************************************80 # ## NORMAL_MS_MOMENT evaluates a moment of the Normal MS distribution. # # Discussion: # # The formula was posted by John D Cook. # # Order Moment # ----- ------ # 0 1 # 1 mu # 2 mu ** 2 + sigma ** 2 # 3 mu ** 3 + 3 mu sigma ** 2 # 4 mu ** 4 + 6 mu ** 2 sigma ** 2 + 3 sigma ** 4 # 5 mu ** 5 + 10 mu ** 3 sigma ** 2 + 15 mu sigma ** 4 # 6 mu ** 6 + 15 mu ** 4 sigma ** 2 + 45 mu ** 2 sigma ** 4 + 15 sigma ** 6 # 7 mu ** 7 + 21 mu ** 5 sigma ** 2 + 105 mu ** 3 sigma ** 4 + 105 mu sigma ** 6 # 8 mu ** 8 + 28 mu ** 6 sigma ** 2 + 210 mu ** 4 sigma ** 4 + 420 mu ** 2 sigma ** 6 + 105 sigma ** 8 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ORDER, the order of the moment. # # Input, real MU, the mean of the distribution. # # Input, real SIGMA, the standard deviation of the distribution. # # Output, real VALUE, the value of the moment. # from r8_choose import r8_choose from r8_factorial2 import r8_factorial2 j_hi = (order // 2) value = 0.0 for j in range(0, j_hi + 1): value = value \ + r8_choose ( order, 2 * j ) \ * r8_factorial2 ( 2 * j - 1 ) \ * mu ** ( order - 2 * j ) * sigma ** ( 2 * j ) return value
def normal_ms_moment ( order, mu, sigma ): #*****************************************************************************80 # ## NORMAL_MS_MOMENT evaluates the moments of the Normal MS distribution. # # Discussion: # # The formula was posted by John D Cook. # # Order Moment # ----- ------ # 0 1 # 1 mu # 2 mu ** 2 + sigma ** 2 # 3 mu ** 3 + 3 mu sigma ** 2 # 4 mu ** 4 + 6 mu ** 2 sigma ** 2 + 3 sigma ** 4 # 5 mu ** 5 + 10 mu ** 3 sigma ** 2 + 15 mu sigma ** 4 # 6 mu ** 6 + 15 mu ** 4 sigma ** 2 + 45 mu ** 2 sigma ** 4 + 15 sigma ** 6 # 7 mu ** 7 + 21 mu ** 5 sigma ** 2 + 105 mu ** 3 sigma ** 4 + 105 mu sigma ** 6 # 8 mu ** 8 + 28 mu ** 6 sigma ** 2 + 210 mu ** 4 sigma ** 4 + 420 mu ** 2 sigma ** 6 + 105 sigma ** 8 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 05 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ORDER, the order of the moment. # # Input, real MU, the mean of the distribution. # # Input, real SIGMA, the standard deviation of the distribution. # # Output, real VALUE, the value of the moment. # from r8_choose import r8_choose from r8_factorial2 import r8_factorial2 j_hi = ( order // 2 ) value = 0.0 for j in range ( 0, j_hi + 1 ): value = value \ + r8_choose ( order, 2 * j ) \ * r8_factorial2 ( 2 * j - 1 ) \ * mu ** ( order - 2 * j ) * sigma ** ( 2 * j ) return value
def boothroyd_condition ( n ): #*****************************************************************************80 # ## BOOTHROYD_CONDITION computes the L1 condition of the BOOTHROYD matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 April 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of the matrix. # # Output, real VALUE, the L1 condition. # from r8_choose import r8_choose a_norm = 0.0 for j in range ( 0, n ): s = 0.0 for i in range ( 0, n ): s = s + r8_choose ( n + i, i ) * r8_choose ( n - 1, n - j - 1 ) * n \ / ( i + j + 1 ); a_norm = max ( a_norm, s ) b_norm = a_norm value = a_norm * b_norm return value
def lotkin_inverse(n): #*****************************************************************************80 # ## LOTKIN_INVERSE returns the inverse of the LOTKIN matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose from r8_mop import r8_mop a = np.zeros((n, n)) for i in range(0, n): for j in range(0, n): if (j == 0): a[i,j] = r8_mop ( n - i - 1 ) \ * r8_choose ( n + i, i ) \ * r8_choose ( n, i + 1 ) else: a[i,j] = r8_mop ( i - j + 1 ) * float ( i + 1 ) \ * r8_choose ( i + j + 1, j ) \ * r8_choose ( i + j, j - 1 ) \ * r8_choose ( n + i, i + j + 1 ) \ * r8_choose ( n + j, i + j + 1 ) return a
def lotkin_inverse ( n ): #*****************************************************************************80 # ## LOTKIN_INVERSE returns the inverse of the LOTKIN matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose from r8_mop import r8_mop a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): if ( j == 0 ): a[i,j] = r8_mop ( n - i - 1 ) \ * r8_choose ( n + i, i ) \ * r8_choose ( n, i + 1 ) else: a[i,j] = r8_mop ( i - j + 1 ) * float ( i + 1 ) \ * r8_choose ( i + j + 1, j ) \ * r8_choose ( i + j, j - 1 ) \ * r8_choose ( n + i, i + j + 1 ) \ * r8_choose ( n + j, i + j + 1 ) return a
def truncated_normal_ab_moment ( order, mu, sigma, a, b ): #*****************************************************************************80 # ## TRUNCATED_NORMAL_AB_MOMENT: a moment of the truncated Normal distribution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 March 2015 # # Author: # # John Burkardt # # Reference: # # Phoebus Dhrymes, # Moments of Truncated Normal Distributions, # May 2005. # # Parameters: # # Input, integer ORDER, the order of the moment. # 0 <= ORDER. # # Input, real MU, SIGMA, the mean and standard deviation of the # parent Normal distribution. # 0 < S. # # Input, real A, B, the lower and upper truncation limits. # A < B. # # Output, real VALUE, the moment of the PDF. # from normal_01_cdf import normal_01_cdf from normal_01_pdf import normal_01_pdf from r8_choose import r8_choose from sys import exit if ( order < 0 ): print '' print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' print ' ORDER < 0.' exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' ) if ( sigma <= 0.0 ): print '' print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' print ' SIGMA <= 0.0.' exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' ) if ( b <= a ): print '' print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' print ' B <= A.' exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' ) a_h = ( a - mu ) / sigma a_pdf = normal_01_pdf ( a_h ) a_cdf = normal_01_cdf ( a_h ) if ( a_cdf == 0.0 ): print '' print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' print ' PDF/CDF ratio fails, because A_CDF is too small.' print ' A_PDF = %g' % ( a_pdf ) print ' A_CDF = %g' % ( a_cdf ) exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' ) b_h = ( b - mu ) / sigma b_pdf = normal_01_pdf ( b_h ) b_cdf = normal_01_cdf ( b_h ) if ( b_cdf == 0.0 ): print '' print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' print ' PDF/CDF ratio fails, because B_CDF too small.' print ' B_PDF = %g' % ( b_pdf ) print ' B_CDF = %g' % ( b_cdf ) exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' ) value = 0.0 irm2 = 0.0 irm1 = 0.0 for r in range ( 0, order + 1 ): if ( r == 0 ): ir = 1.0 elif ( r == 1 ): ir = - ( b_pdf - a_pdf ) / ( b_cdf - a_cdf ) else: ir = ( r - 1 ) * irm2 \ - ( b_h ** ( r - 1 ) * b_pdf - a_h ** ( r - 1 ) * a_pdf ) \ / ( b_cdf - a_cdf ) value = value + r8_choose ( order, r ) \ * mu ** ( order - r ) \ * sigma ** r * ir irm2 = irm1 irm1 = ir return value
def carry_eigen_right ( n, alpha ): #*****************************************************************************80 # #% CARRY_EIGEN_RIGHT returns the right eigenvectors of the CARRY matrix. # # Formula: # # A(I,J) = sum ( N+1-J) <= K <= N ) # S1(N,K) * C(K,N+1-J) ( N - I )^(K-N+J-1) # # where S1(N,K) is a signed Sterling number of the first kind. # # Example: # # N = 4 # # 1 6 11 6 # 1 2 -1 -2 # 1 -2 -1 2 # 1 -6 11 -6 # # Properties: # # A is generally not symmetric: A' /= A. # # The first column is all 1's. # # The last column is reciprocals of binomial coefficients with # alternating sign multiplied by (N-1). # # The top and bottom rows are the unsigned and signed Stirling numbers # of the first kind. # # The entries in the J-th column are a degree (J-1) polynomial # in the row index I. (Column 1 is constant, the first difference # in column 2 is constant, the second difference in column 3 is # constant, and so on.) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose from stirling import stirling s1 = stirling ( n, n ) a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): for k in range ( n - j, n + 1 ): if ( n - 1 - i == 0 and k - n + j == 0 ): a[i,j] = a[i,j] + s1[n-1,k-1] * r8_choose ( k, n - j ) else: a[i,j] = a[i,j] + s1[n-1,k-1] * r8_choose ( k, n - j ) \ * ( n - i - 1 ) ** ( k - n + j ) return a
def pascal2_inverse ( n ): #*****************************************************************************80 # ## PASCAL2_INVERSE returns the inverse of the PASCAL2 matrix. # # Formula: # # A(I,J) = sum ( max(I,J) <= K <= N ) # (-1)^(J+I) * COMB(K-1,I-1) * COMB(K-1,J-1) # # Example: # # N = 5 # # 5 -10 10 -5 1 # -10 30 -35 19 -4 # 10 -35 46 -27 6 # -5 19 -27 17 -4 # 1 -4 6 -4 1 # # Properties: # # A is symmetric: A' = A. # # Because A is symmetric, it is normal. # # Because A is normal, it is diagonalizable. # # A is integral, therefore det ( A ) is integral, and # det ( A ) * inverse ( A ) is integral. # # The first row sums to 1, the others to 0. # # The first column sums to 1, the others to 0. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the order of A. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose from r8_mop import r8_mop a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): klo = max ( i + 1, j + 1 ) for k in range ( klo, n + 1 ): a[i,j] = a[i,j] + r8_mop ( i + j ) * r8_choose ( k - 1, i ) \ * r8_choose ( k - 1, j ) return a
def zernike_poly_coef ( m, n ): #*****************************************************************************80 # ## ## ZERNIKE_POLY_COEF: coefficients of a Zernike polynomial. # # Discussion: # # With our coefficients stored in COEFS(1:N+1), the # radial function R^M_N(RHO) is given by # # R^M_N(RHO) = COEFS(1) # + COEFS(2) * RHO # + COEFS(3) * RHO^2 # + ... # + COEFS(N+1) * RHO^N # # and the odd and even Zernike polynomials are # # Z^M_N(RHO,PHI,odd) = R^M_N(RHO) * sin(PHI) # Z^M_N(RHO,PHI,even) = R^M_N(RHO) * cos(PHI) # # The first few "interesting" values of R are: # # R^0_0 = 1 # # R^1_1 = RHO # # R^0_2 = 2 * RHO^2 - 1 # R^2_2 = RHO^2 # # R^1_3 = 3 * RHO^3 - 2 * RHO # R^3_3 = RHO^3 # # R^0_4 = 6 * RHO^4 - 6 * RHO^2 + 1 # R^2_4 = 4 * RHO^4 - 3 * RHO^2 # R^4_4 = RHO^4 # # R^1_5 = 10 * RHO^5 - 12 * RHO^3 + 3 * RHO # R^3_5 = 5 * RHO^5 - 4 * RHO^3 # R^5_5 = RHO^5 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 05 October 2005 # # Author: # # John Burkardt # # Reference: # # Eric Weisstein, # Zernike Polynomials, # CRC Concise Encyclopedia of Mathematics, # CRC Press, 1998, # QA5.W45 # # Parameters: # # Input, integer M, N, the parameters of the polynomial. # Normally, 0 <= M <= N and 0 <= N. # # Output, real C(1:N+1), the coefficients of the polynomial. # import numpy as np from r8_choose import r8_choose c = np.zeros ( n + 1 ) if ( n < 0 ): return c if ( m < 0 ): return c if ( n < m ): return c if ( ( ( m - n ) % 2 ) == 1 ): return c nm_plus = ( ( m + n ) // 2 ) nm_minus = ( ( n - m ) // 2 ) c[n] = r8_choose ( n, nm_plus ) for l in range ( 0, nm_minus ): c[n-2*l-2] = - float ( ( nm_plus - l ) * ( nm_minus - l ) ) * c[n-2*l] \ / float ( ( n - l ) * ( l + 1 ) ) return c
def carry_eigen_right(n, alpha): #*****************************************************************************80 # #% CARRY_EIGEN_RIGHT returns the right eigenvectors of the CARRY matrix. # # Formula: # # A(I,J) = sum ( N+1-J) <= K <= N ) # S1(N,K) * C(K,N+1-J) ( N - I )^(K-N+J-1) # # where S1(N,K) is a signed Sterling number of the first kind. # # Example: # # N = 4 # # 1 6 11 6 # 1 2 -1 -2 # 1 -2 -1 2 # 1 -6 11 -6 # # Properties: # # A is generally not symmetric: A' /= A. # # The first column is all 1's. # # The last column is reciprocals of binomial coefficients with # alternating sign multiplied by (N-1). # # The top and bottom rows are the unsigned and signed Stirling numbers # of the first kind. # # The entries in the J-th column are a degree (J-1) polynomial # in the row index I. (Column 1 is constant, the first difference # in column 2 is constant, the second difference in column 3 is # constant, and so on.) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 March 2015 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose from stirling import stirling s1 = stirling(n, n) a = np.zeros((n, n)) for i in range(0, n): for j in range(0, n): for k in range(n - j, n + 1): if (n - 1 - i == 0 and k - n + j == 0): a[i, j] = a[i, j] + s1[n - 1, k - 1] * r8_choose(k, n - j) else: a[i,j] = a[i,j] + s1[n-1,k-1] * r8_choose ( k, n - j ) \ * ( n - i - 1 ) ** ( k - n + j ) return a
def carry(n, alpha): #*****************************************************************************80 # ## CARRY returns the CARRY matrix. # # Discussion: # # We assume that arithmetic is being done in base ALPHA. We are adding # a column of N digits base ALPHA, as part of adding N random numbers. # We know the carry digit, between 0 and N-1, that is being carried into the # column sum (the incarry digit), and we want to know the probability of # the various carry digits 0 through N-1 (the outcarry digit) that could # be carried out of the column sum. # # The carry matrix summarizes this data. The entry A(I,J) represents # the probability that, given that the incarry digit is I-1, the # outcarry digit will be J-1. # # Formula: # # A(I,J) = ( 1 / ALPHA )^N * sum ( 0 <= K <= J-1 - floor ( I-1 / ALPHA ) ) # (-1)^K * C(N+1,K) * C(N-I+(J-K)*ALPHA, N ) # # Example: # # ALPHA = 10, N = 4 # # 0.0715 0.5280 0.3795 0.0210 # 0.0495 0.4840 0.4335 0.0330 # 0.0330 0.4335 0.4840 0.0495 # 0.0210 0.3795 0.5280 0.0715 # # Square Properties: # # A is generally not symmetric: A' /= A. # # A is a Markov matrix. # # A is centrosymmetric: A(I,J) = A(N+1-I,N+1-J). # # LAMBDA(I) = 1 / ALPHA^(I-1) # # det ( A ) = 1 / ALPHA^((N*(N-1))/2) # # The eigenvectors do not depend on ALPHA. # # A is generally not normal: A' * A /= A * A'. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 December 2014 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # from r8_choose import r8_choose import numpy as np a = np.zeros([n, n]) for j in range(0, n): for i in range(0, n): temp = 0.0 s = -1.0 for k in range(0, j - (i // alpha) + 1): s = -s c1 = r8_choose(n + 1, k) c2 = r8_choose(n - i - 1 + (j + 1 - k) * alpha, n) temp = temp + s * c1 * c2 a[i, j] = temp / alpha**n return a
def carry ( n, alpha ): #*****************************************************************************80 # ## CARRY returns the CARRY matrix. # # Discussion: # # We assume that arithmetic is being done in base ALPHA. We are adding # a column of N digits base ALPHA, as part of adding N random numbers. # We know the carry digit, between 0 and N-1, that is being carried into the # column sum (the incarry digit), and we want to know the probability of # the various carry digits 0 through N-1 (the outcarry digit) that could # be carried out of the column sum. # # The carry matrix summarizes this data. The entry A(I,J) represents # the probability that, given that the incarry digit is I-1, the # outcarry digit will be J-1. # # Formula: # # A(I,J) = ( 1 / ALPHA )^N * sum ( 0 <= K <= J-1 - floor ( I-1 / ALPHA ) ) # (-1)^K * C(N+1,K) * C(N-I+(J-K)*ALPHA, N ) # # Example: # # ALPHA = 10, N = 4 # # 0.0715 0.5280 0.3795 0.0210 # 0.0495 0.4840 0.4335 0.0330 # 0.0330 0.4335 0.4840 0.0495 # 0.0210 0.3795 0.5280 0.0715 # # Square Properties: # # A is generally not symmetric: A' /= A. # # A is a Markov matrix. # # A is centrosymmetric: A(I,J) = A(N+1-I,N+1-J). # # LAMBDA(I) = 1 / ALPHA^(I-1) # # det ( A ) = 1 / ALPHA^((N*(N-1))/2) # # The eigenvectors do not depend on ALPHA. # # A is generally not normal: A' * A /= A * A'. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 December 2014 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # from r8_choose import r8_choose import numpy as np a = np.zeros ( [ n, n ] ) for j in range ( 0, n ): for i in range ( 0, n ): temp = 0.0 s = -1.0 for k in range ( 0, j - ( i // alpha ) + 1 ): s = - s c1 = r8_choose ( n + 1, k ) c2 = r8_choose ( n - i - 1 + ( j + 1 - k ) * alpha, n ) temp = temp + s * c1 * c2 a[i,j] = temp / alpha ** n return a
def truncated_normal_b_moment(order, mu, sigma, b): #*****************************************************************************80 # ## TRUNCATED_NORMAL_B_MOMENT: a moment of upper truncated Normal distribution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 March 2015 # # Author: # # John Burkardt # # Reference: # # Phoebus Dhrymes, # Moments of Truncated Normal Distributions, # May 2005. # # Parameters: # # Input, integer ORDER, the order of the moment. # 0 <= ORDER. # # Input, real MU, SIGMA, the mean and standard deviation of the # parent Normal distribution. # 0 < S. # # Input, real B, the upper truncation limit. # # Output, real VALUE, the moment of the PDF. # from normal_01_cdf import normal_01_cdf from normal_01_pdf import normal_01_pdf from r8_choose import r8_choose from sys import exit if (order < 0): print '' print 'TRUNCATED_NORMAL_B_MOMENT - Fatal error!' print ' ORDER < 0.' exit('TRUNCATED_NORMAL_B_MOMENT - Fatal error!') if (sigma <= 0.0): print '' print 'TRUNCATED_NORMAL_B_MOMENT - Fatal error!' print ' SIGMA <= 0.0.' exit('TRUNCATED_NORMAL_B_MOMENT - Fatal error!') b_h = (b - mu) / sigma b_pdf = normal_01_pdf(b_h) b_cdf = normal_01_cdf(b_h) if (b_cdf == 0.0): print '' print 'TRUNCATED_NORMAL_B_MOMENT - Fatal error!' print ' PDF/CDF ratio fails, because B_CDF too small.' print ' B_PDF = %g' % (b_pdf) print ' B_CDF = %g' % (b_cdf) exit('TRUNCATED_NORMAL_B_MOMENT - Fatal error!') f = b_pdf / b_cdf value = 0.0 irm2 = 0.0 irm1 = 0.0 for r in range(0, order + 1): if (r == 0): ir = 1.0 elif (r == 1): ir = -f else: ir = -b_h**(r - 1) * f + (r - 1) * irm2 value = value + r8_choose ( order, r ) \ * mu ** ( order - r ) \ * sigma ** r * ir irm2 = irm1 irm1 = ir return value
def carry_eigen_left(n, alpha): #*****************************************************************************80 # #% CARRY_EIGEN_LEFT returns the left eigenvectors for the CARRY matrix. # # Formula: # # A(I,J) = sum ( 0 <= K <= J-1 ) # (-1)^K * C(N+1,K) * ( J - K )^(N+1-I) # # Example: # # N = 4 # # 1 11 11 1 # 1 3 -3 -1 # 1 -1 -1 1 # 1 -3 3 -1 # # Properties: # # A is generally not symmetric: A' /= A. # # Column 1 is all 1's, and column N is (-1)^(I+1). # # The top row is proportional to a row of Eulerian numbers, and # can be normalized to represent the stationary probablities # for the carrying process when adding N random numbers. # # The bottom row is proportional to a row of Pascal's triangle, # with alternating signs. # # The product of the left and right eigenvector matrices of # order N is N! times the identity. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose a = np.zeros((n, n)) for i in range(0, n): for j in range(0, n): s = -1.0 for k in range(0, j + 1): s = -s a[i, j] = a[i, j] + s * r8_choose(n + 1, k) * (j + 1 - k)**(n - i) return a
def carry_eigen_left ( n, alpha ): #*****************************************************************************80 # #% CARRY_EIGEN_LEFT returns the left eigenvectors for the CARRY matrix. # # Formula: # # A(I,J) = sum ( 0 <= K <= J-1 ) # (-1)^K * C(N+1,K) * ( J - K )^(N+1-I) # # Example: # # N = 4 # # 1 11 11 1 # 1 3 -3 -1 # 1 -1 -1 1 # 1 -3 3 -1 # # Properties: # # A is generally not symmetric: A' /= A. # # Column 1 is all 1's, and column N is (-1)^(I+1). # # The top row is proportional to a row of Eulerian numbers, and # can be normalized to represent the stationary probablities # for the carrying process when adding N random numbers. # # The bottom row is proportional to a row of Pascal's triangle, # with alternating signs. # # The product of the left and right eigenvector matrices of # order N is N! times the identity. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 March 2015 # # Author: # # John Burkardt # # Reference: # # John Holte, # Carries, Combinatorics, and an Amazing Matrix, # The American Mathematical Monthly, # February 1997, pages 138-149. # # Parameters: # # Input, integer N, the order of the matrix. # # Input, integer ALPHA, the numeric base used in the addition. # # Output, real A(N,N), the matrix. # import numpy as np from r8_choose import r8_choose a = np.zeros ( ( n, n ) ) for i in range ( 0, n ): for j in range ( 0, n ): s = -1.0 for k in range ( 0, j + 1 ): s = - s a[i,j] = a[i,j] + s * r8_choose ( n + 1, k ) * ( j + 1 - k ) ** ( n - i ) return a