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

Uploaded Python 3

File details

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

File metadata

  • Download URL: batch_tensorsolve-0.1.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-0.1.0.tar.gz
Algorithm Hash digest
SHA256 63c94a4685fcd5f089cf872025e1f23a6d319c402a15b2a078f0838216b3be32
MD5 326d8efa3dc4acbc6f7d0a713ba75d4e
BLAKE2b-256 c20ad5fd41ad4d511b4e4e15ade69c053e4b5275ed327d1e45f80895cc1cf6e6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for batch_tensorsolve-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d85e3e58206909407e461e563594f4a1dc00376035d860880dc4e5c96f928d9a
MD5 7ff3d717d63f4c4bdfc5c7aba1b0d5ab
BLAKE2b-256 11ed1bd65cc6c0ab656aa899c160c01f6081a3bb3c434cf00537072d77433228

See more details on using hashes here.

Provenance

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