Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

apply numba to itertools import product

@nb.njit(nb.int32[:,:](nb.int32[:]))
def cproduct_idx(sizes: np.ndarray):
    """Generates ids tuples for a cartesian product"""
    assert len(sizes) >= 2
    tuples_count  = np.prod(sizes)
    tuples = np.zeros((tuples_count, len(sizes)), dtype=np.int32)
    tuple_idx = 0
    # stores the current combination
    current_tuple = np.zeros(len(sizes))
    while tuple_idx < tuples_count:
        tuples[tuple_idx] = current_tuple
        current_tuple[0] += 1
        # using a condition here instead of including this in the inner loop
        # to gain a bit of speed: this is going to be tested each iteration,
        # and starting a loop to have it end right away is a bit silly
        if current_tuple[0] == sizes[0]:
            # the reset to 0 and subsequent increment amount to carrying
            # the number to the higher "power"
            current_tuple[0] = 0
            current_tuple[1] += 1
            for i in range(1, len(sizes) - 1):
                if current_tuple[i] == sizes[i]:
                    # same as before, but in a loop, since this is going
                    # to get run less often
                    current_tuple[i + 1] += 1
                    current_tuple[i] = 0
                else:
                    break
        tuple_idx += 1
    return tuples

@nb.njit
def cartesian_product(*arrays):
    sizes = [len(a) for a in arrays]
    sizes = np.asarray(sizes, dtype=np.int32)
    tuples_count  = np.prod(sizes)
    array_ids = cproduct_idx(sizes)
    tuples = np.zeros((tuples_count, len(sizes)))
    for i in range(len(arrays)):
        tuples[:, i] = arrays[i][array_ids[:, i]]
    return tuples

@nb.njit
def cartesian_product_repeat(array, repeat):
    sizes = [len(array) for _ in range(repeat)]
    sizes = np.asarray(sizes, dtype=np.int32)
    tuples_count  = np.prod(sizes)
    array_ids = cproduct_idx(sizes)
    tuples = np.zeros((tuples_count, len(sizes)))
    for i in range(repeat):
        tuples[:, i] = array[array_ids[:, i]]
    return tuples
Comment

PREVIOUS NEXT
Code Example
Python :: df.fillna("tagline",inplace=True) in jupyter notebook 
Python :: what does filter do in stackapi python 
Python :: if condition in djangio template 
Python :: how to create datetime from negative epoch in python 
Python :: how to check what version of pygame you have instaled 
Python :: python pyinstler not found 
Python :: how to convert variable in Python ? 
Python :: aggregation with f() in django rest api 
Python :: reportlab line thickness 
Python :: How to create an AI from scratch 
Python :: discord py aliases 
Python :: flask-restx custom ui 
Python :: scrapy itemloader example 
Python :: python creare decoratori 
Python :: str = "This article is written in {}" print (str.format("Python")) 
Python :: reference other libraries in library 
Python :: python triée plusieurs fois avec virgule 
Python :: make large 3d plot in python 
Python :: get the factorial of a number on python 
Python :: dtype cannot be bool python 
Python :: dont squeeze plot when creating colorbar matplotlib 
Python :: python numpy + opencv + overlay image 
Python :: discord.File(fp=image_binary,filename=name) discord py 
Python :: k7yKJk8vdjHvw56q7bCTxibvT 
Python :: %Y-%m-%dT%H:%M:%SZ convert to date time object 
Python :: files and exceptions not working python 
Python :: python-crontab sheduling at specific time 
Python :: when was python 3.7 released 
Python :: spacy text annotation dict comprehension 
Python :: import turtle python 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =