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