Create boolean tensors / sparse matrices from index expressions
Project description
Kronecker
A small library that provides a convenient syntax for creating boolean tensors / sparse matrices.
Examples
i, j = kronecker.indices(4, 4)
arr = (i >= j * 2 - 1).to_numpy()
np.array_equal(arr, np.array([
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 0]
]).astype(bool))
> True
i, j, k = kronecker.indices(2, 2, 3)
arr = (i >= j + k - 1).to_numpy()
np.array_equal(arr, np.array([
[[1, 1, 0], [1, 0, 0]],
[[1, 1, 1], [1, 1, 0]]
]).astype(bool))
> True
# would run out of memory if created as a numpy array
i, j = kronecker.indices(1_000_000, 1_000_000)
x = (i * 5 == j).to_sparse()
assert x.sum() == 200000
Limitations
- When creating sparse matrices linear expressions in the indices are simplified and evaluated once per row, giving a complexity of O(n_rows * n_True_per_row). For non-linear expressions (including ones that contain integer division
//) a slower, O(n_rows * n_cols), path is used. This is mostly useless, creating a numpy array and converting is much faster (but uses more memory). - Only the operators {
+,-,*,/,//,**} are supported. - Multiple comparisons (
i < j < 2 * i) are not supported.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
kronecker-0.0.2.tar.gz
(8.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kronecker-0.0.2.tar.gz.
File metadata
- Download URL: kronecker-0.0.2.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8aa98394669ce4658388c4e3a85e6f3d1e181149c63d351a450eedcc8f053461
|
|
| MD5 |
481e02843f02c3a5d45e256270a405a9
|
|
| BLAKE2b-256 |
66b35f0477489b7e61b174ff80d6c51188c09f72e8bb196297113813343e481e
|
File details
Details for the file kronecker-0.0.2-py3-none-any.whl.
File metadata
- Download URL: kronecker-0.0.2-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b163319b25488c98682068f3a4fa1bf29c13cc133337279f41fe11dbe3241a9
|
|
| MD5 |
35e7f164fdd058f0105070e8f1c682c3
|
|
| BLAKE2b-256 |
d81f417a34aeeab580e313047306d6246848dc467ba4c39d1911331b3c04594c
|