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