Skip to content

Commit 01488be

Browse files
authored
avoid catastrophic cancellation in divided differences (#858)
* avoid catastrophic cancellation in divided differences * rename rteps thresh
1 parent a6c2fc2 commit 01488be

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/Cones/epitrrelentropytri.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ function dder3(
515515
end
516516

517517
function Δ2!(Δ2::Matrix{T}, λ::Vector{T}, log_λ::Vector{T}) where {T <: Real}
518-
rteps = sqrt(eps(T))
518+
thresh = cbrt(eps(T))
519519
d = length(λ)
520520

521521
@inbounds for j in 1:d
@@ -524,7 +524,7 @@ function Δ2!(Δ2::Matrix{T}, λ::Vector{T}, log_λ::Vector{T}) where {T <: Real
524524
for i in 1:(j - 1)
525525
λ_i = λ[i]
526526
λ_ij = λ_i - λ_j
527-
if abs(λ_ij) < rteps
527+
if abs(λ_ij) < thresh
528528
Δ2[i, j] = 2 / (λ_i + λ_j)
529529
else
530530
Δ2[i, j] = (log_λ[i] - lλ_j) / λ_ij
@@ -540,17 +540,17 @@ end
540540

541541
function Δ3!(Δ3::Array{T, 3}, Δ2::Matrix{T}, λ::Vector{T}) where {T <: Real}
542542
@assert issymmetric(Δ2) # must be symmetric (wrapper is less efficient)
543-
rteps = sqrt(eps(T))
543+
thresh = eps(T)^(2/9)
544544
d = length(λ)
545545

546546
@inbounds for k in 1:d, j in 1:k, i in 1:j
547547
λ_j = λ[j]
548548
λ_k = λ[k]
549549
λ_jk = λ_j - λ_k
550-
if abs(λ_jk) < rteps
550+
if abs(λ_jk) < thresh
551551
λ_i = λ[i]
552552
λ_ij = λ_i - λ_j
553-
if abs(λ_ij) < rteps
553+
if abs(λ_ij) < thresh
554554
t = abs2(3 / (λ_i + λ_j + λ_k)) / -2
555555
else
556556
t = (Δ2[i, j] - Δ2[j, k]) / λ_ij
@@ -661,7 +661,7 @@ function Δ4_ij!(
661661
Δ3::Array{T, 3},
662662
λ::Vector{T},
663663
) where {T <: Real}
664-
rteps = sqrt(eps(T))
664+
thresh = eps(T)^(4/27)
665665
d = length(λ)
666666
λ_i = λ[i]
667667
λ_j = λ[j]
@@ -672,11 +672,12 @@ function Δ4_ij!(
672672
λ_ij = λ_i - λ_j
673673
λ_ik = λ_i - λ_k
674674
λ_il = λ_i - λ_l
675-
B_ik = (abs(λ_ik) < rteps)
676-
B_il = (abs(λ_il) < rteps)
675+
B_ik = (abs(λ_ik) < thresh)
676+
B_il = (abs(λ_il) < thresh)
677677

678-
if (abs(λ_ij) < rteps) && B_ik && B_il
679-
t = λ_i^-3 / 3
678+
if (abs(λ_ij) < thresh) && B_ik && B_il
679+
λ_mean = (λ_i + λ_j + λ_k + λ_l)/4
680+
t = λ_mean^-3 / 3
680681
elseif B_ik && B_il
681682
t = (Δ3[i, i, i] - Δ3[i, i, j]) / λ_ij
682683
elseif B_il

0 commit comments

Comments
 (0)