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