3
|
1 // Quad.cc -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
3
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
238
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
3
|
30 #endif |
|
31 |
289
|
32 #include <math.h> |
|
33 #include <float.h> |
|
34 |
3
|
35 #include "Quad.h" |
|
36 #include "f77-uscore.h" |
|
37 #include "sun-utils.h" |
|
38 |
|
39 static integrand_fcn user_fcn; |
|
40 |
260
|
41 // XXX FIXME XXX -- would be nice to not have to have this global |
|
42 // variable. |
|
43 // Nonzero means an error occurred in the calculation of the integrand |
|
44 // function, and the user wants us to quit. |
|
45 int quad_integration_error = 0; |
|
46 |
3
|
47 extern "C" |
|
48 { |
1253
|
49 int F77_FCN (dqagp, DQAGP) (const double (*)(double*, int&), |
|
50 const double&, const double&, |
|
51 const int&, const double*, |
|
52 const double&, const double&, double&, |
|
53 double&, int&, int&, const int&, |
|
54 const int&, int&, int*, double*); |
3
|
55 |
1253
|
56 int F77_FCN (dqagi, DQAGI) (const double (*)(double*, int&), |
|
57 const double&, const int&, |
|
58 const double&, const double&, double&, |
|
59 double&, int&, int&, const int&, |
|
60 const int&, int&, int*, double*); |
3
|
61 } |
|
62 |
|
63 Quad::Quad (integrand_fcn fcn) |
|
64 { |
|
65 f = fcn; |
|
66 } |
|
67 |
|
68 Quad::Quad (integrand_fcn fcn, double abs, double rel) |
|
69 { |
|
70 f = fcn; |
|
71 } |
|
72 |
|
73 double |
|
74 Quad::integrate (void) |
|
75 { |
|
76 int ier, neval; |
|
77 double abserr; |
|
78 return integrate (ier, neval, abserr); |
|
79 } |
|
80 |
|
81 double |
|
82 Quad::integrate (int& ier) |
|
83 { |
|
84 int neval; |
|
85 double abserr; |
|
86 return integrate (ier, neval, abserr); |
|
87 } |
|
88 |
|
89 double |
|
90 Quad::integrate (int& ier, int& neval) |
|
91 { |
|
92 double abserr; |
|
93 return integrate (ier, neval, abserr); |
|
94 } |
|
95 |
|
96 static double |
1251
|
97 user_function (double *x, int& ierr) |
3
|
98 { |
|
99 #if defined (sun) && defined (__GNUC__) |
|
100 double xx = access_double (x); |
|
101 #else |
|
102 double xx = *x; |
|
103 #endif |
|
104 |
260
|
105 quad_integration_error = 0; |
|
106 |
|
107 double retval = (*user_fcn) (xx); |
|
108 |
|
109 if (quad_integration_error) |
1251
|
110 ierr = -1; |
260
|
111 |
|
112 return retval; |
3
|
113 } |
|
114 |
|
115 DefQuad::DefQuad (integrand_fcn fcn) : Quad (fcn) |
|
116 { |
|
117 lower_limit = 0.0; |
|
118 upper_limit = 1.0; |
|
119 } |
|
120 |
|
121 DefQuad::DefQuad (integrand_fcn fcn, double ll, double ul) |
|
122 : Quad (fcn) |
|
123 { |
|
124 lower_limit = ll; |
|
125 upper_limit = ul; |
|
126 } |
|
127 |
|
128 DefQuad::DefQuad (integrand_fcn fcn, double ll, double ul, |
|
129 double abs, double rel) : Quad (fcn, abs, rel) |
|
130 { |
|
131 lower_limit = ll; |
|
132 upper_limit = ul; |
|
133 } |
|
134 |
|
135 DefQuad::DefQuad (integrand_fcn fcn, double ll, double ul, |
|
136 const Vector& sing) : Quad (fcn) |
|
137 { |
|
138 lower_limit = ll; |
|
139 upper_limit = ul; |
|
140 singularities = sing; |
|
141 } |
|
142 |
|
143 DefQuad::DefQuad (integrand_fcn fcn, const Vector& sing, |
|
144 double abs, double rel) : Quad (fcn, abs, rel) |
|
145 { |
|
146 lower_limit = 0.0; |
|
147 upper_limit = 1.0; |
|
148 singularities = sing; |
|
149 } |
|
150 |
|
151 DefQuad::DefQuad (integrand_fcn fcn, const Vector& sing) |
|
152 : Quad (fcn) |
|
153 { |
|
154 lower_limit = 0.0; |
|
155 upper_limit = 1.0; |
|
156 singularities = sing; |
|
157 } |
|
158 |
|
159 DefQuad::DefQuad (integrand_fcn fcn, double ll, double ul, |
|
160 const Vector& sing, double abs, double rel) |
|
161 : Quad (fcn, abs, rel) |
|
162 { |
|
163 lower_limit = ll; |
|
164 upper_limit = ul; |
|
165 singularities = sing; |
|
166 } |
|
167 |
|
168 double |
|
169 DefQuad::integrate (int& ier, int& neval, double& abserr) |
|
170 { |
|
171 int npts = singularities.capacity () + 2; |
|
172 double *points = singularities.fortran_vec (); |
|
173 double result = 0.0; |
|
174 int leniw = 183*npts - 122; |
|
175 int lenw = 2*leniw - npts; |
|
176 int *iwork = new int [leniw]; |
|
177 double *work = new double [lenw]; |
|
178 user_fcn = f; |
|
179 int last; |
|
180 |
289
|
181 double abs_tol = absolute_tolerance (); |
|
182 double rel_tol = relative_tolerance (); |
|
183 |
1253
|
184 F77_FCN (dqagp, DQAGP) (user_function, lower_limit, upper_limit, |
|
185 npts, points, abs_tol, rel_tol, result, |
|
186 abserr, neval, ier, leniw, lenw, last, |
|
187 iwork, work); |
3
|
188 |
|
189 delete [] iwork; |
|
190 delete [] work; |
|
191 |
|
192 return result; |
|
193 } |
|
194 |
|
195 IndefQuad::IndefQuad (integrand_fcn fcn) : Quad (fcn) |
|
196 { |
|
197 bound = 0.0; |
|
198 type = bound_to_inf; |
|
199 } |
|
200 |
|
201 IndefQuad::IndefQuad (integrand_fcn fcn, double b, IntegralType t) |
|
202 : Quad (fcn) |
|
203 { |
|
204 bound = b; |
|
205 type = t; |
|
206 } |
|
207 |
|
208 IndefQuad::IndefQuad (integrand_fcn fcn, double b, IntegralType t, |
|
209 double abs, double rel) : Quad (fcn, abs, rel) |
|
210 { |
|
211 bound = b; |
|
212 type = t; |
|
213 } |
|
214 |
|
215 IndefQuad::IndefQuad (integrand_fcn fcn, double abs, double rel) |
|
216 : Quad (fcn, abs, rel) |
|
217 { |
|
218 bound = 0.0; |
|
219 type = bound_to_inf; |
|
220 } |
|
221 |
|
222 double |
|
223 IndefQuad::integrate (int& ier, int& neval, double& abserr) |
|
224 { |
|
225 double result = 0.0; |
|
226 int leniw = 128; |
|
227 int lenw = 8*leniw; |
|
228 int *iwork = new int [leniw]; |
|
229 double *work = new double [lenw]; |
|
230 user_fcn = f; |
|
231 int last; |
|
232 |
|
233 int inf; |
|
234 switch (type) |
|
235 { |
|
236 case bound_to_inf: |
|
237 inf = 1; |
|
238 break; |
1360
|
239 |
3
|
240 case neg_inf_to_bound: |
|
241 inf = -1; |
|
242 break; |
1360
|
243 |
3
|
244 case doubly_infinite: |
|
245 inf = 2; |
|
246 break; |
1360
|
247 |
3
|
248 default: |
|
249 assert (0); |
|
250 break; |
|
251 } |
|
252 |
289
|
253 double abs_tol = absolute_tolerance (); |
|
254 double rel_tol = relative_tolerance (); |
|
255 |
1253
|
256 F77_FCN (dqagi, DQAGI) (user_function, bound, inf, abs_tol, rel_tol, |
|
257 result, abserr, neval, ier, leniw, lenw, |
|
258 last, iwork, work); |
3
|
259 |
|
260 delete [] iwork; |
|
261 delete [] work; |
|
262 |
|
263 return result; |
|
264 } |
|
265 |
289
|
266 Quad_options::Quad_options (void) |
|
267 { |
|
268 init (); |
|
269 } |
|
270 |
|
271 Quad_options::Quad_options (const Quad_options& opt) |
|
272 { |
|
273 copy (opt); |
|
274 } |
|
275 |
|
276 Quad_options& |
|
277 Quad_options::operator = (const Quad_options& opt) |
|
278 { |
|
279 if (this != &opt) |
|
280 copy (opt); |
|
281 |
|
282 return *this; |
|
283 } |
|
284 |
|
285 Quad_options::~Quad_options (void) |
|
286 { |
|
287 } |
|
288 |
|
289 void |
|
290 Quad_options::init (void) |
|
291 { |
|
292 double sqrt_eps = sqrt (DBL_EPSILON); |
|
293 x_absolute_tolerance = sqrt_eps; |
|
294 x_relative_tolerance = sqrt_eps; |
|
295 } |
|
296 |
|
297 void |
|
298 Quad_options::copy (const Quad_options& opt) |
|
299 { |
|
300 x_absolute_tolerance = opt.x_absolute_tolerance; |
|
301 x_relative_tolerance = opt.x_relative_tolerance; |
|
302 } |
|
303 |
|
304 void |
|
305 Quad_options::set_default_options (void) |
|
306 { |
|
307 init (); |
|
308 } |
|
309 |
|
310 void |
|
311 Quad_options::set_absolute_tolerance (double val) |
|
312 { |
|
313 x_absolute_tolerance = (val > 0.0) ? val : sqrt (DBL_EPSILON); |
|
314 } |
|
315 |
|
316 void |
|
317 Quad_options::set_relative_tolerance (double val) |
|
318 { |
|
319 x_relative_tolerance = (val > 0.0) ? val : sqrt (DBL_EPSILON); |
|
320 } |
|
321 |
|
322 double |
|
323 Quad_options::absolute_tolerance (void) |
|
324 { |
|
325 return x_absolute_tolerance; |
|
326 } |
|
327 |
|
328 double |
|
329 Quad_options::relative_tolerance (void) |
|
330 { |
|
331 return x_relative_tolerance; |
|
332 } |
|
333 |
3
|
334 /* |
|
335 ;;; Local Variables: *** |
|
336 ;;; mode: C++ *** |
|
337 ;;; page-delimiter: "^/\\*" *** |
|
338 ;;; End: *** |
|
339 */ |