Skip to content

Commit c30bc6f

Browse files
committed
Add NoLogAbsDetJacobian
1 parent 4ecf0a9 commit c30bc6f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
```@docs
66
with_logabsdet_jacobian
7+
NoLogAbsDetJacobian
78
```
89

910
## Test utility

src/with_ladj.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ For `(y, ladj) = with_logabsdet_jacobian(f, x)`, the following must hold true:
1414
`with_logabsdet_jacobian` comes with support for broadcasted/mapped functions
1515
(via `Base.Fix1`) and (Julia >=v1.6 only) `ComposedFunction`.
1616
17+
If no volume element is defined/applicable, `with_logabsdet_jacobian(f, x)` returns
18+
[`NoLogAbsDetJacobian(f)`](@ref).
19+
1720
# Examples
1821
1922
```jldoctest a
@@ -67,6 +70,32 @@ Implementations of with_logabsdet_jacobian can be tested (as a
6770
function with_logabsdet_jacobian end
6871
export with_logabsdet_jacobian
6972

73+
74+
75+
"""
76+
struct NoLogAbsDetJacobian{F,T}
77+
78+
An instance `NoLogAbsDetJacobian{F,T}(f)` signifies that
79+
`with_logabsdet_jacobian(f, ::T)` is not defined.
80+
"""
81+
struct NoLogAbsDetJacobian{F,T}
82+
f::F
83+
end
84+
export NoLogAbsDetJacobian
85+
86+
function _no_ladj_errmsg(@nospecialize noladj::NoLogAbsDetJacobian{F,T}) where {F,T}
87+
("with_logabsdet_jacobian(", noladj.f, ", ::", T, " is not defined ")
88+
end
89+
90+
Base.getindex(@nospecialize(noladj::NoLogAbsDetJacobian), args...) = error(_no_ladj_errmsg(noladj)...)
91+
Base.iterate(@nospecialize(noladj::NoLogAbsDetJacobian)) = error(_no_ladj_errmsg(noladj)...)
92+
Base.firstindex(@nospecialize(noladj::NoLogAbsDetJacobian)) = error(_no_ladj_errmsg(noladj)...)
93+
Base.lastindex(@nospecialize(noladj::NoLogAbsDetJacobian)) = error(_no_ladj_errmsg(noladj)...)
94+
95+
with_logabsdet_jacobian(f::F, ::T) where {F,T} = NoLogAbsDetJacobian{F,T}(f, )
96+
97+
98+
7099
@static if VERSION >= v"1.6"
71100
function with_logabsdet_jacobian(f::Base.ComposedFunction, x)
72101
y_inner, ladj_inner = with_logabsdet_jacobian(f.inner, x)

test/test_with_ladj.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ include("getjacobian.jl")
1313

1414

1515
@testset "with_logabsdet_jacobian" begin
16+
@test with_logabsdet_jacobian(sum, rand(5)) == NoLogAbsDetJacobian{typeof(sum),Vector{Float64}}(sum)
17+
noladj = with_logabsdet_jacobian(sum, rand(5))
18+
@test_throws ErrorException noladj[1]
19+
@test_throws ErrorException noladj[2]
20+
@test_throws ErrorException firstindex(noladj)
21+
@test_throws ErrorException lastindex(noladj)
22+
@test_throws ErrorException first(noladj)
23+
@test_throws ErrorException last(noladj)
24+
@test_throws ErrorException _, _ = noladj
25+
1626
function ChangesOfVariables.with_logabsdet_jacobian(::typeof(foo), x)
1727
y = foo(x)
1828
ladj = -x + 2 * log(y)

0 commit comments

Comments
 (0)