From e8d075a7bf80837c74671e76272e67354a91e462 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Sat, 16 Nov 2024 02:35:49 -0500 Subject: [PATCH 1/6] add logabstanh --- src/basicfuns.jl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/basicfuns.jl b/src/basicfuns.jl index e013adf..faf144b 100644 --- a/src/basicfuns.jl +++ b/src/basicfuns.jl @@ -149,6 +149,31 @@ end """ $(SIGNATURES) +Return `log(abs(tanh(x)))`, carefully evaluated without intermediate calculation of `tanh(x)`. + +The implementation ensures `logabstanh(-x) = logabstanh(x)`. +""" +function logabstanh(x::Real) + return log1p(-2/((exp(2abs(x))+1))) +end +function logabstanh(x::Float32) + abs_x = abs(x) + if abs_x < 0.0625f0 + return log(abs_x) - x*x*(1f0/3) + end + return log1p(-2/((exp(2abs_x)+1))) +end +function logabstanh(x::Float64) + abs_x = abs(x) + if abs_x < 0x1p-5 + return log(abs_x) + evalpoly(x*x, (0, -1/3, 7/90, -62/2835)) + end + return log1p(-2/((exp(2abs_x)+1))) +end + +""" +$(SIGNATURES) + Return `log(1+x^2)` evaluated carefully for `abs(x)` very small or very large. """ log1psq(x::Real) = log1p(abs2(x)) From 528545d222bdc033282b1006755f1da3837d7521 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Sat, 16 Nov 2024 02:40:26 -0500 Subject: [PATCH 2/6] export --- src/LogExpFunctions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LogExpFunctions.jl b/src/LogExpFunctions.jl index 739e005..bc5fefd 100644 --- a/src/LogExpFunctions.jl +++ b/src/LogExpFunctions.jl @@ -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, logabssinh, cloglog, cexpexp, + softmax!, logcosh, logabssinh, logabstanh, cloglog, cexpexp, loglogistic, logitexp, log1mlogistic, logit1mexp # expm1(::Float16) is not defined in older Julia versions, From 5cf00585a60bd62801271376ef7685a8d4093e03 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Sat, 16 Nov 2024 09:36:03 -0500 Subject: [PATCH 3/6] Update src/basicfuns.jl Co-authored-by: David Widmann --- src/basicfuns.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basicfuns.jl b/src/basicfuns.jl index faf144b..47c8943 100644 --- a/src/basicfuns.jl +++ b/src/basicfuns.jl @@ -154,7 +154,7 @@ Return `log(abs(tanh(x)))`, carefully evaluated without intermediate calculation The implementation ensures `logabstanh(-x) = logabstanh(x)`. """ function logabstanh(x::Real) - return log1p(-2/((exp(2abs(x))+1))) + return log1p(-2/(exp(2*abs(x))+1)) end function logabstanh(x::Float32) abs_x = abs(x) From 95639d1029bbca72210a2e8af9b6a12c2a308bce Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Sat, 16 Nov 2024 09:36:11 -0500 Subject: [PATCH 4/6] Update src/basicfuns.jl Co-authored-by: David Widmann --- src/basicfuns.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basicfuns.jl b/src/basicfuns.jl index 47c8943..689ae22 100644 --- a/src/basicfuns.jl +++ b/src/basicfuns.jl @@ -159,7 +159,7 @@ end function logabstanh(x::Float32) abs_x = abs(x) if abs_x < 0.0625f0 - return log(abs_x) - x*x*(1f0/3) + return log(abs_x) - x^2/3 end return log1p(-2/((exp(2abs_x)+1))) end From 3364d2035360fe4dbba4ff044f57c0d952c2c632 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Sat, 16 Nov 2024 10:24:52 -0500 Subject: [PATCH 5/6] Update src/basicfuns.jl Co-authored-by: David Widmann --- src/basicfuns.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basicfuns.jl b/src/basicfuns.jl index 689ae22..9c5bf34 100644 --- a/src/basicfuns.jl +++ b/src/basicfuns.jl @@ -161,7 +161,7 @@ function logabstanh(x::Float32) if abs_x < 0.0625f0 return log(abs_x) - x^2/3 end - return log1p(-2/((exp(2abs_x)+1))) + return log1p(-2/(exp(2*abs_x)+1)) end function logabstanh(x::Float64) abs_x = abs(x) From 0b6d9f9c11a47f6aebce1188f5642bf819c679b5 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Sat, 16 Nov 2024 10:24:57 -0500 Subject: [PATCH 6/6] Update src/basicfuns.jl Co-authored-by: David Widmann --- src/basicfuns.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basicfuns.jl b/src/basicfuns.jl index 9c5bf34..e2828f1 100644 --- a/src/basicfuns.jl +++ b/src/basicfuns.jl @@ -168,7 +168,7 @@ function logabstanh(x::Float64) if abs_x < 0x1p-5 return log(abs_x) + evalpoly(x*x, (0, -1/3, 7/90, -62/2835)) end - return log1p(-2/((exp(2abs_x)+1))) + return log1p(-2/(exp(2*abs_x)+1)) end """