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