| 1 | /* multiroots/fdjac.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"cxx . . $ `<file>/external/gsl/gsl__config.h`/ |
| 21 | #include "gsl_multiroots.h"cxx . . $ `<file>/external/gsl/gsl_multiroots.h`/ |
| 22 | |
| 23 | int |
| 24 | gsl_multiroot_fdjacobiancxx . . $ gsl_multiroot_fdjacobian(626cf04f7928da7f). (gsl_multiroot_functioncxx . . $ gsl_multiroot_function# * F, |
| 25 | const gsl_vectorcxx . . $ gsl_vector# * x, const gsl_vectorcxx . . $ gsl_vector# * f, |
| 26 | double epsrel, gsl_matrixcxx . . $ gsl_matrix# * jacobian) |
| 27 | { |
| 28 | const size_t n = x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.; |
| 29 | const size_t m = f->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.; |
| 30 | const size_t n1 = jacobian->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 31 | const size_t n2 = jacobian->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 32 | int status = 0; |
| 33 | |
| 34 | if (m != n1 || n != n2) |
| 35 | { |
| 36 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("function and jacobian are not conformant", GSL_EBADLEN); |
| 37 | } |
| 38 | |
| 39 | { |
| 40 | size_t i,j; |
| 41 | gsl_vectorcxx . . $ gsl_vector# *x1, *f1; |
| 42 | |
| 43 | x1 = gsl_vector_alloccxx . . $ gsl_vector_alloc(71d9e095f27e8ea9). (n); |
| 44 | |
| 45 | if (x1 == 0) |
| 46 | { |
| 47 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for x1 workspace", GSL_ENOMEM); |
| 48 | } |
| 49 | |
| 50 | f1 = gsl_vector_alloccxx . . $ gsl_vector_alloc(71d9e095f27e8ea9). (m); |
| 51 | |
| 52 | if (f1 == 0) |
| 53 | { |
| 54 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (x1); |
| 55 | |
| 56 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for f1 workspace", GSL_ENOMEM); |
| 57 | } |
| 58 | |
| 59 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (x1, x); /* copy x into x1 */ |
| 60 | |
| 61 | for (j = 0; j < n; j++) |
| 62 | { |
| 63 | double xj = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (x, j); |
| 64 | double dx = epsrel * fabs (xj); |
| 65 | |
| 66 | if (dx == 0) |
| 67 | { |
| 68 | dx = epsrel; |
| 69 | } |
| 70 | |
| 71 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (x1, j, xj + dx); |
| 72 | |
| 73 | { |
| 74 | int f_stat = GSL_MULTIROOT_FN_EVALcxx . . $ `external/gsl/gsl_multiroots.h:52:9`! (F, x1, f1); |
| 75 | |
| 76 | if (f_stat != GSL_SUCCESScxx . . $ GSL_SUCCESS.) |
| 77 | { |
| 78 | status = GSL_EBADFUNCcxx . . $ GSL_EBADFUNC.; |
| 79 | break; /* n.b. avoid memory leak for x1,f1 */ |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (x1, j, xj); |
| 84 | |
| 85 | for (i = 0; i < m; i++) |
| 86 | { |
| 87 | double g1 = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (f1, i); |
| 88 | double g0 = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (f, i); |
| 89 | gsl_matrix_setcxx . . $ gsl_matrix_set(b56d954874bd7b13). (jacobian, i, j, (g1 - g0) / dx); |
| 90 | } |
| 91 | |
| 92 | { |
| 93 | gsl_vector_viewcxx . . $ gsl_vector_view# col = gsl_matrix_columncxx . . $ gsl_matrix_column(127874ddf25d9e6a). (jacobian, j); |
| 94 | int null_col = gsl_vector_isnullcxx . . $ gsl_vector_isnull(3ce00cd98cc5e38). (&col.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_1#vector.); |
| 95 | /* if column is null, return an error - this may be due to |
| 96 | dx being too small. Try increasing epsrel */ |
| 97 | if (null_col) { |
| 98 | status = GSL_ESINGcxx . . $ GSL_ESING.; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (x1); |
| 104 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (f1); |
| 105 | } |
| 106 | |
| 107 | if (status) |
| 108 | return status; |
| 109 | else |
| 110 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 111 | } |