Exemplo n.º 1
0
def i4_to_chinese ( j, n, m ):

#*****************************************************************************80
#
## I4_TO_CHINESE converts an integer to its Chinese remainder form.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    25 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer J, the integer to be converted.
#
#    Input, integer N, the number of moduluses.
#
#    Input, integer M(N), the moduluses.  These should be positive
#    and pairwise prime.
#
#    Output, integer R(N), the Chinese remainder representation of the integer.
#
  import numpy as np
  from sys import exit
  from chinese_check import chinese_check
  from i4_modp import i4_modp

  ierror = chinese_check ( n, m )

  if ( ierror != 0 ):
    print ''
    print 'I4_TO_CHINESE - Fatal error!'
    print '  The moduluses are not legal.'
    exit ( 'I4_TO_CHINESE - Fatal error!' )

  r = np.zeros ( n )

  for i in range ( 0, n ):
    r[i] = i4_modp ( j, m[i] )

  return r
Exemplo n.º 2
0
def congruence_test():

    #*****************************************************************************80
    #
    ## CONGRUENCE_TEST tests CONGRUENCE.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    06 December 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    import numpy as np
    from i4_modp import i4_modp

    test_num = 20

    a_test = np.array ( ( \
       1027,  1027,  1027,  1027, -1027, \
      -1027, -1027, -1027,     6,     0, \
          0,     0,     1,     1,     1, \
       1024,     0,     0,     5,     2 ) )
    b_test = np.array ( ( \
        712,   712,  -712,  -712,   712, \
        712,  -712,  -712,     8,     0, \
          1,     1,     0,     0,     1, \
     -15625,     0,     3,     0,     4 ) )
    c_test = np.array ( ( \
          7,    -7,     7,    -7,     7, \
         -7,     7,    -7,    50,     0, \
          0,     1,     0,     1,     0, \
      11529,     1,    11,    19,     7 ) )

    print ''
    print 'CONGRUENCE_TEST'
    print '  CONGRUENCE solves a congruence equation:'
    print '    A * X = C mod ( B )'
    print ''
    print '   I        A         B         C         X     Mod ( A*X-C,B)'
    print ''

    for test_i in range(0, test_num):

        a = a_test[test_i]
        b = b_test[test_i]
        c = c_test[test_i]

        x, ierror = congruence(a, b, c)

        if (b != 0):
            result = i4_modp(a * x - c, b)
        else:
            result = 0

        print '  %2d  %8d  %8d  %8d  %8d  %8d' % (test_i, a, b, c, x, result)

    print ''
    print 'CONGRUENCE_TEST'
    print '  Normal end of execution.'
Exemplo n.º 3
0
def congruence_test ( ):

#*****************************************************************************80
#
## CONGRUENCE_TEST tests CONGRUENCE.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    06 December 2014
#
#  Author:
#
#    John Burkardt
#
  import numpy as np
  from i4_modp import i4_modp

  test_num = 20

  a_test = np.array ( ( \
     1027,  1027,  1027,  1027, -1027, \
    -1027, -1027, -1027,     6,     0, \
        0,     0,     1,     1,     1, \
     1024,     0,     0,     5,     2 ) )
  b_test = np.array ( ( \
      712,   712,  -712,  -712,   712, \
      712,  -712,  -712,     8,     0, \
        1,     1,     0,     0,     1, \
   -15625,     0,     3,     0,     4 ) )
  c_test = np.array ( ( \
        7,    -7,     7,    -7,     7, \
       -7,     7,    -7,    50,     0, \
        0,     1,     0,     1,     0, \
    11529,     1,    11,    19,     7 ) )

  print ''
  print 'CONGRUENCE_TEST'
  print '  CONGRUENCE solves a congruence equation:'
  print '    A * X = C mod ( B )'
  print ''
  print '   I        A         B         C         X     Mod ( A*X-C,B)'
  print ''

  for test_i in range ( 0, test_num ):

    a = a_test[test_i]
    b = b_test[test_i]
    c = c_test[test_i]

    x, ierror = congruence ( a, b, c )

    if ( b != 0 ):
      result = i4_modp ( a * x - c, b )
    else:
      result = 0

    print '  %2d  %8d  %8d  %8d  %8d  %8d' % ( test_i, a, b, c, x, result )

  print ''
  print 'CONGRUENCE_TEST'
  print '  Normal end of execution.'
Exemplo n.º 4
0
def upshift ( n ):

