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 <http://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
31namespace QuantLib {
32
33 //! transformed grid
34 /*! This package encapuslates an array of grid points. It is used primarily
35 in PDE calculations.
36 */
37 class TransformedGrid {
38 public:
39 TransformedGrid (const Array &grid) :
40 grid_(grid), transformedGrid_(grid),
41 dxm_(grid.size()), dxp_(grid.size()),
42 dx_(grid.size()){
43 for (Size i=1; i < transformedGrid_.size() -1 ; i++) {
44 dxm_[i] = transformedGrid_[i] - transformedGrid_[i-1];
45 dxp_[i] = transformedGrid_[i+1] - transformedGrid_[i];
46 dx_[i] = dxm_[i] + dxp_[i];
47 }
48 }
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 grid_(grid), transformedGrid_(grid.size()),
58 dxm_(grid.size()), dxp_(grid.size()),
59 dx_(grid.size()){
60 std::transform(grid_.begin(),
61 grid_.end(),
62 transformedGrid_.begin(),
63 func);
64 for (Size i=1; i < transformedGrid_.size() -1 ; i++) {
65 dxm_[i] = transformedGrid_[i] - transformedGrid_[i-1];
66 dxp_[i] = transformedGrid_[i+1] - transformedGrid_[i];
67 dx_[i] = dxm_[i] + dxp_[i];
68 }
69 }
70
71#if defined(__GNUC__) && (__GNUC__ >= 7)
72#pragma GCC diagnostic pop
73#endif
74
75 const Array &gridArray() const { return grid_;}
76 const Array &transformedGridArray() const { return transformedGrid_;}
77 const Array &dxmArray() const { return dxm_;}
78 const Array &dxpArray() const { return dxp_;}
79 const Array &dxArray() const { return dx_;}
80
81 Real grid(Size i) const { return grid_[i];}
82 Real transformedGrid(Size i) const { return transformedGrid_[i];}
83 Real dxm(Size i) const { return dxm_[i];}
84 Real dxp(Size i) const { return dxp_[i];}
85 Real dx(Size i) const { return dx_[i];}
86 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 class LogGrid : public TransformedGrid {
97 public:
98 LogGrid(const Array &grid) :
99 TransformedGrid(grid, [](Real x) -> Real { return std::log(x: x); }){};
100 const Array & logGridArray() const { return transformedGridArray();}
101 Real logGrid(Size i) const { return transformedGrid(i);}
102 };
103
104}
105
106
107#endif
108

source code of quantlib/ql/math/transformedgrid.hpp