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
31namespace QuantLib {
32 class PdeSecondOrderParabolic {
33 public:
34 virtual ~PdeSecondOrderParabolic() = default;
35 virtual Real diffusion(Time t, Real x) const = 0;
36 virtual Real drift(Time t, Real x) const = 0;
37 virtual Real discount(Time t, Real x) const = 0;
38 virtual void
39 generateOperator(Time t, const TransformedGrid& tg, TridiagonalOperator& L) const {
40 for (Size i = 1; i < tg.size() - 1; i++) {
41 Real sigma = diffusion(t, x: tg.grid(i));
42 Real nu = drift(t, x: tg.grid(i));
43 Real r = discount(t, x: tg.grid(i));
44 Real sigma2 = sigma * sigma;
45
46 Real pd = -(sigma2 / tg.dxm(i) - nu) / tg.dx(i);
47 Real pu = -(sigma2 / tg.dxp(i) + nu) / tg.dx(i);
48 Real pm = sigma2 / (tg.dxm(i) * tg.dxp(i)) + r;
49 L.setMidRow(i, valA: pd, valB: pm, valC: pu);
50 }
51 }
52 };
53
54 template <class PdeClass>
55 class PdeConstantCoeff : public PdeSecondOrderParabolic {
56 public:
57 PdeConstantCoeff(const typename PdeClass::argument_type &process,
58 Time t, Real x) {
59 PdeClass pde(process);
60 diffusion_ = pde.diffusion(t, x);
61 drift_ = pde.drift(t, x);
62 discount_ = pde.discount(t, x);
63 }
64 Real diffusion(Time, Real) const override { return diffusion_; }
65 Real drift(Time, Real) const override { return drift_; }
66 Real discount(Time, Real) const override { return discount_; }
67
68 private:
69 Real diffusion_;
70 Real drift_;
71 Real discount_;
72 };
73
74 template <class PdeClass>
75 class GenericTimeSetter:public TridiagonalOperator::TimeSetter {
76 public:
77 template <class T>
78 GenericTimeSetter(const Array &grid, T process) :
79 grid_(grid), pde_(process) {}
80 void setTime(Time t, TridiagonalOperator& L) const override {
81 pde_.generateOperator(t, grid_, L);
82 }
83
84 private:
85 typename PdeClass::grid_type grid_;
86 PdeClass pde_;
87 };
88
89 template <class PdeClass>
90 class PdeOperator:public TridiagonalOperator {
91 public:
92 template <class T>
93 PdeOperator(const Array& grid,
94 T process,
95 Time residualTime = 0.0) :
96 TridiagonalOperator(grid.size()) {
97 timeSetter_ =
98 ext::shared_ptr<GenericTimeSetter<PdeClass> >(
99 new GenericTimeSetter<PdeClass>(grid, process));
100 setTime(residualTime);
101 }
102 };
103}
104
105
106#endif
107

source code of quantlib/ql/methods/finitedifferences/pde.hpp