Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ inverse_eltype(::TVNeg, ::Type{T}) where T = typeof(-oneunit(T))
inverse(::TVNeg, x::Number) = -x
inverse_and_logjac(::TVNeg, x::Number) = -x, logjac_zero(LogJac(), typeof(x))

"""
$(TYPEDEF)

“Softplus” transformation `x ↦ log(1+exp(x))`.

!!! NOTE
This is *experimental* and not part of the API yet.
"""
struct TVSoftPlus <: ScalarTransform end

transform(::TVSoftPlus, x::Real) = log1pexp(x)
transform_and_logjac(t::TVSoftPlus, x::Real) = transform(t, x), -log1pexp(-x)

inverse_eltype(::TVSoftPlus, ::Type{T}) where T = _ensure_float(T)
inverse(::TVSoftPlus, y::Number) = logexpm1(y)
inverse_and_logjac(::TVSoftPlus, y::Number) = logexpm1(y), -log1mexp(-y)

####
#### composite scalar transforms
####
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,10 @@ end
@test_throws InexactError inverse(t, fill(Complex(0, 1), 3))
end

@testset "TVSoftPlus" begin
test_transformation(TransformVariables.TVSoftPlus(), y -> y > 0)
end

####
#### static analysis with JET
####
Expand Down
Loading