2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
3567
|
29 #include <iomanip> |
3523
|
30 #include <iostream> |
2928
|
31 |
|
32 #include "Quad.h" |
|
33 #include "lo-mappers.h" |
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "pager.h" |
|
39 #include "oct-obj.h" |
2968
|
40 #include "ov-fcn.h" |
3243
|
41 #include "unwind-prot.h" |
2928
|
42 #include "utils.h" |
|
43 #include "variables.h" |
|
44 |
3998
|
45 #include "Quad-opts.cc" |
|
46 |
2928
|
47 #if defined (quad) |
|
48 #undef quad |
|
49 #endif |
|
50 |
|
51 // Global pointer for user defined function required by quadrature functions. |
2968
|
52 static octave_function *quad_fcn; |
2928
|
53 |
4140
|
54 // Have we warned about imaginary values returned from user function? |
|
55 static bool warned_imaginary = false; |
|
56 |
3243
|
57 // Is this a recursive call? |
|
58 static int call_depth = 0; |
|
59 |
2928
|
60 double |
|
61 quad_user_function (double x) |
|
62 { |
|
63 double retval = 0.0; |
|
64 |
|
65 octave_value_list args; |
|
66 args(0) = x; |
|
67 |
|
68 if (quad_fcn) |
|
69 { |
3544
|
70 octave_value_list tmp = quad_fcn->do_multi_index_op (1, args); |
2928
|
71 |
|
72 if (error_state) |
|
73 { |
|
74 quad_integration_error = 1; // XXX FIXME XXX |
|
75 gripe_user_supplied_eval ("quad"); |
|
76 return retval; |
|
77 } |
|
78 |
|
79 if (tmp.length () && tmp(0).is_defined ()) |
|
80 { |
4140
|
81 if (! warned_imaginary && tmp(0).is_complex_type ()) |
|
82 { |
|
83 warning ("quad: ignoring imaginary part returned from user-supplied function"); |
|
84 warned_imaginary = true; |
|
85 } |
|
86 |
2928
|
87 retval = tmp(0).double_value (); |
|
88 |
|
89 if (error_state) |
|
90 { |
|
91 quad_integration_error = 1; // XXX FIXME XXX |
|
92 gripe_user_supplied_eval ("quad"); |
|
93 } |
|
94 } |
|
95 else |
|
96 { |
|
97 quad_integration_error = 1; // XXX FIXME XXX |
|
98 gripe_user_supplied_eval ("quad"); |
|
99 } |
|
100 } |
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
3323
|
105 #define QUAD_ABORT() \ |
|
106 do \ |
|
107 { \ |
4954
|
108 if (fcn_name.length()) \ |
|
109 clear_function (fcn_name); \ |
3323
|
110 unwind_protect::run_frame ("Fquad"); \ |
|
111 return retval; \ |
|
112 } \ |
|
113 while (0) |
|
114 |
|
115 #define QUAD_ABORT1(msg) \ |
|
116 do \ |
|
117 { \ |
3747
|
118 ::error ("quad: " msg); \ |
3323
|
119 QUAD_ABORT (); \ |
|
120 } \ |
|
121 while (0) |
|
122 |
|
123 #define QUAD_ABORT2(fmt, arg) \ |
|
124 do \ |
|
125 { \ |
3747
|
126 ::error ("quad: " fmt, arg); \ |
3323
|
127 QUAD_ABORT (); \ |
|
128 } \ |
|
129 while (0) |
|
130 |
2928
|
131 DEFUN_DLD (quad, args, nargout, |
3368
|
132 "-*- texinfo -*-\n\ |
|
133 @deftypefn {Loadable Function} {[@var{v}, @var{ier}, @var{nfun}, @var{err}] =} quad (@var{f}, @var{a}, @var{b}, @var{tol}, @var{sing})\n\ |
|
134 Integrate a nonlinear function of one variable using Quadpack.\n\ |
4954
|
135 The first argument is the name of the function, the function handle or\n\ |
|
136 the inline function to call to compute the value of the integrand. It\n\ |
|
137 must have the form\n\ |
2928
|
138 \n\ |
3368
|
139 @example\n\ |
|
140 y = f (x)\n\ |
|
141 @end example\n\ |
2928
|
142 \n\ |
3368
|
143 @noindent\n\ |
|
144 where @var{y} and @var{x} are scalars.\n\ |
2928
|
145 \n\ |
|
146 The second and third arguments are limits of integration. Either or\n\ |
|
147 both may be infinite.\n\ |
|
148 \n\ |
3368
|
149 The optional argument @var{tol} is a vector that specifies the desired\n\ |
2928
|
150 accuracy of the result. The first element of the vector is the desired\n\ |
|
151 absolute tolerance, and the second element is the desired relative\n\ |
3368
|
152 tolerance. To choose a relative test only, set the absolute\n\ |
|
153 tolerance to zero. To choose an absolute test only, set the relative\n\ |
|
154 tolerance to zero. \n\ |
2928
|
155 \n\ |
|
156 The optional argument @var{sing} is a vector of values at which the\n\ |
3368
|
157 integrand is known to be singular.\n\ |
|
158 \n\ |
|
159 The result of the integration is returned in @var{v} and @var{ier}\n\ |
|
160 contains an integer error code (0 indicates a successful integration).\n\ |
|
161 The value of @var{nfun} indicates how many function evaluations were\n\ |
|
162 required, and @var{err} contains an estimate of the error in the\n\ |
|
163 solution.\n\ |
3964
|
164 \n\ |
|
165 You can use the function @code{quad_options} to set optional\n\ |
|
166 parameters for @code{quad}.\n\ |
3368
|
167 @end deftypefn") |
2928
|
168 { |
|
169 octave_value_list retval; |
|
170 |
4954
|
171 std::string fcn_name; |
|
172 |
4140
|
173 warned_imaginary = false; |
|
174 |
3243
|
175 unwind_protect::begin_frame ("Fquad"); |
|
176 |
|
177 unwind_protect_int (call_depth); |
|
178 call_depth++; |
|
179 |
|
180 if (call_depth > 1) |
3323
|
181 QUAD_ABORT1 ("invalid recursive call"); |
3243
|
182 |
2928
|
183 int nargin = args.length (); |
|
184 |
3243
|
185 if (nargin > 2 && nargin < 6 && nargout < 5) |
|
186 { |
4954
|
187 if (args(0).is_function_handle () || args(0).is_inline_function ()) |
|
188 quad_fcn = args(0).function_value (); |
|
189 else |
|
190 { |
|
191 fcn_name = unique_symbol_name ("__quad__fcn__"); |
|
192 std::string fname = "function y = "; |
|
193 fname.append (fcn_name); |
|
194 fname.append ("(x) y = "); |
|
195 quad_fcn = extract_function (args(0), "quad", fcn_name, fname, |
|
196 "; endfunction"); |
|
197 } |
|
198 |
3243
|
199 if (! quad_fcn) |
3323
|
200 QUAD_ABORT (); |
3243
|
201 |
|
202 double a = args(1).double_value (); |
|
203 |
|
204 if (error_state) |
3323
|
205 QUAD_ABORT1 ("expecting second argument to be a scalar"); |
3243
|
206 |
|
207 double b = args(2).double_value (); |
|
208 |
|
209 if (error_state) |
3323
|
210 QUAD_ABORT1 ("expecting third argument to be a scalar"); |
3243
|
211 |
|
212 int indefinite = 0; |
|
213 IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite; |
|
214 double bound = 0.0; |
|
215 if (xisinf (a) && xisinf (b)) |
|
216 { |
|
217 indefinite = 1; |
|
218 indef_type = IndefQuad::doubly_infinite; |
|
219 } |
|
220 else if (xisinf (a)) |
|
221 { |
|
222 indefinite = 1; |
|
223 bound = b; |
|
224 indef_type = IndefQuad::neg_inf_to_bound; |
|
225 } |
|
226 else if (xisinf (b)) |
|
227 { |
|
228 indefinite = 1; |
|
229 bound = a; |
|
230 indef_type = IndefQuad::bound_to_inf; |
|
231 } |
|
232 |
|
233 int ier = 0; |
|
234 int nfun = 0; |
|
235 double abserr = 0.0; |
|
236 double val = 0.0; |
3998
|
237 bool have_sing = false; |
3243
|
238 ColumnVector sing; |
3998
|
239 ColumnVector tol; |
|
240 |
3243
|
241 switch (nargin) |
|
242 { |
|
243 case 5: |
|
244 if (indefinite) |
3323
|
245 QUAD_ABORT1 ("singularities not allowed on infinite intervals"); |
3243
|
246 |
3998
|
247 have_sing = true; |
3243
|
248 |
3419
|
249 sing = ColumnVector (args(4).vector_value ()); |
3243
|
250 |
|
251 if (error_state) |
3323
|
252 QUAD_ABORT1 ("expecting vector of singularities as fourth argument"); |
3243
|
253 |
|
254 case 4: |
3419
|
255 tol = ColumnVector (args(3).vector_value ()); |
3243
|
256 |
|
257 if (error_state) |
3323
|
258 QUAD_ABORT1 ("expecting vector of tolerances as fifth argument"); |
3243
|
259 |
|
260 switch (tol.capacity ()) |
|
261 { |
|
262 case 2: |
3998
|
263 quad_opts.set_relative_tolerance (tol (1)); |
3243
|
264 |
|
265 case 1: |
3998
|
266 quad_opts.set_absolute_tolerance (tol (0)); |
3243
|
267 break; |
|
268 |
|
269 default: |
3323
|
270 QUAD_ABORT1 ("expecting tol to contain no more than two values"); |
3243
|
271 } |
|
272 |
|
273 case 3: |
|
274 if (indefinite) |
|
275 { |
3998
|
276 IndefQuad iq (quad_user_function, bound, indef_type); |
4122
|
277 iq.set_options (quad_opts); |
3243
|
278 val = iq.integrate (ier, nfun, abserr); |
|
279 } |
|
280 else |
|
281 { |
|
282 if (have_sing) |
|
283 { |
3998
|
284 DefQuad dq (quad_user_function, a, b, sing); |
4122
|
285 dq.set_options (quad_opts); |
3243
|
286 val = dq.integrate (ier, nfun, abserr); |
|
287 } |
|
288 else |
|
289 { |
3998
|
290 DefQuad dq (quad_user_function, a, b); |
4122
|
291 dq.set_options (quad_opts); |
3243
|
292 val = dq.integrate (ier, nfun, abserr); |
|
293 } |
|
294 } |
|
295 break; |
|
296 |
|
297 default: |
|
298 panic_impossible (); |
|
299 break; |
|
300 } |
|
301 |
|
302 retval(3) = abserr; |
|
303 retval(2) = static_cast<double> (nfun); |
|
304 retval(1) = static_cast<double> (ier); |
|
305 retval(0) = val; |
4954
|
306 |
|
307 if (fcn_name.length()) |
|
308 clear_function (fcn_name); |
3243
|
309 } |
|
310 else |
3323
|
311 print_usage ("quad"); |
2928
|
312 |
3243
|
313 unwind_protect::run_frame ("Fquad"); |
2928
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
|
318 /* |
|
319 ;;; Local Variables: *** |
|
320 ;;; mode: C++ *** |
|
321 ;;; End: *** |
|
322 */ |