7
|
1 // f-quad.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1993, 1994 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 |
|
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 |
516
|
52 // tree_constant name = quad_fcn->name (); |
497
|
53 Octave_object args (2); |
|
54 // args(0) = name; |
516
|
55 args(1) = x; |
1
|
56 |
519
|
57 if (quad_fcn) |
1
|
58 { |
506
|
59 Octave_object tmp = quad_fcn->eval (0, 1, args); |
260
|
60 |
|
61 if (error_state) |
|
62 { |
|
63 quad_integration_error = 1; // XXX FIXME XXX |
|
64 gripe_user_supplied_eval ("quad"); |
|
65 return retval; |
|
66 } |
|
67 |
497
|
68 if (tmp.length () && tmp(0).is_defined ()) |
|
69 retval = tmp(0).to_scalar (); |
1
|
70 else |
|
71 { |
260
|
72 quad_integration_error = 1; // XXX FIXME XXX |
1
|
73 gripe_user_supplied_eval ("quad"); |
|
74 } |
|
75 } |
|
76 |
|
77 return retval; |
|
78 } |
|
79 |
519
|
80 DEFUN_DLD ("quad", Fquad, Squad, 6, 3, |
|
81 "[V, IER, NFUN] = quad (F, A, B [, TOL] [, SING])\n\ |
|
82 \n\ |
|
83 Where the first argument is the name of the function to call to\n\ |
|
84 compute the value of the integrand. It must have the form\n\ |
|
85 \n\ |
|
86 y = f (x) |
|
87 \n\ |
|
88 where y and x are scalars.\n\ |
|
89 \n\ |
|
90 The second and third arguments are limits of integration. Either or\n\ |
|
91 both may be infinite. The optional argument TOL specifies the desired\n\ |
|
92 accuracy of the result. The optional argument SING is a vector of\n\ |
|
93 at which the integrand is singular.") |
1
|
94 { |
497
|
95 Octave_object retval; |
1
|
96 |
506
|
97 int nargin = args.length (); |
|
98 |
519
|
99 if (nargin < 4 || nargin > 6 || nargout > 4) |
|
100 { |
|
101 print_usage ("quad"); |
|
102 return retval; |
|
103 } |
|
104 |
497
|
105 quad_fcn = is_valid_function (args(1), "fsolve", 1); |
519
|
106 if (! quad_fcn || takes_correct_nargs (quad_fcn, 2, "fsolve", 1) != 1) |
1
|
107 return retval; |
|
108 |
497
|
109 double a = args(2).to_scalar (); |
|
110 double b = args(3).to_scalar (); |
1
|
111 |
|
112 int indefinite = 0; |
|
113 IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite; |
|
114 double bound = 0.0; |
|
115 if ((int) xisinf (a) && (int) xisinf (b)) |
|
116 { |
|
117 indefinite = 1; |
|
118 indef_type = IndefQuad::doubly_infinite; |
|
119 } |
|
120 else if ((int) xisinf (a)) |
|
121 { |
|
122 indefinite = 1; |
|
123 bound = b; |
|
124 indef_type = IndefQuad::neg_inf_to_bound; |
|
125 } |
|
126 else if ((int) xisinf (b)) |
|
127 { |
|
128 indefinite = 1; |
|
129 bound = a; |
|
130 indef_type = IndefQuad::bound_to_inf; |
|
131 } |
|
132 |
|
133 int ier = 0; |
|
134 int nfun = 0; |
|
135 double abserr = 0.0; |
|
136 double val = 0.0; |
|
137 double abstol = 1e-6; |
|
138 double reltol = 1e-6; |
|
139 Vector tol (2); |
|
140 Vector sing; |
|
141 int have_sing = 0; |
|
142 switch (nargin) |
|
143 { |
|
144 case 6: |
|
145 if (indefinite) |
|
146 { |
216
|
147 error("quad: singularities not allowed on infinite intervals"); |
1
|
148 return retval; |
|
149 } |
|
150 have_sing = 1; |
497
|
151 sing = args(5).to_vector (); |
1
|
152 case 5: |
497
|
153 tol = args(4).to_vector (); |
1
|
154 switch (tol.capacity ()) |
|
155 { |
|
156 case 2: |
|
157 reltol = tol.elem (1); |
|
158 case 1: |
|
159 abstol = tol.elem (0); |
|
160 break; |
|
161 default: |
216
|
162 error ("quad: expecting tol to contain no more than two values"); |
1
|
163 return retval; |
|
164 } |
|
165 case 4: |
|
166 if (indefinite) |
|
167 { |
|
168 IndefQuad iq (quad_user_function, bound, indef_type, abstol, reltol); |
289
|
169 iq.copy (quad_opts); |
1
|
170 val = iq.integrate (ier, nfun, abserr); |
|
171 } |
|
172 else |
|
173 { |
|
174 if (have_sing) |
|
175 { |
|
176 DefQuad dq (quad_user_function, a, b, sing, abstol, reltol); |
289
|
177 dq.copy (quad_opts); |
1
|
178 val = dq.integrate (ier, nfun, abserr); |
|
179 } |
|
180 else |
|
181 { |
|
182 DefQuad dq (quad_user_function, a, b, abstol, reltol); |
289
|
183 dq.copy (quad_opts); |
1
|
184 val = dq.integrate (ier, nfun, abserr); |
|
185 } |
|
186 } |
|
187 break; |
|
188 default: |
|
189 panic_impossible (); |
|
190 break; |
|
191 } |
|
192 |
497
|
193 retval.resize (4); |
1
|
194 |
516
|
195 retval(0) = val; |
|
196 retval(1) = ier; |
|
197 retval(2) = nfun; |
|
198 retval(3) = abserr; |
1
|
199 |
|
200 return retval; |
|
201 } |
|
202 |
289
|
203 typedef void (Quad_options::*d_set_opt_mf) (double); |
|
204 typedef double (Quad_options::*d_get_opt_mf) (void); |
|
205 |
|
206 #define MAX_TOKENS 2 |
|
207 |
|
208 struct QUAD_OPTIONS |
|
209 { |
540
|
210 const char *keyword; |
|
211 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
212 int min_len[MAX_TOKENS + 1]; |
|
213 int min_toks_to_match; |
|
214 d_set_opt_mf d_set_fcn; |
|
215 d_get_opt_mf d_get_fcn; |
|
216 }; |
|
217 |
497
|
218 static QUAD_OPTIONS quad_option_table [] = |
289
|
219 { |
|
220 { "absolute tolerance", |
519
|
221 { "absolute", "tolerance", 0, }, |
289
|
222 { 1, 0, 0, }, 1, |
|
223 Quad_options::set_absolute_tolerance, |
|
224 Quad_options::absolute_tolerance, }, |
|
225 |
|
226 { "relative tolerance", |
519
|
227 { "relative", "tolerance", 0, }, |
289
|
228 { 1, 0, 0, }, 1, |
|
229 Quad_options::set_relative_tolerance, |
|
230 Quad_options::relative_tolerance, }, |
|
231 |
519
|
232 { 0, |
|
233 { 0, 0, 0, }, |
289
|
234 { 0, 0, 0, }, 0, |
519
|
235 0, 0, }, |
289
|
236 }; |
|
237 |
|
238 static void |
|
239 print_quad_option_list (void) |
|
240 { |
|
241 ostrstream output_buf; |
|
242 |
|
243 print_usage ("quad_options", 1); |
|
244 |
|
245 output_buf << "\n" |
|
246 << "Options for quad include:\n\n" |
|
247 << " keyword value\n" |
|
248 << " ------- -----\n\n"; |
|
249 |
|
250 QUAD_OPTIONS *list = quad_option_table; |
|
251 |
540
|
252 const char *keyword; |
519
|
253 while ((keyword = list->keyword) != 0) |
289
|
254 { |
|
255 output_buf.form (" %-40s ", keyword); |
|
256 |
|
257 double val = (quad_opts.*list->d_get_fcn) (); |
|
258 if (val < 0.0) |
|
259 output_buf << "computed automatically"; |
|
260 else |
|
261 output_buf << val; |
|
262 |
|
263 output_buf << "\n"; |
|
264 list++; |
|
265 } |
|
266 |
|
267 output_buf << "\n" << ends; |
|
268 maybe_page_output (output_buf); |
|
269 } |
|
270 |
|
271 static void |
|
272 do_quad_option (char *keyword, double val) |
|
273 { |
|
274 QUAD_OPTIONS *list = quad_option_table; |
|
275 |
519
|
276 while (list->keyword != 0) |
289
|
277 { |
|
278 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
279 list->min_toks_to_match, MAX_TOKENS)) |
|
280 { |
|
281 (quad_opts.*list->d_set_fcn) (val); |
|
282 |
|
283 return; |
|
284 } |
|
285 list++; |
|
286 } |
|
287 |
|
288 warning ("quad_options: no match for `%s'", keyword); |
|
289 } |
|
290 |
519
|
291 DEFUN_DLD ("quad_options", Fquad_options, Squad_options, -1, 1, |
|
292 "quad_options (KEYWORD, VALUE)\n\ |
|
293 \n\ |
|
294 Set or show options for quad. Keywords may be abbreviated\n\ |
|
295 to the shortest match.") |
272
|
296 { |
497
|
297 Octave_object retval; |
272
|
298 |
506
|
299 int nargin = args.length (); |
|
300 |
289
|
301 if (nargin == 1) |
497
|
302 print_quad_option_list (); |
289
|
303 else if (nargin == 3) |
|
304 { |
497
|
305 if (args(1).is_string_type ()) |
289
|
306 { |
497
|
307 char *keyword = args(1).string_value (); |
|
308 double val = args(2).double_value (); |
289
|
309 do_quad_option (keyword, val); |
|
310 } |
|
311 else |
|
312 print_usage ("quad_options"); |
|
313 } |
|
314 else |
497
|
315 print_usage ("quad_options"); |
289
|
316 |
272
|
317 return retval; |
|
318 } |
|
319 |
1
|
320 /* |
|
321 ;;; Local Variables: *** |
|
322 ;;; mode: C++ *** |
|
323 ;;; page-delimiter: "^/\\*" *** |
|
324 ;;; End: *** |
|
325 */ |