The R Project SVN R

Rev

Rev 13845 | Rev 36820 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13845 Rev 19234
Line 1... Line 1...
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 2001  the R Development Core Team
3
 *  Copyright (C) 2001-2002  the R Development Core Team
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
Line 13... Line 13...
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-
 
18
 
-
 
19
 * C backend of R's integrate() --- via
-
 
20
 *	 .External("call_dqags", ...) -> Rdqags()  -- for finite     interval
-
 
21
 *	 .External("call_dqagi", ...) -> Rdqagi()  -- for indefinite interval
18
 */
22
*/
19
 
23
 
20
#include <Rinternals.h>
24
#include <Rinternals.h>
21
#include <R_ext/Boolean.h>
-
 
22
#ifdef Macintosh
-
 
23
#include <fp.h>
-
 
24
#else
-
 
25
#include <math.h>
-
 
26
#endif
-
 
27
#include <Rmath.h>
25
#include <Rmath.h>
-
 
26
#include <R_ext/Applic.h> /* exporting the API , particularly */
-
 
27
/*--- typedef void integr_fn(double *x, int n, void *ex) ---
-
 
28
 * vectorizing function   f(x[1:n], ...) -> x[]  {overwriting x[]}.
-
 
29
 * Vectorization can be used to speed up the integrand
-
 
30
 * instead of calling it  n  times.
-
 
31
*/
-
 
32
 
-
 
33
/* Only these two are called via .External(.) :*/
-
 
34
SEXP call_dqags(SEXP args);
-
 
35
SEXP call_dqagi(SEXP args);
28
 
36
 
29
typedef struct int_struct
37
typedef struct int_struct
30
{
38
{
31
    SEXP f;    /* function */
39
    SEXP f;    /* function */
32
    SEXP env;  /* where to evaluate the calls */
40
    SEXP env;  /* where to evaluate the calls */
33
} int_struct, *IntStruct;
41
} int_struct, *IntStruct;
34
 
42
 
35
static void Rdqags(IntStruct IS, double *lower, double *upper, 
-
 
36
		   double *epsabs, double *epsrel, double *result,
-
 
37
		   double *abserr, int *neval, int *ier, int *limit, 
-
 
38
		   int *lenw, int *last, int *iwork, double *work);
-
 
39
 
-
 
40
static void Rdqagi(IntStruct IS, double *bound, int *inf, 
-
 
41
		   double *epsabs, double *epsrel, double *result,
-
 
42
		   double *abserr, int *neval, int *ier, int *limit, 
-
 
43
		   int *lenw, int *last, int *iwork, double *work);
-
 
44
 
-
 
45
 
43
 
-
 
44
/* This is *the* ``integr_fn f'' used when called from R : */
46
static void Rintfn(double *x, int n, IntStruct IS)
45
static void Rintfn(double *x, int n, void *ex)
47
{
46
{
48
    SEXP args, resultsxp;
47
    SEXP args, resultsxp;
49
    int i;
48
    int i;
-
 
49
    IntStruct IS = (IntStruct) ex;
50
 
50
 
51
    PROTECT(args = allocVector(REALSXP, n));
51
    PROTECT(args = allocVector(REALSXP, n));
52
    for(i = 0; i < n; i++) REAL(args)[i] = x[i];
52
    for(i = 0; i < n; i++) REAL(args)[i] = x[i];
53
 
53
 
54
    PROTECT(resultsxp = eval(lang2(IS->f , args), IS->env));
54
    PROTECT(resultsxp = eval(lang2(IS->f , args), IS->env));
55
 
55
 
56
    if(length(resultsxp) != n)
56
    if(length(resultsxp) != n)
57
	error("evaluation of function gave a result of wrong length");    
57
	error("evaluation of function gave a result of wrong length");
58
    for(i = 0; i < n; i++) {
58
    for(i = 0; i < n; i++) {
59
	x[i] = REAL(resultsxp)[i];
59
	x[i] = REAL(resultsxp)[i];
60
	if(!R_FINITE(x[i]))
60
	if(!R_FINITE(x[i]))
61
	    error("non-finite function value");
61
	    error("non-finite function value");
62
    }
62
    }
63
    UNPROTECT(2);
63
    UNPROTECT(2);
64
    return;   
64
    return;
65
}
65
}
66
 
66
 
67
 
-
 
68
SEXP call_dqags(SEXP args)
67
SEXP call_dqags(SEXP args)
69
{
68
{
70
    int_struct is;
69
    int_struct is;
71
    SEXP ans, ansnames;
70
    SEXP ans, ansnames;
72
    double lower, upper, epsabs, epsrel, result, abserr, *work;
71
    double lower, upper, epsabs, epsrel, result, abserr, *work;
Line 82... Line 81...
82
    limit = asInteger(CAR(args)); args = CDR(args);
81
    limit = asInteger(CAR(args)); args = CDR(args);
83
    lenw = 4 * limit;
82
    lenw = 4 * limit;
84
    iwork = (int *) R_alloc(limit, sizeof(int));
83
    iwork = (int *) R_alloc(limit, sizeof(int));
85
    work = (double *) R_alloc(lenw, sizeof(double));
84
    work = (double *) R_alloc(lenw, sizeof(double));
86
 
85
 
-
 
86
    Rdqags(Rintfn, (void*)&is,
87
    Rdqags(&is, &lower, &upper, &epsabs, &epsrel, &result,
87
	   &lower, &upper, &epsabs, &epsrel, &result,
88
	   &abserr, &neval, &ier, &limit, &lenw, &last, iwork, work);
88
	   &abserr, &neval, &ier, &limit, &lenw, &last, iwork, work);
89
 
89
 
90
    PROTECT(ans = allocVector(VECSXP, 4));
90
    PROTECT(ans = allocVector(VECSXP, 4));
91
    PROTECT(ansnames = allocVector(STRSXP, 4));
91
    PROTECT(ansnames = allocVector(STRSXP, 4));
92
    SET_STRING_ELT(ansnames, 0, mkChar("value"));
92
    SET_STRING_ELT(ansnames, 0, mkChar("value"));
Line 123... Line 123...
123
    limit = asInteger(CAR(args)); args = CDR(args);
123
    limit = asInteger(CAR(args)); args = CDR(args);
124
    lenw = 4 * limit;
124
    lenw = 4 * limit;
125
    iwork = (int *) R_alloc(limit, sizeof(int));
125
    iwork = (int *) R_alloc(limit, sizeof(int));
126
    work = (double *) R_alloc(lenw, sizeof(double));
126
    work = (double *) R_alloc(lenw, sizeof(double));
127
 
127
 
128
    Rdqagi(&is, &bound,&inf,&epsabs,&epsrel,&result,
128
    Rdqagi(Rintfn, (void*)&is, &bound,&inf,&epsabs,&epsrel,&result,
129
	   &abserr,&neval,&ier,&limit,&lenw,&last,iwork,work);
129
	   &abserr,&neval,&ier,&limit,&lenw,&last,iwork,work);
130
 
130
 
131
    PROTECT(ans = allocVector(VECSXP, 4));
131
    PROTECT(ans = allocVector(VECSXP, 4));
132
    PROTECT(ansnames = allocVector(STRSXP, 4));
132
    PROTECT(ansnames = allocVector(STRSXP, 4));
133
    SET_STRING_ELT(ansnames, 0, mkChar("value"));
133
    SET_STRING_ELT(ansnames, 0, mkChar("value"));
Line 145... Line 145...
145
    setAttrib(ans, R_NamesSymbol, ansnames);
145
    setAttrib(ans, R_NamesSymbol, ansnames);
146
    UNPROTECT(2);
146
    UNPROTECT(2);
147
    return ans;
147
    return ans;
148
}
148
}
149
 
149
 
-
 
150

150
/* f2c-ed translations + modifications of QUADPACK functions from here down */
151
/* f2c-ed translations + modifications of QUADPACK functions from here down */
151
 
152
 
152
 
-
 
-
 
153
static void rdqagie(integr_fn f, void *ex,
153
static void rdqagie(IntStruct IS, double *, int *, double * , double *, int *, 
154
		    double *, int *, double * , double *, int *,
154
		    double *, double *, int *, 
155
		    double *, double *, int *,
155
		    int *, double *, double *, double *, double *,
156
		    int *, double *, double *, double *, double *,
156
		    int *, int *);
157
		    int *, int *);
157
 
158
 
-
 
159
static void rdqk15i(integr_fn f, void *ex,
158
static void  rdqk15i(IntStruct IS, double *, int *, double * , double *, 
160
		    double *, int *, double * , double *,
159
		     double *, double *, double *, double *);
161
		    double *, double *, double *, double *);
-
 
162
 
-
 
163
static void rdqagse(integr_fn f, void *ex, double *, double *,
-
 
164
		    double *, double *, int *, double *, double *,
-
 
165
		    int *, int *, double *, double *, double *,
-
 
166
		    double *, int *, int *);
160
 
167
 
-
 
168
static void rdqk21(integr_fn f, void *ex,
161
static void  rdqelg(int *, double *, double *, double *, double *, int *);
169
		   double *, double *, double *, double *, double *, double *);
162
 
170
 
163
static void rdqpsrt(int *, int *, int *, double *, double *, int *, int *);
171
static void rdqpsrt(int *, int *, int *, double *, double *, int *, int *);
164
 
172
 
165
static void  rdqagse(IntStruct IS, double *, double *, 
-
 
166
		     double *, double *, int *, double *, double *,
-
 
167
		     int *, int *, double *, double *, double *, 
-
 
168
		     double *, int *, int *);
-
 
169
 
-
 
170
static void rdqk21(IntStruct IS, double *, double *, double *, double *, double *, double *);
-
 
171
 
-
 
172
static void rdqelg(int *, double *, double *, double *, double *, int *);
173
static void rdqelg(int *, double *, double *, double *, double *, int *);
173
 
174
 
174
/* Table of constant values */
175
/* Table of constant values */
175
 
176
 
176
static double c_b6 = 0.;
177
static double c_b6 = 0.;
177
static double c_b7 = 1.;
178
static double c_b7 = 1.;
178
 
179
 
179
static
-
 
180
void Rdqagi(IntStruct IS, double *bound, int *inf, double *
180
void Rdqagi(integr_fn f, void *ex, double *bound, int *inf,
-
 
181
	    double *epsabs, double *epsrel,
181
	    epsabs, double *epsrel, double *result, double *abserr, 
182
	    double *result, double *abserr, int *neval, int *ier,
182
	    int *neval, int *ier, int *limit, int *lenw, int *
183
	    int *limit, int *lenw, int *last,
183
	    last, int *iwork, double *work)
184
	    int *iwork, double *work)
