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 /*! \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:
FdmLinearOpLayout(const std::vector<Size> & dim)36         explicit FdmLinearOpLayout(const std::vector<Size>& dim)
37         : dim_(dim), spacing_(dim.size()) {
38             spacing_[0] = 1;
39             std::partial_sum(dim.begin(), dim.end()-1,
40                 spacing_.begin()+1, std::multiplies<Size>());
41 
42             size_ = spacing_.back()*dim.back();
43         }
44 
begin() const45         FdmLinearOpIterator begin() const {
46             return FdmLinearOpIterator(dim_);
47         }
48 
end() const49         FdmLinearOpIterator end() const {
50             return FdmLinearOpIterator(size_);
51         }
52 
dim() const53         const std::vector<Size>& dim() const {
54             return dim_;
55         }
56 
spacing() const57         const std::vector<Size>& spacing() const {
58             return spacing_;
59         }
60 
size() const61         Size size() const {
62             return size_;
63         }
64 
index(const std::vector<Size> & coordinates) const65         Size index(const std::vector<Size>& coordinates) const {
66             return std::inner_product(coordinates.begin(),
67                                       coordinates.end(),
68                                       spacing_.begin(), Size(0));
69         }
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         Disposable<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
89