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