/src/quantlib/ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp
Line | Count | Source |
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 | | <https://www.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 | | /*! \file fdmlinearoplayout.hpp |
23 | | \brief memory layout of a fdm linear operator |
24 | | */ |
25 | | |
26 | | #ifndef quantlib_linear_op_layout_hpp |
27 | | #define quantlib_linear_op_layout_hpp |
28 | | |
29 | | #include <ql/methods/finitedifferences/operators/fdmlinearopiterator.hpp> |
30 | | #include <functional> |
31 | | |
32 | | namespace QuantLib { |
33 | | |
34 | | class FdmLinearOpLayout { |
35 | | public: |
36 | | explicit FdmLinearOpLayout(std::vector<Size> dim) |
37 | 0 | : dim_(std::move(dim)), spacing_(dim_.size()) { |
38 | 0 | spacing_[0] = 1; |
39 | 0 | std::partial_sum(dim_.begin(), dim_.end()-1, |
40 | 0 | spacing_.begin()+1, std::multiplies<>()); |
41 | |
|
42 | 0 | size_ = spacing_.back()*dim_.back(); |
43 | 0 | } |
44 | | |
45 | 0 | FdmLinearOpIterator begin() const { |
46 | 0 | return FdmLinearOpIterator(dim_); |
47 | 0 | } |
48 | | |
49 | 0 | FdmLinearOpIterator end() const { |
50 | 0 | return FdmLinearOpIterator(size_); |
51 | 0 | } |
52 | | |
53 | 0 | const std::vector<Size>& dim() const { |
54 | 0 | return dim_; |
55 | 0 | } |
56 | | |
57 | 0 | const std::vector<Size>& spacing() const { |
58 | 0 | return spacing_; |
59 | 0 | } |
60 | | |
61 | 0 | Size size() const { |
62 | 0 | return size_; |
63 | 0 | } |
64 | | |
65 | 0 | Size index(const std::vector<Size>& coordinates) const { |
66 | 0 | return std::inner_product(coordinates.begin(), |
67 | 0 | coordinates.end(), |
68 | 0 | spacing_.begin(), Size(0)); |
69 | 0 | } |
70 | | |
71 | | Size neighbourhood(const FdmLinearOpIterator& iterator, |
72 | | Size i, Integer offset) const; |
73 | | |
74 | | Size neighbourhood(const FdmLinearOpIterator& iterator, |
75 | | Size i1, Integer offset1, |
76 | | Size i2, Integer offset2) const; |
77 | | |
78 | | // smart but sometimes too slow |
79 | | FdmLinearOpIterator iter_neighbourhood( |
80 | | const FdmLinearOpIterator& iterator, Size i, Integer offset) const; |
81 | | |
82 | | private: |
83 | | Size size_; |
84 | | std::vector<Size> dim_, spacing_; |
85 | | }; |
86 | | } |
87 | | |
88 | | #endif |