Linear Operator

0.6.1 · active · verified Sun Apr 12

LinearOperator is a PyTorch package for abstracting away linear algebra routines needed for structured matrices or operators, primarily designed for finite-dimensional positive definite operators (i.e., kernel matrices). It is actively developed, with its current version being 0.6.1, and typically releases maintenance updates and minor versions frequently.

Warnings

Install

Imports

Quickstart

This example demonstrates how to construct a large-scale structured matrix (a sum of a low-rank matrix and a diagonal matrix) as a `LinearOperator` object. The library implicitly handles the algebraic structure, allowing efficient operations like linear solves using `torch.linalg.solve` without ever forming the full, dense matrix, which is crucial for large-scale problems.

import torch
from linear_operator.operators import LowRankRootLinearOperator, DiagLinearOperator

# Example: Represent a 10000 x 10000 matrix A = C C^T + D
# A is never explicitly instantiated as a dense matrix, saving memory
C = torch.randn(10000, 20) # A "skinny" matrix (e.g., 10000x20)
d = torch.randn(10000).abs() + 1e-6 # Diagonal elements (ensure positive)
b = torch.randn(10000)

A = LowRankRootLinearOperator(C) + DiagLinearOperator(d)

# Perform a linear solve efficiently without instantiating the full dense matrix
# This uses structure-exploiting algorithms like Woodbury formula under the hood
x = torch.linalg.solve(A, b)

print(f"Shape of A: {A.shape}")
print(f"Shape of b: {b.shape}")
print(f"Shape of x (solution): {x.shape}")

view raw JSON →