Compute the lu decomposition of a matrix. Examples -------- >>> p, l, u = da.linalg.lu(x) # doctest: +SKIP Returns ------- p: Array, permutation matrix l: Array, lower triangular matrix with unit diagonal. u: Array, upper triangular matrix
(a)
| 967 | |
| 968 | |
| 969 | def lu(a): |
| 970 | """ |
| 971 | Compute the lu decomposition of a matrix. |
| 972 | |
| 973 | Examples |
| 974 | -------- |
| 975 | |
| 976 | >>> p, l, u = da.linalg.lu(x) # doctest: +SKIP |
| 977 | |
| 978 | Returns |
| 979 | ------- |
| 980 | |
| 981 | p: Array, permutation matrix |
| 982 | l: Array, lower triangular matrix with unit diagonal. |
| 983 | u: Array, upper triangular matrix |
| 984 | """ |
| 985 | import scipy.linalg |
| 986 | |
| 987 | if a.ndim != 2: |
| 988 | raise ValueError("Dimension must be 2 to perform lu decomposition") |
| 989 | |
| 990 | xdim, ydim = a.shape |
| 991 | if xdim != ydim: |
| 992 | raise ValueError("Input must be a square matrix to perform lu decomposition") |
| 993 | if len(set(a.chunks[0] + a.chunks[1])) != 1: |
| 994 | msg = ( |
| 995 | "All chunks must be a square matrix to perform lu decomposition. " |
| 996 | "Use .rechunk method to change the size of chunks." |
| 997 | ) |
| 998 | raise ValueError(msg) |
| 999 | |
| 1000 | vdim = len(a.chunks[0]) |
| 1001 | hdim = len(a.chunks[1]) |
| 1002 | |
| 1003 | token = tokenize(a) |
| 1004 | name_lu = "lu-lu-" + token |
| 1005 | |
| 1006 | name_p = "lu-p-" + token |
| 1007 | name_l = "lu-l-" + token |
| 1008 | name_u = "lu-u-" + token |
| 1009 | |
| 1010 | # for internal calculation |
| 1011 | name_p_inv = "lu-p-inv-" + token |
| 1012 | name_l_permuted = "lu-l-permute-" + token |
| 1013 | name_u_transposed = "lu-u-transpose-" + token |
| 1014 | name_plu_dot = "lu-plu-dot-" + token |
| 1015 | name_lu_dot = "lu-lu-dot-" + token |
| 1016 | |
| 1017 | dsk = {} |
| 1018 | for i in range(min(vdim, hdim)): |
| 1019 | target = (a.name, i, i) |
| 1020 | if i > 0: |
| 1021 | prevs = [] |
| 1022 | for p in range(i): |
| 1023 | prev = name_plu_dot, i, p, p, i |
| 1024 | dsk[prev] = (np.dot, (name_l_permuted, i, p), (name_u, p, i)) |
| 1025 | prevs.append(prev) |
| 1026 | target = (operator.sub, target, (sum, prevs)) |
no test coverage detected