|
1 | 1 | const AbstractBlockSparseMatrix{T} = AbstractBlockSparseArray{T,2} |
| 2 | + |
| 3 | +# SVD is implemented by trying to |
| 4 | +# 1. Attempt to find a block-diagonal implementation by permuting |
| 5 | +# 2. Fallback to AbstractBlockArray implementation via BlockedArray |
| 6 | + |
| 7 | +function eigencopy_oftype(A::AbstractBlockSparseMatrix, T) |
| 8 | + if is_block_permutation_matrix(A) |
| 9 | + Acopy = similar(A, T) |
| 10 | + for bI in eachblockstoredindex(A) |
| 11 | + Acopy[bI] = eigencopy_oftype(@view!(A[bI]), T) |
| 12 | + end |
| 13 | + return Acopy |
| 14 | + else |
| 15 | + return BlockedMatrix{T}(A) |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +function is_block_permutation_matrix(a::AbstractBlockSparseMatrix) |
| 20 | + return allunique(first ∘ Tuple, eachblockstoredindex(a)) && |
| 21 | + allunique(last ∘ Tuple, eachblockstoredindex(a)) |
| 22 | +end |
| 23 | + |
| 24 | +function _allocate_svd_output(A::AbstractBlockSparseMatrix, full::Bool, ::Algorithm) |
| 25 | + @assert !full "TODO" |
| 26 | + bm, bn = blocksize(A) |
| 27 | + bmn = min(bm, bn) |
| 28 | + |
| 29 | + brows = blocklengths(axes(A, 1)) |
| 30 | + bcols = blocklengths(axes(A, 2)) |
| 31 | + slengths = Vector{Int}(undef, bmn) |
| 32 | + |
| 33 | + # fill in values for blocks that are present |
| 34 | + bIs = collect(eachblockstoredindex(A)) |
| 35 | + browIs = Int.(first.(Tuple.(bIs))) |
| 36 | + bcolIs = Int.(last.(Tuple.(bIs))) |
| 37 | + for bI in eachblockstoredindex(A) |
| 38 | + row, col = Int.(Tuple(bI)) |
| 39 | + nrows = brows[row] |
| 40 | + ncols = bcols[col] |
| 41 | + slengths[col] = min(nrows, ncols) |
| 42 | + end |
| 43 | + |
| 44 | + # fill in values for blocks that aren't present, pairing them in order of occurence |
| 45 | + # this is a convention, which at least gives the expected results for blockdiagonal |
| 46 | + emptyrows = setdiff(1:bm, browIs) |
| 47 | + emptycols = setdiff(1:bn, bcolIs) |
| 48 | + for (row, col) in zip(emptyrows, emptycols) |
| 49 | + slengths[col] = min(brows[row], bcols[col]) |
| 50 | + end |
| 51 | + |
| 52 | + s_axis = blockedrange(slengths) |
| 53 | + U = similar(A, axes(A, 1), s_axis) |
| 54 | + S = similar(A, real(eltype(A)), s_axis) |
| 55 | + Vt = similar(A, s_axis, axes(A, 2)) |
| 56 | + |
| 57 | + # also fill in identities for blocks that aren't present |
| 58 | + for (row, col) in zip(emptyrows, emptycols) |
| 59 | + copyto!(@view!(U[Block(row, col)]), LinearAlgebra.I) |
| 60 | + copyto!(@view!(Vt[Block(col, col)]), LinearAlgebra.I) |
| 61 | + end |
| 62 | + |
| 63 | + return U, S, Vt |
| 64 | +end |
| 65 | + |
| 66 | +function svd(A::AbstractBlockSparseMatrix; kwargs...) |
| 67 | + return svd!(eigencopy_oftype(A, LinearAlgebra.eigtype(eltype(A))); kwargs...) |
| 68 | +end |
| 69 | + |
| 70 | +function svd!( |
| 71 | + A::AbstractBlockSparseMatrix; full::Bool=false, alg::Algorithm=default_svd_alg(A) |
| 72 | +) |
| 73 | + @assert is_block_permutation_matrix(A) "Cannot keep sparsity: use `svd` to convert to `BlockedMatrix" |
| 74 | + U, S, Vt = _allocate_svd_output(A, full, alg) |
| 75 | + for bI in eachblockstoredindex(A) |
| 76 | + bUSV = svd!(@view!(A[bI]); full, alg) |
| 77 | + brow, bcol = Tuple(bI) |
| 78 | + U[brow, bcol] = bUSV.U |
| 79 | + S[bcol] = bUSV.S |
| 80 | + Vt[bcol, bcol] = bUSV.Vt |
| 81 | + end |
| 82 | + |
| 83 | + return SVD(U, S, Vt) |
| 84 | +end |
0 commit comments