From c3203bf1c2a2a469f21c38d7b300b775c07ae86a Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Sat, 23 Apr 2022 19:44:41 -0400 Subject: [PATCH] test from discourse, random seed --- test/GradientTest.jl | 8 +++++++- test/JacobianTest.jl | 4 ++++ test/runtests.jl | 6 +++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/test/GradientTest.jl b/test/GradientTest.jl index f5d9db82..13a7a354 100644 --- a/test/GradientTest.jl +++ b/test/GradientTest.jl @@ -175,8 +175,8 @@ end @test ForwardDiff.gradient(f, [0.2,25.0]) == [7875.0, 0.0] end -# Issue 197 @testset "det with branches" begin + # Issue 197 det2(A) = return ( A[1,1]*(A[2,2]*A[3,3]-A[2,3]*A[3,2]) - A[1,2]*(A[2,1]*A[3,3]-A[2,3]*A[3,1]) + @@ -193,6 +193,12 @@ end # And issue 407 @test ForwardDiff.hessian(det, A) ≈ ForwardDiff.hessian(det2, A) + + # https://discourse.julialang.org/t/forwarddiff-and-zygote-return-wrong-jacobian-for-log-det-l/77961 + S = [1.0 0.8; 0.8 1.0] + L = cholesky(S).L + @test ForwardDiff.gradient(L -> log(det(L)), Matrix(L)) ≈ [1.0 -1.3333333333333337; 0.0 1.666666666666667] + @test ForwardDiff.gradient(L -> logdet(L), Matrix(L)) ≈ [1.0 -1.3333333333333337; 0.0 1.666666666666667] end @testset "branches in mul!" begin diff --git a/test/JacobianTest.jl b/test/JacobianTest.jl index 9ea3b0a7..d06f46dc 100644 --- a/test/JacobianTest.jl +++ b/test/JacobianTest.jl @@ -226,6 +226,10 @@ for T in (StaticArrays.SArray, StaticArrays.MArray) @test DiffResults.jacobian(sresult3) == DiffResults.jacobian(result) end +######### +# misc. # +######### + @testset "dimension errors for jacobian" begin @test_throws DimensionMismatch ForwardDiff.jacobian(identity, 2pi) # input @test_throws DimensionMismatch ForwardDiff.jacobian(sum, fill(2pi, 2)) # vector_mode_jacobian diff --git a/test/runtests.jl b/test/runtests.jl index d00acfb2..e97b40ee 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,8 @@ -using ForwardDiff, Test +using ForwardDiff, Test, Random + +SEED = trunc(Int, time()) +println("Testing with Random.seed!($SEED)") +Random.seed!(SEED) @testset "ForwardDiff.jl" begin t0 = time()