What is the fastest way to compute Cartesian product?

  cartesian-product, numpy, python

I have a set of N arrays and I need to compute the N-fold Cartesian product.
Of all the generated ordered tuples I want to keep only the ones whose sum is equal to 1 (plus or minus a certain tolerance), e.g.

p = [[0.4,0.389],

cartesian_product = [[0.4, 0.6],
                     [0.4, 0.611],
                     [0.389, 0.6],
                     [0.389, 0.611]]

filtered_cartesian_product = [[0.4, 0.6],
                              [0.389, 0.611]]

I solved this problem in Python using Itertools’ Product()

np.array([i for i in itertools.product(*p) if np.abs(np.sum(i)-1)<1e-4])

which works quite well. However, since I need to perform this task on a relatively big dataset (100 arrays with 100 entries each), speed and good memory management are crucial.

Could anyone suggest me a faster solution (not necessarily in Python)?

Source: Python Questions