-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
test_zeros.jl
49 lines (37 loc) · 1.28 KB
/
test_zeros.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# NOTE: these tests were taken from the scipy.optimize test suite
#=
- f1 is a simple quadratic with roots 0 and 1
- f2 is a symmetric parabola, x^2 - 1
- f3 is a quartic polynomial with large hump in interval
- f4 is step function with a discontinuity at 1
- f5 is a hyperbola with vertical asymptote at 1
- f6 has random values positive to left of 1, negative to right
Of course these are not real problems. They just test how the 'good' solvers
behave in bad circumstances where bisection is really the best. A good solver
should not be much worse than bisection in such circumstance, while being faster
for smooth monotone sorts of functions.
=#
f1(x) = x*(x-1)
f2(x) = x^2 - 1
f3(x) = x * (x-1) * (x-2) * (x-3.)
f4(x) = x > 1 ? 1.0 + .1*x :
x < 1 ? -1.0 + .1*x :
0.0
f5(x) = x != 1 ? 1.0/(1. - x) : 0.0
f6(x) = x > 1 ? rand() :
x < 1 ? -rand() :
0.0
funcs = [f1, f2, f3, f4, f5, f6]
@testset "Testing univariate root finders" begin
a = 0.5
b = sqrt(3)
for root_finder in [bisect, brent, brenth, ridder]
nm = string(root_finder)
@testset "testing $root_finder" begin
for f in funcs
root = root_finder(f, a, b; xtol=0.1e-12)
@test abs(root - 1.0) < 1e-11
end
end
end
end