| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2015 Ferdinando Ametrano |
| 5 | Copyright (C) 2015 Paolo Mazzocchi |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | #ifndef quantlib_polynomial_math_function_hpp |
| 22 | #define quantlib_polynomial_math_function_hpp |
| 23 | |
| 24 | #include <ql/math/matrix.hpp> |
| 25 | |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace QuantLib { |
| 29 | |
| 30 | //! %Cubic functional form |
| 31 | /*! \f[ f(t) = \sum_{i=0}^n{c_i t^i} \f] */ |
| 32 | class PolynomialFunction { |
| 33 | |
| 34 | public: |
| 35 | /*! \deprecated Use `auto` or `decltype` instead. |
| 36 | Deprecated in version 1.29. |
| 37 | */ |
| 38 | QL_DEPRECATED |
| 39 | typedef Time argument_type; |
| 40 | |
| 41 | /*! \deprecated Use `auto` or `decltype` instead. |
| 42 | Deprecated in version 1.29. |
| 43 | */ |
| 44 | QL_DEPRECATED |
| 45 | typedef Real result_type; |
| 46 | |
| 47 | PolynomialFunction(const std::vector<Real>& coeff); |
| 48 | |
| 49 | //! function value at time t: \f[ f(t) = \sum_{i=0}^n{c_i t^i} \f] |
| 50 | Real operator()(Time t) const; |
| 51 | |
| 52 | /*! first derivative of the function at time t |
| 53 | \f[ f'(t) = \sum_{i=0}^{n-1}{(i+1) c_{i+1} t^i} \f] */ |
| 54 | Real derivative(Time t) const; |
| 55 | |
| 56 | /*! indefinite integral of the function at time t |
| 57 | \f[ \int f(t)dt = \sum_{i=0}^n{c_i t^{i+1} / (i+1)} + K \f] */ |
| 58 | Real primitive(Time t) const; |
| 59 | |
| 60 | /*! definite integral of the function between t1 and t2 |
| 61 | \f[ \int_{t1}^{t2} f(t)dt \f] */ |
| 62 | Real definiteIntegral(Time t1, |
| 63 | Time t2) const; |
| 64 | |
| 65 | /*! Inspectors */ |
| 66 | Size order() const { return order_; } |
| 67 | const std::vector<Real>& coefficients() { return c_; } |
| 68 | const std::vector<Real>& derivativeCoefficients() { return derC_; } |
| 69 | const std::vector<Real>& primitiveCoefficients() { return prC_; } |
| 70 | |
| 71 | /*! coefficients of a PolynomialFunction defined as definite |
| 72 | integral on a rolling window of length tau, with tau = t2-t */ |
| 73 | std::vector<Real> definiteIntegralCoefficients(Time t, |
| 74 | Time t2) const; |
| 75 | |
| 76 | /*! coefficients of a PolynomialFunction defined as definite |
| 77 | derivative on a rolling window of length tau, with tau = t2-t */ |
| 78 | std::vector<Real> definiteDerivativeCoefficients(Time t, |
| 79 | Time t2) const; |
| 80 | |
| 81 | private: |
| 82 | Size order_; |
| 83 | std::vector<Real> c_, derC_, prC_; |
| 84 | Real K_; |
| 85 | mutable Matrix eqs_; |
| 86 | void initializeEqs_(Time t, |
| 87 | Time t2) const; |
| 88 | }; |
| 89 | |
| 90 | } |
| 91 | |
| 92 | #endif |
| 93 | |