gsl_multiroots__convergence.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "gsl__config.h"
  20. #include "gsl_math.h"
  21. #include "gsl_errno.h"
  22. #include "gsl_multiroots.h"
  23. int
  24. gsl_multiroot_test_delta (const gsl_vector * dx, const gsl_vector * x,
  25. double epsabs, double epsrel)
  26. {
  27. size_t i;
  28. int ok = 1;
  29. const size_t n = x->size ;
  30. if (epsrel < 0.0)
  31. {
  32. GSL_ERROR ("relative tolerance is negative", GSL_EBADTOL);
  33. }
  34. for (i = 0 ; i < n ; i++)
  35. {
  36. double xi = gsl_vector_get(x,i);
  37. double dxi = gsl_vector_get(dx,i);
  38. double tolerance = epsabs + epsrel * fabs(xi) ;
  39. if (fabs(dxi) < tolerance)
  40. {
  41. ok = 1;
  42. }
  43. else
  44. {
  45. ok = 0;
  46. break;
  47. }
  48. }
  49. if (ok)
  50. return GSL_SUCCESS ;
  51. return GSL_CONTINUE;
  52. }
  53. int
  54. gsl_multiroot_test_residual (const gsl_vector * f, double epsabs)
  55. {
  56. size_t i;
  57. double residual = 0;
  58. const size_t n = f->size;
  59. if (epsabs < 0.0)
  60. {
  61. GSL_ERROR ("absolute tolerance is negative", GSL_EBADTOL);
  62. }
  63. for (i = 0 ; i < n ; i++)
  64. {
  65. double fi = gsl_vector_get(f, i);
  66. residual += fabs(fi);
  67. }
  68. if (residual < epsabs)
  69. {
  70. return GSL_SUCCESS;
  71. }
  72. return GSL_CONTINUE ;
  73. }