Skip to content

Commit

Permalink
xexpx, xexpy
Browse files Browse the repository at this point in the history
  • Loading branch information
cossio committed Mar 6, 2022
1 parent c8a4c28 commit a36c63e
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LogExpFunctions"
uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
authors = ["StatsFun.jl contributors, Tamas K. Papp <[email protected]>"]
version = "0.3.6"
version = "0.3.7"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 2 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ LogExpFunctions supports [`InverseFunctions.inverse`](https://github.com/JuliaMa
xlogx
xlogy
xlog1py
xexpx
xexpy
logistic
logit
logcosh
Expand Down
2 changes: 1 addition & 1 deletion src/LogExpFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import InverseFunctions
import IrrationalConstants
import LinearAlgebra

export xlogx, xlogy, xlog1py, logistic, logit, log1psq, log1pexp, log1mexp, log2mexp, logexpm1,
export xlogx, xlogy, xlog1py, xexpx, xexpy, logistic, logit, log1psq, log1pexp, log1mexp, log2mexp, logexpm1,
softplus, invsoftplus, log1pmx, logmxp1, logaddexp, logsubexp, logsumexp, softmax,
softmax!, logcosh

Expand Down
30 changes: 30 additions & 0 deletions src/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ function xlog1py(x::Number, y::Number)
return iszero(x) && !isnan(y) ? zero(result) : result
end

"""
$(SIGNATURES)
Return `x * exp(x)` for `x > -Inf`, or zero if `x == -Inf`.
```jldoctest
julia> xexpx(-Inf)
0.0
```
"""
function xexpx(x::Real)
expx = exp(x)
return iszero(expx) ? expx : x * expx
end

"""
$(SIGNATURES)
Return `x * exp(y)` for `y > -Inf`, or zero if `y == -Inf`.
```jldoctest
julia> xexpy(1.0, -Inf)
0.0
```
"""
function xexpy(x::Real, y::Real)
expy = exp(y)
return iszero(expy) && !isnan(x) ? zero(x * expy) : x * expy
end

# The following bounds are precomputed versions of the following abstract
# function, but the implicit interface for AbstractFloat doesn't uniformly
# enforce that all floating point types implement nextfloat and prevfloat.
Expand Down
36 changes: 36 additions & 0 deletions src/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@ ChainRulesCore.@scalar_rule(xlogx(x::Real), (1 + log(x),))
ChainRulesCore.@scalar_rule(xlogy(x::Real, y::Real), (log(y), x / y,))
ChainRulesCore.@scalar_rule(xlog1py(x::Real, y::Real), (log1p(y), x / (1 + y),))

function ChainRulesCore.frule((_, Δx), ::typeof(xexpx), x::Real)
expx = exp(x)
if iszero(expx)
Ω = expx
ΔΩ = expx * Δx
else
Ω = x * expx
ΔΩ = (1 + x) * expx * Δx
end
return Ω, ΔΩ
end

function ChainRulesCore.rrule(::typeof(xexpx), x::Real)
expx = exp(x)
Ω = iszero(expx) ? expx : x * expx
function xexpx_pullback(ΔΩ)
Δx = iszero(expx) ? expx * ΔΩ : (1 + x) * expx * ΔΩ
return (ChainRulesCore.NoTangent(), Δx)
end
return Ω, xexpx_pullback
end

function ChainRulesCore.frule((_, Δx, Δy), ::typeof(xexpy), x::Real, y::Real)
expy = exp(y)
Ω = iszero(expy) && !isnan(x) ? zero(x * expy) : x * expy
ΔΩ = expy * Δx + Ω * Δy
return Ω, ΔΩ
end

function ChainRulesCore.rrule(::typeof(xexpy), x::Real, y::Real)
expy = exp(y)
Ω = iszero(expy) && !isnan(x) ? zero(x * expy) : x * expy
xexpy_pullback(ΔΩ) = (ChainRulesCore.NoTangent(), ΔΩ * expy, ΔΩ * Ω)
return Ω, xexpy_pullback
end

ChainRulesCore.@scalar_rule(logistic(x::Real), (Ω * (1 - Ω),))
ChainRulesCore.@scalar_rule(logit(x::Real), (inv(x * (1 - x)),))
ChainRulesCore.@scalar_rule(logcosh(x::Real), tanh(x))
Expand Down
32 changes: 32 additions & 0 deletions test/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@
@test iszero(xlog1py(0 + im * 0, -1 + im * Inf))
end

@testset "xexpx" begin
for x in (false, 0, 0.0, 0f0, -Inf, -Inf32)
@test (@inferred xexpx(x)) === zero(exp(x))
end
for x in (NaN16, NaN32, NaN64, Inf16, Inf32, Inf64)
@test (@inferred xexpx(x)) === x
end
for x in (1, true, 1.0, 1f0)
@test (@inferred xexpx(x)) === exp(x)
end
for T in (Int, Float32, Float64), x in T.(-2:2)
@test (@inferred xexpx(x)) === x * exp(x)
end
end

@testset "xexpy" begin
for x in (0, 1, 1.0, 1f0, Inf, Inf32), y in (-Inf, -Inf32)
@test (@inferred xexpy(x, y)) === zero(x * exp(y))
end
for x in (0, 1, 1.0, 1f0, Inf, Inf32, -Inf, -Inf32, NaN, NaN32), nan in (NaN, NaN32)
@test (@inferred xexpy(x, nan)) === oftype(x * exp(nan), NaN)
@test (@inferred xexpy(nan, x)) === oftype(nan * exp(x), NaN)
end
Ts = (Int, Float32, Float64)
for Tx in Ts, Ty in Ts, x = -Tx(2):Tx(2), y = -Ty(2):Ty(2)
@test (@inferred xexpy(x, y)) x * exp(y)
end
for x in (randn(), randn(Float32))
@test xexpy(x, x) xexpx(x)
end
end

@testset "logistic & logit" begin
@test logistic(2) 1.0 / (1.0 + exp(-2.0))
@test logistic(-750.0) === 0.0
Expand Down
16 changes: 16 additions & 0 deletions test/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
end
end

@testset "xexpx, xexpy" begin
# regular branch
test_scalar(xexpx, randn())
test_frule(xexpy, randn(), randn())
test_rrule(xexpy, randn(), randn())
# special cases (manually since FiniteDifferences/ChainRulesTestUtils fails at -Inf)
@test @inferred(frule((NoTangent(), 1), xexpx, -Inf)) === (0.0, 0.0)
Ω, back = @inferred(rrule(xexpx, -Inf))
@test Ω === 0.0
@test back(randn()) === (NoTangent(), 0.0)
@test @inferred(frule((NoTangent(), 1, 1), xexpy, x, -Inf)) === (0.0, 0.0)
Ω, back = @inferred(ChainRulesCore.rrule(xexpy, x, -Inf))
@test Ω === 0.0
@test back(randn()) === (NoTangent(), 0.0, 0.0)
end

test_frule(logit, x)
test_rrule(logit, x)

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LogExpFunctions
using ChainRulesTestUtils
using ChainRulesCore
using ChangesOfVariables
using InverseFunctions
using OffsetArrays
Expand Down

0 comments on commit a36c63e

Please sign in to comment.