7
|
1 // f-quad.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
289
|
28 #include <strstream.h> |
|
29 |
1
|
30 #include "Quad.h" |
|
31 |
|
32 #include "tree-const.h" |
|
33 #include "variables.h" |
|
34 #include "mappers.h" |
|
35 #include "gripes.h" |
|
36 #include "error.h" |
|
37 #include "utils.h" |
289
|
38 #include "pager.h" |
544
|
39 #include "help.h" |
519
|
40 #include "defun-dld.h" |
1
|
41 |
|
42 // Global pointer for user defined function required by quadrature functions. |
488
|
43 static tree_fvc *quad_fcn; |
1
|
44 |
289
|
45 static Quad_options quad_opts; |
|
46 |
1
|
47 double |
|
48 quad_user_function (double x) |
|
49 { |
|
50 double retval = 0.0; |
|
51 |
565
|
52 Octave_object args; |
712
|
53 args(0) = x; |
1
|
54 |
519
|
55 if (quad_fcn) |
1
|
56 { |
506
|
57 Octave_object tmp = quad_fcn->eval (0, 1, args); |
260
|
58 |
|
59 if (error_state) |
|
60 { |
|
61 quad_integration_error = 1; // XXX FIXME XXX |
|
62 gripe_user_supplied_eval ("quad"); |
|
63 return retval; |
|
64 } |
|
65 |
497
|
66 if (tmp.length () && tmp(0).is_defined ()) |
636
|
67 { |
|
68 retval = tmp(0).double_value (); |
|
69 |
|
70 if (error_state) |
|
71 { |
|
72 quad_integration_error = 1; // XXX FIXME XXX |
|
73 gripe_user_supplied_eval ("quad"); |
|
74 } |
|
75 } |
1
|
76 else |
|
77 { |
260
|
78 quad_integration_error = 1; // XXX FIXME XXX |
1
|
79 gripe_user_supplied_eval ("quad"); |
|
80 } |
|
81 } |
|
82 |
|
83 return retval; |
|
84 } |
|
85 |
718
|
86 DEFUN_DLD_BUILTIN ("quad", Fquad, Squad, 5, 3, |
519
|
87 "[V, IER, NFUN] = quad (F, A, B [, TOL] [, SING])\n\ |
|
88 \n\ |
|
89 Where the first argument is the name of the function to call to\n\ |
|
90 compute the value of the integrand. It must have the form\n\ |
|
91 \n\ |
|
92 y = f (x) |
|
93 \n\ |
|
94 where y and x are scalars.\n\ |
|
95 \n\ |
|
96 The second and third arguments are limits of integration. Either or\n\ |
|
97 both may be infinite. The optional argument TOL specifies the desired\n\ |
|
98 accuracy of the result. The optional argument SING is a vector of\n\ |
|
99 at which the integrand is singular.") |
1
|
100 { |
497
|
101 Octave_object retval; |
1
|
102 |
506
|
103 int nargin = args.length (); |
|
104 |
712
|
105 if (nargin < 3 || nargin > 5 || nargout > 4) |
519
|
106 { |
|
107 print_usage ("quad"); |
|
108 return retval; |
|
109 } |
|
110 |
718
|
111 quad_fcn = is_valid_function (args(0), "quad", 1); |
|
112 if (! quad_fcn || takes_correct_nargs (quad_fcn, 1, "quad", 1) != 1) |
1
|
113 return retval; |
|
114 |
712
|
115 double a = args(1).double_value (); |
636
|
116 |
|
117 if (error_state) |
|
118 { |
|
119 error ("quad: expecting second argument to be a scalar"); |
|
120 return retval; |
|
121 } |
|
122 |
712
|
123 double b = args(2).double_value (); |
1
|
124 |
636
|
125 if (error_state) |
|
126 { |
|
127 error ("quad: expecting third argument to be a scalar"); |
|
128 return retval; |
|
129 } |
|
130 |
1
|
131 int indefinite = 0; |
|
132 IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite; |
|
133 double bound = 0.0; |
|
134 if ((int) xisinf (a) && (int) xisinf (b)) |
|
135 { |
|
136 indefinite = 1; |
|
137 indef_type = IndefQuad::doubly_infinite; |
|
138 } |
|
139 else if ((int) xisinf (a)) |
|
140 { |
|
141 indefinite = 1; |
|
142 bound = b; |
|
143 indef_type = IndefQuad::neg_inf_to_bound; |
|
144 } |
|
145 else if ((int) xisinf (b)) |
|
146 { |
|
147 indefinite = 1; |
|
148 bound = a; |
|
149 indef_type = IndefQuad::bound_to_inf; |
|
150 } |
|
151 |
|
152 int ier = 0; |
|
153 int nfun = 0; |
|
154 double abserr = 0.0; |
|
155 double val = 0.0; |
|
156 double abstol = 1e-6; |
|
157 double reltol = 1e-6; |
|
158 Vector tol (2); |
|
159 Vector sing; |
|
160 int have_sing = 0; |
|
161 switch (nargin) |
|
162 { |
712
|
163 case 5: |
1
|
164 if (indefinite) |
|
165 { |
216
|
166 error("quad: singularities not allowed on infinite intervals"); |
1
|
167 return retval; |
|
168 } |
636
|
169 |
1
|
170 have_sing = 1; |
636
|
171 |
712
|
172 sing = args(4).vector_value (); |
636
|
173 |
|
174 if (error_state) |
|
175 { |
|
176 error ("quad: expecting vector of singularities as fourth argument"); |
|
177 return retval; |
|
178 } |
|
179 |
712
|
180 case 4: |
|
181 tol = args(3).vector_value (); |
636
|
182 |
|
183 if (error_state) |
|
184 { |
|
185 error ("quad: expecting vector of tolerances as fifth argument"); |
|
186 return retval; |
|
187 } |
|
188 |
1
|
189 switch (tol.capacity ()) |
|
190 { |
|
191 case 2: |
|
192 reltol = tol.elem (1); |
636
|
193 |
1
|
194 case 1: |
|
195 abstol = tol.elem (0); |
|
196 break; |
636
|
197 |
1
|
198 default: |
216
|
199 error ("quad: expecting tol to contain no more than two values"); |
1
|
200 return retval; |
|
201 } |
636
|
202 |
712
|
203 case 3: |
1
|
204 if (indefinite) |
|
205 { |
|
206 IndefQuad iq (quad_user_function, bound, indef_type, abstol, reltol); |
289
|
207 iq.copy (quad_opts); |
1
|
208 val = iq.integrate (ier, nfun, abserr); |
|
209 } |
|
210 else |
|
211 { |
|
212 if (have_sing) |
|
213 { |
|
214 DefQuad dq (quad_user_function, a, b, sing, abstol, reltol); |
289
|
215 dq.copy (quad_opts); |
1
|
216 val = dq.integrate (ier, nfun, abserr); |
|
217 } |
|
218 else |
|
219 { |
|
220 DefQuad dq (quad_user_function, a, b, abstol, reltol); |
289
|
221 dq.copy (quad_opts); |
1
|
222 val = dq.integrate (ier, nfun, abserr); |
|
223 } |
|
224 } |
|
225 break; |
636
|
226 |
1
|
227 default: |
|
228 panic_impossible (); |
|
229 break; |
|
230 } |
|
231 |
636
|
232 retval(3) = abserr; |
|
233 retval(2) = nfun; |
|
234 retval(1) = ier; |
516
|
235 retval(0) = val; |
1
|
236 |
|
237 return retval; |
|
238 } |
|
239 |
289
|
240 typedef void (Quad_options::*d_set_opt_mf) (double); |
|
241 typedef double (Quad_options::*d_get_opt_mf) (void); |
|
242 |
|
243 #define MAX_TOKENS 2 |
|
244 |
|
245 struct QUAD_OPTIONS |
|
246 { |
540
|
247 const char *keyword; |
|
248 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
249 int min_len[MAX_TOKENS + 1]; |
|
250 int min_toks_to_match; |
|
251 d_set_opt_mf d_set_fcn; |
|
252 d_get_opt_mf d_get_fcn; |
|
253 }; |
|
254 |
497
|
255 static QUAD_OPTIONS quad_option_table [] = |
289
|
256 { |
|
257 { "absolute tolerance", |
519
|
258 { "absolute", "tolerance", 0, }, |
289
|
259 { 1, 0, 0, }, 1, |
|
260 Quad_options::set_absolute_tolerance, |
|
261 Quad_options::absolute_tolerance, }, |
|
262 |
|
263 { "relative tolerance", |
519
|
264 { "relative", "tolerance", 0, }, |
289
|
265 { 1, 0, 0, }, 1, |
|
266 Quad_options::set_relative_tolerance, |
|
267 Quad_options::relative_tolerance, }, |
|
268 |
519
|
269 { 0, |
|
270 { 0, 0, 0, }, |
289
|
271 { 0, 0, 0, }, 0, |
519
|
272 0, 0, }, |
289
|
273 }; |
|
274 |
|
275 static void |
|
276 print_quad_option_list (void) |
|
277 { |
|
278 ostrstream output_buf; |
|
279 |
|
280 print_usage ("quad_options", 1); |
|
281 |
|
282 output_buf << "\n" |
|
283 << "Options for quad include:\n\n" |
|
284 << " keyword value\n" |
|
285 << " ------- -----\n\n"; |
|
286 |
|
287 QUAD_OPTIONS *list = quad_option_table; |
|
288 |
540
|
289 const char *keyword; |
519
|
290 while ((keyword = list->keyword) != 0) |
289
|
291 { |
|
292 output_buf.form (" %-40s ", keyword); |
|
293 |
|
294 double val = (quad_opts.*list->d_get_fcn) (); |
|
295 if (val < 0.0) |
|
296 output_buf << "computed automatically"; |
|
297 else |
|
298 output_buf << val; |
|
299 |
|
300 output_buf << "\n"; |
|
301 list++; |
|
302 } |
|
303 |
|
304 output_buf << "\n" << ends; |
|
305 maybe_page_output (output_buf); |
|
306 } |
|
307 |
|
308 static void |
|
309 do_quad_option (char *keyword, double val) |
|
310 { |
|
311 QUAD_OPTIONS *list = quad_option_table; |
|
312 |
519
|
313 while (list->keyword != 0) |
289
|
314 { |
|
315 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
316 list->min_toks_to_match, MAX_TOKENS)) |
|
317 { |
|
318 (quad_opts.*list->d_set_fcn) (val); |
|
319 |
|
320 return; |
|
321 } |
|
322 list++; |
|
323 } |
|
324 |
|
325 warning ("quad_options: no match for `%s'", keyword); |
|
326 } |
|
327 |
701
|
328 DEFUN_DLD_BUILTIN ("quad_options", Fquad_options, Squad_options, -1, 1, |
519
|
329 "quad_options (KEYWORD, VALUE)\n\ |
|
330 \n\ |
|
331 Set or show options for quad. Keywords may be abbreviated\n\ |
|
332 to the shortest match.") |
272
|
333 { |
497
|
334 Octave_object retval; |
272
|
335 |
506
|
336 int nargin = args.length (); |
|
337 |
712
|
338 if (nargin == 0) |
636
|
339 { |
|
340 print_quad_option_list (); |
|
341 return retval; |
|
342 } |
712
|
343 else if (nargin == 2) |
289
|
344 { |
712
|
345 char *keyword = args(0).string_value (); |
636
|
346 |
|
347 if (! error_state) |
289
|
348 { |
712
|
349 double val = args(1).double_value (); |
636
|
350 |
|
351 if (! error_state) |
|
352 { |
|
353 do_quad_option (keyword, val); |
|
354 return retval; |
|
355 } |
289
|
356 } |
|
357 } |
636
|
358 |
|
359 print_usage ("quad_options"); |
289
|
360 |
272
|
361 return retval; |
|
362 } |
|
363 |
1
|
364 /* |
|
365 ;;; Local Variables: *** |
|
366 ;;; mode: C++ *** |
|
367 ;;; page-delimiter: "^/\\*" *** |
|
368 ;;; End: *** |
|
369 */ |