184
{
185
{
185
    int l1, l2, l3;
186
    int l1, l2, l3;
186
 
187
 
187
/*
188
/*
188
***begin prologue  dqagi
189
***begin prologue  dqagi
Line 197... Line 198...
197
***purpose  the routine calculates an approximation result to a given
198
***purpose  the routine calculates an approximation result to a given
198
           integral   i = integral of f over (bound,+infinity)
199
           integral   i = integral of f over (bound,+infinity)
199
           or i = integral of f over (-infinity,bound)
200
           or i = integral of f over (-infinity,bound)
200
           or i = integral of f over (-infinity,+infinity)
201
           or i = integral of f over (-infinity,+infinity)
201
           hopefully satisfying following claim for accuracy
202
           hopefully satisfying following claim for accuracy
202
           abs(i-result).le.max(epsabs,epsrel*abs(i)).
203
           abs(i-result) <= max(epsabs,epsrel*abs(i)).
203
***description
204
***description
204
 
205
 
205
       integration over infinite intervals
206
       integration over infinite intervals
206
       standard fortran subroutine
207
       standard fortran subroutine
207
 
208
 
Line 224... Line 225...
224
 
225
 
225
           epsabs - double precision
226
           epsabs - double precision
226
                    absolute accuracy requested
227
                    absolute accuracy requested
227
           epsrel - double precision
228
           epsrel - double precision
228
                    relative accuracy requested
229
                    relative accuracy requested
229
                    if  epsabs.le.0
230
                    if  epsabs <= 0
230
                    and epsrel.lt.max(50*rel.mach.acc.,0.5d-28),
231
                    and epsrel < max(50*rel.mach.acc.,0.5d-28),
231
                    the routine will end with ier = 6.
232
                    the routine will end with ier = 6.
232
 
233
 
233
 
234
 
234
        on return
235
        on return
235
           result - double precision
236
           result - double precision
Line 244... Line 245...
244
 
245
 
245
           ier    - int
246
           ier    - int
246
                    ier = 0 normal and reliable termination of the
247
                    ier = 0 normal and reliable termination of the
247
                            routine. it is assumed that the requested
248
                            routine. it is assumed that the requested
248
                            accuracy has been achieved.
249
                            accuracy has been achieved.
249
                  - ier.gt.0 abnormal termination of the routine. the
250
                  - ier > 0 abnormal termination of the routine. the
250
                            estimates for result and error are less
251
                            estimates for result and error are less
251
                            reliable. it is assumed that the requested
252
                            reliable. it is assumed that the requested
252
                            accuracy has not been achieved.
253
                            accuracy has not been achieved.
253
           error messages
254
           error messages
254
                    ier = 1 maximum number of subdivisions allowed
255
                    ier = 1 maximum number of subdivisions allowed
Line 284... Line 285...
284
                        = 5 the integral is probably divergent, or
285
                        = 5 the integral is probably divergent, or
285
                            slowly convergent. it must be noted that
286
                            slowly convergent. it must be noted that
286
                            divergence can occur with any other value
287
                            divergence can occur with any other value
287
                            of ier.
288
                            of ier.
288
                        = 6 the input is invalid, because
289
                        = 6 the input is invalid, because
289
                            (epsabs.le.0 and
290
                            (epsabs <= 0 and
290
                             epsrel.lt.max(50*rel.mach.acc.,0.5d-28))
291
                             epsrel < max(50*rel.mach.acc.,0.5d-28))
291
                             or limit.lt.1 or leniw.lt.limit*4.
292
                             or limit < 1 or leniw < limit*4.
292
                            result, abserr, neval, last are set to
293
                            result, abserr, neval, last are set to
293
                            zero. exept when limit or leniw is
294
                            zero. exept when limit or leniw is
294
                            invalid, iwork(1), work(limit*2+1) and
295
                            invalid, iwork(1), work(limit*2+1) and
295
                            work(limit*3+1) are set to zero, work(1)
296
                            work(limit*3+1) are set to zero, work(1)
296
                            is set to a and work(limit+1) to b.
297
                            is set to a and work(limit+1) to b.
Line 298... Line 299...
298
        dimensioning parameters
299
        dimensioning parameters
299
           limit - int
300
           limit - int
300
                   dimensioning parameter for iwork
301
                   dimensioning parameter for iwork
301
                   limit determines the maximum number of subintervals
302
                   limit determines the maximum number of subintervals
302
                   in the partition of the given integration interval
303
                   in the partition of the given integration interval
303
                   (a,b), limit.ge.1.
304
                   (a,b), limit >= 1.
304
                   if limit.lt.1, the routine will end with ier = 6.
305
                   if limit < 1, the routine will end with ier = 6.
305
 
306
 
306
           lenw  - int
307
           lenw  - int
307
                   dimensioning parameter for work
308
                   dimensioning parameter for work
308
                   lenw must be at least limit*4.
309
                   lenw must be at least limit*4.
309
                   if lenw.lt.limit*4, the routine will end
310
                   if lenw < limit*4, the routine will end
310
                   with ier = 6.
311
                   with ier = 6.
311
 
312
 
312
           last  - int
313
           last  - int
313
                   on return, last equals the number of subintervals
314
                   on return, last equals the number of subintervals
314
                   produced in the subdivision process, which
315
                   produced in the subdivision process, which
Line 320... Line 321...
320
                   vector of dimension at least limit, the first
321
                   vector of dimension at least limit, the first
321
                   k elements of which contain pointers
322
                   k elements of which contain pointers
322
                   to the error estimates over the subintervals,
323
                   to the error estimates over the subintervals,
323
                   such that work(limit*3+iwork(1)),... ,
324
                   such that work(limit*3+iwork(1)),... ,
324
                   work(limit*3+iwork(k)) form a decreasing
325
                   work(limit*3+iwork(k)) form a decreasing
325
                   sequence, with k = last if last.le.(limit/2+2), and
326
                   sequence, with k = last if last <= (limit/2+2), and
326
                   k = limit+1-last otherwise
327
                   k = limit+1-last otherwise
327
 
328
 
328
           work  - double precision
329
           work  - double precision
329
                   vector of dimension at least lenw
330
                   vector of dimension at least lenw
330
                   on return
331
                   on return
Line 355... Line 356...
355
 
356
 
356
    l1 = *limit + 1;
357
    l1 = *limit + 1;
357
    l2 = *limit + l1;
358
    l2 = *limit + l1;
358
    l3 = *limit + l2;
359
    l3 = *limit + l2;
359
 
360
 
360
    rdqagie(IS, bound, inf, epsabs, epsrel, limit, result, abserr, neval, ier, 
361
    rdqagie(f, ex, bound, inf, epsabs, epsrel, limit, result, abserr, neval, ier,
361
	    &work[1], &work[l1], &work[l2], &work[l3], &iwork[1], last);
362
	    &work[1], &work[l1], &work[l2], &work[l3], &iwork[1], last);
362
 
363
 
363
    return;
364
    return;
364
} /* Rdqagi */
365
} /* Rdqagi */
365
 
366
 
366
static
367
static
367
void rdqagie(IntStruct IS, double *bound, int *inf, double *
368
void rdqagie(integr_fn f, void *ex, double *bound, int *inf, double *
368
	     epsabs, double *epsrel, int *limit, double *result, 
369
	     epsabs, double *epsrel, int *limit, double *result,
369
	     double *abserr, int *neval, int *ier, double *alist__,
370
	     double *abserr, int *neval, int *ier, double *alist__,
370
	     double *blist, double *rlist, double *elist, int *
371
	     double *blist, double *rlist, double *elist, int *
371
	     iord, int *last)
372
	     iord, int *last)
372
{
373
{
373
    /* System generated locals */
374
    /* System generated locals */
374
    int i__1, i__2;
-
 
375
    double d__1, d__2;
375
    double d__1, d__2;
376
 
376
 
377
    /* Local variables */
377
    /* Local variables */
378
    double area, dres;
378
    double area, dres;
379
    int ksgn;
379
    int ksgn;
Line 412... Line 412...
412
***purpose  the routine calculates an approximation result to a given
412
***purpose  the routine calculates an approximation result to a given
413
           integral   i = integral of f over (bound,+infinity)
413
           integral   i = integral of f over (bound,+infinity)
414
           or i = integral of f over (-infinity,bound)
414
           or i = integral of f over (-infinity,bound)
415
           or i = integral of f over (-infinity,+infinity),
415
           or i = integral of f over (-infinity,+infinity),
416
           hopefully satisfying following claim for accuracy
416
           hopefully satisfying following claim for accuracy
417
           abs(i-result).le.max(epsabs,epsrel*abs(i))
417
           abs(i-result) <= max(epsabs,epsrel*abs(i))
418
***description
418
***description
419
 
419
 
420
integration over infinite intervals
420
integration over infinite intervals
421
standard fortran subroutine
421
standard fortran subroutine
422
 
422
 
Line 437... Line 437...
437
 
437
 
438
           epsabs - double precision
438
           epsabs - double precision
439
                    absolute accuracy requested
439
                    absolute accuracy requested
440
           epsrel - double precision
440
           epsrel - double precision
441
                    relative accuracy requested
441
                    relative accuracy requested
442
                    if  epsabs.le.0
442
                    if  epsabs <= 0
443
                    and epsrel.lt.max(50*rel.mach.acc.,0.5d-28),
443
                    and epsrel < max(50*rel.mach.acc.,0.5d-28),
444
                    the routine will end with ier = 6.
444
                    the routine will end with ier = 6.
445
 
445
 
446
           limit  - int
446
           limit  - int
447
                    gives an upper bound on the number of subintervals
447
                    gives an upper bound on the number of subintervals
448
                    in the partition of (a,b), limit.ge.1
448
                    in the partition of (a,b), limit >= 1
449
 
449
 
450
        on return
450
        on return
451
           result - double precision
451
           result - double precision
452
                    approximation to the integral
452
                    approximation to the integral
453
 
453
 
Line 460... Line 460...
460
 
460
 
461
           ier    - int
461
           ier    - int
462
                    ier = 0 normal and reliable termination of the
462
                    ier = 0 normal and reliable termination of the
463
                            routine. it is assumed that the requested
463
                            routine. it is assumed that the requested
464
                            accuracy has been achieved.
464
                            accuracy has been achieved.
465
                  - ier.gt.0 abnormal termination of the routine. the
465
                  - ier > 0 abnormal termination of the routine. the
466
                            estimates for result and error are less
466
                            estimates for result and error are less
467
                            reliable. it is assumed that the requested
467
                            reliable. it is assumed that the requested
468
                            accuracy has not been achieved.
468
                            accuracy has not been achieved.
469
           error messages
469
           error messages
470
                    ier = 1 maximum number of subdivisions allowed
470
                    ier = 1 maximum number of subdivisions allowed
Line 500... Line 500...
500
                        = 5 the integral is probably divergent, or
500
                        = 5 the integral is probably divergent, or
501
                            slowly convergent. it must be noted that
501
                            slowly convergent. it must be noted that
502
                            divergence can occur with any other value
502
                            divergence can occur with any other value
503
                            of ier.
503
                            of ier.
504
                        = 6 the input is invalid, because
504
                        = 6 the input is invalid, because
505
                            (epsabs.le.0 and
505
                            (epsabs <= 0 and
506
                             epsrel.lt.max(50*rel.mach.acc.,0.5d-28),
506
                             epsrel < max(50*rel.mach.acc.,0.5d-28),
507
                            result, abserr, neval, last, rlist(1),
507
                            result, abserr, neval, last, rlist(1),
508
                            elist(1) and iord(1) are set to zero.
508
                            elist(1) and iord(1) are set to zero.
509
                            alist(1) and blist(1) are set to 0
509
                            alist(1) and blist(1) are set to 0
510
                            and 1 respectively.
510
                            and 1 respectively.
511
 
511
 
Line 535... Line 535...
535
                    vector of dimension limit, the first k
535
                    vector of dimension limit, the first k
536
                    elements of which are pointers to the
536
                    elements of which are pointers to the
537
                    error estimates over the subintervals,
537
                    error estimates over the subintervals,
538
                    such that elist(iord(1)), ..., elist(iord(k))
538
                    such that elist(iord(1)), ..., elist(iord(k))
539
                    form a decreasing sequence, with k = last
539
                    form a decreasing sequence, with k = last
540
                    if last.le.(limit/2+2), and k = limit+1-last
540
                    if last <= (limit/2+2), and k = limit+1-last
541
                    otherwise
541
                    otherwise
542
 
542
 
543
           last   - int
543
           last   - int
544
                    number of subintervals actually produced
544
                    number of subintervals actually produced
545
                    in the subdivision process
545
                    in the subdivision process
Line 623... Line 623...
623
    alist__[1] = 0.;
623
    alist__[1] = 0.;
624
    blist[1] = 1.;
624
    blist[1] = 1.;
625
    rlist[1] = 0.;
625
    rlist[1] = 0.;
626
    elist[1] = 0.;
626
    elist[1] = 0.;
627
    iord[1] = 0;
627
    iord[1] = 0;
628
/* Computing MAX */
-
 
629
    d__1 = epmach * 50.;
-
 
630
    if (*epsabs <= 0. && (*epsrel < fmax2(epmach * 50., 5e-29))) *ier = 6;
628
    if (*epsabs <= 0. && (*epsrel < fmax2(epmach * 50., 5e-29))) *ier = 6;
631
    if (*ier == 6) return;
629
    if (*ier == 6) return;
632
 
630
 
633
/*           first approximation to the integral */
631
/*           first approximation to the integral */
634
/*           ----------------------------------- */
632
/*           ----------------------------------- */
Line 640... Line 638...
640
 
638
 
641
    boun = *bound;
639
    boun = *bound;
642
    if (*inf == 2) {
640
    if (*inf == 2) {
643
	boun = 0.;
641
	boun = 0.;
644
    }
642
    }
645
    rdqk15i(IS, &boun, inf, &c_b6, &c_b7, result, abserr, &defabs, &resabs);
643
    rdqk15i(f, ex, &boun, inf, &c_b6, &c_b7, result, abserr, &defabs, &resabs);
646
 
644
 
647
/*           test on accuracy */
645
/*           test on accuracy */
648
 
646
 
649
    *last = 1;
647
    *last = 1;
650
    rlist[1] = *result;
648
    rlist[1] = *result;
651
    elist[1] = *abserr;
649
    elist[1] = *abserr;
652
    iord[1] = 1;
650
    iord[1] = 1;
653
    dres = fabs(*result);
651
    dres = fabs(*result);
654
/* Computing MAX */
-
 
655
    d__1 = *epsabs, d__2 = *epsrel * dres;
652
    errbnd = fmax2(*epsabs, *epsrel * dres);
656
    errbnd = fmax2(d__1,d__2);
-
 
657
    if (*abserr <= epmach * 100. * defabs && *abserr > errbnd) *ier = 2;
653
    if (*abserr <= epmach * 100. * defabs && *abserr > errbnd) *ier = 2;
658
    if (*limit == 1) *ier = 1;
654
    if (*limit == 1) *ier = 1;
659
    if (*ier != 0 || (*abserr <= errbnd && *abserr != resabs)
655
    if (*ier != 0 || (*abserr <= errbnd && *abserr != resabs)
660
	|| *abserr == 0.) goto L130;
656
	|| *abserr == 0.) goto L130;
661
 
657
 
Line 686... Line 682...
686
    }
682
    }
687
 
683
 
688
/*           main do-loop */
684
/*           main do-loop */
689
/*           ------------ */
685
/*           ------------ */
690
 
686
 
691
    i__1 = *limit;
-
 
692
    for (*last = 2; *last <= i__1; ++(*last)) {
687
    for (*last = 2; *last <= *limit; ++(*last)) {
693
 
688
 
694
/*           bisect the subinterval with nrmax-th largest error estimate. */
689
/*           bisect the subinterval with nrmax-th largest error estimate. */
695
 
690
 
696
	a1 = alist__[maxerr];
691
	a1 = alist__[maxerr];
697
	b1 = (alist__[maxerr] + blist[maxerr]) * .5;
692
	b1 = (alist__[maxerr] + blist[maxerr]) * .5;
698
	a2 = b1;
693
	a2 = b1;
699
	b2 = blist[maxerr];
694
	b2 = blist[maxerr];
700
	erlast = errmax;
695
	erlast = errmax;
701
	rdqk15i(IS, &boun, inf, &a1, &b1, &area1, &error1, &resabs, &defab1);
696
	rdqk15i(f, ex, &boun, inf, &a1, &b1, &area1, &error1, &resabs, &defab1);
702
	rdqk15i(IS, &boun, inf, &a2, &b2, &area2, &error2, &resabs, &defab2);
697
	rdqk15i(f, ex, &boun, inf, &a2, &b2, &area2, &error2, &resabs, &defab2);
703
 
698
 
704
/*           improve previous approximations to integral */
699
/*           improve previous approximations to integral
705
/*           and error and test for accuracy. */
700
	     and error and test for accuracy. */
706
 
701
 
707
	area12 = area1 + area2;
702
	area12 = area1 + area2;
708
	erro12 = error1 + error2;
703
	erro12 = error1 + error2;
709
	errsum = errsum + erro12 - errmax;
704
	errsum = errsum + erro12 - errmax;
710
	area = area + area12 - rlist[maxerr];
705
	area = area + area12 - rlist[maxerr];
711
	if (defab1 == error1 || defab2 == error2) {
706
	if (defab1 == error1 || defab2 == error2) {
712
	    goto L15;
707
	    goto L15;
713
	}
708
	}
714
	if ((d__1 = rlist[maxerr] - area12, fabs(d__1)) > fabs(area12) * 1e-5 ||
709
	if (fabs(rlist[maxerr] - area12) > fabs(area12) * 1e-5 ||
715
		 erro12 < errmax * .99) {
710
		 erro12 < errmax * .99) {
716
	    goto L10;
711
	    goto L10;
717
	}
712
	}
718
	if (extrap) {
713
	if (extrap) {
719
	    ++iroff2;
714
	    ++iroff2;
Line 726... Line 721...
726
	    ++iroff3;
721
	    ++iroff3;
727
	}
722
	}
728
L15:
723
L15:
729
	rlist[maxerr] = area1;
724
	rlist[maxerr] = area1;
730
	rlist[*last] = area2;
725
	rlist[*last] = area2;
731
/* Computing MAX */
-
 
732
	d__1 = *epsabs, d__2 = *epsrel * fabs(area);
726
	errbnd = fmax2(*epsabs, *epsrel * fabs(area));
733
	errbnd = fmax2(d__1,d__2);
-
 
734
 
727
 
735
/*           test for roundoff error and eventually set error flag. */
728
/*           test for roundoff error and eventually set error flag. */
736
 
729
 
737
	if (iroff1 + iroff2 >= 10 || iroff3 >= 20) {
730
	if (iroff1 + iroff2 >= 10 || iroff3 >= 20)
738
	    *ier = 2;
731
	    *ier = 2;
739
	}
-
 
740
	if (iroff2 >= 5) {
732
	if (iroff2 >= 5)
741
	    ierro = 3;
733
	    ierro = 3;
742
	}
-
 
743
 
734
 
744
/*           set error flag in the case that the number of */
735
/*           set error flag in the case that the number of
745
/*           subintervals equals limit. */
736
	     subintervals equals limit. */
746
 
737
 
747
	if (*last == *limit) {
738
	if (*last == *limit)
748
	    *ier = 1;
739
	    *ier = 1;
749
	}
-
 
750
 
740
 
751
/*           set error flag in the case of bad integrand behaviour */
741
/*           set error flag in the case of bad integrand behaviour
752
/*           at some points of the integration range. */
742
	     at some points of the integration range. */
753
 
743
 
754
/* Computing MAX */
-
 
755
	d__1 = fabs(a1), d__2 = fabs(b2);
744
	if (fmax2(fabs(a1), fabs(b2)) <=
756
	if (fmax2(d__1,d__2) <= (epmach * 100. + 1.) * (fabs(a2) + uflow * 1e3)) 
745
	    (epmach * 100. + 1.) * (fabs(a2) + uflow * 1e3))
757
		{
746
		{
758
	    *ier = 4;
747
	    *ier = 4;
759
	}
748
	}
760
 
749
 
761
/*           append the newly-created intervals to the list. */
750
/*           append the newly-created intervals to the list. */
Line 776... Line 765...
776
	rlist[maxerr] = area2;
765
	rlist[maxerr] = area2;
777
	rlist[*last] = area1;
766
	rlist[*last] = area1;
778
	elist[maxerr] = error2;
767
	elist[maxerr] = error2;
779
	elist[*last] = error1;
768
	elist[*last] = error1;
780
 
769
 
781
/*           call subroutine dqpsrt to maintain the descending ordering */
770
/*           call subroutine dqpsrt to maintain the descending ordering
782
/*           in the list of error estimates and select the subinterval */
771
	     in the list of error estimates and select the subinterval
783
/*           with nrmax-th largest error estimate (to be bisected next). */
772
	     with nrmax-th largest error estimate (to be bisected next). */
784
 
773
 
785
L30:
774
L30:
786
	rdqpsrt(limit, last, &maxerr, &errmax, &elist[1], &iord[1], &nrmax);
775
	rdqpsrt(limit, last, &maxerr, &errmax, &elist[1], &iord[1], &nrmax);
787
	if (errsum <= errbnd) {
776
	if (errsum <= errbnd) {
788
	    goto L115;
777
	    goto L115;
789
	}
778
	}
790
	if (*ier != 0) {
779
	if (*ier != 0)	    goto L100;
791
	    goto L100;
-
 
792
	}
-
 
793
	if (*last == 2) {
780
	if (*last == 2)     goto L80;
794
	    goto L80;
-
 
795
	}
-
 
796
	if (noext) {
-
 
797
	    goto L90;
781
	if (noext) 	    goto L90;
798
	}
782
 
799
	erlarg -= erlast;
783
	erlarg -= erlast;
800
	if ((d__1 = b1 - a1, fabs(d__1)) > small) {
784
	if (fabs(b1 - a1) > small) {
801
	    erlarg += erro12;
785
	    erlarg += erro12;
802
	}
786
	}
803
	if (extrap) {
787
	if (extrap) {
804
	    goto L40;
788
	    goto L40;
805
	}
789
	}
806
 
790
 
807
/*           test whether the interval to be bisected next is the */
791
/*           test whether the interval to be bisected next is the
808
/*           smallest interval. */
792
	     smallest interval. */
809
 
793
 
810
	if ((d__1 = blist[maxerr] - alist__[maxerr], fabs(d__1)) > small) {
794
	if (fabs(blist[maxerr] - alist__[maxerr]) > small) {
811
	    goto L90;
795
	    goto L90;
812
	}
796
	}
813
	extrap = TRUE;
797
	extrap = TRUE;
814
	nrmax = 2;
798
	nrmax = 2;
815
L40:
799
L40:
816
	if (ierro == 3 || erlarg <= ertest) {
800
	if (ierro == 3 || erlarg <= ertest) {
817
	    goto L60;
801
	    goto L60;
818
	}
802
	}
819
 
803
 
820
/*           the smallest interval has the largest error. */
804
/*           the smallest interval has the largest error.
821
/*           before bisecting decrease the sum of the errors over the */
805
	     before bisecting decrease the sum of the errors over the
822
/*           larger intervals (erlarg) and perform extrapolation. */
806
	     larger intervals (erlarg) and perform extrapolation. */
823
 
807
 
824
	id = nrmax;
808
	id = nrmax;
825
	jupbnd = *last;
809
	jupbnd = *last;
826
	if (*last > *limit / 2 + 2) {
810
	if (*last > *limit / 2 + 2) {
827
	    jupbnd = *limit + 3 - *last;
811
	    jupbnd = *limit + 3 - *last;
828
	}
812
	}
829
	i__2 = jupbnd;
-
 
830
	for (k = id; k <= i__2; ++k) {
813
	for (k = id; k <= jupbnd; ++k) {
831
	    maxerr = iord[nrmax];
814
	    maxerr = iord[nrmax];
832
	    errmax = elist[maxerr];
815
	    errmax = elist[maxerr];
833
	    if ((d__1 = blist[maxerr] - alist__[maxerr], fabs(d__1)) > small) {
816
	    if (fabs(blist[maxerr] - alist__[maxerr]) > small) {
834
		goto L90;
817
		goto L90;
835
	    }
818
	    }
836
	    ++nrmax;
819
	    ++nrmax;
837
/* L50: */
820
/* L50: */
838
	}
821
	}
Line 932... Line 915...
932
 
915
 
933
/*           compute global integral sum. */
916
/*           compute global integral sum. */
934
 
917
 
935
L115:
918
L115:
936
    *result = 0.;
919
    *result = 0.;
937
    i__1 = *last;
-
 
938
    for (k = 1; k <= i__1; ++k) {
920
    for (k = 1; k <= *last; ++k)
939
	*result += rlist[k];
921
	*result += rlist[k];
940
/* L120: */
-
 
941
    }
922
 
942
    *abserr = errsum;
923
    *abserr = errsum;
943
L130:
924
L130:
944
    *neval = *last * 30 - 15;
925
    *neval = *last * 30 - 15;
945
    if (*inf == 2) {
926
    if (*inf == 2) {
946
	*neval <<= 1;
927
	*neval <<= 1;
Line 949... Line 930...
949
	--(*ier);
930
	--(*ier);
950
    }
931
    }
951
    return;
932
    return;
952
} /* rdqagie_ */
933
} /* rdqagie_ */
953
 
934
 
954
static
-
 
955
void Rdqags(IntStruct IS, double *a, double *b, double *epsabs,
935
void Rdqags(integr_fn f, void *ex, double *a, double *b,
956
	    double *epsrel, double *result, double *abserr, int *
936
	    double *epsabs, double *epsrel,
957
	    neval, int *ier, int *limit, int *lenw, int *last, 
937
	    double *result, double *abserr, int *neval, int *ier,
958
	    int *iwork, double *work)
938
	    int *limit, int *lenw, int *last, int *iwork, double *work)
959
{
939
{
960
    int l1, l2, l3;
940
    int l1, l2, l3;
961
 
941
 
962
/* 
942
/*
963
***begin prologue  dqags
943
***begin prologue  dqags
964
***date written   800101   (yymmdd)
944
***date written   800101   (yymmdd)
965
***revision date  830518   (yymmdd)
945
***revision date  830518   (yymmdd)
966
***category no.  h2a1a1
946
***category no.  h2a1a1
967
***keywords  automatic integrator, general-purpose,
947
***keywords  automatic integrator, general-purpose,
Line 970... Line 950...
970
***author  piessens,robert,appl. math. & progr. div. - k.u.leuven
950
***author  piessens,robert,appl. math. & progr. div. - k.u.leuven
971
          de doncker,elise,appl. math. & prog. div. - k.u.leuven
951
          de doncker,elise,appl. math. & prog. div. - k.u.leuven
972
***purpose  the routine calculates an approximation result to a given
952
***purpose  the routine calculates an approximation result to a given
973
           definite integral  i = integral of f over (a,b),
953
           definite integral  i = integral of f over (a,b),
974
           hopefully satisfying following claim for accuracy
954
           hopefully satisfying following claim for accuracy
975
           abs(i-result).le.max(epsabs,epsrel*abs(i)).
955
           abs(i-result) <= max(epsabs,epsrel*abs(i)).
976
***description
956
***description
977
 
957
 
978
       computation of a definite integral
958
       computation of a definite integral
979
       standard fortran subroutine
959
       standard fortran subroutine
980
       double precision version
960
       double precision version
Line 995... Line 975...
995
 
975
 
996
           epsabs - double precision
976
           epsabs - double precision
997
                    absolute accuracy requested
977
                    absolute accuracy requested
998
           epsrel - double precision
978
           epsrel - double precision
999
                    relative accuracy requested
979
                    relative accuracy requested
1000
                    if  epsabs.le.0
980
                    if  epsabs <= 0
1001
                    and epsrel.lt.max(50*rel.mach.acc.,0.5d-28),
981
                    and epsrel < max(50*rel.mach.acc.,0.5d-28),
1002
                    the routine will end with ier = 6.
982
                    the routine will end with ier = 6.
1003
 
983
 
1004
        on return
984
        on return
1005
           result - double precision
985
           result - double precision
1006
                    approximation to the integral
986
                    approximation to the integral
Line 1014... Line 994...
1014
 
994
 
1015
           ier    - int
995
           ier    - int
1016
                    ier = 0 normal and reliable termination of the
996
                    ier = 0 normal and reliable termination of the
1017
                            routine. it is assumed that the requested
997
                            routine. it is assumed that the requested
1018
                            accuracy has been achieved.
998
                            accuracy has been achieved.
1019
                    ier.gt.0 abnormal termination of the routine
999
                    ier > 0 abnormal termination of the routine
1020
                            the estimates for integral and error are
1000
                            the estimates for integral and error are
1021
                            less reliable. it is assumed that the
1001
                            less reliable. it is assumed that the
1022
                            requested accuracy has not been achieved.
1002
                            requested accuracy has not been achieved.
1023
           error messages
1003
           error messages
1024
                    ier = 1 maximum number of subdivisions allowed
1004
                    ier = 1 maximum number of subdivisions allowed
Line 1054... Line 1034...
1054
                        = 5 the integral is probably divergent, or
1034
                        = 5 the integral is probably divergent, or
1055
                            slowly convergent. it must be noted that
1035
                            slowly convergent. it must be noted that
1056
                            divergence can occur with any other value
1036
                            divergence can occur with any other value
1057
                            of ier.
1037
                            of ier.
1058
                        = 6 the input is invalid, because
1038
                        = 6 the input is invalid, because
1059
                            (epsabs.le.0 and
1039
                            (epsabs <= 0 and
1060
                             epsrel.lt.max(50*rel.mach.acc.,0.5d-28)
1040
                             epsrel < max(50*rel.mach.acc.,0.5d-28)
1061
                            or limit.lt.1 or lenw.lt.limit*4.
1041
                            or limit < 1 or lenw < limit*4.
1062
                            result, abserr, neval, last are set to
1042
                            result, abserr, neval, last are set to
1063
                            zero.except when limit or lenw is invalid,
1043
                            zero.except when limit or lenw is invalid,
1064
                            iwork(1), work(limit*2+1) and
1044
                            iwork(1), work(limit*2+1) and
1065
                            work(limit*3+1) are set to zero, work(1)
1045
                            work(limit*3+1) are set to zero, work(1)
1066
                            is set to a and work(limit+1) to b.
1046
                            is set to a and work(limit+1) to b.
Line 1068... Line 1048...
1068
        dimensioning parameters
1048
        dimensioning parameters
1069
           limit - int
1049
           limit - int
1070
                   dimensioning parameter for iwork
1050
                   dimensioning parameter for iwork
1071
                   limit determines the maximum number of subintervals
1051
                   limit determines the maximum number of subintervals
1072
                   in the partition of the given integration interval
1052
                   in the partition of the given integration interval
1073
                   (a,b), limit.ge.1.
1053
                   (a,b), limit >= 1.
1074
                   if limit.lt.1, the routine will end with ier = 6.
1054
                   if limit < 1, the routine will end with ier = 6.
1075
 
1055
 
1076
           lenw  - int
1056
           lenw  - int
1077
                   dimensioning parameter for work
1057
                   dimensioning parameter for work
1078
                   lenw must be at least limit*4.
1058
                   lenw must be at least limit*4.
1079
                   if lenw.lt.limit*4, the routine will end
1059
                   if lenw < limit*4, the routine will end
1080
                   with ier = 6.
1060
                   with ier = 6.
1081
 
1061
 
1082
           last  - int
1062
           last  - int
1083
                   on return, last equals the number of subintervals
1063
                   on return, last equals the number of subintervals
1084
                   produced in the subdivision process, detemines the
1064
                   produced in the subdivision process, detemines the
Line 1090... Line 1070...
1090
                   vector of dimension at least limit, the first k
1070
                   vector of dimension at least limit, the first k
1091
                   elements of which contain pointers
1071
                   elements of which contain pointers
1092
                   to the error estimates over the subintervals
1072
                   to the error estimates over the subintervals
1093
                   such that work(limit*3+iwork(1)),... ,
1073
                   such that work(limit*3+iwork(1)),... ,
1094
                   work(limit*3+iwork(k)) form a decreasing
1074
                   work(limit*3+iwork(k)) form a decreasing
1095
                   sequence, with k = last if last.le.(limit/2+2),
1075
                   sequence, with k = last if last <= (limit/2+2),
1096
                   and k = limit+1-last otherwise
1076
                   and k = limit+1-last otherwise
1097
 
1077
 
1098
           work  - double precision
1078
           work  - double precision
1099
                   vector of dimension at least lenw
1079
                   vector of dimension at least lenw
1100
                   on return
1080
                   on return
Line 1134... Line 1114...
1134
 
1114
 
1135
    l1 = *limit + 1;
1115
    l1 = *limit + 1;
1136
    l2 = *limit + l1;
1116
    l2 = *limit + l1;
1137
    l3 = *limit + l2;
1117
    l3 = *limit + l2;
1138
 
1118
 
1139
    rdqagse(IS, a, b, epsabs, epsrel, limit, result, abserr, neval, ier, 
1119
    rdqagse(f, ex, a, b, epsabs, epsrel, limit, result, abserr, neval, ier,
1140
	    &work[1], &work[l1], &work[l2], &work[l3], &iwork[1], last);
1120
	    &work[1], &work[l1], &work[l2], &work[l3], &iwork[1], last);
1141
 
1121
 
1142
    return;
1122
    return;
1143
} /* rdqags_ */
1123
} /* rdqags_ */
1144
 
1124
 
1145
static 
1125
static
1146
void rdqagse(IntStruct IS, double *a, double *b, double *
1126
void rdqagse(integr_fn f, void *ex, double *a, double *b, double *
1147
	     epsabs, double *epsrel, int *limit, double *result, 
1127
	     epsabs, double *epsrel, int *limit, double *result,
1148
	     double *abserr, int *neval, int *ier, double *alist__,
1128
	     double *abserr, int *neval, int *ier, double *alist__,
1149
	     double *blist, double *rlist, double *elist, int *
1129
	     double *blist, double *rlist, double *elist, int *
1150
	     iord, int *last)
1130
	     iord, int *last)
1151
{
1131
{
1152
    /* System generated locals */
-
 
1153
    int i__1, i__2;
-
 
1154
    double d__1, d__2;
-
 
1155
 
-
 
1156
    /* Local variables */
1132
    /* Local variables */
1157
    double area, dres;
1133
    Rboolean noext, extrap;
1158
    int ksgn, nres;
1134
    int k,ksgn, nres;
1159
    double area1, area2, area12;
-
 
1160
    int k;
-
 
1161
    double small = 0.0, erro12;
-
 
1162
    int ierro;
1135
    int ierro;
1163
    double a1, a2, b1, b2, defab1, defab2, oflow;
-
 
1164
    int ktmin, nrmax;
1136
    int ktmin, nrmax;
1165
    double uflow;
-
 
1166
    Rboolean noext;
-
 
1167
    int iroff1, iroff2, iroff3;
1137
    int iroff1, iroff2, iroff3;
1168
    double res3la[3], error1, error2;
-
 
1169
    int id;
1138
    int id;
1170
    double rlist2[52];
-
 
1171
    int numrl2;
1139
    int numrl2;
1172
    double defabs, epmach, erlarg = 0.0, abseps, correc = 0.0, errbnd, resabs;
-
 
1173
    int jupbnd;
1140
    int jupbnd;
1174
    double erlast, errmax;
-
 
1175
    int maxerr;
1141
    int maxerr;
1176
    double reseps;
1142
    double res3la[3];
1177
    Rboolean extrap;
1143
    double rlist2[52];
1178
    double ertest = 0.0, errsum;
1144
    double abseps, area, area1, area2, area12, dres, epmach;
-
 
1145
    double a1, a2, b1, b2, defabs, defab1, defab2, oflow, uflow, resabs, reseps;
-
 
1146
    double error1, error2, erro12, errbnd, erlast, errmax, errsum;
1179
 
1147
 
-
 
1148
    double correc = 0.0, erlarg = 0.0, ertest = 0.0, small = 0.0;
1180
/* 
1149
/*
1181
***begin prologue  dqagse
1150
***begin prologue  dqagse
1182
***date written   800101   (yymmdd)
1151
***date written   800101   (yymmdd)
1183
***revision date  830518   (yymmdd)
1152
***revision date  830518   (yymmdd)
1184
***category no.  h2a1a1
1153
***category no.  h2a1a1
1185
***keywords  automatic integrator, general-purpose,
1154
***keywords  automatic integrator, general-purpose,
Line 1188... Line 1157...
1188
***author  piessens,robert,appl. math. & progr. div. - k.u.leuven
1157
***author  piessens,robert,appl. math. & progr. div. - k.u.leuven
1189
          de doncker,elise,appl. math. & progr. div. - k.u.leuven
1158
          de doncker,elise,appl. math. & progr. div. - k.u.leuven
1190
***purpose  the routine calculates an approximation result to a given
1159
***purpose  the routine calculates an approximation result to a given
1191
           definite integral i = integral of f over (a,b),
1160
           definite integral i = integral of f over (a,b),
1192
           hopefully satisfying following claim for accuracy
1161
           hopefully satisfying following claim for accuracy
1193
           abs(i-result).le.max(epsabs,epsrel*abs(i)).
1162
           abs(i-result) <= max(epsabs,epsrel*abs(i)).
1194
***description
1163
***description
1195
 
1164
 
1196
       computation of a definite integral
1165
       computation of a definite integral
1197
       standard fortran subroutine
1166
       standard fortran subroutine
1198
       double precision version
1167
       double precision version
Line 1212... Line 1181...
1212
 
1181
 
1213
           epsabs - double precision
1182
           epsabs - double precision
1214
                    absolute accuracy requested
1183
                    absolute accuracy requested
1215
           epsrel - double precision
1184
           epsrel - double precision
1216
                    relative accuracy requested
1185
                    relative accuracy requested
1217
                    if  epsabs.le.0
1186
                    if  epsabs <= 0
1218
                    and epsrel.lt.max(50*rel.mach.acc.,0.5d-28),
1187
                    and epsrel < max(50*rel.mach.acc.,0.5d-28),
1219
                    the routine will end with ier = 6.
1188
                    the routine will end with ier = 6.
1220
 
1189
 
1221
           limit  - int
1190
           limit  - int
1222
                    gives an upperbound on the number of subintervals
1191
                    gives an upperbound on the number of subintervals
1223
                    in the partition of (a,b)
1192
                    in the partition of (a,b)
Line 1235... Line 1204...
1235
 
1204
 
1236
           ier    - int
1205
           ier    - int
1237
                    ier = 0 normal and reliable termination of the
1206
                    ier = 0 normal and reliable termination of the
1238
                            routine. it is assumed that the requested
1207
                            routine. it is assumed that the requested
1239
                            accuracy has been achieved.
1208
                            accuracy has been achieved.
1240
                    ier.gt.0 abnormal termination of the routine
1209
                    ier > 0 abnormal termination of the routine
1241
                            the estimates for integral and error are
1210
                            the estimates for integral and error are
1242
                            less reliable. it is assumed that the
1211
                            less reliable. it is assumed that the
1243
                            requested accuracy has not been achieved.
1212
                            requested accuracy has not been achieved.
1244
           error messages
1213
           error messages
1245
                        = 1 maximum number of subdivisions allowed
1214
                        = 1 maximum number of subdivisions allowed
Line 1276... Line 1245...
1276
                        = 5 the integral is probably divergent, or
1245
                        = 5 the integral is probably divergent, or
1277
                            slowly convergent. it must be noted that
1246
                            slowly convergent. it must be noted that
1278
                            divergence can occur with any other value
1247
                            divergence can occur with any other value
1279
                            of ier.
1248
                            of ier.
1280
                        = 6 the input is invalid, because
1249
                        = 6 the input is invalid, because
1281
                            epsabs.le.0 and
1250
                            epsabs <= 0 and
1282
                            epsrel.lt.max(50*rel.mach.acc.,0.5d-28).
1251
                            epsrel < max(50*rel.mach.acc.,0.5d-28).
1283
                            result, abserr, neval, last, rlist(1),
1252
                            result, abserr, neval, last, rlist(1),
1284
                            iord(1) and elist(1) are set to zero.
1253
                            iord(1) and elist(1) are set to zero.
1285
                            alist(1) and blist(1) are set to a and b
1254
                            alist(1) and blist(1) are set to a and b
1286
                            respectively.
1255
                            respectively.
1287
 
1256
 
Line 1311... Line 1280...
1311
                    vector of dimension at least limit, the first k
1280
                    vector of dimension at least limit, the first k
1312
                    elements of which are pointers to the
1281
                    elements of which are pointers to the
1313
                    error estimates over the subintervals,
1282
                    error estimates over the subintervals,
1314
                    such that elist(iord(1)), ..., elist(iord(k))
1283
                    such that elist(iord(1)), ..., elist(iord(k))
1315
                    form a decreasing sequence, with k = last
1284
                    form a decreasing sequence, with k = last
1316
                    if last.le.(limit/2+2), and k = limit+1-last
1285
                    if last <= (limit/2+2), and k = limit+1-last
1317
                    otherwise
1286
                    otherwise
1318
 
1287
 
1319
           last   - int
1288
           last   - int
1320
                    number of subintervals actually produced in the
1289
                    number of subintervals actually produced in the
1321
                    subdivision process
1290
                    subdivision process
Line 1399... Line 1368...
1399
    *abserr = 0.;
1368
    *abserr = 0.;
1400
    alist__[1] = *a;
1369
    alist__[1] = *a;
1401
    blist[1] = *b;
1370
    blist[1] = *b;
1402
    rlist[1] = 0.;
1371
    rlist[1] = 0.;
1403
    elist[1] = 0.;
1372
    elist[1] = 0.;
1404
/* Computing MAX */
-
 
1405
    d__1 = epmach * 50.;
-
 
1406
    if (*epsabs <= 0. && (*epsrel < fmax2(d__1,5e-29))) {
1373
    if (*epsabs <= 0. && *epsrel < fmax2(epmach * 50., 5e-29)) {
1407
	*ier = 6;
1374
	*ier = 6;
-
 
1375
	return;
1408
    }
1376
    }
1409
    if (*ier == 6) return;
-
 
1410
 
1377
 
1411
/*           first approximation to the integral */
1378
/*           first approximation to the integral */
1412
/*           ----------------------------------- */
1379
/*           ----------------------------------- */
1413
 
1380
 
1414
    uflow = DBL_MIN;
1381
    uflow = DBL_MIN;
1415
    oflow = DBL_MAX;
1382
    oflow = DBL_MAX;
1416
    ierro = 0;
1383
    ierro = 0;
1417
    rdqk21(IS, a, b, result, abserr, &defabs, &resabs);
1384
    rdqk21(f, ex, a, b, result, abserr, &defabs, &resabs);
1418
 
1385
 
1419
/*           test on accuracy. */
1386
/*           test on accuracy. */
1420
 
1387
 
1421
    dres = fabs(*result);
1388
    dres = fabs(*result);
1422
/* Computing MAX */
-
 
1423
    d__1 = *epsabs, d__2 = *epsrel * dres;
1389
    errbnd = fmax2(*epsabs, *epsrel * dres);
1424
    errbnd = fmax2(d__1,d__2);
-
 
1425
    *last = 1;
1390
    *last = 1;
1426
    rlist[1] = *result;
1391
    rlist[1] = *result;
1427
    elist[1] = *abserr;
1392
    elist[1] = *abserr;
1428
    iord[1] = 1;
1393
    iord[1] = 1;
1429
    if (*abserr <= epmach * 100. * defabs && *abserr > errbnd) {
1394
    if (*abserr <= epmach * 100. * defabs && *abserr > errbnd)
1430
	*ier = 2;
1395
	*ier = 2;
1431
    }
-
 
1432
    if (*limit == 1) {
1396
    if (*limit == 1)
1433
	*ier = 1;
1397
	*ier = 1;
1434
    }
-
 
1435
    if (*ier != 0 || (*abserr <= errbnd && *abserr != resabs) 
1398
    if (*ier != 0 || (*abserr <= errbnd && *abserr != resabs)
1436
	|| *abserr == 0.) goto L140;
1399
	|| *abserr == 0.) goto L140;
1437
 
1400
 
1438
/*           initialization */
1401
/*           initialization */
1439
/*           -------------- */
1402
/*           -------------- */
1440
 
1403
 
Line 1459... Line 1422...
1459
    }
1422
    }
1460
 
1423
 
1461
/*           main do-loop */
1424
/*           main do-loop */
1462
/*           ------------ */
1425
/*           ------------ */
1463
 
1426
 
1464
    i__1 = *limit;
-
 
1465
    for (*last = 2; *last <= i__1; ++(*last)) {
1427
    for (*last = 2; *last <= *limit; ++(*last)) {
1466
 
1428
 
1467
/*           bisect the subinterval with the nrmax-th largest error */
1429
/*           bisect the subinterval with the nrmax-th largest error estimate. */
1468
/*           estimate. */
-
 
1469
 
1430
 
1470
	a1 = alist__[maxerr];
1431
	a1 = alist__[maxerr];
1471
	b1 = (alist__[maxerr] + blist[maxerr]) * .5;
1432
	b1 = (alist__[maxerr] + blist[maxerr]) * .5;
1472
	a2 = b1;
1433
	a2 = b1;
1473
	b2 = blist[maxerr];
1434
	b2 = blist[maxerr];
1474
	erlast = errmax;
1435
	erlast = errmax;
1475
	rdqk21(IS, &a1, &b1, &area1, &error1, &resabs, &defab1);
1436
	rdqk21(f, ex, &a1, &b1, &area1, &error1, &resabs, &defab1);
1476
	rdqk21(IS, &a2, &b2, &area2, &error2, &resabs, &defab2);
1437
	rdqk21(f, ex, &a2, &b2, &area2, &error2, &resabs, &defab2);
1477
 
1438
 
1478
/*           improve previous approximations to integral */
1439
/*           improve previous approximations to integral
1479
/*           and error and test for accuracy. */
1440
	     and error and test for accuracy. */
1480
 
1441
 
1481
	area12 = area1 + area2;
1442
	area12 = area1 + area2;
1482
	erro12 = error1 + error2;
1443
	erro12 = error1 + error2;
1483
	errsum = errsum + erro12 - errmax;
1444
	errsum = errsum + erro12 - errmax;
1484
	area = area + area12 - rlist[maxerr];
1445
	area = area + area12 - rlist[maxerr];
1485
	if (defab1 == error1 || defab2 == error2) {
1446
	if (defab1 == error1 || defab2 == error2) {
1486
	    goto L15;
1447
	    goto L15;
1487
	}
1448
	}
1488
	if ((d__1 = rlist[maxerr] - area12, fabs(d__1)) > fabs(area12) * 1e-5 ||
1449
	if (fabs(rlist[maxerr] - area12) > fabs(area12) * 1e-5 ||
1489
		 erro12 < errmax * .99) {
1450
		 erro12 < errmax * .99) {
1490
	    goto L10;
1451
	    goto L10;
1491
	}
1452
	}
1492
	if (extrap) {
1453
	if (extrap) {
1493
	    ++iroff2;
1454
	    ++iroff2;
Line 1500... Line 1461...
1500
	    ++iroff3;
1461
	    ++iroff3;
1501
	}
1462
	}
1502
L15:
1463
L15:
1503
	rlist[maxerr] = area1;
1464
	rlist[maxerr] = area1;
1504
	rlist[*last] = area2;
1465
	rlist[*last] = area2;
1505
/* Computing MAX */
-
 
1506
	d__1 = *epsabs, d__2 = *epsrel * fabs(area);
1466
	errbnd = fmax2(*epsabs, *epsrel * fabs(area));
1507
	errbnd = fmax2(d__1,d__2);
-
 
1508
 
1467
 
1509
/*           test for roundoff error and eventually set error flag. */
1468
/*           test for roundoff error and eventually set error flag. */
1510
 
1469
 
1511
	if (iroff1 + iroff2 >= 10 || iroff3 >= 20) {
1470
	if (iroff1 + iroff2 >= 10 || iroff3 >= 20)
1512
	    *ier = 2;
1471
	    *ier = 2;
1513
	}
-
 
1514
	if (iroff2 >= 5) {
1472
	if (iroff2 >= 5)
1515
	    ierro = 3;
1473
	    ierro = 3;
1516
	}
-
 
1517
 
-
 
1518
/*           set error flag in the case that the number of subintervals */
-
 
1519
/*           equals limit. */
-
 
1520
 
1474
 
-
 
1475
/* set error flag in the case that the number of subintervals equals limit. */
1521
	if (*last == *limit) {
1476
	if (*last == *limit)
1522
	    *ier = 1;
1477
	    *ier = 1;
1523
	}
-
 
1524
 
1478
 
1525
/*           set error flag in the case of bad integrand behaviour */
1479
/*           set error flag in the case of bad integrand behaviour
1526
/*           at a point of the integration range. */
1480
	     at a point of the integration range. */
1527
 
1481
 
1528
/* Computing MAX */
-
 
1529
	d__1 = fabs(a1), d__2 = fabs(b2);
1482
	if (fmax2(fabs(a1), fabs(b2)) <=
1530
	if (fmax2(d__1,d__2) <= (epmach * 100. + 1.) * (fabs(a2) + uflow * 1e3)) 
1483
	    (epmach * 100. + 1.) * (fabs(a2) + uflow * 1e3)) {
1531
		{
-
 
1532
	    *ier = 4;
1484
	    *ier = 4;
1533
	}
1485
	}
1534
 
1486
 
1535
/*           append the newly-created intervals to the list. */
1487
/*           append the newly-created intervals to the list. */
1536
 
1488
 
1537
	if (error2 > error1) {
1489
	if (error2 > error1) {
1538
	    goto L20;
1490
	    alist__[maxerr] = a2;
1539
	}
-
 
1540
	alist__[*last] = a2;
1491
	    alist__[*last] = a1;
-
 
1492
	    blist[*last] = b1;
1541
	blist[maxerr] = b1;
1493
	    rlist[maxerr] = area2;
1542
	blist[*last] = b2;
1494
	    rlist[*last] = area1;
1543
	elist[maxerr] = error1;
1495
	    elist[maxerr] = error2;
1544
	elist[*last] = error2;
1496
	    elist[*last] = error1;
1545
	goto L30;
1497
	} else {
1546
L20:
-
 
1547
	alist__[maxerr] = a2;
1498
	    alist__[*last] = a2;
1548
	alist__[*last] = a1;
1499
	    blist[maxerr] = b1;
1549
	blist[*last] = b1;
1500
	    blist[*last] = b2;
1550
	rlist[maxerr] = area2;
1501
	    elist[maxerr] = error1;
1551
	rlist[*last] = area1;
1502
	    elist[*last] = error2;
-
 
1503
	}
-
 
1504
 
-
 
1505
/*           call subroutine dqpsrt to maintain the descending ordering
1552
	elist[maxerr] = error2;
1506
	     in the list of error estimates and select the subinterval
1553
	elist[*last] = error1;
1507
	     with nrmax-th largest error estimate (to be bisected next). */
1554
 
1508
 
1555
/*           call subroutine dqpsrt to maintain the descending ordering */
-
 
1556
/*           in the list of error estimates and select the subinterval */
-
 
1557
/*           with nrmax-th largest error estimate (to be bisected next). */
-
 
1558
 
-
 
1559
L30:
1509
/*L30:*/
1560
	rdqpsrt(limit, last, &maxerr, &errmax, &elist[1], &iord[1], &nrmax);
1510
	rdqpsrt(limit, last, &maxerr, &errmax, &elist[1], &iord[1], &nrmax);
1561
/* ***jump out of do-loop */
-
 
1562
	if (errsum <= errbnd) {
-
 
1563
	    goto L115;
-
 
1564
	}
1511
 
1565
/* ***jump out of do-loop */
1512
	if (errsum <= errbnd)   goto L115;/* ***jump out of do-loop */
1566
	if (*ier != 0) {
1513
	if (*ier != 0)		goto L100;/* ***jump out of do-loop */
1567
	    goto L100;
-
 
1568
	}
1514
 
1569
	if (*last == 2) {
1515
	if (*last == 2)		goto L80;
1570
	    goto L80;
-
 
1571
	}
-
 
1572
	if (noext) {
1516
	if (noext)		goto L90;
1573
	    goto L90;
-
 
1574
	}
1517
 
1575
	erlarg -= erlast;
1518
	erlarg -= erlast;
1576
	if ((d__1 = b1 - a1, fabs(d__1)) > small) {
1519
	if (fabs(b1 - a1) > small) {
1577
	    erlarg += erro12;
1520
	    erlarg += erro12;
1578
	}
1521
	}
1579
	if (extrap) {
1522
	if (extrap) {
1580
	    goto L40;
1523
	    goto L40;
1581
	}
1524
	}
1582
 
1525
 
1583
/*           test whether the interval to be bisected next is the */
1526
/*           test whether the interval to be bisected next is the
1584
/*           smallest interval. */
1527
	     smallest interval. */
1585
 
1528
 
1586
	if ((d__1 = blist[maxerr] - alist__[maxerr], fabs(d__1)) > small) {
1529
	if (fabs(blist[maxerr] - alist__[maxerr]) > small) {
1587
	    goto L90;
1530
	    goto L90;
1588
	}
1531
	}
1589
	extrap = TRUE;
1532
	extrap = TRUE;
1590
	nrmax = 2;
1533
	nrmax = 2;
1591
L40:
1534
L40:
1592
	if (ierro == 3 || erlarg <= ertest) {
1535
	if (ierro == 3 || erlarg <= ertest) {
1593
	    goto L60;
1536
	    goto L60;
1594
	}
1537
	}
1595
 
1538
 
1596
/*           the smallest interval has the largest error. */
1539
/*           the smallest interval has the largest error.
1597
/*           before bisecting decrease the sum of the errors over the */
1540
	     before bisecting decrease the sum of the errors over the
1598
/*           larger intervals (erlarg) and perform extrapolation. */
1541
	     larger intervals (erlarg) and perform extrapolation. */
1599
 
1542
 
1600
	id = nrmax;
1543
	id = nrmax;
1601
	jupbnd = *last;
1544
	jupbnd = *last;
1602
	if (*last > *limit / 2 + 2) {
1545
	if (*last > *limit / 2 + 2) {
1603
	    jupbnd = *limit + 3 - *last;
1546
	    jupbnd = *limit + 3 - *last;
1604
	}
1547
	}
1605
	i__2 = jupbnd;
-
 
1606
	for (k = id; k <= i__2; ++k) {
1548
	for (k = id; k <= jupbnd; ++k) {
1607
	    maxerr = iord[nrmax];
1549
	    maxerr = iord[nrmax];
1608
	    errmax = elist[maxerr];
1550
	    errmax = elist[maxerr];
1609
/* ***jump out of do-loop */
-
 
1610
	    if ((d__1 = blist[maxerr] - alist__[maxerr], fabs(d__1)) > small) {
1551
	    if (fabs(blist[maxerr] - alist__[maxerr]) > small) {
1611
		goto L90;
1552
		goto L90;/* ***jump out of do-loop */
1612
	    }
1553
	    }
1613
	    ++nrmax;
1554
	    ++nrmax;
1614
/* L50: */
1555
/* L50: */
1615
	}
1556
	}
1616
 
1557
 
Line 1629... Line 1570...
1629
	}
1570
	}
1630
	ktmin = 0;
1571
	ktmin = 0;
1631
	*abserr = abseps;
1572
	*abserr = abseps;
1632
	*result = reseps;
1573
	*result = reseps;
1633
	correc = erlarg;
1574
	correc = erlarg;
1634
/* Computing MAX */
-
 
1635
	d__1 = *epsabs, d__2 = *epsrel * fabs(reseps);
1575
	ertest = fmax2(*epsabs, *epsrel * fabs(reseps));
1636
	ertest = fmax2(d__1,d__2);
-
 
1637
/* ***jump out of do-loop */
-
 
1638
	if (*abserr <= ertest) {
1576
	if (*abserr <= ertest) {
1639
	    goto L100;
1577
	    goto L100;/* ***jump out of do-loop */
1640
	}
1578
	}
1641
 
1579
 
1642
/*           prepare bisection of the smallest interval. */
1580
/*           prepare bisection of the smallest interval. */
1643
 
1581
 
1644
L70:
1582
L70:
Line 1654... Line 1592...
1654
	extrap = FALSE;
1592
	extrap = FALSE;
1655
	small *= .5;
1593
	small *= .5;
1656
	erlarg = errsum;
1594
	erlarg = errsum;
1657
	goto L90;
1595
	goto L90;
1658
L80:
1596
L80:
1659
	small = (d__1 = *b - *a, fabs(d__1)) * .375;
1597
	small = fabs(*b - *a) * .375;
1660
	erlarg = errsum;
1598
	erlarg = errsum;
1661
	ertest = errbnd;
1599
	ertest = errbnd;
1662
	rlist2[1] = area;
1600
	rlist2[1] = area;
1663
L90:
1601
L90:
1664
	;
1602
	;
1665
    }
1603
    }
1666
 
1604
 
1667
/*           set final result and error estimate. */
-
 
1668
/*           ------------------------------------ */
-
 
1669
 
1605
 
1670
L100:
1606
L100:/*		set final result and error estimate. */
-
 
1607
/*		------------------------------------ */
1671
    if (*abserr == oflow) {
1608
    if (*abserr == oflow) 	goto L115;
1672
	goto L115;
-
 
1673
    }
-
 
1674
    if (*ier + ierro == 0) {
1609
    if (*ier + ierro == 0) 	goto L110;
1675
	goto L110;
-
 
1676
    }
-
 
1677
    if (ierro == 3) {
1610
    if (ierro == 3)
1678
	*abserr += correc;
1611
	*abserr += correc;
1679
    }
-
 
1680
    if (*ier == 0) {
1612
    if (*ier == 0)
1681
	*ier = 3;
1613
	*ier = 3;
1682
    }
-
 
1683
    if (*result != 0. && area != 0.) {
1614
    if (*result != 0. && area != 0.) goto L105;
1684
	goto L105;
-
 
1685
    }
-
 
1686
    if (*abserr > errsum) {
1615
    if (*abserr > errsum) 	goto L115;
1687
	goto L115;
-
 
1688
    }
-
 
1689
    if (area == 0.) {
1616
    if (area == 0.) 		goto L130;
1690
	goto L130;
-
 
1691
    }
-
 
1692
    goto L110;
1617
    goto L110;
-
 
1618
 
1693
L105:
1619
L105:
1694
    if (*abserr / fabs(*result) > errsum / fabs(area)) {
1620
    if (*abserr / fabs(*result) > errsum / fabs(area)) {
1695
	goto L115;
1621
	goto L115;
1696
    }
1622
    }
1697
 
1623
 
1698
/*           test on divergence. */
1624
L110:/*		test on divergence. */
1699
 
-
 
1700
L110:
-
 
1701
/* Computing MAX */
-
 
1702
    d__1 = fabs(*result), d__2 = fabs(area);
-
 
1703
    if (ksgn == -1 && fmax2(d__1,d__2) <= defabs * .01) {
1625
    if (ksgn == -1 && fmax2(fabs(*result), fabs(area)) <= defabs * .01) {
1704
	goto L130;
1626
	goto L130;
1705
    }
1627
    }
1706
    if (.01 > *result / area || *result / area > 100. || errsum > fabs(area)) {
1628
    if (.01 > *result / area || *result / area > 100. || errsum > fabs(area)) {
1707
	*ier = 5;
1629
	*ier = 5;
1708
    }
1630
    }
1709
    goto L130;
1631
    goto L130;
1710
 
1632
 
1711
/*           compute global integral sum. */
1633
L115:/*		compute global integral sum. */
1712
 
-
 
1713
L115:
-
 
1714
    *result = 0.;
1634
    *result = 0.;
1715
    i__1 = *last;
-
 
1716
    for (k = 1; k <= i__1; ++k) {
1635
    for (k = 1; k <= *last; ++k)
1717
	*result += rlist[k];
1636
	*result += rlist[k];
1718
/* L120: */
-
 
1719
    }
-
 
1720
    *abserr = errsum;
1637
    *abserr = errsum;
1721
L130:
1638
L130:
1722
    if (*ier > 2) 
1639
    if (*ier > 2)
1723
L140:
1640
L140:
1724
    *neval = *last * 42 - 21;
1641
    *neval = *last * 42 - 21;
1725
    return;
1642
    return;
1726
} /* rdqagse_ */
1643
} /* rdqagse_ */
1727
 
