def content(self): """Return the content of a Polynomial. Usage: ====== Returns the content, that is, the positive greatest common divisor of the (integer) coefficients. Examples: ========= >>> x, y = symbols('xy') >>> f = Polynomial(6*x + 20*y + 4*x*y) >>> f.content() 2 Also see L{as_primitive}, L{leading_coeff}. """ result = 0 for term in self.coeffs: if not isinstance(term[0], Integer): print "%s is no integer coefficient!" % term[0] return S.Zero result = abs(numbers.gcd(result, abs(int(term[0])))) return Integer(result)
def pollard_rho(n, max_iters=5, seed=1234): """Use Pollard's rho method to try to extract a factor of n. The returned factor may be a composite number. A maximum of max_iters iterations are performed; if no factor is found, None is returned. The rho algorithm is a Monte Carlo method whose outcome can be affected by changing the random seed value. References ========== Richard Crandall & Carl Pomerance (2005), "Prime Numbers: A Computational Perspective", Springer, 2nd edition, 229-231 """ random.seed(seed + max_iters) for i in range(max_iters): a = random.randint(1, n-3) s = random.randint(0, n-1) U = V = s F = lambda x: (x**2 + a) % a while 1: U = F(U) V = F(F(V)) g = numbers.gcd(U-V, n) if g == 1: continue if g == n: break return g return None
def pollard_pm1(n, B=10, seed=1234): """Use Pollard's p-1 method to try to extract a factor of n. The returned factor may be a composite number. The search is performed up to a smoothness bound B; if no factor is found, None is returned. The p-1 algorithm is a Monte Carlo method whose outcome can be affected by changing the random seed value. Example usage ============= With the default smoothness bound, this number can't be cracked: >>> pollard_pm1(21477639576571) Increasing the smoothness bound helps: >>> pollard_pm1(21477639576571, 2000) 4410317L References ========== Richard Crandall & Carl Pomerance (2005), "Prime Numbers: A Computational Perspective", Springer, 2nd edition, 236-238 """ from math import log random.seed(seed + B) a = random.randint(2, n-1) for p in sieve.primerange(2, B): e = int(log(B, p)) a = pow(a, p**e, n) g = numbers.gcd(a-1, n) if 1 < g < n: return g else: return None
def A(n, j): if j == 1: return Float(1) s = Float(0) pi = pi_float() for h in xrange(1, j): if gcd(h,j) == 1: s += cos((g(h,j)-2*h*n)*pi/j) return s
def as_integer(self): """Return the polynomial with integer coefficients. Usage: ====== Starting from an instance of Polynomial with only rational coefficients, this function multiplies it with the common denominator. The result is a tuple consisting of the factor applied to the coefficients and a new instance of Polynomial in the integers. Example: ======== >>> x, y = symbols('xy') >>> f = Polynomial(x/6 + y/4 + x*y/3) >>> denominator, f = f.as_integer() >>> print denominator 12 >>> print f 2*x + 3*y + 4*x*y >>> denominator, f = f.as_integer() >>> print denominator 1 >>> print f 2*x + 3*y + 4*x*y Also see L{as_monic}, L{as_primitive}. """ denom = S.One for term in self.coeffs: if not isinstance(term[0], Rational): print "%s is no rational coefficient!" % term[0] return S.Zero, self else: # Compute the least common multiple of the denominators: denom = term[0].q*denom/numbers.gcd(int(denom), int(term[0].q)) if denom is S.One: return S.One, self else: return (denom, Polynomial(coeffs=tuple([((term[0]*denom),) + term[1:] for term in self.coeffs]), var=self.var, order=self.order))