7
|
1 // f-fsolve.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
277
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
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 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
289
|
28 #include <strstream.h> |
|
29 |
1
|
30 #include "NLEqn.h" |
|
31 |
|
32 #include "tree-const.h" |
|
33 #include "variables.h" |
|
34 #include "gripes.h" |
|
35 #include "error.h" |
|
36 #include "utils.h" |
289
|
37 #include "pager.h" |
7
|
38 #include "f-fsolve.h" |
1
|
39 |
|
40 // Global pointer for user defined function required by hybrd1. |
488
|
41 static tree_fvc *fsolve_fcn; |
1
|
42 |
|
43 #ifdef WITH_DLD |
497
|
44 Octave_object |
|
45 builtin_fsolve_2 (const Octave_object& args, int nargin, int nargout) |
1
|
46 { |
|
47 return fsolve (args, nargin, nargout); |
|
48 } |
272
|
49 |
497
|
50 Octave_object |
|
51 builtin_fsolve_options (const Octave_object& args, int nargin, int nargout) |
272
|
52 { |
|
53 return fsolve_options (args, nargin, nargout); |
|
54 } |
1
|
55 #endif |
|
56 |
289
|
57 static NLEqn_options fsolve_opts; |
|
58 |
1
|
59 int |
|
60 hybrd_info_to_fsolve_info (int info) |
|
61 { |
|
62 switch (info) |
|
63 { |
252
|
64 case -1: |
|
65 info = -2; |
|
66 break; |
1
|
67 case 0: |
|
68 info = -1; |
|
69 break; |
|
70 case 1: |
|
71 break; |
|
72 case 2: |
|
73 info = 4; |
|
74 break; |
|
75 case 3: |
|
76 case 4: |
|
77 case 5: |
|
78 info = 3; |
|
79 break; |
|
80 default: |
|
81 panic_impossible (); |
|
82 break; |
|
83 } |
|
84 return info; |
|
85 } |
|
86 |
|
87 ColumnVector |
162
|
88 fsolve_user_function (const ColumnVector& x) |
1
|
89 { |
|
90 ColumnVector retval; |
|
91 |
|
92 int n = x.capacity (); |
|
93 |
|
94 // tree_constant name = tree_constant (fsolve_fcn->name ()); |
497
|
95 Octave_object args (2); |
|
96 // args(0) = name; |
1
|
97 |
|
98 if (n > 1) |
|
99 { |
|
100 Matrix m (n, 1); |
|
101 for (int i = 0; i < n; i++) |
|
102 m (i, 0) = x.elem (i); |
|
103 tree_constant vars (m); |
497
|
104 args(1) = vars; |
1
|
105 } |
|
106 else |
|
107 { |
|
108 double d = x.elem (0); |
|
109 tree_constant vars (d); |
497
|
110 args(1) = vars; |
1
|
111 } |
|
112 |
488
|
113 if (fsolve_fcn != (tree_fvc *) NULL) |
1
|
114 { |
497
|
115 Octave_object tmp = fsolve_fcn->eval (0, 1, args, 2); |
|
116 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
117 { |
497
|
118 retval = tmp(0).to_vector (); |
252
|
119 |
|
120 if (retval.length () <= 0) |
|
121 gripe_user_supplied_eval ("fsolve"); |
1
|
122 } |
|
123 else |
497
|
124 gripe_user_supplied_eval ("fsolve"); |
1
|
125 } |
|
126 |
|
127 return retval; |
|
128 } |
|
129 |
497
|
130 Octave_object |
|
131 fsolve (const Octave_object& args, int nargin, int nargout) |
1
|
132 { |
|
133 // Assumes that we have been given the correct number of arguments. |
|
134 |
497
|
135 Octave_object retval; |
1
|
136 |
497
|
137 fsolve_fcn = is_valid_function (args(1), "fsolve", 1); |
488
|
138 if (fsolve_fcn == (tree_fvc *) NULL |
1
|
139 || takes_correct_nargs (fsolve_fcn, 2, "fsolve", 1) != 1) |
|
140 return retval; |
|
141 |
497
|
142 ColumnVector x = args(2).to_vector (); |
1
|
143 |
|
144 if (nargin > 3) |
289
|
145 warning ("fsolve: ignoring extra arguments"); |
1
|
146 |
|
147 if (nargout > 2) |
216
|
148 warning ("fsolve: can't compute path output yet"); |
1
|
149 |
|
150 NLFunc foo_fcn (fsolve_user_function); |
|
151 NLEqn foo (x, foo_fcn); |
289
|
152 foo.copy (fsolve_opts); |
1
|
153 |
|
154 int info; |
|
155 ColumnVector soln = foo.solve (info); |
|
156 |
|
157 info = hybrd_info_to_fsolve_info (info); |
|
158 |
497
|
159 retval.resize (nargout ? nargout : 1); |
|
160 retval(0) = tree_constant (soln, 1); |
1
|
161 |
|
162 if (nargout > 1) |
497
|
163 retval(1) = tree_constant ((double) info); |
1
|
164 |
|
165 if (nargout > 2) |
497
|
166 retval(2) = tree_constant (); |
1
|
167 |
|
168 return retval; |
|
169 } |
|
170 |
289
|
171 typedef void (NLEqn_options::*d_set_opt_mf) (double); |
|
172 typedef double (NLEqn_options::*d_get_opt_mf) (void); |
|
173 |
|
174 #define MAX_TOKENS 1 |
|
175 |
|
176 struct NLEQN_OPTIONS |
|
177 { |
|
178 char *keyword; |
|
179 char *kw_tok[MAX_TOKENS + 1]; |
|
180 int min_len[MAX_TOKENS + 1]; |
|
181 int min_toks_to_match; |
|
182 d_set_opt_mf d_set_fcn; |
|
183 d_get_opt_mf d_get_fcn; |
|
184 }; |
|
185 |
497
|
186 static NLEQN_OPTIONS fsolve_option_table [] = |
289
|
187 { |
|
188 { "tolerance", |
|
189 { "tolerance", NULL, }, |
|
190 { 1, 0, }, 1, |
|
191 NLEqn_options::set_tolerance, |
|
192 NLEqn_options::tolerance, }, |
|
193 |
|
194 { NULL, |
|
195 { NULL, NULL, }, |
|
196 { 0, 0, }, 0, |
|
197 NULL, NULL, }, |
|
198 }; |
|
199 |
|
200 static void |
|
201 print_fsolve_option_list (void) |
|
202 { |
|
203 ostrstream output_buf; |
|
204 |
|
205 print_usage ("fsolve_options", 1); |
|
206 |
|
207 output_buf << "\n" |
|
208 << "Options for fsolve include:\n\n" |
|
209 << " keyword value\n" |
|
210 << " ------- -----\n\n"; |
|
211 |
|
212 NLEQN_OPTIONS *list = fsolve_option_table; |
|
213 |
|
214 char *keyword; |
|
215 while ((keyword = list->keyword) != (char *) NULL) |
|
216 { |
|
217 output_buf.form (" %-40s ", keyword); |
|
218 |
|
219 double val = (fsolve_opts.*list->d_get_fcn) (); |
|
220 if (val < 0.0) |
|
221 output_buf << "computed automatically"; |
|
222 else |
|
223 output_buf << val; |
|
224 |
|
225 output_buf << "\n"; |
|
226 list++; |
|
227 } |
|
228 |
|
229 output_buf << "\n" << ends; |
|
230 maybe_page_output (output_buf); |
|
231 } |
|
232 |
|
233 static void |
|
234 do_fsolve_option (char *keyword, double val) |
|
235 { |
|
236 NLEQN_OPTIONS *list = fsolve_option_table; |
|
237 |
|
238 while (list->keyword != (char *) NULL) |
|
239 { |
|
240 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
241 list->min_toks_to_match, MAX_TOKENS)) |
|
242 { |
|
243 (fsolve_opts.*list->d_set_fcn) (val); |
|
244 |
|
245 return; |
|
246 } |
|
247 list++; |
|
248 } |
|
249 |
|
250 warning ("fsolve_options: no match for `%s'", keyword); |
|
251 } |
|
252 |
497
|
253 Octave_object |
|
254 fsolve_options (const Octave_object& args, int nargin, int nargout) |
272
|
255 { |
497
|
256 Octave_object retval; |
272
|
257 |
289
|
258 if (nargin == 1) |
497
|
259 print_fsolve_option_list (); |
289
|
260 else if (nargin == 3) |
|
261 { |
497
|
262 if (args(1).is_string_type ()) |
289
|
263 { |
497
|
264 char *keyword = args(1).string_value (); |
|
265 double val = args(2).double_value (); |
289
|
266 do_fsolve_option (keyword, val); |
|
267 } |
|
268 else |
|
269 print_usage ("fsolve_options"); |
|
270 } |
|
271 else |
497
|
272 print_usage ("fsolve_options"); |
289
|
273 |
272
|
274 return retval; |
|
275 } |
|
276 |
1
|
277 /* |
|
278 ;;; Local Variables: *** |
|
279 ;;; mode: C++ *** |
|
280 ;;; page-delimiter: "^/\\*" *** |
|
281 ;;; End: *** |
|
282 */ |