Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/math/transformedgrid.hpp
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) 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
 <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 transformedgrid.hpp
21
    \brief encapuslates a grid
22
*/
23
24
#ifndef quantlib_transformed_grid_hpp
25
#define quantlib_transformed_grid_hpp
26
27
#include <ql/math/array.hpp>
28
#include <functional>
29
#include <numeric>
30
31
namespace QuantLib {
32
33
    /*! \deprecated Part of the old FD framework; copy this function
34
                    in your codebase if needed.
35
                    Deprecated in version 1.37.
36
    */
37
    class [[deprecated("Part of the old FD framework; copy this function in your codebase if needed")]] TransformedGrid {
38
    public:
39
        TransformedGrid (const Array &grid) :
40
            grid_(grid), transformedGrid_(grid),
41
            dxm_(grid.size()), dxp_(grid.size()),
42
0
            dx_(grid.size()){
43
0
            for (Size i=1; i < transformedGrid_.size() -1 ; i++) {
44
0
                dxm_[i] = transformedGrid_[i] - transformedGrid_[i-1];
45
0
                dxp_[i] = transformedGrid_[i+1] - transformedGrid_[i];
46
0
                dx_[i] = dxm_[i] + dxp_[i];
47
0
            }
48
0
        }
49
50
#if defined(__GNUC__) && (__GNUC__ >= 7)
51
#pragma GCC diagnostic push
52
#pragma GCC diagnostic ignored "-Wnoexcept-type"
53
#endif
54
55
        template <class T>
56
        TransformedGrid (const Array &grid, T func) :
57
0
            grid_(grid), transformedGrid_(grid.size()),
58
0
            dxm_(grid.size()), dxp_(grid.size()),
59
0
            dx_(grid.size()){
60
0
            std::transform(grid_.begin(),
61
0
                           grid_.end(),
62
0
                           transformedGrid_.begin(),
63
0
                           func);
64
0
            for (Size i=1; i < transformedGrid_.size() -1 ; i++) {
65
0
                dxm_[i] = transformedGrid_[i] - transformedGrid_[i-1];
66
0
                dxp_[i] = transformedGrid_[i+1] - transformedGrid_[i];
67
0
                dx_[i] = dxm_[i] + dxp_[i];
68
0
            }
69
0
        }
70
71
#if defined(__GNUC__) && (__GNUC__ >= 7)
72
#pragma GCC diagnostic pop
73
#endif
74
75
0
        const Array &gridArray() const { return grid_;}
76
0
        const Array &transformedGridArray() const { return transformedGrid_;}
77
0
        const Array &dxmArray() const { return dxm_;}
78
0
        const Array &dxpArray() const { return dxp_;}
79
0
        const Array &dxArray() const { return dx_;}
80
81
0
        Real grid(Size i) const { return grid_[i];}
82
0
        Real transformedGrid(Size i) const { return transformedGrid_[i];}
83
0
        Real dxm(Size i) const { return dxm_[i];}
84
0
        Real dxp(Size i) const { return dxp_[i];}
85
0
        Real dx(Size i) const { return dx_[i];}
86
0
        Size size() const {return grid_.size();}
87
88
    protected:
89
        Array grid_;
90
        Array transformedGrid_;
91
        Array dxm_;
92
        Array dxp_;
93
        Array dx_;
94
    };
95
96
97
    QL_DEPRECATED_DISABLE_WARNING
98
99
    /*! \deprecated Part of the old FD framework; copy this function
100
                    in your codebase if needed.
101
                    Deprecated in version 1.37.
102
    */
103
    class [[deprecated("Part of the old FD framework; copy this function in your codebase if needed")]] LogGrid : public TransformedGrid {
104
    public:
105
        LogGrid(const Array &grid) :
106
0
            TransformedGrid(grid, [](Real x) -> Real { return std::log(x); }){};
107
0
        const Array & logGridArray() const { return transformedGridArray();}
108
0
        Real logGrid(Size i) const { return transformedGrid(i);}
109
    };
110
111
    QL_DEPRECATED_ENABLE_WARNING
112
113
}
114
115
116
#endif