Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/fsolve.cc @ 7924:4976f66d469b
miscellaneous cleanup
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 11 Jul 2008 17:59:28 -0400 |
parents | 7ca2735d74c2 |
children | 44d206ae68c9 |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005, 2006, |
4 2007 John W. Eaton | |
2928 | 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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2928 | 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 | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2928 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <string> | |
29 | |
3567 | 30 #include <iomanip> |
3523 | 31 #include <iostream> |
5856 | 32 #include <sstream> |
2928 | 33 |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
34 #include "dNDArray.h" |
2928 | 35 #include "NLEqn.h" |
36 | |
37 #include "defun-dld.h" | |
38 #include "error.h" | |
39 #include "gripes.h" | |
40 #include "oct-obj.h" | |
2968 | 41 #include "ov-fcn.h" |
5729 | 42 #include "ov-cell.h" |
2928 | 43 #include "pager.h" |
3243 | 44 #include "unwind-prot.h" |
2928 | 45 #include "utils.h" |
46 #include "variables.h" | |
47 | |
3998 | 48 #include "NLEqn-opts.cc" |
49 | |
2928 | 50 // Global pointer for user defined function required by hybrd1. |
2968 | 51 static octave_function *fsolve_fcn; |
2928 | 52 |
4140 | 53 // Global pointer for optional user defined jacobian function. |
54 static octave_function *fsolve_jac; | |
55 | |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
56 // Original dimensions of X0. |
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
57 static dim_vector x_dims; |
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
58 |
4139 | 59 // Have we warned about imaginary values returned from user function? |
4140 | 60 static bool warned_fcn_imaginary = false; |
61 static bool warned_jac_imaginary = false; | |
4139 | 62 |
3243 | 63 // Is this a recursive call? |
64 static int call_depth = 0; | |
65 | |
5275 | 66 octave_idx_type |
67 hybrd_info_to_fsolve_info (octave_idx_type info) | |
2928 | 68 { |
69 switch (info) | |
70 { | |
71 case -1: | |
72 break; | |
73 | |
74 case 0: | |
7359 | 75 info = -2; |
2928 | 76 break; |
77 | |
78 case 1: | |
79 break; | |
80 | |
81 case 2: | |
82 info = 4; | |
83 break; | |
84 | |
85 case 3: | |
86 case 4: | |
87 case 5: | |
88 info = 3; | |
89 break; | |
90 | |
91 default: | |
5856 | 92 { |
93 std::ostringstream buf; | |
94 buf << "fsolve: unrecognized value of INFO from MINPACK (= " | |
95 << info << ")"; | |
96 std::string msg = buf.str (); | |
97 warning (msg.c_str ()); | |
98 } | |
2928 | 99 break; |
100 } | |
101 | |
102 return info; | |
103 } | |
104 | |
105 ColumnVector | |
106 fsolve_user_function (const ColumnVector& x) | |
107 { | |
108 ColumnVector retval; | |
109 | |
5275 | 110 octave_idx_type n = x.length (); |
2928 | 111 |
112 octave_value_list args; | |
113 args.resize (1); | |
114 | |
115 if (n > 1) | |
116 { | |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
117 NDArray m (ArrayN<double> (x, x_dims)); |
2928 | 118 octave_value vars (m); |
119 args(0) = vars; | |
120 } | |
121 else | |
122 { | |
123 double d = x (0); | |
124 octave_value vars (d); | |
125 args(0) = vars; | |
126 } | |
127 | |
128 if (fsolve_fcn) | |
129 { | |
3544 | 130 octave_value_list tmp = fsolve_fcn->do_multi_index_op (1, args); |
4139 | 131 |
2928 | 132 if (tmp.length () > 0 && tmp(0).is_defined ()) |
133 { | |
4140 | 134 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
4139 | 135 { |
136 warning ("fsolve: ignoring imaginary part returned from user-supplied function"); | |
4140 | 137 warned_fcn_imaginary = true; |
4139 | 138 } |
139 | |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
140 retval = ColumnVector (tmp(0).vector_value (false, true)); |
2928 | 141 |
142 if (error_state || retval.length () <= 0) | |
143 gripe_user_supplied_eval ("fsolve"); | |
7509
e41d6aff83ea
fsolve: detect nonsquare systems
John W. Eaton <jwe@octave.org>
parents:
7359
diff
changeset
|
144 else if (retval.length () != x.length ()) |
e41d6aff83ea
fsolve: detect nonsquare systems
John W. Eaton <jwe@octave.org>
parents:
7359
diff
changeset
|
145 error ("fsolve: unable to solve non-square systems"); |
2928 | 146 } |
147 else | |
148 gripe_user_supplied_eval ("fsolve"); | |
149 } | |
150 | |
151 return retval; | |
152 } | |
153 | |
4140 | 154 Matrix |
155 fsolve_user_jacobian (const ColumnVector& x) | |
156 { | |
157 Matrix retval; | |
158 | |
5275 | 159 octave_idx_type n = x.length (); |
4140 | 160 |
161 octave_value_list args; | |
162 args.resize (1); | |
163 | |
164 if (n > 1) | |
165 { | |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
166 NDArray m (ArrayN<double> (x, x_dims)); |
4140 | 167 octave_value vars (m); |
168 args(0) = vars; | |
169 } | |
170 else | |
171 { | |
4628 | 172 double d = x(0); |
4140 | 173 octave_value vars (d); |
174 args(0) = vars; | |
175 } | |
176 | |
177 if (fsolve_fcn) | |
178 { | |
179 octave_value_list tmp = fsolve_jac->do_multi_index_op (1, args); | |
180 | |
181 if (tmp.length () > 0 && tmp(0).is_defined ()) | |
182 { | |
183 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) | |
184 { | |
185 warning ("fsolve: ignoring imaginary part returned from user-supplied jacobian function"); | |
186 warned_fcn_imaginary = true; | |
187 } | |
188 | |
189 retval = tmp(0).matrix_value (); | |
190 | |
191 if (error_state || retval.length () <= 0) | |
192 gripe_user_supplied_eval ("fsolve"); | |
7509
e41d6aff83ea
fsolve: detect nonsquare systems
John W. Eaton <jwe@octave.org>
parents:
7359
diff
changeset
|
193 else if (! (retval.rows () == x.length () |
e41d6aff83ea
fsolve: detect nonsquare systems
John W. Eaton <jwe@octave.org>
parents:
7359
diff
changeset
|
194 && retval.columns () == x.length ())) |
e41d6aff83ea
fsolve: detect nonsquare systems
John W. Eaton <jwe@octave.org>
parents:
7359
diff
changeset
|
195 error ("fsolve: invalid Jacobian matrix dimensions"); |
4140 | 196 } |
197 else | |
198 gripe_user_supplied_eval ("fsolve"); | |
199 } | |
200 | |
201 return retval; | |
202 } | |
203 | |
3323 | 204 #define FSOLVE_ABORT() \ |
205 do \ | |
206 { \ | |
207 unwind_protect::run_frame ("Ffsolve"); \ | |
208 return retval; \ | |
209 } \ | |
210 while (0) | |
211 | |
212 #define FSOLVE_ABORT1(msg) \ | |
213 do \ | |
214 { \ | |
3747 | 215 ::error ("fsolve: " msg); \ |
3323 | 216 FSOLVE_ABORT (); \ |
217 } \ | |
218 while (0) | |
219 | |
220 #define FSOLVE_ABORT2(fmt, arg) \ | |
221 do \ | |
222 { \ | |
3747 | 223 ::error ("fsolve: " fmt, arg); \ |
3323 | 224 FSOLVE_ABORT (); \ |
225 } \ | |
226 while (0) | |
227 | |
2928 | 228 DEFUN_DLD (fsolve, args, nargout, |
3368 | 229 "-*- texinfo -*-\n\ |
7279 | 230 @deftypefn {Loadable Function} {[@var{x}, @var{fval}, @var{info}] =} fsolve (@var{fcn}, @var{x0})\n\ |
3368 | 231 Given @var{fcn}, the name of a function of the form @code{f (@var{x})}\n\ |
232 and an initial starting point @var{x0}, @code{fsolve} solves the set of\n\ | |
233 equations such that @code{f(@var{x}) == 0}.\n\ | |
3964 | 234 \n\ |
7359 | 235 On return, @var{fval} contains the value of the function @var{fcn}\n\ |
236 evaluated at @var{x}, and @var{info} may be one of the following values:\n\ | |
237 \n\ | |
238 @table @asis\n\ | |
239 \n\ | |
240 @item -2\n\ | |
241 Invalid input parameters.\n\ | |
242 @item -1\n\ | |
243 Error in user-supplied function.\n\ | |
244 @item 1\n\ | |
245 Relative error between two consecutive iterates is at most the\n\ | |
246 specified tolerance (see @code{fsolve_options}).\n\ | |
247 @item 3\n\ | |
248 Algorithm failed to converge.\n\ | |
249 @item 4\n\ | |
250 Limit on number of function calls reached.\n\ | |
251 @end table\n\ | |
252 \n\ | |
5729 | 253 If @var{fcn} is a two-element string array, or a two element cell array\n\ |
254 containing either the function name or inline or function handle. The\n\ | |
255 first element names the function @math{f} described above, and the second\n\ | |
256 element names a function of the form @code{j (@var{x})} to compute the\n\ | |
257 Jacobian matrix with elements\n\ | |
4144 | 258 @tex\n\ |
259 $$ J = {\\partial f_i \\over \\partial x_j} $$\n\ | |
260 @end tex\n\ | |
261 @ifinfo\n\ | |
262 \n\ | |
263 @example\n\ | |
4197 | 264 df_i\n\ |
265 jac(i,j) = ----\n\ | |
266 dx_j\n\ | |
4144 | 267 @end example\n\ |
268 @end ifinfo\n\ | |
269 \n\ | |
3964 | 270 You can use the function @code{fsolve_options} to set optional\n\ |
271 parameters for @code{fsolve}.\n\ | |
3368 | 272 @end deftypefn") |
2928 | 273 { |
274 octave_value_list retval; | |
275 | |
4140 | 276 warned_fcn_imaginary = false; |
277 warned_jac_imaginary = false; | |
4139 | 278 |
3243 | 279 unwind_protect::begin_frame ("Ffsolve"); |
2928 | 280 |
3243 | 281 unwind_protect_int (call_depth); |
282 call_depth++; | |
283 | |
284 if (call_depth > 1) | |
3323 | 285 FSOLVE_ABORT1 ("invalid recursive call"); |
2928 | 286 |
3243 | 287 int nargin = args.length (); |
2928 | 288 |
3243 | 289 if (nargin == 2 && nargout < 4) |
290 { | |
5729 | 291 std::string fcn_name, fname, jac_name, jname; |
4140 | 292 fsolve_fcn = 0; |
293 fsolve_jac = 0; | |
294 | |
295 octave_value f_arg = args(0); | |
296 | |
5729 | 297 if (f_arg.is_cell ()) |
298 { | |
299 Cell c = f_arg.cell_value (); | |
300 if (c.length() == 1) | |
301 f_arg = c(0); | |
302 else if (c.length() == 2) | |
303 { | |
304 if (c(0).is_function_handle () || c(0).is_inline_function ()) | |
305 fsolve_fcn = c(0).function_value (); | |
306 else | |
307 { | |
308 fcn_name = unique_symbol_name ("__fsolve_fcn__"); | |
309 fname = "function y = "; | |
310 fname.append (fcn_name); | |
311 fname.append (" (x) y = "); | |
312 fsolve_fcn = extract_function | |
313 (c(0), "fsolve", fcn_name, fname, "; endfunction"); | |
314 } | |
315 | |
316 if (fsolve_fcn) | |
317 { | |
318 if (c(1).is_function_handle () || c(1).is_inline_function ()) | |
319 fsolve_jac = c(1).function_value (); | |
320 else | |
321 { | |
322 jac_name = unique_symbol_name ("__fsolve_jac__"); | |
323 jname = "function y = "; | |
324 jname.append (jac_name); | |
325 jname.append (" (x) jac = "); | |
326 fsolve_jac = extract_function | |
327 (c(1), "fsolve", jac_name, jname, "; endfunction"); | |
4140 | 328 |
5729 | 329 if (!fsolve_jac) |
330 { | |
331 if (fcn_name.length()) | |
332 clear_function (fcn_name); | |
333 fsolve_fcn = 0; | |
334 } | |
335 } | |
336 } | |
337 } | |
338 else | |
339 FSOLVE_ABORT1 ("incorrect number of elements in cell array"); | |
340 } | |
4140 | 341 |
5729 | 342 if (!fsolve_fcn && ! f_arg.is_cell()) |
343 { | |
344 if (f_arg.is_function_handle () || f_arg.is_inline_function ()) | |
345 fsolve_fcn = f_arg.function_value (); | |
346 else | |
347 { | |
348 switch (f_arg.rows ()) | |
349 { | |
350 case 1: | |
351 do | |
352 { | |
353 fcn_name = unique_symbol_name ("__fsolve_fcn__"); | |
354 fname = "function y = "; | |
355 fname.append (fcn_name); | |
356 fname.append (" (x) y = "); | |
357 fsolve_fcn = extract_function | |
358 (f_arg, "fsolve", fcn_name, fname, "; endfunction"); | |
359 } | |
360 while (0); | |
361 break; | |
4140 | 362 |
5729 | 363 case 2: |
4140 | 364 { |
5729 | 365 string_vector tmp = f_arg.all_strings (); |
366 | |
367 if (! error_state) | |
368 { | |
369 fcn_name = unique_symbol_name ("__fsolve_fcn__"); | |
370 fname = "function y = "; | |
371 fname.append (fcn_name); | |
372 fname.append (" (x) y = "); | |
373 fsolve_fcn = extract_function | |
374 (tmp(0), "fsolve", fcn_name, fname, "; endfunction"); | |
4140 | 375 |
5729 | 376 if (fsolve_fcn) |
377 { | |
378 jac_name = unique_symbol_name ("__fsolve_jac__"); | |
379 jname = "function y = "; | |
380 jname.append (jac_name); | |
381 jname.append (" (x) jac = "); | |
382 fsolve_jac = extract_function | |
383 (tmp(1), "fsolve", jac_name, jname, | |
384 "; endfunction"); | |
385 | |
386 if (!fsolve_jac) | |
387 { | |
388 if (fcn_name.length()) | |
389 clear_function (fcn_name); | |
390 fsolve_fcn = 0; | |
391 } | |
392 } | |
393 } | |
4140 | 394 } |
5729 | 395 } |
396 } | |
4140 | 397 } |
398 | |
399 if (error_state || ! fsolve_fcn) | |
3323 | 400 FSOLVE_ABORT (); |
2928 | 401 |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
402 NDArray xa = args(1).array_value (); |
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
403 x_dims = xa.dims (); |
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
404 ColumnVector x (xa); |
2928 | 405 |
3243 | 406 if (error_state) |
3323 | 407 FSOLVE_ABORT1 ("expecting vector as second argument"); |
3243 | 408 |
4132 | 409 if (nargin > 3) |
3243 | 410 warning ("fsolve: ignoring extra arguments"); |
2928 | 411 |
4132 | 412 if (nargout > 3) |
3243 | 413 warning ("fsolve: can't compute path output yet"); |
2928 | 414 |
3971 | 415 NLFunc nleqn_fcn (fsolve_user_function); |
4140 | 416 if (fsolve_jac) |
417 nleqn_fcn.set_jacobian_function (fsolve_user_jacobian); | |
418 | |
3971 | 419 NLEqn nleqn (x, nleqn_fcn); |
4122 | 420 nleqn.set_options (fsolve_opts); |
3243 | 421 |
5275 | 422 octave_idx_type info; |
3971 | 423 ColumnVector soln = nleqn.solve (info); |
2928 | 424 |
5729 | 425 if (fcn_name.length()) |
426 clear_function (fcn_name); | |
427 if (jac_name.length()) | |
428 clear_function (jac_name); | |
429 | |
3971 | 430 if (! error_state) |
431 { | |
7279 | 432 retval(2) = static_cast<double> (hybrd_info_to_fsolve_info (info)); |
433 retval(1) = nleqn.function_value (); | |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
434 retval(0) = NDArray (ArrayN<double> (soln.reshape (x_dims))); |
2928 | 435 |
4132 | 436 if (! nleqn.solution_ok () && nargout < 2) |
7279 | 437 { |
438 std::string msg = nleqn.error_message (); | |
439 error ("fsolve: %s", msg.c_str ()); | |
440 } | |
3971 | 441 } |
3243 | 442 } |
443 else | |
5823 | 444 print_usage (); |
2928 | 445 |
3243 | 446 unwind_protect::run_frame ("Ffsolve"); |
2928 | 447 |
448 return retval; | |
449 } | |
450 | |
451 /* | |
7279 | 452 %!function retval = f (p) |
453 %! x = p(1); | |
454 %! y = p(2); | |
455 %! z = p(3); | |
456 %! retval = zeros (3, 1); | |
457 %! retval(1) = sin(x) + y**2 + log(z) - 7; | |
458 %! retval(2) = 3*x + 2**y -z**3 + 1; | |
459 %! retval(3) = x + y + z - 5; | |
460 %!test | |
461 %! x_opt = [ 0.599054; | |
462 %! 2.395931; | |
463 %! 2.005014 ]; | |
464 %! tol = 1.0e-5; | |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
465 %! [x, fval, info] = fsolve ("f", [ 0.5; 2.0; 2.5 ]); |
7279 | 466 %! info_bad = (info != 1); |
467 %! solution_bad = sum (abs (x - x_opt) > tol); | |
468 %! value_bad = sum (abs (fval) > tol); | |
469 %! if (info_bad) | |
470 %! printf_assert ("info bad\n"); | |
471 %! else | |
472 %! printf_assert ("info good\n"); | |
473 %! endif | |
474 %! if (solution_bad) | |
475 %! printf_assert ("solution bad\n"); | |
476 %! else | |
477 %! printf_assert ("solution good\n"); | |
478 %! endif | |
479 %! if (value_bad) | |
480 %! printf_assert ("value bad\n"); | |
481 %! else | |
482 %! printf_assert ("value good\n"); | |
483 %! endif | |
484 %! assert(prog_output_assert("info good\nsolution good\nvalue good")); | |
485 | |
486 %!function retval = f (p) | |
487 %! x = p(1); | |
488 %! y = p(2); | |
489 %! z = p(3); | |
490 %! w = p(4); | |
491 %! retval = zeros (4, 1); | |
492 %! retval(1) = 3*x + 4*y + exp (z + w) - 1.007; | |
493 %! retval(2) = 6*x - 4*y + exp (3*z + w) - 11; | |
494 %! retval(3) = x^4 - 4*y^2 + 6*z - 8*w - 20; | |
495 %! retval(4) = x^2 + 2*y^3 + z - w - 4; | |
496 %!test | |
7892
7ca2735d74c2
simplify & cleanup octave_value::XXX_vector_value functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7509
diff
changeset
|
497 %! x_opt = [ -0.767297326653401, 0.590671081117440, 1.47190018629642, -1.52719341133957 ]; |
7279 | 498 %! tol = 1.0e-5; |
499 %! [x, fval, info] = fsolve ("f", [-1, 1, 2, -1]); | |
500 %! info_bad = (info != 1); | |
501 %! solution_bad = sum (abs (x - x_opt) > tol); | |
502 %! value_bad = sum (abs (fval) > tol); | |
503 %! if (info_bad) | |
504 %! printf_assert ("info bad\n"); | |
505 %! else | |
506 %! printf_assert ("info good\n"); | |
507 %! endif | |
508 %! if (solution_bad) | |
509 %! printf_assert ("solution bad\n"); | |
510 %! else | |
511 %! printf_assert ("solution good\n"); | |
512 %! endif | |
513 %! if (value_bad) | |
514 %! printf_assert ("value bad\n"); | |
515 %! else | |
516 %! printf_assert ("value good\n"); | |
517 %! endif | |
518 %! assert(prog_output_assert("info good\nsolution good\nvalue good")); | |
519 | |
520 %!test | |
521 %! fsolve_options ("tolerance", eps); | |
522 %! assert(fsolve_options ("tolerance") == eps); | |
523 | |
524 %!error <Invalid call to fsolve_options.*> fsolve_options ("foo", 1, 2); | |
525 */ | |
526 | |
527 /* | |
2928 | 528 ;;; Local Variables: *** |
529 ;;; mode: C++ *** | |
530 ;;; End: *** | |
531 */ |