Generalized SVD (GSVD) via LAPACK ?ggsvd3
Project description
gsvd4py
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) andV(n×n) are unitaryC(m×q) andS(n×q) are real diagonal, with the diagonal ofCin descending order andC.T @ C + S.T @ S = IX(p×q) is nonsingularq = k + lis 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:
- Apple Accelerate (macOS) — via
$NEWLAPACKsymbols - scipy-openblas — the OpenBLAS bundle shipped with SciPy
- System LAPACK —
liblapackfound viactypes.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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e62808b833c81f1a2e9cd78101b7ef90b058a455aa046e0a85ca6a576af74adb
|
|
| MD5 |
9cff2efb31600a5956d99925dcd3a6a0
|
|
| BLAKE2b-256 |
6024f55b7b815f699d47e1008d986e493e964210401e69555d89d1f9fd5df39b
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52a563f8df8b587f4b5774adc15ba598108636298d4ab0efdfce84b1299434eb
|
|
| MD5 |
c438f9c0ce081bcd309e28b5ef87d2f7
|
|
| BLAKE2b-256 |
e9ae3a10f801617dbc87d382224dea720fe5d2c6e0c80d2e2544164cf5d55536
|