@@ -4,66 +4,58 @@ using ChangesOfVariables
4
4
using Test
5
5
6
6
using LinearAlgebra
7
- using ForwardDiff: derivative, jacobian
7
+ import ForwardDiff
8
8
9
+ using ChangesOfVariables: test_with_logabsdet_jacobian
9
10
10
- fwddiff_ladj (f, x:: Real ) = log (abs (derivative (f, x)))
11
- fwddiff_ladj (f, x:: AbstractArray{<:Real} ) = logabsdet (jacobian (f, x))[1 ]
12
- fwddiff_with_ladj (f, x) = (f (x), fwddiff_ladj (f, x))
13
11
14
- ascomplex (A:: AbstractArray{T} ) where T = reinterpret (Complex{T}, A)
15
- asreal (A:: AbstractArray{Complex{T}} ) where T = reinterpret (T, A)
16
-
17
- isaprx (a, b) = isapprox (a,b)
18
- isaprx (a:: NTuple{N,Any} , b:: NTuple{N,Any} ) where N = all (map (isaprx, a, b))
19
-
20
-
21
- foo (x) = inv (exp (- x) + 1 )
12
+ @testset " with_logabsdet_jacobian" begin
13
+ foo (x) = inv (exp (- x) + 1 )
22
14
23
- function ChangesOfVariables. with_logabsdet_jacobian (:: typeof (foo), x)
24
- y = foo (x)
25
- ladj = - x + 2 * log (y)
26
- (y, ladj)
27
- end
15
+ function ChangesOfVariables. with_logabsdet_jacobian (:: typeof (foo), x)
16
+ y = foo (x)
17
+ ladj = - x + 2 * log (y)
18
+ (y, ladj)
19
+ end
28
20
29
21
30
- @testset " with_logabsdet_jacobian" begin
31
22
x = 4.2
32
23
X = rand (10 )
33
24
A = rand (5 , 5 )
34
- CA = rand (10 , 5 )
25
+ CA = Complex .( rand (5 , 5 ), rand ( 5 , 5 ) )
35
26
36
- @test isaprx (@inferred (with_logabsdet_jacobian (foo, x)), fwddiff_with_ladj (foo, x))
27
+
28
+ getjacobian = ForwardDiff. jacobian
29
+
30
+ isaprx (a, b; kwargs... ) = isapprox (a,b; kwargs... )
31
+ isaprx (a:: NTuple{N,Any} , b:: NTuple{N,Any} ; kwargs... ) where N = all (map ((a,b) -> isaprx (a, b; kwargs... ), a, b))
32
+
33
+
34
+ test_with_logabsdet_jacobian (foo, x, getjacobian)
37
35
38
36
@static if VERSION >= v " 1.6"
39
- log_foo = log ∘ foo
40
- @test isaprx (@inferred (with_logabsdet_jacobian (log_foo, x)), fwddiff_with_ladj (log_foo, x))
37
+ test_with_logabsdet_jacobian (log ∘ foo, x, getjacobian)
38
+ end
39
+
40
+ @testset " getjacobian on mapped and broadcasted" begin
41
+ for f in (Base. Fix1 (map, foo), Base. Fix1 (broadcast, foo))
42
+ for arg in (x, fill (x,), Ref (x), (x,), X)
43
+ test_with_logabsdet_jacobian (f, arg, getjacobian, compare = isaprx)
44
+ end
45
+ end
41
46
end
42
47
43
- mapped_foo = Base. Fix1 (map, foo)
44
- @test isaprx (@inferred (with_logabsdet_jacobian (mapped_foo, x)), fwddiff_with_ladj (mapped_foo, x))
45
- @test isaprx (@inferred (with_logabsdet_jacobian (mapped_foo, fill (x))), fwddiff_with_ladj (mapped_foo, fill (x)))
46
- @test isaprx (@inferred (with_logabsdet_jacobian (mapped_foo, Ref (x))), fwddiff_with_ladj (mapped_foo, fill (x)))
47
- @test isaprx (@inferred (with_logabsdet_jacobian (mapped_foo, (x,))), (mapped_foo ((x,)), fwddiff_ladj (mapped_foo, x)))
48
- @test isaprx (@inferred (with_logabsdet_jacobian (mapped_foo, X)), fwddiff_with_ladj (mapped_foo, X))
49
-
50
- broadcasted_foo = Base. Fix1 (broadcast, foo)
51
- @test isaprx (@inferred (with_logabsdet_jacobian (broadcasted_foo, x)), fwddiff_with_ladj (broadcasted_foo, x))
52
- @test isaprx (@inferred (with_logabsdet_jacobian (broadcasted_foo, fill (x))), fwddiff_with_ladj (broadcasted_foo, x))
53
- @test isaprx (@inferred (with_logabsdet_jacobian (broadcasted_foo, Ref (x))), fwddiff_with_ladj (broadcasted_foo, x))
54
- @test isaprx (@inferred (with_logabsdet_jacobian (broadcasted_foo, (x,))), (mapped_foo ((x,)), fwddiff_ladj (mapped_foo, x)))
55
- @test isaprx (@inferred (with_logabsdet_jacobian (broadcasted_foo, X)), fwddiff_with_ladj (broadcasted_foo, X))
56
-
57
- for f in (identity, adjoint, transpose)
58
- @test isaprx (@inferred (with_logabsdet_jacobian (f, x)), fwddiff_with_ladj (f, x))
59
- @test isaprx (@inferred (with_logabsdet_jacobian (f, A)), fwddiff_with_ladj (f, A))
48
+ @testset " getjacobian on identity, adjoint and transpose" begin
49
+ for f in (identity, adjoint, transpose)
50
+ for arg in (x, A)
51
+ test_with_logabsdet_jacobian (f, arg, getjacobian)
52
+ end
53
+ end
60
54
end
61
-
62
- @test isaprx (@inferred (with_logabsdet_jacobian (inv, x)), fwddiff_with_ladj (inv, x))
63
- @test isaprx (@inferred (with_logabsdet_jacobian (inv, A)), fwddiff_with_ladj (inv, A))
64
- @test isaprx (@inferred (with_logabsdet_jacobian (inv, ascomplex (CA))), (inv (ascomplex (CA)), fwddiff_ladj (CA -> asreal (inv (ascomplex (CA))), CA)))
65
55
66
- for f in (exp, log, exp2, log2, exp10, log10, expm1, log1p)
67
- @test isaprx (@inferred (with_logabsdet_jacobian (f, x)), fwddiff_with_ladj (f, x))
56
+ @testset " getjacobian on inv" begin
57
+ for arg in (x, A, CA)
58
+ test_with_logabsdet_jacobian (inv, arg, getjacobian)
59
+ end
68
60
end
69
61
end
0 commit comments