Skip to content

Commit

Permalink
add logabssinh (#70)
Browse files Browse the repository at this point in the history
* add logabssinh

* add logabssinh to docs, bump version

* logabssinh: ChainRules test
  • Loading branch information
longemen3000 authored Jun 2, 2023
1 parent 1f85cdd commit 24fcdab
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 3 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.23"
version = "0.3.24"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ xexpy
logistic
logit
logcosh
logabssinh
log1psq
log1pexp
log1mexp
Expand Down
1 change: 1 addition & 0 deletions ext/LogExpFunctionsChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ 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))
ChainRulesCore.@scalar_rule(logabssinh(x::Real), coth(x))
ChainRulesCore.@scalar_rule(log1psq(x::Real), (2 * x / (1 + x^2),))
ChainRulesCore.@scalar_rule(log1pexp(x::Real), (logistic(x),))
ChainRulesCore.@scalar_rule(log1mexp(x::Real), (-exp(x - Ω),))
Expand Down
2 changes: 1 addition & 1 deletion src/LogExpFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import LinearAlgebra

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

include("basicfuns.jl")
include("logsumexp.jl")
Expand Down
12 changes: 12 additions & 0 deletions src/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ end
"""
$(SIGNATURES)
Return `log(abs(sinh(x)))`, carefully evaluated without intermediate calculation of `sinh(x)`.
The implementation ensures `logabssinh(-x) = logabssinh(x)`.
"""
function logabssinh(x::Real)
abs_x = abs(x)
return abs_x + log1mexp(- 2 * abs_x) - IrrationalConstants.logtwo
end

"""
$(SIGNATURES)
Return `log(1+x^2)` evaluated carefully for `abs(x)` very small or very large.
"""
log1psq(x::Real) = log1p(abs2(x))
Expand Down
7 changes: 6 additions & 1 deletion test/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,24 @@ end
@test logit(logistic(2)) 2.0
end

@testset "logcosh" begin
@testset "logcosh and logabssinh" begin
for x in (randn(), randn(Float32))
@test @inferred(logcosh(x)) isa typeof(x)
@test logcosh(x) log(cosh(x))
@test logcosh(-x) == logcosh(x)
@test @inferred(logabssinh(x)) isa typeof(x)
@test logabssinh(x) log(abs(sinh(x)))
@test logabssinh(-x) == logabssinh(x)
end

# special values
for x in (-Inf, Inf, -Inf32, Inf32)
@test @inferred(logcosh(x)) === oftype(x, Inf)
@test @inferred(logabssinh(x)) === oftype(x, Inf)
end
for x in (NaN, NaN32)
@test @inferred(logcosh(x)) === x
@test @inferred(logabssinh(x)) === x
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
for x in (-randexp(), randexp())
test_frule(logcosh, x)
test_rrule(logcosh, x)
test_frule(logabssinh, x)
test_rrule(logabssinh, x)
end

@testset "log1pexp" begin
Expand Down

2 comments on commit 24fcdab

@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/84708

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.24 -m "<description of version>" 24fcdab9a73696a5be925d6bf1d5aefa0db983ef
git push origin v0.3.24

Please sign in to comment.