From d0c80b709c64025ccde96cf56b4d781262f5fd7e Mon Sep 17 00:00:00 2001 From: David Widmann Date: Tue, 19 Oct 2021 22:33:47 +0200 Subject: [PATCH] Define inverse functions (#29) --- Project.toml | 4 +++- src/LogExpFunctions.jl | 2 ++ src/inverse.jl | 9 +++++++++ test/inverse.jl | 11 +++++++++++ test/runtests.jl | 2 ++ 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/inverse.jl create mode 100644 test/inverse.jl diff --git a/Project.toml b/Project.toml index 1ab84f4c..c984e84f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,17 +1,19 @@ name = "LogExpFunctions" uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688" authors = ["StatsFun.jl contributors, Tamas K. Papp "] -version = "0.3.3" +version = "0.3.4" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" +InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" [compat] ChainRulesCore = "1" DocStringExtensions = "0.8" +InverseFunctions = "0.1" IrrationalConstants = "0.1" julia = "1" diff --git a/src/LogExpFunctions.jl b/src/LogExpFunctions.jl index 6aaa503f..d48da449 100644 --- a/src/LogExpFunctions.jl +++ b/src/LogExpFunctions.jl @@ -4,6 +4,7 @@ using DocStringExtensions: SIGNATURES using Base: Math.@horner import ChainRulesCore +import InverseFunctions import IrrationalConstants import LinearAlgebra @@ -14,5 +15,6 @@ export xlogx, xlogy, xlog1py, logistic, logit, log1psq, log1pexp, log1mexp, log2 include("basicfuns.jl") include("logsumexp.jl") include("chainrules.jl") +include("inverse.jl") end # module diff --git a/src/inverse.jl b/src/inverse.jl new file mode 100644 index 00000000..4bd0a9d3 --- /dev/null +++ b/src/inverse.jl @@ -0,0 +1,9 @@ +InverseFunctions.inverse(::typeof(log1pexp)) = logexpm1 +InverseFunctions.inverse(::typeof(logexpm1)) = log1pexp + +InverseFunctions.inverse(::typeof(log1mexp)) = log1mexp + +InverseFunctions.inverse(::typeof(log2mexp)) = log2mexp + +InverseFunctions.inverse(::typeof(logit)) = logistic +InverseFunctions.inverse(::typeof(logistic)) = logit diff --git a/test/inverse.jl b/test/inverse.jl new file mode 100644 index 00000000..959e3d4e --- /dev/null +++ b/test/inverse.jl @@ -0,0 +1,11 @@ +@testset "inverse.jl" begin + InverseFunctions.test_inverse(log1pexp, randn()) + InverseFunctions.test_inverse(logexpm1, randexp()) + + InverseFunctions.test_inverse(log1mexp, -randexp()) + + InverseFunctions.test_inverse(log2mexp, log(2) - randexp()) + + InverseFunctions.test_inverse(logistic, randn()) + InverseFunctions.test_inverse(logit, rand()) +end diff --git a/test/runtests.jl b/test/runtests.jl index ccf134f7..71455b2b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using LogExpFunctions using ChainRulesTestUtils +using InverseFunctions using OffsetArrays using Random @@ -9,3 +10,4 @@ Random.seed!(1234) include("basicfuns.jl") include("chainrules.jl") +include("inverse.jl")