An Abstract Linear Operator Library for pyTorch
This library implements a generic structure for abstract linear operators and enables a number of standard operations on them:
- Arithmetic:
A + B,A - B,-A,A @ Ball work exactly as expected to combine linear operators. - Indexing:
A[k:ell,m:n]works as expected. - Solves:
Ax = bcan be solved withCGfor PSD matrices,minresfor symmetric matrices,LSQR(to be implemented), orLSMR(to be implemented). - Trace estimation: The trace of square matrices, can be estimated via Hutch++ and Hutchinson's estimator.
- Diamond-Boyd stochastic equilibration
- Randomized Nyström Preconditioning
- Automatic adjoint operator generation.
Using LinearOperators
The public API of the LinearOperator library is that every LinearOperator has the
following properties and methods:
class LinearOperator:
# Properties
shape: tuple[int, int]
T: LinearOperator
supports_operator_matrix: bool
device: torch.Device
# Matrix multiply
def __matmul__(self, b: torch.Tensor) -> torch.Tensor: ...
def __rmatmul__(self, b: torch.Tensor) -> torch.Tensor: ...
def __matmul__(self, b: LinearOperator) -> LinearOperator: ...
def __rmatmul__(self, b: LinearOperator) -> LinearOperator: ...
# Linear Solve Methods
def solve_I_p_lambda_AT_A_x_eq_b(self,
lambda_: float,
b: torch.Tensor,
x0: torch.Tensor | None=None,
*, precondition: None | Literal['nsytrom'], hot=False) -> torch.Tensor: ...
def solve_A_x_eq_b(self,
b: torch.Tensor,
x0: torch.Tensor | None=None) -> torch.Tensor: ...
# Transformations on LinearOperator
def __mul__(self, c: float) -> LinearOperator: ...
def __rmul__(self, c: float) -> LinearOperator: ...
def __truediv__(self, c: float) -> LinearOperator: ...
def __pow__(self, k: int) -> LinearOperator: ...
def __add__(self, c: LinearOperator) -> LinearOperator: ...
def __sub__(self, c: LinearOperator) -> LinearOperator: ...
def __neg__(self) -> LinearOperator: ...
def __pos__(self) -> LinearOperator: ...
def __getitem__(self, key) -> LinearOperator: ...
The following functions are available in the root of the library:
def operator_matrix_product(A: LinearOperator, M: torch.Tensor) -> torch.Tensor: ...
def aslinearoperator(A: torch.Tensor | LinearOperator) -> LinearOperator: ...
def vstack(ops: list[LinearOperator] | tuple[LinearOperator, ...]) -> LinearOperator: ...
def hstack(ops: list[LinearOperator] | tuple[LinearOperator, ...]) -> LinearOperator: ...
# To be implemented:
def bmat(ops: list[list[LinearOperator]]) -> LinearOperator: ... # Optimizes out ZeroOperator
The following functions are available in linops.trace for trace estimation:
def hutchpp(A: lo.LinearOperator, m: int) -> float: ...
def hutchinson(A: lo.LinearOperator, m: int) -> float: ...
linops.equilibration contains equilibrate and symmetric_equilibrate.
Their public API is not finalized, if you wish to use them it is recommend you read the source code.
Creating Linear Operators
Linear operators can be constructed in the following way:
- Creating a sub-class of
LinearOperator - Calling one of the following constructors:
IdentityOperator(n: int)DiagonalOperator(diag: torch.Tensor): wherediagis a 1D torch tensor.MatrixOperator(M: torch.Tensor): whereMis a 2D torch tensor.SelectionOperator(shape: tuple[int, int], idxs: slice | list[int | slice])KKTOperator(H: LinearOperator, A: LinearOperator): whereHis a squareLinearOperatorandAis aLinearOperatorVectorJacobianOperator(f: torch.Tensor, x: torch.Tensor): wherefis the output of the function being differentiated which has a torch autograd value andxis the vector on whichensures_gradwas called.ZeroOperator(shape: tuple[int, int])
- Combining operators via:
A + B,A - B,A @ BforA,Blinear operatorshstack,vstackA,c A,A / c,v * A,A / vfor scalarcand vectorv.
Implementing LinearOperators
To implement a LinearOperator the following are mandatory:
- Set
_shape: tuple[int, int]to the shape of the operator. - Set
deviceappropriately, if the operator requires vectors to be on a particular device. - Implement a method
def _matmul_impl(self, v: torch.Tensor) -> torch.Tensor: ...that implements your matrix vector product.
The following are recommended to improve performance:
- If your
_matmul_implmethod handles matrix inputs correctly, setsupports_operator_matrix: booltoTrue. - If it is possible to describe the adjoint operator, set
_adjoint: LinearOperatorto point to the adjoint of your operator. If you do not compute this, then one will be autogenerated by differentiating through your_matmul_impl.
It is suggested that, if possible, you replace any other methods with specialized implementations.
Metadata
Release files for torch-linops 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_linops-0.2.0.tar.gz | 20.1 kB | With provenance | Details for torch_linops-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_linops-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for torch_linops-0.2.0-py3-none-any.whl |
Total release size: 41.5 kB
Release files / torch_linops-0.2.0.tar.gz
| Download URL | torch_linops-0.2.0.tar.gz |
|---|---|
| Size | 20.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
93c052cea5cb348f9613d51eb41f05fec9065dcc4bdc5a2c4d9f5c8b6cd284bb
|
|
BLAKE2b-256 checksum How to use checksums |
58c6c83f1db2f207911ec150451babefd3b04085f82a08193a5d9be1a58055b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 7, 2025.
Transparency logRelease files / torch_linops-0.2.0-py3-none-any.whl
| Download URL | torch_linops-0.2.0-py3-none-any.whl |
|---|---|
| Size | 21.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
85fb3ef7191eaa1e0903ed99150b89406b7bce0b11f14fbea3e3b404a19f1108
|
|
BLAKE2b-256 checksum How to use checksums |
562144676524c678ffe7bfd6a084a7a5c087c7cf7df48424a2157dd77df04e90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 7, 2025.
Transparency logSecurity for torch-linops 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for torch_linops-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 7, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.