Skip to content

Commit 24fcdab

Browse files
authored
add logabssinh (#70)
* add logabssinh * add logabssinh to docs, bump version * logabssinh: ChainRules test
1 parent 1f85cdd commit 24fcdab

File tree

7 files changed

+24
-3
lines changed

7 files changed

+24
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LogExpFunctions"
22
uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
33
authors = ["StatsFun.jl contributors, Tamas K. Papp <[email protected]>"]
4-
version = "0.3.23"
4+
version = "0.3.24"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ xexpy
1515
logistic
1616
logit
1717
logcosh
18+
logabssinh
1819
log1psq
1920
log1pexp
2021
log1mexp

ext/LogExpFunctionsChainRulesCoreExt.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ end
117117
ChainRulesCore.@scalar_rule(logistic(x::Real), (Ω * (1 - Ω),))
118118
ChainRulesCore.@scalar_rule(logit(x::Real), (inv(x * (1 - x)),))
119119
ChainRulesCore.@scalar_rule(logcosh(x::Real), tanh(x))
120+
ChainRulesCore.@scalar_rule(logabssinh(x::Real), coth(x))
120121
ChainRulesCore.@scalar_rule(log1psq(x::Real), (2 * x / (1 + x^2),))
121122
ChainRulesCore.@scalar_rule(log1pexp(x::Real), (logistic(x),))
122123
ChainRulesCore.@scalar_rule(log1mexp(x::Real), (-exp(x - Ω),))

src/LogExpFunctions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import LinearAlgebra
88

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

1313
include("basicfuns.jl")
1414
include("logsumexp.jl")

src/basicfuns.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ end
137137
"""
138138
$(SIGNATURES)
139139
140+
Return `log(abs(sinh(x)))`, carefully evaluated without intermediate calculation of `sinh(x)`.
141+
142+
The implementation ensures `logabssinh(-x) = logabssinh(x)`.
143+
"""
144+
function logabssinh(x::Real)
145+
abs_x = abs(x)
146+
return abs_x + log1mexp(- 2 * abs_x) - IrrationalConstants.logtwo
147+
end
148+
149+
"""
150+
$(SIGNATURES)
151+
140152
Return `log(1+x^2)` evaluated carefully for `abs(x)` very small or very large.
141153
"""
142154
log1psq(x::Real) = log1p(abs2(x))

test/basicfuns.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,24 @@ end
8585
@test logit(logistic(2)) 2.0
8686
end
8787

88-
@testset "logcosh" begin
88+
@testset "logcosh and logabssinh" begin
8989
for x in (randn(), randn(Float32))
9090
@test @inferred(logcosh(x)) isa typeof(x)
9191
@test logcosh(x) log(cosh(x))
9292
@test logcosh(-x) == logcosh(x)
93+
@test @inferred(logabssinh(x)) isa typeof(x)
94+
@test logabssinh(x) log(abs(sinh(x)))
95+
@test logabssinh(-x) == logabssinh(x)
9396
end
9497

9598
# special values
9699
for x in (-Inf, Inf, -Inf32, Inf32)
97100
@test @inferred(logcosh(x)) === oftype(x, Inf)
101+
@test @inferred(logabssinh(x)) === oftype(x, Inf)
98102
end
99103
for x in (NaN, NaN32)
100104
@test @inferred(logcosh(x)) === x
105+
@test @inferred(logabssinh(x)) === x
101106
end
102107
end
103108

test/chainrules.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
for x in (-randexp(), randexp())
6363
test_frule(logcosh, x)
6464
test_rrule(logcosh, x)
65+
test_frule(logabssinh, x)
66+
test_rrule(logabssinh, x)
6567
end
6668

6769
@testset "log1pexp" begin

0 commit comments

Comments
 (0)