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