Exemplo n.º 1
0
def pepins_test(k, Fn):
    leg = legendre_symbol(k, Fn)
    jac = jacobi(k, Fn, verbose=False)
    print("Fn\t\t:\t{}".format(Fn))
    print("Legendre Symbol\t:\t{}".format(leg))
    print("Jacobi Symbol\t:\t{}".format(jac))
    if leg == jac:
        return True
    else:
        return False
Exemplo n.º 2
0
def conference_inverse ( n ):

#*****************************************************************************80
#
## CONFERENCE_INVERSE returns the inverse of the CONFERENCE matrix.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    19 April 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the order of the matrix.
#
#    Output, real CONFERENCE_INVERSE[N,N], the matrix.
#
  import numpy as np
  from legendre_symbol import legendre_symbol

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

  for j in range ( 0, n ):
    for i in range ( 0, n ):
      if ( i == 0 and j == 0 ):
        a[i,j] = 0.0
      elif ( i == 0 ):
        a[i,j] = 1.0
      elif ( j == 0 ):
        if ( ( ( n - 1 ) % 4 ) == 1 ):
          a[i,j] = 1.0
        else:
          a[i,j] = - 1.0
      else:
        l = legendre_symbol ( i - j, n - 1 )
        a[i,j] = float ( l )

  if ( ( ( n - 1 ) % 4 ) == 3 ):
    for i in range ( 0, n ):
      for j in range ( 0, n ):
        a[i,j] = - a[i,j]

  if ( 1 < n ):
    for i in range ( 0, n ):
      for j in range ( 0, n ):
        a[i,j] = a[i,j] / float ( n - 1 )

  return a
Exemplo n.º 3
0
def conference_inverse(n):

    #*****************************************************************************80
    #
    ## CONFERENCE_INVERSE returns the inverse of the CONFERENCE matrix.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    19 April 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the order of the matrix.
    #
    #    Output, real CONFERENCE_INVERSE[N,N], the matrix.
    #
    import numpy as np
    from legendre_symbol import legendre_symbol

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

    for j in range(0, n):
        for i in range(0, n):
            if (i == 0 and j == 0):
                a[i, j] = 0.0
            elif (i == 0):
                a[i, j] = 1.0
            elif (j == 0):
                if (((n - 1) % 4) == 1):
                    a[i, j] = 1.0
                else:
                    a[i, j] = -1.0
            else:
                l = legendre_symbol(i - j, n - 1)
                a[i, j] = float(l)

    if (((n - 1) % 4) == 3):
        for i in range(0, n):
            for j in range(0, n):
                a[i, j] = -a[i, j]

    if (1 < n):
        for i in range(0, n):
            for j in range(0, n):
                a[i, j] = a[i, j] / float(n - 1)

    return a
Exemplo n.º 4
0
def conference ( n ):

#*****************************************************************************80
#
## CONFERENCE returns the CONFERENCE matrix.
#
#  Discussion:
#
#    A conference matrix is a square matrix A of order N, with a zero
#    diagonal, and only 1's and -1's on the offdiagonal, with the property
#    that:
#
#      A * A' = (N-1) * I.
#
#    The algorithm employed here is only valid when N - 1
#    is an odd prime, or a power of an odd prime.
#
#    Conference matrices have a relationship with Hadamard matrices:
#
#      If mod ( P, 4 ) == 3, A is antisymmetric, and
#        I + A is hadamard;
#      Else A is symmetric, and
#        (   I + A, - I + A )
#        ( - I + A, - I - A) is Hadamard.
#
#  Example:
#
#    N = 8
#
#     0  1  1  1  1  1  1  1
#    -1  0 -1 -1  1 -1  1  1
#    -1  1  0 -1 -1  1 -1  1
#    -1  1  1  0 -1 -1  1 -1
#    -1 -1  1  1  0 -1 -1  1
#    -1  1 -1  1  1  0 -1 -1
#    -1 -1  1 -1  1  1  0 -1
#    -1 -1 -1  1 -1  1  1  0
#
#  Properties:
#
#    If N-1 is prime, then A[2:N,2:N] is a circulant matrix.
#
#    If N-1 = 1 mod 4, then A is symmetric.
#
#    If N-1 = 3 mod 4, then A is antisymmetric.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    19 April 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the order of the matrix.  N-1 must be an
#    odd prime, or a power of an odd prime.
#
#    Output, real A(N,N), the matrix.
#
  import numpy as np
  from legendre_symbol import legendre_symbol

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

  for j in range ( 0, n ):
    for i in range ( 0, n ):
      if ( i == 0 and j == 0 ):
        a[i,j] = 0.0
      elif ( i == 0 ):
        a[i,j] = 1.0
      elif ( j == 0 ):
        if ( ( ( n - 1 ) % 4 ) == 1 ):
          a[i,j] = 1.0
        else:
          a[i,j] = -1.0
      else:
        l = legendre_symbol ( i - j, n - 1 )
        a[i,j] = float ( l )

  return a
