Example #1
0
def bvec_sub_test ( ):

#*****************************************************************************80
#
## BVEC_SUB_TEST tests BVEC_SUB.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    25 December 2014
#
#  Author:
#
#    John Burkardt
#
  from bvec_to_i4 import bvec_to_i4
  from i4_to_bvec import i4_to_bvec
  from i4_uniform_ab import i4_uniform_ab

  n = 10
  seed = 123456789
  test_num = 10

  print ''
  print 'BVEC_SUB_TEST'
  print '  BVEC_SUB subtracts binary vectors representing integers;'
  print ''
  print '        I        J        K = I - J   Kb = Ib - Jb'
  print ''

  for test in range ( 0, test_num ):
    
    i, seed = i4_uniform_ab ( -100, 100, seed )
    j, seed = i4_uniform_ab ( -100, 100, seed )

    print '  %8d  %8d' % ( i, j ),

    k = i - j

    print '  %8d' % ( k ),

    bvec1 = i4_to_bvec ( i, n )

    bvec2 = i4_to_bvec ( j, n )

    bvec3 = bvec_sub ( n, bvec1, bvec2 )

    k = bvec_to_i4 ( n, bvec3 )

    print '  %8d' % ( k )

  print ''
  print 'BVEC_SUB_TEST:'
  print '  Normal end of execution.'

  return
Example #2
0
def bvec_add_test():

    #*****************************************************************************80
    #
    ## BVEC_ADD_TEST tests BVEC_ADD.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    24 December 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    from bvec_to_i4 import bvec_to_i4
    from i4_to_bvec import i4_to_bvec
    from i4_uniform_ab import i4_uniform_ab

    n = 10
    seed = 123456789
    test_num = 10

    print ''
    print 'BVEC_ADD_TEST'
    print '  BVEC_ADD adds binary vectors representing integers;'
    print ''
    print '        I        J        K = I + J   Kb = Ib + Jb'
    print ''

    for test in range(0, test_num):

        i, seed = i4_uniform_ab(-100, 100, seed)
        j, seed = i4_uniform_ab(-100, 100, seed)

        print '  %8d  %8d' % (i, j),

        k = i + j

        print '  %8d' % (k),

        bvec1 = i4_to_bvec(i, n)

        bvec2 = i4_to_bvec(j, n)

        bvec3, overflow = bvec_add(n, bvec1, bvec2)

        k = bvec_to_i4(n, bvec3)

        print '  %8d' % (k)

    print ''
    print 'BVEC_ADD_TEST:'
    print '  Normal end of execution.'

    return
def bvec_mul_test ( ):

#*****************************************************************************80
#
## BVEC_MUL_TEST tests BVEC_MUL.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    05 January 2015
#
#  Author:
#
#    John Burkardt
#
  from bvec_to_i4 import bvec_to_i4
  from i4_to_bvec import i4_to_bvec
  from i4_uniform_ab import i4_uniform_ab

  n = 15
  seed = 123456789
  test_num = 10

  print ''
  print 'BVEC_MUL_TEST'
  print '  BVEC_MUL multiplies binary vectors representing integers;'
  print ''
  print '        I        J        I * J   BVEC_MUL'
  print ''

  for test in range ( 0, test_num ):
    
    i, seed = i4_uniform_ab ( -100, 100, seed )
    j, seed = i4_uniform_ab ( -100, 100, seed )

    print '  %8d  %8d' % ( i, j ),

    k = i * j

    print '  %8d' % ( k ),

    bvec1 = i4_to_bvec ( i, n )
    bvec2 = i4_to_bvec ( j, n )
    bvec3 = bvec_mul ( n, bvec1, bvec2 )
    l = bvec_to_i4 ( n, bvec3 )

    print '  %8d' % ( l )

  print ''
  print 'BVEC_MUL_TEST:'
  print '  Normal end of execution.'

  return
def i4_divp_test():

    #*****************************************************************************80
    #
    ## I4_DIVP_TEST tests I4_DIVP.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    08 May 2013
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_divp import i4_divp
    from i4_uniform_ab import i4_uniform_ab

    a_hi = 100
    a_lo = -100
    b_hi = 10
    b_lo = -10
    test_num = 20

    print ''
    print 'I4_DIVP_TEST'
    print '  I4_DIVP(A,B) returns the smallest multiplier of J'
    print '  that is less than I'
    print ''
    print '     A     B     C     D'
    print ''

    seed = 123456789

    for test in range(1, test_num + 1):
        [a, seed] = i4_uniform_ab(a_lo, a_hi, seed)
        [b, seed] = i4_uniform_ab(b_lo, b_hi, seed)
        if (b == 0):
            b = 7
        c = i4_divp(a, b)
        d = c * b
        print '  %4d  %4d  %4d  %4d' % (a, b, c, d)


#
#  Terminate.
#
    print ''
    print 'I4_DIVP_TEST'
    print '  Normal end of execution.'

    return
Example #5
0
def i4_divp_test ( ) :

#*****************************************************************************80
#
## I4_DIVP_TEST tests I4_DIVP.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    08 May 2013
#
#  Author:
#
#    John Burkardt
#
  from i4_divp import i4_divp
  from i4_uniform_ab import i4_uniform_ab

  a_hi =  100
  a_lo = -100
  b_hi =  10
  b_lo = -10
  test_num = 20

  print ''
  print 'I4_DIVP_TEST'
  print '  I4_DIVP(A,B) returns the smallest multiplier of J'
  print '  that is less than I'
  print ''
  print '     A     B     C     D'
  print ''

  seed = 123456789

  for test in range ( 1, test_num + 1 ):
    [ a, seed ] = i4_uniform_ab ( a_lo, a_hi, seed )
    [ b, seed ] = i4_uniform_ab ( b_lo, b_hi, seed )
    if ( b == 0 ):
      b = 7 
    c = i4_divp ( a, b )
    d = c * b
    print '  %4d  %4d  %4d  %4d' % ( a, b, c, d )
#
#  Terminate.
#
  print ''
  print 'I4_DIVP_TEST'
  print '  Normal end of execution.'

  return
Example #6
0
def ubvec_xor_test():

    #*****************************************************************************80
    #
    ## UBVEC_XOR_TEST tests UBVEC_XOR;
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    26 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab
    from ui4_to_ubvec import ui4_to_ubvec
    from ubvec_to_ui4 import ubvec_to_ui4

    n = 10
    seed = 123456789
    test_num = 10

    print ''
    print 'UBVEC_XOR_TEST'
    print '  UBVEC_XOR exclusive-ors unsigned binary vectors representing'
    print '  unsigned integers;'
    print ''
    print '        I        J        K = I XOR J'
    print ''

    for test in range(0, test_num):
        i, seed = i4_uniform_ab(0, 100, seed)
        j, seed = i4_uniform_ab(0, 100, seed)
        bvec1 = ui4_to_ubvec(i, n)
        bvec2 = ui4_to_ubvec(j, n)
        bvec3 = ubvec_xor(n, bvec1, bvec2)
        k = ubvec_to_ui4(n, bvec3)
        print '  %8d  %8d  %8d' % (i, j, k)


#
#  Terminate.
#
    print ''
    print 'UBVEC_XOR_TEST'
    print '  Normal end of execution.'

    return
Example #7
0
def ubvec_xor_test ( ):

#*****************************************************************************80
#
## UBVEC_XOR_TEST tests UBVEC_XOR;
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 May 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab
  from ui4_to_ubvec import ui4_to_ubvec
  from ubvec_to_ui4 import ubvec_to_ui4

  n = 10
  seed = 123456789
  test_num = 10

  print ''
  print 'UBVEC_XOR_TEST'
  print '  UBVEC_XOR exclusive-ors unsigned binary vectors representing'
  print '  unsigned integers;'
  print ''
  print '        I        J        K = I XOR J'
  print ''

  for test in range ( 0, test_num ):
    i, seed = i4_uniform_ab ( 0, 100, seed )
    j, seed = i4_uniform_ab ( 0, 100, seed )
    bvec1 = ui4_to_ubvec ( i, n )
    bvec2 = ui4_to_ubvec ( j, n )
    bvec3 = ubvec_xor ( n, bvec1, bvec2 )
    k = ubvec_to_ui4 ( n, bvec3 )
    print '  %8d  %8d  %8d' % ( i, j, k )
#
#  Terminate.
#
  print ''
  print 'UBVEC_XOR_TEST'
  print '  Normal end of execution.'

  return
def subset_random ( n, seed ):

