I have some code that uses large integers in its calculation. As I also use numpy it seems that some of the variables are set as type 'numpy.int64' which means they over flow. How can I get round this?
If you run the code below you will see lines under "debugging information". For example
19 1 72.7204246831 524288
19 2 2437717.7229 274877906944
19 3 149857055799.0 144115188075855872
19 4 1.73379003246e+16 0
where the first two columns are n and w and the last column is meant to be 2**(n*w). Clearly 0 is an overflow error.
How can I get round this?
#!/usr/bin/python
from __future__ import division
from scipy.misc import comb
from scipy.misc import factorial
import math
import numpy as np
N = 20
X = np.arange(2,N)
def k_loop_n(w,n):
K = np.arange(0, w+1)
return (comb(w,K)*(comb(w,K)/2**w)**n).sum()
def w_loop(n):
print "w loop"
v = [comb(n,w)*k_loop_n(w,n) for w in range(1,n+1)]
print v
return sum(v)
#This is meant to be an upper bound for sum (w choose i)^(n+1), i = 0..w
def sum_upper(i,n):
return (i+1)*((2**i)*math.sqrt(2/(i*np.pi))*(1-1/(4*i)))**(n+1)
def w_loop_upv2(n):
print "w loop upper bound"
print "debugging info"
print type(n)
for w in range(1,n+1):
print n, w, sum_upper(w,n), 2**(w*n)
v = [comb(n,w)*sum_upper(w,n)/2**(w*n) for w in range(1,n+1)]
return sum(v)
def upper_k_loop(w,n):
K = np.arange(0, w+1)
return (upperbin(w,K)*(upperbin(w,K)/2**w)**(3*float(n)/np.log(n))).sum()
def upper_w_loop(n):
v = [upperbin(n,w)*k_loop(w,n) for w in range(1,n+1)]
return sum(v)
print X
Y = [w_loop(n) for n in X]
Yupper = [w_loop_upv2(n) for n in X]
print Y
print Yupper