def modular_root(x, p): """ Find the square root of a quadratic residue x modulo a prime p using the Tonelli-Shanks algorithm. Input: quadratic residue x, prime p Output: r in Z/pZ such that r^2 = x, None if no such root exists """ # Check that x is a quadratic residue mod p if not legendre(x, p): return None # Find q and s such that p - 1 = q * 2^s with q odd q = p - 1 s = 0 while q % 2 == 0: q = q // 2 s += 1 # Find a quadratic non-residue z z = 2 while legendre(z, p): z += 1 # Set initial parameters m = s c = pow(z, q, p) t = pow(x, q, p) r = pow(x, (q + 1)//2, p) # Find successive pairs of r and t that satisfy r^2 == n * t (mod p) # and t is a 2^(m-1) root of 1. Once t == 1 (mod p), we have found # r to be the square root of x. while True: if t == 0: return 0 elif t == 1: return r else: for i in range(1, m + 1): if i == m: return None if pow(t, 2**i, p) == 1: break b = pow(c, 2**(m - i - 1), p) m = i c = pow(b, 2, p) t = (t * c) % p r = (r * b) % p
def lagrange1st(N): # Calculation of 1st derivatives of Lagrange polynomials # at GLL collocation points # out = legendre1st(N) # out is a matrix with columns -> GLL nodes # rows -> order from gll import gll from lagrange import lagrange from legendre import legendre import numpy as np out = np.zeros([N+1, N+1]) [xi, w] = gll(N) # initialize dij matrix (see Funaro 1993 or Diploma thesis Bernhard Schuberth) d = np.zeros([N+1, N+1]) for i in range (-1, N): for j in range (-1, N): if i != j: d[i+1,j+1] = legendre(N,xi[i+1])/legendre(N,xi[j+1])*1/(xi[i+1]-xi[j+1]) if i == -1: if j == -1: d[i+1,j+1] = float(-1)/float(4)*N*(N+1) if i == N-1: if j == N-1: d[i+1,j+1] = float(1)/float(4)*N*(N+1) # Calculate matrix with 1st derivatives of Lagrange polynomials for n in range (-1, N): for i in range (-1, N): sum=0 for j in range(-1, N): sum = sum + d[i+1,j+1]*lagrange(N,n,xi[j+1]) out[n+1,i+1] = sum return(out)
def multicoes(rs, qtopos, nmax=60): coes = [0. for n in range(nmax + 1)] for n in range(nmax + 1): for q, pos in qtopos.items(): rmag, rimag, costheta = decomp(rs, pos) val = q * (rimag**n) * legendre(n, costheta)[0] coes[n] += val return coes
def lagrange1st(N): # Calculation of 1st derivatives of Lagrange polynomials # at GLL collocation points # out = legendre1st(N) # out is a matrix with columns -> GLL nodes # rows -> order from gll import gll from lagrange import lagrange from legendre import legendre import numpy as np out = np.zeros([N + 1, N + 1]) [xi, w] = gll(N) # initialize dij matrix (see Funaro 1993 or Diploma thesis Bernhard Schuberth) d = np.zeros([N + 1, N + 1]) for i in range(-1, N): for j in range(-1, N): if i != j: d[i + 1, j + 1] = legendre(N, xi[i + 1]) / legendre( N, xi[j + 1]) * 1 / (xi[i + 1] - xi[j + 1]) if i == -1: if j == -1: d[i + 1, j + 1] = float(-1) / float(4) * N * (N + 1) if i == N - 1: if j == N - 1: d[i + 1, j + 1] = float(1) / float(4) * N * (N + 1) # Calculate matrix with 1st derivatives of Lagrange polynomials for n in range(-1, N): for i in range(-1, N): sum = 0 for j in range(-1, N): sum = sum + d[i + 1, j + 1] * lagrange(N, n, xi[j + 1]) out[n + 1, i + 1] = sum return (out)
def legendreMatrix(n): primes = primeList(2, n) array = [] for i in primes: row = [] for j in primes: if i != j: row.append(legendre(i, j)) else: row.append(0) array.append(row) return numpy.array(array)
def legnewton(n, xold, kmax=200, tol=1.e-8): for k in range(1, kmax): val, dval = legendre(n, xold) xnew = xold - val / dval xdiff = xnew - xold if abs(xdiff / xnew) < tol: break xold = xnew else: xnew = None return xnew
def Jacob(a, m): a = a % m exponent = 0 factor_list = factoring(m) # print(factor_list) legendre_list = [] flag = 1 for factor in factor_list: legendre_list.append(legendre(a, factor)) for legendre_flag in legendre_list: flag *= legendre_flag # print(legendre_list) return flag
def factor_base(n, t): # res = [-1] # if n % 2 == 0: # res += [2] # pi = skip(1, primes()) # skip 2 res = [-1] pi = primes() p = next(pi) while p <= t: # if legendre(n, p) == 1: if legendre(n, p) != -1: res += [p] p = next(pi) return res
def lagrange1st(N): """ # Calculation of 1st derivatives of Lagrange polynomials # at GLL collocation points # out = legendre1st(N) # out is a matrix with columns -> GLL nodes # rows -> order """ out = np.zeros([N+1, N+1]) [xi, w] = gll(N) # initialize dij matrix (see Funaro 1993 or Diploma thesis Bernhard # Schuberth) d = np.zeros([N + 1, N + 1]) for i in range(-1, N): for j in range(-1, N): if i != j: d[i + 1, j + 1] = legendre(N, xi[i + 1]) / \ legendre(N, xi[j + 1]) * 1.0 / (xi[i + 1] - xi[j + 1]) if i == -1: if j == -1: d[i + 1, j + 1] = -1.0 / 4.0 * N * (N + 1) if i == N-1: if j == N-1: d[i + 1, j + 1] = 1.0 / 4.0 * N * (N + 1) # Calculate matrix with 1st derivatives of Lagrange polynomials for n in range(-1, N): for i in range(-1, N): sum = 0 for j in range(-1, N): sum = sum + d[i + 1, j + 1] * lagrange(N, n, xi[j + 1]) out[n + 1, i + 1] = sum return(out)
def legendre_determinant_test(): #*****************************************************************************80 # ## LEGENDRE_DETERMINANT_TEST tests LEGENDRE_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # from legendre import legendre from r8mat_print import r8mat_print print '' print 'LEGENDRE_DETERMINANT_TEST' print ' LEGENDRE_DETERMINANT computes the LEGENDRE determinant.' m = 5 n = m a = legendre(n) r8mat_print(m, n, a, ' LEGENDRE matrix:') value = legendre_determinant(n) print ' Value = %g' % (value) print '' print 'LEGENDRE_DETERMINANT_TEST' print ' Normal end of execution.' return
def legendre_determinant_test ( ): #*****************************************************************************80 # ## LEGENDRE_DETERMINANT_TEST tests LEGENDRE_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # from legendre import legendre from r8mat_print import r8mat_print print '' print 'LEGENDRE_DETERMINANT_TEST' print ' LEGENDRE_DETERMINANT computes the LEGENDRE determinant.' m = 5 n = m a = legendre ( n ) r8mat_print ( m, n, a, ' LEGENDRE matrix:' ) value = legendre_determinant ( n ) print ' Value = %g' % ( value ) print '' print 'LEGENDRE_DETERMINANT_TEST' print ' Normal end of execution.' return
def integrate(a, b, n, f): l = legendre(n) A = np.zeros((n, n)) B = np.zeros((n, 1)) for k in range(n): row = [] for i in range(n): A[k, i] = l[i]**k B[k] = quadrature(k) """D = np.linalg.inv(A) D = np.matrix(D) C = D * B C = C.transpose() Ai = np.array(C.ravel()) Ai = nparray_to_list(Ai)[0]""" D = matrix.inv(A) Ai = matrix.multi(D, B) return (b - a) / 2 * sum(Ai[i] * f((b - a) / 2 * l[i] + (a + b) / 2) for i in range(n))
def legendre_test(): #*****************************************************************************80 # ## LEGENDRE_TEST tests LEGENDRE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # from r8mat_print import r8mat_print print '' print 'LEGENDRE_TEST' print ' LEGENDRE computes the LEGENDRE matrix.' m = 5 n = m a = legendre(n) r8mat_print(m, n, a, ' LEGENDRE matrix:') print '' print 'LEGENDRE_TEST' print ' Normal end of execution.' return
def legendre_test ( ): #*****************************************************************************80 # ## LEGENDRE_TEST tests LEGENDRE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 February 2015 # # Author: # # John Burkardt # from r8mat_print import r8mat_print print '' print 'LEGENDRE_TEST' print ' LEGENDRE computes the LEGENDRE matrix.' m = 5 n = m a = legendre ( n ) r8mat_print ( m, n, a, ' LEGENDRE matrix:' ) print '' print 'LEGENDRE_TEST' print ' Normal end of execution.' return
def gauleg_params(n): xs = legroots(n) cs = 2 / ((1 - xs**2) * legendre(n, xs)[1]**2) return xs, cs
def testBasic(self): self.assertEqual(legendre(2, 15), 1)
def testBasic(self): self.assertEqual(legendre(2,15),1)
def setUp(self): self.libc = cdll.LoadLibrary("../BUILD/libfem1d.so") self.lp = legendre()
z = S('z') μ = S('μ') J2 = S('J2') J3 = S('J3') J4 = S('J4') J5 = S('J5') J6 = S('J6') J7 = S('J7') J8 = S('J8') R = S('R') r = sqrt(x**2 + y**2+ z**2) sinθ = z/r Φ = atan2(y,x) u = -μ/r *(1 + (J2*legendre(0,2,sinθ)/(r/R)**2 + J3*legendre(0,3,sinθ)/(r/R)**3 + J4*legendre(0,4,sinθ)/(r/R)**4 + J5*legendre(0,5,sinθ)/(r/R)**5) + #m,n m,n m m,n m n (legendre(1,2,sinθ)*(coeficientes(1,2)[0]*cos(1*Φ)+coeficientes(1,2)[1]*sin(1*Φ)) /(r/R)**2 + # n = 2, m = 1 legendre(2,2,sinθ)*(coeficientes(2,2)[0]*cos(2*Φ)+coeficientes(2,2)[1]*sin(2*Φ)) /(r/R)**2 + # n = 2, m = 2 legendre(1,3,sinθ)*(coeficientes(1,3)[0]*cos(1*Φ)+coeficientes(1,3)[1]*sin(1*Φ)) /(r/R)**3 +# n = 3, m = 1 legendre(2,3,sinθ)*(coeficientes(2,3)[0]*cos(2*Φ)+coeficientes(2,3)[1]*sin(2*Φ)) /(r/R)**3 +# n = 3, m = 2 legendre(3,3,sinθ)*(coeficientes(3,3)[0]*cos(3*Φ)+coeficientes(3,3)[1]*sin(3*Φ)) /(r/R)**3 +# n = 3, m = 3 legendre(1,4,sinθ)*(coeficientes(1,4)[0]*cos(1*Φ)+coeficientes(1,4)[1]*sin(1*Φ)) /(r/R)**4 +# n = 4, m = 1 legendre(2,4,sinθ)*(coeficientes(2,4)[0]*cos(2*Φ)+coeficientes(2,4)[1]*sin(2*Φ)) /(r/R)**4 +# n = 4, m = 2 legendre(3,4,sinθ)*(coeficientes(3,4)[0]*cos(3*Φ)+coeficientes(3,4)[1]*sin(3*Φ)) /(r/R)**4 +# n = 4, m = 3 legendre(4,4,sinθ)*(coeficientes(4,4)[0]*cos(4*Φ)+coeficientes(4,4)[1]*sin(4*Φ)) /(r/R)**4 +# n = 4, m = 4