-
Notifications
You must be signed in to change notification settings - Fork 5
[Feature] Add ishermitian, isantihermitian, hermitianpart(!) and antihermitianpart(!)
#64
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
67cb4b9
Add `hermitianpart!` and `antihermitianpart!`
lkdvos 3e3eb13
Add `ishermitian` and `isantihermitian`
lkdvos 935ff79
Start using in pullbacks
lkdvos f0044ae
blocked (anti)hermitian
Jutho 90db13f
rename to project_
Jutho 42a5565
also change includes
Jutho e22af66
fix polar pullback
Jutho 3924196
add blocked ishermitian
Jutho f4ea67c
improve matrixproperties
Jutho 9e9df93
increase coverage
lkdvos 9f7617a
Update src/MatrixAlgebraKit.jl
lkdvos bb077a1
small rename to strided_ishermitian_exact
lkdvos 2af93e8
add back missing import
lkdvos 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
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,95 @@ | ||
| # Inputs | ||
| # ------ | ||
| function copy_input(::typeof(project_hermitian), A::AbstractMatrix) | ||
| return copy!(similar(A, float(eltype(A))), A) | ||
| end | ||
| copy_input(::typeof(project_antihermitian), A) = copy_input(project_hermitian, A) | ||
|
|
||
| function check_input(::typeof(project_hermitian!), A::AbstractMatrix, B::AbstractMatrix, ::AbstractAlgorithm) | ||
| LinearAlgebra.checksquare(A) | ||
| n = Base.require_one_based_indexing(A) | ||
| B === A || @check_size(B, (n, n)) | ||
| return nothing | ||
| end | ||
| function check_input(::typeof(project_antihermitian!), A::AbstractMatrix, B::AbstractMatrix, ::AbstractAlgorithm) | ||
| LinearAlgebra.checksquare(A) | ||
| n = Base.require_one_based_indexing(A) | ||
| B === A || @check_size(B, (n, n)) | ||
| return nothing | ||
| end | ||
|
|
||
| # Outputs | ||
| # ------- | ||
| function initialize_output(::typeof(project_hermitian!), A::AbstractMatrix, ::NativeBlocked) | ||
| return A | ||
| end | ||
| function initialize_output(::typeof(project_antihermitian!), A::AbstractMatrix, ::NativeBlocked) | ||
| return A | ||
| end | ||
|
|
||
| # Implementation | ||
| # -------------- | ||
| function project_hermitian!(A::AbstractMatrix, B, alg::NativeBlocked) | ||
| check_input(project_hermitian!, A, B, alg) | ||
| return project_hermitian_native!(A, B, Val(false); alg.kwargs...) | ||
| end | ||
| function project_antihermitian!(A::AbstractMatrix, B, alg::NativeBlocked) | ||
| check_input(project_antihermitian!, A, B, alg) | ||
| return project_hermitian_native!(A, B, Val(true); alg.kwargs...) | ||
| end | ||
|
|
||
| function project_hermitian_native!(A::AbstractMatrix, B::AbstractMatrix, anti::Val; blocksize = 32) | ||
| n = size(A, 1) | ||
| for j in 1:blocksize:n | ||
| for i in 1:blocksize:(j - 1) | ||
| jb = min(blocksize, n - j + 1) | ||
| ib = blocksize | ||
| _project_hermitian_offdiag!( | ||
| view(A, i:(i + ib - 1), j:(j + jb - 1)), | ||
| view(A, j:(j + jb - 1), i:(i + ib - 1)), | ||
| view(B, i:(i + ib - 1), j:(j + jb - 1)), | ||
| view(B, j:(j + jb - 1), i:(i + ib - 1)), | ||
| anti | ||
| ) | ||
| end | ||
| jb = min(blocksize, n - j + 1) | ||
| _project_hermitian_diag!( | ||
| view(A, j:(j + jb - 1), j:(j + jb - 1)), | ||
| view(B, j:(j + jb - 1), j:(j + jb - 1)), | ||
| anti | ||
| ) | ||
| end | ||
| return B | ||
| end | ||
|
|
||
| function _project_hermitian_offdiag!( | ||
| Au::AbstractMatrix, Al::AbstractMatrix, Bu::AbstractMatrix, Bl::AbstractMatrix, ::Val{anti} | ||
| ) where {anti} | ||
|
|
||
| m, n = size(Au) # == reverse(size(Au)) | ||
| return @inbounds for j in 1:n | ||
| @simd for i in 1:m | ||
| val = anti ? (Au[i, j] - adjoint(Al[j, i])) / 2 : (Au[i, j] + adjoint(Al[j, i])) / 2 | ||
| Bu[i, j] = val | ||
| aval = adjoint(val) | ||
| Bl[j, i] = anti ? -aval : aval | ||
| end | ||
| end | ||
| return nothing | ||
| end | ||
| function _project_hermitian_diag!(A::AbstractMatrix, B::AbstractMatrix, ::Val{anti}) where {anti} | ||
| n = size(A, 1) | ||
| @inbounds for j in 1:n | ||
| @simd for i in 1:(j - 1) | ||
| val = anti ? (A[i, j] - adjoint(A[j, i])) / 2 : (A[i, j] + adjoint(A[j, i])) / 2 | ||
| B[i, j] = val | ||
| aval = adjoint(val) | ||
| B[j, i] = anti ? -aval : aval | ||
| end | ||
| B[j, j] = anti ? _imimag(A[j, j]) : real(A[j, j]) | ||
| end | ||
| return nothing | ||
| end | ||
|
|
||
| _imimag(x::Real) = zero(x) | ||
| _imimag(x::Complex) = im * imag(x) |
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,45 @@ | ||
| @doc """ | ||
| project_hermitian(A; kwargs...) | ||
| project_hermitian(A, alg) | ||
| project_hermitian!(A; kwargs...) | ||
| project_hermitian!(A, alg) | ||
|
|
||
| Compute the hermitian part of a (square) matrix `A`, defined as `(A + A') / 2`. | ||
| For real matrices this corresponds to the symmetric part of `A`. | ||
|
|
||
| See also [`project_antihermitian`](@ref). | ||
| """ | ||
| @functiondef project_hermitian | ||
|
|
||
| @doc """ | ||
| project_antihermitian(A; kwargs...) | ||
| project_antihermitian(A, alg) | ||
| project_antihermitian!(A; kwargs...) | ||
| project_antihermitian!(A, alg) | ||
|
|
||
| Compute the anti-hermitian part of a (square) matrix `A`, defined as `(A - A') / 2`. | ||
| For real matrices this corresponds to the antisymmetric part of `A`. | ||
|
|
||
| See also [`project_hermitian`](@ref). | ||
| """ | ||
| @functiondef project_antihermitian | ||
|
|
||
| """ | ||
| NativeBlocked(; blocksize = 32) | ||
|
|
||
| Algorithm type to denote a native blocked algorithm with given `blocksize` for computing | ||
| the hermitian or anti-hermitian part of a matrix. | ||
| """ | ||
| @algdef NativeBlocked | ||
lkdvos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # TODO: multithreaded? numthreads keyword? | ||
|
|
||
| default_hermitian_algorithm(A; kwargs...) = default_hermitian_algorithm(typeof(A); kwargs...) | ||
| function default_hermitian_algorithm(::Type{A}; kwargs...) where {A <: AbstractMatrix} | ||
| return NativeBlocked(; kwargs...) | ||
| end | ||
|
|
||
| for f in (:project_hermitian!, :project_antihermitian!) | ||
| @eval function default_algorithm(::typeof($f), ::Type{A}; kwargs...) where {A} | ||
| return default_hermitian_algorithm(A; kwargs...) | ||
| end | ||
| 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
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
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.
Uh oh!
There was an error while loading. Please reload this page.