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.1.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.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for batch_tensorsolve-1.0.1.tar.gz
Algorithm Hash digest
SHA256 85d8022bdbf46d85899b9297921094e3563455b7ebb53c187b1432df411e8ab8
MD5 141b27825e367dc331187f4caa217f0f
BLAKE2b-256 3f464a556a2d63d61a9251b732327a39a08b302a9b00c6f8261650139355f3c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_tensorsolve-1.0.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for batch_tensorsolve-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f710235deb8a70603bdb2f6e0e2124ec1df11fa57e14d576ed0fbfa9d7228bf0
MD5 1271ded20b59fb3c4d23d684bebeaf0e
BLAKE2b-256 f62bf18f438bc4c258eadbabd1f78b64d801d5801878715bd7cd0f4ebecfc8ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_tensorsolve-1.0.1-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