1644
 
-
 
1645
 
1728
static void rdqk15i(IntStruct IS, double *boun, int *inf, double *a, 
1646
static void rdqk15i(integr_fn f, void *ex,
1729
		    double *b, double *result, double *abserr, double *
1647
		    double *boun, int *inf, double *a, double *b,
1730
		    resabs, double *resasc)
1648
		    double *result,
-
 
1649
		    double *abserr, double *resabs, double *resasc)
1731
{
1650
{
1732
    /* Initialized data */
1651
    /* Initialized data */
1733
 
1652
 
-
 
1653
    static double wg[8] = {
1734
    static double wg[8] = { 0.,.129484966168869693270611432679082,0.,
1654
	    0., .129484966168869693270611432679082,
1735
	    .27970539148927666790146777142378,0.,
1655
	    0., .27970539148927666790146777142378,
1736
	    .381830050505118944950369775488975,0.,
1656
	    0., .381830050505118944950369775488975,
1737
	    .417959183673469387755102040816327 };
1657
	    0., .417959183673469387755102040816327 };
-
 
1658
    static double xgk[8] = {
1738
    static double xgk[8] = { .991455371120812639206854697526329,
1659
	    .991455371120812639206854697526329,
1739
	    .949107912342758524526189684047851,
1660
	    .949107912342758524526189684047851,
1740
	    .864864423359769072789712788640926,
1661
	    .864864423359769072789712788640926,
1741
	    .741531185599394439863864773280788,
1662
	    .741531185599394439863864773280788,
1742
	    .58608723546769113029414483825873,
1663
	    .58608723546769113029414483825873,
1743
	    .405845151377397166906606412076961,
1664
	    .405845151377397166906606412076961,
1744
	    .207784955007898467600689403773245,0. };
1665
	    .207784955007898467600689403773245, 0. };
-
 
1666
    static double wgk[8] = {
1745
    static double wgk[8] = { .02293532201052922496373200805897,
1667
	    .02293532201052922496373200805897,
1746
	    .063092092629978553290700663189204,
1668
	    .063092092629978553290700663189204,
1747
	    .104790010322250183839876322541518,
1669
	    .104790010322250183839876322541518,
1748
	    .140653259715525918745189590510238,
1670
	    .140653259715525918745189590510238,
1749
	    .16900472663926790282658342659855,
1671
	    .16900472663926790282658342659855,
1750
	    .190350578064785409913256402421014,
1672
	    .190350578064785409913256402421014,
1751
	    .204432940075298892414161999234649,
1673
	    .204432940075298892414161999234649,
1752
	    .209482141084727828012999174891714 };
1674
	    .209482141084727828012999174891714 };
1753
 
1675
 
1754
    /* System generated locals */
-
 
1755
    double d__1, d__2, d__3;
-
 
1756
 
-
 
1757
    /* Local variables */
1676
    /* Local variables */
1758
    double absc, dinf, resg, resk, fsum, absc1, absc2, fval1, fval2;
1677
    double absc, dinf, resg, resk, fsum, absc1, absc2, fval1, fval2;
1759
    int j;
1678
    int j;
1760
    double hlgth, centr, reskh, uflow;
1679
    double hlgth, centr, reskh, uflow;
1761
    double tabsc1, tabsc2, fc, epmach;
1680
    double tabsc1, tabsc2, fc, epmach;
1762
    double fv1[7], fv2[7], vec[15], vec2[15];
1681
    double fv1[7], fv2[7], vec[15], vec2[15];
1763
 
1682
 
1764
/* 
1683
/*
1765
***begin prologue  dqk15i
1684
***begin prologue  dqk15i
1766
***date written   800101   (yymmdd)
1685
***date written   800101   (yymmdd)
1767
***revision date  830518   (yymmdd)
1686
***revision date  830518   (yymmdd)
1768
***category no.  h2a3a2,h2a4a2
1687
***category no.  h2a3a2,h2a4a2
1769
***keywords  15-point transformed gauss-kronrod rules
1688
***keywords  15-point transformed gauss-kronrod rules
Line 1828... Line 1747...
1828
                      approximation to the integral of
1747
                      approximation to the integral of
1829
                      abs((transformed integrand)-i/(b-a)) over (a,b)
1748
                      abs((transformed integrand)-i/(b-a)) over (a,b)
1830
 
1749
 
1831
***references  (none)
1750
***references  (none)
1832
***end prologue  dqk15i
1751
***end prologue  dqk15i
1833
 
1752
 
1834
 
1753
 
1835
          the abscissae and weights are supplied for the interval
1754
          the abscissae and weights are supplied for the interval
1836
          (-1,1).  because of symmetry only the positive abscissae and
1755
          (-1,1).  because of symmetry only the positive abscissae and
1837
          their corresponding weights are given.
1756
          their corresponding weights are given.
1838
 
1757
 
Line 1867... Line 1786...
1867
 
1786
 
1868
          machine dependent constants
1787
          machine dependent constants
1869
          ---------------------------
1788
          ---------------------------
1870
 
1789
 
1871
          epmach is the largest relative spacing.
1790
          epmach is the largest relative spacing.
1872
          uflow is the smallest positive magnitude. */
1791
          uflow is the smallest positive magnitude.
-
 
1792
*/
1873
 
1793
 
1874
/* ***first executable statement  dqk15i */
1794
/* ***first executable statement  dqk15i */
1875
    epmach = DBL_EPSILON;
1795
    epmach = DBL_EPSILON;
1876
    uflow = DBL_MIN;
1796
    uflow = DBL_MIN;
1877
    dinf = (double) imin2(1, *inf);
1797
    dinf = (double) imin2(1, *inf);
Line 1895... Line 1815...
1895
	    vec2[(j << 1) - 1] = -tabsc1;
1815
	    vec2[(j << 1) - 1] = -tabsc1;
1896
	    vec2[j * 2] = -tabsc2;
1816
	    vec2[j * 2] = -tabsc2;
1897
	}
1817
	}
