Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/methods/finitedifferences/operators/fdmcevop.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) 2018 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
/*! \file fdmcevop.cpp */
21
22
#include <ql/termstructures/yieldtermstructure.hpp>
23
#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
24
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
25
#include <ql/methods/finitedifferences/operators/fdmcevop.hpp>
26
#include <ql/methods/finitedifferences/operators/firstderivativeop.hpp>
27
#include <ql/methods/finitedifferences/operators/secondderivativeop.hpp>
28
29
30
namespace QuantLib {
31
32
    FdmCEVOp::FdmCEVOp(
33
        const ext::shared_ptr<FdmMesher>& mesher,
34
        const ext::shared_ptr<YieldTermStructure>& rTS,
35
        Real f0, Real alpha, Real beta,
36
        Size direction)
37
0
    : rTS_(rTS),
38
0
      direction_(direction),
39
0
      dxxMap_(SecondDerivativeOp(0, mesher)
40
0
              .mult(0.5 * alpha * alpha *
41
0
                    Pow(mesher->locations(direction), 2.0 * beta))),
42
0
      mapT_(direction, mesher) {
43
0
    }
44
45
0
    Size FdmCEVOp::size() const { return 1U; }
46
47
0
    void FdmCEVOp::setTime(Time t1, Time t2) {
48
0
        const Rate r = rTS_->forwardRate(t1, t2, Continuous).rate();
49
0
        mapT_.axpyb(Array(), dxxMap_, dxxMap_, Array(1, -r));
50
0
    }
51
52
0
    Array FdmCEVOp::apply(const Array& r) const {
53
0
        return mapT_.apply(r);
54
0
    }
55
56
0
    Array FdmCEVOp::apply_mixed(const Array& r) const {
57
0
        return Array(r.size(), 0.0);
58
0
    }
59
60
0
    Array FdmCEVOp::apply_direction(Size direction, const Array& r) const {
61
0
        if (direction == direction_) {
62
0
            return mapT_.apply(r);
63
0
        }
64
0
        else {
65
0
            return Array(r.size(), 0.0);
66
0
        }
67
0
    }
68
69
0
    Array FdmCEVOp::solve_splitting(Size direction, const Array& r, Real a) const {
70
0
        if (direction == direction_) {
71
0
            return mapT_.solve_splitting(r, a, 1.0);
72
0
        }
73
0
        else {
74
0
            return Array(r.size(), 0.0);
75
0
        }
76
0
    }
77
78
0
    Array FdmCEVOp::preconditioner(const Array& r, Real dt) const {
79
0
        return solve_splitting(direction_, r, dt);
80
0
    }
81
82
0
    std::vector<SparseMatrix> FdmCEVOp::toMatrixDecomp() const {
83
0
        return std::vector<SparseMatrix>(1, mapT_.toMatrix());
84
0
    }
85
86
}
87