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