-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #993 from theogf/logexpfunctions
Implement methods for LogExpFunctions
- Loading branch information
Showing
5 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Implement a few of the LogExpFuncitons methods when those rely on boolean workflows. | ||
|
||
LogExpFunctions.log1mexp(x::RCNum) = log(1 - exp(x)) | ||
LogExpFunctions.log1pexp(x::RCNum) = log(1 + exp(x)) | ||
LogExpFunctions.logexpm1(x::RCNum) = log(exp(x) - 1) | ||
LogExpFunctions.logmxp1(x::RCNum) = log(x) - x + 1 | ||
for (f, op) in ((:logaddexp, +), (:logsubexp, -)) | ||
@eval begin | ||
LogExpFunctions.$(f)(x::RCNum, y::Real) = log($(op)(exp(x), exp(y))) | ||
LogExpFunctions.$(f)(x::Real, y::RCNum) = log($(op)(exp(x), exp(y))) | ||
LogExpFunctions.$(f)(x::RCNum, y::RCNum) = log($(op)(exp(x), exp(y))) | ||
end | ||
end | ||
function LogExpFunctions.logsumexp(x::Union{AbstractVector{<:Num}, Arr}) | ||
log(sum(exp, x; init = 0.0)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Symbolics | ||
using LogExpFunctions | ||
|
||
N = 10 | ||
|
||
@variables a, b, c, x[1:N] | ||
|
||
_a = -0.2 | ||
_b = -1.0 | ||
_c = 2.0 | ||
_x = rand(N) | ||
|
||
vals = Dict(a => _a, b => _b, c => _c, x => _x) | ||
|
||
@test substitute(logaddexp(a, b), vals) ≈ logaddexp(_a, _b) | ||
@test substitute(logaddexp(a, _b), vals) ≈ logaddexp(_a, _b) | ||
@test substitute(logaddexp(_a, b), vals) ≈ logaddexp(_a, _b) | ||
@test substitute(logsubexp(a, b), vals) ≈ logsubexp(_a, _b) | ||
@test substitute(logsubexp(a, _b), vals) ≈ logsubexp(_a, _b) | ||
@test substitute(logsubexp(_a, b), vals) ≈ logsubexp(_a, _b) | ||
@test substitute(log1mexp(a), vals) ≈ log1mexp(_a) | ||
@test substitute(log1pexp(a), vals) ≈ log1pexp(_a) | ||
@test substitute(logexpm1(c), vals) ≈ logexpm1(_c) | ||
@test substitute(logmxp1(c), vals) ≈ logmxp1(_c) | ||
@test substitute(logsumexp(x), vals) ≈ logsumexp(_x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters