| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2007 François du Vignaud |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <ql/math/integrals/integral.hpp> |
| 21 | #include <ql/errors.hpp> |
| 22 | |
| 23 | namespace QuantLib { |
| 24 | |
| 25 | Integrator::Integrator(Real absoluteAccuracy, |
| 26 | Size maxEvaluations) |
| 27 | : absoluteAccuracy_(absoluteAccuracy), |
| 28 | maxEvaluations_(maxEvaluations) { |
| 29 | QL_REQUIRE(absoluteAccuracy > QL_EPSILON, |
| 30 | std::scientific << "required tolerance (" << |
| 31 | absoluteAccuracy << ") not allowed. It must be > " << |
| 32 | QL_EPSILON); |
| 33 | } |
| 34 | |
| 35 | void Integrator::setAbsoluteAccuracy(Real accuracy) { |
| 36 | absoluteAccuracy_= accuracy; |
| 37 | } |
| 38 | |
| 39 | void Integrator::setMaxEvaluations(Size maxEvaluations) { |
| 40 | maxEvaluations_ = maxEvaluations; |
| 41 | } |
| 42 | |
| 43 | Real Integrator::absoluteAccuracy() const { |
| 44 | return absoluteAccuracy_; |
| 45 | } |
| 46 | |
| 47 | Size Integrator::maxEvaluations() const { |
| 48 | return maxEvaluations_; |
| 49 | } |
| 50 | |
| 51 | Real Integrator::absoluteError() const { |
| 52 | return absoluteError_; |
| 53 | } |
| 54 | |
| 55 | void Integrator::setAbsoluteError(Real error) const { |
| 56 | absoluteError_ = error; |
| 57 | } |
| 58 | |
| 59 | Size Integrator::numberOfEvaluations() const { |
| 60 | return evaluations_; |
| 61 | } |
| 62 | |
| 63 | void Integrator::setNumberOfEvaluations(Size evaluations) const { |
| 64 | evaluations_ = evaluations; |
| 65 | } |
| 66 | |
| 67 | void Integrator::increaseNumberOfEvaluations(Size increase) const { |
| 68 | evaluations_ += increase; |
| 69 | } |
| 70 | |
| 71 | bool Integrator::integrationSuccess() const { |
| 72 | return evaluations_ <= maxEvaluations_ |
| 73 | && absoluteError_ <= absoluteAccuracy_; |
| 74 | } |
| 75 | |
| 76 | Real Integrator::operator()(const ext::function<Real (Real)>& f, |
| 77 | Real a, |
| 78 | Real b) const { |
| 79 | evaluations_ = 0; |
| 80 | if (a == b) |
| 81 | return 0.0; |
| 82 | if (b > a) |
| 83 | return integrate(f, a, b); |
| 84 | else |
| 85 | return -integrate(f, a: b, b: a); |
| 86 | } |
| 87 | |
| 88 | } |
| 89 | |