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" |
2928
|
40 #include "utils.h" |
|
41 #include "variables.h" |
|
42 |
|
43 #if defined (quad) |
|
44 #undef quad |
|
45 #endif |
|
46 |
|
47 // Global pointer for user defined function required by quadrature functions. |
2968
|
48 static octave_function *quad_fcn; |
2928
|
49 |
|
50 static Quad_options quad_opts; |
|
51 |
|
52 double |
|
53 quad_user_function (double x) |
|
54 { |
|
55 double retval = 0.0; |
|
56 |
|
57 octave_value_list args; |
|
58 args(0) = x; |
|
59 |
|
60 if (quad_fcn) |
|
61 { |
2968
|
62 octave_value_list tmp = quad_fcn->do_index_op (1, args); |
2928
|
63 |
|
64 if (error_state) |
|
65 { |
|
66 quad_integration_error = 1; // XXX FIXME XXX |
|
67 gripe_user_supplied_eval ("quad"); |
|
68 return retval; |
|
69 } |
|
70 |
|
71 if (tmp.length () && tmp(0).is_defined ()) |
|
72 { |
|
73 retval = tmp(0).double_value (); |
|
74 |
|
75 if (error_state) |
|
76 { |
|
77 quad_integration_error = 1; // XXX FIXME XXX |
|
78 gripe_user_supplied_eval ("quad"); |
|
79 } |
|
80 } |
|
81 else |
|
82 { |
|
83 quad_integration_error = 1; // XXX FIXME XXX |
|
84 gripe_user_supplied_eval ("quad"); |
|
85 } |
|
86 } |
|
87 |
|
88 return retval; |
|
89 } |
|
90 |
|
91 DEFUN_DLD (quad, args, nargout, |
|
92 "[V, IER, NFUN] = quad (F, A, B [, TOL] [, SING])\n\ |
|
93 \n\ |
|
94 Where the first argument is the name of the function to call to\n\ |
|
95 compute the value of the integrand. It must have the form\n\ |
|
96 \n\ |
|
97 y = f (x)\n\ |
|
98 \n\ |
|
99 where y and x are scalars.\n\ |
|
100 \n\ |
|
101 The second and third arguments are limits of integration. Either or\n\ |
|
102 both may be infinite.\n\ |
|
103 \n\ |
|
104 The optional argument tol is a vector that specifies the desired\n\ |
|
105 accuracy of the result. The first element of the vector is the desired\n\ |
|
106 absolute tolerance, and the second element is the desired relative\n\ |
|
107 tolerance.\n\ |
|
108 \n\ |
|
109 The optional argument @var{sing} is a vector of values at which the\n\ |
|
110 integrand is singular.") |
|
111 { |
|
112 octave_value_list retval; |
|
113 |
|
114 int nargin = args.length (); |
|
115 |
|
116 if (nargin < 3 || nargin > 5 || nargout > 4) |
|
117 { |
|
118 print_usage ("quad"); |
|
119 return retval; |
|
120 } |
|
121 |
|
122 quad_fcn = extract_function (args(0), "quad", "__quad_fcn__", |
|
123 "function y = __quad_fcn__ (x) y = ", |
|
124 "; endfunction"); |
|
125 if (! quad_fcn) |
|
126 return retval; |
|
127 |
|
128 double a = args(1).double_value (); |
|
129 |
|
130 if (error_state) |
|
131 { |
|
132 error ("quad: expecting second argument to be a scalar"); |
|
133 return retval; |
|
134 } |
|
135 |
|
136 double b = args(2).double_value (); |
|
137 |
|
138 if (error_state) |
|
139 { |
|
140 error ("quad: expecting third argument to be a scalar"); |
|
141 return retval; |
|
142 } |
|
143 |
|
144 int indefinite = 0; |
|
145 IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite; |
|
146 double bound = 0.0; |
|
147 if (xisinf (a) && xisinf (b)) |
|
148 { |
|
149 indefinite = 1; |
|
150 indef_type = IndefQuad::doubly_infinite; |
|
151 } |
|
152 else if (xisinf (a)) |
|
153 { |
|
154 indefinite = 1; |
|
155 bound = b; |
|
156 indef_type = IndefQuad::neg_inf_to_bound; |
|
157 } |
|
158 else if (xisinf (b)) |
|
159 { |
|
160 indefinite = 1; |
|
161 bound = a; |
|
162 indef_type = IndefQuad::bound_to_inf; |
|
163 } |
|
164 |
|
165 int ier = 0; |
|
166 int nfun = 0; |
|
167 double abserr = 0.0; |
|
168 double val = 0.0; |
|
169 double abstol = 1e-6; |
|
170 double reltol = 1e-6; |
|
171 ColumnVector tol (2); |
|
172 ColumnVector sing; |
|
173 int have_sing = 0; |
|
174 switch (nargin) |
|
175 { |
|
176 case 5: |
|
177 if (indefinite) |
|
178 { |
|
179 error("quad: singularities not allowed on infinite intervals"); |
|
180 return retval; |
|
181 } |
|
182 |
|
183 have_sing = 1; |
|
184 |
|
185 sing = args(4).vector_value (); |
|
186 |
|
187 if (error_state) |
|
188 { |
|
189 error ("quad: expecting vector of singularities as fourth argument"); |
|
190 return retval; |
|
191 } |
|
192 |
|
193 case 4: |
|
194 tol = args(3).vector_value (); |
|
195 |
|
196 if (error_state) |
|
197 { |
|
198 error ("quad: expecting vector of tolerances as fifth argument"); |
|
199 return retval; |
|
200 } |
|
201 |
|
202 switch (tol.capacity ()) |
|
203 { |
|
204 case 2: |
|
205 reltol = tol (1); |
|
206 |
|
207 case 1: |
|
208 abstol = tol (0); |
|
209 break; |
|
210 |
|
211 default: |
|
212 error ("quad: expecting tol to contain no more than two values"); |
|
213 return retval; |
|
214 } |
|
215 |
|
216 case 3: |
|
217 if (indefinite) |
|
218 { |
|
219 IndefQuad iq (quad_user_function, bound, indef_type, abstol, reltol); |
|
220 iq.set_options (quad_opts); |
|
221 val = iq.integrate (ier, nfun, abserr); |
|
222 } |
|
223 else |
|
224 { |
|
225 if (have_sing) |
|
226 { |
|
227 DefQuad dq (quad_user_function, a, b, sing, abstol, reltol); |
|
228 dq.set_options (quad_opts); |
|
229 val = dq.integrate (ier, nfun, abserr); |
|
230 } |
|
231 else |
|
232 { |
|
233 DefQuad dq (quad_user_function, a, b, abstol, reltol); |
|
234 dq.set_options (quad_opts); |
|
235 val = dq.integrate (ier, nfun, abserr); |
|
236 } |
|
237 } |
|
238 break; |
|
239 |
|
240 default: |
|
241 panic_impossible (); |
|
242 break; |
|
243 } |
|
244 |
|
245 retval(3) = abserr; |
|
246 retval(2) = static_cast<double> (nfun); |
|
247 retval(1) = static_cast<double> (ier); |
|
248 retval(0) = val; |
|
249 |
|
250 return retval; |
|
251 } |
|
252 |
|
253 typedef void (Quad_options::*d_set_opt_mf) (double); |
|
254 typedef double (Quad_options::*d_get_opt_mf) (void); |
|
255 |
|
256 #define MAX_TOKENS 2 |
|
257 |
|
258 struct QUAD_OPTIONS |
|
259 { |
|
260 const char *keyword; |
|
261 const char *kw_tok[MAX_TOKENS + 1]; |
|
262 int min_len[MAX_TOKENS + 1]; |
|
263 int min_toks_to_match; |
|
264 d_set_opt_mf d_set_fcn; |
|
265 d_get_opt_mf d_get_fcn; |
|
266 }; |
|
267 |
|
268 static QUAD_OPTIONS quad_option_table [] = |
|
269 { |
|
270 { "absolute tolerance", |
|
271 { "absolute", "tolerance", 0, }, |
|
272 { 1, 0, 0, }, 1, |
3131
|
273 &Quad_options::set_absolute_tolerance, |
|
274 &Quad_options::absolute_tolerance, }, |
2928
|
275 |
|
276 { "relative tolerance", |
|
277 { "relative", "tolerance", 0, }, |
|
278 { 1, 0, 0, }, 1, |
3131
|
279 &Quad_options::set_relative_tolerance, |
|
280 &Quad_options::relative_tolerance, }, |
2928
|
281 |
|
282 { 0, |
|
283 { 0, 0, 0, }, |
|
284 { 0, 0, 0, }, 0, |
|
285 0, 0, }, |
|
286 }; |
|
287 |
|
288 static void |
|
289 print_quad_option_list (ostream& os) |
|
290 { |
|
291 print_usage ("quad_options", 1); |
|
292 |
|
293 os << "\n" |
|
294 << "Options for quad include:\n\n" |
|
295 << " keyword value\n" |
|
296 << " ------- -----\n\n"; |
|
297 |
|
298 QUAD_OPTIONS *list = quad_option_table; |
|
299 |
|
300 const char *keyword; |
|
301 while ((keyword = list->keyword) != 0) |
|
302 { |
|
303 os.form (" %-40s ", keyword); |
|
304 |
|
305 double val = (quad_opts.*list->d_get_fcn) (); |
|
306 if (val < 0.0) |
|
307 os << "computed automatically"; |
|
308 else |
|
309 os << val; |
|
310 |
|
311 os << "\n"; |
|
312 list++; |
|
313 } |
|
314 |
|
315 os << "\n"; |
|
316 } |
|
317 |
|
318 static void |
|
319 set_quad_option (const string& keyword, double val) |
|
320 { |
|
321 QUAD_OPTIONS *list = quad_option_table; |
|
322 |
|
323 while (list->keyword != 0) |
|
324 { |
|
325 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
326 list->min_toks_to_match, MAX_TOKENS)) |
|
327 { |
|
328 (quad_opts.*list->d_set_fcn) (val); |
|
329 |
|
330 return; |
|
331 } |
|
332 list++; |
|
333 } |
|
334 |
|
335 warning ("quad_options: no match for `%s'", keyword.c_str ()); |
|
336 } |
|
337 |
|
338 static octave_value_list |
|
339 show_quad_option (const string& keyword) |
|
340 { |
|
341 octave_value retval; |
|
342 |
|
343 QUAD_OPTIONS *list = quad_option_table; |
|
344 |
|
345 while (list->keyword != 0) |
|
346 { |
|
347 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
348 list->min_toks_to_match, MAX_TOKENS)) |
|
349 { |
|
350 return (quad_opts.*list->d_get_fcn) (); |
|
351 } |
|
352 list++; |
|
353 } |
|
354 |
|
355 warning ("quad_options: no match for `%s'", keyword.c_str ()); |
|
356 |
|
357 return retval; |
|
358 } |
|
359 |
|
360 DEFUN_DLD (quad_options, args, , |
|
361 "quad_options (KEYWORD, VALUE)\n\ |
|
362 \n\ |
|
363 Set or show options for quad. Keywords may be abbreviated\n\ |
|
364 to the shortest match.") |
|
365 { |
|
366 octave_value_list retval; |
|
367 |
|
368 int nargin = args.length (); |
|
369 |
|
370 if (nargin == 0) |
|
371 { |
|
372 print_quad_option_list (octave_stdout); |
|
373 return retval; |
|
374 } |
|
375 else if (nargin == 1 || nargin == 2) |
|
376 { |
|
377 string keyword = args(0).string_value (); |
|
378 |
|
379 if (! error_state) |
|
380 { |
|
381 if (nargin == 1) |
|
382 return show_quad_option (keyword); |
|
383 else |
|
384 { |
|
385 double val = args(1).double_value (); |
|
386 |
|
387 if (! error_state) |
|
388 { |
|
389 set_quad_option (keyword, val); |
|
390 return retval; |
|
391 } |
|
392 } |
|
393 } |
|
394 } |
|
395 |
|
396 print_usage ("quad_options"); |
|
397 |
|
398 return retval; |
|
399 } |
|
400 |
|
401 /* |
|
402 ;;; Local Variables: *** |
|
403 ;;; mode: C++ *** |
|
404 ;;; End: *** |
|
405 */ |