Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/methods/finitedifferences/schemes/craigsneydscheme.cpp
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) 2008 Andreas Gaida
5
 Copyright (C) 2008 Ralph Schreyer
6
 Copyright (C) 2008 Klaus Spanderen
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <http://quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
#include <ql/methods/finitedifferences/schemes/craigsneydscheme.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    CraigSneydScheme::CraigSneydScheme(Real theta,
28
                                       Real mu,
29
                                       ext::shared_ptr<FdmLinearOpComposite> map,
30
                                       const bc_set& bcSet)
31
0
    : dt_(Null<Real>()), theta_(theta), mu_(mu), map_(std::move(map)), bcSet_(bcSet) {}
32
33
0
    void CraigSneydScheme::step(array_type& a, Time t) {
34
0
        QL_REQUIRE(t-dt_ > -1e-8, "a step towards negative time given");
35
36
0
        map_->setTime(std::max(0.0, t-dt_), t);
37
0
        bcSet_.setTime(std::max(0.0, t-dt_));
38
39
0
        bcSet_.applyBeforeApplying(*map_);
40
0
        Array y = a + dt_*map_->apply(a);
41
0
        bcSet_.applyAfterApplying(y);
42
43
0
        Array y0 = y;
44
45
0
        for (Size i=0; i < map_->size(); ++i) {
46
0
            Array rhs = y - theta_*dt_*map_->apply_direction(i, a);
47
0
            y = map_->solve_splitting(i, rhs, -theta_*dt_);
48
0
        }
49
50
0
        bcSet_.applyBeforeApplying(*map_);
51
0
        Array yt = y0 + mu_*dt_*map_->apply_mixed(y-a);
52
0
        bcSet_.applyAfterApplying(yt);
53
54
0
        for (Size i=0; i < map_->size(); ++i) {
55
0
            Array rhs = yt - theta_*dt_*map_->apply_direction(i, a);
56
0
            yt = map_->solve_splitting(i, rhs, -theta_*dt_);
57
0
        }
58
0
        bcSet_.applyAfterSolving(yt);
59
60
0
        a = yt;
61
0
    }
62
63
0
    void CraigSneydScheme::setStep(Time dt) {
64
0
        dt_=dt;
65
0
    }
66
}