From 8ede09e7f7741080aee122f2d412e8f8a8878df3 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 13:39:49 +0300 Subject: [PATCH 01/19] Initial commit with AD --- src/SkewLinearAlgebra.jl | 1 + src/chainrules.jl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/chainrules.jl diff --git a/src/SkewLinearAlgebra.jl b/src/SkewLinearAlgebra.jl index 48d8d65..e3e7953 100644 --- a/src/SkewLinearAlgebra.jl +++ b/src/SkewLinearAlgebra.jl @@ -34,6 +34,7 @@ include("eigen.jl") include("exp.jl") include("cholesky.jl") include("pfaffian.jl") +include("chainrules.jl") end diff --git a/src/chainrules.jl b/src/chainrules.jl new file mode 100644 index 0000000..6991ac0 --- /dev/null +++ b/src/chainrules.jl @@ -0,0 +1,33 @@ +using ChainRulesCore +using ChainRules +using ChainRulesTestUtils + +function ChainRulesCore.rrule(::Type{SkewHermitian}, val) # constructor rrule + y = SkewHermitian(val) + function Foo_pb(ΔFoo) + return (NoTangent(), unthunk(ΔFoo).data) + end + return y, Foo_pb +end + +function ChainRules.rrule(::typeof(pfaffian), x::Union{SkewHermitian}) #pfaffian rrule + Ω = pfaffian(x) + function pullback(ΔΩ) + ∂x = #=0.5 *=# inv(x)' * dot(Ω, ΔΩ) #we removed the 0.5 because changing element ij immediatelyu changes element ji hence this gives a factor 2. + return (NoTangent(), SkewHermitian(∂x)) + end + return Ω, pullback +end + +#perhaps should add more options here ? Defenitely we will need + , - , * . At this point I'm happy though If I can just take the derivative of Pfaffian and it's constructor. All other operators I can do with dense matrices for now... + +#we make a small test for this +A = skewhermitian(rand(2,2)) +B = skewhermitian(rand(2,2)) + +test_rrule(pfaffian, A) #this one doesn't work since it seems to be getting non skew symmetric matrices at some point + +test_rrule(pfaffian, A⊢B) #SO I wanted to specify the tangent but the same happens, judging from our short conversation this might be because I'm ill defining the tangent B ? + +test_rrule(SkewHermitian, A.data ⊢ B.data)#constructor false because ΔFoo supposedly has no field data. But ΔFoo should be a SkewHermitian object, so it should have a field data. + From f825ee2ff71a10c81da70ee1cf56a5dd6f867767 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 14:06:11 +0300 Subject: [PATCH 02/19] updated toml file to also include autodiff optionality --- Project.toml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index d29563c..38bce76 100644 --- a/Project.toml +++ b/Project.toml @@ -1,19 +1,22 @@ name = "SkewLinearAlgebra" uuid = "5c889d49-8c60-4500-9d10-5d3a22e2f4b9" authors = ["smataigne and contributors"] -version = "1.0" +version = "1.0.0" [deps] +ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2" +ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" +ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" [compat] -julia = "1.6" LinearAlgebra = "1.6" +julia = "1.6" [extras] -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test","Random", "SparseArrays"] +test = ["Test", "Random", "SparseArrays"] From 8bc8dacb8ad50588dbd7f9b73909332a1c226953 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 14:23:05 +0300 Subject: [PATCH 03/19] Moved tests to test --- src/chainrules.jl | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/chainrules.jl b/src/chainrules.jl index 6991ac0..4c902b1 100644 --- a/src/chainrules.jl +++ b/src/chainrules.jl @@ -19,15 +19,4 @@ function ChainRules.rrule(::typeof(pfaffian), x::Union{SkewHermitian}) #pfaffia return Ω, pullback end -#perhaps should add more options here ? Defenitely we will need + , - , * . At this point I'm happy though If I can just take the derivative of Pfaffian and it's constructor. All other operators I can do with dense matrices for now... - -#we make a small test for this -A = skewhermitian(rand(2,2)) -B = skewhermitian(rand(2,2)) - -test_rrule(pfaffian, A) #this one doesn't work since it seems to be getting non skew symmetric matrices at some point - -test_rrule(pfaffian, A⊢B) #SO I wanted to specify the tangent but the same happens, judging from our short conversation this might be because I'm ill defining the tangent B ? - -test_rrule(SkewHermitian, A.data ⊢ B.data)#constructor false because ΔFoo supposedly has no field data. But ΔFoo should be a SkewHermitian object, so it should have a field data. - +#perhaps should add more options here ? Defenitely we will need + , - , * . At this point I'm happy though If I can just take the derivative of Pfaffian and it's constructor. All other operators I can do with dense matrices for now... \ No newline at end of file From 8d677ee2ac146239f313a6638076a1478e1b6297 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 14:24:01 +0300 Subject: [PATCH 04/19] Moved tests to tests --- test/runtests.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index fa97c7b..2a7e463 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -519,3 +519,16 @@ end @test E.vectors*Diagonal(E.values)*E.vectors' ≈ B end end + + +@testset "chainrules.jl" begin + #we make a small test for this + A = skewhermitian(rand(2,2)) + B = skewhermitian(rand(2,2)) + + test_rrule(pfaffian, A) #this one doesn't work since it seems to be getting non skew symmetric matrices at some point + + test_rrule(pfaffian, A⊢B) #SO I wanted to specify the tangent but the same happens, judging from our short conversation this might be because I'm ill defining the tangent B ? + + test_rrule(SkewHermitian, A.data ⊢ B.data)#constructor false because ΔFoo supposedly has no field data. But ΔFoo should be a SkewHermitian object, so it should have a field data. +end \ No newline at end of file From 686bde8105a175c7d04f40f1c0ec297cf6a0bbdf Mon Sep 17 00:00:00 2001 From: lkdvos Date: Wed, 25 Sep 2024 14:40:42 +0200 Subject: [PATCH 05/19] add extension --- ext/SkewLinearAlgebraChainRulesCoreExt.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 ext/SkewLinearAlgebraChainRulesCoreExt.jl diff --git a/ext/SkewLinearAlgebraChainRulesCoreExt.jl b/ext/SkewLinearAlgebraChainRulesCoreExt.jl new file mode 100644 index 0000000..e4db226 --- /dev/null +++ b/ext/SkewLinearAlgebraChainRulesCoreExt.jl @@ -0,0 +1,13 @@ +module SkewLinearAlgebraChainRulesCoreExt + +function ChainRulesCore.rrule(::typeof(pfaffian), A::SkewHermitian) + Ω = pfaffian(A) + pfaffian_pullback(ΔΩ) = NoTangent(), SkewHermitian(rmul!(inv(A)', dot(Ω, ΔΩ))) + return Ω, pfaffian_pullback +end + +function ChainRulesCore.ProjectTo{<:SkewHermitian}(x::AbstractArray) + return skewhermitian(x) +end + +end From 3c5e6b2b5007b56a0d7ef0bed6ea02b44c178cbc Mon Sep 17 00:00:00 2001 From: lkdvos Date: Wed, 25 Sep 2024 14:44:37 +0200 Subject: [PATCH 06/19] Add tests --- test/chainrulestests.jl | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/chainrulestests.jl diff --git a/test/chainrulestests.jl b/test/chainrulestests.jl new file mode 100644 index 0000000..5e62798 --- /dev/null +++ b/test/chainrulestests.jl @@ -0,0 +1,36 @@ +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(x::SkewHermitian) + m = size(x, 1) + v = Vector{eltype(x)}(undef, m * (m - 1) ÷ 2) + k = 1 + for i in 2:m, j in 1:i-1 + @inbounds v[k] = x[i, j] + k += 1 + end + + function from_vec(v) + y = zero(x) + k = 1 + for i in 2:m, j in 1:i-1 + @inbounds y[i, j] = v[k] + @inbounds y[j, i] = -v[k] + k += 1 + end + return y + end + return v, from_vec +end + +m = 10 +A = skewhermitian(rand(m, m)) + +test_rrule(pfaffian, A) + From 3396118d5c54870b836273f84ef951e645aa00e7 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 16:10:34 +0300 Subject: [PATCH 07/19] moved chainrules to extensiony --- src/chainrules.jl | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 src/chainrules.jl diff --git a/src/chainrules.jl b/src/chainrules.jl deleted file mode 100644 index 4c902b1..0000000 --- a/src/chainrules.jl +++ /dev/null @@ -1,22 +0,0 @@ -using ChainRulesCore -using ChainRules -using ChainRulesTestUtils - -function ChainRulesCore.rrule(::Type{SkewHermitian}, val) # constructor rrule - y = SkewHermitian(val) - function Foo_pb(ΔFoo) - return (NoTangent(), unthunk(ΔFoo).data) - end - return y, Foo_pb -end - -function ChainRules.rrule(::typeof(pfaffian), x::Union{SkewHermitian}) #pfaffian rrule - Ω = pfaffian(x) - function pullback(ΔΩ) - ∂x = #=0.5 *=# inv(x)' * dot(Ω, ΔΩ) #we removed the 0.5 because changing element ij immediatelyu changes element ji hence this gives a factor 2. - return (NoTangent(), SkewHermitian(∂x)) - end - return Ω, pullback -end - -#perhaps should add more options here ? Defenitely we will need + , - , * . At this point I'm happy though If I can just take the derivative of Pfaffian and it's constructor. All other operators I can do with dense matrices for now... \ No newline at end of file From 6d227f57cf2905b605b892df7e005a77b0ef3b8e Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 16:11:27 +0300 Subject: [PATCH 08/19] updated toml and introduced module --- Project.toml | 12 ++++++++++-- ext/SkewLinearAlgebraChainRulesCoreExt.jl | 10 +++++++++- src/SkewLinearAlgebra.jl | 1 - 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 38bce76..8acb60e 100644 --- a/Project.toml +++ b/Project.toml @@ -4,10 +4,16 @@ authors = ["smataigne and contributors"] version = "1.0.0" [deps] -ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + + +[weakdeps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" +ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" -LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[extensions] +SkewLinearAlgebraChainRulesCoreExt = "ChainRulesCore" [compat] LinearAlgebra = "1.6" @@ -20,3 +26,5 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] test = ["Test", "Random", "SparseArrays"] + + diff --git a/ext/SkewLinearAlgebraChainRulesCoreExt.jl b/ext/SkewLinearAlgebraChainRulesCoreExt.jl index e4db226..817de1f 100644 --- a/ext/SkewLinearAlgebraChainRulesCoreExt.jl +++ b/ext/SkewLinearAlgebraChainRulesCoreExt.jl @@ -1,8 +1,16 @@ module SkewLinearAlgebraChainRulesCoreExt +function ChainRulesCore.rrule(::Type{SkewHermitian}, val) + y = SkewHermitian(val) + function Foo_pb(ΔFoo) + return (NoTangent(), unthunk(ΔFoo).data) + end + return y, Foo_pb +end + function ChainRulesCore.rrule(::typeof(pfaffian), A::SkewHermitian) Ω = pfaffian(A) - pfaffian_pullback(ΔΩ) = NoTangent(), SkewHermitian(rmul!(inv(A)', dot(Ω, ΔΩ))) + pfaffian_pullback(ΔΩ) = NoTangent(), SkewHermitian(rmul!(inv(A)', dot(Ω, ΔΩ))) #potentially need the 0.5 here ! return Ω, pfaffian_pullback end diff --git a/src/SkewLinearAlgebra.jl b/src/SkewLinearAlgebra.jl index e3e7953..48d8d65 100644 --- a/src/SkewLinearAlgebra.jl +++ b/src/SkewLinearAlgebra.jl @@ -34,7 +34,6 @@ include("eigen.jl") include("exp.jl") include("cholesky.jl") include("pfaffian.jl") -include("chainrules.jl") end From 97ae6d69d2580e0dc1a53b113bd2879a3a52464f Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 16:20:14 +0300 Subject: [PATCH 09/19] added deps --- ext/SkewLinearAlgebraChainRulesCoreExt.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ext/SkewLinearAlgebraChainRulesCoreExt.jl b/ext/SkewLinearAlgebraChainRulesCoreExt.jl index 817de1f..53141d2 100644 --- a/ext/SkewLinearAlgebraChainRulesCoreExt.jl +++ b/ext/SkewLinearAlgebraChainRulesCoreExt.jl @@ -1,5 +1,9 @@ module SkewLinearAlgebraChainRulesCoreExt +using SkewLinearAlgebra +using ChainRulesCore +using ChainRules + function ChainRulesCore.rrule(::Type{SkewHermitian}, val) y = SkewHermitian(val) function Foo_pb(ΔFoo) From 70469b9f0a7fd3ea2fc880fc02178d5845f4afe3 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 17:45:18 +0300 Subject: [PATCH 10/19] added missing dependency to LinearAlgebra --- ext/SkewLinearAlgebraChainRulesCoreExt.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/SkewLinearAlgebraChainRulesCoreExt.jl b/ext/SkewLinearAlgebraChainRulesCoreExt.jl index 53141d2..87dacd7 100644 --- a/ext/SkewLinearAlgebraChainRulesCoreExt.jl +++ b/ext/SkewLinearAlgebraChainRulesCoreExt.jl @@ -1,8 +1,9 @@ module SkewLinearAlgebraChainRulesCoreExt +using LinearAlgebra using SkewLinearAlgebra using ChainRulesCore -using ChainRules + function ChainRulesCore.rrule(::Type{SkewHermitian}, val) y = SkewHermitian(val) From f0c1da412060f6eaf0a226b4f7dd7673a4295bd6 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 17:45:32 +0300 Subject: [PATCH 11/19] fixed Toml file. --- Project.toml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 8acb60e..8055167 100644 --- a/Project.toml +++ b/Project.toml @@ -6,25 +6,25 @@ version = "1.0.0" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" - [weakdeps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" -ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2" -ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" [extensions] SkewLinearAlgebraChainRulesCoreExt = "ChainRulesCore" [compat] 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" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +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"] From 45700a9f49cedf0d716be344a7343488707e60d5 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 18:04:44 +0300 Subject: [PATCH 12/19] removed my old, faulty test from the testset. --- test/runtests.jl | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 2a7e463..97d4286 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -518,17 +518,4 @@ end E = eigen(B) @test E.vectors*Diagonal(E.values)*E.vectors' ≈ B end -end - - -@testset "chainrules.jl" begin - #we make a small test for this - A = skewhermitian(rand(2,2)) - B = skewhermitian(rand(2,2)) - - test_rrule(pfaffian, A) #this one doesn't work since it seems to be getting non skew symmetric matrices at some point - - test_rrule(pfaffian, A⊢B) #SO I wanted to specify the tangent but the same happens, judging from our short conversation this might be because I'm ill defining the tangent B ? - - test_rrule(SkewHermitian, A.data ⊢ B.data)#constructor false because ΔFoo supposedly has no field data. But ΔFoo should be a SkewHermitian object, so it should have a field data. end \ No newline at end of file From 17b1c448de51efbe2faf1931188ccc63f96dbb85 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 18:13:32 +0300 Subject: [PATCH 13/19] retrigger checks From 7b182161463815555b62553afd4931fd709da870 Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 18:52:23 +0300 Subject: [PATCH 14/19] further tweaked tests --- test/chainrulestests.jl | 11 ++++++++--- test/runtests.jl | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/test/chainrulestests.jl b/test/chainrulestests.jl index 5e62798..bbb649c 100644 --- a/test/chainrulestests.jl +++ b/test/chainrulestests.jl @@ -29,8 +29,13 @@ function FiniteDifferences.to_vec(x::SkewHermitian) return v, from_vec end -m = 10 -A = skewhermitian(rand(m, m)) +@testset "automatic differentiation" begin + m = 10 + inds = [1,2] + A = skewhermitian(rand(m, m)) -test_rrule(pfaffian, A) + test_rrule(SkewHermitian, A )#test constructor + test_rrule(pfaffian, A)#test pfaffian + test_rrule(pfaffian, SkewHermitian(A[inds, inds]))#test pfaffian of submatrix +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 97d4286..f2e78c1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -518,4 +518,6 @@ end E = eigen(B) @test E.vectors*Diagonal(E.values)*E.vectors' ≈ B end -end \ No newline at end of file +end + +include("chainrulestests.jl") \ No newline at end of file From f8a9fd83ba2cbf41c01dee50fbcd45e15a30b18c Mon Sep 17 00:00:00 2001 From: Gertian Date: Wed, 25 Sep 2024 20:32:48 +0300 Subject: [PATCH 15/19] patched constructor AD so that it can take both matrix and AS-matrix --- ext/SkewLinearAlgebraChainRulesCoreExt.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/SkewLinearAlgebraChainRulesCoreExt.jl b/ext/SkewLinearAlgebraChainRulesCoreExt.jl index 87dacd7..b5f71ef 100644 --- a/ext/SkewLinearAlgebraChainRulesCoreExt.jl +++ b/ext/SkewLinearAlgebraChainRulesCoreExt.jl @@ -8,7 +8,11 @@ using ChainRulesCore function ChainRulesCore.rrule(::Type{SkewHermitian}, val) y = SkewHermitian(val) function Foo_pb(ΔFoo) - return (NoTangent(), unthunk(ΔFoo).data) + if isa(ΔFoo, SkewHermitian) + return NoTangent(), unthunk(ΔFoo).data + else + return (NoTangent(), unthunk(ΔFoo)) + end end return y, Foo_pb end From bfbdfa6082e6ddd90ae1ca80e065fc25bce7e24b Mon Sep 17 00:00:00 2001 From: Gertian Date: Thu, 26 Sep 2024 08:27:43 +0300 Subject: [PATCH 16/19] Update ext/SkewLinearAlgebraChainRulesCoreExt.jl Co-authored-by: Steven G. Johnson --- ext/SkewLinearAlgebraChainRulesCoreExt.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/SkewLinearAlgebraChainRulesCoreExt.jl b/ext/SkewLinearAlgebraChainRulesCoreExt.jl index b5f71ef..0445ace 100644 --- a/ext/SkewLinearAlgebraChainRulesCoreExt.jl +++ b/ext/SkewLinearAlgebraChainRulesCoreExt.jl @@ -23,8 +23,8 @@ function ChainRulesCore.rrule(::typeof(pfaffian), A::SkewHermitian) return Ω, pfaffian_pullback end -function ChainRulesCore.ProjectTo{<:SkewHermitian}(x::AbstractArray) - return skewhermitian(x) +function ChainRulesCore.ProjectTo{<:SkewHermitian}(A::AbstractArray) + return skewhermitian(A) end end From 207148682a2f35e6b3a2fc95f67a4bf8a8ace030 Mon Sep 17 00:00:00 2001 From: Gertian Date: Thu, 26 Sep 2024 08:29:15 +0300 Subject: [PATCH 17/19] Update test/chainrulestests.jl Co-authored-by: Steven G. Johnson --- test/chainrulestests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/chainrulestests.jl b/test/chainrulestests.jl index bbb649c..62c34d7 100644 --- a/test/chainrulestests.jl +++ b/test/chainrulestests.jl @@ -34,7 +34,7 @@ end inds = [1,2] A = skewhermitian(rand(m, m)) - test_rrule(SkewHermitian, A )#test constructor + test_rrule(SkewHermitian, A) # test constructor test_rrule(pfaffian, A)#test pfaffian test_rrule(pfaffian, SkewHermitian(A[inds, inds]))#test pfaffian of submatrix From 5a44cda5cb42375306f7010445f04857a91f592e Mon Sep 17 00:00:00 2001 From: Gertian Date: Thu, 26 Sep 2024 08:29:32 +0300 Subject: [PATCH 18/19] Update test/chainrulestests.jl Co-authored-by: Steven G. Johnson --- test/chainrulestests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/chainrulestests.jl b/test/chainrulestests.jl index 62c34d7..cafb40f 100644 --- a/test/chainrulestests.jl +++ b/test/chainrulestests.jl @@ -36,6 +36,6 @@ end test_rrule(SkewHermitian, A) # test constructor - test_rrule(pfaffian, A)#test pfaffian - test_rrule(pfaffian, SkewHermitian(A[inds, inds]))#test pfaffian of submatrix + test_rrule(pfaffian, A) # test pfaffian + test_rrule(pfaffian, SkewHermitian(A[inds, inds])) # test pfaffian of submatrix end \ No newline at end of file From 5fd77931f6b96b67d3ddc322d30b9a3390bad1b8 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Mon, 29 Sep 2025 10:00:57 +0800 Subject: [PATCH 19/19] Update test/chainrulestests.jl --- test/chainrulestests.jl | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/test/chainrulestests.jl b/test/chainrulestests.jl index cafb40f..b0dd544 100644 --- a/test/chainrulestests.jl +++ b/test/chainrulestests.jl @@ -7,24 +7,17 @@ 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(x::SkewHermitian) - m = size(x, 1) - v = Vector{eltype(x)}(undef, m * (m - 1) ÷ 2) - k = 1 - for i in 2:m, j in 1:i-1 - @inbounds v[k] = x[i, j] - k += 1 - end - +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) - y = zero(x) + B = zero(A) k = 1 for i in 2:m, j in 1:i-1 - @inbounds y[i, j] = v[k] - @inbounds y[j, i] = -v[k] + @inbounds B[i,j] = v[k] k += 1 end - return y + return B end return v, from_vec end