diff --git a/docs/Project.toml b/docs/Project.toml index f471c80..f656746 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,5 +1,7 @@ [deps] Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" +ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" [compat] diff --git a/src/with_ladj.jl b/src/with_ladj.jl index 1a7259d..7f5dc9a 100644 --- a/src/with_ladj.jl +++ b/src/with_ladj.jl @@ -17,6 +17,8 @@ For `(y, ladj) = with_logabsdet_jacobian(f, x)`, the following must hold true: # Examples ```jldoctest a +using ChangesOfVariables + foo(x) = inv(exp(-x) + 1) function ChangesOfVariables.with_logabsdet_jacobian(::typeof(foo), x) @@ -28,25 +30,30 @@ end x = 4.2 y, ladj_y = with_logabsdet_jacobian(foo, x) +using LinearAlgebra, ForwardDiff +y == foo(x) && ladj_y ≈ log(abs(ForwardDiff.derivative(foo, x))) + # output -(0.9852259683067269, -4.229768509343836) +true ``` ```jldoctest a -X = [3, 7, 5] +X = rand(10) broadcasted_foo = Base.Fix1(broadcast, foo) Y, ladj_Y = with_logabsdet_jacobian(broadcasted_foo, X) +Y == broadcasted_foo(X) && ladj_Y ≈ logabsdet(ForwardDiff.jacobian(broadcasted_foo, X))[1] # output -([0.9525741268224334, 0.9990889488055994, 0.9933071490757153], -15.112428333033268) +true ``` ```jldoctest a -# Requires Julia >= v1.6: -z, ladj_z = with_logabsdet_jacobian(log ∘ foo, x) -z == log(foo(x)) && ladj_z == ladj_y + with_logabsdet_jacobian(log, y)[2] +VERSION < v"1.6" || begin # Support for ∘ requires Julia >= v1.6 + z, ladj_z = with_logabsdet_jacobian(log ∘ foo, x) + z == log(foo(x)) && ladj_z == ladj_y + with_logabsdet_jacobian(log, y)[2] +end # output