Skip to content

Commit 7442802

Browse files
authored
Make Test a weak dependency (#30)
* Make Test a weak dependency * Fix typo * Import `logabsdet`
1 parent 9101502 commit 7442802

File tree

5 files changed

+78
-54
lines changed

5 files changed

+78
-54
lines changed

Project.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChangesOfVariables"
22
uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0"
3-
version = "0.1.8"
3+
version = "0.1.9"
44

55
[deps]
66
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
@@ -9,19 +9,23 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
99

1010
[weakdeps]
1111
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
12+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1213

1314
[extensions]
1415
ChangesOfVariablesInverseFunctionsExt = "InverseFunctions"
16+
ChangesOfVariablesTestExt = "Test"
1517

1618
[compat]
1719
InverseFunctions = "0.1"
1820
LinearAlgebra = "<0.0.1, 1"
21+
Test = "<0.0.1, 1"
1922
julia = "1"
2023

2124
[extras]
2225
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
2326
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
2427
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
28+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2529

2630
[targets]
27-
test = ["Documenter", "InverseFunctions", "ForwardDiff"]
31+
test = ["Documenter", "InverseFunctions", "ForwardDiff", "Test"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ changes for functions that perform a change of variables (like coordinate
1212
transformations).
1313

1414
`ChangesOfVariables` is a very lightweight package and has no dependencies
15-
beyond `Base`, `LinearAlgebra` and `Test` (plus a weak depdendency on
16-
`InverseFunctions`).
15+
beyond `Base` and `LinearAlgebra` (plus a weak dependency on `InverseFunctions`
16+
and `Test`).
1717

1818
## Documentation
1919

ext/ChangesOfVariablesTestExt.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module ChangesOfVariablesTestExt
2+
3+
using Test: @inferred, @test, @testset
4+
using ChangesOfVariables: ChangesOfVariables, logabsdet, with_logabsdet_jacobian
5+
6+
function ChangesOfVariables.test_with_logabsdet_jacobian(f, x, getjacobian; compare = isapprox, kwargs...)
7+
@testset "test_with_logabsdet_jacobian: $f with input $x" begin
8+
ref_y, test_type_inference = try
9+
@inferred(f(x)), true
10+
catch err
11+
f(x), false
12+
end
13+
14+
y, ladj = if test_type_inference
15+
@inferred with_logabsdet_jacobian(f, x)
16+
else
17+
with_logabsdet_jacobian(f, x)
18+
end
19+
20+
ref_ladj = _generalized_logabsdet(getjacobian(f, x))[1]
21+
22+
@test compare(y, ref_y; kwargs...)
23+
@test compare(ladj, ref_ladj; kwargs...)
24+
end
25+
return nothing
26+
end
27+
28+
_generalized_logabsdet(A) = logabsdet(A)
29+
_generalized_logabsdet(x::Real) = log(abs(x))
30+
31+
end # module

src/ChangesOfVariables.jl

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,51 @@ transformations).
1010
module ChangesOfVariables
1111

1212
using LinearAlgebra
13-
using Test
1413

1514
include("with_ladj.jl")
1615
include("setladj.jl")
17-
include("test.jl")
16+
17+
"""
18+
ChangesOfVariables.test_with_logabsdet_jacobian(f, x, getjacobian; compare = isapprox, kwargs...)
19+
20+
Test if [`with_logabsdet_jacobian(f, x)`](@ref) is implemented correctly.
21+
22+
Checks if the result of `with_logabsdet_jacobian(f, x)` is approximately
23+
equal to `(f(x), logabsdet(getjacobian(f, x)))`
24+
25+
So the test uses `getjacobian(f, x)` to calculate a reference Jacobian for
26+
`f` at `x`. Passing `ForwardDiff.jabobian`, `Zygote.jacobian` or similar as
27+
the `getjacobian` function will do fine in most cases. If input and output
28+
of `f` are real scalar values, use `ForwardDiff.derivative`.
29+
30+
Note that the result of `getjacobian(f, x)` must be a real-valued matrix
31+
or a real scalar, so you may need to use a custom `getjacobian` function
32+
that transforms the shape of `x` and `f(x)` internally, in conjunction
33+
with automatic differentiation.
34+
35+
`kwargs...` are forwarded to `compare`.
36+
37+
!!! Note
38+
On Julia >= 1.9, you have to load the `Test` standard library to be able to use
39+
this function.
40+
"""
41+
function test_with_logabsdet_jacobian end
1842

1943
@static if !isdefined(Base, :get_extension)
2044
include("../ext/ChangesOfVariablesInverseFunctionsExt.jl")
45+
include("../ext/ChangesOfVariablesTestExt.jl")
46+
end
47+
48+
# Better error message if users forget to load Test
49+
if isdefined(Base, :get_extension) && isdefined(Base.Experimental, :register_error_hint)
50+
function __init__()
51+
Base.Experimental.register_error_hint(MethodError) do io, exc, _, _
52+
if exc.f === test_with_logabsdet_jacobian &&
53+
(Base.get_extension(ChangesOfVariables, :ChangesOfVariablesTest) === nothing)
54+
print(io, "\nDid you forget to load Test?")
55+
end
56+
end
57+
end
2158
end
2259

2360
end # module

src/test.jl

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)