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 |
506
|
45 builtin_fsolve_2 (const Octave_object& args, int nargout) |
1
|
46 { |
506
|
47 return fsolve (args, nargout); |
1
|
48 } |
272
|
49 |
497
|
50 Octave_object |
506
|
51 builtin_fsolve_options (const Octave_object& args, int nargout) |
272
|
52 { |
506
|
53 return fsolve_options (args, nargout); |
272
|
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 { |
506
|
115 Octave_object tmp = fsolve_fcn->eval (0, 1, args); |
497
|
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 |
506
|
131 fsolve (const Octave_object& args, int nargout) |
1
|
132 { |
|
133 // Assumes that we have been given the correct number of arguments. |
|
134 |
497
|
135 Octave_object retval; |
1
|
136 |
506
|
137 int nargin = args.length (); |
|
138 |
497
|
139 fsolve_fcn = is_valid_function (args(1), "fsolve", 1); |
488
|
140 if (fsolve_fcn == (tree_fvc *) NULL |
1
|
141 || takes_correct_nargs (fsolve_fcn, 2, "fsolve", 1) != 1) |
|
142 return retval; |
|
143 |
497
|
144 ColumnVector x = args(2).to_vector (); |
1
|
145 |
|
146 if (nargin > 3) |
289
|
147 warning ("fsolve: ignoring extra arguments"); |
1
|
148 |
|
149 if (nargout > 2) |
216
|
150 warning ("fsolve: can't compute path output yet"); |
1
|
151 |
|
152 NLFunc foo_fcn (fsolve_user_function); |
|
153 NLEqn foo (x, foo_fcn); |
289
|
154 foo.copy (fsolve_opts); |
1
|
155 |
|
156 int info; |
|
157 ColumnVector soln = foo.solve (info); |
|
158 |
|
159 info = hybrd_info_to_fsolve_info (info); |
|
160 |
497
|
161 retval.resize (nargout ? nargout : 1); |
|
162 retval(0) = tree_constant (soln, 1); |
1
|
163 |
|
164 if (nargout > 1) |
497
|
165 retval(1) = tree_constant ((double) info); |
1
|
166 |
|
167 if (nargout > 2) |
497
|
168 retval(2) = tree_constant (); |
1
|
169 |
|
170 return retval; |
|
171 } |
|
172 |
289
|
173 typedef void (NLEqn_options::*d_set_opt_mf) (double); |
|
174 typedef double (NLEqn_options::*d_get_opt_mf) (void); |
|
175 |
|
176 #define MAX_TOKENS 1 |
|
177 |
|
178 struct NLEQN_OPTIONS |
|
179 { |
|
180 char *keyword; |
|
181 char *kw_tok[MAX_TOKENS + 1]; |
|
182 int min_len[MAX_TOKENS + 1]; |
|
183 int min_toks_to_match; |
|
184 d_set_opt_mf d_set_fcn; |
|
185 d_get_opt_mf d_get_fcn; |
|
186 }; |
|
187 |
497
|
188 static NLEQN_OPTIONS fsolve_option_table [] = |
289
|
189 { |
|
190 { "tolerance", |
|
191 { "tolerance", NULL, }, |
|
192 { 1, 0, }, 1, |
|
193 NLEqn_options::set_tolerance, |
|
194 NLEqn_options::tolerance, }, |
|
195 |
|
196 { NULL, |
|
197 { NULL, NULL, }, |
|
198 { 0, 0, }, 0, |
|
199 NULL, NULL, }, |
|
200 }; |
|
201 |
|
202 static void |
|
203 print_fsolve_option_list (void) |
|
204 { |
|
205 ostrstream output_buf; |
|
206 |
|
207 print_usage ("fsolve_options", 1); |
|
208 |
|
209 output_buf << "\n" |
|
210 << "Options for fsolve include:\n\n" |
|
211 << " keyword value\n" |
|
212 << " ------- -----\n\n"; |
|
213 |
|
214 NLEQN_OPTIONS *list = fsolve_option_table; |
|
215 |
|
216 char *keyword; |
|
217 while ((keyword = list->keyword) != (char *) NULL) |
|
218 { |
|
219 output_buf.form (" %-40s ", keyword); |
|
220 |
|
221 double val = (fsolve_opts.*list->d_get_fcn) (); |
|
222 if (val < 0.0) |
|
223 output_buf << "computed automatically"; |
|
224 else |
|
225 output_buf << val; |
|
226 |
|
227 output_buf << "\n"; |
|
228 list++; |
|
229 } |
|
230 |
|
231 output_buf << "\n" << ends; |
|
232 maybe_page_output (output_buf); |
|
233 } |
|
234 |
|
235 static void |
|
236 do_fsolve_option (char *keyword, double val) |
|
237 { |
|
238 NLEQN_OPTIONS *list = fsolve_option_table; |
|
239 |
|
240 while (list->keyword != (char *) NULL) |
|
241 { |
|
242 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
243 list->min_toks_to_match, MAX_TOKENS)) |
|
244 { |
|
245 (fsolve_opts.*list->d_set_fcn) (val); |
|
246 |
|
247 return; |
|
248 } |
|
249 list++; |
|
250 } |
|
251 |
|
252 warning ("fsolve_options: no match for `%s'", keyword); |
|
253 } |
|
254 |
497
|
255 Octave_object |
506
|
256 fsolve_options (const Octave_object& args, int nargout) |
272
|
257 { |
497
|
258 Octave_object retval; |
272
|
259 |
506
|
260 int nargin = args.length (); |
|
261 |
289
|
262 if (nargin == 1) |
497
|
263 print_fsolve_option_list (); |
289
|
264 else if (nargin == 3) |
|
265 { |
497
|
266 if (args(1).is_string_type ()) |
289
|
267 { |
497
|
268 char *keyword = args(1).string_value (); |
|
269 double val = args(2).double_value (); |
289
|
270 do_fsolve_option (keyword, val); |
|
271 } |
|
272 else |
|
273 print_usage ("fsolve_options"); |
|
274 } |
|
275 else |
497
|
276 print_usage ("fsolve_options"); |
289
|
277 |
272
|
278 return retval; |
|
279 } |
|
280 |
1
|
281 /* |
|
282 ;;; Local Variables: *** |
|
283 ;;; mode: C++ *** |
|
284 ;;; page-delimiter: "^/\\*" *** |
|
285 ;;; End: *** |
|
286 */ |