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.2.tar.gz (9.3 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.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: batch_tensorsolve-1.0.2.tar.gz
  • Upload date:
  • Size: 9.3 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.2.tar.gz
Algorithm Hash digest
SHA256 c726f89c1372fe3c840707498344fb7101ea4144cec9e91d95b694dc47f36dd5
MD5 a9583c961c325c07dbb55fe8cdf9dd24
BLAKE2b-256 2cf874cc2cbad732b5ac5a76ddf615d9e15d4e103149557a704edba45f99f838

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for batch_tensorsolve-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 09f023e8dbf2b136d74feb8ad3237f93a2204723d71c6b38ed2ab211180dd013
MD5 33367aef689e9778d0be7697fc05353c
BLAKE2b-256 16c5d382e374531320f84a4f2856d25af6b11e18d2b8443cb4ea3caf275c64f6

See more details on using hashes here.

Provenance

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