#*****************************************************************************80
#
## SUBSET_RANDOM selects a random subset of an N-set.
#
#  Example:
#
#    N = 4
#
#    0 0 1 1
#    0 1 0 1
#    1 1 0 1
#    0 0 1 0
#    0 0 0 1
#    1 1 0 0
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    06 May 2015
#
#  Author:
#
#    John Burkardt.
#
#  Reference:
#
#    Albert Nijenhuis, Herbert Wilf,
#    Combinatorial Algorithms,
#    Academic Press, 1978, second edition,
#    ISBN 0-12-519260-6.
#
#  Parameters:
#
#    Input, integer N, the size of the full set.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer A(N).  A vector to hold the information about
#    the set chosen.  On return, if A(I) = 1, then
#    I is in the random subset, otherwise, A(I) = 0
#    and I is not in the random subset.
#
#    Output, integer SEED, an updated seed for the random number generator.
#
  import numpy as np
  from i4_uniform_ab import i4_uniform_ab

  a = np.zeros ( n )

  i4_lo = 0
  i4_hi = 1
  for i in range ( 0, n ):
    a[i], seed = i4_uniform_ab ( i4_lo, i4_hi, seed )

  return a, seed
Example #9
0
def subset_random(n, seed):

    #*****************************************************************************80
    #
    ## SUBSET_RANDOM selects a random subset of an N-set.
    #
    #  Example:
    #
    #    N = 4
    #
    #    0 0 1 1
    #    0 1 0 1
    #    1 1 0 1
    #    0 0 1 0
    #    0 0 0 1
    #    1 1 0 0
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    06 May 2015
    #
    #  Author:
    #
    #    John Burkardt.
    #
    #  Reference:
    #
    #    Albert Nijenhuis, Herbert Wilf,
    #    Combinatorial Algorithms,
    #    Academic Press, 1978, second edition,
    #    ISBN 0-12-519260-6.
    #
    #  Parameters:
    #
    #    Input, integer N, the size of the full set.
    #
    #    Input, integer SEED, a seed for the random number generator.
    #
    #    Output, integer A(N).  A vector to hold the information about
    #    the set chosen.  On return, if A(I) = 1, then
    #    I is in the random subset, otherwise, A(I) = 0
    #    and I is not in the random subset.
    #
    #    Output, integer SEED, an updated seed for the random number generator.
    #
    import numpy as np
    from i4_uniform_ab import i4_uniform_ab

    a = np.zeros(n)

    i4_lo = 0
    i4_hi = 1
    for i in range(0, n):
        a[i], seed = i4_uniform_ab(i4_lo, i4_hi, seed)

    return a, seed
Example #10
0
def gear_eigenvalues_test():

    #*****************************************************************************80
    #
    ## GEAR_EIGENVALUES_TEST tests GEAR_EIGENVALUES.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    06 February 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from gear import gear
    from i4_uniform_ab import i4_uniform_ab
    from r8mat_print import r8mat_print
    from r8vec_print import r8vec_print

    print ''
    print 'GEAR_EIGENVALUES_TEST'
    print '  GEAR_EIGENVALUES computes the GEAR eigenvalues.'

    m = 5
    n = 5
    i4_lo = -n
    i4_hi = +n
    seed = 123456789
    ii, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
    jj, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
    a = gear(ii, jj, n)

    r8mat_print(m, n, a, '  GEAR matrix:')

    lam = gear_eigenvalues(ii, jj, n)

    r8vec_print(n, lam, '  GEAR eigenvalues:')

    print ''
    print 'GEAR_EIGENVALUES_TEST'
    print '  Normal end of execution.'

    return
Example #11
0
def gear_eigenvalues_test ( ):

#*****************************************************************************80
#
## GEAR_EIGENVALUES_TEST tests GEAR_EIGENVALUES.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    06 February 2015
#
#  Author:
#
#    John Burkardt
#
  from gear import gear
  from i4_uniform_ab import i4_uniform_ab
  from r8mat_print import r8mat_print
  from r8vec_print import r8vec_print

  print ''
  print 'GEAR_EIGENVALUES_TEST'
  print '  GEAR_EIGENVALUES computes the GEAR eigenvalues.'

  m = 5
  n = 5
  i4_lo = -n
  i4_hi = +n
  seed = 123456789
  ii, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
  jj, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
  a = gear ( ii, jj, n )

  r8mat_print ( m, n, a, '  GEAR matrix:' )

  lam = gear_eigenvalues ( ii, jj, n )

  r8vec_print ( n, lam, '  GEAR eigenvalues:' )

  print ''
  print 'GEAR_EIGENVALUES_TEST'
  print '  Normal end of execution.'

  return
def i4_xor_test ( ):

#*****************************************************************************80
#
## I4_XOR_TEST tests I4_XOR.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    28 September 2014
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab

  i4_lo = 0
  i4_hi = 100
  test_num = 10
  seed = 123456789

  print ''
  print 'I4_XOR_TEST'
  print '  I4_XOR returns the bitwise exclusive OR of two I4s.'
  print ''
  print '         I         J    I4_XOR       I^J'
  print ''

  for test in range ( 0, test_num ):

    i, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
    j, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
    k = i4_xor ( i, j )
    l = ( i ^ j )
    print '  %8d  %8d  %8d  %8d' % ( i, j, k, l )
#
#  Terminate.
#
  print ''
  print 'I4_XOR_TEST'
  print '  Normal end of execution.'

  return
Example #13
0
def multiperm_enum_test():

    #*****************************************************************************80
    #
    ## MULTIPERM_ENUM_TEST tests MULTIPERM_ENUM.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    19 December 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    from compnz_random import compnz_random
    from i4_uniform_ab import i4_uniform_ab

    n = 5
    seed = 123456789
    test_num = 5

    print ''
    print 'MULTIPERM_ENUM_TEST:'
    print '  MULTIPERM_ENUM enumerates multipermutations.'
    print ''
    print '  N is the number of objects to be permuted.'
    print '  K is the number of distinct types of objects.'
    print '  COUNTS is the number of objects of each type.'
    print '  NUMBER is the number of multipermutations.'
    print ''
    print '  Number       N       K       Counts(1:K)'
    print ''

    for test in range(0, test_num):

        k, seed = i4_uniform_ab(1, n, seed)

        counts, seed = compnz_random(n, k, seed)

        number = multiperm_enum(n, k, counts)

        print '  %6d  %6d  %6d' % (number, n, k),
        for i in range(0, k):
            print '  %4d' % (counts[i]),
        print ''


#
#  Terminate.
#
    print ''
    print 'MULTIPERM_ENUM_TEST:'
    print '  Normal end of execution.'

    return
def i4_max_test():

    #*****************************************************************************80
    #
    ## I4_MAX_TEST tests I4_MAX.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    09 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab

    print ''
    print 'I4_MAX_TEST'
    print '  I4_MAX computes the maximum of two I4\'s.'

    i4_lo = -100
    i4_hi = +100
    seed = 123456789

    print ''
    print '         A         B     C=I4_MAX(A,B)'
    print ''
    for i in range(0, 10):
        a, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
        b, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
        c = i4_max(a, b)
        print '  %8d  %8d  %8d' % (a, b, c)


#
#  Terminate.
#
    print ''
    print 'I4_MAX_TEST:'
    print '  Normal end of execution.'

    return
Example #15
0
def gear_determinant_test ( ):

#*****************************************************************************80
#
## GEAR_DETERMINANT_TEST tests GEAR_DETERMINANT.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    06 February 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab
  from r8mat_print import r8mat_print

  print ''
  print 'GEAR_DETERMINANT_TEST'
  print '  GEAR_DETERMINANT computes the GEAR determinant.'

  m = 4
  n = 4
  i4_lo = -n
  i4_hi = +n
  seed = 123456789
  ii, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
  jj, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
  a = gear ( ii, jj, n )

  r8mat_print ( m, n, a, '  GEAR matrix:' )

  value = gear_determinant ( ii, jj, n )

  print '  Value =  %g' % ( value )

  print ''
  print 'GEAR_DETERMINANT_TEST'
  print '  Normal end of execution.'

  return
Example #16
0
def gear_determinant_test():

    #*****************************************************************************80
    #
    ## GEAR_DETERMINANT_TEST tests GEAR_DETERMINANT.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    06 February 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab
    from r8mat_print import r8mat_print

    print ''
    print 'GEAR_DETERMINANT_TEST'
    print '  GEAR_DETERMINANT computes the GEAR determinant.'

    m = 4
    n = 4
    i4_lo = -n
    i4_hi = +n
    seed = 123456789
    ii, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
    jj, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
    a = gear(ii, jj, n)

    r8mat_print(m, n, a, '  GEAR matrix:')

    value = gear_determinant(ii, jj, n)

    print '  Value =  %g' % (value)

    print ''
    print 'GEAR_DETERMINANT_TEST'
    print '  Normal end of execution.'

    return
def multiperm_enum_test ( ):

#*****************************************************************************80
#
## MULTIPERM_ENUM_TEST tests MULTIPERM_ENUM.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    19 December 2014
#
#  Author:
#
#    John Burkardt
#
  from compnz_random import compnz_random
  from i4_uniform_ab import i4_uniform_ab

  n = 5
  seed = 123456789
  test_num = 5
  
  print ''
  print 'MULTIPERM_ENUM_TEST:'
  print '  MULTIPERM_ENUM enumerates multipermutations.'
  print ''
  print '  N is the number of objects to be permuted.'
  print '  K is the number of distinct types of objects.'
  print '  COUNTS is the number of objects of each type.'
  print '  NUMBER is the number of multipermutations.'
  print ''
  print '  Number       N       K       Counts(1:K)'
  print ''

  for test in range ( 0, test_num ):

    k, seed = i4_uniform_ab ( 1, n, seed )

    counts, seed = compnz_random ( n, k, seed )

    number = multiperm_enum ( n, k, counts )

    print '  %6d  %6d  %6d' % ( number, n, k ),
    for i in range ( 0, k ):
      print '  %4d' % ( counts[i] ),
    print ''
#
#  Terminate.
#
  print ''
  print 'MULTIPERM_ENUM_TEST:'
  print '  Normal end of execution.'

  return
Example #18
0
def i4_max_test ( ):

#*****************************************************************************80
#
## I4_MAX_TEST tests I4_MAX.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 March 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab

  print ''
  print 'I4_MAX_TEST'
  print '  I4_MAX computes the maximum of two I4\'s.'

  i4_lo = - 100
  i4_hi = + 100
  seed = 123456789

  print ''
  print '         A         B     C=I4_MAX(A,B)'
  print ''
  for i in range ( 0, 10 ):
    a, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
    b, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
    c = i4_max ( a, b )
    print '  %8d  %8d  %8d' % ( a, b, c )
#
#  Terminate.
#
  print ''
  print 'I4_MAX_TEST:'
  print '  Normal end of execution.'

  return
def dvec_complementx_test ( ):

#*****************************************************************************80
#
## DVEC_COMPLEMENTX_TEST tests DVEC_COMPLEMENTX;
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    28 May 2015
#
#  Author:
#
#    John Burkardt
#
  from dvec_to_i4 import dvec_to_i4
  from dvec_print import dvec_print
  from i4_to_dvec import i4_to_dvec
  from i4_uniform_ab import i4_uniform_ab

  n = 10
  seed = 123456789
  test_num = 5

  print ''
  print 'DVEC_COMPLEMENTX_TEST'
  print '  DVEC_COMPLEMENTX returns the ten''s complement'
  print '  of a (signed) decimal vector;'

  for test in range ( 0, test_num ):
    
    i, seed = i4_uniform_ab ( -100, 100, seed )

    dvec1 = i4_to_dvec ( i, n )

    dvec2 = dvec_complementx ( n, dvec1 )

    j = dvec_to_i4 ( n, dvec2 )

    print ''
    print '  I = %8d' % ( i )
    print '  J = %8d' % ( j )
    dvec_print ( n, dvec1, '' )
    dvec_print ( n, dvec2, '' )
#
#  Terminate.
#
  print ''
  print 'DVEC_COMPLEMENTX_TEST:'
  print '  Normal end of execution.'

  return
Example #20
0
def r8_to_dec_test ( ):

#*****************************************************************************80
#
## R8_TO_DEC tests R8_TO_DEC.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    25 May 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab
  from r8_uniform_01 import r8_uniform_01

  print ''
  print 'R8_TO_DEC_TEST'
  print '  R8_TO_DEC converts a real number to a decimal;'

  dec_digit = 5

  print ''
  print '  The number of decimal digits is %d' % ( dec_digit )

  seed = 123456789

  print ''
  print '        R   =>     A    * 10^B'
  print ''

  for i in range ( 0, 10 ):

    r, seed = r8_uniform_01 ( seed )
    e, seed = i4_uniform_ab ( 0, +10, seed )

    r = r * 10.0 ** e

    a, b = r8_to_dec ( r, dec_digit )

    print '  %12g  %6d  %6d' % ( r, a, b )
#
#  Terminate.
#
  print ''
  print 'R8_TO_DEC_TEST'
  print '  Normal end of execution.'

  return
Example #21
0
def perm0_random2(n, seed):

    #*****************************************************************************80
    #
    ## PERM0_RANDOM2 selects a random permutation of (0,...,N-1).
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    06 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    K L Hoffman, D R Shier,
    #    Algorithm 564,
    #    A Test Problem Generator for Discrete Linear L1 Approximation Problems,
    #    ACM Transactions on Mathematical Software,
    #    Volume 6, Number 4, December 1980, pages 615-617.
    #
    #  Parameters:
    #
    #    Input, integer N, the number of entries in the vector.
    #
    #    Input, integer SEED, a seed for the random number generator.
    #
    #    Output, integer P[N], a permutation in standard index form.
    #
    #    Output, integer SEED, an updated seed.
    #
    import numpy as np
    from i4_uniform_ab import i4_uniform_ab
    from i4vec_indicator0 import i4vec_indicator0

    p = i4vec_indicator0(n)

    for i in range(0, n):

        iadd, seed = i4_uniform_ab(1, n, seed)
        j = ((i + iadd) % n)

        k = p[i]
        p[i] = p[j]
        p[j] = k

    return p, seed
def perm0_random2 ( n, seed ):

#*****************************************************************************80
#
## PERM0_RANDOM2 selects a random permutation of (0,...,N-1).
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    06 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    K L Hoffman, D R Shier,
#    Algorithm 564,
#    A Test Problem Generator for Discrete Linear L1 Approximation Problems,
#    ACM Transactions on Mathematical Software,
#    Volume 6, Number 4, December 1980, pages 615-617.
#
#  Parameters:
#
#    Input, integer N, the number of entries in the vector.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer P[N], a permutation in standard index form.
#
#    Output, integer SEED, an updated seed.
#
  import numpy as np
  from i4_uniform_ab import i4_uniform_ab
  from i4vec_indicator0 import i4vec_indicator0

  p = i4vec_indicator0 ( n )

  for i in range ( 0, n ):

    iadd, seed = i4_uniform_ab ( 1, n, seed )
    j = ( ( i + iadd ) % n )

    k    = p[i]
    p[i] = p[j]
    p[j] = k

  return p, seed
def permutation_random ( n, key ):

#*****************************************************************************80
#
## PERMUTATION_RANDOM returns the PERMUTATION_RANDOM matrix.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    27 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Albert Nijenhuis, Herbert Wilf,
#    Combinatorial Algorithms,
#    Academic Press, 1978, second edition,
#    ISBN 0-12-519260-6.
#
#  Parameters:
#
#    Input, integer N, the order of the matrix.
#
#    Input, integer KEY, a positive value that selects the data.
#
#    Output, real A(N,N), the matrix.
#
  from i4_uniform_ab import i4_uniform_ab
  from identity import identity

  a = identity ( n, n )

  seed = key

  for i in range ( 0, n ):
    i4_lo = i
    i4_hi = n - 1
    i2, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
    if ( i2 != i ):
      for j in range ( 0, n ):
        t = a[i,j]
        a[i,j] = a[i2,j]
        a[i2,j] = t

  return a
Example #24
0
def permutation_random(n, key):

    #*****************************************************************************80
    #
    ## PERMUTATION_RANDOM returns the PERMUTATION_RANDOM matrix.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    27 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    Albert Nijenhuis, Herbert Wilf,
    #    Combinatorial Algorithms,
    #    Academic Press, 1978, second edition,
    #    ISBN 0-12-519260-6.
    #
    #  Parameters:
    #
    #    Input, integer N, the order of the matrix.
    #
    #    Input, integer KEY, a positive value that selects the data.
    #
    #    Output, real A(N,N), the matrix.
    #
    from i4_uniform_ab import i4_uniform_ab
    from identity import identity

    a = identity(n, n)

    seed = key

    for i in range(0, n):
        i4_lo = i
        i4_hi = n - 1
        i2, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
        if (i2 != i):
            for j in range(0, n):
                t = a[i, j]
                a[i, j] = a[i2, j]
                a[i2, j] = t

    return a
