3 * Copyright (C) 2000 David Morrison
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.
24 #include <gsl/gsl_math.h>
25 #include <gsl/gsl_deriv.h>
26 #include <gsl/gsl_errno.h>
27 #include <gsl/gsl_test.h>
28 #include <gsl/gsl_ieee_utils.h>
31 f1 (double x, void *params)
37 df1 (double x, void *params)
43 f2 (double x, void *params)
56 df2 (double x, void *params)
60 return 1.5 * sqrt (x);
69 f3 (double x, void *params)
82 df3 (double x, void *params)
86 return -cos (1 / x) / (x * x);
95 f4 (double x, void *params)
101 df4 (double x, void *params)
103 return -2.0 * x * exp (-x * x);
107 f5 (double x, void *params)
113 df5 (double x, void *params)
119 f6 (double x, void *params)
125 df6 (double x, void *params)
127 return -1.0 / (x * x);
130 typedef int (deriv_fn) (const gsl_function * f, double x, double h, double * res, double *abserr);
133 test (deriv_fn * deriv, gsl_function * f, gsl_function * df, double x,
136 double result, abserr;
137 double expected = GSL_FN_EVAL (df, x);
138 (*deriv) (f, x, 1e-4, &result, &abserr);
140 gsl_test_abs (result, expected, GSL_MIN(1e-4,fabs(expected)) + GSL_DBL_EPSILON, desc);
142 if (abserr < fabs(result-expected))
144 gsl_test_factor (abserr, fabs(result-expected), 2, "%s error estimate", desc);
146 else if (result == expected || expected == 0.0)
148 gsl_test_abs (abserr, 0.0, 1e-6, "%s abserr", desc);
152 double d = fabs(result - expected);
153 gsl_test_abs (abserr, fabs(result-expected), 1e6*d, "%s abserr", desc);
160 gsl_function F1, DF1, F2, DF2, F3, DF3, F4, DF4, F5, DF5, F6, DF6;
162 gsl_ieee_env_setup ();
182 test (&gsl_deriv_central, &F1, &DF1, 1.0, "exp(x), x=1, central deriv");
183 test (&gsl_deriv_forward, &F1, &DF1, 1.0, "exp(x), x=1, forward deriv");
184 test (&gsl_deriv_backward, &F1, &DF1, 1.0, "exp(x), x=1, backward deriv");
186 test (&gsl_deriv_central, &F2, &DF2, 0.1, "x^(3/2), x=0.1, central deriv");
187 test (&gsl_deriv_forward, &F2, &DF2, 0.1, "x^(3/2), x=0.1, forward deriv");
188 test (&gsl_deriv_backward, &F2, &DF2, 0.1, "x^(3/2), x=0.1, backward deriv");
190 test (&gsl_deriv_central, &F3, &DF3, 0.45, "sin(1/x), x=0.45, central deriv");
191 test (&gsl_deriv_forward, &F3, &DF3, 0.45, "sin(1/x), x=0.45, forward deriv");
192 test (&gsl_deriv_backward, &F3, &DF3, 0.45, "sin(1/x), x=0.45, backward deriv");
194 test (&gsl_deriv_central, &F4, &DF4, 0.5, "exp(-x^2), x=0.5, central deriv");
195 test (&gsl_deriv_forward, &F4, &DF4, 0.5, "exp(-x^2), x=0.5, forward deriv");
196 test (&gsl_deriv_backward, &F4, &DF4, 0.5, "exp(-x^2), x=0.5, backward deriv");
198 test (&gsl_deriv_central, &F5, &DF5, 0.0, "x^2, x=0, central deriv");
199 test (&gsl_deriv_forward, &F5, &DF5, 0.0, "x^2, x=0, forward deriv");
200 test (&gsl_deriv_backward, &F5, &DF5, 0.0, "x^2, x=0, backward deriv");
202 test (&gsl_deriv_central, &F6, &DF6, 10.0, "1/x, x=10, central deriv");
203 test (&gsl_deriv_forward, &F6, &DF6, 10.0, "1/x, x=10, forward deriv");
204 test (&gsl_deriv_backward, &F6, &DF6, 10.0, "1/x, x=10, backward deriv");
206 exit (gsl_test_summary ());