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 { \ |
|
108 unwind_protect::run_frame ("Fquad"); \ |
|
109 return retval; \ |
|
110 } \ |
|
111 while (0) |
|
112 |
|
113 #define QUAD_ABORT1(msg) \ |
|
114 do \ |
|
115 { \ |
3747
|
116 ::error ("quad: " msg); \ |
3323
|
117 QUAD_ABORT (); \ |
|
118 } \ |
|
119 while (0) |
|
120 |
|
121 #define QUAD_ABORT2(fmt, arg) \ |
|
122 do \ |
|
123 { \ |
3747
|
124 ::error ("quad: " fmt, arg); \ |
3323
|
125 QUAD_ABORT (); \ |
|
126 } \ |
|
127 while (0) |
|
128 |
2928
|
129 DEFUN_DLD (quad, args, nargout, |
3368
|
130 "-*- texinfo -*-\n\ |
|
131 @deftypefn {Loadable Function} {[@var{v}, @var{ier}, @var{nfun}, @var{err}] =} quad (@var{f}, @var{a}, @var{b}, @var{tol}, @var{sing})\n\ |
|
132 Integrate a nonlinear function of one variable using Quadpack.\n\ |
|
133 The first argument is the name of the function to call to compute the\n\ |
|
134 value of the integrand. It must have the form\n\ |
2928
|
135 \n\ |
3368
|
136 @example\n\ |
|
137 y = f (x)\n\ |
|
138 @end example\n\ |
2928
|
139 \n\ |
3368
|
140 @noindent\n\ |
|
141 where @var{y} and @var{x} are scalars.\n\ |
2928
|
142 \n\ |
|
143 The second and third arguments are limits of integration. Either or\n\ |
|
144 both may be infinite.\n\ |
|
145 \n\ |
3368
|
146 The optional argument @var{tol} is a vector that specifies the desired\n\ |
2928
|
147 accuracy of the result. The first element of the vector is the desired\n\ |
|
148 absolute tolerance, and the second element is the desired relative\n\ |
3368
|
149 tolerance. To choose a relative test only, set the absolute\n\ |
|
150 tolerance to zero. To choose an absolute test only, set the relative\n\ |
|
151 tolerance to zero. \n\ |
2928
|
152 \n\ |
|
153 The optional argument @var{sing} is a vector of values at which the\n\ |
3368
|
154 integrand is known to be singular.\n\ |
|
155 \n\ |
|
156 The result of the integration is returned in @var{v} and @var{ier}\n\ |
|
157 contains an integer error code (0 indicates a successful integration).\n\ |
|
158 The value of @var{nfun} indicates how many function evaluations were\n\ |
|
159 required, and @var{err} contains an estimate of the error in the\n\ |
|
160 solution.\n\ |
3964
|
161 \n\ |
|
162 You can use the function @code{quad_options} to set optional\n\ |
|
163 parameters for @code{quad}.\n\ |
3368
|
164 @end deftypefn") |
2928
|
165 { |
|
166 octave_value_list retval; |
|
167 |
4140
|
168 warned_imaginary = false; |
|
169 |
3243
|
170 unwind_protect::begin_frame ("Fquad"); |
|
171 |
|
172 unwind_protect_int (call_depth); |
|
173 call_depth++; |
|
174 |
|
175 if (call_depth > 1) |
3323
|
176 QUAD_ABORT1 ("invalid recursive call"); |
3243
|
177 |
2928
|
178 int nargin = args.length (); |
|
179 |
3243
|
180 if (nargin > 2 && nargin < 6 && nargout < 5) |
|
181 { |
|
182 quad_fcn = extract_function (args(0), "quad", "__quad_fcn__", |
|
183 "function y = __quad_fcn__ (x) y = ", |
|
184 "; endfunction"); |
|
185 if (! quad_fcn) |
3323
|
186 QUAD_ABORT (); |
3243
|
187 |
|
188 double a = args(1).double_value (); |
|
189 |
|
190 if (error_state) |
3323
|
191 QUAD_ABORT1 ("expecting second argument to be a scalar"); |
3243
|
192 |
|
193 double b = args(2).double_value (); |
|
194 |
|
195 if (error_state) |
3323
|
196 QUAD_ABORT1 ("expecting third argument to be a scalar"); |
3243
|
197 |
|
198 int indefinite = 0; |
|
199 IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite; |
|
200 double bound = 0.0; |
|
201 if (xisinf (a) && xisinf (b)) |
|
202 { |
|
203 indefinite = 1; |
|
204 indef_type = IndefQuad::doubly_infinite; |
|
205 } |
|
206 else if (xisinf (a)) |
|
207 { |
|
208 indefinite = 1; |
|
209 bound = b; |
|
210 indef_type = IndefQuad::neg_inf_to_bound; |
|
211 } |
|
212 else if (xisinf (b)) |
|
213 { |
|
214 indefinite = 1; |
|
215 bound = a; |
|
216 indef_type = IndefQuad::bound_to_inf; |
|
217 } |
|
218 |
|
219 int ier = 0; |
|
220 int nfun = 0; |
|
221 double abserr = 0.0; |
|
222 double val = 0.0; |
3998
|
223 bool have_sing = false; |
3243
|
224 ColumnVector sing; |
3998
|
225 ColumnVector tol; |
|
226 |
3243
|
227 switch (nargin) |
|
228 { |
|
229 case 5: |
|
230 if (indefinite) |
3323
|
231 QUAD_ABORT1 ("singularities not allowed on infinite intervals"); |
3243
|
232 |
3998
|
233 have_sing = true; |
3243
|
234 |
3419
|
235 sing = ColumnVector (args(4).vector_value ()); |
3243
|
236 |
|
237 if (error_state) |
3323
|
238 QUAD_ABORT1 ("expecting vector of singularities as fourth argument"); |
3243
|
239 |
|
240 case 4: |
3419
|
241 tol = ColumnVector (args(3).vector_value ()); |
3243
|
242 |
|
243 if (error_state) |
3323
|
244 QUAD_ABORT1 ("expecting vector of tolerances as fifth argument"); |
3243
|
245 |
|
246 switch (tol.capacity ()) |
|
247 { |
|
248 case 2: |
3998
|
249 quad_opts.set_relative_tolerance (tol (1)); |
3243
|
250 |
|
251 case 1: |
3998
|
252 quad_opts.set_absolute_tolerance (tol (0)); |
3243
|
253 break; |
|
254 |
|
255 default: |
3323
|
256 QUAD_ABORT1 ("expecting tol to contain no more than two values"); |
3243
|
257 } |
|
258 |
|
259 case 3: |
|
260 if (indefinite) |
|
261 { |
3998
|
262 IndefQuad iq (quad_user_function, bound, indef_type); |
4122
|
263 iq.set_options (quad_opts); |
3243
|
264 val = iq.integrate (ier, nfun, abserr); |
|
265 } |
|
266 else |
|
267 { |
|
268 if (have_sing) |
|
269 { |
3998
|
270 DefQuad dq (quad_user_function, a, b, sing); |
4122
|
271 dq.set_options (quad_opts); |
3243
|
272 val = dq.integrate (ier, nfun, abserr); |
|
273 } |
|
274 else |
|
275 { |
3998
|
276 DefQuad dq (quad_user_function, a, b); |
4122
|
277 dq.set_options (quad_opts); |
3243
|
278 val = dq.integrate (ier, nfun, abserr); |
|
279 } |
|
280 } |
|
281 break; |
|
282 |
|
283 default: |
|
284 panic_impossible (); |
|
285 break; |
|
286 } |
|
287 |
|
288 retval(3) = abserr; |
|
289 retval(2) = static_cast<double> (nfun); |
|
290 retval(1) = static_cast<double> (ier); |
|
291 retval(0) = val; |
|
292 } |
|
293 else |
3323
|
294 print_usage ("quad"); |
2928
|
295 |
3243
|
296 unwind_protect::run_frame ("Fquad"); |
2928
|
297 |
|
298 return retval; |
|
299 } |
|
300 |
|
301 /* |
|
302 ;;; Local Variables: *** |
|
303 ;;; mode: C++ *** |
|
304 ;;; End: *** |
|
305 */ |