3
|
1 // NLEqn.cc -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
3
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
238
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
3
|
30 #endif |
|
31 |
|
32 #include <float.h> |
465
|
33 #include <math.h> |
238
|
34 |
3
|
35 #include "NLEqn.h" |
465
|
36 #include "dMatrix.h" |
3
|
37 #include "f77-uscore.h" |
227
|
38 #include "lo-error.h" |
3
|
39 |
|
40 extern "C" |
|
41 { |
1253
|
42 int F77_FCN (hybrd1, HYBRD1) (int (*)(int*, double*, double*, int*), |
|
43 const int&, double*, double*, |
|
44 const double&, int&, double*, |
|
45 const int&); |
3
|
46 |
1254
|
47 int F77_FCN (hybrj1, HYBRJ1) (int (*)(int*, double*, double*, |
|
48 double*, int*, int*), |
|
49 const int&, double*, double*, double*, |
|
50 const int&, const double&, int&, |
|
51 double*, const int&); |
3
|
52 } |
|
53 |
|
54 static nonlinear_fcn user_fun; |
|
55 static jacobian_fcn user_jac; |
|
56 |
|
57 // error handling |
|
58 |
|
59 void |
|
60 NLEqn::error (const char* msg) |
|
61 { |
227
|
62 (*current_liboctave_error_handler) ("fatal NLEqn error: %s", msg); |
3
|
63 } |
|
64 |
|
65 // Constructors |
|
66 |
645
|
67 NLEqn::NLEqn (void) : NLFunc (), n (0), x () { } |
3
|
68 |
|
69 NLEqn::NLEqn (const Vector& xvec, const NLFunc f) |
645
|
70 : NLFunc (f), n (xvec.capacity ()), x (xvec) { } |
3
|
71 |
645
|
72 NLEqn::NLEqn (const NLEqn& a) : NLFunc (a.fun, a.jac), n (a.n), x (a.x) { } |
3
|
73 |
|
74 void |
|
75 NLEqn::resize (int nn) |
|
76 { |
|
77 if (n != nn) |
|
78 { |
|
79 n = nn; |
|
80 x.resize (n); |
|
81 } |
|
82 } |
|
83 |
|
84 int |
|
85 NLEqn::size (void) const |
|
86 { |
|
87 return n; |
|
88 } |
|
89 |
|
90 // Assignment |
|
91 |
|
92 NLEqn& |
|
93 NLEqn::operator = (const NLEqn& a) |
|
94 { |
|
95 fun = a.fun; |
|
96 jac = a.jac; |
|
97 x = a.n; |
|
98 |
|
99 return *this; |
|
100 } |
|
101 |
|
102 Vector |
|
103 NLEqn::states (void) const |
|
104 { |
|
105 return x; |
|
106 } |
|
107 |
|
108 void |
|
109 NLEqn::set_states (const Vector& xvec) |
|
110 { |
|
111 if (xvec.capacity () != n) |
227
|
112 { |
|
113 error ("dimension error"); |
|
114 return; |
|
115 } |
3
|
116 |
|
117 x = xvec; |
|
118 } |
|
119 |
|
120 // Other operations |
|
121 |
|
122 Vector |
|
123 NLEqn::solve (const Vector& xvec) |
|
124 { |
|
125 set_states (xvec); |
|
126 int info; |
|
127 return solve (info); |
|
128 } |
|
129 |
|
130 Vector |
|
131 NLEqn::solve (const Vector& xvec, int& info) |
|
132 { |
|
133 set_states (xvec); |
|
134 return solve (info); |
|
135 } |
|
136 |
|
137 Vector |
|
138 NLEqn::solve (void) |
|
139 { |
|
140 int info; |
|
141 return solve (info); |
|
142 } |
|
143 |
|
144 int |
|
145 hybrd1_fcn (int *n, double *x, double *fvec, int *iflag) |
|
146 { |
|
147 int nn = *n; |
|
148 Vector tmp_f (nn); |
|
149 Vector tmp_x (nn); |
|
150 |
|
151 for (int i = 0; i < nn; i++) |
|
152 tmp_x.elem (i) = x[i]; |
|
153 |
|
154 tmp_f = (*user_fun) (tmp_x); |
|
155 |
253
|
156 if (tmp_f.length () == 0) |
|
157 *iflag = -1; |
|
158 else |
|
159 { |
1321
|
160 for (int i = 0; i < nn; i++) |
253
|
161 fvec[i] = tmp_f.elem (i); |
|
162 } |
3
|
163 |
|
164 return 0; |
|
165 } |
|
166 |
|
167 int |
|
168 hybrj1_fcn (int *n, double *x, double *fvec, double *fjac, |
|
169 int *ldfjac, int *iflag) |
|
170 { |
|
171 int nn = *n; |
|
172 Vector tmp_x (nn); |
|
173 |
|
174 for (int i = 0; i < nn; i++) |
|
175 tmp_x.elem (i) = x[i]; |
|
176 |
|
177 int flag = *iflag; |
|
178 if (flag == 1) |
|
179 { |
|
180 Vector tmp_f (nn); |
|
181 |
|
182 tmp_f = (*user_fun) (tmp_x); |
|
183 |
253
|
184 if (tmp_f.length () == 0) |
|
185 *iflag = -1; |
|
186 else |
|
187 { |
1321
|
188 for (int i = 0; i < nn; i++) |
253
|
189 fvec[i] = tmp_f.elem (i); |
|
190 } |
3
|
191 } |
|
192 else |
|
193 { |
|
194 Matrix tmp_fj (nn, nn); |
|
195 |
|
196 tmp_fj = (*user_jac) (tmp_x); |
|
197 |
253
|
198 if (tmp_fj.rows () == 0 || tmp_fj.columns () == 0) |
|
199 *iflag = -1; |
|
200 else |
|
201 { |
|
202 int ld = *ldfjac; |
|
203 for (int j = 0; j < nn; j++) |
1321
|
204 for (int i = 0; i < nn; i++) |
253
|
205 fjac[j*ld+i] = tmp_fj.elem (i, j); |
|
206 } |
3
|
207 } |
|
208 |
|
209 return 0; |
|
210 } |
|
211 |
|
212 Vector |
|
213 NLEqn::solve (int& info) |
|
214 { |
|
215 if (n == 0) |
227
|
216 { |
|
217 error ("equation set not initialized"); |
|
218 return Vector (); |
|
219 } |
3
|
220 |
289
|
221 double tol = tolerance (); |
3
|
222 |
|
223 double *fvec = new double [n]; |
|
224 double *px = new double [n]; |
|
225 for (int i = 0; i < n; i++) |
|
226 px[i] = x.elem (i); |
|
227 |
|
228 user_fun = fun; |
|
229 user_jac = jac; |
|
230 |
465
|
231 if (jac) |
3
|
232 { |
|
233 int lwa = (n*(n+13))/2; |
|
234 double *wa = new double [lwa]; |
|
235 double *fjac = new double [n*n]; |
|
236 |
1253
|
237 F77_FCN (hybrj1, HYBRJ1) (hybrj1_fcn, n, px, fvec, fjac, n, tol, |
|
238 info, wa, lwa); |
3
|
239 |
|
240 delete [] wa; |
|
241 delete [] fjac; |
|
242 } |
465
|
243 else |
|
244 { |
|
245 int lwa = (n*(3*n+13))/2; |
|
246 double *wa = new double [lwa]; |
|
247 |
1253
|
248 F77_FCN (hybrd1, HYBRD1) (hybrd1_fcn, n, px, fvec, tol, info, |
|
249 wa, lwa); |
465
|
250 |
|
251 delete [] wa; |
|
252 } |
3
|
253 |
253
|
254 Vector retval; |
|
255 |
|
256 if (info >= 0) |
|
257 { |
|
258 retval.resize (n); |
3
|
259 |
1321
|
260 for (int i = 0; i < n; i++) |
253
|
261 retval.elem (i) = px[i]; |
|
262 } |
3
|
263 |
657
|
264 delete [] fvec; |
|
265 delete [] px; |
|
266 |
3
|
267 return retval; |
|
268 } |
|
269 |
289
|
270 NLEqn_options::NLEqn_options (void) |
|
271 { |
|
272 init (); |
|
273 } |
|
274 |
|
275 NLEqn_options::NLEqn_options (const NLEqn_options& opt) |
|
276 { |
|
277 copy (opt); |
|
278 } |
|
279 |
|
280 NLEqn_options& |
|
281 NLEqn_options::operator = (const NLEqn_options& opt) |
|
282 { |
|
283 if (this != &opt) |
|
284 copy (opt); |
|
285 |
|
286 return *this; |
|
287 } |
|
288 |
|
289 NLEqn_options::~NLEqn_options (void) |
|
290 { |
|
291 } |
|
292 |
|
293 void |
|
294 NLEqn_options::init (void) |
|
295 { |
|
296 double sqrt_eps = sqrt (DBL_EPSILON); |
|
297 x_tolerance = sqrt_eps; |
|
298 } |
|
299 |
|
300 void |
|
301 NLEqn_options::copy (const NLEqn_options& opt) |
|
302 { |
|
303 x_tolerance = opt.x_tolerance; |
|
304 } |
|
305 |
|
306 void |
|
307 NLEqn_options::set_default_options (void) |
|
308 { |
|
309 init (); |
|
310 } |
|
311 |
|
312 void |
|
313 NLEqn_options::set_tolerance (double val) |
|
314 { |
|
315 x_tolerance = (val > 0.0) ? val : sqrt (DBL_EPSILON); |
|
316 } |
|
317 |
|
318 double |
|
319 NLEqn_options::tolerance (void) |
|
320 { |
|
321 return x_tolerance; |
|
322 } |
|
323 |
3
|
324 /* |
|
325 ;;; Local Variables: *** |
|
326 ;;; mode: C++ *** |
|
327 ;;; page-delimiter: "^/\\*" *** |
|
328 ;;; End: *** |
|
329 */ |