-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
nanlogsumexp
and nanlogcumsumexp
- Loading branch information
1 parent
3853027
commit 7d14749
Showing
5 changed files
with
143 additions
and
6 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,85 @@ | ||
|
||
""" | ||
```julia | ||
nanlogsumexp(A) | ||
``` | ||
Return the logarithm of the sum of `exp.(A)` -- i.e., `log(sum(exp.(A)))`, but ignoring `NaN`s and avoiding numerical over/underflow. | ||
As `nancumsum`, but operating on logarithms; as `nanlogsumexp`, but returning a array of cumulative sums, rather than a single value. | ||
## Examples | ||
```julia | ||
``` | ||
""" | ||
nanlogsumexp(A) = _nanlogsumexp(A, nanmaximum(A)) | ||
export nanlogsumexp | ||
|
||
function _nanlogsumexp(A, c) | ||
Σ = ∅ = zero(Base.promote_op(exp, eltype(A))) | ||
@inbounds @simd ivdep for i ∈ eachindex(A) | ||
Aᵢ = exp(A[i] - c) | ||
Σ += ifelse(isnan(Aᵢ), ∅, Aᵢ) | ||
end | ||
return log(Σ) + c | ||
end | ||
|
||
|
||
""" | ||
```julia | ||
nanlogcumsumexp(A) | ||
``` | ||
Return the logarithm of the cumulative sum of `exp.(A)` -- i.e., `log.(cumsum.(exp.(A)))`, but ignoring `NaN`s and avoiding numerical over/underflow. | ||
As `nancumsum`, but operating on logarithms; as `nanlogsumexp`, but returning a array of cumulative sums, rather than a single value. | ||
## Examples | ||
```julia | ||
``` | ||
""" | ||
nanlogcumsumexp(A; reverse=false) = _nanlogcumsumexp(A, static(reverse)) | ||
export nanlogcumsumexp | ||
|
||
function _nanlogcumsumexp(A, ::False) | ||
Tᵣ = Base.promote_op(exp, eltype(A)) | ||
Σ = ∅ = zero(Tᵣ) | ||
lΣ = fill!(similar(A, Tᵣ), ∅) | ||
c = linit(eltype(A)) | ||
i₀ = firstindex(A) | ||
@inbounds for i ∈ eachindex(A) | ||
isnan(c) && (c = A[i]) | ||
if A[i] > c | ||
c = A[i] | ||
Σ = ∅ | ||
@simd ivdep for j ∈ i₀:i | ||
Aᵢ = exp(A[j] - c) | ||
Σ += ifelse(isnan(Aᵢ), ∅, Aᵢ) | ||
end | ||
lΣ[i] = log(Σ) + c | ||
else | ||
Aᵢ = exp(A[i] - c) | ||
Σ += ifelse(isnan(Aᵢ), ∅, Aᵢ) | ||
lΣ[i] = log(Σ) + c | ||
end | ||
end | ||
return lΣ | ||
end | ||
function _nanlogcumsumexp(A, ::True) | ||
Tᵣ = Base.promote_op(exp, eltype(A)) | ||
Σ = ∅ = zero(Tᵣ) | ||
lΣ = fill!(similar(A, Tᵣ), ∅) | ||
c = linit(eltype(A)) | ||
iₗ = lastindex(A) | ||
@inbounds for i ∈ reverse(eachindex(A)) | ||
isnan(c) && (c = A[i]) | ||
if A[i] > c | ||
c = A[i] | ||
Σ = ∅ | ||
@simd ivdep for j ∈ i:iₗ | ||
Aᵢ = exp(A[j] - c) | ||
Σ += ifelse(isnan(Aᵢ), ∅, Aᵢ) | ||
end | ||
lΣ[i] = log(Σ) + c | ||
else | ||
Aᵢ = exp(A[i] - c) | ||
Σ += ifelse(isnan(Aᵢ), ∅, Aᵢ) | ||
lΣ[i] = log(Σ) + c | ||
end | ||
end | ||
return lΣ | ||
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
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
7d14749
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
Release notes:
nanlogsumexp
andnanlogcumsumexp
7d14749
There was a problem hiding this comment.
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/110138
Tagging
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: