gsl_multiroots__newton.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* multiroots/newton.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 <stddef.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <float.h>
  25. #include "gsl_math.h"
  26. #include "gsl_errno.h"
  27. #include "gsl_multiroots.h"
  28. #include "gsl_linalg.h"
  29. typedef struct
  30. {
  31. gsl_matrix * lu;
  32. gsl_permutation * permutation;
  33. }
  34. newton_state_t;
  35. static int newton_alloc (void * vstate, size_t n);
  36. static int newton_set (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
  37. static int newton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
  38. static void newton_free (void * vstate);
  39. static int
  40. newton_alloc (void * vstate, size_t n)
  41. {
  42. newton_state_t * state = (newton_state_t *) vstate;
  43. gsl_permutation * p;
  44. gsl_matrix * m;
  45. m = gsl_matrix_calloc (n,n);
  46. if (m == 0)
  47. {
  48. GSL_ERROR ("failed to allocate space for lu", GSL_ENOMEM);
  49. }
  50. state->lu = m ;
  51. p = gsl_permutation_calloc (n);
  52. if (p == 0)
  53. {
  54. gsl_matrix_free(m);
  55. GSL_ERROR ("failed to allocate space for permutation", GSL_ENOMEM);
  56. }
  57. state->permutation = p ;
  58. return GSL_SUCCESS;
  59. }
  60. static int
  61. newton_set (void * vstate, gsl_multiroot_function_fdf * FDF, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
  62. {
  63. newton_state_t * state = (newton_state_t *) vstate;
  64. size_t i, n = FDF->n ;
  65. state = 0 ; /* avoid warnings about unused parameters */
  66. GSL_MULTIROOT_FN_EVAL_F_DF (FDF, x, f, J);
  67. for (i = 0; i < n; i++)
  68. {
  69. gsl_vector_set (dx, i, 0.0);
  70. }
  71. return GSL_SUCCESS;
  72. }
  73. static int
  74. newton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
  75. {
  76. newton_state_t * state = (newton_state_t *) vstate;
  77. int signum;
  78. size_t i;
  79. size_t n = fdf->n ;
  80. gsl_matrix_memcpy (state->lu, J);
  81. gsl_linalg_LU_decomp (state->lu, state->permutation, &signum);
  82. gsl_linalg_LU_solve (state->lu, state->permutation, f, dx);
  83. for (i = 0; i < n; i++)
  84. {
  85. double e = gsl_vector_get (dx, i);
  86. double y = gsl_vector_get (x, i);
  87. gsl_vector_set (dx, i, -e);
  88. gsl_vector_set (x, i, y - e);
  89. }
  90. {
  91. int status = GSL_MULTIROOT_FN_EVAL_F_DF (fdf, x, f, J);
  92. if (status != GSL_SUCCESS)
  93. {
  94. return GSL_EBADFUNC;
  95. }
  96. }
  97. return GSL_SUCCESS;
  98. }
  99. static void
  100. newton_free (void * vstate)
  101. {
  102. newton_state_t * state = (newton_state_t *) vstate;
  103. gsl_matrix_free(state->lu);
  104. gsl_permutation_free(state->permutation);
  105. }
  106. static const gsl_multiroot_fdfsolver_type newton_type =
  107. {"newton", /* name */
  108. sizeof (newton_state_t),
  109. &newton_alloc,
  110. &newton_set,
  111. &newton_iterate,
  112. &newton_free};
  113. const gsl_multiroot_fdfsolver_type * gsl_multiroot_fdfsolver_newton = &newton_type;