My second attempt to use numba also fails but this time with a
different error message. My code is
import numpy as np
import itertools
from math import factorial as fac
import sys
from numba import jit
@jit
def fast_glynn_perm(M):
row_comb = []
for column in range(len(M[0])):
t = 0
for row in M:
t += row[column]
row_comb.append(t)
n=len(M)
total = 0
old_grey = 0
sign = +1
num_loops = 2**(n-1)
for bin_index in xrange(1, num_loops + 1):
i_product = 1
for row_num in row_comb:
i_product = i_product * row_num
total += sign * i_product
new_grey = bin_index^(bin_index/2)
grey_diff = old_grey ^ new_grey
grey_diff_index = grey_diff.bit_length()-1
new_vector = M[grey_diff_index]
direction = 2 * cmp(old_grey,new_grey)
for i in range(n):
row_comb[i] += new_vector[i] * direction
sign = -sign
old_grey = new_grey
return total/num_loops
def rvs(dim=3):
random_state = np.random
H = np.eye(dim)
D = np.ones((dim,))
for n in range(1, dim):
x = random_state.normal(size=(dim-n+1,))
D[n-1] = np.sign(x[0])
x[0] -= D[n-1]*np.sqrt((x*x).sum())
# Householder transformation
Hx = (np.eye(dim-n+1) - 2.*np.outer(x, x)/(x*x).sum())
mat = np.eye(dim)
mat[n-1:, n-1:] = Hx
H = np.dot(H, mat)
# Fix the last sign such that the determinant is 1
D[-1] = (-1)**(1-(dim % 2))*D.prod()
# Equivalent to np.dot(np.diag(D), H) but faster, apparently
H = (D*H.T).T
return H
p = 6
U = rvs(p**2)
A = U[:,:p]
probs = []
A = A.tolist()
num= num_withreplacement(p*p,p)
flag = 0
for subset in itertools.combinations_with_replacement(range(p**2),p):
AS = [A[i] for i in subset]
factors = [fac(subset.count(i)) for i in set(subset)]
norm_factor = 1.0
for factor in factors:
norm_factor = norm_factor * factor
prob = fast_glynn_perm(AS)**2
probs.append(fast_glynn_perm(AS)**2/norm_factor)
This code runs in python without error if I remove the @jit line.
However using numba I get
Traceback (most recent call last):
File "boson.py", line 129, in <module>
prob = fast_glynn_perm(AS)**2
ValueError: cannot compute fingerprint of empty list
Raphael