Skip to content

Commit

Permalink
xexpx, xexpy
Browse files Browse the repository at this point in the history
  • Loading branch information
cossio committed Mar 4, 2022
1 parent c8a4c28 commit 3b9a8b5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
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
25 changes: 25 additions & 0 deletions src/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ function xlog1py(x::Number, y::Number)
return iszero(x) && !isnan(y) ? zero(result) : result
end

"""
$(SIGNATURES)
Return `x * exp(x)` for `x > -Inf`, handling `x = -Inf` by taking the downward limit.
```jldoctest
julia> xexpx(-Inf)
0.0
```
"""
xexpx(x::Real) = isfinite(x) ? x * exp(x) : exp(x)

"""
$(SIGNATURES)
Return `x * exp(y)` for `y > -Inf`, handling `y = -Inf` by taking the downward limit.
```jldoctest
julia> xexpy(1.0, -Inf)
0.0
```
"""
xexpy(x::T, y::T) where {T<:Real} = isnan(x) || isfinite(y) ? x * exp(y) : exp(y)
xexpy(x::Real, y::Real) = xexpy(promote(x, y)...)

# 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
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
@test iszero(xexpx(-Inf))
@test iszero(xexpx(0))
@test isnan(xexpx(NaN))

@test xexpx(1) exp(1)
@test xexpx(2) 2exp(2)

@inferred xexpx(0)
@inferred xexpx(0.)
@inferred xexpx(0f0)
end

@testset "xexpy" begin
@test iszero(xexpy(Inf, -Inf))

@test isnan(xexpy(NaN, -Inf))
@test isnan(xexpy(NaN, 1))
@test isnan(xexpy(0, NaN))

@inferred xexpy(0,-Inf)
@inferred xexpy(1,1)
@inferred xexpy(1.,-Inf)

@test xexpy(2,3) 2exp(3)

for x = -10:10, y = -10:10
@test xexpy(x,y) x * exp(y)
end
end

@testset "logistic & logit" begin
@test logistic(2) 1.0 / (1.0 + exp(-2.0))
@test logistic(-750.0) === 0.0
Expand Down

0 comments on commit 3b9a8b5

Please sign in to comment.