#! /usr/bin/env python # def legendre_ek_compute ( n ): #*****************************************************************************80 # ## LEGENDRE_EK_COMPUTE: Gauss-Legendre, Elhay-Kautsky method. # # Discussion: # # The integral: # # integral ( -1 < x < +1 ) f(x) dx # # The quadrature rule: # # sum ( 1 <= i <= n ) w(i) * f ( x(i) ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 June 2015 # # Author: # # John Burkardt. # # Reference: # # Sylvan Elhay, Jaroslav Kautsky, # Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of # Interpolatory Quadrature, # ACM Transactions on Mathematical Software, # Volume 13, Number 4, December 1987, pages 399-415. # # Parameters: # # Input, integer N, the number of abscissas. # # Output, real X(N), the abscissas. # # Output, real W(N), the weights. # import numpy as np from imtqlx import imtqlx # # Define the zero-th moment. # zemu = 2.0 # # Define the Jacobi matrix. # bj = np.zeros ( n ) for i in range ( 0, n ): ip1_r8 = float ( i + 1 ) bj[i] = ip1_r8 * ip1_r8 / ( 4.0 * ip1_r8 * ip1_r8 - 1.0 ) bj[i] = np.sqrt ( bj[i] ) x = np.zeros ( n ) w = np.zeros ( n ) w[0] = np.sqrt ( zemu ) # # Diagonalize the Jacobi matrix. # x, w = imtqlx ( n, x, bj, w ) for i in range ( 0, n ): w[i] = w[i] ** 2 return x, w def legendre_ek_compute_test ( ): #*****************************************************************************80 # ## LEGENDRE_EK_COMPUTE_TEST tests LEGENDRE_EK_COMPUTE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 June 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'LEGENDRE_EK_COMPUTE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' LEGENDRE_EK_COMPUTE computes' ) print ( ' a Legendre quadrature rule' ) print ( ' using the Elhay-Kautsky algorithm.' ) print ( '' ) print ( ' Index X W' ) for n in range ( 1, 11 ): x, w = legendre_ek_compute ( n ) print ( '' ) for i in range ( 0, n ): print ( ' %2d %24.16g %24.16g' % ( i, x[i], w[i] ) ) # # Terminate. # print ( '' ) print ( 'LEGENDRE_EK_COMPUTE_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) legendre_ek_compute_test ( ) timestamp ( )