1898
/* L5: */
1818
/* L5: */
1899
    }
1819
    }
1900
    Rintfn(vec, 15, IS);
1820
    f(vec, 15, ex); /* -> new vec[] overwriting old vec[] */
1901
    if (*inf == 2) Rintfn(vec2, 15, IS);
1821
    if (*inf == 2) f(vec2, 15, ex);
1902
    fval1 = vec[0];
1822
    fval1 = vec[0];
1903
    if (*inf == 2) fval1 += vec2[0];
1823
    if (*inf == 2) fval1 += vec2[0];
1904
    fc = fval1 / centr / centr;
1824
    fc = fval1 / centr / centr;
1905
 
1825
 
1906
/*           compute the 15-point kronrod approximation to */
1826
/*           compute the 15-point kronrod approximation to
1907
/*           the integral, and estimate the error. */
1827
	     the integral, and estimate the error. */
1908
 
1828
 
1909
    resg = wg[7] * fc;
1829
    resg = wg[7] * fc;
1910
    resk = wgk[7] * fc;
1830
    resk = wgk[7] * fc;
1911
    *resabs = fabs(resk);
1831
    *resabs = fabs(resk);
1912
    for (j = 1; j <= 7; ++j) {
1832
    for (j = 1; j <= 7; ++j) {
Line 1932... Line 1852...
1932
	resk += wgk[j - 1] * fsum;
1852
	resk += wgk[j - 1] * fsum;
1933
	*resabs += wgk[j - 1] * (fabs(fval1) + fabs(fval2));
1853
	*resabs += wgk[j - 1] * (fabs(fval1) + fabs(fval2));
1934
/* L10: */
1854
/* L10: */
1935
    }
1855
    }
1936
    reskh = resk * .5;
1856
    reskh = resk * .5;
1937
    *resasc = wgk[7] * (d__1 = fc - reskh, fabs(d__1));
1857
    *resasc = wgk[7] * fabs(fc - reskh);
1938
    for (j = 1; j <= 7; ++j) {
1858
    for (j = 1; j <= 7; ++j) {
1939
	*resasc += wgk[j - 1] * ((d__1 = fv1[j - 1] - reskh, fabs(d__1)) + (
1859
	*resasc += wgk[j - 1] * (fabs(fv1[j - 1] - reskh) +
1940
		d__2 = fv2[j - 1] - reskh, fabs(d__2)));
1860
				 fabs(fv2[j - 1] - reskh));
1941
/* L20: */
1861
/* L20: */
1942
    }
1862
    }
1943
    *result = resk * hlgth;
1863
    *result = resk * hlgth;
1944
    *resasc *= hlgth;
1864
    *resasc *= hlgth;
1945
    *resabs *= hlgth;
1865
    *resabs *= hlgth;
1946
    *abserr = (d__1 = (resk - resg) * hlgth, fabs(d__1));
1866
    *abserr = fabs((resk - resg) * hlgth);
1947
    if (*resasc != 0. && *abserr != 0.) {
1867
    if (*resasc != 0. && *abserr != 0.) {
1948
/* Computing MIN */
-
 
1949
	d__3 = *abserr * 200. / *resasc;
-
 
1950
	d__1 = 1., d__2 = pow(d__3, 1.5);
-
 
1951
	*abserr = *resasc * fmin2(d__1,d__2);
1868
	*abserr = *resasc * fmin2(1., pow(*abserr * 200. / *resasc, 1.5));
1952
    }
1869
    }