Exemplo n.º 5
0
def jacobi_symbol(q, p):

    #*****************************************************************************80
    #
    ## JACOBI_SYMBOL evaluates the Jacobi symbol (Q/P).
    #
    #  Definition:
    #
    #    If P is prime, then
    #
    #      Jacobi Symbol (Q/P) = Legendre Symbol (Q/P)
    #
    #    Else
    #
    #      let P have the prime factorization
    #
    #        P = Product ( 1 <= I <= N ) P(I)^E(I)
    #
    #      Jacobi Symbol (Q/P) =
    #
    #        Product ( 1 <= I <= N ) Legendre Symbol (Q/P(I))^E(I)
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    17 February 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    Daniel Zwillinger,
    #    CRC Standard Mathematical Tables and Formulae,
    #    30th Edition,
    #    CRC Press, 1996, pages 86-87.
    #
    #  Parameters:
    #
    #    Input, integer Q, an integer whose Jacobi symbol with
    #    respect to P is desired.
    #
    #    Input, integer P, the number with respect to which the Jacobi
    #    symbol of Q is desired.  P should be 2 or greater.
    #
    #    Output, integer J, the Jacobi symbol (Q/P).
    #    Ordinarily, J will be -1, 0 or 1.
    #    -2, not enough factorization space.
    #    -3, an error during Legendre symbol calculation.
    #
    from i4_factor import i4_factor
    from legendre_symbol import legendre_symbol
    #
    #  P must be greater than 1.
    #
    if (p <= 1):
        print('')
        print('JACOBI_SYMBOL - Fatal error!')
        print('  P must be greater than 1.')
        j = -2
        return l
#
#  Decompose P into factors of prime powers.
#
    nfactor, factor, power, nleft = i4_factor(p)

    if (nleft != 1):
        print('')
        print('JACOBI_SYMBOL - Fatal error!')
        print('  Not enough factorization space.')
        j = -2
        return j
#
#  Force Q to be nonnegative.
#
    qq = q

    while (qq < 0):
        qq = qq + p


#
#  For each prime factor, compute the Legendre symbol, and
#  multiply the Jacobi symbol by the appropriate factor.
#
    j = 1
    for i in range(0, nfactor):
        pp = factor[i]
        l = legendre_symbol(qq, pp)
        if (l < -1):
            print('')
            print('JACOBI_SYMBOL - Fatal error!')
            print('  Error during Legendre symbol calculation.')
            j = -3

        j = j * l**power[i]

    return j
Exemplo n.º 6
0
def jacobi_symbol ( q, p ):

