SVD
Overview
The SVD
function computes the Singular Value Decomposition of a given matrix, factorizing it into two unitary matrices and a diagonal matrix of singular values. This is a fundamental operation in linear algebra, used in applications such as signal processing, statistics, and machine learning for dimensionality reduction, pseudoinverse computation, and more. The decomposition is defined as:
where is the input matrix, and are unitary matrices, and is a diagonal matrix of singular values. For more details, see the scipy.linalg.svd documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=SVD(matrix, [full_matrices], [compute_uv])
matrix
(2D list, required): The matrix to decompose. Must be at least 2x2.full_matrices
(bool, optional, default=True): If True, U and Vh have full shapes; if False, reduced shapes.compute_uv
(bool, optional, default=True): If True, returns U, S, Vh; if False, returns only S.overwrite_a
(bool, optional, default=False): If True, allows overwriting the input matrix to improve performance.check_finite
(bool, optional, default=True): If True, checks that the input matrix contains only finite numbers.lapack_driver
(str, optional, default=‘gesdd’): LAPACK driver to use (‘gesdd’ or ‘gesvd’).return_type
(str, optional, default=‘u’): Controls the output. ‘u’, ‘s’, or ‘vh’ returns only the specified matrix as a 2D list. If ‘s’, singular values are returned as a 2D row vector. Only one matrix can be returned per call.
The function returns a 2D list: either U, S (as a diagonal matrix or row), or Vh, depending on return_type
.
Examples
Example 1: U matrix of a 2x2 Matrix
In Excel:
=SVD({1,2;3,4}, TRUE, TRUE, FALSE, TRUE, "gesdd", "u")
Expected output:
U₁ | U₂ |
---|---|
-0.4046 | -0.9145 |
-0.9145 | 0.4046 |
Example 2: S (singular values) of a 3x2 Matrix
In Excel:
=SVD({1,0;0,1;1,1}, FALSE, TRUE, TRUE, FALSE, "gesdd", "s")
Expected output:
S₁ | S₂ |
---|---|
1.7321 | 1.0 |
Example 3: Vh matrix of a 2x2 Matrix, Custom LAPACK Driver
In Excel:
=SVD({1,2;3,4}, TRUE, TRUE, FALSE, TRUE, "gesvd", "vh")
Expected output:
Vh₁ | Vh₂ |
---|---|
-0.576 | -0.8174 |
0.8174 | -0.576 |
Example 4: S (singular values) of a 2x3 Matrix
In Excel:
=SVD({1,2,3;4,5,6}, TRUE, TRUE, FALSE, TRUE, "gesdd", "s")
Expected output:
S₁ | S₂ |
---|---|
9.508 | 0.7729 |
Python Code
import numpy as np
from scipy.linalg import svd as scipy_svd
def svd(matrix, full_matrices=True, compute_uv=True, overwrite_a=False, check_finite=True, lapack_driver='gesdd', return_type='u'):
"""
Compute the Singular Value Decomposition (SVD) of a matrix.
Args:
matrix: 2D list representing the matrix to decompose.
full_matrices: If True, U and Vh have full shapes; if False, reduced shapes (default: True).
compute_uv: If True, returns U, S, Vh; if False, returns only S (default: True).
overwrite_a: If True, allows overwriting the input matrix to improve performance (default: False).
check_finite: If True, checks that the input matrix contains only finite numbers (default: True).
lapack_driver: LAPACK driver to use ('gesdd' or 'gesvd') (default: 'gesdd').
return_type: 'u' (default), 's', or 'vh' to return only the specified matrix as a 2D list.
Returns:
2D list: The specified matrix (U, S, or Vh) as a 2D list. If 's', singular values are returned as a 2D row vector.
Returns an error message (str or list[list[str]]) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate input
if not isinstance(matrix, list) or len(matrix) < 2 or not isinstance(matrix[0], list):
return [["Invalid input: matrix must be a 2D list with at least 2 rows."]]
try:
arr = np.array(matrix, dtype=float)
except Exception:
return [["Invalid input: matrix must contain numeric values."]]
try:
if compute_uv:
U, s, Vh = scipy_svd(arr, full_matrices=full_matrices, compute_uv=True, overwrite_a=overwrite_a, check_finite=check_finite, lapack_driver=lapack_driver)
S = np.zeros_like(arr, dtype=float)
for i in range(min(arr.shape)):
S[i, i] = s[i]
U = np.round(U, 4).tolist()
S = np.round(S, 4).tolist()
Vh = np.round(Vh, 4).tolist()
if return_type == 'u':
return U
elif return_type == 's':
return [s.round(4).tolist()]
elif return_type == 'vh':
return Vh
else:
return [["Invalid return_type"]]
else:
s = scipy_svd(arr, compute_uv=False, overwrite_a=overwrite_a, check_finite=check_finite, lapack_driver=lapack_driver)
if return_type == 's':
return [np.round(s, 4).tolist()]
else:
return [["Invalid return_type for compute_uv=False"]]
except Exception as e:
return [[f"SVD error: {e}"]]
Live Notebook
Edit this function in a live notebook .