#*****************************************************************************80
#
## UPSHIFT returns the UPSHIFT matrix.
#
#  Formula:
#
#    if ( J-I == 1 mod ( n ) )
#      A(I,J) = 1
#    else
#      A(I,J) = 0
#
#  Example:
#
#    N = 4
#
#    0 1 0 0
#    0 0 1 0
#    0 0 0 1
#    1 0 0 0
#
#  Rectangular properties:
#
#    A is integral: int ( A ) = A.
#
#    A is a zero/one matrix.
#
#  Square Properties:
#
#    A is generally not symmetric: A' /= A.
#
#    A is nonsingular.
#
#    A is a permutation matrix.
#
#    If N is even, det ( A ) = -1.
#    If N is odd,  det ( A ) = +1.
#
#    A is unimodular.
#
#    A is persymmetric: A(I,J) = A(N+1-J,N+1-I).
#
#    A is a Hankel matrix: constant along anti-diagonals.
#
#    A is an N-th root of the identity matrix.
#
#    The inverse of A is the downshift matrix.
#
#    A circulant matrix C, whose first row is (c1, c2, ..., cn), can be
#    written as a polynomial in A:
#
#      C = c1 * I + c2 * A + c3 * A**2 + ... + cn * A**n-1.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    07 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the number of rows and columns 
#    of the matrix.
#
#    Output, real A(N,N), the matrix.
#
  import numpy as np
  from i4_modp import i4_modp

  a = np.zeros ( [ n, n ] )

  for i in range ( 0, n ):
    for j in range ( 0, n ):
      if ( i4_modp ( j - i, n ) == 1 ):
        a[i,j] = 1.0

  return a
Exemplo n.º 5
0
def i4_wrap ( ival, ilo, ihi ):

#*****************************************************************************80
#
## I4_WRAP forces an integer to lie between given limits by wrapping.
#
#  Example:
#
#    ILO = 4, IHI = 8
#
#    I   Value
#
#    -2     8
#    -1     4
#     0     5
#     1     6
#     2     7
#     3     8
#     4     4
#     5     5
#     6     6
#     7     7
#     8     8
#     9     4
#    10     5
#    11     6
#    12     7
#    13     8
#    14     4
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    08 May 2013
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer IVAL, an integer value.
#
#    Input, integer ILO, IHI, the desired bounds for the integer value.
#
#    Output, integer VALUE, a "wrapped" version of IVAL.
#
  from i4_modp import i4_modp

  jlo = min ( ilo, ihi )
  jhi = max ( ilo, ihi )

  wide = jhi - jlo + 1

  if ( wide == 1 ):
    value = jlo
  else:
    value = jlo + i4_modp ( ival - jlo, wide )

  return value
Exemplo n.º 6
0
def josephus ( n, m, k ):

#*****************************************************************************80
#
## JOSEPHUS returns the position X of the K-th man to be executed.
#
#  Discussion:
#
#    The classic Josephus problem concerns a circle of 41 men.
#    Every third man is killed and removed from the circle.  Counting
#    and executing continues until all are dead.  Where was the last
#    survivor sitting?
#
#    Note that the first person killed was sitting in the third position.
#    Moreover, when we get down to 2 people, and we need to count the
#    "third" one, we just do the obvious thing, which is to keep counting
#    around the circle until our count is completed.
#
#    The process may be regarded as generating a permutation of
#    the integers from 1 to N.  The permutation would be the execution
#    list, that is, the list of the executed men, by position number.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    08 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    W W Rouse Ball,
#    Mathematical Recreations and Essays,
#    Macmillan, 1962, pages 32-36.
#
#    Donald Knuth,
#    The Art of Computer Programming,
#    Volume 1, Fundamental Algorithms,
#    Addison Wesley, 1968, pages 158-159.
#
#    Donald Knuth,
#    The Art of Computer Programming,
#    Volume 3, Sorting and Searching,
#    Addison Wesley, 1968, pages 18-19.
#
#  Parameters:
#
#    Input, integer N, the number of men.
#    N must be positive.
#
#    Input, integer M, the counting index.
#    M must not be zero.  Ordinarily, M is positive, and no greater than N.
#
#    Input, integer K, the index of the executed man of interest.
#    K must be between 1 and N.
#
#    Output, integer X, the position of the K-th man.
#    X will be between 1 and N.
#
  from sys import exit
  from i4_modp import i4_modp

  if ( n <= 0 ):
    print ''
    print 'JOSEPHUS - Fatal error!'
    print '  N <= 0.'
    exit ( 'JOSEPHUS - Fatal error!' )

  if ( m == 0 ):
    print ''
    print 'JOSEPHUS - Fatal error!'
    print '  M = 0.'
    exit ( 'JOSEPHUS - Fatal error!' )

  if ( k <= 0 or n < k ):
    print ''
    print 'JOSEPHUS - Fatal error!'
    print '  J <= 0 or N < K.'
    exit ( 'JOSEPHUS - Fatal error!' )
