3
|
1 // NLEqn.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
238
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
3
|
26 #endif |
|
27 |
|
28 #include <float.h> |
238
|
29 |
3
|
30 #include "NLEqn.h" |
|
31 #include "f77-uscore.h" |
227
|
32 #include "lo-error.h" |
3
|
33 |
|
34 extern "C" |
|
35 { |
253
|
36 int F77_FCN (hybrd1) (int (*)(int*, double*, double*, int*), |
|
37 const int*, double*, double*, const double*, |
|
38 int*, double*, const int*); |
3
|
39 |
253
|
40 int F77_FCN (hybrj1) (int (*)(int*, double*, double*, double*, int*, int*), |
|
41 const int*, double*, double*, double*, const int*, |
|
42 const double*, int*, double*, const int*); |
3
|
43 } |
|
44 |
|
45 static nonlinear_fcn user_fun; |
|
46 static jacobian_fcn user_jac; |
|
47 |
|
48 // error handling |
|
49 |
|
50 void |
|
51 NLEqn::error (const char* msg) |
|
52 { |
227
|
53 (*current_liboctave_error_handler) ("fatal NLEqn error: %s", msg); |
3
|
54 } |
|
55 |
|
56 // Constructors |
|
57 |
|
58 NLEqn::NLEqn (void) : NLFunc (), x (), n (0) {} |
|
59 |
|
60 NLEqn::NLEqn (const Vector& xvec, const NLFunc f) |
190
|
61 : NLFunc (f), x (xvec), n (xvec.capacity ()) {} |
3
|
62 |
|
63 NLEqn::NLEqn (const NLEqn& a) : NLFunc (a.fun, a.jac), x (a.x), n (a.n) {} |
|
64 |
|
65 void |
|
66 NLEqn::resize (int nn) |
|
67 { |
|
68 if (n != nn) |
|
69 { |
|
70 n = nn; |
|
71 x.resize (n); |
|
72 } |
|
73 } |
|
74 |
|
75 int |
|
76 NLEqn::size (void) const |
|
77 { |
|
78 return n; |
|
79 } |
|
80 |
|
81 // Assignment |
|
82 |
|
83 NLEqn& |
|
84 NLEqn::operator = (const NLEqn& a) |
|
85 { |
|
86 fun = a.fun; |
|
87 jac = a.jac; |
|
88 x = a.n; |
|
89 |
|
90 return *this; |
|
91 } |
|
92 |
|
93 Vector |
|
94 NLEqn::states (void) const |
|
95 { |
|
96 return x; |
|
97 } |
|
98 |
|
99 void |
|
100 NLEqn::set_states (const Vector& xvec) |
|
101 { |
|
102 if (xvec.capacity () != n) |
227
|
103 { |
|
104 error ("dimension error"); |
|
105 return; |
|
106 } |
3
|
107 |
|
108 x = xvec; |
|
109 } |
|
110 |
|
111 // Other operations |
|
112 |
|
113 Vector |
|
114 NLEqn::solve (const Vector& xvec) |
|
115 { |
|
116 set_states (xvec); |
|
117 int info; |
|
118 return solve (info); |
|
119 } |
|
120 |
|
121 Vector |
|
122 NLEqn::solve (const Vector& xvec, int& info) |
|
123 { |
|
124 set_states (xvec); |
|
125 return solve (info); |
|
126 } |
|
127 |
|
128 Vector |
|
129 NLEqn::solve (void) |
|
130 { |
|
131 int info; |
|
132 return solve (info); |
|
133 } |
|
134 |
|
135 int |
|
136 hybrd1_fcn (int *n, double *x, double *fvec, int *iflag) |
|
137 { |
|
138 int nn = *n; |
|
139 Vector tmp_f (nn); |
|
140 Vector tmp_x (nn); |
|
141 |
|
142 for (int i = 0; i < nn; i++) |
|
143 tmp_x.elem (i) = x[i]; |
|
144 |
|
145 tmp_f = (*user_fun) (tmp_x); |
|
146 |
253
|
147 if (tmp_f.length () == 0) |
|
148 *iflag = -1; |
|
149 else |
|
150 { |
|
151 for (i = 0; i < nn; i++) |
|
152 fvec[i] = tmp_f.elem (i); |
|
153 } |
3
|
154 |
|
155 return 0; |
|
156 } |
|
157 |
|
158 int |
|
159 hybrj1_fcn (int *n, double *x, double *fvec, double *fjac, |
|
160 int *ldfjac, int *iflag) |
|
161 { |
|
162 int nn = *n; |
|
163 Vector tmp_x (nn); |
|
164 |
|
165 for (int i = 0; i < nn; i++) |
|
166 tmp_x.elem (i) = x[i]; |
|
167 |
|
168 int flag = *iflag; |
|
169 if (flag == 1) |
|
170 { |
|
171 Vector tmp_f (nn); |
|
172 |
|
173 tmp_f = (*user_fun) (tmp_x); |
|
174 |
253
|
175 if (tmp_f.length () == 0) |
|
176 *iflag = -1; |
|
177 else |
|
178 { |
|
179 for (i = 0; i < nn; i++) |
|
180 fvec[i] = tmp_f.elem (i); |
|
181 } |
3
|
182 } |
|
183 else |
|
184 { |
|
185 Matrix tmp_fj (nn, nn); |
|
186 |
|
187 tmp_fj = (*user_jac) (tmp_x); |
|
188 |
253
|
189 if (tmp_fj.rows () == 0 || tmp_fj.columns () == 0) |
|
190 *iflag = -1; |
|
191 else |
|
192 { |
|
193 int ld = *ldfjac; |
|
194 for (int j = 0; j < nn; j++) |
|
195 for (i = 0; i < nn; i++) |
|
196 fjac[j*ld+i] = tmp_fj.elem (i, j); |
|
197 } |
3
|
198 } |
|
199 |
|
200 return 0; |
|
201 } |
|
202 |
|
203 Vector |
|
204 NLEqn::solve (int& info) |
|
205 { |
|
206 int tmp_info = 0; |
|
207 |
|
208 if (n == 0) |
227
|
209 { |
|
210 error ("equation set not initialized"); |
|
211 return Vector (); |
|
212 } |
3
|
213 |
289
|
214 double tol = tolerance (); |
3
|
215 |
|
216 double *fvec = new double [n]; |
|
217 double *px = new double [n]; |
|
218 for (int i = 0; i < n; i++) |
|
219 px[i] = x.elem (i); |
|
220 |
|
221 user_fun = fun; |
|
222 user_jac = jac; |
|
223 |
|
224 if (jac == NULL) |
|
225 { |
|
226 int lwa = (n*(3*n+13))/2; |
|
227 double *wa = new double [lwa]; |
|
228 |
|
229 F77_FCN (hybrd1) (hybrd1_fcn, &n, px, fvec, &tol, &tmp_info, wa, &lwa); |
|
230 |
|
231 delete [] wa; |
|
232 } |
|
233 else |
|
234 { |
|
235 int lwa = (n*(n+13))/2; |
|
236 double *wa = new double [lwa]; |
|
237 double *fjac = new double [n*n]; |
|
238 |
|
239 F77_FCN (hybrj1) (hybrj1_fcn, &n, px, fvec, fjac, &n, &tol, |
|
240 &tmp_info, wa, &lwa); |
|
241 |
|
242 delete [] wa; |
|
243 delete [] fjac; |
|
244 } |
|
245 |
253
|
246 Vector retval; |
|
247 |
3
|
248 info = tmp_info; |
|
249 |
253
|
250 if (info >= 0) |
|
251 { |
|
252 retval.resize (n); |
3
|
253 |
253
|
254 for (i = 0; i < n; i++) |
|
255 retval.elem (i) = px[i]; |
|
256 } |
3
|
257 |
|
258 return retval; |
|
259 } |
|
260 |
289
|
261 NLEqn_options::NLEqn_options (void) |
|
262 { |
|
263 init (); |
|
264 } |
|
265 |
|
266 NLEqn_options::NLEqn_options (const NLEqn_options& opt) |
|
267 { |
|
268 copy (opt); |
|
269 } |
|
270 |
|
271 NLEqn_options& |
|
272 NLEqn_options::operator = (const NLEqn_options& opt) |
|
273 { |
|
274 if (this != &opt) |
|
275 copy (opt); |
|
276 |
|
277 return *this; |
|
278 } |
|
279 |
|
280 NLEqn_options::~NLEqn_options (void) |
|
281 { |
|
282 } |
|
283 |
|
284 void |
|
285 NLEqn_options::init (void) |
|
286 { |
|
287 double sqrt_eps = sqrt (DBL_EPSILON); |
|
288 x_tolerance = sqrt_eps; |
|
289 } |
|
290 |
|
291 void |
|
292 NLEqn_options::copy (const NLEqn_options& opt) |
|
293 { |
|
294 x_tolerance = opt.x_tolerance; |
|
295 } |
|
296 |
|
297 void |
|
298 NLEqn_options::set_default_options (void) |
|
299 { |
|
300 init (); |
|
301 } |
|
302 |
|
303 void |
|
304 NLEqn_options::set_tolerance (double val) |
|
305 { |
|
306 x_tolerance = (val > 0.0) ? val : sqrt (DBL_EPSILON); |
|
307 } |
|
308 |
|
309 double |
|
310 NLEqn_options::tolerance (void) |
|
311 { |
|
312 return x_tolerance; |
|
313 } |
|
314 |
3
|
315 /* |
|
316 ;;; Local Variables: *** |
|
317 ;;; mode: C++ *** |
|
318 ;;; page-delimiter: "^/\\*" *** |
|
319 ;;; End: *** |
|
320 */ |