2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #include <iostream.h> |
|
30 |
|
31 #include "Quad.h" |
|
32 #include "lo-mappers.h" |
|
33 |
|
34 #include "defun-dld.h" |
|
35 #include "error.h" |
|
36 #include "gripes.h" |
|
37 #include "pager.h" |
|
38 #include "oct-obj.h" |
2968
|
39 #include "ov-fcn.h" |
3243
|
40 #include "unwind-prot.h" |
2928
|
41 #include "utils.h" |
|
42 #include "variables.h" |
|
43 |
|
44 #if defined (quad) |
|
45 #undef quad |
|
46 #endif |
|
47 |
|
48 // Global pointer for user defined function required by quadrature functions. |
2968
|
49 static octave_function *quad_fcn; |
2928
|
50 |
|
51 static Quad_options quad_opts; |
|
52 |
3243
|
53 // Is this a recursive call? |
|
54 static int call_depth = 0; |
|
55 |
2928
|
56 double |
|
57 quad_user_function (double x) |
|
58 { |
|
59 double retval = 0.0; |
|
60 |
|
61 octave_value_list args; |
|
62 args(0) = x; |
|
63 |
|
64 if (quad_fcn) |
|
65 { |
2968
|
66 octave_value_list tmp = quad_fcn->do_index_op (1, args); |
2928
|
67 |
|
68 if (error_state) |
|
69 { |
|
70 quad_integration_error = 1; // XXX FIXME XXX |
|
71 gripe_user_supplied_eval ("quad"); |
|
72 return retval; |
|
73 } |
|
74 |
|
75 if (tmp.length () && tmp(0).is_defined ()) |
|
76 { |
|
77 retval = tmp(0).double_value (); |
|
78 |
|
79 if (error_state) |
|
80 { |
|
81 quad_integration_error = 1; // XXX FIXME XXX |
|
82 gripe_user_supplied_eval ("quad"); |
|
83 } |
|
84 } |
|
85 else |
|
86 { |
|
87 quad_integration_error = 1; // XXX FIXME XXX |
|
88 gripe_user_supplied_eval ("quad"); |
|
89 } |
|
90 } |
|
91 |
|
92 return retval; |
|
93 } |
|
94 |
3323
|
95 #define QUAD_ABORT() \ |
|
96 do \ |
|
97 { \ |
|
98 unwind_protect::run_frame ("Fquad"); \ |
|
99 return retval; \ |
|
100 } \ |
|
101 while (0) |
|
102 |
|
103 #define QUAD_ABORT1(msg) \ |
|
104 do \ |
|
105 { \ |
|
106 ::error ("quad: " ## msg); \ |
|
107 QUAD_ABORT (); \ |
|
108 } \ |
|
109 while (0) |
|
110 |
|
111 #define QUAD_ABORT2(fmt, arg) \ |
|
112 do \ |
|
113 { \ |
|
114 ::error ("quad: " ## fmt, arg); \ |
|
115 QUAD_ABORT (); \ |
|
116 } \ |
|
117 while (0) |
|
118 |
2928
|
119 DEFUN_DLD (quad, args, nargout, |
3368
|
120 "-*- texinfo -*-\n\ |
|
121 @deftypefn {Loadable Function} {[@var{v}, @var{ier}, @var{nfun}, @var{err}] =} quad (@var{f}, @var{a}, @var{b}, @var{tol}, @var{sing})\n\ |
|
122 Integrate a nonlinear function of one variable using Quadpack.\n\ |
|
123 The first argument is the name of the function to call to compute the\n\ |
|
124 value of the integrand. It must have the form\n\ |
2928
|
125 \n\ |
3368
|
126 @example\n\ |
|
127 y = f (x)\n\ |
|
128 @end example\n\ |
2928
|
129 \n\ |
3368
|
130 @noindent\n\ |
|
131 where @var{y} and @var{x} are scalars.\n\ |
2928
|
132 \n\ |
|
133 The second and third arguments are limits of integration. Either or\n\ |
|
134 both may be infinite.\n\ |
|
135 \n\ |
3368
|
136 The optional argument @var{tol} is a vector that specifies the desired\n\ |
2928
|
137 accuracy of the result. The first element of the vector is the desired\n\ |
|
138 absolute tolerance, and the second element is the desired relative\n\ |
3368
|
139 tolerance. To choose a relative test only, set the absolute\n\ |
|
140 tolerance to zero. To choose an absolute test only, set the relative\n\ |
|
141 tolerance to zero. \n\ |
2928
|
142 \n\ |
|
143 The optional argument @var{sing} is a vector of values at which the\n\ |
3368
|
144 integrand is known to be singular.\n\ |
|
145 \n\ |
|
146 The result of the integration is returned in @var{v} and @var{ier}\n\ |
|
147 contains an integer error code (0 indicates a successful integration).\n\ |
|
148 The value of @var{nfun} indicates how many function evaluations were\n\ |
|
149 required, and @var{err} contains an estimate of the error in the\n\ |
|
150 solution.\n\ |
|
151 @end deftypefn") |
2928
|
152 { |
|
153 octave_value_list retval; |
|
154 |
3243
|
155 unwind_protect::begin_frame ("Fquad"); |
|
156 |
|
157 unwind_protect_int (call_depth); |
|
158 call_depth++; |
|
159 |
|
160 if (call_depth > 1) |
3323
|
161 QUAD_ABORT1 ("invalid recursive call"); |
3243
|
162 |
2928
|
163 int nargin = args.length (); |
|
164 |
3243
|
165 if (nargin > 2 && nargin < 6 && nargout < 5) |
|
166 { |
|
167 quad_fcn = extract_function (args(0), "quad", "__quad_fcn__", |
|
168 "function y = __quad_fcn__ (x) y = ", |
|
169 "; endfunction"); |
|
170 if (! quad_fcn) |
3323
|
171 QUAD_ABORT (); |
3243
|
172 |
|
173 double a = args(1).double_value (); |
|
174 |
|
175 if (error_state) |
3323
|
176 QUAD_ABORT1 ("expecting second argument to be a scalar"); |
3243
|
177 |
|
178 double b = args(2).double_value (); |
|
179 |
|
180 if (error_state) |
3323
|
181 QUAD_ABORT1 ("expecting third argument to be a scalar"); |
3243
|
182 |
|
183 int indefinite = 0; |
|
184 IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite; |
|
185 double bound = 0.0; |
|
186 if (xisinf (a) && xisinf (b)) |
|
187 { |
|
188 indefinite = 1; |
|
189 indef_type = IndefQuad::doubly_infinite; |
|
190 } |
|
191 else if (xisinf (a)) |
|
192 { |
|
193 indefinite = 1; |
|
194 bound = b; |
|
195 indef_type = IndefQuad::neg_inf_to_bound; |
|
196 } |
|
197 else if (xisinf (b)) |
|
198 { |
|
199 indefinite = 1; |
|
200 bound = a; |
|
201 indef_type = IndefQuad::bound_to_inf; |
|
202 } |
|
203 |
|
204 int ier = 0; |
|
205 int nfun = 0; |
|
206 double abserr = 0.0; |
|
207 double val = 0.0; |
|
208 double abstol = 1e-6; |
|
209 double reltol = 1e-6; |
|
210 ColumnVector tol (2); |
|
211 ColumnVector sing; |
|
212 int have_sing = 0; |
|
213 switch (nargin) |
|
214 { |
|
215 case 5: |
|
216 if (indefinite) |
3323
|
217 QUAD_ABORT1 ("singularities not allowed on infinite intervals"); |
3243
|
218 |
|
219 have_sing = 1; |
|
220 |
|
221 sing = args(4).vector_value (); |
|
222 |
|
223 if (error_state) |
3323
|
224 QUAD_ABORT1 ("expecting vector of singularities as fourth argument"); |
3243
|
225 |
|
226 case 4: |
|
227 tol = args(3).vector_value (); |
|
228 |
|
229 if (error_state) |
3323
|
230 QUAD_ABORT1 ("expecting vector of tolerances as fifth argument"); |
3243
|
231 |
|
232 switch (tol.capacity ()) |
|
233 { |
|
234 case 2: |
|
235 reltol = tol (1); |
|
236 |
|
237 case 1: |
|
238 abstol = tol (0); |
|
239 break; |
|
240 |
|
241 default: |
3323
|
242 QUAD_ABORT1 ("expecting tol to contain no more than two values"); |
3243
|
243 } |
|
244 |
|
245 case 3: |
|
246 if (indefinite) |
|
247 { |
|
248 IndefQuad iq (quad_user_function, bound, indef_type, abstol, reltol); |
|
249 iq.set_options (quad_opts); |
|
250 val = iq.integrate (ier, nfun, abserr); |
|
251 } |
|
252 else |
|
253 { |
|
254 if (have_sing) |
|
255 { |
|
256 DefQuad dq (quad_user_function, a, b, sing, abstol, reltol); |
|
257 dq.set_options (quad_opts); |
|
258 val = dq.integrate (ier, nfun, abserr); |
|
259 } |
|
260 else |
|
261 { |
|
262 DefQuad dq (quad_user_function, a, b, abstol, reltol); |
|
263 dq.set_options (quad_opts); |
|
264 val = dq.integrate (ier, nfun, abserr); |
|
265 } |
|
266 } |
|
267 break; |
|
268 |
|
269 default: |
|
270 panic_impossible (); |
|
271 break; |
|
272 } |
|
273 |
|
274 retval(3) = abserr; |
|
275 retval(2) = static_cast<double> (nfun); |
|
276 retval(1) = static_cast<double> (ier); |
|
277 retval(0) = val; |
|
278 } |
|
279 else |
3323
|
280 print_usage ("quad"); |
2928
|
281 |
3243
|
282 unwind_protect::run_frame ("Fquad"); |
2928
|
283 |
|
284 return retval; |
|
285 } |
|
286 |
|
287 typedef void (Quad_options::*d_set_opt_mf) (double); |
|
288 typedef double (Quad_options::*d_get_opt_mf) (void); |
|
289 |
|
290 #define MAX_TOKENS 2 |
|
291 |
|
292 struct QUAD_OPTIONS |
|
293 { |
|
294 const char *keyword; |
|
295 const char *kw_tok[MAX_TOKENS + 1]; |
|
296 int min_len[MAX_TOKENS + 1]; |
|
297 int min_toks_to_match; |
|
298 d_set_opt_mf d_set_fcn; |
|
299 d_get_opt_mf d_get_fcn; |
|
300 }; |
|
301 |
|
302 static QUAD_OPTIONS quad_option_table [] = |
|
303 { |
|
304 { "absolute tolerance", |
|
305 { "absolute", "tolerance", 0, }, |
|
306 { 1, 0, 0, }, 1, |
3131
|
307 &Quad_options::set_absolute_tolerance, |
|
308 &Quad_options::absolute_tolerance, }, |
2928
|
309 |
|
310 { "relative tolerance", |
|
311 { "relative", "tolerance", 0, }, |
|
312 { 1, 0, 0, }, 1, |
3131
|
313 &Quad_options::set_relative_tolerance, |
|
314 &Quad_options::relative_tolerance, }, |
2928
|
315 |
|
316 { 0, |
|
317 { 0, 0, 0, }, |
|
318 { 0, 0, 0, }, 0, |
|
319 0, 0, }, |
|
320 }; |
|
321 |
|
322 static void |
|
323 print_quad_option_list (ostream& os) |
|
324 { |
|
325 print_usage ("quad_options", 1); |
|
326 |
|
327 os << "\n" |
|
328 << "Options for quad include:\n\n" |
|
329 << " keyword value\n" |
|
330 << " ------- -----\n\n"; |
|
331 |
|
332 QUAD_OPTIONS *list = quad_option_table; |
|
333 |
|
334 const char *keyword; |
|
335 while ((keyword = list->keyword) != 0) |
|
336 { |
|
337 os.form (" %-40s ", keyword); |
|
338 |
|
339 double val = (quad_opts.*list->d_get_fcn) (); |
|
340 if (val < 0.0) |
|
341 os << "computed automatically"; |
|
342 else |
|
343 os << val; |
|
344 |
|
345 os << "\n"; |
|
346 list++; |
|
347 } |
|
348 |
|
349 os << "\n"; |
|
350 } |
|
351 |
|
352 static void |
|
353 set_quad_option (const string& keyword, double val) |
|
354 { |
|
355 QUAD_OPTIONS *list = quad_option_table; |
|
356 |
|
357 while (list->keyword != 0) |
|
358 { |
|
359 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
360 list->min_toks_to_match, MAX_TOKENS)) |
|
361 { |
|
362 (quad_opts.*list->d_set_fcn) (val); |
|
363 |
|
364 return; |
|
365 } |
|
366 list++; |
|
367 } |
|
368 |
|
369 warning ("quad_options: no match for `%s'", keyword.c_str ()); |
|
370 } |
|
371 |
|
372 static octave_value_list |
|
373 show_quad_option (const string& keyword) |
|
374 { |
|
375 octave_value retval; |
|
376 |
|
377 QUAD_OPTIONS *list = quad_option_table; |
|
378 |
|
379 while (list->keyword != 0) |
|
380 { |
|
381 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
382 list->min_toks_to_match, MAX_TOKENS)) |
|
383 { |
|
384 return (quad_opts.*list->d_get_fcn) (); |
|
385 } |
|
386 list++; |
|
387 } |
|
388 |
|
389 warning ("quad_options: no match for `%s'", keyword.c_str ()); |
|
390 |
|
391 return retval; |
|
392 } |
|
393 |
|
394 DEFUN_DLD (quad_options, args, , |
3368
|
395 "-*- texinfo -*-\n\ |
|
396 @deftypefn {Loadable Function} {} quad_options (@var{opt}, @var{val})\n\ |
|
397 When called with two arguments, this function allows you set options\n\ |
|
398 parameters for the function @code{quad}. Given one argument,\n\ |
|
399 @code{quad_options} returns the value of the corresponding option. If\n\ |
|
400 no arguments are supplied, the names of all the available options and\n\ |
|
401 their current values are displayed.\n\ |
|
402 @end deftypefn") |
2928
|
403 { |
|
404 octave_value_list retval; |
|
405 |
|
406 int nargin = args.length (); |
|
407 |
|
408 if (nargin == 0) |
|
409 { |
|
410 print_quad_option_list (octave_stdout); |
|
411 return retval; |
|
412 } |
|
413 else if (nargin == 1 || nargin == 2) |
|
414 { |
|
415 string keyword = args(0).string_value (); |
|
416 |
|
417 if (! error_state) |
|
418 { |
|
419 if (nargin == 1) |
|
420 return show_quad_option (keyword); |
|
421 else |
|
422 { |
|
423 double val = args(1).double_value (); |
|
424 |
|
425 if (! error_state) |
|
426 { |
|
427 set_quad_option (keyword, val); |
|
428 return retval; |
|
429 } |
|
430 } |
|
431 } |
|
432 } |
|
433 |
|
434 print_usage ("quad_options"); |
|
435 |
|
436 return retval; |
|
437 } |
|
438 |
|
439 /* |
|
440 ;;; Local Variables: *** |
|
441 ;;; mode: C++ *** |
|
442 ;;; End: *** |
|
443 */ |