/src/quantlib/ql/methods/finitedifferences/schemes/modifiedcraigsneydscheme.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2009 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 | | <https://www.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 | | #include <ql/methods/finitedifferences/schemes/modifiedcraigsneydscheme.hpp> |
21 | | #include <utility> |
22 | | |
23 | | namespace QuantLib { |
24 | | |
25 | | ModifiedCraigSneydScheme::ModifiedCraigSneydScheme(Real theta, |
26 | | Real mu, |
27 | | ext::shared_ptr<FdmLinearOpComposite> map, |
28 | | const bc_set& bcSet) |
29 | 0 | : dt_(Null<Real>()), theta_(theta), mu_(mu), map_(std::move(map)), bcSet_(bcSet) {} |
30 | | |
31 | 0 | void ModifiedCraigSneydScheme::step(array_type& a, Time t) { |
32 | 0 | QL_REQUIRE(t-dt_ > -1e-8, "a step towards negative time given"); |
33 | 0 | map_->setTime(std::max(0.0, t-dt_), t); |
34 | 0 | bcSet_.setTime(std::max(0.0, t-dt_)); |
35 | |
|
36 | 0 | bcSet_.applyBeforeApplying(*map_); |
37 | 0 | Array y = a + dt_*map_->apply(a); |
38 | 0 | bcSet_.applyAfterApplying(y); |
39 | |
|
40 | 0 | Array y0 = y; |
41 | |
|
42 | 0 | for (Size i=0; i < map_->size(); ++i) { |
43 | 0 | Array rhs = y - theta_*dt_*map_->apply_direction(i, a); |
44 | 0 | y = map_->solve_splitting(i, rhs, -theta_*dt_); |
45 | 0 | } |
46 | |
|
47 | 0 | bcSet_.applyBeforeApplying(*map_); |
48 | 0 | Array yt = y0 + mu_*dt_*map_->apply_mixed(y-a) |
49 | 0 | +(0.5-mu_)*dt_*map_->apply(y-a);; |
50 | 0 | bcSet_.applyAfterApplying(yt); |
51 | |
|
52 | 0 | for (Size i=0; i < map_->size(); ++i) { |
53 | 0 | Array rhs = yt - theta_*dt_*map_->apply_direction(i, a); |
54 | 0 | yt = map_->solve_splitting(i, rhs, -theta_*dt_); |
55 | 0 | } |
56 | 0 | bcSet_.applyAfterSolving(yt); |
57 | |
|
58 | 0 | a = yt; |
59 | 0 | } |
60 | | |
61 | 0 | void ModifiedCraigSneydScheme::setStep(Time dt) { |
62 | 0 | dt_=dt; |
63 | 0 | } |
64 | | } |