def field_isomorphism_pslq(a, b): """Construct field isomorphism using PSLQ algorithm. """ if not a.root.is_real or not b.root.is_real: raise NotImplementedError("PSLQ doesn't support complex coefficients") f = a.minpoly g = b.minpoly.replace(f.gen) n, m, prev = 100, b.minpoly.degree(), None for i in range(1, 5): A = a.root.evalf(n) B = b.root.evalf(n) basis = [1, B] + [ B**i for i in xrange(2, m) ] + [A] dps, mp.dps = mp.dps, n coeffs = pslq(basis, maxcoeff=int(1e10), maxsteps=1000) mp.dps = dps if coeffs is None: break if coeffs != prev: prev = coeffs else: break coeffs = [S(c)/coeffs[-1] for c in coeffs[:-1]] while not coeffs[-1]: coeffs.pop() coeffs = list(reversed(coeffs)) h = Poly(coeffs, f.gen, domain='QQ') if f.compose(h).rem(g).is_zero: d, approx = len(coeffs) - 1, 0 for i, coeff in enumerate(coeffs): approx += coeff*B**(d - i) if A*approx < 0: return [ -c for c in coeffs ] else: return coeffs elif f.compose(-h).rem(g).is_zero: return [ -c for c in coeffs ] else: n *= 2 return None
def field_isomorphism_pslq(a, b): """Construct field isomorphism using PSLQ algorithm. """ if not a.root.is_real or not b.root.is_real: raise NotImplementedError("PSLQ doesn't support complex coefficients") f = a.minpoly g = b.minpoly.replace(f.gen) n, m, prev = 100, b.minpoly.degree(), None for i in range(1, 5): A = a.root.evalf(n) B = b.root.evalf(n) basis = [1, B] + [B**i for i in xrange(2, m)] + [A] dps, mp.dps = mp.dps, n coeffs = pslq(basis, maxcoeff=int(1e10), maxsteps=1000) mp.dps = dps if coeffs is None: break if coeffs != prev: prev = coeffs else: break coeffs = [S(c) / coeffs[-1] for c in coeffs[:-1]] while not coeffs[-1]: coeffs.pop() coeffs = list(reversed(coeffs)) h = Poly(coeffs, f.gen, domain='QQ') if f.compose(h).rem(g).is_zero: d, approx = len(coeffs) - 1, 0 for i, coeff in enumerate(coeffs): approx += coeff * B**(d - i) if A * approx < 0: return [-c for c in coeffs] else: return coeffs elif f.compose(-h).rem(g).is_zero: return [-c for c in coeffs] else: n *= 2 return None
##Consider r = 1.2599210498948732. Apply the PSLQ ##algorithm to find the algebraic number that is closest to r. from sympy.mpmath import pslq r = 1.2599210498948732 print pslq([r**n for n in xrange(5)]) # [0, 2, 0, 0, -1] # This shows that r is a root of 0+2*x+0*x**2+0*x**3-1x**4 # =2*x-x**4=x(2-x^3) # The algebraic number corresponding to r is 2**(1/3.0) print 2**(1/3.0)
# L-1 MCS 507 Mon 27 Aug 2012 : hex4pi.py # Discovery of the formula to compute hexadecimal digits of pi # with the PSLQ algorithm using the PSLQ algorithm of sympy. from sympy.mpmath import pslq S = [sum([1.0/(16**k*(8*k+j)) \ for k in xrange(8)]) \ for j in xrange(1,8)] print S import math S.append(math.pi) P = pslq(S) print P
##Consider r = 1.2599210498948732. Apply the PSLQ ##algorithm to find the algebraic number that is closest to r. from sympy.mpmath import pslq r = 1.2599210498948732 print pslq([r**n for n in xrange(5)]) # [0, 2, 0, 0, -1] # This shows that r is a root of 0+2*x+0*x**2+0*x**3-1x**4 # =2*x-x**4=x(2-x^3) # The algebraic number corresponding to r is 2**(1/3.0) print 2**(1 / 3.0)