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