| 1 | /* multiroots/convergence.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | #include "gsl__config.h" |
| 21 | #include "gsl_math.h" |
| 22 | #include "gsl_errno.h" |
| 23 | #include "gsl_multiroots.h" |
| 24 | |
| 25 | int |
| 26 | gsl_multiroot_test_delta (const gsl_vector * dx, const gsl_vector * x, |
| 27 | double epsabs, double epsrel) |
| 28 | { |
| 29 | size_t i; |
| 30 | int ok = 1; |
| 31 | const size_t n = x->size ; |
| 32 | |
| 33 | if (epsrel < 0.0) |
| 34 | { |
| 35 | GSL_ERROR ("relative tolerance is negative", GSL_EBADTOL); |
| 36 | } |
| 37 | |
| 38 | for (i = 0 ; i < n ; i++) |
| 39 | { |
| 40 | double xi = gsl_vector_get(x,i); |
| 41 | double dxi = gsl_vector_get(dx,i); |
| 42 | double tolerance = epsabs + epsrel * fabs(xi) ; |
| 43 | |
| 44 | if (fabs(dxi) < tolerance) |
| 45 | { |
| 46 | ok = 1; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | ok = 0; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (ok) |
| 56 | return GSL_SUCCESS ; |
| 57 | |
| 58 | return GSL_CONTINUE; |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | gsl_multiroot_test_residual (const gsl_vector * f, double epsabs) |
| 63 | { |
| 64 | size_t i; |
| 65 | |
| 66 | double residual = 0; |
| 67 | |
| 68 | const size_t n = f->size; |
| 69 | |
| 70 | if (epsabs < 0.0) |
| 71 | { |
| 72 | GSL_ERROR ("absolute tolerance is negative", GSL_EBADTOL); |
| 73 | } |
| 74 | |
| 75 | for (i = 0 ; i < n ; i++) |
| 76 | { |
| 77 | double fi = gsl_vector_get(f, i); |
| 78 | |
| 79 | residual += fabs(fi); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | if (residual < epsabs) |
| 84 | { |
| 85 | return GSL_SUCCESS; |
| 86 | } |
| 87 | |
| 88 | return GSL_CONTINUE ; |
| 89 | } |
| 90 |