def npartitions(n, verbose=False): """ Calculate the partition function P(n), i.e. the number of ways that n can be written as a sum of positive integers. P(n) is computed using the Hardy-Ramanujan-Rademacher formula [1]_. The correctness of this implementation has been tested through 10**10. Examples ======== >>> from sympy.ntheory import npartitions >>> npartitions(25) 1958 References ========== .. [1] http://mathworld.wolfram.com/PartitionFunctionP.html """ n = int(n) if n < 0: return 0 if n <= 5: return [1, 1, 2, 3, 5, 7][n] if '_factor' not in globals(): _pre() # Estimate number of bits in p(n). This formula could be tidied pbits = int(( math.pi*(2*n/3.)**0.5 - math.log(4*n))/math.log(10) + 1) * \ math.log(10, 2) prec = p = int(pbits*1.1 + 100) s = fzero M = max(6, int(0.24*n**0.5 + 4)) if M > 10**5: raise ValueError("Input too big") # Corresponds to n > 1.7e11 sq23pi = mpf_mul(mpf_sqrt(from_rational(2, 3, p), p), mpf_pi(p), p) sqrt8 = mpf_sqrt(from_int(8), p) for q in range(1, M): a = _a(n, q, p) d = _d(n, q, p, sq23pi, sqrt8) s = mpf_add(s, mpf_mul(a, d), prec) if verbose: print("step", q, "of", M, to_str(a, 10), to_str(d, 10)) # On average, the terms decrease rapidly in magnitude. # Dynamically reducing the precision greatly improves # performance. p = bitcount(abs(to_int(d))) + 50 return int(to_int(mpf_add(s, fhalf, prec)))
def npartitions(n, verbose=False): """ Calculate the partition function P(n), i.e. the number of ways that n can be written as a sum of positive integers. P(n) is computed using the Hardy-Ramanujan-Rademacher formula [1]_. The correctness of this implementation has been tested through 10**10. Examples ======== >>> from sympy.ntheory import npartitions >>> npartitions(25) 1958 References ========== .. [1] http://mathworld.wolfram.com/PartitionFunctionP.html """ n = int(n) if n < 0: return 0 if n <= 5: return [1, 1, 2, 3, 5, 7][n] if '_factor' not in globals(): _pre() # Estimate number of bits in p(n). This formula could be tidied pbits = int(( math.pi*(2*n/3.)**0.5 - math.log(4*n))/math.log(10) + 1) * \ math.log(10, 2) prec = p = int(pbits * 1.1 + 100) s = fzero M = max(6, int(0.24 * n**0.5 + 4)) if M > 10**5: raise ValueError("Input too big") # Corresponds to n > 1.7e11 sq23pi = mpf_mul(mpf_sqrt(from_rational(2, 3, p), p), mpf_pi(p), p) sqrt8 = mpf_sqrt(from_int(8), p) for q in range(1, M): a = _a(n, q, p) d = _d(n, q, p, sq23pi, sqrt8) s = mpf_add(s, mpf_mul(a, d), prec) if verbose: print("step", q, "of", M, to_str(a, 10), to_str(d, 10)) # On average, the terms decrease rapidly in magnitude. # Dynamically reducing the precision greatly improves # performance. p = bitcount(abs(to_int(d))) + 50 return int(to_int(mpf_add(s, fhalf, prec)))
def _d(n, j, prec, sq23pi, sqrt8): """ Compute the sinh term in the outer sum of the HRR formula. The constants sqrt(2/3*pi) and sqrt(8) must be precomputed. """ j = from_int(j) pi = mpf_pi(prec) a = mpf_div(sq23pi, j, prec) b = mpf_sub(from_int(n), from_rational(1, 24, prec), prec) c = mpf_sqrt(b, prec) ch, sh = mpf_cosh_sinh(mpf_mul(a, c), prec) D = mpf_div(mpf_sqrt(j, prec), mpf_mul(mpf_mul(sqrt8, b), pi), prec) E = mpf_sub(mpf_mul(a, ch), mpf_div(sh, c, prec), prec) return mpf_mul(D, E)
def npartitions(n): """Calculate the partition function P(n), i.e. the number of ways that n can be written as a sum of positive integers. P(n) is computed using the Hardy-Ramanujan-Rademacher formula. The correctness of this implementation has been tested for 10**n up to n = 8. Examples ======== >>> npartitions(25) 1958 References ========== * https://mathworld.wolfram.com/PartitionFunctionP.html """ n = int(n) if n < 0: return 0 if n <= 5: return [1, 1, 2, 3, 5, 7][n] # Estimate number of bits in p(n). This formula could be tidied pbits = int((math.pi*(2*n/3.)**0.5 - math.log(4*n))/math.log(10) + 1) * \ math.log(10, 2) prec = p = int(pbits * 1.1 + 100) s = fzero M = max(6, int(0.24 * n**0.5 + 4)) sq23pi = mpf_mul(mpf_sqrt(from_rational(2, 3, p), p), mpf_pi(p), p) sqrt8 = mpf_sqrt(from_int(8), p) for q in range(1, M): a = _a(n, q, p) d = _d(n, q, p, sq23pi, sqrt8) s = mpf_add(s, mpf_mul(a, d), prec) debug('step', q, 'of', M, to_str(a, 10), to_str(d, 10)) # On average, the terms decrease rapidly in magnitude. Dynamically # reducing the precision greatly improves performance. p = bitcount(abs(to_int(d))) + 50 return int(to_int(mpf_add(s, fhalf, prec)))
def npartitions(n): """Calculate the partition function P(n), i.e. the number of ways that n can be written as a sum of positive integers. P(n) is computed using the Hardy-Ramanujan-Rademacher formula. The correctness of this implementation has been tested for 10**n up to n = 8. Examples ======== >>> npartitions(25) 1958 References ========== * http://mathworld.wolfram.com/PartitionFunctionP.html """ n = int(n) if n < 0: return 0 if n <= 5: return [1, 1, 2, 3, 5, 7][n] # Estimate number of bits in p(n). This formula could be tidied pbits = int((math.pi*(2*n/3.)**0.5 - math.log(4*n))/math.log(10) + 1) * \ math.log(10, 2) prec = p = int(pbits*1.1 + 100) s = fzero M = max(6, int(0.24*n**0.5 + 4)) sq23pi = mpf_mul(mpf_sqrt(from_rational(2, 3, p), p), mpf_pi(p), p) sqrt8 = mpf_sqrt(from_int(8), p) for q in range(1, M): a = _a(n, q, p) d = _d(n, q, p, sq23pi, sqrt8) s = mpf_add(s, mpf_mul(a, d), prec) debug("step", q, "of", M, to_str(a, 10), to_str(d, 10)) # On average, the terms decrease rapidly in magnitude. Dynamically # reducing the precision greatly improves performance. p = bitcount(abs(to_int(d))) + 50 return int(to_int(mpf_add(s, fhalf, prec)))
def _make_tol(ctx): hundred = (0, 25, 2, 5) eps = (0, MPZ_ONE, 1-ctx.prec, 1) return mpf_mul(hundred, eps)
def evalf_mul(v, prec, options): res = pure_complex(v) if res: # the only pure complex that is a mul is h*I _, h = res im, _, im_acc, _ = evalf(h, prec, options) return None, im, None, im_acc args = list(v.args) # see if any argument is NaN or oo and thus warrants a special return special = [] from sympy.core.numbers import Float for arg in args: arg = evalf(arg, prec, options) if arg[0] is None: continue arg = Float._new(arg[0], 1) if arg is S.NaN or arg.is_infinite: special.append(arg) if special: from sympy.core.mul import Mul special = Mul(*special) return evalf(special, prec + 4, {}) # With guard digits, multiplication in the real case does not destroy # accuracy. This is also true in the complex case when considering the # total accuracy; however accuracy for the real or imaginary parts # separately may be lower. acc = prec # XXX: big overestimate working_prec = prec + len(args) + 5 # Empty product is 1 start = man, exp, bc = MPZ(1), 0, 1 # First, we multiply all pure real or pure imaginary numbers. # direction tells us that the result should be multiplied by # I**direction; all other numbers get put into complex_factors # to be multiplied out after the first phase. last = len(args) direction = 0 args.append(S.One) complex_factors = [] for i, arg in enumerate(args): if i != last and pure_complex(arg): args[-1] = (args[-1] * arg).expand() continue elif i == last and arg is S.One: continue re, im, re_acc, im_acc = evalf(arg, working_prec, options) if re and im: complex_factors.append((re, im, re_acc, im_acc)) continue elif re: (s, m, e, b), w_acc = re, re_acc elif im: (s, m, e, b), w_acc = im, im_acc direction += 1 else: return None, None, None, None direction += 2 * s man *= m exp += e bc += b if bc > 3 * working_prec: man >>= working_prec exp += working_prec acc = min(acc, w_acc) sign = (direction & 2) >> 1 if not complex_factors: v = normalize(sign, man, exp, bitcount(man), prec, rnd) # multiply by i if direction & 1: return None, v, None, acc else: return v, None, acc, None else: # initialize with the first term if (man, exp, bc) != start: # there was a real part; give it an imaginary part re, im = (sign, man, exp, bitcount(man)), (0, MPZ(0), 0, 0) i0 = 0 else: # there is no real part to start (other than the starting 1) wre, wim, wre_acc, wim_acc = complex_factors[0] acc = min(acc, complex_accuracy((wre, wim, wre_acc, wim_acc))) re = wre im = wim i0 = 1 for wre, wim, wre_acc, wim_acc in complex_factors[i0:]: # acc is the overall accuracy of the product; we aren't # computing exact accuracies of the product. acc = min(acc, complex_accuracy((wre, wim, wre_acc, wim_acc))) use_prec = working_prec A = mpf_mul(re, wre, use_prec) B = mpf_mul(mpf_neg(im), wim, use_prec) C = mpf_mul(re, wim, use_prec) D = mpf_mul(im, wre, use_prec) re = mpf_add(A, B, use_prec) im = mpf_add(C, D, use_prec) if options.get('verbose'): print("MUL: wanted", prec, "accurate bits, got", acc) # multiply by I if direction & 1: re, im = mpf_neg(im), re return re, im, acc, acc
def _a(n, k, prec): """ Compute the inner sum in HRR formula [1]_ References ========== .. [1] http://msp.org/pjm/1956/6-1/pjm-v6-n1-p18-p.pdf """ if k == 1: return fone k1 = k e = 0 p = _factor[k] while k1 % p == 0: k1 //= p e += 1 k2 = k//k1 # k2 = p^e v = 1 - 24*n pi = mpf_pi(prec) if k1 == 1: # k = p^e if p == 2: mod = 8*k v = mod + v % mod v = (v*pow(9, k - 1, mod)) % mod m = _sqrt_mod_prime_power(v, 2, e + 3)[0] arg = mpf_div(mpf_mul( from_int(4*m), pi, prec), from_int(mod), prec) return mpf_mul(mpf_mul( from_int((-1)**e*jacobi_symbol(m - 1, m)), mpf_sqrt(from_int(k), prec), prec), mpf_sin(arg, prec), prec) if p == 3: mod = 3*k v = mod + v % mod if e > 1: v = (v*pow(64, k//3 - 1, mod)) % mod m = _sqrt_mod_prime_power(v, 3, e + 1)[0] arg = mpf_div(mpf_mul(from_int(4*m), pi, prec), from_int(mod), prec) return mpf_mul(mpf_mul( from_int(2*(-1)**(e + 1)*legendre_symbol(m, 3)), mpf_sqrt(from_int(k//3), prec), prec), mpf_sin(arg, prec), prec) v = k + v % k if v % p == 0: if e == 1: return mpf_mul( from_int(jacobi_symbol(3, k)), mpf_sqrt(from_int(k), prec), prec) return fzero if not is_quad_residue(v, p): return fzero _phi = p**(e - 1)*(p - 1) v = (v*pow(576, _phi - 1, k)) m = _sqrt_mod_prime_power(v, p, e)[0] arg = mpf_div( mpf_mul(from_int(4*m), pi, prec), from_int(k), prec) return mpf_mul(mpf_mul( from_int(2*jacobi_symbol(3, k)), mpf_sqrt(from_int(k), prec), prec), mpf_cos(arg, prec), prec) if p != 2 or e >= 3: d1, d2 = igcd(k1, 24), igcd(k2, 24) e = 24//(d1*d2) n1 = ((d2*e*n + (k2**2 - 1)//d1)* pow(e*k2*k2*d2, _totient[k1] - 1, k1)) % k1 n2 = ((d1*e*n + (k1**2 - 1)//d2)* pow(e*k1*k1*d1, _totient[k2] - 1, k2)) % k2 return mpf_mul(_a(n1, k1, prec), _a(n2, k2, prec), prec) if e == 2: n1 = ((8*n + 5)*pow(128, _totient[k1] - 1, k1)) % k1 n2 = (4 + ((n - 2 - (k1**2 - 1)//8)*(k1**2)) % 4) % 4 return mpf_mul(mpf_mul( from_int(-1), _a(n1, k1, prec), prec), _a(n2, k2, prec)) n1 = ((8*n + 1)*pow(32, _totient[k1] - 1, k1)) % k1 n2 = (2 + (n - (k1**2 - 1)//8) % 2) % 2 return mpf_mul(_a(n1, k1, prec), _a(n2, k2, prec), prec)
def _make_tol(self): hundred = (0, 25, 2, 5) eps = (0, MPZ_ONE, 1 - self.prec, 1) return mpf_mul(hundred, eps)
def _a(n, k, prec): """ Compute the inner sum in HRR formula [1]_ References ========== .. [1] http://msp.org/pjm/1956/6-1/pjm-v6-n1-p18-p.pdf """ if k == 1: return fone k1 = k e = 0 p = _factor[k] while k1 % p == 0: k1 //= p e += 1 k2 = k // k1 # k2 = p^e v = 1 - 24 * n pi = mpf_pi(prec) if k1 == 1: # k = p^e if p == 2: mod = 8 * k v = mod + v % mod v = (v * pow(9, k - 1, mod)) % mod m = _sqrt_mod_prime_power(v, 2, e + 3)[0] arg = mpf_div(mpf_mul(from_int(4 * m), pi, prec), from_int(mod), prec) return mpf_mul( mpf_mul(from_int((-1)**e * jacobi_symbol(m - 1, m)), mpf_sqrt(from_int(k), prec), prec), mpf_sin(arg, prec), prec) if p == 3: mod = 3 * k v = mod + v % mod if e > 1: v = (v * pow(64, k // 3 - 1, mod)) % mod m = _sqrt_mod_prime_power(v, 3, e + 1)[0] arg = mpf_div(mpf_mul(from_int(4 * m), pi, prec), from_int(mod), prec) return mpf_mul( mpf_mul(from_int(2 * (-1)**(e + 1) * legendre_symbol(m, 3)), mpf_sqrt(from_int(k // 3), prec), prec), mpf_sin(arg, prec), prec) v = k + v % k if v % p == 0: if e == 1: return mpf_mul(from_int(jacobi_symbol(3, k)), mpf_sqrt(from_int(k), prec), prec) return fzero if not is_quad_residue(v, p): return fzero _phi = p**(e - 1) * (p - 1) v = (v * pow(576, _phi - 1, k)) m = _sqrt_mod_prime_power(v, p, e)[0] arg = mpf_div(mpf_mul(from_int(4 * m), pi, prec), from_int(k), prec) return mpf_mul( mpf_mul(from_int(2 * jacobi_symbol(3, k)), mpf_sqrt(from_int(k), prec), prec), mpf_cos(arg, prec), prec) if p != 2 or e >= 3: d1, d2 = igcd(k1, 24), igcd(k2, 24) e = 24 // (d1 * d2) n1 = ((d2 * e * n + (k2**2 - 1) // d1) * pow(e * k2 * k2 * d2, _totient[k1] - 1, k1)) % k1 n2 = ((d1 * e * n + (k1**2 - 1) // d2) * pow(e * k1 * k1 * d1, _totient[k2] - 1, k2)) % k2 return mpf_mul(_a(n1, k1, prec), _a(n2, k2, prec), prec) if e == 2: n1 = ((8 * n + 5) * pow(128, _totient[k1] - 1, k1)) % k1 n2 = (4 + ((n - 2 - (k1**2 - 1) // 8) * (k1**2)) % 4) % 4 return mpf_mul(mpf_mul(from_int(-1), _a(n1, k1, prec), prec), _a(n2, k2, prec)) n1 = ((8 * n + 1) * pow(32, _totient[k1] - 1, k1)) % k1 n2 = (2 + (n - (k1**2 - 1) // 8) % 2) % 2 return mpf_mul(_a(n1, k1, prec), _a(n2, k2, prec), prec)
def evalf_mul(v, prec, options): res = pure_complex(v) if res: # the only pure complex that is a mul is h*I _, h = res im, _, im_acc, _ = evalf(h, prec, options) return None, im, None, im_acc args = list(v.args) # see if any argument is NaN or oo and thus warrants a special return special = [] from sympy.core.numbers import Float for arg in args: arg = evalf(arg, prec, options) if arg[0] is None: continue arg = Float._new(arg[0], 1) if arg is S.NaN or arg.is_infinite: special.append(arg) if special: from sympy.core.mul import Mul special = Mul(*special) return evalf(special, prec + 4, {}) # With guard digits, multiplication in the real case does not destroy # accuracy. This is also true in the complex case when considering the # total accuracy; however accuracy for the real or imaginary parts # separately may be lower. acc = prec # XXX: big overestimate working_prec = prec + len(args) + 5 # Empty product is 1 start = man, exp, bc = MPZ(1), 0, 1 # First, we multiply all pure real or pure imaginary numbers. # direction tells us that the result should be multiplied by # I**direction; all other numbers get put into complex_factors # to be multiplied out after the first phase. last = len(args) direction = 0 args.append(S.One) complex_factors = [] for i, arg in enumerate(args): if i != last and pure_complex(arg): args[-1] = (args[-1]*arg).expand() continue elif i == last and arg is S.One: continue re, im, re_acc, im_acc = evalf(arg, working_prec, options) if re and im: complex_factors.append((re, im, re_acc, im_acc)) continue elif re: (s, m, e, b), w_acc = re, re_acc elif im: (s, m, e, b), w_acc = im, im_acc direction += 1 else: return None, None, None, None direction += 2*s man *= m exp += e bc += b if bc > 3*working_prec: man >>= working_prec exp += working_prec acc = min(acc, w_acc) sign = (direction & 2) >> 1 if not complex_factors: v = normalize(sign, man, exp, bitcount(man), prec, rnd) # multiply by i if direction & 1: return None, v, None, acc else: return v, None, acc, None else: # initialize with the first term if (man, exp, bc) != start: # there was a real part; give it an imaginary part re, im = (sign, man, exp, bitcount(man)), (0, MPZ(0), 0, 0) i0 = 0 else: # there is no real part to start (other than the starting 1) wre, wim, wre_acc, wim_acc = complex_factors[0] acc = min(acc, complex_accuracy((wre, wim, wre_acc, wim_acc))) re = wre im = wim i0 = 1 for wre, wim, wre_acc, wim_acc in complex_factors[i0:]: # acc is the overall accuracy of the product; we aren't # computing exact accuracies of the product. acc = min(acc, complex_accuracy((wre, wim, wre_acc, wim_acc))) use_prec = working_prec A = mpf_mul(re, wre, use_prec) B = mpf_mul(mpf_neg(im), wim, use_prec) C = mpf_mul(re, wim, use_prec) D = mpf_mul(im, wre, use_prec) re = mpf_add(A, B, use_prec) im = mpf_add(C, D, use_prec) if options.get('verbose'): print("MUL: wanted", prec, "accurate bits, got", acc) # multiply by I if direction & 1: re, im = mpf_neg(im), re return re, im, acc, acc