#! /usr/bin/env python # def u_polynomial ( m, n, x ): #*****************************************************************************80 # ## U_POLYNOMIAL evaluates Chebyshev polynomials U(n,x). # # Differential equation: # # (1-X*X) Y'' - 3 X Y' + N (N+2) Y = 0 # # First terms: # # U(0,X) = 1 # U(1,X) = 2 X # U(2,X) = 4 X^2 - 1 # U(3,X) = 8 X^3 - 4 X # U(4,X) = 16 X^4 - 12 X^2 + 1 # U(5,X) = 32 X^5 - 32 X^3 + 6 X # U(6,X) = 64 X^6 - 80 X^4 + 24 X^2 - 1 # U(7,X) = 128 X^7 - 192 X^5 + 80 X^3 - 8X # # Recursion: # # U(0,X) = 1, # U(1,X) = 2 * X, # U(N,X) = 2 * X * U(N-1,X) - U(N-2,X) # # Norm: # # Integral ( -1 <= X <= 1 ) ( 1 - X^2 ) * U(N,X)^2 dX = PI/2 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 April 2012 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, the number of evaluation points. # # Input, integer N, the highest polynomial to compute. # # Input, real X(M,1), the evaluation points. # # Output, real V(1:M,1:N+1), the values of the N+1 Chebyshev polynomials. # import numpy as np from sys import exit if ( n < 0 ): print ( '' ) print ( 'U_POLYNOMIAL - Fatal error!' ) print ( ' N < 0' ) exit ( 'U_POLYNOMIAL - Fatal error.' ) v = np.zeros ( [ m, n+1 ] ) for i in range ( 0, m ): v[i,0] = 1.0 if ( n < 1 ): return v for i in range ( 0, m ): v[i,1] = 2.0 * x[i] for i in range ( 0, m ): for j in range ( 2, n + 1 ): v[i,j] = 2.0 * x[i] * v[i,j-1] - v[i,j-2] return v def u_polynomial_test ( ): #*****************************************************************************80 # ## U_POLYNOMIAL_TEST tests U_POLYNOMIAL. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 July 2015 # # Author: # # John Burkardt # import numpy as np import platform from u_polynomial_values import u_polynomial_values print ( '' ) print ( 'U_POLYNOMIAL_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' U_POLYNOMIAL evaluates the Chebyshev polynomial U(n,x).' ) print ( '' ) print ( ' Tabulated Computed' ) print ( ' N X U(n,x) U(n,x) Error' ) print ( '' ) n_data = 0 x_vec = np.zeros ( 1 ) while ( True ): n_data, n, x, fx1 = u_polynomial_values ( n_data ) if ( n_data == 0 ): break if ( n < 0 ): continue x_vec[0] = x fx2_vec = u_polynomial ( 1, n, x_vec ) fx2 = fx2_vec[0,n] e = fx1 - fx2 print ( ' %4d %12g %24.16g %24.16g %8.2g' % ( n, x, fx1, fx2, e ) ) # # Terminate. # print ( '' ) print ( 'U_POLYNOMIAL_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) u_polynomial_test ( ) timestamp ( )