1953
    if (*resabs > uflow / (epmach * 50.)) {
1870
    if (*resabs > uflow / (epmach * 50.)) {
1954
/* Computing MAX */
-
 
1955
	d__1 = epmach * 50. * *resabs;
-
 
1956
	*abserr = fmax2(d__1,*abserr);
1871
	*abserr = fmax2(epmach * 50. * *resabs, *abserr);
1957
    }
1872
    }
1958
    return;
1873
    return;
1959
} /* rdqk15i_ */
1874
} /* rdqk15i_ */
1960
 
1875
 
1961
static void rdqelg(int *n, double *epstab, double *
1876
static void rdqelg(int *n, double *epstab, double *
1962
		   result, double *abserr, double *res3la, int *nres)
1877
		   result, double *abserr, double *res3la, int *nres)
1963
{
1878
{
1964
    /* System generated locals */
-
 
1965
    int i__1;
-
 
1966
    double d__1, d__2, d__3;
-
 
1967
 
-
 
1968
    /* Local variables */
1879
    /* Local variables */
1969
    int indx;
-
 
1970
    double e1abs;
-
 
1971
    int i__;
-
 
1972
    double e0, e1, e2, e3, error, oflow;
1880
    int i__, indx, ib, ib2, ie, k1, k2, k3, num, newelm, limexp;
1973
    int k1;
-
 
1974
    int k2, k3;
-
 
1975
    double delta1, delta2, delta3;
1881
    double delta1, delta2, delta3, e0, e1, e1abs, e2, e3, epmach, epsinf;
1976
    int ib, ie;
-
 
1977
    double epmach, ss, epsinf;
1882
    double oflow, ss, res;
1978
    int newelm, ib2, limexp;
-
 
1979
    double res;
-
 
1980
    int num;
-
 
1981
    double err1, err2, err3, tol1, tol2, tol3;
1883
    double errA, err1, err2, err3, tol1, tol2, tol3;
1982
 
1884
 
1983
/* ***begin prologue  dqelg
1885
/* ***begin prologue  dqelg
1984
***refer to  dqagie,dqagoe,dqagpe,dqagse
1886
***refer to  dqagie,dqagoe,dqagpe,dqagse
1985
***revision date  830518   (yymmdd)
1887
***revision date  830518   (yymmdd)
1986
***keywords  epsilon algorithm, convergence acceleration,
1888
***keywords  epsilon algorithm, convergence acceleration,
Line 2036... Line 1938...
2036
          e1       element in the epsilon table is based
1938
          e1       element in the epsilon table is based
2037
          e2
1939
          e2
2038
          e3                 e0
1940
          e3                 e0
2039
                       e3    e1    new
1941
                       e3    e1    new
2040
                             e2
1942
                             e2
-
 
1943
 
2041
          newelm - number of elements to be computed in the new
1944
          newelm - number of elements to be computed in the new diagonal
2042
                   diagonal
-
 
2043
          error  - error = abs(e1-e0)+abs(e2-e1)+abs(new-e2)
1945
          errA   - errA = abs(e1-e0)+abs(e2-e1)+abs(new-e2)
2044
          result - the element in the new diagonal with least value
1946
          result - the element in the new diagonal with least value of errA
2045
                   of error
-
 
2046
 
1947
 
2047
          machine dependent constants
1948
          machine dependent constants
2048
          ---------------------------
1949
          ---------------------------
2049
 
1950
 
2050
          epmach is the largest relative spacing.
1951
          epmach is the largest relative spacing.
Line 2071... Line 1972...
2071
    epstab[*n + 2] = epstab[*n];
1972
    epstab[*n + 2] = epstab[*n];
2072
    newelm = (*n - 1) / 2;
1973
    newelm = (*n - 1) / 2;
2073
    epstab[*n] = oflow;
1974
    epstab[*n] = oflow;
2074
    num = *n;
1975
    num = *n;
2075
    k1 = *n;
1976
    k1 = *n;
2076
    i__1 = newelm;
-
 
2077
    for (i__ = 1; i__ <= i__1; ++i__) {
1977
    for (i__ = 1; i__ <= newelm; ++i__) {
2078
	k2 = k1 - 1;
1978
	k2 = k1 - 1;
2079
	k3 = k1 - 2;
1979
	k3 = k1 - 2;
2080
	res = epstab[k1 + 2];
1980
	res = epstab[k1 + 2];
2081
	e0 = epstab[k3];
1981
	e0 = epstab[k3];
2082
	e1 = epstab[k2];
1982
	e1 = epstab[k2];
2083
	e2 = res;
1983
	e2 = res;
2084
	e1abs = fabs(e1);
1984
	e1abs = fabs(e1);
2085
	delta2 = e2 - e1;
1985
	delta2 = e2 - e1;
2086
	err2 = fabs(delta2);
1986
	err2 = fabs(delta2);
2087
/* Computing MAX */
-
 
2088
	d__1 = fabs(e2);
-
 
2089
	tol2 = fmax2(d__1,e1abs) * epmach;
1987
	tol2 = fmax2(fabs(e2), e1abs) * epmach;
2090
	delta3 = e1 - e0;
1988
	delta3 = e1 - e0;
2091
	err3 = fabs(delta3);
1989
	err3 = fabs(delta3);
2092
/* Computing MAX */
1990
	tol3 = fmax2(e1abs, fabs(e0)) * epmach;
2093
	d__1 = e1abs, d__2 = fabs(e0);
1991
	if (err2 <= tol2 && err3 <= tol3) {
-
 
1992
	    /*           if e0, e1 and e2 are equal to within machine
2094
	tol3 = fmax2(d__1,d__2) * epmach;
1993
			 accuracy, convergence is assumed. */
2095
	if (err2 > tol2 || err3 > tol3) {
1994
	    *result = res;/*		result = e2 */
-
 
1995
	    *abserr = err2 + err3;/*	abserr = fabs(e1-e0)+fabs(e2-e1) */
-
 
1996
 
2096
	    goto L10;
1997
	    goto L100;	/* ***jump out of do-loop */
2097
	}
1998
	}
2098
 
1999
 
2099
/*           if e0, e1 and e2 are equal to within machine */
-
 
2100
/*           accuracy, convergence is assumed. */
-
 
2101
/*           result = e2 */
-
 
2102
/*           abserr = fabs(e1-e0)+fabs(e2-e1) */
-
 
2103
 
-
 
2104
	*result = res;
-
 
2105
	*abserr = err2 + err3;
-
 
2106
/* ***jump out of do-loop */
-
 
2107
	goto L100;
-
 
2108
L10:
-
 
2109
	e3 = epstab[k1];
2000
	e3 = epstab[k1];
2110
	epstab[k1] = e1;
2001
	epstab[k1] = e1;
2111
	delta1 = e1 - e3;
2002
	delta1 = e1 - e3;
2112
	err1 = fabs(delta1);
2003
	err1 = fabs(delta1);
2113
/* Computing MAX */
-
 
2114
	d__1 = e1abs, d__2 = fabs(e3);
-
 
2115
	tol1 = fmax2(d__1,d__2) * epmach;
2004
	tol1 = fmax2(e1abs, fabs(e3)) * epmach;
2116
 
2005
 
2117
/*           if two elements are very close to each other, omit */
2006
/*           if two elements are very close to each other, omit
2118
/*           a part of the table by adjusting the value of n */
2007
	     a part of the table by adjusting the value of n */
2119
 
2008
 
2120
	if (err1 <= tol1 || err2 <= tol2 || err3 <= tol3) {
2009
	if (err1 > tol1 && err2 > tol2 && err3 > tol3) {
2121
	    goto L20;
-
 
2122
	}
-
 
2123
	ss = 1. / delta1 + 1. / delta2 - 1. / delta3;
2010
	    ss = 1. / delta1 + 1. / delta2 - 1. / delta3;
2124
	epsinf = (d__1 = ss * e1, fabs(d__1));
2011
	    epsinf = fabs(ss * e1);
2125
 
2012
 
2126
/*           test to detect irregular behaviour in the table, and */
2013
/*           test to detect irregular behaviour in the table, and
2127
/*           eventually omit a part of the table adjusting the value */
2014
	     eventually omit a part of the table adjusting the value of n. */
2128
/*           of n. */
-
 
2129
 
2015
 
2130
	if (epsinf > 1e-4) {
2016
	    if (epsinf > 1e-4) {
2131
	    goto L30;
2017
		goto L30;
-
 
2018
	    }
2132
	}
2019
	}
2133
L20:
2020
 
2134
	*n = i__ + i__ - 1;
2021
	*n = i__ + i__ - 1;
2135
/* ***jump out of do-loop */
2022
	goto L50;/* ***jump out of do-loop */
2136
	goto L50;
-
 
2137
 
2023
 
2138
/*           compute a new element and eventually adjust */
-
 
2139
/*           the value of result. */
-
 
2140
 
2024
 
-
 
2025
L30:/* compute a new element and eventually adjust the value of result. */
2141
L30:
2026
 
2142
	res = e1 + 1. / ss;
2027
	res = e1 + 1. / ss;
2143
	epstab[k1] = res;
2028
	epstab[k1] = res;
2144
	k1 += -2;
2029
	k1 += -2;
2145
	error = err2 + (d__1 = res - e2, fabs(d__1)) + err3;
2030
	errA = err2 + fabs(res - e2) + err3;
2146
	if (error > *abserr) {
2031
	if (errA <= *abserr) {
-
 
2032
	    *abserr = errA;
2147
	    goto L40;
2033
	    *result = res;
2148
	}
2034
	}
2149
	*abserr = error;
-
 
2150
	*result = res;
-
 
2151
L40:
-
 
2152
	;
-
 
2153
    }
2035
    }
