25 #if !defined(LSST_AFW_MATH_INTEGRATE_H) 26 #define LSST_AFW_MATH_INTEGRATE_H 1 175 if (false) (*_dbgout) 177 if (false) (*(reg.getDbgout())) 179 if (false) (*(tempreg.getDbgout())) 182 if (_dbgout) (*_dbgout) 184 if (reg.getDbgout()) (*(reg.getDbgout())) 186 if (tempreg.getDbgout()) (*(tempreg.getDbgout())) 212 : _a(a), _b(b), _error(0.0), _area(0), _dbgout(dbgout) {}
214 bool operator<(IntRegion<T>
const &r2)
const {
return _error < r2._error; }
218 assert(children->size() == 0);
219 if (_splitpoints.size() == 0) {
222 if (_splitpoints.size() > 1) {
223 std::sort(_splitpoints.begin(), _splitpoints.end());
227 if (_a > _splitpoints[0] || _b < _splitpoints.back()) {
230 for (
size_t i = 0; i<_splitpoints.size(); i++) {
236 assert(_splitpoints[0] >= _a);
237 assert(_splitpoints.back() <= _b);
238 children->push_back(
IntRegion<T>(_a, _splitpoints[0], _dbgout));
239 for (
size_t i = 1; i < _splitpoints.size(); i++) {
240 children->push_back(
IntRegion<T>(_splitpoints[i - 1], _splitpoints[i], _dbgout));
242 children->push_back(
IntRegion<T>(_splitpoints.back(), _b, _dbgout));
245 void Bisect() { _splitpoints.push_back((_a + _b) / 2.0); }
247 size_t NSplit()
const {
return _splitpoints.size(); }
249 T
const &
Left()
const {
return _a; }
250 T
const &
Right()
const {
return _b; }
251 T
const &
Err()
const {
return _error; }
252 T
const &
Area()
const {
return _area; }
261 T _a, _b, _error, _area;
282 inline Quad Epsilon<Quad>() {
283 return 3.08148791094e-33;
286 inline Quad MinRep<Quad>() {
287 return 2.2250738585072014e-308;
293 if (resasc != 0.0 && err != 0.0) {
294 T
const scale = (200.0 * err / resasc);
296 err = resasc * scale * sqrt(scale);
301 if (resabs > MinRep<T>() / (50.0 * Epsilon<T>())) {
302 T
const min_err = 50.0 * Epsilon<T>() * resabs;
328 typename UF::result_type
const epsabs,
typename UF::result_type
const epsrel,
330 typedef typename UF::result_type UfResult;
331 UfResult
const a = reg.
Left();
332 UfResult
const b = reg.
Right();
334 UfResult
const halfLength = 0.5 * (b - a);
335 UfResult
const absHalfLength = fabs(halfLength);
336 UfResult
const center = 0.5 * (b + a);
337 UfResult
const fCenter = func(center);
342 assert(gkp_wb<UfResult>(0).size() == gkp_x<UfResult>(0).size() + 1);
343 UfResult area1 = gkp_wb<UfResult>(0).back() * fCenter;
345 fv1.
reserve(2 * gkp_x<UfResult>(0).size() + 1);
346 fv2.
reserve(2 * gkp_x<UfResult>(0).size() + 1);
347 for (
size_t k = 0; k < gkp_x<UfResult>(0).size(); k++) {
348 UfResult
const abscissa = halfLength * gkp_x<UfResult>(0)[k];
349 UfResult
const fval1 = func(center - abscissa);
350 UfResult
const fval2 = func(center + abscissa);
351 area1 += gkp_wb<UfResult>(0)[k] * (fval1 + fval2);
355 (*fxmap)[center - abscissa] = fval1;
356 (*fxmap)[center + abscissa] = fval2;
360 nfeval += gkp_x<UfResult>(0).size() * 2;
366 bool calcabsasc =
true;
367 UfResult resabs = 0.0, resasc = 0.0;
368 for (
int level = 1; level < NGKPLEVELS; level++) {
369 assert(gkp_wa<UfResult>(level).size() == fv1.
size());
370 assert(gkp_wa<UfResult>(level).size() == fv2.
size());
371 assert(gkp_wb<UfResult>(level).size() == gkp_x<UfResult>(level).size() + 1);
372 UfResult area2 = gkp_wb<UfResult>(level).back() * fCenter;
375 resabs = fabs(area2);
377 for (
size_t k = 0; k < fv1.
size(); k++) {
378 area2 += gkp_wa<UfResult>(level)[k] * (fv1[k] + fv2[k]);
380 resabs += gkp_wa<UfResult>(level)[k] * (fabs(fv1[k]) + fabs(fv2[k]));
383 for (
size_t k = 0; k < gkp_x<UfResult>(level).size(); k++) {
384 UfResult
const abscissa = halfLength * gkp_x<UfResult>(level)[k];
385 UfResult
const fval1 = func(center - abscissa);
386 UfResult
const fval2 = func(center + abscissa);
387 UfResult
const fval = fval1 + fval2;
388 area2 += gkp_wb<UfResult>(level)[k] * fval;
390 resabs += gkp_wb<UfResult>(level)[k] * (fabs(fval1) + fabs(fval2));
395 (*fxmap)[center - abscissa] = fval1;
396 (*fxmap)[center + abscissa] = fval2;
400 nfeval += gkp_x<UfResult>(level).size() * 2;
403 UfResult
const mean = area1 * UfResult(0.5);
405 resasc = gkp_wb<UfResult>(level).back() * fabs(fCenter - mean);
406 for (
size_t k = 0; k < gkp_wa<UfResult>(level).size(); k++) {
407 resasc += gkp_wa<UfResult>(level)[k] * (fabs(fv1[k] - mean) + fabs(fv2[k] - mean));
409 for (
size_t k = 0; k < gkp_x<UfResult>(level).size(); k++) {
410 resasc += gkp_wb<UfResult>(level)[k] * (fabs(fv1[k] - mean) + fabs(fv2[k] - mean));
412 resasc *= absHalfLength;
413 resabs *= absHalfLength;
421 integ_dbg2 <<
"at level " << level <<
" area2 = " << area2;
425 if (err < epsabs || err < epsrel * fabs(area2)) {
435 integ_dbg2 <<
"Failed to reach tolerance with highest-order GKP rule";
458 typename UF::result_type
const epsabs,
typename UF::result_type
const epsrel,
460 typedef typename UF::result_type UfResult;
463 assert(epsabs >= 0.0);
464 assert(epsrel > 0.0);
467 bool done =
intGKPNA(func, reg, epsabs, epsrel, fxmap);
470 integ_dbg2 <<
"In adaptive GKP, failed first pass... subdividing\n";
473 int roundoffType1 = 0, errorType = 0;
474 UfResult roundoffType2 = 0;
475 size_t iteration = 1;
478 allregions.
push(reg);
479 UfResult finalarea = reg.
Area();
480 UfResult finalerr = reg.
Err();
481 UfResult tolerance =
std::max(epsabs, epsrel * fabs(finalarea));
482 assert(finalerr > tolerance);
484 while (!errorType && finalerr > tolerance) {
486 integ_dbg2 <<
"Current answer = " << finalarea <<
" +- " << finalerr;
487 integ_dbg2 <<
" (tol = " << tolerance <<
")\n";
490 integ_dbg2 <<
"Subdividing largest error region ";
500 UfResult factor = 3 * children.
size() * finalerr / tolerance;
501 UfResult newepsabs = fabs(parent.
Err() / factor);
502 UfResult newepsrel = newepsabs / fabs(parent.
Area());
503 integ_dbg2 <<
"New epsabs, rel = " << newepsabs <<
", " << newepsrel;
506 UfResult newarea = UfResult(0.0);
507 UfResult newerror = 0.0;
508 for (
size_t i = 0; i < children.
size(); i++) {
513 hasConverged =
intGKPNA(func, child, newepsabs, newepsrel);
514 integ_dbg2 <<
"child (" << i + 1 <<
'/' << children.
size() <<
") ";
522 newarea += child.
Area();
523 newerror += child.
Err();
525 integ_dbg2 <<
"Compare: newerr = " << newerror;
528 finalerr += (newerror - parent.
Err());
529 finalarea += newarea - parent.
Area();
531 UfResult delta = parent.
Area() - newarea;
532 if (newerror <= parent.
Err() && fabs(delta) <= parent.
Err() && newerror >= 0.99 * parent.
Err()) {
533 integ_dbg2 <<
"roundoff type 1: delta/newarea = ";
538 if (iteration >= 10 && newerror > parent.
Err() && fabs(delta) <= newerror - parent.
Err()) {
539 integ_dbg2 <<
"roundoff type 2: newerror/error = ";
541 roundoffType2 +=
std::min(newerror / parent.
Err() - 1.0, UfResult(1.0));
544 tolerance =
std::max(epsabs, epsrel * fabs(finalarea));
545 if (finalerr > tolerance) {
546 if (roundoffType1 >= 200) {
550 if (roundoffType2 >= 200.0) {
554 if (fabs((parent.
Right() - parent.
Left()) / (reg.
Right() - reg.
Left())) < Epsilon<double>()) {
559 for (
size_t i = 0; i < children.
size(); i++) {
560 allregions.
push(children[i]);
568 while (!allregions.
empty()) {
570 finalarea += r.
Area();
574 reg.
SetArea(finalarea, finalerr);
576 if (errorType == 1) {
578 s <<
"Type 1 roundoff's = " << roundoffType1;
579 s <<
", Type 2 = " << roundoffType2 <<
std::endl;
580 s <<
"Roundoff error 1 prevents tolerance from being achieved ";
583 }
else if (errorType == 2) {
585 s <<
"Type 1 roundoff's = " << roundoffType1;
586 s <<
", Type 2 = " << roundoffType2 <<
std::endl;
587 s <<
"Roundoff error 2 prevents tolerance from being achieved ";
590 }
else if (errorType == 3) {
592 s <<
"Bad integrand behavior found in the integration interval ";
607 typename UF::result_type
operator()(
typename UF::argument_type
x)
const {
608 return _f(1.0 / x - 1.0) / (x *
x);
629 typename UF::result_type
operator()(
typename UF::argument_type
x)
const {
630 return _f(1.0 / x + 1.0) / (x *
x);
670 binder2_1(
const BF &oper,
typename BF::first_argument_type val) : _oper(oper), _value(val) {}
671 typename BF::result_type
operator()(
const typename BF::second_argument_type &
x)
const {
672 return _oper(_value, x);
680 template <
class BF,
class Tp>
682 typedef typename BF::first_argument_type Arg;
686 template <
class BF,
class YREG>
689 Int2DAuxType(BF
const &func, YREG
const &yreg,
typename BF::result_type
const &abserr,
690 typename BF::result_type
const &relerr)
691 : _func(func), _yreg(yreg), _abserr(abserr), _relerr(relerr) {}
693 typename BF::result_type
operator()(
typename BF::first_argument_type
x)
const {
694 typename YREG::result_type tempreg = _yreg(x);
695 typename BF::result_type result =
int1d(
bind21(_func, x), tempreg, _abserr, _relerr);
704 typename BF::result_type _abserr, _relerr;
710 typename TF::thirdof3_argument_type, typename TF::result_type> {
712 binder3_1(
const TF &oper,
typename TF::firstof3_argument_type val) : _oper(oper), _value(val) {}
713 typename TF::result_type
operator()(
typename TF::secondof3_argument_type
const &x1,
714 typename TF::thirdof3_argument_type
const &x2)
const {
715 return _oper(_value, x1, x2);
720 typename TF::firstof3_argument_type
_value;
723 template <
class TF,
class Tp>
725 typedef typename TF::firstof3_argument_type Arg;
729 template <
class TF,
class YREG,
class ZREG>
731 :
public std::unary_function<typename TF::firstof3_argument_type, typename TF::result_type> {
733 Int3DAuxType(
const TF &func,
const YREG &yreg,
const ZREG &zreg,
const typename TF::result_type &abserr,
734 const typename TF::result_type &relerr)
735 : _func(func), _yreg(yreg), _zreg(zreg), _abserr(abserr), _relerr(relerr) {}
737 typename TF::result_type
operator()(
typename TF::firstof3_argument_type
x)
const {
738 typename YREG::result_type tempreg = _yreg(x);
739 typename TF::result_type result =
750 typename TF::result_type _abserr, _relerr;
760 typename UF::result_type
const &abserr = DEFABSERR,
761 typename UF::result_type
const &relerr = DEFRELERR) {
762 typedef typename UF::result_type UfResult;
767 if ((reg.
Left() <= -MOCK_INF && reg.
Right() > 0) || (reg.
Right() >= MOCK_INF && reg.
Left() < 0)) {
774 integ_dbg2 <<
"Subdivided into " << children.
size() <<
" children\n";
775 UfResult answer = UfResult();
777 for (
size_t i = 0; i < children.
size(); i++) {
781 answer +=
int1d(func, child, abserr, relerr);
791 assert(reg.
Right() <= 0.0);
793 intGKP(Aux2<UF>(func), modreg, abserr, relerr);
794 reg.
SetArea(modreg.Area(), modreg.Err());
797 assert(reg.
Left() >= 0.0);
799 intGKP(Aux1<UF>(func), modreg, abserr, relerr);
800 reg.
SetArea(modreg.Area(), modreg.Err());
804 intGKP(func, reg, abserr, relerr);
815 template <
class BF,
class YREG>
817 YREG
const &yreg,
typename BF::result_type
const &abserr = DEFABSERR,
818 typename BF::result_type
const &relerr = DEFRELERR) {
822 Int2DAuxType<BF, YREG> faux(func, yreg, abserr * 1.0e-3, relerr * 1.0e-3);
823 typename BF::result_type answer =
int1d(faux, reg, abserr, relerr);
831 template <
class TF,
class YREG,
class ZREG>
833 YREG
const &yreg, ZREG
const &zreg,
834 typename TF::result_type
const &abserr = DEFABSERR,
835 typename TF::result_type
const &relerr = DEFRELERR) {
839 Int3DAuxType<TF, YREG, ZREG> faux(func, yreg, zreg, abserr * 1.e-3, relerr * 1.e-3);
840 typename TF::result_type answer =
int1d(faux, reg, abserr, relerr);
851 typename BF::result_type
const &abserr = DEFABSERR,
852 typename BF::result_type
const &relerr = DEFRELERR) {
854 return int2d(func, reg, ConstantReg1<typename BF::result_type>(yreg), abserr, relerr);
864 typename TF::result_type
const &abserr = DEFABSERR,
865 typename TF::result_type
const &relerr = DEFRELERR) {
867 return int3d(func, reg, ConstantReg1<typename TF::result_type>(yreg),
868 ConstantReg2<typename TF::result_type>(zreg), abserr, relerr);
879 template <
typename UnaryFunctionT>
880 typename UnaryFunctionT::result_type
integrate(UnaryFunctionT func,
881 typename UnaryFunctionT::argument_type
const a,
882 typename UnaryFunctionT::argument_type
const b,
883 double eps = 1.0e-6) {
884 typedef typename UnaryFunctionT::argument_type Arg;
887 return int1d(func, region, DEFABSERR, eps);
901 template <
typename BinaryFunctionT>
903 typename BinaryFunctionT::result_type> {
905 FunctionWrapper(BinaryFunctionT func,
typename BinaryFunctionT::first_argument_type
const x1,
906 typename BinaryFunctionT::first_argument_type
const x2,
double const eps = 1.0e-6)
907 : _func(func), _x1(x1), _x2(x2), _eps(eps) {}
909 typename BinaryFunctionT::second_argument_type
const y)
const {
910 return integrate(std::bind2nd(_func, y), _x1, _x2, _eps);
914 BinaryFunctionT _func;
915 typename BinaryFunctionT::first_argument_type _x1, _x2;
928 template <
typename BinaryFunctionT>
929 typename BinaryFunctionT::result_type
integrate2d(BinaryFunctionT func,
930 typename BinaryFunctionT::first_argument_type
const x1,
931 typename BinaryFunctionT::first_argument_type
const x2,
932 typename BinaryFunctionT::second_argument_type
const y1,
933 typename BinaryFunctionT::second_argument_type
const y2,
934 double eps = 1.0e-6) {
938 FunctionWrapper<BinaryFunctionT> fwrap(func, x1, x2, eps);
IntRegion< T > operator()(T) const
BF::result_type int2d(BF const &func, IntRegion< typename BF::result_type > ®, YREG const &yreg, typename BF::result_type const &abserr=DEFABSERR, typename BF::result_type const &relerr=DEFRELERR)
Front end for the 2d integrator.
bool operator>(IntRegion< T > const &r2) const
FunctionWrapper(BinaryFunctionT func, typename BinaryFunctionT::first_argument_type const x1, typename BinaryFunctionT::first_argument_type const x2, double const eps=1.0e-6)
binder2_1< BF > bind21(const BF &oper, const Tp &x)
BinaryFunctionT::result_type integrate2d(BinaryFunctionT func, typename BinaryFunctionT::first_argument_type const x1, typename BinaryFunctionT::first_argument_type const x2, typename BinaryFunctionT::second_argument_type const y1, typename BinaryFunctionT::second_argument_type const y2, double eps=1.0e-6)
The 2D integrator.
def scale(algorithm, min, max=None, frame=None)
void SetArea(const T &a, const T &e)
BinaryFunctionT::result_type operator()(typename BinaryFunctionT::second_argument_type const y) const
void SubDivide(std::vector< IntRegion< T > > *children)
AuxFunc1< UF > Aux1(UF uf)
Auxiliary function 1.
TF::result_type operator()(typename TF::secondof3_argument_type const &x1, typename TF::thirdof3_argument_type const &x2) const
ConstantReg2(IntRegion< T > const &r)
UF::result_type int1d(UF const &func, IntRegion< typename UF::result_type > ®, typename UF::result_type const &abserr=DEFABSERR, typename UF::result_type const &relerr=DEFRELERR)
Front end for the 1d integrator.
A base class for image defects.
UF::result_type operator()(typename UF::argument_type x) const
ConstantReg1(IntRegion< T > const &r)
AuxFunc2< UF > Aux2(UF uf)
Auxiliary function 2.
Int3DAuxType(const TF &func, const YREG &yreg, const ZREG &zreg, const typename TF::result_type &abserr, const typename TF::result_type &relerr)
binder3_1< TF > bind31(const TF &oper, const Tp &x)
IntRegion< T > operator()(T x, T y) const
TF::firstof3_argument_type _value
TF::result_type operator()(typename TF::firstof3_argument_type x) const
BF::first_argument_type _value
Int2DAuxType(BF const &func, YREG const &yreg, typename BF::result_type const &abserr, typename BF::result_type const &relerr)
BF::result_type operator()(const typename BF::second_argument_type &x) const
binder3_1(const TF &oper, typename TF::firstof3_argument_type val)
#define LSST_EXCEPT(type,...)
std::ostream * getDbgout()
UF::result_type operator()(typename UF::argument_type x) const
BF::result_type operator()(typename BF::first_argument_type x) const
bool intGKPNA(UF const &func, IntRegion< typename UF::result_type > ®, typename UF::result_type const epsabs, typename UF::result_type const epsrel, std::map< typename UF::result_type, typename UF::result_type > *fxmap=0)
Non-adaptive integration of the function f over the region 'reg'.
TF::result_type int3d(TF const &func, IntRegion< typename TF::result_type > ®, YREG const &yreg, ZREG const &zreg, typename TF::result_type const &abserr=DEFABSERR, typename TF::result_type const &relerr=DEFRELERR)
Front end for the 3d integrator.
binder2_1(const BF &oper, typename BF::first_argument_type val)
T rescale_error(T err, T const &resabs, T const &resasc)
IntRegion(T const a, T const b, std::ostream *dbgout=0)
void intGKP(UF const &func, IntRegion< typename UF::result_type > ®, typename UF::result_type const epsabs, typename UF::result_type const epsrel, std::map< typename UF::result_type, typename UF::result_type > *fxmap=0)
An adaptive integration algorithm which computes the integral of f over the region reg...
Wrap an integrand in a call to a 1D integrator: romberg()
UnaryFunctionT::result_type integrate(UnaryFunctionT func, typename UnaryFunctionT::argument_type const a, typename UnaryFunctionT::argument_type const b, double eps=1.0e-6)
The 1D integrator.
Helpers for constant regions for int2d, int3d: