-
Notifications
You must be signed in to change notification settings - Fork 33
Add support for SVD on BlockArray
#426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lkdvos
wants to merge
3
commits into
JuliaArrays:master
Choose a base branch
from
lkdvos:svd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#= | ||
SVD on blockmatrices: | ||
Interpret the matrix as a linear map acting on vector spaces with a direct sum structure, which is reflected in the structure of U and V. | ||
In the generic case, the SVD does not preserve this structure, and can mix up the blocks, so S becomes a single block. | ||
(Implementation-wise, also most efficiently done by first mapping to a `BlockedArray`) | ||
In the case of `BlockDiagonal` however, the structure is preserved and carried over to the structure of `S`. | ||
=# | ||
|
||
LinearAlgebra.eigencopy_oftype(A::AbstractBlockMatrix, S) = BlockedArray(Array{S}(A), blocksizes(A, 1), blocksizes(A, 2)) | ||
|
||
function LinearAlgebra.eigencopy_oftype(A::BlockDiagonal, S) | ||
diag = map(Base.Fix2(LinearAlgebra.eigencopy_oftype, S), A.blocks.diag) | ||
return BlockDiagonal(diag) | ||
end | ||
|
||
function LinearAlgebra.svd!(A::BlockedMatrix; full::Bool=false, alg::LinearAlgebra.Algorithm=default_svd_alg(A)) | ||
USV = svd!(parent(A); full, alg) | ||
|
||
# restore block pattern | ||
m = length(USV.S) | ||
bsz1, bsz2, bsz3 = blocksizes(A, 1), [m], blocksizes(A, 2) | ||
|
||
u = BlockedArray(USV.U, bsz1, bsz2) | ||
s = BlockedVector(USV.S, bsz2) | ||
vt = BlockedArray(USV.Vt, bsz2, bsz3) | ||
return SVD(u, s, vt) | ||
end | ||
|
||
function LinearAlgebra.svd!(A::BlockDiagonal; full::Bool=false, alg::LinearAlgebra.Algorithm=default_svd_alg(A)) | ||
USVs = map(a -> svd!(a; full, alg), A.blocks.diag) | ||
Us = map(Base.Fix2(getproperty, :U), USVs) | ||
Ss = map(Base.Fix2(getproperty, :S), USVs) | ||
Vts = map(Base.Fix2(getproperty, :Vt), USVs) | ||
return SVD(BlockDiagonal(Us), mortar(Ss), BlockDiagonal(Vts)) | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
module TestBlockSVD | ||
|
||
using BlockArrays, Test, LinearAlgebra, Random | ||
using BlockArrays: BlockDiagonal | ||
|
||
Random.seed!(0) | ||
|
||
eltypes = (Float32, Float64, ComplexF32, ComplexF64, Int) | ||
|
||
@testset "Block SVD ($T)" for T in eltypes | ||
x = rand(T, 4, 4) | ||
USV = svd(x) | ||
U, S, Vt = USV.U, USV.S, USV.Vt | ||
|
||
y = BlockArray(x, [2, 2], [2, 2]) | ||
# https://github.com/JuliaArrays/BlockArrays.jl/issues/425 | ||
# USV_blocked = @inferred svd(y) | ||
USV_block = svd(y) | ||
U_block, S_block, Vt_block = USV_block.U, USV_block.S, USV_block.Vt | ||
|
||
# test types | ||
@test U_block isa BlockedMatrix | ||
@test eltype(U_block) == float(T) | ||
@test S_block isa BlockedVector | ||
@test eltype(S_block) == real(float(T)) | ||
@test Vt_block isa BlockedMatrix | ||
@test eltype(Vt_block) == float(T) | ||
|
||
# test structure | ||
@test blocksizes(U_block, 1) == blocksizes(y, 1) | ||
@test length(blocksizes(U_block, 2)) == 1 | ||
@test blocksizes(Vt_block, 2) == blocksizes(y, 2) | ||
@test length(blocksizes(Vt_block, 1)) == 1 | ||
|
||
# test correctness | ||
@test U_block ≈ U | ||
@test S_block ≈ S | ||
@test Vt_block ≈ Vt | ||
@test U_block * Diagonal(S_block) * Vt_block ≈ y | ||
end | ||
|
||
@testset "Blocked SVD ($T)" for T in eltypes | ||
x = rand(T, 4, 4) | ||
USV = svd(x) | ||
U, S, Vt = USV.U, USV.S, USV.Vt | ||
|
||
y = BlockedArray(x, [2, 2], [2, 2]) | ||
# https://github.com/JuliaArrays/BlockArrays.jl/issues/425 | ||
# USV_blocked = @inferred svd(y) | ||
USV_blocked = svd(y) | ||
U_blocked, S_blocked, Vt_blocked = USV_blocked.U, USV_blocked.S, USV_blocked.Vt | ||
|
||
# test types | ||
@test U_blocked isa BlockedMatrix | ||
@test eltype(U_blocked) == float(T) | ||
@test S_blocked isa BlockedVector | ||
@test eltype(S_blocked) == real(float(T)) | ||
@test Vt_blocked isa BlockedMatrix | ||
@test eltype(Vt_blocked) == float(T) | ||
|
||
# test structure | ||
@test blocksizes(U_blocked, 1) == blocksizes(y, 1) | ||
@test length(blocksizes(U_blocked, 2)) == 1 | ||
@test blocksizes(Vt_blocked, 2) == blocksizes(y, 2) | ||
@test length(blocksizes(Vt_blocked, 1)) == 1 | ||
|
||
# test correctness | ||
@test U_blocked ≈ U | ||
@test S_blocked ≈ S | ||
@test Vt_blocked ≈ Vt | ||
@test U_blocked * Diagonal(S_blocked) * Vt_blocked ≈ y | ||
end | ||
|
||
@testset "BlockDiagonal SVD ($T)" for T in eltypes | ||
blocksz = (2, 3, 1) | ||
y = BlockDiagonal([rand(T, d, d) for d in blocksz]) | ||
x = Array(y) | ||
|
||
USV = svd(x) | ||
U, S, Vt = USV.U, USV.S, USV.Vt | ||
|
||
# https://github.com/JuliaArrays/BlockArrays.jl/issues/425 | ||
# USV_blocked = @inferred svd(y) | ||
USV_block = svd(y) | ||
U_block, S_block, Vt_block = USV_block.U, USV_block.S, USV_block.Vt | ||
|
||
# test types | ||
@test U_block isa BlockDiagonal | ||
@test eltype(U_block) == float(T) | ||
@test S_block isa BlockVector | ||
@test eltype(S_block) == real(float(T)) | ||
@test Vt_block isa BlockDiagonal | ||
@test eltype(Vt_block) == float(T) | ||
|
||
# test structure | ||
@test blocksizes(U_block, 1) == blocksizes(y, 1) | ||
@test length(blocksizes(U_block, 2)) == length(blocksz) | ||
@test blocksizes(Vt_block, 2) == blocksizes(y, 2) | ||
@test length(blocksizes(Vt_block, 1)) == length(blocksz) | ||
|
||
# test correctness: SVD is not unique, so cannot compare to dense | ||
@test U_block * BlockDiagonal(Diagonal.(S_block.blocks)) * Vt_block ≈ y | ||
@test U_block' * U_block ≈ LinearAlgebra.I | ||
@test Vt_block * Vt_block' ≈ LinearAlgebra.I | ||
end | ||
|
||
end # module |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.