2154
 
2036
 
2155
/*           shift the table. */
2037
/*           shift the table. */
2156
 
2038
 
2157
L50:
2039
L50:
2158
    if (*n == limexp) {
2040
    if (*n == limexp) {
2159
	*n = (limexp / 2 << 1) - 1;
2041
	*n = (limexp / 2 << 1) - 1;
2160
    }
2042
    }
2161
    ib = 1;
2043
 
2162
    if (num / 2 << 1 == num) {
2044
    if (num / 2 << 1 == num) ib = 2; else ib = 1;
2163
	ib = 2;
-
 
2164
    }
-
 
2165
    ie = newelm + 1;
2045
    ie = newelm + 1;
2166
    i__1 = ie;
-
 
2167
    for (i__ = 1; i__ <= i__1; ++i__) {
2046
    for (i__ = 1; i__ <= ie; ++i__) {
2168
	ib2 = ib + 2;
2047
	ib2 = ib + 2;
2169
	epstab[ib] = epstab[ib2];
2048
	epstab[ib] = epstab[ib2];
2170
	ib = ib2;
2049
	ib = ib2;
2171
/* L60: */
-
 
2172
    }
2050
    }
2173
    if (num == *n) {
2051
    if (num != *n) {
2174
	goto L80;
-
 
2175
    }
-
 
2176
    indx = num - *n + 1;
2052
	indx = num - *n + 1;
2177
    i__1 = *n;
-
 
2178
    for (i__ = 1; i__ <= i__1; ++i__) {
2053
	for (i__ = 1; i__ <= *n; ++i__) {
2179
	epstab[i__] = epstab[indx];
2054
	    epstab[i__] = epstab[indx];
2180
	++indx;
2055
	    ++indx;
2181
/* L70: */
2056
	}
2182
    }
2057
    }
2183
L80:
2058
    /*L80:*/
2184
    if (*nres >= 4) {
2059
    if (*nres >= 4) {
2185
	goto L90;
2060
	/* L90: */
-
 
2061
	*abserr = fabs(*result - res3la[3]) +
-
 
2062
	          fabs(*result - res3la[2]) +
-
 
2063
	          fabs(*result - res3la[1]);
-
 
2064
	res3la[1] = res3la[2];
-
 
2065
	res3la[2] = res3la[3];
-
 
2066
	res3la[3] = *result;
-
 
2067
    } else {
-
 
2068
	res3la[*nres] = *result;
-
 
2069
	*abserr = oflow;
2186
    }
2070
    }
2187
    res3la[*nres] = *result;
-
 
2188
    *abserr = oflow;
-
 
2189
    goto L100;
-
 
2190
 
-
 
2191
/*           compute error estimate */
-
 
2192
 
2071
 
2193
L90:
-
 
2194
    *abserr = (d__1 = *result - res3la[3], fabs(d__1)) + (d__2 = *result - 
-
 
2195
	    res3la[2], fabs(d__2)) + (d__3 = *result - res3la[1], fabs(d__3));
-
 
2196
    res3la[1] = res3la[2];
-
 
2197
    res3la[2] = res3la[3];
-
 
2198
    res3la[3] = *result;
-
 
2199
L100:
-
 
2200
/* Computing MAX */
2072
L100:/* compute error estimate */
2201
    d__1 = *abserr, d__2 = epmach * 5. * fabs(*result);
2073
    *abserr = fmax2(*abserr, epmach * 5. * fabs(*result));
2202
    *abserr = fmax2(d__1,d__2);
-
 
2203
    return;
2074
    return;
2204
} /* rdqelg_ */
2075
} /* rdqelg_ */
2205
 
2076
 
2206
static void  rdqk21(IntStruct IS, double *a, double *b, double *result,
2077
static void  rdqk21(integr_fn f, void *ex, double *a, double *b, double *result,
2207
		    double *abserr, double *resabs, double *resasc)
2078
		    double *abserr, double *resabs, double *resasc)
2208
{
2079
{
2209
    /* Initialized data */
2080
    /* Initialized data */
2210
 
2081
 
2211
    static double wg[5] = { .066671344308688137593568809893332,
2082
    static double wg[5] = { .066671344308688137593568809893332,
Line 2233... Line 2104...
2233
	    .134709217311473325928054001771707,
2104
	    .134709217311473325928054001771707,
2234
	    .142775938577060080797094273138717,
2105
	    .142775938577060080797094273138717,
2235
	    .147739104901338491374841515972068,
2106
	    .147739104901338491374841515972068,
2236
	    .149445554002916905664936468389821 };
2107
	    .149445554002916905664936468389821 };
2237
 
2108
 
2238
    /* System generated locals */
-
 
2239
    double d__1, d__2, d__3;
-
 
2240
 
-
 
2241
 
2109
 
2242
    /* Local variables */
2110
    /* Local variables */
-
 
2111
    double fv1[10], fv2[10], vec[21];
2243
    double absc, resg, resk, fsum, fval1, fval2;
2112
    double absc, resg, resk, fsum, fval1, fval2;
2244
    int jtwm1, j;
-
 
2245
    double hlgth, centr, reskh, uflow;
2113
    double hlgth, centr, reskh, uflow;
2246
    double fc, epmach, dhlgth;
2114
    double fc, epmach, dhlgth;
2247
    double fv1[10], fv2[10], vec[21];
-
 
2248
    int jtw;
2115
    int j, jtw, jtwm1;
2249
 
2116
 
2250
/* ***begin prologue  dqk21
2117
/* ***begin prologue  dqk21
2251
***date written   800101   (yymmdd)
2118
***date written   800101   (yymmdd)
2252
***revision date  830518   (yymmdd)
2119
***revision date  830518   (yymmdd)
2253
***category no.  h2a1a2
2120
***category no.  h2a1a2
Line 2347... Line 2214...
2347
 
2214
 
2348
    centr = (*a + *b) * .5;
2215
    centr = (*a + *b) * .5;
2349
    hlgth = (*b - *a) * .5;
2216
    hlgth = (*b - *a) * .5;
2350
    dhlgth = fabs(hlgth);
2217
    dhlgth = fabs(hlgth);
2351
 
2218
 
2352
/*           compute the 21-point kronrod approximation to */
2219
/*           compute the 21-point kronrod approximation to
2353
/*           the integral, and estimate the absolute error. */
2220
	     the integral, and estimate the absolute error. */
2354
 
2221
 
2355
    resg = 0.;
2222
    resg = 0.;
2356
    vec[0] = centr;
2223
    vec[0] = centr;
2357
    for (j = 1; j <= 5; ++j) {
2224
    for (j = 1; j <= 5; ++j) {
2358
	jtw = j << 1;
2225
	jtw = j << 1;
Line 2363... Line 2230...
2363
    }
2230
    }
2364
    for (j = 1; j <= 5; ++j) {
2231
    for (j = 1; j <= 5; ++j) {
2365
	jtwm1 = (j << 1) - 1;
2232
	jtwm1 = (j << 1) - 1;
2366
	absc = hlgth * xgk[jtwm1 - 1];
2233
	absc = hlgth * xgk[jtwm1 - 1];
2367
	vec[(j << 1) + 9] = centr - absc;
2234
	vec[(j << 1) + 9] = centr - absc;
2368
/* L6: */
-
 
2369
	vec[(j << 1) + 10] = centr + absc;
2235
	vec[(j << 1) + 10] = centr + absc;
2370
    }
2236
    }
2371
    Rintfn(vec, 21, IS);
2237
    f(vec, 21, ex);
2372
    fc = vec[0];
2238
    fc = vec[0];
2373
    resk = wgk[10] * fc;
2239
    resk = wgk[10] * fc;
2374
    *resabs = fabs(resk);
2240
    *resabs = fabs(resk);
2375
    for (j = 1; j <= 5; ++j) {
2241
    for (j = 1; j <= 5; ++j) {
2376
	jtw = j << 1;
2242
	jtw = j << 1;
Line 2396... Line 2262...
2396
	resk += wgk[jtwm1 - 1] * fsum;
2262
	resk += wgk[jtwm1 - 1] * fsum;
2397
	*resabs += wgk[jtwm1 - 1] * (fabs(fval1) + fabs(fval2));
2263
	*resabs += wgk[jtwm1 - 1] * (fabs(fval1) + fabs(fval2));
2398
/* L15: */
2264
/* L15: */
2399
    }
2265
    }
2400
    reskh = resk * .5;
2266
    reskh = resk * .5;
2401
    *resasc = wgk[10] * (d__1 = fc - reskh, fabs(d__1));
2267
    *resasc = wgk[10] * fabs(fc - reskh);
2402
    for (j = 1; j <= 10; ++j) {
2268
    for (j = 1; j <= 10; ++j) {
2403
	*resasc += wgk[j - 1] * ((d__1 = fv1[j - 1] - reskh, fabs(d__1)) + (
2269
	*resasc += wgk[j - 1] * (fabs(fv1[j - 1] - reskh) +
2404
		d__2 = fv2[j - 1] - reskh, fabs(d__2)));
2270
				 fabs(fv2[j - 1] - reskh));
2405
/* L20: */
2271
/* L20: */
2406
    }
2272
    }
2407
    *result = resk * hlgth;
2273
    *result = resk * hlgth;
2408
    *resabs *= dhlgth;
2274
    *resabs *= dhlgth;
2409
    *resasc *= dhlgth;
2275
    *resasc *= dhlgth;
2410
    *abserr = (d__1 = (resk - resg) * hlgth, fabs(d__1));
2276
    *abserr = fabs((resk - resg) * hlgth);
2411
    if (*resasc != 0. && *abserr != 0.) {
2277
    if (*resasc != 0. && *abserr != 0.) {
2412
/* Computing MIN */
-
 
2413
	d__3 = *abserr * 200. / *resasc;
-
 
2414
	d__1 = 1., d__2 = pow(d__3, 1.5);
-
 
2415
	*abserr = *resasc * fmin2(d__1,d__2);
2278
	*abserr = *resasc * fmin2(1., pow(*abserr * 200. / *resasc, 1.5));
2416
    }
2279
    }
2417
    if (*resabs > uflow / (epmach * 50.)) {
2280
    if (*resabs > uflow / (epmach * 50.)) {
2418
/* Computing MAX */
-
 
2419
	d__1 = epmach * 50. * *resabs;
-
 
2420
	*abserr = fmax2(d__1,*abserr);
2281
	*abserr = fmax2(epmach * 50. * *resabs, *abserr);
2421
    }
2282
    }
2422
    return;
2283
    return;
2423
} /* rdqk21_ */
2284
} /* rdqk21_ */
2424
 
