Cartesian Product for NumPy - 40x faster than NumPy + itertools.product
pip install cythoncartesian2
Tested against Windows / Python 3.11 / Anaconda
Cython (and a C/C++ compiler) must be installed
cartesian_product(*args, outputdtype=np.uint32, dtype=np.uint32):
Calculate the Cartesian product of input arrays.
Parameters:
- *args: Variable number of input arrays.
- outputdtype (numpy.dtype): Data type of the output array.
- dtype (numpy.dtype): Data type used for intermediate calculations. # be careful!
Returns:
- numpy.ndarray: Cartesian product of input arrays.
import random
from cythoncartesian2 import cartesian_product
import numpy as np
# Strings are NOT supported!
args=[[h*random.uniform(1,4) for h in (range(random.randint(2,9)))] for x in range(9)]
q=cartesian_product(*args,outputdtype=np.float32,dtype=np.uint32)
# array([[0. , 0. , 0. , ..., 0. , 0. ,
# 0. ],
# [3.529998 , 0. , 0. , ..., 0. , 0. ,
# 0. ],
# [0. , 3.715651 , 0. , ..., 0. , 0. ,
# 0. ],
# ...,
# [3.529998 , 7.956308 , 5.9014587, ..., 1.0379078, 7.9018135,
# 8.816498 ],
# [0. , 9.456019 , 5.9014587, ..., 1.0379078, 7.9018135,
# 8.816498 ],
# [3.529998 , 9.456019 , 5.9014587, ..., 1.0379078, 7.9018135,
# 8.816498 ]], dtype=float32)
args=[[h for h in (range(8))] for x in range(9)]
q=cartesian_product(*args,outputdtype=np.uint8,dtype=np.uint32)
# %timeit q=cartesian_product(*args,outputdtype=np.uint8,dtype=np.uint32)
# 1.63 s ± 36.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# %timeit (list(itertools.product(*args)))
# 11.3 s ± 180 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# %timeit q=np.array(list(itertools.product(*args)),dtype=np.uint8)
# 1min 6s ± 282 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# q
# Out[3]:
# array([[0, 0, 0, ..., 0, 0, 0],
# [1, 0, 0, ..., 0, 0, 0],
# [2, 0, 0, ..., 0, 0, 0],
# ...,
# [5, 7, 7, ..., 7, 7, 7],
# [6, 7, 7, ..., 7, 7, 7],
# [7, 7, 7, ..., 7, 7, 7]], dtype=uint8)
Metadata
Release files for cythoncartesian2 0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cythoncartesian2-0.10.tar.gz | 22.0 kB | Without provenance | Details for cythoncartesian2-0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cythoncartesian2-0.10-py3-none-any.whl | Python 3 | none | any | Details for cythoncartesian2-0.10-py3-none-any.whl |
Total release size: 44.3 kB
Release files / cythoncartesian2-0.10.tar.gz
| Download URL | cythoncartesian2-0.10.tar.gz |
|---|---|
| Size | 22.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
25d601d4ee7f586201f06e044d82ab458c94c6238350af830b984414203a2b0e
|
|
BLAKE2b-256 checksum How to use checksums |
493edc9239059cb81db257f7727d1833a8d982d97cc64a1756228ea180ea03ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / cythoncartesian2-0.10-py3-none-any.whl
| Download URL | cythoncartesian2-0.10-py3-none-any.whl |
|---|---|
| Size | 22.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d537c8c10293507d16457fa3d09b52de0a7145c4db60a60346756f26a4dc3dcd
|
|
BLAKE2b-256 checksum How to use checksums |
93300f505293e93230618b396afd605052fd093331b313aa51a3f945aca76f1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for cythoncartesian2 0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.