def bvec_complement2_test():

    #*****************************************************************************80
    #
    ## BVEC_COMPLEMENT2_TEST tests BVEC_COMPLEMENT2.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    24 December 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    from bvec_print import bvec_print
    from bvec_to_i4 import bvec_to_i4
    from i4_to_bvec import i4_to_bvec
    from i4_uniform_ab import i4_uniform_ab

    n = 10

    seed = 123456789
    test_num = 5

    print ''
    print 'BVEC_COMPLEMENT2_TEST'
    print '  BVEC_COMPLEMENT2 returns the twos complement'
    print '  of a (signed) binary vector;'

    for test in range(0, test_num):

        print ''

        i, seed = i4_uniform_ab(-100, 100, seed)
        bvec1 = i4_to_bvec(i, n)
        bvec_print(n, bvec1, '')

        bvec2 = bvec_complement2(n, bvec1)
        bvec_print(n, bvec2, '')

        j = bvec_to_i4(n, bvec2)

    print ''
    print 'BVEC_COMPLEMENT2_TEST'
    print '  Normal end of execution.'

    return
def perm0_uniform ( n, seed ):

#*****************************************************************************80
#
## PERM0_UNIFORM selects a random permutation of 0, ..., N-1.
#
#  Discussion:
#
#    An I4VEC is a vector of I4 values.
#
#    The algorithm is known as the Fisher-Yates or Knuth shuffle.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    25 October 2014
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the number of entries in the vector.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer P[N], a permutation of the digits 0 through N-1.
#
#    Output, integer SEED, an updated seed.
#
  from i4_uniform_ab import i4_uniform_ab
  import numpy as np

  p = np.zeros ( n, dtype = np.int32 )

  for i in range ( 0, n ):
    p[i] = i

  for i in range ( 0, n - 1 ):
    j, seed = i4_uniform_ab ( i, n - 1, seed )
    k    = p[i]
    p[i] = p[j]
    p[j] = k

  return p, seed
Example #27
0
def perm0_random(n, seed):

    #*****************************************************************************80
    #
    ## PERM0_RANDOM selects a random permutation of N objects.
    #
    #  Discussion:
    #
    #    An I4VEC is a vector of I4 values.
    #
    #    The algorithm is known as the Fisher-Yates or Knuth shuffle.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    25 October 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the number of entries in the vector.
    #
    #    Input, integer SEED, a seed for the random number generator.
    #
    #    Output, integer P[N], a permutation of the digits 0 through N-1.
    #
    #    Output, integer SEED, an updated seed.
    #
    from i4_uniform_ab import i4_uniform_ab
    import numpy as np

    p = np.zeros(n, dtype=np.int32)

    for i in range(0, n):
        p[i] = i

    for i in range(0, n - 1):
        j, seed = i4_uniform_ab(i, n - 1, seed)
        k = p[i]
        p[i] = p[j]
        p[j] = k

    return p, seed
Example #28
0
def dvec_to_i4_test():

    #*****************************************************************************80
    #
    ## DVEC_TO_I4_TEST tests DVEC_TO_I4
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    28 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_to_dvec import i4_to_dvec
    from i4_uniform_ab import i4_uniform_ab

    print ''
    print 'DVEC_TO_I4_TEST'
    print '  DVEC_TO_I4 converts a DVEC to an I4.'
    print ''
    print '        I4 => DVEC => I4'
    print ''

    seed = 123456789
    i1, seed = i4_uniform_ab(-10000, 10000, seed)

    n = 6
    dvec = i4_to_dvec(i1, n)

    i2 = dvec_to_i4(n, dvec)

    print '  %6d  ' % (i1),
    for i in range(n - 1, -1, -1):
        print '%2d' % (dvec[i]),
    print '  %6d' % (i2)
    #
    #  Terminate.
    #
    print ''
    print 'DVEC_TO_I4_TEST:'
    print '  Normal end of execution.'

    return
Example #29
0
def dvec_to_i4_test ( ):

#*****************************************************************************80
#
## DVEC_TO_I4_TEST tests DVEC_TO_I4
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    28 May 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_to_dvec import i4_to_dvec
  from i4_uniform_ab import i4_uniform_ab

  print ''
  print 'DVEC_TO_I4_TEST'
  print '  DVEC_TO_I4 converts a DVEC to an I4.'
  print ''
  print '        I4 => DVEC => I4'
  print ''

  seed = 123456789
  i1, seed = i4_uniform_ab ( -10000, 10000, seed )

  n = 6
  dvec = i4_to_dvec ( i1, n )

  i2 = dvec_to_i4 ( n, dvec )

  print '  %6d  ' % ( i1 ),
  for i in range ( n - 1, -1, -1 ):
    print '%2d' % ( dvec[i] ),
  print '  %6d' % ( i2 )
#
#  Terminate.
#
  print ''
  print 'DVEC_TO_I4_TEST:'
  print '  Normal end of execution.'

  return
Example #30
0
def perm1_uniform(n, seed):

    #*****************************************************************************80
    #
    ## PERM1_UNIFORM selects a random permutation of 1,...,N.
    #
    #  Discussion:
    #
    #    An I4VEC is a vector of I4 values.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    23 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the number of entries in the vector.
    #
    #    Input, integer SEED, a seed for the random number generator.
    #
    #    Output, integer P[N], a permutation of 1, ..., N.
    #
    #    Output, integer SEED, an updated seed.
    #
    from i4_uniform_ab import i4_uniform_ab
    import numpy as np

    p = np.zeros(n, dtype=np.int32)

    for i in range(0, n):
        p[i] = i + 1

    for i in range(0, n - 1):
        j, seed = i4_uniform_ab(i, n - 1, seed)
        k = p[i]
        p[i] = p[j]
        p[j] = k

    return p, seed
def perm1_uniform ( n, seed ):

#*****************************************************************************80
#
## PERM1_UNIFORM selects a random permutation of 1,...,N.
#
#  Discussion:
#
#    An I4VEC is a vector of I4 values.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    23 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the number of entries in the vector.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer P[N], a permutation of 1, ..., N.
#
#    Output, integer SEED, an updated seed.
#
  from i4_uniform_ab import i4_uniform_ab
  import numpy as np

  p = np.zeros ( n, dtype = np.int32 )

  for i in range ( 0, n ):
    p[i] = i + 1

  for i in range ( 0, n - 1 ):
    j, seed = i4_uniform_ab ( i, n - 1, seed )
    k    = p[i]
    p[i] = p[j]
    p[j] = k

  return p, seed
Example #32
0
def r8_mop_test():

    #*****************************************************************************80
    #
    ## R8_MOP_TEST tests R8_MOP.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    06 December 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    import platform
    from i4_uniform_ab import i4_uniform_ab

    print('')
    print('R8_MOP_TEST')
    print('  Python version: %s' % (platform.python_version()))
    print('  R8_MOP evaluates (-1.0)^I4 as an R8.')
    print('')
    print('    I4  R8_MOP(I4)')
    print('')

    i4_min = -100
    i4_max = +100
    seed = 123456789

    for test in range(0, 10):
        i4, seed = i4_uniform_ab(i4_min, i4_max, seed)
        r8 = r8_mop(i4)
        print('  %4d  %4.1f' % (i4, r8))


#
#  Terminate.
#
    print('')
    print('R8_MOP_TEST')
    print('  Normal end of execution.')
    return
Example #33
0
def i4_abs_test():

    #*****************************************************************************80
    #
    ## I4_ABS_TEST tests I4_ABS.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    10 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab

    print ''
    print 'I4_ABS_TEST'
    print '  I4_ABS computes the absolute value of an I4.'

    i4_lo = -100
    i4_hi = +100
    seed = 123456789

    print ''
    print '         A         B=I4_ABS(A)'
    print ''
    for i in range(0, 10):
        a, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
        b = i4_abs(a)
        print '  %8d  %8d' % (a, b)


#
#  Terminate.
#
    print ''
    print 'I4_ABS_TEST:'
    print '  Normal end of execution.'

    return
def i4_bit_lo1_test():

    #*****************************************************************************80
    #
    ## I4_BIT_LO1_TEST tests I4_BIT_LO1.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    27 September 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab

    seed = 123456789
    test_num = 10

    print ''
    print 'I4_BIT_LO1_TEST'
    print '  I4_BIT_LO1 returns the location of the low 1 bit.'
    print ''
    print '       I  I4_BIT_LO1(I)'
    print ''

    for test in range(0, test_num):
        i, seed = i4_uniform_ab(0, 100, seed)
        j = i4_bit_lo1(i)
        print '  %8d  %8d' % (i, j)


