3
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
3
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
24 #pragma implementation |
|
25 #endif |
|
26 |
238
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
3
|
29 #endif |
|
30 |
|
31 #include "NLEqn.h" |
465
|
32 #include "dMatrix.h" |
1847
|
33 #include "f77-fcn.h" |
227
|
34 #include "lo-error.h" |
4180
|
35 #include "quit.h" |
3
|
36 |
3507
|
37 typedef int (*hybrd1_fcn_ptr) (int*, double*, double*, int*); |
|
38 |
|
39 typedef int (*hybrj1_fcn_ptr) (int*, double*, double*, double*, int*, int*); |
|
40 |
|
41 extern "C" |
4552
|
42 { |
|
43 F77_RET_T |
|
44 F77_FUNC (hybrd1, HYBRD1) (hybrd1_fcn_ptr, const int&, double*, |
|
45 double*, const double&, int&, double*, |
|
46 const int&); |
|
47 |
|
48 |
|
49 F77_RET_T |
|
50 F77_FUNC (hybrj1, HYBRJ1) (hybrj1_fcn_ptr, const int&, double*, |
|
51 double*, double*, const int&, const |
|
52 double&, int&, double*, const int&); |
|
53 } |
3
|
54 |
1536
|
55 static NLFunc::nonlinear_fcn user_fun; |
|
56 static NLFunc::jacobian_fcn user_jac; |
3
|
57 |
|
58 // error handling |
|
59 |
|
60 void |
|
61 NLEqn::error (const char* msg) |
|
62 { |
227
|
63 (*current_liboctave_error_handler) ("fatal NLEqn error: %s", msg); |
3
|
64 } |
|
65 |
|
66 // Other operations |
|
67 |
|
68 int |
|
69 hybrd1_fcn (int *n, double *x, double *fvec, int *iflag) |
|
70 { |
4180
|
71 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
72 |
3
|
73 int nn = *n; |
1536
|
74 ColumnVector tmp_f (nn); |
|
75 ColumnVector tmp_x (nn); |
3
|
76 |
|
77 for (int i = 0; i < nn; i++) |
|
78 tmp_x.elem (i) = x[i]; |
|
79 |
|
80 tmp_f = (*user_fun) (tmp_x); |
|
81 |
253
|
82 if (tmp_f.length () == 0) |
|
83 *iflag = -1; |
|
84 else |
|
85 { |
1321
|
86 for (int i = 0; i < nn; i++) |
253
|
87 fvec[i] = tmp_f.elem (i); |
|
88 } |
3
|
89 |
4180
|
90 END_INTERRUPT_WITH_EXCEPTIONS; |
|
91 |
3
|
92 return 0; |
|
93 } |
|
94 |
|
95 int |
|
96 hybrj1_fcn (int *n, double *x, double *fvec, double *fjac, |
|
97 int *ldfjac, int *iflag) |
|
98 { |
4180
|
99 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
100 |
3
|
101 int nn = *n; |
1536
|
102 ColumnVector tmp_x (nn); |
3
|
103 |
|
104 for (int i = 0; i < nn; i++) |
|
105 tmp_x.elem (i) = x[i]; |
|
106 |
|
107 int flag = *iflag; |
|
108 if (flag == 1) |
|
109 { |
1536
|
110 ColumnVector tmp_f (nn); |
3
|
111 |
|
112 tmp_f = (*user_fun) (tmp_x); |
|
113 |
253
|
114 if (tmp_f.length () == 0) |
|
115 *iflag = -1; |
|
116 else |
|
117 { |
1321
|
118 for (int i = 0; i < nn; i++) |
253
|
119 fvec[i] = tmp_f.elem (i); |
|
120 } |
3
|
121 } |
|
122 else |
|
123 { |
|
124 Matrix tmp_fj (nn, nn); |
|
125 |
|
126 tmp_fj = (*user_jac) (tmp_x); |
|
127 |
253
|
128 if (tmp_fj.rows () == 0 || tmp_fj.columns () == 0) |
|
129 *iflag = -1; |
|
130 else |
|
131 { |
|
132 int ld = *ldfjac; |
|
133 for (int j = 0; j < nn; j++) |
1321
|
134 for (int i = 0; i < nn; i++) |
253
|
135 fjac[j*ld+i] = tmp_fj.elem (i, j); |
|
136 } |
3
|
137 } |
|
138 |
4180
|
139 END_INTERRUPT_WITH_EXCEPTIONS; |
|
140 |
3
|
141 return 0; |
|
142 } |
|
143 |
1536
|
144 ColumnVector |
3
|
145 NLEqn::solve (int& info) |
|
146 { |
1536
|
147 ColumnVector retval; |
|
148 |
1878
|
149 int n = x.capacity (); |
|
150 |
3
|
151 if (n == 0) |
227
|
152 { |
|
153 error ("equation set not initialized"); |
1536
|
154 return retval; |
227
|
155 } |
3
|
156 |
289
|
157 double tol = tolerance (); |
3
|
158 |
1937
|
159 retval = x; |
|
160 double *px = retval.fortran_vec (); |
3
|
161 |
|
162 user_fun = fun; |
|
163 user_jac = jac; |
|
164 |
465
|
165 if (jac) |
3
|
166 { |
1937
|
167 Array<double> fvec (n); |
|
168 double *pfvec = fvec.fortran_vec (); |
|
169 |
3
|
170 int lwa = (n*(n+13))/2; |
1937
|
171 Array<double> wa (lwa); |
|
172 double *pwa = wa.fortran_vec (); |
3
|
173 |
1937
|
174 Array<double> fjac (n*n); |
|
175 double *pfjac = fjac.fortran_vec (); |
3
|
176 |
1937
|
177 F77_XFCN (hybrj1, HYBRJ1, (hybrj1_fcn, n, px, pfvec, pfjac, n, |
|
178 tol, info, pwa, lwa)); |
|
179 |
3971
|
180 solution_status = info; |
|
181 |
1937
|
182 if (f77_exception_encountered) |
|
183 (*current_liboctave_error_handler) ("unrecoverable error in hybrj1"); |
3
|
184 } |
465
|
185 else |
|
186 { |
1937
|
187 Array<double> fvec (n); |
|
188 double *pfvec = fvec.fortran_vec (); |
465
|
189 |
1937
|
190 int lwa = (n*(3*n+13))/2; |
|
191 Array<double> wa (lwa); |
|
192 double *pwa = wa.fortran_vec (); |
465
|
193 |
1937
|
194 F77_XFCN (hybrd1, HYBRD1, (hybrd1_fcn, n, px, pfvec, tol, info, |
|
195 pwa, lwa)); |
|
196 |
3971
|
197 solution_status = info; |
|
198 |
1937
|
199 if (f77_exception_encountered) |
|
200 (*current_liboctave_error_handler) ("unrecoverable error in hybrd1"); |
465
|
201 } |
3
|
202 |
|
203 return retval; |
|
204 } |
|
205 |
3971
|
206 std::string |
|
207 NLEqn::error_message (void) const |
|
208 { |
|
209 std::string retval; |
|
210 |
|
211 std::string prefix; |
|
212 |
|
213 int info = solution_status; |
|
214 if (info < 0) |
|
215 info = -info; |
|
216 |
|
217 switch (info) |
|
218 { |
|
219 case 0: |
|
220 retval = "improper input parameters"; |
|
221 break; |
|
222 |
|
223 case 1: |
|
224 retval = "solution converged within specified tolerance"; |
|
225 break; |
|
226 |
|
227 case 2: |
|
228 retval = "number of function calls exceeded limit"; |
|
229 break; |
|
230 |
|
231 case 3: |
|
232 retval = "no further improvement possible (tolerance may be too small)"; |
|
233 break; |
|
234 |
|
235 case 4: |
|
236 retval = "iteration is not making good progress"; |
|
237 break; |
|
238 |
|
239 default: |
|
240 retval = "unknown error state"; |
|
241 break; |
|
242 } |
|
243 |
|
244 if (solution_status < 0) |
|
245 retval = std::string ("user requested termination: ") + retval; |
|
246 |
|
247 return retval; |
|
248 } |
|
249 |
3
|
250 /* |
|
251 ;;; Local Variables: *** |
|
252 ;;; mode: C++ *** |
|
253 ;;; End: *** |
|
254 */ |