Skip to main content

Batched tensorsolve() for NumPy / PyTorch / JAX

Project description

Batch Tensorsolve

CI Status Documentation Status Test coverage percentage

uv Ruff pre-commit

PyPI Version Supported Python versions License


Documentation: https://batch-tensorsolve.readthedocs.io

Source Code: https://github.com/34j/batch-tensorsolve


Batched tensorsolve() for NumPy / PyTorch / JAX. (numpy/numpy#28099)

Installation

Install this via pip (or your favourite package manager):

pip install batch-tensorsolve

Usage

import numpy as np
from numpy.testing import assert_allclose

from batch_tensorsolve import btensorsolve

a = np.random.randn(2, 2, 3, 6)
b = np.random.randn(2, 2, 3)
assert_allclose(np.einsum("...ijk,...k->...ij", a, btensorsolve(a, b)), b)

Advanced Usage

It is recommended to explicitly specify the batch axes, as the desired shape will be ambiguous if axes of size 1 are present.

import numpy as np

from batch_tensorsolve import btensorsolve

a = np.random.randn(2, 1, 2, 2)
b = np.random.randn(2, 1, 2)
# 2 possibilities:
assert btensorsolve(a, b, num_batch_axes=1).shape == (2, 2) # 1st axis is batch
assert btensorsolve(a, b, num_batch_axes=2).shape == (2, 1, 2) # 1st and 2nd axes are batch

Broadcasting-like behavior is also supported:

import numpy as np
from numpy.testing import assert_allclose

from batch_tensorsolve import btensorsolve

a = np.random.randn(1, 2, 3, 6) # -> (2, 2, 3, 6)
b = np.random.randn(2, 1, 1) # -> (2, 2, 3)
left = np.einsum("...ijk,...k->...ij", a, btensorsolve(a, b))
assert_allclose(left, np.broadcast_to(b, left.shape))

Note that broadcasting (repeating) a for non-batch axes will result in numpy.linalg.LinAlgError: Singular matrix because the matrix representation of a has duplicate rows.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

Credits

Copier

This package was created with Copier and the browniebroke/pypackage-template project template.

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

batch_tensorsolve-1.0.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

batch_tensorsolve-1.0.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file batch_tensorsolve-1.0.0.tar.gz.

File metadata

  • Download URL: batch_tensorsolve-1.0.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for batch_tensorsolve-1.0.0.tar.gz
Algorithm Hash digest
SHA256 875a1cf3d570b8dd7b43900264bbfb7f15510e4783ba57397312eff5a1414152
MD5 f5b8bbf8957d5dcbf2c7ffb146b5cc6a
BLAKE2b-256 75efcfe560278bec612a8c2bcd35b194ae5e9f8669c67aa5e9cc92afd032a672

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_tensorsolve-1.0.0.tar.gz:

Publisher: ci.yml on 34j/batch-tensorsolve

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file batch_tensorsolve-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for batch_tensorsolve-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e5a3c8b269d36c43d89cbf9b940f312e70c270b409c39b8b7b0b3821c68516a
MD5 2ef0fd627ff7276dbd00c5b1045a9820
BLAKE2b-256 fbaa25c14b31af260422be4dac3280dfe5e8de6ae5d5184a0a60c62f459eaaa0

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_tensorsolve-1.0.0-py3-none-any.whl:

Publisher: ci.yml on 34j/batch-tensorsolve

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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