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