ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
integrate.cpp
Go to the documentation of this file.
1 /*
2  * R : A Computer Language for Statistical Data Analysis
3  * Copyright (C) 2001-2014 The R Core Team
4  *
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
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, a copy is available at
17  * https://www.R-project.org/Licenses/
18  *
19  * Most of this file is C translations of Fortran routines in
20  * QUADPACK: the latter is part of SLATEC 'and therefore in the public
21  * domain' (https://en.wikipedia.org/wiki/QUADPACK).
22  *
23  *
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #ifdef _MSC_VER
31  #ifdef small
32  #undef small
33  #endif
34 #endif
35 
36 #include <math.h>
37 #include <float.h>
38 //#include <Rmath.h> /* for fmax2, fmin2, imin2 */
39 //#include <R_ext/Applic.h> /* exporting the API , particularly */
40 /*--- typedef void integr_fn(double *x, int n, void *ex) ---
41  * vectorizing function f(x[1:n], ...) -> x[] {overwriting x[]}.
42  * Vectorization can be used to speed up the integrand
43  * instead of calling it n times.
44 */
45 
46 /* f2c-ed translations + modifications of QUADPACK functions */
47 
48 template<class Float, class integr_fn> static void rdqagie(integr_fn f, void *ex,
49  Float *, int *, Float * , Float *, int *,
50  Float *, Float *, int *,
51  int *, Float *, Float *, Float *, Float *,
52  int *, int *);
53 
54 template<class Float, class integr_fn> static void rdqk15i(integr_fn f, void *ex,
55  Float *, int *, Float * , Float *,
56  Float *, Float *, Float *, Float *);
57 
58 template<class Float, class integr_fn> static void rdqagse(integr_fn f, void *ex, Float *, Float *,
59  Float *, Float *, int *, Float *, Float *,
60  int *, int *, Float *, Float *, Float *,
61  Float *, int *, int *);
62 
63 template<class Float, class integr_fn> static void rdqk21(integr_fn f, void *ex,
64  Float *, Float *, Float *, Float *, Float *, Float *);
65 
66 template<class Float> static void rdqpsrt(int *, int *, int *, Float *, Float *, int *, int *);
67 
68 template<class Float> static void rdqelg(int *, Float *, Float *, Float *, Float *, int *);
69 
70 template<class Float, class integr_fn>
71 void Rdqagi(integr_fn f, void *ex, Float *bound, int *inf,
72  Float *epsabs, Float *epsrel,
73  Float *result, Float *abserr, int *neval, int *ier,
74  int *limit, int *lenw, int *last,
75  int *iwork, Float *work)
76 {
77  int l1, l2, l3;
78 
79 /*
80 ***begin prologue dqagi
81 ***date written 800101 (yymmdd)
82 ***revision date 830518 (yymmdd)
83 ***category no. h2a3a1,h2a4a1
84 ***keywords automatic integrator, infinite intervals,
85  general-purpose, transformation, extrapolation,
86  globally adaptive
87 ***author piessens,robert,appl. math. & progr. div. - k.u.leuven
88  de doncker,elise,appl. math. & progr. div. -k.u.leuven
89 ***purpose the routine calculates an approximation result to a given
90  integral i = integral of f over (bound,+infinity)
91  or i = integral of f over (-infinity,bound)
92  or i = integral of f over (-infinity,+infinity)
93  hopefully satisfying following claim for accuracy
94  abs(i-result) <= max(epsabs,epsrel*abs(i)).
95 ***description
96 
97  integration over infinite intervals
98  standard fortran subroutine
99 
100  parameters
101  on entry
102  f - double precision
103  function subprogram defining the integrand
104  function f(x). the actual name for f needs to be
105  declared e x t e r n a l in the driver program.
106 
107  bound - double precision
108  finite bound of integration range
109  (has no meaning if interval is doubly-infinite)
110 
111  inf - int
112  indicating the kind of integration range involved
113  inf = 1 corresponds to (bound,+infinity),
114  inf = -1 to (-infinity,bound),
115  inf = 2 to (-infinity,+infinity).
116 
117  epsabs - double precision
118  absolute accuracy requested
119  epsrel - double precision
120  relative accuracy requested
121  if epsabs <= 0
122  and epsrel < max(50*rel.mach.acc.,0.5d-28),
123  the routine will end with ier = 6.
124 
125 
126  on return
127  result - double precision
128  approximation to the integral
129 
130  abserr - double precision
131  estimate of the modulus of the absolute error,
132  which should equal or exceed abs(i-result)
133 
134  neval - int
135  number of integrand evaluations
136 
137  ier - int
138  ier = 0 normal and reliable termination of the
139  routine. it is assumed that the requested
140  accuracy has been achieved.
141  - ier > 0 abnormal termination of the routine. the
142  estimates for result and error are less
143  reliable. it is assumed that the requested
144  accuracy has not been achieved.
145  error messages
146  ier = 1 maximum number of subdivisions allowed
147  has been achieved. one can allow more
148  subdivisions by increasing the value of
149  limit (and taking the according dimension
150  adjustments into account). however, if
151  this yields no improvement it is advised
152  to analyze the integrand in order to
153  determine the integration difficulties. if
154  the position of a local difficulty can be
155  determined (e.g. singularity,
156  discontinuity within the interval) one
157  will probably gain from splitting up the
158  interval at this point and calling the
159  integrator on the subranges. if possible,
160  an appropriate special-purpose integrator
161  should be used, which is designed for
162  handling the type of difficulty involved.
163  = 2 the occurrence of roundoff error is
164  detected, which prevents the requested
165  tolerance from being achieved.
166  the error may be under-estimated.
167  = 3 extremely bad integrand behaviour occurs
168  at some points of the integration
169  interval.
170  = 4 the algorithm does not converge.
171  roundoff error is detected in the
172  extrapolation table.
173  it is assumed that the requested tolerance
174  cannot be achieved, and that the returned
175  result is the best which can be obtained.
176  = 5 the integral is probably divergent, or
177  slowly convergent. it must be noted that
178  divergence can occur with any other value
179  of ier.
180  = 6 the input is invalid, because
181  (epsabs <= 0 and
182  epsrel < max(50*rel.mach.acc.,0.5d-28))
183  or limit < 1 or leniw < limit*4.
184  result, abserr, neval, last are set to
185  zero. exept when limit or leniw is
186  invalid, iwork(1), work(limit*2+1) and
187  work(limit*3+1) are set to zero, work(1)
188  is set to a and work(limit+1) to b.
189 
190  dimensioning parameters
191  limit - int
192  dimensioning parameter for iwork
193  limit determines the maximum number of subintervals
194  in the partition of the given integration interval
195  (a,b), limit >= 1.
196  if limit < 1, the routine will end with ier = 6.
197 
198  lenw - int
199  dimensioning parameter for work
200  lenw must be at least limit*4.
201  if lenw < limit*4, the routine will end
202  with ier = 6.
203 
204  last - int
205  on return, last equals the number of subintervals
206  produced in the subdivision process, which
207  determines the number of significant elements
208  actually in the work arrays.
209 
210  work arrays
211  iwork - int
212  vector of dimension at least limit, the first
213  k elements of which contain pointers
214  to the error estimates over the subintervals,
215  such that work(limit*3+iwork(1)),... ,
216  work(limit*3+iwork(k)) form a decreasing
217  sequence, with k = last if last <= (limit/2+2), and
218  k = limit+1-last otherwise
219 
220  work - double precision
221  vector of dimension at least lenw
222  on return
223  work(1), ..., work(last) contain the left
224  end points of the subintervals in the
225  partition of (a,b),
226  work(limit+1), ..., work(limit+last) contain
227  the right end points,
228  work(limit*2+1), ...,work(limit*2+last) contain the
229  integral approximations over the subintervals,
230  work(limit*3+1), ..., work(limit*3)
231  contain the error estimates.
232 
233 ***routines called dqagie
234 ***end prologue dqagi */
235 
236  *ier = 6;
237  *neval = 0;
238  *last = 0;
239  *result = 0.;
240  *abserr = 0.;
241  if (*limit < 1 || *lenw < *limit << 2) return;
242 
243  l1 = *limit;
244  l2 = *limit + l1;
245  l3 = *limit + l2;
246 
247  rdqagie(f, ex, bound, inf, epsabs, epsrel, limit, result, abserr, neval, ier,
248  work, &work[l1], &work[l2], &work[l3], iwork, last);
249 
250  return;
251 } /* Rdqagi */
252 
253 template<class Float, class integr_fn> static
254 void rdqagie(integr_fn f, void *ex, Float *bound, int *inf, Float *
255  epsabs, Float *epsrel, int *limit, Float *result,
256  Float *abserr, int *neval, int *ier, Float *alist,
257  Float *blist, Float *rlist, Float *elist, int *
258  iord, int *last)
259 {
260  /* Local variables */
261  Float area, dres;
262  int ksgn;
263  Float boun;
264  int nres;
265  Float area1, area2, area12;
266  int k;
267  Float small, erro12;
268  small = 0.0;
269  int ierro;
270  Float a1, a2, b1, b2, defab1, defab2, oflow;
271  int ktmin, nrmax;
272  Float uflow;
273  bool noext;
274  int iroff1, iroff2, iroff3;
275  Float res3la[3], error1, error2;
276  int id;
277  Float rlist2[52];
278  int numrl2;
279  Float defabs, epmach, erlarg, abseps, correc, errbnd, resabs;
280  erlarg = 0.0;
281  correc = 0.0;
282  int jupbnd;
283  Float erlast, errmax;
284  int maxerr;
285  Float reseps;
286  bool extrap;
287  Float ertest, errsum;
288  ertest = 0.0;
289 
490 /* ***first executable statement dqagie */
491  /* Parameter adjustments */
492  --iord;
493  --elist;
494  --rlist;
495  --blist;
496  --alist;
497 
498  /* Function Body */
499  epmach = DBL_EPSILON;
500 
501 /* test on validity of parameters */
502 /* ----------------------------- */
503 
504  *ier = 0;
505  *neval = 0;
506  *last = 0;
507  *result = 0.;
508  *abserr = 0.;
509  alist[1] = 0.;
510  blist[1] = 1.;
511  rlist[1] = 0.;
512  elist[1] = 0.;
513  iord[1] = 0;
514  if (*epsabs <= 0. && (*epsrel < fmax2(epmach * 50., 5e-29))) *ier = 6;
515  if (*ier == 6) return;
516 
517 /* first approximation to the integral */
518 /* ----------------------------------- */
519 
520 /* determine the interval to be mapped onto (0,1).
521  if inf = 2 the integral is computed as i = i1+i2, where
522  i1 = integral of f over (-infinity,0),
523  i2 = integral of f over (0,+infinity). */
524 
525  boun = *bound;
526  if (*inf == 2) {
527  boun = 0.;
528  }
529 
530  /* Table of constant values */
531  static Float c_b6 = 0.;
532  static Float c_b7 = 1.;
533 
534  rdqk15i(f, ex, &boun, inf, &c_b6, &c_b7, result, abserr, &defabs, &resabs);
535 
536 /* test on accuracy */
537 
538  *last = 1;
539  rlist[1] = *result;
540  elist[1] = *abserr;
541  iord[1] = 1;
542  dres = fabs(*result);
543  errbnd = fmax2(*epsabs, *epsrel * dres);
544  if (*abserr <= epmach * 100. * defabs && *abserr > errbnd) *ier = 2;
545  if (*limit == 1) *ier = 1;
546  if (*ier != 0 || (*abserr <= errbnd && *abserr != resabs)
547  || *abserr == 0.) goto L130;
548 
549 /* initialization */
550 /* -------------- */
551 
552  uflow = DBL_MIN;
553  oflow = DBL_MAX;
554  rlist2[0] = *result;
555  errmax = *abserr;
556  maxerr = 1;
557  area = *result;
558  errsum = *abserr;
559  *abserr = oflow;
560  nrmax = 1;
561  nres = 0;
562  ktmin = 0;
563  numrl2 = 2;
564  extrap = FALSE;
565  noext = FALSE;
566  ierro = 0;
567  iroff1 = 0;
568  iroff2 = 0;
569  iroff3 = 0;
570  ksgn = -1;
571  if (dres >= (1. - epmach * 50.) * defabs) {
572  ksgn = 1;
573  }
574 
575 /* main do-loop */
576 /* ------------ */
577 
578  for (*last = 2; *last <= *limit; ++(*last)) {
579 
580 /* bisect the subinterval with nrmax-th largest error estimate. */
581 
582  a1 = alist[maxerr];
583  b1 = (alist[maxerr] + blist[maxerr]) * .5;
584  a2 = b1;
585  b2 = blist[maxerr];
586  erlast = errmax;
587  rdqk15i(f, ex, &boun, inf, &a1, &b1, &area1, &error1, &resabs, &defab1);
588  rdqk15i(f, ex, &boun, inf, &a2, &b2, &area2, &error2, &resabs, &defab2);
589 
590 /* improve previous approximations to integral
591  and error and test for accuracy. */
592 
593  area12 = area1 + area2;
594  erro12 = error1 + error2;
595  errsum = errsum + erro12 - errmax;
596  area = area + area12 - rlist[maxerr];
597  if (!(defab1 == error1 || defab2 == error2)) {
598  if (fabs(rlist[maxerr] - area12) <= fabs(area12) * 1e-5 &&
599  erro12 >= errmax * .99) {
600  if (extrap)
601  ++iroff2;
602  else /* if (! extrap) */
603  ++iroff1;
604  }
605  if (*last > 10 && erro12 > errmax)
606  ++iroff3;
607  }
608 
609  rlist[maxerr] = area1;
610  rlist[*last] = area2;
611  errbnd = fmax2(*epsabs, *epsrel * fabs(area));
612 
613 /* test for roundoff error and eventually set error flag. */
614 
615  if (iroff1 + iroff2 >= 10 || iroff3 >= 20)
616  *ier = 2;
617  if (iroff2 >= 5)
618  ierro = 3;
619 
620 /* set error flag in the case that the number of
621  subintervals equals limit. */
622 
623  if (*last == *limit)
624  *ier = 1;
625 
626 /* set error flag in the case of bad integrand behaviour
627  at some points of the integration range. */
628 
629  if (fmax2(fabs(a1), fabs(b2)) <=
630  (epmach * 100. + 1.) * (fabs(a2) + uflow * 1e3))
631  {
632  *ier = 4;
633  }
634 
635 /* append the newly-created intervals to the list. */
636 
637  if (error2 <= error1) {
638  alist[*last] = a2;
639  blist[maxerr] = b1;
640  blist[*last] = b2;
641  elist[maxerr] = error1;
642  elist[*last] = error2;
643  }
644  else {
645  alist[maxerr] = a2;
646  alist[*last] = a1;
647  blist[*last] = b1;
648  rlist[maxerr] = area2;
649  rlist[*last] = area1;
650  elist[maxerr] = error2;
651  elist[*last] = error1;
652  }
653 
654 /* call subroutine dqpsrt to maintain the descending ordering
655  in the list of error estimates and select the subinterval
656  with nrmax-th largest error estimate (to be bisected next). */
657 
658  rdqpsrt(limit, last, &maxerr, &errmax, &elist[1], &iord[1], &nrmax);
659  if (errsum <= errbnd) {
660  goto L115;
661  }
662  if (*ier != 0) break;
663  if (*last == 2) { /* L80: */
664  small = .375;
665  erlarg = errsum;
666  ertest = errbnd;
667  rlist2[1] = area; continue;
668  }
669  if (noext) continue;
670 
671  erlarg -= erlast;
672  if (fabs(b1 - a1) > small) {
673  erlarg += erro12;
674  }
675  if (!extrap) {
676 
677 /* test whether the interval to be bisected next is the
678  smallest interval. */
679 
680  if (fabs(blist[maxerr] - alist[maxerr]) > small) {
681  continue;
682  }
683  extrap = TRUE;
684  nrmax = 2;
685  }
686 
687  if (ierro != 3 && erlarg > ertest) {
688 
689 /* the smallest interval has the largest error.
690  before bisecting decrease the sum of the errors over the
691  larger intervals (erlarg) and perform extrapolation. */
692 
693  id = nrmax;
694  jupbnd = *last;
695  if (*last > *limit / 2 + 2) {
696  jupbnd = *limit + 3 - *last;
697  }
698  for (k = id; k <= jupbnd; ++k) {
699  maxerr = iord[nrmax];
700  errmax = elist[maxerr];
701  if (fabs(blist[maxerr] - alist[maxerr]) > small) {
702  goto L90;
703  }
704  ++nrmax;
705  /* L50: */
706  }
707  }
708 /* perform extrapolation. L60: */
709  ++numrl2;
710  rlist2[numrl2 - 1] = area;
711  rdqelg(&numrl2, rlist2, &reseps, &abseps, res3la, &nres);
712  ++ktmin;
713  if (ktmin > 5 && *abserr < errsum * .001) {
714  *ier = 5;
715  }
716  if (abseps >= *abserr) {
717  goto L70;
718  }
719  ktmin = 0;
720  *abserr = abseps;
721  *result = reseps;
722  correc = erlarg;
723  ertest = fmax2(*epsabs, *epsrel * fabs(reseps));
724  if (*abserr <= ertest) {
725  break;
726  }
727 
728 /* prepare bisection of the smallest interval. */
729 
730 L70:
731  if (numrl2 == 1) {
732  noext = TRUE;
733  }
734  if (*ier == 5) {
735  break;
736  }
737  maxerr = iord[1];
738  errmax = elist[maxerr];
739  nrmax = 1;
740  extrap = FALSE;
741  small *= .5;
742  erlarg = errsum;
743  L90:
744  ;
745  }
746 
747 /* L100: set final result and error estimate. */
748 /* ------------------------------------ */
749 
750  if (*abserr == oflow) {
751  goto L115;
752  }
753  if (*ier + ierro == 0) {
754  goto L110;
755  }
756  if (ierro == 3) {
757  *abserr += correc;
758  }
759  if (*ier == 0) {
760  *ier = 3;
761  }
762  if (*result == 0. || area == 0.) {
763  if (*abserr > errsum)
764  goto L115;
765 
766  if (area == 0.)
767  goto L130;
768  }
769  else { /* L105: */
770  if (*abserr / fabs(*result) > errsum / fabs(area)) {
771  goto L115;
772  }
773  }
774 
775 /* test on divergence */
776 L110:
777  if (ksgn == -1 && fmax2(fabs(*result), fabs(area)) <= defabs * .01) {
778  goto L130;
779  }
780  if (.01 > *result / area || *result / area > 100. || errsum > fabs(area)) {
781  *ier = 6;
782  }
783  goto L130;
784 
785 /* compute global integral sum. */
786 
787 L115:
788  *result = 0.;
789  for (k = 1; k <= *last; ++k)
790  *result += rlist[k];
791 
792  *abserr = errsum;
793 L130:
794  *neval = *last * 30 - 15;
795  if (*inf == 2) {
796  *neval <<= 1;
797  }
798  if (*ier > 2) {
799  --(*ier);
800  }
801  return;
802 } /* rdqagie_ */
803 
804 template<class Float, class integr_fn>
805 void Rdqags(integr_fn f, void *ex, Float *a, Float *b,
806  Float *epsabs, Float *epsrel,
807  Float *result, Float *abserr, int *neval, int *ier,
808  int *limit, int *lenw, int *last, int *iwork, Float *work)
809 {
810  int l1, l2, l3;
811 
812 /*
813 ***begin prologue dqags
814 ***date written 800101 (yymmdd)
815 ***revision date 830518 (yymmdd)
816 ***category no. h2a1a1
817 ***keywords automatic integrator, general-purpose,
818  (end-point) singularities, extrapolation,
819  globally adaptive
820 ***author piessens,robert,appl. math. & progr. div. - k.u.leuven
821  de doncker,elise,appl. math. & prog. div. - k.u.leuven
822 ***purpose the routine calculates an approximation result to a given
823  definite integral i = integral of f over (a,b),
824  hopefully satisfying following claim for accuracy
825  abs(i-result) <= max(epsabs,epsrel*abs(i)).
826 ***description
827 
828  computation of a definite integral
829  standard fortran subroutine
830  double precision version
831 
832 
833  parameters
834  on entry
835  f - double precision
836  function subprogram defining the integrand
837  function f(x). the actual name for f needs to be
838  declared e x t e r n a l in the driver program.
839 
840  a - double precision
841  lower limit of integration
842 
843  b - double precision
844  upper limit of integration
845 
846  epsabs - double precision
847  absolute accuracy requested
848  epsrel - double precision
849  relative accuracy requested
850  if epsabs <= 0
851  and epsrel < max(50*rel.mach.acc.,0.5d-28),
852  the routine will end with ier = 6.
853 
854  on return
855  result - double precision
856  approximation to the integral
857 
858  abserr - double precision
859  estimate of the modulus of the absolute error,
860  which should equal or exceed abs(i-result)
861 
862  neval - int
863  number of integrand evaluations
864 
865  ier - int
866  ier = 0 normal and reliable termination of the
867  routine. it is assumed that the requested
868  accuracy has been achieved.
869  ier > 0 abnormal termination of the routine
870  the estimates for integral and error are
871  less reliable. it is assumed that the
872  requested accuracy has not been achieved.
873  error messages
874  ier = 1 maximum number of subdivisions allowed
875  has been achieved. one can allow more sub-
876  divisions by increasing the value of limit
877  (and taking the according dimension
878  adjustments into account. however, if
879  this yields no improvement it is advised
880  to analyze the integrand in order to
881  determine the integration difficulties. if
882  the position of a local difficulty can be
883  determined (e.g. singularity,
884  discontinuity within the interval) one
885  will probably gain from splitting up the
886  interval at this point and calling the
887  integrator on the subranges. if possible,
888  an appropriate special-purpose integrator
889  should be used, which is designed for
890  handling the type of difficulty involved.
891  = 2 the occurrence of roundoff error is detec-
892  ted, which prevents the requested
893  tolerance from being achieved.
894  the error may be under-estimated.
895  = 3 extremely bad integrand behaviour
896  occurs at some points of the integration
897  interval.
898  = 4 the algorithm does not converge.
899  roundoff error is detected in the
900  extrapolation table. it is presumed that
901  the requested tolerance cannot be
902  achieved, and that the returned result is
903  the best which can be obtained.
904  = 5 the integral is probably divergent, or
905  slowly convergent. it must be noted that
906  divergence can occur with any other value
907  of ier.
908  = 6 the input is invalid, because
909  (epsabs <= 0 and
910  epsrel < max(50*rel.mach.acc.,0.5d-28)
911  or limit < 1 or lenw < limit*4.
912  result, abserr, neval, last are set to
913  zero.except when limit or lenw is invalid,
914  iwork(1), work(limit*2+1) and
915  work(limit*3+1) are set to zero, work(1)
916  is set to a and work(limit+1) to b.
917 
918  dimensioning parameters
919  limit - int
920  dimensioning parameter for iwork
921  limit determines the maximum number of subintervals
922  in the partition of the given integration interval
923  (a,b), limit >= 1.
924  if limit < 1, the routine will end with ier = 6.
925 
926  lenw - int
927  dimensioning parameter for work
928  lenw must be at least limit*4.
929  if lenw < limit*4, the routine will end
930  with ier = 6.
931 
932  last - int
933  on return, last equals the number of subintervals
934  produced in the subdivision process, detemines the
935  number of significant elements actually in the work
936  arrays.
937 
938  work arrays
939  iwork - int
940  vector of dimension at least limit, the first k
941  elements of which contain pointers
942  to the error estimates over the subintervals
943  such that work(limit*3+iwork(1)),... ,
944  work(limit*3+iwork(k)) form a decreasing
945  sequence, with k = last if last <= (limit/2+2),
946  and k = limit+1-last otherwise
947 
948  work - double precision
949  vector of dimension at least lenw
950  on return
951  work(1), ..., work(last) contain the left
952  end-points of the subintervals in the
953  partition of (a,b),
954  work(limit+1), ..., work(limit+last) contain
955  the right end-points,
956  work(limit*2+1), ..., work(limit*2+last) contain
957  the integral approximations over the subintervals,
958  work(limit*3+1), ..., work(limit*3+last)
959  contain the error estimates.
960 
961 ***routines called dqagse
962 ***end prologue dqags */
963 
964 /* check validity of limit and lenw. */
965 
966  *ier = 6;
967  *neval = 0;
968  *last = 0;
969  *result = 0.;
970  *abserr = 0.;
971  if (*limit < 1 || *lenw < *limit *4) return;
972 
973 /* prepare call for dqagse. */
974 
975  l1 = *limit;
976  l2 = *limit + l1;
977  l3 = *limit + l2;
978 
979  rdqagse(f, ex, a, b, epsabs, epsrel, limit, result, abserr, neval, ier,
980  work, &work[l1], &work[l2], &work[l3], iwork, last);
981 
982  return;
983 } /* rdqags_ */
984 
985 template<class Float, class integr_fn> static
986 void rdqagse(integr_fn f, void *ex, Float *a, Float *b, Float *
987  epsabs, Float *epsrel, int *limit, Float *result,
988  Float *abserr, int *neval, int *ier, Float *alist,
989  Float *blist, Float *rlist, Float *elist, int *
990  iord, int *last)
991 {
992  /* Local variables */
993  bool noext, extrap;
994  int k,ksgn, nres;
995  int ierro;
996  int ktmin, nrmax;
997  int iroff1, iroff2, iroff3;
998  int id;
999  int numrl2;
1000  int jupbnd;
1001  int maxerr;
1002  Float res3la[3];
1003  Float rlist2[52];
1004  Float abseps, area, area1, area2, area12, dres, epmach;
1005  Float a1, a2, b1, b2, defabs, defab1, defab2, oflow, uflow, resabs, reseps;
1006  Float error1, error2, erro12, errbnd, erlast, errmax, errsum;
1007 
1008  Float correc = 0.0, erlarg = 0.0, ertest = 0.0, small = 0.0;
1009 /*
1010 ***begin prologue dqagse
1011 ***date written 800101 (yymmdd)
1012 ***revision date 830518 (yymmdd)
1013 ***category no. h2a1a1
1014 ***keywords automatic integrator, general-purpose,
1015  (end point) singularities, extrapolation,
1016  globally adaptive
1017 ***author piessens,robert,appl. math. & progr. div. - k.u.leuven
1018  de doncker,elise,appl. math. & progr. div. - k.u.leuven
1019 ***purpose the routine calculates an approximation result to a given
1020  definite integral i = integral of f over (a,b),
1021  hopefully satisfying following claim for accuracy
1022  abs(i-result) <= max(epsabs,epsrel*abs(i)).
1023 ***description
1024 
1025  computation of a definite integral
1026  standard fortran subroutine
1027  double precision version
1028 
1029  parameters
1030  on entry
1031  f - double precision
1032  function subprogram defining the integrand
1033  function f(x). the actual name for f needs to be
1034  declared e x t e r n a l in the driver program.
1035 
1036  a - double precision
1037  lower limit of integration
1038 
1039  b - double precision
1040  upper limit of integration
1041 
1042  epsabs - double precision
1043  absolute accuracy requested
1044  epsrel - double precision
1045  relative accuracy requested
1046  if epsabs <= 0
1047  and epsrel < max(50*rel.mach.acc.,0.5d-28),
1048  the routine will end with ier = 6.
1049 
1050  limit - int
1051  gives an upperbound on the number of subintervals
1052  in the partition of (a,b)
1053 
1054  on return
1055  result - double precision
1056  approximation to the integral
1057 
1058  abserr - double precision
1059  estimate of the modulus of the absolute error,
1060  which should equal or exceed abs(i-result)
1061 
1062  neval - int
1063  number of integrand evaluations
1064 
1065  ier - int
1066  ier = 0 normal and reliable termination of the
1067  routine. it is assumed that the requested
1068  accuracy has been achieved.
1069  ier > 0 abnormal termination of the routine
1070  the estimates for integral and error are
1071  less reliable. it is assumed that the
1072  requested accuracy has not been achieved.
1073  error messages
1074  = 1 maximum number of subdivisions allowed
1075  has been achieved. one can allow more sub-
1076  divisions by increasing the value of limit
1077  (and taking the according dimension
1078  adjustments into account). however, if
1079  this yields no improvement it is advised
1080  to analyze the integrand in order to
1081  determine the integration difficulties. if
1082  the position of a local difficulty can be
1083  determined (e.g. singularity,
1084  discontinuity within the interval) one
1085  will probably gain from splitting up the
1086  interval at this point and calling the
1087  integrator on the subranges. if possible,
1088  an appropriate special-purpose integrator
1089  should be used, which is designed for
1090  handling the type of difficulty involved.
1091  = 2 the occurrence of roundoff error is detec-
1092  ted, which prevents the requested
1093  tolerance from being achieved.
1094  the error may be under-estimated.
1095  = 3 extremely bad integrand behaviour
1096  occurs at some points of the integration
1097  interval.
1098  = 4 the algorithm does not converge.
1099  roundoff error is detected in the
1100  extrapolation table.
1101  it is presumed that the requested
1102  tolerance cannot be achieved, and that the
1103  returned result is the best which can be
1104  obtained.
1105  = 5 the integral is probably divergent, or
1106  slowly convergent. it must be noted that
1107  divergence can occur with any other value
1108  of ier.
1109  = 6 the input is invalid, because
1110  epsabs <= 0 and
1111  epsrel < max(50*rel.mach.acc.,0.5d-28).
1112  result, abserr, neval, last, rlist(1),
1113  iord(1) and elist(1) are set to zero.
1114  alist(1) and blist(1) are set to a and b
1115  respectively.
1116 
1117  alist - double precision
1118  vector of dimension at least limit, the first
1119  last elements of which are the left end points
1120  of the subintervals in the partition of the
1121  given integration range (a,b)
1122 
1123  blist - double precision
1124  vector of dimension at least limit, the first
1125  last elements of which are the right end points
1126  of the subintervals in the partition of the given
1127  integration range (a,b)
1128 
1129  rlist - double precision
1130  vector of dimension at least limit, the first
1131  last elements of which are the integral
1132  approximations on the subintervals
1133 
1134  elist - double precision
1135  vector of dimension at least limit, the first
1136  last elements of which are the moduli of the
1137  absolute error estimates on the subintervals
1138 
1139  iord - int
1140  vector of dimension at least limit, the first k
1141  elements of which are pointers to the
1142  error estimates over the subintervals,
1143  such that elist(iord(1)), ..., elist(iord(k))
1144  form a decreasing sequence, with k = last
1145  if last <= (limit/2+2), and k = limit+1-last
1146  otherwise
1147 
1148  last - int
1149  number of subintervals actually produced in the
1150  subdivision process
1151 
1152 ***references (none)
1153 ***routines called dqelg,dqk21,dqpsrt
1154 ***end prologue dqagse
1155 
1156 
1157 
1158  the dimension of rlist2 is determined by the value of
1159  limexp in subroutine dqelg (rlist2 should be of dimension
1160  (limexp+2) at least).
1161 
1162  list of major variables
1163  -----------------------
1164 
1165  alist - list of left end points of all subintervals
1166  considered up to now
1167  blist - list of right end points of all subintervals
1168  considered up to now
1169  rlist(i) - approximation to the integral over
1170  (alist(i),blist(i))
1171  rlist2 - array of dimension at least limexp+2 containing
1172  the part of the epsilon table which is still
1173  needed for further computations
1174  elist(i) - error estimate applying to rlist(i)
1175  maxerr - pointer to the interval with largest error
1176  estimate
1177  errmax - elist(maxerr)
1178  erlast - error on the interval currently subdivided
1179  (before that subdivision has taken place)
1180  area - sum of the integrals over the subintervals
1181  errsum - sum of the errors over the subintervals
1182  errbnd - requested accuracy max(epsabs,epsrel*
1183  abs(result))
1184  *****1 - variable for the left interval
1185  *****2 - variable for the right interval
1186  last - index for subdivision
1187  nres - number of calls to the extrapolation routine
1188  numrl2 - number of elements currently in rlist2. if an
1189  appropriate approximation to the compounded
1190  integral has been obtained it is put in
1191  rlist2(numrl2) after numrl2 has been increased
1192  by one.
1193  small - length of the smallest interval considered up
1194  to now, multiplied by 1.5
1195  erlarg - sum of the errors over the intervals larger
1196  than the smallest interval considered up to now
1197  extrap - logical variable denoting that the routine is
1198  attempting to perform extrapolation i.e. before
1199  subdividing the smallest interval we try to
1200  decrease the value of erlarg.
1201  noext - logical variable denoting that extrapolation
1202  is no longer allowed (true value)
1203 
1204  machine dependent constants
1205  ---------------------------
1206 
1207  epmach is the largest relative spacing.
1208  uflow is the smallest positive magnitude.
1209  oflow is the largest positive magnitude. */
1210 
1211 /* ***first executable statement dqagse */
1212  /* Parameter adjustments */
1213  --iord;
1214  --elist;
1215  --rlist;
1216  --blist;
1217  --alist;
1218 
1219  /* Function Body */
1220  epmach = DBL_EPSILON;
1221 
1222 /* test on validity of parameters */
1223 /* ------------------------------ */
1224  *ier = 0;
1225  *neval = 0;
1226  *last = 0;
1227  *result = 0.;
1228  *abserr = 0.;
1229  alist[1] = *a;
1230  blist[1] = *b;
1231  rlist[1] = 0.;
1232  elist[1] = 0.;
1233  if (*epsabs <= 0. && *epsrel < fmax2(epmach * 50., 5e-29)) {
1234  *ier = 6;
1235  return;
1236  }
1237 
1238 /* first approximation to the integral */
1239 /* ----------------------------------- */
1240 
1241  uflow = DBL_MIN;
1242  oflow = DBL_MAX;
1243  ierro = 0;
1244  rdqk21(f, ex, a, b, result, abserr, &defabs, &resabs);
1245 
1246 /* test on accuracy. */
1247 
1248  dres = fabs(*result);
1249  errbnd = fmax2(*epsabs, *epsrel * dres);
1250  *last = 1;
1251  rlist[1] = *result;
1252  elist[1] = *abserr;
1253  iord[1] = 1;
1254  if (*abserr <= epmach * 100. * defabs && *abserr > errbnd)
1255  *ier = 2;
1256  if (*limit == 1)
1257  *ier = 1;
1258  if (*ier != 0 || (*abserr <= errbnd && *abserr != resabs)
1259  || *abserr == 0.) goto L140;
1260 
1261 /* initialization */
1262 /* -------------- */
1263 
1264  rlist2[0] = *result;
1265  errmax = *abserr;
1266  maxerr = 1;
1267  area = *result;
1268  errsum = *abserr;
1269  *abserr = oflow;
1270  nrmax = 1;
1271  nres = 0;
1272  numrl2 = 2;
1273  ktmin = 0;
1274  extrap = FALSE;
1275  noext = FALSE;
1276  iroff1 = 0;
1277  iroff2 = 0;
1278  iroff3 = 0;
1279  ksgn = -1;
1280  if (dres >= (1. - epmach * 50.) * defabs) {
1281  ksgn = 1;
1282  }
1283 
1284 /* main do-loop */
1285 /* ------------ */
1286 
1287  for (*last = 2; *last <= *limit; ++(*last)) {
1288 
1289 /* bisect the subinterval with the nrmax-th largest error estimate. */
1290 
1291  a1 = alist[maxerr];
1292  b1 = (alist[maxerr] + blist[maxerr]) * .5;
1293  a2 = b1;
1294  b2 = blist[maxerr];
1295  erlast = errmax;
1296  rdqk21(f, ex, &a1, &b1, &area1, &error1, &resabs, &defab1);
1297  rdqk21(f, ex, &a2, &b2, &area2, &error2, &resabs, &defab2);
1298 
1299 /* improve previous approximations to integral
1300  and error and test for accuracy. */
1301 
1302  area12 = area1 + area2;
1303  erro12 = error1 + error2;
1304  errsum = errsum + erro12 - errmax;
1305  area = area + area12 - rlist[maxerr];
1306  if (!(defab1 == error1 || defab2 == error2)) {
1307 
1308  if (fabs(rlist[maxerr] - area12) <= fabs(area12) * 1e-5 &&
1309  erro12 >= errmax * .99) {
1310  if (extrap)
1311  ++iroff2;
1312  else /* if(! extrap) */
1313  ++iroff1;
1314  }
1315  if (*last > 10 && erro12 > errmax)
1316  ++iroff3;
1317  }
1318  rlist[maxerr] = area1;
1319  rlist[*last] = area2;
1320  errbnd = fmax2(*epsabs, *epsrel * fabs(area));
1321 
1322 /* test for roundoff error and eventually set error flag. */
1323 
1324  if (iroff1 + iroff2 >= 10 || iroff3 >= 20)
1325  *ier = 2;
1326  if (iroff2 >= 5)
1327  ierro = 3;
1328 
1329 /* set error flag in the case that the number of subintervals equals limit. */
1330  if (*last == *limit)
1331  *ier = 1;
1332 
1333 /* set error flag in the case of bad integrand behaviour
1334  at a point of the integration range. */
1335 
1336  if (fmax2(fabs(a1), fabs(b2)) <=
1337  (epmach * 100. + 1.) * (fabs(a2) + uflow * 1e3)) {
1338  *ier = 4;
1339  }
1340 
1341 /* append the newly-created intervals to the list. */
1342 
1343  if (error2 > error1) {
1344  alist[maxerr] = a2;
1345  alist[*last] = a1;
1346  blist[*last] = b1;
1347  rlist[maxerr] = area2;
1348  rlist[*last] = area1;
1349  elist[maxerr] = error2;
1350  elist[*last] = error1;
1351  } else {
1352  alist[*last] = a2;
1353  blist[maxerr] = b1;
1354  blist[*last] = b2;
1355  elist[maxerr] = error1;
1356  elist[*last] = error2;
1357  }
1358 
1359 /* call subroutine dqpsrt to maintain the descending ordering
1360  in the list of error estimates and select the subinterval
1361  with nrmax-th largest error estimate (to be bisected next). */
1362 
1363 /*L30:*/
1364  rdqpsrt(limit, last, &maxerr, &errmax, &elist[1], &iord[1], &nrmax);
1365 
1366  if (errsum <= errbnd) goto L115;/* ***jump out of do-loop */
1367  if (*ier != 0) break;
1368  if (*last == 2) { /* L80: */
1369  small = fabs(*b - *a) * .375;
1370  erlarg = errsum;
1371  ertest = errbnd;
1372  rlist2[1] = area; continue;
1373  }
1374  if (noext) continue;
1375 
1376  erlarg -= erlast;
1377  if (fabs(b1 - a1) > small) {
1378  erlarg += erro12;
1379  }
1380  if (!extrap) {
1381 
1382 /* test whether the interval to be bisected next is the
1383  smallest interval. */
1384 
1385  if (fabs(blist[maxerr] - alist[maxerr]) > small) {
1386  continue;
1387  }
1388  extrap = TRUE;
1389  nrmax = 2;
1390  }
1391 
1392  if (ierro != 3 && erlarg > ertest) {
1393 
1394 /* the smallest interval has the largest error.
1395  before bisecting decrease the sum of the errors over the
1396  larger intervals (erlarg) and perform extrapolation. */
1397 
1398  id = nrmax;
1399  jupbnd = *last;
1400  if (*last > *limit / 2 + 2) {
1401  jupbnd = *limit + 3 - *last;
1402  }
1403  for (k = id; k <= jupbnd; ++k) {
1404  maxerr = iord[nrmax];
1405  errmax = elist[maxerr];
1406  if (fabs(blist[maxerr] - alist[maxerr]) > small) {
1407  goto L90;
1408  }
1409  ++nrmax;
1410  /* L50: */
1411  }
1412  }
1413 /* perform extrapolation. L60: */
1414 
1415  ++numrl2;
1416  rlist2[numrl2 - 1] = area;
1417  rdqelg(&numrl2, rlist2, &reseps, &abseps, res3la, &nres);
1418  ++ktmin;
1419  if (ktmin > 5 && *abserr < errsum * .001) {
1420  *ier = 5;
1421  }
1422  if (abseps < *abserr) {
1423  ktmin = 0;
1424  *abserr = abseps;
1425  *result = reseps;
1426  correc = erlarg;
1427  ertest = fmax2(*epsabs, *epsrel * fabs(reseps));
1428  if (*abserr <= ertest) {
1429  break;
1430  }
1431  }
1432 
1433 /* prepare bisection of the smallest interval. L70: */
1434 
1435  if (numrl2 == 1) {
1436  noext = TRUE;
1437  }
1438  if (*ier == 5) {
1439  break;
1440  }
1441  maxerr = iord[1];
1442  errmax = elist[maxerr];
1443  nrmax = 1;
1444  extrap = FALSE;
1445  small *= .5;
1446  erlarg = errsum;
1447  L90:
1448  ;
1449  }
1450 
1451 
1452 /* L100: set final result and error estimate. */
1453 /* ------------------------------------ */
1454 
1455  if (*abserr == oflow) goto L115;
1456  if (*ier + ierro == 0) goto L110;
1457  if (ierro == 3)
1458  *abserr += correc;
1459  if (*ier == 0)
1460  *ier = 3;
1461  if (*result == 0. || area == 0.) {
1462  if (*abserr > errsum) goto L115;
1463  if (area == 0.) goto L130;
1464  }
1465  else { /* L105:*/
1466  if (*abserr / fabs(*result) > errsum / fabs(area))
1467  goto L115;
1468  }
1469 
1470 L110:/* test on divergence. */
1471  if (ksgn == -1 && fmax2(fabs(*result), fabs(area)) <= defabs * .01) {
1472  goto L130;
1473  }
1474  if (.01 > *result / area || *result / area > 100. || errsum > fabs(area)) {
1475  *ier = 5;
1476  }
1477  goto L130;
1478 
1479 L115:/* compute global integral sum. */
1480  *result = 0.;
1481  for (k = 1; k <= *last; ++k)
1482  *result += rlist[k];
1483  *abserr = errsum;
1484 L130:
1485  if (*ier > 2)
1486 L140:
1487  *neval = *last * 42 - 21;
1488  return;
1489 } /* rdqagse_ */
1490 
1491 
1492 template<class Float, class integr_fn> static void rdqk15i(integr_fn f, void *ex,
1493  Float *boun, int *inf, Float *a, Float *b,
1494  Float *result,
1495  Float *abserr, Float *resabs, Float *resasc)
1496 {
1497  /* Initialized data */
1498 
1499  static double wg[8] = {
1500  0., .129484966168869693270611432679082,
1501  0., .27970539148927666790146777142378,
1502  0., .381830050505118944950369775488975,
1503  0., .417959183673469387755102040816327 };
1504  static double xgk[8] = {
1505  .991455371120812639206854697526329,
1506  .949107912342758524526189684047851,
1507  .864864423359769072789712788640926,
1508  .741531185599394439863864773280788,
1509  .58608723546769113029414483825873,
1510  .405845151377397166906606412076961,
1511  .207784955007898467600689403773245, 0. };
1512  static double wgk[8] = {
1513  .02293532201052922496373200805897,
1514  .063092092629978553290700663189204,
1515  .104790010322250183839876322541518,
1516  .140653259715525918745189590510238,
1517  .16900472663926790282658342659855,
1518  .190350578064785409913256402421014,
1519  .204432940075298892414161999234649,
1520  .209482141084727828012999174891714 };
1521 
1522  /* Local variables */
1523  Float absc, dinf, resg, resk, fsum, absc1, absc2, fval1, fval2;
1524  int j;
1525  Float hlgth, centr, reskh, uflow;
1526  Float tabsc1, tabsc2, fc, epmach;
1527  Float fv1[7], fv2[7], vec[15], vec2[15];
1528 
1529 /*
1530 ***begin prologue dqk15i
1531 ***date written 800101 (yymmdd)
1532 ***revision date 830518 (yymmdd)
1533 ***category no. h2a3a2,h2a4a2
1534 ***keywords 15-point transformed gauss-kronrod rules
1535 ***author piessens,robert,appl. math. & progr. div. - k.u.leuven
1536  de doncker,elise,appl. math. & progr. div. - k.u.leuven
1537 ***purpose the original (infinite integration range is mapped
1538  onto the interval (0,1) and (a,b) is a part of (0,1).
1539  it is the purpose to compute
1540  i = integral of transformed integrand over (a,b),
1541  j = integral of abs(transformed integrand) over (a,b).
1542 ***description
1543 
1544  integration rule
1545  standard fortran subroutine
1546  double precision version
1547 
1548  parameters
1549  on entry
1550  f - double precision
1551  fuction subprogram defining the integrand
1552  function f(x). the actual name for f needs to be
1553  declared e x t e r n a l in the calling program.
1554 
1555  boun - double precision
1556  finite bound of original integration
1557  range (set to zero if inf = +2)
1558 
1559  inf - int
1560  if inf = -1, the original interval is
1561  (-infinity,bound),
1562  if inf = +1, the original interval is
1563  (bound,+infinity),
1564  if inf = +2, the original interval is
1565  (-infinity,+infinity) and
1566  the integral is computed as the sum of two
1567  integrals, one over (-infinity,0) and one over
1568  (0,+infinity).
1569 
1570  a - double precision
1571  lower limit for integration over subrange
1572  of (0,1)
1573 
1574  b - double precision
1575  upper limit for integration over subrange
1576  of (0,1)
1577 
1578  on return
1579  result - double precision
1580  approximation to the integral i
1581  result is computed by applying the 15-point
1582  kronrod rule(resk) obtained by optimal addition
1583  of abscissae to the 7-point gauss rule(resg).
1584 
1585  abserr - double precision
1586  estimate of the modulus of the absolute error,
1587  which should equal or exceed abs(i-result)
1588 
1589  resabs - double precision
1590  approximation to the integral j
1591 
1592  resasc - double precision
1593  approximation to the integral of
1594  abs((transformed integrand)-i/(b-a)) over (a,b)
1595 
1596 ***references (none)
1597 ***end prologue dqk15i
1598 
1599 
1600  the abscissae and weights are supplied for the interval
1601  (-1,1). because of symmetry only the positive abscissae and
1602  their corresponding weights are given.
1603 
1604  xgk - abscissae of the 15-point kronrod rule
1605  xgk(2), xgk(4), ... abscissae of the 7-point
1606  gauss rule
1607  xgk(1), xgk(3), ... abscissae which are optimally
1608  added to the 7-point gauss rule
1609 
1610  wgk - weights of the 15-point kronrod rule
1611 
1612  wg - weights of the 7-point gauss rule, corresponding
1613  to the abscissae xgk(2), xgk(4), ...
1614  wg(1), wg(3), ... are set to zero.
1615 
1616 
1617 
1618 
1619 
1620  list of major variables
1621  -----------------------
1622 
1623  centr - mid point of the interval
1624  hlgth - half-length of the interval
1625  absc* - abscissa
1626  tabsc* - transformed abscissa
1627  fval* - function value
1628  resg - result of the 7-point gauss formula
1629  resk - result of the 15-point kronrod formula
1630  reskh - approximation to the mean value of the transformed
1631  integrand over (a,b), i.e. to i/(b-a)
1632 
1633  machine dependent constants
1634  ---------------------------
1635 
1636  epmach is the largest relative spacing.
1637  uflow is the smallest positive magnitude.
1638 */
1639 
1640 /* ***first executable statement dqk15i */
1641  epmach = DBL_EPSILON;
1642  uflow = DBL_MIN;
1643  dinf = (double) imin2(1, *inf);
1644 
1645  centr = (*a + *b) * .5;
1646  hlgth = (*b - *a) * .5;
1647  tabsc1 = *boun + dinf * (1. - centr) / centr;
1648  vec[0] = tabsc1;
1649  if (*inf == 2) {
1650  vec2[0] = -tabsc1;
1651  }
1652  for (j = 1; j <= 7; ++j) {
1653  absc = hlgth * xgk[j - 1];
1654  absc1 = centr - absc;
1655  absc2 = centr + absc;
1656  tabsc1 = *boun + dinf * (1. - absc1) / absc1;
1657  tabsc2 = *boun + dinf * (1. - absc2) / absc2;
1658  vec[(j << 1) - 1] = tabsc1;
1659  vec[j * 2] = tabsc2;
1660  if (*inf == 2) {
1661  vec2[(j << 1) - 1] = -tabsc1;
1662  vec2[j * 2] = -tabsc2;
1663  }
1664 /* L5: */
1665  }
1666  f(vec, 15, ex); /* -> new vec[] overwriting old vec[] */
1667  if (*inf == 2) f(vec2, 15, ex);
1668  fval1 = vec[0];
1669  if (*inf == 2) fval1 += vec2[0];
1670  fc = fval1 / centr / centr;
1671 
1672 /* compute the 15-point kronrod approximation to
1673  the integral, and estimate the error. */
1674 
1675  resg = wg[7] * fc;
1676  resk = wgk[7] * fc;
1677  *resabs = fabs(resk);
1678  for (j = 1; j <= 7; ++j) {
1679  absc = hlgth * xgk[j - 1];
1680  absc1 = centr - absc;
1681  absc2 = centr + absc;
1682  tabsc1 = *boun + dinf * (1. - absc1) / absc1;
1683  tabsc2 = *boun + dinf * (1. - absc2) / absc2;
1684  fval1 = vec[(j << 1) - 1];
1685  fval2 = vec[j * 2];
1686  if (*inf == 2) {
1687  fval1 += vec2[(j << 1) - 1];
1688  }
1689  if (*inf == 2) {
1690  fval2 += vec2[j * 2];
1691  }
1692  fval1 = fval1 / absc1 / absc1;
1693  fval2 = fval2 / absc2 / absc2;
1694  fv1[j - 1] = fval1;
1695  fv2[j - 1] = fval2;
1696  fsum = fval1 + fval2;
1697  resg += wg[j - 1] * fsum;
1698  resk += wgk[j - 1] * fsum;
1699  *resabs += wgk[j - 1] * (fabs(fval1) + fabs(fval2));
1700 /* L10: */
1701  }
1702  reskh = resk * .5;
1703  *resasc = wgk[7] * fabs(fc - reskh);
1704  for (j = 1; j <= 7; ++j) {
1705  *resasc += wgk[j - 1] * (fabs(fv1[j - 1] - reskh) +
1706  fabs(fv2[j - 1] - reskh));
1707 /* L20: */
1708  }
1709  *result = resk * hlgth;
1710  *resasc *= hlgth;
1711  *resabs *= hlgth;
1712  *abserr = fabs((resk - resg) * hlgth);
1713  if (*resasc != 0. && *abserr != 0.) {
1714  *abserr = *resasc * fmin2(1., pow(*abserr * 200. / *resasc, 1.5));
1715  }
1716  if (*resabs > uflow / (epmach * 50.)) {
1717  *abserr = fmax2(epmach * 50. * *resabs, *abserr);
1718  }
1719  return;
1720 } /* rdqk15i_ */
1721 
1722 template<class Float> static void rdqelg(int *n, Float *epstab, Float *
1723  result, Float *abserr, Float *res3la, int *nres)
1724 {
1725  /* Local variables */
1726  int i__, indx, ib, ib2, ie, k1, k2, k3, num, newelm, limexp;
1727  Float delta1, delta2, delta3, e0, e1, e1abs, e2, e3, epmach, epsinf;
1728  Float oflow, ss, res;
1729  Float errA, err1, err2, err3, tol1, tol2, tol3;
1730 
1731 /* ***begin prologue dqelg
1732 ***refer to dqagie,dqagoe,dqagpe,dqagse
1733 ***revision date 830518 (yymmdd)
1734 ***keywords epsilon algorithm, convergence acceleration,
1735  extrapolation
1736 ***author piessens,robert,appl. math. & progr. div. - k.u.leuven
1737  de doncker,elise,appl. math & progr. div. - k.u.leuven
1738 ***purpose the routine determines the limit of a given sequence of
1739  approximations, by means of the epsilon algorithm of
1740  p.wynn. an estimate of the absolute error is also given.
1741  the condensed epsilon table is computed. only those
1742  elements needed for the computation of the next diagonal
1743  are preserved.
1744 ***description
1745 
1746  epsilon algorithm
1747  standard fortran subroutine
1748  double precision version
1749 
1750  parameters
1751  n - int
1752  epstab(n) contains the new element in the
1753  first column of the epsilon table.
1754 
1755  epstab - double precision
1756  vector of dimension 52 containing the elements
1757  of the two lower diagonals of the triangular
1758  epsilon table. the elements are numbered
1759  starting at the right-hand corner of the
1760  triangle.
1761 
1762  result - double precision
1763  resulting approximation to the integral
1764 
1765  abserr - double precision
1766  estimate of the absolute error computed from
1767  result and the 3 previous results
1768 
1769  res3la - double precision
1770  vector of dimension 3 containing the last 3
1771  results
1772 
1773  nres - int
1774  number of calls to the routine
1775  (should be zero at first call)
1776 
1777 ***end prologue dqelg
1778 
1779 
1780  list of major variables
1781  -----------------------
1782 
1783  e0 - the 4 elements on which the computation of a new
1784  e1 element in the epsilon table is based
1785  e2
1786  e3 e0
1787  e3 e1 new
1788  e2
1789 
1790  newelm - number of elements to be computed in the new diagonal
1791  errA - errA = abs(e1-e0)+abs(e2-e1)+abs(new-e2)
1792  result - the element in the new diagonal with least value of errA
1793 
1794  machine dependent constants
1795  ---------------------------
1796 
1797  epmach is the largest relative spacing.
1798  oflow is the largest positive magnitude.
1799  limexp is the maximum number of elements the epsilon
1800  table can contain. if this number is reached, the upper
1801  diagonal of the epsilon table is deleted. */
1802 
1803 /* ***first executable statement dqelg */
1804  /* Parameter adjustments */
1805  --res3la;
1806  --epstab;
1807 
1808  /* Function Body */
1809  epmach = DBL_EPSILON;
1810  oflow = DBL_MAX;
1811  ++(*nres);
1812  *abserr = oflow;
1813  *result = epstab[*n];
1814  if (*n < 3) {
1815  goto L100;
1816  }
1817  limexp = 50;
1818  epstab[*n + 2] = epstab[*n];
1819  newelm = (*n - 1) / 2;
1820  epstab[*n] = oflow;
1821  num = *n;
1822  k1 = *n;
1823  for (i__ = 1; i__ <= newelm; ++i__) {
1824  k2 = k1 - 1;
1825  k3 = k1 - 2;
1826  res = epstab[k1 + 2];
1827  e0 = epstab[k3];
1828  e1 = epstab[k2];
1829  e2 = res;
1830  e1abs = fabs(e1);
1831  delta2 = e2 - e1;
1832  err2 = fabs(delta2);
1833  tol2 = fmax2(fabs(e2), e1abs) * epmach;
1834  delta3 = e1 - e0;
1835  err3 = fabs(delta3);
1836  tol3 = fmax2(e1abs, fabs(e0)) * epmach;
1837  if (err2 <= tol2 && err3 <= tol3) {
1838  /* if e0, e1 and e2 are equal to within machine
1839  accuracy, convergence is assumed. */
1840  *result = res;/* result = e2 */
1841  *abserr = err2 + err3;/* abserr = fabs(e1-e0)+fabs(e2-e1) */
1842 
1843  goto L100; /* ***jump out of do-loop */
1844  }
1845 
1846  e3 = epstab[k1];
1847  epstab[k1] = e1;
1848  delta1 = e1 - e3;
1849  err1 = fabs(delta1);
1850  tol1 = fmax2(e1abs, fabs(e3)) * epmach;
1851 
1852 /* if two elements are very close to each other, omit
1853  a part of the table by adjusting the value of n */
1854 
1855  if (err1 > tol1 && err2 > tol2 && err3 > tol3) {
1856  ss = 1. / delta1 + 1. / delta2 - 1. / delta3;
1857  epsinf = fabs(ss * e1);
1858 
1859 /* test to detect irregular behaviour in the table, and
1860  eventually omit a part of the table adjusting the value of n. */
1861 
1862  if (epsinf > 1e-4) {
1863  goto L30;
1864  }
1865  }
1866 
1867  *n = i__ + i__ - 1;
1868  goto L50;/* ***jump out of do-loop */
1869 
1870 
1871 L30:/* compute a new element and eventually adjust the value of result. */
1872 
1873  res = e1 + 1. / ss;
1874  epstab[k1] = res;
1875  k1 += -2;
1876  errA = err2 + fabs(res - e2) + err3;
1877  if (errA <= *abserr) {
1878  *abserr = errA;
1879  *result = res;
1880  }
1881  }
1882 
1883 /* shift the table. */
1884 
1885 L50:
1886  if (*n == limexp) {
1887  *n = (limexp / 2 << 1) - 1;
1888  }
1889 
1890  if (num / 2 << 1 == num) ib = 2; else ib = 1;
1891  ie = newelm + 1;
1892  for (i__ = 1; i__ <= ie; ++i__) {
1893  ib2 = ib + 2;
1894  epstab[ib] = epstab[ib2];
1895  ib = ib2;
1896  }
1897  if (num != *n) {
1898  indx = num - *n + 1;
1899  for (i__ = 1; i__ <= *n; ++i__) {
1900  epstab[i__] = epstab[indx];
1901  ++indx;
1902  }
1903  }
1904  /*L80:*/
1905  if (*nres >= 4) {
1906  /* L90: */
1907  *abserr = fabs(*result - res3la[3]) +
1908  fabs(*result - res3la[2]) +
1909  fabs(*result - res3la[1]);
1910  res3la[1] = res3la[2];
1911  res3la[2] = res3la[3];
1912  res3la[3] = *result;
1913  } else {
1914  res3la[*nres] = *result;
1915  *abserr = oflow;
1916  }
1917 
1918 L100:/* compute error estimate */
1919  *abserr = fmax2(*abserr, epmach * 5. * fabs(*result));
1920  return;
1921 } /* rdqelg_ */
1922 
1923 template<class Float, class integr_fn> static void rdqk21(integr_fn f, void *ex, Float *a, Float *b, Float *result,
1924  Float *abserr, Float *resabs, Float *resasc)
1925 {
1926  /* Initialized data */
1927 
1928  static double wg[5] = { .066671344308688137593568809893332,
1929  .149451349150580593145776339657697,
1930  .219086362515982043995534934228163,
1931  .269266719309996355091226921569469,
1932  .295524224714752870173892994651338 };
1933  static double xgk[11] = { .995657163025808080735527280689003,
1934  .973906528517171720077964012084452,
1935  .930157491355708226001207180059508,
1936  .865063366688984510732096688423493,
1937  .780817726586416897063717578345042,
1938  .679409568299024406234327365114874,
1939  .562757134668604683339000099272694,
1940  .433395394129247190799265943165784,
1941  .294392862701460198131126603103866,
1942  .14887433898163121088482600112972,0. };
1943  static double wgk[11] = { .011694638867371874278064396062192,
1944  .03255816230796472747881897245939,
1945  .05475589657435199603138130024458,
1946  .07503967481091995276704314091619,
1947  .093125454583697605535065465083366,
1948  .109387158802297641899210590325805,
1949  .123491976262065851077958109831074,
1950  .134709217311473325928054001771707,
1951  .142775938577060080797094273138717,
1952  .147739104901338491374841515972068,
1953  .149445554002916905664936468389821 };
1954 
1955 
1956  /* Local variables */
1957  Float fv1[10], fv2[10], vec[21];
1958  Float absc, resg, resk, fsum, fval1, fval2;
1959  Float hlgth, centr, reskh, uflow;
1960  Float fc, epmach, dhlgth;
1961  int j, jtw, jtwm1;
1962 
1963 /* ***begin prologue dqk21
1964 ***date written 800101 (yymmdd)
1965 ***revision date 830518 (yymmdd)
1966 ***category no. h2a1a2
1967 ***keywords 21-point gauss-kronrod rules
1968 ***author piessens,robert,appl. math. & progr. div. - k.u.leuven
1969  de doncker,elise,appl. math. & progr. div. - k.u.leuven
1970 ***purpose to compute i = integral of f over (a,b), with error
1971  estimate
1972  j = integral of abs(f) over (a,b)
1973 ***description
1974 
1975  integration rules
1976  standard fortran subroutine
1977  double precision version
1978 
1979  parameters
1980  on entry
1981  f - double precision
1982  function subprogram defining the integrand
1983  function f(x). the actual name for f needs to be
1984  declared e x t e r n a l in the driver program.
1985 
1986  a - double precision
1987  lower limit of integration
1988 
1989  b - double precision
1990  upper limit of integration
1991 
1992  on return
1993  result - double precision
1994  approximation to the integral i
1995  result is computed by applying the 21-point
1996  kronrod rule (resk) obtained by optimal addition
1997  of abscissae to the 10-point gauss rule (resg).
1998 
1999  abserr - double precision
2000  estimate of the modulus of the absolute error,
2001  which should not exceed abs(i-result)
2002 
2003  resabs - double precision
2004  approximation to the integral j
2005 
2006  resasc - double precision
2007  approximation to the integral of abs(f-i/(b-a))
2008  over (a,b)
2009 
2010 ***references (none)
2011 ***end prologue dqk21
2012 
2013 
2014 
2015  the abscissae and weights are given for the interval (-1,1).
2016  because of symmetry only the positive abscissae and their
2017  corresponding weights are given.
2018 
2019  xgk - abscissae of the 21-point kronrod rule
2020  xgk(2), xgk(4), ... abscissae of the 10-point
2021  gauss rule
2022  xgk(1), xgk(3), ... abscissae which are optimally
2023  added to the 10-point gauss rule
2024 
2025  wgk - weights of the 21-point kronrod rule
2026 
2027  wg - weights of the 10-point gauss rule
2028 
2029 
2030 gauss quadrature weights and kronron quadrature abscissae and weights
2031 as evaluated with 80 decimal digit arithmetic by l. w. fullerton,
2032 bell labs, nov. 1981.
2033 
2034 
2035 
2036 
2037 
2038  list of major variables
2039  -----------------------
2040 
2041  centr - mid point of the interval
2042  hlgth - half-length of the interval
2043  absc - abscissa
2044  fval* - function value
2045  resg - result of the 10-point gauss formula
2046  resk - result of the 21-point kronrod formula
2047  reskh - approximation to the mean value of f over (a,b),
2048  i.e. to i/(b-a)
2049 
2050 
2051  machine dependent constants
2052  ---------------------------
2053 
2054  epmach is the largest relative spacing.
2055  uflow is the smallest positive magnitude. */
2056 
2057 /* ***first executable statement dqk21 */
2058  epmach = DBL_EPSILON;
2059  uflow = DBL_MIN;
2060 
2061  centr = (*a + *b) * .5;
2062  hlgth = (*b - *a) * .5;
2063  dhlgth = fabs(hlgth);
2064 
2065 /* compute the 21-point kronrod approximation to
2066  the integral, and estimate the absolute error. */
2067 
2068  resg = 0.;
2069  vec[0] = centr;
2070  for (j = 1; j <= 5; ++j) {
2071  jtw = j << 1;
2072  absc = hlgth * xgk[jtw - 1];
2073  vec[(j << 1) - 1] = centr - absc;
2074 /* L5: */
2075  vec[j * 2] = centr + absc;
2076  }
2077  for (j = 1; j <= 5; ++j) {
2078  jtwm1 = (j << 1) - 1;
2079  absc = hlgth * xgk[jtwm1 - 1];
2080  vec[(j << 1) + 9] = centr - absc;
2081  vec[(j << 1) + 10] = centr + absc;
2082  }
2083  f(vec, 21, ex);
2084  fc = vec[0];
2085  resk = wgk[10] * fc;
2086  *resabs = fabs(resk);
2087  for (j = 1; j <= 5; ++j) {
2088  jtw = j << 1;
2089  absc = hlgth * xgk[jtw - 1];
2090  fval1 = vec[(j << 1) - 1];
2091  fval2 = vec[j * 2];
2092  fv1[jtw - 1] = fval1;
2093  fv2[jtw - 1] = fval2;
2094  fsum = fval1 + fval2;
2095  resg += wg[j - 1] * fsum;
2096  resk += wgk[jtw - 1] * fsum;
2097  *resabs += wgk[jtw - 1] * (fabs(fval1) + fabs(fval2));
2098 /* L10: */
2099  }
2100  for (j = 1; j <= 5; ++j) {
2101  jtwm1 = (j << 1) - 1;
2102  absc = hlgth * xgk[jtwm1 - 1];
2103  fval1 = vec[(j << 1) + 9];
2104  fval2 = vec[(j << 1) + 10];
2105  fv1[jtwm1 - 1] = fval1;
2106  fv2[jtwm1 - 1] = fval2;
2107  fsum = fval1 + fval2;
2108  resk += wgk[jtwm1 - 1] * fsum;
2109  *resabs += wgk[jtwm1 - 1] * (fabs(fval1) + fabs(fval2));
2110 /* L15: */
2111  }
2112  reskh = resk * .5;
2113  *resasc = wgk[10] * fabs(fc - reskh);
2114  for (j = 1; j <= 10; ++j) {
2115  *resasc += wgk[j - 1] * (fabs(fv1[j - 1] - reskh) +
2116  fabs(fv2[j - 1] - reskh));
2117 /* L20: */
2118  }
2119  *result = resk * hlgth;
2120  *resabs *= dhlgth;
2121  *resasc *= dhlgth;
2122  *abserr = fabs((resk - resg) * hlgth);
2123  if (*resasc != 0. && *abserr != 0.) {
2124  *abserr = *resasc * fmin2(1., pow(*abserr * 200. / *resasc, 1.5));
2125  }
2126  if (*resabs > uflow / (epmach * 50.)) {
2127  *abserr = fmax2(epmach * 50. * *resabs, *abserr);
2128  }
2129  return;
2130 } /* rdqk21_ */
2131 
2132 template<class Float> static void rdqpsrt(int *limit, int *last, int *maxerr,
2133  Float *ermax, Float *elist, int *iord, int *nrmax)
2134 {
2135  /* Local variables */
2136  int i, j, k, ido, jbnd, isucc, jupbn;
2137  Float errmin, errmax;
2138 
2139 /* ***begin prologue dqpsrt
2140  ***refer to dqage,dqagie,dqagpe,dqawse
2141  ***routines called (none)
2142  ***revision date 810101 (yymmdd)
2143  ***keywords sequential sorting
2144  ***author piessens,robert,appl. math. & progr. div. - k.u.leuven
2145  de doncker,elise,appl. math. & progr. div. - k.u.leuven
2146  ***purpose this routine maintains the descending ordering in the
2147  list of the local error estimated resulting from the
2148  interval subdivision process. at each call two error
2149  estimates are inserted using the sequential search
2150  method, top-down for the largest error estimate and
2151  bottom-up for the smallest error estimate.
2152  ***description
2153 
2154  ordering routine
2155  standard fortran subroutine
2156  double precision version
2157 
2158  parameters (meaning at output)
2159  limit - int
2160  maximum number of error estimates the list
2161  can contain
2162 
2163  last - int
2164  number of error estimates currently in the list
2165 
2166  maxerr - int
2167  maxerr points to the nrmax-th largest error
2168  estimate currently in the list
2169 
2170  ermax - double precision
2171  nrmax-th largest error estimate
2172  ermax = elist(maxerr)
2173 
2174  elist - double precision
2175  vector of dimension last containing
2176  the error estimates
2177 
2178  iord - int
2179  vector of dimension last, the first k elements
2180  of which contain pointers to the error
2181  estimates, such that
2182  elist(iord(1)),..., elist(iord(k))
2183  form a decreasing sequence, with
2184  k = last if last <= (limit/2+2), and
2185  k = limit+1-last otherwise
2186 
2187  nrmax - int
2188  maxerr = iord(nrmax)
2189 
2190 ***end prologue dqpsrt
2191 */
2192 
2193 
2194  /* Parameter adjustments */
2195  --iord;
2196  --elist;
2197 
2198  /* Function Body */
2199 
2200 /* check whether the list contains more than
2201  two error estimates. */
2202  if (*last <= 2) {
2203  iord[1] = 1;
2204  iord[2] = 2;
2205  goto Last;
2206  }
2207 /* this part of the routine is only executed if, due to a
2208  difficult integrand, subdivision increased the error
2209  estimate. in the normal case the insert procedure should
2210  start after the nrmax-th largest error estimate. */
2211 
2212  errmax = elist[*maxerr];
2213  if (*nrmax > 1) {
2214  ido = *nrmax - 1;
2215  for (i = 1; i <= ido; ++i) {
2216  isucc = iord[*nrmax - 1];
2217  if (errmax <= elist[isucc])
2218  break; /* out of for-loop */
2219  iord[*nrmax] = isucc;
2220  --(*nrmax);
2221  /* L20: */
2222  }
2223  }
2224 
2225 /*L30: compute the number of elements in the list to be maintained
2226  in descending order. this number depends on the number of
2227  subdivisions still allowed. */
2228  if (*last > *limit / 2 + 2)
2229  jupbn = *limit + 3 - *last;
2230  else
2231  jupbn = *last;
2232 
2233  errmin = elist[*last];
2234 
2235 /* insert errmax by traversing the list top-down,
2236  starting comparison from the element elist(iord(nrmax+1)). */
2237 
2238  jbnd = jupbn - 1;
2239  for (i = *nrmax + 1; i <= jbnd; ++i) {
2240  isucc = iord[i];
2241  if (errmax >= elist[isucc]) {/* ***jump out of do-loop */
2242  /* L60: insert errmin by traversing the list bottom-up. */
2243  iord[i - 1] = *maxerr;
2244  for (j = i, k = jbnd; j <= jbnd; j++, k--) {
2245  isucc = iord[k];
2246  if (errmin < elist[isucc]) {
2247  /* goto L80; ***jump out of do-loop */
2248  iord[k + 1] = *last;
2249  goto Last;
2250  }
2251  iord[k + 1] = isucc;
2252  }
2253  iord[i] = *last;
2254  goto Last;
2255  }
2256  iord[i - 1] = isucc;
2257  }
2258 
2259  iord[jbnd] = *maxerr;
2260  iord[jupbn] = *last;
2261 
2262 Last:/* set maxerr and ermax. */
2263 
2264  *maxerr = iord[*nrmax];
2265  *ermax = elist[*maxerr];
2266  return;
2267 } /* rdqpsrt_ */
static void rdqpsrt(int *, int *, int *, Float *, Float *, int *, int *)
Definition: integrate.cpp:2132
double fmin2(S x, T y)
Definition: integrate.hpp:31
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
#define TRUE
#define FALSE
static void rdqk15i(integr_fn f, void *ex, Float *, int *, Float *, Float *, Float *, Float *, Float *, Float *)
Definition: integrate.cpp:1492
double fmax2(double a, const dvariable &v)
Definition: dtweedie.cpp:14
void Rdqagi(integr_fn f, void *ex, Float *bound, int *inf, Float *epsabs, Float *epsrel, Float *result, Float *abserr, int *neval, int *ier, int *limit, int *lenw, int *last, int *iwork, Float *work)
Definition: integrate.cpp:71
static void rdqagse(integr_fn f, void *ex, Float *, Float *, Float *, Float *, int *, Float *, Float *, int *, int *, Float *, Float *, Float *, Float *, int *, int *)
Definition: integrate.cpp:986
void Rdqags(integr_fn f, void *ex, Float *a, Float *b, Float *epsabs, Float *epsrel, Float *result, Float *abserr, int *neval, int *ier, int *limit, int *lenw, int *last, int *iwork, Float *work)
Definition: integrate.cpp:805
static void rdqelg(int *, Float *, Float *, Float *, Float *, int *)
Definition: integrate.cpp:1722
static void rdqagie(integr_fn f, void *ex, Float *, int *, Float *, Float *, int *, Float *, Float *, int *, int *, Float *, Float *, Float *, Float *, int *, int *)
Definition: integrate.cpp:254
int imin2(int a, int b)
Definition: dtweedie.cpp:10
static void rdqk21(integr_fn f, void *ex, Float *, Float *, Float *, Float *, Float *, Float *)
Definition: integrate.cpp:1923
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17