Skip to main content

librla - Randomized Linear Algebra Library

A unified, multi-language library implementing randomized algorithms for low-rank matrix factorizations of both real and complex matrices. The library provides efficient sketching-based methods for large-scale matrix decompositions with consistent APIs across Python, MATLAB/Octave, and Julia.

Features

Core Algorithms

All algorithms support both tolerance mode (rtol < 1) for adaptive rank selection and rank mode (rtol >= 1) for fixed-rank approximations:

Function Description
orth_sketch Approximate orthonormal basis for column space via randomized sketching
qr_sketch Truncated QR factorization with column pivoting
svd_sketch Truncated singular value decomposition
id_sketch Interpolative decomposition via randomized sketching
id_qrpiv Interpolative decomposition via deterministic QR pivoting

LinearOperator Support

All algorithms support LinearOperator abstraction for matrix-free computation:

  • Dense matrices - Standard NumPy/MATLAB/Julia arrays
  • Explicit LinearOperators - Matrix wrappers with unified interface
  • Matrix-free LinearOperators - Function handles for A*x and A'*x operations only

Matrix-free operators enable sketching of implicit matrices (FFT, convolution, Toeplitz, circulant, etc.) without explicit storage.

Method Options for ID

The id_sketch and id_qrpiv functions support three methods for computing the interpolation matrix T:

Method Description
'fast' Triangular solve (fastest, default)
'svd' SVD-based pseudoinverse
'lstsq' Least-squares from original A (most accurate, slowest)

Quick Start

Python

For Python, it is often a good idea to run in a virtual environment. A script named 'setup-venv.sh' is available for users. To utilize it, run the shell script in the terminal. To end the virtual environment, type 'deactivate' in the terminal.

The package librla can be used by either calling the .py file or using the following pip install command:

pip install librla

Here is an example using the library.

import numpy as np
from librla import orth_sketch, qr_sketch, svd_sketch, id_sketch

# Create a test matrix (Hilbert matrix is ill-conditioned)
A = 1.0 / (np.arange(1, 1001)[:, None] + np.arange(1, 501) - 1)

# Orthonormal basis with relative tolerance
Q, flag, diagR = orth_sketch(A, rtol=1e-6)

# Truncated QR factorization
Q, R, p = qr_sketch(A, rtol=1e-6)
# A[:, p] approx Q @ R

# Truncated SVD
U, s, Vh = svd_sketch(A, rtol=1e-6)
# A approx U @ np.diag(s) @ Vh

# Interpolative decomposition
k, piv, T = id_sketch(A, rtol=1e-6, method='lstsq')
# A[:, piv[k:]] approx A[:, piv[:k]] @ T

Usage Modes

Tolerance Mode (rtol < 1)

Adaptively determines rank to achieve specified relative tolerance:

# Python: Adaptive rank selection to achieve 10^-6 tolerance
Q, flag, diagR = orth_sketch(A, 1e-6)

Rank Mode (rtol >= 1)

Returns fixed-rank approximation:

# Python: Rank-20 approximation
U, s, Vh = svd_sketch(A, 20)

API Notes

svd_sketch Return Values

The svd_sketch function returns slightly different formats across languages:

Language Returns Reconstruction
Python U, s, Vh A approx U @ np.diag(s) @ Vh

Python returns V transposed/adjoint

Indexing

  • Python: 0-based indexing (piv[:k] for skeleton columns)

Algorithm Details

Randomized Sketching (id_sketch)

  • Uses random test matrix multiplication for fast column space approximation
  • Geometric block growth for adaptive rank determination in tolerance mode
  • Optional power iterations for improved accuracy (power_iter parameter)
  • Stochastic (results vary slightly between runs)

Deterministic QR Pivoting (id_qrpiv)

  • Uses LAPACK geqp3 column-pivoted QR factorization
  • Deterministic and reproducible results
  • Slower than randomized sketching but guaranteed behavior
  • Same interface as id_sketch
  • Useful for verification and when reproducibility is critical

Accuracy of Randomized Methods

Randomized sketching algorithms have inherent variance in reconstruction error. In rank mode (rtol >= 1), the reconstruction error of randomized methods is typically within a small factor of the optimal (deterministic) error. The validation tests use these thresholds:

Function Threshold Description
svd_sketch 4x Error within 4x of truncated SVD
qr_sketch 4x Error within 4x of pivoted QR
orth_sketch 8x Span error within 8x of optimal
id_sketch 10.0 Absolute error < 10.0 (lenient for full-rank matrices)

This variance is expected for randomized algorithms and does not indicate a bug.

For matrices with slowly decaying singular values (small spectral gap), use power iterations to improve accuracy:

# Use power_iter=2 for matrices with slowly decaying singular values
U, s, Vh = svd_sketch(A, 20, power_iter=2)

LinearOperator Usage

Create matrix-free operators for implicit matrices:

Python

