Skip to content

Commit

Permalink
Stabilize doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
oschulz committed Oct 15, 2021
1 parent 69e26e8 commit b744a19
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
16 changes: 11 additions & 5 deletions src/with_ladj.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,29 +30,33 @@ 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, 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, 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]
z == log(foo(x)), ladj_z == ladj_y + with_logabsdet_jacobian(log, y)[2]
# output
true
(true, true)
```
Implementations of with_logabsdet_jacobian can be tested (as a
Expand Down

0 comments on commit b744a19

Please sign in to comment.