{"id":4977,"library":"linear-operator","title":"Linear Operator","description":"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.","status":"active","version":"0.6.1","language":"en","source_language":"en","source_url":"https://github.com/cornellius-gp/linear_operator/","tags":["linear algebra","pytorch","structured matrices","gpu","machine learning"],"install":[{"cmd":"pip install linear_operator","lang":"bash","label":"PyPI"},{"cmd":"conda install linear_operator -c gpytorch","lang":"bash","label":"Conda"}],"dependencies":[{"reason":"Requires Python 3.10 or higher.","package":"python","affected_versions":">=0.6"},{"reason":"Core dependency for tensor operations and GPU support, requires PyTorch 2.0 or higher.","package":"pytorch","affected_versions":">=0.6"}],"imports":[{"note":"This is the base class for all linear operators. Specific operators like DiagLinearOperator, KroneckerProductLinearOperator, etc., are also imported from `linear_operator.operators`.","symbol":"LinearOperator","correct":"from linear_operator.operators import LinearOperator"},{"symbol":"DiagLinearOperator","correct":"from linear_operator.operators import DiagLinearOperator"},{"symbol":"LowRankRootLinearOperator","correct":"from linear_operator.operators import LowRankRootLinearOperator"}],"quickstart":{"code":"import torch\nfrom linear_operator.operators import LowRankRootLinearOperator, DiagLinearOperator\n\n# Example: Represent a 10000 x 10000 matrix A = C C^T + D\n# A is never explicitly instantiated as a dense matrix, saving memory\nC = torch.randn(10000, 20) # A \"skinny\" matrix (e.g., 10000x20)\nd = torch.randn(10000).abs() + 1e-6 # Diagonal elements (ensure positive)\nb = torch.randn(10000)\n\nA = LowRankRootLinearOperator(C) + DiagLinearOperator(d)\n\n# Perform a linear solve efficiently without instantiating the full dense matrix\n# This uses structure-exploiting algorithms like Woodbury formula under the hood\nx = torch.linalg.solve(A, b)\n\nprint(f\"Shape of A: {A.shape}\")\nprint(f\"Shape of b: {b.shape}\")\nprint(f\"Shape of x (solution): {x.shape}\")","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python to 3.10 or higher and PyTorch to 2.0 or higher. For PyTorch, ensure you install the correct version for your CUDA toolkit.","message":"Version 0.6 introduced stricter Python and PyTorch version requirements. Python 3.10+ and PyTorch 2.0+ are now mandatory.","severity":"breaking","affected_versions":">=0.6"},{"fix":"Review your sparse tensor construction logic and update it to align with PyTorch's current best practices, as the library now uses deprecated sparse tensor construction methods internally.","message":"The internal methods for sparse tensor construction were updated in v0.6.1. Users who have custom `LinearOperator` implementations or interact directly with sparse tensor construction might encounter changes or deprecation warnings.","severity":"gotcha","affected_versions":">=0.6.1"},{"fix":"Adjust the argument passing to `_dtype_value_context` in your custom `LinearOperator` implementations according to the new signature in v0.4.0.","message":"The initialization arguments for `_dtype_value_context` changed in version 0.4.0. If you have custom `LinearOperator` subclasses that override or interact with this internal context, your code may break.","severity":"breaking","affected_versions":">=0.4.0"},{"fix":"Be aware of potential memory and performance impacts when using `BlockDiagLinearOperator` with complex `base_linear_op` instances. Evaluate if this change affects your specific use case.","message":"In version 0.6.1, `BlockDiagLinearOperator` now converts its `base_linear_op` to a dense linear operator. This might lead to increased memory usage or altered performance if you were relying on lazy evaluation of the base operator within `BlockDiagLinearOperator` in previous versions.","severity":"gotcha","affected_versions":">=0.6.1"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}