Python uses scipy.sparse.linalg.LinearOperator:

import numpy as np
from scipy.sparse.linalg import LinearOperator
from librla import orth_sketch

# Define matrix-vector products
def matvec(x):
    return np.fft.fft(x)

def rmatvec(x):
    return np.fft.ifft(x).real

# Create LinearOperator
n = 1000
A_op = LinearOperator(shape=(n, n), matvec=matvec, rmatvec=rmatvec, dtype=complex)

# Use with librla
Q, flag, diagR = orth_sketch(A_op, 20)  # Rank-20 approximation

Note: Matrix-free LinearOperators support both modes. In tolerance mode (rtol < 1) the deterministic fallback may materialize the operator as a dense matrix, one matvec per column (O(n) matvecs) — prefer rank mode when that cost is unacceptable.

Optional Parameters

All sketching functions support these optional parameters:

Parameter Default Description
block_size 42 Initial sketch size for tolerance mode
power_iter 0 Number of power iterations for accuracy
extra_samples 12 Oversampling / buffer beyond the target rank (both modes)

For id_sketch and id_qrpiv only:

Parameter Default Description
method 'fast' T matrix computation: 'fast', 'svd', or 'lstsq'

Example with optional parameters:

# Python: Use power iterations for better accuracy
k, piv, T = id_sketch(A, 1e-6, power_iter=2, method='svd')

Requirements

Language Requirements
Python Python 3.9+, NumPy >= 1.20, SciPy >= 1.7

Installation

Python

Requirements: Python 3.9+, NumPy >= 1.20, SciPy >= 1.7

Option 1: Add to Python path (recommended for testing)

import sys
sys.path.append('/path/to/distrib/python')
from librla import id_sketch, svd_sketch

Option 2: Add to PYTHONPATH environment variable

export PYTHONPATH="/path/to/distrib/python:$PYTHONPATH"

Option 3: Copy files to your project

cp distrib/python/librla.py your_project/

Troubleshooting:

  • ImportError: No module named 'librla' - Verify path is correct and use absolute paths
  • ImportError: No module named 'numpy' - Run pip install numpy scipy

Demos

Run the demos to see the library in action:

# Python
cd distrib/python/demo
python demo01_basic.py
python demo02_svd.py

The pip install librla wheel contains only the library module. To obtain the demos and tests after installing from PyPI, either clone the GitHub repository or download and unpack the source distribution:

pip download --no-binary :all: --no-deps librla
tar xf librla-*.tar.gz
cd librla-*/demo && python demo01_basic.py
cd ../test && python test_all.py

Examples

The demo suite provides examples for:

Demo Description
demo01_basic Basic ID algorithms (id_sketch, id_qrpiv)
demo02_svd SVD and QR sketching
demo03_linop LinearOperator abstraction
demo04_power Power iteration effects
demo05_methods T computation methods comparison

Tests

Tests for validating that the algorithms are working are:

Test Description
test_id Validates Interpolatory decomposition
test_svd Validates the SVD
test_qr Validates the randomized QR
test_orth Validates the orthogonal sketch
test_all runs all the test listed above

Additional resources

Additional resources provided for users are:

  • The folder '/distrib/compare' provides codes that compare 'librla' with other randomized linear algebra packages:
    • compare_id_scipy.py - Python: librla vs SciPy (ID)
    • compare_svd_torch.py - Python: librla vs PyTorch (SVD)
  • The repo librla_appliations contains examples of using 'librla' for data compression. The examples are taken from common applications of interest and are taken from the randomized linear algebra literature.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

librla-1.2.1.tar.gz (50.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

librla-1.2.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file librla-1.2.1.tar.gz.

File metadata

  • Download URL: librla-1.2.1.tar.gz
  • Upload date:
  • Size: 50.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for librla-1.2.1.tar.gz
Algorithm Hash digest
SHA256 a5b836718b5f8be64d873659de9e0616249f37a7850fc8c3853a0d444bc0e209
MD5 678faa360852209e981f3dbf5a3c6d77
BLAKE2b-256 2943beb41226564837acef46d3c040cfe9889309eb65277b58f6214235270a11

See more details on using hashes here.

Provenance

The following attestation bundles were made for librla-1.2.1.tar.gz:

Publisher: publish.yml on agillman20/librla

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file librla-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: librla-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for librla-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 165b7b70aeabb5ea5b24401683299d4ce6bd17795b12c22525a97c4724e3285c
MD5 13e0ab4ec9fa5aeb8f5155992a786829
BLAKE2b-256 2a334234857856c24176eacf912799a43dfee01cb03c8a58af59721b74ca3636

See more details on using hashes here.

Provenance

The following attestation bundles were made for librla-1.2.1-py3-none-any.whl:

Publisher: publish.yml on agillman20/librla

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 files

1.2.0

2 files

1.1.0

2 files

1.0.2

2 files

1.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page