Skip to main content

Generalized SVD (GSVD) via LAPACK ?ggsvd3

Project description

gsvd4py

PyPI version

A lightweight Python wrapper for the LAPACK ?ggsvd3 routines, providing the Generalized Singular Value Decomposition (GSVD) in a style similar to scipy.linalg. It links to the same LAPACK library that SciPy uses on your machine — no separate LAPACK installation required.

Installation

pip install gsvd4py

Requires SciPy >= 1.13 and NumPy >= 2.0.

Background

The GSVD decomposes a pair of matrices A (m×p) and B (n×p) as:

A = U @ C @ X.conj().T
B = V @ S @ X.conj().T

where:

  • U (m×m) and V (n×n) are unitary
  • C (m×q) and S (n×q) are real diagonal, with the diagonal of C in descending order and C.T @ C + S.T @ S = I
  • X (p×q) is nonsingular
  • q = k + l is the numerical rank of the stacked matrix [A; B]

The generalized singular values are the ratios C[i,i] / S[i,i].

Usage

import numpy as np
from gsvd4py import gsvd

A = np.random.randn(5, 6)
B = np.random.randn(4, 6)

Full GSVD (default)

U, V, C, S, X = gsvd(A, B)
# U: (5,5), V: (4,4), C: (5,q), S: (4,q), X: (6,q)
# diagonal of C is in descending order

Economy GSVD

Truncates U and V to at most q columns:

U, V, C, S, X = gsvd(A, B, mode='econ')

Raw LAPACK output

Returns the LAPACK decomposition A = U @ D1 @ [0, R] @ Q.T directly:

U, V, D1, D2, R, Q, k, l = gsvd(A, B, mode='separate')

Skipping U and/or V

C, S, X = gsvd(A, B, compute_u=False, compute_v=False)
U, C, S, X = gsvd(A, B, compute_v=False)
V, C, S, X = gsvd(A, B, compute_u=False)

Skipping X (or Q in mode='separate')

To retrieve the full diagonal matrices C and S alongside singular vectors, set compute_right=False on gsvd. This skips the accumulation of X and can give a significant speedup when p is large:

U, V, C, S = gsvd(A, B, compute_right=False)

# In separate mode, R is still returned; only Q is omitted:
U, V, D1, D2, R, k, l = gsvd(A, B, mode='separate', compute_right=False)

Generalized singular values only

Use gsvdvals to get just the generalized cosine/sine pairs (c, s) without computing any singular vectors or the right factor X:

from gsvd4py import gsvdvals

c, s = gsvdvals(A, B)
# c[i]**2 + s[i]**2 == 1; generalized singular values are c[i] / s[i]
# c is non-increasing (equivalently, s is non-decreasing)

API Reference

gsvd

gsvd(a, b, mode='full', compute_u=True, compute_v=True, compute_right=True,
     overwrite_a=False, overwrite_b=False, lwork=None, check_finite=True)
Parameter Description
a (m, p) array
b (n, p) array
mode 'full' (default), 'econ', or 'separate'
compute_u Compute left singular vectors of a (default True)
compute_v Compute left singular vectors of b (default True)
compute_right Compute X (or Q in separate mode); set False to skip the O(p³) accumulation (default True)
overwrite_a Allow overwriting a to avoid a copy (default False)
overwrite_b Allow overwriting b to avoid a copy (default False)
lwork Work array size; None triggers an optimal workspace query
check_finite Check inputs for non-finite values (default True)

gsvdvals

gsvdvals(a, b, overwrite_a=False, overwrite_b=False, lwork=None, check_finite=True)

Returns (c, s) — 1D real arrays of length q = k + l (the numerical rank of [a; b]) containing the generalized cosines and sines in non-increasing / non-decreasing order respectively. Parameters have the same meaning as for gsvd.

Return value Description
c Generalized cosines, shape (q,), non-increasing. c[i] == 1 ↔ infinite GSV; c[i] == 0 ↔ zero GSV.
s Generalized sines, shape (q,), non-decreasing. s[i] == 0 ↔ infinite GSV; s[i] == 1 ↔ zero GSV.

Supported dtypes: float32, float64, complex64, complex128. Integer inputs are upcast to float64.

LAPACK backend

gsvd4py discovers the LAPACK library at runtime in the following order:

  1. Apple Accelerate (macOS) — via $NEWLAPACK symbols
  2. scipy-openblas — the OpenBLAS bundle shipped with SciPy
  3. System LAPACKliblapack found via ctypes.util.find_library

No compilation is required.

License

MIT

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

gsvd4py-0.2.1.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

gsvd4py-0.2.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file gsvd4py-0.2.1.tar.gz.

File metadata

  • Download URL: gsvd4py-0.2.1.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for gsvd4py-0.2.1.tar.gz
Algorithm Hash digest
SHA256 e62808b833c81f1a2e9cd78101b7ef90b058a455aa046e0a85ca6a576af74adb
MD5 9cff2efb31600a5956d99925dcd3a6a0
BLAKE2b-256 6024f55b7b815f699d47e1008d986e493e964210401e69555d89d1f9fd5df39b

See more details on using hashes here.

File details

Details for the file gsvd4py-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: gsvd4py-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for gsvd4py-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52a563f8df8b587f4b5774adc15ba598108636298d4ab0efdfce84b1299434eb
MD5 c438f9c0ce081bcd309e28b5ef87d2f7
BLAKE2b-256 e9ae3a10f801617dbc87d382224dea720fe5d2c6e0c80d2e2544164cf5d55536

See more details on using hashes here.

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