Skip to content

Commit

Permalink
Add few missing tests (#205)
Browse files Browse the repository at this point in the history
* Add test from #88 for #87

* Add root at infinity test

* Merge stationary point tests
  • Loading branch information
Kolaru authored Nov 6, 2024
1 parent 321599d commit b9c3c7e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
37 changes: 29 additions & 8 deletions test/roots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ end
end
end

@testset "Dimension mismatch" begin
f21(xy) = [xy[1]^2 - 2]
f23(xy) = [xy[1]^2 - 2, xy[2]^2 - 3, xy[1] + xy[2]]

X = [interval(0, 5), interval(0, 5)]

for contractor in newtonlike_methods
@test_throws DimensionMismatch roots(f21, X ; contractor)
@test_throws DimensionMismatch roots(f23, X ; contractor)
end
end

@testset "Out of domain" begin
for contractor in newtonlike_methods
@test length(roots(log, interval(-100, 2) ; contractor)) == 1
Expand All @@ -158,16 +170,25 @@ end
end
end

@testset "Stationary points" begin
f(xx) = ( (x, y) = xx; sin(x) * sin(y) )
gradf = xx -> ForwardDiff.gradient(f, xx)
@testset "Root at infinity" begin
for contractor in newtonlike_methods
pb = RootProblem(x -> 1/x, interval(1, Inf) ; contractor)

XX = [interval(-5, 6), interval(-5, 6)]
tol = 1e-5
state = nothing
for (k, s) in enumerate(pb)
state = s

k > 1000 && break
end

for method in newtonlike_methods
deriv = xx -> ForwardDiff.jacobian(gradf, xx)
test_newtonlike(gradf, deriv, XX, method, 25, tol)
rts = [leaf.region for leaf in BranchAndPrune.Leaves(state.tree)]
for rt in rts
# Some roots have an empty trivial interval for unknown reason,
# which is unhelpful but not incorrect
if decoration(rt.region) != trv
@test sup(rt.region) == Inf
end
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using IntervalRootFinding
using IntervalArithmetic.Symbols
using BranchAndPrune
using Test

newtonlike_methods = [Newton, Krawczyk]

include("roots.jl")
include("stationary_points.jl")
include("svectors.jl")
include("test_smiley.jl")
include("linear_eq.jl")
Expand Down
36 changes: 36 additions & 0 deletions test/stationary_points.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Test

import ForwardDiff

@testset "Stationary points" begin
f(xx) = ( (x, y) = xx; sin(x) * sin(y) )
gradf = xx -> ForwardDiff.gradient(f, xx)

XX = [interval(-5, 6), interval(-5, 6)]
tol = 1e-5

for method in newtonlike_methods
deriv = xx -> ForwardDiff.jacobian(gradf, xx)
test_newtonlike(gradf, deriv, XX, method, 25, tol)
end

# Rastrigin function
function rastrigin(x, y, A)
return 2A + x^2 - A*cos(2π*x) + y^2 - A*cos(2π*y)
end

f(xy) = rastrigin(xy..., 10)

∇f = X -> ForwardDiff.gradient(f, X)

L = 5.0
X = interval(-L, (L+1))
XX = [X, X]

rts = IntervalRootFinding.roots(∇f, XX ; contractor = Newton, abstol = 1e-5)
rts2 = IntervalRootFinding.roots(∇f, XX ; contractor = Krawczyk, abstol = 1e-5)

@test length(rts) == length(rts2) == 529
@test all(isunique, rts)
@test all(isunique, rts2)
end

0 comments on commit b9c3c7e

Please sign in to comment.