|
| 1 | +# This file is a part of ChangesOfVariables.jl, licensed under the MIT License (MIT). |
| 2 | + |
| 3 | +using Test |
| 4 | +using ChangesOfVariables |
| 5 | +using InverseFunctions |
| 6 | + |
| 7 | +const ChangesOfVariablesInverseFunctionsExt = if isdefined(Base, :get_extension) |
| 8 | + Base.get_extension(ChangesOfVariables, :ChangesOfVariablesInverseFunctionsExt) |
| 9 | +else |
| 10 | + ChangesOfVariables.ChangesOfVariablesInverseFunctionsExt |
| 11 | +end |
| 12 | +const InverseFunctionWithLADJ = ChangesOfVariablesInverseFunctionsExt.InverseFunctionWithLADJ |
| 13 | + |
| 14 | +include("getjacobian.jl") |
| 15 | + |
| 16 | + |
| 17 | +# Dummy testing type that looks like something that represents abstract zeros: |
| 18 | +struct _Zero{T} end |
| 19 | +_Zero(::T) where {T} = _Zero{T}() |
| 20 | + |
| 21 | + |
| 22 | +@testset "setladj" begin |
| 23 | + @test @inferred(setladj(Real, _Zero)) isa ChangesOfVariables.FunctionWithLADJ{Type{Real},Type{_Zero}} |
| 24 | + @test @inferred(ChangesOfVariables.FunctionWithLADJ(Real, _Zero)) isa ChangesOfVariables.FunctionWithLADJ{Type{Real},Type{_Zero}} |
| 25 | + @test @inferred(ChangesOfVariables.FunctionWithLADJ(widen, _Zero)) isa ChangesOfVariables.FunctionWithLADJ{typeof(widen),Type{_Zero}} |
| 26 | + @test @inferred(ChangesOfVariables.FunctionWithLADJ(Real, zero)) isa ChangesOfVariables.FunctionWithLADJ{Type{Real},typeof(zero)} |
| 27 | + @test @inferred(ChangesOfVariables.FunctionWithLADJ(widen, zero)) isa ChangesOfVariables.FunctionWithLADJ{typeof(widen),typeof(zero)} |
| 28 | + |
| 29 | + @test @inferred(InverseFunctionWithLADJ(Real, _Zero)) isa InverseFunctionWithLADJ{Type{Real},Type{_Zero}} |
| 30 | + @test @inferred(InverseFunctionWithLADJ(widen, _Zero)) isa InverseFunctionWithLADJ{typeof(widen),Type{_Zero}} |
| 31 | + @test @inferred(InverseFunctionWithLADJ(Real, zero)) isa InverseFunctionWithLADJ{Type{Real},typeof(zero)} |
| 32 | + @test @inferred(InverseFunctionWithLADJ(widen, zero)) isa InverseFunctionWithLADJ{typeof(widen),typeof(zero)} |
| 33 | + |
| 34 | + @test @inferred(setladj(setladj(exp, x -> 0), x -> x)) isa ChangesOfVariables.FunctionWithLADJ{typeof(exp)} |
| 35 | + ChangesOfVariables.test_with_logabsdet_jacobian(setladj(setladj(exp, x -> 0), x -> x), 1.7, getjacobian) |
| 36 | + |
| 37 | + x = 4.2 |
| 38 | + y = x^2 |
| 39 | + |
| 40 | + f_fwd = setladj(x -> x^2, x -> log(2*x)) |
| 41 | + f_inv = setladj(y -> sqrt(y), y -> log(inv(2*sqrt(y)))) |
| 42 | + ChangesOfVariables.test_with_logabsdet_jacobian(f_fwd, x, getjacobian) |
| 43 | + ChangesOfVariables.test_with_logabsdet_jacobian(f_inv, y, getjacobian) |
| 44 | + |
| 45 | + f = @inferred setladj(setinverse(x -> x^2, x -> sqrt(x)), x -> log(2*x)) |
| 46 | + @test @inferred(f(x)) == y |
| 47 | + ChangesOfVariables.test_with_logabsdet_jacobian(f, x, getjacobian) |
| 48 | + ChangesOfVariables.test_with_logabsdet_jacobian(inverse(f), y, getjacobian) |
| 49 | + ChangesOfVariables.test_with_logabsdet_jacobian(inverse(inverse(f)), x, getjacobian) |
| 50 | + @inferred(inverse(inverse(f))) isa ChangesOfVariables.FunctionWithLADJ |
| 51 | + |
| 52 | + @static if isdefined(InverseFunctions, :setinverse) |
| 53 | + g = setinverse(f_fwd, f_inv) |
| 54 | + ChangesOfVariables.test_with_logabsdet_jacobian(g, x, getjacobian) |
| 55 | + ChangesOfVariables.test_with_logabsdet_jacobian(inverse(g), y, getjacobian) |
| 56 | + ChangesOfVariables.test_with_logabsdet_jacobian(inverse(inverse(g)), x, getjacobian) |
| 57 | + end |
| 58 | +end |
0 commit comments