#! /usr/bin/env python # def chebyshev_even1 ( n, f ): #*****************************************************************************80 # ## CHEBYSHEV_EVEN1 returns the even Chebyshev coefficients of F. # # Discussion: # # The coefficients are calculated using the extreme points of Tn(x). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 January 2016 # # Author: # # John Burkardt # # Reference: # # D B Hunter, H V Smith, # A quadrature formula of Clenshaw-Curtis type for the Gegenbauer weight function, # Journal of Computational and Applied Mathematics, # Volume 177, 2005, pages 389-400. # # Parameters: # # Input, integer N, the number of points to use. # 1 <= N. # # Input, real F(x), the handle for the function to be integrated with the # Gegenbauer weight. # # Output, real A2(1+N/2), the even Chebyshev coefficients of F. # import numpy as np from r8_mop import r8_mop s = ( n // 2 ) sigma = ( n % 2 ) a2 = np.zeros ( s + 1 ) for r in range ( 0, 2 * s + 1, 2 ): total = 0.5 * f ( 1.0 ) for j in range ( 1, n ): total = total + f ( np.cos ( float ( j ) * np.pi / float ( n ) ) ) \ * np.cos ( float ( r * j ) * np.pi / float ( n ) ) total = total + 0.5 * r8_mop ( r ) * f ( -1.0 ) rh = ( r // 2 ) a2[rh] = ( 2.0 / float ( n ) ) * total return a2 def chebyshev_even1_test ( ): #*****************************************************************************80 # ## CHEBYSHEV_EVEN1_TEST tests CHEBYSHEV_EVEN1. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 January 2016 # # Author: # # John Burkardt # import numpy as np import platform from r8vec2_print import r8vec2_print print ( '' ) print ( 'CHEBYSHEV_EVEN1_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CHEBYSHEV_EVEN1 computes the even Chebyshev coefficients' ) print ( ' of a function F, using the extreme points of Tn(x).' ) a2_exact = np.array ( [ \ 0.4477815660, \ -0.7056685603, \ 0.0680357987, \ -0.0048097159 ] ) lam = 0.75 a = 2.0 n = 6 a2 = chebyshev_even1 ( n, f ) s = ( n // 2 ) r8vec2_print ( s + 1, a2, a2_exact, ' Computed and Exact Coefficients:' ) # # Terminate. # print ( '' ) print ( 'CHEBYSHEV_EVEN1_TEST' ) print ( ' Normal end of execution.' ) return def f ( x ): #*****************************************************************************80 # ## F defines the function whose Fourier coefficients are desired. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 January 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real X, the argument. # # Output, real VALUE, the value. # import numpy as np a = 2.0 value = np.cos ( a * x ) return value if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) chebyshev_even1_test ( ) timestamp ( )