#
#  Terminate.
#
    print ''
    print 'I4_BIT_LO1_TEST'
    print '  Normal end of execution.'

    return
def i4_mop_test():

    #*****************************************************************************80
    #
    ## I4_MOP_TEST tests I4_MOP.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    12 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab

    print ''
    print 'I4_MOP_TEST'
    print '  I4_MOP computes a minus-one-power (-1)^I.'
    print ' '
    print '        I4  I4_MOP(I4)'
    print ' '

    i4_lo = -1000000
    i4_hi = +1000000
    seed = 123456789

    for i in range(0, 10):
        i4, seed = i4_uniform_ab(i4_lo, i4_hi, seed)
        print '  %8d  %10d' % (i4, i4_mop(i4))


#
#  Terminate.
#
    print ''
    print 'I4_MOP_TEST:'
    print '  Normal end of execution.'

    return
Example #36
0
def mono_between_random(m, n1, n2, seed):

    #*****************************************************************************80
    #
    ## MONO_BETWEEN_RANDOM: random monomial with total degree between N1 and N2.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    25 October 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer M, the spatial dimension.
    #
    #    Input, integer N1, N2, the minimum and maximum degrees.
    #    0 <= N1 <= N2.
    #
    #    Input, integer SEED, the random number seed.
    #
    #    Output integer X[M], the random monomial.
    #
    #    Output integer RANK, the rank of the monomial.
    #
    #    Output, integer SEED, the updated random number seed.
    #
    from i4_uniform_ab import i4_uniform_ab
    from mono_unrank_grlex import mono_unrank_grlex
    from mono_upto_enum import mono_upto_enum

    n1_copy = max(n1, 0)
    rank_min = mono_upto_enum(m, n1_copy - 1) + 1
    rank_max = mono_upto_enum(m, n2)
    rank, seed = i4_uniform_ab(rank_min, rank_max, seed)
    x = mono_unrank_grlex(m, rank)

    return x, rank, seed
def mono_upto_random ( m, n, seed  ):

#*****************************************************************************80
#
## MONO_UPTO_RANDOM: random monomial with total degree less than or equal to N.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    25 October 2014
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer M, the spatial dimension.
#
#    Input, integer N, the degree.
#    0 <= N.
#
#    Input, integer SEED, the random number seed.
#
#    Output, integer X[M], the random monomial.
#
#    Output, integer RANK, the rank of the monomial.
#
#    Output, integer SEED, the random number seed.
#
  from i4_uniform_ab import i4_uniform_ab
  from mono_unrank_grlex import mono_unrank_grlex
  from mono_upto_enum import mono_upto_enum

  rank_min = 1
  rank_max = mono_upto_enum ( m, n )
  rank, seed = i4_uniform_ab ( rank_min, rank_max, seed )
  x = mono_unrank_grlex ( m, rank )

  return x, rank, seed
Example #38
0
def i4_abs_test ( ):

#*****************************************************************************80
#
## I4_ABS_TEST tests I4_ABS.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    10 March 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab

  print ''
  print 'I4_ABS_TEST'
  print '  I4_ABS computes the absolute value of an I4.'

  i4_lo = - 100
  i4_hi = + 100
  seed = 123456789

  print ''
  print '         A         B=I4_ABS(A)'
  print ''
  for i in range ( 0, 10 ):
    a, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
    b = i4_abs ( a )
    print '  %8d  %8d' % ( a, b )
#
#  Terminate.
#
  print ''
  print 'I4_ABS_TEST:'
  print '  Normal end of execution.'

  return
Example #39
0
def r8_mop_test ( ):

#*****************************************************************************80
#
## R8_MOP_TEST tests R8_MOP.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    06 December 2014
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab

  print ''
  print 'R8_MOP_TEST'
  print '  R8_MOP evaluates (-1.0)^I4 as an R8.'
  print ''
  print '    I4  R8_MOP(I4)'
  print ''

  i4_min = -100;
  i4_max = +100;
  seed = 123456789;

  for test in range ( 0, 10 ):
    i4, seed = i4_uniform_ab ( i4_min, i4_max, seed )
    r8 = r8_mop ( i4 )
    print '  %4d  %4.1f' % ( i4, r8 )
#
#  Terminate.
#
  print ''
  print 'R8_MOP_TEST'
  print '  Normal end of execution.'

  return
def mono_total_random(m, n, seed):

    #*****************************************************************************80
    #
    ## MONO_TOTAL_RANDOM: random monomial with total degree equal to N.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    21 November 2013
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer M, the spatial dimension.
    #
    #    Input, integer N, the degree.
    #    0 <= N.
    #
    #    Input, integer SEED, the random number seed.
    #
    #    Output, integer X[M], the random monomial.
    #
    #    Output, integer RANK, the rank of the monomial.
    #
    #    Output, integer SEED, the random number seed.
    #
    from i4_uniform_ab import i4_uniform_ab
    from mono_unrank_grlex import mono_unrank_grlex
    from mono_upto_enum import mono_upto_enum

    rank_min = mono_upto_enum(m, n - 1) + 1
    rank_max = mono_upto_enum(m, n)
    rank, seed = i4_uniform_ab(rank_min, rank_max, seed)
    x = mono_unrank_grlex(m, rank)

    return x, rank, seed
Example #41
0
def i4_mop_test ( ):

#*****************************************************************************80
#
## I4_MOP_TEST tests I4_MOP.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    12 May 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab

  print ''
  print 'I4_MOP_TEST'
  print '  I4_MOP computes a minus-one-power (-1)^I.'
  print ' '
  print '        I4  I4_MOP(I4)'
  print ' '

  i4_lo = -1000000
  i4_hi = +1000000
  seed = 123456789
  
  for i in range ( 0, 10 ):
    i4, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
    print '  %8d  %10d' % ( i4, i4_mop ( i4 ) )
#
#  Terminate.
#
  print ''
  print 'I4_MOP_TEST:'
  print '  Normal end of execution.'

  return
Example #42
0
def vec_random ( n, base, seed ):

#*****************************************************************************80
#
## VEC_RANDOM selects a random N-vector of integers modulo a given base.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    06 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the size of the vector to be generated.
#
#    Input, integer BASE, the base to be used.
#
#    Input, integer SEED, a random number seed.
#
#    Output, integer A(N), a list of N random values between
#    0 and BASE-1.
#
#    Input, integer SEED, an updated random number seed.
#
  import numpy as np
  from i4_uniform_ab import i4_uniform_ab

  a = np.zeros ( n )
  i4_lo = 0
  i4_hi = base - 1

  for i in range ( 0, n ):
    a[i], seed = i4_uniform_ab ( i4_lo, i4_hi, seed )

  return a, seed
Example #43
0
def i4_bit_lo0_test ( ):

#*****************************************************************************80
#
## I4_BIT_LO0_TEST tests I4_BIT_LO0.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    27 September 2014
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab

  seed = 123456789
  test_num = 10

  print ''
  print 'I4_BIT_LO0_TEST'
  print '  I4_BIT_LO0 returns the location of the low 0 bit.'
  print ''
  print '       I  I4_BIT_LO0(I)'
  print ''
 
  for test in range ( 0, test_num ):
    i, seed = i4_uniform_ab ( 0, 100, seed )
    j = i4_bit_lo0 ( i )
    print '  %8d  %8d' % ( i, j )
#
#  Terminate.
#
  print ''
  print 'I4_BIT_LO0_TEST'
  print '  Normal end of execution.'

  return
Example #44
0
def vec_random(n, base, seed):

    #*****************************************************************************80
    #
    ## VEC_RANDOM selects a random N-vector of integers modulo a given base.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    06 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the size of the vector to be generated.
    #
    #    Input, integer BASE, the base to be used.
    #
    #    Input, integer SEED, a random number seed.
    #
    #    Output, integer A(N), a list of N random values between
    #    0 and BASE-1.
    #
    #    Input, integer SEED, an updated random number seed.
    #
    import numpy as np
    from i4_uniform_ab import i4_uniform_ab

    a = np.zeros(n)
    i4_lo = 0
    i4_hi = base - 1

    for i in range(0, n):
        a[i], seed = i4_uniform_ab(i4_lo, i4_hi, seed)

    return a, seed
