QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mixedscheme.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2002, 2003 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
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/*! \file mixedscheme.hpp
22 \brief Mixed (explicit/implicit) scheme for finite difference methods
23*/
24
25#ifndef quantlib_mixed_scheme_hpp
26#define quantlib_mixed_scheme_hpp
27
29#include <utility>
30
31namespace QuantLib {
32
33 //! Mixed (explicit/implicit) scheme for finite difference methods
34 /*! In this implementation, the passed operator must be derived
35 from either TimeConstantOperator or TimeDependentOperator.
36 Also, it must implement at least the following interface:
37
38 \code
39 typedef ... array_type;
40
41 // copy constructor/assignment
42 // (these will be provided by the compiler if none is defined)
43 Operator(const Operator&);
44 Operator& operator=(const Operator&);
45
46 // inspectors
47 Size size();
48
49 // modifiers
50 void setTime(Time t);
51
52 // operator interface
53 array_type applyTo(const array_type&);
54 array_type solveFor(const array_type&);
55 static Operator identity(Size size);
56
57 // operator algebra
58 Operator operator*(Real, const Operator&);
59 Operator operator+(const Operator&, const Operator&);
60 Operator operator+(const Operator&, const Operator&);
61 \endcode
62
63 \warning The differential operator must be linear for
64 this evolver to work.
65
66 \todo
67 - derive variable theta schemes
68 - introduce multi time-level schemes.
69
70 \ingroup findiff
71 */
72 template <class Operator>
74 public:
75 // typedefs
79 typedef typename traits::bc_set bc_set;
81 // constructors
83 : L_(L), I_(operator_type::identity(L.size())), dt_(0.0), theta_(theta),
84 bcs_(std::move(bcs)) {}
85 void step(array_type& a,
86 Time t);
87 void setStep(Time dt) {
88 dt_ = dt;
89 if (theta_!=1.0) // there is an explicit part
90 explicitPart_ = I_-((1.0-theta_) * dt_)*L_;
91 if (theta_!=0.0) // there is an implicit part
93 }
94 protected:
99 };
100
101
102 // inline definitions
103
104 template <class Operator>
106 Size i;
107 for (i=0; i<bcs_.size(); i++)
108 bcs_[i]->setTime(t);
109 if (theta_!=1.0) { // there is an explicit part
110 if (L_.isTimeDependent()) {
111 L_.setTime(t);
112 explicitPart_ = I_-((1.0-theta_) * dt_)*L_;
113 }
114 for (i=0; i<bcs_.size(); i++)
115 bcs_[i]->applyBeforeApplying(explicitPart_);
116 a = explicitPart_.applyTo(a);
117 for (i=0; i<bcs_.size(); i++)
118 bcs_[i]->applyAfterApplying(a);
119 }
120 if (theta_!=0.0) { // there is an implicit part
121 if (L_.isTimeDependent()) {
122 L_.setTime(t-dt_);
123 implicitPart_ = I_+(theta_ * dt_)*L_;
124 }
125 for (i=0; i<bcs_.size(); i++)
126 bcs_[i]->applyBeforeSolving(implicitPart_,a);
127 implicitPart_.solveFor(a, a);
128 for (i=0; i<bcs_.size(); i++)
129 bcs_[i]->applyAfterSolving(a);
130 }
131 }
132
133}
134
135
136#endif
Mixed (explicit/implicit) scheme for finite difference methods.
Definition: mixedscheme.hpp:73
traits::operator_type operator_type
Definition: mixedscheme.hpp:77
operator_type explicitPart_
Definition: mixedscheme.hpp:95
operator_type implicitPart_
Definition: mixedscheme.hpp:95
void setStep(Time dt)
Definition: mixedscheme.hpp:87
traits::bc_set bc_set
Definition: mixedscheme.hpp:79
traits::condition_type condition_type
Definition: mixedscheme.hpp:80
OperatorTraits< Operator > traits
Definition: mixedscheme.hpp:76
traits::array_type array_type
Definition: mixedscheme.hpp:78
void step(array_type &a, Time t)
MixedScheme(const operator_type &L, Real theta, bc_set bcs)
Definition: mixedscheme.hpp:82
std::vector< ext::shared_ptr< bc_type > > bc_set
Operator::array_type array_type
condition to be applied at every time step
const DefaultType & t
generic finite difference model
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Real theta
Definition: any.hpp:35
STL namespace.