|
| 1 | +using ArrayLayouts: LayoutMatrix |
| 2 | +using LinearAlgebra: LinearAlgebra, Diagonal |
| 3 | + |
| 4 | +function qr(a::AbstractArray, biperm::BlockedPermutation{2}) |
| 5 | + a_matricized = fusedims(a, biperm) |
| 6 | + # TODO: Make this more generic, allow choosing thin or full, |
| 7 | + # make sure this works on GPU. |
| 8 | + q_fact, r_matricized = LinearAlgebra.qr(a_matricized) |
| 9 | + q_matricized = typeof(a_matricized)(q_fact) |
| 10 | + axes_codomain, axes_domain = blockpermute(axes(a), biperm) |
| 11 | + axes_q = (axes_codomain..., axes(q_matricized, 2)) |
| 12 | + axes_r = (axes(r_matricized, 1), axes_domain...) |
| 13 | + q = splitdims(q_matricized, axes_q) |
| 14 | + r = splitdims(r_matricized, axes_r) |
| 15 | + return q, r |
| 16 | +end |
| 17 | + |
| 18 | +function qr(a::AbstractArray, labels_a, labels_codomain, labels_domain) |
| 19 | + # TODO: Generalize to conversion to `Tuple` isn't needed. |
| 20 | + return qr( |
| 21 | + a, blockedperm_indexin(Tuple(labels_a), Tuple(labels_codomain), Tuple(labels_domain)) |
| 22 | + ) |
| 23 | +end |
| 24 | + |
| 25 | +function svd(a::AbstractArray, biperm::BlockedPermutation{2}) |
| 26 | + a_matricized = fusedims(a, biperm) |
| 27 | + usv_matricized = LinearAlgebra.svd(a_matricized) |
| 28 | + u_matricized = usv_matricized.U |
| 29 | + s_diag = usv_matricized.S |
| 30 | + v_matricized = usv_matricized.Vt |
| 31 | + axes_codomain, axes_domain = blockpermute(axes(a), biperm) |
| 32 | + axes_u = (axes_codomain..., axes(u_matricized, 2)) |
| 33 | + axes_v = (axes(v_matricized, 1), axes_domain...) |
| 34 | + u = splitdims(u_matricized, axes_u) |
| 35 | + # TODO: Use `DiagonalArrays.diagonal` to make it more general. |
| 36 | + s = Diagonal(s_diag) |
| 37 | + v = splitdims(v_matricized, axes_v) |
| 38 | + return u, s, v |
| 39 | +end |
| 40 | + |
| 41 | +function svd(a::AbstractArray, labels_a, labels_codomain, labels_domain) |
| 42 | + return svd( |
| 43 | + a, blockedperm_indexin(Tuple(labels_a), Tuple(labels_codomain), Tuple(labels_domain)) |
| 44 | + ) |
| 45 | +end |
0 commit comments