Example #45
0
def r8_mop_test():

    #*****************************************************************************80
    #
    ## R8_MOP_TEST tests R8_MOP.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    06 December 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab

    print ''
    print 'R8_MOP_TEST'
    print '  R8_MOP evaluates (-1.0)^I4 as an R8.'
    print ''
    print '    I4  R8_MOP(I4)'
    print ''

    i4_min = -100
    i4_max = +100
    seed = 123456789

    for test in range(0, 10):
        i4, seed = i4_uniform_ab(i4_min, i4_max, seed)
        r8 = r8_mop(i4)
        print '  %4d  %4.1f' % (i4, r8)

    print ''
    print 'R8_MOP_TEST'
    print '  Normal end of execution.'

    return
Example #46
0
def ch_uniform_ab ( a, b, seed ):

#*****************************************************************************80
#
## CH_UNIFORM_AB returns a scaled pseudorandom CH.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    05 April 2013
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, character A, B, the minimum and maximum acceptable characters.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, character CH, the randomly chosen character.
#
#    Output, integer SEED, a seed for the random number generator.
#
  import i4_uniform_ab

  i1 = ord ( a )
  i2 = ord ( b )
  [ i3, seed ] = i4_uniform_ab.i4_uniform_ab ( i1, i2, seed )
  ch = chr ( i3 )

  return ch, seed
Example #47
0
def ytb_random(n, lam, seed):

    #*****************************************************************************80
    #
    ## YTB_RANDOM selects a random Young tableau of a given shape.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    08 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    Albert Nijenhuis, Herbert Wilf,
    #    Combinatorial Algorithms,
    #    Academic Press, 1978, second edition,
    #    ISBN 0-12-519260-6.
    #
    #  Parameters:
    #
    #    Input, integer N, the integer which has been partitioned.
    #
    #    Input, integer LAM(N), the partition of N.
    #    N = sum ( 1 <= I <= N ) LAM(I).
    #
    #    Input, integer SEED, a seed for the random number generator.
    #
    #    Output, integer A(N), the vector describing the Young tableau.
    #
    #    Output, integer SEED, an updated seed for the random number generator.
    #
    import numpy as np
    from i4_uniform_ab import i4_uniform_ab

    a = np.zeros(n)

    i = 0
    k = 0

    while (True):

        for j in range(0, lam[i]):
            a[j] = a[j] + 1
            k = k + 1

        i = i + 1

        if (n <= k):
            break

    for m in range(0, n):

        while (True):

            i, seed = i4_uniform_ab(1, a[0], seed)
            im1 = i - 1
            j, seed = i4_uniform_ab(1, lam[0], seed)
            jm1 = j - 1

            if (i <= a[jm1] and j <= lam[im1]):
                break

        while (True):

            ih = a[jm1] + lam[im1] - i - j

            if (ih == 0):
                break

            k, seed = i4_uniform_ab(1, ih, seed)

            if (k <= lam[im1] - j):
                j = j + k
                jm1 = j - 1
            else:
                i = k - lam[im1] + i + j
                im1 = i - 1

        lam[im1] = lam[im1] - 1
        a[jm1] = a[jm1] - 1
        a[n - 1 - m] = i

    for i in range(0, n):
        lam[a[i] - 1] = lam[a[i] - 1] + 1

    return a, seed
def ksub_random5 ( n, k, seed ):

#*****************************************************************************80
#
## KSUB_RANDOM5 selects a random subset of size K from a set of size N.
#
#  Discussion:
#
#    Consider the set A(1:N) = 1, 2, 3, ... N.
#    Choose a random index I1 between 1 and N, and swap items A(1) and A(I1).
#    Choose a random index I2 between 2 and N, and swap items A(2) and A(I2).
#    repeat K times.
#    A(1:K) is your random K-subset.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 June 2011
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the size of the set from which subsets
#    are drawn.
#
#    Input, integer K, number of elements in desired subsets.
#    1 <= K <= N.
#
#    Input/output, integer SEED, a seed for the random
#    number generator.
#
#    Output, integer A(K), the indices of the randomly
#    chosen elements.
#
  import numpy as np
  from i4_uniform_ab import i4_uniform_ab


#
#  Let B index the set.
#
  b = np.zeros ( n )
  for i in range ( 0, n ):
    b[i] = i
#
#  Choose item 1 from N things,
#  choose item 2 from N-1 things,
#  choose item K from N-K+1 things.
#
  for i in range ( 0, k ): 

    j, seed = i4_uniform_ab ( i, n - 1, seed )

    t    = b[i]
    b[i] = b[j]
    b[j] = t
#
#  Copy the first K elements.
#
  a = np.zeros ( k )
  for i in range ( 0, k ):
    a[i] = b[i]
#
#  Put the elements in ascending order.
#
  a = np.sort ( a )

  return a, seed
def count_pose_random ( seed ):

#*****************************************************************************80
#
## COUNT_POSE_RANDOM poses a problem for the game "The Count is Good"
#
#  Discussion:
#
#    The French television show "The Count is Good" has a game that goes
#    as follows:
#
#      A number is chosen at random between 100 and 999.  This is the GOAL.
#
#      Six numbers are randomly chosen from the set 1, 2, 3, 4, 5, 6, 7, 8,
#      9, 10, 25, 50, 75, 100.  These numbers are the BLOCKS.
#
#      The player must construct a formula, using some or all of the blocks,
#      (but not more than once), and the operations of addition, subtraction,
#      multiplication and division.  Parentheses should be used to remove
#      all ambiguity.  However, it is forbidden to use subtraction in a
#      way that produces a negative result, and all division must come out
#      exactly, with no remainder.
#
#    This routine poses a sample problem from the show.  The point is,
#    to determine how to write a program that can solve such a problem.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    06 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Raymond Seroul,
#    Programming for Mathematicians,
#    Springer Verlag, 2000, page 355-357.
#
#  Parameters:
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer BLOCKS(6), the six numbers available for the formula.
#
#    Output, integer GOAL, the goal number.
#
#    Output, integer SEED, an updated seed for the random number generator.
#
  import numpy as np
  from i4_uniform_ab import i4_uniform_ab
  from ksub_random2 import ksub_random2

  stuff = np.array ( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100 ] )

  i4_lo = 100
  i4_hi = 999
  goal, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )

  m = 14
  n = 6
  ind, seed = ksub_random2 ( m, n, seed )

  blocks = np.zeros ( 6 )

  for i in range ( 0, 6 ):
    blocks[i] = stuff[ind[i]-1]

  return blocks, goal, seed
def equiv_random ( n, seed ):

#*****************************************************************************80
#
## EQUIV_RANDOM selects a random partition of a set.
#
#  Discussion:
#
#    The user does not control the number of parts in the partition.
#
#    The equivalence classes are numbered in no particular order.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    31 May 2015
#
#  Author:
#
#    John Burkardt.
#
#  Reference:
#
#    Albert Nijenhuis, Herbert Wilf,
#    Combinatorial Algorithms,
#    Academic Press, 1978, second edition,
#    ISBN 0-12-519260-6.
#
#  Parameters:
#
#    Input, integer N, the number of elements in the set to be partitioned.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer NPART, the number of classes or parts in the 
#    partition.  NPART will be between 1 and N.
#
#    Output, integer A(N), indicates the class to which each element
#    is assigned.
#
#    Output, integer SEED, an updated seed for the random number generator.
#
  import numpy as np
  from i4_uniform_ab import i4_uniform_ab
  from r8_uniform_01 import r8_uniform_01

  b = np.zeros ( n )

  b[0] = 1.0

  for l in range ( 1, n ):

    sum1 = 1.0 / float ( l )
    for k in range ( 1, l ):
      sum1 = ( sum1 + b[k-1] ) / float ( l - k )

    b[l] = ( sum1 + b[l-1] ) / float ( l + 1 )

  a = np.zeros ( n, dtype = np.int32 )

  m = n
  npart = 0

  while ( True ):

    z, seed = r8_uniform_01 ( seed )
    z = m * b[m-1] * z
    k = 0
    npart = npart + 1

    while ( 0.0 <= z ):

      a[m-1] = npart
      m = m - 1

      if ( m == 0 ):
        break

      z = z - b[m-1]
      k = k + 1
      z = z * k

    if ( m == 0 ):
      break
#
#  Randomly permute the assignments.
#
  for i in range ( 1, n ):
    j, seed = i4_uniform_ab ( i, n, seed )
    t      = a[i-1]
    a[i-1] = a[j-1]
    a[j-1] = t

  return npart, a, seed
Example #51
0
def dvec_mul_test ( ):

