Skip to content

Commit

Permalink
Merge pull request #27 from dingraha/brent_cs_safe
Browse files Browse the repository at this point in the history
Make `ksmax` and friends complex-step safe
  • Loading branch information
dingraha authored Jan 30, 2025
2 parents 95a5bcd + 126b2c2 commit 6b4b258
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FLOWMath"
uuid = "6cb5d3fb-0fe8-4cc2-bd89-9fe0b19a99d3"
authors = ["Andrew Ning <[email protected]>"]
version = "0.4.1"
version = "0.4.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
6 changes: 3 additions & 3 deletions src/smooth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ goes to infinity the maximum function is returned. Is mathematically guaranteed
overestimate the maximum function, i.e. `maximum(x) <= ksmax(x, hardness)`.
"""
function ksmax(x, hardness=50)
k = maximum(x)
k = maximum(real(x))
return 1.0 / hardness * log(sum(exp.(hardness * (x .- k)))) .+ k
end

Expand Down Expand Up @@ -83,7 +83,7 @@ Computes the derivative of the Kreisselmeier–Steinhauser constraint aggregatio
function with respect to `hardness`.
"""
function ksmax_h(x, hardness)
k = maximum(x)
k = maximum(real(x))
tmp1 = exp.(hardness * (x .- k))
tmp2 = sum((x .- k) .* tmp1)
tmp3 = sum(tmp1)
Expand All @@ -98,7 +98,7 @@ Computes the second derivative of the Kreisselmeier–Steinhauser constraint agg
function with respect to `hardness`.
"""
function ksmax_hh(x, hardness)
k = maximum(x)
k = maximum(real(x))
tmp1 = exp.(hardness * (x .- k))
tmp2 = sum((x .- k) .* tmp1)
tmp2_h = sum((x .- k) .^ 2 .* tmp1)
Expand Down
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ hardness = 100.0
x_max_smooth = ksmin(x, hardness)
@test isapprox(x_max_smooth, -0.006931471805599453)

# Test we can diff through ksmax and ksmin:
ksmax_wrapper(x) = sin(ksmax(x.^2 .+ 2))
x0 = [1.0, 1.5, 2.0, 2.5, 3.0]
g1 = ForwardDiff.gradient(ksmax_wrapper, x0)
g2 = FiniteDiff.finite_difference_gradient(ksmax_wrapper, x0, Val(:complex))
@test maximum(abs.(g2 .- g1)) < 1e-12

ksmin_wrapper(x) = sin(ksmin(x.^2 .+ 2))
x0 = [1.0, 1.5, 2.0, 2.5, 3.0]
g1 = ForwardDiff.gradient(ksmin_wrapper, x0)
g2 = FiniteDiff.finite_difference_gradient(ksmin_wrapper, x0, Val(:complex))
@test maximum(abs.(g2 .- g1)) < 1e-12

# -------------------------

# ------ ksmax_adaptive ---------
Expand Down Expand Up @@ -348,6 +361,19 @@ smoothing_fraction = 0.2
x_max_smooth = ksmin_adaptive(x, smoothing_fraction=smoothing_fraction)
@test isapprox(x_max_smooth, -5.2856933329025475e-6)

# Test we can diff through ksmax_adaptive and ksmin_adaptive:
ksmax_adaptive_wrapper(x) = sin(ksmax_adaptive(x.^2 .+ 2))
x0 = [1.0, 1.5, 2.0, 2.5, 3.0]
g1 = ForwardDiff.gradient(ksmax_adaptive_wrapper, x0)
g2 = FiniteDiff.finite_difference_gradient(ksmax_adaptive_wrapper, x0, Val(:complex))
@test maximum(abs.(g2 .- g1)) < 1e-12

ksmin_adaptive_wrapper(x) = sin(ksmin_adaptive(x.^2 .+ 2))
x0 = [1.0, 1.5, 2.0, 2.5, 3.0]
g1 = ForwardDiff.gradient(ksmin_adaptive_wrapper, x0)
g2 = FiniteDiff.finite_difference_gradient(ksmin_adaptive_wrapper, x0, Val(:complex))
@test maximum(abs.(g2 .- g1)) < 1e-12

# -------------------------

# ------ sigmoid ---------
Expand Down

0 comments on commit 6b4b258

Please sign in to comment.