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