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