#! /usr/bin/env python # def cosine_transform_data ( n, d ): #*****************************************************************************80 # ## COSINE_TRANSFORM_DATA does a cosine transform on a vector of data. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 August 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the number of data points. # # Input, real D(N), the vector of data. # # Output, real C(N), the cosine transform coefficients. # import numpy as np c = np.zeros ( n ) for i in range ( 0, n ): for j in range ( 0, n ): angle = np.pi * float ( 2 * j + 1 ) * float ( i ) / 2.0 / float ( n ) c[i] = c[i] + d[j] * np.cos ( angle ) c[i] = c[i] * np.sqrt ( 2.0 / float ( n ) ) return c def cosine_transform_inverse ( n, c ): #*****************************************************************************80 # ## COSINE_TRANSFORM_INVERSE does an inverse cosine transform. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 August 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the number of data points. # # Input, real C(N), the cosine transform coefficients. # # Output, real D(N), the vector of data. # import numpy as np d = np.zeros ( n ) for i in range ( 0, n ): d[i] = c[0] / 2.0 for j in range ( 1, n ): d[i] = d[i] + np.cos ( np.pi * float ( 2 * i + 1 ) \ * float ( j ) / 2.0 / float ( n ) ) * c[j] d[i] = d[i] * np.sqrt ( 2.0 / float ( n ) ) return d def cosine_transform_data_test ( ): #*****************************************************************************80 # ## COSINE_TRANSFORM_DATA_TEST carries out a DCT and its inverse. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 August 2015 # # Author: # # John Burkardt # import platform n = 10 print ( '' ) print ( 'COSINE_TRANSFORM_DATA_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' COSINE_TRANSFORM_DATA does a cosine transform of data' ) print ( ' defined by a vector.' ) print ( '' ) print ( ' Apply the transform, then its inverse.' ) print ( ' Let R be a random N vector.' ) print ( ' Let S be the transform of D.' ) print ( ' Let T be the transform of E.' ) print ( ' Then R and T will be equal.' ) seed = 123456789 r, seed = r8vec_uniform_01 ( n, seed ) s = cosine_transform_data ( n, r ) t = cosine_transform_inverse ( n, s ) print ( '' ) print ( ' I R(I) S(I) T(I)' ) print ( '' ) for i in range ( 0, n ): print ( ' %4d %10f %10f %10f' % ( i, r[i], s[i], t[i] ) ) # # Terminate. # print ( '' ) print ( 'COSINE_TRANSFORM_DATA_TEST' ) print ( ' Normal end of execution.' ) return def cosine_transform_test ( ): #*****************************************************************************80 # ## COSINE_TRANSFORM_TEST tests the COSINE_TRANSFORM library. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 August 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'COSINE_TRANSFORM_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test the COSINE_TRANSFORM library.' ) r8vec_print_test ( ) r8vec_uniform_01_test ( ) cosine_transform_data_test ( ) # # Terminate. # print ( '' ) print ( 'COSINE_TRANSFORM_TEST:' ) print ( ' Normal end of execution.' ) return def r8vec_print ( n, a, title ): #*****************************************************************************80 # ## R8VEC_PRINT prints an R8VEC. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the dimension of the vector. # # Input, real A(N), the vector to be printed. # # Input, string TITLE, a title. # print ( '' ) print ( title ) print ( '' ) for i in range ( 0, n ): print ( '%6d: %12g' % ( i, a[i] ) ) return def r8vec_print_test ( ): #*****************************************************************************80 # ## R8VEC_PRINT_TEST tests R8VEC_PRINT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 29 October 2014 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'R8VEC_PRINT_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' R8VEC_PRINT prints an R8VEC.' ) n = 4 v = np.array ( [ 123.456, 0.000005, -1.0E+06, 3.14159265 ], dtype = np.float64 ) r8vec_print ( n, v, ' Here is an R8VEC:' ) # # Terminate. # print ( '' ) print ( 'R8VEC_PRINT_TEST:' ) print ( ' Normal end of execution.' ) return def r8vec_uniform_01 ( n, seed ): #*****************************************************************************80 # ## R8VEC_UNIFORM_01 returns a unit pseudorandom R8VEC. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # # Reference: # # Paul Bratley, Bennett Fox, Linus Schrage, # A Guide to Simulation, # Second Edition, # Springer, 1987, # ISBN: 0387964673, # LC: QA76.9.C65.B73. # # Bennett Fox, # Algorithm 647: # Implementation and Relative Efficiency of Quasirandom # Sequence Generators, # ACM Transactions on Mathematical Software, # Volume 12, Number 4, December 1986, pages 362-376. # # Pierre L'Ecuyer, # Random Number Generation, # in Handbook of Simulation, # edited by Jerry Banks, # Wiley, 1998, # ISBN: 0471134031, # LC: T57.62.H37. # # Peter Lewis, Allen Goodman, James Miller, # A Pseudo-Random Number Generator for the System/360, # IBM Systems Journal, # Volume 8, Number 2, 1969, pages 136-143. # # Parameters: # # Input, integer N, the number of entries in the vector. # # Input, integer SEED, a seed for the random number generator. # # Output, real X(N), the vector of pseudorandom values. # # Output, integer SEED, an updated seed for the random number generator. # import numpy as np from sys import exit i4_huge = 2147483647; seed = int ( seed ) if ( seed < 0 ): seed = seed + i4_huge if ( seed == 0 ): print ( '' ) print ( 'R8VEC_UNIFORM_01 - Fatal error!' ) print ( ' Input SEED = 0!' ) exit ( 'R8VEC_UNIFORM_01 - Fatal error!' ) x = np.zeros ( n ); for i in range ( 0, n ): k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ): seed = seed + i4_huge x[i] = seed * 4.656612875E-10 return x, seed def r8vec_uniform_01_test ( ): #*****************************************************************************80 # ## R8VEC_UNIFORM_01_TEST tests R8VEC_UNIFORM_01. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 29 October 2014 # # Author: # # John Burkardt # import numpy as np import platform n = 10 seed = 123456789 print ( '' ) print ( 'R8VEC_UNIFORM_01_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' R8VEC_UNIFORM_01 computes a random R8VEC.' ) print ( '' ) print ( ' Initial seed is %d' % ( seed ) ) v, seed = r8vec_uniform_01 ( n, seed ) r8vec_print ( n, v, ' Random R8VEC:' ) # # Terminate. # print ( '' ) print ( 'R8VEC_UNIFORM_01_TEST:' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## TIMESTAMP prints the date as a timestamp. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # # Parameters: # # None # import time t = time.time ( ) print ( time.ctime ( t ) ) return None def timestamp_test ( ): #*****************************************************************************80 # ## TIMESTAMP_TEST tests TIMESTAMP. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 03 December 2014 # # Author: # # John Burkardt # # Parameters: # # None # import platform print ( '' ) print ( 'TIMESTAMP_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' TIMESTAMP prints a timestamp of the current date and time.' ) print ( '' ) timestamp ( ) # # Terminate. # print ( '' ) print ( 'TIMESTAMP_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): timestamp ( ) cosine_transform_test ( ) timestamp ( )