#
#  In case M is bigger than N, or negative, get the
#  equivalent positive value between 1 and N.
#  You can skip this operation if 1 <= M <= N.
#
  m2 = i4_modp ( m, n )

  x = k * m2

  while ( n < x ):
    x = ( ( m2 * ( x - n ) - 1 ) // ( m2 - 1 ) )

  return x
Exemplo n.º 7
0
def downshift(n):

    #*****************************************************************************80
    #
    ## DOWNSHIFT returns the DOWNSHIFT matrix.
    #
    #  Formula:
    #
    #    if ( I-J == 1 mod ( n ) )
    #      A(I,J) = 1
    #    else
    #      A(I,J) = 0
    #
    #  Example:
    #
    #    N = 4
    #
    #    0 0 0 1
    #    1 0 0 0
    #    0 1 0 0
    #    0 0 1 0
    #
    #  Rectangular properties:
    #
    #    A is integral: int ( A ) = A.
    #
    #    A is a zero/one matrix.
    #
    #  Square Properties:
    #
    #    A is generally not symmetric: A' /= A.
    #
    #    A is nonsingular.
    #
    #    A is a permutation matrix.
    #
    #    det ( A ) = (-1)^(N-1)
    #
    #    A is persymmetric: A(I,J) = A(N+1-J,N+1-I).
    #
    #    A is a Hankel matrix: constant along anti-diagonals.
    #
    #    A is an N-th root of the identity matrix.
    #    Therefore, the inverse of A = A^(N-1).
    #
    #    Any circulant matrix generated by a column vector v can be regarded as
    #    the Krylov matrix ( v, A*v, A^2*V, ..., A^(N-1)*v).
    #
    #    The inverse of A is the upshift operator.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    23 January 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the number of rows and columns of the matrix.
    #
    #    Output, real A(N,N), the matrix.
    #
    import numpy as np
    from i4_modp import i4_modp

    a = np.zeros([n, n])

    for i in range(0, n):
        for j in range(0, n):
            if (i4_modp(i - j, n) == 1):
                a[i, j] = 1.0

    return a
Exemplo n.º 8
0
def upshift(n):

    #*****************************************************************************80
    #
    ## UPSHIFT returns the UPSHIFT matrix.
    #
    #  Formula:
    #
    #    if ( J-I == 1 mod ( n ) )
    #      A(I,J) = 1
    #    else
    #      A(I,J) = 0
    #
    #  Example:
    #
    #    N = 4
    #
    #    0 1 0 0
    #    0 0 1 0
    #    0 0 0 1
    #    1 0 0 0
    #
    #  Rectangular properties:
    #
    #    A is integral: int ( A ) = A.
    #
    #    A is a zero/one matrix.
    #
    #  Square Properties:
    #
    #    A is generally not symmetric: A' /= A.
    #
    #    A is nonsingular.
    #
    #    A is a permutation matrix.
    #
    #    If N is even, det ( A ) = -1.
    #    If N is odd,  det ( A ) = +1.
    #
    #    A is unimodular.
    #
    #    A is persymmetric: A(I,J) = A(N+1-J,N+1-I).
    #
    #    A is a Hankel matrix: constant along anti-diagonals.
    #
    #    A is an N-th root of the identity matrix.
    #
    #    The inverse of A is the downshift matrix.
    #
    #    A circulant matrix C, whose first row is (c1, c2, ..., cn), can be
    #    written as a polynomial in A:
    #
    #      C = c1 * I + c2 * A + c3 * A**2 + ... + cn * A**n-1.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    07 February 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the number of rows and columns
    #    of the matrix.
    #
    #    Output, real A(N,N), the matrix.
    #
    import numpy as np
    from i4_modp import i4_modp

    a = np.zeros([n, n])

    for i in range(0, n):
        for j in range(0, n):
            if (i4_modp(j - i, n) == 1):
                a[i, j] = 1.0

    return a
Exemplo n.º 9
0
def i4_wrap(ival, ilo, ihi):

    # *****************************************************************************80
    #
    ## I4_WRAP forces an integer to lie between given limits by wrapping.
    #
    #  Example:
    #
    #    ILO = 4, IHI = 8
    #
    #    I   Value
    #
    #    -2     8
    #    -1     4
    #     0     5
    #     1     6
    #     2     7
    #     3     8
    #     4     4
    #     5     5
    #     6     6
    #     7     7
    #     8     8
    #     9     4
    #    10     5
    #    11     6
    #    12     7
    #    13     8
    #    14     4
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    08 May 2013
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer IVAL, an integer value.
    #
    #    Input, integer ILO, IHI, the desired bounds for the integer value.
    #
    #    Output, integer VALUE, a "wrapped" version of IVAL.
    #
    from i4_modp import i4_modp

    jlo = min(ilo, ihi)
    jhi = max(ilo, ihi)

    wide = jhi - jlo + 1

    if wide == 1:
        value = jlo
    else:
        value = jlo + i4_modp(ival - jlo, wide)

    return value
Exemplo n.º 10
0
def downshift ( n ):

#*****************************************************************************80
#
## DOWNSHIFT returns the DOWNSHIFT matrix.
#
#  Formula:
#
#    if ( I-J == 1 mod ( n ) )
#      A(I,J) = 1
#    else
#      A(I,J) = 0
#
#  Example:
#
#    N = 4
#
#    0 0 0 1
#    1 0 0 0
#    0 1 0 0
#    0 0 1 0
#
#  Rectangular properties:
#
#    A is integral: int ( A ) = A.
#
#    A is a zero/one matrix.
#
#  Square Properties:
#
#    A is generally not symmetric: A' /= A.
#
#    A is nonsingular.
#
#    A is a permutation matrix.
#
#    det ( A ) = (-1)^(N-1)
#
#    A is persymmetric: A(I,J) = A(N+1-J,N+1-I).
#
#    A is a Hankel matrix: constant along anti-diagonals.
#
#    A is an N-th root of the identity matrix.
#    Therefore, the inverse of A = A^(N-1).
#
#    Any circulant matrix generated by a column vector v can be regarded as
#    the Krylov matrix ( v, A*v, A^2*V, ..., A^(N-1)*v).
#
#    The inverse of A is the upshift operator.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    23 January 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the number of rows and columns of the matrix.
#
#    Output, real A(N,N), the matrix.
#
  import numpy as np
  from i4_modp import i4_modp

  a = np.zeros ( [ n, n ] )

  for i in range ( 0, n ):
    for j in range ( 0, n ):
      if ( i4_modp ( i - j, n ) == 1 ):
        a[i,j] = 1.0

  return a
Exemplo n.º 11
0
def josephus(n, m, k):

    #*****************************************************************************80
    #
    ## JOSEPHUS returns the position X of the K-th man to be executed.
    #
    #  Discussion:
    #
    #    The classic Josephus problem concerns a circle of 41 men.
    #    Every third man is killed and removed from the circle.  Counting
    #    and executing continues until all are dead.  Where was the last
    #    survivor sitting?
    #
    #    Note that the first person killed was sitting in the third position.
    #    Moreover, when we get down to 2 people, and we need to count the
    #    "third" one, we just do the obvious thing, which is to keep counting
    #    around the circle until our count is completed.
    #
    #    The process may be regarded as generating a permutation of
    #    the integers from 1 to N.  The permutation would be the execution
    #    list, that is, the list of the executed men, by position number.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    08 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    W W Rouse Ball,
    #    Mathematical Recreations and Essays,
    #    Macmillan, 1962, pages 32-36.
    #
    #    Donald Knuth,
    #    The Art of Computer Programming,
    #    Volume 1, Fundamental Algorithms,
    #    Addison Wesley, 1968, pages 158-159.
    #
    #    Donald Knuth,
    #    The Art of Computer Programming,
    #    Volume 3, Sorting and Searching,
    #    Addison Wesley, 1968, pages 18-19.
    #
    #  Parameters:
    #
    #    Input, integer N, the number of men.
    #    N must be positive.
    #
    #    Input, integer M, the counting index.
    #    M must not be zero.  Ordinarily, M is positive, and no greater than N.
    #
    #    Input, integer K, the index of the executed man of interest.
    #    K must be between 1 and N.
    #
    #    Output, integer X, the position of the K-th man.
    #    X will be between 1 and N.
    #
    from sys import exit
    from i4_modp import i4_modp

    if (n <= 0):
        print ''
        print 'JOSEPHUS - Fatal error!'
        print '  N <= 0.'
        exit('JOSEPHUS - Fatal error!')

    if (m == 0):
        print ''
        print 'JOSEPHUS - Fatal error!'
        print '  M = 0.'
        exit('JOSEPHUS - Fatal error!')

    if (k <= 0 or n < k):
        print ''
        print 'JOSEPHUS - Fatal error!'
        print '  J <= 0 or N < K.'
        exit('JOSEPHUS - Fatal error!')


#
#  In case M is bigger than N, or negative, get the
#  equivalent positive value between 1 and N.
#  You can skip this operation if 1 <= M <= N.
#
    m2 = i4_modp(m, n)

    x = k * m2

    while (n < x):
        x = ((m2 * (x - n) - 1) // (m2 - 1))

    return x