/src/quantlib/ql/methods/finitedifferences/pde.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2005 Joseph Wang |
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 pde.hpp |
21 | | \brief General class for one dimensional PDE's |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_pde_hpp |
25 | | #define quantlib_pde_hpp |
26 | | |
27 | | #include <ql/math/array.hpp> |
28 | | #include <ql/methods/finitedifferences/tridiagonaloperator.hpp> |
29 | | #include <ql/math/transformedgrid.hpp> |
30 | | #include <utility> |
31 | | |
32 | | namespace QuantLib { |
33 | | |
34 | | /*! \deprecated Part of the old FD framework; copy this function |
35 | | in your codebase if needed. |
36 | | Deprecated in version 1.37. |
37 | | */ |
38 | | class [[deprecated("Part of the old FD framework; copy this function in your codebase if needed")]] PdeSecondOrderParabolic { |
39 | | public: |
40 | | virtual ~PdeSecondOrderParabolic() = default; |
41 | | virtual Real diffusion(Time t, Real x) const = 0; |
42 | | virtual Real drift(Time t, Real x) const = 0; |
43 | | virtual Real discount(Time t, Real x) const = 0; |
44 | | QL_DEPRECATED_DISABLE_WARNING |
45 | | virtual void |
46 | 0 | generateOperator(Time t, const TransformedGrid& tg, TridiagonalOperator& L) const { |
47 | 0 | for (Size i = 1; i < tg.size() - 1; i++) { |
48 | 0 | Real sigma = diffusion(t, tg.grid(i)); |
49 | 0 | Real nu = drift(t, tg.grid(i)); |
50 | 0 | Real r = discount(t, tg.grid(i)); |
51 | 0 | Real sigma2 = sigma * sigma; |
52 | 0 |
|
53 | 0 | Real pd = -(sigma2 / tg.dxm(i) - nu) / tg.dx(i); |
54 | 0 | Real pu = -(sigma2 / tg.dxp(i) + nu) / tg.dx(i); |
55 | 0 | Real pm = sigma2 / (tg.dxm(i) * tg.dxp(i)) + r; |
56 | 0 | L.setMidRow(i, pd, pm, pu); |
57 | 0 | } |
58 | 0 | } |
59 | | }; |
60 | | |
61 | | /*! \deprecated Part of the old FD framework; copy this function |
62 | | in your codebase if needed. |
63 | | Deprecated in version 1.37. |
64 | | */ |
65 | | template <class PdeClass> |
66 | | class [[deprecated("Part of the old FD framework; copy this function in your codebase if needed")]] PdeConstantCoeff : public PdeSecondOrderParabolic { |
67 | | public: |
68 | | PdeConstantCoeff(const typename PdeClass::argument_type &process, |
69 | | Time t, Real x) { |
70 | | PdeClass pde(process); |
71 | | diffusion_ = pde.diffusion(t, x); |
72 | | drift_ = pde.drift(t, x); |
73 | | discount_ = pde.discount(t, x); |
74 | | } |
75 | | Real diffusion(Time, Real) const override { return diffusion_; } |
76 | | Real drift(Time, Real) const override { return drift_; } |
77 | | Real discount(Time, Real) const override { return discount_; } |
78 | | |
79 | | private: |
80 | | Real diffusion_; |
81 | | Real drift_; |
82 | | Real discount_; |
83 | | }; |
84 | | |
85 | | QL_DEPRECATED_ENABLE_WARNING |
86 | | |
87 | | /*! \deprecated Part of the old FD framework; copy this function |
88 | | in your codebase if needed. |
89 | | Deprecated in version 1.37. |
90 | | */ |
91 | | template <class PdeClass> |
92 | | class [[deprecated("Part of the old FD framework; copy this function in your codebase if needed")]] GenericTimeSetter:public TridiagonalOperator::TimeSetter { |
93 | | public: |
94 | | template <class T> |
95 | | GenericTimeSetter(const Array &grid, T process) : |
96 | | grid_(grid), pde_(std::move(process)) {} |
97 | | void setTime(Time t, TridiagonalOperator& L) const override { |
98 | | pde_.generateOperator(t, grid_, L); |
99 | | } |
100 | | |
101 | | private: |
102 | | typename PdeClass::grid_type grid_; |
103 | | PdeClass pde_; |
104 | | }; |
105 | | |
106 | | /*! \deprecated Part of the old FD framework; copy this function |
107 | | in your codebase if needed. |
108 | | Deprecated in version 1.37. |
109 | | */ |
110 | | template <class PdeClass> |
111 | | class [[deprecated("Part of the old FD framework; copy this function in your codebase if needed")]] PdeOperator:public TridiagonalOperator { |
112 | | public: |
113 | | template <class T> |
114 | | PdeOperator(const Array& grid, |
115 | | T process, |
116 | | Time residualTime = 0.0) : |
117 | | TridiagonalOperator(grid.size()) { |
118 | | QL_DEPRECATED_DISABLE_WARNING |
119 | | timeSetter_ = |
120 | | ext::shared_ptr<GenericTimeSetter<PdeClass> >( |
121 | | new GenericTimeSetter<PdeClass>(grid, std::move(process))); |
122 | | QL_DEPRECATED_ENABLE_WARNING |
123 | | setTime(residualTime); |
124 | | } |
125 | | }; |
126 | | } |
127 | | |
128 | | |
129 | | #endif |