3
|
1 // Quad.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
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 |
|
25 #include "config.h" |
3
|
26 #endif |
|
27 |
|
28 #include "Quad.h" |
|
29 #include "f77-uscore.h" |
|
30 #include "sun-utils.h" |
|
31 |
|
32 static integrand_fcn user_fcn; |
|
33 |
260
|
34 // XXX FIXME XXX -- would be nice to not have to have this global |
|
35 // variable. |
|
36 // Nonzero means an error occurred in the calculation of the integrand |
|
37 // function, and the user wants us to quit. |
|
38 int quad_integration_error = 0; |
|
39 |
3
|
40 extern "C" |
|
41 { |
260
|
42 int F77_FCN (dqagp) (const double (*)(double*, int*), const double*, |
3
|
43 const double*, const int*, const double*, |
|
44 const double*, const double*, double*, double*, |
|
45 int*, int*, const int*, const int*, int*, int*, |
|
46 double*); |
|
47 |
260
|
48 int F77_FCN (dqagi) (const double (*)(double*, int*), const double*, |
3
|
49 const int*, const double*, const double*, |
|
50 double*, double*, int*, int*, const int*, |
|
51 const int*, int*, int*, double*); |
|
52 } |
|
53 |
|
54 Quad::Quad (integrand_fcn fcn) |
|
55 { |
|
56 absolute_tolerance = 1.0e-6; |
|
57 relative_tolerance = 1.0e-6; |
|
58 f = fcn; |
|
59 } |
|
60 |
|
61 Quad::Quad (integrand_fcn fcn, double abs, double rel) |
|
62 { |
|
63 absolute_tolerance = abs; |
|
64 relative_tolerance = rel; |
|
65 f = fcn; |
|
66 } |
|
67 |
|
68 double |
|
69 Quad::integrate (void) |
|
70 { |
|
71 int ier, neval; |
|
72 double abserr; |
|
73 return integrate (ier, neval, abserr); |
|
74 } |
|
75 |
|
76 double |
|
77 Quad::integrate (int& ier) |
|
78 { |
|
79 int neval; |
|
80 double abserr; |
|
81 return integrate (ier, neval, abserr); |
|
82 } |
|
83 |
|
84 double |
|
85 Quad::integrate (int& ier, int& neval) |
|
86 { |
|
87 double abserr; |
|
88 return integrate (ier, neval, abserr); |
|
89 } |
|
90 |
|
91 static double |
260
|
92 user_function (double *x, int *ierr) |
3
|
93 { |
|
94 #if defined (sun) && defined (__GNUC__) |
|
95 double xx = access_double (x); |
|
96 #else |
|
97 double xx = *x; |
|
98 #endif |
|
99 |
260
|
100 quad_integration_error = 0; |
|
101 |
|
102 double retval = (*user_fcn) (xx); |
|
103 |
|
104 if (quad_integration_error) |
|
105 *ierr = -1; |
|
106 |
|
107 return retval; |
3
|
108 } |
|
109 |
|
110 DefQuad::DefQuad (integrand_fcn fcn) : Quad (fcn) |
|
111 { |
|
112 lower_limit = 0.0; |
|
113 upper_limit = 1.0; |
|
114 } |
|
115 |
|
116 DefQuad::DefQuad (integrand_fcn fcn, double ll, double ul) |
|
117 : Quad (fcn) |
|
118 { |
|
119 lower_limit = ll; |
|
120 upper_limit = ul; |
|
121 } |
|
122 |
|
123 DefQuad::DefQuad (integrand_fcn fcn, double ll, double ul, |
|
124 double abs, double rel) : Quad (fcn, abs, rel) |
|
125 { |
|
126 lower_limit = ll; |
|
127 upper_limit = ul; |
|
128 } |
|
129 |
|
130 DefQuad::DefQuad (integrand_fcn fcn, double ll, double ul, |
|
131 const Vector& sing) : Quad (fcn) |
|
132 { |
|
133 lower_limit = ll; |
|
134 upper_limit = ul; |
|
135 singularities = sing; |
|
136 } |
|
137 |
|
138 DefQuad::DefQuad (integrand_fcn fcn, const Vector& sing, |
|
139 double abs, double rel) : Quad (fcn, abs, rel) |
|
140 { |
|
141 lower_limit = 0.0; |
|
142 upper_limit = 1.0; |
|
143 singularities = sing; |
|
144 } |
|
145 |
|
146 DefQuad::DefQuad (integrand_fcn fcn, const Vector& sing) |
|
147 : Quad (fcn) |
|
148 { |
|
149 lower_limit = 0.0; |
|
150 upper_limit = 1.0; |
|
151 singularities = sing; |
|
152 } |
|
153 |
|
154 DefQuad::DefQuad (integrand_fcn fcn, double ll, double ul, |
|
155 const Vector& sing, double abs, double rel) |
|
156 : Quad (fcn, abs, rel) |
|
157 { |
|
158 lower_limit = ll; |
|
159 upper_limit = ul; |
|
160 singularities = sing; |
|
161 } |
|
162 |
|
163 double |
|
164 DefQuad::integrate (int& ier, int& neval, double& abserr) |
|
165 { |
|
166 int npts = singularities.capacity () + 2; |
|
167 double *points = singularities.fortran_vec (); |
|
168 double result = 0.0; |
|
169 int leniw = 183*npts - 122; |
|
170 int lenw = 2*leniw - npts; |
|
171 int *iwork = new int [leniw]; |
|
172 double *work = new double [lenw]; |
|
173 user_fcn = f; |
|
174 int last; |
|
175 |
|
176 F77_FCN (dqagp) (user_function, &lower_limit, &upper_limit, &npts, |
|
177 points, &absolute_tolerance, &relative_tolerance, |
|
178 &result, &abserr, &neval, &ier, &leniw, &lenw, |
|
179 &last, iwork, work); |
|
180 |
|
181 delete [] iwork; |
|
182 delete [] work; |
|
183 |
|
184 return result; |
|
185 } |
|
186 |
|
187 IndefQuad::IndefQuad (integrand_fcn fcn) : Quad (fcn) |
|
188 { |
|
189 bound = 0.0; |
|
190 type = bound_to_inf; |
|
191 } |
|
192 |
|
193 IndefQuad::IndefQuad (integrand_fcn fcn, double b, IntegralType t) |
|
194 : Quad (fcn) |
|
195 { |
|
196 bound = b; |
|
197 type = t; |
|
198 } |
|
199 |
|
200 IndefQuad::IndefQuad (integrand_fcn fcn, double b, IntegralType t, |
|
201 double abs, double rel) : Quad (fcn, abs, rel) |
|
202 { |
|
203 bound = b; |
|
204 type = t; |
|
205 } |
|
206 |
|
207 IndefQuad::IndefQuad (integrand_fcn fcn, double abs, double rel) |
|
208 : Quad (fcn, abs, rel) |
|
209 { |
|
210 bound = 0.0; |
|
211 type = bound_to_inf; |
|
212 } |
|
213 |
|
214 double |
|
215 IndefQuad::integrate (int& ier, int& neval, double& abserr) |
|
216 { |
|
217 double result = 0.0; |
|
218 int leniw = 128; |
|
219 int lenw = 8*leniw; |
|
220 int *iwork = new int [leniw]; |
|
221 double *work = new double [lenw]; |
|
222 user_fcn = f; |
|
223 int last; |
|
224 |
|
225 int inf; |
|
226 switch (type) |
|
227 { |
|
228 case bound_to_inf: |
|
229 inf = 1; |
|
230 break; |
|
231 case neg_inf_to_bound: |
|
232 inf = -1; |
|
233 break; |
|
234 case doubly_infinite: |
|
235 inf = 2; |
|
236 break; |
|
237 default: |
|
238 assert (0); |
|
239 break; |
|
240 } |
|
241 |
|
242 F77_FCN (dqagi) (user_function, &bound, &inf, &absolute_tolerance, |
|
243 &relative_tolerance, &result, &abserr, &neval, |
|
244 &ier, &leniw, &lenw, &last, iwork, work); |
|
245 |
|
246 delete [] iwork; |
|
247 delete [] work; |
|
248 |
|
249 return result; |
|
250 } |
|
251 |
|
252 /* |
|
253 ;;; Local Variables: *** |
|
254 ;;; mode: C++ *** |
|
255 ;;; page-delimiter: "^/\\*" *** |
|
256 ;;; End: *** |
|
257 */ |