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