/src/quantlib/ql/methods/finitedifferences/operators/fdmlinearoplayout.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) 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 | | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | Size FdmLinearOpLayout::neighbourhood(const FdmLinearOpIterator& iterator, |
27 | 0 | Size i, Integer offset) const { |
28 | 0 | Size myIndex = iterator.index() |
29 | 0 | - iterator.coordinates()[i]*spacing_[i]; |
30 | |
|
31 | 0 | Integer coorOffset = Integer(iterator.coordinates()[i])+offset; |
32 | 0 | if (coorOffset < 0) { |
33 | 0 | coorOffset=-coorOffset; |
34 | 0 | } |
35 | 0 | else if (Size(coorOffset) >= dim_[i]) { |
36 | 0 | coorOffset = 2*(dim_[i]-1) - coorOffset; |
37 | 0 | } |
38 | 0 | return myIndex + coorOffset*spacing_[i]; |
39 | 0 | } |
40 | | |
41 | | Size FdmLinearOpLayout::neighbourhood(const FdmLinearOpIterator& iterator, |
42 | | Size i1, Integer offset1, |
43 | 0 | Size i2, Integer offset2) const { |
44 | |
|
45 | 0 | Size myIndex = iterator.index() |
46 | 0 | - iterator.coordinates()[i1]*spacing_[i1] |
47 | 0 | - iterator.coordinates()[i2]*spacing_[i2]; |
48 | |
|
49 | 0 | Integer coorOffset1 = Integer(iterator.coordinates()[i1])+offset1; |
50 | 0 | if (coorOffset1 < 0) { |
51 | 0 | coorOffset1=-coorOffset1; |
52 | 0 | } |
53 | 0 | else if (Size(coorOffset1) >= dim_[i1]) { |
54 | 0 | coorOffset1 = 2*(dim_[i1]-1) - coorOffset1; |
55 | 0 | } |
56 | |
|
57 | 0 | Integer coorOffset2 = Integer(iterator.coordinates()[i2])+offset2; |
58 | 0 | if (coorOffset2 < 0) { |
59 | 0 | coorOffset2=-coorOffset2; |
60 | 0 | } |
61 | 0 | else if (Size(coorOffset2) >= dim_[i2]) { |
62 | 0 | coorOffset2 = 2*(dim_[i2]-1) - coorOffset2; |
63 | 0 | } |
64 | |
|
65 | 0 | return myIndex + coorOffset1*spacing_[i1]+coorOffset2*spacing_[i2]; |
66 | 0 | } |
67 | | |
68 | | // smart but sometimes too slow |
69 | | FdmLinearOpIterator FdmLinearOpLayout::iter_neighbourhood( |
70 | 0 | const FdmLinearOpIterator& iterator, Size i, Integer offset) const { |
71 | |
|
72 | 0 | std::vector<Size> coordinates = iterator.coordinates(); |
73 | |
|
74 | 0 | Integer coorOffset = Integer(coordinates[i])+offset; |
75 | 0 | if (coorOffset < 0) { |
76 | 0 | coorOffset=-coorOffset; |
77 | 0 | } |
78 | 0 | else if (Size(coorOffset) >= dim_[i]) { |
79 | 0 | coorOffset = 2*(dim_[i]-1) - coorOffset; |
80 | 0 | } |
81 | 0 | coordinates[i] = Size(coorOffset); |
82 | |
|
83 | 0 | return FdmLinearOpIterator(dim_, coordinates, |
84 | 0 | index(coordinates)); |
85 | 0 | } |
86 | | |
87 | | } |