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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
require 'test_helper'
class RootsTest < GSL::TestCase
EPSREL = 10.0 * GSL::DBL_EPSILON
EPSABS = 10.0 * GSL::DBL_EPSILON
MAX_ITERATIONS = 150
def _test_f(type, desc, f, lower, upper, correct)
s = GSL::Root::FSolver.alloc(type)
s.set(f, lower, upper)
status = iter = 0
begin
iter += 1
s.iterate
r = s.root
a = s.x_lower
b = s.x_upper
refute a > b, "interval is invalid (#{a},#{b})"
refute r < a || r > b, "r lies outside interval #{r} (#{a},#{b})"
status = GSL::Root.test_interval(a, b, EPSABS, EPSREL)
end while status == GSL::CONTINUE && iter < MAX_ITERATIONS
assert status.zero?, "#{s.name}, #{desc} (#{s.root} obs vs #{correct} expected)"
assert iter <= MAX_ITERATIONS, 'exceeded maximum number of iterations'
assert_tol r, correct, "incorrect precision (#{r} obs vs #{correct} expected)"
end
def _test_fdf(type, desc, fdf, root, correct)
s = GSL::Root::FdfSolver.alloc(type)
s.set(fdf, root)
status = iter = 0
begin
iter += 1
prev = s.root
begin
s.iterate
rescue GSL::ERROR::EBADFUNC
raise unless type == 'secant'
end
status = GSL::Root.test_delta(s.root, prev, EPSABS, EPSREL)
end while status == GSL::CONTINUE && iter < MAX_ITERATIONS
assert status.zero?, "#{s.name} #{desc} (#{s.root} obs vs #{correct} expected)"
assert iter <= MAX_ITERATIONS, 'exceeded maximum number of iterations'
assert_tol r = s.root, correct, "incorrect precision (#{r} obs vs #{correct} expected)"
end
def setup
@func = GSL::Function.alloc { |x| GSL.pow(x, 20.0) - 1 }
@fdf = GSL::Function_fdf.alloc(@func.f, lambda { |x| 20.0 * GSL.pow(x, 19.0) })
end
%w[bisection brent falsepos].each { |type|
define_method("test_f_#{type}") {
_test_f(type, 'x^20 - 1 [0.1, 2]', @func, 0.1, 2.0, 1.0)
}
}
%w[newton secant steffenson].each { |type|
define_method("test_fdf_#{type}") {
_test_fdf(type, 'x^{20} - 1 {0.9}', @fdf, 0.9, 1.0)
}
}
end
|