Skip to main content

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


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.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kronecker-0.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file kronecker-0.0.1.tar.gz.

File metadata

  • Download URL: kronecker-0.0.1.tar.gz
  • Upload date:
  • Size: 8.5 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

Hashes for kronecker-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2690714601a5af336926c1e8a0104b0cb16a1dce7ba1ea364dba04586a01237b
MD5 34220a92bcfa2207dfa7195a300dc40a
BLAKE2b-256 9c5e0046c1707ed9f7833755791aa83e73c152f75df97d4c8f0618723bf91169

See more details on using hashes here.

File details

Details for the file kronecker-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: kronecker-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 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

Hashes for kronecker-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d6f0acfa782db1c4ad7a2aeb95ca6412bd60fb51df2918aff7ac1112cfd87c14
MD5 4f3c58d11ca50963f5597052318739fd
BLAKE2b-256 5f02b851fc72794f3df2f1bcc88c87420e6f01ca090dc81070165af0c2c69002

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page