#*****************************************************************************80
#
## DVEC_MUL_TEST tests DVEC_MUL;
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    29 May 2015
#
#  Author:
#
#    John Burkardt
#
  from dvec_to_i4 import dvec_to_i4
  from i4_to_dvec import i4_to_dvec
  from i4_uniform_ab import i4_uniform_ab

  n = 10
  seed = 123456789
  test_num = 10
  test2_num = 2

  print ''
  print 'DVEC_MUL_TEST'
  print '  DVEC_MUL multiplies decimal vectors'
  print '  representing integers;'

  for test2 in range ( 0, test2_num ):

    if ( test2 == 0 ):

      n2 = n

    elif ( test2 == 1 ):

      n2 = 6

      print ''
      print '  NOW REPEAT THE TEST...'
      print ''
      print '  but use too few digits to represent big products.'
      print '  This corresponds to an "overflow".'
      print '  The result here should get the final decimal'
      print '  digits correctly, though.'

    print ''
    print '        I        J        K = I * J'

    for test in range ( 0, test_num ):
    
      i, seed = i4_uniform_ab ( -1000, 1000, seed )
      j, seed = i4_uniform_ab ( -1000, 1000, seed )

      print ''

      print '  %8d  %8d' % ( i, j )

      k = i * j

      print '  Directly:           %8d' % ( k )

      dvec1 = i4_to_dvec ( i, n2 )
      dvec2 = i4_to_dvec ( j, n2 )
      dvec3 = dvec_mul ( n2, dvec1, dvec2 )
      k = dvec_to_i4 ( n2, dvec3 )

      print '  DVEC_MUL            %8d\n' % ( k )
#
#  Terminate.
#
  print ''
  print 'DVEC_MUL_TEST'
  print '  Normal end of execution.'

  return
