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, |
|
120 "[V, IER, NFUN] = quad (F, A, B [, TOL] [, SING])\n\ |
|
121 \n\ |
|
122 Where the first argument is the name of the function to call to\n\ |
|
123 compute the value of the integrand. It must have the form\n\ |
|
124 \n\ |
|
125 y = f (x)\n\ |
|
126 \n\ |
|
127 where y and x are scalars.\n\ |
|
128 \n\ |
|
129 The second and third arguments are limits of integration. Either or\n\ |
|
130 both may be infinite.\n\ |
|
131 \n\ |
|
132 The optional argument tol is a vector that specifies the desired\n\ |
|
133 accuracy of the result. The first element of the vector is the desired\n\ |
|
134 absolute tolerance, and the second element is the desired relative\n\ |
|
135 tolerance.\n\ |
|
136 \n\ |
|
137 The optional argument @var{sing} is a vector of values at which the\n\ |
|
138 integrand is singular.") |
|
139 { |
|
140 octave_value_list retval; |
|
141 |
3243
|
142 unwind_protect::begin_frame ("Fquad"); |
|
143 |
|
144 unwind_protect_int (call_depth); |
|
145 call_depth++; |
|
146 |
|
147 if (call_depth > 1) |
3323
|
148 QUAD_ABORT1 ("invalid recursive call"); |
3243
|
149 |
2928
|
150 int nargin = args.length (); |
|
151 |
3243
|
152 if (nargin > 2 && nargin < 6 && nargout < 5) |
|
153 { |
|
154 quad_fcn = extract_function (args(0), "quad", "__quad_fcn__", |
|
155 "function y = __quad_fcn__ (x) y = ", |
|
156 "; endfunction"); |
|
157 if (! quad_fcn) |
3323
|
158 QUAD_ABORT (); |
3243
|
159 |
|
160 double a = args(1).double_value (); |
|
161 |
|
162 if (error_state) |
3323
|
163 QUAD_ABORT1 ("expecting second argument to be a scalar"); |
3243
|
164 |
|
165 double b = args(2).double_value (); |
|
166 |
|
167 if (error_state) |
3323
|
168 QUAD_ABORT1 ("expecting third argument to be a scalar"); |
3243
|
169 |
|
170 int indefinite = 0; |
|
171 IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite; |
|
172 double bound = 0.0; |
|
173 if (xisinf (a) && xisinf (b)) |
|
174 { |
|
175 indefinite = 1; |
|
176 indef_type = IndefQuad::doubly_infinite; |
|
177 } |
|
178 else if (xisinf (a)) |
|
179 { |
|
180 indefinite = 1; |
|
181 bound = b; |
|
182 indef_type = IndefQuad::neg_inf_to_bound; |
|
183 } |
|
184 else if (xisinf (b)) |
|
185 { |
|
186 indefinite = 1; |
|
187 bound = a; |
|
188 indef_type = IndefQuad::bound_to_inf; |
|
189 } |
|
190 |
|
191 int ier = 0; |
|
192 int nfun = 0; |
|
193 double abserr = 0.0; |
|
194 double val = 0.0; |
|
195 double abstol = 1e-6; |
|
196 double reltol = 1e-6; |
|
197 ColumnVector tol (2); |
|
198 ColumnVector sing; |
|
199 int have_sing = 0; |
|
200 switch (nargin) |
|
201 { |
|
202 case 5: |
|
203 if (indefinite) |
3323
|
204 QUAD_ABORT1 ("singularities not allowed on infinite intervals"); |
3243
|
205 |
|
206 have_sing = 1; |
|
207 |
|
208 sing = args(4).vector_value (); |
|
209 |
|
210 if (error_state) |
3323
|
211 QUAD_ABORT1 ("expecting vector of singularities as fourth argument"); |
3243
|
212 |
|
213 case 4: |
|
214 tol = args(3).vector_value (); |
|
215 |
|
216 if (error_state) |
3323
|
217 QUAD_ABORT1 ("expecting vector of tolerances as fifth argument"); |
3243
|
218 |
|
219 switch (tol.capacity ()) |
|
220 { |
|
221 case 2: |
|
222 reltol = tol (1); |
|
223 |
|
224 case 1: |
|
225 abstol = tol (0); |
|
226 break; |
|
227 |
|
228 default: |
3323
|
229 QUAD_ABORT1 ("expecting tol to contain no more than two values"); |
3243
|
230 } |
|
231 |
|
232 case 3: |
|
233 if (indefinite) |
|
234 { |
|
235 IndefQuad iq (quad_user_function, bound, indef_type, abstol, reltol); |
|
236 iq.set_options (quad_opts); |
|
237 val = iq.integrate (ier, nfun, abserr); |
|
238 } |
|
239 else |
|
240 { |
|
241 if (have_sing) |
|
242 { |
|
243 DefQuad dq (quad_user_function, a, b, sing, abstol, reltol); |
|
244 dq.set_options (quad_opts); |
|
245 val = dq.integrate (ier, nfun, abserr); |
|
246 } |
|
247 else |
|
248 { |
|
249 DefQuad dq (quad_user_function, a, b, abstol, reltol); |
|
250 dq.set_options (quad_opts); |
|
251 val = dq.integrate (ier, nfun, abserr); |
|
252 } |
|
253 } |
|
254 break; |
|
255 |
|
256 default: |
|
257 panic_impossible (); |
|
258 break; |
|
259 } |
|
260 |
|
261 retval(3) = abserr; |
|
262 retval(2) = static_cast<double> (nfun); |
|
263 retval(1) = static_cast<double> (ier); |
|
264 retval(0) = val; |
|
265 } |
|
266 else |
3323
|
267 print_usage ("quad"); |
2928
|
268 |
3243
|
269 unwind_protect::run_frame ("Fquad"); |
2928
|
270 |
|
271 return retval; |
|
272 } |
|
273 |
|
274 typedef void (Quad_options::*d_set_opt_mf) (double); |
|
275 typedef double (Quad_options::*d_get_opt_mf) (void); |
|
276 |
|
277 #define MAX_TOKENS 2 |
|
278 |
|
279 struct QUAD_OPTIONS |
|
280 { |
|
281 const char *keyword; |
|
282 const char *kw_tok[MAX_TOKENS + 1]; |
|
283 int min_len[MAX_TOKENS + 1]; |
|
284 int min_toks_to_match; |
|
285 d_set_opt_mf d_set_fcn; |
|
286 d_get_opt_mf d_get_fcn; |
|
287 }; |
|
288 |
|
289 static QUAD_OPTIONS quad_option_table [] = |
|
290 { |
|
291 { "absolute tolerance", |
|
292 { "absolute", "tolerance", 0, }, |
|
293 { 1, 0, 0, }, 1, |
3131
|
294 &Quad_options::set_absolute_tolerance, |
|
295 &Quad_options::absolute_tolerance, }, |
2928
|
296 |
|
297 { "relative tolerance", |
|
298 { "relative", "tolerance", 0, }, |
|
299 { 1, 0, 0, }, 1, |
3131
|
300 &Quad_options::set_relative_tolerance, |
|
301 &Quad_options::relative_tolerance, }, |
2928
|
302 |
|
303 { 0, |
|
304 { 0, 0, 0, }, |
|
305 { 0, 0, 0, }, 0, |
|
306 0, 0, }, |
|
307 }; |
|
308 |
|
309 static void |
|
310 print_quad_option_list (ostream& os) |
|
311 { |
|
312 print_usage ("quad_options", 1); |
|
313 |
|
314 os << "\n" |
|
315 << "Options for quad include:\n\n" |
|
316 << " keyword value\n" |
|
317 << " ------- -----\n\n"; |
|
318 |
|
319 QUAD_OPTIONS *list = quad_option_table; |
|
320 |
|
321 const char *keyword; |
|
322 while ((keyword = list->keyword) != 0) |
|
323 { |
|
324 os.form (" %-40s ", keyword); |
|
325 |
|
326 double val = (quad_opts.*list->d_get_fcn) (); |
|
327 if (val < 0.0) |
|
328 os << "computed automatically"; |
|
329 else |
|
330 os << val; |
|
331 |
|
332 os << "\n"; |
|
333 list++; |
|
334 } |
|
335 |
|
336 os << "\n"; |
|
337 } |
|
338 |
|
339 static void |
|
340 set_quad_option (const string& keyword, double val) |
|
341 { |
|
342 QUAD_OPTIONS *list = quad_option_table; |
|
343 |
|
344 while (list->keyword != 0) |
|
345 { |
|
346 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
347 list->min_toks_to_match, MAX_TOKENS)) |
|
348 { |
|
349 (quad_opts.*list->d_set_fcn) (val); |
|
350 |
|
351 return; |
|
352 } |
|
353 list++; |
|
354 } |
|
355 |
|
356 warning ("quad_options: no match for `%s'", keyword.c_str ()); |
|
357 } |
|
358 |
|
359 static octave_value_list |
|
360 show_quad_option (const string& keyword) |
|
361 { |
|
362 octave_value retval; |
|
363 |
|
364 QUAD_OPTIONS *list = quad_option_table; |
|
365 |
|
366 while (list->keyword != 0) |
|
367 { |
|
368 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
369 list->min_toks_to_match, MAX_TOKENS)) |
|
370 { |
|
371 return (quad_opts.*list->d_get_fcn) (); |
|
372 } |
|
373 list++; |
|
374 } |
|
375 |
|
376 warning ("quad_options: no match for `%s'", keyword.c_str ()); |
|
377 |
|
378 return retval; |
|
379 } |
|
380 |
|
381 DEFUN_DLD (quad_options, args, , |
|
382 "quad_options (KEYWORD, VALUE)\n\ |
|
383 \n\ |
|
384 Set or show options for quad. Keywords may be abbreviated\n\ |
|
385 to the shortest match.") |
|
386 { |
|
387 octave_value_list retval; |
|
388 |
|
389 int nargin = args.length (); |
|
390 |
|
391 if (nargin == 0) |
|
392 { |
|
393 print_quad_option_list (octave_stdout); |
|
394 return retval; |
|
395 } |
|
396 else if (nargin == 1 || nargin == 2) |
|
397 { |
|
398 string keyword = args(0).string_value (); |
|
399 |
|
400 if (! error_state) |
|
401 { |
|
402 if (nargin == 1) |
|
403 return show_quad_option (keyword); |
|
404 else |
|
405 { |
|
406 double val = args(1).double_value (); |
|
407 |
|
408 if (! error_state) |
|
409 { |
|
410 set_quad_option (keyword, val); |
|
411 return retval; |
|
412 } |
|
413 } |
|
414 } |
|
415 } |
|
416 |
|
417 print_usage ("quad_options"); |
|
418 |
|
419 return retval; |
|
420 } |
|
421 |
|
422 /* |
|
423 ;;; Local Variables: *** |
|
424 ;;; mode: C++ *** |
|
425 ;;; End: *** |
|
426 */ |