# 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
```python
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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/cythoncartesian2",
"name": "cythoncartesian2",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "cython,arrays,cartesian,numpy",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/49/3e/dc9239059cb81db257f7727d1833a8d982d97cc64a1756228ea180ea03ff/cythoncartesian2-0.10.tar.gz",
"platform": null,
"description": "\r\n# Cartesian Product for NumPy - 40x faster than NumPy + itertools.product \r\n\r\n## pip install cythoncartesian2\r\n\r\n### Tested against Windows / Python 3.11 / Anaconda\r\n\r\n## Cython (and a C/C++ compiler) must be installed\r\n\r\n\r\n\r\n\r\n```python\r\n\r\ncartesian_product(*args, outputdtype=np.uint32, dtype=np.uint32):\r\n Calculate the Cartesian product of input arrays.\r\n\r\n Parameters:\r\n - *args: Variable number of input arrays.\r\n - outputdtype (numpy.dtype): Data type of the output array.\r\n - dtype (numpy.dtype): Data type used for intermediate calculations. # be careful!\r\n\r\n Returns:\r\n - numpy.ndarray: Cartesian product of input arrays.\r\n\r\n\t\r\nimport random\r\nfrom cythoncartesian2 import cartesian_product\r\nimport numpy as np\r\n\r\n# Strings are NOT supported!\r\n\r\nargs=[[h*random.uniform(1,4) for h in (range(random.randint(2,9)))] for x in range(9)]\r\nq=cartesian_product(*args,outputdtype=np.float32,dtype=np.uint32)\r\n\r\n# array([[0. , 0. , 0. , ..., 0. , 0. ,\r\n# 0. ],\r\n# [3.529998 , 0. , 0. , ..., 0. , 0. ,\r\n# 0. ],\r\n# [0. , 3.715651 , 0. , ..., 0. , 0. ,\r\n# 0. ],\r\n# ...,\r\n# [3.529998 , 7.956308 , 5.9014587, ..., 1.0379078, 7.9018135,\r\n# 8.816498 ],\r\n# [0. , 9.456019 , 5.9014587, ..., 1.0379078, 7.9018135,\r\n# 8.816498 ],\r\n# [3.529998 , 9.456019 , 5.9014587, ..., 1.0379078, 7.9018135,\r\n# 8.816498 ]], dtype=float32)\r\n\r\nargs=[[h for h in (range(8))] for x in range(9)]\r\nq=cartesian_product(*args,outputdtype=np.uint8,dtype=np.uint32)\r\n\r\n# %timeit q=cartesian_product(*args,outputdtype=np.uint8,dtype=np.uint32)\r\n# 1.63 s \u00b1 36.2 ms per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\r\n\r\n# %timeit (list(itertools.product(*args)))\r\n# 11.3 s \u00b1 180 ms per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\r\n\r\n# %timeit q=np.array(list(itertools.product(*args)),dtype=np.uint8)\r\n# 1min 6s \u00b1 282 ms per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\r\n\r\n# q\r\n# Out[3]:\r\n# array([[0, 0, 0, ..., 0, 0, 0],\r\n# [1, 0, 0, ..., 0, 0, 0],\r\n# [2, 0, 0, ..., 0, 0, 0],\r\n# ...,\r\n# [5, 7, 7, ..., 7, 7, 7],\r\n# [6, 7, 7, ..., 7, 7, 7],\r\n# [7, 7, 7, ..., 7, 7, 7]], dtype=uint8)\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Cartesian Product for NumPy - 40x faster than NumPy + itertools.product",
"version": "0.10",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/cythoncartesian2"
},
"split_keywords": [
"cython",
"arrays",
"cartesian",
"numpy"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "93300f505293e93230618b396afd605052fd093331b313aa51a3f945aca76f1a",
"md5": "785bb3cbec72d1de4fb49cec2b751f73",
"sha256": "d537c8c10293507d16457fa3d09b52de0a7145c4db60a60346756f26a4dc3dcd"
},
"downloads": -1,
"filename": "cythoncartesian2-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "785bb3cbec72d1de4fb49cec2b751f73",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 22293,
"upload_time": "2023-12-12T21:13:05",
"upload_time_iso_8601": "2023-12-12T21:13:05.452629Z",
"url": "https://files.pythonhosted.org/packages/93/30/0f505293e93230618b396afd605052fd093331b313aa51a3f945aca76f1a/cythoncartesian2-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "493edc9239059cb81db257f7727d1833a8d982d97cc64a1756228ea180ea03ff",
"md5": "3427372acd18b0e3cf3f174d245f14ec",
"sha256": "25d601d4ee7f586201f06e044d82ab458c94c6238350af830b984414203a2b0e"
},
"downloads": -1,
"filename": "cythoncartesian2-0.10.tar.gz",
"has_sig": false,
"md5_digest": "3427372acd18b0e3cf3f174d245f14ec",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 21969,
"upload_time": "2023-12-12T21:13:08",
"upload_time_iso_8601": "2023-12-12T21:13:08.218756Z",
"url": "https://files.pythonhosted.org/packages/49/3e/dc9239059cb81db257f7727d1833a8d982d97cc64a1756228ea180ea03ff/cythoncartesian2-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-12 21:13:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "cythoncartesian2",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "cythoncartesian2"
}