| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2013 Klaus Spanderen |
| 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 | /*! \file twodimensionalintegral.hpp |
| 21 | \brief two dimensional integration |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_two_dimensional_integral_2d_hpp |
| 25 | #define quantlib_two_dimensional_integral_2d_hpp |
| 26 | |
| 27 | #include <ql/math/integrals/integral.hpp> |
| 28 | #include <ql/shared_ptr.hpp> |
| 29 | #include <ql/functional.hpp> |
| 30 | #include <utility> |
| 31 | |
| 32 | namespace QuantLib { |
| 33 | |
| 34 | //! Integral of a two-dimensional function |
| 35 | /*! The integral of a two dimensional function\f$ f(x,y) \f$ |
| 36 | * between \f$ (a_x, a_y) \f$ and \f$ (b_x, b_y) \f$ |
| 37 | is calculated by means of two nested integrations |
| 38 | */ |
| 39 | |
| 40 | class TwoDimensionalIntegral { |
| 41 | public: |
| 42 | TwoDimensionalIntegral(ext::shared_ptr<Integrator> integratorX, |
| 43 | ext::shared_ptr<Integrator> integratorY) |
| 44 | : integratorX_(std::move(integratorX)), integratorY_(std::move(integratorY)) {} |
| 45 | |
| 46 | Real operator()(const ext::function<Real (Real, Real)>& f, |
| 47 | const std::pair<Real, Real>& a, |
| 48 | const std::pair<Real, Real>& b) const { |
| 49 | return (*integratorX_)([&](Real x) { return g(f, x, a: a.second, b: b.second); }, |
| 50 | a.first, b.first); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | Real g(const ext::function<Real (Real, Real)>& f, |
| 55 | Real x, Real a, Real b) const { |
| 56 | return (*integratorY_)([&](Real y) { return f(x, y); }, a, b); |
| 57 | } |
| 58 | |
| 59 | const ext::shared_ptr<Integrator> integratorX_, integratorY_; |
| 60 | }; |
| 61 | } |
| 62 | #endif |
| 63 | |