-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Symmetric/Hermitian rules and tests to own file (#322)
* Move symmetric rules to own file * Move symmetric tests to own file * Increment version number
- Loading branch information
Showing
7 changed files
with
126 additions
and
123 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,79 @@ | ||
##### | ||
##### `Symmetric`/`Hermitian` | ||
##### | ||
|
||
function frule((_, ΔA, _), T::Type{<:LinearAlgebra.HermOrSym}, A::AbstractMatrix, uplo) | ||
return T(A, uplo), T(ΔA, uplo) | ||
end | ||
|
||
function rrule(T::Type{<:LinearAlgebra.HermOrSym}, A::AbstractMatrix, uplo) | ||
Ω = T(A, uplo) | ||
function HermOrSym_pullback(ΔΩ) | ||
return (NO_FIELDS, _symherm_back(T, ΔΩ, Ω.uplo), DoesNotExist()) | ||
end | ||
return Ω, HermOrSym_pullback | ||
end | ||
|
||
function frule((_, ΔA), TM::Type{<:Matrix}, A::LinearAlgebra.HermOrSym) | ||
return TM(A), TM(_symherm_forward(A, ΔA)) | ||
end | ||
function frule((_, ΔA), ::Type{Array}, A::LinearAlgebra.HermOrSym) | ||
return Array(A), Array(_symherm_forward(A, ΔA)) | ||
end | ||
|
||
function rrule(TM::Type{<:Matrix}, A::LinearAlgebra.HermOrSym) | ||
function Matrix_pullback(ΔΩ) | ||
TA = _symhermtype(A) | ||
T∂A = TA{eltype(ΔΩ),typeof(ΔΩ)} | ||
uplo = A.uplo | ||
∂A = T∂A(_symherm_back(A, ΔΩ, uplo), uplo) | ||
return NO_FIELDS, ∂A | ||
end | ||
return TM(A), Matrix_pullback | ||
end | ||
rrule(::Type{Array}, A::LinearAlgebra.HermOrSym) = rrule(Matrix, A) | ||
|
||
# Get type (Symmetric or Hermitian) from type or matrix | ||
_symhermtype(::Type{<:Symmetric}) = Symmetric | ||
_symhermtype(::Type{<:Hermitian}) = Hermitian | ||
_symhermtype(A) = _symhermtype(typeof(A)) | ||
|
||
# for Ω = Matrix(A::HermOrSym), push forward ΔA to get ∂Ω | ||
function _symherm_forward(A, ΔA) | ||
TA = _symhermtype(A) | ||
return if ΔA isa TA | ||
ΔA | ||
else | ||
TA{eltype(ΔA),typeof(ΔA)}(ΔA, A.uplo) | ||
end | ||
end | ||
|
||
# for Ω = HermOrSym(A, uplo), pull back ΔΩ to get ∂A | ||
_symherm_back(::Type{<:Symmetric}, ΔΩ, uplo) = _symmetric_back(ΔΩ, uplo) | ||
function _symherm_back(::Type{<:Hermitian}, ΔΩ::AbstractMatrix{<:Real}, uplo) | ||
return _symmetric_back(ΔΩ, uplo) | ||
end | ||
_symherm_back(::Type{<:Hermitian}, ΔΩ, uplo) = _hermitian_back(ΔΩ, uplo) | ||
_symherm_back(Ω, ΔΩ, uplo) = _symherm_back(typeof(Ω), ΔΩ, uplo) | ||
|
||
function _symmetric_back(ΔΩ, uplo) | ||
L, U, D = LowerTriangular(ΔΩ), UpperTriangular(ΔΩ), Diagonal(ΔΩ) | ||
return uplo == 'U' ? U .+ transpose(L) - D : L .+ transpose(U) - D | ||
end | ||
_symmetric_back(ΔΩ::Diagonal, uplo) = ΔΩ | ||
_symmetric_back(ΔΩ::UpperTriangular, uplo) = Matrix(uplo == 'U' ? ΔΩ : transpose(ΔΩ)) | ||
_symmetric_back(ΔΩ::LowerTriangular, uplo) = Matrix(uplo == 'U' ? transpose(ΔΩ) : ΔΩ) | ||
|
||
function _hermitian_back(ΔΩ, uplo) | ||
L, U, rD = LowerTriangular(ΔΩ), UpperTriangular(ΔΩ), real.(Diagonal(ΔΩ)) | ||
return uplo == 'U' ? U .+ L' - rD : L .+ U' - rD | ||
end | ||
_hermitian_back(ΔΩ::Diagonal, uplo) = real.(ΔΩ) | ||
function _hermitian_back(ΔΩ::LinearAlgebra.AbstractTriangular, uplo) | ||
∂UL = ΔΩ .- Diagonal(_extract_imag.(diag(ΔΩ))) | ||
return if istriu(ΔΩ) | ||
return Matrix(uplo == 'U' ? ∂UL : ∂UL') | ||
else | ||
return Matrix(uplo == 'U' ? ∂UL' : ∂UL) | ||
end | ||
end |
This file contains 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 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,44 @@ | ||
@testset "Symmetric/Hermitian rules" begin | ||
@testset "$(SymHerm)(::AbstractMatrix{$T}, :$(uplo))" for | ||
SymHerm in (Symmetric, Hermitian), | ||
T in (Float64, ComplexF64), | ||
uplo in (:U, :L) | ||
|
||
N = 3 | ||
@testset "frule" begin | ||
x = randn(T, N, N) | ||
Δx = randn(T, N, N) | ||
# can't use frule_test here because it doesn't yet ignore nothing tangents | ||
Ω = SymHerm(x, uplo) | ||
Ω_ad, ∂Ω_ad = frule((Zero(), Δx, Zero()), SymHerm, x, uplo) | ||
@test Ω_ad == Ω | ||
∂Ω_fd = jvp(_fdm, z -> SymHerm(z, uplo), (x, Δx)) | ||
@test ∂Ω_ad ≈ ∂Ω_fd | ||
end | ||
@testset "rrule" begin | ||
x = randn(T, N, N) | ||
∂x = randn(T, N, N) | ||
ΔΩ = randn(T, N, N) | ||
@testset "back(::$MT)" for MT in (Matrix, LowerTriangular, UpperTriangular) | ||
rrule_test(SymHerm, MT(ΔΩ), (x, ∂x), (uplo, nothing)) | ||
end | ||
@testset "back(::Diagonal)" begin | ||
rrule_test(SymHerm, Diagonal(ΔΩ), (x, Diagonal(∂x)), (uplo, nothing)) | ||
end | ||
end | ||
end | ||
@testset "$(f)(::$(SymHerm){$T}) with uplo=:$uplo" for f in (Matrix, Array), | ||
SymHerm in (Symmetric, Hermitian), | ||
T in (Float64, ComplexF64), | ||
uplo in (:U, :L) | ||
|
||
N = 3 | ||
x = SymHerm(randn(T, N, N), uplo) | ||
Δx = randn(T, N, N) | ||
∂x = SymHerm(randn(T, N, N), uplo) | ||
ΔΩ = f(SymHerm(randn(T, N, N), uplo)) | ||
frule_test(f, (x, Δx)) | ||
frule_test(f, (x, SymHerm(Δx, uplo))) | ||
rrule_test(f, ΔΩ, (x, ∂x)) | ||
end | ||
end |
This file contains 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
eb10848
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.
@JuliaRegistrator register
eb10848
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.
Registration pull request created: JuliaRegistries/General/25905
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:
Also, note the warning: Version 0.7.36 skips over 0.7.35
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.