Example #52
0
def test_plu():

    #*****************************************************************************80
    #
    ## TEST_PLU tests the PLU factors.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    07 April 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    import numpy as np
    from bodewig import bodewig
    from bodewig import bodewig_plu
    from borderband import borderband
    from borderband import borderband_plu
    from dif2 import dif2
    from dif2 import dif2_plu
    from gfpp import gfpp
    from gfpp import gfpp_plu
    from givens import givens
    from givens import givens_plu
    from i4_uniform_ab import i4_uniform_ab
    from kms import kms
    from kms import kms_plu
    from lehmer import lehmer
    from lehmer import lehmer_plu
    from maxij import maxij
    from maxij import maxij_plu
    from minij import minij
    from minij import minij_plu
    from moler1 import moler1
    from moler1 import moler1_plu
    from moler3 import moler3
    from moler3 import moler3_plu
    from oto import oto
    from oto import oto_plu
    from pascal2 import pascal2
    from pascal2 import pascal2_plu
    from plu import plu
    from plu import plu_plu
    from r8_uniform_ab import r8_uniform_ab
    from r8mat_is_plu import r8mat_is_plu
    from r8mat_norm_fro import r8mat_norm_fro
    from r8vec_uniform_ab import r8vec_uniform_ab
    from vand2 import vand2
    from vand2 import vand2_plu
    from wilson import wilson
    from wilson import wilson_plu

    print ''
    print 'TEST_PLU'
    print '  A = a test matrix of order M by N'
    print '  P, L, U are the PLU factors.'
    print ''
    print '  ||A|| = Frobenius norm of A.'
    print '  ||A-PLU|| = Frobenius norm of A-P*L*U.'
    print ''
    print '  Title                    M     N         ',
    print '||A||        ||A-PLU||'
    print ''
    #
    #  BODEWIG
    #
    title = 'BODEWIG'
    m = 4
    n = 4
    a = bodewig()
    p, l, u = bodewig_plu()
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  BORDERBAND
    #
    title = 'BORDERBAND'
    m = 5
    n = 5
    a = borderband(n)
    p, l, u = borderband_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  DIF2
    #
    title = 'DIF2'
    m = 5
    n = 5
    a = dif2(m, n)
    p, l, u = dif2_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  GFPP
    #
    title = 'GFPP'
    m = 5
    n = 5
    r8_lo = -5.0
    r8_hi = +5.0
    seed = 123456789
    alpha, seed = r8_uniform_ab(r8_lo, r8_hi, seed)
    a = gfpp(n, alpha)
    p, l, u = gfpp_plu(n, alpha)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  GIVENS
    #
    title = 'GIVENS'
    m = 5
    n = 5
    a = givens(m, n)
    p, l, u = givens_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  KMS
    #
    title = 'KMS'
    m = 5
    n = 5
    r8_lo = -5.0
    r8_hi = +5.0
    seed = 123456789
    alpha, seed = r8_uniform_ab(r8_lo, r8_hi, seed)
    a = kms(alpha, m, n)
    p, l, u = kms_plu(alpha, n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  LEHMER
    #
    title = 'LEHMER'
    m = 5
    n = 5
    a = lehmer(m, n)
    p, l, u = lehmer_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  MAXIJ
    #
    title = 'MAXIJ'
    m = 5
    n = 5
    a = maxij(m, n)
    p, l, u = maxij_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  MINIJ
    #
    title = 'MINIJ'
    m = 5
    n = 5
    a = minij(n, n)
    p, l, u = minij_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  MOLER1
    #
    title = 'MOLER1'
    m = 5
    n = 5
    r8_lo = -5.0
    r8_hi = +5.0
    seed = 123456789
    alpha, seed = r8_uniform_ab(r8_lo, r8_hi, seed)
    a = moler1(alpha, n, n)
    p, l, u = moler1_plu(alpha, n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  MOLER3
    #
    title = 'MOLER3'
    m = 5
    n = 5
    a = moler3(m, n)
    p, l, u = moler3_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  OTO
    #
    title = 'OTO'
    m = 5
    n = 5
    a = oto(m, n)
    p, l, u = oto_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  PASCAL2
    #
    title = 'PASCAL2'
    m = 5
    n = 5
    a = pascal2(n)
    p, l, u = pascal2_plu(n)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  PLU
    #
    title = 'PLU'
    m = 5
    n = 5
    pivot = np.zeros(n)
    seed = 123456789
    for i in range(0, n):
        i4_lo = i
        i4_hi = n - 1
        pivot[i], seed = i4_uniform_ab(i4_lo, i4_hi, seed)
    a = plu(n, pivot)
    p, l, u = plu_plu(n, pivot)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  VAND2
    #
    title = 'VAND2'
    m = 4
    n = 4
    r8_lo = -5.0
    r8_hi = +5.0
    seed = 123456789
    x, seed = r8vec_uniform_ab(m, r8_lo, r8_hi, seed)
    a = vand2(m, x)
    p, l, u = vand2_plu(m, x)
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  WILSON
    #
    title = 'WILSON'
    m = 4
    n = 4
    a = wilson()
    p, l, u = wilson_plu()
    error_frobenius = r8mat_is_plu(m, n, a, p, l, u)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    print '  %-20s  %4d  %4d  %14g  %14g' \
      % ( title, m, n, norm_a_frobenius, error_frobenius )
    #
    #  Terminate.
    #
    print ''
    print 'TEST_PLU:'
    print '  Normal end of execution.'

    return
def i4_division_test():

    #*****************************************************************************80
    #
    ## I4_DIVISION_TEST tests I4_DIVISION.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    10 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab
    from math import floor

    a_hi = 100
    a_lo = -100
    b_hi = 10
    b_lo = -10
    test_num = 20

    print ' '
    print 'I4_DIVISION_TEST'
    print '  I4_DIVISION performs integer division.'
    print ' '
    print '  C0 = real ( a ) / real ( b )'
    print '  C1 = I4_DIVISION ( A, B )'
    print '  C2 = nint ( real ( a ) / real ( b ) )'
    print '  C3 = int ( A / B )'
    print '  C4 = floor ( real ( a ) / real ( b ) )'
    print '  C5 = a // b'
    print ' '
    print '  C1 and C3 and C4 and C5 should be equal.'
    print '  (They are not, for some negative cases!)'
    print '  C2 may differ;'
    print ' '
    print '     A     B           C0         C1    C2      C3    C4      C5'
    print ' '

    seed = 123456789

    for test in range(1, test_num):
        a, seed = i4_uniform_ab(a_lo, a_hi, seed)
        b, seed = i4_uniform_ab(b_lo, b_hi, seed)
        if (b == 0):
            b = 7
        c0 = float(a) / float(b)
        c1 = i4_division(a, b)
        c2 = round(float(a) / float(b))
        c3 = int(a / b)
        c4 = floor(float(a) / float(b))
        c5 = a // b
        print '  %4d  %4d    %14.6f  %4d  %4d    %4d  %4d    %4d' \
          % ( a, b, c0, c1, c2, c3, c4, c5 )


#
#  Terminate.
#
    print ' '
    print 'I4_DIVISION_TEST'
    print '  Normal end of execution.'

    return
Example #54
0
def test_plu ( ):

#*****************************************************************************80
#
## TEST_PLU tests the PLU factors.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    07 April 2014
#
#  Author:
#
#    John Burkardt
#
  import numpy as np
  from bodewig          import bodewig
  from bodewig          import bodewig_plu
  from borderband       import borderband
  from borderband       import borderband_plu
  from dif2             import dif2
  from dif2             import dif2_plu
  from gfpp             import gfpp
  from gfpp             import gfpp_plu
  from givens           import givens
  from givens           import givens_plu
  from i4_uniform_ab    import i4_uniform_ab
  from kms              import kms
  from kms              import kms_plu
  from lehmer           import lehmer
  from lehmer           import lehmer_plu
  from maxij            import maxij
  from maxij            import maxij_plu
  from minij            import minij
  from minij            import minij_plu
  from moler1           import moler1
  from moler1           import moler1_plu
  from moler3           import moler3
  from moler3           import moler3_plu
  from oto              import oto
  from oto              import oto_plu
  from pascal2          import pascal2
  from pascal2          import pascal2_plu
  from plu              import plu
  from plu              import plu_plu
  from r8_uniform_ab    import r8_uniform_ab
  from r8mat_is_plu     import r8mat_is_plu
  from r8mat_norm_fro   import r8mat_norm_fro
  from r8vec_uniform_ab import r8vec_uniform_ab
  from vand2            import vand2
  from vand2            import vand2_plu
  from wilson           import wilson
  from wilson           import wilson_plu

  print ''
  print 'TEST_PLU'
  print '  A = a test matrix of order M by N'
  print '  P, L, U are the PLU factors.'
  print ''
  print '  ||A|| = Frobenius norm of A.'
  print '  ||A-PLU|| = Frobenius norm of A-P*L*U.'
  print ''
  print '  Title                    M     N         ',
  print '||A||        ||A-PLU||'
  print ''
#
#  BODEWIG
#
  title = 'BODEWIG'
  m = 4
  n = 4
  a = bodewig ( )
  p, l, u = bodewig_plu ( )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  BORDERBAND
#
  title = 'BORDERBAND'
  m = 5
  n = 5
  a = borderband ( n )
  p, l, u = borderband_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  DIF2
#
  title = 'DIF2'
  m = 5
  n = 5
  a = dif2 ( m, n )
  p, l, u = dif2_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  GFPP
#
  title = 'GFPP'
  m = 5
  n = 5
  r8_lo = -5.0
  r8_hi = +5.0
  seed = 123456789
  alpha, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  a = gfpp ( n, alpha )
  p, l, u = gfpp_plu ( n, alpha )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  GIVENS
#
  title = 'GIVENS'
  m = 5
  n = 5
  a = givens ( m, n )
  p, l, u = givens_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  KMS
#
  title = 'KMS'
  m = 5
  n = 5
  r8_lo = -5.0
  r8_hi = +5.0
  seed = 123456789
  alpha, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  a = kms ( alpha, m, n )
  p, l, u = kms_plu ( alpha, n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  LEHMER
#
  title = 'LEHMER'
  m = 5
  n = 5
  a = lehmer ( m, n )
  p, l, u = lehmer_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  MAXIJ
#
  title = 'MAXIJ'
  m = 5
  n = 5
  a = maxij ( m, n )
  p, l, u = maxij_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  MINIJ
#
  title = 'MINIJ'
  m = 5
  n = 5
  a = minij ( n, n )
  p, l, u = minij_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  MOLER1
#
  title = 'MOLER1'
  m = 5
  n = 5
  r8_lo = -5.0
  r8_hi = +5.0
  seed = 123456789
  alpha, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  a = moler1 ( alpha, n, n )
  p, l, u = moler1_plu ( alpha, n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  MOLER3
#
  title = 'MOLER3'
  m = 5
  n = 5
  a = moler3 ( m, n )
  p, l, u = moler3_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  OTO
#
  title = 'OTO'
  m = 5
  n = 5
  a = oto ( m, n )
  p, l, u = oto_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  PASCAL2
#
  title = 'PASCAL2'
  m = 5
  n = 5
  a = pascal2 ( n )
  p, l, u = pascal2_plu ( n )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  PLU
#
  title = 'PLU'
  m = 5
  n = 5
  pivot = np.zeros ( n )
  seed = 123456789
  for i in range ( 0, n ):
    i4_lo = i
    i4_hi = n - 1
    pivot[i], seed = i4_uniform_ab ( i4_lo, i4_hi, seed )
  a = plu ( n, pivot )
  p, l, u = plu_plu ( n, pivot )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  VAND2
#
  title = 'VAND2'
  m = 4
  n = 4
  r8_lo = -5.0
  r8_hi = +5.0
  seed = 123456789
  x, seed = r8vec_uniform_ab ( m, r8_lo, r8_hi, seed )
  a = vand2 ( m, x )
  p, l, u = vand2_plu ( m, x )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  WILSON
#
  title = 'WILSON'
  m = 4
  n = 4
  a = wilson ( )
  p, l, u = wilson_plu ( )
  error_frobenius = r8mat_is_plu ( m, n, a, p, l, u )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  print '  %-20s  %4d  %4d  %14g  %14g' \
    % ( title, m, n, norm_a_frobenius, error_frobenius )
#
#  Terminate.
#
  print ''
  print 'TEST_PLU:'
  print '  Normal end of execution.'

  return
Example #55
0
def dvec_add_test ( ):

#*****************************************************************************80
#
## DVEC_ADD_TEST tests DVEC_ADD.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    28 May 2015
#
#  Author:
#
#    John Burkardt
#
  from dvec_to_i4 import dvec_to_i4
  from i4_to_dvec import i4_to_dvec
  from i4_uniform_ab import i4_uniform_ab

  n = 10
  seed = 123456789
  test_num = 10

  print ''
  print 'DVEC_ADD_TEST'
  print '  DVEC_ADD adds decimal vectors representing integers'
  print ''
  print '        I        J        K = I + J'
  print ''

  for test in range ( 0, test_num ):
    
    i, seed = i4_uniform_ab ( -100, 100, seed )
    j, seed = i4_uniform_ab ( -100, 100, seed )

    print ''

    print '  %8d  %8d' % ( i, j )

    k = i + j

    print '  Directly:           %8d' % ( k )

    dvec1 = i4_to_dvec ( i, n )
    dvec2 = i4_to_dvec ( j, n )

    dvec3 = dvec_add ( n, dvec1, dvec2 )
    k = dvec_to_i4 ( n, dvec3 )

    print '  DVEC_ADD  %8d' % ( k )
#
#  Terminate.
#
  print ''
  print 'DVEC_ADD_TEST'
  print '  Normal end of execution.'

  return
def comp_random_grlex ( kc, rank1, rank2, seed ):

#*****************************************************************************80
#
## COMP_RANDOM_GRLEX: random composition with degree less than or equal to NC.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 September 2014
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, int KC, the number of parts in the composition.
#
#    Input, int RANK1, RANK2, the minimum and maximum ranks.
#    1 <= RANK1 <= RANK2.
#
#    Input, int SEED, the random number seed.
#
#    Output, int X[KC], the random composition.
#
#    Output, int RANK, the rank of the composition.
#
#    Output, int SEED, the random number seed.
#
  from comp_unrank_grlex import comp_unrank_grlex
  from i4_uniform_ab import i4_uniform_ab
  from sys import exit
#
#  Ensure that 1 <= KC.
#
  if ( kc < 1 ):
    print ''
    print 'COMP_RANDOM_GRLEX - Fatal error!'
    print '  KC < 1'
    exit ( 'COMP_RANDOM_GRLEX - Fatal error!' );
#
#  Ensure that 1 <= RANK1.
#
  if ( rank1 < 1 ):
    print ''
    print 'COMP_RANDOM_GRLEX - Fatal error!'
    print '  RANK1 < 1'
    exit ( 'COMP_RANDOM_GRLEX - Fatal error!' );
#
#  Ensure that RANK1 <= RANK2.
#
  if ( rank2 < rank1 ):
    print ''
    print 'COMP_RANDOM_GRLEX - Fatal error!'
    print '  RANK2 < RANK1'
    exit ( 'COMP_RANDOM_GRLEX - Fatal error!' )
#
#  Choose RANK between RANK1 and RANK2.
#
  rank, seed = i4_uniform_ab ( rank1, rank2, seed )
#
#  Recover the composition of given RANK.
#
  xc = comp_unrank_grlex ( kc, rank )

  return xc, rank, seed