Skip to content

Commit

Permalink
xexpx, xexpy (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
cossio authored Mar 7, 2022
1 parent c8a4c28 commit 8ce6807
Show file tree
Hide file tree
Showing 8 changed files with 126 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
31 changes: 31 additions & 0 deletions src/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ 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)
result = x * expy
return iszero(expy) && !isnan(x) ? zero(result) : result
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
38 changes: 38 additions & 0 deletions src/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@ 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)
result = x * expy
Ω = iszero(expy) && !isnan(x) ? zero(result) : result
ΔΩ = expy * Δx + Ω * Δy
return Ω, ΔΩ
end

function ChainRulesCore.rrule(::typeof(xexpy), x::Real, y::Real)
expy = exp(y)
result = x * expy
Ω = iszero(expy) && !isnan(x) ? zero(result) : result
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
31 changes: 31 additions & 0 deletions test/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@
@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 a in (2, 2f0, 2.0), x in -a:a
@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
for x in (2, -2f0, 2.0), y in (1, -1f0, 1.0)
@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
21 changes: 21 additions & 0 deletions test/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@
end
end

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

@testset "xexpy" begin
# regular branch
test_frule(xexpy, randn(), randn())
test_rrule(xexpy, randn(), randn())
# special cases (manually since FiniteDifferences/ChainRulesTestUtils fails at -Inf)
@test @inferred(frule((NoTangent(), rand(), rand()), xexpy, x, -Inf)) === (0.0, 0.0)
Ω, back = @inferred(rrule(xexpy, x, -Inf))
@test Ω === 0.0
@test back(rand()) === (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

2 comments on commit 8ce6807

@devmotion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/56119

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.7 -m "<description of version>" 8ce680747d13543a56a687bca8fbef93a9b45a2c
git push origin v0.3.7

Please sign in to comment.