|
| 1 | +using MatrixAlgebraKit: TruncationStrategy, diagview, svd_trunc! |
| 2 | + |
| 3 | +function MatrixAlgebraKit.diagview(A::BlockSparseMatrix{T,Diagonal{T,Vector{T}}}) where {T} |
| 4 | + D = BlockSparseVector{T}(undef, axes(A, 1)) |
| 5 | + for I in eachblockstoredindex(A) |
| 6 | + if ==(Int.(Tuple(I))...) |
| 7 | + D[Tuple(I)[1]] = diagview(A[I]) |
| 8 | + end |
| 9 | + end |
| 10 | + return D |
| 11 | +end |
| 12 | + |
| 13 | +""" |
| 14 | + BlockPermutedDiagonalTruncationStrategy(strategy::TruncationStrategy) |
| 15 | +
|
| 16 | +A wrapper for `TruncationStrategy` that implements the wrapped strategy on a block-by-block |
| 17 | +basis, which is possible if the input matrix is a block-diagonal matrix or a block permuted |
| 18 | +block-diagonal matrix. |
| 19 | +""" |
| 20 | +struct BlockPermutedDiagonalTruncationStrategy{T<:TruncationStrategy} <: TruncationStrategy |
| 21 | + strategy::T |
| 22 | +end |
| 23 | + |
| 24 | +const TBlockUSVᴴ = Tuple{ |
| 25 | + <:AbstractBlockSparseMatrix,<:AbstractBlockSparseMatrix,<:AbstractBlockSparseMatrix |
| 26 | +} |
| 27 | + |
| 28 | +function MatrixAlgebraKit.truncate!( |
| 29 | + ::typeof(svd_trunc!), (U, S, Vᴴ)::TBlockUSVᴴ, strategy::TruncationStrategy |
| 30 | +) |
| 31 | + # TODO assert blockdiagonal |
| 32 | + return MatrixAlgebraKit.truncate!( |
| 33 | + svd_trunc!, (U, S, Vᴴ), BlockPermutedDiagonalTruncationStrategy(strategy) |
| 34 | + ) |
| 35 | +end |
| 36 | + |
| 37 | +# cannot use regular slicing here: I want to slice without altering blockstructure |
| 38 | +# solution: use boolean indexing and slice the mask, effectively cheaply inverting the map |
| 39 | +function MatrixAlgebraKit.findtruncated( |
| 40 | + values::AbstractVector, strategy::BlockPermutedDiagonalTruncationStrategy |
| 41 | +) |
| 42 | + ind = MatrixAlgebraKit.findtruncated(values, strategy.strategy) |
| 43 | + indexmask = falses(length(values)) |
| 44 | + indexmask[ind] .= true |
| 45 | + return indexmask |
| 46 | +end |
| 47 | + |
| 48 | +function MatrixAlgebraKit.truncate!( |
| 49 | + ::typeof(svd_trunc!), |
| 50 | + (U, S, Vᴴ)::TBlockUSVᴴ, |
| 51 | + strategy::BlockPermutedDiagonalTruncationStrategy, |
| 52 | +) |
| 53 | + indexmask = MatrixAlgebraKit.findtruncated(diagview(S), strategy) |
| 54 | + |
| 55 | + # first determine the block structure of the output to avoid having assumptions on the |
| 56 | + # data structures |
| 57 | + ax = axes(S, 1) |
| 58 | + counter = Base.Fix1(count, Base.Fix1(getindex, indexmask)) |
| 59 | + Slengths = filter!(>(0), map(counter, blocks(ax))) |
| 60 | + Sax = blockedrange(Slengths) |
| 61 | + Ũ = similar(U, axes(U, 1), Sax) |
| 62 | + S̃ = similar(S, Sax, Sax) |
| 63 | + Ṽᴴ = similar(Vᴴ, Sax, axes(Vᴴ, 2)) |
| 64 | + |
| 65 | + # then loop over the blocks and assign the data |
| 66 | + # TODO: figure out if we can presort and loop over the blocks - |
| 67 | + # for now this has issues with missing blocks |
| 68 | + bI_Us = collect(eachblockstoredindex(U)) |
| 69 | + bI_Ss = collect(eachblockstoredindex(S)) |
| 70 | + bI_Vᴴs = collect(eachblockstoredindex(Vᴴ)) |
| 71 | + |
| 72 | + I′ = 0 # number of skipped blocks that got fully truncated |
| 73 | + for I in 1:blocksize(ax, 1) |
| 74 | + b = ax[Block(I)] |
| 75 | + mask = indexmask[b] |
| 76 | + |
| 77 | + if !any(mask) |
| 78 | + I′ += 1 |
| 79 | + continue |
| 80 | + end |
| 81 | + |
| 82 | + bU_id = @something findfirst(x -> last(Tuple(x)) == Block(I), bI_Us) error( |
| 83 | + "No U-block found for $I" |
| 84 | + ) |
| 85 | + bU = Tuple(bI_Us[bU_id]) |
| 86 | + Ũ[bU[1], bU[2] - Block(I′)] = view(U, bU...)[:, mask] |
| 87 | + |
| 88 | + bVᴴ_id = @something findfirst(x -> first(Tuple(x)) == Block(I), bI_Vᴴs) error( |
| 89 | + "No Vᴴ-block found for $I" |
| 90 | + ) |
| 91 | + bVᴴ = Tuple(bI_Vᴴs[bVᴴ_id]) |
| 92 | + Ṽᴴ[bVᴴ[1] - Block(I′), bVᴴ[2]] = view(Vᴴ, bVᴴ...)[mask, :] |
| 93 | + |
| 94 | + bS_id = findfirst(x -> last(Tuple(x)) == Block(I), bI_Ss) |
| 95 | + if !isnothing(bS_id) |
| 96 | + bS = Tuple(bI_Ss[bS_id]) |
| 97 | + S̃[(bS .- Block(I′))...] = Diagonal(diagview(view(S, bS...))[mask]) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + return Ũ, S̃, Ṽᴴ |
| 102 | +end |
0 commit comments