-
Notifications
You must be signed in to change notification settings - Fork 2
Implement left_orth/right_orth #122
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
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
784c469
[WIP] Implement left_orth/right_orth
mtfishman 1b918eb
Better implementation of left_orth/right_orth
mtfishman d4699b9
Add polar, add tests
mtfishman 969b49e
Improve polar a bit
mtfishman ce43787
More in-place
mtfishman 835c523
Merge branch 'main' into mf/orth
mtfishman 825c816
Simplify implementation
mtfishman bd696cd
Simplify test imports
mtfishman be112c8
Stricter checks, fix left_orth trunc, test trunc
mtfishman 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "BlockSparseArrays" | ||
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" | ||
authors = ["ITensor developers <[email protected]> and contributors"] | ||
version = "0.6.2" | ||
version = "0.6.3" | ||
|
||
[deps] | ||
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" | ||
|
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,96 @@ | ||
using MatrixAlgebraKit: | ||
MatrixAlgebraKit, | ||
left_orth_polar!, | ||
left_orth_qr!, | ||
left_orth_svd!, | ||
left_polar!, | ||
lq_compact!, | ||
qr_compact!, | ||
right_orth_lq!, | ||
right_orth_polar!, | ||
right_orth_svd!, | ||
right_polar!, | ||
select_algorithm, | ||
svd_compact! | ||
|
||
function MatrixAlgebraKit.left_orth!( | ||
A::AbstractBlockSparseMatrix; | ||
trunc=nothing, | ||
kind=isnothing(trunc) ? :qr : :svd, | ||
alg_qr=(; positive=true), | ||
alg_polar=(;), | ||
alg_svd=(;), | ||
) | ||
if !isnothing(trunc) && kind != :svd | ||
throw(ArgumentError("truncation not supported for `left_orth` with `kind=$kind`")) | ||
end | ||
if kind == :qr | ||
return left_orth_qr!(A, alg_qr) | ||
elseif kind == :polar | ||
return left_orth_polar!(A, alg_polar) | ||
elseif kind == :svd | ||
return left_orth_svd!(A, alg_svd, trunc) | ||
else | ||
throw(ArgumentError("`left_orth` received unknown value `kind = $kind`")) | ||
end | ||
end | ||
function MatrixAlgebraKit.left_orth_qr!(A::AbstractBlockSparseMatrix, alg) | ||
alg′ = select_algorithm(qr_compact!, A, alg) | ||
return qr_compact!(A, alg′) | ||
end | ||
function MatrixAlgebraKit.left_orth_polar!(A::AbstractBlockSparseMatrix, alg) | ||
alg′ = select_algorithm(left_polar!, A, alg) | ||
return left_polar!(A, alg′) | ||
end | ||
function MatrixAlgebraKit.left_orth_svd!( | ||
A::AbstractBlockSparseMatrix, alg, trunc::Nothing=nothing | ||
) | ||
alg′ = select_algorithm(svd_compact!, A, alg) | ||
U, S, Vᴴ = svd_compact!(A, alg′) | ||
return U, S * Vᴴ | ||
end | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function MatrixAlgebraKit.right_orth!( | ||
A::AbstractBlockSparseMatrix; | ||
trunc=nothing, | ||
kind=isnothing(trunc) ? :lq : :svd, | ||
alg_lq=(; positive=true), | ||
alg_polar=(;), | ||
alg_svd=(;), | ||
) | ||
if !isnothing(trunc) && kind != :svd | ||
throw(ArgumentError("truncation not supported for `right_orth` with `kind=$kind`")) | ||
end | ||
if kind == :qr | ||
# TODO: Implement this. | ||
# return right_orth_lq!(A, alg_lq) | ||
return right_orth_svd!(A, alg_svd) | ||
elseif kind == :polar | ||
return right_orth_polar!(A, alg_polar) | ||
elseif kind == :svd | ||
return right_orth_svd!(A, alg_svd, trunc) | ||
else | ||
throw(ArgumentError("`right_orth` received unknown value `kind = $kind`")) | ||
end | ||
end | ||
function MatrixAlgebraKit.right_orth_lq!(A::AbstractBlockSparseMatrix, alg) | ||
alg′ = select_algorithm(lq_compact, A, alg) | ||
return lq_compact!(A, alg′) | ||
end | ||
function MatrixAlgebraKit.right_orth_polar!(A::AbstractBlockSparseMatrix, alg) | ||
alg′ = select_algorithm(right_polar!, A, alg) | ||
return right_polar!(A, alg′) | ||
end | ||
function MatrixAlgebraKit.right_orth_svd!( | ||
A::AbstractBlockSparseMatrix, alg, trunc::Nothing=nothing | ||
) | ||
alg′ = select_algorithm(svd_compact!, A, alg) | ||
U, S, Vᴴ = svd_compact!(A, alg′) | ||
return U * S, Vᴴ | ||
end | ||
function MatrixAlgebraKit.right_orth_svd!(A::AbstractBlockSparseMatrix, alg, trunc) | ||
alg′ = select_algorithm(svd_compact!, A, alg) | ||
alg_trunc = select_algorithm(svd_trunc!, A, alg′; trunc) | ||
U, S, Vᴴ = svd_trunc!(A, alg_trunc) | ||
return U * S, Vᴴ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using MatrixAlgebraKit: | ||
MatrixAlgebraKit, | ||
PolarViaSVD, | ||
check_input, | ||
default_algorithm, | ||
left_polar!, | ||
right_polar!, | ||
svd_compact! | ||
|
||
function MatrixAlgebraKit.check_input(::typeof(left_polar!), A::AbstractBlockSparseMatrix) | ||
@views for I in eachblockstoredindex(A) | ||
m, n = size(A[I]) | ||
m >= n || | ||
throw(ArgumentError("each input matrix block needs at least as many rows as columns")) | ||
end | ||
lkdvos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nothing | ||
end | ||
function MatrixAlgebraKit.check_input(::typeof(right_polar!), A::AbstractBlockSparseMatrix) | ||
@views for I in eachblockstoredindex(A) | ||
m, n = size(A[I]) | ||
m <= n || | ||
throw(ArgumentError("each input matrix block needs at least as many columns as rows")) | ||
end | ||
return nothing | ||
end | ||
|
||
function MatrixAlgebraKit.left_polar!(A::AbstractBlockSparseMatrix, alg::PolarViaSVD) | ||
check_input(left_polar!, A) | ||
# TODO: Use more in-place operations here, avoid `copy`. | ||
lkdvos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
U, S, Vᴴ = svd_compact!(A, alg.svdalg) | ||
W = U * Vᴴ | ||
P = copy(Vᴴ') * S * Vᴴ | ||
return (W, P) | ||
end | ||
function MatrixAlgebraKit.right_polar!(A::AbstractBlockSparseMatrix, alg::PolarViaSVD) | ||
check_input(right_polar!, A) | ||
# TODO: Use more in-place operations here, avoid `copy`. | ||
U, S, Vᴴ = svd_compact!(A, alg.svdalg) | ||
Wᴴ = U * Vᴴ | ||
P = U * S * copy(U') | ||
return (P, Wᴴ) | ||
end | ||
|
||
function MatrixAlgebraKit.default_algorithm( | ||
::typeof(left_polar!), a::AbstractBlockSparseMatrix; kwargs... | ||
) | ||
return PolarViaSVD(default_algorithm(svd_compact!, a; kwargs...)) | ||
end | ||
function MatrixAlgebraKit.default_algorithm( | ||
::typeof(right_polar!), a::AbstractBlockSparseMatrix; kwargs... | ||
) | ||
return PolarViaSVD(default_algorithm(svd_compact!, a; kwargs...)) | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this not caught by the MatrixAlgebraKit implementation? If not, it probably should?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit subtle, the MatrixAlgebraKit.jl version is designed around the output being pre-allocated, so it has an extra argument for the output. That is tricky to support for block sparse matrices since the different backends (SVD, QR/LQ, polar) might have different block structures/sectors so it isn't straightforward to pre-allocate the output in that way.
We can move this code over to MatrixAlgebraKit.jl, that was the main thing I wanted your feedback on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see now! This was indeed something that I at some point saw in MatrixAlgebraKit but then kind of forgot about in the end.
My initial reaction would be that I do think this should indeed be moved over there, such that the general codepath follows that of the
@algdef
-defined functions, with a little extra implementations that make it hard to simply use@algdef
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, in that case I'll make a PR to MatrixAlgebraKit.jl, it would be nice to not have this logic here.