3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <gsl/gsl_math.h>
23 #include <gsl/gsl_test.h>
24 #include <gsl/gsl_roots.h>
25 #include <gsl/gsl_errno.h>
26 #include <gsl/gsl_ieee_utils.h>
31 /* stopping parameters */
32 const double EPSREL = (10 * GSL_DBL_EPSILON);
33 const double EPSABS = (10 * GSL_DBL_EPSILON);
34 const unsigned int MAX_ITERATIONS = 150;
36 void my_error_handler (const char *reason, const char *file,
39 #define WITHIN_TOL(a, b, epsrel, epsabs) \
40 ((fabs((a) - (b)) < (epsrel) * GSL_MIN(fabs(a), fabs(b)) + (epsabs)))
45 gsl_function F_sin, F_cos, F_func1, F_func2, F_func3, F_func4,
48 gsl_function_fdf FDF_sin, FDF_cos, FDF_func1, FDF_func2, FDF_func3, FDF_func4,
51 const gsl_root_fsolver_type * fsolver[4] ;
52 const gsl_root_fdfsolver_type * fdfsolver[4] ;
54 const gsl_root_fsolver_type ** T;
55 const gsl_root_fdfsolver_type ** S;
59 fsolver[0] = gsl_root_fsolver_bisection;
60 fsolver[1] = gsl_root_fsolver_brent;
61 fsolver[2] = gsl_root_fsolver_falsepos;
64 fdfsolver[0] = gsl_root_fdfsolver_newton;
65 fdfsolver[1] = gsl_root_fdfsolver_secant;
66 fdfsolver[2] = gsl_root_fdfsolver_steffenson;
69 F_sin = create_function (sin_f) ;
70 F_cos = create_function (cos_f) ;
71 F_func1 = create_function (func1) ;
72 F_func2 = create_function (func2) ;
73 F_func3 = create_function (func3) ;
74 F_func4 = create_function (func4) ;
75 F_func5 = create_function (func5) ;
76 F_func6 = create_function (func6) ;
78 FDF_sin = create_fdf (sin_f, sin_df, sin_fdf) ;
79 FDF_cos = create_fdf (cos_f, cos_df, cos_fdf) ;
80 FDF_func1 = create_fdf (func1, func1_df, func1_fdf) ;
81 FDF_func2 = create_fdf (func2, func2_df, func2_fdf) ;
82 FDF_func3 = create_fdf (func3, func3_df, func3_fdf) ;
83 FDF_func4 = create_fdf (func4, func4_df, func4_fdf) ;
84 FDF_func5 = create_fdf (func5, func5_df, func5_fdf) ;
85 FDF_func6 = create_fdf (func6, func6_df, func6_fdf) ;
87 gsl_set_error_handler (&my_error_handler);
89 for (T = fsolver ; *T != 0 ; T++)
91 test_f (*T, "sin(x) [3, 4]", &F_sin, 3.0, 4.0, M_PI);
92 test_f (*T, "sin(x) [-4, -3]", &F_sin, -4.0, -3.0, -M_PI);
93 test_f (*T, "sin(x) [-1/3, 1]", &F_sin, -1.0 / 3.0, 1.0, 0.0);
94 test_f (*T, "cos(x) [0, 3]", &F_cos, 0.0, 3.0, M_PI / 2.0);
95 test_f (*T, "cos(x) [-3, 0]", &F_cos, -3.0, 0.0, -M_PI / 2.0);
96 test_f (*T, "x^20 - 1 [0.1, 2]", &F_func1, 0.1, 2.0, 1.0);
97 test_f (*T, "sqrt(|x|)*sgn(x)", &F_func2, -1.0 / 3.0, 1.0, 0.0);
98 test_f (*T, "x^2 - 1e-8 [0, 1]", &F_func3, 0.0, 1.0, sqrt (1e-8));
99 test_f (*T, "x exp(-x) [-1/3, 2]", &F_func4, -1.0 / 3.0, 2.0, 0.0);
100 test_f (*T, "(x - 1)^7 [0.9995, 1.0002]", &F_func6, 0.9995, 1.0002, 1.0);
102 test_f_e (*T, "invalid range check [4, 0]", &F_sin, 4.0, 0.0, M_PI);
103 test_f_e (*T, "invalid range check [1, 1]", &F_sin, 1.0, 1.0, M_PI);
104 test_f_e (*T, "invalid range check [0.1, 0.2]", &F_sin, 0.1, 0.2, M_PI);
107 for (S = fdfsolver ; *S != 0 ; S++)
109 test_fdf (*S,"sin(x) {3.4}", &FDF_sin, 3.4, M_PI);
110 test_fdf (*S,"sin(x) {-3.3}", &FDF_sin, -3.3, -M_PI);
111 test_fdf (*S,"sin(x) {0.5}", &FDF_sin, 0.5, 0.0);
112 test_fdf (*S,"cos(x) {0.6}", &FDF_cos, 0.6, M_PI / 2.0);
113 test_fdf (*S,"cos(x) {-2.5}", &FDF_cos, -2.5, -M_PI / 2.0);
114 test_fdf (*S,"x^{20} - 1 {0.9}", &FDF_func1, 0.9, 1.0);
115 test_fdf (*S,"x^{20} - 1 {1.1}", &FDF_func1, 1.1, 1.0);
116 test_fdf (*S,"sqrt(|x|)*sgn(x) {1.001}", &FDF_func2, 0.001, 0.0);
117 test_fdf (*S,"x^2 - 1e-8 {1}", &FDF_func3, 1.0, sqrt (1e-8));
118 test_fdf (*S,"x exp(-x) {-2}", &FDF_func4, -2.0, 0.0);
119 test_fdf_e (*S,"max iterations x -> +Inf, x exp(-x) {2}", &FDF_func4, 2.0, 0.0);
120 test_fdf_e (*S,"max iterations x -> -Inf, 1/(1 + exp(-x)) {0}", &FDF_func5, 0.0, 0.0);
123 test_fdf (gsl_root_fdfsolver_steffenson,
124 "(x - 1)^7 {0.9}", &FDF_func6, 0.9, 1.0);
126 /* now summarize the results */
128 exit (gsl_test_summary ());
132 /* Using gsl_root_bisection, find the root of the function pointed to by f,
133 using the interval [lower_bound, upper_bound]. Check if f succeeded and
134 that it was accurate enough. */
137 test_f (const gsl_root_fsolver_type * T, const char * description, gsl_function *f,
138 double lower_bound, double upper_bound, double correct_root)
141 size_t iterations = 0;
143 double x_lower, x_upper;
144 gsl_root_fsolver * s;
146 x_lower = lower_bound;
147 x_upper = upper_bound;
149 s = gsl_root_fsolver_alloc(T);
150 gsl_root_fsolver_set(s, f, x_lower, x_upper) ;
156 gsl_root_fsolver_iterate (s);
158 r = gsl_root_fsolver_root(s);
160 a = gsl_root_fsolver_x_lower(s);
161 b = gsl_root_fsolver_x_upper(s);
164 gsl_test (GSL_FAILURE, "interval is invalid (%g,%g)", a, b);
167 gsl_test (GSL_FAILURE, "r lies outside interval %g (%g,%g)", r, a, b);
169 status = gsl_root_test_interval (a,b, EPSABS, EPSREL);
171 while (status == GSL_CONTINUE && iterations < MAX_ITERATIONS);
174 gsl_test (status, "%s, %s (%g obs vs %g expected) ",
175 gsl_root_fsolver_name(s), description,
176 gsl_root_fsolver_root(s), correct_root);
178 if (iterations == MAX_ITERATIONS)
180 gsl_test (GSL_FAILURE, "exceeded maximum number of iterations");
183 /* check the validity of the returned result */
185 if (!WITHIN_TOL (r, correct_root, EPSREL, EPSABS))
187 gsl_test (GSL_FAILURE, "incorrect precision (%g obs vs %g expected)",
192 gsl_root_fsolver_free(s);
196 test_f_e (const gsl_root_fsolver_type * T,
197 const char * description, gsl_function *f,
198 double lower_bound, double upper_bound, double correct_root)
201 size_t iterations = 0;
202 double x_lower, x_upper;
203 gsl_root_fsolver * s;
205 x_lower = lower_bound;
206 x_upper = upper_bound;
208 s = gsl_root_fsolver_alloc(T);
209 status = gsl_root_fsolver_set(s, f, x_lower, x_upper) ;
211 gsl_test (status != GSL_EINVAL, "%s (set), %s", T->name, description);
213 if (status == GSL_EINVAL)
215 gsl_root_fsolver_free(s);
222 gsl_root_fsolver_iterate (s);
223 x_lower = gsl_root_fsolver_x_lower(s);
224 x_upper = gsl_root_fsolver_x_lower(s);
225 status = gsl_root_test_interval (x_lower, x_upper,
228 while (status == GSL_CONTINUE && iterations < MAX_ITERATIONS);
230 gsl_test (!status, "%s, %s", gsl_root_fsolver_name(s), description,
231 gsl_root_fsolver_root(s) - correct_root);
233 gsl_root_fsolver_free(s);
237 test_fdf (const gsl_root_fdfsolver_type * T, const char * description,
238 gsl_function_fdf *fdf, double root, double correct_root)
241 size_t iterations = 0;
244 gsl_root_fdfsolver * s = gsl_root_fdfsolver_alloc(T);
245 gsl_root_fdfsolver_set (s, fdf, root) ;
250 prev = gsl_root_fdfsolver_root(s);
251 gsl_root_fdfsolver_iterate (s);
252 status = gsl_root_test_delta(gsl_root_fdfsolver_root(s), prev,
255 while (status == GSL_CONTINUE && iterations < MAX_ITERATIONS);
257 gsl_test (status, "%s, %s (%g obs vs %g expected) ",
258 gsl_root_fdfsolver_name(s), description,
259 gsl_root_fdfsolver_root(s), correct_root);
261 if (iterations == MAX_ITERATIONS)
263 gsl_test (GSL_FAILURE, "exceeded maximum number of iterations");
266 /* check the validity of the returned result */
268 if (!WITHIN_TOL (gsl_root_fdfsolver_root(s), correct_root,
271 gsl_test (GSL_FAILURE, "incorrect precision (%g obs vs %g expected)",
272 gsl_root_fdfsolver_root(s), correct_root);
275 gsl_root_fdfsolver_free(s);
279 test_fdf_e (const gsl_root_fdfsolver_type * T,
280 const char * description, gsl_function_fdf *fdf,
281 double root, double correct_root)
284 size_t iterations = 0;
287 gsl_root_fdfsolver * s = gsl_root_fdfsolver_alloc(T);
288 status = gsl_root_fdfsolver_set (s, fdf, root) ;
290 gsl_test (status, "%s (set), %s", T->name, description);
295 prev = gsl_root_fdfsolver_root(s);
296 gsl_root_fdfsolver_iterate (s);
297 status = gsl_root_test_delta(gsl_root_fdfsolver_root(s), prev,
300 while (status == GSL_CONTINUE && iterations < MAX_ITERATIONS);
302 gsl_test (!status, "%s, %s", gsl_root_fdfsolver_name(s),
303 description, gsl_root_fdfsolver_root(s) - correct_root);
304 gsl_root_fdfsolver_free(s);
308 my_error_handler (const char *reason, const char *file, int line, int err)
311 printf ("(caught [%s:%d: %s (%d)])\n", file, line, reason, err);