#! /usr/bin/env python # def grid_generate ( m, n, center, seed ): #*****************************************************************************80 # ## GRID_GENERATE generates a grid dataset. # # Discussion: # # N points are needed in an M dimensional space. # # The points are to lie on a uniform grid of side N_SIDE. # # Unless the N = N_SIDE^M for some N_SIDE, we can't use all the # points on a grid. What we do is find the smallest N_SIDE # that's big enough, and randomly omit some points. # # If N_SIDE is 4, then the choices in 1D are: # # A: 0, 1/3, 2/3, 1 # B: 1/5, 2/5, 3/5, 4/5 # C: 0, 1/4, 2/4, 3/4 # D: 1/4, 2/4, 3/4, 1 # E: 1/8, 3/8, 5/8, 7/8 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 August 2016 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, the spatial dimension. # # Input, integer N, the number of points to generate. # # Input, integer CENTER, specifies the 1D grid centering: # 1: first point is 0.0, last point is 1.0 # 2: first point is 1/(N+1), last point is N/(N+1) # 3: first point is 0, last point is (N-1)/N # 4: first point is 1/N, last point is 1 # 5: first point is 1/(2*N), last point is (2*N-1)/(2*N) # # Input, integer SEED, the random number seed. # # Output, real R(M,N), the points. # # Output, integer SEED, the updated random number seed. # import numpy as np from grid_side import grid_side from ksub_random2 import ksub_random2 from tuple_next_fast import tuple_next_fast # # Find the dimension of the smallest grid with N points. # n_side = grid_side ( m, n ) # # We need to select N points out of N_SIDE^M set. # n_grid = n_side ** m # # Generate a random subset of N items from a set of size N_GRID. # rank_list, seed = ksub_random2 ( n_grid, n, seed ) # # Must make one dummy call to TUPLE_NEXT_FAST with RANK = 0. # base = np.zeros ( m ) rank = -1 tuple, base = tuple_next_fast ( n_side, m, rank, base ) # # Now generate the appropriate indices, and "center" them. # r = np.zeros ( [ m, n ] ) for j in range ( 0, n ): rank = rank_list[j] - 1 tuple, base = tuple_next_fast ( n_side, m, rank, base ) for i in range ( 0, m ): if ( center == 1 ): r[i,j] = float ( tuple[i] - 1 ) / float ( n_side - 1 ) elif ( center == 2 ): r[i,j] = float ( tuple[i] ) / float ( n_side + 1 ) elif ( center == 3 ): r[i,j] = float ( tuple[i] - 1 ) / float ( n_side ) elif ( center == 4 ): r[i,j] = float ( tuple[i] ) / float ( n_side ) elif ( center == 5 ): r[i,j] = float ( 2 * tuple[i] - 1 ) / float ( 2 * n_side ) return r, seed def grid_generate_test ( center, seed ): #*****************************************************************************80 # ## GRID_GENERATE_TEST tests GRID_GENERATE. # # Licensing: # # This code is distributed under the GNU LGPL license. # import platform from r8mat_transpose_print import r8mat_transpose_print m = 2 n = 10 print ( '' ) print ( 'GRID_GENERATE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' GRID_GENERATE randomly chooses a given number of' ) print ( ' points on a uniform grid.' ) print ( '' ) print ( ' Spatial dimension = %d' % ( m ) ) print ( ' Number of points = %d' % ( n ) ) print ( ' Random number SEED = %d' % ( seed ) ) print ( ' Centering option = %d' % ( center ) ) x, seed = grid_generate ( m, n, center, seed ) r8mat_transpose_print ( m, n, x, ' Grid points:' ) # # Terminate. # print ( '' ) print ( 'GRID_GENERATE_TEST:' ) print ( ' Normal end of execution.' ) return def grid_generate_tests ( ): #*****************************************************************************80 # ## GRID_GENERATE_TESTS tests GRID_GENERATE_TEST. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 August 2016 # # Author: # # John Burkardt # print ( '' ) print ( 'GRID_GENERATE_TESTS:' ) print ( ' GRID_GENERATE_TEST generates a specific grid.' ) center = 1 seed = 123456789 grid_generate_test ( center, seed ) print ( '' ) print ( ' Repeat with a different seed from the first run.' ) seed = 987654321 grid_generate_test ( center, seed ) print ( '' ) print ( ' Repeat with the same seed as the first run.' ) seed = 123456789 grid_generate_test ( center, seed ) print ( '' ) print ( ' Repeat with different centering values.' ) for center in range ( 1, 6 ): seed = 123456789 grid_generate_test ( center, seed ) # # Terminate. # print ( '' ) print ( 'GRID_GENERATE_TESTS:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) grid_generate_tests ( ) timestamp ( )