#*****************************************************************************80
#
## JACOBI_SYMBOL evaluates the Jacobi symbol (Q/P).
#
#  Definition:
#
#    If P is prime, then
#
#      Jacobi Symbol (Q/P) = Legendre Symbol (Q/P)
#
#    Else 
#
#      let P have the prime factorization
#
#        P = Product ( 1 <= I <= N ) P(I)^E(I)
#
#      Jacobi Symbol (Q/P) =
#
#        Product ( 1 <= I <= N ) Legendre Symbol (Q/P(I))^E(I)
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    17 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Daniel Zwillinger,
#    CRC Standard Mathematical Tables and Formulae,
#    30th Edition,
#    CRC Press, 1996, pages 86-87.
#
#  Parameters:
#
#    Input, integer Q, an integer whose Jacobi symbol with
#    respect to P is desired.
#
#    Input, integer P, the number with respect to which the Jacobi
#    symbol of Q is desired.  P should be 2 or greater.
#
#    Output, integer J, the Jacobi symbol (Q/P).
#    Ordinarily, J will be -1, 0 or 1.
#    -2, not enough factorization space.
#    -3, an error during Legendre symbol calculation.
#
  from i4_factor import i4_factor
  from legendre_symbol import legendre_symbol
#
#  P must be greater than 1.
#
  if ( p <= 1 ):
    print ('')
    print ('JACOBI_SYMBOL - Fatal error!')
    print ('  P must be greater than 1.')
    j = -2
    return 1
#
#  Decompose P into factors of prime powers.
#
  nfactor, factor, power, nleft = pfdic(p)#i4_factor ( p )

  if ( nleft != 1 ):
    print ('')
    print ('JACOBI_SYMBOL - Fatal error!')
    print ('  Not enough factorization space.')
    j = -2
    return j
#
#  Force Q to be nonnegative.
#
  qq = q

  while ( qq < 0 ):
    qq = qq + p
#
#  For each prime factor, compute the Legendre symbol, and
#  multiply the Jacobi symbol by the appropriate factor.
#
  j = 1
  for i in range ( 0, nfactor ):
    pp = factor[i]
    l = legendre_symbol ( qq, pp )
    if ( l < -1 ):
      print ('')
      print ('JACOBI_SYMBOL - Fatal error!')
      print ('  Error during Legendre symbol calculation.')
      j = -3

    j = j * l ** power[i]

  return j
Exemplo n.º 7
0
def conference(n):

    #*****************************************************************************80
    #
    ## CONFERENCE returns the CONFERENCE matrix.
    #
    #  Discussion:
    #
    #    A conference matrix is a square matrix A of order N, with a zero
    #    diagonal, and only 1's and -1's on the offdiagonal, with the property
    #    that:
    #
    #      A * A' = (N-1) * I.
    #
    #    The algorithm employed here is only valid when N - 1
    #    is an odd prime, or a power of an odd prime.
    #
    #    Conference matrices have a relationship with Hadamard matrices:
    #
    #      If mod ( P, 4 ) == 3, A is antisymmetric, and
    #        I + A is hadamard;
    #      Else A is symmetric, and
    #        (   I + A, - I + A )
    #        ( - I + A, - I - A) is Hadamard.
    #
    #  Example:
    #
    #    N = 8
    #
    #     0  1  1  1  1  1  1  1
    #    -1  0 -1 -1  1 -1  1  1
    #    -1  1  0 -1 -1  1 -1  1
    #    -1  1  1  0 -1 -1  1 -1
    #    -1 -1  1  1  0 -1 -1  1
    #    -1  1 -1  1  1  0 -1 -1
    #    -1 -1  1 -1  1  1  0 -1
    #    -1 -1 -1  1 -1  1  1  0
    #
    #  Properties:
    #
    #    If N-1 is prime, then A[2:N,2:N] is a circulant matrix.
    #
    #    If N-1 = 1 mod 4, then A is symmetric.
    #
    #    If N-1 = 3 mod 4, then A is antisymmetric.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    19 April 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the order of the matrix.  N-1 must be an
    #    odd prime, or a power of an odd prime.
    #
    #    Output, real A(N,N), the matrix.
    #
    import numpy as np
    from legendre_symbol import legendre_symbol

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

    for j in range(0, n):
        for i in range(0, n):
            if (i == 0 and j == 0):
                a[i, j] = 0.0
            elif (i == 0):
                a[i, j] = 1.0
            elif (j == 0):
                if (((n - 1) % 4) == 1):
                    a[i, j] = 1.0
                else:
                    a[i, j] = -1.0
            else:
                l = legendre_symbol(i - j, n - 1)
                a[i, j] = float(l)

    return a