-
Notifications
You must be signed in to change notification settings - Fork 4
Automatic differentiation of pfaffian #143
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
Gertian
wants to merge
19
commits into
JuliaLinearAlgebra:main
Choose a base branch
from
Gertian:main
base: main
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.
+80
−3
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8ede09e
Initial commit with AD
Gertian f825ee2
updated toml file to also include autodiff optionality
Gertian 8bc8dac
Moved tests to test
Gertian 8d677ee
Moved tests to tests
Gertian 686bde8
add extension
lkdvos 3c5e6b2
Add tests
lkdvos 3396118
moved chainrules to extensiony
Gertian 6d227f5
updated toml and introduced module
Gertian 97ae6d6
added deps
Gertian 70469b9
added missing dependency to LinearAlgebra
Gertian f0c1da4
fixed Toml file.
Gertian 45700a9
removed my old, faulty test from the testset.
Gertian 17b1c44
retrigger checks
Gertian 7b18216
further tweaked tests
Gertian f8a9fd8
patched constructor AD so that it can take both matrix and AS-matrix
Gertian bfbdfa6
Update ext/SkewLinearAlgebraChainRulesCoreExt.jl
Gertian 2071486
Update test/chainrulestests.jl
Gertian 5a44cda
Update test/chainrulestests.jl
Gertian 5fd7793
Update test/chainrulestests.jl
stevengj 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,19 +1,30 @@ | ||
name = "SkewLinearAlgebra" | ||
uuid = "5c889d49-8c60-4500-9d10-5d3a22e2f4b9" | ||
authors = ["smataigne <[email protected]> and contributors"] | ||
version = "1.0" | ||
version = "1.0.0" | ||
|
||
[deps] | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
|
||
[weakdeps] | ||
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
|
||
[extensions] | ||
SkewLinearAlgebraChainRulesCoreExt = "ChainRulesCore" | ||
|
||
[compat] | ||
julia = "1.6" | ||
LinearAlgebra = "1.6" | ||
ChainRulesCore = "1" | ||
julia = "1.6" | ||
|
||
[extras] | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" | ||
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" | ||
|
||
[targets] | ||
test = ["Test","Random", "SparseArrays"] | ||
test = ["Test", "LinearAlgebra", "Random", "SparseArrays", "ChainRulesCore", "ChainRulesTestUtils", "FiniteDifferences"] |
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,30 @@ | ||
module SkewLinearAlgebraChainRulesCoreExt | ||
|
||
using LinearAlgebra | ||
using SkewLinearAlgebra | ||
using ChainRulesCore | ||
|
||
|
||
function ChainRulesCore.rrule(::Type{SkewHermitian}, val) | ||
y = SkewHermitian(val) | ||
function Foo_pb(ΔFoo) | ||
if isa(ΔFoo, SkewHermitian) | ||
return NoTangent(), unthunk(ΔFoo).data | ||
else | ||
return (NoTangent(), unthunk(ΔFoo)) | ||
end | ||
end | ||
return y, Foo_pb | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(pfaffian), A::SkewHermitian) | ||
Ω = pfaffian(A) | ||
pfaffian_pullback(ΔΩ) = NoTangent(), SkewHermitian(rmul!(inv(A)', dot(Ω, ΔΩ))) #potentially need the 0.5 here ! | ||
return Ω, pfaffian_pullback | ||
end | ||
|
||
function ChainRulesCore.ProjectTo{<:SkewHermitian}(A::AbstractArray) | ||
return skewhermitian(A) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using ChainRulesTestUtils | ||
using Random | ||
using SkewLinearAlgebra | ||
using LinearAlgebra | ||
using FiniteDifferences | ||
|
||
ChainRulesTestUtils.rand_tangent(rng::AbstractRNG, x::SkewHermitian) = skewhermitian!(rand(rng, eltype(x), size(x)...)) | ||
|
||
# Required to make finite differences behave correctly | ||
function FiniteDifferences.to_vec(A::SkewHermitian) | ||
m = size(A, 1) | ||
v = [A[i,j] for i in 2:m for j in 1:i-1] | ||
function from_vec(v) | ||
B = zero(A) | ||
k = 1 | ||
for i in 2:m, j in 1:i-1 | ||
@inbounds B[i,j] = v[k] | ||
k += 1 | ||
end | ||
return B | ||
end | ||
return v, from_vec | ||
end | ||
|
||
@testset "automatic differentiation" begin | ||
m = 10 | ||
inds = [1,2] | ||
A = skewhermitian(rand(m, m)) | ||
|
||
test_rrule(SkewHermitian, A) # test constructor | ||
|
||
test_rrule(pfaffian, A) # test pfaffian | ||
test_rrule(pfaffian, SkewHermitian(A[inds, inds])) # test pfaffian of submatrix | ||
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 |
---|---|---|
|
@@ -519,3 +519,5 @@ end | |
@test E.vectors*Diagonal(E.values)*E.vectors' ≈ B | ||
end | ||
end | ||
|
||
include("chainrulestests.jl") |
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.