Skip to content

Commit

Permalink
Patched bisection newton method tests (#325)
Browse files Browse the repository at this point in the history
* test_newton_bisection_methods
* Test newton_method_bissection_method
* runtests_modified

---------

Co-authored-by: Alexis Vuille <alexis.vuille@uclouvain.be>
Co-authored-by: adrienbanse <adrien.banse@gmail.com>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent 69580d5 commit 1b9f0e4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dionysos.jl
Submodule Dionysos.jl added at de849a
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ include("./utils/rectangle.jl")
include("./utils/ellipsoid.jl")
include("./utils/scalar_functions.jl")
include("./utils/test_lazy_set_operations.jl")
include("./utils/optim/newton_method.jl")
include("./utils/optim/bisection.jl")

include("./domain/test_griddomain.jl")
include("./domain/test_general_domain.jl")
Expand Down
29 changes: 29 additions & 0 deletions test/utils/optim/bisection.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Test
using Dionysos
bisection = Dionysos.Utils.bisection

# Define the test function, its derivative, and second derivative
@testset "Bissection" begin
# Define a test function
test_function(x) = x^2 - 4
ϵ = 1e-6

# Call the bisection function
result = bisection(test_function; interval = [0, 4], δ = ϵ, verbose = false)
@test isapprox(result[1], -4.0; atol = ϵ) && 0 result[2] 4

result = bisection(test_function; interval = [0, 4], δ = ϵ, verbose = true)
@test isapprox(result[1], -4.0; atol = ϵ) && 0 result[2] 4

result = bisection(test_function; interval = [1, 3], δ = ϵ, verbose = true)
@test isapprox(result[1], -3.0; atol = ϵ) && 0 result[2] 4

result = bisection(
test_function;
interval = [-5, 4],
δ = ϵ,
verbose = false,
stopIfNegative = true,
)
@test (-5 result[2] 4) && abs(result[1] + 4.0) > ϵ && result[1] < 0
end
41 changes: 41 additions & 0 deletions test/utils/optim/newton_method.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Test
using Dionysos
newton_method = Dionysos.Utils.newton_method

# Define the test function, its derivative, and second derivative
@testset "EllipsoidBasics" begin
f(x) = x^2 - 4
df(x) = 2x
ddf(x) = 2

# Test parameters
interval = [1, 3]
x0 = 1.5
ϵ = 1e-6

# Call the newton_method function with the test parameters
result = newton_method(
f,
df,
ddf;
interval = interval,
x0 = x0,
ϵ = ϵ,
verbose = false,
stopIfNegative = false,
)
# Check the result
@test isapprox(result[1], -3.0; atol = ϵ) && interval[1] result[2] interval[2]

result = newton_method(
f,
df,
ddf;
interval = [0, 4],
x0 = x0,
ϵ = ϵ,
verbose = false,
stopIfNegative = false,
)
@test isapprox(result[1], -4.0; atol = ϵ) && 0 result[2] 4
end

0 comments on commit 1b9f0e4

Please sign in to comment.