2285
 
2425
static void rdqpsrt(int *limit, int *last, int *maxerr, 
2286
static void rdqpsrt(int *limit, int *last, int *maxerr,
2426
		    double *ermax, double *elist, int *iord, int *nrmax)
2287
		    double *ermax, double *elist, int *iord, int *nrmax)
2427
{
2288
{
2428
    /* System generated locals */
-
 
2429
    int i__1;
-
 
2430
 
-
 
2431
    /* Local variables */
2289
    /* Local variables */
2432
    int ibeg, jbnd, i__, j, k, isucc, jupbn;
2290
    int i, j, k, ido, jbnd, isucc, jupbn;
2433
    double errmin, errmax;
2291
    double errmin, errmax;
2434
    int ido;
-
 
2435
 
2292
 
2436
/* ***begin prologue  dqpsrt */
2293
/* ***begin prologue  dqpsrt
2437
/* ***refer to  dqage,dqagie,dqagpe,dqawse */
2294
 ***refer to  dqage,dqagie,dqagpe,dqawse
2438
/* ***routines called  (none) */
2295
 ***routines called  (none)
2439
/* ***revision date  810101   (yymmdd) */
2296
 ***revision date  810101   (yymmdd)
2440
/* ***keywords  sequential sorting */
2297
 ***keywords  sequential sorting
2441
/* ***author  piessens,robert,appl. math. & progr. div. - k.u.leuven */
2298
 ***author  piessens,robert,appl. math. & progr. div. - k.u.leuven
2442
/*           de doncker,elise,appl. math. & progr. div. - k.u.leuven */
2299
           de doncker,elise,appl. math. & progr. div. - k.u.leuven
2443
/* ***purpose  this routine maintains the descending ordering in the */
2300
 ***purpose  this routine maintains the descending ordering in the
2444
/*            list of the local error estimated resulting from the */
2301
            list of the local error estimated resulting from the
2445
/*            interval subdivision process. at each call two error */
2302
            interval subdivision process. at each call two error
2446
/*            estimates are inserted using the sequential search */
2303
            estimates are inserted using the sequential search
2447
/*            method, top-down for the largest error estimate and */
2304
            method, top-down for the largest error estimate and
2448
/*            bottom-up for the smallest error estimate. */
2305
            bottom-up for the smallest error estimate.
2449
/* ***description */
2306
 ***description
2450
 
2307
 
2451
/*           ordering routine */
2308
           ordering routine
2452
/*           standard fortran subroutine */
2309
           standard fortran subroutine
2453
/*           double precision version */
2310
           double precision version
2454
 
2311
 
2455
/*           parameters (meaning at output) */
2312
           parameters (meaning at output)
2456
/*              limit  - int */
2313
              limit  - int
2457
/*                       maximum number of error estimates the list */
2314
                       maximum number of error estimates the list
2458
/*                       can contain */
2315
                       can contain
2459
 
2316
 
2460
/*              last   - int */
2317
              last   - int
2461
/*                       number of error estimates currently in the list */
2318
                       number of error estimates currently in the list
2462
 
2319
 
2463
/*              maxerr - int */
2320
              maxerr - int
2464
/*                       maxerr points to the nrmax-th largest error */
2321
                       maxerr points to the nrmax-th largest error
2465
/*                       estimate currently in the list */
2322
                       estimate currently in the list
2466
 
2323
 
2467
/*              ermax  - double precision */
2324
              ermax  - double precision
2468
/*                       nrmax-th largest error estimate */
2325
                       nrmax-th largest error estimate
2469
/*                       ermax = elist(maxerr) */
2326
                       ermax = elist(maxerr)
2470
 
2327
 
2471
/*              elist  - double precision */
2328
              elist  - double precision
2472
/*                       vector of dimension last containing */
2329
                       vector of dimension last containing
2473
/*                       the error estimates */
2330
                       the error estimates
2474
 
2331
 
2475
/*              iord   - int */
2332
              iord   - int
2476
/*                       vector of dimension last, the first k elements */
2333
                       vector of dimension last, the first k elements
2477
/*                       of which contain pointers to the error */
2334
                       of which contain pointers to the error
2478
/*                       estimates, such that */
2335
                       estimates, such that
2479
/*                       elist(iord(1)),...,  elist(iord(k)) */
2336
                       elist(iord(1)),...,  elist(iord(k))
2480
/*                       form a decreasing sequence, with */
2337
                       form a decreasing sequence, with
2481
/*                       k = last if last.le.(limit/2+2), and */
2338
                       k = last if last <= (limit/2+2), and
2482
/*                       k = limit+1-last otherwise */
2339
                       k = limit+1-last otherwise
2483
 
-
 
2484
/*              nrmax  - int */
-
 
2485
/*                       maxerr = iord(nrmax) */
-
 
2486
 
2340
 
2487
/* ***end prologue  dqpsrt */
2341
              nrmax  - int
-
 
2342
                       maxerr = iord(nrmax)
2488
 
2343
 
-
 
2344
***end prologue  dqpsrt
-
 
2345
*/
2489
 
2346
 
2490
/*           check whether the list contains more than */
-
 
2491
/*           two error estimates. */
-
 
2492
 
2347
 
2493
/* ***first executable statement  dqpsrt */
-
 
2494
    /* Parameter adjustments */
2348
    /* Parameter adjustments */
2495
    --iord;
2349
    --iord;
2496
    --elist;
2350
    --elist;
2497
 
2351
 
2498
    /* Function Body */
2352
    /* Function Body */
2499
    if (*last > 2) {
-
 
2500
	goto L10;
-
 
2501
    }
-
 
2502
    iord[1] = 1;
-
 
2503
    iord[2] = 2;
-
 
2504
    goto L90;
-
 
2505
 
2353
 
-
 
2354
/*           check whether the list contains more than
-
 
2355
	     two error estimates. */
-
 
2356
    if (*last <= 2) {
-
 
2357
	iord[1] = 1;
-
 
2358
	iord[2] = 2;
-
 
2359
	goto Last;
-
 
2360
    }
2506
/*           this part of the routine is only executed if, due to a */
2361
/*           this part of the routine is only executed if, due to a
2507
/*           difficult integrand, subdivision increased the error */
2362
	     difficult integrand, subdivision increased the error
2508
/*           estimate. in the normal case the insert procedure should */
2363
	     estimate. in the normal case the insert procedure should
2509
/*           start after the nrmax-th largest error estimate. */
2364
	     start after the nrmax-th largest error estimate. */
2510
 
2365
 
2511
L10:
-
 
2512
    errmax = elist[*maxerr];
2366
    errmax = elist[*maxerr];
2513
    if (*nrmax == 1) {
2367
    if (*nrmax > 1) {
2514
	goto L30;
-
 
2515
    }
-
 
2516
    ido = *nrmax - 1;
2368
	ido = *nrmax - 1;
2517
    i__1 = ido;
-
 
2518
    for (i__ = 1; i__ <= i__1; ++i__) {
2369
	for (i = 1; i <= ido; ++i) {
2519
	isucc = iord[*nrmax - 1];
2370
	    isucc = iord[*nrmax - 1];
-
 
2371
	    if (errmax <= elist[isucc])
2520
/* ***jump out of do-loop */
2372
		break; /* out of for-loop */
2521
	if (errmax <= elist[isucc]) {
2373
	    iord[*nrmax] = isucc;
-
 
2374
	    --(*nrmax);
2522
	    goto L30;
2375
	    /* L20: */
2523
	}
2376
	}
2524
	iord[*nrmax] = isucc;
-
 
2525
	--(*nrmax);
-
 
2526
/* L20: */
-
 
2527
    }
2377
    }
2528
 
2378
 
2529
/*           compute the number of elements in the list to be maintained */
2379
/*L30:       compute the number of elements in the list to be maintained
2530
/*           in descending order. this number depends on the number of */
2380
	     in descending order. this number depends on the number of
2531
/*           subdivisions still allowed. */
2381
	     subdivisions still allowed. */
2532
 
-
 
2533
L30:
-
 
2534
    jupbn = *last;
-
 
2535
    if (*last > *limit / 2 + 2) {
2382
    if (*last > *limit / 2 + 2)
2536
	jupbn = *limit + 3 - *last;
2383
	jupbn = *limit + 3 - *last;
2537
    }
2384
    else
-
 
2385
	jupbn = *last;
-
 
2386
 
2538
    errmin = elist[*last];
2387
    errmin = elist[*last];
2539
 
2388
 
2540
/*           insert errmax by traversing the list top-down, */
2389
/*           insert errmax by traversing the list top-down,
2541
/*           starting comparison from the element elist(iord(nrmax+1)). */
2390
	     starting comparison from the element elist(iord(nrmax+1)). */
2542
 
2391
 
2543
    jbnd = jupbn - 1;
2392
    jbnd = jupbn - 1;
2544
    ibeg = *nrmax + 1;
2393
    for (i = *nrmax + 1; i <= jbnd; ++i) {
2545
    if (ibeg > jbnd) {
2394
	isucc = iord[i];
2546
	goto L50;
2395
	if (errmax >= elist[isucc]) {/* ***jump out of do-loop */
2547
    }
-
 
-
 
2396
	    /* L60: insert errmin by traversing the list bottom-up. */
2548
    i__1 = jbnd;
2397
	    iord[i - 1] = *maxerr;
2549
    for (i__ = ibeg; i__ <= i__1; ++i__) {
2398
	    for (j = i, k = jbnd; j <= jbnd; j++, k--) {
2550
	isucc = iord[i__];
2399
		isucc = iord[k];
-
 
2400
		if (errmin < elist[isucc]) {
2551
/* ***jump out of do-loop */
2401
		    /* goto L80; ***jump out of do-loop */
-
 
2402
		    iord[k + 1] = *last;
-
 
2403
		    goto Last;
-
 
2404
		}
2552
	if (errmax >= elist[isucc]) {
2405
		iord[k + 1] = isucc;
-
 
2406
	    }
-
 
2407
	    iord[i] = *last;
2553
	    goto L60;
2408
	    goto Last;
2554
	}
2409
	}
2555
	iord[i__ - 1] = isucc;
2410
	iord[i - 1] = isucc;
2556
/* L40: */
-
 
2557
    }
2411
    }
2558
L50:
2412
 
2559
    iord[jbnd] = *maxerr;
2413
    iord[jbnd] = *maxerr;
2560
    iord[jupbn] = *last;
2414
    iord[jupbn] = *last;
2561
    goto L90;
-
 
2562
 
2415
 
2563
/*           insert errmin by traversing the list bottom-up. */
2416
Last:/* set maxerr and ermax. */
2564
 
2417
 
2565
L60:
-
 
2566
    iord[i__ - 1] = *maxerr;
-
 
2567
    k = jbnd;
-
 
2568
    i__1 = jbnd;
-
 
2569
    for (j = i__; j <= i__1; ++j) {
-
 
2570
	isucc = iord[k];
-
 
2571
/* ***jump out of do-loop */
-
 
2572
	if (errmin < elist[isucc]) {
-
 
2573
	    goto L80;
-
 
2574
	}
-
 
2575
	iord[k + 1] = isucc;
-
 
2576
	--k;
-
 
2577
/* L70: */
-
 
2578
    }
-
 
2579
    iord[i__] = *last;
-
 
2580
    goto L90;
-
 
2581
L80:
-
 
2582
    iord[k + 1] = *last;
-
 
2583
 
-
 
2584
/*           set maxerr and ermax. */
-
 
2585
 
-
 
2586
L90:
-
 
2587
    *maxerr = iord[*nrmax];
2418
    *maxerr = iord[*nrmax];
2588
    *ermax = elist[*maxerr];
2419
    *ermax = elist[*maxerr];
2589
    return;
2420
    return;
2590
} /* rdqpsrt_ */
2421
} /* rdqpsrt_ */