gsl_multifit__convergence.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* multifit/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_multifit_nlin.h"
  23. int
  24. gsl_multifit_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_multifit_test_gradient (const gsl_vector * g, double epsabs)
  55. {
  56. size_t i;
  57. double residual = 0;
  58. const size_t n = g->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 gi = gsl_vector_get(g, i);
  66. residual += fabs(gi);
  67. }
  68. if (residual < epsabs)
  69. {
  70. return GSL_SUCCESS;
  71. }
  72. return GSL_CONTINUE ;
  73. }