Compute the singular value decomposition of a matrix. Parameters ---------- a : (M, N) Array coerce_signs : bool Whether or not to apply sign coercion to singular vectors in order to maintain deterministic results, by default True. Examples --------
(a, coerce_signs=True)
| 872 | |
| 873 | |
| 874 | def svd(a, coerce_signs=True): |
| 875 | """ |
| 876 | Compute the singular value decomposition of a matrix. |
| 877 | |
| 878 | Parameters |
| 879 | ---------- |
| 880 | a : (M, N) Array |
| 881 | coerce_signs : bool |
| 882 | Whether or not to apply sign coercion to singular vectors in |
| 883 | order to maintain deterministic results, by default True. |
| 884 | |
| 885 | Examples |
| 886 | -------- |
| 887 | |
| 888 | >>> u, s, v = da.linalg.svd(x) # doctest: +SKIP |
| 889 | |
| 890 | Returns |
| 891 | ------- |
| 892 | |
| 893 | u : (M, K) Array, unitary / orthogonal |
| 894 | Left-singular vectors of `a` (in columns) with shape (M, K) |
| 895 | where K = min(M, N). |
| 896 | s : (K,) Array, singular values in decreasing order (largest first) |
| 897 | Singular values of `a`. |
| 898 | v : (K, N) Array, unitary / orthogonal |
| 899 | Right-singular vectors of `a` (in rows) with shape (K, N) |
| 900 | where K = min(M, N). |
| 901 | |
| 902 | Warnings |
| 903 | -------- |
| 904 | |
| 905 | SVD is only supported for arrays with chunking in one dimension. |
| 906 | This requires that all inputs either contain a single column |
| 907 | of chunks (tall-and-skinny) or a single row of chunks (short-and-fat). |
| 908 | For arrays with chunking in both dimensions, see da.linalg.svd_compressed. |
| 909 | |
| 910 | See Also |
| 911 | -------- |
| 912 | |
| 913 | np.linalg.svd : Equivalent NumPy Operation |
| 914 | da.linalg.svd_compressed : Randomized SVD for fully chunked arrays |
| 915 | dask.array.linalg.tsqr : QR factorization for tall-and-skinny arrays |
| 916 | dask.array.utils.svd_flip : Sign normalization for singular vectors |
| 917 | """ |
| 918 | nb = a.numblocks |
| 919 | if a.ndim != 2: |
| 920 | raise ValueError( |
| 921 | f"Array must be 2D.\nInput shape: {a.shape}\nInput ndim: {a.ndim}\n" |
| 922 | ) |
| 923 | if nb[0] > 1 and nb[1] > 1: |
| 924 | raise NotImplementedError( |
| 925 | "Array must be chunked in one dimension only. " |
| 926 | "This function (svd) only supports tall-and-skinny or short-and-fat " |
| 927 | "matrices (see da.linalg.svd_compressed for SVD on fully chunked arrays).\n" |
| 928 | f"Input shape: {a.shape}\n" |
| 929 | f"Input numblocks: {nb}\n" |
| 930 | ) |
| 931 |