Beispiel #1
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
Beispiel #2
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
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 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 i4_to_bvec_test ( ):

#*****************************************************************************80
#
## I4_TO_BVEC_TEST tests I4_TO_BVEC;
#
#  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

  n = 10

  print ''
  print 'I4_TO_BVEC_TEST'
  print '  I4_TO_BVEC converts an integer to a '
  print '  signed binary vector.'
  print ''
  print '  I --> BVEC  -->  I'
  print ''

  for i in range ( -3, 11 ):
    bvec = i4_to_bvec ( i, n )
    i2 = bvec_to_i4 ( n, bvec )
    print '  %2d  ' % ( i ),
    for j in range ( n - 1, -1, -1 ):
      print '%1d' % ( bvec[j] ),
    print '  %2d' % ( i2 ) 
 
  print ''
  print 'I4_TO_BVEC_TEST'
  print '  Normal end of execution.'

  return
Beispiel #6
0
def i4_to_bvec_test():

    #*****************************************************************************80
    #
    ## I4_TO_BVEC_TEST tests I4_TO_BVEC;
    #
    #  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

    n = 10

    print ''
    print 'I4_TO_BVEC_TEST'
    print '  I4_TO_BVEC converts an integer to a '
    print '  signed binary vector.'
    print ''
    print '  I --> BVEC  -->  I'
    print ''

    for i in range(-3, 11):
        bvec = i4_to_bvec(i, n)
        i2 = bvec_to_i4(n, bvec)
        print '  %2d  ' % (i),
        for j in range(n - 1, -1, -1):
            print '%1d' % (bvec[j]),
        print '  %2d' % (i2)

    print ''
    print 'I4_TO_BVEC_TEST'
    print '  Normal end of execution.'

    return